tls_plugin.py 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. import time
  2. import json
  3. import random
  4. import re
  5. import os
  6. import uuid
  7. import shutil
  8. import socket
  9. from datetime import datetime
  10. from typing import List, Dict, Optional, Any, Callable
  11. from urllib.parse import urljoin, urlparse, urlencode, parse_qs
  12. from concurrent.futures import ThreadPoolExecutor
  13. from DrissionPage import ChromiumPage, ChromiumOptions
  14. import configure
  15. from vs_plg import IVSPlg
  16. from vs_types import VSPlgConfig, AppointmentType, VSQueryResult, VSBookResult, AvailabilityStatus, TimeSlot, DateAvailability, NotFoundError, PermissionDeniedError, RateLimiteddError, SessionExpiredOrInvalidError, BizLogicError
  17. from utils.cloudflare_bypass_for_scraping import CloudflareBypasser
  18. from toolkit.mihomo_tunnel import MihomoTunnel
  19. from utils.mouse import HumanMouse, MOUSE_PROFILES
  20. from utils.keyboard import HumanKeyboard
  21. from utils.fingerprint_utils import FingerprintGenerator
  22. class BrowserResponse:
  23. """模拟 requests.Response"""
  24. def __init__(self, result_dict):
  25. result_dict = result_dict or {}
  26. self.status_code = result_dict.get('status', 0)
  27. self.text = result_dict.get('body', '')
  28. self.headers = result_dict.get('headers', {})
  29. self.url = result_dict.get('url', '')
  30. self._json = None
  31. def json(self):
  32. if self._json is None:
  33. if not self.text:
  34. return {}
  35. try:
  36. self._json = json.loads(self.text)
  37. except:
  38. self._json = {}
  39. return self._json
  40. class TlsPlugin(IVSPlg):
  41. """
  42. TLSContact 签证预约插件 (DrissionPage 版)
  43. """
  44. def __init__(self, group_id: str):
  45. self.group_id = group_id
  46. self.config: Optional[VSPlgConfig] = None
  47. self.free_config: Dict[str, Any] = {}
  48. self.is_healthy = True
  49. self.logger = None
  50. self.mouse = None
  51. self.keyboard = None
  52. self.page: Optional[ChromiumPage] = None
  53. self.travel_group: Optional[Dict] = None
  54. self.instance_id = uuid.uuid4().hex[:8]
  55. self.root_workspace = os.path.abspath(os.path.join("data/temp_browser_data", f"{self.group_id}.{self.instance_id}"))
  56. self.user_data_path = os.path.join(self.root_workspace, "user_data")
  57. if not os.path.exists(self.root_workspace):
  58. os.makedirs(self.root_workspace)
  59. self.tunnel = None
  60. self.session_create_time: float = 0
  61. def set_log(self, logger: Callable[[str], None]):
  62. self.logger = logger
  63. def _log(self, message):
  64. if self.logger:
  65. self.logger(f'[TlsPlugin] [{self.group_id}] [{self.instance_id}] {message}')
  66. else:
  67. print(f'[TlsPlugin] [{self.group_id}] [{self.instance_id}] {message}')
  68. def set_config(self, config: VSPlgConfig):
  69. self.config = config
  70. self.free_config = config.free_config or {}
  71. def keep_alive(self):
  72. try:
  73. self.page.refresh()
  74. self.page.wait.load_start(timeout=2)
  75. self.page.wait.doc_loaded()
  76. time.sleep(random.uniform(1, 3))
  77. self._check_page_is_session_expired_or_invalid('Book your appointment', html = self.page.html)
  78. self.simulate_random_human_mouse_move()
  79. except SessionExpiredOrInvalidError as e:
  80. self.is_healthy = False
  81. except Exception as e:
  82. self._log(f"Unexpected error in keep_alive: {e}")
  83. def simulate_random_human_mouse_move(self, min_x=100, max_x=800, min_y=100, max_y=800, min_points=1, max_points=2):
  84. """
  85. 在指定区域内模拟人类随机移动鼠标并点击数次。
  86. :param min_x: X坐标最小范围
  87. :param max_x: X坐标最大范围
  88. :param min_y: Y坐标最小范围
  89. :param max_y: Y坐标最大范围
  90. :param min_point: 随便移动的最少次数
  91. :param max_point: 随便移动的最多次数
  92. """
  93. move_cnt = random.randint(min_points, max_points)
  94. self._log(f"Starting random human simulation: will move {move_cnt} times in the area.")
  95. for i in range(move_cnt):
  96. rand_x = random.randint(min_x, max_x)
  97. rand_y = random.randint(min_y, max_y)
  98. self._log(f"[{i+1}/{move_cnt}] Moving mouse to ({rand_x}, {rand_y})")
  99. self.mouse.move(rand_x, rand_y, humanize=True)
  100. self._log("Random human move simulation completed.")
  101. def health_check(self) -> bool:
  102. if not self.is_healthy:
  103. return False
  104. if self.page is None:
  105. return False
  106. try:
  107. if not self.page.run_js("return 1;"):
  108. return False
  109. except:
  110. return False
  111. if self.config.session_max_life > 0:
  112. current_time = time.time()
  113. elapsed_time = current_time - self.session_create_time
  114. if elapsed_time > self.config.session_max_life:
  115. self._log(f"Session expired.")
  116. return False
  117. return True
  118. def _save_screenshot(self, name_prefix):
  119. try:
  120. timestamp = int(time.time())
  121. filename = f"{self.instance_id}_{name_prefix}_{timestamp}.jpg"
  122. save_path = os.path.join("data", filename)
  123. os.makedirs("data", exist_ok=True)
  124. self.page.get_screenshot(path=save_path, full_page=False)
  125. self._log(f"Screenshot saved to {save_path}")
  126. except Exception as e:
  127. self._log(f"Failed to save screenshot: {e}")
  128. def create_session(self):
  129. """
  130. 全浏览器会话创建:过盾 -> JS注入登录 -> 状态机自动路由导航 -> 到达目标页
  131. """
  132. self._log(f"Initializing Session (ID: {self.instance_id})...")
  133. captcha_future = None
  134. captcha_executor = ThreadPoolExecutor(max_workers=1)
  135. login_captcha_cfg = self.free_config.get("login_captcha", {})
  136. if login_captcha_cfg.get('solve_advance'):
  137. login_page = login_captcha_cfg.get("page_url")
  138. site_key = login_captcha_cfg.get("site_key")
  139. task_type = login_captcha_cfg.get("task")
  140. self._log(f"🚀 Early starting background Captcha solve for sitekey={site_key}")
  141. rc_params = {
  142. "type": task_type,
  143. "page": login_page,
  144. "siteKey": site_key,
  145. "apiToken": self.free_config.get("capsolver_key", "")
  146. }
  147. captcha_future = captcha_executor.submit(self._solve_recaptcha, rc_params)
  148. co = ChromiumOptions()
  149. def get_free_port():
  150. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  151. s.bind(('', 0))
  152. return s.getsockname()[1]
  153. debug_port = get_free_port()
  154. self._log(f"Assigned Debug Port: {debug_port}")
  155. self._log(f"Account id={self.config.account.id}, proxy id={self.config.proxy.id}")
  156. co.set_local_port(debug_port)
  157. co.set_user_data_path(self.user_data_path)
  158. chrome_path = configure.CHROME_PATH
  159. if not chrome_path:
  160. chrome_path = os.getenv("CHROME_BIN")
  161. if chrome_path and os.path.exists(chrome_path):
  162. co.set_paths(browser_path=chrome_path)
  163. if self.config.proxy and self.config.proxy.ip:
  164. p = self.config.proxy
  165. if p.username and p.password:
  166. self._log(f"Starting Proxy Tunnel for {p.ip}...")
  167. exit_node = {
  168. "name": "ExitNode",
  169. "type": p.proto,
  170. "server": p.ip,
  171. "port": p.port,
  172. "username": p.username,
  173. "password": p.password
  174. }
  175. relay_node = None
  176. if configure.MIHOMO_RELAY_NODES:
  177. relay_node = random.choice(configure.MIHOMO_RELAY_NODES)
  178. mihomo_path = configure.MIHOMO_BIN_PATH
  179. if not mihomo_path:
  180. mihomo_path = os.getenv("MIHOMO_BIN")
  181. if not mihomo_path:
  182. raise BizLogicError(message='Mihomo path is null, You need set mihomo bin path in configure or os env')
  183. self.tunnel = MihomoTunnel(mihomo_path, exit_node=exit_node, relay_node=relay_node)
  184. local_proxy = self.tunnel.start()
  185. self._log(f"Tunnel started at {local_proxy}")
  186. co.set_argument(f'--proxy-server={local_proxy}')
  187. else:
  188. proxy_str = f"{p.proto}://{p.ip}:{p.port}"
  189. co.set_argument(f'--proxy-server={proxy_str}')
  190. else:
  191. self._log("[WARN] No proxy configured!")
  192. specific_fp = FingerprintGenerator().generate(self.config.account.username)
  193. fp_seed = specific_fp.get("seed")
  194. fp_platform = specific_fp.get("platform")
  195. fp_brand = specific_fp.get("brand")
  196. self._log(f'browser fingerprint seed={fp_seed}')
  197. co.headless(False)
  198. co.set_argument('--no-sandbox')
  199. co.set_argument('--disable-dev-shm-usage')
  200. co.set_argument('--window-size=1920,1080')
  201. co.set_argument('--disable-blink-features=AutomationControlled')
  202. # co.set_argument('--ignore-gpu-blocklist')
  203. # co.set_argument('--enable-webgl')
  204. # co.set_argument('--use-gl=angle')
  205. # co.set_argument('--use-angle=swiftshader')
  206. co.set_argument(f"--fingerprint={fp_seed}")
  207. co.set_argument(f"--fingerprint-platform={fp_platform}")
  208. co.set_argument(f"--fingerprint-brand={fp_brand}")
  209. self.page = ChromiumPage(co)
  210. if self.config.debug:
  211. self.page.get('https://example.com')
  212. js_script = """
  213. function getFingerprint() {
  214. let webglVendor = 'Unknown';
  215. let webglRenderer = 'Unknown';
  216. try {
  217. let canvas = document.createElement('canvas');
  218. let gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
  219. if (gl) {
  220. let debugInfo = gl.getExtension('WEBGL_debug_renderer_info');
  221. if (debugInfo) {
  222. webglVendor = gl.getParameter(debugInfo.UNMASKED_VENDOR_WEBGL);
  223. webglRenderer = gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL);
  224. }
  225. }
  226. } catch(e) {}
  227. return {
  228. "User-Agent": navigator.userAgent,
  229. "Platform": navigator.userAgentData ? navigator.userAgentData.platform : navigator.platform,
  230. "Brands": navigator.userAgentData ? navigator.userAgentData.brands.map(b => b.brand).join(', ') : 'Not Supported',
  231. "CPU Cores": navigator.hardwareConcurrency,
  232. "Language": navigator.language,
  233. "Timezone": Intl.DateTimeFormat().resolvedOptions().timeZone,
  234. "WebGL Vendor": webglVendor,
  235. "WebGL Renderer": webglRenderer
  236. };
  237. }
  238. return getFingerprint();
  239. """
  240. fp_data = self.page.run_js(js_script)
  241. self._log("================ 预检浏览器指纹数据 ================")
  242. self._log(json.dumps(fp_data, indent=4, ensure_ascii=False))
  243. self._log("====================================================")
  244. # --- 初始化访问与过盾 ---
  245. tls_url = self.free_config.get('tls_url', '')
  246. self._log(f"Navigating: {tls_url}")
  247. self.page.get(tls_url)
  248. time.sleep(5)
  249. if 'Attention Required! | Cloudflare' in self.page.title and 'Sorry, you have been blocked' in self.page.html:
  250. self._log(f'Block by cloudflare, try refresh...')
  251. self.page.refresh()
  252. self.page.wait.load_start(timeout=2)
  253. self.page.wait.doc_loaded()
  254. cf_bypasser = CloudflareBypasser(self.page, log=self.config.debug)
  255. if not cf_bypasser.bypass(max_retry=6):
  256. raise BizLogicError("Cloudflare bypass timeout")
  257. time.sleep(3)
  258. cf_bypasser.handle_waiting_room()
  259. self._log("Init humanize tools...")
  260. profile_name = random.choice(list(MOUSE_PROFILES.keys()))
  261. self._log(f"[HumanMouse] current mouse profiles: {profile_name}")
  262. self.mouse = HumanMouse(self.page, timing=MOUSE_PROFILES[profile_name], debug=self.config.debug)
  263. self.keyboard = HumanKeyboard(self.page)
  264. viewport_width = self.page.rect.viewport_size[0]
  265. viewport_height = self.page.rect.viewport_size[1]
  266. init_x = random.randint(10, viewport_width - 10)
  267. init_y = random.randint(10, viewport_height - 10)
  268. self.mouse.move(init_x, init_y)
  269. max_steps = 10
  270. stuck_counter = 0
  271. last_url = ""
  272. session_created = False
  273. has_submitted_login = False
  274. 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']
  275. for step in range(max_steps):
  276. self.page.wait.doc_loaded()
  277. time.sleep(1)
  278. current_url = self.page.url
  279. current_title = self.page.title.lower()
  280. current_html_content = self.page.html
  281. self._log(f"--- [Router Step {step+1}] Current URL: {current_url} ---")
  282. if current_url == last_url:
  283. stuck_counter += 1
  284. else:
  285. last_url = current_url
  286. stuck_counter = 0
  287. # 在某个页面卡住3轮以上,直接重试
  288. if stuck_counter >= 3:
  289. self._log("[WARN] Page stucked, try to refresh...")
  290. self.page.refresh()
  291. self.page.wait.load_start(timeout=5)
  292. stuck_counter = 0
  293. continue
  294. server_error_indicators = [
  295. "502 Bad Gateway",
  296. "503 Service Temporarily Unavailable",
  297. ]
  298. # 网络出现故障,直接重试
  299. if any(err in current_html_content for err in server_error_indicators):
  300. self._log(f"[WARN] Server network error, try to refresh (Step: {step})...")
  301. time.sleep(2)
  302. self.page.refresh()
  303. self.page.wait.load_start(timeout=5)
  304. continue
  305. # 特征1: 必须是 <p> 标签,且包含指定文案 (绝对不会匹配到 <script> 标签里的字典)
  306. spa_error_p = self.page.ele('xpath://p[contains(text(), "It looks like something went wrong")]', timeout=0.1)
  307. # 特征2: 必须是 <div> 标签,且直接包含 "Error code:" 文本
  308. error_code_div = self.page.ele('xpath://div[contains(text(), "Error code:")]', timeout=0.1)
  309. if spa_error_p or error_code_div:
  310. extracted_code = "Unknown"
  311. if error_code_div:
  312. span_ele = error_code_div.ele('tag:span', timeout=0.1)
  313. if span_ele:
  314. extracted_code = span_ele.text
  315. self._log(f"[WARN] Frontend application error page detected (Error Code: {extracted_code}). Triggering fallback page refresh (Step: {step})...")
  316. time.sleep(2)
  317. self.page.refresh()
  318. self.page.wait.load_start(timeout=5)
  319. continue
  320. cloudflare_blocked_indicators = [
  321. "Sorry, you have been blocked" in current_html_content,
  322. "You are being rate limited" in current_html_content,
  323. "Cloudflare Ray ID" in current_html_content
  324. ]
  325. if any(cloudflare_blocked_indicators):
  326. raise BizLogicError(message="Blocked by Cloudflare WAF. Need to change IP or browser fingerprint.")
  327. # 遇到五秒盾先绕盾
  328. if "just a moment" in current_title:
  329. cf_bypasser.bypass(max_retry=3)
  330. time.sleep(3)
  331. continue
  332. # 如果语言不匹配, 切换语言到英语
  333. matched_lang = next((lang for lang in other_langs if lang in current_url), None)
  334. if matched_lang:
  335. current_url = current_url.replace(matched_lang, 'en-us')
  336. self.page.get(current_url)
  337. self.page.wait.load_start(timeout=3)
  338. continue
  339. # 到达终极目标页面 (成功退出条件)
  340. if "appointment-booking" in current_url or self.page.ele('tag:button@text():Book your appointment', timeout=1):
  341. btn_selector = 'tag:button@text():Book your appointment'
  342. if self.page.wait.ele_displayed(btn_selector, timeout=10):
  343. self._handle_cookie_dialog()
  344. self.session_create_time = time.time()
  345. self._log("✅ Login & Navigation Success! Reached appointment-booking.")
  346. session_created = True
  347. break
  348. # 页面发生react渲染错误
  349. if 'a client-side exception has occurred while loading' in current_html_content:
  350. self.page.refresh()
  351. self.page.wait.load_start(timeout=3)
  352. continue
  353. # 改签的情况
  354. if '/workflow/application-summary' in current_url:
  355. change_btn_sel = 'tag:button@@text():Change'
  356. self.page.wait.ele_displayed(change_btn_sel, timeout=10)
  357. self.page.ele(change_btn_sel).click(by_js=True)
  358. confirm_btn_sel = 'tag:button@@text():Yes'
  359. self.page.wait.ele_displayed(confirm_btn_sel, timeout=5)
  360. self.page.ele(confirm_btn_sel).click(by_js=True)
  361. self.page.wait.load_start(timeout=5)
  362. continue
  363. # 遇到没有申请人的拦截页 (致命错误退出条件)
  364. no_applicant_indicators = [
  365. "Add a new applicant" in current_html_content,
  366. "You have not yet added an applicant" in current_html_content,
  367. "applicants-information" in current_url
  368. ]
  369. if any(no_applicant_indicators):
  370. raise BizLogicError(message="No applicant added. Cannot proceed to booking.")
  371. # 首页/登录入口页 -> 需要点击进入登录
  372. if current_url == tls_url:
  373. if self.page.ele("tag:a@@href:login", timeout=1) and not self.page.ele('tag:label@@text():Email', timeout=1):
  374. self._log("State: Login Portal. Clicking login link...")
  375. login_link = self.page.ele("tag:a@@href:login")
  376. self.mouse.human_click_ele(login_link)
  377. self.page.wait.load_start(timeout=3)
  378. continue
  379. if self.page.ele("tag:svg@@data-testid=user-button", timeout=1):
  380. self._log("State: Already login, logout now...")
  381. user_btn = self.page.ele("tag:svg@@data-testid=user-button")
  382. self.mouse.human_click_ele(user_btn)
  383. time.sleep(1.5)
  384. logout_btn = self.page.ele("#logout")
  385. self.mouse.human_click_ele(logout_btn)
  386. self.page.wait.load_start(timeout=3)
  387. self.page.get(tls_url)
  388. self.page.wait.load_start(timeout=3)
  389. continue
  390. # 真正的登录表单页
  391. if self.page.ele('tag:label@@text():Email', timeout=1) and not has_submitted_login:
  392. self._log("State: Login Form. Processing credentials and Captcha...")
  393. recaptchav2_token = ""
  394. if not captcha_future and (self.page.ele('.g-recaptcha') or self.page.ele('xpath://iframe[contains(@src, "recaptcha")]')):
  395. rec_iframe = self.page.ele('xpath://iframe[contains(@src, "recaptcha")]')
  396. rec_iframe_src = rec_iframe.attr('src')
  397. rec_parsed = urlparse(rec_iframe_src)
  398. rec_params = parse_qs(rec_parsed.query)
  399. rec_sitekey = rec_params.get("k", [None])[0]
  400. rec_size = rec_params.get("size", [None])[0]
  401. if 'normal' == rec_size:
  402. self._log(f"Found dynamic sitekey={rec_sitekey}. Starting async Captcha solver...")
  403. rc_params = {
  404. "type": "ReCaptchaV2TaskProxyLess",
  405. "page": current_url,
  406. "siteKey": rec_sitekey,
  407. "apiToken": self.free_config.get("capsolver_key", "")
  408. }
  409. captcha_future = captcha_executor.submit(self._solve_recaptcha, rc_params)
  410. username = self.config.account.username
  411. password = self.config.account.password
  412. input_ele = self.page.ele('tag:label@@text():Email').next()
  413. self.mouse.human_click_ele(input_ele)
  414. time.sleep(random.uniform(0.2, 0.6))
  415. self.keyboard.type_text(username, humanize=True)
  416. time.sleep(random.uniform(0.5, 1.2))
  417. input_ele = self.page.ele('tag:label@@text():Password').next()
  418. self.mouse.human_click_ele(input_ele)
  419. time.sleep(random.uniform(0.2, 0.6))
  420. self.keyboard.type_text(password, humanize=True)
  421. # 注入 Token
  422. if captcha_future:
  423. self._log("Waiting for background Captcha result...")
  424. try:
  425. # 设一个合理的超时,防止死锁
  426. recaptchav2_token = captcha_future.result(timeout=120)
  427. self._log("Background Captcha solved successfully!")
  428. except Exception as e:
  429. raise BizLogicError(f"Captcha solving failed or timed out: {e}")
  430. # 注入 Token
  431. if recaptchav2_token:
  432. inject_js = f"var g = document.getElementById('g-recaptcha-response'); if(g) {{ g.value = '{recaptchav2_token}'; }}"
  433. self.page.run_js(inject_js)
  434. time.sleep(random.uniform(0.5, 1.0))
  435. self._log("Submitting Login...")
  436. login_btn = self.page.ele('tag:button@@text():Login')
  437. self.mouse.human_click_ele(login_btn)
  438. has_submitted_login = True
  439. self.page.wait.load_start(timeout=5)
  440. continue
  441. # Travel Groups 页面
  442. if "travel-groups" in current_url:
  443. self._log("State: Travel Groups. Selecting targeted group...")
  444. groups = self._parse_travel_groups(current_html_content)
  445. location = self.free_config.get('location')
  446. self.travel_group = next((g for g in groups if location in g['location']), None)
  447. if not self.travel_group or not self.travel_group.get("submitted"):
  448. self._save_screenshot("group_not_found")
  449. raise NotFoundError(f"Group not found for {location}")
  450. formgroup_id = self.travel_group.get('group_number')
  451. btn_selector = f'tag:button@@name=formGroupId@@value={formgroup_id}'
  452. if self.page.wait.eles_loaded(btn_selector, timeout=10):
  453. buttons = self.page.eles(btn_selector)
  454. select_btn = next((btn for btn in reversed(buttons) if btn.rect.size[0] > 0 and btn.rect.size[1] > 0), None)
  455. if select_btn:
  456. time.sleep(random.uniform(0.5, 1.2))
  457. self.mouse.human_click_ele(select_btn)
  458. self.page.wait.load_start(timeout=3)
  459. continue
  460. else:
  461. self._log("[WARN] Select button found but not visible.")
  462. else:
  463. self._log(f"[WARN] Wait timeout for group button {formgroup_id}")
  464. # 中间过渡页,需点击 "Book Appointment" 继续往下走
  465. if self.page.ele('#book-appointment-btn', timeout=1):
  466. self._log("State: Intermediate Dashboard. Clicking Book Appointment button...")
  467. self.mouse.human_click_ele(self.page.ele('#book-appointment-btn'))
  468. self.page.wait.load_start(timeout=3)
  469. continue
  470. # 登录失败校验 或 未知加载状态
  471. if "login-actions" in current_url and has_submitted_login:
  472. self._log("Waiting on login-actions... (Might be authenticating or invalid credentials)")
  473. time.sleep(2)
  474. if self.page.ele('text:Invalid username or password', timeout=1): # 假设网页上有错误提示
  475. raise BizLogicError(message="Login Failed! Invalid credentials or Captcha rejected.")
  476. continue
  477. self._log("State: Transitioning or Unknown. Waiting 2 seconds...")
  478. time.sleep(2)
  479. # 如果处于某些特定的复杂页面且久久不动,尝试主动刷新
  480. if step > 0 and step % 4 == 0:
  481. self._log("[WARN] Unknown state persisted for too long. Attempting fallback refresh...")
  482. self.page.refresh()
  483. self.page.wait.load_start(timeout=3)
  484. if not session_created:
  485. raise BizLogicError(f"Failed to reach appointment-booking after {max_steps} navigation steps. Stuck at: {self.page.url}")
  486. def query(self, apt_type: AppointmentType) -> VSQueryResult:
  487. res = VSQueryResult()
  488. res.success = False
  489. slots = []
  490. self._log(f"Executing silent JS fetch...")
  491. resp = self._perform_request("GET", self.page.url, retry_count=0)
  492. self._check_page_is_session_expired_or_invalid('Book your appointment', resp.text)
  493. slots = self._parse_appointment_slots(resp.text)
  494. if slots:
  495. res.success = True
  496. earliest_date = slots[0]["date"]
  497. earliest_dt = datetime.strptime(earliest_date, "%Y-%m-%d")
  498. res.availability_status = AvailabilityStatus.Available
  499. res.earliest_date = earliest_dt
  500. date_map: dict[datetime, list[TimeSlot]] = {}
  501. for s in slots:
  502. date_str = s["date"]
  503. dt = datetime.strptime(date_str, "%Y-%m-%d")
  504. date_map.setdefault(dt, []).append(
  505. TimeSlot(time=s["time"], label=str(s.get("label", "")))
  506. )
  507. res.availability = [DateAvailability(date=d, times=slots) for d, slots in date_map.items()]
  508. self._log(f"Slot Found! size={len(slots)}")
  509. else:
  510. self._log("No slots available.")
  511. res.success = False
  512. res.availability_status = AvailabilityStatus.NoneAvailable
  513. # TODO(TEST): 临时测试预约提交
  514. if configure.TLS_TEST_BOOK_AFTER_QUERY:
  515. test_date = "2026-06-10"
  516. test_time = "09:00"
  517. test_label = ""
  518. test_dt = datetime.strptime(test_date, "%Y-%m-%d")
  519. query_res = VSQueryResult()
  520. query_res.success = True
  521. query_res.availability_status = AvailabilityStatus.Available
  522. query_res.earliest_date = test_dt
  523. query_res.availability = [
  524. DateAvailability(
  525. date=test_dt,
  526. times=[TimeSlot(time=test_time, label=test_label)]
  527. )
  528. ]
  529. self._log(f"[TEST] using fixed June slot: {test_date} {test_time} {test_label}")
  530. test_userinput = {
  531. "support_pta": False,
  532. "expected_end_date": "2100-01-01",
  533. "expected_start_date": "2000-01-01"
  534. }
  535. try:
  536. self.book(query_res, test_userinput)
  537. except Exception as e:
  538. self._log(f"[TEST] book() after query failed: {e}")
  539. self.is_healthy = False
  540. return res
  541. def book(self, slot_info: VSQueryResult, user_inputs: Dict = None) -> VSBookResult:
  542. res = VSBookResult()
  543. res.success = False
  544. exp_start = user_inputs.get('expected_start_date', '')
  545. exp_end = user_inputs.get('expected_end_date', '')
  546. support_pta = user_inputs.get('support_pta', True)
  547. target_labels = ['']
  548. if support_pta:
  549. target_labels.append('pta')
  550. available_dates_str =[
  551. da.date.strftime("%Y-%m-%d")
  552. for da in slot_info.availability if da.date
  553. ]
  554. valid_dates_list = self._filter_dates(available_dates_str, exp_start, exp_end)
  555. if not valid_dates_list:
  556. raise NotFoundError(message="No dates match user constraints")
  557. all_possible_slots =[]
  558. for da in slot_info.availability:
  559. if not da.date:
  560. continue
  561. date_str = da.date.strftime("%Y-%m-%d")
  562. if date_str in valid_dates_list:
  563. for t in da.times:
  564. if t.label in target_labels:
  565. all_possible_slots.append({
  566. "date": date_str,
  567. "time_obj": t,
  568. "label": t.label
  569. })
  570. if not all_possible_slots:
  571. raise NotFoundError(message="No suitable slot found (after label filtering)")
  572. selected_slot = random.choice(all_possible_slots)
  573. selected_date = selected_slot["date"]
  574. selected_time = selected_slot["time_obj"]
  575. selected_label = selected_slot["label"]
  576. self._log(f"Found {len(all_possible_slots)} valid slots. selected slot: {selected_date} {selected_time.time} {selected_label}")
  577. self.page.listen.start('/workflow/appointment-booking', method='POST')
  578. js_update_form = f"""
  579. try {{
  580. const buttons = Array.from(document.querySelectorAll('button[type="submit"]'));
  581. const submitBtn = buttons.find(btn => {{
  582. return btn.textContent.trim().toLowerCase().includes('book your appointment');
  583. }});
  584. if (!submitBtn) return 'Submit button not found';
  585. const form = submitBtn.closest('form');
  586. if (!form) return 'Correct form not found';
  587. function setReactValue(input, value) {{
  588. if (!input) return;
  589. input.value = value;
  590. }}
  591. setReactValue(form.querySelector('input[name="date"]'), '{selected_date}');
  592. setReactValue(form.querySelector('input[name="time"]'), '{selected_time.time}');
  593. setReactValue(form.querySelector('input[name="appointmentLabel"]'), '{selected_label}');
  594. submitBtn.removeAttribute('disabled');
  595. submitBtn.classList.remove('opacity-50', 'cursor-not-allowed');
  596. return 'form_updated';
  597. }} catch (e) {{
  598. return e.toString();
  599. }}
  600. """
  601. update_res = self.page.run_js(js_update_form)
  602. self._log(f"Form update triggered: {update_res}")
  603. if update_res != 'form_updated':
  604. raise BizLogicError(message=f"Failed to update form: {update_res}")
  605. submit_btn = self.page.ele('tag:button@@type=submit@@text():Book your appointment')
  606. if not submit_btn:
  607. raise BizLogicError(message="Submit button not found for mouse click")
  608. self._log("Moving mouse to submit button and clicking")
  609. self.mouse.human_click_ele(submit_btn)
  610. packet = self.page.listen.wait(timeout=10)
  611. if not packet:
  612. raise BizLogicError(message='Listening data failed')
  613. self.page.listen.stop()
  614. self._log(f"URL: {packet.url}")
  615. self._log(f"POST Body: {packet.request.postData}")
  616. self._log(f"POST Resp: {packet.response.body}")
  617. self._log("Waiting for Next.js to process the form submission...")
  618. for _ in range(10):
  619. try:
  620. current_page_url = self.page.url
  621. current_page_html = self.page.html
  622. appointment_confirmation_indicators = [
  623. "order-summary" in current_page_url,
  624. "partner-services" in current_page_url,
  625. "appointment-confirmation" in current_page_url,
  626. "Change my appointment" in current_page_html,
  627. "Book a new appointment" in current_page_html,
  628. ]
  629. if any(appointment_confirmation_indicators):
  630. self._log(f"✅ BOOKING SUCCESS! Redirected to: {current_page_url}")
  631. res.success = True
  632. res.label = selected_label
  633. res.book_date = selected_date
  634. res.book_time = selected_time.time
  635. self._save_screenshot("book_slot_success")
  636. break
  637. toast_selector = 'tag:div@role=alert'
  638. toast_ele = self.page.ele(toast_selector, timeout=0.5)
  639. if toast_ele:
  640. error_msg = toast_ele.text
  641. self._log(f"❌ BOOKING FAILED! Detected popup: {error_msg}")
  642. break
  643. time.sleep(0.5)
  644. except Exception:
  645. pass
  646. return res
  647. def _perform_request(self, method, url, headers=None, data=None, json_data=None, params=None, retry_count=0):
  648. """
  649. 在浏览器上下文中注入 JS 执行 Fetch
  650. """
  651. if not self.page:
  652. raise BizLogicError("Browser not initialized")
  653. if params:
  654. from urllib.parse import urlencode
  655. if '?' in url:
  656. url += '&' + urlencode(params)
  657. else:
  658. url += '?' + urlencode(params)
  659. fetch_options = {
  660. "method": method.upper(),
  661. "headers": headers or {},
  662. "credentials": "include"
  663. }
  664. # Body 处理
  665. if json_data:
  666. fetch_options['body'] = json.dumps(json_data)
  667. fetch_options['headers']['Content-Type'] = 'application/json'
  668. elif data:
  669. if isinstance(data, dict):
  670. from urllib.parse import urlencode
  671. fetch_options['body'] = urlencode(data)
  672. fetch_options['headers']['Content-Type'] = 'application/x-www-form-urlencoded'
  673. else:
  674. fetch_options['body'] = data
  675. js_script = f"""
  676. const url = "{url}";
  677. const options = {json.dumps(fetch_options)};
  678. return fetch(url, options)
  679. .then(async response => {{
  680. const text = await response.text();
  681. const headers = {{}};
  682. response.headers.forEach((value, key) => headers[key] = value);
  683. return {{
  684. status: response.status,
  685. body: text,
  686. headers: headers,
  687. url: response.url
  688. }};
  689. }})
  690. .catch(error => {{
  691. return {{
  692. status: 0,
  693. body: error.toString(),
  694. headers: {{}},
  695. url: url
  696. }};
  697. }});
  698. """
  699. res_dict = self.page.run_js(js_script, timeout=30)
  700. resp = BrowserResponse(res_dict)
  701. if resp.status_code == 200:
  702. return resp
  703. elif resp.status_code == 401:
  704. self.is_healthy = False
  705. raise SessionExpiredOrInvalidError()
  706. elif resp.status_code == 403:
  707. if retry_count < 2:
  708. self._log(f"HTTP 403 Detected. Cloudflare session expired? Attempting refresh (Try {retry_count+1}/2)...")
  709. if self._refresh_firewall_session():
  710. self._log("Firewall session refreshed. Retrying request...")
  711. return self._perform_request(method, url, headers, data, json_data, params, retry_count+1)
  712. else:
  713. self._log("Failed to refresh firewall session.")
  714. raise PermissionDeniedError(f"HTTP 403: {resp.text[:100]}")
  715. elif resp.status_code == 429:
  716. self.is_healthy = False
  717. raise RateLimiteddError()
  718. else:
  719. if resp.status_code == 0:
  720. raise BizLogicError(f"Network Error: {resp.text}")
  721. raise BizLogicError(message=f"HTTP Error {resp.status_code}: {resp.text[:100]}")
  722. def _refresh_firewall_session(self) -> bool:
  723. """
  724. 主动刷新页面以触发 Cloudflare 挑战并尝试通过
  725. """
  726. try:
  727. self._log("Refreshing page to trigger Cloudflare...")
  728. self.page.refresh()
  729. time.sleep(5)
  730. cf = CloudflareBypasser(self.page, log=self.config.debug)
  731. success = cf.bypass(max_retry=6)
  732. if success:
  733. title = self.page.title.lower()
  734. if "access denied" in title:
  735. return False
  736. time.sleep(2)
  737. return True
  738. return False
  739. except Exception as e:
  740. self._log(f"Error during firewall refresh: {e}")
  741. return False
  742. def _solve_recaptcha(self, params) -> str:
  743. """调用 VSCloudApi 解决 ReCaptcha"""
  744. key = params.get("apiToken")
  745. if not key:
  746. raise NotFoundError("Api-token required")
  747. submit_url = "https://api.capsolver.com/createTask"
  748. task = {
  749. "type": params.get("type"),
  750. "websiteURL": params.get("page"),
  751. "websiteKey": params.get("siteKey"),
  752. }
  753. if params.get("action"):
  754. task["pageAction"] = params.get("action")
  755. if params.get("proxy"):
  756. p = self.config.proxy
  757. task["proxyType"] = p.proto
  758. task["proxyAddress"] = p.ip
  759. task["proxyPort"] = p.port
  760. if p.username:
  761. task["proxyLogin"] = p.username
  762. task["proxyPassword"] = p.password
  763. payload = {"clientKey": key, "task": task}
  764. import requests as req
  765. r = req.post(submit_url, json=payload, timeout=20)
  766. if r.status_code != 200:
  767. raise BizLogicError(message="Failed to submit capsolver task")
  768. task_id = r.json().get("taskId")
  769. for _ in range(20):
  770. r = req.post("https://api.capsolver.com/getTaskResult", json={"clientKey": key, "taskId": task_id}, timeout=20)
  771. if r.status_code == 200:
  772. d = r.json()
  773. if d.get("status") == "ready":
  774. return d["solution"]["gRecaptchaResponse"]
  775. time.sleep(3)
  776. raise BizLogicError(message="Capsolver task timeout")
  777. def _parse_travel_groups(self, html_content) -> List[Dict]:
  778. groups = []
  779. js_pattern = r'\\"travelGroups\\":\s*(\[.*?\]),\\"availableCountriesToCreateGroups'
  780. js_match = re.search(js_pattern, html_content, re.DOTALL)
  781. if js_match:
  782. json_str = js_match.group(1).replace(r'\"', '"')
  783. data = json.loads(json_str)
  784. for g in data:
  785. groups.append({
  786. 'group_name': g.get('groupName'),
  787. 'group_number': g.get('formGroupId'),
  788. 'location': g.get('vacName'),
  789. 'submitted': g.get('submitted')
  790. })
  791. else:
  792. self._log('Parsed travel group page, but not found travelGroups')
  793. return groups
  794. def _parse_appointment_slots(self, html_content) -> List[Dict]:
  795. slots = []
  796. pattern = r'"availableAppointments\\":\s*(\[.*\]),\\"showFlexiAppointment'
  797. match = re.search(pattern, html_content, re.DOTALL)
  798. if match:
  799. json_str = match.group(1).replace(r'\"', '"')
  800. data = json.loads(json_str)
  801. for day in data:
  802. d_str = day.get('day')
  803. for s in day.get('slots', []):
  804. labels = s.get('labels', [])
  805. lbl = None
  806. if 'pta' in labels: lbl = 'pta'
  807. elif 'ptaw' in labels: lbl = 'ptaw'
  808. elif '' in labels: lbl = ''
  809. if lbl is not None:
  810. slots.append({
  811. 'date': d_str,
  812. 'time': s.get('time'),
  813. 'label': lbl
  814. })
  815. return slots
  816. def _handle_cookie_dialog(self, timeout: int = 3) -> bool:
  817. """
  818. 检测并处理 Osano Cookie 弹窗
  819. """
  820. try:
  821. dialog = self.page.ele('@aria-label=Cookie Consent Banner', timeout=timeout)
  822. if not dialog:
  823. return False
  824. target_btn = dialog.ele('tag:button@text():Save')
  825. if not target_btn:
  826. target_btn = dialog.ele('tag:button@text():Accept')
  827. if target_btn:
  828. self.mouse.human_click_ele(target_btn)
  829. self._log(f"Handle cookie window success")
  830. return True
  831. else:
  832. self._log(f"Found cookie window, but button not found")
  833. return False
  834. except Exception as e:
  835. self._log(f"Handle cookie window exception: {e}")
  836. return False
  837. def _check_page_is_session_expired_or_invalid(self, keyword, html: str) -> bool:
  838. if not html:
  839. self.is_healthy = False
  840. raise SessionExpiredOrInvalidError()
  841. html_lower = html.lower()
  842. if keyword.lower() not in html_lower:
  843. session_expire_or_invalid_indicators = [
  844. 'redirected automatically' in html_lower,
  845. 'login' in html_lower and 'password' in html_lower,
  846. 'session expired' in html_lower
  847. ]
  848. if any(session_expire_or_invalid_indicators):
  849. self.is_healthy = False
  850. raise SessionExpiredOrInvalidError()
  851. def _filter_dates(self, dates: List[str], start_str: str, end_str: str) -> List[str]:
  852. if not start_str or not end_str:
  853. return dates
  854. valid_dates = []
  855. s_date = datetime.strptime(start_str[:10], "%Y-%m-%d")
  856. e_date = datetime.strptime(end_str[:10], "%Y-%m-%d")
  857. for date_str in dates:
  858. curr_date = datetime.strptime(date_str, "%Y-%m-%d")
  859. if s_date <= curr_date <= e_date:
  860. valid_dates.append(date_str)
  861. random.shuffle(valid_dates)
  862. return valid_dates
  863. # --- 资源清理核心方法 ---
  864. def cleanup(self):
  865. """
  866. 销毁浏览器并彻底删除临时文件
  867. """
  868. if self.page:
  869. try:
  870. self.page.quit(force=True)
  871. except Exception:
  872. pass
  873. self.page = None
  874. if os.path.exists(self.root_workspace):
  875. for _ in range(3):
  876. try:
  877. time.sleep(0.2)
  878. shutil.rmtree(self.root_workspace, ignore_errors=True)
  879. break
  880. except Exception as e:
  881. self._log(f"Cleanup retry: {e}")
  882. time.sleep(0.5)
  883. if os.path.exists(self.root_workspace):
  884. self._log(f"[WARN] Failed to fully remove workspace: {self.root_workspace}")
  885. if self.tunnel:
  886. try: self.tunnel.stop()
  887. except: pass
  888. self.tunnel = None
  889. def __del__(self):
  890. """
  891. 析构函数:当对象被垃圾回收时自动调用
  892. """
  893. self.cleanup()