gamexg
2016-11-28 09:58:33 +08:00
下面是通过绑定本地 ip 实现线路切换,不过也可以设置为通过用户 id 之类的分配线路。那种需要用 iptables 打标签来做。
计划使用的策略师程序绑定不同的本地地址就走不同的路由表走。
root@ubuntu-1u:/home/gamexg# echo 300 tel >>/etc/iproute2/rt_tables
root@ubuntu-1u:/home/gamexg# echo 400 cmcc >>/etc/iproute2/rt_tables
root@ubuntu-1u:/home/gamexg# ip route add default via 192.168.101.1 table cmcc
root@ubuntu-1u:/home/gamexg# ip route add default via 192.168.87.1 table tel
root@ubuntu-1u:/home/gamexg# ip rule add from 192.168.87.88/32 table tel pref 300
root@ubuntu-1u:/home/gamexg# ip rule add from 192.168.101.88/32 table cmcc pref 400
上面操作后会出现一个问题,局域网连接不上啦,因为本机的全部转发给了网关…
解决办法,修改 cmcc 、 tel 路由表加上本地网络或者添加策略路由规则,局域网的走默认路由。(见结尾)
root@ubuntu-1u:/home/gamexg# ./d
HTTP/1.1 200 OK
Server: Cowboy
Connection: close
Content-Type: text/plain
Date: Mon, 26 Sep 2016 10:13:35 GMT
Content-Length: 14
Via: 1.1 vegur
*******
root@ubuntu-1u:/home/gamexg# ./d -sip 192.168.87.88
HTTP/1.1 200 OK
Server: Cowboy
Connection: close
Content-Type: text/plain
Date: Mon, 26 Sep 2016 10:13:57 GMT
Content-Length: 13
Via: 1.1 vegur
******
root@ubuntu-1u:/home/gamexg# ./d -sip 192.168.101.88
HTTP/1.1 200 OK
Server: Cowboy
Connection: close
Content-Type: text/plain
Date: Mon, 26 Sep 2016 10:14:16 GMT
Content-Length: 14
Via: 1.1 vegur
******
可以看到已经可以通过指定本地地址的方式指定出口了。
# 显示当前策略路由
root@ubuntu-1u:/home/gamexg# ip rule list
0: from all lookup local
300: from 192.168.87.88 lookup tel
400: from 192.168.101.88 lookup cmcc
32766: from all lookup main
32767: from all lookup default
# 显示默认路由表(main)
root@ubuntu-1u:/home/gamexg# ip route list
default via 192.168.101.1 dev enp2s0 onlink
default via 192.168.87.1 dev enx00e04c5200a2 metric 30 onlink
10.10.10.0/24 dev zt0 proto kernel scope link src 10.10.10.2
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
192.168.87.0/24 dev enx00e04c5200a2 proto kernel scope link src 192.168.87.88
192.168.101.0/24 dev enp2s0 proto kernel scope link src 192.168.101.88
# 显示指定路由表 main
root@ubuntu-1u:/home/gamexg# ip route list table main
default via 192.168.101.1 dev enp2s0 onlink
default via 192.168.87.1 dev enx00e04c5200a2 metric 30 onlink
10.10.10.0/24 dev zt0 proto kernel scope link src 10.10.10.2
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
192.168.87.0/24 dev enx00e04c5200a2 proto kernel scope link src 192.168.87.88
192.168.101.0/24 dev enp2s0 proto kernel scope link src 192.168.101.88
# 显示制定路由表 local
root@ubuntu-1u:/home/gamexg# ip route list table local
broadcast 10.10.10.0 dev zt0 proto kernel scope link src 10.10.10.2
local 10.10.10.2 dev zt0 proto kernel scope host src 10.10.10.2
broadcast 10.10.10.255 dev zt0 proto kernel scope link src 10.10.10.2
broadcast 127.0.0.0 dev lo proto kernel scope link src 127.0.0.1
local 127.0.0.0/8 dev lo proto kernel scope host src 127.0.0.1
local 127.0.0.1 dev lo proto kernel scope host src 127.0.0.1
broadcast 127.255.255.255 dev lo proto kernel scope link src 127.0.0.1
broadcast 172.17.0.0 dev docker0 proto kernel scope link src 172.17.0.1
local 172.17.0.1 dev docker0 proto kernel scope host src 172.17.0.1
broadcast 172.17.255.255 dev docker0 proto kernel scope link src 172.17.0.1
broadcast 192.168.87.0 dev enx00e04c5200a2 proto kernel scope link src 192.168.87.88
local 192.168.87.88 dev enx00e04c5200a2 proto kernel scope host src 192.168.87.88
broadcast 192.168.87.255 dev enx00e04c5200a2 proto kernel scope link src 192.168.87.88
broadcast 192.168.101.0 dev enp2s0 proto kernel scope link src 192.168.101.88
local 192.168.101.88 dev enp2s0 proto kernel scope host src 192.168.101.88
broadcast 192.168.101.255 dev enp2s0 proto kernel scope link src 192.168uu 锁.101.88
# 修复之前策略路由造成本地无法访问的问题
root@ubuntu-1u:/home/gamexg# ip rule add to 192.168.101.0/24 table main
oot@ubuntu-1u:/home/gamexg# ip rule add to 192.168.87.0/24 table main=
gamexg@ubuntu-1u:~$ ip rule
0: from all lookup local
298: from all to 192.168.87.0/24 lookup main
299: from all to 192.168.101.0/24 lookup main
300: from 192.168.87.88 lookup tel
400: from 192.168.101.88 lookup cmcc
32766: from all lookup main
32767: from all lookup default