forked from hellofwy/ss-bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssadmin.sh
executable file
·693 lines (665 loc) · 15.3 KB
/
ssadmin.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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
#!/bin/bash
# Copyright (c) 2014 hellofwy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
. $DIR/sslib.sh
usage () {
cat $DIR/sshelp
}
wrong_para_prompt() {
echo "参数输入错误!"
echo "查看帮助:ssadmin.sh -h"
}
#根据用户文件生成ssserver配置文件
create_json () {
echo '{' > $JSON_FILE.tmp
sed -E 's/(.*)/ \1/' $DIR/ssmlt.template >> $JSON_FILE.tmp
awk '
BEGIN {
i=1;
printf(" \"port_password\": {\n");
}
! /^#|^\s*$/ {
port=$1;
pw=$2;
ports[i++] = port;
pass[port]=pw;
}
END {
for(j=1;j<i;j++) {
port=ports[j];
printf(" \"%s\": \"%s\"", port, pass[port]);
if(j<i-1) printf(",");
printf("\n");
}
printf(" }\n");
}
' $USER_FILE >> $JSON_FILE.tmp
echo '}' >> $JSON_FILE.tmp
mv $JSON_FILE.tmp $JSON_FILE
}
run_ssserver () {
$SSSERVER -qq -c $JSON_FILE 2>/dev/null >/dev/null &
echo $! > $SSSERVER_PID
}
check_ssserver () {
if [ -e $SSSERVER_PID ]; then
ps $(cat $SSSERVER_PID) 2>/dev/null | grep $SSSERVER_NAME 2>/dev/null
return $?
else
return 1
fi
}
check_sscounter () {
if [ -e $SSCOUNTER_PID ]; then
ps $(cat $SSCOUNTER_PID) 2>/dev/null | grep sscounter 2>/dev/null
return $?
else
return 1
fi
}
start_ss () {
if [ ! -e $USER_FILE ]; then
echo "还没有用户,请先添加一个用户"
return 1
fi
if [ -e $SSSERVER_PID ]; then
if check_ssserver; then
echo 'ss服务已启动,同一实例不能启动多次!'
return 1
else
rm $SSSERVER_PID
fi
fi
create_json
if [ -e $SSCOUNTER_PID ]; then
if check_sscounter ; then
kill `cat $SSCOUNTER_PID`
else
rm $SSCOUNTER_PID
fi
fi
echo 'sscounter.sh启动中...'
( $DIR/sscounter.sh ) &
echo $! > $SSCOUNTER_PID
if check_sscounter; then
echo 'sscounter.sh已启动'
else
echo 'sscounter.sh启动失败'
return 1
fi
echo 'ssserver启动中...'
run_ssserver
sleep 1
if check_ssserver; then
echo 'ssserver已启动'
else
echo 'ssserver启动失败'
return 1
fi
}
stop_ss () {
if check_ssserver; then
kill `cat $SSSERVER_PID`
rm $SSSERVER_PID
del_ipt_chains 2> /dev/null
echo 'ssserver已关闭'
else
echo 'ssserver未启动'
fi
if check_sscounter; then
kill `cat $SSCOUNTER_PID`
rm $SSCOUNTER_PID
echo 'sscounter.sh已关闭'
else
echo 'sscounter.sh未启动'
fi
}
restart_ss () {
stop_ss
start_ss
}
soft_restart_ss () {
if check_ssserver; then
kill -9 `cat $SSSERVER_PID`
echo 'ssserver已关闭'
kill `cat $SSCOUNTER_PID`
echo 'sscounter.sh已关闭'
rm $SSSERVER_PID $SSCOUNTER_PID
del_ipt_chains 2> /dev/null
start_ss
else
echo 'ssserver未启动'
fi
}
status_ss () {
if check_ssserver; then
echo 'ssserver正在运行'
else
echo 'ssserver未启动'
fi
if check_sscounter; then
echo 'sscounter.sh正在运行'
else
echo 'sscounter.sh未启动'
fi
}
bytes2gb () {
TLIMIT=$1
echo "$TLIMIT" |
sed -E 's/[kK][bB]?/ * 1024/' |
sed -E 's/[mM][bB]?/ * 1024 * 1024/' |
sed -E 's/[gG][bB]?/ * 1024 * 1024 * 1024/' |
sed -E 's/[tT][bB]?/ * 1024 * 1024 * 1024 * 1024/' |
bc |
awk '{printf("%.0f", $1)}'
}
check_port_range () {
PORT=$1
if (( ($PORT > 0) && ($PORT <= 65535 ) )); then
return 0
else
return 1
fi
}
add_user () {
if [ "$#" -ne 3 ]; then
wrong_para_prompt;
return 1
fi
PORT=$1
if check_port_range $PORT; then
:
else
wrong_para_prompt;
return 1
fi
PWORD=$2
TLIMIT=$3
TLIMIT=`bytes2gb $TLIMIT`
if [ ! -e $USER_FILE ]; then
echo "\
# 以空格、制表符分隔
# 端口 密码 流量限制
# 2345 abcde 1000000" > $USER_FILE;
fi
cat $USER_FILE |
awk '
{
if($1=='$PORT') exit 1
}'
if [ $? -eq 0 ]; then
echo "\
$PORT $PWORD $TLIMIT" >> $USER_FILE;
else
echo "用户已存在!"
return 1
fi
# 重新生成配置文件,并加载
if [ -e $SSSERVER_PID ]; then
create_json
kill -9 `cat $SSSERVER_PID`
add_rules $PORT
run_ssserver
fi
# 更新流量记录文件
update_or_create_traffic_file_from_users
calc_remaining
}
del_user () {
if [ "$#" -ne 1 ]; then
wrong_para_prompt;
return 1
fi
PORT=$1
if check_port_range $PORT; then
:
else
wrong_para_prompt;
return 1
fi
if [ -e $USER_FILE ]; then
sed -i '/^\s*'$PORT'\s/ d' $USER_FILE
fi
# 重新生成配置文件,并加载
if [ -e $SSSERVER_PID ]; then
create_json
kill -9 `cat $SSSERVER_PID`
del_rules $PORT 2>/dev/null
del_reject_rules $PORT 2>/dev/null
run_ssserver
fi
# 更新流量记录文件
update_or_create_traffic_file_from_users
calc_remaining
}
change_user () {
if [ "$#" -ne 3 ]; then
wrong_para_prompt;
return 1
fi
PORT=$1
if check_port_range $PORT; then
:
else
wrong_para_prompt;
return 1
fi
PWORD=$2
TLIMIT=$3
TLIMIT=`bytes2gb $TLIMIT`
if [ ! -e $USER_FILE ]; then
echo "目前还无用户,请先添加用户"
return 1
fi
if grep -q "^\s*$PORT\s" $USER_FILE; then
cat $USER_FILE |
awk '
{
if($1=='$PORT') {
printf("'$PORT' '$PWORD' '$TLIMIT'\n");
} else {
print $0
}
}' > $USER_FILE.tmp;
mv $USER_FILE.tmp $USER_FILE
# 重新生成配置文件,并加载
if [ -e $SSSERVER_PID ]; then
create_json
kill -9 `cat $SSSERVER_PID`
add_rules $PORT
run_ssserver
fi
# 更新流量记录文件
update_or_create_traffic_file_from_users
calc_remaining
else
echo "此用户不存在!"
return 1
fi
}
change_passwd () {
if [ "$#" -ne 2 ]; then
wrong_para_prompt;
return 1
fi
PORT=$1
if check_port_range $PORT; then
:
else
wrong_para_prompt;
return 1
fi
PWORD=$2
if [ ! -e $USER_FILE ]; then
echo "目前还无用户,请先添加用户"
return 1
fi
if grep -q "^\s*$PORT\s" $USER_FILE; then
cat $USER_FILE |
awk '
{
if($1=='$PORT') {
printf("'$PORT' '$PWORD' %s\n", $3);
} else {
print $0
}
}' > $USER_FILE.tmp;
mv $USER_FILE.tmp $USER_FILE
# 重新生成配置文件,并加载
if [ -e $SSSERVER_PID ]; then
create_json
kill -9 `cat $SSSERVER_PID`
add_rules $PORT
run_ssserver
fi
# 更新流量记录文件
update_or_create_traffic_file_from_users
calc_remaining
else
echo "此用户不存在!"
return 1
fi
}
change_limit () {
if [ "$#" -ne 2 ]; then
wrong_para_prompt;
return 1
fi
PORT=$1
if check_port_range $PORT; then
:
else
wrong_para_prompt;
return 1
fi
TLIMIT=$2
TLIMIT=`bytes2gb $TLIMIT`
if [ ! -e $USER_FILE ]; then
echo "目前还无用户,请先添加用户"
return 1
fi
if grep -q "^\s*$PORT\s" $USER_FILE; then
cat $USER_FILE |
awk '
{
if($1=='$PORT') {
printf("'$PORT' %s '$TLIMIT'\n", $2);
} else {
print $0
}
}' > $USER_FILE.tmp;
mv $USER_FILE.tmp $USER_FILE
# 更新流量记录文件
update_or_create_traffic_file_from_users
calc_remaining
else
echo "此用户不存在!"
return 1
fi
}
change_all_limit () {
if [ "$#" -ne 1 ]; then
wrong_para_prompt;
return 1
fi
TLIMIT=$1
TLIMIT=`bytes2gb $TLIMIT`
if [ ! -e $USER_FILE ]; then
echo "目前还无用户,请先添加用户"
return 1
fi
cat $USER_FILE |
awk '
{
if($0 !~ /^#/ && $0 !~ /^\s.*$/) {
printf("%s %s '$TLIMIT'\n", $1, $2);
} else {
print $0
}
}' > $USER_FILE.tmp;
mv $USER_FILE.tmp $USER_FILE
# 更新流量记录文件
update_or_create_traffic_file_from_users
calc_remaining
}
show_user () {
if [ $# -eq 0 ]; then
cat $TRAFFIC_FILE;
else
if [ "$#" -ne 1 ]; then
wrong_para_prompt;
return 1
fi
PORT=$1
if check_port_range $PORT; then
:
else
wrong_para_prompt;
return 1
fi
res=`grep "^\s*$PORT\s" $TRAFFIC_FILE`
if [ -z "$res" ]; then
echo "此用户不存在!"
else
head -n1 $TRAFFIC_FILE
echo "$res"
fi
fi
}
show_passwd () {
if [ $# -eq 0 ]; then
cat $USER_FILE;
else
if [ "$#" -ne 1 ]; then
wrong_para_prompt;
return 1
fi
PORT=$1
if check_port_range $PORT; then
:
else
wrong_para_prompt;
return 1
fi
res=`grep "^\s*$PORT\s" $USER_FILE`
if [ -z "$res" ]; then
echo "此用户不存在!"
else
head -n2 $USER_FILE
echo "$res"
fi
fi
}
reset_limit () {
if [ ! -e $USER_FILE ]; then
echo "目前还无用户,请先添加用户"
return 1
fi
if [ $# -eq 0 ]; then
cat $USER_FILE |
awk '
{
if($0 !~ /^#/ && $0 !~ /^\s.*$/) {
printf("%s %s 0\n", $1, $2);
} else {
print $0
}
}' > $USER_FILE.tmp;
mv $USER_FILE.tmp $USER_FILE
# 更新流量记录文件
update_or_create_traffic_file_from_users
calc_remaining
else
if [ "$#" -ne 1 ]; then
wrong_para_prompt;
return 1
fi
PORT=$1
if check_port_range $PORT; then
:
else
wrong_para_prompt;
return 1
fi
if grep -q "^\s*$PORT\s" $USER_FILE; then
cat $USER_FILE |
awk '
{
if($1=='$PORT') {
printf("'$PORT' %s 0\n", $2);
} else {
print $0
}
}' > $USER_FILE.tmp;
mv $USER_FILE.tmp $USER_FILE
# 更新流量记录文件
update_or_create_traffic_file_from_users
calc_remaining
else
echo "此用户不存在!"
return 1
fi
fi
}
reset_used () {
if [ ! -e $USER_FILE ]; then
echo "目前还无用户,请先添加用户"
return 1
fi
while [ -e $TRAFFIC_LOG.lock ]; do
sleep 1
done
touch $TRAFFIC_LOG.lock
if [ $# -eq 0 ]; then
cat $TRAFFIC_LOG |
awk '
{
if($0 !~ /^#/ && $0 !~ /^\s.*$/) {
printf("%-5d\t0\n", $1);
} else {
print $0
}
}' > $TRAFFIC_LOG.tmp;
mv $TRAFFIC_LOG.tmp $TRAFFIC_LOG
else
if [ "$#" -ne 1 ]; then
wrong_para_prompt;
return 1
fi
PORT=$1
if check_port_range $PORT; then
:
else
wrong_para_prompt;
return 1
fi
if grep -q "^\s*$PORT\s" $USER_FILE; then
cat $TRAFFIC_LOG |
awk '
{
if($1=='$PORT') {
printf("%-5d\t0\n", '$PORT');
} else {
print $0
}
}' > $TRAFFIC_LOG.tmp;
mv $TRAFFIC_LOG.tmp $TRAFFIC_LOG
else
echo "此用户不存在!"
rm $TRAFFIC_LOG.lock
return 1
fi
fi
rm $TRAFFIC_LOG.lock
# 更新流量记录文件
calc_remaining
}
if [ "$#" -eq 0 ]; then
usage
exit 0
fi
case $1 in
-h|h|help )
usage
exit 0;
;;
-v|v|version )
echo 'ss-bash Version 1.0-beta.3, 2014-12-3, Copyright (c) 2014 hellofwy'
exit 0;
;;
esac
if [ "$EUID" -ne 0 ]; then
echo "必需以root身份运行,请使用sudo等命令"
exit 1;
fi
if type $SSSERVER 2>&1 >/dev/null; then
:
else
echo "无法找到ssserver程序,请在sslib.sh中指定其路径"
exit 1;
fi
case $1 in
add )
shift
add_user $1 $2 $3
;;
del )
shift
del_user $1
;;
show )
shift
show_user $1
;;
showpw )
shift
show_passwd $1
;;
change )
shift
change_user $1 $2 $3
;;
cpw )
shift
change_passwd $1 $2
;;
clim )
shift
change_limit $1 $2
;;
rlim )
shift
if [ $# -eq 0 ]; then
echo "请指定用户端口号"
exit 1
else
reset_limit $1
fi
;;
change_all_limit )
shift
change_all_limit $1
;;
reset_all_limit )
shift
reset_limit
;;
rused )
shift
if [ $# -eq 0 ]; then
echo "请指定用户端口号"
exit 1
else
reset_used $1
fi
;;
reset_all_used )
shift
reset_used
;;
start )
start_ss
;;
stop )
stop_ss
;;
restart )
restart_ss
;;
status )
status_ss
;;
soft_restart )
soft_restart_ss
;;
lrules )
list_rules
;;
* )
usage
;;
esac