-
Notifications
You must be signed in to change notification settings - Fork 473
/
recover_vless.py
62 lines (51 loc) · 2.23 KB
/
recover_vless.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import json
import subprocess
import requests
def send_telegram_message(token, chat_id, message):
telegram_url = f"https://api.telegram.org/bot{token}/sendMessage"
telegram_payload = {
"chat_id": chat_id,
"text": message,
"reply_markup": '{"inline_keyboard":[[{"text":"问题反馈❓","url":"https://t.me/yxjsjl"}]]}'
}
response = requests.post(telegram_url, json=telegram_payload)
print(f"Telegram 请求状态码:{response.status_code}")
print(f"Telegram 请求返回内容:{response.text}")
if response.status_code != 200:
print("发送 Telegram 消息失败")
else:
print("发送 Telegram 消息成功")
# 从环境变量中获取密钥
accounts_json = os.getenv('ACCOUNTS_JSON')
telegram_token = os.getenv('TELEGRAM_TOKEN')
telegram_chat_id = os.getenv('TELEGRAM_CHAT_ID')
# 检查并解析 JSON 字符串
try:
servers = json.loads(accounts_json)
except json.JSONDecodeError:
error_message = "ACCOUNTS_JSON 参数格式错误"
print(error_message)
send_telegram_message(telegram_token, telegram_chat_id, error_message)
exit(1)
# 初始化汇总消息
summary_message = "serv00-vless 恢复操作结果:\n"
# 默认恢复命令
default_restore_command = "cd ~/domains/$USER.serv00.net/vless && ./check_vless.sh"
# 遍历服务器列表并执行恢复操作
for server in servers:
host = server['host']
port = server['port']
username = server['username']
password = server['password']
cron_command = server.get('cron', default_restore_command)
print(f"连接到 {host}...")
# 执行恢复命令(这里假设使用 SSH 连接和密码认证)
restore_command = f"sshpass -p '{password}' ssh -o StrictHostKeyChecking=no -p {port} {username}@{host} '{cron_command}'"
try:
output = subprocess.check_output(restore_command, shell=True, stderr=subprocess.STDOUT)
summary_message += f"\n成功恢复 {host} 上的 vless 服务:\n{output.decode('utf-8')}"
except subprocess.CalledProcessError as e:
summary_message += f"\n无法恢复 {host} 上的 vless 服务:\n{e.output.decode('utf-8')}"
# 发送汇总消息到 Telegram
send_telegram_message(telegram_token, telegram_chat_id, summary_message)