~大幅度优化tg_bot控制脚本

This commit is contained in:
juewuy
2025-12-26 20:42:13 +08:00
parent 91686c82da
commit ab774251b0
2 changed files with 113 additions and 65 deletions

View File

@@ -1,18 +1,10 @@
. "$CRASHDIR"/libs/set_proxy.sh . "$CRASHDIR"/libs/set_proxy.sh
#$1:目标地址 $2:json字符串 #$1:目标地址 $2:json字符串
web_json_get() {
setproxy
if curl --version >/dev/null 2>&1; then
curl -ksSl --connect-timeout 3 "$1" 2>/dev/null
else
wget -Y on -q --timeout=3 -O - "$1"
fi
}
web_json_post() { web_json_post() {
setproxy setproxy
if curl --version >/dev/null 2>&1; then if curl --version >/dev/null 2>&1; then
curl -ksSl -X POST --connect-timeout 3 -H "Content-Type: application/json; charset=utf-8" "$1" -d "$2" >/dev/null 2>&1 curl -kfsSl -X POST --connect-timeout 3 -H "Content-Type: application/json" "$1" -d "$2" >/dev/null
else else
wget -Y on -q --timeout=3 --method=POST --header="Content-Type: application/json; charset=utf-8" --body-data="$2" "$1" wget -Y on -q --timeout=3 --method=POST --header="Content-Type: application/json" --body-data="$2" "$1"
fi fi
} }

View File

