-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy path1pctl
More file actions
424 lines (392 loc) · 12.8 KB
/
1pctl
File metadata and controls
424 lines (392 loc) · 12.8 KB
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
#!/bin/bash
action=$1
target=$2
args=$@
BASE_DIR=directory
ORIGINAL_PORT=port
ORIGINAL_VERSION=version
ORIGINAL_ENTRANCE=entrance
ORIGINAL_USERNAME=username
ORIGINAL_PASSWORD=password
LANGUAGE=en
PANEL_EDITION=cn
if [ -f "/usr/local/bin/lang/$LANGUAGE.sh" ]; then
source "/usr/local/bin/lang/$LANGUAGE.sh"
else
LANGUAGE=en
fi
check_root() {
if [[ $EUID -ne 0 ]]; then
echo "$TXT_RUN_AS_ROOT"
exit 1
fi
}
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
generate_border_line() {
local width="$1"
local char="${2:-=}"
local color="${3:-$NC}"
printf "${color}%${width}s${NC}\n" | tr ' ' "$char"
}
print_centered_title() {
local title="$1"
local total_width="${2:-50}"
local border_char="${3:-=}"
local color="${4:-$NC}"
local title_length=${#title}
local padding=$(( (total_width - title_length) / 2 ))
local border_line=$(generate_border_line "$total_width" "$border_char" "$color")
echo "$border_line"
printf "${color}%*s%s${NC}\n" $padding "" "$title"
echo "$border_line"
}
function usage() {
print_centered_title "$TXT_PANEL_CONTROL_SCRIPT" 60 "-" "$BLUE"
echo -e "${YELLOW}Usage:${NC}"
echo " ./1pctl [COMMAND] [ARGS...]"
echo " ./1pctl --help"
echo
echo -e "${YELLOW}Commands:${NC}"
echo -e " ${GREEN}status${NC} [core|agent] ${YELLOW}$TXT_PANEL_SERVICE_STATUS${NC}"
echo -e " ${GREEN}start${NC} [core|agent|all] ${YELLOW}$TXT_PANEL_SERVICE_START${NC}"
echo -e " ${GREEN}stop${NC} [core|agent|all] ${YELLOW}$TXT_PANEL_SERVICE_STOP${NC}"
echo -e " ${GREEN}restart${NC} [core|agent|all] ${YELLOW}$TXT_PANEL_SERVICE_RESTART${NC}"
echo -e " ${RED}uninstall${NC} ${YELLOW}$TXT_PANEL_SERVICE_UNINSTALL${NC}"
echo -e " ${GREEN}user-info${NC} ${YELLOW}$TXT_PANEL_SERVICE_USER_INFO${NC}"
echo -e " ${GREEN}listen-ip${NC} ${YELLOW}$TXT_PANEL_SERVICE_LISTEN_IP${NC}"
echo -e " ${GREEN}version${NC} ${YELLOW}$TXT_PANEL_SERVICE_VERSION${NC}"
echo -e " ${GREEN}update${NC} ${YELLOW}$TXT_PANEL_SERVICE_UPDATE${NC}"
echo -e " ${RED}reset${NC} ${YELLOW}$TXT_PANEL_SERVICE_RESET${NC}"
echo -e " ${RED}restore${NC} ${YELLOW}$TXT_PANEL_SERVICE_RESTORE${NC}"
generate_border_line 60 "-" "$BLUE"
}
_service_manager() {
local service_name=$1
if command -v systemctl &>/dev/null; then
service_manager="systemctl"
service_target="${service_name}.service"
elif command -v rc-service &>/dev/null; then
service_manager="openrc"
service_target="${service_name}"
else
service_manager="sysvinit"
service_target="${service_name}"
fi
}
_service_cmd() {
local service_name=$1
local cmd=$2
local success_msg=$3
local error_msg=$4
_service_manager $service_name
case $service_manager in
systemctl)
if systemctl "$cmd" "$service_target" >/dev/null 2>&1; then echo "$success_msg"; else echo "$error_msg"; fi
;;
openrc)
if rc-service "$service_target" "$cmd" >/dev/null 2>&1; then echo "$success_msg"; else echo "$error_msg"; fi
;;
*)
if service "$service_target" "$cmd" >/dev/null 2>&1; then echo "$success_msg"; else echo "$error_msg"; fi
;;
esac
}
function require_core_agent_or_all() {
echo -e "$TXT_PANEL_SERVICE_REQUIRE_CORE_AGENT_OR_ALL"
exit 1
}
function status() {
print_centered_title "$TXT_PANEL_SERVICE_STATUS" 60 "-" "$BLUE"
if [[ -z "$target" ]] || [[ "$target" == "all" ]]; then
for svc in core agent; do
_service_cmd 1panel-$svc "status" \
"🟢[OK]$TXT_PANEL_GEN_SERVICE_NAME${svc^}: $TXT_PANEL_GEN_STATUS_RUNNING" \
"🔴[FAIL]$TXT_PANEL_GEN_SERVICE_NAME${svc^}: $TXT_PANEL_GEN_STATUS_STOPPED"
done
else
case "$target" in
core|agent)
_service_cmd 1panel-$target "status" \
"🟢[OK]$TXT_PANEL_GEN_SERVICE_NAME${target^}: $TXT_PANEL_GEN_STATUS_RUNNING" \
"🔴[FAIL]$TXT_PANEL_GEN_SERVICE_NAME${target^}: $TXT_PANEL_GEN_STATUS_STOPPED"
;;
*)
require_core_agent_or_all
;;
esac
fi
generate_border_line 60 "-" "$BLUE"
}
function start() {
print_centered_title "$TXT_PANEL_SERVICE_START" 60 "-" "$BLUE"
if [[ -z "$target" ]] || [[ "$target" == "all" ]]; then
for svc in core agent; do
_service_cmd 1panel-$svc "start" \
"🟢[OK]$TXT_PANEL_GEN_START $TXT_PANEL_GEN_SERVICE_NAME${svc^}: $TXT_SUCCESSFULLY_MESSAGE" \
"🔴[FAIL]$TXT_PANEL_GEN_START $TXT_PANEL_GEN_SERVICE_NAME${svc^}: $TXT_FAIELD_MESSAGE"
done
else
case "$target" in
core|agent)
_service_cmd 1panel-$target "start" \
"🟢[OK]$TXT_PANEL_GEN_START $TXT_PANEL_GEN_SERVICE_NAME${target^}: $TXT_SUCCESSFULLY_MESSAGE" \
"🔴[FAIL]$TXT_PANEL_GEN_START $TXT_PANEL_GEN_SERVICE_NAME${target^}: $TXT_FAIELD_MESSAGE"
;;
*)
require_core_agent_or_all
;;
esac
fi
generate_border_line 60 "-" "$BLUE"
}
function stop() {
print_centered_title "$TXT_PANEL_SERVICE_STOP" 60 "=" "$RED"
check_root
if [[ -z "$target" ]] || [[ "$target" == "all" ]]; then
for svc in core agent; do
_service_cmd 1panel-$svc "stop" \
"🟢[OK]$TXT_PANEL_GEN_STOP $TXT_PANEL_GEN_SERVICE_NAME${svc^}: $TXT_SUCCESSFULLY_MESSAGE" \
"🔴[FAIL]$TXT_PANEL_GEN_STOP $TXT_PANEL_GEN_SERVICE_NAME${svc^}: $TXT_FAIELD_MESSAGE"
done
else
_service_cmd 1panel-$target "stop" \
"🟢[OK]$TXT_PANEL_GEN_STOP $TXT_PANEL_GEN_SERVICE_NAME${target^}: $TXT_SUCCESSFULLY_MESSAGE" \
"🔴[FAIL]$TXT_PANEL_GEN_STOP $TXT_PANEL_GEN_SERVICE_NAME${target^}: $TXT_FAIELD_MESSAGE"
fi
generate_border_line 60 "-" "$RED"
}
function restart() {
print_centered_title "$TXT_PANEL_SERVICE_RESTART" 60 "+" "$YELLOW"
check_root
if [[ -z "$target" ]] || [[ "$target" == "all" ]]; then
for svc in core agent; do
_service_cmd 1panel-$svc "restart" \
"🟢[OK]$TXT_PANEL_GEN_RESTART $TXT_PANEL_GEN_SERVICE_NAME${svc^}: $TXT_SUCCESSFULLY_MESSAGE" \
"🔴[FAIL]$TXT_PANEL_GEN_RESTART $TXT_PANEL_GEN_SERVICE_NAME${svc^}: $TXT_FAIELD_MESSAGE"
done
else
_service_cmd 1panel-$target "restart" \
"🟢[OK]$TXT_PANEL_GEN_RESTART 1Panel-$target: $TXT_SUCCESSFULLY_MESSAGE" \
"🔴[FAIL]$TXT_PANEL_GEN_RESTART 1Panel-$target: $TXT_FAIELD_MESSAGE"
fi
generate_border_line 60 "-" "$YELLOW"
}
function uninstall() {
print_centered_title "$TXT_PANEL_SERVICE_UNINSTALL" 60 "=" "$RED"
check_root
while true; do
read -p "$TXT_PANEL_SERVICE_UNINSTALL_NOTICE" yn
case $yn in
[Yy] )
echo -e "$TXT_PANEL_SERVICE_UNINSTALL_START"
_service_manager
case $service_manager in
systemctl)
systemctl stop 1panel-core.service
systemctl stop 1panel-agent.service
systemctl disable 1panel-core.service >/dev/null 2>&1
systemctl disable 1panel-agent.service >/dev/null 2>&1
rm -rf /etc/systemd/system/{1panel-core.service,1panel-agent.service}
echo -e "$TXT_PANEL_SERVICE_UNINSTALL_REMOVE_CONFIG"
systemctl daemon-reload
systemctl reset-failed
;;
openrc)
rc-service 1panel-core stop
rc-service 1panel-agent stop
rc-update del 1panel-core >/dev/null 2>&1
rc-update del 1panel-agent >/dev/null 2>&1
rm -rf /etc/init.d/{1panel-core,1panel-agent}
;;
*)
service 1panel-core stop
service 1panel-agent stop
rm -rf /etc/init.d/{1panel-core,1panel-agent}
;;
esac
rm -rf /usr/local/bin/{1pctl,1panel-core,1panel-agent,lang} \
/usr/bin/{1pctl,1panel,1panel-core,1panel-agent} \
/etc/1panel
while true; do
read -p "$TXT_PANEL_DATA_KEEP_PROMPT" keep_data
case $keep_data in
[Yy] )
echo "$TXT_PANEL_DATA_DELETE"
rm -rf $BASE_DIR/1panel
break
;;
[Nn] )
echo "$TXT_PANEL_DATA_KEEP"
break
;;
* )
echo "$TXT_INVALID_YN_INPUT"
;;
esac
done
echo -e "$TXT_PANEL_SERVICE_UNINSTALL_REMOVE_SUCCESS"
break
;;
[Nn] )
exit 0
;;
* )
echo "$TXT_INVALID_YN_INPUT"
;;
esac
done
generate_border_line 60 "-" "$NC"
}
function user-info() {
print_centered_title "$TXT_PANEL_SERVICE_USER_INFO" 60 "=" "$BLUE"
1panel -l $LANGUAGE user-info
generate_border_line 60 "-" "$BLUE"
}
function listen-ip() {
print_centered_title "$TXT_PANEL_SERVICE_LISTEN_IP" 60 "=" "$BLUE"
case "${target}" in
ipv4)
1panel -l $LANGUAGE listen-ip ipv4
target=all
restart
;;
ipv6)
1panel -l $LANGUAGE listen-ip ipv6
target=all
restart
;;
*)
1panel -l $LANGUAGE listen-ip
;;
esac
generate_border_line 60 "-" "$BLUE"
}
function restore() {
print_centered_title "$TXT_PANEL_SERVICE_RESTORE" 60 "=" "$YELLOW"
check_root
while true; do
read -p "$TXT_PANEL_SERVICE_RESTORE_NOTICE" yn
case "$yn" in
[Yy])
1panel -l $LANGUAGE restore
read -t 0.1 _ || true
break
;;
[Nn])
exit 0
;;
*)
echo -e "$TXT_INVALID_YN_INPUT"
;;
esac
done
generate_border_line 60 "-" "$YELLOW"
}
function version() {
print_centered_title "$TXT_PANEL_SERVICE_VERSION" 60 "-" "$BLUE"
1panel -l $LANGUAGE version
generate_border_line 60 "-" "$BLUE"
}
function reset() {
print_centered_title "$TXT_PANEL_SERVICE_RESET" 60 "=" "$YELLOW"
check_root
case "${target}" in
domain)
1panel -l $LANGUAGE reset domain
;;
entrance)
1panel -l $LANGUAGE reset entrance
;;
https)
1panel -l $LANGUAGE reset https
target=all
restart
;;
ips)
1panel -l $LANGUAGE reset ips
;;
mfa)
1panel -l $LANGUAGE reset mfa
;;
passkey)
1panel -l $LANGUAGE reset passkey
;;
*)
1panel -l $LANGUAGE reset
;;
esac
generate_border_line 60 "-" "$YELLOW"
}
function update() {
print_centered_title "$TXT_PANEL_SERVICE_UPDATE" 60 "=" "$YELLOW"
check_root
case "${target}" in
username)
1panel -l $LANGUAGE update username
;;
password)
1panel -l $LANGUAGE update password
;;
port)
1panel -l $LANGUAGE update port
;;
*)
1panel -l $LANGUAGE update
;;
esac
generate_border_line 60 "-" "$YELLOW"
}
function main() {
case "${action}" in
status)
status
;;
start)
start
;;
stop)
stop
;;
restart)
restart
;;
restore)
restore
;;
uninstall)
uninstall
;;
user-info)
user-info
;;
listen-ip)
listen-ip
;;
version)
version
;;
reset)
reset
;;
update)
update
;;
help)
usage
;;
--help)
usage
;;
"")
usage
;;
*)
echo "$TXT_PANEL_SERVICE_UNSUPPORTED_PARAMETER"
;;
esac
}
main