-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstall.sh
325 lines (283 loc) · 11.2 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
#!/bin/bash
# DDS : DailyDigtalSKiills
# YOUTUBE LINK: https://www.youtube.com/@DailyDigtalSKills
#Telegram: @DailyDigtalSKiills
# Colors for better readability
colors=(
"\033[38;2;255;105;180m" # Foreground (#EA549F)
"\033[38;2;255;20;147m" # Red (#E92888)
"\033[38;2;0;255;144m" # Green (#4EC9B0)
"\033[38;2;0;191;255m" # Blue (#579BD5)
"\033[38;2;102;204;255m" # Bright Blue (#9CDCFE)
"\033[38;2;242;242;242m" # Bright White (#EAEAEA)
"\033[38;2;0;255;255m" # Cyan (#00B6D6)
"\033[38;2;255;215;0m" # Bright Yellow (#e9ad95)
"\033[38;2;160;32;240m" # Purple (#714896)
"\033[38;2;255;36;99m" # Bright Red (#EB2A88)
"\033[38;2;0;255;100m" # Bright Green (#1AD69C)
"\033[38;2;0;255;255m" # Bright Cyan (#2BC4E2)
"\033[0m" # Reset
)
foreground=${colors[0]} red=${colors[1]} green=${colors[2]} blue=${colors[3]} brightBlue=${colors[4]} brightWhite=${colors[5]} cyan=${colors[6]} brightYellow=${colors[7]} purple=${colors[8]} brightRed=${colors[9]} brightGreen=${colors[10]} brightCyan=${colors[11]} reset=${colors[12]}
# Helper functions
print() { echo -e "${cyan}$1${reset}"; }
error() { echo -e "${red}✗ $1${reset}"; }
success() { echo -e "${green}✓ $1${reset}"; }
log() { echo -e "${blue}! $1${reset}"; }
input() { read -p "$(echo -e "${brightYellow}▶ $1${reset}")" "$2"; }
confirm() { read -p "$(echo -e "\n${purple}Press any key to continue...${reset}")"; }
check_success() {
if [ $? -eq 0 ]; then
success "$1"
else
error "$2"
exit 1
fi
}
# Function to display the main menu
show_menu() {
log "SQLite Web Service Manager"
print ""
print "1) Install Service"
print "2) Manage Service"
print "3) Uninstall Service"
print "0) Exit"
print ""
input "Please choose an option: " choice
}
# Function to display the management submenu
show_manage_menu() {
clear
log "Manage SQLite-Web Service"
print ""
print "0) Show Full Log"
print "1) Stop Service"
print "2) Restart Service"
print "3) Change URL Path"
print "4) Change Port"
print "5) Change Password"
print "6) Change SQLite File Path"
print "7) Change SSL Certificate"
print "8) Back to Main Menu"
input "Please choose an option: " manage_choice
}
# Function to install the service
install_service() {
log "Updating and upgrading the system...(Please wait)"
sudo apt update -y > /dev/null 2>&1
sudo apt upgrade -y > /dev/null 2>&1
check_success "System updated" "Failed to update system"
log "Installing required packages...(Please wait)"
sudo apt install -y sqlite3 python3 python3-pip > /dev/null 2>&1
pip install sqlite-web > /dev/null 2>&1
check_success "Required packages installed" "Failed to install required packages"
DEFAULT_SQLITE_FILE="/var/lib/marzban/db.sqlite3"
while true; do
input "Please enter the path to the SQLite file (e.g., $DEFAULT_SQLITE_FILE): " SQLITE_FILE
if [$SQLITE_FILE == ""]; then
SQLITE_FILE=$DEFAULT_SQLITE_FILE
success "Using default SQLite file: $SQLITE_FILE"
fi
if [ -f "$SQLITE_FILE" ]; then
break
else
error "The specified SQLite file does not exist. Please try again."
fi
done
backup_sqlite_file "$SQLITE_FILE"
#Input Port check it is not already in use and is a valid port number
DEFAULT_PORT=8010;
while true; do
input "Please enter the port for the web interface (e.g., $DEFAULT_PORT): " PORT
if [$PORT == ""]; then
PORT=$DEFAULT_PORT
success "Using default port: $PORT"
fi
if ! [[ $PORT =~ ^[0-9]+$ ]] || [ $PORT -lt 1 ] || [ $PORT -gt 65535 ]; then
error "Invalid port number. Please try again."
elif lsof -i :$PORT | grep -q LISTEN; then
error "Port $PORT is already in use. Please try again."
else
break
fi
done
while true; do
input "Please enter the Password for accessing the web interface: " PASSWORD
if [ "$PASSWORD" == "" ]; then
error "Password cannot be empty. Please try again."
continue
fi
break
done
input "Do you want to create a random URL path? (y/n): " random_path_choice
if [ "$random_path_choice" == "y" ]; then
URL_PATH=$(openssl rand -hex 12)
else
input "Please enter a custom URL path (e.g., /sqlite-web): " URL_PATH
fi
while true; do
input "Do you want to enable SSL (HTTPS)? (y / n): " ssl_choice
if [ -z "$ssl_choice" ] || [ "$ssl_choice" = "y" ]; then
input "Please enter the domain name for the SSL certificate (e.g., example.com): " domain
input "Please enter the path to the SSL certificate (e.g /etc/ssl/certs/example.crt): " SSL_CERT
input "Please enter the path to the SSL private key (e.g /etc/ssl/private/example.key): " SSL_KEY
if [ ! -f "$SSL_CERT" ] || [ ! -f "$SSL_KEY" ]; then
error "The SSL certificate or key file does not exist at the specified paths."
continue
fi
SSL_OPTIONS="-c $SSL_CERT -k $SSL_KEY"
break
else
SSL_OPTIONS=""
break
fi
done
USER=$(whoami)
SERVICE_FILE="/etc/systemd/system/sqlite-web.service"
log "Creating systemd service at $SERVICE_FILE..."
unset SQLITE_WEB_PASSWORD
sudo bash -c "cat > $SERVICE_FILE" <<EOL
[Unit]
Description=sqlite-web service
After=network.target
[Service]
Environment="SQLITE_WEB_PASSWORD=$PASSWORD"
ExecStart=/usr/local/bin/sqlite_web -H 0.0.0.0 -p $PORT -u /$URL_PATH -P $SSL_OPTIONS $SQLITE_FILE
Restart=always
User=$USER
WorkingDirectory=$(dirname $SQLITE_FILE)
[Install]
WantedBy=multi-user.target
EOL
sudo systemctl daemon-reload
sudo systemctl enable sqlite-web
sudo systemctl start sqlite-web
if [ $? -eq 0 ]; then
success "The sqlite-web service has been created and started."
if [ "$ssl_choice" == "y" ]; then
success "To access the service over HTTPS, use the following URL: https://$domain:$PORT/$URL_PATH"
else
success "To access the service over HTTP, use the following URL: http://$(curl -4 -s ifconfig.me):$PORT/$URL_PATH"
fi
print ""
success " Your password is: $PASSWORD [Please don't forget it & keep it safe]"
confirm "Press any key to continue..."
else
error "Failed to start the sqlite-web service."
fi
}
# Function to manage the service
manage_service() {
if [ ! -f "/etc/systemd/system/sqlite-web.service" ]; then
error "The sqlite-web service is not installed. Please install the service first."
confirm "Press any key to continue..."
break
else
while true; do
show_manage_menu
case $manage_choice in
0) sudo journalctl -u sqlite-web --no-pager ;;
1) sudo systemctl stop sqlite-web; success "Service stopped." ;;
2) sudo systemctl restart sqlite-web; success "Service restarted." ;;
3) change_url_path ;;
4) change_port ;;
5) change_password ;;
6) change_sqlite_path ;;
7) change_ssl_cert ;;
8) break;;
*) error "Invalid option. Please try again." ;;
esac
confirm "Press any key to continue..."
done
fi
}
# Function to change the service port
change_port() {
input "Please enter the new port: " PORT
sudo sed -i "s/-p [0-9]*/-p $PORT/" /etc/systemd/system/sqlite-web.service
sudo systemctl daemon-reload > /dev/null 2>&1
sudo systemctl restart sqlite-web > /dev/null 2>&1
check_success "Port changed to $PORT and service restarted." "Failed to change port."
}
# Function to change the service password
change_password() {
input "Please enter the new password: " PASSWORD
sudo sed -i "s/Environment=\"SQLITE_WEB_PASSWORD=.*/Environment=\"SQLITE_WEB_PASSWORD=$PASSWORD\"/" /etc/systemd/system/sqlite-web.service
sudo systemctl daemon-reload
sudo systemctl restart sqlite-web
check_success "Password changed and service restarted." "Failed to change password."
}
# Function to change the SQLite file path
change_sqlite_path() {
input "Please enter the new path to the SQLite file: " SQLITE_FILE
sudo sed -i "s| -P .*| -P $SQLITE_FILE|" /etc/systemd/system/sqlite-web.service
sudo systemctl daemon-reload
sudo systemctl restart sqlite-web
check_success "SQLite file path changed to $SQLITE_FILE and service restarted." "Failed to change SQLite file path."
}
# Function to change the URL path
change_url_path() {
input "Please enter the new URL path: " URL_PATH
sudo sed -i "s| -u .*| -u /$URL_PATH|" /etc/systemd/system/sqlite-web.service
sudo systemctl daemon-reload
sudo systemctl restart sqlite-web
check_success "URL path changed to /$URL_PATH and service restarted." "Failed to change URL path."
}
# Function to change the SSL certificate
change_ssl_cert() {
input "Please enter the path to the new SSL certificate (.crt file): " SSL_CERT
input "Please enter the path to the new SSL private key (.key file): " SSL_KEY
if [ ! -f "$SSL_CERT" ] || [ ! -f "$SSL_KEY" ]; then
error "The SSL certificate or key file does not exist at the specified paths."
return 1
fi
sudo sed -i "s|-c .* -k .*|-c $SSL_CERT -k $SSL_KEY|" /etc/systemd/system/sqlite-web.service
sudo systemctl daemon-reload
sudo systemctl restart sqlite-web
check_success "SSL certificate changed and service restarted." "Failed to change SSL certificate."
}
# Function to backup the SQLite file
backup_sqlite_file() {
local file_path=$1
local backup_path="${file_path}.$(date +%F_%T).bak"
cp "$file_path" "$backup_path"
check_success "Backup of SQLite file created at $backup_path" "Failed to create backup of SQLite file"
}
# Function to uninstall the service
uninstall_service() {
log "Uninstalling sqlite-web service..."
log "Stopping and disabling service..."
sudo systemctl stop sqlite-web
check_success "Service stopped." "Failed to stop service."
log "disabling service..."
sudo systemctl disable sqlite-web
check_success "Service disabled." "Failed to disable service."
log "removing service file..."
sudo rm -f /etc/systemd/system/sqlite-web.service
check_success "Service file removed." "Failed to remove service file."
log "reloading systemd..."
sudo systemctl daemon-reload
check_success "sqlite-web service uninstalled." "Failed to uninstall sqlite-web service."
confirm "Press any key to continue..."
}
clear
# Main script logic
while true; do
print ""
print " Welcome to the SQLite3-Web + MARZBAN setup script (v1.1)."
log " ______________________________________________________________"
print " "
success " @DailyDigtalSKills | GITHUB :@azavaxhuman"
print " "
show_menu
case $choice in
1) install_service ;;
2) manage_service ;;
3) uninstall_service ;;
0) exit 0 ;;
*) error "Invalid option. Please try again."
confirm "Press any key to continue..."
;;
esac
clear
done