原标题:9929 GRE隧道的套娃尝试
导读:
Olink的GRE 9929 隧道有一点局限性就是只能绑定一个ip,而我帮朋友买了好几台欧洲的服务器,回程线路太差需要优化,而他们网站基本也没啥流量,如果每个服务器都要单独买一...
之前的一篇文章提到了自建GRE隧道修改服务器的回程的操作,看上去这个技术很不错,但是实际操作起来很有局限性,因为99%以上的服务器都是无法伪造ip的,线路过得去的就更加没有了(gia对于源ip还有白名单,即使机房允许你伪造ip,但是回程依旧走不了gia),所以真想用来优化线路还是建议直接购买现有的产品,不过Olink的隧道有一点局限性就是只能绑定一个ip,如果每个服务器都要单独买一个GRE就有点浪费了,所以我想着能否套娃一下,在我的服务器和Olink的转发服务器之间建立隧道,再用我的服务器通过隧道转发来自我朋友的服务器的数据包。下面开始实际操作:
先用一台服务器作为中间层,创建隧道。记得在POSTROUTING中设置SNAT。
echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf sysctl -p ip tunnel add mgre0 mode gre key 0xfffffffe ttl 255 ip addr add 10.0.1.1/24 dev mgre0 #注意 10.0.1.2 对应的就是待优化的服务器 ip neigh add 10.0.1.2 lladdr [待优化的机器的ip] dev mgre0 ip link set mgre0 up iptables -t filter -I FORWARD -i mgre0 -o tun_olink -j ACCEPT iptables -t nat -I POSTROUTING -s 10.0.1.2 -j SNAT --to [待优化的服务器的公网ip]
在待优化的服务器下进行下面的操作:
#反向路由校验改为宽松模式 sysctl -w net.ipv4.conf.all.rp_filter=2 ip tunnel add mgre0 mode gre key 0xfffffffe ttl 255 ip addr add 10.0.1.2/24 dev mgre0 ip neigh add 10.0.1.1 lladdr [中间机器的ip] dev mgre0 ip link set mgre0 up echo '100 mgre' >> /etc/iproute2/rt_tables ip rule add from all table mgre
然后使用上一篇文章提到的py脚本,生成批量命令脚本:
# -*- coding: utf-8 -*- import requests url = 'https://raw.githubusercontent.com/metowolf/iplist/master/data/special/china.txt' r = requests.get(url, allow_redirects=True) open('ips.txt', 'wb').write(r.content) # 之后生成添加路由表命令与卸载路由表命令 start = open('startCMD.sh', 'w') end = open('endCMD.sh', 'w') with open('ips.txt','r') as f: ips = f.read().splitlines() for ip in ips: start.write('ip route add ' + ip + ' via 10.0.1.1 table mgre\n') end.write('ip route del ' + ip + ' via 10.0.1.1 table mgre\n') # 生成启动脚本与卸载脚本 (暂时没分运营商) # print(ip) start.close() end.close()
然后执行 basg sttartCMD.sh 来批量修改路由。如果要持久化,让客户服务器重启后依旧能维持修改,可以把下面的命令写到脚本中去,然后开机的时候执行。
ip tunnel add mgre0 mode gre key 0xfffffffe ttl 255 ip addr add 10.0.1.2/24 dev mgre0 ip neigh add 10.0.1.1 lladdr 161.97.1.1 dev mgre0 ip link set mgre0 up ip rule add from all table mgre bash /root/startCMD.sh #卸载连接只需要 ip rule del from all table mgre
优化后可以说效果显著了(腾讯云过滤10099的流量,所以丢包比较严重)Olink的gre隧道还是很好用的,感兴趣的朋友可以试试。
还没有评论,来说两句吧...