-
Notifications
You must be signed in to change notification settings - Fork 32
/
install.sh
456 lines (401 loc) · 17.9 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
#!/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'
rest='\033[0m'
# Check for root user
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi
# Detect the Linux distribution
detect_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
p_m="apt-get"
[ "${ID}" = "centos" ] && p_m="yum"
[ "${ID}" = "fedora" ] && p_m="dnf"
else
echo "Unsupported distribution!"
exit 1
fi
else
echo "Unsupported distribution!"
exit 1
fi
}
# Check dependencies
check_dependencies() {
detect_distribution
sudo "${p_m}" -y update && sudo "${p_m}" -y upgrade
local dependencies=("nginx" "git" "wget" "certbot" "ufw" "python3-certbot-nginx")
for dep in "${dependencies[@]}"; do
if ! command -v "${dep}" &> /dev/null; then
echo -e "${yellow}${dep} is not installed. Installing...${rest}"
sudo "${p_m}" install "${dep}" -y
fi
done
}
# Display error and exit
display_error() {
echo -e "${red}Error: $1${rest}"
exit 1
}
# Store domain name
d_f="/etc/nginx/d.txt"
# Read domain from file
saved_domain=$(cat "$d_f" 2>/dev/null)
# Install Reverse nginx
install() {
# Check if NGINX is already installed
if [ -d "/etc/letsencrypt/live/$saved_domain" ]; then
echo -e "${yellow}×××××××××××××××××××××××${rest}"
echo -e "${cyan}N R P${green} is already installed.${rest}"
echo -e "${yellow}×××××××××××××××××××××××${rest}"
else
# Ask the user for the domain name
echo -e "${yellow}×××××××××××××××××××××××${rest}"
read -p "Enter your domain name: " domain
echo -e "${yellow}×××××××××××××××××××××××${rest}"
read -p "Enter GRPC Path (Service Name) [default: grpc]: " grpc_path
grpc_path=${grpc_path:-grpc}
echo -e "${yellow}×××××××××××××××××××××××${rest}"
read -p "Enter WebSocket Path (Service Name) [default: ws]: " ws_path
ws_path=${ws_path:-ws}
echo -e "${yellow}×××××××××××××××××××××××${rest}"
check_dependencies
echo "$domain" > "$d_f"
# Copy default NGINX config to your website
sudo cp /etc/nginx/sites-available/default "/etc/nginx/sites-available/$domain" || display_error "Failed to copy NGINX config"
# Enable your website
sudo ln -s "/etc/nginx/sites-available/$domain" "/etc/nginx/sites-enabled/" || display_error "Failed to enable your website"
# Remove default_server from the copied config
sudo sed -i -e 's/listen 80 default_server;/listen 80;/g' \
-e 's/listen \[::\]:80 default_server;/listen \[::\]:80;/g' \
-e "s/server_name _;/server_name $domain;/g" "/etc/nginx/sites-available/$domain" || display_error "Failed to modify NGINX config"
# Restart NGINX service
sudo systemctl restart nginx || display_error "Failed to restart NGINX service"
# Allow ports in firewall
sudo ufw allow 80/tcp || display_error "Failed to allow port 80"
sudo ufw allow 443/tcp || display_error "Failed to allow port 443"
# Get a free SSL certificate
echo -e "${yellow}×××××××××××××××××××××××${rest}"
echo -e "${green}Get SSL certificate ${rest}"
sudo certbot --nginx -d "$domain" --register-unsafely-without-email --non-interactive --agree-tos --redirect || display_error "Failed to obtain SSL certificate"
# NGINX config file content
cat <<EOL > /etc/nginx/sites-available/$domain
server {
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name $domain;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files \$uri \$uri/ =404;
}
# GRPC configuration
location ~ ^/$grpc_path/(?<port>\d+)/(.*)$ {
if (\$content_type !~ "application/grpc") {
return 404;
}
set \$grpc_port \$port;
client_max_body_size 0;
client_body_buffer_size 512k;
grpc_set_header X-Real-IP \$remote_addr;
client_body_timeout 1w;
grpc_read_timeout 1w;
grpc_send_timeout 1w;
grpc_pass grpc://127.0.0.1:\$grpc_port;
}
# WebSocket configuration
location ~ ^/$ws_path/(?<port>\d+)$ {
if (\$http_upgrade != "websocket") {
return 404;
}
set \$ws_port \$port;
proxy_pass http://127.0.0.1:\$ws_port/;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
}
listen [::]:443 ssl http2 ipv6only=on; # managed by Certbot
listen 443 ssl http2; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/$domain/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/$domain/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if (\$host = $domain) {
return 301 https://\$host\$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name $domain;
return 404; # managed by Certbot
}
EOL
# Restart NGINX service
sudo systemctl restart nginx || display_error "Failed to restart NGINX service"
check_installation
fi
}
# Check installation statu
check_status() {
if systemctl is-active --quiet nginx && [ -f "/etc/nginx/sites-available/$saved_domain" ] > /dev/null 2>&1; then
echo -e "${green} 🌐 Service Installed.${rest}"
else
echo -e "${red}🌐Service Not installed${rest}"
fi
}
# Function to check installation status
check_installation() {
if systemctl is-active --quiet nginx && [ -f "/etc/nginx/sites-available/$domain" ]; then
(crontab -l 2>/dev/null | grep -v 'certbot renew --nginx --force-renewal --non-interactive --post-hook "nginx -s reload"' ; echo '0 0 1 * * certbot renew --nginx --force-renewal --non-interactive --post-hook "nginx -s reload" > /dev/null 2>&1;') | crontab -
echo ""
echo -e "${purple}Certificate and Key saved at:${rest}"
echo -e "${yellow}×××××××××××××××××××××××××××××××××××××××××××××××××××××${rest}"
echo -e "${cyan}/etc/letsencrypt/live/$domain/fullchain.pem${rest}"
echo -e "${cyan}/etc/letsencrypt/live/$domain/privkey.pem${rest}"
echo -e "${yellow}×××××××××××××××××××××××××××××××××××××××××××××××××××××${rest}"
echo -e "${cyan}🌟 N R P installed Successfully.🌟${rest}"
echo -e "${yellow}××××××××××××××××××××××××××××××××${rest}"
else
echo ""
echo -e "${yellow}×××××××××××××××××××××××${rest}"
echo -e "${red}❌N R P installation failed.❌${rest}"
echo -e "${yellow}×××××××××××××××××××××××${rest}"
fi
}
# Change Paths
change_path() {
if systemctl is-active --quiet nginx && [ -f "/etc/nginx/sites-available/$saved_domain" ]; then
echo -e "${yellow}×××××××××××××××××××××××${rest}"
read -p "Enter the new GRPC path (Service Name) [default: grpc]: " new_grpc_path
new_grpc_path=${new_grpc_path:-grpc}
echo -e "${yellow}×××××××××××××××××××××××${rest}"
read -p "Enter the new WebSocket path (Service Name) [default: ws]: " new_ws_path
new_ws_path=${new_ws_path:-ws}
echo -e "${yellow}×××××××××××××××××××××××${rest}"
sed -i "14s|location ~ .* {$|location ~ ^/${new_grpc_path}/(?<port>\\\d+)/(.*)$ {|" /etc/nginx/sites-available/$saved_domain
sed -i "28s|location ~ .* {$|location ~ ^/${new_ws_path}/(?<port>\\\d+)$ {|" /etc/nginx/sites-available/$saved_domain
# Restart Nginx
systemctl restart nginx
echo -e " ${purple}Paths Changed Successfully${cyan}:
|-----------------|-------|
| GRPC Path | ${yellow}$new_grpc_path
${cyan}| WebSocket Path | ${yellow}$new_ws_path ${cyan}
|-----------------|-------|${rest}"
echo -e "${yellow}×××××××××××××××××××××××${rest}"
else
echo -e "${yellow}×××××××××××××××××××××××${rest}"
echo -e "${red}N R P is not installed.${rest}"
echo -e "${yellow}×××××××××××××××××××××××${rest}"
fi
}
# Install random site
install_random_fake_site() {
if [ ! -d "/etc/letsencrypt/live/$saved_domain" ]; then
echo -e "${yellow}×××××××××××××××××××××××${rest}"
echo -e "${red}Nginx is not installed.${rest}"
echo -e "${yellow}×××××××××××××××××××××××${rest}"
exit 1
fi
if [ ! -d "/var/www/html" ]; then
echo -e "${yellow}×××××××××××××××××××××××${rest}"
echo -e "${red}/var/www/html does not exist.${rest}"
exit 1
fi
if [ ! -d "/var/www/website-templates" ]; then
echo -e "${yellow}×××××××××××××××××××××××${rest}"
echo -e "${yellow}Downloading Websites list...${rest}"
sudo git clone https://github.com/learning-zone/website-templates.git /var/www/website-templates
fi
cd /var/www/website-templates
sudo rm -rf /var/www/html/*
random_folder=$(ls -d */ | shuf -n 1)
sudo mv "$random_folder"/* /var/www/html
echo -e "${yellow}×××××××××××××××××××××××${rest}"
echo -e "${green}Website Installed Successfully${rest}"
echo -e "${yellow}×××××××××××××××××××××××${rest}"
}
# Limitation
add_limit() {
# Check if NGINX service is installed
if [ ! -d "/etc/letsencrypt/live/$saved_domain" ]; then
echo -e "${yellow}×××××××××××××××××××××××${rest}"
echo -e "${red}N R P is not installed.${rest}"
echo -e "${yellow}×××××××××××××××××××××××${rest}"
exit 1
fi
total_usage(){
interface=$(ip -o link show | awk -F': ' '{print $2}' | grep -v "lo" | head -n 1) > /dev/null 2>&1
data=$(grep "$interface:" /proc/net/dev)
download=$(echo "$data" | awk '{print $2}')
upload=$(echo "$data" | awk '{print $10}')
total_mb=$(echo "scale=2; ($download + $upload) / 1024 / 1024" | bc)
echo -e "${cyan}T${yellow}o${cyan}t${yellow}a${cyan}l${yellow} U${cyan}s${yellow}a${cyan}g${yellow}e${cyan}: ${purple}[$total_mb] ${cyan}MB${rest}"
}
echo -e "${yellow}×××××××××××××××××××××××${rest}"
echo -e "${yellow}* ${cyan}This option adds a traffic limit to monitor increases in traffic compared to the last 24 hours.${yellow}*${rest}"
echo ""
echo -e "${yellow}* ${cyan}If the traffic exceeds this limit, the nginx service will be stopped.${yellow}*${rest}"
echo ""
total_usage
echo -e "${yellow}* ${cyan}[${yellow}Note${cyan}]: ${cyan}After restarting the server, the ${cyan}T${yellow}o${cyan}t${yellow}a${cyan}l${yellow} U${cyan}s${yellow}a${cyan}g${yellow}e${cyan} will also be reset.${yellow}*${rest}"
echo -e "${yellow}×××××××××××××××××××××××${rest}"
read -p "Enter the percentage limit [default: 50]: " percentage_limit
percentage_limit=${percentage_limit:-50}
echo -e "${yellow}×××××××××××××××××××××××${rest}"
if [ ! -d "/root/usage" ]; then
mkdir -p /root/usage
fi
cat <<EOL > /root/usage/limit.sh
#!/bin/bash
# Define the interface
interface=\$(ip -o link show | awk -F': ' '{print \$2}' | grep -v "lo" | head -n 1)
# Current total traffic data
get_total(){
data=\$(grep "\$interface:" /proc/net/dev)
download=\$(echo "\$data" | awk '{print \$2}')
upload=\$(echo "\$data" | awk '{print \$10}')
total_mb=\$(echo "scale=2; (\$download + \$upload) / 1024 / 1024" | bc)
echo "\$total_mb"
}
# Check traffic increase
check_traffic_increase() {
current_total_mb=\$(get_total)
# Check if file exists
if [ -f "/root/usage/\${interface}_traffic.txt" ]; then
# Read the traffic data from file
read -r prev_total_mb < "/root/usage/\${interface}_traffic.txt"
# Calculate traffic increase percentage
increase=\$(echo "scale=2; (\$current_total_mb - \$prev_total_mb) / \$prev_total_mb * 100" | bc)
# Display message if traffic increase is greater than \$percentage_limit%
if (( \$(echo "\$increase > $percentage_limit" | bc) )); then
sudo systemctl stop nginx
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Traffic on interface \$interface increased by more than $percentage_limit% compared to previous:" >> /root/usage/log.txt
fi
fi
# Save current traffic data to file
echo "\$current_total_mb" > "/root/usage/\${interface}_traffic.txt"
}
check_traffic_increase
EOL
# Set execute permission for the created script
chmod +x /root/usage/limit.sh && /root/usage/limit.sh
# Schedule the script to run every 24 hours using cron job
(crontab -l 2>/dev/null | grep -v '/root/usage/limit.sh' ; echo '0 0 * * * /root/usage/limit.sh > /dev/null 2>&1;') | crontab -
}
# Change port
change_port() {
if [ -f "/etc/nginx/sites-available/$saved_domain" ]; then
current_port=$(grep -oP "listen \[::\]:\K\d+" "/etc/nginx/sites-available/$saved_domain" | head -1)
echo -e "${yellow}×××××××××××××××××××××××${rest}"
echo -e "${cyan}Current HTTPS port: ${purple}$current_port${rest}"
echo -e "${yellow}×××××××××××××××××××××××${rest}"
read -p "Enter the new HTTPS port [default: 443]: " new_port
new_port=${new_port:-443}
echo -e "${yellow}×××××××××××××××××××××××${rest}"
# Change the port in NGINX configuration file
sed -i "s/listen \[::\]:$current_port ssl http2 ipv6only=on;/listen [::]:$new_port ssl http2 ipv6only=on;/g" "/etc/nginx/sites-available/$saved_domain"
sed -i "s/listen $current_port ssl http2;/listen $new_port ssl http2;/g" "/etc/nginx/sites-available/$saved_domain"
# Restart NGINX service
systemctl restart nginx
# Check if NGINX restarted successfully
if systemctl is-active --quiet nginx; then
echo -e "${green}✅ HTTPS port changed successfully to ${purple}$new_port${rest}"
else
echo -e "${red}❌ Error: NGINX failed to restart.${rest}"
fi
echo -e "${yellow}×××××××××××××××××××××××${rest}"
else
echo -e "${yellow}×××××××××××××××××××××××××××××××××××${rest}"
echo -e "${red}N R P is not installed.${rest}"
echo -e "${yellow}×××××××××××××××××××××××××××××××××××${rest}"
fi
}
# Uninstall N R P
uninstall() {
# Check if NGINX is installed
if [ ! -d "/etc/letsencrypt/live/$saved_domain" ]; then
echo -e "${yellow}×××××××××××××××××××××××${rest}"
echo -e "${red}N R P is not installed.${rest}"
echo -e "${yellow}×××××××××××××××××××××××${rest}"
else
echo -e "${green}☑️Uninstalling... ${rest}"
# Remove SSL certificate files
rm -rf /etc/letsencrypt > /dev/null 2>&1
rm -rf /var/www/html/* > /dev/null 2>&1
# Remove NGINX configuration files
find /etc/nginx/sites-available/ -mindepth 1 -maxdepth 1 ! -name 'default' -exec rm -rf {} +
find /etc/nginx/sites-enabled/ -mindepth 1 -maxdepth 1 ! -name 'default' -exec rm -rf {} +
# Restart NGINX service
systemctl restart nginx
echo -e "${yellow}××××××××××××××××××××××××××××××${rest}"
echo -e "${green}N R P uninstalled successfully.${rest}"
echo -e "${yellow}××××××××××××××××××××××××××××××${rest}"
fi
}
clear
echo -e "${cyan}By --> Peyman * Github.com/Ptechgithub * ${rest}"
echo ""
check_status
echo -e "${purple}***********************${rest}"
echo -e "${yellow}* ${cyan}N${green}ginx ${cyan}R${green}everse ${cyan}P${green}roxy${yellow} *${rest}"
echo -e "${purple}***********************${rest}"
echo -e "${yellow} 1) ${green}Install ${purple}*${rest}"
echo -e "${purple} * ${rest}"
echo -e "${yellow} 2) ${green}Change Paths${rest} ${purple}*${rest}"
echo -e "${purple} * ${rest}"
echo -e "${yellow} 3) ${green}Change Https Port${rest} ${purple}*${rest}"
echo -e "${purple} * ${rest}"
echo -e "${yellow} 4) ${green}Install Fake Site${rest} ${purple}*${rest}"
echo -e "${purple} * ${rest}"
echo -e "${yellow} 5) ${green}Add Traffic Limit${rest} ${purple}*${rest}"
echo -e "${purple} * ${rest}"
echo -e "${yellow} 6) ${green}Uninstall${rest} ${purple}*${rest}"
echo -e "${purple} * ${rest}"
echo -e "${yellow} 0) ${purple}Exit${rest}${purple} *${rest}"
echo -e "${purple}***********************${rest}"
read -p "Enter your choice: " choice
case "$choice" in
1)
install
;;
2)
change_path
;;
3)
change_port
;;
4)
install_random_fake_site
;;
5)
add_limit
;;
6)
uninstall
;;
0)
echo -e "${cyan}By 🖐${rest}"
exit
;;
*)
echo -e "${yellow}××××××××××××××××××××××××××××××${rest}"
echo "Invalid choice. Please select a valid option."
echo -e "${yellow}××××××××××××××××××××××××××××××${rest}"
;;
esac