原理:客户端请求至dnsmasq–>使用指定域名服务器解析请求域名并将请求打上gfwlist标签–>iptables将带gfwlist标签的流量转发至ss代理。
1.安装、配置shadowsocks
参见OpenWrt-dist,添加软件源。
添加Openwrt-disk的gpg key:
wget http://openwrt-dist.sourceforge.net/packages/openwrt-dist.pub
opkg-key add openwrt-dist.pub
编辑 /etc/opkg/customfeeds.conf
,添加软件源:
src/gz openwrt_dist http://openwrt-dist.sourceforge.net/packages/base/x86_64
src/gz openwrt_dist_luci http://openwrt-dist.sourceforge.net/packages/luci
请根据自己的CPU架构(可以执行 opkg print-architecture
查看),将 x86_64 替换成相应架构。
安装shadowsocks-libev:
opkg update
opkg remove dnsmasq && opkg install dnsmasq-full #替换默认dnsmasq,其不含ipset
opkg install ipset libpthread shadowsocks-libev
配置 /etc/shadowsocks.json
(TCP Fast Open 开启方法见下文),格式如下:
{
"server": "x.x.x.x",
"server_port": "your_server_port",
"password": "your_passwd",
"local_port": "1080",
"timeout": 300,
"method": "chacha20-ietf",
"fast_open": true
}
根据你的ss服务器,修改IP、端口、密码及加密方式。
创建文件 /etc/init.d/shadowsocks
:
#!/bin/sh /etc/rc.common
START=95
SERVICE_USE_PID=1
SERVICE_WRITE_PID=1
SERVICE_DAEMONIZE=1
CONFIG=/etc/shadowsocks.json
DNS=8.8.8.8:53
TUNNEL_PORT=5353
start() {
# Proxy Mode
service_start /usr/bin/ss-redir -c $CONFIG -b 0.0.0.0
# Tunnel
service_start /usr/bin/ss-tunnel -c $CONFIG -b 0.0.0.0 -u -l $TUNNEL_PORT -L $DNS
}
stop() {
# Proxy Mode
service_stop /usr/bin/ss-redir
# Tunnel
service_stop /usr/bin/ss-tunnel
}
在此用ss-tunnel转发UDP的DNS请求,防止污染DNS。如需修改上游DNS,请修改 DNS=8.8.8.8:53
字段,本地端口修改 TUNNEL_PORT=5353
。
给予执行权限,启动并设置自启:
chmod +x /etc/init.d/shadowsocks
/etc/init.d/shadowsocks enable
/etc/init.d/shadowsocks start
检查一下是否正常启动:
netstat -lnp | grep ss-redir
2.开启 TCP Fast Open
需要系统内核版本≥3.7,shadowsocks-libev≥3.0.4,shadowsocks服务端开启 TCP Fast Open:
echo "net.ipv4.tcp_fastopen = 3" >> /etc/sysctl.conf
sysctl -p
3.配置dnsmasp和ipset
将如下规则加入到“网络→防火墙→自定义规则”中(最后的1080是shadowsocks的本地端口,酌情修改):
ipset -N gfwlist iphash
iptables -t nat -A PREROUTING -p tcp -m set --match-set gfwlist dst -j REDIRECT --to-port 1080
iptables -t nat -A OUTPUT -p tcp -m set --match-set gfwlist dst -j REDIRECT --to-port 1080
修改dnsmasq配置:
echo "conf-dir=/etc/dnsmasq.d" >> /etc/dnsmasq.conf
添加gfwlist配置文件:
mkdir /etc/dnsmasq.d
wget --no-check-certificate https://cokebar.github.io/gfwlist2dnsmasq/dnsmasq_gfwlist_ipset.conf && mv dnsmasq_gfwlist_ipset.conf /etc/dnsmasq.d/
4.自动更新gfwlist
opkg update
opkg install libustream-mbedtls coreutils-base64 ca-certificates ca-bundle
wget https://raw.githubusercontent.com/cokebar/gfwlist2dnsmasq/master/gfwlist2dnsmasq.sh
crontab -e
0 1 * * * sh /root/gfwlist2dnsmasq.sh -p 5353 -s gfwlist -o /tmp/dnsmasq_gfwlist_ipset.conf \
&& cp /tmp/dnsmasq_gfwlist_ipset.conf /etc/dnsmasq.d/ \
&& /etc/init.d/dnsmasq restart
Reboot,across the Great Wall.
- 感谢cokebar大佬提供的教程。