vfs_plugin.py 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  1. # plugins/vfs_plugin2.py
  2. import os
  3. import time
  4. import json
  5. import random
  6. import base64
  7. import uuid
  8. import re
  9. import urllib.parse
  10. from datetime import datetime
  11. from typing import Dict, Any, Optional, List, Tuple, Callable
  12. from DrissionPage import ChromiumPage, ChromiumOptions
  13. from cryptography.hazmat.primitives import serialization, hashes
  14. from cryptography.hazmat.primitives.asymmetric import padding
  15. from cryptography.hazmat.backends import default_backend
  16. from plugin import IPlugin
  17. from core_types import (
  18. PluginConfig,
  19. AppointmentType,
  20. QueryResult,
  21. BookResult,
  22. DateAvailability,
  23. AvailabilityStatus,
  24. NotFoundError,
  25. PermissionDeniedError,
  26. RateLimiteddError,
  27. SessionExpiredOrInvalidError,
  28. BizLogicError,
  29. PluginStatus,
  30. utc_now_str
  31. )
  32. from toolkit.cloud_api import CloudApi
  33. from toolkit.captcha_breaker import CaptchaBreaker, CaptchaBreakerError
  34. from utils.cloudflare_bypass_for_scraping import CloudflareBypasser
  35. VFS_PUBLIC_KEY_PEM = """-----BEGIN PUBLIC KEY-----
  36. MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuupFgB+lYIOtSxrRoHzc
  37. LmCZKJ6+oSbgqgOPzFMM0TasOeLw0NXEn1XfIzXdx75+tegNKwyIZumoh0yhubKs
  38. t59GV321kN0iquYRHrdh3ygfDDHlS9rROQeBqRga0ncSADtbLMrBPqXJjPCoV76y
  39. t92towriKoH75BhiazY0mghm4LjmAWrV0u/GNpV3tk9bxbtHEXGaFmxCJqjg+7x6
  40. 1e5wXLfvpj9w1QsiSWOSJxLOyICz/9ByxXycQQFdNmjnnnwco9Gt/Mi33NYH71j0
  41. 5oXIjklFC4lvJqaqSY5lS7Vwb9oCt9zX9J0Yz4z4e/3V+0jgRnWOFGofyks4FKe2
  42. GQIDAQAB
  43. -----END PUBLIC KEY-----"""
  44. # VFS Cloudflare Turnstile 官方 Sitekey 与极轻量纯净入口
  45. VFS_TURNSTILE_SITEKEY = "0x4AAAAAABhlz7Ei4byodYjs"
  46. VFS_CLEAN_ENTRY_URL = "https://visa.vfsglobal.com/404"
  47. COUNTRY_MAP = {
  48. "afghanistan": "AFG", "albania": "ALB", "algeria": "DZA", "andorra": "AND", "angola": "AGO",
  49. "antigua and barbuda": "ATG", "argentina": "ARG", "armenia": "ARM", "australia": "AUS", "austria": "AUT",
  50. "azerbaijan": "AZE", "bahamas": "BHS", "bahrain": "BHR", "bangladesh": "BGD", "barbados": "BRB", "belarus": "BLR",
  51. "belgium": "BEL", "belize": "BLZ", "benin": "BEN", "bhutan": "BTN", "bolivia": "BOL", "bosnia and herzegovina": "BIH",
  52. "botswana": "BWA", "brazil": "BRA", "brunei": "BRN", "bulgaria": "BGR", "burkina faso": "BFA", "burundi": "BDI",
  53. "cabo verde": "CPV", "cambodia": "KHM", "cameroon": "CMR", "canada": "CAN", "central african republic": "CAF",
  54. "chad": "TCD", "chile": "CHL", "china": "CHN", "colombia": "COL", "comoros": "COM", "congo (brazzaville)": "COG",
  55. "congo (kinshasa)": "COD", "costa rica": "CRI", "croatia": "HRV", "cuba": "CUB", "cyprus": "CYP", "czech republic": "CZE",
  56. "denmark": "DNK", "djibouti": "DJI", "dominica": "DMA", "dominican republic": "DOM", "ecuador": "ECU", "egypt": "EGY",
  57. "el salvador": "SLV", "equatorial guinea": "GNQ", "eritrea": "ERI", "estonia": "EST", "eswatini": "SWZ", "ethiopia": "ETH",
  58. "fiji": "FJI", "finland": "FIN", "france": "FRA", "gabon": "GAB", "gambia": "GMB", "georgia": "GEO", "germany": "DEU",
  59. "ghana": "GHA", "greece": "GRC", "grenada": "GRD", "guatemala": "GTM", "guinea": "GIN", "guinea-bissau": "GNB", "guyana": "GUY",
  60. "haiti": "HTI", "honduras": "HND", "hungary": "HUN", "iceland": "ISL", "india": "IND", "indonesia": "IDN", "iran": "IRN",
  61. "iraq": "IRQ", "ireland": "IRL", "israel": "ISR", "italy": "ITA", "jamaica": "JAM", "japan": "JPN", "jordan": "JOR",
  62. "kazakhstan": "KAZ", "kenya": "KEN", "kiribati": "KIR", "korea, north": "PRK", "korea, south": "KOR", "kuwait": "KWT",
  63. "kyrgyzstan": "KGZ", "laos": "LAO", "latvia": "LVA", "lebanon": "LBN", "lesotho": "LSO", "liberia": "LBR", "libya": "LBY",
  64. "liechtenstein": "LIE", "lithuania": "LTU", "luxembourg": "LUX", "madagascar": "MDG", "malawi": "MWI", "malaysia": "MYS",
  65. "maldives": "MDV", "mali": "MLI", "malta": "MLT", "marshall islands": "MHL", "mauritania": "MRT", "mauritius": "MUS",
  66. "mexico": "MEX", "micronesia": "FSM", "moldova": "MDA", "monaco": "MCO", "mongolia": "MNG", "montenegro": "MNE", "morocco": "MAR",
  67. "mozambique": "MOZ", "myanmar": "MMR", "namibia": "NAM", "nauru": "NRU", "nepal": "NPL", "netherlands": "NLD", "new zealand": "NZL",
  68. "nicaragua": "NIC", "niger": "NER", "nigeria": "NGA", "north macedonia": "MKD", "norway": "NOR", "oman": "OMN", "pakistan": "PAK",
  69. "palau": "PLW", "panama": "PAN", "papua new guinea": "PNG", "paraguay": "PRY", "peru": "PER", "philippines": "PHL", "poland": "POL",
  70. "portugal": "PRT", "qatar": "QAT", "romania": "ROU", "russia": "RUS", "rwanda": "RWA", "saudi arabia": "SAU", "senegal": "SEN",
  71. "serbia": "SRB", "seychelles": "SYC", "sierra leone": "SLE", "singapore": "SGP", "slovakia": "SVK", "slovenia": "SVN",
  72. "solomon islands": "SLB", "somalia": "SOM", "south africa": "ZAF", "spain": "ESP", "sri lanka": "LKA", "sudan": "SDN",
  73. "suriname": "SUR", "sweden": "SWE", "switzerland": "CHE", "syria": "SYR", "tajikistan": "TJK", "tanzania": "TZA", "thailand": "THA",
  74. "timor-leste": "TLS", "togo": "TGO", "tonga": "TON", "tunisia": "TUN", "turkey": "TUR", "turkmenistan": "TKM", "uganda": "UGA",
  75. "ukraine": "UKR", "united arab emirates": "ARE", "united kingdom": "GBR", "united states": "USA", "uruguay": "URY", "uzbekistan": "UZB",
  76. "vanuatu": "VUT", "venezuela": "VEN", "vietnam": "VNM", "yemen": "YEM", "zambia": "ZMB", "zimbabwe": "ZWE"
  77. }
  78. def get_country_iso3(name: str) -> str:
  79. return COUNTRY_MAP.get(name.lower(), "CHN")
  80. def to_yyyymmdd(data_str: str, date_str_format: str, target_format: str="%Y-%m-%d"):
  81. try:
  82. dt = datetime.strptime(data_str, date_str_format)
  83. return dt.strftime(target_format)
  84. except:
  85. return data_str
  86. def get_alias_email(email: str, new_domain: str = "text.skin") -> str:
  87. """
  88. 将邮箱域名替换为指定域名(默认 text.skin)
  89. """
  90. if "@" not in email:
  91. raise ValueError(f"Invalid email: {email}")
  92. local_part, _ = email.rsplit("@", 1)
  93. return f"{local_part}@{new_domain}"
  94. from utils.plugin_utils import (
  95. BrowserResponse,
  96. init_chromium_options,
  97. perform_browser_fetch,
  98. filter_dates_in_range,
  99. handle_cookie_consent,
  100. cleanup_browser_workspace
  101. )
  102. class VfsPlugin(IPlugin):
  103. name = "vfs_plugin"
  104. version = "1.0.1"
  105. description = "VFS Global全球多国签证预约插件 (支持多中心、多签证类型与CF破解)"
  106. def __init__(self, group_id: str):
  107. self.group_id = group_id
  108. self.config: Optional[PluginConfig] = None
  109. self.free_config: Dict[str, Any] = {}
  110. self.logger = None
  111. self.page: Optional[ChromiumPage] = None
  112. self.jwt_token: str = ""
  113. self.real_ip: str = ""
  114. self.is_healthy: bool = True
  115. self.center_conf = None
  116. self.category_conf: Dict = {}
  117. self.subcategory_conf: Dict = {}
  118. self.booking_wait_applied = False
  119. self.public_key = serialization.load_pem_public_key(
  120. VFS_PUBLIC_KEY_PEM.encode(),
  121. backend=default_backend()
  122. )
  123. self.instance_id = uuid.uuid4().hex[:8]
  124. self.root_workspace = os.path.abspath(os.path.join("data/temp_browser_data", f"{self.group_id}.{self.instance_id}"))
  125. self.user_data_path = os.path.join(self.root_workspace, "user_data")
  126. self.vfs_api_domain = 'lift-api.vfsglobal.com'
  127. if not os.path.exists(self.root_workspace):
  128. os.makedirs(self.root_workspace)
  129. self.tunnel = None
  130. self.session_create_time: float = 0
  131. self.captcha_breaker = CaptchaBreaker()
  132. self.sitekey = VFS_TURNSTILE_SITEKEY
  133. self.injected = False
  134. def set_config(self, config: PluginConfig):
  135. self.config = config
  136. self.free_config = config.free_config or {}
  137. if self.free_config.get('country_code') == 'chn':
  138. self.vfs_api_domain = 'lift-apicn.vfsglobal.com'
  139. def set_log(self, logger: Callable[[str], None]) -> None:
  140. self.logger = logger
  141. def _log(self, message):
  142. if self.logger:
  143. self.logger(f'[VfsPlugin] [{self.group_id}] [{self.instance_id}] {message}')
  144. else:
  145. print(f'[VfsPlugin] [{self.group_id}] [{self.instance_id}] {message}')
  146. def keep_alive(self):
  147. try:
  148. keep_alive_funcs = [
  149. self._get_application,
  150. self._query_center,
  151. ]
  152. random.choice(keep_alive_funcs)()
  153. self._log(f'keep_alive request, status ok')
  154. except Exception as e:
  155. self.is_healthy = False
  156. self._log(f'keep_alive failed: {e}')
  157. def health_check(self) -> bool:
  158. if not self.is_healthy:
  159. return False
  160. if self.page is None:
  161. return False
  162. try:
  163. if not self.page.run_js("return 1;"):
  164. return False
  165. except:
  166. return False
  167. if self.config.session_max_life > 0:
  168. current_time = time.time()
  169. elapsed_time = current_time - self.session_create_time
  170. if elapsed_time > self.config.session_max_life:
  171. self._log(f"Session expired.")
  172. return False
  173. return True
  174. def _prepare_clean_page(self):
  175. """进入极轻量 404 页面并注入独占 Turnstile 容器 (绕过复杂首页)"""
  176. if self.injected:
  177. self._log("Resetting existing Turnstile widget...")
  178. self.page.run_js("""
  179. window.cf_token = null;
  180. var st = document.getElementById('status');
  181. if(st) { st.innerText = 'Resetting verification...'; st.style.color = '#666'; }
  182. try {
  183. if (window.turnstile) {
  184. if (window.my_widget_id !== undefined) {
  185. window.turnstile.reset(window.my_widget_id);
  186. } else {
  187. window.turnstile.reset();
  188. }
  189. }
  190. } catch(e) {}
  191. """)
  192. return
  193. # 1. 先打开 login 建立会话上下文与 Cloudflare 信任凭证
  194. mission = self.free_config.get("mission_code", "")
  195. country = self.free_config.get("country_code", "")
  196. lang = self.free_config.get("language", "en")
  197. if country and mission:
  198. login_url = f"https://visa.vfsglobal.com/{country}/{lang}/{mission}/login"
  199. else:
  200. login_url = "https://visa.vfsglobal.com/irl/en/dnk/login"
  201. self._log(f"Establishing session context via login portal: {login_url}")
  202. self.page.set.timeouts(page_load=30, script=20)
  203. try:
  204. self.page.get(login_url)
  205. time.sleep(3)
  206. # 处理 Cloudflare 拦截或等待室
  207. cf_bypasser = CloudflareBypasser(self.page, log=self.config.debug if self.config else True)
  208. if 'Attention Required! | Cloudflare' in self.page.title and 'Sorry, you have been blocked' in self.page.html:
  209. self._log('Block by cloudflare, try refresh...')
  210. self.page.refresh()
  211. self.page.wait.doc_loaded()
  212. cf_bypasser.handle_waiting_room()
  213. # 处理 OneTrust Cookie 弹窗
  214. cookie_btn = self.page.ele('#onetrust-accept-btn-handler', timeout=3)
  215. if cookie_btn and cookie_btn.states.is_displayed:
  216. cookie_btn.click()
  217. self._log("Accepted OneTrust cookie consent.")
  218. time.sleep(0.5)
  219. except Exception as e:
  220. self._log(f"Handling login portal context warning: {e}")
  221. # 强力清除任何可能阻挡交互的遮罩层
  222. try:
  223. self.page.run_js("""
  224. var ot = document.getElementById('onetrust-consent-sdk');
  225. if (ot) ot.remove();
  226. var ov = document.querySelector('.onetrust-pc-dark-filter');
  227. if (ov) ov.remove();
  228. """)
  229. except Exception:
  230. pass
  231. # 2. 建立上下文后,进入轻量页面准备注入 Turnstile
  232. self._log(f"Loading lightweight origin endpoint: {VFS_CLEAN_ENTRY_URL}")
  233. self.page.set.timeouts(page_load=15, script=15)
  234. self.page.get(VFS_CLEAN_ENTRY_URL)
  235. time.sleep(1)
  236. self._log("Injecting standalone Cloudflare Turnstile into empty page...")
  237. inject_js = f"""
  238. if (!document.body) {{
  239. var b = document.createElement('body');
  240. if (document.documentElement) {{
  241. document.documentElement.appendChild(b);
  242. }} else {{
  243. document.appendChild(b);
  244. }}
  245. }}
  246. document.body.innerHTML = `
  247. <div style="display:flex;flex-direction:column;align-items:center;justify-content:center;height:100vh;background:#f5f7fa;font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,sans-serif;">
  248. <div style="background:#ffffff;padding:40px 30px;border-radius:12px;box-shadow:0 4px 20px rgba(0,0,0,0.08);text-align:center;min-width:340px;">
  249. <h2 style="margin:0 0 10px 0;color:#1e293b;font-size:20px;">VFS Pure Turnstile</h2>
  250. <p style="margin:0 0 25px 0;color:#64748b;font-size:13px;">Domain Origin: visa.vfsglobal.com</p>
  251. <div id="my-turnstile" style="display:flex;justify-content:center;margin-bottom:20px;"></div>
  252. <div id="status" style="font-size:14px;font-weight:500;color:#64748b;">Initializing Turnstile...</div>
  253. </div>
  254. </div>
  255. `;
  256. window.cf_token = null;
  257. window.my_widget_id = undefined;
  258. function initTurnstile() {{
  259. try {{
  260. window.my_widget_id = window.turnstile.render('#my-turnstile', {{
  261. sitekey: '{self.sitekey}',
  262. callback: function(token) {{
  263. window.cf_token = token;
  264. var st = document.getElementById('status');
  265. if (st) {{
  266. st.innerText = '✅ Verified! Token obtained.';
  267. st.style.color = '#16a34a';
  268. }}
  269. }},
  270. 'error-callback': function() {{
  271. var st = document.getElementById('status');
  272. if (st) {{
  273. st.innerText = '❌ Verification failed. Retrying...';
  274. st.style.color = '#dc2626';
  275. }}
  276. }}
  277. }});
  278. var st = document.getElementById('status');
  279. if (st) st.innerText = 'Please complete verification...';
  280. }} catch(e) {{
  281. console.error("Render error:", e);
  282. }}
  283. }}
  284. if (window.turnstile) {{
  285. initTurnstile();
  286. }} else {{
  287. var script = document.createElement('script');
  288. script.src = 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit';
  289. script.onload = initTurnstile;
  290. document.head.appendChild(script);
  291. }}
  292. """
  293. self.page.run_js(inject_js)
  294. self.injected = True
  295. self._log("Turnstile injected successfully.")
  296. def _get_turnstile_token(self, timeout_sec: int = 60) -> Optional[str]:
  297. """等待或自动点击 Turnstile 获取 token"""
  298. self._log("Waiting for Turnstile verification...")
  299. cf_bypasser = CloudflareBypasser(self.page, log=self.config.debug if self.config else True)
  300. start_t = time.time()
  301. last_click_t = 0.0
  302. while time.time() - start_t < timeout_sec:
  303. try:
  304. js_check = """
  305. if (window.cf_token) return window.cf_token;
  306. var el = document.querySelector('input[name="cf-turnstile-response"]');
  307. if (el && el.value) return el.value;
  308. return null;
  309. """
  310. token = self.page.run_js(js_check)
  311. if token:
  312. self._log("✅ Turnstile token acquired!")
  313. return token
  314. except Exception:
  315. pass
  316. try:
  317. ele = self.page.ele('@name=cf-turnstile-response', timeout=0.1)
  318. if ele and ele.value:
  319. self._log("✅ Turnstile token acquired from input tag!")
  320. return ele.value
  321. except Exception:
  322. pass
  323. # 尝试点击验证复选框 (点击后至少保持 6 秒冷静期避免打断正在计算的验证)
  324. now = time.time()
  325. if now - start_t > 3 and now - last_click_t > 6:
  326. try:
  327. res = cf_bypasser.click_verification_button(is_dfs=False)
  328. if res:
  329. last_click_t = now
  330. except Exception:
  331. pass
  332. time.sleep(0.8)
  333. return None
  334. @property
  335. def bypass_landing_page(self) -> bool:
  336. """是否绕过首页加载(默认 True,兼容 bypass_landing_page 与 bypass_loading_page 配置)"""
  337. if "bypass_landing_page" in self.free_config:
  338. return bool(self.free_config.get("bypass_landing_page"))
  339. if "bypass_loading_page" in self.free_config:
  340. return bool(self.free_config.get("bypass_loading_page"))
  341. return True
  342. def create_session(self) -> None:
  343. """
  344. 使用 DrissionPage 创建会话:
  345. 1. 启动浏览器
  346. 2. 根据 bypass_landing_page 开关选择建立登录会话上下文并进入轻量页面或官方登录首页
  347. 3. 提取 Turnstile Token
  348. 4. JS fetch 登录
  349. """
  350. co, self.tunnel = init_chromium_options(
  351. user_data_path=self.user_data_path,
  352. username_seed=self.config.account.username if self.config and self.config.account else None,
  353. proxy=self.config.proxy if self.config else None,
  354. headless=False,
  355. window_size="1920,1080",
  356. logger=self._log
  357. )
  358. self.page = ChromiumPage(co)
  359. self.page.set.timeouts(15)
  360. mission = self.free_config.get("mission_code", "")
  361. country = self.free_config.get("country_code", "")
  362. if not mission or not country:
  363. raise BizLogicError("Missing mission/country code config")
  364. if self.bypass_landing_page:
  365. self._log("Using bypass landing mode: opening login to establish context, then jumping to bypass landing page...")
  366. # 1. 建立登录上下文并跳转至极轻量页面注入独占 Turnstile 容器
  367. self._prepare_clean_page()
  368. # 2. 获取 Turnstile Token
  369. cf_token = self._get_turnstile_token(timeout_sec=60)
  370. else:
  371. lang = self.free_config.get("language", "en")
  372. login_page_url = f"https://visa.vfsglobal.com/{country}/{lang}/{mission}/login"
  373. self._log(f"Using traditional landing page mode. Navigating to {login_page_url}...")
  374. self.page.get(login_page_url)
  375. self._log("Handling Cloudflare challenge...")
  376. cf_bypasser = CloudflareBypasser(self.page, log=self.config.debug if self.config else True)
  377. if 'Attention Required! | Cloudflare' in self.page.title and 'Sorry, you have been blocked' in self.page.html:
  378. self._log('Block by cloudflare, try refresh...')
  379. self.page.refresh()
  380. self.page.wait.doc_loaded()
  381. time.sleep(random.uniform(3, 5))
  382. cf_bypasser.handle_waiting_room()
  383. cf_token = self._extract_turnstile_token_from_landing_page(timeout_sec=40, reset_first=False)
  384. if not cf_token:
  385. self._log("[WARN] Could not extract Turnstile token.")
  386. raise BizLogicError("Could not extract Turnstile token.")
  387. email = self.config.account.username
  388. password = self.config.account.password
  389. enc_password = self._encrypt_password(password)
  390. client_src = self._get_client_source()
  391. orange_src = self._get_orange_source(email)
  392. url = f"https://{self.vfs_api_domain}/user/login"
  393. headers = self._get_common_headers(with_auth=False)
  394. headers.update({
  395. "clientsource": client_src,
  396. "orangex": orange_src
  397. })
  398. data = {
  399. "username": email,
  400. "password": enc_password,
  401. "missioncode": mission,
  402. "countrycode": country,
  403. "languageCode": "en-US",
  404. "captcha_version": "cloudflare-v1",
  405. "captcha_api_key": cf_token
  406. }
  407. now_utc = datetime.utcnow()
  408. sent_at = now_utc.strftime("%Y-%m-%d %H:%M:%S")
  409. self._log("Sending Login Request via Browser Fetch...")
  410. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  411. resp_json = resp.json()
  412. if resp_json.get('accessToken'):
  413. self.jwt_token = resp_json["accessToken"]
  414. self._log("Login successful, JWT obtained.")
  415. elif resp_json.get("enableOTPAuthentication"):
  416. self._log("Login requires OTP.")
  417. otp = self._read_otp_email(sent_at=sent_at)
  418. self._submit_login_otp(cf_token, otp)
  419. else:
  420. raise BizLogicError(f"Login failed: {resp.text[:200]}")
  421. self.session_create_time = time.time()
  422. try:
  423. self.real_ip = self._get_realnetwork_ip()
  424. except:
  425. self.real_ip = self.config.proxy.ip
  426. def query(self, apt_type: AppointmentType) -> QueryResult:
  427. """查询可预约 Slot"""
  428. result = QueryResult()
  429. apt_config = self.free_config.get("apt_configs", {}).get(apt_type.routing_key)
  430. self._fetch_configurations(apt_config)
  431. query_result = self._query_earliest_slot(apt_config)
  432. result.success = False
  433. result.availability_status = AvailabilityStatus.NoneAvailable
  434. if query_result:
  435. result.success = True
  436. if "WaitList" in query_result:
  437. result.availability_status = AvailabilityStatus.Waitlist
  438. else:
  439. earliest_dt = datetime.strptime(query_result, "%Y-%m-%d")
  440. result.availability_status = AvailabilityStatus.Available
  441. result.earliest_date = earliest_dt
  442. result.availability = [DateAvailability(date=earliest_dt, times=[])]
  443. self._log(f"Slot Found! -> {query_result}")
  444. else:
  445. self._log("No slots available.")
  446. return result
  447. def _perform_request(self, method, url, headers=None, data=None, json_data=None, params=None, retry_count=0):
  448. """
  449. 核心方法:在 DrissionPage 浏览器上下文中注入 JS 执行 fetch
  450. 并记录详细的 Traffic 日志用于分析
  451. """
  452. if not self.page:
  453. raise BizLogicError("Browser session not initialized")
  454. if self.config.debug:
  455. self._log(f"┌── [TRAFFIC REQUEST] {method} {url}")
  456. self._log(f"├── Headers: {json.dumps(headers or {})}")
  457. self._log(f"└── Body: {json_data if json_data is not None else data}")
  458. else:
  459. self._log(f"[TRAFFIC REQUEST] {method} {url}")
  460. try:
  461. resp = perform_browser_fetch(
  462. self.page,
  463. method=method,
  464. url=url,
  465. headers=headers,
  466. data=data,
  467. json_data=json_data,
  468. params=params,
  469. timeout=60
  470. )
  471. except Exception as e:
  472. self._log(f"[TRAFFIC ERROR] JS Execution failed: {e}")
  473. raise BizLogicError(f"Browser JS Execution Error: {e}")
  474. resp_preview = resp.text[:1000] + "..." if len(resp.text) > 1000 else resp.text
  475. if self.config.debug:
  476. self._log(f"┌── [TRAFFIC RESPONSE] Status: {resp.status_code}")
  477. self._log(f"└── Body: {resp_preview}")
  478. else:
  479. self._log(f"[TRAFFIC RESPONSE] {method} {url} -> Status: {resp.status_code}")
  480. if resp.status_code == 200:
  481. return resp
  482. elif resp.status_code == 401:
  483. self.is_healthy = False
  484. raise SessionExpiredOrInvalidError(f"401 Unauthorized: {resp.text[:100]}")
  485. elif resp.status_code == 403:
  486. if "Just a moment" in resp.text or "cloudflare" in resp.text.lower():
  487. self._log(f"[TRAFFIC] HTTP 403 (Cloudflare) detected. Re-verifying (Try {retry_count+1}/3)...")
  488. if retry_count < 3:
  489. new_token = self._refresh_turnstile_token()
  490. if new_token:
  491. self._log("[TRAFFIC] In-page verification success. Retrying...")
  492. if json_data and "captcha_api_key" in json_data:
  493. json_data["captcha_api_key"] = new_token
  494. return self._perform_request(method, url, headers, data, json_data, params, retry_count+1)
  495. raise PermissionDeniedError(f"HTTP 403 Forbidden: {resp.text[:100]}")
  496. elif resp.status_code == 429:
  497. self.is_healthy = False
  498. raise RateLimiteddError(f"429 Rate Limit: {resp.text[:100]}")
  499. elif resp.status_code == 0:
  500. raise PermissionDeniedError(f"Network Error (Fetch Failed / WAF Blocked): {resp.text}")
  501. else:
  502. if url.endswith("/login") and resp.status_code == 400:
  503. return resp
  504. raise BizLogicError(message=f"HTTP Error {resp.status_code}: {resp.text[:100]}")
  505. def _get_realnetwork_ip(self):
  506. """
  507. 通过新建标签页获取 IP
  508. 解决 CORS 403 问题:新标签页请求属于 Top-Level Navigation,
  509. 不带 Origin: visa.vfsglobal.com,也不带 credentials,符合 ipify 规则。
  510. """
  511. try:
  512. tab = self.page.new_tab("https://api.ipify.org/?format=json")
  513. if tab.ele('tag:pre'):
  514. json_text = tab.ele('tag:pre').text
  515. else:
  516. json_text = tab.ele('tag:body').text
  517. ip = json.loads(json_text)['ip']
  518. tab.close()
  519. self._log(f"Real Network IP: {ip}")
  520. return ip
  521. except Exception as e:
  522. self._log(f"[WARN] Failed to check IP via new tab: {e}")
  523. try:
  524. if self.page.tabs_count > 1:
  525. tab.close()
  526. except:
  527. pass
  528. return "0.0.0.0"
  529. def _get_common_headers(self, with_auth=True) -> Dict[str, str]:
  530. # DrissionPage 浏览器会自动带上 Origin, Referer, User-Agent, Sec-CH-UA 等,这里只需要补充业务特定的 Headers
  531. mission = self.free_config.get("mission_code", "")
  532. country = self.free_config.get("country_code", "")
  533. lang = self.free_config.get("language", "en")
  534. route = f"{country}/{lang}/{mission}"
  535. h = {
  536. "accept": "application/json, text/plain, */*",
  537. "route": route
  538. }
  539. h["clientsource"] = self._get_client_source()
  540. if with_auth and self.jwt_token:
  541. h["authorize"] = self.jwt_token
  542. return h
  543. def _encrypt_password(self, password: str) -> str:
  544. ciphertext = self.public_key.encrypt(
  545. password.encode(),
  546. padding.OAEP(
  547. mgf=padding.MGF1(algorithm=hashes.SHA256()),
  548. algorithm=hashes.SHA256(),
  549. label=None
  550. )
  551. )
  552. return base64.b64encode(ciphertext).decode()
  553. def _get_orange_source(self, email: str) -> str:
  554. timestamp = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
  555. payload = f"{email};{timestamp}"
  556. return self._encrypt_password(payload)
  557. def _get_client_source(self) -> str:
  558. timestamp = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
  559. payload = f"GA;{timestamp}Z"
  560. return self._encrypt_password(payload)
  561. def _query_earliest_slot(self, apt_config) -> Optional[str]:
  562. url = f"https://{self.vfs_api_domain}/appointment/CheckIsSlotAvailable"
  563. data = {
  564. "missioncode": self.free_config.get("mission_code"),
  565. "countrycode": self.free_config.get("country_code"),
  566. "vacCode": apt_config.get("vac_code"),
  567. "visaCategoryCode": apt_config.get("subcategory_code"),
  568. "roleName": "Individual",
  569. "loginUser": self.config.account.username,
  570. "payCode": ""
  571. }
  572. headers = self._get_common_headers(with_auth=True)
  573. resp = self._perform_request("POST", url, headers=headers, json_data=data, retry_count=2)
  574. if "WaitList" in resp.text:
  575. return "WaitList"
  576. j = resp.json()
  577. if j.get("earliestSlotLists"):
  578. raw_date = j["earliestSlotLists"][0]["date"]
  579. return to_yyyymmdd(raw_date, "%m/%d/%Y %H:%M:%S")
  580. return ""
  581. def _fetch_configurations(self, apt_config: Dict[str, Any]):
  582. if not self.center_conf:
  583. self.center_conf = self._query_center()
  584. vac_code = apt_config.get("vac_code")
  585. category_code = apt_config.get("category_code")
  586. if category_code not in self.category_conf:
  587. visa_categories = self._query_visa_category(vac_code)
  588. found = False
  589. for vc in visa_categories:
  590. if vc.get("code") == category_code:
  591. self.category_conf[category_code] = vc
  592. found = True
  593. break
  594. if not found:
  595. self._log(f"WARN: Category {category_code} not found")
  596. sub_category_code = apt_config.get("subcategory_code")
  597. if sub_category_code not in self.subcategory_conf:
  598. visa_subcategories = self._query_visa_sub_category(vac_code, category_code)
  599. found = False
  600. for svc in visa_subcategories:
  601. if svc.get("code") == sub_category_code:
  602. self.subcategory_conf[sub_category_code] = svc
  603. found = True
  604. break
  605. if not found:
  606. self._log(f"WARN: SubCategory {sub_category_code} not found")
  607. def _query_center(self) -> List:
  608. mission = self.free_config.get("mission_code")
  609. country = self.free_config.get("country_code")
  610. url = f"https://{self.vfs_api_domain}/master/center/{mission}/{country}/en-US"
  611. headers = self._get_common_headers(with_auth=False)
  612. resp = self._perform_request("GET", url, headers=headers)
  613. return resp.json()
  614. def _query_visa_category(self, center_code: str) -> List:
  615. mission = self.free_config.get("mission_code")
  616. country = self.free_config.get("country_code")
  617. enc_center = urllib.parse.quote(center_code)
  618. url = f"https://{self.vfs_api_domain}/master/visacategory/{mission}/{country}/{enc_center}/en-US"
  619. headers = self._get_common_headers(with_auth=False)
  620. resp = self._perform_request("GET", url, headers=headers)
  621. return resp.json()
  622. def _query_visa_sub_category(self, center_code: str, category_code: str) -> List:
  623. mission = self.free_config.get("mission_code")
  624. country = self.free_config.get("country_code")
  625. enc_center = urllib.parse.quote(center_code)
  626. enc_cat = urllib.parse.quote(category_code)
  627. url = f"https://{self.vfs_api_domain}/master/subvisacategory/{mission}/{country}/{enc_center}/{enc_cat}/en-US"
  628. headers = self._get_common_headers(with_auth=False)
  629. resp = self._perform_request("GET", url, headers=headers)
  630. return resp.json()
  631. def _read_otp_email(self, sent_at='', sender='VFS Global') -> str:
  632. # 保持原样,这部分使用云API读取邮件,不依赖本地网络库
  633. master_email = "hujiarui8@gmail.com"
  634. recipient = self.config.account.username
  635. subject_keywords = "One Time Password"
  636. body_keywords = "OTP"
  637. if not sent_at:
  638. now_utc = datetime.utcnow()
  639. sent_at = now_utc.strftime("%Y-%m-%d %H:%M:%S")
  640. self._log(f"Waiting for OTP email...")
  641. content_out = CloudApi.Instance().fetch_mail_content(
  642. master_email, sender, recipient, subject_keywords, body_keywords, sent_at, 300, only_text=False
  643. )
  644. if content_out:
  645. parts = content_out.split("\n")
  646. text_parts = [p for p in parts if not p.startswith("data:image")]
  647. email_text = " ".join(text_parts)
  648. image_urls = [p for p in parts if p.startswith("data:image")]
  649. match = re.search(r'\b\d{6}\b', email_text)
  650. if match:
  651. self._log("Found OTP from email text via Regex.")
  652. return match.group(0)
  653. if image_urls:
  654. self._log("OTP not found in text, trying LLM image recognition...")
  655. target_image_b64 = image_urls[0]
  656. vfs_otp_prompt = "图片中有多组验证码,其中OTP箭头指向的验证码为真,其余为假。只输出真实验证码的内容,不要任何多余的汉字或标点符号。"
  657. try:
  658. otp_code = self.captcha_breaker.break_captcha(target_image_b64, prompt=vfs_otp_prompt)
  659. if otp_code:
  660. self._log(f"Successfully extracted OTP from image: {otp_code}")
  661. return otp_code
  662. except CaptchaBreakerError as e:
  663. self._log(f"LLM OTP extraction failed: {e}")
  664. raise BizLogicError(message="OTP code not found")
  665. def _submit_login_otp(self, old_cf_token: str, otp: str):
  666. self._log("Submitting Login OTP...")
  667. new_cf_token = self._refresh_turnstile_token()
  668. email = self.config.account.username
  669. password = self.config.account.password
  670. enc_password = self._encrypt_password(password)
  671. mission = self.free_config.get("mission_code", "")
  672. country = self.free_config.get("country_code", "")
  673. client_src = self._get_client_source()
  674. orange_src = self._get_orange_source(email)
  675. url = f"https://{self.vfs_api_domain}/user/login"
  676. headers = self._get_common_headers(with_auth=False)
  677. headers.update({
  678. "clientsource": client_src,
  679. "orangex": orange_src
  680. })
  681. data = {
  682. "username": email,
  683. "password": enc_password,
  684. "missioncode": mission,
  685. "countrycode": country,
  686. "languageCode": "en-US",
  687. "captcha_version": "cloudflare-v1",
  688. "captcha_api_key": new_cf_token,
  689. "otp": otp
  690. }
  691. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  692. resp_json = resp.json()
  693. if resp_json.get("accessToken"):
  694. self.jwt_token = resp_json["accessToken"]
  695. self._log("OTP Login successful.")
  696. return
  697. error_desc = resp_json.get("description", resp.text)
  698. raise PermissionDeniedError(message=f"OTP Login Failed: {error_desc}")
  699. def _extract_turnstile_token_from_landing_page(self, timeout_sec: int = 40, reset_first: bool = False) -> str:
  700. """
  701. [旧方式] 从官方页面中提取或重置刷新 Cloudflare Turnstile Token
  702. """
  703. if reset_first:
  704. self._log("Resetting Cloudflare Turnstile in landing page...")
  705. js_reset = """
  706. try {
  707. var input = document.querySelector('input[name="cf-turnstile-response"]');
  708. if (input) input.value = "";
  709. if (window.turnstile && window.turnstile.reset) {
  710. window.turnstile.reset();
  711. }
  712. } catch(e) {}
  713. """
  714. self.page.run_js(js_reset)
  715. cf_bypasser = CloudflareBypasser(self.page, log=self.config.debug if self.config else True)
  716. total_cycles = int(timeout_sec * 2)
  717. for i in range(total_cycles):
  718. time.sleep(0.5)
  719. handle_cookie_consent(self.page, logger=self._log)
  720. try:
  721. ele = self.page.ele('@name=cf-turnstile-response', timeout=0.5)
  722. if ele and ele.value:
  723. self._log("Cloudflare Turnstile token extracted successfully.")
  724. return ele.value
  725. except Exception:
  726. pass
  727. if i > 4:
  728. try:
  729. cf_bypasser.click_verification_button(is_dfs=False)
  730. except Exception:
  731. pass
  732. return ""
  733. def _extract_turnstile_token(self, timeout_sec: int = 40, reset_first: bool = False) -> str:
  734. """
  735. 统一提取或重置刷新 Cloudflare Turnstile Token(根据 bypass_landing_page 开关自动路由)
  736. """
  737. if self.bypass_landing_page:
  738. if reset_first or not self.injected:
  739. self._prepare_clean_page()
  740. token = self._get_turnstile_token(timeout_sec=timeout_sec)
  741. return token or ""
  742. else:
  743. return self._extract_turnstile_token_from_landing_page(timeout_sec=timeout_sec, reset_first=reset_first)
  744. def _refresh_turnstile_token(self) -> str:
  745. """
  746. 强制刷新 Cloudflare Turnstile 并获取新 Token
  747. """
  748. self._log("Refreshing Cloudflare Turnstile token...")
  749. token = self._extract_turnstile_token(timeout_sec=30, reset_first=True)
  750. if not token:
  751. raise BizLogicError("Failed to refresh Cloudflare Turnstile token (Timeout)")
  752. return token
  753. def book(self, slot_info: QueryResult, user_inputs) -> BookResult:
  754. """
  755. 执行完整的预约流程
  756. """
  757. self._log("Starting booking process...")
  758. user_email = user_inputs.get('email')
  759. user_inputs['alias_email'] = get_alias_email(user_email, new_domain="text.skin")
  760. res = BookResult()
  761. app_type = slot_info.apt_type
  762. from_date = slot_info.earliest_date.strftime("%Y-%m-%d") if slot_info.earliest_date else datetime.now().strftime("%Y-%m-%d")
  763. expected_start = user_inputs.get("expected_start_date", "2000-01-01")
  764. expected_end = user_inputs.get("expected_end_date", "2100-01-01")
  765. if from_date > expected_end:
  766. self._log("No valid slots in date preference.")
  767. res.success = False
  768. return res
  769. apt_config = self.free_config.get("apt_configs", {}).get(app_type.routing_key)
  770. if not apt_config:
  771. raise NotFoundError(message="Book: Config missing for this routing key.")
  772. self._fetch_configurations(apt_config)
  773. sub_cc = apt_config.get("subcategory_code")
  774. sub_conf = self.subcategory_conf.get(sub_cc, {})
  775. ocr_enabled = sub_conf.get("isOCREnable", False)
  776. if ocr_enabled:
  777. self._log("OCR Enabled, uploading documents...")
  778. upload_res = self._upload_applicant_documents(apt_config, user_inputs)
  779. user_inputs["applicant_image"] = upload_res.get("passportImageFilename")
  780. user_inputs["applicant_image_data"] = upload_res.get("passportImageFileBytes")
  781. user_inputs["guid"] = upload_res.get("uploadDocumentGUID")
  782. enable_reference_number = sub_conf.get("enableReferenceNumber", False)
  783. final_urn = None
  784. is_waitlist = (slot_info.availability_status == AvailabilityStatus.Waitlist)
  785. if not self.booking_wait_applied:
  786. self._log("pre-booking wait: sleeping 20s before booking to avoid risk control")
  787. time.sleep(20)
  788. self.booking_wait_applied = True
  789. MAX_RETRY = 2
  790. for i in range(MAX_RETRY):
  791. try:
  792. final_urn = self._add_primary_applicant(apt_config, user_inputs, is_waitlist, ocr_enabled, enable_reference_number)
  793. break
  794. except BizLogicError as e:
  795. err_msg = str(e)
  796. self._log(f"Add Applicant retry {i+1}/{MAX_RETRY}: {err_msg}")
  797. if 'Capping has exceeded' in err_msg:
  798. raise e
  799. time.sleep(10.0)
  800. if not final_urn:
  801. raise BizLogicError(message="Failed to add primary applicant (Slot likely taken or API error)")
  802. self._log(f"Applicant Added. URN: {final_urn}")
  803. otp_enabled = sub_conf.get("isApplicantOTPEnabled", False)
  804. if otp_enabled:
  805. self._log("Applicant OTP Required.")
  806. now_utc = datetime.utcnow()
  807. sent_at = now_utc.strftime("%Y-%m-%d %H:%M:%S")
  808. if not self._applicant_otp_send(apt_config, final_urn):
  809. raise BizLogicError(message='Applicant OTP send failed')
  810. otp_code = self._read_otp_email(sent_at=sent_at)
  811. if not self._applicant_otp_verify(apt_config, final_urn, otp_code):
  812. raise BizLogicError(message='Applicant OTP verify failed')
  813. if is_waitlist:
  814. if self._confirm_waitlist(apt_config, final_urn):
  815. res.success = True
  816. res.urn = final_urn
  817. res.account = self.config.account.username
  818. self._log("Waitlist confirmed.")
  819. return res
  820. raise BizLogicError(message='Confirm waitlist failed')
  821. months = self._get_filtered_covered_months(expected_start, expected_end, from_date)
  822. self._log(f"Scanning months: {months} (Start looking from: {from_date})")
  823. selected_slot_id = ""
  824. selected_slot_date = ""
  825. selected_slot_time_range = ""
  826. all_ads = set()
  827. forbidden_dates = set()
  828. found_slot = False
  829. for m_str in months:
  830. self._log(f"Checking calendar for {m_str}...")
  831. ads = self._query_slot_calendar(apt_config, final_urn, m_str)
  832. new_ads = [d for d in ads if d not in all_ads]
  833. all_ads.update(new_ads)
  834. for _ in range(3):
  835. avail_candidates = [d for d in list(all_ads) if d not in forbidden_dates]
  836. sel_dates = filter_dates_in_range(avail_candidates, expected_start, expected_end, shuffle=True)
  837. if not sel_dates:
  838. break
  839. tmp_date = sel_dates[0]
  840. forbidden_dates.add(tmp_date)
  841. if not self._saveuseractionaudit(apt_config, final_urn, tmp_date):
  842. self._log(f"Audit failed for {tmp_date}, skipping...")
  843. time.sleep(1)
  844. continue
  845. ats = self._query_slot_time(apt_config, final_urn, tmp_date)
  846. if not ats:
  847. self._log(f"No timeslots for {tmp_date}")
  848. continue
  849. sel_tm = random.choice(ats)
  850. selected_slot_id = sel_tm.get("allocationId")
  851. selected_slot_date = tmp_date
  852. selected_slot_time_range = sel_tm.get("slot")
  853. found_slot = True
  854. break
  855. if found_slot:
  856. break
  857. if not found_slot:
  858. self._log("No valid slots found after scanning.")
  859. res.success = False
  860. return res
  861. self._log(f"Slot Selected: {selected_slot_date} {selected_slot_time_range} (ID: {selected_slot_id})")
  862. self._submit_no_addition_service(final_urn)
  863. amount, currency = self._query_fee(apt_config, final_urn)
  864. self._log("Submitting schedule...")
  865. schedule_res = self._schedule(apt_config, final_urn, amount, currency, selected_slot_id)
  866. if not schedule_res.get("IsAppointmentBooked"):
  867. self._log(f"Booking failed: {schedule_res}")
  868. res.success = False
  869. return res
  870. res.success = True
  871. res.account = self.config.account.username
  872. res.book_date = selected_slot_date
  873. res.book_time = selected_slot_time_range
  874. res.urn = final_urn
  875. if schedule_res.get("IsPaymentRequired", False):
  876. payload = schedule_res.get("payLoad", "")
  877. if payload:
  878. self._log("Processing payment link...")
  879. payment_url = self._pay_request(payload)
  880. if payment_url:
  881. res.payment_link = payment_url
  882. return res
  883. def _get_application(self):
  884. url = f"https://{self.vfs_api_domain}/appointment/application"
  885. headers = self._get_common_headers(with_auth=True)
  886. data = {
  887. 'countryCode': self.free_config.get("country_code"),
  888. 'missionCode': self.free_config.get("mission_code"),
  889. 'loginUser': self.config.account.username,
  890. 'languageCode': 'en-US',
  891. }
  892. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  893. return resp.json()
  894. def _upload_applicant_documents(self, apt_config, user_inputs) -> Dict:
  895. """上传图片:根据前缀判断是下载外部图片还是读取本地文件,并上传到 VFS"""
  896. import requests as standard_requests
  897. url = f"https://{self.vfs_api_domain}/appointment/UploadApplicantDocument"
  898. passport_path_or_url = user_inputs.get("passport_image_url")
  899. if not passport_path_or_url:
  900. raise NotFoundError(message="Missing passport_image_url")
  901. if passport_path_or_url.startswith("http://") or passport_path_or_url.startswith("https://"):
  902. img_resp = standard_requests.get(passport_path_or_url, timeout=30)
  903. if img_resp.status_code != 200:
  904. raise BizLogicError(message=f"Failed to download passport image: {img_resp.status_code}")
  905. img_data = img_resp.content
  906. else:
  907. if not os.path.exists(passport_path_or_url):
  908. raise BizLogicError(message=f"Local passport image file not found: {passport_path_or_url}")
  909. with open(passport_path_or_url, "rb") as f:
  910. img_data = f.read()
  911. b64_str = base64.b64encode(img_data).decode('utf-8')
  912. headers = self._get_common_headers(with_auth=True)
  913. data = {
  914. "missioncode": self.free_config.get("mission_code"),
  915. "countryCode": self.free_config.get("country_code"),
  916. "centerCode": apt_config.get("vac_code"),
  917. "loginUser": self.config.account.username,
  918. "languageCode": "en-US",
  919. "visaCategoryCode": apt_config.get("subcategory_code"),
  920. "fileBytes": b64_str,
  921. "selfiImageFileBytes": ""
  922. }
  923. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  924. result = resp.json()
  925. result["passportImageFilename"] = "passport_img.jpg"
  926. result["passportImageFileBytes"] = b64_str
  927. return result
  928. def _add_primary_applicant(self, apt_config: Dict[str, Any], user_inputs: Dict[str, Any],
  929. is_waitlist: bool, ocr_enabled: bool, enable_ref: bool) -> str:
  930. """构造申请人 payload 并提交"""
  931. url = f"https://{self.vfs_api_domain}/appointment/applicants"
  932. headers = self._get_common_headers(with_auth=True)
  933. gender_str = str(user_inputs.get("gender", "")).lower()
  934. gender_code = 1 if gender_str == "male" else 2
  935. raw_dial = user_inputs.get("phone_country_code", "86")
  936. dial_code = str(raw_dial)
  937. # 日期格式转换 YYYY-MM-DD -> DD/MM/YYYY
  938. def _to_ddmmyyyy(d_str):
  939. try:
  940. return datetime.strptime(str(d_str), "%Y-%m-%d").strftime("%d/%m/%Y")
  941. except:
  942. return str(d_str)
  943. dob = _to_ddmmyyyy(user_inputs.get("birthday", ""))
  944. ppt_exp = _to_ddmmyyyy(user_inputs.get("passport_expiry_date", ""))
  945. applicant = {
  946. "urn": "",
  947. "arn": "",
  948. "loginUser": self.config.account.username,
  949. "firstName": str(user_inputs.get("first_name", "")).strip().upper(),
  950. "middleName": "",
  951. "lastName": str(user_inputs.get("last_name", "")).strip().upper(),
  952. "employerFirstName": "",
  953. "employerLastName": "",
  954. "salutation": "",
  955. "Subclasscode": None,
  956. "VisaToken": None,
  957. "centerClassCode": None,
  958. "dateOfApplication": None,
  959. "selectedSubvisaCategory": None,
  960. "gender": gender_code,
  961. "contactNumber": str(user_inputs.get("phone", "")).strip().lstrip("0"),
  962. "dialCode": dial_code,
  963. "employerContactNumber": "",
  964. "employerDialCode": "",
  965. "emailId": str(user_inputs.get("alias_email", "")).strip().upper(),
  966. "employerEmailId": "",
  967. "passportNumber": str(user_inputs.get("passport_no", "")).strip().upper(),
  968. "confirmPassportNumber": "",
  969. "passportExpirtyDate": ppt_exp,
  970. "dateOfBirth": dob,
  971. "nationalId": None,
  972. "nationalityCode": get_country_iso3(str(user_inputs.get("nationality", ""))),
  973. "state": user_inputs.get("state"),
  974. "city": user_inputs.get("city"),
  975. "addressline1": user_inputs.get("addressline1"),
  976. "addressline2": user_inputs.get("addressline2"),
  977. "pincode": None,
  978. "isEndorsedChild": False,
  979. "applicantType": 0,
  980. "vlnNumber": None,
  981. "applicantGroupId": 0,
  982. "parentPassportNumber": "",
  983. "parentPassportExpiry": "",
  984. "dateOfDeparture": None,
  985. "entryType": "",
  986. "eoiVisaType": "",
  987. "passportType": "",
  988. "vfsReferenceNumber": "",
  989. "familyReunificationCerificateNumber": "",
  990. "PVRequestRefNumber": "",
  991. "PVStatus": "",
  992. "PVStatusDescription": "",
  993. "PVCanAllowRetry": True,
  994. "PVisVerified": False,
  995. "eefRegistrationNumber": "",
  996. "isAutoRefresh": True,
  997. "helloVerifyNumber": "",
  998. "OfflineCClink": "",
  999. "idenfystatuscheck": False,
  1000. "vafStatus": None,
  1001. "SpecialAssistance": "",
  1002. "AdditionalRefNo": None,
  1003. "juridictionCode": "",
  1004. "canInitiateVAF": False,
  1005. "canEditVAF": False,
  1006. "canDeleteVAF": False,
  1007. "canDownloadVAF": False,
  1008. "Retryleft": "",
  1009. # 这里的 IP 应该已经在 create_session 时获取到了
  1010. "ipAddress": self.real_ip
  1011. }
  1012. if enable_ref:
  1013. applicant["referenceNumber"] = str(user_inputs.get("cover_letter_id", "")).strip()
  1014. else:
  1015. applicant["referenceNumber"] = None
  1016. if ocr_enabled:
  1017. applicant["applicantImage"] = str(user_inputs.get("applicant_image", ""))
  1018. applicant["applicantImageData"] = str(user_inputs.get("applicant_image_data", ""))
  1019. applicant["GUID"] = str(user_inputs.get("guid", ""))
  1020. payload = {
  1021. "countryCode": self.free_config.get("country_code"),
  1022. "missionCode": self.free_config.get("mission_code"),
  1023. "centerCode": apt_config.get("vac_code"),
  1024. "loginUser": self.config.account.username,
  1025. "visaCategoryCode": apt_config.get("subcategory_code"),
  1026. "applicantList": [applicant],
  1027. "languageCode": "en-US",
  1028. "isWaitlist": is_waitlist,
  1029. "isEdit": False,
  1030. "feeEntryTypeCode": None, "feeExemptionTypeCode": None,
  1031. "feeExemptionDetailsCode": None, "juridictionCode": None, "regionCode": None
  1032. }
  1033. resp = self._perform_request("POST", url, headers=headers, json_data=payload)
  1034. urn = resp.json().get("urn")
  1035. if not urn:
  1036. err_msg = resp.json().get('error')
  1037. raise BizLogicError(message=str(err_msg))
  1038. return urn
  1039. def _applicant_otp_send(self, apt_config, urn) -> bool:
  1040. url = f"https://{self.vfs_api_domain}/appointment/applicantotp"
  1041. headers = self._get_common_headers(with_auth=True)
  1042. data = {
  1043. "urn": urn,
  1044. "loginUser": self.config.account.username,
  1045. "missionCode": self.free_config.get("mission_code"),
  1046. "countryCode": self.free_config.get("country_code"),
  1047. "centerCode": apt_config.get("vac_code"),
  1048. "OTP": "",
  1049. "otpAction": "GENERATE",
  1050. "languageCode": "en-US"
  1051. }
  1052. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  1053. return resp.json().get("isOTPGenerated", False)
  1054. def _applicant_otp_verify(self, apt_config, urn, otp) -> bool:
  1055. url = f"https://{self.vfs_api_domain}/appointment/applicantotp"
  1056. headers = self._get_common_headers(with_auth=True)
  1057. # VFS 这里的 header 有时需要 datacenter,原代码有就加上
  1058. headers["datacenter"] = "GERMANY"
  1059. data = {
  1060. "urn": urn,
  1061. "loginUser": self.config.account.username,
  1062. "missionCode": self.free_config.get("mission_code"),
  1063. "countryCode": self.free_config.get("country_code"),
  1064. "centerCode": apt_config.get("vac_code"),
  1065. "OTP": otp,
  1066. "otpAction": "VALIDATE",
  1067. "languageCode": "en-US"
  1068. }
  1069. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  1070. return resp.json().get("isOTPValidated", False)
  1071. def _query_slot_calendar(self, apt_config, urn, from_date) -> List:
  1072. url = f"https://{self.vfs_api_domain}/appointment/calendar"
  1073. headers = self._get_common_headers(with_auth=True)
  1074. # 将 YYYY-MM-DD 转为 DD/MM/YYYY 用于 API
  1075. dt_m = datetime.strptime(from_date, "%Y-%m-%d")
  1076. converted_date = dt_m.strftime("%d/%m/%Y")
  1077. data = {
  1078. "missionCode": self.free_config.get("mission_code"),
  1079. "countryCode": self.free_config.get("country_code"),
  1080. "centerCode": apt_config.get("vac_code"),
  1081. "loginUser": self.config.account.username,
  1082. "visaCategoryCode": apt_config.get("subcategory_code"),
  1083. "fromDate": converted_date,
  1084. "urn": urn,
  1085. "payCode": ""
  1086. }
  1087. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  1088. calendars = resp.json().get("calendars")
  1089. ads_out = []
  1090. if calendars:
  1091. for item in calendars:
  1092. # 通常是 MM/DD/YYYY
  1093. raw = item.get("date")
  1094. ads_out.append(to_yyyymmdd(raw, "%m/%d/%Y"))
  1095. return ads_out
  1096. def _query_slot_time(self, apt_config, urn, slot_date) -> List:
  1097. url = f"https://{self.vfs_api_domain}/appointment/timeslot"
  1098. headers = self._get_common_headers(with_auth=True)
  1099. dt_m = datetime.strptime(slot_date, "%Y-%m-%d")
  1100. converted_date = dt_m.strftime("%d/%m/%Y")
  1101. data = {
  1102. "missionCode": self.free_config.get("mission_code"),
  1103. "countryCode": self.free_config.get("country_code"),
  1104. "centerCode": apt_config.get("vac_code"),
  1105. "loginUser": self.config.account.username,
  1106. "visaCategoryCode": apt_config.get("subcategory_code"),
  1107. "slotDate": converted_date,
  1108. "urn": urn
  1109. }
  1110. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  1111. return resp.json().get("slots", [])
  1112. def _saveuseractionaudit(self, apt_config, urn, earliest_date) -> bool:
  1113. url = f"https://{self.vfs_api_domain}/appointment/saveuseractionaudit"
  1114. headers = self._get_common_headers(with_auth=True)
  1115. dt = datetime.strptime(earliest_date, "%Y-%m-%d")
  1116. data = {
  1117. "missionCode": self.free_config.get("mission_code"),
  1118. "countryCode": self.free_config.get("country_code"),
  1119. "centerCode": apt_config.get("vac_code"),
  1120. "loginUser": self.config.account.username,
  1121. "urn": urn,
  1122. "firstEarliestSlotDate": dt.strftime("%d/%m/%Y"),
  1123. "action": "schedule",
  1124. "ipAddress": self.real_ip,
  1125. "eadAppointmentDetail": dt.strftime("%Y-%m-%dT%H:%M:%S")
  1126. }
  1127. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  1128. return resp.json().get("isSavedSuccess", False)
  1129. def _submit_no_addition_service(self, urn):
  1130. url = f"https://{self.vfs_api_domain}/vas/mapvas"
  1131. headers = self._get_common_headers(with_auth=True)
  1132. data = {
  1133. "loginUser": self.config.account.username,
  1134. "missionCode": self.free_config.get("mission_code"),
  1135. "countryCode": self.free_config.get("country_code"),
  1136. "urn": urn,
  1137. "applicants": []
  1138. }
  1139. self._perform_request("POST", url, headers=headers, json_data=data)
  1140. def _query_fee(self, apt_config, urn) -> Tuple[float, str]:
  1141. url = f"https://{self.vfs_api_domain}/appointment/fees"
  1142. headers = self._get_common_headers(with_auth=True)
  1143. data = {
  1144. "missionCode": self.free_config.get("mission_code"),
  1145. "countryCode": self.free_config.get("country_code"),
  1146. "centerCode": apt_config.get("vac_code"),
  1147. "loginUser": self.config.account.username,
  1148. "urn": urn,
  1149. "languageCode": "en-US"
  1150. }
  1151. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  1152. j = resp.json()
  1153. total = j.get("totalamount", 0.0)
  1154. currency = "EUR"
  1155. if j.get("feeDetails"):
  1156. currency = j["feeDetails"][0].get("currency", "EUR")
  1157. return total, currency
  1158. def _schedule(self, apt_config, urn, amount, currency, slot_id) -> Dict:
  1159. url = f"https://{self.vfs_api_domain}/appointment/schedule"
  1160. headers = self._get_common_headers(with_auth=True)
  1161. data = {
  1162. "missionCode": self.free_config.get("mission_code"),
  1163. "countryCode": self.free_config.get("country_code"),
  1164. "centerCode": apt_config.get("vac_code"),
  1165. "loginUser": self.config.account.username,
  1166. "urn": urn,
  1167. "notificationType": "none",
  1168. "paymentdetails": {
  1169. "paymentmode": "Online",
  1170. "RequestRefNo": "",
  1171. "clientId": "",
  1172. "merchantId": "",
  1173. "amount": amount,
  1174. "currency": currency
  1175. },
  1176. "allocationId": str(slot_id),
  1177. "CanVFSReachoutToApplicant": True
  1178. }
  1179. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  1180. return resp.json()
  1181. def _pay_request(self, payload) -> str:
  1182. """
  1183. 解析支付重定向 URL (DrissionPage 新标签页版)
  1184. """
  1185. start_url = f"https://online.vfsglobal.com/PG-Component/Payment/PayRequest?payLoad={payload}"
  1186. final_url = ""
  1187. try:
  1188. self._log(f"Resolving payment redirect...<{start_url}>")
  1189. pay_tab = self.page.new_tab(start_url)
  1190. pay_tab.wait.url_change(start_url, timeout=15)
  1191. final_url = pay_tab.url
  1192. self._log(f"Payment URL resolved: {final_url}")
  1193. pay_tab.close()
  1194. except Exception as e:
  1195. self._log(f"[WARN] Failed to resolve payment URL: {e}")
  1196. try:
  1197. pay_tab.close()
  1198. except:
  1199. pass
  1200. return final_url
  1201. def _confirm_waitlist(self, apt_config: Dict[str, Any], urn: str) -> bool:
  1202. url = f"https://{self.vfs_api_domain}/appointment/ConfirmWaitlist"
  1203. headers = self._get_common_headers(with_auth=True)
  1204. data = {
  1205. "missionCode": self.free_config.get("mission_code"),
  1206. "countryCode": self.free_config.get("country_code"),
  1207. "centerCode": apt_config.get("vac_code"),
  1208. "loginUser": self.config.account.username,
  1209. "urn": urn,
  1210. "notificationType": "none",
  1211. "CanVFSReachoutToApplicant": True
  1212. }
  1213. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  1214. return resp.json().get("isConfirmed", False)
  1215. def _get_filtered_covered_months(self, start_date, end_date, from_date) -> List[str]:
  1216. fmt = "%Y-%m-%d"
  1217. try:
  1218. dt_start = datetime.strptime(start_date, fmt) if start_date else datetime.now()
  1219. dt_end = datetime.strptime(end_date, fmt) if end_date else datetime.now().replace(year=datetime.now().year + 1)
  1220. try:
  1221. dt_from = datetime.strptime(from_date, fmt)
  1222. except:
  1223. dt_from = datetime.now()
  1224. except:
  1225. return []
  1226. dt_start = dt_start.replace(day=1)
  1227. dt_end = dt_end.replace(day=1)
  1228. dt_from = dt_from.replace(day=1)
  1229. curr = max(dt_start, dt_from)
  1230. months = []
  1231. while curr <= dt_end:
  1232. months.append(curr.strftime(fmt))
  1233. if curr.month == 12:
  1234. curr = curr.replace(year=curr.year + 1, month=1)
  1235. else:
  1236. curr = curr.replace(month=curr.month + 1)
  1237. return months
  1238. def cleanup(self):
  1239. """销毁浏览器并彻底删除临时文件"""
  1240. cleanup_browser_workspace(self.page, self.root_workspace, self.tunnel, self._log)
  1241. self.page = None
  1242. self.tunnel = None
  1243. self.injected = False
  1244. def get_plugin_status(self) -> PluginStatus:
  1245. account_name = self.config.account.username if self.config and self.config.account else ""
  1246. proxy_info = f"{self.config.proxy.ip}:{self.config.proxy.port}" if self.config and self.config.proxy and self.config.proxy.ip else ""
  1247. current_url = ""
  1248. if self.page:
  1249. try:
  1250. current_url = self.page.url
  1251. except Exception:
  1252. pass
  1253. session_age = int(time.time() - self.session_create_time) if self.session_create_time > 0 else 0
  1254. state = "running" if self.is_healthy else "unhealthy"
  1255. if not self.jwt_token and self.page:
  1256. state = "logging_in"
  1257. return PluginStatus(
  1258. instance_id=self.instance_id,
  1259. is_healthy=self.is_healthy,
  1260. state=state,
  1261. current_url=current_url,
  1262. account=account_name,
  1263. proxy=proxy_info,
  1264. session_age_sec=session_age,
  1265. last_active_at=utc_now_str(),
  1266. )
  1267. def __del__(self):
  1268. """
  1269. 析构函数:当对象被垃圾回收时自动调用
  1270. """
  1271. self.cleanup()