~继续优化

This commit is contained in:
juewuy
2025-12-20 20:30:50 +08:00
parent 51f5795731
commit 9a2d98a6b8
6 changed files with 78 additions and 80 deletions

View File

@@ -1,45 +1,25 @@
#!/bin/sh
. "$CRASHDIR"/configs/ShellCrash.cfg
. "$CRASHDIR"/configs/gateway.cfg
. "$CRASHDIR"/libs/web_json.sh
OFFSET=0
API="https://api.telegram.org/bot$TG_TOKEN"
STATE_FILE="/tmp/ShellCrash/tgbot_state"
LOGFILE="/tmp/ShellCrash/tgbot.log"
### --- 基础函数 --- ###
setproxy(){
[ -n "$(pidof CrashCore)" ] && {
[ -n "$authentication" ] && auth="$authentication@"
[ -z "$mix_port" ] && mix_port=7890
export https_proxy="http://${auth}127.0.0.1:$mix_port"
}
}
webget() {
setproxy
if curl --version >/dev/null 2>&1; then
curl -kfsSl --connect-timeout 3 $1 2>/dev/null
else
wget -Y on -q --timeout=3 -O - $1
fi
}
webpost() {
setproxy
if curl --version >/dev/null 2>&1; then
curl -kfsSl -X POST --connect-timeout 3 -H "Content-Type: application/json; charset=utf-8" "$1" -d "$2" >/dev/null 2>&1
else
wget -Y on -q --timeout=3 --method=POST --header="Content-Type: application/json; charset=utf-8" --body-data="$2" "$1"
fi
}
send_msg() {
TEXT="$1"
webpost "$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\"}"
}
send_help(){
TEXT=$(cat <<EOF
进群讨论:
https://t.me/+RKujv98Gbx5zGY-P
https://t.me/+6AElkMDzwPxmMmM1
项目地址:
https://github.com/juewuy/ShellCrash
https://github.com/juewuy/ShellClash
相关教程:
https://juewuy.github.io
请喝咖啡:
@@ -93,7 +73,7 @@ EOF
EOF
)
webpost "$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}"
}
@@ -129,7 +109,7 @@ do_set_sub() {
### --- 轮询主进程 --- ###
polling(){
while true; do
UPDATES=$(webget "$API/getUpdates?timeout=25&offset=$OFFSET")
UPDATES=$(web_json_get "$API/getUpdates?timeout=25&offset=$OFFSET")
echo "$UPDATES" | grep -q '"update_id"' || continue
@@ -205,6 +185,6 @@ polling(){
done
}
send_menu
polling

View File

@@ -2,6 +2,7 @@
# Copyright (C) Juewuy
CFG="$CRASHDIR"/configs/gateway.cfg
touch "$CFG"
. "$CFG"
gateway(){ #访问与控制主菜单
@@ -9,13 +10,13 @@ gateway(){ #访问与控制主菜单
echo -e "\033[30;47m欢迎使用访问与控制菜单\033[0m"
echo -----------------------------------------------
echo -e " 1 配置公网访问防火墙"
echo -e " 2 配置Telegram专属控制机器人"
echo -e " 2 配置Telegram专属控制机器人 \033[32m$bot_tg_service\033[0m"
echo -e " 3 配置DDNS自动域名"
[ "$disoverride" != "1" ] && {
echo -e " 4 自定义\033[32m公网Vmess入站\033[0m节点"
echo -e " 5 自定义\033[32m公网ShadowSocks入站\033[0m节点"
echo -e " 6 配置\033[32mTailscale内网穿透\033[0m(限Singbox)"
echo -e " 7 配置\033[32mWireguard客户端\033[0m"
echo -e " 4 自定义\033[33m公网Vmess入站\033[0m节点 \033[32m$vms_service\033[0m"
echo -e " 5 自定义\033[33m公网ShadowSocks入站\033[0m节点 \033[32m$sss_service\033[0m"
echo -e " 6 配置\033[36mTailscale内网穿透\033[0m(限Singbox) \033[32m$ts_service\033[0m"
echo -e " 7 配置\033[36mWireguard客户端\033[0m(限Singbox) \033[32m$wg_service\033[0m"
}
echo -e " 0 返回上级菜单 \033[0m"
echo -----------------------------------------------
@@ -48,12 +49,16 @@ gateway(){ #访问与控制主菜单
else
echo -e "\033[33m$crashcore内核暂不支持此功能,请先更换内核!\033[0m"
sleep 1
checkupdate && setcore
fi
gateway
;;
7)
set_wireguard
if echo "$crashcore" | grep -q 'sing';then
set_wireguard
else
echo -e "\033[33m$crashcore内核暂不支持此功能,请先更换内核!\033[0m"
sleep 1
fi
gateway
;;
*) errornum ;;
@@ -112,10 +117,9 @@ set_bot_tg_init(){
echo -----------------------------------------------
read -p "我已经发送完成(1/0) > " res
if [ "$res" = 1 ]; then
url_tg=https://api.telegram.org/bot${TOKEN}/getUpdates
[ -n "$authentication" ] && auth="$authentication@"
export https_proxy="http://${auth}127.0.0.1:$mix_port"
chat=$(webget $url_tg | tail -n -1)
. "$CRASHDIR"/libs/web_json.sh #加载web工具
bot_api=https://api.telegram.org/bot$TOKEN
chat=$(web_json_get "$bot_api/getUpdates" | tail -n -1)
[ -n "$chat" ] && chat_ID=$(echo $chat | grep -oE '"id":.*,"is_bot":false' | sed s'/"id"://'g | sed s'/,"is_bot":false//'g)
[ -z "$chat_ID" ] && {
echo -e "\033[31m无法获取对话ID请确认使用的不是已经被绑定的机器人或手动输入ChatID\033[0m"
@@ -126,15 +130,19 @@ set_bot_tg_init(){
setconfig TG_TOKEN $TOKEN "$CFG"
setconfig TG_CHATID $chat_ID "$CFG"
#设置机器人快捷命令
curl -s -X POST "https://api.telegram.org/bot$TOKEN/setMyCommands" \
-H "Content-Type: application/json" \
-d '{
"commands": [
{"command": "crash", "description": "呼出ShellCrash菜单"},
{"command": "help", "description": "查看帮助"}
]
}'
echo -e "\033[32m已完成Telegram机器人设置\033[0m"
JSON=$(cat <<EOF
{
"commands": [
{"command": "crash", "description": "呼出ShellCrash菜单"},
{"command": "help", "description": "查看帮助"}
]
}
EOF
)
TEXT='已完成Telegram机器人设置'
web_json_post "$bot_api/setMyCommands" "$JSON"
web_json_post "$bot_api/sendMessage" '{"chat_id":"'"$chat_ID"'","text":"'"$TEXT"'","parse_mode":"Markdown"}'
echo -e "\033[32m$TEXT\033[0m"
return 0
else
echo -e "\033[31m无法获取对话ID请重新配置\033[0m"
@@ -143,12 +151,13 @@ set_bot_tg_init(){
fi
}
set_bot_tg_service(){
PID=$(pidof /bin/sh "$CRASHDIR"/menus/bot_tg.sh)
if [ "$bot_tg_service" = ON ];then
bot_tg_service=OFF
PID=$(pidof bot_tg.sh) && [ -n "$PID" ] && kill -9 $PID >/dev/null 2>&1
[ -n "$PID" ] && kill -9 $PID >/dev/null 2>&1
else
bot_tg_service=ON
[ -z "$(pidof bot_tg.sh)" ] && "$CRASHDIR"/components/bot_tg.sh &
[ -z "$PID" ] && "$CRASHDIR"/menus/bot_tg.sh &
fi
setconfig bot_tg_service "$bot_tg_service"
}
@@ -172,9 +181,12 @@ set_bot_tg(){
else
set_bot_tg_init && set_bot_tg_service
fi
sleep 1
set_bot_tg
;;
2)
set_bot_tg_init && set_bot_tg_service
set_bot_tg
;;
*)
errornum