forked from P3TERX/warp.sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
warp.sh
executable file
·1246 lines (1145 loc) · 38.5 KB
/
warp.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
#!/usr/bin/env bash
#
# https://github.com/P3TERX/warp.sh
# Description: Cloudflare WARP Installer
# System Required: Debian, Ubuntu, Fedora, CentOS, Oracle Linux, Arch Linux
# Version: beta38
#
# MIT License
#
# Copyright (c) 2021-2022 P3TERX <https://p3terx.com>
#
# 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.
#
shVersion='beta38'
FontColor_Red="\033[31m"
FontColor_Red_Bold="\033[1;31m"
FontColor_Green="\033[32m"
FontColor_Green_Bold="\033[1;32m"
FontColor_Yellow="\033[33m"
FontColor_Yellow_Bold="\033[1;33m"
FontColor_Purple="\033[35m"
FontColor_Purple_Bold="\033[1;35m"
FontColor_Suffix="\033[0m"
log() {
local LEVEL="$1"
local MSG="$2"
case "${LEVEL}" in
INFO)
local LEVEL="[${FontColor_Green}${LEVEL}${FontColor_Suffix}]"
local MSG="${LEVEL} ${MSG}"
;;
WARN)
local LEVEL="[${FontColor_Yellow}${LEVEL}${FontColor_Suffix}]"
local MSG="${LEVEL} ${MSG}"
;;
ERROR)
local LEVEL="[${FontColor_Red}${LEVEL}${FontColor_Suffix}]"
local MSG="${LEVEL} ${MSG}"
;;
*) ;;
esac
echo -e "${MSG}"
}
if [[ $(uname -s) != Linux ]]; then
log ERROR "This operating system is not supported."
exit 1
fi
if [[ $(id -u) != 0 ]]; then
log ERROR "This script must be run as root."
exit 1
fi
if [[ -z $(command -v curl) ]]; then
log ERROR "cURL is not installed."
exit 1
fi
WGCF_Profile='wgcf-profile.conf'
WGCF_ProfileDir="/etc/warp"
WGCF_ProfilePath="${WGCF_ProfileDir}/${WGCF_Profile}"
WireGuard_Interface='wgcf'
WireGuard_ConfPath="/etc/wireguard/${WireGuard_Interface}.conf"
WireGuard_Interface_DNS_IPv4='8.8.8.8,8.8.4.4'
WireGuard_Interface_DNS_IPv6='2001:4860:4860::8888,2001:4860:4860::8844'
WireGuard_Interface_DNS_46="${WireGuard_Interface_DNS_IPv4},${WireGuard_Interface_DNS_IPv6}"
WireGuard_Interface_DNS_64="${WireGuard_Interface_DNS_IPv6},${WireGuard_Interface_DNS_IPv4}"
WireGuard_Interface_Rule_table='51888'
WireGuard_Interface_Rule_fwmark='51888'
WireGuard_Interface_MTU='1280'
WireGuard_Peer_Endpoint_IP4='162.159.192.1'
WireGuard_Peer_Endpoint_IP6='2606:4700:d0::a29f:c001'
WireGuard_Peer_Endpoint_IPv4="${WireGuard_Peer_Endpoint_IP4}:2408"
WireGuard_Peer_Endpoint_IPv6="[${WireGuard_Peer_Endpoint_IP6}]:2408"
WireGuard_Peer_Endpoint_Domain='engage.cloudflareclient.com:2408'
WireGuard_Peer_AllowedIPs_IPv4='0.0.0.0/0'
WireGuard_Peer_AllowedIPs_IPv6='::/0'
WireGuard_Peer_AllowedIPs_DualStack='0.0.0.0/0,::/0'
TestIPv4_1='1.0.0.1'
TestIPv4_2='9.9.9.9'
TestIPv6_1='2606:4700:4700::1001'
TestIPv6_2='2620:fe::fe'
CF_Trace_URL='https://www.cloudflare.com/cdn-cgi/trace'
Get_System_Info() {
source /etc/os-release
SysInfo_OS_CodeName="${VERSION_CODENAME}"
SysInfo_OS_Name_lowercase="${ID}"
SysInfo_OS_Name_Full="${PRETTY_NAME}"
SysInfo_RelatedOS="${ID_LIKE}"
SysInfo_Kernel="$(uname -r)"
SysInfo_Kernel_Ver_major="$(uname -r | awk -F . '{print $1}')"
SysInfo_Kernel_Ver_minor="$(uname -r | awk -F . '{print $2}')"
SysInfo_Arch="$(uname -m)"
SysInfo_Virt="$(systemd-detect-virt)"
case ${SysInfo_RelatedOS} in
*fedora* | *rhel*)
SysInfo_OS_Ver_major="$(rpm -E '%{rhel}')"
;;
*)
SysInfo_OS_Ver_major="$(echo ${VERSION_ID} | cut -d. -f1)"
;;
esac
}
Print_System_Info() {
echo -e "
System Information
---------------------------------------------------
Operating System: ${SysInfo_OS_Name_Full}
Linux Kernel: ${SysInfo_Kernel}
Architecture: ${SysInfo_Arch}
Virtualization: ${SysInfo_Virt}
---------------------------------------------------
"
}
Install_Requirements_Debian() {
if [[ ! $(command -v gpg) ]]; then
apt update
apt install gnupg -y
fi
if [[ ! $(apt list 2>/dev/null | grep apt-transport-https | grep installed) ]]; then
apt update
apt install apt-transport-https -y
fi
}
Install_WARP_Client_Debian() {
if [[ ${SysInfo_OS_Name_lowercase} = ubuntu ]]; then
OS_CodeName='focal'
elif [[ ${SysInfo_OS_Name_lowercase} = debian ]]; then
case ${SysInfo_OS_Ver_major} in
11)
OS_CodeName='bullseye'
;;
10)
OS_CodeName='buster'
;;
*)
log ERROR "This operating system is not supported."
exit 1
;;
esac
elif [[ ${SysInfo_RelatedOS} = *debian* ]]; then
OS_CodeName='buster'
else
OS_CodeName="${SysInfo_OS_CodeName}"
fi
Install_Requirements_Debian
curl https://pkg.cloudflareclient.com/pubkey.gpg | gpg --yes --dearmor --output /usr/share/keyrings/cloudflare-warp-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/cloudflare-warp-archive-keyring.gpg] https://pkg.cloudflareclient.com/ ${OS_CodeName} main" | tee /etc/apt/sources.list.d/cloudflare-client.list
apt update
apt install cloudflare-warp -y
}
Install_WARP_Client_CentOS() {
if [[ ${SysInfo_OS_Ver_major} = 8 ]]; then
rpm -ivh http://pkg.cloudflareclient.com/cloudflare-release-el8.rpm
yum install cloudflare-warp -y
else
log ERROR "This operating system is not supported."
exit 1
fi
}
Check_WARP_Client() {
WARP_Client_Status=$(systemctl is-active warp-svc)
WARP_Client_SelfStart=$(systemctl is-enabled warp-svc 2>/dev/null)
}
Install_WARP_Client() {
Print_System_Info
log INFO "Installing Cloudflare WARP Client..."
if [[ ${SysInfo_Arch} != x86_64 ]]; then
log ERROR "This CPU architecture is not supported: ${SysInfo_Arch}"
exit 1
fi
case ${SysInfo_OS_Name_lowercase} in
*debian* | *ubuntu*)
Install_WARP_Client_Debian
;;
*centos* | *rhel*)
Install_WARP_Client_CentOS
;;
*)
if [[ ${SysInfo_RelatedOS} = *debian* ]]; then
Install_WARP_Client_Debian
elif [[ ${SysInfo_RelatedOS} = *rhel* || ${SysInfo_RelatedOS} = *fedora* ]]; then
Install_WARP_Client_CentOS
else
log ERROR "This operating system is not supported."
exit 1
fi
;;
esac
Check_WARP_Client
if [[ ${WARP_Client_Status} = active ]]; then
log INFO "Cloudflare WARP Client installed successfully!"
else
log ERROR "warp-svc failure to run!"
journalctl -u warp-svc --no-pager
exit 1
fi
}
Uninstall_WARP_Client() {
log INFO "Uninstalling Cloudflare WARP Client..."
case ${SysInfo_OS_Name_lowercase} in
*debian* | *ubuntu*)
apt purge cloudflare-warp -y
rm -f /etc/apt/sources.list.d/cloudflare-client.list /usr/share/keyrings/cloudflare-warp-archive-keyring.gpg
;;
*centos* | *rhel*)
yum remove cloudflare-warp -y
;;
*)
if [[ ${SysInfo_RelatedOS} = *rhel* || ${SysInfo_RelatedOS} = *fedora* ]]; then
yum remove cloudflare-warp -y
else
log ERROR "This operating system is not supported."
exit 1
fi
;;
esac
}
Restart_WARP_Client() {
log INFO "Restarting Cloudflare WARP Client..."
systemctl restart warp-svc
Check_WARP_Client
if [[ ${WARP_Client_Status} = active ]]; then
log INFO "Cloudflare WARP Client has been restarted."
else
log ERROR "Cloudflare WARP Client failure to run!"
journalctl -u warp-svc --no-pager
exit 1
fi
}
Init_WARP_Client() {
Check_WARP_Client
if [[ ${WARP_Client_SelfStart} != enabled || ${WARP_Client_Status} != active ]]; then
Install_WARP_Client
fi
if [[ $(warp-cli --accept-tos account) = *Missing* ]]; then
log INFO "Cloudflare WARP Account Registration in progress..."
warp-cli --accept-tos register
fi
}
Connect_WARP() {
log INFO "Connecting to WARP..."
warp-cli --accept-tos connect
log INFO "Enable WARP Always-On..."
warp-cli --accept-tos enable-always-on
}
Disconnect_WARP() {
log INFO "Disable WARP Always-On..."
warp-cli --accept-tos disable-always-on
log INFO "Disconnect from WARP..."
warp-cli --accept-tos disconnect
}
Set_WARP_Mode_Proxy() {
log INFO "Setting up WARP Proxy Mode..."
warp-cli --accept-tos set-mode proxy
}
Enable_WARP_Client_Proxy() {
Init_WARP_Client
Set_WARP_Mode_Proxy
Connect_WARP
Print_WARP_Client_Status
}
Get_WARP_Proxy_Port() {
WARP_Proxy_Port='40000'
}
Print_Delimiter() {
printf '=%.0s' $(seq $(tput cols))
echo
}
Install_wgcf() {
curl -fsSL git.io/wgcf.sh | bash
}
Uninstall_wgcf() {
rm -f /usr/local/bin/wgcf
}
Register_WARP_Account() {
while [[ ! -f wgcf-account.toml ]]; do
Install_wgcf
log INFO "Cloudflare WARP Account registration in progress..."
yes | wgcf register
sleep 5
done
}
Generate_WGCF_Profile() {
while [[ ! -f ${WGCF_Profile} ]]; do
Register_WARP_Account
log INFO "WARP WireGuard profile (wgcf-profile.conf) generation in progress..."
wgcf generate
done
Uninstall_wgcf
}
Backup_WGCF_Profile() {
mkdir -p ${WGCF_ProfileDir}
mv -f wgcf* ${WGCF_ProfileDir}
}
Read_WGCF_Profile() {
WireGuard_Interface_PrivateKey=$(cat ${WGCF_ProfilePath} | grep ^PrivateKey | cut -d= -f2- | awk '$1=$1')
WireGuard_Interface_Address=$(cat ${WGCF_ProfilePath} | grep ^Address | cut -d= -f2- | awk '$1=$1' | sed ":a;N;s/\n/,/g;ta")
WireGuard_Peer_PublicKey=$(cat ${WGCF_ProfilePath} | grep ^PublicKey | cut -d= -f2- | awk '$1=$1')
WireGuard_Interface_Address_IPv4=$(echo ${WireGuard_Interface_Address} | cut -d, -f1 | cut -d'/' -f1)
WireGuard_Interface_Address_IPv6=$(echo ${WireGuard_Interface_Address} | cut -d, -f2 | cut -d'/' -f1)
}
Load_WGCF_Profile() {
if [[ -f ${WGCF_Profile} ]]; then
Backup_WGCF_Profile
Read_WGCF_Profile
elif [[ -f ${WGCF_ProfilePath} ]]; then
Read_WGCF_Profile
else
Generate_WGCF_Profile
Backup_WGCF_Profile
Read_WGCF_Profile
fi
}
Install_WireGuardTools_Debian() {
case ${SysInfo_OS_Ver_major} in
10)
if [[ -z $(grep "^deb.*buster-backports.*main" /etc/apt/sources.list{,.d/*}) ]]; then
echo "deb http://deb.debian.org/debian buster-backports main" | tee /etc/apt/sources.list.d/backports.list
fi
;;
*)
if [[ ${SysInfo_OS_Ver_major} -lt 10 ]]; then
log ERROR "This operating system is not supported."
exit 1
fi
;;
esac
apt update
apt install iproute2 openresolv -y
apt install wireguard-tools --no-install-recommends -y
}
Install_WireGuardTools_Ubuntu() {
apt update
apt install iproute2 openresolv -y
apt install wireguard-tools --no-install-recommends -y
}
Install_WireGuardTools_CentOS() {
yum install epel-release -y || yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-${SysInfo_OS_Ver_major}.noarch.rpm -y
yum install iproute iptables wireguard-tools -y
}
Install_WireGuardTools_Fedora() {
dnf install iproute iptables wireguard-tools -y
}
Install_WireGuardTools_Arch() {
pacman -Sy iproute2 openresolv wireguard-tools --noconfirm
}
Install_WireGuardTools() {
log INFO "Installing wireguard-tools..."
case ${SysInfo_OS_Name_lowercase} in
*debian*)
Install_WireGuardTools_Debian
;;
*ubuntu*)
Install_WireGuardTools_Ubuntu
;;
*centos* | *rhel*)
Install_WireGuardTools_CentOS
;;
*fedora*)
Install_WireGuardTools_Fedora
;;
*arch*)
Install_WireGuardTools_Arch
;;
*)
if [[ ${SysInfo_RelatedOS} = *rhel* || ${SysInfo_RelatedOS} = *fedora* ]]; then
Install_WireGuardTools_CentOS
else
log ERROR "This operating system is not supported."
exit 1
fi
;;
esac
}
Install_WireGuardGo() {
case ${SysInfo_Virt} in
openvz | lxc*)
curl -fsSL git.io/wireguard-go.sh | bash
;;
*)
if [[ ${SysInfo_Kernel_Ver_major} -lt 5 || ${SysInfo_Kernel_Ver_minor} -lt 6 ]]; then
curl -fsSL git.io/wireguard-go.sh | bash
fi
;;
esac
}
Check_WireGuard() {
WireGuard_Status=$(systemctl is-active wg-quick@${WireGuard_Interface})
WireGuard_SelfStart=$(systemctl is-enabled wg-quick@${WireGuard_Interface} 2>/dev/null)
}
Install_WireGuard() {
Print_System_Info
Check_WireGuard
if [[ ${WireGuard_SelfStart} != enabled || ${WireGuard_Status} != active ]]; then
Install_WireGuardTools
Install_WireGuardGo
else
log INFO "WireGuard is installed and running."
fi
}
Start_WireGuard() {
Check_WARP_Client
log INFO "Starting WireGuard..."
if [[ ${WARP_Client_Status} = active ]]; then
systemctl stop warp-svc
systemctl enable wg-quick@${WireGuard_Interface} --now
systemctl start warp-svc
else
systemctl enable wg-quick@${WireGuard_Interface} --now
fi
Check_WireGuard
if [[ ${WireGuard_Status} = active ]]; then
log INFO "WireGuard is running."
else
log ERROR "WireGuard failure to run!"
journalctl -u wg-quick@${WireGuard_Interface} --no-pager
exit 1
fi
}
Restart_WireGuard() {
Check_WARP_Client
log INFO "Restarting WireGuard..."
if [[ ${WARP_Client_Status} = active ]]; then
systemctl stop warp-svc
systemctl restart wg-quick@${WireGuard_Interface}
systemctl start warp-svc
else
systemctl restart wg-quick@${WireGuard_Interface}
fi
Check_WireGuard
if [[ ${WireGuard_Status} = active ]]; then
log INFO "WireGuard has been restarted."
else
log ERROR "WireGuard failure to run!"
journalctl -u wg-quick@${WireGuard_Interface} --no-pager
exit 1
fi
}
Enable_IPv6_Support() {
if [[ $(sysctl -a | grep 'disable_ipv6.*=.*1') || $(cat /etc/sysctl.{conf,d/*} | grep 'disable_ipv6.*=.*1') ]]; then
sed -i '/disable_ipv6/d' /etc/sysctl.{conf,d/*}
echo 'net.ipv6.conf.all.disable_ipv6 = 0' >/etc/sysctl.d/ipv6.conf
sysctl -w net.ipv6.conf.all.disable_ipv6=0
fi
}
Enable_WireGuard() {
Enable_IPv6_Support
Check_WireGuard
if [[ ${WireGuard_SelfStart} = enabled ]]; then
Restart_WireGuard
else
Start_WireGuard
fi
}
Stop_WireGuard() {
Check_WARP_Client
if [[ ${WireGuard_Status} = active ]]; then
log INFO "Stoping WireGuard..."
if [[ ${WARP_Client_Status} = active ]]; then
systemctl stop warp-svc
systemctl stop wg-quick@${WireGuard_Interface}
systemctl start warp-svc
else
systemctl stop wg-quick@${WireGuard_Interface}
fi
Check_WireGuard
if [[ ${WireGuard_Status} != active ]]; then
log INFO "WireGuard has been stopped."
else
log ERROR "WireGuard stop failure!"
fi
else
log INFO "WireGuard is stopped."
fi
}
Disable_WireGuard() {
Check_WARP_Client
Check_WireGuard
if [[ ${WireGuard_SelfStart} = enabled || ${WireGuard_Status} = active ]]; then
log INFO "Disabling WireGuard..."
if [[ ${WARP_Client_Status} = active ]]; then
systemctl stop warp-svc
systemctl disable wg-quick@${WireGuard_Interface} --now
systemctl start warp-svc
else
systemctl disable wg-quick@${WireGuard_Interface} --now
fi
Check_WireGuard
if [[ ${WireGuard_SelfStart} != enabled && ${WireGuard_Status} != active ]]; then
log INFO "WireGuard has been disabled."
else
log ERROR "WireGuard disable failure!"
fi
else
log INFO "WireGuard is disabled."
fi
}
Print_WireGuard_Log() {
journalctl -u wg-quick@${WireGuard_Interface} -f
}
Check_Network_Status_IPv4() {
if ping -c1 -W1 ${TestIPv4_1} >/dev/null 2>&1 || ping -c1 -W1 ${TestIPv4_2} >/dev/null 2>&1; then
IPv4Status='on'
else
IPv4Status='off'
fi
}
Check_Network_Status_IPv6() {
if ping6 -c1 -W1 ${TestIPv6_1} >/dev/null 2>&1 || ping6 -c1 -W1 ${TestIPv6_2} >/dev/null 2>&1; then
IPv6Status='on'
else
IPv6Status='off'
fi
}
Check_Network_Status() {
Disable_WireGuard
Check_Network_Status_IPv4
Check_Network_Status_IPv6
}
Check_IPv4_addr() {
IPv4_addr=$(
ip route get ${TestIPv4_1} 2>/dev/null | grep -oP 'src \K\S+' ||
ip route get ${TestIPv4_2} 2>/dev/null | grep -oP 'src \K\S+'
)
}
Check_IPv6_addr() {
IPv6_addr=$(
ip route get ${TestIPv6_1} 2>/dev/null | grep -oP 'src \K\S+' ||
ip route get ${TestIPv6_2} 2>/dev/null | grep -oP 'src \K\S+'
)
}
Get_IP_addr() {
Check_Network_Status
if [[ ${IPv4Status} = on ]]; then
log INFO "Getting the network interface IPv4 address..."
Check_IPv4_addr
if [[ ${IPv4_addr} ]]; then
log INFO "IPv4 Address: ${IPv4_addr}"
else
log WARN "Network interface IPv4 address not obtained."
fi
fi
if [[ ${IPv6Status} = on ]]; then
log INFO "Getting the network interface IPv6 address..."
Check_IPv6_addr
if [[ ${IPv6_addr} ]]; then
log INFO "IPv6 Address: ${IPv6_addr}"
else
log WARN "Network interface IPv6 address not obtained."
fi
fi
}
Get_WireGuard_Interface_MTU() {
log INFO "Getting the best MTU value for WireGuard..."
MTU_Preset=1500
MTU_Increment=10
if [[ ${IPv4Status} = off && ${IPv6Status} = on ]]; then
CMD_ping='ping6'
MTU_TestIP_1="${TestIPv6_1}"
MTU_TestIP_2="${TestIPv6_2}"
else
CMD_ping='ping'
MTU_TestIP_1="${TestIPv4_1}"
MTU_TestIP_2="${TestIPv4_2}"
fi
while true; do
if ${CMD_ping} -c1 -W1 -s$((${MTU_Preset} - 28)) -Mdo ${MTU_TestIP_1} >/dev/null 2>&1 || ${CMD_ping} -c1 -W1 -s$((${MTU_Preset} - 28)) -Mdo ${MTU_TestIP_2} >/dev/null 2>&1; then
MTU_Increment=1
MTU_Preset=$((${MTU_Preset} + ${MTU_Increment}))
else
MTU_Preset=$((${MTU_Preset} - ${MTU_Increment}))
if [[ ${MTU_Increment} = 1 ]]; then
break
fi
fi
if [[ ${MTU_Preset} -le 1360 ]]; then
log WARN "MTU is set to the lowest value."
MTU_Preset='1360'
break
fi
done
WireGuard_Interface_MTU=$((${MTU_Preset} - 80))
log INFO "WireGuard MTU: ${WireGuard_Interface_MTU}"
}
Generate_WireGuardProfile_Interface() {
Get_WireGuard_Interface_MTU
log INFO "WireGuard profile (${WireGuard_ConfPath}) generation in progress..."
cat <<EOF >${WireGuard_ConfPath}
# Generated by P3TERX/warp.sh
# Visit https://github.com/P3TERX/warp.sh for more information
[Interface]
PrivateKey = ${WireGuard_Interface_PrivateKey}
Address = ${WireGuard_Interface_Address}
DNS = ${WireGuard_Interface_DNS}
MTU = ${WireGuard_Interface_MTU}
EOF
}
Generate_WireGuardProfile_Interface_Rule_TableOff() {
cat <<EOF >>${WireGuard_ConfPath}
Table = off
EOF
}
Generate_WireGuardProfile_Interface_Rule_IPv4_nonGlobal() {
cat <<EOF >>${WireGuard_ConfPath}
PostUP = ip -4 route add default dev ${WireGuard_Interface} table ${WireGuard_Interface_Rule_table}
PostUP = ip -4 rule add from ${WireGuard_Interface_Address_IPv4} lookup ${WireGuard_Interface_Rule_table}
PostDown = ip -4 rule delete from ${WireGuard_Interface_Address_IPv4} lookup ${WireGuard_Interface_Rule_table}
PostUP = ip -4 rule add fwmark ${WireGuard_Interface_Rule_fwmark} lookup ${WireGuard_Interface_Rule_table}
PostDown = ip -4 rule delete fwmark ${WireGuard_Interface_Rule_fwmark} lookup ${WireGuard_Interface_Rule_table}
PostUP = ip -4 rule add table main suppress_prefixlength 0
PostDown = ip -4 rule delete table main suppress_prefixlength 0
EOF
}
Generate_WireGuardProfile_Interface_Rule_IPv6_nonGlobal() {
cat <<EOF >>${WireGuard_ConfPath}
PostUP = ip -6 route add default dev ${WireGuard_Interface} table ${WireGuard_Interface_Rule_table}
PostUP = ip -6 rule add from ${WireGuard_Interface_Address_IPv6} lookup ${WireGuard_Interface_Rule_table}
PostDown = ip -6 rule delete from ${WireGuard_Interface_Address_IPv6} lookup ${WireGuard_Interface_Rule_table}
PostUP = ip -6 rule add fwmark ${WireGuard_Interface_Rule_fwmark} lookup ${WireGuard_Interface_Rule_table}
PostDown = ip -6 rule delete fwmark ${WireGuard_Interface_Rule_fwmark} lookup ${WireGuard_Interface_Rule_table}
PostUP = ip -6 rule add table main suppress_prefixlength 0
PostDown = ip -6 rule delete table main suppress_prefixlength 0
EOF
}
Generate_WireGuardProfile_Interface_Rule_DualStack_nonGlobal() {
Generate_WireGuardProfile_Interface_Rule_TableOff
Generate_WireGuardProfile_Interface_Rule_IPv4_nonGlobal
Generate_WireGuardProfile_Interface_Rule_IPv6_nonGlobal
}
Generate_WireGuardProfile_Interface_Rule_IPv4_Global_srcIP() {
cat <<EOF >>${WireGuard_ConfPath}
PostUp = ip -4 rule add from ${IPv4_addr} lookup main prio 18
PostDown = ip -4 rule delete from ${IPv4_addr} lookup main prio 18
EOF
}
Generate_WireGuardProfile_Interface_Rule_IPv6_Global_srcIP() {
cat <<EOF >>${WireGuard_ConfPath}
PostUp = ip -6 rule add from ${IPv6_addr} lookup main prio 18
PostDown = ip -6 rule delete from ${IPv6_addr} lookup main prio 18
EOF
}
Generate_WireGuardProfile_Peer() {
cat <<EOF >>${WireGuard_ConfPath}
[Peer]
PublicKey = ${WireGuard_Peer_PublicKey}
AllowedIPs = ${WireGuard_Peer_AllowedIPs}
Endpoint = ${WireGuard_Peer_Endpoint}
EOF
}
Check_WARP_Client_Status() {
Check_WARP_Client
case ${WARP_Client_Status} in
active)
WARP_Client_Status_en="${FontColor_Green}Running${FontColor_Suffix}"
WARP_Client_Status_zh="${FontColor_Green}运行中${FontColor_Suffix}"
;;
*)
WARP_Client_Status_en="${FontColor_Red}Stopped${FontColor_Suffix}"
WARP_Client_Status_zh="${FontColor_Red}未运行${FontColor_Suffix}"
;;
esac
}
Check_WARP_Proxy_Status() {
Check_WARP_Client
if [[ ${WARP_Client_Status} = active ]]; then
Get_WARP_Proxy_Port
WARP_Proxy_Status=$(curl -sx "socks5h://127.0.0.1:${WARP_Proxy_Port}" ${CF_Trace_URL} --connect-timeout 2 | grep warp | cut -d= -f2)
else
unset WARP_Proxy_Status
fi
case ${WARP_Proxy_Status} in
on)
WARP_Proxy_Status_en="${FontColor_Green}${WARP_Proxy_Port}${FontColor_Suffix}"
WARP_Proxy_Status_zh="${WARP_Proxy_Status_en}"
;;
plus)
WARP_Proxy_Status_en="${FontColor_Green}${WARP_Proxy_Port}(WARP+)${FontColor_Suffix}"
WARP_Proxy_Status_zh="${WARP_Proxy_Status_en}"
;;
*)
WARP_Proxy_Status_en="${FontColor_Red}Off${FontColor_Suffix}"
WARP_Proxy_Status_zh="${FontColor_Red}未开启${FontColor_Suffix}"
;;
esac
}
Check_WireGuard_Status() {
Check_WireGuard
case ${WireGuard_Status} in
active)
WireGuard_Status_en="${FontColor_Green}Running${FontColor_Suffix}"
WireGuard_Status_zh="${FontColor_Green}运行中${FontColor_Suffix}"
;;
*)
WireGuard_Status_en="${FontColor_Red}Stopped${FontColor_Suffix}"
WireGuard_Status_zh="${FontColor_Red}未运行${FontColor_Suffix}"
;;
esac
}
Check_WARP_WireGuard_Status() {
Check_Network_Status_IPv4
if [[ ${IPv4Status} = on ]]; then
WARP_IPv4_Status=$(curl -s4 ${CF_Trace_URL} --connect-timeout 2 | grep warp | cut -d= -f2)
else
unset WARP_IPv4_Status
fi
case ${WARP_IPv4_Status} in
on)
WARP_IPv4_Status_en="${FontColor_Green}WARP${FontColor_Suffix}"
WARP_IPv4_Status_zh="${WARP_IPv4_Status_en}"
;;
plus)
WARP_IPv4_Status_en="${FontColor_Green}WARP+${FontColor_Suffix}"
WARP_IPv4_Status_zh="${WARP_IPv4_Status_en}"
;;
off)
WARP_IPv4_Status_en="Normal"
WARP_IPv4_Status_zh="正常"
;;
*)
Check_Network_Status_IPv4
if [[ ${IPv4Status} = on ]]; then
WARP_IPv4_Status_en="Normal"
WARP_IPv4_Status_zh="正常"
else
WARP_IPv4_Status_en="${FontColor_Red}Unconnected${FontColor_Suffix}"
WARP_IPv4_Status_zh="${FontColor_Red}未连接${FontColor_Suffix}"
fi
;;
esac
Check_Network_Status_IPv6
if [[ ${IPv6Status} = on ]]; then
WARP_IPv6_Status=$(curl -s6 ${CF_Trace_URL} --connect-timeout 2 | grep warp | cut -d= -f2)
else
unset WARP_IPv6_Status
fi
case ${WARP_IPv6_Status} in
on)
WARP_IPv6_Status_en="${FontColor_Green}WARP${FontColor_Suffix}"
WARP_IPv6_Status_zh="${WARP_IPv6_Status_en}"
;;
plus)
WARP_IPv6_Status_en="${FontColor_Green}WARP+${FontColor_Suffix}"
WARP_IPv6_Status_zh="${WARP_IPv6_Status_en}"
;;
off)
WARP_IPv6_Status_en="Normal"
WARP_IPv6_Status_zh="正常"
;;
*)
Check_Network_Status_IPv6
if [[ ${IPv6Status} = on ]]; then
WARP_IPv6_Status_en="Normal"
WARP_IPv6_Status_zh="正常"
else
WARP_IPv6_Status_en="${FontColor_Red}Unconnected${FontColor_Suffix}"
WARP_IPv6_Status_zh="${FontColor_Red}未连接${FontColor_Suffix}"
fi
;;
esac
if [[ ${IPv4Status} = off && ${IPv6Status} = off ]]; then
log ERROR "Cloudflare WARP network anomaly, WireGuard tunnel established failed."
Disable_WireGuard
exit 1
fi
}
Check_ALL_Status() {
Check_WARP_Client_Status
Check_WARP_Proxy_Status
Check_WireGuard_Status
Check_WARP_WireGuard_Status
}
Print_WARP_Client_Status() {
log INFO "Status check in progress..."
sleep 3
Check_WARP_Client_Status
Check_WARP_Proxy_Status
echo -e "
----------------------------
WARP Client\t: ${WARP_Client_Status_en}
SOCKS5 Port\t: ${WARP_Proxy_Status_en}
----------------------------
"
log INFO "Done."
}
Print_WARP_WireGuard_Status() {
log INFO "Status check in progress..."
Check_WireGuard_Status
Check_WARP_WireGuard_Status
echo -e "
----------------------------
WireGuard\t: ${WireGuard_Status_en}
IPv4 Network\t: ${WARP_IPv4_Status_en}
IPv6 Network\t: ${WARP_IPv6_Status_en}
----------------------------
"
log INFO "Done."
}
Print_ALL_Status() {
log INFO "Status check in progress..."
Check_ALL_Status
echo -e "
----------------------------
WARP Client\t: ${WARP_Client_Status_en}
SOCKS5 Port\t: ${WARP_Proxy_Status_en}
----------------------------
WireGuard\t: ${WireGuard_Status_en}
IPv4 Network\t: ${WARP_IPv4_Status_en}
IPv6 Network\t: ${WARP_IPv6_Status_en}
----------------------------
"
}
View_WireGuard_Profile() {
Print_Delimiter
cat ${WireGuard_ConfPath}
Print_Delimiter
}
Check_WireGuard_Peer_Endpoint() {
if ping -c1 -W1 ${WireGuard_Peer_Endpoint_IP4} >/dev/null 2>&1; then
WireGuard_Peer_Endpoint="${WireGuard_Peer_Endpoint_IPv4}"
elif ping6 -c1 -W1 ${WireGuard_Peer_Endpoint_IP6} >/dev/null 2>&1; then
WireGuard_Peer_Endpoint="${WireGuard_Peer_Endpoint_IPv6}"
else
WireGuard_Peer_Endpoint="${WireGuard_Peer_Endpoint_Domain}"
fi
}
Set_WARP_IPv4() {
Install_WireGuard
Get_IP_addr
Load_WGCF_Profile
if [[ ${IPv4Status} = off && ${IPv6Status} = on ]]; then
WireGuard_Interface_DNS="${WireGuard_Interface_DNS_64}"
else
WireGuard_Interface_DNS="${WireGuard_Interface_DNS_46}"
fi
WireGuard_Peer_AllowedIPs="${WireGuard_Peer_AllowedIPs_IPv4}"
Check_WireGuard_Peer_Endpoint
Generate_WireGuardProfile_Interface
if [[ -n ${IPv4_addr} ]]; then
Generate_WireGuardProfile_Interface_Rule_IPv4_Global_srcIP
fi
Generate_WireGuardProfile_Peer
View_WireGuard_Profile
Enable_WireGuard
Print_WARP_WireGuard_Status
}
Set_WARP_IPv6() {
Install_WireGuard
Get_IP_addr
Load_WGCF_Profile
if [[ ${IPv4Status} = off && ${IPv6Status} = on ]]; then
WireGuard_Interface_DNS="${WireGuard_Interface_DNS_64}"
else
WireGuard_Interface_DNS="${WireGuard_Interface_DNS_46}"
fi
WireGuard_Peer_AllowedIPs="${WireGuard_Peer_AllowedIPs_IPv6}"
Check_WireGuard_Peer_Endpoint
Generate_WireGuardProfile_Interface
if [[ -n ${IPv6_addr} ]]; then
Generate_WireGuardProfile_Interface_Rule_IPv6_Global_srcIP
fi
Generate_WireGuardProfile_Peer
View_WireGuard_Profile
Enable_WireGuard
Print_WARP_WireGuard_Status
}
Set_WARP_DualStack() {
Install_WireGuard
Get_IP_addr
Load_WGCF_Profile
WireGuard_Interface_DNS="${WireGuard_Interface_DNS_46}"
WireGuard_Peer_AllowedIPs="${WireGuard_Peer_AllowedIPs_DualStack}"
Check_WireGuard_Peer_Endpoint
Generate_WireGuardProfile_Interface
if [[ -n ${IPv4_addr} ]]; then
Generate_WireGuardProfile_Interface_Rule_IPv4_Global_srcIP
fi
if [[ -n ${IPv6_addr} ]]; then
Generate_WireGuardProfile_Interface_Rule_IPv6_Global_srcIP
fi
Generate_WireGuardProfile_Peer
View_WireGuard_Profile
Enable_WireGuard
Print_WARP_WireGuard_Status
}
Set_WARP_DualStack_nonGlobal() {
Install_WireGuard
Get_IP_addr
Load_WGCF_Profile
WireGuard_Interface_DNS="${WireGuard_Interface_DNS_46}"
WireGuard_Peer_AllowedIPs="${WireGuard_Peer_AllowedIPs_DualStack}"
Check_WireGuard_Peer_Endpoint
Generate_WireGuardProfile_Interface
Generate_WireGuardProfile_Interface_Rule_DualStack_nonGlobal
Generate_WireGuardProfile_Peer
View_WireGuard_Profile
Enable_WireGuard
Print_WARP_WireGuard_Status
}
Menu_Title="${FontColor_Yellow_Bold}Cloudflare WARP 一键安装脚本${FontColor_Suffix} ${FontColor_Red}[${shVersion}]${FontColor_Suffix} by ${FontColor_Purple_Bold}P3TERX.COM${FontColor_Suffix}"
Menu_WARP_Client() {
clear
echo -e "
${Menu_Title}