This repository has been archived by the owner on Oct 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
mtprotoproxy-go.sh
2380 lines (2348 loc) · 73.6 KB
/
mtprotoproxy-go.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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /bin/bash
######################################################
# Anything wrong? Contact me via telegram: @CN_SZTL. #
######################################################
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
function set_fonts_colors(){
clear
# Font colors
default_fontcolor="\033[0m"
red_fontcolor="\033[31m"
green_fontcolor="\033[32m"
warning_fontcolor="\033[33m"
info_fontcolor="\033[36m"
# Background colors
red_backgroundcolor="\033[41;37m"
green_backgroundcolor="\033[42;37m"
yellow_backgroundcolor="\033[43;37m"
# Fonts
error_font="${red_fontcolor}[Error]${default_fontcolor}"
ok_font="${green_fontcolor}[OK]${default_fontcolor}"
warning_font="${warning_fontcolor}[Warning]${default_fontcolor}"
info_font="${info_fontcolor}[Info]${default_fontcolor}"
}
function check_os(){
clear
echo -e "正在检测当前是否为ROOT用户..."
if [ "${EUID}" -eq "0" ]; then
clear
echo -e "${ok_font}检测到当前为Root用户。"
else
clear
echo -e "${error_font}当前并非ROOT用户,请先切换到ROOT用户后再使用本脚本。"
exit 1
fi
clear
echo -e "正在检测此系统是否被支持..."
if [ -n "$(grep 'Aliyun Linux release' /etc/issue)" -o -e /etc/redhat-release ]; then
System_OS="CentOS"
[ -n "$(grep ' 7\.' /etc/redhat-release)" ] && OS_Version=7
[ -n "$(grep ' 6\.' /etc/redhat-release)" -o -n "$(grep 'Aliyun Linux release6 15' /etc/issue)" ] && OS_Version=6
[ -n "$(grep ' 5\.' /etc/redhat-release)" -o -n "$(grep 'Aliyun Linux release5' /etc/issue)" ] && OS_Version=5
elif [ -n "$(grep 'Amazon Linux AMI release' /etc/issue)" -o -e /etc/system-release ]; then
System_OS="CentOS"
OS_Version=6
elif [ -n "$(grep Debian /etc/issue)" -o "$(lsb_release -is 2>/dev/null)" == 'Debian' ]; then
System_OS="Debian"
[ ! -e "$(command -v lsb_release)" ] && { apt-get -y update; apt-get -y install lsb-release; clear; }
OS_Version="$(lsb_release -sr | awk -F. '{print $1}')"
elif [ -n "$(grep Deepin /etc/issue)" -o "$(lsb_release -is 2>/dev/null)" == 'Deepin' ]; then
System_OS="Debian"
[ ! -e "$(command -v lsb_release)" ] && { apt-get -y update; apt-get -y install lsb-release; clear; }
OS_Version="$(lsb_release -sr | awk -F. '{print $1}')"
elif [ -n "$(grep Ubuntu /etc/issue)" -o "$(lsb_release -is 2>/dev/null)" == 'Ubuntu' -o -n "$(grep 'Linux Mint' /etc/issue)" ]; then
System_OS="Ubuntu"
[ ! -e "$(command -v lsb_release)" ] && { apt-get -y update; apt-get -y install lsb-release; clear; }
OS_Version="$(lsb_release -sr | awk -F. '{print $1}')"
[ -n "$(grep 'Linux Mint 18' /etc/issue)" ] && OS_Version=16
else
clear
echo -e "${error_font}目前暂不支持您使用的操作系统。"
exit 1
fi
clear
echo -e "${ok_font}该脚本支持您的系统。"
clear
echo -e "正在检测系统构架是否被支持..."
if [[ "$(uname -m)" == "i686" ]] || [[ "$(uname -m)" == "i386" ]]; then
System_Bit="32"
elif [[ "$(uname -m)" == *"armv7"* ]] || [[ "$(uname -m)" == "armv6l" ]]; then
System_Bit="arm"
elif [[ "$(uname -m)" == *"armv8"* ]] || [[ "$(uname -m)" == "aarch64" ]]; then
System_Bit="arm64"
elif [[ "$(uname -m)" == *"x86_64"* ]]; then
System_Bit="64"
elif [[ "$(uname -m)" == *"mips64le"* ]]; then
System_Bit="mips64le"
elif [[ "$(uname -m)" == *"mips64"* ]]; then
System_Bit="mips64"
elif [[ "$(uname -m)" == *"mipsle"* ]]; then
System_Bit="mipsle"
elif [[ "$(uname -m)" == *"mips"* ]]; then
System_Bit="mips"
elif [[ "$(uname -m)" == *"s390x"* ]]; then
System_Bit="s390x"
else
clear
echo -e "${error_font}目前暂不支持此系统的构架。"
exit 1
fi
clear
echo -e "${ok_font}该脚本支持您的系统构架。"
clear
echo -e "正在检测进程守护安装情况..."
if [ -n "$(command -v systemctl)" ]; then
clear
daemon_name="systemctl"
echo -e "${ok_font}您的系统中已安装 systemctl。"
elif [ -n "$(command -v chkconfig)" ]; then
clear
daemon_name="chkconfig"
echo -e "${ok_font}您的系统中已安装 chkconfig。"
elif [ -n "$(command -v update-rc.d)" ]; then
clear
daemon_name="update-rc.d"
echo -e "${ok_font}您的系统中已安装 update-rc.d。"
else
clear
echo -e "${error_font}您的系统中没有配置进程守护工具,安装无法继续!"
exit 1
fi
clear
echo -e "${ok_font}Support OS: ${System_OS}${OS_Version} ${System_Bit} with ${daemon_name}."
}
function check_install_status(){
install_type="$(cat /usr/local/mtprotoproxy/install_type.txt)"
if [ ! -n "${install_type}" ]; then
install_status="${red_fontcolor}未安装${default_fontcolor}"
mtprotoproxy_use_command="${red_fontcolor}未安装${default_fontcolor}"
else
install_status="${green_fontcolor}已安装${default_fontcolor}"
fi
mtprotoproxy_program="$(find /usr/local/mtprotoproxy/mtprotoproxy.py)"
if [ ! -n "${mtprotoproxy_program}" ]; then
mtprotoproxy_status="${red_fontcolor}未安装${default_fontcolor}"
else
mtprotoproxy_pid="$(ps -ef |grep "mtprotoproxy" |grep -v "grep" | grep -v ".sh"| grep -v "init.d" |grep -v "service" |awk '{print $2}')"
if [ ! -n "${mtprotoproxy_pid}" ]; then
mtprotoproxy_status="${red_fontcolor}未运行${default_fontcolor}"
mtprotoproxy_use_command="${red_fontcolor}未运行${default_fontcolor}"
else
mtprotoproxy_status="${green_fontcolor}正在运行${default_fontcolor} | ${green_fontcolor}${mtprotoproxy_pid}${default_fontcolor}"
Address="$(wget -qO- -t1 -T2 -4 https://api.ip.sb/ip)"
if [ ! -n "${Address}" ]; then
Address="$(wget -qO- -t1 -T2 https://ipinfo.io/ip)"
fi
if [ -n "$(cat "/usr/local/mtprotoproxy/running_log.log" | sed 's/tg: //g' | grep "tg://")" ]; then
mtprotoproxy_use_command="\n${green_backgroundcolor}$(cat "/usr/local/mtprotoproxy/running_log.log" | sed 's/tg: //g' | grep "tg://")${default_fontcolor}"
elif [ -n "$(cat /usr/local/mtprotoproxy/config.py | grep "tg" | awk -F "\"tg\": \"" '{print $2}' | sed 's/\",//g')" ]; then
mtprotoproxy_use_command="${green_backgroundcolor}https://t.me/proxy?server=${Address}&port=$(cat /usr/local/mtprotoproxy/config.py | grep "PORT = " | awk -F "PORT = " '{print $2}')&secret=$(cat /usr/local/mtprotoproxy/config.py | grep "tg" | awk -F "\"tg\": \"" '{print $2}' | sed 's/\",//g')${default_fontcolor}"
else
mtprotoproxy_use_command="${green_backgroundcolor}$(cat /usr/local/mtprotoproxy/telegram_link.txt)${default_fontcolor}"
fi
fi
fi
}
function echo_install_list(){
clear
echo -e "脚本当前安装状态:${install_status}
--------------------------------------------------------------------------------------------------
1.安装MTProtoProxy
--------------------------------------------------------------------------------------------------
MTProtoProxy当前运行状态:${mtprotoproxy_status}
2.更新脚本
3.更新程序
4.卸载程序
5.启动程序
6.关闭程序
7.重启程序
8.显示连接信息
9.封禁IP
10.解封IP
--------------------------------------------------------------------------------------------------
Telegram代理链接:${mtprotoproxy_use_command}
--------------------------------------------------------------------------------------------------"
stty erase '^H' && read -p "请输入序号:" determine_type
if [[ ${determine_type} -ge 1 ]] && [[ ${determine_type} -le 10 ]]; then
data_processing
else
clear
echo -e "${error_font}请输入正确的序号!"
exit 1
fi
}
function data_processing(){
clear
echo -e "正在处理请求中..."
if [[ ${determine_type} = "2" ]]; then
upgrade_shell_script
elif [[ ${determine_type} = "3" ]]; then
prevent_uninstall_check
upgrade_program
restart_service
clear
echo -e "${ok_font}MTProtoProxy更新成功。"
elif [[ ${determine_type} = "4" ]]; then
prevent_uninstall_check
uninstall_program
elif [[ ${determine_type} = "5" ]]; then
prevent_uninstall_check
start_service
elif [[ ${determine_type} = "6" ]]; then
prevent_uninstall_check
stop_service
elif [[ ${determine_type} = "7" ]]; then
prevent_uninstall_check
restart_service
elif [[ ${determine_type} = "8" ]]; then
prevent_uninstall_check
echo_connection_info
elif [[ ${determine_type} = "9" ]]; then
prevent_uninstall_check
banned_ip
elif [[ ${determine_type} = "10" ]]; then
prevent_uninstall_check
unbanned_ip
else
prevent_install_check
os_update
generate_base_config
clear
if [[ ${determine_type} = "1" ]]; then
clear
mkdir -p /usr/local/mtprotoproxy
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}建立文件夹成功。"
else
clear
echo -e "${error_font}建立文件夹失败!"
clear_install_reason="建立文件夹失败。"
clear_install
exit 1
fi
cd /usr/local/mtprotoproxy
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}进入文件夹成功。"
else
clear
echo -e "${error_font}进入文件夹失败!"
clear_install_reason="进入文件夹失败。"
clear_install
exit 1
fi
curl "https://raw.githubusercontent.com/shell-script/mtprotoproxy-onekey/master/program.zip" -o "/usr/local/mtprotoproxy/program.zip"
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}下载MTProtoProxy成功。"
else
clear
echo -e "${error_font}下载MTProtoProxy文件失败!"
clear_install_reason="下载MTProtoProxy文件失败。"
clear_install
exit 1
fi
unzip program.zip
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}解压MTProtoProxy文件成功。"
else
clear
echo -e "${error_font}解压MTProtoProxy文件失败!"
clear_install_reason="解压MTProtoProxy文件失败。"
clear_install
exit 1
fi
rm -f program.zip
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}删除MTProtoProxy压缩包成功。"
else
clear
echo -e "${error_font}删除MTProtoProxy压缩包失败!"
clear_install_reason="删除MTProtoProxy压缩包失败。"
clear_install
exit 1
fi
clear
chmod +x "/usr/local/mtprotoproxy/mtprotoproxy.py"
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}设置MTProtoProxy执行权限成功。"
else
clear
echo -e "${error_font}设置MTProtoProxy执行权限失败!"
clear_install_reason="设置MTProtoProxy执行权限失败。"
clear_install
exit 1
fi
clear
input_port
clear
stty erase '^H' && read -p "请输入Secret(可空):" install_secret
if [ ! -n "${install_secret}" ]; then
install_secret="$(head -n 512 /dev/urandom | md5sum | head -c 32)"
fi
clear
echo -e "${info_font}This is your mtproto proxy connection info:"
echo -e "Host:Port | ${green_backgroundcolor}${Address}:${install_port}${default_fontcolor}"
echo -e "Secret | ${green_backgroundcolor}${install_secret}${default_fontcolor}\n\n"
stty erase '^H' && read -p "请输入Proxy Tag(可空):" install_proxytag
if [ -n "${install_proxytag}" ]; then
install_proxytag="AD_TAG = \"${install_proxytag}\""
fi
cat <<-EOF > /usr/local/mtprotoproxy/config.py
PORT = ${install_port}
USERS = {
"tg": "${install_secret}",
}
${install_proxytag}
EOF
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}写入MTPrtoProxy配置文件成功。"
else
clear
echo -e "${error_font}写入MTPrtoProxy配置文件失败!"
clear_install_reason="写入MTPrtoProxy配置文件失败。"
clear_install
exit 1
fi
if [ "${daemon_name}" == "systemctl" ]; then
curl "https://raw.githubusercontent.com/shell-script/mtprotoproxy-onekey/master/mtprotoproxy.service" -o "/etc/systemd/system/mtprotoproxy.service"
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}下载进程守护文件成功。"
else
clear
echo -e "${error_font}下载进程守护文件失败!"
clear_install_reason="下载进程守护文件失败。"
clear_install
exit 1
fi
systemctl daemon-reload
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}重载进程守护文件成功。"
else
clear
echo -e "${error_font}重载进程守护文件失败!"
clear_install_reason="重载进程守护文件失败。"
clear_install
exit 1
fi
systemctl enable mtprotoproxy.service
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}设置MTProtoProxy开启自启动成功。"
else
clear
echo -e "${error_font}设置MTProtoProxy开启自启动失败!"
clear_install_reason="设置MTProtoProxy开启自启动失败。"
clear_install
exit 1
fi
elif [ "${daemon_name}" == "update-rc.d" ] || [ "${daemon_name}" == "chkconfig" ]; then
curl "https://raw.githubusercontent.com/shell-script/mtprotoproxy-onekey/master/mtprotoproxy.sh" -o "/etc/init.d/mtprotoproxy"
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}下载进程守护文件成功。"
else
clear
echo -e "${error_font}下载进程守护文件失败!"
clear_install_reason="下载进程守护文件失败。"
clear_install
exit 1
fi
chmod +x "/etc/init.d/mtprotoproxy"
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}设置进程守护文件执行权限成功。"
else
clear
echo -e "${error_font}设置进程守护文件执行权限失败!"
clear_install_reason="设置进程守护文件执行权限失败。"
clear_install
exit 1
fi
if [ "${System_OS}" == "CentOS" ]; then
chkconfig --add mtprotoproxy
chkconfig mtprotoproxy on
elif [ "${System_OS}" == "Debian" ] || [ "${System_OS}" == "Ubuntu" ]; then
update-rc.d -f mtprotoproxy defaults
fi
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}设置MTProtoProxy开启自启动成功。"
else
clear
echo -e "${error_font}设置MTProtoProxy开启自启动失败!"
clear_install_reason="设置MTProtoProxy开启自启动失败。"
clear_install
exit 1
fi
fi
echo "1" > /usr/local/mtprotoproxy/install_type.txt
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}写入安装信息成功。"
else
clear
echo -e "${error_font}写入安装信息失败!"
clear_install_reason="写入安装信息失败。"
clear_install
exit 1
fi
restart_service
echo_mtprotoproxy_config
fi
fi
echo -e "\n${ok_font}请求处理完毕。"
}
function upgrade_shell_script(){
clear
echo -e "正在更新脚本中..."
filepath=$(cd "$(dirname "$0")"; pwd)
filename=$(echo -e "${filepath}"|awk -F "$0" '{print $1}')
curl "https://raw.githubusercontent.com/shell-script/mtprotoproxy-onekey/master/mtprotoproxy-go.sh" -o "${filename}/mtprotoproxy-go.sh"
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}脚本更新成功,脚本位置:\"${green_backgroundcolor}${filename}/$0${default_fontcolor}\",使用:\"${green_backgroundcolor}bash ${filename}/$0${default_fontcolor}\"。"
else
clear
echo -e "${error_font}脚本更新失败!"
fi
}
function prevent_uninstall_check(){
clear
echo -e "正在检查安装状态中..."
install_type=$(cat /usr/local/mtprotoproxy/install_type.txt)
if [ "${install_type}" = "" ]; then
clear
echo -e "${error_font}您未安装本程序。"
exit 1
else
echo -e "${ok_font}您已安装本程序,正在执行相关命令中..."
fi
}
function start_service(){
clear
echo -e "正在启动服务中..."
install_type=$(cat /usr/local/mtprotoproxy/install_type.txt)
if [ "${install_type}" -eq "1" ]; then
if [[ ${mtprotoproxy_pid} -eq 0 ]]; then
service mtprotoproxy start
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}MTProtoProxy 启动成功。"
else
clear
echo -e "${error_font}MTProtoProxy 启动失败!"
fi
else
clear
echo -e "${error_font}MTProtoProxy 正在运行。"
fi
fi
}
function stop_service(){
clear
echo -e "正在停止服务中..."
install_type=$(cat /usr/local/mtprotoproxy/install_type.txt)
if [ "${install_type}" -eq "1" ]; then
if [[ ${mtprotoproxy_pid} -eq 0 ]]; then
clear
echo -e "${error_font}MTProtoProxy 未在运行。"
else
service mtprotoproxy stop
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}MTProtoProxy 停止成功。"
else
clear
echo -e "${error_font}MTProtoProxy 停止失败!"
fi
fi
fi
}
function restart_service(){
clear
echo -e "正在重启服务中..."
install_type=$(cat /usr/local/mtprotoproxy/install_type.txt)
if [ "${install_type}" -eq "1" ]; then
service mtprotoproxy restart
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}MTProtoProxy 重启成功。"
else
clear
echo -e "${error_font}MTProtoProxy 重启失败!"
fi
fi
}
function echo_connection_info(){
clear
echo -e "正在获取连接信息中..."
connecting_port="$(cat "/usr/local/mtprotoproxy/config.py" | grep "PORT = " | awk -F "PORT = " '{print $2}')"
if [ ! -n "${connecting_port}" ]; then
connecting_port="$(cat "/usr/local/mtproto/install_port.txt")"
fi
connecting_ips="$(netstat -anp | grep 'ESTABLISHED' | grep 'python3' | grep 'tcp' | grep ":${connecting_port}" | awk '{print $5}' | awk -F ":" '{print $1}' | sort -u | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}")"
connecting_ips_with_port="$(netstat -anp | grep 'ESTABLISHED' | grep 'python3' | grep 'tcp' | grep ":${connecting_port}" | awk '{print $5}' | sort -u)"
if [ ! -n "${connecting_ips}" ]; then
connecting_ips_number=0
else
connecting_ips_number="$(echo -e "${connecting_ips}" | wc -l)"
fi
clear
echo -e "${info_font}目前共有 ${connecting_ips_number} 位用户连接到了此MTProtoProxy。"
if [ "${connecting_ips_number}" -gt "0" ]; then
echo -e "\n${info_font}连接IP如下:"
for((count_number = "${connecting_ips_number}"; count_number >= 1; count_number--))
do
current_ip="$(echo "${connecting_ips}" |sed -n "$count_number"p)"
current_ip_with_port="$(echo "${connecting_ips_with_port}" |sed -n "$count_number"p)"
current_ip_address="$(wget -qO- -t1 -T2 http://freeapi.ipip.net/${current_ip}|sed 's/\"//g;s/,//g;s/\[//g;s/\]//g')"
echo -e "${green_backgroundcolor}[${current_ip_address}] ${current_ip_with_port}${default_fontcolor}"
sleep 1
done
fi
}
function banned_ip(){
clear
stty erase '^H' && read -r -p "请输入欲封禁的IP地址[IPv4/IPv6]:" wanna_banned_ip
if [ "${wanna_banned_ip}" != "${wanna_banned_ip#*[0-9].[0-9]}" ]; then
wanna_banned_ip_type="IPv4"
elif [ "${wanna_banned_ip}" != "${wanna_banned_ip#*:[0-9a-fA-F]}" ]; then
wanna_banned_ip_type="IPv6"
else
clear
echo -e "IP地址不正确,请检查无误后再试。"
exit 1
fi
clear
echo -e "${info_font}封禁说明:1.禁止该IP访问所有端口 2.仅禁止该IP访问当前端口"
stty erase '^H' && read -r -p "请输入禁止类型(1/2,默认:2):" wanna_banned_type
if [ ! -n "${wanna_banned_type}" ]; then
wanna_banned_type=2
fi
if [ "${wanna_banned_type}" == "1" ]; then
if [ "${System_OS}" == "CentOS" ] && [ "${OS_Version}" == "7" ]; then
if [ "${wanna_banned_ip}" != "${wanna_banned_ip#*[0-9].[0-9]}" ]; then
firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='${wanna_banned_ip}' reject"
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}禁止 ${wanna_banned_ip}访问所有端口 请求成功。"
else
clear
echo -e "${error_font}禁止 ${wanna_banned_ip}访问所有端口 请求失败!"
exit 1
fi
firewall-cmd --complete-reload
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}重载firewalld规则成功。"
else
clear
echo -e "${error_font}重载firewalld规则失败!"
exit 1
fi
if [ -n "$(iptables -L -n | grep "${wanna_banned_ip}" | grep "REJECT" | grep "all")" ]; then
clear
echo -e "${ok_font}禁止 ${wanna_banned_ip}访问所有端口 成功。"
else
clear
echo -e "${error_font}禁止 ${wanna_banned_ip}访问所有端口 失败。"
exit 1
fi
elif [ "${wanna_banned_ip}" != "${wanna_banned_ip#*:[0-9a-fA-F]}" ]; then
firewall-cmd --permanent --add-rich-rule="rule family='ipv6' source address='${wanna_banned_ip}' reject"
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}禁止 ${wanna_banned_ip}访问所有端口 请求成功。"
else
clear
echo -e "${error_font}禁止 ${wanna_banned_ip}访问所有端口 请求失败!"
exit 1
fi
firewall-cmd --complete-reload
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}重载firewalld规则成功。"
else
clear
echo -e "${error_font}重载firewalld规则失败!"
exit 1
fi
if [ -n "$(iptables -L -n | grep "${wanna_banned_ip}" | grep "REJECT" | grep "all")" ]; then
clear
echo -e "${ok_font}禁止 ${wanna_banned_ip}访问所有端口 成功。"
else
clear
echo -e "${error_font}禁止 ${wanna_banned_ip}访问所有端口 失败。"
exit 1
fi
else
clear
echo -e "IP地址不正确,请检查无误后再试。"
exit 1
fi
elif [ "${System_OS}" == "CentOS" ] && [ "${OS_Version}" == "5" ] || [ "${OS_Version}" == "6" ]; then
service iptables save
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}保存当前iptables规则成功。"
else
clear
echo -e "${warning_font}保存当前iptables规则失败!"
fi
iptables -I INPUT -s "${wanna_banned_ip}" -j DROP
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}禁止 ${wanna_banned_ip}访问所有端口 请求成功。"
else
clear
echo -e "${error_font}禁止 ${wanna_banned_ip}访问所有端口 请求失败!"
exit 1
fi
service iptables save
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}保存iptables规则成功。"
else
clear
echo -e "${warning_font}保存iptables规则失败!"
fi
if [ -n "$(iptables -L -n | grep "${wanna_banned_ip}" | grep "DROP" | grep "all")" ]; then
clear
echo -e "${ok_font}禁止 ${wanna_banned_ip}访问所有端口 成功。"
else
clear
echo -e "${error_font}禁止 ${wanna_banned_ip}访问所有端口 失败。"
exit 1
fi
elif [[ ${System_OS} =~ ^Debian$|^Ubuntu$ ]];then
iptables-save > /etc/iptables.up.rules
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}保存当前iptables规则成功。"
else
clear
echo -e "${warning_font}保存当前iptables规则失败!"
fi
echo -e '#!/bin/bash\n/sbin/iptables-restore < /etc/iptables.up.rules' > /etc/network/if-pre-up.d/iptables
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}配置iptables启动规则成功。"
else
clear
echo -e "${error_font}配置iptables启动规则失败!"
exit 1
fi
chmod +x /etc/network/if-pre-up.d/iptables
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}设置iptables启动文件执行权限成功。"
else
clear
echo -e "${error_font}设置iptables启动文件执行权限失败!"
exit 1
fi
iptables-restore < /etc/iptables.up.rules
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}导入iptables规则成功。"
else
clear
echo -e "${error_font}导入iptables规则失败!"
exit 1
fi
iptables -I INPUT -s "${wanna_banned_ip}" -j DROP
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}禁止 ${wanna_banned_ip}访问所有端口 请求成功。"
else
clear
echo -e "${error_font}禁止 ${wanna_banned_ip}访问所有端口 请求失败!"
exit 1
fi
iptables-save > /etc/iptables.up.rules
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}保存iptables规则成功。"
else
clear
echo -e "${warning_font}保存iptables规则失败!"
fi
if [ -n "$(iptables -L -n | grep "${wanna_banned_ip}" | grep "DROP" | grep "all")" ]; then
clear
echo -e "${ok_font}禁止 ${wanna_banned_ip}访问所有端口 成功。"
else
clear
echo -e "${error_font}禁止 ${wanna_banned_ip}访问所有端口 失败。"
exit 1
fi
else
clear
echo -e "${error_font}目前暂不支持您使用的操作系统。"
exit 1
fi
elif [ "${wanna_banned_type}" == "2" ]; then
wanna_banned_port="$(cat /usr/local/mtprotoproxy/config.py | grep "PORT = " | awk -F "PORT = " '{print $2}')"
if [ ! -n "${wanna_banned_port}" ]; then
wanna_banned_port="$(cat "/usr/local/mtproto/install_port.txt")"
fi
if [ "${System_OS}" == "CentOS" ] && [ "${OS_Version}" == "7" ]; then
if [ "${wanna_banned_ip}" != "${wanna_banned_ip#*[0-9].[0-9]}" ]; then
firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='${wanna_banned_ip}' port port='${wanna_banned_port}' protocol=tcp reject"
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}禁止${wanna_banned_ip}访问 ${wanna_banned_port}端口tcp协议 请求成功。"
else
clear
echo -e "${error_font}禁止${wanna_banned_ip}访问 ${wanna_banned_port}端口tcp协议 请求失败!"
exit 1
fi
firewall-cmd --complete-reload
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}重载firewalld规则成功。"
else
clear
echo -e "${error_font}重载firewalld规则失败!"
exit 1
fi
if [ -n "$(iptables -L -n | grep "${wanna_banned_ip}" | grep "REJECT" | grep "tcp")" ]; then
clear
echo -e "${ok_font}禁止${wanna_banned_ip}访问 ${wanna_banned_port}端口tcp协议 成功。"
else
clear
echo -e "${error_font}禁止${wanna_banned_ip}访问 ${wanna_banned_port}端口tcp协议 失败。"
exit 1
fi
elif [ "${wanna_banned_ip}" != "${wanna_banned_ip#*:[0-9a-fA-F]}" ]; then
firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='${wanna_banned_ip}' port port='${wanna_banned_port}' protocol=tcp reject"
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}禁止${wanna_banned_ip}访问 ${wanna_banned_port}端口tcp协议 请求成功。"
else
clear
echo -e "${error_font}禁止${wanna_banned_ip}访问 ${wanna_banned_port}端口tcp协议 请求失败!"
exit 1
fi
firewall-cmd --complete-reload
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}重载firewalld规则成功。"
else
clear
echo -e "${error_font}重载firewalld规则失败!"
exit 1
fi
if [ -n "$(iptables -L -n | grep "${wanna_banned_ip}" | grep "${wanna_banned_port}" | grep "REJECT" | grep "tcp")" ]; then
clear
echo -e "${ok_font}禁止${wanna_banned_ip}访问 ${wanna_banned_port}端口tcp协议 成功。"
else
clear
echo -e "${error_font}禁止${wanna_banned_ip}访问 ${wanna_banned_port}端口tcp协议 失败。"
exit 1
fi
else
clear
echo -e "IP地址不正确,请检查无误后再试。"
exit 1
fi
elif [ "${System_OS}" == "CentOS" ] && [ "${OS_Version}" == "5" ] || [ "${OS_Version}" == "6" ]; then
service iptables save
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}保存当前iptables规则成功。"
else
clear
echo -e "${warning_font}保存当前iptables规则失败!"
fi
iptables -I INPUT -s "${wanna_banned_ip}" -p tcp --dport "${wanna_banned_port}" -j DROP
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}禁止${wanna_banned_ip}访问 ${wanna_banned_port}端口tcp协议 请求成功。"
else
clear
echo -e "${error_font}禁止${wanna_banned_ip}访问 ${wanna_banned_port}端口tcp协议 请求失败!"
exit 1
fi
service iptables save
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}保存iptables规则成功。"
else
clear
echo -e "${warning_font}保存iptables规则失败!"
fi
if [ -n "$(iptables -L -n | grep "${wanna_banned_ip}" | grep "${wanna_banned_port}" | grep "DROP" | grep "tcp")" ]; then
clear
echo -e "${ok_font}禁止${wanna_banned_ip}访问 ${wanna_banned_port}端口tcp协议 成功。"
else
clear
echo -e "${error_font}禁止${wanna_banned_ip}访问 ${wanna_banned_port}端口tcp协议 失败。"
exit 1
fi
elif [[ ${System_OS} =~ ^Debian$|^Ubuntu$ ]];then
iptables-save > /etc/iptables.up.rules
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}保存当前iptables规则成功。"
else
clear
echo -e "${warning_font}保存当前iptables规则失败!"
fi
echo -e '#!/bin/bash\n/sbin/iptables-restore < /etc/iptables.up.rules' > /etc/network/if-pre-up.d/iptables
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}配置iptables启动规则成功。"
else
clear
echo -e "${error_font}配置iptables启动规则失败!"
exit 1
fi
chmod +x /etc/network/if-pre-up.d/iptables
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}设置iptables启动文件执行权限成功。"
else
clear
echo -e "${error_font}设置iptables启动文件执行权限失败!"
exit 1
fi
iptables-restore < /etc/iptables.up.rules
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}导入iptables规则成功。"
else
clear
echo -e "${error_font}导入iptables规则失败!"
exit 1
fi
iptables -I INPUT -s "${wanna_banned_ip}" -p tcp --dport "${wanna_banned_port}" -j DROP
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}禁止${wanna_banned_ip}访问 ${wanna_banned_port}端口tcp协议 请求成功。"
else
clear
echo -e "${error_font}禁止${wanna_banned_ip}访问 ${wanna_banned_port}端口tcp协议 请求失败!"
exit 1
fi
iptables-save > /etc/iptables.up.rules
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}保存iptables规则成功。"
else
clear
echo -e "${warning_font}保存iptables规则失败!"
fi
if [ -n "$(iptables -L -n | grep "${wanna_banned_ip}" | grep "${wanna_banned_port}" | grep "DROP" | grep "tcp")" ]; then
clear
echo -e "${ok_font}禁止${wanna_banned_ip}访问 ${wanna_banned_port}端口tcp协议 成功。"
else
clear
echo -e "${error_font}禁止${wanna_banned_ip}访问 ${wanna_banned_port}端口tcp协议 失败。"
exit 1
fi
else
clear
echo -e "${error_font}目前暂不支持您使用的操作系统。"
exit 1
fi
else
clear
echo -e "${error_font}输入错误,请检查后重试。"
exit 1
fi
}
function unbanned_ip(){
clear
stty erase '^H' && read -r -p "请输入欲解封的IP地址[IPv4/IPv6]:" wanna_banned_ip
if [ "${wanna_banned_ip}" != "${wanna_banned_ip#*[0-9].[0-9]}" ]; then
wanna_banned_ip_type="IPv4"
elif [ "${wanna_banned_ip}" != "${wanna_banned_ip#*:[0-9a-fA-F]}" ]; then
wanna_banned_ip_type="IPv6"
else
clear
echo -e "IP地址不正确,请检查无误后再试。"
exit 1
fi
clear
echo -e "${info_font}解禁说明:1.解禁该IP访问所有端口 2.仅解禁该IP访问当前端口"
stty erase '^H' && read -r -p "请输入禁止类型(1/2,默认:2):" wanna_banned_type
if [ ! -n "${wanna_banned_type}" ]; then
wanna_banned_type=2
fi
if [ "${wanna_banned_type}" == "1" ]; then
if [ "${System_OS}" == "CentOS" ] && [ "${OS_Version}" == "7" ]; then
if [ "${wanna_banned_ip}" != "${wanna_banned_ip#*[0-9].[0-9]}" ]; then
firewall-cmd --permanent --remove-rich-rule="rule family='ipv4' source address='${wanna_banned_ip}' reject"
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}解禁 ${wanna_banned_ip}访问所有端口 请求成功。"
else
clear
echo -e "${error_font}解禁 ${wanna_banned_ip}访问所有端口 请求失败!"
exit 1
fi
firewall-cmd --complete-reload
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}重载firewalld规则成功。"
else
clear
echo -e "${error_font}重载firewalld规则失败!"
exit 1
fi
if [ ! -n "$(iptables -L -n | grep "${wanna_banned_ip}" | grep "REJECT" | grep "all")" ]; then
clear
echo -e "${ok_font}解禁 ${wanna_banned_ip}访问所有端口 成功。"
else
clear
echo -e "${error_font}解禁 ${wanna_banned_ip}访问所有端口 失败。"
exit 1
fi
elif [ "${wanna_banned_ip}" != "${wanna_banned_ip#*:[0-9a-fA-F]}" ]; then
firewall-cmd --permanent --remove-rich-rule="rule family='ipv6' source address='${wanna_banned_ip}' reject"
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}解禁 ${wanna_banned_ip}访问所有端口 请求成功。"
else
clear
echo -e "${error_font}解禁 ${wanna_banned_ip}访问所有端口 请求失败!"
exit 1
fi
firewall-cmd --complete-reload
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}重载firewalld规则成功。"
else
clear
echo -e "${error_font}重载firewalld规则失败!"
exit 1
fi
if [ ! -n "$(iptables -L -n | grep "${wanna_banned_ip}" | grep "REJECT" | grep "all")" ]; then
clear
echo -e "${ok_font}解禁 ${wanna_banned_ip}访问所有端口 成功。"
else
clear
echo -e "${error_font}解禁 ${wanna_banned_ip}访问所有端口 失败。"
exit 1
fi
else
clear
echo -e "IP地址不正确,请检查无误后再试。"
exit 1
fi
elif [ "${System_OS}" == "CentOS" ] && [ "${OS_Version}" == "5" ] || [ "${OS_Version}" == "6" ]; then
service iptables save
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}保存当前iptables规则成功。"
else
clear
echo -e "${warning_font}保存当前iptables规则失败!"
fi
iptables -D INPUT -s "${wanna_banned_ip}" -j DROP
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}解禁 ${wanna_banned_ip}访问所有端口 请求成功。"
else
clear
echo -e "${error_font}解禁 ${wanna_banned_ip}访问所有端口 请求失败!"
exit 1
fi
service iptables save
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}保存iptables规则成功。"
else
clear
echo -e "${warning_font}保存iptables规则失败!"
fi
if [ ! -n "$(iptables -L -n | grep "${wanna_banned_ip}" | grep "DROP" | grep "all")" ]; then
clear
echo -e "${ok_font}解禁 ${wanna_banned_ip}访问所有端口 成功。"
else
clear
echo -e "${error_font}解禁 ${wanna_banned_ip}访问所有端口 失败。"
exit 1
fi
elif [[ ${System_OS} =~ ^Debian$|^Ubuntu$ ]];then
iptables-save > /etc/iptables.up.rules
if [[ $? -eq 0 ]];then
clear
echo -e "${ok_font}保存当前iptables规则成功。"
else
clear