Socat多端口转发配置 Jun 21st 2018 Words: 393
This post is created 2 years ago, the content may be outdated.

socat不支持一次转发多个端口,本文介绍使用脚本一次启动多个socat进程以进行多端口转发的方法,供日后配置时参考。

配置过程

创建批量启动脚本

/usr/local/bin创建脚本socat-autostart并添加启动权限。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/sh

proc=/usr/bin/socat
args="-d"
logfile="/var/log/socat.log"

echo "Strating socat processes..."

nohup $proc $args TCP4-LISTEN:80,reuseaddr,fork TCP4:example.com:80 >> $logfile 2>&1 &
nohup $proc $args UDP4-LISTEN:80,reuseaddr,fork UDP4:example.com:80 >> $logfile 2>&1 &

nohup $proc $args TCP4-LISTEN:81,reuseaddr,fork TCP4:example.com:81 >> $logfile 2>&1 &
nohup $proc $args UDP4-LISTEN:81,reuseaddr,fork UDP4:example.com:81 >> $logfile 2>&1 &

nohup $proc $args TCP4-LISTEN:443,reuseaddr,fork TCP4:example.com:443 >> $logfile 2>&1 &
nohup $proc $args UDP4-LISTEN:443,reuseaddr,fork UDP4:example.com:443 >> $logfile 2>&1 &

nohup $proc $args TCP4-LISTEN:444,reuseaddr,fork TCP4:example.com:444 >> $logfile 2>&1 &
nohup $proc $args UDP4-LISTEN:444,reuseaddr,fork UDP4:example.com:444 >> $logfile 2>&1 &

nohup $proc $args TCP4-LISTEN:284,reuseaddr,fork TCP4:example.com:284 >> $logfile 2>&1 &
nohup $proc $args UDP4-LISTEN:284,reuseaddr,fork UDP4:example.com:284 >> $logfile 2>&1 &

nohup $proc $args TCP4-LISTEN:2041,reuseaddr,fork TCP4:example.com:2041 >> $logfile 2>&1 &
nohup $proc $args UDP4-LISTEN:2041,reuseaddr,fork UDP4:example.com:2041 >> $logfile 2>&1 &

echo "Completed. "
exit 0

创建Systemd服务

/etc/systemd/system下建立socat.service,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Unit]
Description=Socat Multi-forward Server Service
Documentation=man:socat
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/socat-autostart
ExecStop=/usr/bin/pkill socat
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target


EOF