openwrt

2022-10-18 宋洋葱 宋洋葱

前言

环境:

工具介绍:

  • shadowsocks-libev
    • ss-redir ,代理 tcp 流量(开启 udp_relay)
    • ss-tunnel ,转发 dns 解析请求
  • ChinaDNS,同时配置国内和国外 dns,对比后返回正确 ip,代替国内 DNS。
  • DNS-Forwarder,缓存 dns 结果 ,提高效率。

使用命令行配置比较麻烦可以配合 LuCI 的界面工具使用。

下载安装包:

  • shadowsocks 可以通过 opkg 安装,参考下方
  • 在 ChinaDNS 的 releases 界面下载 ChinaDNS_mipsel_24kc.ipk
  • 在 luci 的 releases界面下载 luci-app-chinadns、luci-app-dns-forwarde。
  • 把下载的 ipk 文件通过路由器后台依次上传安装。
  • 安装完可能需要退出后重新登录。

安装 ss

  1. 准备环境
#更改软件源
cp /etc/opkg/distfeeds.conf /etc/opkg/distfeeds.conf.bak
sed -i 's_downloads.openwrt.org_mirrors.tuna.tsinghua.edu.cn/openwrt_' /etc/opkg/distfeeds.conf
opkg update

opkg install luci-i18n-base-zh-cn luci-app-opkg luci-i18n-opkg-zh-cn
  1. 安装
# 查看  CPU 架构
opkg print-architecture
# arch mipsel_24kc 10
# 安装 ss
opkg install shadowsocks-libev-ss-local shadowsocks-libev-ss-redir shadowsocks-libev-ss-rules shadowsocks-libev-ss-tunnel
# 安装 ui
opkg install luci luci-base luci-compat luci-app-shadowsocks-libev luci-i18n-shadowsocks-libev-zh-cn
  1. 配置 先配置远端服务器 开启 ss_redir

安装 kcptun

opkg install kcptun-client

配置 dns-forwarder

  1. 勾选启用
  2. 如果提示「DNS 转发 - 未运行」可以在路由器的 shell 环境执行
/etc/init.d/dns-forwarder status
/etc/init.d/dns-forwarder start
# 开机自启动
# /etc/init.d/dns-forwarder enable 

配置 chinadns

  1. 勾选启用
  2. 上游服务器改成 114.114.114.114,127.0.0.1:5300
  3. 如果提示「ChinaDNS - 未运行」,可以在路由器的 shell 环境执行
/etc/init.d/chinadns status
/etc/init.d/chinadns start
 # 开机自启动
 # /etc/init.d/chinadns enable 
 
  1. 如果还是启动不起来,可以手动运行调试
chinadns -b 0.0.0.0 -p 5353 -s 114.114.114.114,127.0.0.1:5300 -c /etc/chinadns_chnroute.txt -v

# -b 本地监听ip
# -p 本地监听port
# -s 指定上游dns服务器,必须至少有一个国内dns、一个国外dns
# -c 指定大陆地址段文件
# -l 指定ip黑名单文件
# -m 启用dns压缩指针
# -v 打印详细信息

配置 dns 服务器

  1. 路由器后台点击 网络 -> dns
  2. 本地服务器: 127.0.0.1#5353
  3. 测试结果如下
nslookup www.google.com
Server:		127.0.0.1
Address:	127.0.0.1:53

Non-authoritative answer:
Name:	www.google.com
Address: 128.121.243.77

Non-authoritative answer:
Name:	www.google.com
Address: 2001::a88f:a234

# nslookup www.baidu.com

转发规则

  1. 点击 服务 -> shadowsocks -> 转发规则
  2. 常用设置,取消勾选禁用
  3. 目的地地址,绕过:
# ss 远端服务器 ip
0.0.0.0/8
127.0.0.0/8
192.168.0.0/16
  1. 目的未匹配时默认行为:forward
  2. 重启路由器,测试如下:
root@OpenWrt:~# nslookup www.google.com
Server:		127.0.0.1
Address:	127.0.0.1:53

Non-authoritative answer:
Name:	www.google.com
Address: 142.250.217.132

Non-authoritative answer:
Name:	www.google.com
Address: 2001::a88f:a234
  1. 浏览器打开 google.com 正常,打开百度搜索 id,发现是国外的 ip。证明没有做分流。也就是除了转发规则里「绕过」的 ip 外,其他都会走代理。我们需要做到国内 ip 不走代理。

分流

