-
Notifications
You must be signed in to change notification settings - Fork 946
/
utils.sh
543 lines (463 loc) · 13.2 KB
/
utils.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#!/bin/bash
RED='\033[0;91m'
GREEN='\033[0;92m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;96m'
WHITE='\033[0;37m'
RESET='\033[0m'
yellow() {
echo -e "${YELLOW}$1${RESET}"
}
green() {
echo -e "${GREEN}$1${RESET}"
}
red() {
echo -e "${RED}$1${RESET}"
}
installpath="$HOME"
checknezhaAgentAlive() {
if ps aux | grep nezha-agent | grep -v "grep" >/dev/null; then
return 0
else
return 1
fi
}
checkvmessAlive() {
local c=0
if ps aux | grep serv00sb | grep -v "grep" >/dev/null; then
((c++))
fi
if ps aux | grep cloudflared | grep -v "grep" >/dev/null; then
((c++))
fi
if [ $c -eq 2 ]; then
return 0
fi
return 1 # 有一个或多个进程不在运行
}
#返回0表示成功, 1表示失败
#在if条件中,0会执行,1不会执行
checkProcAlive() {
local procname=$1
if ps aux | grep "$procname" | grep -v "grep" >/dev/null; then
return 0
else
return 1
fi
}
stopProc() {
local procname=$1
r=$(ps aux | grep "$procname" | grep -v "grep" | awk '{print $2}')
if [ -z "$r" ]; then
return 0
else
kill -9 $r
fi
echo "已停掉$procname!"
}
checkSingboxAlive() {
local c=0
if ps aux | grep serv00sb | grep -v "grep" >/dev/null; then
((c++))
fi
if ps aux | grep cloudflare | grep -v "grep" >/dev/null; then
((c++))
fi
if [ $c -eq 2 ]; then
return 0
fi
return 1 # 有一个或多个进程不在运行
}
checkMtgAlive() {
if ps aux | grep mtg | grep -v "grep" >/dev/null; then
return 0
else
return 1
fi
}
stopNeZhaAgent() {
r=$(ps aux | grep nezha-agent | grep -v "grep" | awk '{print $2}')
if [ -z "$r" ]; then
return 0
else
kill -9 $r
fi
echo "已停掉nezha-agent!"
}
writeWX() {
has_fd=$(echo "$config_content" | jq 'has("wxsendkey")')
if [ "$has_fd" == "true" ]; then
wx_sendkey=$(echo "$config_content" | jq -r ".wxsendkey")
read -p "已有 WXSENDKEY ($wx_sendkey), 是否修改? [y/n] [n]:" input
input=${input:-n}
if [ "$input" == "y" ]; then
read -p "请输入 WXSENDKEY:" wx_sendkey
fi
json_content+=" \"wxsendkey\": \"${wx_sendkey}\", \n"
else
read -p "请输入 WXSENDKEY:" wx_sendkey
json_content+=" \"wxsendkey\": \"${wx_sendkey}\", \n"
fi
}
writeTG() {
has_fd=$(echo "$config_content" | jq 'has("telegram_token")')
if [ "$has_fd" == "true" ]; then
tg_token=$(echo "$config_content" | jq -r ".telegram_token")
read -p "已有 TELEGRAM_TOKEN ($tg_token), 是否修改? [y/n] [n]:" input
input=${input:-n}
if [ "$input" == "y" ]; then
read -p "请输入 TELEGRAM_TOKEN:" tg_token
fi
json_content+=" \"telegram_token\": \"${tg_token}\", \n"
else
read -p "请输入 TELEGRAM_TOKEN:" tg_token
json_content+=" \"telegram_token\": \"${tg_token}\", \n"
fi
has_fd=$(echo "$config_content" | jq 'has("telegram_userid")')
if [ "$has_fd" == "true" ]; then
tg_userid=$(echo "$config_content" | jq -r ".telegram_userid")
read -p "已有 TELEGRAM_USERID ($tg_userid), 是否修改? [y/n] [n]:" input
input=${input:-n}
if [ "$input" == "y" ]; then
read -p "请输入 TELEGRAM_USERID:" tg_userid
fi
json_content+=" \"telegram_userid\": \"${tg_userid}\", \n"
else
read -p "请输入 TELEGRAM_USERID:" tg_userid
json_content+=" \"telegram_userid\": \"${tg_userid}\",\n"
fi
}
cleanCron() {
echo "" >null
crontab null
rm null
}
delCron() {
crontab -l | grep -v "keepalive" >mycron
crontab mycron >/dev/null 2>&1
rm mycron
}
addCron() {
local tm=$1
crontab -l | grep -v "keepalive" >mycron
echo "*/$tm * * * * bash ${installpath}/serv00-play/keepalive.sh > /dev/null 2>&1 " >>mycron
crontab mycron >/dev/null 2>&1
rm mycron
}
get_webip() {
# 获取主机名称,例如:s2.serv00.com
local hostname=$(hostname)
# 提取主机名称中的数字,例如:2
local host_number=$(echo "$hostname" | awk -F'[s.]' '{print $2}')
# 构造主机名称的数组
local hosts=("web${host_number}.serv00.com" "cache${host_number}.serv00.com")
# 初始化最终 IP 变量
local final_ip=""
# 遍历主机名称数组
for host in "${hosts[@]}"; do
# 获取 API 返回的数据
local response=$(curl -s "https://ss.botai.us.kg/api/getip?host=$host")
# 检查返回的结果是否包含 "not found"
if [[ "$response" =~ "not found" ]]; then
continue
fi
# 提取第一个字段作为 IP,并检查第二个字段是否为 "Accessible"
local ip=$(echo "$response" | awk -F "|" '{ if ($2 == "Accessible") print $1 }')
# webxx.serv00.com域名对应的ip作为兜底ip
if [[ "$host" == "web${host_number}.serv00.com" ]]; then
final_ip=$(echo "$response" | awk -F "|" '{print $1}')
fi
# 如果找到了 "Accessible",返回 IP
if [[ -n "$ip" ]]; then
echo "$ip"
return
fi
done
echo "$final_ip"
}
get_ip() {
# 获取主机名称,例如:s2.serv00.com
local hostname=$(hostname)
# 提取主机名称中的数字,例如:2
local host_number=$(echo "$hostname" | awk -F'[s.]' '{print $2}')
# 构造主机名称的数组
local hosts=("cache${host_number}.serv00.com" "web${host_number}.serv00.com" "$hostname")
# 初始化最终 IP 变量
local final_ip=""
# 遍历主机名称数组
for host in "${hosts[@]}"; do
# 获取 API 返回的数据
local response=$(curl -s "https://ss.botai.us.kg/api/getip?host=$host")
# 检查返回的结果是否包含 "not found"
if [[ "$response" =~ "not found" ]]; then
continue
fi
# 提取第一个字段作为 IP,并检查第二个字段是否为 "Accessible"
local ip=$(echo "$response" | awk -F "|" '{ if ($2 == "Accessible") print $1 }')
# 如果找到了 "Accessible",返回 IP
if [[ -n "$ip" ]]; then
echo "$ip"
return
fi
final_ip=$ip
done
echo "$final_ip"
}
isServ00() {
[[ $(hostname) == *"serv00"* ]]
}
#获取端口
getPort() {
local type=$1
local opts=$2
local key="$type|$opts"
#echo "key: $key"
#port list中查找,如果没有随机分配一个
if [[ -n "${port_array["$key"]}" ]]; then
#echo "找到list中的port"
echo "${port_array["$key"]}"
else
# echo "devil port add $type random $opts"
rt=$(devil port add $type random $opts)
if [[ "$rt" =~ .*succesfully.*$ || "$rt" =~ .*Ok.*$ ]]; then
loadPort
if [[ -n "$port_array["$key"]" ]]; then
echo "${port_array["$key"]}"
else
echo "failed"
fi
else
echo "failed"
fi
fi
}
randomPort() {
local type=$1
local opts=$2
port=""
#echo "type:$type, opts:$opts"
read -p "是否自动分配${opts}端口($type)?[y/n] [y]:" input
input=${input:-y}
if [[ "$input" == "y" ]]; then
port=$(getPort $type $opts)
if [[ "$port" == "failed" ]]; then
read -p "自动分配端口失败,请手动输入${opts}端口:" port
else
green "自动分配${opts}端口为:${port}"
fi
else
read -p "请输入${opts}端口($type):" port
fi
}
declare -A port_array
#检查是否可以自动分配端口
loadPort() {
output=$(devil port list)
port_array=()
# 解析输出内容
index=0
while read -r port typ opis; do
# 跳过标题行
if [[ "$port" =~ "Port" ]]; then
continue
fi
#echo "port:$port,typ:$typ, opis:$opis"
if [[ "$port" =~ "Brak" || "$port" == "No" ]]; then
echo "未分配端口"
return 0
fi
# 将 Typ 和 Opis 合并并存储到数组中
if [[ -n "$typ" ]]; then
# 如果 Opis 为空则用空字符串代替
opis=${opis:-""}
combined="${typ}|${opis}"
port_array["$combined"]="$port"
# echo "port_array 读入 key=$combined, value=$port"
((index++))
fi
done <<<"$output"
return 0
}
cleanPort() {
output=$(devil port list)
while read -r port typ opis; do
# 跳过标题行
if [[ "$typ" == "Type" ]]; then
continue
fi
if [[ "$port" == "Brak" || "$port" == "No" ]]; then
return 0
fi
if [[ -n "$typ" ]]; then
devil port del $typ $port >/dev/null 2>&1
fi
done <<<"$output"
return 0
}
checkDownload() {
local file=$1
local filegz="$file.gz"
local is_dir=${2:-0}
if [[ $is_dir -eq 1 ]]; then
filegz="$file.tar.gz"
fi
#检查并下载核心程序
if [[ ! -e $file ]] || [[ $(file $file) == *"text"* ]]; then
echo "正在下载 $file..."
url="https://gfg.fkj.pp.ua/app/serv00/$filegz?pwd=fkjyyds666"
curl -L -sS --max-time 20 -o $filegz "$url"
if file $filegz | grep -q "text"; then
echo "无法正确下载!!!"
rm -f $filegz
return 1
fi
if [ -e $filegz ]; then
if [[ $is_dir -eq 1 ]]; then
tar -zxf $filegz
else
gzip -d $filegz
fi
else
echo "下载失败,可能是网络问题."
return 1
fi
#下载失败
if [[ $is_dir -eq 0 && ! -e $file ]]; then
echo "无法下载核心程序,可能网络问题,请检查!"
return 1
fi
# 设置可执行权限
if [[ $is_dir -eq 0 ]]; then
chmod +x "$file"
fi
echo "下载完毕!"
fi
return 0
}
# 对json文件字段进行插入或修改
# usage: upInsertFd jsonfile fieldname value
upInsertFd() {
local jsonfile=$1
local field=$2
local value=$3
jq --arg field "$field" --arg value "$value" '
if has($field) then
.[$field] = $value
else
. + {($field): $value}
end
' "$jsonfile" >tmp.json && mv tmp.json "$jsonfile"
return $?
}
# 针对singbox.json, 对指定字段进行修改
upSingboxFd() {
local jsonfile=$1
local array_name=$2
local selector_key=$3
local selector_value=$4
local field_path=$5
local value=$6
jq --arg selector_key "$selector_key" \
--arg selector_value "$selector_value" \
--arg field_path "$field_path" \
--arg value "$value" "
(.$array_name[] | select(.$selector_key == \$selector_value) | .[\$field_path]) = \$value
" "$jsonfile" >tmp.json && mv tmp.json "$jsonfile"
return $?
}
# php默认配置文件操作
PHPCONFIG_FILE="phpconfig.json"
# 判断JSON文件是否存在,若不存在则创建并初始化
initialize_json() {
if [ ! -f "$PHPCONFIG_FILE" ]; then
echo '{"domains": []}' >"$PHPCONFIG_FILE"
fi
}
# 添加新域名
add_domain() {
local new_domain="$1"
local webip="$2"
# 初始化JSON文件(如果不存在的话)
initialize_json
# 读取当前的JSON配置文件并检查域名是否已存在
if grep -q "\"$new_domain\"" "$PHPCONFIG_FILE"; then
echo "域名 '$new_domain' 已存在!"
return 1
fi
# 使用jq来处理JSON,添加新的域名到domains数组
#jq --arg domain "$new_domain" '.domains += [$domain]' "$PHPCONFIG_FILE" >temp.json && mv temp.json "$PHPCONFIG_FILE"
jq ".domains += [{\"domain\": \"$domain\", \"webip\": \"$webip\"}]" "$PHPCONFIG_FILE" >"$PHPCONFIG_FILE.tmp" && mv "$PHPCONFIG_FILE.tmp" "$PHPCONFIG_FILE"
#echo "域名 '$new_domain' 添加成功!"
return 0
}
# 删除域名
delete_domain() {
local domain_to_delete="$1"
# 初始化JSON文件(如果不存在的话)
initialize_json
# 读取当前的JSON配置文件并检查域名是否存在
if ! grep -q "\"$domain_to_delete\"" "$PHPCONFIG_FILE"; then
echo "域名 '$domain_to_delete' 不存在!"
return 1
fi
local webip=$(jq -r ".domains[] | select(.domain == \"$domain\") | .webip" "$PHPCONFIG_FILE")
# 使用jq来处理JSON,删除指定的域名
jq "del(.domains[] | select(.domain == \"$domain\"))" "$PHPCONFIG_FILE" >"$PHPCONFIG_FILE.tmp" && mv "$PHPCONFIG_FILE.tmp" "$PHPCONFIG_FILE"
local domainPath="$installpath/domains/$domain"
echo "正在删除域名相关服务,请等待..."
rm -rf "$domainPath"
resp=$(devil ssl www del $webip $domain)
resp=$(devil www del $domain --remove)
echo "已卸载域名[$domain_to_delete]相关服务!"
return 0
}
# 判断domains数组是否为空
check_domains_empty() {
initialize_json
local domains_count=$(jq '.domains | length' "$PHPCONFIG_FILE")
if [ "$domains_count" -eq 0 ]; then
return 0
else
return 1
fi
}
print_domains() {
yellow "----------------------------"
green "域名\t\t|\t服务IP"
yellow "----------------------------"
# 使用jq格式化输出
jq -r '.domains[] | "\(.domain)\t|\(.webip)"' "$PHPCONFIG_FILE"
}
delete_all_domains() {
initialize_json
jq -r '.domains[] | "\(.domain)\t\(.webip)"' "$PHPCONFIG_FILE" | while read -r domain webip; do
echo "域名: $domain, 服务IP: $webip"
delete_domain "$domain"
done
}
download_from_github_release() {
local user=$1
local repository=$2
local package=$3
local zippackage="$package.zip"
local url="https://github.com/${user}/${repository}"
local latestUrl="$url/releases/latest"
local latest_version=$(curl -sL $latestUrl | sed -n 's/.*tag\/\(v[0-9.]*\).*/\1/p' | head -1)
local download_url="${url}/releases/download/$latest_version/$zippackage"
curl -sL -o "$zippackage" "$download_url"
if [[ ! -e "$zippackage" || -n $(file "$zippackage" | grep "text") ]]; then
echo "下载 $zippackage 文件失败!"
return 1
fi
# 原地解压缩
unzip -o "$zippackage" -d .
if [[ $? -ne 0 ]]; then
echo "解压 $zippackage 文件失败!"
return 1
fi
rm -rf "$zippackage"
echo "下载并解压 $zippackage 成功!"
return 0
}