@@ -5,12 +5,32 @@
. "$CRASHDIR"/configs/gateway.cfg . "$CRASHDIR"/configs/gateway.cfg
. "$CRASHDIR"/configs/ShellCrash.cfg . "$CRASHDIR"/configs/ShellCrash.cfg
OFFSET=0 TMPDIR='/tmp/ShellCrash'
API="https://api.telegram.org/bot$TG_TOKEN" API="https://api.telegram.org/bot$TG_TOKEN"
STATE_FILE="/tmp/ShellCrash/tgbot_state" STATE_FILE="$TMPDIR/tgbot_state"
LOGFILE="/tmp/ShellCrash/tgbot.log" LOGFILE="$TMPDIR/tgbot.log"
OFFSET=0
### --- 基础函数 --- ### ### --- 基础函数 --- ###
web_get(){
setproxy
if curl1 --version >/dev/null 2>&1; then
curl -kfsSl --connect-timeout 3 "$1"
else
wget -Y on -q --timeout=3 -O - "$1"
fi
}
web_download(){
setproxy
if curl --version >/dev/null 2>&1; then
curl -kfsSl "$1" -o "$2"
else
wget -Y on -q --timeout=3 -O "$2" "$1"
fi
}
web_upload(){
curl -ksSfl -X POST --connect-timeout 20 "$API/sendDocument" -F "chat_id=$TG_CHATID" -F "document=@$1" >/dev/null
}
send_msg(){ send_msg(){
TEXT="$1" TEXT="$1"
web_json_post "$API/sendMessage" "{\"chat_id\":\"$TG_CHATID\",\"text\":\"$TEXT\",\"parse_mode\":\"Markdown\"}" web_json_post "$API/sendMessage" "{\"chat_id\":\"$TG_CHATID\",\"text\":\"$TEXT\",\"parse_mode\":\"Markdown\"}"
@@ -36,53 +56,45 @@ send_menu() {
#获取运行状态 #获取运行状态
PID=$(pidof CrashCore | awk '{print $NF}') PID=$(pidof CrashCore | awk '{print $NF}')
if [ -n "$PID" ]; then if [ -n "$PID" ]; then
run=正在运行 run='🟢正在运行'
running_status running_status
else else
run=未运行 run='🟡未运行'
fi fi
corename=$(echo $crashcore | sed 's/singboxr/SingBoxR/' | sed 's/singbox/SingBox/' | sed 's/clash/Clash/' | sed 's/meta/Mihomo/') corename=$(echo $crashcore | sed 's/singboxr/SingBoxR/' | sed 's/singbox/SingBox/' | sed 's/clash/Clash/' | sed 's/meta/Mihomo/')
TEXT=$(cat <<EOF TEXT=$(cat <<EOF
*欢迎使用ShellCrash* 版本:$versionsh_l *欢迎使用ShellCrash*_${versionsh_l}_
$corename服务$run 【*$redir_mod*】 $corename服务$run
内存占用:$VmRSS 已运行:$day$time 【*$redir_mod*】内存占用:$VmRSS
已运行:$day$time
请选择操作: 请选择操作:
EOF EOF
) )
MENU=$(cat <<EOF MENU=$(cat <<EOF
{ {
"inline_keyboard":[ "inline_keyboard":[
[ [
{"text":" 启用劫持","callback_data":"start_redir"}, {"text":"✈️ 启用劫持","callback_data":"start_redir"},
{"text":" 纯净模式","callback_data":"stop_redir"}, {"text":"💧 纯净模式","callback_data":"stop_redir"},
{"text":"🔄 重启内核","callback_data":"restart"} {"text":"🕹 重启服务","callback_data":"restart"}
], ],
[ [
{"text":"📄 查看日志","callback_data":"readlog"}, {"text":"📄 查看日志","callback_data":"readlog"},
{"text":"🔃 文件传输","callback_data":"transport"}, {"text":"🔃 文件传输","callback_data":"transport"}
{"text":"🌀 添加订阅","callback_data":"set_sub"}
] ]
] ]
} }
EOF EOF
) )
web_json_post "$API/sendMessage" "{\"chat_id\":\"$TG_CHATID\",\"text\":\"$TEXT\",\"parse_mode\":\"Markdown\",\"reply_markup\":$MENU}" web_json_post "$API/sendMessage" "{\"chat_id\":\"$TG_CHATID\",\"text\":\"$TEXT\",\"parse_mode\":\"Markdown\",\"reply_markup\":$MENU}"
} }
### --- 文件传输 --- ###
send_transport_menu(){ send_transport_menu(){
TEXT=$(cat <<EOF TEXT='请选择需要上传或下载的具体文件:'
请选择需要上传或下载的具体文件:
EOF
)
core_config=config.yaml
if echo "$crashcore" | grep -q 'singbox';then if echo "$crashcore" | grep -q 'singbox';then
core_config=config.json config_type=json
ccdir=jsons
else else
core_config=config.yaml config_type=yaml
ccdir=yamls
fi fi
if curl -h >/dev/null 2>&1;then if curl -h >/dev/null 2>&1;then
@@ -90,9 +102,10 @@ EOF
[ [
{"text":"📥 下载日志","callback_data":"ts_get_log"}, {"text":"📥 下载日志","callback_data":"ts_get_log"},
{"text":"💾 备份设置","callback_data":"ts_get_bak"}, {"text":"💾 备份设置","callback_data":"ts_get_bak"},
{"text":"⬇️ $core_config","callback_data":"ts_get_ccf"} {"text":"⬇️ 下载配置","callback_data":"ts_get_ccf"}
], ],
EOF) EOF
)
else else
CURL_KB='[{"text":"⚠️ 因当前设备缺少curl应用仅支持上传功能","callback_data":"noop"}],' CURL_KB='[{"text":"⚠️ 因当前设备缺少curl应用仅支持上传功能","callback_data":"noop"}],'
fi fi
@@ -101,9 +114,9 @@ EOF)
"inline_keyboard":[ "inline_keyboard":[
$CURL_KB $CURL_KB
[ [
{"text":"📤 上传内核","callback_data":"ts_up_core"}, {"text":"🪐 上传内核","callback_data":"ts_up_core"},
{"text":"🔄 还原设置","callback_data":"ts_up_bak"}, {"text":"🔄 还原设置","callback_data":"ts_up_bak"},
{"text":"⬆️ $core_config","callback_data":"ts_up_ccf"} {"text":"⬆️ 上传配置","callback_data":"ts_up_ccf"}
] ]
] ]
} }
@@ -113,8 +126,43 @@ EOF
web_json_post "$API/sendMessage" "{\"chat_id\":\"$TG_CHATID\",\"text\":\"$TEXT\",\"parse_mode\":\"Markdown\",\"reply_markup\":$MENU}" web_json_post "$API/sendMessage" "{\"chat_id\":\"$TG_CHATID\",\"text\":\"$TEXT\",\"parse_mode\":\"Markdown\",\"reply_markup\":$MENU}"
} }
upload_file(){ process_file(){
curl -k -X POST --connect-timeout 20 "$API/sendDocument" -F "chat_id=$TG_CHATID" -F "document=@$1" case "$FILE_TYPE" in
1)
. "$CRASHDIR"/libs/core_tools.sh
core_check "$TMPDIR/$FILE_NAME" && res='成功!即将重启服务!' || res='失败,请仔细检查文件或重试!'
send_msg "内核更新$res"
sleep 2
"$CRASHDIR"/start.sh start
;;
2)
tar -zxf "$TMPDIR/$FILE_NAME" -C "$CRASHDIR"/configs && res='配置文件已还原,请手动重启服务!' || res='解压还原失败,请仔细检查文件或重试!'
send_msg "$res"
;;
3)
mv -f "$TMPDIR/$FILE_NAME" "$CRASHDIR/${config_type}s/" && res='配置文件已上传,请手动重启服务!' || res='上传失败,请仔细检查文件或重试!'
send_msg "$res"
;;
esac
rm -f "$TMPDIR/$FILE_NAME"
send_menu
}
download_file(){
FILE_NAME=$(echo "$UPDATES" | sed 's/"callback_query".*//g' | grep -o '"file_name":"[^"]*"' | head -n1 | sed 's/.*:"//;s/"$//' | grep -E '\.(gz|upx|json|yaml)$')
if [ -n "$FILE_NAME" ];then
FILE_PATH=$(web_get "$API/getFile?file_id=$FILE_ID" | grep -o '"file_path":"[^"]*"' | sed 's/.*:"//;s/"$//')
API_FILE="https://api.telegram.org/file/bot$TG_TOKEN"
web_download "$API_FILE/$FILE_PATH" "$TMPDIR/$FILE_NAME"
if [ "$?" = 0 ];then
process_file
else
send_msg "网络错误,上传失败!请重试!"
fi
else
send_msg "文件格式不匹配,上传失败!"
fi
OFFSET=$((OFFSET + 1))
continue
} }
### --- 具体操作函数 --- ### ### --- 具体操作函数 --- ###
do_start_fw(){ do_start_fw(){
@@ -140,40 +188,46 @@ do_set_sub() {
echo "错误,还未完成的功能!" > "$LOGFILE" echo "错误,还未完成的功能!" > "$LOGFILE"
} }
transport(){ transport(){ #文件传输
case "$CALLBACK" in case "$CALLBACK" in
"ts_get_log") "ts_get_log")
upload_file '/tmp/ShellCrash/ShellCrash.log' web_upload "$TMPDIR"/ShellCrash.log
sleep 3
send_menu send_menu
;; ;;
"ts_get_bak") "ts_get_bak")
now=$(date +%Y%m%d_%H%M%S) now=$(date +%Y%m%d_%H%M%S)
FILE="$CRASHDIR"/configs_"$now".tar.gz FILE="$TMPDIR/configs_$now.tar.gz"
tar -zcf "$FILE" "$CRASHDIR"/configs/ tar -zcf "$FILE" -C "$CRASHDIR/configs/" .
upload_file "$FILE" web_upload "$FILE"
rm -rf "$FILE" rm -rf "$FILE"
sleep 3
send_menu send_menu
;; ;;
"ts_get_ccf") "ts_get_ccf")
upload_file "$CRASHDIR/$ccdir/$core_config" FILE="$TMPDIR/$config_type.tar.gz"
sleep 3 tar -zcf "$FILE" -C "$CRASHDIR/${config_type}s/" .
web_upload "$FILE"
rm -rf "$FILE"
send_menu send_menu
;; ;;
ts_up_core) "ts_up_core")
send_msg "请发送需要上传的文件:" FILE_TYPE=1
send_msg "请发送需要上传的内核必须是以tar.gz,.gz或.upx结尾的【${corename}】内核!"
;;
"ts_up_bak")
FILE_TYPE=2
send_msg "请发送需要还原的备份文件,必须是【.tar.gz】格式"
;;
"ts_up_ccf")
FILE_TYPE=3
send_msg "请发送需要上传的配置文件,必须是【.${config_type}】格式,支持自定义配置文件"
;; ;;
esac esac
} }
download_file(){
FILE_PATH=$(web_json_get "$API/getFile?file_id=$FILE_ID" | grep -o '"file_path":"[^"]*"' | sed 's/.*:"//;s/"$//')
echo $FILE_PATH
}
### --- 轮询主进程 --- ### ### --- 轮询主进程 --- ###
polling(){ polling(){
while true; do while true; do
UPDATES=$(web_json_get "$API/getUpdates?timeout=25&offset=$OFFSET") UPDATES=$(web_get "$API/getUpdates?timeout=25&offset=$OFFSET")
echo "$UPDATES" | grep -q '"update_id"' || continue echo "$UPDATES" | grep -q '"update_id"' || continue
@@ -214,7 +268,7 @@ polling(){
continue continue
;; ;;
"readlog") "readlog")
send_msg "📄 日志内容如下(已过滤任务日志)\n\`\`\`$(grep -v '任务' /tmp/ShellCrash/ShellCrash.log |tail -n 20)\`\`\`" send_msg "📄 日志内容如下(已过滤任务日志)\n\`\`\`$(grep -v '任务' $TMPDIR/ShellCrash.log |tail -n 20)\`\`\`"
sleep 3 sleep 3
send_menu send_menu
continue continue
@@ -259,6 +313,8 @@ polling(){
done done
} }
send_menu send_menu
polling polling