-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmtproxy.sh
More file actions
1429 lines (1246 loc) · 43.4 KB
/
mtproxy.sh
File metadata and controls
1429 lines (1246 loc) · 43.4 KB
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
# MTProxy 增强版管理脚本
# 包含完整的检查、诊断和修复功能
# 支持 Alpine Linux, AlmaLinux/RHEL/CentOS, Debian/Ubuntu
WORKDIR=$(dirname $(readlink -f $0))
cd $WORKDIR
pid_file=$WORKDIR/pid/pid_mtproxy
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m'
# 打印函数
print_info() { echo -e "${BLUE}[信息]${NC} $1"; }
print_success() { echo -e "${GREEN}[成功]${NC} $1"; }
print_warning() { echo -e "${YELLOW}[警告]${NC} $1"; }
print_error() { echo -e "${RED}[错误]${NC} $1"; }
print_debug() { echo -e "${CYAN}[调试]${NC} $1"; }
print_line() { echo "========================================"; }
# 系统检测
detect_system() {
if [[ -f /etc/alpine-release ]]; then
OS="alpine"
PKG_MANAGER="apk"
DISTRO="Alpine Linux $(cat /etc/alpine-release)"
elif [[ -f /etc/redhat-release ]] || [[ -f /etc/almalinux-release ]] || [[ -f /etc/centos-release ]]; then
OS="rhel"
PKG_MANAGER="yum"
command -v dnf >/dev/null 2>&1 && PKG_MANAGER="dnf"
if [[ -f /etc/almalinux-release ]]; then
DISTRO="AlmaLinux $(grep VERSION_ID /etc/os-release | cut -d'"' -f2)"
elif [[ -f /etc/centos-release ]]; then
DISTRO="CentOS $(cat /etc/centos-release | grep -oE '[0-9]+\.[0-9]+')"
else
DISTRO="RHEL $(grep VERSION_ID /etc/os-release | cut -d'"' -f2)"
fi
elif [[ -f /etc/debian_version ]]; then
OS="debian"
PKG_MANAGER="apt"
if grep -q "Ubuntu" /etc/os-release; then
DISTRO="Ubuntu $(grep VERSION_ID /etc/os-release | cut -d'"' -f2)"
else
DISTRO="Debian $(cat /etc/debian_version)"
fi
else
print_error "不支持的操作系统"
exit 1
fi
}
# 系统信息检查
check_system_info() {
print_line
print_info "系统信息检查"
print_line
echo -e "操作系统: ${GREEN}$DISTRO${NC}"
echo -e "包管理器: ${GREEN}$PKG_MANAGER${NC}"
echo -e "系统架构: ${GREEN}$(uname -m)${NC}"
echo -e "内核版本: ${GREEN}$(uname -r)${NC}"
echo -e "运行时间: ${GREEN}$(uptime | awk '{print $3,$4}' | sed 's/,//')${NC}"
# 检查内存
local mem_total=$(free -h | awk '/^Mem:/ {print $2}')
local mem_used=$(free -h | awk '/^Mem:/ {print $3}')
echo -e "内存使用: ${GREEN}$mem_used / $mem_total${NC}"
# 检查磁盘空间
local disk_usage=$(df -h . | awk 'NR==2 {print $5}')
echo -e "磁盘使用: ${GREEN}$disk_usage${NC}"
}
# 网络检查
check_network() {
print_line
print_info "网络连接检查"
print_line
# 检查IPv4连接
print_info "检查IPv4连接..."
local ipv4=$(curl -s --connect-timeout 2 https://api.ip.sb/ip -A Mozilla --ipv4)
if [[ -n "$ipv4" && "$ipv4" != *"curl:"* ]]; then
echo -e "IPv4地址: ${GREEN}$ipv4${NC}"
else
echo -e "IPv4连接: ${RED}失败${NC}"
fi
# 检查IPv6连接
print_info "检查IPv6连接..."
local ipv6=$(curl -s --connect-timeout 2 https://api.ip.sb/ip -A Mozilla --ipv6 2>/dev/null)
if [[ -n "$ipv6" && "$ipv6" != *"curl:"* && "$ipv6" != *"error"* ]]; then
echo -e "IPv6地址: ${GREEN}$ipv6${NC}"
else
echo -e "IPv6连接: ${YELLOW}不可用${NC}"
fi
# 检查DNS解析
print_info "检查DNS解析..."
if nslookup google.com >/dev/null 2>&1; then
echo -e "DNS解析: ${GREEN}正常${NC}"
else
echo -e "DNS解析: ${RED}异常${NC}"
fi
# 检查网络接口
print_info "网络接口信息:"
ip addr show | grep -E "inet |inet6 " | grep -v "127.0.0.1" | grep -v "::1" | while read line; do
echo -e " ${CYAN}$line${NC}"
done
}
# 端口检查
check_ports() {
print_line
print_info "端口使用情况检查"
print_line
# 检查常用端口
local common_ports=(22 80 443 8080 8443 8888 9999)
for port in "${common_ports[@]}"; do
if netstat -tulpn 2>/dev/null | grep -q ":$port "; then
local process=$(netstat -tulpn 2>/dev/null | grep ":$port " | awk '{print $7}' | head -1)
echo -e "端口 $port: ${RED}被占用${NC} ($process)"
else
echo -e "端口 $port: ${GREEN}可用${NC}"
fi
done
# 如果有配置文件,检查配置的端口
if [ -f "./mtp_config" ]; then
source ./mtp_config
echo ""
print_info "MTProxy配置端口检查:"
if netstat -tulpn 2>/dev/null | grep -q ":$port "; then
local process=$(netstat -tulpn 2>/dev/null | grep ":$port " | awk '{print $7}' | head -1)
echo -e "客户端端口 $port: ${RED}被占用${NC} ($process)"
else
echo -e "客户端端口 $port: ${GREEN}可用${NC}"
fi
if netstat -tulpn 2>/dev/null | grep -q ":$web_port "; then
local process=$(netstat -tulpn 2>/dev/null | grep ":$web_port " | awk '{print $7}' | head -1)
echo -e "管理端口 $web_port: ${RED}被占用${NC} ($process)"
else
echo -e "管理端口 $web_port: ${GREEN}可用${NC}"
fi
fi
}
# 防火墙检查
check_firewall() {
print_line
print_info "防火墙状态检查"
print_line
case $OS in
"rhel")
if command -v firewall-cmd >/dev/null 2>&1; then
if systemctl is-active firewalld >/dev/null 2>&1; then
echo -e "Firewalld: ${GREEN}运行中${NC}"
if [ -f "./mtp_config" ]; then
source ./mtp_config
local port_open=$(firewall-cmd --list-ports | grep -c "$port/tcp")
local web_port_open=$(firewall-cmd --list-ports | grep -c "$web_port/tcp")
echo -e "端口 $port/tcp: $([ $port_open -gt 0 ] && echo -e "${GREEN}已开放${NC}" || echo -e "${RED}未开放${NC}")"
echo -e "端口 $web_port/tcp: $([ $web_port_open -gt 0 ] && echo -e "${GREEN}已开放${NC}" || echo -e "${RED}未开放${NC}")"
fi
else
echo -e "Firewalld: ${YELLOW}未运行${NC}"
fi
else
echo -e "Firewalld: ${YELLOW}未安装${NC}"
fi
;;
"debian")
if command -v ufw >/dev/null 2>&1; then
local ufw_status=$(ufw status | head -1)
if [[ "$ufw_status" == *"active"* ]]; then
echo -e "UFW: ${GREEN}激活${NC}"
if [ -f "./mtp_config" ]; then
source ./mtp_config
ufw status | grep -q "$port/tcp" && echo -e "端口 $port/tcp: ${GREEN}已开放${NC}" || echo -e "端口 $port/tcp: ${RED}未开放${NC}"
ufw status | grep -q "$web_port/tcp" && echo -e "端口 $web_port/tcp: ${GREEN}已开放${NC}" || echo -e "端口 $web_port/tcp: ${RED}未开放${NC}"
fi
else
echo -e "UFW: ${YELLOW}未激活${NC}"
fi
else
echo -e "UFW: ${YELLOW}未安装${NC}"
fi
;;
"alpine")
if command -v iptables >/dev/null 2>&1; then
local iptables_rules=$(iptables -L INPUT -n | wc -l)
echo -e "iptables规则数: ${GREEN}$iptables_rules${NC}"
else
echo -e "iptables: ${YELLOW}未安装${NC}"
fi
;;
esac
}
# MTProxy状态检查
check_mtproxy_status() {
print_line
print_info "MTProxy状态检查"
print_line
# 检查配置文件
if [ -f "./mtp_config" ]; then
echo -e "配置文件: ${GREEN}存在${NC}"
source ./mtp_config
echo -e "客户端端口: ${GREEN}$port${NC}"
echo -e "管理端口: ${GREEN}$web_port${NC}"
echo -e "伪装域名: ${GREEN}$domain${NC}"
[[ -n "$proxy_tag" ]] && echo -e "推广TAG: ${GREEN}$proxy_tag${NC}" || echo -e "推广TAG: ${YELLOW}未设置${NC}"
else
echo -e "配置文件: ${RED}不存在${NC}"
return 1
fi
# 检查MTG程序
if [ -f "./mtg" ]; then
echo -e "MTG程序: ${GREEN}存在${NC}"
local mtg_version=$(./mtg --version 2>/dev/null | head -1 || echo "未知版本")
echo -e "MTG版本: ${GREEN}$mtg_version${NC}"
else
echo -e "MTG程序: ${RED}不存在${NC}"
return 1
fi
# 检查进程状态
if [ -f "$pid_file" ]; then
local pid=$(cat $pid_file)
if kill -0 $pid 2>/dev/null; then
echo -e "进程状态: ${GREEN}运行中${NC} (PID: $pid)"
# 检查进程详情
local process_info=$(ps aux | grep $pid | grep -v grep | head -1)
echo -e "进程信息: ${CYAN}$process_info${NC}"
# 检查端口监听
if netstat -tulpn 2>/dev/null | grep -q ":$port "; then
echo -e "端口监听: ${GREEN}正常${NC} ($port)"
else
echo -e "端口监听: ${RED}异常${NC} ($port)"
fi
else
echo -e "进程状态: ${RED}已停止${NC} (PID文件存在但进程不存在)"
rm -f $pid_file
fi
else
echo -e "进程状态: ${YELLOW}未运行${NC} (无PID文件)"
fi
# 检查所有mtg进程
local mtg_processes=$(ps aux | grep -v grep | grep mtg | wc -l)
if [ $mtg_processes -gt 0 ]; then
echo -e "MTG进程数: ${GREEN}$mtg_processes${NC}"
ps aux | grep -v grep | grep mtg | while read line; do
echo -e " ${CYAN}$line${NC}"
done
else
echo -e "MTG进程数: ${YELLOW}0${NC}"
fi
}
# 连接测试
test_connection() {
print_line
print_info "连接测试"
print_line
if [ ! -f "./mtp_config" ]; then
print_error "配置文件不存在,无法进行连接测试"
return 1
fi
source ./mtp_config
local public_ip=$(curl -s --connect-timeout 2 https://api.ip.sb/ip -A Mozilla --ipv4)
# 测试端口连通性
print_info "测试端口连通性..."
if timeout 5 bash -c "</dev/tcp/127.0.0.1/$port" 2>/dev/null; then
echo -e "本地端口 $port: ${GREEN}可连接${NC}"
else
echo -e "本地端口 $port: ${RED}无法连接${NC}"
fi
if timeout 5 bash -c "</dev/tcp/127.0.0.1/$web_port" 2>/dev/null; then
echo -e "管理端口 $web_port: ${GREEN}可连接${NC}"
else
echo -e "管理端口 $web_port: ${RED}无法连接${NC}"
fi
# 测试外部连接
print_info "测试外部连接..."
# 测试IPv4外部连接
if [[ -n "$public_ip" ]]; then
print_info "测试IPv4外部连接 ($public_ip:$port)..."
if timeout 10 bash -c "</dev/tcp/$public_ip/$port" 2>/dev/null; then
echo -e "IPv4外部端口 $port: ${GREEN}可连接${NC}"
else
echo -e "IPv4外部端口 $port: ${RED}无法连接${NC}"
fi
fi
# 测试IPv6外部连接
local public_ipv6=$(curl -s --connect-timeout 2 https://api.ip.sb/ip -A Mozilla --ipv6 2>/dev/null)
if [[ -n "$public_ipv6" && "$public_ipv6" != *"curl:"* && "$public_ipv6" != *"error"* ]]; then
print_info "测试IPv6外部连接 ([$public_ipv6]:$port)..."
if timeout 10 bash -c "</dev/tcp/$public_ipv6/$port" 2>/dev/null; then
echo -e "IPv6外部端口 $port: ${GREEN}可连接${NC}"
else
echo -e "IPv6外部端口 $port: ${RED}无法连接${NC}"
fi
else
echo -e "IPv6地址: ${YELLOW}不可用,跳过IPv6连接测试${NC}"
fi
# 生成连接信息
if [[ -n "$public_ip" ]]; then
local domain_hex=$(printf "%s" "$domain" | od -An -tx1 | tr -d ' \n')
local client_secret="ee${secret}${domain_hex}"
print_info "连接信息:"
echo -e "服务器IP: ${GREEN}$public_ip${NC}"
echo -e "端口: ${GREEN}$port${NC}"
echo -e "密钥: ${GREEN}$client_secret${NC}"
echo ""
echo -e "${BLUE}Telegram连接链接:${NC}"
echo "https://t.me/proxy?server=${public_ip}&port=${port}&secret=${client_secret}"
echo "tg://proxy?server=${public_ip}&port=${port}&secret=${client_secret}"
fi
}
# 依赖检查
check_dependencies() {
print_line
print_info "依赖检查"
print_line
local deps=("curl" "wget" "netstat" "ps" "kill" "tar" "od")
for dep in "${deps[@]}"; do
if command -v $dep >/dev/null 2>&1; then
echo -e "$dep: ${GREEN}已安装${NC}"
else
echo -e "$dep: ${RED}未安装${NC}"
fi
done
# 检查特定系统的包
case $OS in
"alpine")
local alpine_deps=("procps" "net-tools")
for dep in "${alpine_deps[@]}"; do
if apk info -e $dep >/dev/null 2>&1; then
echo -e "$dep: ${GREEN}已安装${NC}"
else
echo -e "$dep: ${RED}未安装${NC}"
fi
done
;;
"rhel")
local rhel_deps=("procps-ng" "net-tools")
for dep in "${rhel_deps[@]}"; do
if rpm -q $dep >/dev/null 2>&1; then
echo -e "$dep: ${GREEN}已安装${NC}"
else
echo -e "$dep: ${RED}未安装${NC}"
fi
done
;;
"debian")
local debian_deps=("procps" "net-tools")
for dep in "${debian_deps[@]}"; do
if dpkg -l | grep -q "^ii $dep "; then
echo -e "$dep: ${GREEN}已安装${NC}"
else
echo -e "$dep: ${RED}未安装${NC}"
fi
done
;;
esac
}
# 自动修复功能
auto_fix() {
print_line
print_info "自动修复功能"
print_line
# 安装缺失的依赖
print_info "检查并安装缺失的依赖..."
case $OS in
"alpine")
apk update >/dev/null 2>&1
apk add --no-cache curl wget procps net-tools >/dev/null 2>&1
;;
"rhel")
$PKG_MANAGER install -y curl wget procps-ng net-tools >/dev/null 2>&1
;;
"debian")
apt update >/dev/null 2>&1
apt install -y curl wget procps net-tools >/dev/null 2>&1
;;
esac
print_success "依赖检查完成"
# 清理僵尸进程
print_info "清理可能的僵尸进程..."
pkill -f mtg 2>/dev/null
rm -f $pid_file
print_success "进程清理完成"
# 检查并修复MTG程序
if [ ! -f "./mtg" ]; then
print_info "MTG程序不存在,正在下载..."
download_mtg
fi
# 修复权限
print_info "修复文件权限..."
chmod +x ./mtg 2>/dev/null
chmod +x ./*.sh 2>/dev/null
print_success "权限修复完成"
}
# 端口修改功能
change_ports() {
print_line
print_info "端口修改功能"
print_line
if [ ! -f "./mtp_config" ]; then
print_error "配置文件不存在,请先安装MTProxy"
return 1
fi
source ./mtp_config
echo -e "当前客户端端口: ${GREEN}$port${NC}"
echo -e "当前管理端口: ${GREEN}$web_port${NC}"
echo ""
# 输入新端口
while true; do
read -p "请输入新的客户端端口 (直接回车保持 $port): " new_port
[ -z "$new_port" ] && new_port=$port
if [[ "$new_port" =~ ^[0-9]+$ ]] && [ $new_port -ge 1 ] && [ $new_port -le 65535 ]; then
if netstat -tulpn 2>/dev/null | grep -q ":$new_port " && [ $new_port -ne $port ]; then
print_warning "端口 $new_port 已被占用"
netstat -tulpn 2>/dev/null | grep ":$new_port "
read -p "是否强制使用此端口? (y/N): " force
if [[ "$force" == "y" || "$force" == "Y" ]]; then
break
fi
else
break
fi
else
print_error "请输入有效的端口号 [1-65535]"
fi
done
while true; do
read -p "请输入新的管理端口 (直接回车保持 $web_port): " new_web_port
[ -z "$new_web_port" ] && new_web_port=$web_port
if [[ "$new_web_port" =~ ^[0-9]+$ ]] && [ $new_web_port -ge 1 ] && [ $new_web_port -le 65535 ]; then
if [ $new_web_port -eq $new_port ]; then
print_error "管理端口不能与客户端端口相同"
elif netstat -tulpn 2>/dev/null | grep -q ":$new_web_port " && [ $new_web_port -ne $web_port ]; then
print_warning "端口 $new_web_port 已被占用"
read -p "是否强制使用此端口? (y/N): " force
if [[ "$force" == "y" || "$force" == "Y" ]]; then
break
fi
else
break
fi
else
print_error "请输入有效的端口号 [1-65535]"
fi
done
# 确认修改
if [ $new_port -eq $port ] && [ $new_web_port -eq $web_port ]; then
print_info "端口未发生变化"
return 0
fi
print_warning "端口修改确认:"
echo -e "客户端端口: $port → ${GREEN}$new_port${NC}"
echo -e "管理端口: $web_port → ${GREEN}$new_web_port${NC}"
read -p "确认修改? (y/N): " confirm
if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
print_info "取消修改"
return 0
fi
# 停止服务
stop_mtproxy
# 修改配置
sed -i "s/port=$port/port=$new_port/g" mtp_config
sed -i "s/web_port=$web_port/web_port=$new_web_port/g" mtp_config
print_success "端口修改完成"
# 显示防火墙提示
show_firewall_commands $new_port $new_web_port
# 重启服务
read -p "是否立即重启服务? (Y/n): " restart
if [[ "$restart" != "n" && "$restart" != "N" ]]; then
start_mtproxy
fi
}
# 显示防火墙命令
show_firewall_commands() {
local client_port=$1
local manage_port=$2
print_line
print_warning "防火墙配置提示"
print_line
case $OS in
"rhel")
echo "AlmaLinux/RHEL/CentOS 防火墙配置:"
echo "firewall-cmd --permanent --add-port=$client_port/tcp"
echo "firewall-cmd --permanent --add-port=$manage_port/tcp"
echo "firewall-cmd --reload"
;;
"debian")
echo "Debian/Ubuntu 防火墙配置:"
echo "ufw allow $client_port/tcp"
echo "ufw allow $manage_port/tcp"
;;
"alpine")
echo "Alpine Linux 通常不需要额外的防火墙配置"
echo "如果使用iptables:"
echo "iptables -A INPUT -p tcp --dport $client_port -j ACCEPT"
echo "iptables -A INPUT -p tcp --dport $manage_port -j ACCEPT"
;;
esac
print_line
}
# 获取架构
get_architecture() {
case $(uname -m) in
i386|i686) echo "386" ;;
x86_64) echo "amd64" ;;
aarch64) echo "arm64" ;;
arm*) echo "armv6l" ;;
*) print_error "不支持的架构: $(uname -m)" && exit 1 ;;
esac
}
# 下载MTG
download_mtg() {
local arch=$(get_architecture)
local url="https://github.com/9seconds/mtg/releases/download/v1.0.11/mtg-1.0.11-linux-$arch.tar.gz"
print_info "下载MTG ($arch)..."
wget $url -O mtg.tar.gz -q --timeout=30 || {
print_error "下载失败,请检查网络连接"
return 1
}
tar -xzf mtg.tar.gz mtg-1.0.11-linux-$arch/mtg --strip-components 1
chmod +x mtg
rm -f mtg.tar.gz
if [ -f "./mtg" ]; then
print_success "MTG下载完成"
else
print_error "MTG安装失败"
return 1
fi
}
# 生成随机字符串
gen_rand_hex() {
dd if=/dev/urandom bs=1 count=500 status=none 2>/dev/null | od -An -tx1 | tr -d ' \n' | head -c $1
}
# 字符串转十六进制
str_to_hex() {
printf "%s" "$1" | od -An -tx1 | tr -d ' \n'
}
# 安装依赖
install_dependencies() {
print_info "安装系统依赖..."
case $OS in
"alpine")
apk update && apk add --no-cache curl wget procps net-tools
;;
"rhel")
$PKG_MANAGER install -y curl wget procps-ng net-tools
;;
"debian")
apt update && apt install -y curl wget procps net-tools
;;
esac
if [ $? -eq 0 ]; then
print_success "依赖安装完成"
else
print_error "依赖安装失败"
return 1
fi
}
# 配置MTProxy
config_mtproxy() {
print_line
print_info "配置MTProxy"
print_line
# 端口配置
while true; do
read -p "请输入客户端连接端口 (默认 443): " input_port
[ -z "$input_port" ] && input_port=443
if [[ "$input_port" =~ ^[0-9]+$ ]] && [ $input_port -ge 1 ] && [ $input_port -le 65535 ]; then
if netstat -tulpn 2>/dev/null | grep -q ":$input_port "; then
print_warning "端口 $input_port 已被占用"
netstat -tulpn 2>/dev/null | grep ":$input_port "
read -p "是否继续使用此端口? (y/N): " continue_port
if [[ "$continue_port" == "y" || "$continue_port" == "Y" ]]; then
break
fi
else
break
fi
else
print_error "请输入有效的端口号 [1-65535]"
fi
done
# 管理端口配置
while true; do
read -p "请输入管理端口 (默认 8888): " input_manage_port
[ -z "$input_manage_port" ] && input_manage_port=8888
if [[ "$input_manage_port" =~ ^[0-9]+$ ]] && [ $input_manage_port -ge 1 ] && [ $input_manage_port -le 65535 ]; then
if [ $input_manage_port -eq $input_port ]; then
print_error "管理端口不能与客户端端口相同"
elif netstat -tulpn 2>/dev/null | grep -q ":$input_manage_port "; then
print_warning "端口 $input_manage_port 已被占用"
read -p "是否继续使用此端口? (y/N): " continue_port
if [[ "$continue_port" == "y" || "$continue_port" == "Y" ]]; then
break
fi
else
break
fi
else
print_error "请输入有效的端口号 [1-65535]"
fi
done
# 域名配置
read -p "请输入伪装域名 (默认 azure.microsoft.com): " input_domain
[ -z "$input_domain" ] && input_domain="azure.microsoft.com"
# TAG配置
read -p "请输入推广TAG (可选,直接回车跳过): " input_tag
# 生成配置
local secret=$(gen_rand_hex 32)
cat > ./mtp_config <<EOF
secret="$secret"
port=$input_port
web_port=$input_manage_port
domain="$input_domain"
proxy_tag="$input_tag"
os="$OS"
pkg_manager="$PKG_MANAGER"
EOF
print_success "配置生成完成"
show_firewall_commands $input_port $input_manage_port
}
# 启动MTProxy
start_mtproxy() {
if [ ! -f "./mtp_config" ]; then
print_error "配置文件不存在,请先安装"
return 1
fi
source ./mtp_config
# 检查是否已运行
if [ -f "$pid_file" ]; then
local pid=$(cat $pid_file)
if kill -0 $pid 2>/dev/null; then
print_warning "MTProxy已经在运行中 (PID: $pid)"
return 0
else
rm -f $pid_file
fi
fi
# 检查MTG程序
if [ ! -f "./mtg" ]; then
print_error "MTG程序不存在,请重新安装"
return 1
fi
# 杀死可能占用端口的进程
if netstat -tulpn 2>/dev/null | grep -q ":$port "; then
print_info "释放端口 $port..."
local pids=$(netstat -tulpn 2>/dev/null | grep ":$port " | awk '{print $7}' | sed 's|/.*||')
for pid in $pids; do
kill -9 $pid 2>/dev/null
done
fi
if netstat -tulpn 2>/dev/null | grep -q ":$web_port "; then
print_info "释放端口 $web_port..."
local pids=$(netstat -tulpn 2>/dev/null | grep ":$web_port " | awk '{print $7}' | sed 's|/.*||')
for pid in $pids; do
kill -9 $pid 2>/dev/null
done
fi
# 创建pid目录
mkdir -p pid
# 构建运行命令
local domain_hex=$(str_to_hex $domain)
local client_secret="ee${secret}${domain_hex}"
local public_ip=$(curl -s --connect-timeout 2 https://api.ip.sb/ip -A Mozilla --ipv4)
print_info "正在启动MTProxy..."
# 启动MTG
if [[ -n "$proxy_tag" ]]; then
./mtg run $client_secret $proxy_tag -b 0.0.0.0:$port --multiplex-per-connection 500 --prefer-ip=ipv6 -t 127.0.0.1:$web_port -4 "$public_ip:$port" >/dev/null 2>&1 &
else
./mtg run $client_secret -b 0.0.0.0:$port --multiplex-per-connection 500 --prefer-ip=ipv6 -t 127.0.0.1:$web_port -4 "$public_ip:$port" >/dev/null 2>&1 &
fi
echo $! > $pid_file
sleep 3
# 检查启动状态
if [ -f "$pid_file" ]; then
local pid=$(cat $pid_file)
if kill -0 $pid 2>/dev/null; then
print_success "MTProxy启动成功 (PID: $pid)"
show_proxy_info
else
print_error "MTProxy启动失败"
rm -f $pid_file
return 1
fi
else
print_error "MTProxy启动失败"
return 1
fi
}
# 停止MTProxy
stop_mtproxy() {
if [ -f "$pid_file" ]; then
local pid=$(cat $pid_file)
if kill -0 $pid 2>/dev/null; then
print_info "正在停止MTProxy (PID: $pid)..."
kill -9 $pid 2>/dev/null
rm -f $pid_file
else
print_info "PID文件存在但进程不存在,清理PID文件"
rm -f $pid_file
fi
fi
# 额外确保所有mtg进程被杀死
pkill -f mtg 2>/dev/null
sleep 1
# 检查是否还有mtg进程
if pgrep -f mtg >/dev/null 2>&1; then
print_warning "仍有MTG进程在运行,强制终止..."
pkill -9 -f mtg 2>/dev/null
fi
print_success "MTProxy已停止"
}
# 显示代理信息
show_proxy_info() {
if [ ! -f "./mtp_config" ]; then
print_error "配置文件不存在"
return 1
fi
source ./mtp_config
# 缓存IP地址,避免每次都重新获取
local ip_cache_file="./ip_cache"
local public_ip=""
local public_ipv6=""
# 检查缓存文件是否存在且不超过5分钟
if [ -f "$ip_cache_file" ]; then
# 兼容不同系统的stat命令
local cache_time=0
if command -v stat >/dev/null 2>&1; then
if stat -c %Y "$ip_cache_file" >/dev/null 2>&1; then
cache_time=$(stat -c %Y "$ip_cache_file" 2>/dev/null || echo 0)
elif stat -f %m "$ip_cache_file" >/dev/null 2>&1; then
cache_time=$(stat -f %m "$ip_cache_file" 2>/dev/null || echo 0)
fi
fi
local current_time=$(date +%s)
local time_diff=$((current_time - cache_time))
if [ $time_diff -lt 300 ]; then # 5分钟内使用缓存
source "$ip_cache_file"
fi
fi
# 如果缓存无效或不存在,重新获取IP
if [ -z "$public_ip" ]; then
print_info "获取服务器IP地址..."
public_ip=$(curl -s --connect-timeout 2 https://api.ip.sb/ip -A Mozilla --ipv4)
public_ipv6=$(curl -s --connect-timeout 2 https://api.ip.sb/ip -A Mozilla --ipv6 2>/dev/null)
# 保存到缓存文件
echo "public_ip='$public_ip'" > "$ip_cache_file"
echo "public_ipv6='$public_ipv6'" >> "$ip_cache_file"
fi
local domain_hex=$(str_to_hex $domain)
local client_secret="ee${secret}${domain_hex}"
print_line
if [ -f "$pid_file" ] && kill -0 $(cat $pid_file) 2>/dev/null; then
print_success "MTProxy状态: 运行中"
else
print_warning "MTProxy状态: 已停止"
fi
echo -e "系统类型: ${PURPLE}$os${NC}"
echo -e "服务器IPv4: ${GREEN}$public_ip${NC}"
if [[ -n "$public_ipv6" && "$public_ipv6" != *"curl:"* && "$public_ipv6" != *"error"* ]]; then
echo -e "服务器IPv6: ${GREEN}$public_ipv6${NC}"
fi
echo -e "客户端端口: ${GREEN}$port${NC}"
echo -e "管理端口: ${GREEN}$web_port${NC}"
echo -e "代理密钥: ${GREEN}$client_secret${NC}"
echo -e "伪装域名: ${GREEN}$domain${NC}"
[[ -n "$proxy_tag" ]] && echo -e "推广TAG: ${GREEN}$proxy_tag${NC}"
echo -e "\n${BLUE}Telegram连接链接 (IPv4):${NC}"
echo "https://t.me/proxy?server=${public_ip}&port=${port}&secret=${client_secret}"
echo "tg://proxy?server=${public_ip}&port=${port}&secret=${client_secret}"
if [[ -n "$public_ipv6" && "$public_ipv6" != *"curl:"* && "$public_ipv6" != *"error"* ]]; then
echo -e "\n${BLUE}Telegram连接链接 (IPv6):${NC}"
echo "https://t.me/proxy?server=${public_ipv6}&port=${port}&secret=${client_secret}"
echo "tg://proxy?server=${public_ipv6}&port=${port}&secret=${client_secret}"
fi
print_line
}
# 进程监控和自动重启
monitor_mtproxy() {
print_info "启动MTProxy进程监控..."
# 检查是否已有监控进程在运行
local monitor_pid_file="./pid/monitor.pid"
if [ -f "$monitor_pid_file" ]; then
local monitor_pid=$(cat $monitor_pid_file)
if kill -0 $monitor_pid 2>/dev/null; then
print_warning "进程监控已在运行中 (PID: $monitor_pid)"
print_info "如需停止监控,请运行: kill $monitor_pid"
return 0
else
rm -f $monitor_pid_file
fi
fi
# 创建监控脚本
local monitor_script="./monitor_script.sh"
cat > $monitor_script << 'EOF'
#!/bin/bash
# MTProxy进程监控脚本
pid_file="./pid/mtproxy.pid"
monitor_pid_file="./pid/monitor.pid"
log_file="./logs/monitor.log"
# 创建必要的目录
mkdir -p pid logs
# 记录监控进程PID
echo $$ > $monitor_pid_file
# 监控循环
restart_count=0
max_restarts=5
check_interval=30
echo "$(date): 启动MTProxy进程监控" >> $log_file
while true; do
if [ -f "$pid_file" ]; then
local pid=$(cat $pid_file)
if kill -0 $pid 2>/dev/null; then
echo "$(date): MTProxy运行正常 (PID: $pid)" >> $log_file
restart_count=0
else
echo "$(date): MTProxy进程已停止,尝试重启..." >> $log_file
if [ $restart_count -lt $max_restarts ]; then
# 启动MTProxy
if [ -f "./mtp_config" ]; then
source ./mtp_config
local domain_hex=$(printf "%s" "$domain" | od -An -tx1 | tr -d ' \n')
local client_secret="ee${secret}${domain_hex}"
local public_ip=$(curl -s --connect-timeout 2 https://api.ip.sb/ip -A Mozilla --ipv4)
if [[ -n "$proxy_tag" ]]; then
./mtg run $client_secret $proxy_tag -b 0.0.0.0:$port --multiplex-per-connection 500 --prefer-ip=ipv6 -t 127.0.0.1:$web_port -4 "$public_ip:$port" >/dev/null 2>&1 &
else
./mtg run $client_secret -b 0.0.0.0:$port --multiplex-per-connection 500 --prefer-ip=ipv6 -t 127.0.0.1:$web_port -4 "$public_ip:$port" >/dev/null 2>&1 &
fi
echo $! > $pid_file
sleep 3
if kill -0 $(cat $pid_file) 2>/dev/null; then
restart_count=$((restart_count + 1))
echo "$(date): 重启成功 (第${restart_count}次)" >> $log_file
else
echo "$(date): 重启失败" >> $log_file
fi
else
echo "$(date): 配置文件不存在,无法重启" >> $log_file
fi
else
echo "$(date): 重启次数过多,停止监控" >> $log_file
break
fi
fi
else
echo "$(date): PID文件不存在,尝试启动..." >> $log_file
# 这里可以添加启动逻辑,但通常由用户手动启动
fi
sleep $check_interval
done
# 清理
rm -f $monitor_pid_file
echo "$(date): 监控进程已停止" >> $log_file
EOF
chmod +x $monitor_script
# 启动后台监控
nohup $monitor_script >/dev/null 2>&1 &
local monitor_pid=$!
sleep 2
# 检查监控是否启动成功
if kill -0 $monitor_pid 2>/dev/null; then
print_success "进程监控已启动 (PID: $monitor_pid)"
print_info "监控日志: tail -f ./logs/monitor.log"
print_info "停止监控: kill $monitor_pid"
print_warning "注意: 这是一个额外的监控功能,不影响主要功能"
else
print_error "进程监控启动失败"