-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathapp.py
More file actions
594 lines (520 loc) · 22.1 KB
/
Copy pathapp.py
File metadata and controls
594 lines (520 loc) · 22.1 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os, re, sys, time, json, requests, subprocess
import urllib.request, urllib.parse, urllib.error
from datetime import datetime
from seleniumbase import SB
# 环境变量配置(可以直接私库在双引号里填写)
EMAIL = os.environ.get("EMAIL") or "" # 邮箱,只用于通知使用,可随意填写
SESSION_TOKEN = os.environ.get("SESSION_TOKEN") or "" # session token,默认登录方式,非必须
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN") or "" # Discord Token 备用登录方式, 失败时才使用,必须填写
GH_TOKEN = os.environ.get("GH_TOKEN") or "" # GitHub PAT token,用于自动更新session token,可选
TG_CHAT_ID = os.environ.get("TG_CHAT_ID") or "" # TG chat id,不填写不通知,需和bot token一起填写生效
TG_BOT_TOKEN = os.environ.get("TG_BOT_TOKEN") or "" # TG bot token
# 解析 DISCORD_TOKEN
DC_TOKEN = ""
if DISCORD_TOKEN:
_parts = DISCORD_TOKEN.split(",", 1)
DC_TOKEN = _parts[-1].strip()
if not SESSION_TOKEN and not DC_TOKEN:
print("ℹ️ 未配置 SESSION_TOKEN 和 DISCORD_TOKEN,脚本终止。")
sys.exit(1)
# 构造cookie
COOKIES = {
"session_token": SESSION_TOKEN,
"login": "true",
"theme": "system",
}
# 记录本次登录方式(用于通知)
_LOGIN_METHOD = "SESSION_TOKEN"
# 获取cookie到期时间
def get_cookie_info(sb, name):
cookies = sb.get_cookies()
for c in cookies:
if c.get('name') == name:
value = c.get('value')
expiry_ts = c.get('expiry')
expiry_dt = datetime.fromtimestamp(expiry_ts) if expiry_ts else None
return value, expiry_dt
return None, None
# 检查是否需要更新cookie
def should_update_cookie(new_value, old_value, expiry_dt, days_threshold=3):
if new_value is None:
return False
if new_value != old_value:
return True
if expiry_dt:
remaining = (expiry_dt - datetime.now()).total_seconds()
if remaining < days_threshold * 24 * 3600:
return True
return False
# 更新cookie到secrets
def update_github_secret(secret_name, new_value):
if not new_value:
print(f"⚠️ 跳过更新 {secret_name}:新值为空")
return False
masked = new_value[:4] + "..." + new_value[-4:] if len(new_value) > 8 else "***"
print(f"🔄 更新 Secret: {secret_name} (新值: {masked})")
try:
env = os.environ.copy()
if GH_TOKEN:
env["GH_TOKEN"] = GH_TOKEN
proc = subprocess.run(
["gh", "secret", "set", secret_name, "--body", new_value],
capture_output=True, text=True, timeout=30, check=False,
env=env
)
if proc.returncode == 0:
return True
else:
print(f"❌ 更新失败: {proc.stderr.strip()}")
return False
except Exception as e:
print(f"❌ 异常: {e}")
return False
# 发送tg通知
def send_telegram_message(message: str):
if not TG_BOT_TOKEN or not TG_CHAT_ID:
print("⚠️ Telegram 未配置,跳过通知")
return
url = f"https://api.telegram.org/bot{TG_BOT_TOKEN}/sendMessage"
try:
requests.post(url, json={"chat_id": TG_CHAT_ID, "text": message}, timeout=10)
print("✅ Telegram 通知已发送")
except Exception as e:
print(f"❌ Telegram 发送失败: {e}")
# 通知格式
def format_notification(status: str, extra: str = "", error: str = "", expiry_date: str = "") -> str:
local_time = time.gmtime(time.time() + 8 * 3600)
now = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
if '@' in EMAIL:
name, domain = EMAIL.split('@', 1)
if len(name) > 4:
masked_email = f"{name[:2]}****{name[-2:]}@{domain}"
else:
masked_email = f"{name}@{domain}"
else:
masked_email = EMAIL[:2] + '****'
lines = [
"🇫🇮 Bot-hosting 续期通知",
"",
f"{status}",
f"👤 登录账户: {masked_email}",
]
if _LOGIN_METHOD != "SESSION_TOKEN":
lines.append(f"🔐 登录方式: {_LOGIN_METHOD}")
if expiry_date:
lines.append(f"📅 到期时间: {expiry_date}")
if extra:
lines.append(extra)
if error:
lines.append(f"⚠️ 错误信息: {error}")
lines.append(f"⏱️ 登录时间: {now}")
return "\n".join(lines)
# 等待Turnstile验证通过
def wait_for_turnstile_pass(sb, timeout=30):
start = time.time()
cf_indicators = ["verify you are human", "确认您是真人", "troubleshoot", "just a moment"]
while time.time() - start < timeout:
page_lower = sb.get_page_source().lower()
if not any(x in page_lower for x in cf_indicators):
print("✅ Turnstile 验证已通过")
# sb.save_screenshot("turnstile_passed.png")
return True
sb.sleep(1)
print("❌ Turnstile 验证超时未通过")
return False
# 获取当前出口ip
def get_current_ip(proxy_server: str = "") -> str:
proxies = None
if proxy_server:
proxies = {"http": proxy_server, "https": proxy_server}
response = requests.get("https://api.ip.sb/ip", proxies=proxies, timeout=15)
response.raise_for_status()
return response.text.strip()
# 时间格式化
def format_countdown(countdown_str: str) -> str:
try:
h, m, _ = countdown_str.split(':')
h = int(h)
m = int(m)
if h > 0:
return f"{h}h{m}min"
else:
return f"{m}min"
except:
return countdown_str
# 获取过期日期
def extract_expiry_date(page_source: str) -> str:
patterns = [
r"[Ee]xpires\s*[:\-]?\s*(\d{4}/\d{2}/\d{2})", # Expires 2026/07/07
r"[Ee]xpires\s*[:\-]?\s*(\d{2}/\d{2}/\d{4})", # Expires 07/07/2026 (MM/DD/YYYY)
r"(\d{4}/\d{2}/\d{2})\s*[\-–]\s*renew", # 2026/07/07 - renew
r"(\d{2}/\d{2}/\d{4})\s*[\-–]\s*renew", # 07/07/2026 - renew
r"(\d{4}/\d{2}/\d{2})\s*[\-–]\s*renew manually to extend for 4 days", # 2026/07/07 - renew manually to extend for 4 days
]
for pattern in patterns:
match = re.search(pattern, page_source)
if match:
date_str = match.group(1)
# 如果是 MM/DD/YYYY 格式,转换为 YYYY/MM/DD
if len(date_str.split('/')[-1]) == 4: # 年份长度4
parts = date_str.split('/')
if len(parts[0]) == 2: # 第一部分是2位(月)
# 修正:将 MM/DD/YYYY 转为 YYYY/MM/DD
return f"{parts[2]}/{parts[0]}/{parts[1]}"
return date_str
return None
# Discord OAuth 登录(SESSION_TOKEN 失效时的备用方案)
DISCORD_CLIENT_ID = "884382422530158623"
OAUTH_REDIRECT_URI = "https://bot-hosting.net/login"
OAUTH_SCOPE = "identify email guilds"
DISCORD_API = "https://discord.com/api/v9/oauth2/authorize"
DISCORD_UA = (
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36"
)
STATE_RE = re.compile(r"[?&]state=([^&]+)")
def capture_discord_state(sb) -> str:
"""打开 /login/discord,从落地页 URL 里提取本次会话的 state"""
print("🔎 获取 Discord OAuth state...")
sb.uc_open_with_reconnect("https://bot-hosting.net/login/discord", reconnect_time=4)
time.sleep(2)
url = sb.get_current_url()
if "discord.com" not in url:
print(f"⚠️ 未跳转到 Discord 相关页面,当前 URL:{url}")
return ""
m = STATE_RE.search(url)
if not m:
print(f"❌ 未能从 URL 中解析出 state,当前 URL:{url}")
return ""
state = urllib.parse.unquote(m.group(1))
print(f"✅ 已捕获 state(当前落地页:{urllib.parse.urlparse(url).path})")
return state
def discord_authorize(state: str) -> str:
"""用 DC_TOKEN 直接完成 Discord 侧授权,返回跳转回 bot-hosting.net 的 location"""
query = urllib.parse.urlencode({
"client_id": DISCORD_CLIENT_ID,
"response_type": "code",
"redirect_uri": OAUTH_REDIRECT_URI,
"scope": OAUTH_SCOPE,
"state": state,
})
authorize_url = f"{DISCORD_API}?{query}"
referer = (
"https://discord.com/oauth2/authorize?" +
urllib.parse.urlencode({
"client_id": DISCORD_CLIENT_ID,
"redirect_uri": OAUTH_REDIRECT_URI,
"response_type": "code",
"scope": OAUTH_SCOPE,
"state": state,
})
)
headers = {
"accept": "*/*",
"authorization": DC_TOKEN,
"content-type": "application/json",
"origin": "https://discord.com",
"referer": referer,
"user-agent": DISCORD_UA,
"x-discord-locale": "zh-CN",
}
body = json.dumps({
"permissions": "0",
"authorize": True,
"integration_type": 0,
"location_context": {
"guild_id": "10000",
"channel_id": "10000",
"channel_type": 10000,
},
})
# 如果配置了代理,Discord API 请求也走代理
proxies = None
_is_proxy = os.environ.get("IS_PROXY", "false").lower() == "true"
_proxy_server = os.environ.get("PROXY_SERVER", "").strip() or "http://127.0.0.1:1080"
if _is_proxy:
proxies = {"http": _proxy_server, "https": _proxy_server}
try:
resp = requests.post(authorize_url, headers=headers, data=body, proxies=proxies, timeout=20)
if resp.status_code != 200:
print(f"❌ Discord OAuth2 授权失败: HTTP {resp.status_code} - {resp.text[:300]}")
return ""
resp_data = resp.json()
except Exception as e:
print(f"❌ Discord OAuth2 授权异常: {e}")
return ""
location = resp_data.get("location", "")
if not location:
print(f"❌ 授权响应中未找到 location 字段: {resp_data}")
return ""
masked = re.sub(r"code=[^&]+", "code=***", location)
print(f"✅ 拿到回调 URL: {masked}")
return location
def do_discord_login(sb) -> bool:
"""通过 Discord Token 走完整 OAuth 流程登录 bot-hosting.net"""
print("\n🔑 通过 Discord Token 登录...")
state = capture_discord_state(sb)
if not state:
sb.save_screenshot("login_no_state.png")
return False
location = discord_authorize(state)
if not location:
return False
print("↩️ 携带授权码打开回调链接...")
sb.uc_open_with_reconnect(location, reconnect_time=4)
time.sleep(3)
url = sb.get_current_url()
if "/error/banned" in url:
print("🚫 账号已被封禁")
sb.save_screenshot("login_banned.png")
return False
if "bot-hosting.net" not in url:
print(f"❌ 回调后未跳转至 bot-hosting.net,当前 URL:{url}")
sb.save_screenshot("login_no_redirect.png")
return False
try:
body_text = sb.get_text("body")
except Exception:
body_text = ""
if "fraud" in body_text.lower():
print("🚫 触发风控(fraud attempt),可能是 IP 被拦截")
sb.save_screenshot("login_fraud.png")
return False
for _ in range(30):
url = sb.get_current_url()
path = urllib.parse.urlparse(url).path
if "bot-hosting.net" in url and path != "/login" and not path.startswith("/login/discord"):
print(f"✅ Discord OAuth 登录成功!当前页面:{url}")
return True
time.sleep(0.5)
print(f"❌ 登录超时或未跳转成功,最终停留在:{url}")
try:
body_text = sb.get_text("body")
print(f"📄 页面正文片段:{body_text[:200].strip()!r}")
except Exception:
pass
sb.save_screenshot("login_timeout.png")
return False
# 主流程
def main():
print("#" * 25)
print(" Bot-hosting 自动续期")
print("#" * 25)
IS_PROXY = os.environ.get("IS_PROXY", "false").lower() == "true"
PROXY_SERVER = os.environ.get("PROXY_SERVER", "").strip() or "http://127.0.0.1:1080"
HEADLESS = os.environ.get("HEADLESS", "false").lower() == "true"
sb_kwargs = {"uc": True, "headless": HEADLESS}
if IS_PROXY:
print(f"🔗 挂载代理: {PROXY_SERVER}")
sb_kwargs["proxy"] = PROXY_SERVER
else:
print("🍭 未使用代理,直连访问")
global _LOGIN_METHOD
with SB(**sb_kwargs) as sb:
try:
ip = get_current_ip(PROXY_SERVER if IS_PROXY else "")
print(f"📍 当前出口IP: {ip}")
except Exception as e:
print(f"⚠️ 获取出口 IP 失败: {e}")
login_ok = False
# 方式1: SESSION_TOKEN Cookie 登录(默认)
if SESSION_TOKEN:
print("🚀 启动浏览器...")
sb.open("https://bot-hosting.net/")
sb.wait_for_ready_state_complete()
sb.sleep(2)
print("📝 注入 Cookie...")
for name, value in COOKIES.items():
if value:
sb.add_cookie({"name": name, "value": value, "domain": "bot-hosting.net"})
print("🌐 访问 https://bot-hosting.net/a/billings ...")
sb.open("https://bot-hosting.net/a/billings")
sb.wait_for_ready_state_complete()
sb.sleep(3)
current_url = sb.get_current_url()
current_title = sb.get_title()
print(f"📝 当前URL: {current_url}, Title: {current_title}")
if "/a/billings" in current_url and "/login" not in current_url and "error=" not in current_url:
login_ok = True
print("✅ SESSION_TOKEN 登录成功, 当前已到达账单页")
else:
print(f"❌ SESSION_TOKEN 登录失败,当前URL: {current_url}, 当前标题: {current_title}")
# 方式2: Discord OAuth 登录(备用)
if not login_ok and DC_TOKEN:
_LOGIN_METHOD = "Discord Token"
print("\n🔄 SESSION_TOKEN 登录失败或未配置,尝试 Discord OAuth 登录...")
if do_discord_login(sb):
print("🌐 访问 https://bot-hosting.net/a/billings ...")
sb.open("https://bot-hosting.net/a/billings")
sb.wait_for_ready_state_complete()
sb.sleep(3)
current_url = sb.get_current_url()
current_title = sb.get_title()
print(f"📝 当前URL: {current_url}, Title: {current_title}")
if "a/billings" in current_url:
login_ok = True
print("✅ Discord OAuth 登录成功,当前已到达账单页")
else:
print(f"❌ Discord OAuth 登录后仍未到达账单页,当前URL: {current_url}")
else:
print("❌ Discord OAuth 登录失败")
if not login_ok:
error_msg = "Cookie 已失效或页面异常"
if not SESSION_TOKEN and DC_TOKEN:
error_msg = "Discord OAuth 登录失败"
elif SESSION_TOKEN and DC_TOKEN:
error_msg = "SESSION_TOKEN 和 Discord OAuth 均失败"
send_telegram_message(format_notification("❌ 登录失败", error=error_msg))
return
if _LOGIN_METHOD == "Discord Token":
print("ℹ️ 本次使用 Discord OAuth 登录,新的 SESSION_TOKEN 将自动更新到 Secrets")
# 提取当前到期日期
sb.sleep(2)
page_source = sb.get_page_source()
current_expiry = extract_expiry_date(page_source)
if current_expiry:
print(f"📅 当前到期日期: {current_expiry}")
else:
print("⚠️ 未能提取当前到期日期")
# 寻找外部续期按钮
outer_renew_selector = None
countdown_text = None
possible_selectors = [
'button:contains("Renew")',
'button:contains("Renew free plan")',
'a:contains("Renew")',
'[class*="renew"]',
'[class*="Renew"]',
]
for selector in possible_selectors:
try:
if sb.is_element_visible(selector):
button_text = sb.get_text(selector)
if "Renew in" in button_text:
match = re.search(r"Renew in (\d{2}:\d{2}:\d{2})", button_text)
if match:
countdown_text = match.group(1)
break
elif "Renew" in button_text and "in" not in button_text.lower():
outer_renew_selector = selector
print(f"✅ 续期按钮可用: '{button_text}'")
break
except Exception as e:
pass
# 点击外部续期按钮等待弹窗
if outer_renew_selector:
print("🔄 点击外部续期按钮,等待验证窗口...")
try:
sb.sleep(2)
sb.click(outer_renew_selector)
sb.sleep(15) # 等待模态框加载,可能因网络因素加载慢
except Exception as e:
print(f"❌ 点击外部按钮失败: {e}")
send_telegram_message(format_notification("❌ 续期失败", error="点击外部续期按钮出错"))
return
# 处理弹窗中的 Turnstile
print("🔒 检测弹窗中的 Turnstile 验证...")
turnstile_passed = False
for attempt in range(1, 4):
try:
sb.uc_gui_click_captcha()
time.sleep(12)
except Exception as e:
print(f"⚠️ 点击 Turnstile 出错: {e}")
if wait_for_turnstile_pass(sb, timeout=20):
turnstile_passed = True
break
else:
print(f"⏳ 第 {attempt} 次未通过,重试点击...")
if not turnstile_passed:
print("❌ Turnstile 验证最终未通过,脚本退出")
send_telegram_message(format_notification("❌ 续期失败", error="Turnstile 验证未通过"))
return
# 点击续期按钮
print("⏳ 等待续期按钮可用并点击...")
time.sleep(5)
modal_button_clicked = False
try:
sb.click('button:contains("Renew for 4 days")', timeout=8)
modal_button_clicked = True
print("✅ 已点击续期按钮")
except Exception as e:
print(f"续期按钮点击失败: {e}")
print("⏳ 等待新的过期时间...")
sb.sleep(6)
# 提取新的到期日期和倒计时
new_page_text = sb.get_page_source()
new_expiry = extract_expiry_date(new_page_text)
new_match = re.search(r"Renew in (\d{2}:\d{2}:\d{2})", new_page_text)
if new_match:
new_countdown = new_match.group(1)
print(f"✅ 续期成功!新的倒计时: {new_countdown}")
if new_expiry:
print(f"📅 新的到期日期: {new_expiry}")
send_telegram_message(
format_notification(
"✅ 续期成功",
extra=f"⏱️ 可续期时间: {format_countdown(new_countdown)}后",
expiry_date=new_expiry or "(未获取到)"
)
)
else:
if new_expiry and new_expiry != current_expiry:
print(f"✅ 续期成功,到期日期已更新为: {new_expiry}")
send_telegram_message(
format_notification(
"✅ 续期成功",
extra="到期日期已更新",
expiry_date=new_expiry
)
)
else:
print("⚠️ 续期结果未知,到期日期未变化,请手动检查")
send_telegram_message(
format_notification(
"⚠️ 续期可能未成功",
extra="请登录后台检查",
expiry_date=current_expiry or "(未获取到)"
)
)
else:
if countdown_text:
friendly = format_countdown(countdown_text)
print(f"⏳ 未到续期时间,倒计时: {countdown_text} ({friendly})")
send_telegram_message(
format_notification(
"⏳ 未到续期时间",
extra=f"⏱️ 可续期时间: {friendly}后",
expiry_date=current_expiry or "(未获取到)"
)
)
else:
print("ℹ️ 未找到续期按钮或倒计时,状态未知")
send_telegram_message(
format_notification(
"ℹ️ 无需续期",
extra="当前状态未知,请手动检查",
expiry_date=current_expiry or "(未获取到)"
)
)
# 更新SESSION_TOKEN
print("🔄 检查 SESSION_TOKEN 是否需要更新")
new_token, token_expiry = get_cookie_info(sb, "session_token")
old_token = SESSION_TOKEN
if should_update_cookie(new_token, old_token, token_expiry):
print("🔄 SESSION_TOKEN 需要更新")
if GH_TOKEN:
if update_github_secret("SESSION_TOKEN", new_token):
print("✅ SESSION_TOKEN 更新成功")
else:
print("⚠️ 更新失败,请检查 GH_TOKEN 权限")
else:
print("⚠️ 未设置 GH_TOKEN,无法自动更新")
print(f"📋 请手动设置 SESSION_TOKEN = {new_token[:4]}...{new_token[-4:]}")
else:
print("✅ SESSION_TOKEN 无需更新")
print("🏁 脚本执行完毕")
if __name__ == "__main__":
main()