配置中转服务器

配置中转服务器

配置端口转发,适用于国外访问国内延迟比较高,香港地区访问国内延迟低,使用香港服务器做中转,转发到国内。同时香港服务器接收海外数据,比如新加坡。

直接运行脚本,按照提示配置香港服务器端口和远程服务器ip和端口,

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#! /bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# ====================================================
# System Request:CentOS 6+ 、Debian 7+、Ubuntu 14+
# Author: Rat's
# Dscription: Socat一键脚本
# Version: 1.0
# Blog: https://www.moerats.com
# Github:https://github.com/iiiiiii1/Socat
# ====================================================

Green="\033[32m"
Font="\033[0m"
Blue="\033[33m"

rootness(){
if [[ $EUID -ne 0 ]]; then
echo "Error:This script must be run as root!" 1>&2
exit 1
fi
}

checkos(){
if [[ -f /etc/redhat-release ]];then
OS=CentOS
elif cat /etc/issue | grep -q -E -i "debian";then
OS=Debian
elif cat /etc/issue | grep -q -E -i "ubuntu";then
OS=Ubuntu
elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat";then
OS=CentOS
elif cat /proc/version | grep -q -E -i "debian";then
OS=Debian
elif cat /proc/version | grep -q -E -i "ubuntu";then
OS=Ubuntu
elif cat /proc/version | grep -q -E -i "centos|red hat|redhat";then
OS=CentOS
else
echo "Not supported OS, Please reinstall OS and try again."
exit 1
fi
}

disable_selinux(){
if [ -s /etc/selinux/config ] && grep 'SELINUX=enforcing' /etc/selinux/config; then
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
fi
}

disable_iptables(){
systemctl stop firewalld.service >/dev/null 2>&1
systemctl disable firewalld.service >/dev/null 2>&1
service iptables stop >/dev/null 2>&1
chkconfig iptables off >/dev/null 2>&1
}

get_ip(){
ip=`curl http://whatismyip.akamai.com`
}

config_socat(){
echo -e "${Green}请输入Socat配置信息!${Font}"
read -p "请输入本地端口:" port1
read -p "请输入远程端口:" port2
read -p "请输入远程IP:" socatip
}

start_socat(){
echo -e "${Green}正在配置Socat...${Font}"
nohup socat TCP4-LISTEN:${port1},reuseaddr,fork TCP4:${socatip}:${port2} >> /root/socat.log 2>&1 &
nohup socat -T 600 UDP4-LISTEN:${port1},reuseaddr,fork UDP4:${socatip}:${port2} >> /root/socat.log 2>&1 &
if [ "${OS}" == 'CentOS' ];then
sed -i '/exit/d' /etc/rc.d/rc.local
echo "nohup socat TCP4-LISTEN:${port1},reuseaddr,fork TCP4:${socatip}:${port2} >> /root/socat.log 2>&1 &
nohup socat -T 600 UDP4-LISTEN:${port1},reuseaddr,fork UDP4:${socatip}:${port2} >> /root/socat.log 2>&1 &
" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
elif [ -s /etc/rc.local ]; then
sed -i '/exit/d' /etc/rc.local
echo "nohup socat TCP4-LISTEN:${port1},reuseaddr,fork TCP4:${socatip}:${port2} >> /root/socat.log 2>&1 &
nohup socat -T 600 UDP4-LISTEN:${port1},reuseaddr,fork UDP4:${socatip}:${port2} >> /root/socat.log 2>&1 &
" >> /etc/rc.local
chmod +x /etc/rc.local
else
echo -e "${Green}检测到系统无rc.local自启,正在为其配置... ${Font} "
echo "[Unit]
Description=/etc/rc.local
ConditionPathExists=/etc/rc.local

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99

[Install]
WantedBy=multi-user.target
" > /etc/systemd/system/rc-local.service

if [ ! -f /etc/rc.local ]; then
echo "#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
" > /etc/rc.local
echo "nohup socat TCP4-LISTEN:${port1},reuseaddr,fork TCP4:${socatip}:${port2} >> /root/socat.log 2>&1 &
nohup socat -T 600 UDP4-LISTEN:${port1},reuseaddr,fork UDP4:${socatip}:${port2} >> /root/socat.log 2>&1 &
" >> /etc/rc.local
chmod +x /etc/rc.local
systemctl enable rc-local >/dev/null 2>&1
systemctl start rc-local >/dev/null 2>&1
fi
get_ip
sleep 3
echo
echo -e "${Green}Socat安装并配置成功!${Font}"
echo -e "${Blue}你的本地端口为:${port1}${Font}"
echo -e "${Blue}你的远程端口为:${port2}${Font}"
echo -e "${Blue}你的本地服务器IP为:${ip}${Font}"
exit 0
}

install_socat(){
echo -e "${Green}即将安装Socat...${Font}"
if [ "${OS}" == 'CentOS' ];then
yum install -y socat
else
apt-get -y update
apt-get install -y socat
fi
if [ -s /usr/bin/socat ]; then
echo -e "${Green}Socat安装完成!${Font}"
fi
}

status_socat(){
if [ -s /usr/bin/socat ]; then
echo -e "${Green}检测到Socat已存在,并跳过安装步骤!${Font}"
main_x
else
main_y
fi
}

main_x(){
checkos
rootness
disable_selinux
disable_iptables
config_socat
start_socat
}

main_y(){
checkos
rootness
disable_selinux
disable_iptables
install_socat
config_socat
start_socat
}

status_socat

2. 删除配置好的转发端口

上面运行的脚本是把配置的端口转发写入的开机自启动配置里面的,所以删掉对应的就行。

1
2
3
4
5
6
7
8
vim  /etc/rc.local

echo 1 > /proc/sys/net/ipv4/ip_forward)&
nohup socat TCP4-LISTEN:999,reuseaddr,fork TCP4:47.116.139.66:33189 >> /root/socat.log 2>&1 &
nohup socat -T 600 UDP4-LISTEN:999,reuseaddr,fork UDP4:47.116.139.66:33189 >> /root/socat.log 2>&1 &

nohup socat TCP4-LISTEN:8080,reuseaddr,fork TCP4:47.116.139.66:33189 >> /root/socat.log 2>&1 &
nohup socat -T 600 UDP4-LISTEN:8080,reuseaddr,fork UDP4:47.116.139.66:33189 >> /root/socat.log 2>&1 &

评论


:D 一言句子获取中...

加载中,最新评论有1分钟缓存...