forked from aria81g/ReverseTlsTunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RtTunnel.sh
670 lines (577 loc) · 20 KB
/
RtTunnel.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
#!/bin/bash
#colors
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
blue='\033[0;34m'
purple='\033[0;35m'
cyan='\033[0;36m'
white='\033[0;37m'
rest='\033[0m'
root_access() {
# Check if the script is running as root
if [ "$EUID" -ne 0 ]; then
echo "This script requires root access. please run as root."
exit 1
fi
}
detect_distribution() {
# Detect the Linux distribution
local supported_distributions=("ubuntu" "debian" "centos" "fedora")
if [ -f /etc/os-release ]; then
source /etc/os-release
if [[ "${ID}" = "ubuntu" || "${ID}" = "debian" || "${ID}" = "centos" || "${ID}" = "fedora" ]]; then
package_manager="apt-get"
[ "${ID}" = "centos" ] && package_manager="yum"
[ "${ID}" = "fedora" ] && package_manager="dnf"
else
echo "Unsupported distribution!"
exit 1
fi
else
echo "Unsupported distribution!"
exit 1
fi
}
check_dependencies() {
detect_distribution
local dependencies=("wget" "lsof" "iptables" "unzip" "gcc" "git" "curl" "tar")
for dep in "${dependencies[@]}"; do
if ! command -v "${dep}" &> /dev/null; then
echo "${dep} is not installed. Installing..."
sudo "${package_manager}" install "${dep}" -y
fi
done
}
#Check installed service
check_installed() {
if [ -f "/etc/systemd/system/tunnel.service" ]; then
echo "The service is already installed."
exit 1
fi
}
# last or custom version
install_selected_version() {
read -p "Do you want to install the Latest version? [yes/no] default: yes): " choice
if [[ "$choice" == "no" ]]; then
install_rtt_custom
else
install_rtt
fi
}
# Function to download and install RTT
install_rtt() {
wget "https://raw.githubusercontent.com/radkesvat/ReverseTlsTunnel/master/scripts/install.sh" -O install.sh && chmod +x install.sh && bash install.sh
}
#custom version
install_rtt_custom() {
if pgrep -x "RTT" > /dev/null; then
echo "Tunnel is running! You must stop the tunnel before update. (pkill RTT)"
echo "Update is canceled."
exit
fi
# Get custom version
read -p "Please Enter your custom version (e.g : 3.6) : " version
apt-get update -y
echo "Downloading ReverseTlsTunnel version : $version"
printf "\n"
case $(uname -m) in
x86_64) URL="https://github.com/radkesvat/ReverseTlsTunnel/releases/download/V$version/v${version}_linux_amd64.zip" ;;
arm) URL="https://github.com/radkesvat/ReverseTlsTunnel/releases/download/V$version/v${version}_linux_arm64.zip" ;;
aarch64) URL="https://github.com/radkesvat/ReverseTlsTunnel/releases/download/V$version/v${version}_linux_arm64.zip" ;;
*) echo "Unable to determine system architecture."; exit 1 ;;
esac
wget $URL -O v${version}_linux_amd64.zip
unzip -o v${version}_linux_amd64.zip
chmod +x RTT
rm v${version}_linux_amd64.zip
echo "Finished."
}
# Function to configure arguments based on user's choice
configure_arguments() {
read -p "Which server do you want to use? (Enter '1' for Iran(internal-server) or '2' for Kharej(external-server) ) : " server_choice
read -p "Please Enter SNI (default : sheypoor.com): " sni
sni=${sni:-sheypoor.com}
if [ "$server_choice" == "2" ]; then
read -p "Please Enter IRAN IP(internal-server) : " server_ip
read -p "Please Enter Password (Please choose the same password on both servers): " password
arguments="--kharej --iran-ip:$server_ip --iran-port:443 --toip:127.0.0.1 --toport:multiport --password:$password --sni:$sni --terminate:24"
elif [ "$server_choice" == "1" ]; then
read -p "Please Enter Password (Please choose the same password on both servers): " password
read -p "Do you want to use fake upload? (yes/no): " use_fake_upload
if [ "$use_fake_upload" == "yes" ]; then
read -p "Enter upload-to-download ratio (e.g., 5 for 5:1 ratio): " upload_ratio
upload_ratio=$((upload_ratio - 1))
arguments="--iran --lport:23-65535 --sni:$sni --password:$password --noise:$upload_ratio --terminate:24"
else
arguments="--iran --lport:23-65535 --sni:$sni --password:$password --terminate:24"
fi
else
echo "Invalid choice. Please enter '1' or '2'."
exit 1
fi
}
# Function to handle installation
install() {
root_access
check_dependencies
check_installed
install_selected_version
# Change directory to /etc/systemd/system
cd /etc/systemd/system
configure_arguments
# Create a new service file named tunnel.service
cat <<EOL > tunnel.service
[Unit]
Description=my tunnel service
[Service]
Type=idle
User=root
WorkingDirectory=/root
ExecStart=/root/RTT $arguments
Restart=always
[Install]
WantedBy=multi-user.target
EOL
# Reload systemctl daemon and start the service
sudo systemctl daemon-reload
sudo systemctl start tunnel.service
sudo systemctl enable tunnel.service
}
check_lbinstalled() {
if [ -f "/etc/systemd/system/lbtunnel.service" ]; then
echo "The Load-balancer is already installed."
exit 1
fi
}
# Function to configure arguments2 based on user's choice
configure_arguments2() {
read -p "Which server do you want to use? (Enter '1' for Iran(internal-server) or '2' for Kharej(external-server) ) : " server_choice
read -p "Please Enter SNI (default : sheypoor.com): " sni
sni=${sni:-sheypoor.com}
if [ "$server_choice" == "2" ]; then
read -p "Is this your main server (VPN server)? (yes/no): " is_main_server
read -p "Please Enter IRAN IP(internal-server) : " server_ip
read -p "Please Enter Password (Please choose the same password on both servers): " password
if [ "$is_main_server" == "yes" ]; then
arguments="--kharej --iran-ip:$server_ip --iran-port:443 --toip:127.0.0.1 --toport:multiport --password:$password --sni:$sni --terminate:24"
elif [ "$is_main_server" == "no" ]; then
read -p "Enter your main IP (VPN Server): " main_ip
arguments="--kharej --iran-ip:$server_ip --iran-port:443 --toip:$main_ip --toport:multiport --password:$password --sni:$sni --terminate:24"
else
echo "Invalid choice for main server. Please enter 'yes' or 'no'."
exit 1
fi
elif [ "$server_choice" == "1" ]; then
read -p "Please Enter Password (Please choose the same password on both servers): " password
read -p "Do you want to use fake upload? (yes/no): " use_fake_upload
if [ "$use_fake_upload" == "yes" ]; then
read -p "Enter upload-to-download ratio (e.g., 5 for 5:1 ratio): " upload_ratio
upload_ratio=$((upload_ratio - 1))
arguments="--iran --lport:23-65535 --password:$password --sni:$sni --noise:$upload_ratio --terminate:24"
else
arguments="--iran --lport:23-65535 --password:$password --sni:$sni --terminate:24"
fi
num_ips=0
while true; do
((num_ips++))
read -p "Please enter ip server $num_ips (or type 'done' to finish): " ip
if [ "$ip" == "done" ]; then
break
else
arguments="$arguments --peer:$ip"
fi
done
else
echo "Invalid choice. Please enter '1' or '2'."
exit 1
fi
echo "Configured arguments: $arguments"
}
load-balancer() {
root_access
check_dependencies
check_lbinstalled
install_selected_version
# Change directory to /etc/systemd/system
cd /etc/systemd/system
configure_arguments2
# Create a new service file named tunnel.service
cat <<EOL > lbtunnel.service
[Unit]
Description=my lbtunnel service
[Service]
Type=idle
User=root
WorkingDirectory=/root
ExecStart=/root/RTT $arguments
Restart=always
[Install]
WantedBy=multi-user.target
EOL
# Reload systemctl daemon and start the service
sudo systemctl daemon-reload
sudo systemctl start lbtunnel.service
sudo systemctl enable lbtunnel.service
}
lb_uninstall() {
# Check if the service is installed
if [ ! -f "/etc/systemd/system/lbtunnel.service" ]; then
echo "The Load-balancer is not installed."
return
fi
# Stop and disable the service
sudo systemctl stop lbtunnel.service
sudo systemctl disable lbtunnel.service
# Remove service file
sudo rm /etc/systemd/system/lbtunnel.service
sudo systemctl reset-failed
sudo rm RTT
sudo rm install.sh 2>/dev/null
echo "Uninstallation completed successfully."
}
# Function to handle uninstallation
uninstall() {
# Check if the service is installed
if [ ! -f "/etc/systemd/system/tunnel.service" ]; then
echo "The service is not installed."
return
fi
# Stop and disable the service
sudo systemctl stop tunnel.service
sudo systemctl disable tunnel.service
# Remove service file
sudo rm /etc/systemd/system/tunnel.service
sudo systemctl reset-failed
sudo rm RTT
sudo rm install.sh 2>/dev/null
echo "Uninstallation completed successfully."
}
update_services() {
# Get the current installed version of RTT
installed_version=$(./RTT -v 2>&1 | grep -o '"[0-9.]*"')
# Fetch the latest version from GitHub releases
latest_version=$(curl -s https://api.github.com/repos/radkesvat/ReverseTlsTunnel/releases/latest | grep -o '"tag_name": "[^"]*"' | cut -d":" -f2 | sed 's/["V ]//g' | sed 's/^/"/;s/$/"/')
# Compare the installed version with the latest version
if [[ "$latest_version" > "$installed_version" ]]; then
echo "Updating to $latest_version (Installed: $installed_version)..."
if sudo systemctl is-active --quiet tunnel.service; then
echo "tunnel.service is active, stopping..."
sudo systemctl stop tunnel.service > /dev/null 2>&1
elif sudo systemctl is-active --quiet lbtunnel.service; then
echo "lbtunnel.service is active, stopping..."
sudo systemctl stop lbtunnel.service > /dev/null 2>&1
fi
# Download and run the installation script
wget "https://raw.githubusercontent.com/radkesvat/ReverseTlsTunnel/master/scripts/install.sh" -O install.sh && chmod +x install.sh && bash install.sh
# Start the previously active service
if sudo systemctl list-units --type=service --all | grep -q 'tunnel.service'; then
echo "Starting tunnel.service..."
sudo systemctl start tunnel.service
fi
if sudo systemctl list-units --type=service --all | grep -q 'lbtunnel.service'; then
echo "Starting lbtunnel.service..."
sudo systemctl start lbtunnel.service
fi
echo "Service updated and restarted successfully."
else
echo "You have the latest version ($installed_version)."
fi
}
compile() {
detect_distribution
check_dependencies
# Detect the operating system
if [[ "$OSTYPE" == "linux-gnu" ]]; then
# Linux operating system
if [[ "$(uname -m)" == "x86_64" ]]; then
# 64-bit architecture
file_url="https://github.com/nim-lang/nightlies/releases/download/latest-version-2-0/linux_x64.tar.xz"
elif [[ "$(uname -m)" == "x86" ]]; then
# 32-bit architecture
file_url="https://github.com/nim-lang/nightlies/releases/download/latest-version-2-0/linux_x32.tar.xz"
elif [[ "$(uname -m)" == "aarch64" ]]; then
# arm64 architecture
file_url="https://github.com/nim-lang/nightlies/releases/download/latest-version-2-0/linux_arm64.tar.xz"
elif [[ "$(uname -m)" == "armv7l" ]]; then
# armv7l architecture
file_url="https://github.com/nim-lang/nightlies/releases/download/latest-version-2-0/linux_armv7l.tar.xz"
else
echo "Unknown architecture!"
exit 1
fi
elif [[ "$OSTYPE" == "darwin"* ]]; then
# macOS operating system
file_url="https://github.com/nim-lang/nightlies/releases/download/latest-version-2-0/macosx_x64.tar.xz"
else
echo "Unsupported operating system!"
exit 1
fi
# Download the file based on the operating system and architecture
wget "$file_url"
tar -xvf "$(basename "$file_url")"
# Add the Nim path to PATH
export PATH="$(pwd)/nim-2.0.1/bin:$PATH"
# Clone the project
git clone https://github.com/radkesvat/ReverseTlsTunnel.git
# Navigate to the project directory
cd ReverseTlsTunnel
# Install and compile the project
nim install
nim build
# Successful message
echo "Project compiled successfully."
# Display the path of the RTT file
echo "RTT file is located at: ReverseTlsTunnel/dist"
}
# Function to start the tunnel service
start_tunnel() {
# Check if the service is installed
if sudo systemctl is-enabled --quiet tunnel.service; then
# Service is installed, start it
sudo systemctl start tunnel.service > /dev/null 2>&1
if sudo systemctl is-active --quiet tunnel.service; then
echo "Tunnel service started."
else
echo "Tunnel service failed to start."
fi
else
echo "Multiport Tunnel is not installed."
fi
}
stop_tunnel() {
# Check if the service is installed
if sudo systemctl is-enabled --quiet tunnel.service; then
# Service is installed, stop it
sudo systemctl stop tunnel.service > /dev/null 2>&1
if sudo systemctl is-active --quiet tunnel.service; then
echo "Tunnel service failed to stop."
else
echo "Tunnel service stopped."
fi
else
echo "Multiport Tunnel is not installed."
fi
}
check_tunnel_status() {
# Check the status of the tunnel service
if sudo systemctl is-active --quiet tunnel.service; then
echo -e "${yellow}Multiport is: ${green} [running ✔]${rest}"
else
echo -e "${yellow}Multiport is:${red} [Not running ✗ ]${rest}"
fi
}
# Function to start the tunnel service
start_lb_tunnel() {
# Check if the service is installed
if sudo systemctl is-enabled --quiet lbtunnel.service; then
# Service is installed, start it
sudo systemctl start lbtunnel.service > /dev/null 2>&1
if sudo systemctl is-active --quiet lbtunnel.service; then
echo "Tunnel service started."
else
echo "Tunnel service failed to start."
fi
else
echo "Load-Balancer is not installed."
fi
}
stop_lb_tunnel() {
# Check if the service is installed
if sudo systemctl is-enabled --quiet lbtunnel.service; then
# Service is installed, stop it
sudo systemctl stop lbtunnel.service > /dev/null 2>&1
if sudo systemctl is-active --quiet lbtunnel.service; then
echo "Load-Balancer failed to stop."
else
echo "Load-Balancer stopped."
fi
else
echo "Load-Balancer is not installed."
fi
}
check_lb_tunnel_status() {
# Check the status of the load balancer tunnel service
if sudo systemctl is-active --quiet lbtunnel.service; then
echo -e "${yellow}Load balancer is: ${green}[running ✔]${rest}"
else
echo -e "${yellow}Load balancer is:${red}[Not running ✗ ]${rest}"
fi
}
check_c_installed() {
if [ -f "/etc/systemd/system/custom_tunnel.service" ]; then
echo "The Custom Tunnel is already installed."
exit 1
fi
}
# Function to start the custom tunnel service
start_c_tunnel() {
# Check if the service is installed
if sudo systemctl is-enabled --quiet custom_tunnel.service; then
sudo systemctl start custom_tunnel.service > /dev/null 2>&1
if sudo systemctl is-active --quiet custom_tunnel.service; then
echo "Custom Tunnel started."
else
echo "Custom Tunnel failed to start."
fi
else
echo "Custom Tunnel is not installed."
fi
}
check_c_tunnel_status() {
# Check the status of the load balancer tunnel service
if sudo systemctl is-active --quiet custom_tunnel.service; then
echo -e "${yellow}Custom Tunnel is: ${green}[running ✔]${rest}"
else
echo -e "${yellow}Custom Tunnel is:${red}[Not running ✗ ]${rest}"
fi
}
stop_c_tunnel() {
# Check if the service is installed
if sudo systemctl is-enabled --quiet custom_tunnel.service; then
sudo systemctl stop custom_tunnel.service > /dev/null 2>&1
if sudo systemctl is-active --quiet custom_tunnel.service; then
echo "Custom Tunnel failed to stop."
else
echo "Custom Tunnel stopped."
fi
else
echo "Custom Tunnel is not installed."
fi
}
install_custom() {
root_access
check_dependencies
check_c_installed
install_selected_version
cd /etc/systemd/system
read -p "Enter RTT arguments (Example: RTT --iran --lport:443 --sni:splus.ir --password:123): " arguments
# Create the custom_tunnel.service file with user input
cat <<EOL > custom_tunnel.service
[Unit]
Description=my custom tunnel service
[Service]
Type=idle
User=root
WorkingDirectory=/root
ExecStart=/root/$arguments
Restart=always
[Install]
WantedBy=multi-user.target
EOL
# Reload systemctl daemon and start the service
sudo systemctl daemon-reload
sudo systemctl start custom_tunnel.service
sudo systemctl enable custom_tunnel.service
}
c_uninstall() {
# Check if the service is installed
if [ ! -f "/etc/systemd/system/custom_tunnel.service" ]; then
echo "The Custom Tunnel is not installed."
return
fi
# Stop and disable the service
sudo systemctl stop custom_tunnel.service
sudo systemctl disable custom_tunnel.service
# Remove service file
sudo rm /etc/systemd/system/custom_tunnel.service
sudo systemctl reset-failed
sudo rm RTT
sudo rm install.sh 2>/dev/null
echo "Uninstallation completed successfully."
}
#ip & version
myip=$(hostname -I | awk '{print $1}')
version=$(./RTT -v 2>&1 | grep -o 'version="[0-9.]*"')
# Main menu
clear
echo -e "${cyan}By --> Peyman * Github.com/Ptechgithub * ${rest}"
echo -e "Your IP is: ${cyan}($myip)${rest} "
echo -e "${yellow}******************************${rest}"
check_tunnel_status
check_lb_tunnel_status
check_c_tunnel_status
echo -e "${yellow}******************************${rest}"
echo -e " ${purple}--------#- Reverse Tls Tunnel -#--------${rest}"
echo -e "${green}1) Install (Multiport)${rest}"
echo -e "${red}2) Uninstall (Multiport)${rest}"
echo "3) Start Multiport"
echo "4) Stop Multiport"
echo "5) Check Status"
echo -e "${yellow} ----------------------------${rest}"
echo -e "${green}6) Install Load-balancer${rest}"
echo -e "${red}7) Uninstall Load-balancer${rest}"
echo "8) Start Load Balancer"
echo "9) Stop Load Balancer"
echo "10) Check status"
echo -e "${yellow} ----------------------------${rest}"
echo -e "${green}11) Install Custom${rest}"
echo -e "${red}12) Uninstall Custom${rest}"
echo "13) Start Custom"
echo "14) Stop Custom"
echo "15) Check status"
echo -e "${yellow} ----------------------------${rest}"
echo -e "${cyan}16) Update RTT${rest}"
echo -e "${cyan}17 Compile RTT${rest}"
echo "0) Exit"
echo -e "${purple} --------------${cyan}$version${purple}--------------${rest}"
read -p "Please choose: " choice
case $choice in
1)
install
;;
2)
uninstall
;;
3)
start_tunnel
;;
4)
stop_tunnel
;;
5)
check_tunnel_status
;;
6)
load-balancer
;;
7)
lb_uninstall
;;
8)
start_lb_tunnel
;;
9)
stop_lb_tunnel
;;
10)
check_lb_tunnel_status
;;
11)
install_custom
;;
12)
c_uninstall
;;
13)
start_c_tunnel
;;
14)
stop_c_tunnel
;;
15)
check_c_tunnel_status
;;
16)
update_services
;;
17)
compile
;;
0)
exit
;;
*)
echo "Invalid choice. Please try again."
;;
esac