这是一种基于域名的 VPN 智能翻越方案。不同于 chnroutes 这类通过维护一个 IP 地址列表来区分国内外网站的方案,基于域名的方式不受 IP 地址变动的影响。仅需维护一个相对很少有变化的域名列表即可。
dnsmasq 在 2.66 版之后加入了对 ipset 的支持,可将指定域名的 IP 解析后自动加入某一 ipset 中。 再配置路由规制,使该 ipset 中的 IP 走 VPN 即可。
感谢 @wzyboy 提供了此方案的思路。
大致流程opkg update
opkg install ip ipset kmod-ipt-ipset dnsmasq-full
列举一下涉及的各部分配置文件,供参考。
VPN各类 VPN 均可,请参考 VPN overview。注意请勿将 VPN 设为默认路由。
rt_tables添加outwall路由表。 走 VPN 的流量都将使用此表。
echo "200 outwall" >> /etc/iproute2/rt_tables
使系统启动时创建一个名为outwall的 ipset。
在 /etc/rc.local 中添加:
# vi /etc/rc.local
ipset create outwall hash:ip
名字可以随便取,与 dnsmasq.conf 和 firewall.user 中的保持一致即可。
firewall.user将匹配 ipset outwall的包全部标上 mark 8。
在 /etc/firewall.user 中添加:
# vi /etc/firewall.user
iptables -t mangle -A fwmark -m set --match-set outwall dst -j MARK --set-mark 8
打上 mark 以后,就可以指定它们所使用的路由表了。
mark 值可随便选,保持一致即可。若同时装有 qos-scripts,mark 可选一个较大的值,以防与其发生冲突。
确保以下脚本在每次 VPN 连接建立后执行。
不同 VPN 的配置方法可能不同。 一个比较通用的方法是使用 Hotplug。
(我这次用的是 vpnc,所以比较偷懒地将脚本放在了/etc/vpnc/post-connect.d/下)
#!/bin/sh
ip route add 8.8.8.8 dev $TUNDEV
ip route add default dev $TUNDEV table outwall
ip rule add fwmark 8 table outwall
注意,须将$TUNDEV替换为 VPN 设备名,比如ppp0。
这段脚本有三个作用:
见上节说明,须在 VPN 断线时 自动执行的脚本:
#!/bin/sh
ip rule del table outwall
请参考 dnsmasq man(8)。
修改 /etc/dnsmasq.conf,在其中加入需要翻越的域名。 格式如下:
server=/域名/8.8.8.8
ipset=/域名/outwall
一个可供参考的配置如下, 该配置:
server=/google.com/8.8.8.8
server=/googleusercontent.com/8.8.8.8
server=/gstatic.com/8.8.8.8
server=/googlehosted.com/8.8.8.8
server=/golang.org/8.8.8.8
server=/googleapis.com/8.8.8.8
ipset=/google.com/outwall
ipset=/googleusercontent.com/outwall
ipset=/gstatic.com/outwall
ipset=/googlehosted.com/outwall
ipset=/golang.org/outwall
ipset=/googleapis.com/outwall
server=/twitter.com/8.8.8.8
server=/twimg.com/8.8.8.8
server=/t.co/8.8.8.8
ipset=/twitter.com/outwall
ipset=/twimg.com/outwall
ipset=/t.co/outwall
server=/facebook.com/8.8.8.8
ipset=/facebook.com/outwall
server=/youtube.com/8.8.8.8
server=/ytimg.com/8.8.8.8
server=/ggpht.com/8.8.8.8
server=/youtu.be/8.8.8.8
server=/googlevideo.com/8.8.8.8
server=/youtube-nocookie.com/8.8.8.8
ipset=/youtube.com/outwall
ipset=/ytimg.com/outwall
ipset=/ggpht.com/outwall
ipset=/youtu.be/outwall
ipset=/googlevideo.com/outwall
ipset=/youtube-nocookie.com/outwall
server=/githubusercontent.com/8.8.8.8
server=/github.global.ssl.fastly.net/8.8.8.8
server=/githubapp.com/8.8.8.8
ipset=/githubusercontent.com/outwall
ipset=/github.global.ssl.fastly.net/outwall
ipset=/githubapp.com/outwall
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |