tls_plugin.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. import time
  2. import json
  3. import random
  4. import re
  5. import os
  6. import uuid
  7. from urllib.parse import urlparse
  8. from datetime import datetime
  9. from typing import List, Dict, Optional, Any, Callable
  10. from DrissionPage import ChromiumPage, ChromiumOptions
  11. from plugin import IPlugin
  12. from core_types import (
  13. PluginConfig,
  14. AppointmentType,
  15. QueryResult,
  16. BookResult,
  17. AvailabilityStatus,
  18. TimeSlot,
  19. DateAvailability,
  20. NotFoundError,
  21. PermissionDeniedError,
  22. RateLimiteddError,
  23. SessionExpiredOrInvalidError,
  24. BizLogicError,
  25. PluginStatus,
  26. utc_now_str
  27. )
  28. from utils.cloudflare_bypass_for_scraping import CloudflareBypasser
  29. from utils.mouse import HumanMouse, MOUSE_PROFILES
  30. from utils.keyboard import HumanKeyboard
  31. from utils.flow_engine import BaseFlow, on, guard, done, fatal
  32. from utils.plugin_utils import (
  33. BrowserResponse,
  34. init_chromium_options,
  35. perform_browser_fetch,
  36. save_screenshot,
  37. filter_dates_in_range,
  38. solve_capsolver_recaptcha,
  39. handle_cookie_consent,
  40. cleanup_browser_workspace
  41. )
  42. class TlsLoginFlow(BaseFlow):
  43. """
  44. 基于 BaseFlow 注解状态机驱动的 TLSContact 会话创建与登录流
  45. """
  46. def __init__(self, page, plugin: 'TlsPlugin', interval: float = 1.0):
  47. super().__init__(
  48. page=page,
  49. config=plugin.config,
  50. context={"plugin": plugin},
  51. interval=interval,
  52. logger=plugin._log,
  53. cf_bypasser=CloudflareBypasser(page, log=plugin.config.debug if plugin.config else False)
  54. )
  55. self.plugin = plugin
  56. self.has_submitted_login = False
  57. self.has_cleaned_residual_session = False
  58. self.cf_block_refreshed = False
  59. self.other_langs = ['fr-fr', 'ar-ar', 'cb-ph', 'id-id', 'km-kh', 'mk-mk', 'ru-ru', 'sq-al', 'th-th', 'tl-ph', 'uz-uz', 'vi-vn', 'zh-cn', 'hy-am', 'zh-hk']
  60. # --- 异常守卫与自愈 (Priority 5~9) ---
  61. @guard(text="502 Bad Gateway", priority=5)
  62. @guard(text="503 Service Temporarily Unavailable", priority=5)
  63. @guard(text="504 Gateway Time-out", priority=5)
  64. def on_server_network_error(self):
  65. self.log("[WARN] 检测到服务器网络错误 (502/503/504),等待 2 秒后刷新重试...", level="warning")
  66. time.sleep(2)
  67. self.page.refresh()
  68. try:
  69. self.page.wait.load_start(timeout=5)
  70. except Exception:
  71. pass
  72. @guard(ele="xpath://p[contains(text(), 'It looks like something went wrong')]", priority=5)
  73. @guard(ele="xpath://div[contains(text(), 'Error code:')]", priority=5)
  74. @guard(text="a client-side exception has occurred", priority=5)
  75. def on_spa_render_error(self):
  76. self.log("[WARN] 检测到前端 SPA 渲染或应用异常页面,尝试刷新自愈...", level="warning")
  77. time.sleep(2)
  78. self.page.refresh()
  79. try:
  80. self.page.wait.load_start(timeout=5)
  81. except Exception:
  82. pass
  83. @guard(text="Sorry, you have been blocked", priority=6)
  84. @guard(text="You are being rate limited", priority=6)
  85. @guard(text="Cloudflare Ray ID", priority=6)
  86. def on_cloudflare_blocked(self):
  87. if not self.cf_block_refreshed:
  88. self.cf_block_refreshed = True
  89. self.log("[WARN] 🛡️ 检测到 Cloudflare 阻断或速率限制页面,尝试刷新自愈 (1/1)...", level="warning")
  90. time.sleep(2)
  91. self.page.refresh()
  92. try:
  93. self.page.wait.load_start(timeout=5)
  94. except Exception:
  95. pass
  96. return
  97. raise BizLogicError("Blocked by Cloudflare WAF. Need to change IP or browser fingerprint.")
  98. @guard(title="just a moment", priority=7)
  99. @guard(title="请稍候", priority=7)
  100. def on_cloudflare_turnstile_challenge(self):
  101. self.log("🛡️ 检测到 Cloudflare 5秒盾挑战,正在尝试穿透...")
  102. self.cf_bypasser.bypass(max_retry=3)
  103. time.sleep(2)
  104. @guard(title="waitting room", priority=7)
  105. def on_cloudflare_waiting_room(self):
  106. self.log("🛡️ 检测到 Cloudflare 等候室,正在等待自动排队刷新...")
  107. self.cf_bypasser.handle_waiting_room()
  108. time.sleep(2)
  109. @guard(text="You have not yet added an applicant", priority=8)
  110. @guard(text="Add a new applicant", priority=8)
  111. @guard(url="applicants-information", priority=8)
  112. @fatal("No applicant added. Cannot proceed to booking.")
  113. def on_no_applicant_error(self):
  114. pass
  115. @guard(url="login-actions", text="Invalid username or password", priority=9)
  116. @guard(text="Invalid username or password", priority=9)
  117. @guard(text="Please solve the captcha", priority=9)
  118. @guard(text="reCAPTCHA validation failed", priority=9)
  119. @fatal("Login Failed! Invalid credentials or Captcha rejected.")
  120. def on_login_credentials_invalid(self):
  121. pass
  122. # --- 状态流转与表单处理 (Priority 15~55) ---
  123. @on(
  124. predicate=lambda flow: any(lang in flow.page.url for lang in flow.other_langs),
  125. priority=15
  126. )
  127. def handle_language_redirect(self):
  128. """如果 URL 语言非英语,自动切回 en-us"""
  129. curr_url = self.page.url
  130. matched_lang = next((lang for lang in self.other_langs if lang in curr_url), None)
  131. if matched_lang:
  132. target_url = curr_url.replace(matched_lang, 'en-us')
  133. self.log(f"Detected non-English locale '{matched_lang}'. Redirecting to: {target_url}")
  134. self.page.get(target_url)
  135. try:
  136. self.page.wait.load_start(timeout=3)
  137. except Exception:
  138. pass
  139. @on(
  140. predicate=lambda flow: (
  141. not getattr(flow, 'has_submitted_login', False)
  142. and not getattr(flow, 'has_cleaned_residual_session', False)
  143. and bool(flow.page.ele("tag:svg@@data-testid=user-button", timeout=0.2))
  144. ),
  145. priority=20
  146. )
  147. def handle_already_login_state(self):
  148. self.log("Detected active session from previous run (prior to login). Logging out first to clean state...")
  149. user_btn = self.page.ele("tag:svg@@data-testid=user-button")
  150. if user_btn:
  151. self.plugin.mouse.human_click_ele(user_btn)
  152. time.sleep(1.5)
  153. logout_btn = self.page.ele("#logout")
  154. if logout_btn:
  155. self.plugin.mouse.human_click_ele(logout_btn)
  156. self.has_cleaned_residual_session = True
  157. tls_url = self.plugin.free_config.get('tls_url', '')
  158. if tls_url:
  159. self.page.get(tls_url)
  160. try:
  161. self.page.wait.load_start(timeout=3)
  162. except Exception:
  163. pass
  164. @on(url="/workflow/application-summary", ele="tag:button@@text():Change", priority=30)
  165. def handle_application_summary_change_flow(self):
  166. self.log("On application-summary page. Clicking 'Change' to enter slot booking...")
  167. change_btn = self.page.ele('tag:button@@text():Change')
  168. if change_btn:
  169. change_btn.click(by_js=True)
  170. time.sleep(0.5)
  171. confirm_btn = self.page.ele('tag:button@@text():Yes')
  172. if confirm_btn:
  173. confirm_btn.click(by_js=True)
  174. try:
  175. self.page.wait.load_start(timeout=5)
  176. except Exception:
  177. pass
  178. @on(url="travel-groups", priority=40)
  179. def handle_travel_groups(self):
  180. self.log("State: Travel Groups. Parsing and selecting targeted group...")
  181. location = self.plugin.free_config.get('location', '')
  182. # 1. 如果页面有城市 Tab,确保选中目标城市 (例如 Shanghai)
  183. if location:
  184. tab_ele = self.page.ele(f'xpath://div[contains(@class, "TlsTab")]//p[contains(text(), "{location}")]') or \
  185. self.page.ele(f'tag:p@@text():{location}')
  186. if tab_ele:
  187. parent_tab = tab_ele.parent('tag:div')
  188. if parent_tab and 'TlsTab_--selected' not in (parent_tab.attr('class') or ''):
  189. self.log(f"Switching to VAC Tab: {location}...")
  190. self.plugin.mouse.human_click_ele(tab_ele)
  191. time.sleep(1.5)
  192. # 2. 解析旅行团组
  193. current_html = self.page.html
  194. groups = self.plugin._parse_travel_groups(current_html)
  195. self.log(f"Parsed {len(groups)} travel group(s): {groups}")
  196. # 3. 匹配目标团组
  197. target_group = None
  198. if location:
  199. target_group = next((g for g in groups if g.get('location') and location.lower() in g['location'].lower() and g.get('submitted')), None)
  200. if not target_group:
  201. target_group = next((g for g in groups if g.get('submitted')), None)
  202. if not target_group and groups:
  203. target_group = groups[0]
  204. self.plugin.travel_group = target_group
  205. if not self.plugin.travel_group:
  206. save_screenshot(self.page, self.plugin.instance_id, "group_not_found", logger=self.log)
  207. raise NotFoundError(f"Group not found for {location}")
  208. formgroup_id = self.plugin.travel_group.get('group_number')
  209. self.log(f"Target travel group selected: {formgroup_id} (submitted={self.plugin.travel_group.get('submitted')})")
  210. # 4. 点击 Select 按钮
  211. btn_selector = f'tag:button@@name=formGroupId@@value={formgroup_id}'
  212. if not self.page.ele(btn_selector):
  213. btn_selector = 'tag:button@@name=formGroupId'
  214. if self.page.wait.eles_loaded(btn_selector, timeout=10):
  215. buttons = self.page.eles(btn_selector)
  216. select_btn = next((btn for btn in reversed(buttons) if btn.rect.size[0] > 0 and btn.rect.size[1] > 0), None)
  217. if not select_btn and buttons:
  218. select_btn = buttons[0]
  219. if select_btn:
  220. self.log(f"Clicking Select button for group {formgroup_id}...")
  221. time.sleep(random.uniform(0.5, 1.0))
  222. self.plugin.mouse.human_click_ele(select_btn)
  223. try:
  224. self.page.wait.load_start(timeout=5)
  225. except Exception:
  226. pass
  227. else:
  228. self.log("[WARN] Select button found but not visible.")
  229. else:
  230. self.log(f"[WARN] Wait timeout for group button {formgroup_id}")
  231. @on(ele="#book-appointment-btn", priority=45)
  232. def handle_intermediate_book_appointment_btn(self):
  233. self.log("State: Intermediate Dashboard. Clicking '#book-appointment-btn'...")
  234. self.plugin.mouse.human_click_ele(self.page.ele('#book-appointment-btn'))
  235. try:
  236. self.page.wait.load_start(timeout=3)
  237. except Exception:
  238. pass
  239. @on(ele="tag:label@@text():Email", priority=50)
  240. def handle_login_form(self):
  241. if getattr(self, "is_submitting_login", False):
  242. return
  243. self.login_attempts = getattr(self, "login_attempts", 0) + 1
  244. if self.login_attempts > 3:
  245. raise BizLogicError("Login submission failed: remained on login page after multiple attempts.")
  246. self.log(f"State: Login Form (Attempt {self.login_attempts}). Processing credentials and ReCaptcha...")
  247. login_captcha_cfg = self.plugin.free_config.get("login_captcha", {})
  248. capsolver_key = self.plugin.free_config.get("capsolver_key") or os.getenv("CAPSOLVER_KEY", "")
  249. # 1. 求解 ReCaptcha
  250. recaptcha_token = None
  251. has_recaptcha = bool(
  252. self.page.ele('.g-recaptcha') or
  253. self.page.ele('xpath://iframe[contains(@src, "recaptcha")]') or
  254. self.page.ele('#g-recaptcha-response') or
  255. login_captcha_cfg
  256. )
  257. if has_recaptcha and capsolver_key:
  258. site_key = login_captcha_cfg.get("site_key")
  259. if not site_key and self.page.ele('xpath://iframe[contains(@src, "recaptcha")]'):
  260. try:
  261. from urllib.parse import urlparse, parse_qs
  262. rec_iframe = self.page.ele('xpath://iframe[contains(@src, "recaptcha")]')
  263. rec_iframe_src = rec_iframe.attr('src') or ""
  264. rec_parsed = urlparse(rec_iframe_src)
  265. rec_params = parse_qs(rec_parsed.query)
  266. site_key = rec_params.get("k", [None])[0]
  267. except Exception:
  268. pass
  269. if not site_key:
  270. site_key = "6LcDpXcfAAAAAM7wOEsF_38DNsL20tTvPTKxpyn0"
  271. task_type = login_captcha_cfg.get("task", "ReCaptchaV2TaskProxyLess")
  272. login_page_url = login_captcha_cfg.get("page_url") or self.page.url
  273. self.log(f"Solving ReCaptcha with Capsolver (site_key={site_key})...")
  274. recaptcha_token = solve_capsolver_recaptcha(
  275. api_token=capsolver_key,
  276. site_key=site_key,
  277. page_url=login_page_url,
  278. proxy=self.plugin.config.proxy if self.plugin.config else None,
  279. task_type=task_type
  280. )
  281. self.log(f"ReCaptcha successfully solved (token length: {len(recaptcha_token)}).")
  282. # 2. 填写账号密码
  283. username = self.plugin.config.account.username
  284. password = self.plugin.config.account.password
  285. email_input = self.page.ele('tag:label@@text():Email').next()
  286. self.plugin.mouse.human_click_ele(email_input)
  287. time.sleep(random.uniform(0.2, 0.5))
  288. self.plugin.keyboard.type_text(username, humanize=True)
  289. time.sleep(random.uniform(0.4, 0.8))
  290. pwd_input = self.page.ele('tag:label@@text():Password').next()
  291. self.plugin.mouse.human_click_ele(pwd_input)
  292. time.sleep(random.uniform(0.2, 0.5))
  293. self.plugin.keyboard.type_text(password, humanize=True)
  294. # 3. 注入 Token
  295. if recaptcha_token:
  296. self.log("Injecting ReCaptcha token via JS...")
  297. inject_js = f"""
  298. (function() {{
  299. var token = "{recaptcha_token}";
  300. var g = document.getElementById('g-recaptcha-response');
  301. if (g) {{
  302. g.value = token;
  303. g.innerHTML = token;
  304. g.dispatchEvent(new Event('input', {{ bubbles: true }}));
  305. g.dispatchEvent(new Event('change', {{ bubbles: true }}));
  306. }}
  307. var textareas = document.querySelectorAll('textarea[name="g-recaptcha-response"], [name="g-recaptcha-response"]');
  308. textareas.forEach(function(el) {{
  309. el.value = token;
  310. el.innerHTML = token;
  311. el.dispatchEvent(new Event('input', {{ bubbles: true }}));
  312. el.dispatchEvent(new Event('change', {{ bubbles: true }}));
  313. }});
  314. try {{
  315. if (typeof ___grecaptcha_cfg !== 'undefined') {{
  316. for (var cid in ___grecaptcha_cfg.clients) {{
  317. var client = ___grecaptcha_cfg.clients[cid];
  318. for (var p in client) {{
  319. if (client[p] && typeof client[p].callback === 'function') {{
  320. client[p].callback(token);
  321. }} else if (client[p] && client[p].callback && typeof window[client[p].callback] === 'function') {{
  322. window[client[p].callback](token);
  323. }}
  324. }}
  325. }}
  326. }}
  327. }} catch(e) {{}}
  328. }})();
  329. """
  330. self.page.run_js(inject_js)
  331. time.sleep(random.uniform(0.5, 1.0))
  332. # 4. 点击登录
  333. self.log("Submitting Login...")
  334. login_btn = self.page.ele('tag:button@@text():Login')
  335. if login_btn:
  336. self.plugin.mouse.human_click_ele(login_btn)
  337. self.has_submitted_login = True
  338. try:
  339. self.page.wait.url_change(self.page.url, timeout=12)
  340. except Exception:
  341. try:
  342. self.page.wait.load_start(timeout=5)
  343. except Exception:
  344. pass
  345. self.is_submitting_login = False
  346. @on(ele="tag:a@@href:login", not_ele="tag:label@@text():Email", priority=55)
  347. def handle_login_portal_link(self):
  348. self.log("State: Login Portal. Clicking login link...")
  349. login_link = self.page.ele("tag:a@@href:login")
  350. if login_link:
  351. self.plugin.mouse.human_click_ele(login_link)
  352. try:
  353. self.page.wait.load_start(timeout=3)
  354. except Exception:
  355. pass
  356. # --- 成功终态到达 (Done, Priority 10) ---
  357. @on(url="appointment-booking", priority=10)
  358. @on(ele="tag:button@text():Book your appointment", priority=10)
  359. @done
  360. def on_reached_appointment_booking(self):
  361. btn_selector = 'tag:button@text():Book your appointment'
  362. if self.page.wait.ele_displayed(btn_selector, timeout=10):
  363. handle_cookie_consent(self.page, mouse=self.plugin.mouse, logger=self.log)
  364. self.plugin.session_create_time = time.time()
  365. self.log("✅ Login & Navigation Success! Reached appointment-booking target page.")
  366. class TlsPlugin(IPlugin):
  367. """
  368. TLSContact 签证预约插件 (DrissionPage 版)
  369. """
  370. name = "tls_plugin"
  371. version = "1.0.0"
  372. description = "TLSContact欧洲多国签证预约插件 (DrissionPage拟人化与CF挑战破解)"
  373. def __init__(self, group_id: str):
  374. self.group_id = group_id
  375. self.config: Optional[PluginConfig] = None
  376. self.free_config: Dict[str, Any] = {}
  377. self.is_healthy = True
  378. self.logger = None
  379. self.mouse = None
  380. self.keyboard = None
  381. self.page: Optional[ChromiumPage] = None
  382. self.travel_group: Optional[Dict] = None
  383. self.instance_id = uuid.uuid4().hex[:8]
  384. self.root_workspace = os.path.abspath(os.path.join("data/temp_browser_data", f"{self.group_id}.{self.instance_id}"))
  385. self.user_data_path = os.path.join(self.root_workspace, "user_data")
  386. if not os.path.exists(self.root_workspace):
  387. os.makedirs(self.root_workspace)
  388. self.tunnel = None
  389. self.session_create_time: float = 0
  390. def set_log(self, logger: Callable[[str], None]):
  391. self.logger = logger
  392. def _log(self, message):
  393. if self.logger:
  394. self.logger(f'[TlsPlugin] [{self.group_id}] [{self.instance_id}] {message}')
  395. else:
  396. print(f'[TlsPlugin] [{self.group_id}] [{self.instance_id}] {message}')
  397. def set_config(self, config: PluginConfig):
  398. self.config = config
  399. self.free_config = config.free_config or {}
  400. def keep_alive(self):
  401. try:
  402. self.page.refresh()
  403. self.page.wait.load_start(timeout=2)
  404. self.page.wait.doc_loaded()
  405. time.sleep(random.uniform(1, 3))
  406. self._check_page_is_session_expired_or_invalid('Book your appointment', html = self.page.html)
  407. except SessionExpiredOrInvalidError as e:
  408. self.is_healthy = False
  409. except Exception as e:
  410. self._log(f"Unexpected error in keep_alive: {e}")
  411. def health_check(self) -> bool:
  412. if not self.is_healthy:
  413. return False
  414. if self.page is None:
  415. return False
  416. try:
  417. if not self.page.run_js("return 1;"):
  418. return False
  419. except:
  420. return False
  421. if self.config.session_max_life > 0:
  422. current_time = time.time()
  423. elapsed_time = current_time - self.session_create_time
  424. if elapsed_time > self.config.session_max_life:
  425. self._log(f"Session expired.")
  426. return False
  427. return True
  428. def create_session(self):
  429. """
  430. 全浏览器会话创建:过盾 -> JS注入登录 -> 状态机自动路由导航 -> 到达目标页
  431. """
  432. self._log(f"Initializing Session (ID: {self.instance_id})...")
  433. self._log(f"Account id={self.config.account.id}, proxy id={self.config.proxy.id if self.config and self.config.proxy else 'None'}")
  434. co, self.tunnel = init_chromium_options(
  435. user_data_path=self.user_data_path,
  436. username_seed=self.config.account.username if self.config and self.config.account else None,
  437. proxy=self.config.proxy if self.config else None,
  438. headless=False,
  439. window_size="1920,1080",
  440. logger=self._log
  441. )
  442. self.page = ChromiumPage(co)
  443. self._log("Init humanize tools...")
  444. profile_name = random.choice(list(MOUSE_PROFILES.keys()))
  445. self._log(f"[HumanMouse] current mouse profile: {profile_name}")
  446. self.mouse = HumanMouse(self.page, timing=MOUSE_PROFILES[profile_name], debug=self.config.debug)
  447. self.keyboard = HumanKeyboard(self.page)
  448. # 打开初始站点入口:先访问对应主域名建立上下文与过盾,再跳转深层目标路径
  449. tls_url = self.free_config.get('tls_url', '')
  450. if tls_url:
  451. parsed = urlparse(tls_url)
  452. domain_url = f"{parsed.scheme}://{parsed.netloc}" if (parsed.scheme and parsed.netloc) else ''
  453. # 建立主站域名上下文,避免冷启动直接跳转深层路径导致被 Cloudflare 拦截
  454. if domain_url and domain_url != tls_url:
  455. self._log(f"Establishing domain context first: {domain_url}")
  456. self.page.get(domain_url)
  457. try:
  458. self.page.wait.load_start(timeout=5)
  459. except Exception:
  460. pass
  461. # 若主站根域名触发 5秒盾挑战或等候室,先行穿透自愈
  462. cf = CloudflareBypasser(self.page, log=self.config.debug if self.config else False, logger=self._log)
  463. if not cf.is_bypassed():
  464. self._log("🛡️ Cloudflare challenge detected on domain root, bypassing...")
  465. cf.bypass(max_retry=3)
  466. cf.handle_waiting_room(max_wait_sec=30)
  467. time.sleep(random.uniform(2.0, 3.5))
  468. handle_cookie_consent(self.page)
  469. time.sleep(random.uniform(0.5, 1.5))
  470. self._log(f"Navigating to entry: {tls_url}")
  471. self.page.get(tls_url)
  472. # 启动 TlsLoginFlow 注解流状态机(首屏5秒盾、等候室、登录等全权由状态机响应式调度)
  473. login_flow = TlsLoginFlow(
  474. self.page,
  475. plugin=self,
  476. interval=1.0
  477. )
  478. login_flow.run(max_steps=20)
  479. self.session_create_time = time.time()
  480. self._log("🎉 TlsLoginFlow completed successfully. Session created.")
  481. def query(self, apt_type: AppointmentType) -> QueryResult:
  482. res = QueryResult()
  483. res.success = False
  484. slots = []
  485. self._log(f"Executing silent JS fetch...")
  486. resp = self._perform_request("GET", self.page.url, retry_count=0)
  487. self._check_page_is_session_expired_or_invalid('Book your appointment', resp.text)
  488. slots = self._parse_appointment_slots(resp.text)
  489. if slots:
  490. res.success = True
  491. earliest_date = slots[0]["date"]
  492. earliest_dt = datetime.strptime(earliest_date, "%Y-%m-%d")
  493. res.availability_status = AvailabilityStatus.Available
  494. res.earliest_date = earliest_dt
  495. date_map: dict[datetime, list[TimeSlot]] = {}
  496. for s in slots:
  497. date_str = s["date"]
  498. dt = datetime.strptime(date_str, "%Y-%m-%d")
  499. date_map.setdefault(dt, []).append(
  500. TimeSlot(time=s["time"], label=str(s.get("label", "")))
  501. )
  502. res.availability = [DateAvailability(date=d, times=slots) for d, slots in date_map.items()]
  503. self._log(f"Slot Found! earliest_date={earliest_date}, size={len(slots)}")
  504. else:
  505. self._log("No slots available.")
  506. res.success = False
  507. res.availability_status = AvailabilityStatus.NoneAvailable
  508. return res
  509. def book(self, slot_info: QueryResult, user_inputs: Dict = None) -> BookResult:
  510. res = BookResult()
  511. res.success = False
  512. exp_start = user_inputs.get('expected_start_date', '')
  513. exp_end = user_inputs.get('expected_end_date', '')
  514. support_pta = user_inputs.get('support_pta', True)
  515. target_labels = ['']
  516. if support_pta:
  517. target_labels.append('pta')
  518. available_dates_str =[
  519. da.date.strftime("%Y-%m-%d")
  520. for da in slot_info.availability if da.date
  521. ]
  522. valid_dates_list = filter_dates_in_range(available_dates_str, exp_start, exp_end, shuffle=True)
  523. if not valid_dates_list:
  524. raise NotFoundError(message="No dates match user constraints")
  525. all_possible_slots =[]
  526. for da in slot_info.availability:
  527. if not da.date:
  528. continue
  529. date_str = da.date.strftime("%Y-%m-%d")
  530. if date_str in valid_dates_list:
  531. for t in da.times:
  532. if t.label in target_labels:
  533. all_possible_slots.append({
  534. "date": date_str,
  535. "time_obj": t,
  536. "label": t.label
  537. })
  538. if not all_possible_slots:
  539. raise NotFoundError(message="No suitable slot found (after label filtering)")
  540. selected_slot = random.choice(all_possible_slots)
  541. selected_date = selected_slot["date"]
  542. selected_time = selected_slot["time_obj"]
  543. selected_label = selected_slot["label"]
  544. self._log(f"Found {len(all_possible_slots)} valid slots. selected slot: {selected_date} {selected_time.time} {selected_label}")
  545. self.page.listen.start('/workflow/appointment-booking', method='POST')
  546. js_update_form = f"""
  547. try {{
  548. const buttons = Array.from(document.querySelectorAll('button[type="submit"]'));
  549. const submitBtn = buttons.find(btn => {{
  550. return btn.textContent.trim().toLowerCase().includes('book your appointment');
  551. }});
  552. if (!submitBtn) return 'Submit button not found';
  553. const form = submitBtn.closest('form');
  554. if (!form) return 'Correct form not found';
  555. function setReactValue(input, value) {{
  556. if (!input) return;
  557. input.value = value;
  558. }}
  559. setReactValue(form.querySelector('input[name="date"]'), '{selected_date}');
  560. setReactValue(form.querySelector('input[name="time"]'), '{selected_time.time}');
  561. setReactValue(form.querySelector('input[name="appointmentLabel"]'), '{selected_label}');
  562. submitBtn.removeAttribute('disabled');
  563. submitBtn.classList.remove('opacity-50', 'cursor-not-allowed');
  564. return 'form_updated';
  565. }} catch (e) {{
  566. return e.toString();
  567. }}
  568. """
  569. update_res = self.page.run_js(js_update_form)
  570. self._log(f"Form update triggered: {update_res}")
  571. if update_res != 'form_updated':
  572. raise BizLogicError(message=f"Failed to update form: {update_res}")
  573. submit_btn = self.page.ele('tag:button@@type=submit@@text():Book your appointment')
  574. if not submit_btn:
  575. raise BizLogicError(message="Submit button not found for mouse click")
  576. self._log("Moving mouse to submit button and clicking")
  577. self.mouse.human_click_ele(submit_btn)
  578. packet = self.page.listen.wait(timeout=10)
  579. if not packet:
  580. raise BizLogicError(message='Listening data failed')
  581. self.page.listen.stop()
  582. self._log(f"URL: {packet.url}")
  583. self._log(f"POST Body: {packet.request.postData}")
  584. self._log(f"POST Resp: {packet.response.body}")
  585. self._log("Waiting for Next.js to process the form submission...")
  586. for _ in range(10):
  587. try:
  588. current_page_url = self.page.url
  589. current_page_html = self.page.html
  590. appointment_confirmation_indicators = [
  591. "order-summary" in current_page_url,
  592. "partner-services" in current_page_url,
  593. "appointment-confirmation" in current_page_url,
  594. "Change my appointment" in current_page_html,
  595. "Book a new appointment" in current_page_html,
  596. ]
  597. if any(appointment_confirmation_indicators):
  598. self._log(f"✅ BOOKING SUCCESS! Redirected to: {current_page_url}")
  599. res.success = True
  600. res.label = selected_label
  601. res.book_date = selected_date
  602. res.book_time = selected_time.time
  603. save_screenshot(self.page, self.instance_id, "book_slot_success", logger=self._log)
  604. break
  605. toast_selector = 'tag:div@role=alert'
  606. toast_ele = self.page.ele(toast_selector, timeout=0.5)
  607. if toast_ele:
  608. error_msg = toast_ele.text
  609. self._log(f"❌ BOOKING FAILED! Detected popup: {error_msg}")
  610. break
  611. time.sleep(0.5)
  612. except Exception:
  613. pass
  614. return res
  615. def _perform_request(self, method, url, headers=None, data=None, json_data=None, params=None, retry_count=0):
  616. """
  617. 在浏览器上下文中注入 JS 执行 Fetch
  618. """
  619. resp = perform_browser_fetch(self.page, method, url, headers=headers, data=data, json_data=json_data, params=params)
  620. if resp.status_code == 200:
  621. return resp
  622. elif resp.status_code == 401:
  623. self.is_healthy = False
  624. raise SessionExpiredOrInvalidError()
  625. # 将 403 和 429 合并,共享绕盾重试逻辑
  626. elif resp.status_code in (403, 429):
  627. if retry_count < 2:
  628. # 动态打印是 403 还是 429
  629. self._log(f"HTTP {resp.status_code} Detected. Cloudflare block or rate limit? Attempting refresh (Try {retry_count+1}/2)...")
  630. if self._refresh_firewall_session():
  631. self._log("Firewall session refreshed. Retrying request...")
  632. return self._perform_request(method, url, headers, data, json_data, params, retry_count+1)
  633. else:
  634. self._log("Failed to refresh firewall session.")
  635. # 如果重试 2 次仍然失败,或者刷新防火墙失败,则按原来的逻辑抛出各自的异常
  636. if resp.status_code == 403:
  637. raise PermissionDeniedError(f"HTTP 403: {resp.text[:100]}")
  638. else:
  639. self.is_healthy = False
  640. # 注意:保留了原代码中 RateLimiteddError 的拼写(双d)
  641. raise RateLimiteddError(f"HTTP 429: {resp.text[:100]}")
  642. else:
  643. if resp.status_code == 0:
  644. raise BizLogicError(f"Network Error: {resp.text}")
  645. raise BizLogicError(message=f"HTTP Error {resp.status_code}: {resp.text[:100]}")
  646. def _refresh_firewall_session(self) -> bool:
  647. """
  648. 主动刷新页面以触发 Cloudflare 挑战并尝试通过
  649. """
  650. try:
  651. self._log("Refreshing page to trigger Cloudflare...")
  652. self.page.refresh()
  653. time.sleep(5)
  654. cf = CloudflareBypasser(self.page, log=self.config.debug)
  655. success = cf.bypass(max_retry=6)
  656. if success:
  657. title = self.page.title.lower()
  658. if "access denied" in title:
  659. return False
  660. time.sleep(2)
  661. return True
  662. return False
  663. except Exception as e:
  664. self._log(f"Error during firewall refresh: {e}")
  665. return False
  666. def _parse_travel_groups(self, html_content) -> List[Dict]:
  667. groups = []
  668. # 1. 兼容新版 applications 与旧版 travelGroups 正则
  669. patterns = [
  670. r'(?:\\\\\"|\")?(?:travelGroups|applications)(?:\\\\\"|\")?:\s*(\[.*?\])\s*,\s*(?:\\\\\"|\")?availableCountriesToCreateGroups',
  671. r'\\"travelGroups\\":\s*(\[.*?\])',
  672. r'\\"applications\\":\s*(\[.*?\])',
  673. r'"applications":\s*(\[.*?\])'
  674. ]
  675. for pat in patterns:
  676. js_match = re.search(pat, html_content, re.DOTALL)
  677. if js_match:
  678. try:
  679. json_str = js_match.group(1).replace(r'\"', '"').replace(r'\\"', '"')
  680. data = json.loads(json_str)
  681. for g in data:
  682. groups.append({
  683. 'group_name': g.get('groupName') or g.get('group_name') or 'My travel group',
  684. 'group_number': str(g.get('formGroupId') or g.get('group_number') or ''),
  685. 'location': g.get('vacName') or g.get('location') or '',
  686. 'submitted': bool(g.get('submitted', True))
  687. })
  688. if groups:
  689. break
  690. except Exception as e:
  691. self._log(f"Error parsing travelGroups JSON: {e}")
  692. if not groups and getattr(self, 'page', None):
  693. try:
  694. btn_eles = self.page.eles('tag:button@@name=formGroupId')
  695. for btn in btn_eles:
  696. val = btn.attr('value')
  697. if val and str(val) not in [g['group_number'] for g in groups]:
  698. parent_row = btn.parent('tag:tr') or btn.parent('tag:div')
  699. is_submitted = True
  700. if parent_row and 'created' in parent_row.text.lower() and 'submitted' not in parent_row.text.lower():
  701. is_submitted = False
  702. groups.append({
  703. 'group_name': 'My travel group',
  704. 'group_number': str(val),
  705. 'location': '',
  706. 'submitted': is_submitted
  707. })
  708. except Exception as e:
  709. self._log(f"DOM fallback parsing travel groups failed: {e}")
  710. if not groups:
  711. self._log('Parsed travel group page, but found no travel groups in JSON or DOM')
  712. return groups
  713. def _parse_appointment_slots(self, html_content) -> List[Dict]:
  714. slots = []
  715. pattern = r'"availableAppointments\\":\s*(\[.*\]),\\"showFlexiAppointment'
  716. match = re.search(pattern, html_content, re.DOTALL)
  717. if match:
  718. json_str = match.group(1).replace(r'\"', '"')
  719. data = json.loads(json_str)
  720. for day in data:
  721. d_str = day.get('day')
  722. for s in day.get('slots', []):
  723. labels = s.get('labels', [])
  724. lbl = None
  725. if 'pta' in labels: lbl = 'pta'
  726. elif 'ptaw' in labels: lbl = 'ptaw'
  727. elif '' in labels: lbl = ''
  728. if lbl is not None:
  729. slots.append({
  730. 'date': d_str,
  731. 'time': s.get('time'),
  732. 'label': lbl
  733. })
  734. return slots
  735. def _check_page_is_session_expired_or_invalid(self, keyword, html: str) -> bool:
  736. if not html:
  737. self.is_healthy = False
  738. raise SessionExpiredOrInvalidError("Empty page HTML content")
  739. html_lower = html.lower()
  740. if keyword.lower() not in html_lower:
  741. session_expire_or_invalid_indicators = [
  742. 'redirected automatically' in html_lower,
  743. 'session expired' in html_lower,
  744. 'session timeout' in html_lower,
  745. ('login' in html_lower and 'password' in html_lower)
  746. ]
  747. if any(session_expire_or_invalid_indicators):
  748. self.is_healthy = False
  749. raise SessionExpiredOrInvalidError("Session expired or invalid state detected in page HTML")
  750. # --- 资源清理核心方法 ---
  751. def cleanup(self):
  752. """销毁浏览器并彻底删除临时文件"""
  753. cleanup_browser_workspace(self.page, self.root_workspace, self.tunnel, self._log)
  754. self.page = None
  755. self.tunnel = None
  756. def get_plugin_status(self) -> PluginStatus:
  757. account_name = self.config.account.username if self.config and self.config.account else ""
  758. proxy_info = f"{self.config.proxy.ip}:{self.config.proxy.port}" if self.config and self.config.proxy and self.config.proxy.ip else ""
  759. current_url = ""
  760. if self.page:
  761. try:
  762. current_url = self.page.url
  763. except Exception:
  764. pass
  765. session_age = int(time.time() - self.session_create_time) if self.session_create_time > 0 else 0
  766. state = "running" if self.is_healthy else "unhealthy"
  767. return PluginStatus(
  768. instance_id=self.instance_id,
  769. is_healthy=self.is_healthy,
  770. state=state,
  771. current_url=current_url,
  772. account=account_name,
  773. proxy=proxy_info,
  774. session_age_sec=session_age,
  775. last_active_at=utc_now_str(),
  776. )
  777. def __del__(self):
  778. """
  779. 析构函数:当对象被垃圾回收时自动调用
  780. """
  781. self.cleanup()