forked from masoudgb/Gost-ip6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
502 lines (414 loc) · 18.8 KB
/
install.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
#!/bin/bash
# Check if the user has root access
if [ "$EUID" -ne 0 ]; then
echo $'\e[32mPlease run with root privileges.\e[0m'
exit
fi
echo $'\e[35m'" ___| | _ _| _ \ /
| _ \ __| __| | | | _ \
| | ( | \__ \ | | ___/ ( |
\____|\___/ ____/ \__| ___|_| \___/
"$'\e[0m'
echo -e "\e[36mCreated By Masoud Gb Special Thanks Hamid Router\e[0m"
echo $'\e[35m'"Gost Ip6 Script v2.2.0"$'\e[0m'
options=($'\e[36m1. \e[0mGost Tunnel By IP4'
$'\e[36m2. \e[0mGost Tunnel By IP6'
$'\e[36m3. \e[0mGost Status'
$'\e[36m4. \e[0mUpdate Script'
$'\e[36m5. \e[0mAdd New IP'
$'\e[36m6. \e[0mChange Gost Version'
$'\e[36m7. \e[0mAuto Restart Gost'
$'\e[36m8. \e[0mAuto Clear Cache'
$'\e[36m9. \e[0mInstall BBR'
$'\e[36m10. \e[0mUninstall'
$'\e[36m11. \e[0mExit')
# Print prompt and options with cyan color
printf "\e[32mPlease Choice Your Options:\e[0m\n"
printf "%s\n" "${options[@]}"
# Read user input with white color
read -p $'\e[97mYour choice: \e[0m' choice
# If option 1 or 2 is selected
if [ "$choice" -eq 1 ] || [ "$choice" -eq 2 ]; then
if [ "$choice" -eq 1 ]; then
read -p $'\e[97mPlease enter the destination (Kharej) IPv4: \e[0m' destination_ip
elif [ "$choice" -eq 2 ]; then
read -p $'\e[97mPlease enter the destination (Kharej) IPv6: \e[0m' destination_ip
fi
read -p $'\e[32mPlease choose one of the options below:\n\e[0m\e[36m1. \e[0mEnter "Manually" Ports\n\e[36m2. \e[0mEnter "Range" Ports\e[32m\nYour choice: \e[0m' port_option
if [ "$port_option" -eq 1 ]; then
read -p $'\e[36mPlease enter the desired ports (separated by commas): \e[0m' ports
elif [ "$port_option" -eq 2 ]; then
read -p $'\e[36mPlease enter the port range (e.g., 54,65000): \e[0m' port_range
IFS=',' read -ra port_array <<< "$port_range"
# Check if the start and end port values are within the valid range
if [ "${port_array[0]}" -lt 54 -o "${port_array[1]}" -gt 65000 ]; then
echo $'\e[33mInvalid port range. Please enter a valid range starting from 54 and up to 65000.\e[0m'
exit
fi
ports=$(seq -s, "${port_array[0]}" "${port_array[1]}")
else
echo $'\e[31mInvalid option. Exiting...\e[0m'
exit
fi
read -p $'\e[32mSelect the protocol:\n\e[0m\e[36m1. \e[0mBy "Tcp" Protocol \n\e[36m2. \e[0mBy "Udp" Protocol \n\e[36m3. \e[0mBy "Grpc" Protocol \e[32m\nYour choice: \e[0m' protocol_option
if [ "$protocol_option" -eq 1 ]; then
protocol="tcp"
elif [ "$protocol_option" -eq 2 ]; then
protocol="udp"
elif [ "$protocol_option" -eq 3 ]; then
protocol="grpc"
else
echo $'\e[31mInvalid protocol option. Exiting...\e[0m'
exit
fi
echo $'\e[32mYou chose option\e[0m' $choice
echo $'\e[97mDestination IP:\e[0m' $destination_ip
echo $'\e[97mPorts:\e[0m' $ports
echo $'\e[97mProtocol:\e[0m' $protocol
# Commands to install and configure Gost
echo $'\e[32mUpdating system packages, please wait...\e[0m'
sysctl net.ipv4.ip_local_port_range="1024 65535"
# Add the sysctl command to the end of the script
echo "sysctl net.ipv4.ip_local_port_range=\"1024 65535\"" >> /etc/rc.local
# Enable the systemd service to run the sysctl command after reboot
cat <<EOL > /etc/systemd/system/sysctl-custom.service
[Unit]
Description=Custom sysctl settings
[Service]
ExecStart=/sbin/sysctl net.ipv4.ip_local_port_range="1024 65535"
[Install]
WantedBy=multi-user.target
EOL
# Enable the service
systemctl enable sysctl-custom
apt update && sudo apt install wget nano -y && \
# Add alias for 'gost' to execute the script
echo 'alias gost="bash /etc/gost/install.sh"' >> ~/.bashrc
source ~/.bashrc
echo $'\e[32mSymbolic link created: /usr/local/bin/gost\e[0m'
echo $'\e[32mSystem update completed.\e[0m'
# Prompt user to choose Gost version
echo $'\e[32mChoose Gost version:\e[0m'
echo $'\e[36m1. \e[0mGost version 2.11.5 (official)'
echo $'\e[36m2. \e[0mGost version 3.0.0 (latest)'
# Read user input for Gost version
read -p $'\e[97mYour choice: \e[0m' gost_version_choice
# Download and install Gost based on user's choice
if [ "$gost_version_choice" -eq 1 ]; then
echo $'\e[32mInstalling Gost version 2.11.5, please wait...\e[0m' && \
wget https://github.com/ginuerzh/gost/releases/download/v2.11.5/gost-linux-amd64-2.11.5.gz && \
echo $'\e[32mGost downloaded successfully.\e[0m' && \
gunzip gost-linux-amd64-2.11.5.gz && \
sudo mv gost-linux-amd64-2.11.5 /usr/local/bin/gost && \
sudo chmod +x /usr/local/bin/gost && \
echo $'\e[32mGost installed successfully.\e[0m'
else
if [ "$gost_version_choice" -eq 2 ]; then
echo $'\e[32mInstalling Gost version 3.0.0, please wait...\e[0m'
wget -O /tmp/gost.tar.gz https://github.com/go-gost/gost/releases/download/v3.0.0-nightly.20240426/gost_3.0.0-nightly.20240426_linux_amd64.tar.gz
tar -xvzf /tmp/gost.tar.gz -C /usr/local/bin/
chmod +x /usr/local/bin/gost
echo $'\e[32mGost installed successfully.\e[0m'
else
echo $'\e[31mInvalid choice. Exiting...\e[0m'
exit
fi
fi
# Continue creating the systemd service file
exec_start_command="ExecStart=/usr/local/bin/gost"
# Add lines for each port
IFS=',' read -ra port_array <<< "$ports"
port_count=${#port_array[@]}
# Set the maximum number of ports per file
max_ports_per_file=12000
# Calculate the number of files needed
file_count=$(( (port_count + max_ports_per_file - 1) / max_ports_per_file ))
# Continue creating the systemd service files
for ((file_index = 0; file_index < file_count; file_index++)); do
# Create a new systemd service file
cat <<EOL | sudo tee "/usr/lib/systemd/system/gost_$file_index.service" > /dev/null
[Unit]
Description=GO Simple Tunnel
After=network.target
Wants=network.target
[Service]
Type=simple
Environment="GOST_LOGGER_LEVEL=fatal"
EOL
# Add lines for each port in the current file
exec_start_command="ExecStart=/usr/local/bin/gost"
for ((i = file_index * max_ports_per_file; i < (file_index + 1) * max_ports_per_file && i < port_count; i++)); do
port="${port_array[i]}"
exec_start_command+=" -L=$protocol://:$port/[$destination_ip]:$port"
done
# Append the ExecStart command to the current file
echo "$exec_start_command" | sudo tee -a "/usr/lib/systemd/system/gost_$file_index.service" > /dev/null
# Complete the current systemd service file
cat <<EOL | sudo tee -a "/usr/lib/systemd/system/gost_$file_index.service" > /dev/null
[Install]
WantedBy=multi-user.target
EOL
# Reload and restart the systemd service
sudo systemctl enable "gost_$file_index.service"
sudo systemctl start "gost_$file_index.service"
sudo systemctl daemon-reload
sudo systemctl restart "gost_$file_index.service"
done
echo $'\e[32mGost configuration applied successfully.\e[0m'
# If option 3 is selected
elif [ "$choice" -eq 3 ]; then
# Check if Gost is installed
if command -v gost &>/dev/null; then
echo $'\e[32mGost is installed. Checking configuration and status...\e[0m'
# Check Gost configuration and status
systemctl list-unit-files | grep -q "gost_"
if [ $? -eq 0 ]; then
echo $'\e[32mGost is configured and active.\e[0m'
# Get and display used IPs and ports
for service_file in /usr/lib/systemd/system/gost_*.service; do
# Extract the IP, port, and protocol information using awk
service_info=$(awk -F'[-=:/\\[\\]]+' '/ExecStart=/ {print $14,$15,$22,$20,$23}' "$service_file")
# Split the extracted information into an array
read -a info_array <<< "$service_info"
# Display IP, port, and protocol information with corrected port range
echo -e "\e[97mIP:\e[0m ${info_array[0]} \e[97mPort:\e[0m ${info_array[1]},... \e[97mProtocol:\e[0m ${info_array[2]}"
done
else
echo $'\e[33mGost is installed, but not configured or active.\e[0m'
fi
else
echo $'\e[33mGost Tunnel is not installed. \e[0m'
fi
read -n 1 -s -r -p $'\e[36m0. \e[0mBack to menu: \e[0m' choice
if [ "$choice" -eq 0 ]; then
bash "$0"
fi
# If option 4 is selected
elif [ "$choice" -eq 4 ]; then
read -p $'\e[32mDo you want to update Gost script? (y/n): \e[0m' update_choice
if [ "$update_choice" == "y" ]; then
echo $'\e[32mUpdating Gost, please wait...\e[0m'
# Save install.sh in /etc/gost directory
sudo mkdir -p /etc/gost
wget -O /etc/gost/install.sh https://github.com/masoudgb/Gost-ip6/raw/main/install.sh
chmod +x /etc/gost/install.sh
echo $'\e[32mUpdate completed.\e[0m'
else
echo $'\e[32mUpdate canceled.\e[0m'
fi
bash "$0"
fi
# If option 5 is selected
if [ "$choice" -eq 5 ]; then
read -p $'\e[97mPlease enter the new destination (Kharej) IP 4 or 6: \e[0m' destination_ip
read -p $'\e[36mPlease enter the new port (separated by commas): \e[0m' port
read -p $'\e[32mSelect the protocol:\n\e[0m\e[36m1. \e[0mBy Tcp Protocol \n\e[36m2. \e[0mBy Grpc Protocol \e[32m\nYour choice: \e[0m' protocol_option
if [ "$protocol_option" -eq 1 ]; then
protocol="tcp"
elif [ "$protocol_option" -eq 2 ]; then
protocol="grpc"
else
echo $'\e[31mInvalid protocol option. Exiting...\e[0m'
exit
fi
# Use the default protocol previously entered
echo $'\e[32mYou chose option\e[0m' $choice
echo $'\e[97mDestination IP:\e[0m' $destination_ip
echo $'\e[97mPort(s):\e[0m' $port
echo $'\e[97mProtocol:\e[0m' $protocol
# Create the systemd service file
cat <<EOL | sudo tee "/usr/lib/systemd/system/gost_$destination_ip.service" > /dev/null
[Unit]
Description=GO Simple Tunnel
After=network.target
Wants=network.target
[Service]
Type=simple
Environment="GOST_LOGGER_LEVEL=fatal"
EOL
# Add lines for each port
IFS=',' read -ra port_array <<< "$port"
port_count=${#port_array[@]}
# Set the maximum number of ports per file
max_ports_per_file=12000
# Calculate the number of files needed
file_count=$(( (port_count + max_ports_per_file - 1) / max_ports_per_file ))
for ((file_index = 0; file_index < file_count; file_index++)); do
# Add lines for each port in the current file
exec_start_command="ExecStart=/usr/local/bin/gost"
for ((i = file_index * max_ports_per_file; i < (file_index + 1) * max_ports_per_file && i < port_count; i++)); do
port="${port_array[i]}"
exec_start_command+=" -L=$protocol://:$port/[$destination_ip]:$port"
done
# Append the ExecStart command to the current file
echo "$exec_start_command" | sudo tee -a "/usr/lib/systemd/system/gost_$destination_ip.service" > /dev/null
done
# Complete the systemd service file
cat <<EOL | sudo tee -a "/usr/lib/systemd/system/gost_$destination_ip.service" > /dev/null
[Install]
WantedBy=multi-user.target
EOL
# Reload and restart the systemd service
sudo systemctl enable "gost_$destination_ip.service"
sudo systemctl start "gost_$destination_ip.service"
sudo systemctl daemon-reload
sudo systemctl restart "gost_$destination_ip.service"
echo $'\e[32mGost configuration applied successfully.\e[0m'
bash "$0"
# If option 6 is selected
elif [ "$choice" -eq 6 ]; then
echo $'\e[32mChoose Gost version:\e[0m'
echo $'\e[36m1. \e[0mGost version 2.11.5 (official)'
echo $'\e[36m2. \e[0mGost version 3.0.0 (latest)'
# Read user input for Gost version
read -p $'\e[97mYour choice: \e[0m' gost_version_choice
# Download and install Gost based on user's choice
case "$gost_version_choice" in
1)
echo $'\e[32mInstalling Gost version 2.11.5, please wait...\e[0m' && \
wget https://github.com/ginuerzh/gost/releases/download/v2.11.5/gost-linux-amd64-2.11.5.gz && \
echo $'\e[32mGost downloaded successfully.\e[0m' && \
gunzip gost-linux-amd64-2.11.5.gz && \
sudo mv gost-linux-amd64-2.11.5 /usr/local/bin/gost && \
sudo chmod +x /usr/local/bin/gost && \
echo $'\e[32mGost installed successfully.\e[0m'
;;
2)
echo $'\e[32mInstalling Gost version 3.0.0, please wait...\e[0m' && \
wget -O /tmp/gost.tar.gz https://github.com/go-gost/gost/releases/download/v3.0.0-nightly.20240426/gost_3.0.0-nightly.20240426_linux_amd64.tar.gz
tar -xvzf /tmp/gost.tar.gz -C /usr/local/bin/
chmod +x /usr/local/bin/gost
echo $'\e[32mGost installed successfully.\e[0m'
;;
*)
echo $'\e[31mInvalid choice. Exiting...\e[0m'
exit
;;
esac
bash "$0"
# If option 7 is selected
elif [ "$choice" -eq 7 ]; then
echo $'\e[32mChoose Auto Restart option:\e[0m'
echo $'\e[36m1. \e[0mEnable Auto Restart'
echo $'\e[36m2. \e[0mDisable Auto Restart'
# Read user input for Auto Restart option
read -p $'\e[97mYour choice: \e[0m' auto_restart_option
# Process user choice for Auto Restart
case "$auto_restart_option" in
1)
# Logic to enable Auto Restart
echo $'\e[32mAuto Restart Enabled.\e[0m'
# Remove any existing scheduled restart using 'at' command
sudo at -l | awk '{print $1}' | xargs -I {} atrm {}
# Prompt the user for the restart time in hours
read -p $'\e[97mEnter the restart time in hours: \e[0m' restart_time_hours
# Convert hours to minutes
restart_time_minutes=$((restart_time_hours * 60))
# Write a script to restart Gost
echo -e "#!/bin/bash\n\nsudo systemctl daemon-reload\nsudo systemctl restart gost_*.service" | sudo tee /usr/bin/auto_restart_cronjob.sh > /dev/null
# Give execute permission to the script
sudo chmod +x /usr/bin/auto_restart_cronjob.sh
# Remove any existing cron job for Auto Restart
crontab -l | grep -v '/usr/bin/auto_restart_cronjob.sh' | crontab -
# Write a new cron job to execute the script at the specified intervals
(crontab -l ; echo "0 */$restart_time_hours * * * /usr/bin/auto_restart_cronjob.sh") | crontab -
echo $'\e[32mAuto Restart scheduled successfully.\e[0m'
;;
2)
# Logic to disable Auto Restart
echo $'\e[32mAuto Restart Disabled.\e[0m'
# Remove the script and cron job for Auto Restart
sudo rm -f /usr/bin/auto_restart_cronjob.sh
crontab -l | grep -v '/usr/bin/auto_restart_cronjob.sh' | crontab -
echo $'\e[32mAuto Restart disabled successfully.\e[0m'
;;
*)
echo $'\e[31mInvalid choice. Exiting...\e[0m'
exit
;;
esac
bash "$0"
fi
# If option 8 is selected
if [ "$choice" -eq 8 ]; then
echo $'\e[32mChoose Auto Clear Cache option:\e[0m'
echo $'\e[36m1. \e[0mEnable Auto Clear Cache'
echo $'\e[36m2. \e[0mDisable Auto Clear Cache'
# Read user input for Auto Clear Cache option
read -p $'\e[97mYour choice: \e[0m' auto_clear_cache_option
# Process user choice for Auto Clear Cache
case "$auto_clear_cache_option" in
1)
# Enable Auto Clear Cache
enable_auto_clear_cache() {
echo $'\e[32mAuto Clear Cache Enabled.\e[0m'
# Prompt user to choose the interval in days
read -p $'\e[97mEnter the interval in days (e.g., 1 for daily, 7 for weekly): \e[0m' interval_days
# Set up the cron job based on the interval
cron_interval="0 0 */$interval_days * *"
# Write a new cron job to execute the cache clearing commands at the specified interval
(crontab -l 2>/dev/null; echo "$cron_interval sync; echo 1 > /proc/sys/vm/drop_caches && sync; echo 2 > /proc/sys/vm/drop_caches && sync; echo 3 > /proc/sys/vm/drop_caches") | crontab -
echo $'\e[32mAuto Clear Cache scheduled successfully.\e[0m'
}
# Call the function to enable Auto Clear Cache
enable_auto_clear_cache
;;
2)
# Disable Auto Clear Cache
disable_auto_clear_cache() {
echo $'\e[32mAuto Clear Cache Disabled.\e[0m'
# Remove only the cron job related to auto clearing cache
crontab -l | grep -v "drop_caches" | crontab -
echo $'\e[32mAuto Clear Cache disabled successfully.\e[0m'
}
# Call the function to disable Auto Clear Cache
disable_auto_clear_cache
;;
*)
echo $'\e[31mInvalid choice. Exiting...\e[0m'
exit
;;
esac
bash "$0"
fi
# If option 9 is selected
if [ "$choice" -eq 9 ]; then
echo $'\e[32mInstalling BBR, please wait...\e[0m' && \
wget -N --no-check-certificate https://github.com/teddysun/across/raw/master/bbr.sh && \
chmod +x bbr.sh && \
bash bbr.sh
bash "$0"
# If option 10 is selected
elif [ "$choice" -eq 10 ]; then
# Prompt the user for confirmation
read -p $'\e[91mWarning\e[33m: This will uninstall Gost and remove all related data. Are you sure you want to continue? (y/n): ' uninstall_confirm
# Check user confirmation
if [ "$uninstall_confirm" == "y" ]; then
# Countdown for uninstallation in a single line
echo $'\e[32mUninstalling Gost in 3 seconds... \e[0m' && sleep 1 && echo $'\e[32m2... \e[0m' && sleep 1 && echo $'\e[32m1... \e[0m' && sleep 1 && {
# Remove the auto_restart_cronjob.sh script
sudo rm -f /usr/bin/auto_restart_cronjob.sh
# Remove the cron job for Auto Restart
crontab -l | grep -v '/usr/bin/auto_restart_cronjob.sh' | crontab -
# Continue with the rest of the uninstallation process
sudo systemctl daemon-reload
sudo systemctl stop gost_*.service
sudo rm -f /usr/local/bin/gost
sudo rm -rf /etc/gost
sudo rm -f /usr/lib/systemd/system/gost_*.service
sudo rm -f /etc/systemd/system/multi-user.target.wants/gost_*.service
systemctl stop sysctl-custom
systemctl disable sysctl-custom
sudo rm -f /etc/systemd/system/sysctl-custom.service
sudo rm -f /etc/systemd/system/multi-user.target.wants/sysctl-custom.service
systemctl daemon-reload
echo $'\e[32mGost successfully uninstalled.\e[0m'
}
else
echo $'\e[32mUninstallation canceled.\e[0m'
fi
# If option 11 is selected
elif [ "$choice" -eq 11 ]; then
echo $'\e[32mYou have exited the script.\e[0m'
exit
fi