forked from shuuzhoou/doubi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socat.sh
387 lines (379 loc) · 13.6 KB
/
socat.sh
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#=================================================
# System Required: CentOS/Debian/Ubuntu
# Description: Socat
# Version: 1.0.1
# Author: Toyo
# Blog: https://doub.io/wlzy-18/
#=================================================
socat_file="/etc/socat"
socat_log_file="/etc/socat/socat.log"
#检查是否安装Socat
check_socat(){
socat_exist=`socat -h`
if [[ ${socat_exist} = "" ]]; then
echo -e "\033[41;37m [错误] \033[0m 没有安装Socat,请检查 !"
exit 1
fi
}
#检查系统
check_sys(){
if [[ -f /etc/redhat-release ]]; then
release="centos"
elif cat /etc/issue | grep -q -E -i "debian"; then
release="debian"
elif cat /etc/issue | grep -q -E -i "ubuntu"; then
release="ubuntu"
elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then
release="centos"
elif cat /proc/version | grep -q -E -i "debian"; then
release="debian"
elif cat /proc/version | grep -q -E -i "ubuntu"; then
release="ubuntu"
elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then
release="centos"
fi
#bit=`uname -m`
}
# 安装Socat
installSocat(){
# 判断是否安装Socat
socat_exist=`socat -h`
if [[ ${socat_exist} != "" ]]; then
echo -e "\033[41;37m [错误] \033[0m 已经安装Socat,请检查 !"
exit 1
fi
check_sys
# 系统判断
if [[ ${release} == "centos" ]]; then
yum update
yum install -y vim curl socat
else
apt-get update
apt-get install -y vim curl socat
fi
chmod +x /etc/rc.local
#修改DNS为8.8.8.8
echo "nameserver 8.8.8.8" > /etc/resolv.conf
echo "nameserver 8.8.4.4" >> /etc/resolv.conf
mkdir ${socat_file}
#判断socat是否安装成功
socat_exist=`socat -h`
if [[ ${socat_exist} = "" ]]; then
echo -e "\033[41;37m [错误] \033[0m 安装Socat失败,请检查 !"
exit 1
else
echo -e "\033[42;37m [信息] \033[0m Socat 安装完成 !"
fi
}
addSocat(){
# 判断是否安装Socat
check_socat
# 设置本地监听端口
while true
do
echo -e "请输入 Socat 的 本地监听端口 [1-65535]"
read -p "(默认端口: 23333):" Socatport
[[ -z "$Socatport" ]] && Socatport="23333"
expr ${Socatport} + 0 &>/dev/null
if [[ $? -eq 0 ]]; then
if [[ ${Socatport} -ge 1 ]] && [[ ${Socatport} -le 65535 ]]; then
echo
echo "——————————————————————————————"
echo -e " 本地监听端口 : \033[41;37m ${Socatport} \033[0m"
echo "——————————————————————————————"
echo
break
else
echo -e "\033[41;37m [错误] \033[0m 请输入正确的数字 !"
fi
else
echo -e "\033[41;37m [错误] \033[0m 请输入正确的数字 !"
fi
done
# 设置欲转发端口
while true
do
echo -e "请输入 Socat 欲转发的 端口 [1-65535]"
read -p "(默认端口: ${Socatport}):" Socatport1
[[ -z "$Socatport1" ]] && Socatport1=${Socatport}
expr ${Socatport1} + 0 &>/dev/null
if [[ $? -eq 0 ]]; then
if [[ ${Socatport1} -ge 1 ]] && [[ ${Socatport1} -le 65535 ]]; then
echo
echo "——————————————————————————————"
echo -e " 欲转发端口 : \033[41;37m ${Socatport1} \033[0m"
echo "——————————————————————————————"
echo
break
else
echo -e "\033[41;37m [错误] \033[0m 请输入正确的数字 !"
fi
else
echo -e "\033[41;37m [错误] \033[0m 请输入正确的数字 !"
fi
done
# 设置欲转发 IP
read -p "请输入 Socat 欲转发的 IP:" socatip
[[ -z "${socatip}" ]] && echo "取消..." && exit 1
echo
echo "——————————————————————————————"
echo -e " 欲转发 IP : \033[41;37m ${socatip} \033[0m"
echo "——————————————————————————————"
echo
#设置 转发类型
echo "请输入数字 来选择 Socat 转发类型:"
echo "1. TCP"
echo "2. UDP"
echo "3. TCP+UDP"
echo
read -p "(默认: TCP+UDP):" socattype_num
[ -z "${socattype_num}" ] && socattype_num="3"
if [ ${socattype_num} = "1" ]; then
socattype="TCP"
elif [ ${socattype_num} = "2" ]; then
socattype="UDP"
elif [ ${socattype_num} = "3" ]; then
socattype="TCP+UDP"
else
socattype="TCP+UDP"
fi
#最后确认
echo
echo "——————————————————————————————"
echo " 请检查 Socat 配置是否有误 !"
echo
echo -e " 本地监听端口 : \033[41;37m ${Socatport} \033[0m"
echo -e " 欲转发 IP : \033[41;37m ${socatip} \033[0m"
echo -e " 欲转发端口 : \033[41;37m ${Socatport1} \033[0m"
echo -e " 转发类型 : \033[41;37m ${socattype} \033[0m"
echo "——————————————————————————————"
echo
read -p "请按任意键继续,如有配置错误请使用 Ctrl+C 退出。" var
if [ ${socattype} = "TCP" ]; then
nohup socat TCP4-LISTEN:${Socatport},reuseaddr,fork TCP4:${socatip}:${Socatport1} >> ${socat_log_file} 2>&1 &
sleep 2s
PID=`ps -ef | grep "socat TCP4-LISTEN:${Socatport}" | grep -v grep | awk '{print $2}'`
if [[ -z $PID ]]; then
echo -e "\033[41;37m [错误] \033[0m Socat TCP 启动失败 !"
exit 1
fi
# 系统判断
check_sys
if [[ ${release} == "debian" ]]; then
sed -i '$d' /etc/rc.local
echo -e "nohup socat TCP4-LISTEN:${Socatport},reuseaddr,fork TCP4:${socatip}:${Socatport1} >> ${socat_log_file} 2>&1 &" >> /etc/rc.local
echo -e "exit 0" >> /etc/rc.local
else
echo -e "nohup socat TCP4-LISTEN:${Socatport},reuseaddr,fork TCP4:${socatip}:${Socatport1} >> ${socat_log_file} 2>&1 &" >> /etc/rc.local
fi
iptables -I INPUT -p tcp --dport ${Socatport} -j ACCEPT
elif [ ${socattype} = "UDP" ]; then
nohup socat UDP4-LISTEN:${Socatport},reuseaddr,fork UDP4:${socatip}:${Socatport1} >> ${socat_log_file} 2>&1 &
sleep 2s
PID=`ps -ef | grep "socat UDP4-LISTEN:${Socatport}" | grep -v grep | awk '{print $2}'`
if [[ -z $PID ]]; then
echo -e "\033[41;37m [错误] \033[0m Socat UDP 启动失败 !"
exit 1
fi
# 系统判断
check_sys
if [[ ${release} == "debian" ]]; then
sed -i '$d' /etc/rc.local
echo -e "nohup socat UDP4-LISTEN:${Socatport},reuseaddr,fork UDP4:${socatip}:${Socatport1} >> ${socat_log_file} 2>&1 &" >> /etc/rc.local
echo -e "exit 0" >> /etc/rc.local
else
echo -e "nohup socat UDP4-LISTEN:${Socatport},reuseaddr,fork UDP4:${socatip}:${Socatport1} >> ${socat_log_file} 2>&1 &" >> /etc/rc.local
fi
iptables -I INPUT -p udp --dport ${Socatport} -j ACCEPT
elif [ ${socattype} = "TCP+UDP" ]; then
nohup socat TCP4-LISTEN:${Socatport},reuseaddr,fork TCP4:${socatip}:${Socatport1} >> ${socat_log_file} 2>&1 &
nohup socat UDP4-LISTEN:${Socatport},reuseaddr,fork UDP4:${socatip}:${Socatport1} >> ${socat_log_file} 2>&1 &
sleep 2s
PID=`ps -ef | grep "socat TCP4-LISTEN:${Socatport}" | grep -v grep | awk '{print $2}'`
PID1=`ps -ef | grep "socat UDP4-LISTEN:${Socatport}" | grep -v grep | awk '{print $2}'`
if [[ -z $PID ]]; then
echo -e "\033[41;37m [错误] \033[0m Socat TCP 启动失败 !"
exit 1
else
if [[ -z $PID ]]; then
echo -e "\033[41;37m [错误] \033[0m Socat TCP 启动成功,但 UDP 启动失败 !"
# 系统判断
check_sys
if [[ ${release} == "debian" ]]; then
sed -i '$d' /etc/rc.local
echo -e "nohup socat TCP4-LISTEN:${Socatport},reuseaddr,fork TCP4:${socatip}:${Socatport1} >> ${socat_log_file} 2>&1 &" >> /etc/rc.local
echo -e "exit 0" >> /etc/rc.local
else
echo -e "nohup socat TCP4-LISTEN:${Socatport},reuseaddr,fork TCP4:${socatip}:${Socatport1} >> ${socat_log_file} 2>&1 &" >> /etc/rc.local
fi
exit 1
iptables -I INPUT -p tcp --dport ${Socatport} -j ACCEPT
fi
# 系统判断
check_sys
if [[ ${release} == "debian" ]]; then
sed -i '$d' /etc/rc.local
echo -e "nohup socat TCP4-LISTEN:${Socatport},reuseaddr,fork TCP4:${socatip}:${Socatport1} >> ${socat_log_file} 2>&1 &" >> /etc/rc.local
echo -e "nohup socat UDP4-LISTEN:${Socatport},reuseaddr,fork UDP4:${socatip}:${Socatport1} >> ${socat_log_file} 2>&1 &" >> /etc/rc.local
echo -e "exit 0" >> /etc/rc.local
else
echo -e "nohup socat TCP4-LISTEN:${Socatport},reuseaddr,fork TCP4:${socatip}:${Socatport1} >> ${socat_log_file} 2>&1 &" >> /etc/rc.local
echo -e "nohup socat UDP4-LISTEN:${Socatport},reuseaddr,fork UDP4:${socatip}:${Socatport1} >> ${socat_log_file} 2>&1 &" >> /etc/rc.local
fi
iptables -I INPUT -p tcp --dport ${Socatport} -j ACCEPT
iptables -I INPUT -p udp --dport ${Socatport} -j ACCEPT
fi
fi
# 获取IP
ip=`curl -m 10 -s http://members.3322.org/dyndns/getip`
if [[ -z $ip ]]; then
ip="ip"
fi
clear
echo
echo "——————————————————————————————"
echo " Socat 已启动 !"
echo
echo -e " 本地 IP : \033[41;37m ${ip} \033[0m"
echo -e " 本地监听端口 : \033[41;37m ${Socatport} \033[0m"
echo
echo -e " 欲转发 IP : \033[41;37m ${socatip} \033[0m"
echo -e " 欲转发端口 : \033[41;37m ${Socatport1} \033[0m"
echo -e " 转发类型 : \033[41;37m ${socattype} \033[0m"
echo "——————————————————————————————"
echo
}
# 查看Socat列表
listSocat(){
# 检查是否安装
check_socat
socat_total=`ps -ef | grep socat | grep -v grep | grep -v "socat.sh" | wc -l`
if [[ ${socat_total} = "0" ]]; then
echo -e "\033[41;37m [错误] \033[0m 没有发现 Socat 进程运行,请检查 !"
exit 1
fi
socat_list_all=""
for((integer = 1; integer <= ${socat_total}; integer++))
do
socat_type=`ps -ef | grep socat | grep -v grep | grep -v "socat.sh" | awk '{print $9}' | sed -n "${integer}p" | cut -c 1-4`
socat_listen=`ps -ef | grep socat | grep -v grep | grep -v "socat.sh" | awk '{print $9}' | sed -n "${integer}p" | perl -e 'while($_=<>){ /LISTEN:(.*),reuseaddr/; print $1;}'`
socat_fork=`ps -ef | grep socat | grep -v grep | grep -v "socat.sh" | awk '{print $10}' | sed -n "${integer}p" | cut -c 6-26`
socat_pid=`ps -ef | grep socat | grep -v grep | grep -v "socat.sh" | awk '{print $2}' | sed -n "${integer}p"`
socat_list_all=${socat_list_all}${integer}". 进程PID: "${socat_pid}" 类型: "${socat_type}" 监听端口: "${socat_listen}" 转发IP和端口: "${socat_fork}"\n"
done
echo
echo -e "当前有 \033[42;37m "${socat_total}" \033[0m 个Socat转发进程。"
echo -e ${socat_list_all}
}
delSocat(){
# 检查是否安装
check_socat
# 判断进程是否存在
PID=`ps -ef | grep socat | grep -v grep | grep -v "socat.sh" | awk '{print $2}'`
if [[ -z $PID ]]; then
echo -e "\033[41;37m [错误] \033[0m 没有发现 Socat 进程运行,请检查 !"
exit 1
fi
while true
do
# 列出 Socat
listSocat
read -p "请输入数字 来选择要终止的 Socat 进程:" stopsocat
[[ -z "${stopsocat}" ]] && stopsocat="0"
expr ${stopsocat} + 0 &>/dev/null
if [[ $? -eq 0 ]]; then
if [[ ${stopsocat} -ge 1 ]] && [[ ${stopsocat} -le ${socat_total} ]]; then
# 删除开机启动
socat_del_rc1=`ps -ef | grep socat | grep -v grep | grep -v "socat.sh" | awk '{print $8}' | sed -n "${stopsocat}p"`
socat_del_rc2=`ps -ef | grep socat | grep -v grep | grep -v "socat.sh" | awk '{print $9}' | sed -n "${stopsocat}p"`
socat_del_rc3=`ps -ef | grep socat | grep -v grep | grep -v "socat.sh" | awk '{print $10}' | sed -n "${stopsocat}p"`
socat_del_rc4=${socat_del_rc1}" "${socat_del_rc2}" "${socat_del_rc3}
#echo ${socat_del_rc4}
sed -i "/${socat_del_rc4}/d" /etc/rc.local
# 删除防火墙规则
socat_listen=`ps -ef | grep socat | grep -v grep | grep -v "socat.sh" | awk '{print $9}' | sed -n "${stopsocat}p" | perl -e 'while($_=<>){ /LISTEN:(.*),reuseaddr/; print $1;}'`
socat_type=`ps -ef | grep socat | grep -v grep | grep -v "socat.sh" | awk '{print $9}' | sed -n "${stopsocat}p" | cut -c 1-4`
if [[ ${socat_type} = "TCP4" ]]; then
iptables -D INPUT -p tcp --dport ${socat_listen} -j ACCEPT
else
iptables -D INPUT -p udp --dport ${socat_listen} -j ACCEPT
fi
socat_total=`ps -ef | grep socat | grep -v grep | grep -v "socat.sh" | wc -l`
PID=`ps -ef | grep socat | grep -v grep | grep -v "socat.sh" | awk '{print $2}' | sed -n "${stopsocat}p"`
kill -2 ${PID}
sleep 2s
socat_total1=$[ $socat_total - 1 ]
socat_total=`ps -ef | grep socat | grep -v grep | grep -v "socat.sh" | wc -l`
if [[ ${socat_total} != ${socat_total1} ]]; then
echo -e "\033[41;37m [错误] \033[0m Socat 停止失败 !"
exit 1
else
echo
echo " Socat 已停止 !"
echo
fi
break
else
echo -e "\033[41;37m [错误] \033[0m 请输入正确的数字 !"
fi
else
echo "取消..."
exit 1
fi
done
}
# 查看日志
tailSocat(){
# 判断日志是否存在
if [[ ! -e ${socat_log_file} ]]; then
echo -e "\033[41;37m [错误] \033[0m Socat 日志文件不存在 !"
exit 1
else
tail -f ${socat_log_file}
fi
}
uninstallSocat(){
# 检查是否安装
check_socat
printf "确定要卸载 Socat ? (y/N)"
printf "\n"
read -p "(默认: n):" unyn
[[ -z ${unyn} ]] && unyn="n"
if [[ ${unyn} == [Yy] ]]; then
check_sys
# 系统判断
if [[ ${release} == "centos" ]]; then
yum remove socat -y
else
sudo apt-get remove socat -y
sudo apt-get autoremove
fi
rm -rf ${socat_file}
socat_exist=`socat -h`
if [[ ${socat_exist} != "" ]]; then
echo -e "\033[41;37m [错误] \033[0m Socat卸载失败,请检查 !"
exit 1
fi
echo
echo " Socat 已卸载 !"
echo
else
echo
echo "卸载已取消..."
echo
fi
}
action=$1
[[ -z $1 ]] && action=install
case "$action" in
install|add|del|list|tail|uninstall)
${action}Socat
;;
*)
echo "输入错误 !"
echo "用法: {install | add | del | list | tail | uninstall}"
;;
esac