用 Ubuntu 自己设定软路由,不用现成的软路由系统( 四 )


/sbin/iptables -A POSTROUTING -t nat -s $NETWORK/24 -d $MAIL/32 -p tcp -m tcp --dport 25 -j SNAT --to-source $LANIP
/sbin/iptables -A POSTROUTING -t nat -s $NETWORK/24 -d $MAIL/32 -p tcp -m tcp --dport 465 -j SNAT --to-source $LANIP
/sbin/iptables -A POSTROUTING -t nat -s $NETWORK/24 -d $MAIL/32 -p tcp -m tcp --dport 587 -j SNAT --to-source $LANIP
/sbin/iptables -A POSTROUTING -t nat -s $NETWORK/24 -d $MAIL/32 -p tcp -m tcp --dport 993 -j SNAT --to-source $LANIP
/sbin/iptables -A POSTROUTING -t nat -s $NETWORK/24 -d $MAIL/32 -p tcp -m tcp --dport 995 -j SNAT --to-source $LANIP
##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
将以上四个脚本设为“可执行”: chmod +x filename 。重启 ...现在终于可以经这个路由上网了!
 
6. DDNS
我没有固定 IP,所以要用 DDNS 。我用的是 dnsexit(免费、收费都有),更新 DNS 非常简单 - 在 /etc/rc.local 执行以下脚本就行 。
/usr/myscripts/update-ddns
#!/bin/bash
USER=username;
PASSword=updatepassword;
DOMAINS=(domain1.net domain2.net); # multiple domains at the same IP separated by spaces
LOG=/tmp/update-ddns.log
echo "* $(date)" | tee -a $LOG
echo | tee -a $LOG
echo "Updating DDNS to dnsexit ..."
for domain in "${DOMAINS[@]}"; do
url="http://update.dnsexit.com/RemoteUpdate.sv?login=$USER&password=$PASSWORD&host=$domain";
echo $url | tee -a $LOG
result=$(curl -s $url)
echo "$result" | tee -a $LOG
echo | tee -a $LOG
done
echo | tee -a $LOG
 
当然要 chmod +x
/usr/myscripts/update-ddns 将这脚本设为“可执行” 。
 
7. overlayroot
我们当然要求软路由能经得起断电而系统不坏,系统不会在我们不想改变的时候有任何变化 。所以我使用了 overlayroot 。
apt-get install overlayroot
其实上面的是不需要的,因为 Ubuntu 已经预装了 overlayroot 。
修改 /etc/overlayroot.conf,将 overlayroot="" 改为:
 
overlayroot="tmpfs"
重启系统后,整个 root filesystem 已经被保护了 。之后的任何改变,都会在重启之后恢复回之前的状态 。
之后如果确实要改变系统,可以输入 overlayroot-chroot 进入真实的档案系统,然后修改 /etc/overlayroot.conf,改回 overlayroot="",重启 ... 这样就停用了 overlayroot 。
详细可以参考:
https://spin.atomicobject.com/2015/03/10/protecting-ubuntu-root-filesystem/ 。
8. 其他脚本
我测试过把 WAN 口网线拔掉重插,if-pre-up.d 和 if-up.d 并没有触发 。这就让我担心掉线之后会不会不能上网,或者 ip 改变导致 dns 和 port forwarding 规则失败 。其实最佳的解决办法可能是用 ip monitor address 然后 grep,一直监视 WAN 口 。但我在网上找到 ip monitor address 的例子非常少,而且也不知道怎样改变地址来测试,所以只能用 cron 。每隔 5分钟或者10分钟检查一遍 。
脚本不贴出来了,在附件里面 。

用 Ubuntu 自己设定软路由,不用现成的软路由系统

文章插图
 
other-scripts.zip
  • vars : 存储 ping-sites 和 dns-check 两个脚本通用的资料 。
  • ping-sites : ping 一些外面的网站,第一次不通,ifdown && ifup;连续第二次不通,reboot 。
  • dns-check : 比较 authoritative servers 和 WAN 口的 ip 地址,并且检查 port forwarding 规则是不是含正确的 ip 。
  • send-mail : 发送 email 的脚本 。
 
9. WAN-to-LAN 速度测试
测试环境:
用 Ubuntu 自己设定软路由,不用现成的软路由系统

文章插图
 
al@DESKTOP-CFK04JL:~$ iperf3 -c 10.0.0.133 -t 10
Connecting to host 10.0.0.133, port 5201
[ 4] local 10.0.1.29 port 41268 connected to 10.0.0.133 port 5201
[ ID] Interval Transfer Bandwidth Retr Cwnd
[ 4] 0.00-1.00 sec 111 MBytes 929 Mbits/sec 0 731 KBytes
[ 4] 1.00-2.00 sec 110 MBytes 923 Mbits/sec 0 1.07 MBytes
[ 4] 2.00-3.00 sec 109 MBytes 912 Mbits/sec 0 1.07 MBytes
[ 4] 3.00-4.00 sec 110 MBytes 923 Mbits/sec 0 1.07 MBytes
[ 4] 4.00-5.00 sec 111 MBytes 933 Mbits/sec 0 1.07 MBytes
[ 4] 5.00-6.00 sec 109 MBytes 912 Mbits/sec 0 1.07 MBytes
[ 4] 6.00-7.00 sec 109 MBytes 912 Mbits/sec 0 1.07 MBytes
[ 4] 7.00-8.00 sec 108 MBytes 902 Mbits/sec 0 1.13 MBytes
[ 4] 8.00-9.00 sec 109 MBytes 912 Mbits/sec 0 1.13 MBytes
[ 4] 9.00-10.00 sec 109 MBytes 912 Mbits/sec 0 1.13 MBytes
- - - - - - - - - - - - - - - - - - - - - - - - -


推荐阅读