全局模式
GFWList
准备
在开始以下步骤之前,你需要确保透明代理网关上已经做好以下准备
- 安装好
ipset
和dnsmasq
- 无污染的DNS代理(端口不要和dnsmasq需要用到的53端口冲突)
shadowsocks-libev
已安装并且ss-redir
已配置
开始
配置NAT规则
gfwlist模式需要记录一份IP列表,以便把这个列表中的IP都转向到SS,创建ipset并配置NAT:
ipset -N gfwlist iphash
iptables -t nat -A PREROUTING -p tcp -m set --match-set gfwlist dst -j REDIRECT --to-port 1234
其中--to-port
指向到ss-redir配置的端口
如果想每次开机启动都自动生效可以把上面两行加到/etc/rc.local
里
如果是不同机,网关使用DNAT转发到代理机上指定端口
iptables -t nat -A PREROUTING -p tcp -m set --match-set gfwlist dst -j DNAT --to 192.168.10.10:1235
代理机上将指定端口REDIRECT到ss-redir的端口
iptables -t nat -A PREROUTING --proto tcp --dport 1235 -j REDIRECT --to-port 1234
其它操作
# 日志
sudo iptables -A INPUT -j LOG --log-prefix "curious_log " --log-level 6
# 查看NAT规则与删除
sudo iptables -t nat -L --line-numbers
sudo iptables -t nat -D PREROUTING 1
配置IP清单
为了批量且易于管理,可以使用dnsmasq向ipset里添加IP,此外dnsmasq可以进行本地的DNS管理。首先配置/etc/dnsmasq.conf
,添加conf-dir目录便于管理:
conf-dir=/etc/dnsmasq.d
这样在/etc/dnsmasq.d/
下面所有.conf
文件都会加载,为了和dnsmasq的其它配置隔离,可以新建一个gfwlist.conf
文件,添加需要NAT的域名,参考格式如下:
server=/remotewebsite.com/127.0.0.1#5353
ipset=/remotewebsite.com/gfwlist
其中5353
是本地DNS的代理端口,后面有详细描述。
github里有各种gfwlist转dnsmasq配置的工具(如cokebar/gfwlist2dnsmasq)。使用方式大同小异。
其它
dnsmasq的默认上级DNS使用的是/etc/resolv.conf
指定的DNS也就是主机网络的DNS,也可以改成指定的:
resolv-file=/etc/resolv.dnsmasq.conf
另外,需要排查问题时可以开启日志:
log-queries
log-facility=/var/log/dnsmasq.log
Chnroute
新建一个文件 /root/update_ignore_list
写入如下内容:
#!/bin/sh
set -e -o pipefail
wget -O- 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | \
awk -F\| '/CN\|ipv4/ { printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > \
/tmp/ignore.list
mv /tmp/ignore.list /etc/
if pidof ss-redir>/dev/null; then
/etc/init.d/shadowsocks rules
fi
使用 chmod +x /root/update_ignore_list
添加可执行权限
添加计划任务
Openwrt可以打开路由器管理页面 系统 - 计划任务 填写如下内容(每周日 04:30 执行):
30 4 * * 0 /root/update_ignore_list>/dev/null 2>&1