优化 web_save 逻辑,确保成功响应再保存

This commit is contained in:
Feng Yu
2026-03-25 21:16:20 +08:00
parent 837f99727a
commit 8d51f0aab6
2 changed files with 80 additions and 66 deletions

View File

@@ -1,32 +1,42 @@
get_save() { #获取面板信息并内部处理所有异常
local response exit_code
if curl --version >/dev/null 2>&1; then
response=$(curl -sf -H "Authorization: Bearer ${secret}" -H "Content-Type:application/json" "$1" 2>&1)
exit_code=$?
[ $exit_code -eq 0 ] && [ -n "$response" ] && [ "$response" != "{}" ] && {
echo "$response"
return 0
}
return 1
elif [ -n "$(wget --help 2>&1 | grep '\-\-method')" ]; then
response=$(wget -q --header="Authorization: Bearer ${secret}" --header="Content-Type:application/json" -O - "$1" 2>&1)
exit_code=$?
[ $exit_code -eq 0 ] && [ -n "$response" ] && [ "$response" != "{}" ] && {
echo "$response"
return 0
}
return 1
fi
return 1
}
#
get_save() { #获取面板信息
if curl --version >/dev/null 2>&1; then
curl -s -H "Authorization: Bearer ${secret}" -H "Content-Type:application/json" "$1"
elif [ -n "$(wget --help 2>&1 | grep '\-\-method')" ]; then
wget -q --header="Authorization: Bearer ${secret}" --header="Content-Type:application/json" -O - "$1"
fi
}
web_save() { #最小化保存面板节点选择 web_save() { #最小化保存面板节点选择
#使用get_save获取面板节点设置 #使用get_save获取面板节点设置,失败自动退出
get_save "http://127.0.0.1:${db_port}/proxies" | sed 's/{}//g' | sed 's/:{/\ response=$(get_save "http://127.0.0.1:${db_port}/proxies") || return 1
/g'| grep -aE '"Selector"' >"$TMPDIR"/web_proxies
[ -s "$TMPDIR"/web_proxies ] && while read line; do echo "$response" | sed 's/{}//g' | sed 's/:{/\
def=$(echo $line | grep -oE '"all".*",' | awk -F "[\"]" '{print $4}') /g' | grep -aE '"Selector"' >"$TMPDIR"/web_proxies
now=$(echo $line | grep -oE '"now".*",' | awk -F "[\"]" '{print $4}')
[ "$def" != "$now" ] && { >"$TMPDIR"/web_save
name=$(echo $line | grep -oE '"name".*",' | awk -F "[\"]" '{print $4}') [ -s "$TMPDIR"/web_proxies ] && while read line; do
echo "${name},${now}" >>"$TMPDIR"/web_save def=$(echo "$line" | grep -oE '"all".*",' | awk -F "[\"]" '{print $4}')
} now=$(echo "$line" | grep -oE '"now".*",' | awk -F "[\"]" '{print $4}')
done <"$TMPDIR"/web_proxies [ "$def" != "$now" ] && {
rm -rf "$TMPDIR"/web_proxies name=$(echo "$line" | grep -oE '"name".*",' | awk -F "[\"]" '{print $4}')
#对比文件,如果有变动则写入磁盘,否则清除缓存 echo "${name},${now}" >>"$TMPDIR"/web_save
for file in web_save; do }
if [ -s "$TMPDIR/$file" ]; then done <"$TMPDIR"/web_proxies
. "$CRASHDIR"/libs/compare.sh && compare "$TMPDIR/$file" "$CRASHDIR/configs/$file" rm -f "$TMPDIR"/web_proxies
[ "$?" = 0 ] && rm -f "$TMPDIR/$file" || mv -f "$TMPDIR/$file" "$CRASHDIR/configs/$file" . "$CRASHDIR"/libs/compare.sh && compare "$TMPDIR"/web_save "$CRASHDIR"/configs/web_save
else [ "$?" = 0 ] && rm -f "$TMPDIR"/web_save || mv -f "$TMPDIR"/web_save "$CRASHDIR"/configs/web_save
> "$CRASHDIR/configs/$file" #空文件时移除旧文件
fi
done
} }

View File

