vfs_plugin.py 60 KB

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