印象中,大家觉得linux系统中好像只有一张路由表,然而并不是,我们看到的只是main标识的路由表,如果有多路由表的需求,也可以使用其它路由表
介绍
- 官方文档:http://linux-ip.net/html/routing-tables.html
- ip route:http://linux-ip.net/html/tools-ip-route.html
- https://zhujinliang.com/iptables-fwmark-ce-lue-lu-you/
在文件/etc/iproute2/rt_tables
文件中包含所有的路由表标识:
cat /etc/iproute2/rt_tables
#
# reserved values
#
255 local
254 main
253 default
0 unspec
#
# local
#
#1 inr.ruhep
local为本地路由表,kernel自动生成和维护,路由表中包含的是本地接口路由和广播路由。
main路由表是主路由表,route命令默认就是管理它,由kernel自动生成和维护。
Routing Policy database - 路由策略数据库,控制kernel搜索多路由表的顺序,每一条rule规则都可以定义一个0到32767之间的优先级,数字越小,优先级越高。
ip rule show
0: from all lookup local
32766: from all lookup main
32767: from all lookup default
实操: 跨机器透明翻墙
# 添加路由表govpn
echo 10 govpn >> /etc/iproute2/rt_tables
# 查看路由表govpn
ip route show table govpn
# 添加默认路由到govpn
ip route add default via 192.168.223.100 dev switch0 table govpn
# 绑定fwmark 8走govpn路由表
ip rule add fwmark 8 table govpn
# 查看rule
# mangle表,如果目标地址匹配 ipset govpn中的规则,那么设置 mark为8,PREROUTING为转发的流量,OUTPUT为本机流量
iptables -t mangle -A PREROUTING -m set ! --match-set bypass dst -m set --match-set govpn dst -j MARK --set-mark 8
iptables -t mangle -A OUTPUT -m set ! --match-set bypass dst -m set --match-set govpn dst -j MARK --set-mark 8
ipset add cnips 10.0.0.0/8
ipset add cnips 172.16.0.0/12
ipset add cnips 192.168.0.0/16
ipset add cnips 127.0.0.1/32
ipset add cnips 169.254.0.0/16
ipset add cnips 43.154.6.6/32
ip route add default dev wg0 table govpn
iptables -t mangle -A PREROUTING -m set ! --match-set cnips dst -j MARK --set-mark 8
iptables -t mangle -A OUTPUT -m set ! --match-set cnips dst -j MARK --set-mark 8
附录待整理
随手记录点命令
iptables -t mangle -A PREROUTING -m set --match-set govpn dst -j MARK --set-mark 8
iptables -t mangle -A PREROUTING -m set ! --match-set bypass dst -m set --match-set govpn dst -j MARK --set-mark 8
iptables -t mangle -A OUTPUT -m set ! --match-set bypass dst -m set --match-set govpn dst -j MARK --set-mark 8
iptables -t nat -D PREROUTING -p tcp -m set ! --match-set bypass dst -m set --match-set govpn dst -j REDIRECT --to-ports 1081
iptables -t nat -D OUTPUT -p tcp -m set ! --match-set bypass dst -m set --match-set govpn dst -j REDIRECT --to-ports 1081
iptables -t mangle -D PREROUTING -m set ! --match-set bypass dst -m set --match-set govpn dst -j MARK --set-mark 8
iptables -t mangle -D OUTPUT -m set ! --match-set bypass dst -m set --match-set govpn dst -j MARK --set-mark 8
iptables -t mangle -A PREROUTING -m set ! --match-set bypass dst -m set --match-set govpn dst -j MARK --set-mark 8
iptables -t mangle -A OUTPUT -m set ! --match-set bypass dst -m set --match-set govpn dst -j MARK --set-mark 8
## 192.168.223.100
iptables -t nat -A PREROUTING -p tcp -m set ! --match-set bypass dst -j REDIRECT --to-ports 1081
iptables -t nat -A OUTPUT -p tcp -m set ! --match-set bypass dst -j REDIRECT --to-ports 1081
## 置顶优先跳转的ipset
iptables -t nat -I PREROUTING -p tcp -m set --match-set gotokeep dst -j REDIRECT --to-ports 1081
# 获取最新准确的中国网段,日常更新
wget https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt
# ipv4 中国
curl https://raw.githubusercontent.com/gaoyifan/china-operator-ip/ip-lists/china.txt 2> /dev/null | xargs -n1 ipset add bypass &> /dev/null
# ipv6 中国
curl https://raw.githubusercontent.com/gaoyifan/china-operator-ip/ip-lists/china6.txt 2> /dev/null | xargs -n1 ipset add bypass6 &> /dev/null
# 备用
## ubnt调整path-mtu
set firewall options mss-clamp6 interface-type pppoe
set firewall options mss-clamp6 mss 1420
## linux通用调整
# 自动MSS,假设PPPOE虚接口是pppoe0
iptables -t mangle -A POSTROUTING -p tcp --tcp-flags SYN,RST SYN -o pppoe0 -j TCPMSS --clamp-mss-to-pmtu
ip6tables -t mangle -A POSTROUTING -p tcp --tcp-flags SYN,RST SYN -o pppoe0 -j TCPMSS --clamp-mss-to-pmtu
# 手动指定MSS,假设PPPOE虚接口是pppoe0
$ iptables -t mangle -A POSTROUTING -p tcp --tcp-flags SYN,RST SYN -o pppoe0 -j TCPMSS --set-mss 1420
$ ip6tables -t mangle -A POSTROUTING -p tcp --tcp-flags SYN,RST SYN -o pppoe0 -j TCPMSS --set-mss 1420
腾讯云打通
apt update && \
apt install wireguard
# 获取数据文件
wget -c http://ftp.apnic.net/stats/apnic/delegated-apnic-latest
# 获取所有国内网段
cat delegated-apnic-latest | awk -F '|' '/CN/&&/ipv4/ {print $4 "/" 32-log($5)/log(2)}'
# 根据所有国内网段添加路由
awk -F '|' '/CN/&&/ipv4/ {print "ip route add " $4 "/" 32-log($5)/log(2) " via 10.222.3.1 dev eth0" }' delegated-apnic-latest|bash
# 做snat
iptables -t nat -A POSTROUTING -s 10.222.0.0/16 -j MASQUERADE
# 调整mtu
iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
# 重启wireguard
wg-quick down wg0 && wg-quick up wg0
问题
- ubnt上运行glider本身有性能问题,如果流量较大,或者加解密较频繁,都会导致cpu飙升,轻则缓慢,重则进程挂掉
- 白名单模式的维护麻烦,所以给glider增加了webapi,并且能够根据数据动态修改已经加载的数据结构
- 目前使用某vm作为公司vpn专属vm,比如ht,kp,sy,现在讲fq也加入该节点,针对需要bypass的ip,统一使用ipset来进行跳过,由于openvpn的服务端也在这上面,所以需要讲chnroutes中的网段全部加到bypass中
- ubnt使用多路由表+iptables的方式,将需fq的ip抛到专属vm上
- dns可以切回dnsmasq,然后使用glider来做dns抗污染解析
- 最终home,根据白名单fq;连接openvpn,国外IP全fq;连接ss,所有流量全fq
- sy的链路为主,kp的链路为辅
- socks5 over ws,nginx代理ws协议接入生产网络
- wireguard可翻墙,但udp会存在qos,所以需要转tcp封包
- 如果跨gfw使用openvpn,需要增加混淆层,参考https://github.com/wangyu-/udp2raw
- 腾讯云的网关cvm实例比较特殊,需要独立子网独立路由表
- 困扰已久的connection reset问题最终解决了,竟然是因为glider的两个不同的rule,cidr重合了!!fq偶发性reset应该也与这个有关
- 现在家中已经开启ipv6,但是走根据域名加白ip翻墙的那些依旧是ipv4,有空需要看一下如何支持
- ipv6 运营商竟然没有封 80 和 443,可以在此之上做些文章
- ipv6 每个设备都可以分配一个公网可以访问的地址,vpn组网就能网状就近通信了
- wireguard 主动连接 ddns 域名,如果ip切换了,wireguard 主动连接端会失效;需要外挂检测重连脚本 --> 现在可以通过 ipv6 直接连接 wireguard 服务端,ipv6 变更较慢
- ubnt 升级 2.x 后,dhcp更换了机制,导致原本两个dhcp server 冲突了!!!将192.168.223.0/24删了,就好了