@@ -2,7 +2,11 @@
# Copyright (C) Juewuy # Copyright (C) Juewuy
#初始化目录 #初始化目录
[ -z "$CRASHDIR" ] && CRASHDIR=$( cd $(dirname $0);cd ..;pwd) [ -z "$CRASHDIR" ] && CRASHDIR=$(
cd $(dirname $0)
cd ..
pwd
)
. "$CRASHDIR"/libs/get_config.sh . "$CRASHDIR"/libs/get_config.sh
#加载工具 #加载工具
. "$CRASHDIR"/libs/check_cmd.sh . "$CRASHDIR"/libs/check_cmd.sh
@@ -12,45 +16,45 @@
[ -z "$firewall_area" ] && firewall_area=1 [ -z "$firewall_area" ] && firewall_area=1
#延迟启动 #延迟启动
[ ! -f "$TMPDIR"/crash_start_time ] && [ -n "$start_delay" ] && [ "$start_delay" -gt 0 ] && { [ ! -f "$TMPDIR"/crash_start_time ] && [ -n "$start_delay" ] && [ "$start_delay" -gt 0 ] && {
logger "ShellCrash将延迟$start_delay秒启动" 31 logger "ShellCrash将延迟$start_delay秒启动" 31
sleep "$start_delay" sleep "$start_delay"
} }
#设置循环检测面板端口以判定服务启动是否成功 #设置循环检测面板端口以判定服务启动是否成功
. "$CRASHDIR"/libs/start_wait.sh . "$CRASHDIR"/libs/start_wait.sh
if [ -n "$test" -o -n "$(pidof CrashCore)" ]; then if [ -n "$test" -o -n "$(pidof CrashCore)" ]; then
[ "$start_old" = "ON" ] && [ ! -L "$TMPDIR"/CrashCore ] && rm -f "$TMPDIR"/CrashCore #删除缓存目录内核文件 [ "$start_old" = "ON" ] && [ ! -L "$TMPDIR"/CrashCore ] && rm -f "$TMPDIR"/CrashCore #删除缓存目录内核文件
. "$CRASHDIR"/starts/fw_start.sh #配置防火墙流量劫持 . "$CRASHDIR"/starts/fw_start.sh #配置防火墙流量劫持
date +%s >"$TMPDIR"/crash_start_time #标记启动时间 date +%s >"$TMPDIR"/crash_start_time #标记启动时间
#后台还原面板配置 #还原面板配置
[ -s "$CRASHDIR"/configs/web_save ] && { [ -s "$CRASHDIR"/configs/web_save ] && {
. "$CRASHDIR"/libs/web_restore.sh . "$CRASHDIR"/libs/web_restore.sh
web_restore >/dev/null 2>&1 & web_restore >/dev/null 2>&1
} }
#推送日志 #推送日志
{ {
sleep 5 sleep 5
logger ShellCrash服务已启动 logger ShellCrash服务已启动
} & } &
ckcmd mtd_storage.sh && mtd_storage.sh save >/dev/null 2>&1 #Padavan保存/etc/storage ckcmd mtd_storage.sh && mtd_storage.sh save >/dev/null 2>&1 #Padavan保存/etc/storage
#加载定时任务 #加载定时任务
cronload | grep -v '^$' > "$TMPDIR"/cron_tmp cronload | grep -v '^$' >"$TMPDIR"/cron_tmp
[ -s "$CRASHDIR"/task/cron ] && cat "$CRASHDIR"/task/cron >> "$TMPDIR"/cron_tmp [ -s "$CRASHDIR"/task/cron ] && cat "$CRASHDIR"/task/cron >>"$TMPDIR"/cron_tmp
[ -s "$CRASHDIR"/task/running ] && cat "$CRASHDIR"/task/running >> "$TMPDIR"/cron_tmp [ -s "$CRASHDIR"/task/running ] && cat "$CRASHDIR"/task/running >>"$TMPDIR"/cron_tmp
[ "$bot_tg_service" = ON ] && echo "* * * * * /bin/sh $CRASHDIR/starts/start_legacy_wd.sh bot_tg #ShellCrash-TG_BOT守护进程" >> "$TMPDIR"/cron_tmp [ "$bot_tg_service" = ON ] && echo "* * * * * /bin/sh $CRASHDIR/starts/start_legacy_wd.sh bot_tg #ShellCrash-TG_BOT守护进程" >>"$TMPDIR"/cron_tmp
[ "$start_old" = ON ] && echo "* * * * * /bin/sh $CRASHDIR/starts/start_legacy_wd.sh shellcrash #ShellCrash保守模式守护进程" >> "$TMPDIR"/cron_tmp [ "$start_old" = ON ] && echo "* * * * * /bin/sh $CRASHDIR/starts/start_legacy_wd.sh shellcrash #ShellCrash保守模式守护进程" >>"$TMPDIR"/cron_tmp
awk '!x[$0]++' "$TMPDIR"/cron_tmp > "$TMPDIR"/cron_tmp2 #删除重复行 awk '!x[$0]++' "$TMPDIR"/cron_tmp >"$TMPDIR"/cron_tmp2 #删除重复行
cronadd "$TMPDIR"/cron_tmp2 cronadd "$TMPDIR"/cron_tmp2
rm -f "$TMPDIR"/cron_tmp "$TMPDIR"/cron_tmp2 rm -f "$TMPDIR"/cron_tmp "$TMPDIR"/cron_tmp2
#加载条件任务 #加载条件任务
[ -s "$CRASHDIR"/task/afstart ] && { . "$CRASHDIR"/task/afstart; } & [ -s "$CRASHDIR"/task/afstart ] && { . "$CRASHDIR"/task/afstart; } &
[ -s "$CRASHDIR"/task/affirewall -a -s /etc/init.d/firewall -a ! -f /etc/init.d/firewall.bak ] && { [ -s "$CRASHDIR"/task/affirewall -a -s /etc/init.d/firewall -a ! -f /etc/init.d/firewall.bak ] && {
#注入防火墙 #注入防火墙
line=$(grep -En "fw.* restart" /etc/init.d/firewall | cut -d ":" -f 1) line=$(grep -En "fw.* restart" /etc/init.d/firewall | cut -d ":" -f 1)
sed -i.bak "${line}a\\. $CRASHDIR/task/affirewall" /etc/init.d/firewall sed -i.bak "${line}a\\. $CRASHDIR/task/affirewall" /etc/init.d/firewall
line=$(grep -En "fw.* start" /etc/init.d/firewall | cut -d ":" -f 1) line=$(grep -En "fw.* start" /etc/init.d/firewall | cut -d ":" -f 1)
sed -i "${line}a\\. $CRASHDIR/task/affirewall" /etc/init.d/firewall sed -i "${line}a\\. $CRASHDIR/task/affirewall" /etc/init.d/firewall
} & } &
exit 0 exit 0
else else
. "$CRASHDIR"/starts/start_error.sh . "$CRASHDIR"/starts/start_error.sh
fi fi