-
Notifications
You must be signed in to change notification settings - Fork 39
/
xstack-installer.sh
413 lines (285 loc) · 10.9 KB
/
xstack-installer.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
#!/usr/bin/env bash
# COLORS
RED='\033[0;31m'
NC='\033[0m'
BLUE='\033[0;34m'
WHITE='\033[0;37m'
RED='\033[0;31m'
# Set the color variable
green='\033[0;32m'
# Clear the color after that
clear='\033[0m'
# Functions
function progress {
barr=''
for (( y=50; y <= 100; y++ )); do
sleep 0.05
barr="${barr} "
echo -ne "\r"
echo -ne "\e[43m$barr\e[0m"
local left="$(( 100 - $y ))"
printf " %${left}s"
echo -n "${y}% Please Wait"
done
echo -e "\n"
}
# check port 80 is in use or no
check_port() {
if [ -z "$(netstat -tulpn | grep :'80\s')" ];
then
return 1;
else
echo -e "${RED}[ERROR]${clear} Port 80 is currently in use."
exit;
fi
}
show_menu(){
normal=`echo "\033[m"`
menu=`echo "\033[36m"` #Blue
number=`echo "\033[33m"` #yellow
bgred=`echo "\033[41m"`
fgred=`echo "\033[31m"`
printf "\n${menu}*********************************************${normal}\n"
printf "${menu}* *${normal}\n"
printf "${menu}* ${RED}X${WHITE}stack plugin started!${menu} *${normal}\n"
printf "${menu}* *${normal}\n"
printf "${menu}*********************************************${normal}\n"
printf "${menu}**${number} 0)${menu} Install the script requirements ${normal}\n"
printf "${menu}**${number} 1)${menu} Setup Domain on IP ${normal}\n"
printf "${menu}**${number} 2)${menu} Install X-UI ${normal}\n"
printf "${menu}**${number} 3)${menu} Uninstall X-UI ${normal}\n"
printf "${menu}**${number} 4)${menu} setup SSL on X-UI panel ${normal}\n"
printf "${menu}**${number} 5)${menu} Install API Panel on Server ${normal}\n"
printf "${menu}**${number} 6)${menu} Exit ${normal}\n"
printf "${menu}*********************************************${normal}\n"
printf "Please choice: ${normal}"
read opt
}
# check nano is installed or no
nano_check(){
REQUIRED_PKG="nano"
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG|grep "install ok installed")
progress
echo -e Packege $REQUIRED_PKG was ${green}installed${clear}.
if [ "" = "$PKG_OK" ]; then
echo "No $REQUIRED_PKG. Setting up $REQUIRED_PKG."
apt-get --yes install $REQUIRED_PKG #install nano if not installed
fi
}
# install wget,curl,certbot,sqlite3,git and then unzip that
install_check(){
progress
apt-get install nano wget curl net-tools certbot sqlite3 git unzip -y
}
install_req() {
update_fun
install_check
nano_check
}
# first server update and upgrade
update_fun(){
apt-get update
apt-get upgrade
}
# Command for install and set api panel
install_API_Panel() {
if ! check_xui_nue; then
FILE=$(find / -name "x-ui*.db*")
if [[ -f "$FILE" ]]; then
apt-get apache2 php php-mysql sqlite3 libsqlite3-dev php-sqlite3
mv /var/www/html/index.html /var/www/html/index.back
chmod 751 /etc/
chmod 777 /etc/x-ui*/
chmod 777 ${FILE}
cd /var/www/html/
# wget -q https://github.com/4xmen/x-ui/releases/download/0.5.4/api-x-ui.zip -O api-x-ui.zip
wget -O api-x-ui.zip -N https://github.com/4xmen/x-ui/releases/download/0.5.4/api-x-ui.zip
unzip -o api-x-ui.zip
progress
ee=$(sed -i "s|\$fileAddress = .*;|\$fileAddress = '$FILE';|" /var/www/html/config.php)
echo $ee
else
echo -e "${RED}[ERORR]${clear} The database file was not found! Please reinstall."
fi
else
echo -e "${RED}[ERORR]${clear} Unfortunately, x-ui is not installed yet!please istall that."
exit 1;
fi
}
# check x-ui is installed or no for install
check_xui(){
if ! netstat -tulpn | grep 'x-ui'
then
progress
else
echo ""
echo ""
echo -e "${RED}[ERROR]${clear} You have already installed x-ui ! please unistall that."
exit 1
fi
}
# check x-ui is installed or no for uninstall
check_xui_nue(){
if ! netstat -tulpn | grep 'x-ui'
then
echo -e "${RED}[ERROR]${clear} Unfortunately, x-ui is not installed yet!please istall that."
exit 1
else
progress
return 1
fi
}
# check root access in terminal
check_root(){
if [ "$EUID" -ne 0 ]
then echo -e "${RED}[ERROR]${clear} Please run as root !"
exit
fi
}
# first choice for setup domain on IP
setupDomain() {
update_fun
sleep 1
if ! check_port; then
printf "Please enter email:"
read email
printf "Please enter domain:"
read domain
certbot certonly --standalone --non-interactive --preferred-challenges http --agree-tos --email ${email} -d ${domain}
echo -e "${green}[success]${clear} the domain was setup :)"
echo ""
echo "launch again xstack installer!"
fi
}
# seconde choice is for install x-ui panel
installXUI() {
update_fun
if ! check_xui; then
exit 1
else
echo -e "${green}[SUCCESS]${clear} Your server has been checked and is ready to install"
wget -q https://raw.githubusercontent.com/4xmen/x-ui/master/install.sh -O install.sh
printf "Please enter username:"
read username
printf "Please enter password:"
read password
printf "Please enter port:"
read port
echo "y
$username
$password
$port
" | bash install.sh;
fi
}
# third choice for uninstall x-ui
uninstallXUI() {
if ! check_xui_nue; then
echo -e "${green}[SUCCESS]${clear} Your server has been checked and is ready to uninstall"
printf "Are you sure to uninstall x-ui? Y/n : "
read questunistall
if [[ $questunistall == 'Y' || $questunistall == 'y' ]]; then
echo 'Y' | x-ui uninstall
progress
rm /usr/bin/x-ui -f
echo -e "${green}[SUCCESS]${clear} x-ui successfully uninstalled"
else
exit 1
fi
else
exit 1
fi
}
# setup SSL on panel without going to the panel
setupSSL() {
if ! check_xui_nue; then
FILE=$(find / -name "x-ui*.db*")
sql='select * from settings'
if [[ -f "$FILE" ]]; then
if sqlite3 ${FILE} "select * from settings where key='webCertFile';" | grep -i 'webCertFile' && sqlite3 ${FILE} "select * from settings where key='webKeyFile';" | grep -i 'webKeyFile'; then
CertificatePath=$(certbot certificates | grep -oP '(?<=Certificate Path: ).*(?=.pem)')
PrivateKeyPath=$(certbot certificates | grep -oP '(?<=Private Key Path: ).*(?=.pem)')
webCertFile="${CertificatePath}.pem"
webKeyFile="${PrivateKeyPath}.pem"
progress
sqlite3 ${FILE} "UPDATE settings SET key='webCertFile',value=\"${webCertFile}\" WHERE key='webCertFile';"
echo -e "${green}[SUCCESS]${clear} The certificate public key file is set."
progress
sqlite3 ${FILE} "UPDATE settings SET key='webKeyFile',value=\"${webKeyFile}\" WHERE key='webKeyFile';"
echo -e "${green}[SUCCESS]${clear} The certificate key file is set."
x-ui restart
echo -e "${green}[SUCCESS]${clear} ssl was successfully set on the panel."
else
CertificatePath=$(certbot certificates | grep -oP '(?<=Certificate Path: ).*(?=.pem)')
PrivateKeyPath=$(certbot certificates | grep -oP '(?<=Private Key Path: ).*(?=.pem)')
webCertFile="${CertificatePath}.pem"
webKeyFile="${PrivateKeyPath}.pem"
progress
sqlite3 ${FILE} "insert into settings (key,value) values (\"webCertFile\",\"$webCertFile\");"
echo -e "${green}[SUCCESS]${clear} The certificate public key file is set."
progress
sqlite3 ${FILE} "insert into settings (key,value) values (\"webKeyFile\",\"$webKeyFile\");"
echo -e "${green}[SUCCESS]${clear} The certificate key file is set."
x-ui restart
echo -e "${green}[SUCCESS]${clear} ssl was successfully set on the panel."
fi
else
echo -e "${RED}[ERORR]${clear} The database file was not found! Please reinstall."
fi
else
echo -e "${RED}[ERORR]${clear} Unfortunately, x-ui is not installed yet!please istall that."
exit 1;
fi
}
# starting installer with check root access and then show menu
start_shell(){
string="
▀████ ▐████▀ ▄████████ ███ ▄████████ ▄████████ ▄█ ▄█▄
███▌ ████▀ ███ ███ ▀█████████▄ ███ ███ ███ ███ ███ ▄███▀
███ ▐███ ███ █▀ ▀███▀▀██ ███ ███ ███ █▀ ███▐██▀
▀███▄███▀ ███ ███ ▀ ███ ███ ███ ▄█████▀
████▀██▄ ▀███████████ ███ ▀███████████ ███ ▀▀█████▄
▐███ ▀███ ███ ███ ███ ███ ███ █▄ ███▐██▄
▄███ ███▄ ▄█ ███ ███ ███ ███ ███ ███ ███ ▀███▄
████ ███▄ ▄████████▀ ▄████▀ ███ █▀ ████████▀ ███ ▀█▀
▀
"
clear
for ((i=0; i<=${#string}; i++)); do
printf '%s' "${string:$i:1}"
sleep 0.0005
done
DIR=/etc/nginx/sites-available/default
echo ""
check_root
show_menu
}
start_shell
case $opt in
0)
install_req
;;
1)
setupDomain
;;
2)
installXUI
;;
3)
uninstallXUI
;;
4)
setupSSL
;;
5)
install_API_Panel
;;
6)
echo "installer was closed !"
exit
;;
*)
echo -e "${RED}[ERROR]${WHITE} Wrong input, please be careful!"
exit 1
;;
esac