-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcattools.sh
1928 lines (1693 loc) · 66.4 KB
/
cattools.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
# env
DEFAULT_IP="192.168.1.4"
RELEASE="/etc/catwrt_release"
BACKUP_FILE="/etc/catwrt_opkg_list_installed"
API_URL="https://api.miaoer.net/api/v2/snippets/catwrt/update"
BASE_URL="https://mirror.ghproxy.com/https://raw.githubusercontent.com/miaoermua/cattools/main/repo"
# sysupgrade env
AMD64_EFI_SYSUP="https://raw.githubusercontent.com/miaoermua/cattools/main/sysupgrade/amd64/sysup_efi"
AMD64_BIOS_SYSUP="https://raw.githubusercontent.com/miaoermua/cattools/main/sysupgrade/amd64/sysup_bios"
MT7621_SYSUP="https://raw.githubusercontent.com/miaoermua/cattools/main/sysupgrade/mt7621/"
MT798X_SYSUP="https://raw.githubusercontent.com/miaoermua/cattools/main/sysupgrade/mt798x/"
# Check ROOT & CatWrt/Lean's LEDE(QWRT)
if [ $(id -u) != "0" ]; then
echo "Error: You must be root to run this cattools, please use root user"
exit 1
fi
if ! grep -qi -E "OpenWrt|QWRT" /etc/openwrt_release; then
echo "Error: Your system is not supported by cattools!"
exit 1
fi
# Update CatTools
update_cattools() {
echo "Please wait for the cattools to be updated."
local target_file="/usr/bin/cattools"
local temp_file="/tmp/cattools/cattools.sh"
local retries=3
local success=false
local urls=(
"https://service.miaoer.xyz/cattools/cattools.sh"
"https://mirror.ghproxy.com/https://raw.githubusercontent.com/miaoermua/cattools/main/cattools.sh"
"https://raw.githubusercontent.com/miaoermua/cattools/main/cattools.sh"
)
mkdir -p /tmp/cattools
while [ $retries -gt 0 ]; do
for url in "${urls[@]}"; do
curl --silent --connect-timeout 3 --max-time 5 -o "$temp_file" "$url"
curl_exit_code=$?
if [ $curl_exit_code -eq 0 ] && [ -s "$temp_file" ]; then
success=true
break 2
fi
done
echo "Attempt $((4 - retries)) failed. Retrying..."
retries=$((retries - 1))
done
if [ "$success" = false ]; then
echo "Unable to download the latest version, continue to use the current offline version."
echo ""
rm -f "$temp_file"
return
fi
mv "$temp_file" "$target_file"
chmod +x "$target_file"
echo "cattools updated successfully."
echo ""
}
# Menu Function
menu() {
echo ""
echo "----------------------------------------------------------"
echo " CatTools "
echo " https://www.miaoer.net/posts/blog/cattools "
echo "----------------------------------------------------------"
echo "1. SetIP - 设置 IP"
echo "2. Network_Wizard - 网络向导"
echo "3. Apply_repo - 软件源配置"
echo "4. Diagnostics - 网络诊断"
echo "5. Debug - 抓取日志"
echo "6. Catwrt_update - 检查更新"
echo "7. Sysupgrade - 系统更新"
echo "8. Restore - 恢复软件包"
echo "9. Utilities(more) - 实用工具"
echo "0. Exit - 退出"
echo "----------------------------------------------------------"
echo -n "请输入数字并回车(Please enter your choice): "
}
# Parameters
help() {
echo "Usage: $0 [-help] [-update]"
echo
echo "Options:"
echo " -help or -h 帮助"
echo " -update or -u 跳过 Cattools 脚本更新检查"
echo ""
echo "HELP:"
echo "遇到问题了? 请使用本工具菜单中的 debug 选项,尝试反馈以解决问题!"
echo "https://github.com/miaoermua/CatWrt/issues/new?assignees=&labels=&projects=&template=report.md&title="
echo "TG Guoup: t.me/miaoergroup // QQ Guoup: 669190476 // Blog: miaoer.net"
exit 0
}
skip_update=false
while [[ "$#" -gt 0 ]]; do
case $1 in
-h) help ;;
-help) help ;;
-u) skip_update=true ;;
-update) skip_update=true ;;
*)
echo "Unknown parameter passed: $1"
exit 1
;;
esac
shift
done
if [ "$skip_update" = false ]; then
update_cattools
fi
# Setup
setip() {
DEFAULT_IP="192.168.1.4"
while true; do
read -p "Please enter the IP Addr and press Enter /// 请输入 IP (默认为 $DEFAULT_IP): " input_ip
if [ -z "$input_ip" ]; then
input_ip=$DEFAULT_IP
fi
if echo "$input_ip" | grep -Eo '^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$' >/dev/null; then
break
else
echo "Invalid IP address format /// 非法的 IP 地址格式,请重新输入"
fi
done
uci set network.lan.ipaddr=$input_ip
uci commit network
/etc/init.d/network restart
echo "默认 IP 已设置为 $input_ip"
}
configure_network_interfaces() {
local interfaces="$1"
bridge_ports=""
for iface in $interfaces; do
if [ "$iface" != "eth0" ]; then
bridge_ports="$bridge_ports $iface"
fi
done
uci set network.wan.ifname='eth0'
uci set network.wan.proto='dhcp'
uci set network.lan.type='bridge'
uci set network.lan.ifname="$bridge_ports"
uci set network.lan._orig_ifname="$bridge_ports"
uci set network.lan._orig_bridge='true'
uci set network.wan6._orig_bridge='false'
uci set network.wan6._orig_ifname='eth1'
uci set network.wan6.ifname='eth0'
uci set network.wan6.reqaddress='try'
uci set network.wan6.reqprefix='auto'
echo "[Step10] Network interfaces configured: WAN (ETH0), LAN ($bridge_ports) /// 网口已配置: WAN (ETH0), LAN ($bridge_ports)"
}
# Network Wizard
network_wizard() {
echo
echo
echo
read -p "[Step1] Do you want Network Wizard? /// 是否使用网络向导?([Enter] 确认 / [0] 退出): " use_wizard
if [ "$use_wizard" == "0" ]; then
echo "网络向导已退出。"
return
fi
interfaces=$(ls /sys/class/net | grep -E 'eth[0-9]+')
iface_count=$(echo "$interfaces" | wc -w)
if [ "$iface_count" -eq 1 ]; then
echo
echo "[Step2] Detected a single network interface /// 检测到单个网口"
read -p "是否进行旁路网关设置?([Enter] 确认 / [0] 跳过旁路设置):" choice
if [ "$choice" != "0" ]; then
bypass_gateway
return
fi
fi
echo
echo "[Step3] CatWrt default IP is 192.168.1.4 /// 默认 CatWrt IP 为 192.168.1.4"
read -p "是否修改 IP 地址?([Enter] 保持默认 / [0] 自定义): " modify_ip
if [ "$modify_ip" == "0" ]; then
read -p "请输入 IP (默认为 $DEFAULT_IP): " input_ip
if [[ -z $input_ip ]]; then
input_ip=$DEFAULT_IP
elif ! [[ $input_ip =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
echo "[ERROR] 无效的 IP 地址。"
return
fi
uci set network.lan.ipaddr=$input_ip
echo "[INFO] IP 地址已设置为 $input_ip"
else
echo "[INFO] 保持默认 IP 地址:$DEFAULT_IP"
fi
echo
echo "[Step4] IPv6 is enabled by default /// IPv6 默认是开启的"
read -p "是否禁用 IPv6 网络?([Enter] 跳过 / [1] 禁用): " disable_ipv6
if [ "$disable_ipv6" == "1" ]; then
uci delete dhcp.lan.dhcpv6
uci delete dhcp.lan.ra
uci delete dhcp.lan.ra_management
uci delete network.lan.ip6assign
echo "[INFO] IPv6 已禁用"
fi
echo
echo "[Step5] Default connection mode is DHCP /// 默认模式为 DHCP"
read -p "是否进行 PPPoE 拨号?([Enter] 继续 DHCP / [1] PPPoE 拨号): " use_pppoe
if [ "$use_pppoe" == "1" ]; then
echo "如不知道账号密码,可以寻求宽带师傅,必须要正确填写!"
read -p "[PPPoE] 请输入宽带账号: " username
read -s -p "[PPPoE] 请输入宽带密码: " password
uci set network.wan.proto=pppoe
uci set network.wan.username=$username
uci set network.wan.password=$password
echo "[INFO] PPPoE 拨号配置已完成"
fi
echo
echo "[Step6] Use recommended DNS servers 223.6.6.6 119.29.29.99?"
read -p " /// 使用推荐的 DNS 服务器 223.6.6.6 119.29.29.99 吗?([Enter] 确认 / [0] 跳过): " use_dns
if [ "$use_dns" = "0" ]; then
exit 0
elif [ -z "$use_dns" ]; then
uci set network.lan.dns="223.6.6.6 119.29.29.99"
else
if [[ $use_dns =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}(\s+([0-9]{1,3}\.){3}[0-9]{1,3})*$ ]]; then
uci set network.lan.dns="$use_dns"
else
echo "[ERROR] Invalid DNS format /// 无效的 DNS 格式"
exit 1
fi
fi
echo
echo "[Step7] Do you want to change the DHCP IP pool range? (default: 30-200)"
read -p " /// 是否修改 IP 可用段?(默认: 30-200 按 [Enter] 确认 / [1] 自定义范围 ): " dhcp_choice
if [ "$dhcp_choice" = "1" ]; then
echo
read -p "Enter the DHCP IP pool range (e.g., 40-210) /// 输入 DHCP IP 地址范围 (例如: 40-210): " dhcp_range
if [[ $dhcp_range =~ ^([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\-([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]]; then
dhcp_start=$(echo $dhcp_range | cut -d '-' -f 1)
dhcp_limit=$(echo $dhcp_range | cut -d '-' -f 2)
uci set dhcp.lan.start=$dhcp_start
uci set dhcp.lan.limit=$dhcp_limit
else
echo
echo "[ERROR] Invalid DHCP range format /// 无效的 DHCP 范围格式"
exit 1
fi
else
uci set dhcp.lan.start=30
uci set dhcp.lan.limit=200
fi
echo
echo "[Step8] Enable DHCP force /// 开启 DHCP 强制可以避免局域网收到 AP 吐地址的问题"
read -p "是否开启强制 DHCP 模式?([Enter] 确认,按 [1] 跳过): " force_dhcp
if [ "$force_dhcp" != "1" ]; then
uci set dhcp.lan.force=1
echo "[INFO] 强制 DHCP 模式已开启"
fi
echo
echo "[Step9] Enable UPNP by default /// 默认开启 UPNP,可提升 BT/P2P 软件连接性,但客户端容易受到流氓软件滥用 P2P 网络导致上行带宽异常!"
read -p "是否开启 UPNP?([Enter] 确认,按 [1] 跳过): " enable_upnp
if [ "$enable_upnp" != "1" ]; then
uci set upnpd.config.enabled=1
echo "[INFO] UPNP 已开启"
fi
arch=$(grep "arch" /etc/catwrt_release | cut -d'=' -f2)
# BIND Interfaces
if [ "$arch" = "amd64" ] || [ "$arch" = "aarch64_generic" ]; then
echo
echo "[Step10] Configure network interfaces /// 配置网口"
echo ""
echo " Wan LAN1 LAN2 LANX ..."
echo " eth0 eth1 eth2 ethX ..."
echo " □ □ □ □ ..."
echo ""
echo "Press [Enter] to skip network configuration, press [1] to configure /// 按 [Enter] 跳过网口配置,按 [1] 确认配置: "
read -p "" configure_network
echo
interfaces=$(ls /sys/class/net | grep -E 'eth[0-9]+')
iface_count=$(echo "$interfaces" | wc -w)
if [ "$iface_count" -eq 1 ]; then
echo "[Step10] Detected a single network interface, no configuration needed /// 检测到单个网口,无需配置"
elif [ "$iface_count" -eq 2 ]; then
echo "[Step10] Detected two network interfaces, configuration not recommended /// 检测到两个网口,不推荐配置"
echo "Press [1] to configure, press [Enter] to skip /// 按 [1] 配置,按 [Enter] 跳过"
read -p "" continue_network
if [ "$continue_network" != "1" ]; then
echo "[Step10] Skipping network interface configuration /// 跳过网口配置"
else
echo "[Step10] Configuring network interfaces... /// 开始配置网口..."
configure_network_interfaces "$interfaces"
fi
else
echo "[Step10] Detected multiple network interfaces /// 检测到多个网口"
echo "[Step10] Configuring network interfaces... /// 开始配置网口..."
configure_network_interfaces "$interfaces"
fi
else
echo "[Step10] System architecture $arch is not supported. No changes made. /// 系统架构 $arch 不支持该脚本,未进行任何更改"
fi
echo
echo "[INFO] Ready to reboot CatWrt!"
uci commit
/etc/init.d/network restart
/etc/init.d/dnsmasq restart
/etc/init.d/firewall restart
/etc/init.d/miniupnpd restart
reboot
}
# BypassGateway
bypass_gateway() {
# 输入主路由的 IP 地址
while true; do
echo ""
read -p "[Step3] 请输入主路由的 IP 地址 (如 192.168.31.1): " router_ip
if [ -z "$router_ip" ]; then
echo "[ERROR] 主路由 IP 地址不能为空,请重新输入。"
elif ! echo "$router_ip" | grep -Eq '^(10|172\.(1[6-9]|2[0-9]|3[01])|192\.168)\.'; then
echo "[ERROR] 输入的 IP 地址无效,请输入有效的 IP 地址"
else
break
fi
done
# 提取子网地址和设置本机 IP 地址
subnet=$(echo "$router_ip" | cut -d. -f1-3)
default_device_ip="${subnet}.4"
while true; do
read -p "[Step4] 本机 IP 地址为 $default_device_ip 按回车键确认,或输入新的 IP 地址:" device_ip
if [ -z "$device_ip" ]; then
device_ip="$default_device_ip"
break
elif ! echo "$device_ip" | grep -Eq '^(10|172\.(1[6-9]|2[0-9]|3[01])|192\.168)\.'; then
echo "[ERROR] 输入的 IP 地址无效,请输入有效的 IP 地址。"
else
break
fi
done
echo "INFO ========================"
echo "主路由 IP 地址:$router_ip"
echo "本机(旁路网关) IP 地址:$device_ip"
echo
echo "[Step5] Use recommended DNS servers 223.6.6.6 119.29.29.99?"
read -p " /// 使用推荐的 DNS 服务器 223.6.6.6 119.29.29.99 吗?([Enter] 确认 / [0] 跳过): " use_dns
if [ "$use_dns" = "0" ]; then
exit 0
elif [ -z "$use_dns" ]; then
uci set network.lan.dns="223.6.6.6 119.29.29.99"
else
if [[ $use_dns =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}(\s+([0-9]{1,3}\.){3}[0-9]{1,3})*$ ]]; then
uci set network.lan.dns="$use_dns"
else
echo "[ERROR] Invalid DNS format /// 无效的 DNS 格式"
exit 1
fi
fi
# 配置网络
uci set network.lan.ipaddr="$device_ip"
uci set network.lan.gateway="$router_ip"
uci set network.lan.proto='static'
uci commit network
# 禁用 LAN 口的 IPv6 服务器
uci set dhcp.lan.dhcpv6='disabled'
uci set dhcp.lan.ra='disabled'
uci commit dhcp
# 启用 MSS 钳制
uci set firewall.@defaults[0].mss_clamping='1'
# 启用 IP 伪装和 MTU fix
# lan 区域
uci set firewall.@zone[0].masq='1'
uci set firewall.@zone[0].mtu_fix='1'
# wan 区域
uci set firewall.@zone[1].masq='1'
uci set firewall.@zone[1].mtu_fix='1'
uci commit firewall
# 关闭 LAN 口的 DHCP 服务并删除相关配置
uci set dhcp.lan.ignore='1'
uci delete dhcp.lan.leasetime
uci delete dhcp.lan.limit
uci delete dhcp.lan.start
uci commit dhcp
lan_ip=$(uci get network.lan.ipaddr)
echo "旁路网关配置完成 IP: $lan_ip "
# 重启相关服务以应用更改
/etc/init.d/network restart
/etc/init.d/firewall restart
/etc/init.d/dnsmasq restart
echo
echo "[INFO] 如出现 Warning 是因为旁路防火墙是这样报错的,部分配置可以忽略不影响使用"
echo
}
# Debug
debug() {
if [ -f /www/logs.txt ]; then
rm /www/logs.txt
fi
cat /etc/banner >>/www/logs.txt
date >>/www/logs.txt
echo "## RELEASE" >>/www/logs.txt
echo "==========" >>/www/logs.txt
cat /etc/catwrt_release >>/www/logs.txt
echo "## STATUS" >>/www/logs.txt
echo "=================" >>/www/logs.txt
eval $UPTIME >>/www/logs.txt
echo "## Memory Usage" >>/www/logs.txt
echo "==========" >>/www/logs.txt
free -h >>/www/logs.txt
echo "## Disk Usage" >>/www/logs.txt
echo "==========" >>/www/logs.txt
df -h >>/www/logs.txt
echo "## Application" >>/www/logs.txt
echo "==========" >>/www/logs.txt
opkg list_installed >>/www/logs.txt
echo "## SYSLOG" >>/www/logs.txt
echo "==========" >>/www/logs.txt
logread >>/www/logs.txt
echo "## DMESG" >>/www/logs.txt
echo "==========" >>/www/logs.txt
dmesg >>/www/logs.txt
echo "## Plugins" >>/www/logs.txt
echo "==========" >>/www/logs.txt
cat /tmp/openclash.log >>/www/logs.txt
cat /tmp/log/ssrplus.log >>/www/logs.txt
cat /tmp/log/passwall.log >>/www/logs.txt
cat /tmp/log/passwall2.log >>/www/logs.txt
echo "## Task" >>/www/logs.txt
echo "==========" >>/www/logs.txt
top -b -n 1 >>/www/logs.txt
echo "## Network Configuration" >>/www/logs.txt
echo "==========" >>/www/logs.txt
ifconfig -a >>/www/logs.txt
echo "## UCI Network" >>/www/logs.txt
echo "==========" >>/www/logs.txt
uci show network >>/www/logs.txt
echo "## Firewall" >>/www/logs.txt
echo "==========" >>/www/logs.txt
iptables -L -v -n >>/www/logs.txt
ip6tables -L -v -n >>/www/logs.txt
echo "## Routing Table" >>/www/logs.txt
echo "==========" >>/www/logs.txt
ip route >>/www/logs.txt
ip -6 route >>/www/logs.txt
lan_ip=$(uci get network.lan.ipaddr)
echo
echo "Finish! ==========================================================================================="
echo "请使用浏览器访问此地址下载 LOG 文件 http://$lan_ip/logs.txt"
echo "日志已收集到 /www/logs.txt 如果你使用 PPPoE 拨号请手动将宽带账密删除,再使用以下链接上传 Github issues 附件!"
echo
echo "https://github.com/miaoermua/CatWrt/issues/new?assignees=&labels=bug&projects=&template=report.yaml"
echo "尽可能使用 Github 提交你的问题不会操作再使用社交软件 TG Guoup: t.me/miaoergroup // QQ Guoup: 669190476"
echo
sleep 5
exit
}
# catwrt_update
catwrt_update() {
source /etc/catwrt_release
api_data=$(curl -s $API_URL)
version_compare() {
local v1_year=$(echo "$1" | cut -d'.' -f1 | sed 's/^v//')
local v1_month=$(echo "$1" | cut -d'.' -f2)
local v2_year=$(echo "$2" | cut -d'.' -f1 | sed 's/^v//')
local v2_month=$(echo "$2" | cut -d'.' -f2)
if [ "$v1_year" -gt "$v2_year" ]; then
echo 1
elif [ "$v1_year" -lt "$v2_year" ]; then
echo -1
else
if [ "$v1_month" -gt "$v2_month" ]; then
echo 1
elif [ "$v1_month" -lt "$v2_month" ]; then
echo -1
else
echo 0
fi
fi
}
check_update() {
local current_version=$1
local current_hash=$2
local arch=$3
local channel=$4
echo
echo "LOCAL ================================================="
echo "当前版本: $current_version"
echo "当前架构: $arch"
echo "当前通道: $channel"
echo "========================================================"
versions=$(echo "$api_data" | jq -r 'keys[]')
latest_stable_version=""
latest_beta_version=""
stable_releases=""
beta_releases=""
stable_blogs=""
beta_blogs=""
for version in $versions; do
version_data=$(echo "$api_data" | jq -r ".\"$version\".\"$arch\"")
if [ "$version_data" != "null" ]; then
api_channel=$(echo "$version_data" | jq -r ".channel")
api_hash=$(echo "$version_data" | jq -r ".hash")
api_latest=$(echo "$version_data" | jq -r ".latest")
api_releases=$(echo "$version_data" | jq -r ".releases")
api_blogs=$(echo "$version_data" | jq -r ".blogs")
compare_result=$(version_compare "$version" "$current_version")
if [ "$compare_result" -gt 0 ]; then
if [ "$channel" == "Beta" ]; then
if [ "$api_channel" == "Beta" ]; then
if [ -z "$latest_beta_version" ] || [ "$(version_compare "$version" "$latest_beta_version")" -gt 0 ]; then
latest_beta_version="$version"
beta_releases="$api_releases"
beta_blogs="$api_blogs"
fi
elif [ "$api_channel" == "Stable" ]; then
if [ -z "$latest_stable_version" ] || [ "$(version_compare "$version" "$latest_stable_version")" -gt 0 ]; then
latest_stable_version="$version"
stable_releases="$api_releases"
stable_blogs="$api_blogs"
fi
fi
elif [ "$channel" == "Stable" ] && [ "$api_channel" == "Stable" ]; then
if [ -z "$latest_stable_version" ] || [ "$(version_compare "$version" "$latest_stable_version")" -gt 0 ]; then
latest_stable_version="$version"
stable_releases="$api_releases"
stable_blogs="$api_blogs"
fi
fi
fi
fi
done
if [ -n "$latest_stable_version" ] || [ -n "$latest_beta_version" ]; then
if [ -n "$latest_stable_version" ]; then
echo
echo "UPDATE ================================================"
echo "发现新版本: $current_version > $latest_stable_version (Stable)"
echo "版本: $stable_releases"
echo "博客: $stable_blogs"
echo "========================================================"
echo
fi
if [ -n "$latest_beta_version" ]; then
echo
echo "UPDATE ================================================"
echo "发现新版本: $current_version > $latest_beta_version (Beta)"
echo "版本: $beta_releases"
echo "博客: $beta_blogs"
echo "========================================================"
fi
echo
echo "INFO =================================================="
echo " New CatWrt updates found!"
echo " Preview blog to learn more."
if [ -n "$stable_blogs" ]; then
echo " $stable_blogs"
elif [ -n "$beta_blogs" ]; then
echo " $beta_blogs"
fi
echo "========================================================"
sleep 1
else
echo
echo "INFO ================================================="
echo " Your CatWrt is latest version!"
echo "======================================================="
sleep 1
fi
}
current_version=$(grep 'version' /etc/catwrt_release | cut -d'=' -f2)
current_hash=$(grep 'hash' /etc/catwrt_release | cut -d'=' -f2)
arch=$(grep 'arch' /etc/catwrt_release | cut -d'=' -f2)
channel=$(echo "$api_data" | jq -r ".\"$current_version\".\"$arch\".channel")
check_update $current_version $current_hash $arch $channel
}
# Apply_repo
apply_repo() {
arch=$(grep -o 'arch=[^ ]*' $RELEASE | cut -d= -f2)
version=$(grep -o 'version=[^ ]*' $RELEASE | cut -d= -f2)
case "$arch" in
amd64)
case "$version" in
v22.12) REPO_URL="$BASE_URL/history/v22.12/amd64" ;;
v23.2) REPO_URL="$BASE_URL/history/v23.2/amd64" ;;
v23.8) REPO_URL="$BASE_URL/history/v23.8/amd64" ;;
v24.9) REPO_URL="$BASE_URL/amd64" ;;
*) echo "Unknown amd64 version: $version" && exit 1 ;;
esac
;;
mt798x)
case "$version" in
v22.12) REPO_URL="$BASE_URL/history/v22.12/aarch64_cortex-a53" ;;
v23.2) REPO_URL="$BASE_URL/history/v23.2/mt7986a" ;;
v23.8) REPO_URL="$BASE_URL/mt798x" ;;
v24.3) REPO_URL="$BASE_URL/pr/v24.3/mt798x" ;;
*) echo "Unknown mt798x version: $version" && exit 1 ;;
esac
;;
rock64)
case "$version" in
v22.12) REPO_URL="$BASE_URL/rkarm" ;;
v24.1) REPO_URL="$BASE_URL/pr/v24.1/rkarm" ;;
*) echo "Unknown rock64 version: $version" && exit 1 ;;
esac
;;
mt7621)
case "$version" in
v22.12) REPO_URL="$BASE_URL/history/v22.12/mt7621" ;;
v24.9) REPO_URL="$BASE_URL/mt7621" ;;
*) echo "Unknown mt7621 version: $version" && exit 1 ;;
esac
;;
*) echo "Unknown arch" && exit 1 ;;
esac
echo ""
echo "INFO ================================================================="
echo "软件源纯属免费分享,但你可以使用免费的境外软件源托管,如果你需要更快的速度请使用主站。"
echo "本人不对所有软件进行保证,我们没有提供第三方商业服务,使用风险需要自行承担。"
echo "你需要同意 CatWrt 软件源用户协议,请确认是否继续。 (10 秒内按 [Ctrl]+[C] 取消操作)"
if { [ "$arch" == "mt798x" ] && [ "$version" == "v24.3" ]; } || \
{ [ "$arch" == "rkarm" ] && [ "$version" == "v24.1" ]; }; then
echo "你目前使用的 BETA 版本,只能临时搭建的镜像站软件源,请注意关注 CatWrt 的更新情况,避免软件源失效!"
echo "============================================================================"
echo "请选择要使用的软件源:"
echo "1) netlify"
echo "2) vercel (默认)"
read -t 10 -p "Please enter your choice /// 请输入选择 (1-2): " choice
choice=${choice:-2}
case $choice in
1) conf_file="netlify.conf" ;;
2) conf_file="vercel.conf" ;;
*) conf_file="vercel.conf" ;;
esac
else
echo "============================================================================"
echo "请选择要使用的软件源:"
echo "1) repo.miaoer.xyz (主站)"
echo "2) Github-Pages"
echo "3) Cloudflare-Netlify"
echo "4) Netlify"
echo "5) Cloudflare-Vercel"
echo "6) Vercel (默认)"
read -t 10 -p "Please enter your choice /// 请输入选择 (1-5): " choice
choice=${choice:-6}
case $choice in
1)
echo "以赞助我们并获取支持代码,请访问链接: https://www.miaoer.net/sponsor"
echo "我们将使用用户支持的费用用于继续维护 CatWrt 及博客"
read -p "请输入支持代码: " sponsor_code
if [ "$sponsor_code" != "vme50" ]; then
echo "[ERROR] 支持代码无效,返回菜单选择其他软件源。"
apply_repo
return
fi
conf_file="distfeeds.conf"
;;
2) conf_file="github.conf" ;;
3) conf_file="cfnetlify.conf" ;;
4) conf_file="netlify.conf" ;;
5) conf_file="cfvercel.conf" ;;
6) conf_file="vercel.conf" ;;
*) conf_file="vercel.conf" ;;
esac
fi
CONF_PATH="$REPO_URL/$conf_file"
if curl --output /dev/null --silent --head --fail "$CONF_PATH"; then
echo "[INFO] 使用 $CONF_PATH"
else
echo "[Error] 源文件: $CONF_PATH 不存在请反馈"
exit 1
fi
curl -sL "$CONF_PATH" -o /etc/opkg/distfeeds.conf
if [ -f /var/lock/opkg.lock ]; then
rm /var/lock/opkg.lock
fi
# fack istore_compat
if [ -f /var/opkg-lists/istore_compat ]; then
rm /var/opkg-lists/istore_compat
fi
opkg update
echo "[INFO] 软件源配置已完成可以通过 opkg install pkg 来安装插件/组件/内核模块!"
}
# catnd
catnd() {
echo "$(date) - Starting CatWrt Network Diagnostics"
echo
# Ping & PPPoE
ping -c 3 223.5.5.5 >/dev/null
if [ $? -eq 0 ]; then
echo "[Ping] Network connection succeeded!"
echo
else
ping -c 3 119.29.29.99 >/dev/null
if [ $? -eq 0 ]; then
echo "[Ping] Network connection succeeded,But there may be problems!"
echo
else
pppoe_config=$(grep 'pppoe' /etc/config/network)
if [ ! -z "$pppoe_config" ]; then
echo "[PPPoE] Please check if your PPPoE account and password are correct."
echo
fi
exit 1
fi
fi
# DNS
valid_dns="1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4 223.6.6.6 223.5.5.5 180.76.76.76 208.67.222.222 208.67.220.220 119.29.29.99"
dns_config=$(grep 'option dns' /etc/config/network)
dns_servers=$(echo $dns_config | awk -F "'" '{print $2}')
for ip in $dns_servers; do
if ! [[ $valid_dns =~ (^|[ ])$ip($|[ ]) ]]; then
echo "[DNS] Recommended to delete DNS $ip"
echo
exit 1
fi
done
# Bad DNS
echo "[DNS] DNS configuration looks good!"
echo
bad_dns="114.114.114.114 114.114.115.115 119.29.29.29"
if [[ $dns_config =~ $bad_dns ]]; then
echo "[DNS] DNS may be polluted or unreliable"
echo
fi
# nslookup
nslookup bilibili.com >/dev/null
if [ $? -ne 0 ]; then
nslookup www.miaoer.net >/dev/null
if [ $? -eq 0 ]; then
echo "[DNS] DNS resolution succeeded"
echo
else
echo "[DNS] NS resolution failed for 'www.miaoer.net'"
echo "[DNS] Your DNS server may have issues"
echo
fi
fi
# Public IP
echo CatWrt IPv4 Addr: $(curl --silent --connect-timeout 5 4.ipw.cn)
echo
curl 6.ipw.cn --connect-timeout 5 >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "[IPv6] IPv6 network connection timed out"
echo
else
echo CatWrt IPv6 Addr: $(curl --silent 6.ipw.cn)
echo
fi
# IPv6
resp=$(curl --silent test.ipw.cn)
if echo "$resp" | grep -q -E '240e|2408|2409|2401'; then
echo "[IPv6] IPv6 access is preferred"
echo
else
echo "[IPv6] IPv4 access is preferred"
echo
fi
# Default IP
ipaddr_config=$(grep '192.168.1.4' /etc/config/network)
if [ -z "$ipaddr_config" ]; then
echo "[Default-IP] address is not the catwrt default 192.168.1.4"
echo "Please configure your network at 'https://www.miaoer.net/posts/network/quickstart-catwrt'"
echo
fi
# Bypass Gateway
wan_config=$(grep 'config interface' /etc/config/network | grep 'wan')
if [ -z "$wan_config" ]; then
echo "[Bypass Gateway] No config for 'wan' interface found in /etc/config/network"
echo "Please check if your device is set as a Bypass Gateway"
echo
fi
# Rotuer Mode(PPPoE)
pass_config=$(grep 'password' /etc/config/network)
user_config=$(grep 'username' /etc/config/network)
pppoe_config=$(grep 'pppoe' /etc/config/network)
if [ -n "$pass_config" ] && [ -n "$user_config" ] && [ -n "$pppoe_config" ]; then
echo "[PPPoE] PPPoE Rotuer Mode"
echo
else
echo "[PPPoE] DHCP protocol detected in WAN interface"
echo "The device may not be in PPPoE Rotuer Mode"
echo
fi
# IPv6 WAN6
grep 'config interface' /etc/config/network | grep 'wan6' >/dev/null
if [ $? -ne 0 ]; then
echo "[wan6] Your IPv6 network may have issues"
echo
fi
grep 'dhcpv6' /etc/config/network >/dev/null
if [ $? -ne 0 ]; then
echo "[wan6] Your IPv6 network may have issues"
echo
fi
# Tcping
echo "[Tcping] Testing..."
tcping -q -c 1 cn.bing.com
[ $? -ne 0 ] && echo "Failed: cn.bing.com"
tcping -q -c 1 bilibili.com
[ $? -ne 0 ] && echo "Failed: bilibili.com"
tcping -q -c 1 github.com
[ $? -ne 0 ] && echo "Failed: github.com"
tcping -q -c 1 google.com.hk
[ $? -ne 0 ] && echo "Failed: google.com.hk"
echo
echo "$(date) - Network check completed!"
echo "CatWrt Network Diagnostics by @miaoermua"
}
# Sysupgrade
sysupgrade() {
arch=$(cat /etc/catwrt_release | grep "^arch=" | cut -d'=' -f2)
board_name=$(cat /tmp/sysinfo/board_name)
# 检查架构
if [ "$arch" != "mt7621" ] && [ "$arch" != "amd64" ] && [ "$arch" != "mt798x" ]; then
echo "[Error] 不支持的架构: $arch"
exit 1
fi
if [ "$arch" = "mt7621" ]; then
case "$board_name" in
*"newifi-d2")
base_fw_url="sysup_newifi-d2"
;;
*"redmi-router-ac2100")
base_fw_url="sysup_redmi-router-ac2100"
;;
*"mi-router-ac2100")
base_fw_url="sysup_mi-router-ac2100"
;;
*)
echo "[Error] 该设备不支持通过此 Cattools 进行系统升级,请反馈给我们以支持!"
exit 1
;;
esac
firmware_url="$mt7621_sysup$base_fw_url"
fi
if [ "$arch" = "amd64" ]; then
echo
disk_size=$(fdisk -l /dev/sda | grep "Disk /dev/sda:" | awk '{print $3}')
tolerance=$(echo "200/1024/1024" | bc -l)
allowed_size=800.28
min_size=$(echo "$allowed_size - $tolerance" | bc -l)
max_size=$(echo "$allowed_size + $tolerance" | bc -l)
if (($(echo "$disk_size < $min_size" | bc -l))) || (($(echo "$disk_size > $max_size" | bc -l))); then
echo "[Error] 磁盘空间出现过修改或不匹配,无法继续升级。"
exit 1
fi
efi_mode=0
if [ -d /sys/firmware/efi ]; then
efi_mode=1
fi
if [ -e /dev/sda128 ] || [ -e /dev/vda128 ] || [ -e /dev/nvme0n1p128 ] || [ $efi_mode -eq 1 ]; then
firmware_url=$AMD64_EFI_SYSUP
else
firmware_url=$AMD64_BIOS_SYSUP
fi
fi
if [ "$arch" = "mt798x" ]; then
case "$board_name" in
*"redmi-router-ax6000")
base_fw_url="sysup_redmi-router-ax6000"
;;
*"tl-xdr6088")
base_fw_url="sysup_tl-xdr6088"
;;
*"tl-xdr4288")
base_fw_url="sysup_tl-xdr4288"
;;
*"tl-xdr6086")
base_fw_url="sysup_tl-xdr6086"
;;
*)
echo "[Error] 该设备不支持通过此 Cattools 进行系统升级,请反馈给我们以支持!"
exit 1