bls_plugin.py 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. import re
  2. import os
  3. import uuid
  4. import base64
  5. import time
  6. import json
  7. import shutil
  8. import random
  9. import string
  10. from datetime import datetime, timedelta
  11. from pathlib import Path
  12. from urllib.parse import urlparse, parse_qs, urlencode
  13. from typing import Dict, List, Optional, Any, Callable
  14. from curl_cffi import requests, const
  15. from bs4 import BeautifulSoup
  16. # DrissionPage 核心
  17. from DrissionPage import ChromiumPage, ChromiumOptions
  18. from cryptography.hazmat.primitives import serialization, hashes
  19. from cryptography.hazmat.primitives.asymmetric import padding
  20. from cryptography.hazmat.backends import default_backend
  21. # 框架依赖
  22. from vs_plg import IVSPlg
  23. from vs_types import VSPlgConfig, VSQueryResult, VSBookResult, DateAvailability, TimeSlot, AvailabilityStatus, NotFoundError, PermissionDeniedError, RateLimiteddError, SessionExpiredOrInvalidError, BizLogicError
  24. from toolkit.vs_cloud_api import VSCloudApi
  25. from toolkit.ocr_engine import PyTorchEngine
  26. class BlsPlugin(IVSPlg):
  27. """
  28. BLS 签证预约插件 (精简版)
  29. """
  30. def __init__(self, group_id: str):
  31. self.group_id = group_id
  32. self.config: Optional[VSPlgConfig] = None
  33. self.free_config: Dict[str, Any] = {}
  34. self.logger = None
  35. self.session: Optional[requests.Session] = None
  36. # 运行时状态
  37. self.book_params: Dict = {}
  38. self.is_healthy: bool = True
  39. # 浏览器实例
  40. self.page: Optional[ChromiumPage] = None
  41. # --- [核心修改] 并发隔离与资源管理 ---
  42. # 生成唯一实例 ID
  43. self.instance_id = uuid.uuid4().hex[:8]
  44. self.root_workspace = os.path.abspath(os.path.join("temp_browser_data", f"{self.group_id}_{self.instance_id}"))
  45. # 定义子目录:代理插件目录 & 浏览器用户数据目录
  46. self.user_data_path = os.path.join(self.root_workspace, "user_data")
  47. # 字符识别引擎
  48. self.ocr_engine: Optional[PyTorchEngine] = None
  49. # OCR 服务地址默认值
  50. self.local_service_url: str = ""
  51. self.session_create_time: float = 0
  52. def get_group_id(self) -> str:
  53. return self.group_id
  54. def set_log(self, logger: Callable[[str], None]) -> None:
  55. self.logger = logger
  56. def _log(self, message):
  57. if self.logger:
  58. self.logger(f'[BlsPlugin] [{self.group_id}] {message}')
  59. else:
  60. print(f'[BlsPlugin] [{self.group_id}] {message}')
  61. def set_config(self, config: VSPlgConfig):
  62. self.config = config
  63. self.free_config = config.free_config or {}
  64. # 从配置中读取 OCR 服务地址,如果没有则使用默认
  65. if self.free_config.get("local_service_url"):
  66. self.local_service_url = self.free_config["local_service_url"]
  67. def health_check(self) -> bool:
  68. if not self.is_healthy:
  69. return False
  70. if self.session is None:
  71. return False
  72. if self.config.session_max_life > 0:
  73. current_time = time.time()
  74. elapsed_time = current_time - self.session_create_time
  75. if elapsed_time > self.config.session_max_life * 60:
  76. self._log(f"Session expired.")
  77. return False
  78. return True
  79. def create_session(self):
  80. self._log(f"Initializing Session (ID: {self.instance_id})...")
  81. co = ChromiumOptions()
  82. # -------------------------------------------------------------
  83. # [核心修复] 解决 'not enough values to unpack'
  84. # -------------------------------------------------------------
  85. # 1. 不要用 co.auto_port(),因为它依赖解析 stdout,会被 DBus 报错干扰
  86. # 2. 我们手动随机生成一个端口
  87. import random
  88. import socket
  89. def get_free_port():
  90. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  91. s.bind(('', 0))
  92. return s.getsockname()[1]
  93. debug_port = get_free_port()
  94. self._log(f"Assigned Debug Port: {debug_port}")
  95. # 3. 强制指定端口,DrissionPage 就会直接连接,不再解析日志
  96. co.set_local_port(debug_port)
  97. # --- [关键配置] 设置独立的用户数据目录 ---
  98. # 这样每个实例的 Cache, Cookies, LocalStorage 都是完全隔离的
  99. # 同时也防止了多进程争抢同一个 Default 文件夹导致的崩溃
  100. co.set_user_data_path(self.user_data_path)
  101. # --- 1. 指定浏览器路径 (适配 Docker) ---
  102. chrome_path = os.getenv("CHROME_BIN")
  103. if chrome_path and os.path.exists(chrome_path):
  104. co.set_paths(browser_path=chrome_path)
  105. co.headless(False)
  106. co.set_argument('--no-sandbox')
  107. co.set_argument('--disable-gpu')
  108. # Docker 默认 /dev/shm 只有 64MB,Chromium 很容易爆内存崩溃
  109. co.set_argument('--disable-dev-shm-usage')
  110. co.set_argument('--window-size=1920,1080')
  111. co.set_argument('--disable-blink-features=AutomationControlled')
  112. try:
  113. self.page = ChromiumPage(co)
  114. except Exception as e:
  115. self._log(f"Session Create Error: {e}")
  116. self.cleanup()
  117. raise e
  118. self.ocr_engine = PyTorchEngine(self.free_config.get('ocr_model'))
  119. self.session = requests.Session(
  120. proxy=self._get_proxy_url(),
  121. impersonate="chrome124",
  122. curl_options={
  123. const.CurlOpt.MAXAGE_CONN: 1800,
  124. const.CurlOpt.VERBOSE: self.config.debug
  125. }
  126. )
  127. domain = self.free_config.get("domain")
  128. if not domain:
  129. raise NotFoundError(message="Required field [domain] in free config")
  130. # 1.1 获取登录页 & 解析参数
  131. login_url = f"https://{domain}/Global/account/login"
  132. resp = self._perform_request('GET', login_url)
  133. if self.config.debug:
  134. self._save_debug_html(resp.text, prefix="Bls_Login_Page")
  135. soup = BeautifulSoup(resp.text, 'html.parser')
  136. form_data = self._extract_hidden_fields(soup)
  137. real_user = None
  138. real_pass = None
  139. # 解析动态 ID (UserId1, Password1 等)
  140. for inp in soup.find_all('input'):
  141. name = inp.get('name', '')
  142. if inp.has_attr('required'):
  143. if 'UserId' in name:
  144. real_user = name
  145. elif 'Password' in name:
  146. real_pass = name
  147. # 解析 data 参数 (用于验证码)
  148. data_val = self._extract_js_var(resp.text, "iframeOpenUrl", r"data=([^']+)")
  149. # 1.2 处理验证码
  150. captcha_token = self._solve_bls_captcha(data_val)
  151. # 1.3 提交登录
  152. submit_url = f"https://{domain}/Global/account/loginsubmit"
  153. payload = form_data
  154. payload["X-Requested-With"] = "XMLHttpRequest"
  155. payload["CaptchaData"] = captcha_token
  156. # 填入账号密码
  157. payload[real_user] = self.config.account.username
  158. payload[real_pass] = self.config.account.password
  159. login_resp = self._perform_request('POST', submit_url, data=payload)
  160. if not login_resp.json()['success']:
  161. raise BizLogicError(message='Login failed')
  162. self.session_create_time = time.time()
  163. self._log("Session created successfully.")
  164. # =========================================================================
  165. # 2. 查询流程 (Query)
  166. # =========================================================================
  167. def query(self) -> VSQueryResult:
  168. res = VSQueryResult()
  169. domain = self.free_config.get("domain")
  170. # 2.1 签证类型验证
  171. url_vtv = f"https://{domain}/Global/bls/visatypeverification"
  172. resp = self._perform_request('GET', url_vtv)
  173. if self.config.debug:
  174. self._save_debug_html(resp.text, prefix="Bls_Visatypeverification_Page")
  175. self._check_resp_is_session_expired_or_invalid('APPLICATION PROCESS', resp)
  176. form_vtv = self._extract_hidden_fields(BeautifulSoup(resp.text, 'html.parser'))
  177. captcha_token = self._solve_bls_captcha()
  178. form_vtv['CaptchaData'] = captcha_token
  179. form_vtv["X-Requested-With"] = "XMLHttpRequest"
  180. vtv_resp = self._perform_request('POST', f"https://{domain}/Global/bls/VisaTypeVerification", data=form_vtv)
  181. if not vtv_resp.json()['success']:
  182. raise BizLogicError(message='Submit VisaTypeVerification Failed')
  183. # 2.2 签证类型选择
  184. return_url = vtv_resp.json()['returnUrl'] # 包含 data=xxx
  185. data_val = re.search(r"data=([^&]+)", return_url).group(1)
  186. url_vt = f"https://{domain}/Global/bls/visatype?data={data_val}"
  187. vt_resp = self._perform_request('GET', url_vt)
  188. if self.config.debug:
  189. self._save_debug_html(resp.text, prefix="Bls_Visatype_Page")
  190. self._check_resp_is_session_expired_or_invalid('APPLICATION PROCESS', resp)
  191. # 这里需要极其复杂的 JS 变量提取 (JS Arrays -> Match Name -> Get ID)
  192. vt_payload = self._construct_visatype_payload(vt_resp.text, BeautifulSoup(vt_resp.text, 'html.parser'))
  193. res.city = self.free_config.get('city', '')
  194. res.country = self.free_config.get('country', '')
  195. res.visa_type = self.free_config.get('visa_type', '')
  196. res.routing_key = self.free_config.get('routing_key', '')
  197. vt_res = self._perform_request('POST', f"https://{domain}/Global/bls/VisaType", data=vt_payload)
  198. if not vt_res.json()['success']:
  199. if not vt_res.json()['available']:
  200. res.success = False
  201. res.availability_status = AvailabilityStatus.NoneAvailable
  202. return res
  203. # 2.3 获取预约参数
  204. final_url = vt_res.json()['returnUrl']
  205. q_params = parse_qs(urlparse(final_url).query)
  206. self.book_params = {k: v[0] for k, v in q_params.items()}
  207. # 2.4 查询日历
  208. url_ma = f"https://{domain}/Global/blsAppointment/ManageAppointment?{urlencode(self.book_params)}"
  209. resp_ma = self._perform_request('GET', url_ma)
  210. if self.config.debug:
  211. self._save_debug_html(resp.text, prefix="Bls_ManageAppointment_Page")
  212. self._check_resp_is_session_expired_or_invalid('APPLICATION PROCESS', resp)
  213. avail_str = self._extract_js_var(resp_ma.text, "var availDates", r"var availDates =(.*?);")
  214. if avail_str:
  215. avail_json = json.loads(avail_str)
  216. # 提取日期
  217. dates = [x['DateText'] for x in avail_json['ad'] if x['SingleSlotAvailable']]
  218. if dates:
  219. res.success = True
  220. res.availability_status = AvailabilityStatus.Available
  221. res.earliest_date = dates[0]
  222. res.availability = [
  223. DateAvailability(
  224. date=d,
  225. times=[],
  226. )
  227. for d in dates
  228. ]
  229. else:
  230. # 查询成功,但没有可用日期
  231. res.success = True
  232. res.availability_status = AvailabilityStatus.NoneAvailable
  233. res.availability = []
  234. return res
  235. raise BizLogicError(message='Query page not found required field [var availDates]')
  236. def book(self, slot_info: VSQueryResult, user_inputs: Dict) -> VSBookResult:
  237. res = VSBookResult()
  238. domain = self.free_config.get("domain")
  239. # 3.1 获取 Manage Page (为了 Token 和 JS 变量)
  240. url_ma = f"https://{domain}/Global/blsAppointment/ManageAppointment?{urlencode(self.book_params)}"
  241. resp_ma = self._perform_request('GET', url_ma)
  242. ma_soup = BeautifulSoup(resp_ma.text, 'html.parser')
  243. ma_form = self._extract_hidden_fields(ma_soup)
  244. req_token = ma_form.get('__RequestVerificationToken')
  245. # 3.2 上传照片
  246. if 'passport_image_url' not in user_inputs:
  247. raise NotFoundError()
  248. photo_bytes = requests.get(user_inputs['passport_image_url']).content
  249. boundary = "----WebKitFormBoundary" + "".join(random.choices(string.ascii_letters + string.digits, k=16))
  250. upload_headers = {
  251. "content-type": f"multipart/form-data; boundary={boundary}",
  252. "requestverificationtoken": req_token,
  253. "x-requested-with": "XMLHttpRequest",
  254. }
  255. body = (f"--{boundary}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"photo.jpg\"\r\n"
  256. f"Content-Type: image/jpeg\r\n\r\n").encode("utf-8") + photo_bytes + f"\r\n--{boundary}--\r\n".encode("utf-8")
  257. up_resp = self.session.post(f"https://{domain}/Global/query/UploadProfileImage", headers=upload_headers, data=body)
  258. if up_resp.status_code !=200:
  259. raise BizLogicError(message='Upload Passport Image failed')
  260. ma_form['ApplicantPhotoId'] = up_resp.json()['fileId']
  261. # 3.3 邮箱 OTP 流程
  262. data_val = self._extract_js_var(resp_ma.text, "win.iframeOpenUrl", r"data=([^&]+)")
  263. # 发送 OTP
  264. headers = {
  265. "X-Requested-With": "XMLHttpRequest"
  266. }
  267. self._perform_request('GET', f"https://{domain}/Global/blsappointment/SendAppointmentVerificationCode?code={data_val}", headers=headers)
  268. # 读取 OTP (Wait 30s max)
  269. otp_code = self._read_otp_email(wait_sec=30)
  270. # 验证 OTP
  271. verify_payload = {
  272. "Code": otp_code,
  273. "Value": ma_form.get('EmailCode'),
  274. "Id": ma_form.get('Id')
  275. }
  276. headers['requestverificationtoken'] = req_token
  277. v_resp = self._perform_request('POST', f"https://{domain}/Global/blsappointment/VerifyEmail", data=verify_payload, headers=headers)
  278. headers.pop('requestverificationtoken')
  279. if not v_resp.json().get('success'):
  280. raise BizLogicError(message='Email verification failed')
  281. ma_form['EmailVerified'] = 'True'
  282. ma_form['EmailVerificationCode'] = otp_code
  283. # 3.4 锁定时间 (简单随机)
  284. target_date = slot_info.earliest_date
  285. # Query Slots in Day
  286. slot_url = f"https://{domain}/Global/blsappointment/GetAvailableSlotsByDate"
  287. # 构造复杂的 query params... 省略部分非关键参数
  288. slot_params = {
  289. "appointmentDate": target_date,
  290. "locationId": ma_form.get("LocationId"),
  291. "categoryId": ma_form.get("AppointmentCategoryId"),
  292. "visaType": ma_form.get("VisaType"),
  293. "visaSubType": ma_form.get("VisaSubTypeId"),
  294. "applicantCount": 1,
  295. "dataSource": ma_form.get("DataSource"),
  296. "missionId": ma_form.get("MissionId")
  297. }
  298. headers['requestverificationtoken'] = req_token
  299. slots_resp = self._perform_request('POST', slot_url, params=slot_params, headers=headers)
  300. headers.pop('requestverificationtoken')
  301. slots_data = sorted(slots_resp.json(), key=lambda x: -x["Count"]) # 选剩余最多的
  302. if not slots_data or slots_data[0]['Count'] <= 0:
  303. self._log('Available slot times not found')
  304. res.success = False
  305. return res
  306. target_time = slots_data[0]['Name']
  307. ma_form['ServerAppointmentDate'] = target_date
  308. ma_form['AppointmentDetailsList'] = '[]'
  309. # 这里的 key 是动态的 ID,需重新解析 ID
  310. date_id = re.search(r'AppointmentDate(\d+)', str(ma_soup)).group(1)
  311. slot_id = re.search(r'AppointmentSlot(\d+)', str(ma_soup)).group(1)
  312. ma_form[f'AppointmentDate{date_id}'] = target_date
  313. ma_form[f'AppointmentSlot{slot_id}'] = target_time
  314. # 3.5 再次验证码 & 提交 ManageAppointment
  315. captcha_token = self._solve_bls_captcha(data_val)
  316. ma_form['CaptchaData'] = captcha_token
  317. final_ma_resp = self._perform_request('POST', f"https://{domain}/Global/BLSAppointment/ManageAppointment", data=ma_form, headers=headers)
  318. appt_model_id = final_ma_resp.json().get('model', {}).get('Id')
  319. if not appt_model_id:
  320. raise NotFoundError(message='Appointment model id not found')
  321. # 3.6 填写申请表 (VisaAppointmentForm)
  322. # 获取页面 -> 解析 JS 变量 -> 映射 UserInfo -> 提交
  323. # 这里逻辑较深,核心是映射。简化为提交一个空的 applicants JSON,实际需完整映射。
  324. # 假设 _fill_applicant_form 做了这些工作
  325. self._submit_final_form(appt_model_id, user_inputs, self.book_params, req_token)
  326. # 成功,返回 Liveness 链接
  327. Liveness_page = f"https://{domain}/Global/BlsAppointment/livenessView?id={appt_model_id}"
  328. session_data = self._save_http_session(Liveness_page)
  329. res.success = True
  330. res.account = self.config.account.username
  331. res.session_id = session_data['session_id']
  332. res.book_date = target_date
  333. res.book_time = target_time
  334. self._log(f"Book Success. Liveness URL: {res.payment_link}")
  335. return res
  336. def _get_proxy_url(self):
  337. # 构造代理
  338. proxy_url = ""
  339. if self.config.proxy.ip:
  340. s = self.config.proxy
  341. if s.username:
  342. proxy_url = f"{s.scheme}://{s.username}:{s.password}@{s.ip}:{s.port}"
  343. else:
  344. proxy_url = f"{s.scheme}://{s.ip}:{s.port}"
  345. return proxy_url
  346. def _save_debug_html(self, content: str, prefix: str = "debug"):
  347. save_dir = "debug_pages"
  348. if not os.path.exists(save_dir):
  349. os.makedirs(save_dir)
  350. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  351. filename = f"{save_dir}/{prefix}_{timestamp}.html"
  352. with open(filename, "w", encoding="utf-8") as f:
  353. f.write(content)
  354. self._log(f"HTML saved to: {filename}")
  355. def _perform_request(self, method, url, headers=None, data=None, json_data=None, params=None):
  356. """
  357. 统一 HTTP 请求封装,严格复刻 C++ 逻辑:
  358. 1. 发送 OPTIONS 请求
  359. 2. 发送实际请求
  360. """
  361. resp = self.session.request(method, url, headers=headers, data=data, json=json_data, params=params, timeout=30)
  362. if self.config.debug:
  363. self._log(f'[perform request] Response={resp.text}\nMethod={method}, Url={url}, Data={data}, JsonData={json_data}, Params={params}')
  364. if resp.status_code == 200:
  365. return resp
  366. elif resp.status_code == 401:
  367. self.is_healthy = False
  368. raise SessionExpiredOrInvalidError()
  369. elif resp.status_code == 403:
  370. raise PermissionDeniedError()
  371. elif resp.status_code == 429:
  372. self.is_healthy = False
  373. raise RateLimiteddError()
  374. else:
  375. raise BizLogicError(message=f"HTTP Error {resp.status_code}: {resp.text[:100]}")
  376. def _extract_captcha_data(self, tmp_file):
  377. # 1. 加载文件
  378. html_file_path = Path(tmp_file).resolve()
  379. self.page.get(f'file://{html_file_path}')
  380. # 2. 定位主容器 (作为后续查找的基准,减少全局扫描)
  381. main_div = self.page.ele('#captcha-main-div', timeout=5)
  382. if not main_div:
  383. raise BizLogicError(message='Captcha main container not found')
  384. # --- 3. 提取提示数字 ---
  385. # 假设结构是 main -> div -> div[1] (header)
  386. # 使用相对 XPath 定位 header 区域
  387. header_ele = main_div.ele('xpath:./div/div[1]')
  388. caption_text = ""
  389. if header_ele:
  390. # 遍历子元素寻找可见的提示语
  391. for child in header_ele.children():
  392. # 这里的 is_displayed 检查是否有大小,is_covered 检查是否被遮挡
  393. if child.states.is_displayed and not child.states.is_covered:
  394. caption_text = child.text
  395. if caption_text: # 找到文本就跳出
  396. break
  397. # 安全提取数字
  398. number_match = re.search(r'\d+', caption_text)
  399. if not number_match:
  400. # 如果没找到数字,返回错误或特定的 status
  401. raise BizLogicError(message="No number found in caption")
  402. number = number_match.group()
  403. # --- 4. 提取图片 ID ---
  404. images_ids = []
  405. # 优化策略:直接查找所有 class 为 captcha-img 的图片元素
  406. # 语法: tag:img @@ class:captcha-img
  407. all_imgs = main_div.eles('tag:img@@class:captcha-img')
  408. for img in all_imgs:
  409. # 1. 检查可见性 (有尺寸且未被遮挡)
  410. if img.states.has_rect and not img.states.is_covered:
  411. # 2. 检查 src 属性
  412. src = img.attr('src')
  413. if src and src.startswith('data:image'):
  414. # 3. 获取父级元素的 ID (根据原逻辑,ID 在 img 的父级容器上)
  415. parent_id = img.parent().attr('id')
  416. if parent_id:
  417. images_ids.append(parent_id)
  418. data = {
  419. "number": number,
  420. "image_ids": images_ids,
  421. }
  422. return data
  423. def _solve_bls_captcha(self, data='') -> Optional[str]:
  424. """
  425. 验证码处理:获取图片 -> 调用远程 OCR 服务 -> 提交验证
  426. """
  427. domain = self.free_config.get("domain")
  428. url = f"https://{domain}/Global/NewCaptcha/GenerateCaptcha"
  429. if data:
  430. url = f"https://{domain}/Global/CaptchaPublic/GenerateCaptcha?data={data}"
  431. resp = self._perform_request("GET", url)
  432. if self.config.debug:
  433. self._save_debug_html(resp.text, prefix="Bls_Captcha_Page")
  434. self._check_resp_is_session_expired_or_invalid('Please select all boxes with number', resp)
  435. tmpfile = os.path.join(self.root_workspace, "tmp.html")
  436. with open(tmpfile, 'w', encoding='utf-8') as tfp:
  437. tfp.write(resp.text)
  438. soup = BeautifulSoup(resp.text, 'html.parser')
  439. extract_data = self._extract_captcha_data(tmpfile)
  440. numbers = extract_data['number']
  441. image_ids = extract_data['image_ids']
  442. selected_ids = []
  443. for sid in image_ids:
  444. div = soup.find("div", id=sid)
  445. img = div.find("img")
  446. src = img.get("src")
  447. base64_data = src.split("base64,", 1)[1]
  448. img_bytes = base64.b64decode(base64_data)
  449. ocr_output = self.ocr_engine.inference_bytes(img_bytes)
  450. ocr_res = ocr_output.replace('$', '')[:3]
  451. self._log(f'ocr captcha id={sid} result={ocr_res}, target={numbers}')
  452. if ocr_res == numbers:
  453. selected_ids.append(sid)
  454. if not selected_ids:
  455. raise BizLogicError(message='Captcha selected ids is empty')
  456. # 3. 提交选中结果
  457. self._log(f'select_ids={selected_ids}')
  458. form = self._extract_hidden_fields(soup)
  459. form['SelectedImages'] = ",".join(selected_ids)
  460. submit_url = f"https://{domain}/Global/{'CaptchaPublic' if data else 'NewCaptcha'}/SubmitCaptcha"
  461. headers = {
  462. "X-Requested-With": "XMLHttpRequest"
  463. }
  464. resp = self._perform_request('POST', submit_url, headers=headers, data=form)
  465. j = resp.json()
  466. if j.get('success'):
  467. if data:
  468. return resp.json()['captcha']
  469. else:
  470. return resp.json()['cd']
  471. else:
  472. # 存盘所有错误验证码后续进行数据分析
  473. self._log('Captcha Selection Invalid, Saving important data to data/bls_captcha')
  474. for img in soup.select("img.captcha-img"):
  475. src = img.get("src", "")
  476. if not src.startswith("data:image"):
  477. continue
  478. b64 = src.split("base64,", 1)[1]
  479. with open(f'data/bls_captcha/{uuid.uuid4().hex}.jpg', "wb") as fp:
  480. fp.write(base64.b64decode(b64))
  481. raise BizLogicError(message="Sovle captcha failed")
  482. def _extract_hidden_fields(self, soup) -> Dict:
  483. params = {}
  484. form = soup.find("form")
  485. if form:
  486. for inp in form.find_all("input"):
  487. name = inp.get("name")
  488. if name: params[name] = inp.get("value", "")
  489. else:
  490. self._log('Form element not found')
  491. return params
  492. def _extract_js_var(self, html, context, pattern):
  493. # 简单正则提取
  494. if context in html:
  495. match = re.search(pattern, html)
  496. if match: return match.group(1)
  497. return ""
  498. def _construct_visatype_payload(self, html: str, soup: BeautifulSoup) -> Optional[Dict]:
  499. """
  500. 构造 VisaType 提交参数 (对应原代码 parse_visatype_form)
  501. """
  502. # 基础表单参数 (__RequestVerificationToken 等)
  503. params = self._extract_hidden_fields(soup)
  504. # 提取页面中的 JS 数据变量
  505. def get_js_data(var_name):
  506. try:
  507. # 匹配 var name = [...]; 结构
  508. pattern = f"var {var_name}\\s*=\\s*(.*?);"
  509. match = re.search(pattern, html, re.DOTALL)
  510. if match:
  511. return json.loads(match.group(1))
  512. except Exception as e:
  513. self._log(f"Failed to parse JS var {var_name}: {e}")
  514. return []
  515. # 读取配置
  516. query_selector = self.free_config.get("query_selector", {})
  517. cfg_jur = query_selector.get("jurisdiction")
  518. cfg_loc = query_selector.get("location")
  519. cfg_type = query_selector.get("visa_type")
  520. cfg_subtype = query_selector.get("visa_subtype")
  521. cfg_cat = query_selector.get("appointment_category")
  522. jur_value = None
  523. loc_value = None
  524. type_value = None
  525. subtype_value = None
  526. cat_value = None
  527. jur_id = None
  528. loc_id = None
  529. type_id = None
  530. subtype_id = None
  531. cat_id = None
  532. tmpfile = os.path.join(self.root_workspace, "tmp.html")
  533. with open(tmpfile, 'w', encoding='utf-8') as tfp:
  534. tfp.write(html)
  535. self.page.get(f'file://{tmpfile}')
  536. # 匹配 ID
  537. app_category_labels = self.page.eles(f'Appointment Category', timeout=1)
  538. for app_category_label in app_category_labels:
  539. if app_category_label.states.has_rect and app_category_label.tag == 'label':
  540. eid = app_category_label.after('tag:input').attr('id')
  541. cat_id = int(''.join(filter(str.isdigit, eid)))
  542. break
  543. jurisdiction_labels = self.page.eles(f'Jurisdiction', timeout=1)
  544. if jurisdiction_labels:
  545. for jurisdiction_label in jurisdiction_labels:
  546. if jurisdiction_label.states.has_rect and jurisdiction_label.tag == 'label':
  547. eid = jurisdiction_label.after('tag:input').attr('id')
  548. jur_id = int(''.join(filter(str.isdigit, eid)))
  549. break
  550. location_labels = self.page.eles(f'Location', timeout=1)
  551. for location_label in location_labels:
  552. if location_label.states.has_rect and location_label.tag == 'label':
  553. eid = location_label.after('tag:input', index=2).attr('id')
  554. loc_id = int(''.join(filter(str.isdigit, eid)))
  555. break
  556. visa_type_labels = self.page.eles(f'Visa Type', timeout=1)
  557. for visa_type_label in visa_type_labels:
  558. if visa_type_label.states.has_rect and visa_type_label.tag == 'label':
  559. eid = visa_type_label.after('tag:input').attr('id')
  560. type_id = int(''.join(filter(str.isdigit, eid)))
  561. break
  562. visa_subtype_labels = self.page.eles(f'Visa Sub Type', timeout=1)
  563. for visa_subtype_label in visa_subtype_labels:
  564. if visa_subtype_label.states.has_rect and visa_subtype_label.tag == 'label':
  565. eid = visa_subtype_label.after('tag:input').attr('id')
  566. subtype_id = int(''.join(filter(str.isdigit, eid)))
  567. break
  568. jurisdiction_list = get_js_data("jurisdictionData")
  569. location_list = get_js_data("locationData")
  570. visa_type_list = get_js_data("visaIdData")
  571. visa_subtype_list = get_js_data("visasubIdData")
  572. app_category_list = get_js_data("AppointmentCategoryIdData")
  573. # 4. 匹配 Value
  574. # (A) Appointment Category
  575. for item in app_category_list:
  576. if item.get("Name") == cfg_cat:
  577. cat_value = item.get("Id")
  578. break
  579. # (B) Jurisdiction (如果配置了)
  580. if cfg_jur and jurisdiction_list:
  581. for item in jurisdiction_list:
  582. if item.get("Name") == cfg_jur:
  583. jur_value = item.get("Id")
  584. break
  585. # (C) Location
  586. for item in location_list:
  587. if item.get("Name") == cfg_loc:
  588. loc_value = item.get("Id")
  589. break
  590. # (D) Visa Type (需匹配 LocationId)
  591. if loc_value:
  592. for item in visa_type_list:
  593. # 比较 Name 和 LocationId
  594. if item.get("Name") == cfg_type and str(item.get("LocationId")) == str(loc_value):
  595. type_value = item.get("Id")
  596. break
  597. # (E) Visa SubType (需匹配 VisaType Value)
  598. if type_value:
  599. for item in visa_subtype_list:
  600. # BLS 逻辑: visasubIdData 中的 Value 字段对应 VisaTypeId
  601. if item.get("Name") == cfg_subtype and str(item.get("Value")) == str(type_value):
  602. subtype_value = item.get("Id")
  603. break
  604. # 5. 构造动态参数 & 校验
  605. if not cat_value:
  606. raise NotFoundError(message=f"Config: AppCategory '{cfg_cat}' not found")
  607. params[f"AppointmentCategoryId{cat_id}"] = cat_value
  608. if cfg_jur:
  609. if not jur_value:
  610. raise NotFoundError(message=f"Config: Jurisdiction '{cfg_jur}' not found")
  611. params[f"JurisdictionId{jur_id}"] = jur_value
  612. if not loc_value:
  613. raise NotFoundError(message=f"Config: Location '{cfg_loc}' not found")
  614. params[f"Location{loc_id}"] = loc_value
  615. if not type_value:
  616. raise NotFoundError(message=f"Config: VisaType '{cfg_type}' not found for Loc '{cfg_loc}'")
  617. params[f"VisaType{type_id}"] = type_value
  618. if not subtype_value:
  619. raise NotFoundError(message=f"Config: VisaSubType '{cfg_subtype}' not found")
  620. params[f"VisaSubType{subtype_id}"] = subtype_value
  621. # 固定参数
  622. for k in list(params.keys()):
  623. if k.startswith("AppointmentFor"):
  624. params[k] = "Individual"
  625. # 6. 构造 ResponseData (行为轨迹模拟)
  626. # BLS 后端会校验这个字段,模拟用户选择下拉框的时间间隔
  627. response_data = []
  628. current_time = datetime.utcnow()
  629. def add_trace(prefix, val_id):
  630. nonlocal current_time
  631. # 模拟 1-3 秒的操作间隔
  632. duration = random.randint(1000, 3000)
  633. gap = random.randint(500, 1500)
  634. start = current_time
  635. end = start + timedelta(milliseconds=duration)
  636. # BLS 时间格式: 2023-10-27T10:00:00.123Z
  637. fmt = "%Y-%m-%dT%H:%M:%S.%f"
  638. response_data.append({
  639. "Id": f"{prefix}{val_id}",
  640. "Start": start.strftime(fmt)[:-3] + "Z",
  641. "End": end.strftime(fmt)[:-3] + "Z",
  642. "Total": duration,
  643. "Selected": True
  644. })
  645. current_time = end + timedelta(milliseconds=gap)
  646. # 按顺序添加轨迹
  647. add_trace("AppointmentCategoryId", cat_id)
  648. if jur_id: add_trace("JurisdictionId", jur_id)
  649. add_trace("Location", loc_id)
  650. add_trace("VisaType", type_id)
  651. add_trace("VisaSubType", subtype_id)
  652. params["ResponseData"] = json.dumps(response_data)
  653. params["X-Requested-With"] = "XMLHttpRequest"
  654. return params
  655. def _submit_final_form(self, model_id: str, user_inputs: Dict, book_params: Dict, token: str):
  656. """
  657. 提交最终签证申请表 (VisaAppointmentForm)
  658. 对应原代码的: get_visa_appointment_form_html -> parse -> fix_data -> submit
  659. """
  660. domain = self.free_config.get("domain")
  661. # 1. 获取表单页面 (为了提取 JS 变量映射表)
  662. url_get = f"https://{domain}/Global/BlsAppointment/VisaAppointmentForm?appointmentId={model_id}"
  663. # 构造 Referer
  664. ref_query = urlencode(book_params)
  665. referer = f"Global/blsAppointment/ManageAppointment?{ref_query}"
  666. headers = {
  667. 'X-Requested-With': "XMLHttpRequest"
  668. }
  669. resp = self._perform_request('GET', url_get, headers=headers)
  670. headers.pop['X-Requested-With']
  671. html = resp.text
  672. soup = BeautifulSoup(resp.text, 'html.parser')
  673. # 2. 提取基础隐藏域 (包含 __RequestVerificationToken 等)
  674. form_data = self._extract_hidden_fields(soup)
  675. # 3. 提取下拉菜单数据源 (JS Variables)
  676. # BLS 的页面里有很多 var countryData = [...]; 这种数据
  677. def get_list(name):
  678. val = self._extract_js_var(html, f"var {name}", rf"var {name}\s*=\s*(.*?);")
  679. return json.loads(val) if val else []
  680. # 提取关键数据源
  681. country_data = get_list("countryData")
  682. gender_data = get_list("genderData")
  683. marital_data = get_list("maritalStatusData")
  684. occupation_data = get_list("occupationData")
  685. # passport_type_data = get_list("passportTypeData") # 通常默认 Ordinary
  686. # 4. 辅助函数:根据文本找 ID
  687. def find_id(data_list, text_val, default=None):
  688. if not text_val: return default
  689. text_val = str(text_val).lower().strip()
  690. for item in data_list:
  691. if str(item.get("Name")).lower() == text_val:
  692. return item.get("Id")
  693. return default
  694. # 5. 准备日期 (YYYY-MM-DD)
  695. # uinfo 中的日期可能是不同格式,需统一
  696. def fmt_date(d_str):
  697. try:
  698. # 尝试解析常见格式
  699. for fmt in ["%Y-%m-%d", "%d/%m/%Y", "%d-%m-%Y"]:
  700. try:
  701. return datetime.strptime(d_str, fmt).strftime("%Y-%m-%d")
  702. except: pass
  703. except: pass
  704. return d_str # 原样返回 fallback
  705. dob = fmt_date(user_inputs.get("birthday", ""))
  706. ppt_issue = fmt_date(user_inputs.get("passport_issue_date", ""))
  707. ppt_expiry = fmt_date(user_inputs.get("passport_expiry_date", ""))
  708. # 自动计算行程日期 (如果未提供,默认一个月后)
  709. try:
  710. travel_date = (datetime.now() + timedelta(days=30)).strftime("%Y-%m-%d")
  711. except: travel_date = ""
  712. # 6. 构造申请人详细数据对象 (JSON)
  713. # 注意:这里的字段名必须严格匹配 BLS 后端实体定义
  714. applicant_detail = {
  715. "ApplicantSerialNo": "1",
  716. "ApplicantId": form_data.get("applicantId", "0"), # 从页面隐藏域提取
  717. "Id": form_data.get("applicantId", "0"),
  718. "ParentId": form_data.get("Id", model_id), # 关联的 Appointment ID
  719. # 基本信息
  720. "FirstName": user_inputs.get("first_name", ""),
  721. "SurName": user_inputs.get("last_name", ""),
  722. "LastName": user_inputs.get("last_name", ""),
  723. "SurnameAtBirth": user_inputs.get("last_name", ""), # 默认同名
  724. "GenderId": find_id(gender_data, user_inputs.get("gender"), "1"), # 默认 Male
  725. "MaritalStatusId": find_id(marital_data, user_inputs.get("marital_status", "Single"), "1"),
  726. "ServerDateOfBirth": dob,
  727. # 国籍/出生地
  728. "PlaceOfBirth": user_inputs.get("place_of_birth", "-"),
  729. "CountryOfBirthId": find_id(country_data, user_inputs.get("nationality"), "0"),
  730. "NationalityAtBirthId": find_id(country_data, user_inputs.get("nationality"), "0"),
  731. "NationalityId": find_id(country_data, user_inputs.get("nationality"), "0"),
  732. # 护照信息
  733. "PassportType": "Ordinary Passport", # 默认
  734. "PassportNo": user_inputs.get("passport_no", ""),
  735. "ServerPassportIssueDate": ppt_issue,
  736. "ServerPassportExpiryDate": ppt_expiry,
  737. "IssuePlace": user_inputs.get("place_of_issue", "-"),
  738. "IssueCountryId": find_id(country_data, user_inputs.get("nationality"), "0"),
  739. # 联系方式 (必填占位符)
  740. "HomeAddressLine1": "-",
  741. "HomeAddressCity": "-",
  742. "HomeAddressPostalCode": "-",
  743. "HomeAddressContactNumber": user_inputs.get("phone", "-"),
  744. "HomeAddressCountryId": find_id(country_data, user_inputs.get("nationality"), "0"),
  745. "EmployerName": "-",
  746. "EmployerAddress": "-",
  747. # 职业
  748. "CurrentOccupationId": find_id(occupation_data, user_inputs.get("occupation", "Others"), "20"),
  749. # 行程信息 (部分写死为常规旅游)
  750. "PurposeOfJourneyId": "Tourism",
  751. "MemberStateDestinationId": "Spain",
  752. "MemberStateFirstEntryId": "Spain",
  753. "NumberOfEntriesRequested": "Multiple Entries",
  754. "IntendedStayDuration": "5",
  755. "ServerTravelDate": travel_date,
  756. "ServerIntendedDateOfArrival": travel_date,
  757. "ServerIntendedDateOfDeparture": travel_date, # 简化
  758. # 费用承担
  759. "CostCoveredById": "By the Applicant himself / herself",
  760. "MeansOfSupportId": "Cash",
  761. # 杂项
  762. "IsMinor": False,
  763. "IsVisaIssuedBefore": False,
  764. "BlsInvitingAuthority": "1", # 这里的 1 通常代表 "No" 或者特定枚举
  765. "PreviousFingerPrintStatus": "2", # 2 通常代表 No
  766. # 邀请人信息 (旅游通常填酒店或空)
  767. "InvitingAuthorityName": "-",
  768. "InvitingAddress": "-",
  769. "InvitingCity": "-",
  770. "InvitingEmail": "no-reply@example.com"
  771. }
  772. # 7. 更新表单数据
  773. # ApplicantsDetailsList 需要是一个 JSON 字符串
  774. form_data['ApplicantsDetailsList'] = json.dumps([applicant_detail])
  775. # 补全其他可能需要的字段
  776. form_data['PreviousFingerPrintStatus_0'] = "2"
  777. form_data['BlsInvitingAuthority_0'] = "1"
  778. form_data["X-Requested-With"] = "XMLHttpRequest"
  779. # 8. 提交
  780. # 注意:提交地址通常和 manage appointment 相同,或者是特定的 Save 接口
  781. # 根据你的原代码,是 Global/BLSAppointment/ManageAppointment
  782. url_post = f"https://{domain}/Global/BLSAppointment/ManageAppointment"
  783. # Headers 需要 Token
  784. headers = {
  785. "Referer": f"https://{domain}/{referer}",
  786. "X-Requested-With": "XMLHttpRequest",
  787. "requestverificationtoken": token
  788. }
  789. # 这里的 form_data['params'] 逻辑在 _extract_hidden_fields 可能会有差异
  790. # 确保 form_data 是扁平的字典
  791. submit_resp = self._perform_request('POST', url_post, data=form_data, headers=headers)
  792. if submit_resp.json().get('success'):
  793. self._log("Final Form Submitted Successfully.")
  794. return True
  795. raise BizLogicError(message='Submit application form failed')
  796. def _read_otp_email(self, wait_sec: int = 60) -> str:
  797. """
  798. 读取 BLS 的 OTP 邮件
  799. """
  800. master_email = "visafly666@gmail.com"
  801. recipient = self.config.account.username
  802. sender = "Info@blsinternational.com"
  803. subject_keywords = "BLS"
  804. body_keywords = "verification code"
  805. # 设置时间起点 (UTC)
  806. now_utc = datetime.utcnow()
  807. formatted_utc_time = now_utc.strftime("%Y-%m-%d %H:%M:%S")
  808. self._log(f"Waiting for OTP from {sender}...")
  809. # 轮询查收, 每 5 秒查一次
  810. attempts = wait_sec // 5
  811. for i in range(attempts):
  812. # 调用云端接口获取邮件内容
  813. # expiry=300 表示邮件有效搜索窗口为 5 分钟
  814. content_out = VSCloudApi.Instance().fetch_mail_content(
  815. master_email,
  816. sender,
  817. recipient,
  818. subject_keywords,
  819. body_keywords,
  820. formatted_utc_time,
  821. 300
  822. )
  823. # 正则匹配 6 位数字验证码
  824. match = re.search(r'\b\d{6}\b', content_out)
  825. if match:
  826. otp = match.group(0)
  827. self._log("OTP code found: {otp}")
  828. return otp
  829. # 等待下一次轮询
  830. time.sleep(5)
  831. if i % 2 == 0:
  832. self._log("OTP not received yet, retrying...")
  833. # 超时处理
  834. raise NotFoundError(f"OTP email not found within {wait_sec}s")
  835. def _check_resp_is_session_expired_or_invalid(self, keyword, resp) -> bool:
  836. """
  837. 检测是否发生了 Session 过期
  838. """
  839. # 1. 检查最终 URL 是否包含登录页特征
  840. # 这里的判断依据是你提供的日志:Redirect to /Global/Account/LogIn
  841. if "/Account/LogIn" in resp.url or "/Account/Login" in resp.url:
  842. self.is_healthy = False
  843. raise SessionExpiredOrInvalidError()
  844. # 2. (备用) 如果 _perform_request 禁止了重定向,检查 302 Location
  845. if resp.status_code == 302:
  846. location = resp.headers.get("Location", "")
  847. if "/Account/LogIn" in location or "/Account/Login" in location:
  848. self.is_healthy = False
  849. raise SessionExpiredOrInvalidError()
  850. resp_text = resp.text
  851. if not resp_text:
  852. self.is_healthy = False
  853. raise SessionExpiredOrInvalidError()
  854. if keyword not in resp_text:
  855. if 'your session has expired, please login again.' in resp_text.lower():
  856. self.is_healthy = False
  857. raise SessionExpiredOrInvalidError()
  858. def _save_http_session(self, page_url):
  859. """
  860. 提取 cookies, local_storage, 存入 VSCloudApi
  861. """
  862. cookies_dict = {}
  863. # 方式 1: curl_cffi 的 cookies 对象通常支持 get_dict()
  864. if hasattr(self.session.cookies, "get_dict"):
  865. cookies_dict = self.session.cookies.get_dict()
  866. else:
  867. # 方式 2: 迭代 (兼容标准 CookieJar)
  868. for c in self.session.cookies:
  869. cookies_dict[c.name] = c.value
  870. cookies_str = json.dumps(cookies_dict)
  871. # 简单生成 SessionID hash
  872. ua_str = self.user_agent or "unknown_ua"
  873. raw = cookies_str + ua_str + page_url
  874. session_id = hashes.Hash(hashes.SHA256(), backend=default_backend())
  875. session_id.update(raw.encode())
  876. sid = session_id.finalize().hex()
  877. proxy_str = ""
  878. if self.config.proxy.ip:
  879. proxy_str = f"{self.config.proxy.scheme}://"
  880. if self.config.proxy.username:
  881. proxy_str += f"{self.config.proxy.username}:{self.config.proxy.password}@"
  882. proxy_str += f"{self.config.proxy.ip}:{self.config.proxy.port}"
  883. return VSCloudApi.Instance().create_http_session(
  884. sid, cookies_str, "", ua_str, proxy_str, page_url
  885. )
  886. # --- 资源清理核心方法 ---
  887. def cleanup(self):
  888. """
  889. 销毁浏览器并彻底删除临时文件
  890. """
  891. # 1. 关闭浏览器
  892. if self.page:
  893. try:
  894. self.page.quit() # 这会关闭 Chrome 进程
  895. except Exception:
  896. pass # 忽略已关闭的错误
  897. self.page = None
  898. # 2. 删除文件
  899. # 注意:Chrome 关闭后可能需要几百毫秒释放文件锁,稍微等待
  900. if os.path.exists(self.root_workspace):
  901. for _ in range(3):
  902. try:
  903. time.sleep(0.2)
  904. shutil.rmtree(self.root_workspace, ignore_errors=True)
  905. break
  906. except Exception as e:
  907. # 如果删除失败(通常是Windows文件占用),重试
  908. self._log(f"Cleanup retry: {e}")
  909. time.sleep(0.5)
  910. # 如果依然存在,打印警告(虽然 ignore_errors=True 会掩盖报错,但可以 check exists)
  911. if os.path.exists(self.root_workspace):
  912. self._log(f"[WARN] Failed to fully remove workspace: {self.root_workspace}")
  913. def __del__(self):
  914. """
  915. 析构函数:当对象被垃圾回收时自动调用
  916. """
  917. self.cleanup()