tls_plugin.py 36 KB

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