仅国外域名走代理,使用 gwlist。

  1. 安装依赖和下载 gwlist
# 查看 dnsmasq 是否支持 ipset,Compile time options 默认是  no-ipset 
dnsmasq -v |grep "^ipset"
# 不支持的话需要安装 dnsmasq-full
opkg update
# 安装与卸载应该同步进行, 否则卸载完就无法安装了
opkg remove dnsmasq && opkg install dnsmasq-full --force-overwrite
opkg install ipset iptables-mod-nat-extra

mkdir /etc/dnsmasq.d
# 在 dnsmasq.d 的 配置项会自动合并到 /var/etc/dnsmasq.conf.cfg01411c
echo 'conf-dir=/etc/dnsmasq.d' >> /etc/dnsmasq.conf
cd /etc/dnsmasq.d
wget https://cokebar.github.io/gfwlist2dnsmasq/dnsmasq_gfwlist_ipset.conf
# curl -O https://cokebar.github.io/gfwlist2dnsmasq/dnsmasq_gfwlist_ipset.conf
dnsmasq --test
# dnsmasq: syntax check OK.
# 不支持 ipset 的话会提示需要重新编译 dnsmasq: recompile with HAVE_IPSET defined to enable ipset directives at line 5 of
  1. 配置 ipset 和 iptables
# 路由器重启后需要重新配置
# 创建名为gfwlist,格式为iphash的集合
ipset -L
iptables -t nat -L
# 没有 iptables 的话需要安装
opkg install iptables
ipset -N gfwlist iphash
# 匹配 gfwlist 中 ip 的 nat 流量均被转发到 shadowsocks 端口
iptables -t nat -A PREROUTING -p tcp -m set --match-set gfwlist dst -j REDIRECT --to-port 1100
# 匹配 gfwlist 中 ip 的本机流量均被转发到 shadowsocks 端口
iptables -t nat -A OUTPUT -p tcp -m set --match-set gfwlist dst -j REDIRECT --to-port 1100
# 查看是否配置正确
iptables -t nat -L
  1. 点击 服务 -> shadowsocks -> 转发规则
  2. 目的地址设定 -> 目的未匹配时默认行为:bypass
  3. 重启防火墙
# 检测 dnsmasq 是否配置正确
dnsmasq --test
# 重启防火墙
/etc/init.d/dnsmasq restart
# udhcpc: started, v1.35.0
# udhcpc: broadcasting discover
# udhcpc: no lease, failing

# 测试 dns 国内域名
nslookup www.baidu.com

# 测试 dns 国外域名
nslookup www.google.com
# Server:		127.0.0.1
# Address:	127.0.0.1:53
# Non-authoritative answer:
# Name:	www.google.com
# Address: x.x.x.x
# Non-authoritative answer:
# Name:	www.google.com
# Address: 2001::68f4:2bf8


# 查看是否有 ip 地址
ipset -L
# 数量大雨 0 就行
# Number of entries: 17
  1. 上网检测
    1. 百度搜索 ip,ip 地址正常(国内)。
    2. 访问谷歌,正常访问,搜索 my ip 提示是 vps 的 ip。

备份设置


问题解决

  1. dns 启动失败
root@OpenWrt:~# nslookup www.baidu.com
nslookup: write to '127.0.0.1': Connection refused
nslookup: write to '::1': Connection refused
;; connection timed out; no servers could be reached

lsof -i:53
# 无任何输出,端口没有被程序监控,应该是 dnsmasq 没有起来
/etc/init.d/dnsmasq status
# 提示 running

# 有可能是 dnsmasq 启动较慢,等几分钟,或者重启路由器。
# 也可以手动启动排查
dnsmasq -C /etc/dnsmasq.conf --log-debug 
  1. 路由器断电重启后,可以上网但无法访问 openwrt 后台 提示为:192.168.1.1 拒绝了我们的连接请求。
lsof -i:53  # dnsmasq,正常
lsof -i:5353 # chinadns,正常
lsof -i:5300 # dns-forwad,正常
lsof -i:1100 # ss-redir,无
ipset -L  # 无
iptables -t nat -L  # 无


nslookup www.baidu.com
/etc/init.d/dns-forwarder status
/etc/init.d/dns-forwarder start
/etc/init.d/chinadns status
/etc/init.d/chinadns start
nslookup www.baidu.com
dnsmasq
/etc/init.d/dnsmasq enable
/etc/init.d/dnsmasq status
nslookup www.baidu.com

参考文档