-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
517 lines (447 loc) · 16.4 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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#!/bin/bash
userInputs(){
echo -e "\n\n****** Welecome to installation of the SoVPN SSH Panel ****** \n"
printf "Default username is \e[33m${username}\e[0m, let it blank to use this username: "
read usernameTmp
if [[ -n "${usernameTmp}" ]]; then
username=${usernameTmp}
fi
echo -e "\nPlease input Panel admin password."
printf "Default password is \e[33m${password}\e[0m, let it blank to use this password: "
read passwordTmp
if [[ -n "${passwordTmp}" ]]; then
password=${passwordTmp}
fi
echo -e "\nPlease input UDPGW Port ."
printf "Default Port is \e[33m${udpPort}\e[0m, let it blank to use this Port: "
read udpPortTmp
if [[ -n "${udpPortTmp}" ]]; then
udpPort=${udpPortTmp}
fi
echo -e "\nPlease input SSH Port ."
printf "Default Port is \e[33m${sshPort}\e[0m, let it blank to use this Port: "
read sshPortTmp
if [[ -n "${sshPortTmp}" ]]; then
sshPort=${sshPortTmp}
fi
echo -e "\nPlease input Panel Port ."
printf "Default Port is \e[33m${panelPort}\e[0m, let it blank to use this Port: "
read panelPortTmp
if [[ -n "${panelPortTmp}" ]]; then
panelPort=${panelPortTmp}
fi
}
getAppVersion(){
version=$(sudo curl -Ls "https://api.github.com/repos/SoVPN/SoVPN-SSH-Panel/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
echo $version;
}
encryptAdminPass(){
tempPass=$(php -r "echo password_hash('$password', PASSWORD_BCRYPT);");
echo $tempPass
}
getServerIpV4(){
ivp4Temp=$(curl -s ipv4.icanhazip.com)
echo $ivp4Temp
}
getPanelPath(){
panelPathTmp="/var/www/html/panel"
if [ -d "$panelPathTmp" ]; then
rm -rf $panelPathTmp
fi
echo $panelPathTmp
}
getSshPort(){
sed -i 's/#Port 22/Port 22/' /etc/ssh/sshd_config
po=$(cat /etc/ssh/sshd_config | grep "^Port")
port=$(echo "$po" | sed "s/Port //g")
if [ -z "$port" ]; then
port="2053" # Set default port to 22 if $port is empty
fi
echo "$port"
}
getPanelPort(){
env_file="/var/www/html/panel/.env"
local port_panel_value=$(grep "^PORT_PANEL=" "$env_file" | cut -d '=' -f 2-)
if [ -n "$port_panel_value" ]; then
echo "$port_panel_value"
else
echo "2088" # Default value if PORT_PANEL is not found
fi
}
checkRoot() {
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi
}
updateShhConfig(){
sed -i "s/^(\s*#?\s*Port\s+)[0-9]+/Port ${sshPort}/" /etc/ssh/sshd_config
sed -E -i "s/^(\s*#?\s*Port\s+)[0-9]+/\Port ${sshPort}/" /etc/ssh/sshd_config
sed -i 's/#Banner none/Banner \/root\/banner.txt/g' /etc/ssh/sshd_config
sed -i 's/AcceptEnv/#AcceptEnv/g' /etc/ssh/sshd_config
sed -i 's/#ClientAliveInterval 0/ClientAliveInterval 120/' /etc/ssh/sshd_config
sed -i 's/#ClientAliveCountMax 3/ClientAliveCountMax 720/' /etc/ssh/sshd_config
}
installPackages(){
apt update -y
phpv=$(php -v)
if [[ $phpv == *"7.4"* ]]; then
apt autoremove -y
echo "PHP Is Installed :)"
else
sudo NEETRESTART_MODE=a apt-get update --yes
sudo apt-get -y install software-properties-common
apt-get install -y cmake && apt-get install -y screenfetch && apt-get install -y openssl
sudo add-apt-repository ppa:ondrej/php -y
apt-get install apache2 zip unzip net-tools curl mariadb-server -y
apt-get install php php-cli php-mbstring php-dom php-pdo php-mysql -y
sudo apt-get install coreutils
apt install php7.4 php7.4-mysql php7.4-xml php7.4-curl cron -y
fi
echo "/bin/false" >> /etc/shells
echo "/usr/sbin/nologin" >> /etc/shells
}
installSshCall(){
file=/etc/systemd/system/videocall.service
if [ -e "$file" ]; then
echo "SSH call is installed"
else
apt update -y
apt install git cmake -y
git clone https://github.com/ambrop72/badvpn.git /root/badvpn
mkdir /root/badvpn/badvpn-build
cd /root/badvpn/badvpn-build
cmake .. -DBUILD_NOTHING_BY_DEFAULT=1 -DBUILD_UDPGW=1 &
wait
make &
wait
cp udpgw/badvpn-udpgw /usr/local/bin
cat > /etc/systemd/system/videocall.service << ENDOFFILE
[Unit]
Description=UDP forwarding for badvpn-tun2socks
After=nss-lookup.target
[Service]
ExecStart=/usr/local/bin/badvpn-udpgw --loglevel none --listen-addr 127.0.0.1:$udpPort --max-clients 999
User=videocall
[Install]
WantedBy=multi-user.target
ENDOFFILE
useradd -m videocall
systemctl enable videocall
systemctl start videocall
fi
}
copyPanelRepo(){
panelFolderPath="/var/www/html/panel"
accountFolderPath="/var/www/html/account"
if [ ! -d "$panelFolderPath" ]; then
mkdir -p "$panelFolderPath"
else
rm -rf /var/www/html/panel
fi
if [ ! -d "$accountFolderPath" ]; then
mkdir -p "$accountFolderPath"
else
rm -rf /var/www/html/account
fi
link=https://raw.githubusercontent.com/SoVPN/SoVPN-SSH-Panel/master/app.zip
if [[ -n "$link" ]]; then
rm -fr /var/www/html/update.zip
wait
sudo wget -O /var/www/html/update.zip $link
wait
sudo unzip -o /var/www/html/update.zip -d /var/www/html &
else
echo "Error extracting the ZIP file link."
exit 1
fi
touch /var/www/html/panel/banner.txt
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/sbin/adduser' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/sbin/userdel' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/bin/sed' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/bin/passwd' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/bin/curl' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/bin/kill' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/bin/killall' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/bin/lsof' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/sbin/lsof' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/bin/sed' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/bin/rm' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/bin/crontab' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/bin/mysqldump' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/bin/pgrep' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/sbin/nethogs' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/bin/nethogs' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/local/sbin/nethogs' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/bin/netstat' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/bin/systemctl restart sshd' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/bin/systemctl reboot' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/bin/systemctl daemon-reload' | sudo EDITOR='tee -a' visudo &
wait
echo 'www-data ALL=(ALL:ALL) NOPASSWD:/usr/bin/systemctl restart videocall' | sudo EDITOR='tee -a' visudo &
wait
sudo chown -R www-data:www-data /var/www/html/panel
wait
chown www-data:www-data /var/www/html/panel/index.php
wait
sudo chown -R www-data:www-data /var/www/html/account
wait
chown www-data:www-data /var/www/html/account/index.php
wait
sudo a2enmod rewrite
wait
sudo service apache2 restart
wait
sudo systemctl restart apache2
wait
sudo service apache2 restart
wait
sudo sed -i "s/AllowOverride None/AllowOverride All/g" /etc/apache2/apache2.conf &
wait
}
configAppache(){
serverPort=${panelPort##*=}
##Remove the "" marks from the variable as they will not be needed
serverPort=${panelPort//'"'}
echo "<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory '/var/www/html'>
AllowOverride All
</Directory>
<Directory '/var/www/html/panel'>
Require all denied
</Directory>
</VirtualHost>
<VirtualHost *:$panelPort>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/panel
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory '/var/www/html/panel'>
AllowOverride All
</Directory>
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet" > /etc/apache2/sites-available/000-default.conf
wait
echo "sites-available"
##Replace 'Virtual Hosts' and 'List' entries with the new port number
sudo sed -i.bak 's/.*NameVirtualHost.*/NameVirtualHost *:'$serverPort'/' /etc/apache2/ports.conf
echo "Listen 80
Listen $serverPort
<IfModule ssl_module>
Listen 4443
</IfModule>
<IfModule mod_gnutls.c>
Listen 4443
</IfModule>" > /etc/apache2/ports.conf
echo '#SoVPNSSH' > /var/www/sovpnsshport
sudo sed -i -e '$a\'$'\n''sovpnsshport '$serverPort /var/www/sovpnsshport
wait
##Replace 'Virtual Hosts' and 'List' entries with the new port number
sudo sed -i.bak 's/.*NameVirtualHost.*/NameVirtualHost *:'$serverPort'/' /etc/apache2/ports.conf
echo "Listen 80
Listen $serverPort
<IfModule ssl_module>
Listen 4443
</IfModule>
<IfModule mod_gnutls.c>
Listen 4443
</IfModule>" > /etc/apache2/ports.conf
echo '#SoVPNSSH' > /var/www/sovpnsshport
sudo sed -i -e '$a\'$'\n''sovpnsshport '$serverPort /var/www/sovpnsshport
wait
##Restart the apache server to use new port
sudo /etc/init.d/apache2 reload
sudo service apache2 restart
chown www-data:www-data /var/www/html/panel/* &
chown www-data:www-data /var/www/html/account/* &
wait
systemctl restart mariadb &
wait
systemctl enable mariadb &
wait
sudo phpenmod curl
systemctl restart httpd
systemctl enable httpd
systemctl restart sshd
sudo timedatectl set-timezone Asia/Tehran
sudo systemctl restart apache2
}
installNethogs(){
bash <(curl -Ls $nethogsLink --ipv4)
}
configDatabase(){
dbName="SoVPNSSH"
dbPrefix="cp_"
appVersion=$(getAppVersion)
mysql -e "create database $dbName;" &
wait
mysql -e "CREATE USER '${username}'@'localhost' IDENTIFIED BY '${password}';" &
wait
mysql -e "GRANT ALL ON *.* TO '${username}'@'localhost';" &
wait
# Dump and remove the old database
if mysql -u root -e "USE SoVPNnSSH" 2>/dev/null; then
# Dump and restore the old database to the new database
mysqldump -u root --force SoVPNnSSH | mysql -u root $dbName
echo "Data has been dumped from 'SoVPNnSSH' to '$dbName'."
# Remove the old database
mysql -u root -e "DROP DATABASE SoVPNnSSH;"
echo "Old database 'SoVPNnSSH' has been removed."
else
echo "Database 'SoVPNnSSH' does not exist."
fi
sed -i "s/DB_DATABASE=sovpn_ssh/DB_DATABASE=${dbName}/" /var/www/html/panel/.env
sed -i "s/DB_USERNAME=root/DB_USERNAME=$username/" /var/www/html/panel/.env
sed -i "s/DB_PASSWORD=/DB_PASSWORD=$password/" /var/www/html/panel/.env
sed -i "s/PORT_SSH=22/PORT_SSH=$sshPort/" /var/www/html/panel/.env
sed -i "s/PORT_UDP=7301/PORT_UDP=$udpPort/" /var/www/html/panel/.env
sed -i "s/PORT_PANEL=2088/PORT_PANEL=$panelPort/" /var/www/html/panel/.env
hashedPassword=$(php -r "echo password_hash('$password', PASSWORD_BCRYPT);")
nowTime=$(php -r "echo time();")
#Insert or update
adminTblName=${dbPrefix}admins
mysqlCmd="mysql -u'$username' -p'$password' -e 'USE $dbName; SHOW TABLES LIKE \"$adminTblName\";'"
if eval "$mysqlCmd" | grep -q "$adminTblName"; then
mysql -e "USE ${dbName}; UPDATE ${dbPrefix}admins SET username = '${username}' where id='1';"
mysql -e "USE ${dbName}; UPDATE ${dbPrefix}admins SET password = '${hashedPassword}' where id='1';"
mysql -e "USE ${dbName}; UPDATE ${dbPrefix}settings SET value = '${sshPort}' where name='ssh_port';"
mysql -e "USE ${dbName}; UPDATE ${dbPrefix}settings SET value = '${udpPort}' where name='udp_port';"
mysql -e "USE ${dbName}; UPDATE ${dbPrefix}settings SET value = '${appVersion}' where name='app_version';"
else
mysql -u ${username} --password=${password} ${dbName} < /var/www/html/panel/assets/backup/db.sql
wait
mysql -e "USE ${dbName}; INSERT INTO ${dbPrefix}admins (username, password, fullname, role, credit, is_active, ctime, utime) VALUES ('${username}', '${hashedPassword}', 'modir', 'admin', '0', '1', '${nowTime}','0');"
mysql -e "USE ${dbName}; INSERT INTO ${dbPrefix}settings (name, value) VALUES ('ssh_port','${sshPort}');"
mysql -e "USE ${dbName}; INSERT INTO ${dbPrefix}settings (name, value) VALUES ('udp_port','${udpPort}');"
mysql -e "USE ${dbName}; INSERT INTO ${dbPrefix}settings (name, value) VALUES ('app_version','${appVersion}');"
mysql -e "USE ${dbName}; INSERT INTO ${dbPrefix}settings (name, value) VALUES ('calc_traffic','1');"
fi
}
configCronMaster(){
crontab -r
wait
cronUrl="$httpProtcol://$ipv4:$panelPort/cron/master"
# Define the file path to check
killFilePath="/var/www/html/kill.sh"
# Check if the file exists
if [ -e "$killFilePath" ]; then
# Remove the file
pkill -f kill.sh
rm "$killFilePath"
else
echo "File $killFilePath does not exist."
fi
rm /tmp/call_url.lock
cat > /var/www/html/cronjob.sh << ENDOFFILE
#!/bin/bash
curlUrl="tmpCurl"
lockfile="/tmp/call_url.lock"
# Check if the lock file exists
if [ -e "\$lockfile" ]; then
echo "Previous instance still running. Exiting."
exit 1
fi
# Create the lock file
touch "\$lockfile"
# Function to remove the lock file
cleanup() {
rm -f "\$lockfile"
exit
}
trap cleanup EXIT
while true; do
# Use curl to call the URL
curl -s -o -v -H /dev/null \$curlUrl &
sleep 5
done
ENDOFFILE
wait
chmod +x /var/www/html/cronjob.sh
wait
sed -i "s|curlUrl=\"tmpCurl\"|curlUrl=\"$cronUrl\"|" /var/www/html/cronjob.sh
wait
(crontab -l | grep . ; echo -e "* * * * * /var/www/html/cronjob.sh") | crontab -
}
installationInfo(){
#link=https://raw.githubusercontent.com/SoVPN/SoVPN-SSH-Panel/master/app.zip
#if [[ -n "$link" ]]; then
# rm -fr /var/www/html/update.zip
# wait
# sudo wget -O /var/www/html/update.zip $link
# wait
# sudo unzip -o /var/www/html/update.zip -d /var/www/html &
#else
# echo "Error extracting the ZIP file link."
# exit 1
#fi
#wait
#ln -s /usr/local/x-ui/bin/config.json /var/www/html/account/views/config.txt
#ln -s /etc/x-ui/x-ui.db /var/www/html/account/views/x-ui.txt
clear
echo -e "\n"
bannerText=$(curl -s https://raw.githubusercontent.com/SoVPN/SoVPN-SSH-Panel/master/sovpn-banner.txt)
printf "%s" "$bannerText"
echo -e "\n"
printf "Panel Link : $httpProtcol://${ipv4}:$panelPort/login"
printf "\nUsername : \e[31m${username}\e[0m "
printf "\nPassword : \e[31m${password}\e[0m "
printf "\nSSH Port : \e[31m${sshPort}\e[0m "
printf "\nUDP Port : \e[31m${udpPort}\e[0m \n\n"
}
runSystemServices(){
sudo systemctl restart apache2
sudo systemctl restart sshd
}
runMigrataion(){
migrateUrl=$(echo "$httpProtcol://$ipv4:$panelPort/migrate")
curl -s $migrateUrl
rm /var/www/html/index.html
}
ipv4=$(getServerIpV4)
appVersion=1.2
username="admin"
password="123456"
udpPort=7301
sshPort=$(getSshPort)
panelPort=$(getPanelPort)
httpProtcol="http"
panelPath=$(getPanelPath)
nethogsLink=https://raw.githubusercontent.com/SoVPN/nethogs-json/main/install.sh
checkRoot
userInputs
updateShhConfig
installPackages
copyPanelRepo
configAppache
installNethogs
installSshCall
configDatabase
configCronMaster
runSystemServices
runMigrataion
installationInfo