diff --git a/keepalive.sh b/keepalive.sh index 85d8b5f5..a6a764f1 100644 --- a/keepalive.sh +++ b/keepalive.sh @@ -1,355 +1,355 @@ -#!/bin/bash - - -installpath="$HOME" -source ${installpath}/serv00-play/utils.sh - -autoUp=$1 -sendtype=$2 -TELEGRAM_TOKEN="$3" -TELEGRAM_USERID="$4" -WXSENDKEY="$5" -BUTTON_URL="$6" -PASS="$7" - -#echo "TELEGRAM_TOKEN=$TELEGRAM_TOKEN, TELEGRAM_USERID=$TELEGRAM_USERID,WXSENDKEY=$WXSENDKEY,BUTTON_URL=$BUTTON_URL,pass=$PASS" - -checkHy2Alive() { - if ps aux | grep serv00sb | grep -v "grep" >/dev/null; then - return 0 - else - return 1 - fi - -} - - -sendMsg() { - local msg=$1 - if [ -n "$msg" ]; then - cd $installpath/serv00-play - msg="Host:$host, user:$user, $msg" - if [ "$sendtype" == "1" ]; then - ./tgsend.sh "$msg" - elif [ "$sendtype" == "2" ]; then - ./wxsend.sh "$msg" - elif [ "$sendtype" == "3" ]; then - ./tgsend.sh "$msg" - ./wxsend.sh "$msg" - fi - fi -} - -checkResetCron() { - echo "run checkResetCron" - local msg="" - cd ${installpath}/serv00-play/ - crontab -l | grep keepalive - if ! crontab -l | grep keepalive; then - msg="crontab记录被删过,并且已重建。" - tm=$(jq -r ".chktime" config.json) - addCron "$tm" - sendMsg $msg - fi -} - -#构建消息配置文件 -makeMsgConfig(){ - echo "构造消息配置文件..." - cat > msg.json </dev/null 2>&1 & - -} - -startMtg() { - cd ${installpath}/serv00-play/dmtg - - config="config.json" - - secret=$(jq -r ".secret" $config) - port=$(jq -r ".port" $config) - cmd="nohup ./mtg simple-run -n 1.1.1.1 -t 30s -a 1MB 0.0.0.0:$port $secret -c 8192 --prefer-ip=\"prefer-ipv6\" >/dev/null 2>&1 &" - eval "$cmd" - sleep 3 - if checkMtgAlive; then - echo "启动成功" - else - echo "启动失败,请检查进程" - fi - -} - - -startAlist() { - alistpath="${installpath}/serv00-play/alist" - - if [[ -d "$alistpath/data" && -e "$alistpath/alist" ]]; then - echo "正在启动alist..." - cd $alistpath - domain=$(jq -r ".domain" config.json) - - if checkProcAlive "alist"; then - echo "alist已启动,请勿重复启动!" - else - nohup ./alist server >/dev/null 2>&1 & - sleep 3 - if ! checkProcAlive "alist"; then - red "启动失败,请检查!" - return 1 - else - echo "启动成功!" - fi - fi - else - red "请先行安装再启动!" - return - fi - -} - -startSunPanel(){ - cd ${installpath}/serv00-play/sunpanel - cmd="nohup ./sun-panel >/dev/null 2>&1 &" - eval "$cmd" -} - -startWebSSH(){ - cd ${installpath}/serv00-play/webssh - ssh_port=$(jq -r ".port" config.json) - cmd="nohup ./wssh --port=$ssh_port --fbidhttp=False --xheaders=False --encoding='utf-8' --delay=10 >/dev/null 2>&1 &" - eval "$cmd" -} - -#main -host=$(hostname) -user=$(whoami) - -echo "正在调用keepalive.sh" -if [[ "$autoUp" == "autoupdate" ]]; then - echo "run autoUpdate" - autoUpdate -fi - - -echo "Host:$host, user:$user" -cd ${installpath}/serv00-play/ - -if [[ -n "$autoUp" ]];then - makeMsgConfig -fi -if [ ! -f config.json ]; then - echo "未配置保活项目,请先行配置!" - exit 0 -fi - -monitor=($(jq -r ".item[]" config.json)) - -tg_token=$(jq -r ".telegram_token // empty" config.json) - -if [[ -z "$tg_token" ]]; then - echo "从msg.json获取 telegram_token" - TELEGRAM_TOKEN=$(jq -r '.telegram_token // empty' msg.json) -else - TELEGRAM_TOKEN=$tg_token -fi - -tg_userid=$(jq -r ".telegram_userid // empty" config.json) - -if [[ -z "$tg_userid" ]]; then - echo "从msg.json获取telegram_userid" - if [[ -e "msg.json" ]]; then - TELEGRAM_USERID=$(jq -r ".telegram_userid // empty" msg.json) - fi -else - TELEGRAM_USERID=$tg_userid -fi - -wx_sendkey=$(jq -r ".wxsendkey // empty" config.json) - -if [[ -z "$wx_sendkey" ]]; then - echo "从msg.json获取wxsendkey" - if [[ -e "msg.json" ]]; then - WXSENDKEY=$(jq -r ".wxsendkey // empty" msg.json) - fi -else - WXSENDKEY=$wx_sendkey -fi - -send_type=$(jq -r ".sendtype // empty" config.json) -if [ -z "$send_type" ]; then - echo "从msg.json获取 sendtype" - if [[ -e "msg.json" ]]; then - sendtype=$(jq -r ".sendtype // empty" msg.json) - fi -else - sendtype=$send_type -fi - -if [ -z "$BUTTON_URL" ]; then - echo "从msg.json获取 button_url" - BUTTON_URL=$(jq -r ".button_url // empty" msg.json) -fi - -if [ -z "$PASS" ]; then - echo "从msg.json获取 password" - PASS=$(jq -r ".password // empty" msg.json) -fi - -export TELEGRAM_TOKEN TELEGRAM_USERID WXSENDKEY sendtype BUTTON_URL PASS - -#echo "最终TELEGRAM_TOKEN=$TELEGRAM_TOKEN,TELEGRAM_USERID=$TELEGRAM_USERID" - - -for obj in "${monitor[@]}"; do - msg="" - # echo "obj= $obj" - if [ "$obj" == "sun-panel" ]; then - if ! checkProcAlive "sun-panel"; then - startSunPanel - sleep 3 - if ! checkProcAlive "sun-panel"; then - msg="sun-panel restarted failure." - else - msg="sun-panel restarted successfully." - fi - fi - elif [ "$obj" == "webssh" ]; then - if ! checkProcAlive "wssh"; then - startWebSSH - sleep 5 - if ! checkProcAlive "wssh"; then - msg="webssh restarted failure." - else - msg="webssh restarted successfully." - fi - fi - elif [ "$obj" == "vmess" ]; then - if ! checkvmessAlive; then - cd ${installpath}/serv00-play/singbox - chmod +x ./start.sh && ./start.sh 1 keep - sleep 5 - if ! checkvmessAlive; then - msg="vmess restarted failure." - else - msg="vmess restarted successfully." - fi - fi - #hy2和vmess+ws都只需要启动serv00sb,所以可以这么写 - elif [[ "$obj" == "hy2/vmess+ws" || "$obj" == "hy2" ]]; then - if ! checkHy2Alive; then - #echo "重启serv00sb中..." - cd ${installpath}/serv00-play/singbox - chmod +x ./start.sh && ./start.sh 2 keep - sleep 5 - if ! checkHy2Alive; then - msg="hy2 restarted failure." - else - msg="hy2 restarted successfully." - fi - fi - elif [ "$obj" == "nezha-agent" ]; then - if ! checknezhaAgentAlive; then - cd ${installpath}/serv00-play/nezha - startNeZhaAgent - sleep 5 - if ! checknezhaAgentAlive; then - msg="nezha-agent restarted failure." - else - msg="nezha-agent restarted successfully." - fi - fi - elif [ "$obj" == "mtg" ]; then - if ! checkMtgAlive; then - cd ${installpath}/serv00-play/dmtg - startMtg - sleep 5 - if ! checkMtgAlive; then - msg="mtproto restarted failure." - else - msg="mtproto restarted successfully." - fi - fi - elif [ "$obj" == "alist" ]; then - if ! checkProcAlive "alist"; then - startAlist - sleep 5 - if ! checkProcAlive "alist"; then - msg="alist restarted failure." - else - msg="alist restarted successfully." - fi - fi - elif [ "$obj" == "wssh" ]; then - if ! checkProcAlive wssh; then - startAlist - sleep 5 - if ! checkAlistAlive; then - msg="alist restarted failure." - else - msg="alist restarted successfully." - fi - fi - else - continue - fi - - sendMsg "$msg" - -done - -if [ ${#monitor[@]} -gt 0 ]; then - checkResetCron -fi +#!/bin/bash + + +installpath="$HOME" +source ${installpath}/serv00-play/utils.sh + +autoUp=$1 +sendtype=$2 +TELEGRAM_TOKEN="$3" +TELEGRAM_USERID="$4" +WXSENDKEY="$5" +BUTTON_URL="$6" +PASS="$7" + +#echo "TELEGRAM_TOKEN=$TELEGRAM_TOKEN, TELEGRAM_USERID=$TELEGRAM_USERID,WXSENDKEY=$WXSENDKEY,BUTTON_URL=$BUTTON_URL,pass=$PASS" + +checkHy2Alive() { + if ps aux | grep serv00sb | grep -v "grep" >/dev/null; then + return 0 + else + return 1 + fi + +} + + +sendMsg() { + local msg=$1 + if [ -n "$msg" ]; then + cd $installpath/serv00-play + msg="Host:$host, user:$user, $msg" + if [ "$sendtype" == "1" ]; then + ./tgsend.sh "$msg" + elif [ "$sendtype" == "2" ]; then + ./wxsend.sh "$msg" + elif [ "$sendtype" == "3" ]; then + ./tgsend.sh "$msg" + ./wxsend.sh "$msg" + fi + fi +} + +checkResetCron() { + echo "run checkResetCron" + local msg="" + cd ${installpath}/serv00-play/ + crontab -l | grep keepalive + if ! crontab -l | grep keepalive; then + msg="crontab记录被删过,并且已重建。" + tm=$(jq -r ".chktime" config.json) + addCron "$tm" + sendMsg $msg + fi +} + +#构建消息配置文件 +makeMsgConfig(){ + echo "构造消息配置文件..." + cat > msg.json </dev/null 2>&1 & + +} + +startMtg() { + cd ${installpath}/serv00-play/dmtg + + config="config.json" + + secret=$(jq -r ".secret" $config) + port=$(jq -r ".port" $config) + cmd="nohup ./mtg simple-run -n 1.1.1.1 -t 30s -a 1MB 0.0.0.0:$port $secret -c 8192 --prefer-ip=\"prefer-ipv6\" >/dev/null 2>&1 &" + eval "$cmd" + sleep 3 + if checkMtgAlive; then + echo "启动成功" + else + echo "启动失败,请检查进程" + fi + +} + + +startAlist() { + alistpath="${installpath}/serv00-play/alist" + + if [[ -d "$alistpath/data" && -e "$alistpath/alist" ]]; then + echo "正在启动alist..." + cd $alistpath + domain=$(jq -r ".domain" config.json) + + if checkProcAlive "alist"; then + echo "alist已启动,请勿重复启动!" + else + nohup ./alist server >/dev/null 2>&1 & + sleep 3 + if ! checkProcAlive "alist"; then + red "启动失败,请检查!" + return 1 + else + echo "启动成功!" + fi + fi + else + red "请先行安装再启动!" + return + fi + +} + +startSunPanel(){ + cd ${installpath}/serv00-play/sunpanel + cmd="nohup ./sun-panel >/dev/null 2>&1 &" + eval "$cmd" +} + +startWebSSH(){ + cd ${installpath}/serv00-play/webssh + ssh_port=$(jq -r ".port" config.json) + cmd="nohup ./wssh --port=$ssh_port --fbidhttp=False --xheaders=False --encoding='utf-8' --delay=10 >/dev/null 2>&1 &" + eval "$cmd" +} + +#main +host=$(hostname) +user=$(whoami) + +echo "正在调用keepalive.sh" +if [[ "$autoUp" == "autoupdate" ]]; then + echo "run autoUpdate" + autoUpdate +fi + + +echo "Host:$host, user:$user" +cd ${installpath}/serv00-play/ + +if [[ -n "$autoUp" ]];then + makeMsgConfig +fi +if [ ! -f config.json ]; then + echo "未配置保活项目,请先行配置!" + exit 0 +fi + +monitor=($(jq -r ".item[]" config.json)) + +tg_token=$(jq -r ".telegram_token // empty" config.json) + +if [[ -z "$tg_token" ]]; then + echo "从msg.json获取 telegram_token" + TELEGRAM_TOKEN=$(jq -r '.telegram_token // empty' msg.json) +else + TELEGRAM_TOKEN=$tg_token +fi + +tg_userid=$(jq -r ".telegram_userid // empty" config.json) + +if [[ -z "$tg_userid" ]]; then + echo "从msg.json获取telegram_userid" + if [[ -e "msg.json" ]]; then + TELEGRAM_USERID=$(jq -r ".telegram_userid // empty" msg.json) + fi +else + TELEGRAM_USERID=$tg_userid +fi + +wx_sendkey=$(jq -r ".wxsendkey // empty" config.json) + +if [[ -z "$wx_sendkey" ]]; then + echo "从msg.json获取wxsendkey" + if [[ -e "msg.json" ]]; then + WXSENDKEY=$(jq -r ".wxsendkey // empty" msg.json) + fi +else + WXSENDKEY=$wx_sendkey +fi + +send_type=$(jq -r ".sendtype // empty" config.json) +if [ -z "$send_type" ]; then + echo "从msg.json获取 sendtype" + if [[ -e "msg.json" ]]; then + sendtype=$(jq -r ".sendtype // empty" msg.json) + fi +else + sendtype=$send_type +fi + +if [ -z "$BUTTON_URL" ]; then + echo "从msg.json获取 button_url" + BUTTON_URL=$(jq -r ".button_url // empty" msg.json) +fi + +if [ -z "$PASS" ]; then + echo "从msg.json获取 password" + PASS=$(jq -r ".password // empty" msg.json) +fi + +export TELEGRAM_TOKEN TELEGRAM_USERID WXSENDKEY sendtype BUTTON_URL PASS + +#echo "最终TELEGRAM_TOKEN=$TELEGRAM_TOKEN,TELEGRAM_USERID=$TELEGRAM_USERID" + + +for obj in "${monitor[@]}"; do + msg="" + # echo "obj= $obj" + if [ "$obj" == "sun-panel" ]; then + if ! checkProcAlive "sun-panel"; then + startSunPanel + sleep 3 + if ! checkProcAlive "sun-panel"; then + msg="sun-panel restarted failure." + else + msg="sun-panel restarted successfully." + fi + fi + elif [ "$obj" == "webssh" ]; then + if ! checkProcAlive "wssh"; then + startWebSSH + sleep 5 + if ! checkProcAlive "wssh"; then + msg="webssh restarted failure." + else + msg="webssh restarted successfully." + fi + fi + elif [ "$obj" == "vmess" ]; then + if ! checkvmessAlive; then + cd ${installpath}/serv00-play/singbox + chmod +x ./start.sh && ./start.sh 1 keep + sleep 5 + if ! checkvmessAlive; then + msg="vmess restarted failure." + else + msg="vmess restarted successfully." + fi + fi + #hy2和vmess+ws都只需要启动serv00sb,所以可以这么写 + elif [[ "$obj" == "hy2/vmess+ws" || "$obj" == "hy2" ]]; then + if ! checkHy2Alive; then + #echo "重启serv00sb中..." + cd ${installpath}/serv00-play/singbox + chmod +x ./start.sh && ./start.sh 2 keep + sleep 5 + if ! checkHy2Alive; then + msg="hy2 restarted failure." + else + msg="hy2 restarted successfully." + fi + fi + elif [ "$obj" == "nezha-agent" ]; then + if ! checknezhaAgentAlive; then + cd ${installpath}/serv00-play/nezha + startNeZhaAgent + sleep 5 + if ! checknezhaAgentAlive; then + msg="nezha-agent restarted failure." + else + msg="nezha-agent restarted successfully." + fi + fi + elif [ "$obj" == "mtg" ]; then + if ! checkMtgAlive; then + cd ${installpath}/serv00-play/dmtg + startMtg + sleep 5 + if ! checkMtgAlive; then + msg="mtproto restarted failure." + else + msg="mtproto restarted successfully." + fi + fi + elif [ "$obj" == "alist" ]; then + if ! checkProcAlive "alist"; then + startAlist + sleep 5 + if ! checkProcAlive "alist"; then + msg="alist restarted failure." + else + msg="alist restarted successfully." + fi + fi + elif [ "$obj" == "wssh" ]; then + if ! checkProcAlive wssh; then + startAlist + sleep 5 + if ! checkAlistAlive; then + msg="alist restarted failure." + else + msg="alist restarted successfully." + fi + fi + else + continue + fi + + sendMsg "$msg" + +done + +if [ ${#monitor[@]} -gt 0 ]; then + checkResetCron +fi diff --git a/tgsend.sh b/tgsend.sh index a30992d1..ee3d4b94 100644 --- a/tgsend.sh +++ b/tgsend.sh @@ -1,106 +1,106 @@ -#!/bin/bash - -message_text=$1 - -replaceValue() { - local url=$1 - local target=$2 - local value=$3 - local result - result=$(printf '%s' "$url" | sed "s|#${target}|${value//&/\\&}|g") - echo "$result" -} - -toBase64() { - echo -n "$1" | base64 -} - -urlencode() { - local input="$1" - local output="" - local length=${#input} - for ((i = 0; i < length; i++)); do - local char="${input:i:1}" - case "$char" in - [a-zA-Z0-9.~_-]) output+="$char" ;; - *) output+="$(printf '%%%02X' "'$char")" ;; - esac - done - echo "$output" -} - -toTGMsg() { - local msg=$1 - local title="*Serv00-play通知*" - local host_icon="🖥️" - local user_icon="👤" - local time_icon="⏰" - local notify_icon="📢" - - # 获取当前时间 - local current_time=$(date "+%Y-%m-%d %H:%M:%S") - - if [[ "$msg" != Host:* ]]; then - local formatted_msg="${title} \n\n" - formatted_msg+="${time_icon} *时间:* ${current_time} \n" - formatted_msg+="${notify_icon} *通知内容:* \n$msg \n\n" - echo -e "$formatted_msg" - return - fi - - local host=$(echo "$msg" | sed -n 's/.*Host:\([^,]*\).*/\1/p' | xargs) - local user=$(echo "$msg" | sed -n 's/.*user:\([^,]*\).*/\1/p' | xargs) - local notify_content=$(echo "$msg" | sed -E 's/.*user:[^,]*,//' | xargs) - - # 格式化消息内容,Markdown 换行使用两个空格 + 换行 - local formatted_msg="${title} \n\n" - formatted_msg+="${host_icon} *主机:* ${host} \n" - formatted_msg+="${user_icon} *用户:* ${user} \n" - formatted_msg+="${time_icon} *时间:* ${current_time} \n\n" - formatted_msg+="${notify_icon} *通知内容:* ${notify_content} \n\n" - - echo -e "$formatted_msg" # 使用 -e 选项以确保换行符生效 -} - -telegramBotToken=${TELEGRAM_TOKEN} -telegramBotUserId=${TELEGRAM_USERID} -formatted_msg=$(toTGMsg "$message_text") -button_url=${BUTTON_URL:-"https://www.youtube.com/@frankiejun8965"} -URL="https://api.telegram.org/bot${telegramBotToken}/sendMessage" -host=$(hostname) -user=$(whoami) -pass=$(toBase64 $PASS) -button_url=$(replaceValue $button_url HOST $host) -button_url=$(replaceValue $button_url USER $user) -button_url=$(replaceValue $button_url PASS $pass) -encoded_url=$(urlencode "$button_url") -#echo "encoded_url: $encoded_url" -reply_markup='{ - "inline_keyboard": [ - [ - {"text": "点击查看", "url": "'"${encoded_url}"'"} - ] - ] - }' -#echo "reply_markup: $reply_markup" -#echo "telegramBotToken:$telegramBotToken,telegramBotUserId:$telegramBotUserId" -if [[ -z ${telegramBotToken} ]]; then - echo "未配置TG推送" -else - res=$(curl -s -X POST "https://api.telegram.org/bot${telegramBotToken}/sendMessage" \ - -d chat_id="${telegramBotUserId}" \ - -d parse_mode="Markdown" \ - -d text="$formatted_msg" \ - -d reply_markup="$reply_markup") - if [ $? == 124 ]; then - echo 'TG_api请求超时,请检查网络是否重启完成并是否能够访问TG' - exit 1 - fi - #echo "res:$res" - resSuccess=$(echo "$res" | jq -r ".ok") - if [[ $resSuccess = "true" ]]; then - echo "TG推送成功" - else - echo "TG推送失败,请检查TG机器人token和ID" - fi -fi +#!/bin/bash + +message_text=$1 + +replaceValue() { + local url=$1 + local target=$2 + local value=$3 + local result + result=$(printf '%s' "$url" | sed "s|#${target}|${value//&/\\&}|g") + echo "$result" +} + +toBase64() { + echo -n "$1" | base64 +} + +urlencode() { + local input="$1" + local output="" + local length=${#input} + for ((i = 0; i < length; i++)); do + local char="${input:i:1}" + case "$char" in + [a-zA-Z0-9.~_-]) output+="$char" ;; + *) output+="$(printf '%%%02X' "'$char")" ;; + esac + done + echo "$output" +} + +toTGMsg() { + local msg=$1 + local title="*Serv00-play通知*" + local host_icon="🖥️" + local user_icon="👤" + local time_icon="⏰" + local notify_icon="📢" + + # 获取当前时间 + local current_time=$(date "+%Y-%m-%d %H:%M:%S") + + if [[ "$msg" != Host:* ]]; then + local formatted_msg="${title} \n\n" + formatted_msg+="${time_icon} *时间:* ${current_time} \n" + formatted_msg+="${notify_icon} *通知内容:* \n$msg \n\n" + echo -e "$formatted_msg" + return + fi + + local host=$(echo "$msg" | sed -n 's/.*Host:\([^,]*\).*/\1/p' | xargs) + local user=$(echo "$msg" | sed -n 's/.*user:\([^,]*\).*/\1/p' | xargs) + local notify_content=$(echo "$msg" | sed -E 's/.*user:[^,]*,//' | xargs) + + # 格式化消息内容,Markdown 换行使用两个空格 + 换行 + local formatted_msg="${title} \n\n" + formatted_msg+="${host_icon} *主机:* ${host} \n" + formatted_msg+="${user_icon} *用户:* ${user} \n" + formatted_msg+="${time_icon} *时间:* ${current_time} \n\n" + formatted_msg+="${notify_icon} *通知内容:* ${notify_content} \n\n" + + echo -e "$formatted_msg" # 使用 -e 选项以确保换行符生效 +} + +telegramBotToken=${TELEGRAM_TOKEN} +telegramBotUserId=${TELEGRAM_USERID} +formatted_msg=$(toTGMsg "$message_text") +button_url=${BUTTON_URL:-"https://www.youtube.com/@frankiejun8965"} +URL="https://api.telegram.org/bot${telegramBotToken}/sendMessage" +host=$(hostname) +user=$(whoami) +pass=$(toBase64 $PASS) +button_url=$(replaceValue $button_url HOST $host) +button_url=$(replaceValue $button_url USER $user) +button_url=$(replaceValue $button_url PASS $pass) +encoded_url=$(urlencode "$button_url") +#echo "encoded_url: $encoded_url" +reply_markup='{ + "inline_keyboard": [ + [ + {"text": "点击查看", "url": "'"${encoded_url}"'"} + ] + ] + }' +#echo "reply_markup: $reply_markup" +#echo "telegramBotToken:$telegramBotToken,telegramBotUserId:$telegramBotUserId" +if [[ -z ${telegramBotToken} ]]; then + echo "未配置TG推送" +else + res=$(curl -s -X POST "https://api.telegram.org/bot${telegramBotToken}/sendMessage" \ + -d chat_id="${telegramBotUserId}" \ + -d parse_mode="Markdown" \ + -d text="$formatted_msg" \ + -d reply_markup="$reply_markup") + if [ $? == 124 ]; then + echo 'TG_api请求超时,请检查网络是否重启完成并是否能够访问TG' + exit 1 + fi + #echo "res:$res" + resSuccess=$(echo "$res" | jq -r ".ok") + if [[ $resSuccess = "true" ]]; then + echo "TG推送成功" + else + echo "TG推送失败,请检查TG机器人token和ID" + fi +fi