firewalld中转(转发)流量

今天做了件有意义的事儿,在腾讯云(香港)轻量云服务器(简称中转服务器A)上做了一个中转服务,把监听的某个端口(比如8080)的流量转发到国外某个VPS(此例为俄罗斯Gcorelabs家叶卡捷琳堡的VPS B)。

原因很简单,腾讯云这款服务器在香港,到大陆和境外的速度都很不错,ping到黑龙江才70ms,到叶卡捷琳堡的VPS B 120ms左右,加起来200ms左右,比直连叶卡捷琳堡的VPS B(300ms左右而且容易丢包)要快,也更加稳定。

中转之前,要先配置好境外服务器(比如这里的VPS B)的爱国上网服务,记住该服务的端口和该VPS的IP。

参考:

https://www.tkmiss.com/archives/vps_port_forward.html

首先检查防火墙伪装功能(SNAT)(这一步很重要)
# 检查是否允许伪装IP
firewall-cmd --query-masquerade
# 允许防火墙伪装IP
firewall-cmd --add-masquerade --permanent
# 禁止防火墙伪装IP
firewall-cmd --remove-masquerade --permanent
如果查询出来为no,则需要通过add添加。

设置端口转发

# 将80端口的流量转发至8080
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080
# 将80端口的流量转发至192.168.0.1
firewall-cmd --add-forward-port=port=80:proto=tcp:toaddr=192.168.1.0.1
# 将80端口的流量转发至192.168.0.1的8080端口
firewall-cmd --add-forward-port=port=80:proto=tcp:toaddr=192.168.0.1:toport=8080
#查询所有配置
firewall-cmd --list-all
systemctl enable firewalld
systemctl start firewalld
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
sysctl -p firewall-cmd --permanent --add-masquerade 

# 8080可以改成其他端口 
firewall-cmd --permanent --add-port=8080/tcp 
firewall-cmd --permanent --add-port=8080/udp 

# 8080和上面保持一致,国外ip改成你国外vps的ip,443改成国外VPS上的爱国上网等软件的端口 
firewall-cmd --permanent --add-forward-port=port=8080:proto=tcp:toaddr=国外ip:toport=443 
firewall-cmd --permanent --add-forward-port=port=8080:proto=udp:toaddr=国外ip:toport=443 
firewall-cmd --reload

运行上述命令后,打开爱国上网等客户端软件,把ip和端口 改成国内vps的ip和端口号,其他配置不变,应该就能上网了。

流量转发的方式有很多种,这里用了最简单也是效率最高的firewalld。流量转发对于访问外网非常有意义,因为这里可以借助国内的NAT VPS(共享IP VPS,价格低,带宽足,到国内和国外的线路好)作为中转服务器来加速访问外网。对于我这种常年国内外跑的人来说,爱国上网是必备技能,商用的一些软件会经常down掉,尤其是六一,国庆等特殊时期。

在这里赞一下腾讯云轻量云(香港),30M uploads, 1Gbps downloads,最低配24/月,1T流量,我入手的34的那款,2T流量,内存也大些,性价比无敌。要知道,它用了非常昂贵的CN2 GIA线路,用我家的百兆带宽(下行100-150Mbps,上行30Mbps)可以跑满它的带宽,即使在晚高峰时间段。

阿里云晚高峰照样跑满30M带宽,看4K视频没有丝毫压力
Gcorelabs的VPS晚高峰直连速度很慢,但是通过腾讯轻量云中转下也能跑到10-20M带宽

Python 脚本自启动及定时任务

爬虫脚本定时运行(crontab)

linux定时执行python脚本的方法以及失败不执行的处理方法

https://www.jianshu.com/p/5cd74add11ba 没有采用简书的方案,上面的博客方案更加清晰。以下是我的笔记(重点是环境变量,路径,scp的使用方法)

不需要root,执行以下命令:
crontab -e
编辑:
#注意这里最好把环境变量写上
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/home/chen/miniconda3/bin/
MAILTO=root
#测试:两分钟执行一次
*/2 * * * * echo "crontab test" >> /home/chen/ctest.txt
#写的python脚本每5分钟执行一次(注意python脚本中文件路径必须是绝对路径,否则报错)
*/5 * * * * /home/chen/miniconda3/bin/python /home/chen/CoV_copy2.py 
#scp python脚本产生的HTML到博客服务器(这里用了密钥而不是密码)。注意:所有的路径都#使用绝对路径,否则无效。
* * * * * scp -i /home/chen/.ssh/do248 -P 4705 /home/chen/2019-nCov数据一览2.html chen@207.148.96.113:/home/wwwroot/geoseis.cn/CoV.html >> /home/chen/cov.log
# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

linux定时执行失败的处理方法就是查看 /var/spool/mail/ ,一般root里都有日志,可以看到哪里出错了。

这个爬虫脚本放到vpn后需要更新很多:

  • 文件路径必须为绝对路径
  • pyecharts的Geo函数bug:遇到不存在的地点Geo函数就会使程序退出,即使用了except也不行,和笔记本环境不一样。笔记本里运行程序不会退出。解决方案就是复制了旧版本的一些.py程序到相应的目录
  • proxy在VPN中没有设置好,因为笔记本里用的SSR代理的(5个VPN)。这个脚本五分钟执行一次不需要代理也可以

scp的使用

scp推荐使用密钥传输,更加安全。密钥产生后我直接append到目标服务器的/home/chen/.ssh/authorized_keys,然后scp -i 就可以无密码传输了。

运行 ssh-keygen -t dsa 时最好设置密码,否则密钥没有密码也不安全。

参考:

It’s better to set up ssh to used key-based authentication rather than trying to figure out how to send text to the login process with something like expect.

Take a look at:

https://help.ubuntu.com/community/SSH/OpenSSH/Keys

So, basically, run ssh-keygen -t dsa on the machine that will run your script. When it asks you for a passphrase, hit ENTER to accept a blank. You will get two files. If you followed the default suggestions, the files will be ~/.ssh/id_dsa and ~/.ssh/id_dsa.pub. The first one is the private key. The second one is the public key.

Copy the public key to the second server using ssh-copy-id user@server2. This will add the public key to the authorized_keys file of the user on server2.

You should now be able to run ssh from the first machine and log in without a password.

For copying the files, scp or rsync are fine. It depends on what you’re doing. rsync will use ssh by default, so will use the key-based authentication you just set up