#!/bin/sh . "$CRASHDIR"/libs/web_json.sh . "$CRASHDIR"/menus/running_status.sh . "$CRASHDIR"/configs/gateway.cfg . "$CRASHDIR"/configs/ShellCrash.cfg OFFSET=0 API="https://api.telegram.org/bot$TG_TOKEN" STATE_FILE="/tmp/ShellCrash/tgbot_state" LOGFILE="/tmp/ShellCrash/tgbot.log" ### --- 基础函数 --- ### send_msg() { TEXT="$1" web_json_post "$API/sendMessage" "{\"chat_id\":\"$TG_CHATID\",\"text\":\"$TEXT\",\"parse_mode\":\"Markdown\"}" } send_help(){ TEXT=$(cat </dev/null 2>&1;then CURL_KB=$(cat < "$LOGFILE" } do_stop_fw() { redir_mod_bf=$redir_mod redir_mod='纯净模式' setconfig redir_mod $redir_mod "$CRASHDIR"/start.sh stop_firewall echo "ShellCrash 已切换到纯净模式!" > "$LOGFILE" } do_restart() { "$CRASHDIR"/start.sh restart echo "ShellCrash 服务已重启!" > "$LOGFILE" } do_set_sub() { #echo "$1" "$2" >> "$CRASHDIR"/configs/providers.cfg echo "错误,还未完成的功能!" > "$LOGFILE" } transport(){ case "$CALLBACK" in "ts_get_log") upload_file '/tmp/ShellCrash/ShellCrash.log' sleep 3 send_menu ;; "ts_get_bak") now=$(date +%Y%m%d_%H%M%S) FILE="$CRASHDIR"/configs_"$now".tar.gz tar -zcf "$FILE" "$CRASHDIR"/configs/ upload_file "$FILE" rm -rf "$FILE" sleep 3 send_menu ;; "ts_get_ccf") upload_file "$CRASHDIR/$ccdir/$core_config" sleep 3 send_menu ;; ts_up_core) send_msg "请发送需要上传的文件:" ;; 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(){ while true; do UPDATES=$(web_json_get "$API/getUpdates?timeout=25&offset=$OFFSET") echo "$UPDATES" | grep -q '"update_id"' || continue OFFSET=$(echo "$UPDATES" | grep -o '"update_id":[0-9]*' | tail -n1 | cut -d: -f2) OFFSET=$((OFFSET + 1)) ### --- 处理按钮事件 --- ### CALLBACK=$(echo "$UPDATES" | grep -o '"data":"[^"]*"' | head -n1 | sed 's/.*:"//;s/"$//') FILE_ID=$(echo "$UPDATES" | sed 's/"callback_query".*//g' | grep -o '"file_id":"[^"]*"' | head -n1 | sed 's/.*:"//;s/"$//') [ -n "$FILE_ID" ] && download_file [ -n "$CALLBACK" ] && case "$CALLBACK" in "start_redir") if [ "$redir_mod" = '纯净模式' ];then do_start_fw send_msg "已切换到$redir_mod_bf!" else send_msg "当前已经是$redir_mod!" fi send_menu continue ;; "stop_redir") if [ "$redir_mod" != '纯净模式' ];then do_stop_fw send_msg "已切换到纯净模式" else send_msg "当前已经是纯净模式!" fi send_menu continue ;; "restart") do_restart send_msg "🔄 服务已重启" sleep 10 send_menu continue ;; "readlog") send_msg "📄 日志内容如下(已过滤任务日志):\n\`\`\`$(grep -v '任务' /tmp/ShellCrash/ShellCrash.log |tail -n 20)\`\`\`" sleep 3 send_menu continue ;; "transport") send_transport_menu continue ;; "set_sub") echo "await_sub" > "$STATE_FILE" send_msg "✏ 请输入新的订阅链接:" continue ;; ts_*) transport continue ;; esac ### --- 处理订阅输入 --- ### TEXT=$(echo "$UPDATES" | grep -o '"text":"[^"]*"' | tail -n1 | sed 's/.*"text":"//;s/"$//') if [ "$(cat "$STATE_FILE" 2>/dev/null)" = "await_sub" ]; then echo "" > "$STATE_FILE" do_set_sub "$TEXT" send_msg "订阅更新完成:\n$(cat "$LOGFILE")" send_menu continue fi ### 处理命令 ### case "$TEXT" in /crash) send_menu ;; /help) send_help ;; esac done } send_menu polling