This repository has been archived by the owner on Sep 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dig.sh
5358 lines (5303 loc) · 199 KB
/
Dig.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
scriptVersion="0.7.7"
# The URL of the script project is:
# https://github.com/MohsenHNSJ/TunlDigr
# The URL of the script is:
# https://raw.githubusercontent.com/MohsenHNSJ/TunlDigr/main/Dig.sh
# If the script executes incorrectly, go to:
# https://github.com/MohsenHNSJ/TunlDigr/issues
# Color Table
RED=$(tput setaf 160) # Error
GREEN=$(tput setaf 34) # Success
YELLOW=$(tput setaf 226) # Warning
BLUE=$(tput setaf 39) # Information
RESET=$(tput sgr0) # Reset
# Global Variables
# The file path for the access log.
# The value is a valid file path, such as "/var/log/Xray/access.log".
# When this item is not specified or is an empty value, the log is output to stdout.
# The special value (none) disables access logs.
XRAY_ACCESS_LOG_PATH=""
# If set to 1, script should set XRAY_ACCESS_LOG_PATH as (none).
DISABLE_XRAY_ACCESS_LOG=0
# The file path for the error log.
# The value is a valid file path, such as "/var/log/Xray/error.log".
# When this item is not specified or is an empty value, the log is output to stdout.
# The special value none disables error logs.
XRAY_ERROR_LOG_PATH=""
# If set to 1, script should set XRAY_ERROR_LOG_PATH as (none).
DISABLE_XRAY_ERROR_LOG=0
# The log level for error logs, indicating the information that needs to be recorded.
# The default value is "warning".
# (0) "none": Do not record any content.
# (1) "debug": Output information used for debugging the program. Includes all "info" content.
# (2) "info": Runtime status information, etc., which does not affect normal use. Includes all "warning" content.
# (3) "warning": Information output when there are some problems that do not affect normal operation but may affect user experience. Includes all "error" content.
# (4) "error": Xray encountered a problem that cannot be run normally and needs to be resolved immediately.
XRAY_LOG_LEVEL=3
# Whether to enable DNS query logs,
# for example: DOH//doh.server got answer: domain.com -> [ip1, ip2] 2.333ms.
# false
# true
XRAY_LOG_DNS=false
# Generates a random variable and echoes it back.
# <<<Options
# user-name: generate and return a random short ( 6 - 10 ) user-name
# password: generate and return a random long ( 18 - 22 ) password
generateRandom() {
# We read the argument supplied to the function to determine which type of random variable to generate and echo back.
case "$1" in
username)
choose() { echo ${1:RANDOM%${#1}:1} $RANDOM; }
local randomVariable="$({ choose 'abcdefghijklmnopqrstuvwxyz'
for i in $( seq 1 $(( 6 + RANDOM % 4 )) )
do
choose 'abcdefghijklmnopqrstuvwxyz'
done
} | sort -R | awk '{printf "%s",$1}')"
;;
password)
# We avoid adding symbols inside the password as it sometimes caused problems, therefore the password length is high.
choose() { echo ${1:RANDOM%${#1}:1} $RANDOM; }
local randomVariable="$({ choose '123456789'
choose 'abcdefghijklmnopqrstuvwxyz'
choose 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
for i in $( seq 1 $(( 18 + RANDOM % 4 )) )
do
choose '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
done
} | sort -R | awk '{printf "%s",$1}')"
;;
esac
echo $randomVariable
}
# Asks the user to select a tunneling method by entering a numeric value from 1 to 3.
# Can be skipped by -tm and specifying a method in the argument.
# Checks input for validity and if it's a valid, it will convert it to a string and save it in (tunnelingMethod) variable.
# Current available tunnels: (1) Hysteria 2, (2) Reality, (3) ShadowSocks
askTunnelingMethod() {
echo "========================================================================="
echo "| Select the desired tunneling method to set up |"
echo "| Enter only numbers between 1 - 3 |"
echo "========================================================================="
echo "1 - Hysteria 2 (Sing-Box)"
echo "2 - Reality (XTLS VLESS)"
echo "3 - Shadowsocks-libev (Obsolete)"
# We ask the user to select the desired tunneling method.
# We limit the input character count to 1 by using (-n) argument.
read -n 1 -p "Select tunneling method: " tunnelingMethod
# We validate user input and show an error if it's invalid, then loop the process until the value is valid.
until [[ $tunnelingMethod == +([1-3]) ]]; do
echo
read -n 1 -p "${YELLOW}Invalid input: ${RESET}Please only input a number from 1 - 3: " tunnelingMethod
done
# We convert the input value from user, to a string for better code readability.
case $tunnelingMethod in
1)
tunnelingMethod="hysteria2"
;;
2)
tunnelingMethod="reality"
;;
3)
tunnelingMethod="shadowsocks"
;;
esac
echo
}
# Installs the required packages for specified tunneling method.
# Can be disabled by -dpakup
installPackages() {
echo "========================================================================="
echo "| Updating repositories and installing the required packages |"
echo "| (This may take a few minutes, Please wait...) |"
echo "========================================================================="
# We update 'apt' repository.
# We install/update the packages we use during the process to ensure optimal performance.
# This installation must run without confirmation (-y)
sudo apt update
# We define the tunnel specific package
case $tunnelingMethod in
hysteria2)
local tunnelSpecificPackage=tar
;;
reality)
local tunnelSpecificPackage=unzip
;;
shadowsocks)
local tunnelSpecificPackage=snapd
;;
esac
# We install the general + tunnel specific packages
sudo apt -y install wget openssl gawk sshpass ufw coreutils curl adduser sed grep util-linux qrencode haveged $tunnelSpecificPackage
}
# Shows a startup message and version of the script.
# can be disabled by -dstartmsg
showStartupMessage() {
echo "========================================================================="
echo "| TunlDigr by @MohsenHNSJ (Github) |"
echo "========================================================================="
echo "Check out the github page, contribute and suggest ideas/bugs/improvements."
echo
echo "=========================="
echo "| ${BLUE}Script version $scriptVersion ${RESET}|"
echo "=========================="
}
# Optimizes the server settings (sysctl.conf, limits.conf) to better handle connections.
# Can be disabled by -dservopti
optimizeServerSettings() {
echo "========================================================================="
echo "| Optimizing server settings |"
echo "========================================================================="
# We optimise 'sysctl.conf' file for better performance.
sudo echo "net.ipv4.tcp_keepalive_time = 90" >> /etc/sysctl.conf
sudo echo "net.ipv4.ip_local_port_range = 1024 65535" >> /etc/sysctl.conf
sudo echo "net.ipv4.tcp_fastopen = 3" >> /etc/sysctl.conf
sudo echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
sudo echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sudo echo "fs.file-max = 65535000" >> /etc/sysctl.conf
# ShadowSocks specific optimizations
if [ $tunnelingMethod == shadowsocks ]; then
sudo echo "net.core.netdev_max_backlog = 250000" >> /etc/sysctl.conf
sudo echo "net.core.somaxconn = 4096" >> /etc/sysctl.conf
sudo echo "net.ipv4.tcp_syncookies = 1" >> /etc/sysctl.conf
sudo echo "net.ipv4.tcp_tw_reuse = 1" >> /etc/sysctl.conf
sudo echo "net.ipv4.tcp_tw_recycle = 0" >> /etc/sysctl.conf
sudo echo "net.ipv4.tcp_fin_timeout = 30" >> /etc/sysctl.conf
sudo echo "net.ipv4.tcp_max_syn_backlog = 8192" >> /etc/sysctl.conf
sudo echo "net.ipv4.tcp_max_tw_buckets = 5000" >> /etc/sysctl.conf
sudo echo "net.ipv4.tcp_mtu_probing = 1" >> /etc/sysctl.conf
sudo echo "net.core.rmem_max = 67108864" >> /etc/sysctl.conf
sudo echo "net.core.wmem_max = 67108864" >> /etc/sysctl.conf
sudo echo "net.ipv4.tcp_mem = 25600 51200 102400" >> /etc/sysctl.conf
sudo echo "net.ipv4.tcp_rmem = 4096 87380 67108864" >> /etc/sysctl.conf
sudo echo "net.ipv4.tcp_wmem = 4096 65536 67108864" >> /etc/sysctl.conf
fi
# We optimise 'limits.conf' file for better performance.
sudo echo "* soft nproc 655350" >> /etc/security/limits.conf
sudo echo "* hard nproc 655350" >> /etc/security/limits.conf
sudo echo "* soft nofile 655350" >> /etc/security/limits.conf
sudo echo "* hard nofile 655350" >> /etc/security/limits.conf
sudo echo "root soft nproc 655350" >> /etc/security/limits.conf
sudo echo "root hard nproc 655350" >> /etc/security/limits.conf
sudo echo "root soft nofile 655350" >> /etc/security/limits.conf
sudo echo "root hard nofile 655350" >> /etc/security/limits.conf
# We apply the changes.
sudo sysctl -p
}
# Saves the new user's name and password as well as the latest version of selected tunneling method to be used after switching to the new user.
saveAndTransferCredentials() {
echo "========================================================================="
echo "| Saving Credentials |"
echo "========================================================================="
# We save the new user credentials to use after switching user.
# We first must check if it already exists or not.
# If it does exist, we must delete it and make a new one to store new temporary data.
if [ -d "/tunlDigrTemp" ]
then
sudo rm -r /tunlDigrTemp
sudo mkdir /tunlDigrTemp
else
sudo mkdir /tunlDigrTemp
fi
# We save the credentials into files to access later.
echo $newAccUsername > /tunlDigrTemp/tempNewAccUsername.txt
echo $newAccPassword > /tunlDigrTemp/tempNewAccPassword.txt
# If selected tunneling method is not shadowsocks, We save the latest version of tunneling method.
if [ ! $tunnelingMethod == shadowsocks ]; then
echo $latestPackageVersion > /tunlDigrTemp/tempLatestPackageVersion.txt
fi
# We transfer ownership of the temp folder to the new user, so the new user is able to Access and delete the sensitive information when it's no longer needed.
sudo chown -R $newAccUsername /tunlDigrTemp/
}
# Reads the new user's name and password as well as the latest version of selected tunneling method, then removed the files containing these information.
readAndRemoveCredentials() {
echo "========================================================================="
echo "| Reading Credentials |"
echo "========================================================================="
# We read the saved credentials.
tempNewAccUsername=$(</tunlDigrTemp/tempNewAccUsername.txt)
tempNewAccPassword=$(</tunlDigrTemp/tempNewAccPassword.txt)
# If selected tunneling method is not shadowsocks, We read the latest version of tunneling method.
if [ ! $tunnelingMethod == shadowsocks ]; then
tempLatestPackageVersion=$(</tunlDigrTemp/tempLatestPackageVersion.txt)
sudo rm /tunlDigrTemp/tempLatestPackageVersion.txt
fi
# We delete sensitive information.
sudo rm /tunlDigrTemp/tempNewAccUsername.txt
sudo rm /tunlDigrTemp/tempNewAccPassword.txt
}
# Allows the specified port (tunnelPort) on ufw.
# If port is not provided, 443 is used by default.
allowPortOnUfw() {
echo "========================================================================="
echo "| Allowing Port |"
echo "========================================================================="
# We provide password to 'sudo' command and open protocol port.
# We check whether user has provided custom port and if so, we check if it's in the acceptable range (0 - 65535).
# If not, we will use the default 443.
if [ ! -v tunnelPort ] || [[ $tunnelPort != +([0-9]) ]] || [ $tunnelPort -gt 65535 ]; then
tunnelPort=443
fi
echo $tempNewAccPassword | sudo -S ufw allow $tunnelPort
}
# Creates a new user.
# Uses the supplied newAccUsername(-setusername) & newAccPassword(-setuserpass) if available.
# if not, it will randomly generate unavailable ones, using (generateRandom) function.
addNewUser() {
echo "========================================================================="
echo "| Adding a new user and configuring |"
echo "========================================================================="
# We check whether user has provided custom username.
# If not, we will generate a random username.
if [ ! -v newAccUsername ]; then
newAccUsername=$(generateRandom username)
fi
# We check whether user has provided custom password.
# If not, we will generate a random password.
if [ ! -v newAccPassword ]; then
newAccPassword=$(generateRandom password)
fi
# We create a new user.
adduser --gecos "" --disabled-password $newAccUsername
# We set a password for the new user.
chpasswd <<<"$newAccUsername:$newAccPassword"
# We grant root privileges to the new user.
usermod -aG sudo $newAccUsername
}
# Creates the required service file for the selected tunnel and saves it the specific service path.
# TODO: Rework the reality service to use reality as name not xray (this makes some confusion later on).
createService() {
# We create some local variables to hold tunnel specific data.
# Hysteria 2
local hysteria2ServicePath="/etc/systemd/system/hysteria2.service"
local hysteria2serviceDescription="sing-box service"
local hysteria2ServiceDocumentation="https://sing-box.sagernet.org"
local hysteria2ServiceAfter="network.target nss-lookup.target"
local hysteriaCapabilityBoundingSet="CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_SYS_PTRACE CAP_DAC_READ_SEARCH"
local hysteriaAmbientCapabilities="CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_SYS_PTRACE CAP_DAC_READ_SEARCH"
local hysteriaExecStart="/home/$newAccUsername/hysteria2/sing-box -D /home/$newAccUsername/hysteria2/ run -c /home/$newAccUsername/hysteria2/config.json"
local hysteriaLimitNOFILE="infinity"
# Reality
local realityServicePath="/etc/systemd/system/xray.service"
local realityServiceDescription="XTLS Xray-Core a VMESS/VLESS Server"
local realityServiceAfter="network.target nss-lookup.target"
local realityCapabilityBoundingSet="CAP_NET_ADMIN CAP_NET_BIND_SERVICE"
local realityAmbientCapabilities="CAP_NET_ADMIN CAP_NET_BIND_SERVICE"
local realityExecStart="/home/$newAccUsername/xray/xray run -config /home/$newAccUsername/xray/config.json"
local realityLimitNOFILE="1000000"
# ShadowSocks
local shadowsocksServicePath="/etc/systemd/system/shadowsocks-libev-server@.service"
local shadowsocksServiceDescription="Shadowsocks-Libev Custom Server Service"
local shadowsocksServiceDocumentation="man:ss-server(1)"
local shadowsocksServiceAfter="network-online.target"
local shadowsocksExecStart="/usr/bin/snap run shadowsocks-libev.ss-server -c /var/snap/shadowsocks-libev/common/etc/shadowsocks-libev/config.json"
# We determine the selected tunneling method and set service variables accordingly.
case $tunnelingMethod in
hysteria2)
local servicePath=$hysteria2ServicePath
local serviceName="Hysteria 2"
local serviceDescription=$hysteria2serviceDescription
local serviceDocumentation=$hysteria2ServiceDocumentation
local serviceAfter=$hysteria2ServiceAfter
local serviceCapabilityBoundingSet=$hysteriaCapabilityBoundingSet
local serviceAmbientCapabilities=$hysteriaAmbientCapabilities
local serviceExecStart=$hysteriaExecStart
local serviceLimitNOFILE=$hysteriaLimitNOFILE
;;
reality)
local servicePath=$realityServicePath
local serviceName="Reality"
local serviceDescription=$realityServiceDescription
local serviceAfter=$realityServiceAfter
local serviceCapabilityBoundingSet=$realityCapabilityBoundingSet
local serviceAmbientCapabilities=$realityAmbientCapabilities
local serviceExecStart=$realityExecStart
local serviceLimitNOFILE=$realityLimitNOFILE
;;
shadowsocks)
local servicePath=$shadowsocksServicePath
local serviceDescription=$shadowsocksServiceDescription
local serviceDocumentation=$shadowsocksServiceDocumentation
local serviceAfter=$shadowsocksServiceAfter
local serviceExecStart=$shadowsocksExecStart
;;
esac
echo "========================================================================="
echo "| Creating $serviceName service "
echo "========================================================================="
# We create a service file using preset variables.
sudo echo "[Unit]" > $servicePath
sudo echo "Description=$serviceDescription" >> $servicePath
# Hysteria 2 & ShadowSocks have Documentation.
case $tunnelingMethod in
'hysteria2' | 'shadowsocks')
sudo echo "Documentation=$serviceDocumentation" >> $servicePath
;;
esac
sudo echo "After=$serviceAfter" >> $servicePath
# ShadowSocks Wants
if [ $tunnelingMethod == shadowsocks ]; then
sudo echo "Wants=network-online.target" >> $servicePath
fi
sudo echo "[Service]" >> $servicePath
# ShadowSocks Type
if [ $tunnelingMethod == shadowsocks ]; then
sudo echo "Type=simple" >> $servicePath
fi
# Hysteria 2 & Reality User, Group, CapabilityBoundingSet, AmbientCapabilities
case $tunnelingMethod in
'hysteria2' | 'reality')
sudo echo "User=$newAccUsername" >> $servicePath
sudo echo "Group=$newAccUsername" >> $servicePath
sudo echo "CapabilityBoundingSet=$serviceCapabilityBoundingSet" >> $servicePath
sudo echo "AmbientCapabilities=$serviceAmbientCapabilities" >> $servicePath
;;
esac
# Reality NoNewPrivileges
if [ $tunnelingMethod == reality ]; then
sudo echo "NoNewPrivileges=true" >> $servicePath
fi
sudo echo "ExecStart=$serviceExecStart" >> $servicePath
# Hysteria 2 ExecReload
if [ $tunnelingMethod == hysteria2 ]; then
sudo echo "ExecReload=/bin/kill -HUP \$MAINPID" >> $servicePath
fi
# Hysteria 2 & Reality Restart
case $tunnelingMethod in
'hysteria2' | 'reality')
sudo echo "Restart=on-failure" >> $servicePath
;;
esac
# Reality RestartPreventExitStatus and StandardOutput
if [ $tunnelingMethod == reality ]; then
sudo echo "RestartPreventExitStatus=23" >> $servicePath
sudo echo "StandardOutput=journal" >> $servicePath
fi
# Hysteria 2 RestartSec
if [ $tunnelingMethod == hysteria2 ]; then
sudo echo "RestartSec=10s" >> $servicePath
fi
# Reality LimitNPROC
if [ $tunnelingMethod == reality ]; then
sudo echo "LimitNPROC=100000" >> $servicePath
fi
# Hysteria 2 & Reality LimitNOFILE
case $tunnelingMethod in
'hysteria2' | 'reality')
sudo echo "LimitNOFILE=$serviceLimitNOFILE" >> $servicePath
;;
esac
sudo echo "" >> $servicePath
sudo echo "[Install]" >> $servicePath
sudo echo "WantedBy=multi-user.target" >> $servicePath
}
# Switches to the newly created user.
switchUser() {
echo "========================================================================="
echo "| Switching user |"
echo "========================================================================="
# We now switch to the new user.
sshpass -p $newAccPassword ssh -o "StrictHostKeyChecking=no" $newAccUsername@127.0.0.1
}
# Checks whether the selected tunnel already exists on the machine or not.
# TODO: If so, we have to ask for removal or updating.
# TODO: CURRENTLY IT DOES NOTHING!!!
checkIfTunnelAlreadyExists() {
# We check if the selected tunnel already exists or not.
case $tunnelingMethod in
reality)
if [ -f '/etc/systemd/system/xray.service' ]; then
xrayServiceAlreadyExists=1
fi
;;
esac
}
# Restarts the tunneling pipeline for a fresh start.
reloadScript() {
# We clear the tunnelingMethod variable
unset tunnelingMethod
# We ask the user to select a tunneling method again.
askTunnelingMethod
# We check whether the selected tunneling method already exists or not.
checkIfTunnelAlreadyExists
# We start installing the tunnel.
installTunnel
}
# Shows an error message, then calls the reloadScript function.
hardwareNotSupported() {
echo "${RED}Error: ${RESET}This architecture is NOT Supported by $tunnelName"
echo "Try selecting another tunnel"
echo "the script will now return to tunnel selection menu in 5 seconds"
# We will pause the script for 5 seconds and then reload the script
sleep 5s
reloadScript
}
# Checks whether the CPU architecture has VFP (Vector Floating Point accelerator) support or not.
# Returns:
# "Present"
# "Absent"
checkVfpSupport() {
# We get cpu features located at /proc/cpuinfo and look for 'vfp' in it.
# If the count is greater than 0, then we have VFP support, else we don't have.
if [ $(grep -c 'vfp' /proc/cpuinfo) -gt 0 ]; then
echo "Present"
else
echo "Absent"
fi
}
# Gets the current machine's hardware architecture and translates it to appropriate string based on selected tunneling method.
# If the tunnel does not support the current hardware architecture, an error message will be shown and the script will exit.
getHardwareArch() {
# We check and save the hardware architecture of current machine.
local hwarch="$(uname -m)"
# We translate it to appropriate string based on each tunneling method.
# If the architecture is not supported by the selected tunneling method, we will show an error message and exit the script.
case $hwarch in
# x86 - 32 Bit
'i386' | 'i686')
case $tunnelingMethod in
hysteria2)
hwarch="386"
;;
xray)
hwarch="32"
;;
esac
;;
# x86 - 64 Bit
'x86_64' | 'amd64')
case $tunnelingMethod in
hysteria2)
# We check if cpu support AVX.
avxsupport="$(lscpu | grep -o avx)"
if [ -z "$avxsupport" ]; then
hwarch="amd64"
else
# If so, we will use the package with AVX functions for faster performance
hwarch="amd64v3"
fi
;;
xray)
hwarch="64"
;;
esac
;;
# Arm - 32 Bit - V5
'armv5tel')
case $tunnelingMethod in
hysteria2)
# Hysteria does not support armv5tel, we will show an error and reload the script.
hardwareNotSupported
;;
xray)
hwarch="arm32-v5"
;;
esac
;;
# Arm - 32 Bit - V6
'armv6l')
case $tunnelingMethod in
# Hysteria does not support armv6l, we will show an error and reload the script.
hysteria2)
hardwareNotSupported
;;
xray)
# We check whether the cpu has Vector Floating Point (VFP) accelerator or not.
# If cpu does not support it, we revert back to an older package, omitted from these instruction calls.
if [ checkVfpSupport == "Present" ]; then
hwarch="arm32-v6"
else
hwarch="arm32-v5"
fi
;;
esac
;;
# Arm - 32 Bit - V7
'armv7' | 'armv7l')
case $tunnelingMethod in
hysteria2)
hwarch="armv7"
;;
xray)
# We check whether the cpu has Vector Floating Point (VFP) accelerator or not.
# If cpu does not support it, we revert back to an older package, omitted from these instruction calls.
if [ checkVfpSupport == "Present" ]; then
hwarch="arm32-v7a"
else
hwarch="arm32-v5"
fi
;;
esac
;;
# Arm - 64 Bit - V8
'aarch64' | 'armv8')
case $tunnelingMethod in
hysteria2)
hwarch="arm64"
;;
xray)
hwarch="arm64-v8a"
;;
esac
;;
# MIPS - 32 Bit
'mips')
case $tunnelingMethod in
# Hysteria does not support mips, we will show an error and reload the script.
hysteria2)
hardwareNotSupported
;;
xray)
hwarch="mips32"
;;
esac
;;
# MIPS - 32 Bit - Little Endian
'mipsle')
case $tunnelingMethod in
# Hysteria does not support mipsle, we will show an error and reload the script.
hysteria2)
hardwareNotSupported
;;
xray)
hwarch="mips32le"
;;
esac
;;
# MIPS - 64 Bit
'mips64')
case $tunnelingMethod in
# Hysteria does not support mips64, we will show an error and reload the script.
hysteria2)
hardwareNotSupported
;;
xray)
# We check whether the architecture byte order is little endian or not.
if [ $(lscpu | grep -c "Little Endian") -gt 0 ]; then
hwarch="mips64le"
else
hwarch="mips64"
fi
;;
esac
;;
# MIPS - 64 Bit - Little Endian
'mips64le')
case $tunnelingMethod in
# Hysteria does not support mips64le, we will show an error and reload the script.
hysteria2)
hardwareNotSupported
;;
xray)
hwarch="mips64le"
;;
esac
;;
# PowerPC - 64 Bit
'ppc64')
case $tunnelingMethod in
# Hysteria does not support ppc64, we will show an error and exit the script
hysteria2)
hardwareNotSupported
;;
xray)
hwarch="ppc64"
;;
esac
;;
# PowerPC - 64 Bit - Little Endian
'ppc64le')
case $tunnelingMethod in
# Hysteria does not support ppc64le, we will show an error and exit the script
hysteria2)
hardwareNotSupported
;;
xray)
hwarch="ppc64le"
;;
esac
;;
# RISC V - 64 Bit
'riscv64')
case $tunnelingMethod in
# Hysteria does not support riscv64, we will show an error and exit the script
hysteria2)
hardwareNotSupported
;;
xray)
hwarch="riscv64"
;;
esac
;;
# IBM System/390
# Because there is no difference, we don't check anything here.
's390x')
hwarch="s390x"
;;
# If nothing matched, either it's not implemented yet OR the tunnels don't support such architecture.
# We show an error message and reload the script.
*)
hardwareNotSupported
;;
esac
# We echo back the translated hardware architecture.
echo $hwarch
}
# Creates SSL certificate key pairs.
# Uses the supplied sslcn(-seth2sslcn) if available.
# If not, will use the default (google-analytics.com)
createSSLCertificateKeyPairs() {
# We create certificate keys.
openssl ecparam -genkey -name prime256v1 -out ca.key
# We check whether user has provided custom common name for SSL certificate.
# If not, we will use default.
if [ ! -v sslcn ]; then
sslcn="google-analytics.com"
fi
openssl req -new -x509 -days 36500 -key ca.key -out ca.crt -subj "/CN=$sslcn"
}
# Downloads the required files for selected tunnel and extracts them, then removes the downloaded package.
# TODO: Rework the reality downloader to use reality as name not xray (this makes some confusion later on).
downloadFiles() {
# If selected tunnel is ShadowSocks, only one command is needed and other steps are not required.
if [ $tunnelingMethod == shadowsocks ]; then
# We check whether user has requested to install edge channel or not.
# If so, we will use edge channel.
if [ -v ssUseEdgeChannel ]; then
snap install shadowsocks-libev --edge
else
snap install shadowsocks-libev
fi
return
fi
# Hysteria 2
local singBoxUrl="https://github.com/SagerNet/sing-box/releases/download/v$tempLatestPackageVersion/sing-box-$tempLatestPackageVersion-linux-$hardwareArch.tar.gz"
local singBoxPackageName="sing-box-$tempLatestPackageVersion-linux-$hardwareArch.tar.gz"
# Reality
local xrayUrl="https://github.com/XTLS/Xray-core/releases/download/v$tempLatestPackageVersion/Xray-linux-$hardwareArch.zip"
local xrayPackageName="Xray-linux-$hardwareArch.zip"
# We determine the selected tunneling method and set download variables accordingly.
case $tunnelingMethod in
hysteria2)
local protocolName="Hysteria 2"
local packageUrl=$singBoxUrl
local packageName=$singBoxPackageName
local directoryName="hysteria2"
;;
reality)
local protocolName="Xray"
local packageUrl=$xrayUrl
local packageName=$xrayPackageName
local directoryName="xray"
;;
esac
echo "========================================================================="
echo "| Downloading $protocolName and required files |"
echo "========================================================================="
# We create directory to hold files.
# If it does exist, we must delete it and make a new one to avoid conflicts.
if [ -d "/$directoryName" ]; then
sudo rm -r /$directoryName
fi
mkdir $directoryName
# We navigate to directory we created.
cd $directoryName/
# We download the latest suitable package for current machine.
wget $packageUrl
# If we running Reality, we also download geoasset file.
if [ $tunnelingMethod == reality ]; then
wget https://github.com/bootmortis/iran-hosted-domains/releases/latest/download/iran.dat
fi
# We extract the package.
case $tunnelingMethod in
hysteria2)
tar -xzf $packageName --strip-components=1 sing-box-$tempLatestPackageVersion-linux-$hwarch/sing-box
;;
reality)
unzip $packageName
;;
esac
# We remove downloaded file.
sudo rm $packageName
}
configureSingBox() {
echo "========================================================================="
echo "| Configuring Sing-Box |"
echo "========================================================================="
# We restart the service and enable auto-start
sudo systemctl daemon-reload && sudo systemctl enable hysteria2
# We check whether user has provided custom hysteria obfs password
# If not, we will generate a random password for salamander obfs
if [ ! -v h2ObfsPass ]; then
h2ObfsPass=$(generateRandom password)
fi
# We check whether user has provided custom hysteria authentication password
# If not, we will generate a random password for hysteria user
if [ ! -v h2UserPass ]; then
h2UserPass=$(generateRandom password)
fi
# We store path of 'config.json' file
local configfile=/home/$tempNewAccUsername/hysteria2/config.json
# We create 'config.json' file
cat > $configfile << EOL
{
"log":{
"level":"info",
"timestamp":true
},
"inbounds":[
{
"type":"hysteria2",
"tag":"hy2-in",
"listen":"::",
"listen_port":$tunnelPort,
"domain_strategy":"prefer_ipv4",
"up_mbps":0,
"down_mbps":0,
"obfs":{
"type":"salamander",
"password":"$h2ObfsPass"
},
"users":[
{
"name":"user",
"password":"$h2UserPass"
}
],
"ignore_client_bandwidth":true,
"tls":{
"enabled":true,
"certificate_path":"/home/$tempNewAccUsername/hysteria2/ca.crt",
"key_path":"/home/$tempNewAccUsername/hysteria2/ca.key"
}
}
],
"outbounds":[
{
"type":"direct",
"tag":"direct"
},
{
"type":"block",
"tag":"block"
},
{
"type":"dns",
"tag":"dns-out"
}
],
"dns":{
"servers":[
{
"tag":"dns-out",
"address":"https://1.1.1.1/dns-query",
"address_strategy":"prefer_ipv4",
"strategy":"prefer_ipv4",
"detour":"direct"
}
]
},
"route":{
"geosite":{
"path":"iran-geosite.db",
"download_url":"https://github.com/bootmortis/iran-hosted-domains/releases/latest/download/iran-geosite.db"
},
"geoip":{
"path":"geoip.db",
"download_url":"https://github.com/SagerNet/sing-geoip/releases/latest/download/geoip.db"
},
"rules":[
{
"port":53,
"outbound":"dns-out"
},
{
"domain_suffix":".ir",
"outbound":"block"
},
{
"outbound":"block",
"geosite":[
"ir",
"other",
"ads"
]
},
{
"outbound":"block",
"geoip":[
"ir",
"private"
]
},
{
"outbound":"block",
"domain":[
"sb24.com",
"sheypoor.com",
"tebyan.net",
"beytoote.com",
"telewebion.com",
"Film2movie.ws",
"Setare.com",
"Filimo.com",
"Torob.com",
"Tgju.org",
"Sarzamindownload.com",
"downloadha.com",
"P30download.com",
"Sanjesh.org",
"patriciamolina.org",
"ajl.net",
"akidoo.top",
"orbsrv.com",
"s.orbsrv.com",
"syndication.realsrv.com",
"realsrv.com",
"nsimg.net",
"app.adjust.com",
"dbankcloud.asia",
"sckm.org",
"ubzrr.net",
"cgqv.net",
"xz2.d0d.com",
"www.xz2.d0d.com",
"d0d.com",
"magicfiles123.com",
"15ty.gx6.org",
"intrack.ir",
"divar.ir",
"irancell.ir",
"yooz.ir",
"iran-cell.com",
"irancell.i-r",
"shaparak.ir",
"learnit.ir",
"yooz.ir",
"baadesaba.ir",
"webgozar.ir",
"balad.ir",
"web.bale.ir",
"bale.ir",
"bale.ai",
"bale.io",
"dt.beyla.site",
"beyla.site"
]
},
{
"outbound":"block",
"ip_cidr":[
"6.0.0.0/8",
"7.0.0.0/8",
"11.0.0.0/8",
"21.0.0.0/8",
"22.0.0.0/8",
"26.0.0.0/8",
"28.0.0.0/8",
"29.0.0.0/8",
"30.0.0.0/8",
"33.0.0.0/8",
"2.144.0.0/14",
"2.176.0.0/12",
"5.1.43.0/24",
"5.22.0.0/17",
"5.22.192.0/21",
"5.22.200.0/22",
"5.23.112.0/21",
"5.34.192.0/20",
"5.42.217.0/24",
"5.42.223.0/24",
"5.52.0.0/16",
"5.53.32.0/19",
"5.56.128.0/22",
"5.56.132.0/24",
"5.56.134.0/23",
"5.57.32.0/21",
"5.61.24.0/23",
"5.61.26.0/24",
"5.61.28.0/22",
"5.62.160.0/19",
"5.62.192.0/18",
"5.63.8.0/21",
"5.72.0.0/15",
"5.74.0.0/16",
"5.75.0.0/17",
"5.104.208.0/21",
"5.106.0.0/16",
"5.112.0.0/12",
"5.134.128.0/18",
"5.134.192.0/21",
"5.135.116.200/30",
"5.144.128.0/21",
"5.145.112.0/22",
"5.145.116.0/24",
"5.159.48.0/21",
"5.160.0.0/16",
"5.182.44.0/22",
"5.190.0.0/16",
"5.198.160.0/19",
"5.200.64.0/18",
"5.200.128.0/17",
"5.201.128.0/17",
"5.202.0.0/16",
"5.208.0.0/12",
"5.213.255.36/31",
"5.232.0.0/14",
"5.236.0.0/17",
"5.236.128.0/20",
"5.236.144.0/21",
"5.236.156.0/22",
"5.236.160.0/19",
"5.236.192.0/18",
"5.237.0.0/16",
"5.238.0.0/15",
"5.250.0.0/17",
"5.252.216.0/22",
"5.253.24.0/22",
"5.253.96.0/22",
"5.253.225.0/24",
"8.27.67.32/32",
"8.27.67.41/32",
"31.2.128.0/17",
"31.7.64.0/21",
"31.7.72.0/22",
"31.7.76.0/23",