tls_plugin.py 36 KB

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