-
Notifications
You must be signed in to change notification settings - Fork 3
/
udptools-lowdelay.sh
620 lines (595 loc) · 14.4 KB
/
udptools-lowdelay.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#判断系统
#useradd -M -s /sbin/nologin -d /usr/local/udptools udptools
if [[ -e /etc/debian_version ]]; then
OS=debian
GROUPNAME=nogroup
RCLOCAL='/etc/rc.local'
IPTABLES=`dpkg --get-selections | grep iptables`
elif [[ -e /etc/centos-release || -e /etc/redhat-release ]]; then
OS=centos
GROUPNAME=nobody
RCLOCAL='/etc/rc.d/rc.local'
IPTABLES=`rpm -qa |grep iptables`
else
echo "只支持Debian\Ubuntu\CentOS系统"
exit
fi
#检查依赖IPTABLES
if [ "$IPTABLES" == '' ]; then
if [[ "$OS" = 'debian' ]]; then
apt-get update
apt-get install iptables -y
else
yum install iptables -y
fi
fi
#检查目录
if [ ! -d "/usr/local/udptools" ]; then
echo "未安装udptools,请先安装."
exit
fi
if [ ! -d "/usr/local/udptools/pid" ]; then
mkdir /usr/local/udptools/pid
fi
if [ ! -d "/usr/local/udptools/log" ]; then
mkdir /usr/local/udptools/log
fi
if [ ! -d "/usr/local/udptools/conf" ]; then
mkdir /usr/local/udptools/conf
fi
build_Udp2raw_Server()
{
#写入Udp2Raw配置
cat > /usr/local/udptools/conf/udp2raw-s${MPORT}.conf <<EOF
-s
# 服务器模式
-l 0.0.0.0:$LPORT
# 监听端口给UDP2RAW客户端
-r 127.0.0.1:$MPORT
# 连接UDPSpeeder端口
-k $PASSWD
# 密码
--cipher-mode xor
# 简单xor加密
--fix-gro
# 修复粘包
EOF
#写入Udp2Raw脚本
cat > /usr/local/udptools/udp2raw-s${MPORT}.sh <<EOF
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#进程名
PROG=Udp2Raw-Server-${MPORT}
#BIN路径
BIN_FILE=/usr/local/udptools/bin/udp2raw
#配置路径
CONFIG_FILE=/usr/local/udptools/conf/udp2raw-s${MPORT}.conf
#日志路径
LOG_FILE=/usr/local/udptools/log/udp2raw-s${MPORT}.log
#PID路径
PID_FILE=/usr/local/udptools/pid/udp2raw-s${MPORT}.pid
EOF
cat >> /usr/local/udptools/udp2raw-s${MPORT}.sh <<"EOF"
checkSet(){
#获取监听端口
SERVER_PORT=`cat $CONFIG_FILE | grep '\-l ' | awk -F ":" '{print $2}'`
#检查iptables规则
IPTALBES=`iptables -nvL | grep DROP | grep tcp | grep $SERVER_PORT`
echo "IPTABLES_DEBUG" >> $LOG_FILE
echo $IPTALBES >> $LOG_FILE
if [ ! -n "$IPTALBES" ]; then
echo "Adding iptables rules."
#添加iptables规则
RULES=`$BIN_FILE --conf-file $CONFIG_FILE -g | grep iptables |grep -v rule`
$RULES
fi
#赋权
setcap cap_net_raw+ep $BIN_FILE
}
status(){
PID=`ps aux|grep $CONFIG_FILE|grep -v sudo|grep -v grep | awk '{print $2}'`
if [ ! -n "$PID" ]; then
rm -f $PID_FILE
echo "$PROG已停止."
else
echo $PID > $PID_FILE
echo "$PROG已启动. PID: $PID"
fi
}
start(){
#启动进程
nohup $BIN_FILE --keep-rule --conf-file $CONFIG_FILE >> $LOG_FILE 2>&1 &
#checkSet
#sudo -u nobody -b $BIN_FILE --conf-file $CONFIG_FILE >> $LOG_FILE 2>&1
#Centos8无法nobody运行
status
}
stop(){
#结束进程
status > /dev/null 2>&1
PID=`cat $PID_FILE`
kill $PID >/dev/null 2>&1
sleep 1
status
}
showLog(){
cat $LOG_FILE | tail -n 50
}
case "$1" in
start)
echo "Starting $PROG..."
start
;;
stop)
echo "Stopping $PROG..."
stop
;;
restart)
echo "Stopping $PROG..."
stop
sleep 2
echo "Starting $PROG..."
start
;;
status)
status
;;
log)
showLog
;;
*)
echo "Usage: $PROG {start|stop|restart|status|log}"
;;
esac
exit 0
EOF
chmod +x /usr/local/udptools/udp2raw-s${MPORT}.sh
#判断服务模式
if pgrep systemd-journal > /dev/null; then
if [ ! -f "/usr/lib/systemd/system/[email protected]" ]; then
cp ./systemctl/service/[email protected] /usr/lib/systemd/system/
fi
systemctl enable udp2raw-server@${MPORT}.service
systemctl start udp2raw-server@${MPORT}.service
else
cat > /etc/init.d/udp2raw-s${MPORT} <<EOF
#!/bin/bash
# startup script for the Udp2Raw Server
# chkconfig: 345 80 20
# description: start the Udp2Raw deamon
#
# Source function library
. /etc/rc.d/init.d/functions
prog=Udp2Raw-Server-${MPORT}
/usr/local/udptools/udp2raw-s${MPORT}.sh \$1
EOF
chmod +x /etc/init.d/udp2raw-s${MPORT}
chkconfig --add udp2raw-s${MPORT}
chkconfig udp2raw-s${MPORT} on
service udp2raw-s${MPORT} start
fi
}
build_UdpSpeeder_Server()
{
#写入UdpSpeeder脚本
cat > /usr/local/udptools/udpspeeder-s${MPORT}.sh <<EOF
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#进程名
PROG=UDPspeeder-Server-${MPORT}
#BIN路径
BIN_FILE=/usr/local/udptools/bin/udpspeeder
#配置参数
CONFIG="-s -l 127.0.0.1:${MPORT} -r 127.0.0.1:${RPORT} --mode 0 -f2:1 --timeout 1"
#-l 127.0.0.1:${MPORT}监听端口给udp2raw用
#-r 127.0.0.1:${RPORT}连接原始服务端口
#日志路径
LOG_FILE=/usr/local/udptools/log/udpspeeder-s${MPORT}.log
#PID路径
PID_FILE=/usr/local/udptools/pid/udpspeeder-s${MPORT}.pid
#PS查找进程用的关键词
PSKEY=":${MPORT}"
EOF
cat >> /usr/local/udptools/udpspeeder-s${MPORT}.sh <<"EOF"
status(){
PID=`ps aux|grep udpspeeder|grep "$PSKEY"|grep -v sudo|grep -v grep | awk '{print $2}'`
if [ ! -n "$PID" ]; then
rm -f $PID_FILE
echo "$PROG已停止."
else
echo $PID > $PID_FILE
echo "$PROG已启动. PID: $PID"
fi
}
start(){
#启动进程
#sudo -u nobody -b $BIN_FILE $CONFIG >> $LOG_FILE 2>&1
nohup $BIN_FILE $CONFIG >> $LOG_FILE 2>&1 &
status
}
stop(){
#结束进程
status > /dev/null 2>&1
PID=`cat $PID_FILE`
kill $PID >/dev/null 2>&1
sleep 1
status
}
showLog(){
cat $LOG_FILE | tail -n 50
}
case "$1" in
start)
echo "Starting $PROG..."
start
;;
stop)
echo "Stopping $PROG..."
stop
;;
restart)
echo "Stopping $PROG..."
stop
sleep 2
echo "Starting $PROG..."
start
;;
status)
status
;;
log)
showLog
;;
*)
echo "Usage: $PROG {start|stop|restart|status|log}"
;;
esac
exit 0
EOF
chmod +x /usr/local/udptools/udpspeeder-s${MPORT}.sh
#判断服务模式
if pgrep systemd-journal > /dev/null; then
if [ ! -f "/usr/lib/systemd/system/[email protected]" ]; then
cp ./systemctl/service/[email protected] /usr/lib/systemd/system/
fi
systemctl enable udpspeeder-server@${MPORT}.service
systemctl start udpspeeder-server@${MPORT}.service
else
cat > /etc/init.d/udpspeeder-s${MPORT} <<EOF
#!/bin/bash
# startup script for the UDPspeeder Server
# chkconfig: 345 80 20
# description: start the UDPspeeder deamon
#
# Source function library
. /etc/rc.d/init.d/functions
prog=UDPspeeder-Server-${MPORT}
/usr/local/udptools/udpspeeder-s${MPORT}.sh \$1
EOF
chmod +x /etc/init.d/udpspeeder-s${MPORT}
chkconfig --add udpspeeder-s${MPORT}
chkconfig udpspeeder-s${MPORT} on
service udpspeeder-s${MPORT} start
fi
}
build_Udp2raw_Client()
{
#写入Udp2Raw配置
cat > /usr/local/udptools/conf/udp2raw-c${MPORT}.conf <<EOF
-c
# 客户端模式
-l 127.0.0.1:$MPORT
# 监听端口给UdpSpeeder用
-r $REMOTEIP:$RPORT
# 连接UDP2RAW服务端
-k $PASSWD
# 密码
--cipher-mode xor
# 简单xor加密
--fix-gro
# 修复粘包
EOF
#写入Udp2Raw脚本
cat > /usr/local/udptools/udp2raw-c${MPORT}.sh <<EOF
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#进程名
PROG=Udp2Raw-Client-${MPORT}
#BIN路径
BIN_FILE=/usr/local/udptools/bin/udp2raw
#配置路径
CONFIG_FILE=/usr/local/udptools/conf/udp2raw-c${MPORT}.conf
#日志路径
LOG_FILE=/usr/local/udptools/log/udp2raw-c${MPORT}.log
#PID路径
PID_FILE=/usr/local/udptools/pid/udp2raw-c${MPORT}.pid
EOF
cat >> /usr/local/udptools/udp2raw-c${MPORT}.sh <<"EOF"
checkSet(){
#获取监听端口
SERVER_PORT=`cat $CONFIG_FILE | grep '\-r ' | awk -F ":" '{print $2}'`
SERVER_ADDRESS=`cat $CONFIG_FILE | grep '\-r ' | awk -F ":" '{print $1}' | awk '{print $2}'`
#检查iptables规则
IPTALBES=`iptables -nvL | grep DROP | grep tcp | grep $SERVER_PORT | grep $SERVER_ADDRESS`
echo "IPTABLES_DEBUG" >> $LOG_FILE
echo $IPTALBES >> $LOG_FILE
if [ ! -n "$IPTALBES" ]; then
echo "Adding iptables rules."
#添加iptables规则
RULES=`$BIN_FILE --conf-file $CONFIG_FILE -g | grep iptables |grep -v rule`
$RULES
fi
#赋权
setcap cap_net_raw+ep $BIN_FILE
}
status(){
PID=`ps aux|grep $CONFIG_FILE|grep -v sudo|grep -v grep | awk '{print $2}'`
if [ ! -n "$PID" ]; then
rm -f $PID_FILE
echo "$PROG已停止."
else
echo $PID > $PID_FILE
echo "$PROG已启动. PID: $PID"
fi
}
start(){
#启动进程
nohup $BIN_FILE --keep-rule --conf-file $CONFIG_FILE >> $LOG_FILE 2>&1 &
#checkSet
#sudo -u nobody -b $BIN_FILE --conf-file $CONFIG_FILE >> $LOG_FILE 2>&1
#Centos8无法nobody运行
status
}
stop(){
#结束进程
status > /dev/null 2>&1
PID=`cat $PID_FILE`
kill $PID >/dev/null 2>&1
sleep 1
status
}
showLog(){
cat $LOG_FILE | tail -n 50
}
case "$1" in
start)
echo "Starting $PROG..."
start
;;
stop)
echo "Stopping $PROG..."
stop
;;
restart)
echo "Stopping $PROG..."
stop
sleep 2
echo "Starting $PROG..."
start
;;
status)
status
;;
log)
showLog
;;
*)
echo "Usage: $PROG {start|stop|restart|status|log}"
;;
esac
exit 0
EOF
chmod +x /usr/local/udptools/udp2raw-c${MPORT}.sh
#判断服务模式
if pgrep systemd-journal > /dev/null; then
if [ ! -f "/usr/lib/systemd/system/[email protected]" ]; then
cp ./systemctl/service/[email protected] /usr/lib/systemd/system/
fi
systemctl enable udp2raw-client@${MPORT}.service
systemctl start udp2raw-client@${MPORT}.service
else
cat > /etc/init.d/udp2raw-c${MPORT} <<EOF
#!/bin/bash
# startup script for the Udp2Raw Client
# chkconfig: 345 80 20
# description: start the Udp2Raw deamon
#
# Source function library
. /etc/rc.d/init.d/functions
prog=Udp2Raw-Client-${MPORT}
/usr/local/udptools/udp2raw-c${MPORT}.sh \$1
EOF
chmod +x /etc/init.d/udp2raw-c${MPORT}
chkconfig --add udp2raw-c${MPORT}
chkconfig udp2raw-c${MPORT} on
service udp2raw-c${MPORT} start
fi
}
build_UdpSpeeder_Client()
{
#写入UdpSpeeder脚本
cat > /usr/local/udptools/udpspeeder-c${MPORT}.sh <<EOF
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#进程名
PROG=UDPspeeder-Client-${MPORT}
#BIN路径
BIN_FILE=/usr/local/udptools/bin/udpspeeder
#配置参数
CONFIG="-c -l 0.0.0.0:${LPORT} -r 127.0.0.1:${MPORT} --mode 0 -f2:1 --timeout 1"
#-l 0.0.0.0:${LPORT}监听端口给其他程序用
#-r 127.0.0.1:${MPORT}连接Udp2Raw
#日志路径
LOG_FILE=/usr/local/udptools/log/udpspeeder-c${MPORT}.log
#PID路径
PID_FILE=/usr/local/udptools/pid/udpspeeder-c${MPORT}.pid
#PS查找进程用的关键词
PSKEY=":${MPORT}"
EOF
cat >> /usr/local/udptools/udpspeeder-c${MPORT}.sh <<"EOF"
status(){
PID=`ps aux|grep udpspeeder|grep "$PSKEY"|grep -v sudo|grep -v grep | awk '{print $2}'`
if [ ! -n "$PID" ]; then
rm -f $PID_FILE
echo "$PROG已停止."
else
echo $PID > $PID_FILE
echo "$PROG已启动. PID: $PID"
fi
}
start(){
#启动进程
#sudo -u nobody -b $BIN_FILE $CONFIG >> $LOG_FILE 2>&1
nohup $BIN_FILE $CONFIG >> $LOG_FILE 2>&1 &
status
}
stop(){
#结束进程
status > /dev/null 2>&1
PID=`cat $PID_FILE`
kill $PID >/dev/null 2>&1
sleep 1
status
}
showLog(){
cat $LOG_FILE | tail -n 50
}
case "$1" in
start)
echo "Starting $PROG..."
start
;;
stop)
echo "Stopping $PROG..."
stop
;;
restart)
echo "Stopping $PROG..."
stop
sleep 2
echo "Starting $PROG..."
start
;;
status)
status
;;
log)
showLog
;;
*)
echo "Usage: $PROG {start|stop|restart|status|log}"
;;
esac
exit 0
EOF
chmod +x /usr/local/udptools/udpspeeder-c${MPORT}.sh
#判断服务模式
if pgrep systemd-journal > /dev/null; then
if [ ! -f "/usr/lib/systemd/system/[email protected]" ]; then
cp ./systemctl/service/[email protected] /usr/lib/systemd/system/
fi
systemctl enable udpspeeder-client@${MPORT}.service
systemctl start udpspeeder-client@${MPORT}.service
else
cat > /etc/init.d/udpspeeder-c${MPORT} <<EOF
#!/bin/bash
# startup script for the UDPspeeder Client
# chkconfig: 345 80 20
# description: start the UDPspeeder deamon
#
# Source function library
. /etc/rc.d/init.d/functions
prog=UDPspeeder-Client-${MPORT}
/usr/local/udptools/udpspeeder-c${MPORT}.sh \$1
EOF
chmod +x /etc/init.d/udpspeeder-c${MPORT}
chkconfig --add udpspeeder-c${MPORT}
chkconfig udpspeeder-c${MPORT} on
service udpspeeder-c${MPORT} start
fi
}
echo
echo "Which mode to run this?"
echo "你想运行在什么模式?"
echo " 1) Client(Default) - 客户端(默认)"
echo " 2) Server - 服务端"
read -p "Run Mode(运行模式) [1-2]: " -e -i 1 RUNMODE
case $RUNMODE in
1)
read -p "Udp2Raw Password(Udp2Raw密码): " PASSWD
echo""
read -p "Remote Udp2Raw IP(Udp2Raw远程IP): " REMOTEIP
echo""
while [[ ! "$RPORT" =~ [1-65535] ]];do
read -p "Remote Udp2Raw Port(Udp2Raw远程端口): " RPORT
echo""
done
while [[ ! "$MPORT" =~ [1-65535] ]];do
read -p "Middle Port(Udp2Raw与UDPSpeeder中间端口): " MPORT
CHECKMPORT=`netstat -nul | grep $MPORT`
if [[ "$CHECKMPORT" != "" ]]; then
MPORT=0
echo "端口已被占用";
fi
echo""
done
while [[ ! "$LPORT" =~ [1-65535] ]];do
read -p "Listen Port(监听给本地其他业务用端口): " LPORT
CHECKLPORT=`netstat -nul | grep $LPORT`
if [[ "$CHECKLPORT" != "" ]]; then
LPORT=0
echo "端口已被占用";
fi
if [ "$LPORT" == "$MPORT" ]; then
LPORT=0
echo "端口已被占用";
fi
echo""
done
build_Udp2raw_Client
build_UdpSpeeder_Client
;;
2)
read -p "Udp2Raw Password(Udp2Raw密码): " PASSWD
echo""
while [[ ! "$RPORT" =~ [1-65535] ]];do
read -p "Other Server Port(本地其他业务端口): " RPORT
echo""
done
while [[ ! "$MPORT" =~ [1-65535] ]];do
read -p "Middle Port(Udp2Raw与UDPSpeeder中间端口): " MPORT
CHECKMPORT=`netstat -nul | grep $MPORT`
if [[ "$CHECKMPORT" != "" ]]; then
MPORT=0
echo "端口已被占用";
fi
if [ "$MPORT" == "$RPORT" ]; then
MPORT=0
echo "端口已被占用";
fi
echo""
done
while [[ ! "$LPORT" =~ [1-65535] ]];do
read -p "Udp2Raw Listen Port(监听给远程UDP2RAW用): " LPORT
CHECKLPORT=`netstat -ntl | grep $LPORT`
if [[ "$CHECKLPORT" != "" ]]; then
LPORT=0
echo "端口已被占用";
fi
if [ "$LPORT" == "$MPORT" ]; then
LPORT=0
echo "端口已被占用";
fi
echo""
done
build_Udp2raw_Server
build_UdpSpeeder_Server
;;
esac