最近国内的网络环境是越来越差了,改绕的还是绕,不该绕的也开始绕了…
那么看着那上天的延迟,总要想点办法解决对吧,那么除了出国最好的方案估计就是加钱了,世界加钱可及
加钱买一台阿里云香港B是个不错的方案,但是阿里云到全球的速度却不如香港SoftLayer好,那么我们可以通过中转的方案让香港SL也能随便跑到100M
然而通过haproxy的方式中转的流量不支持UDP,那么就没法用来玩游戏了,所以只能通过iptables中转Shadowsocks TCP和UDP的流量
首先我们要打开服务器的转发功能:vi /etc/sysctl.conf
把net.ipv4.ip_forward= 0
修改成net.ipv4.ip_forward= 1
保存后执行sysctl -p
让修改生效
然后执行下列命令添加iptables规则
iptables -t nat -A PREROUTING -p tcp --dport [端口号] -j DNAT --to-destination [目标IP] iptables -t nat -A PREROUTING -p udp --dport [端口号] -j DNAT --to-destination [目标IP] iptables -t nat -A POSTROUTING -p tcp -d [目标IP] --dport [端口号] -j SNAT --to-source [中转服务器IP] iptables -t nat -A POSTROUTING -p udp -d [目标IP] --dport [端口号] -j SNAT --to-source [中转服务器IP]
如果需要把10000到20000的端口全部转发到目标服务器的对应端口,就在[端口号]这填写
10000:20000
非相同端口的转发也差不多
iptables -t nat -A PREROUTING -p tcp --dport [源端口号] -j DNAT --to-destination [目标IP:目标端口号] iptables -t nat -A PREROUTING -p udp --dport [源端口号] -j DNAT --to-destination [目标IP:目标端口号] iptables -t nat -A POSTROUTING -p tcp -d [目标IP] --dport [目标端口号] -j SNAT --to-source [中转服务器IP] iptables -t nat -A POSTROUTING -p udp -d [目标IP] --dport [目标端口号] -j SNAT --to-source [中转服务器IP]
举个栗子,你中转中转服务器的10000端口转发到目标服务器8.8.8.8的53端口上应该这么写:
iptables -t nat -A PREROUTING -p tcp --dport 10000 -j DNAT --to-destination 8.8.8.8:53 iptables -t nat -A PREROUTING -p udp --dport 10000 -j DNAT --to-destination 8.8.8.8:53 iptables -t nat -A POSTROUTING -p tcp -d 8.8.8.8 --dport 53 -j SNAT --to-source [中转服务器IP] iptables -t nat -A POSTROUTING -p udp -d 8.8.8.8 --dport 53 -j SNAT --to-source [中转服务器IP]
最后别忘了保存规则和重启iptablesservice iptables save
service iptables restart
如果你感觉自己特聪明,已经身近百战了,你还能直接编辑iptables文件
vi /etc/sysconfig/iptables
然后重启iptables service iptables restart
然后就连接中转服务器IP爽爽吧~