vfs_plugin.py 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  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 cryptography.hazmat.primitives import serialization, hashes
  16. from cryptography.hazmat.primitives.asymmetric import padding
  17. from cryptography.hazmat.backends import default_backend
  18. import configure
  19. from vs_plg import IVSPlg
  20. from vs_types import VSPlgConfig, AppointmentType, VSQueryResult, VSBookResult, DateAvailability, AvailabilityStatus, NotFoundError, PermissionDeniedError, RateLimiteddError, SessionExpiredOrInvalidError, BizLogicError, PluginStatus, utc_now_str
  21. from toolkit.vs_cloud_api import VSCloudApi
  22. from toolkit.mihomo_tunnel import MihomoTunnel
  23. from toolkit.captcha_breaker import recognize_vfs_captcha_with_qwen
  24. from utils.cloudflare_bypass_for_scraping import CloudflareBypasser
  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 = "text.skin") -> str:
  74. """
  75. 将邮箱域名替换为指定域名(默认 text.skin)
  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. 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[VSPlgConfig] = 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. def set_config(self, config: VSPlgConfig):
  132. self.config = config
  133. self.free_config = config.free_config or {}
  134. if self.free_config.get('country_code') == 'chn':
  135. self.vfs_api_domain = 'lift-apicn.vfsglobal.com'
  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}] [{self.instance_id}] {message}')
  141. else:
  142. print(f'[VfsPlugin] [{self.group_id}] [{self.instance_id}] {message}')
  143. def keep_alive(self):
  144. try:
  145. keep_alive_funcs = [
  146. self._get_application,
  147. self._query_center,
  148. ]
  149. random.choice(keep_alive_funcs)()
  150. self._log(f'keep_alive request, status ok')
  151. except Exception as e:
  152. self.is_healthy = False
  153. self._log(f'keep_alive failed: {e}')
  154. def health_check(self) -> bool:
  155. if not self.is_healthy:
  156. return False
  157. if self.page is None:
  158. return False
  159. try:
  160. if not self.page.run_js("return 1;"):
  161. return False
  162. except:
  163. return False
  164. if self.config.session_max_life > 0:
  165. current_time = time.time()
  166. elapsed_time = current_time - self.session_create_time
  167. if elapsed_time > self.config.session_max_life:
  168. self._log(f"Session expired.")
  169. return False
  170. return True
  171. def create_session(self) -> None:
  172. """
  173. 使用 DrissionPage 创建会话:
  174. 1. 启动浏览器
  175. 2. 导航到登录页
  176. 3. 自动过盾并提取 Token (集成 CloudflareBypasser)
  177. 4. JS fetch 登录
  178. """
  179. self._log(f"Initializing Session (ID: {self.instance_id})...")
  180. co = ChromiumOptions()
  181. def get_free_port():
  182. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  183. s.bind(('', 0))
  184. return s.getsockname()[1]
  185. debug_port = get_free_port()
  186. self._log(f"Assigned Debug Port: {debug_port}")
  187. self._log(f"Account id={self.config.account.id}, proxy id={self.config.proxy.id}")
  188. co.set_local_port(debug_port)
  189. co.set_user_data_path(self.user_data_path)
  190. chrome_path = configure.CHROME_PATH
  191. if not chrome_path:
  192. chrome_path = os.getenv("CHROME_BIN")
  193. if chrome_path and os.path.exists(chrome_path):
  194. co.set_paths(browser_path=chrome_path)
  195. if self.config.proxy and self.config.proxy.ip:
  196. p = self.config.proxy
  197. if p.username and p.password:
  198. self._log(f"Starting Proxy Tunnel for {p.ip}...")
  199. exit_node = {
  200. "name": "ExitNode",
  201. "type": p.proto,
  202. "server": p.ip,
  203. "port": p.port,
  204. "username": p.username,
  205. "password": p.password
  206. }
  207. relay_node = None
  208. if configure.MIHOMO_RELAY_NODES:
  209. relay_node = random.choice(configure.MIHOMO_RELAY_NODES)
  210. mihomo_path = configure.MIHOMO_BIN_PATH
  211. if not mihomo_path:
  212. mihomo_path = os.getenv("MIHOMO_BIN")
  213. if not mihomo_path:
  214. raise BizLogicError(message='Mihomo path is null, You need set mihomo bin path in configure or os env')
  215. self.tunnel = MihomoTunnel(mihomo_path, exit_node=exit_node, relay_node=relay_node)
  216. local_proxy = self.tunnel.start()
  217. self._log(f"Tunnel started at {local_proxy}")
  218. co.set_argument(f'--proxy-server={local_proxy}')
  219. else:
  220. proxy_str = f"{p.proto}://{p.ip}:{p.port}"
  221. co.set_argument(f'--proxy-server={proxy_str}')
  222. else:
  223. self._log("[WARN] No proxy configured!")
  224. co.headless(False)
  225. co.set_argument('--no-sandbox')
  226. co.set_argument('--disable-gpu')
  227. co.set_argument('--disable-dev-shm-usage')
  228. co.set_argument('--window-size=1920,1080')
  229. co.set_argument('--disable-blink-features=AutomationControlled')
  230. self.page = ChromiumPage(co)
  231. mission = self.free_config.get("mission_code", "")
  232. country = self.free_config.get("country_code", "")
  233. lang = self.free_config.get("language", "en")
  234. if not mission or not country:
  235. raise BizLogicError("Missing mission/country code config")
  236. login_page_url = f"https://visa.vfsglobal.com/{country}/{lang}/{mission}/login"
  237. self._log(f"Navigating to {login_page_url}...")
  238. self.page.get(login_page_url)
  239. self._log("Handling Cloudflare challenge...")
  240. cf_bypasser = CloudflareBypasser(self.page, log=self.config.debug)
  241. if 'Attention Required! | Cloudflare' in self.page.title and 'Sorry, you have been blocked' in self.page.html:
  242. self._log(f'Block by cloudflare, try refresh...')
  243. self.page.refresh()
  244. self.page.wait.doc_loaded()
  245. time.sleep(random.uniform(3, 5))
  246. cf_bypasser.handle_waiting_room()
  247. cf_token = ""
  248. form_logged = False
  249. for i in range(40):
  250. time.sleep(1)
  251. self._handle_cookie_banner()
  252. try:
  253. ele = self.page.ele('@name=cf-turnstile-response', timeout=1)
  254. if ele and ele.value:
  255. cf_token = ele.value
  256. self._log("Cloudflare Turnstile token extracted.")
  257. break
  258. except:
  259. pass
  260. if i > 2:
  261. try:
  262. use_dfs = False
  263. cf_bypasser.click_verification_button(use_dfs)
  264. except Exception as e:
  265. pass
  266. if self.page.ele('tag:form', timeout=0.5) or self.page.ele('#mat-input-0', timeout=0.5):
  267. if not form_logged:
  268. self._log("Login form detected.")
  269. form_logged = True
  270. if i > 10 and not cf_token and not form_logged:
  271. self._log("Form visible but token not found yet...")
  272. if not cf_token:
  273. try:
  274. cf_token = self.page.ele('@name=cf-turnstile-response').value
  275. except:
  276. pass
  277. if not cf_token:
  278. self._log("[WARN] Could not extract Turnstile token.")
  279. raise BizLogicError(f"Could not extract Turnstile token.")
  280. email = self.config.account.username
  281. password = self.config.account.password
  282. enc_password = self._encrypt_password(password)
  283. client_src = self._get_client_source()
  284. orange_src = self._get_orange_source(email)
  285. url = f"https://{self.vfs_api_domain}/user/login"
  286. headers = self._get_common_headers(with_auth=False)
  287. headers.update({
  288. "clientsource": client_src,
  289. "orangex": orange_src
  290. })
  291. data = {
  292. "username": email,
  293. "password": enc_password,
  294. "missioncode": mission,
  295. "countrycode": country,
  296. "languageCode": "en-US",
  297. "captcha_version": "cloudflare-v1",
  298. "captcha_api_key": cf_token
  299. }
  300. now_utc = datetime.utcnow()
  301. sent_at = now_utc.strftime("%Y-%m-%d %H:%M:%S")
  302. self._log("Sending Login Request via Browser Fetch...")
  303. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  304. resp_json = resp.json()
  305. if resp_json.get('accessToken'):
  306. self.jwt_token = resp_json["accessToken"]
  307. self._log("Login successful, JWT obtained.")
  308. elif resp_json.get("enableOTPAuthentication"):
  309. self._log("Login requires OTP.")
  310. otp = self._read_otp_email(sent_at=sent_at)
  311. self._submit_login_otp(cf_token, otp)
  312. else:
  313. raise BizLogicError(f"Login failed: {resp.text[:200]}")
  314. self.session_create_time = time.time()
  315. try:
  316. self.real_ip = self._get_realnetwork_ip()
  317. except:
  318. self.real_ip = self.config.proxy.ip
  319. def query(self, apt_type: AppointmentType) -> VSQueryResult:
  320. """查询可预约 Slot"""
  321. result = VSQueryResult()
  322. apt_config = self.free_config.get("apt_configs", {}).get(apt_type.routing_key)
  323. self._fetch_configurations(apt_config)
  324. query_result = self._query_earliest_slot(apt_config)
  325. result.success = False
  326. result.availability_status = AvailabilityStatus.NoneAvailable
  327. if query_result:
  328. result.success = True
  329. if "WaitList" in query_result:
  330. result.availability_status = AvailabilityStatus.Waitlist
  331. else:
  332. earliest_dt = datetime.strptime(query_result, "%Y-%m-%d")
  333. result.availability_status = AvailabilityStatus.Available
  334. result.earliest_date = earliest_dt
  335. result.availability = [DateAvailability(date=earliest_dt, times=[])]
  336. self._log(f"Slot Found! -> {query_result}")
  337. else:
  338. self._log("No slots available.")
  339. return result
  340. def _perform_request(self, method, url, headers=None, data=None, json_data=None, params=None, retry_count=0):
  341. """
  342. 核心方法:在 DrissionPage 浏览器上下文中注入 JS 执行 fetch
  343. 并记录详细的 Traffic 日志用于分析
  344. """
  345. if not self.page:
  346. raise BizLogicError("Browser session not initialized")
  347. req_url = url
  348. if params:
  349. sep = '&' if '?' in req_url else '?'
  350. req_url += sep + urllib.parse.urlencode(params)
  351. final_headers = headers or {}
  352. fetch_options = {
  353. "method": method.upper(),
  354. "headers": final_headers,
  355. "credentials": "include" # 关键:带上浏览器 Cookie
  356. }
  357. log_body = "None"
  358. if json_data:
  359. json_str = json.dumps(json_data)
  360. fetch_options['body'] = json_str
  361. fetch_options['headers']['Content-Type'] = 'application/json'
  362. log_body = json_str
  363. elif data:
  364. if isinstance(data, dict):
  365. encoded_data = urllib.parse.urlencode(data)
  366. fetch_options['body'] = encoded_data
  367. fetch_options['headers']['Content-Type'] = 'application/x-www-form-urlencoded'
  368. log_body = encoded_data
  369. else:
  370. fetch_options['body'] = data
  371. log_body = str(data)
  372. if self.config.debug:
  373. self._log(f"┌── [TRAFFIC REQUEST] {method} {req_url}")
  374. self._log(f"├── Headers: {json.dumps(final_headers)}")
  375. self._log(f"└── Body: {log_body}")
  376. else:
  377. self._log(f"[TRAFFIC REQUEST] {method} {req_url}")
  378. js_script = f"""
  379. const url = "{req_url}";
  380. const options = {json.dumps(fetch_options)};
  381. const startTime = Date.now();
  382. return fetch(url, options)
  383. .then(async response => {{
  384. const text = await response.text();
  385. const headers = {{}};
  386. response.headers.forEach((value, key) => headers[key] = value);
  387. const endTime = Date.now();
  388. return {{
  389. status: response.status,
  390. body: text,
  391. headers: headers,
  392. url: response.url,
  393. duration: endTime - startTime
  394. }};
  395. }})
  396. .catch(error => {{
  397. return {{
  398. status: 0,
  399. body: error.toString(),
  400. headers: {{}},
  401. url: url,
  402. duration: Date.now() - startTime
  403. }};
  404. }});
  405. """
  406. try:
  407. res_dict = self.page.run_js(js_script, timeout=60)
  408. except Exception as e:
  409. self._log(f"[TRAFFIC ERROR] JS Execution failed: {e}")
  410. raise BizLogicError(f"Browser JS Execution Error: {e}")
  411. resp = BrowserResponse(res_dict)
  412. duration = res_dict.get('duration', 0)
  413. resp_preview = resp.text[:1000] + "..." if len(resp.text) > 1000 else resp.text
  414. if self.config.debug:
  415. self._log(f"┌── [TRAFFIC RESPONSE] Status: {resp.status_code} | Time: {duration}ms")
  416. self._log(f"└── Body: {resp_preview}")
  417. else:
  418. self._log(f"[TRAFFIC RESPONSE] {method} {req_url} -> Status: {resp.status_code} ({duration}ms)")
  419. if resp.status_code == 200:
  420. return resp
  421. elif resp.status_code == 401:
  422. self.is_healthy = False
  423. raise SessionExpiredOrInvalidError(f"401 Unauthorized: {resp.text[:100]}")
  424. elif resp.status_code == 403:
  425. if "Just a moment" in resp.text or "cloudflare" in resp.text.lower():
  426. self._log(f"[TRAFFIC] HTTP 403 (Cloudflare) detected. Re-verifying (Try {retry_count+1}/3)...")
  427. if retry_count < 3:
  428. new_token = self._refresh_turnstile_token()
  429. if new_token:
  430. self._log("[TRAFFIC] In-page verification success. Retrying...")
  431. if json_data and "captcha_api_key" in json_data:
  432. json_data["captcha_api_key"] = new_token
  433. return self._perform_request(method, url, headers, data, json_data, params, retry_count+1)
  434. raise PermissionDeniedError(f"HTTP 403 Forbidden: {resp.text[:100]}")
  435. elif resp.status_code == 429:
  436. self.is_healthy = False
  437. raise RateLimiteddError(f"429 Rate Limit: {resp.text[:100]}")
  438. elif resp.status_code == 0:
  439. raise BizLogicError(f"Network Error (Fetch Failed): {resp.text}")
  440. else:
  441. if url.endswith("/login") and resp.status_code == 400:
  442. return resp
  443. raise BizLogicError(message=f"HTTP Error {resp.status_code}: {resp.text[:100]}")
  444. def _handle_cookie_banner(self):
  445. """
  446. 处理 OneTrust Cookie 遮挡
  447. 策略:尝试点击“接受所有”,如果点不到就直接移除 DOM
  448. """
  449. try:
  450. js = """
  451. try {
  452. var acceptBtn = document.getElementById('onetrust-accept-btn-handler');
  453. if (acceptBtn) {
  454. acceptBtn.click();
  455. return true;
  456. }
  457. var banner = document.getElementById('onetrust-banner-sdk');
  458. if (banner) {
  459. banner.style.display = 'none';
  460. banner.remove();
  461. return true;
  462. }
  463. } catch(e) {}
  464. return false;
  465. """
  466. self.page.run_js(js)
  467. except:
  468. pass
  469. def _get_realnetwork_ip(self):
  470. """
  471. 通过新建标签页获取 IP
  472. 解决 CORS 403 问题:新标签页请求属于 Top-Level Navigation,
  473. 不带 Origin: visa.vfsglobal.com,也不带 credentials,符合 ipify 规则。
  474. """
  475. try:
  476. tab = self.page.new_tab("https://api.ipify.org/?format=json")
  477. if tab.ele('tag:pre'):
  478. json_text = tab.ele('tag:pre').text
  479. else:
  480. json_text = tab.ele('tag:body').text
  481. ip = json.loads(json_text)['ip']
  482. tab.close()
  483. self._log(f"Real Network IP: {ip}")
  484. return ip
  485. except Exception as e:
  486. self._log(f"[WARN] Failed to check IP via new tab: {e}")
  487. try:
  488. if self.page.tabs_count > 1:
  489. tab.close()
  490. except:
  491. pass
  492. return "0.0.0.0"
  493. def _get_common_headers(self, with_auth=True) -> Dict[str, str]:
  494. # DrissionPage 浏览器会自动带上 Origin, Referer, User-Agent, Sec-CH-UA 等,这里只需要补充业务特定的 Headers
  495. mission = self.free_config.get("mission_code", "")
  496. country = self.free_config.get("country_code", "")
  497. lang = self.free_config.get("language", "en")
  498. route = f"{country}/{lang}/{mission}"
  499. h = {
  500. "accept": "application/json, text/plain, */*",
  501. "route": route
  502. }
  503. h["clientsource"] = self._get_client_source()
  504. if with_auth and self.jwt_token:
  505. h["authorize"] = self.jwt_token
  506. return h
  507. def _encrypt_password(self, password: str) -> str:
  508. ciphertext = self.public_key.encrypt(
  509. password.encode(),
  510. padding.OAEP(
  511. mgf=padding.MGF1(algorithm=hashes.SHA256()),
  512. algorithm=hashes.SHA256(),
  513. label=None
  514. )
  515. )
  516. return base64.b64encode(ciphertext).decode()
  517. def _get_orange_source(self, email: str) -> str:
  518. timestamp = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
  519. payload = f"{email};{timestamp}"
  520. return self._encrypt_password(payload)
  521. def _get_client_source(self) -> str:
  522. timestamp = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
  523. payload = f"GA;{timestamp}Z"
  524. return self._encrypt_password(payload)
  525. def _query_earliest_slot(self, apt_config) -> Optional[str]:
  526. url = f"https://{self.vfs_api_domain}/appointment/CheckIsSlotAvailable"
  527. data = {
  528. "missioncode": self.free_config.get("mission_code"),
  529. "countrycode": self.free_config.get("country_code"),
  530. "vacCode": apt_config.get("vac_code"),
  531. "visaCategoryCode": apt_config.get("subcategory_code"),
  532. "roleName": "Individual",
  533. "loginUser": self.config.account.username,
  534. "payCode": ""
  535. }
  536. headers = self._get_common_headers(with_auth=True)
  537. resp = self._perform_request("POST", url, headers=headers, json_data=data, retry_count=2)
  538. if "WaitList" in resp.text:
  539. return "WaitList"
  540. j = resp.json()
  541. if j.get("earliestSlotLists"):
  542. raw_date = j["earliestSlotLists"][0]["date"]
  543. return to_yyyymmdd(raw_date, "%m/%d/%Y %H:%M:%S")
  544. return ""
  545. def _fetch_configurations(self, apt_config: Dict[str, Any]):
  546. if not self.center_conf:
  547. self.center_conf = self._query_center()
  548. vac_code = apt_config.get("vac_code")
  549. category_code = apt_config.get("category_code")
  550. if category_code not in self.category_conf:
  551. visa_categories = self._query_visa_category(vac_code)
  552. found = False
  553. for vc in visa_categories:
  554. if vc.get("code") == category_code:
  555. self.category_conf[category_code] = vc
  556. found = True
  557. break
  558. if not found:
  559. self._log(f"WARN: Category {category_code} not found")
  560. sub_category_code = apt_config.get("subcategory_code")
  561. if sub_category_code not in self.subcategory_conf:
  562. visa_subcategories = self._query_visa_sub_category(vac_code, category_code)
  563. found = False
  564. for svc in visa_subcategories:
  565. if svc.get("code") == sub_category_code:
  566. self.subcategory_conf[sub_category_code] = svc
  567. found = True
  568. break
  569. if not found:
  570. self._log(f"WARN: SubCategory {sub_category_code} not found")
  571. def _query_center(self) -> List:
  572. mission = self.free_config.get("mission_code")
  573. country = self.free_config.get("country_code")
  574. url = f"https://{self.vfs_api_domain}/master/center/{mission}/{country}/en-US"
  575. headers = self._get_common_headers(with_auth=False)
  576. resp = self._perform_request("GET", url, headers=headers)
  577. return resp.json()
  578. def _query_visa_category(self, center_code: str) -> List:
  579. mission = self.free_config.get("mission_code")
  580. country = self.free_config.get("country_code")
  581. enc_center = urllib.parse.quote(center_code)
  582. url = f"https://{self.vfs_api_domain}/master/visacategory/{mission}/{country}/{enc_center}/en-US"
  583. headers = self._get_common_headers(with_auth=False)
  584. resp = self._perform_request("GET", url, headers=headers)
  585. return resp.json()
  586. def _query_visa_sub_category(self, center_code: str, category_code: str) -> List:
  587. mission = self.free_config.get("mission_code")
  588. country = self.free_config.get("country_code")
  589. enc_center = urllib.parse.quote(center_code)
  590. enc_cat = urllib.parse.quote(category_code)
  591. url = f"https://{self.vfs_api_domain}/master/subvisacategory/{mission}/{country}/{enc_center}/{enc_cat}/en-US"
  592. headers = self._get_common_headers(with_auth=False)
  593. resp = self._perform_request("GET", url, headers=headers)
  594. return resp.json()
  595. def _read_otp_email(self, sent_at='', sender='VFS Global') -> str:
  596. # 保持原样,这部分使用云API读取邮件,不依赖本地网络库
  597. master_email = "hujiarui8@gmail.com"
  598. recipient = self.config.account.username
  599. subject_keywords = "One Time Password"
  600. body_keywords = "OTP"
  601. if not sent_at:
  602. now_utc = datetime.utcnow()
  603. sent_at = now_utc.strftime("%Y-%m-%d %H:%M:%S")
  604. self._log(f"Waiting for OTP email...")
  605. content_out = VSCloudApi.Instance().fetch_mail_content(
  606. master_email, sender, recipient, subject_keywords, body_keywords, sent_at, 300, only_text=False
  607. )
  608. if content_out:
  609. parts = content_out.split("\n")
  610. text_parts = [p for p in parts if not p.startswith("data:image")]
  611. email_text = " ".join(text_parts)
  612. image_urls = [p for p in parts if p.startswith("data:image")]
  613. match = re.search(r'\b\d{6}\b', email_text)
  614. if match:
  615. self._log("Found OTP from email text via Regex.")
  616. return match.group(0)
  617. if image_urls:
  618. self._log("OTP not found in text, trying LLM image recognition...")
  619. target_image_b64 = image_urls[0]
  620. otp_code = recognize_vfs_captcha_with_qwen(target_image_b64)
  621. if otp_code:
  622. self._log(f"Successfully extracted OTP from image: {otp_code}")
  623. return otp_code
  624. raise BizLogicError(message="OTP code not found")
  625. def _submit_login_otp(self, old_cf_token: str, otp: str):
  626. self._log("Submitting Login OTP...")
  627. new_cf_token = self._refresh_turnstile_token()
  628. email = self.config.account.username
  629. password = self.config.account.password
  630. enc_password = self._encrypt_password(password)
  631. mission = self.free_config.get("mission_code", "")
  632. country = self.free_config.get("country_code", "")
  633. client_src = self._get_client_source()
  634. orange_src = self._get_orange_source(email)
  635. url = f"https://{self.vfs_api_domain}/user/login"
  636. headers = self._get_common_headers(with_auth=False)
  637. headers.update({
  638. "clientsource": client_src,
  639. "orangex": orange_src
  640. })
  641. data = {
  642. "username": email,
  643. "password": enc_password,
  644. "missioncode": mission,
  645. "countrycode": country,
  646. "languageCode": "en-US",
  647. "captcha_version": "cloudflare-v1",
  648. "captcha_api_key": new_cf_token,
  649. "otp": otp
  650. }
  651. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  652. resp_json = resp.json()
  653. if resp_json.get("accessToken"):
  654. self.jwt_token = resp_json["accessToken"]
  655. self._log("OTP Login successful.")
  656. return
  657. error_desc = resp_json.get("description", resp.text)
  658. raise PermissionDeniedError(message=f"OTP Login Failed: {error_desc}")
  659. def _refresh_turnstile_token(self) -> str:
  660. """
  661. 强制刷新 Cloudflare Turnstile 并获取新 Token (集成 CloudflareBypasser 版)
  662. """
  663. self._log("Refreshing Cloudflare Turnstile token...")
  664. js_reset = """
  665. try {
  666. var input = document.querySelector('input[name="cf-turnstile-response"]');
  667. if (input) input.value = "";
  668. window.turnstile.reset();
  669. } catch(e) {
  670. console.log("Turnstile reset error:", e);
  671. }
  672. """
  673. self.page.run_js(js_reset)
  674. cf_bypasser = CloudflareBypasser(self.page, log=self.config.debug)
  675. for i in range(60):
  676. time.sleep(0.5)
  677. try:
  678. ele = self.page.ele('@name=cf-turnstile-response', timeout=1)
  679. if ele and ele.value:
  680. self._log("Turnstile token refreshed successfully.")
  681. return ele.value
  682. except:
  683. pass
  684. if i > 4:
  685. self._handle_cookie_banner()
  686. try:
  687. use_dfs = False
  688. cf_bypasser.click_verification_button(use_dfs)
  689. except Exception as e:
  690. pass
  691. raise BizLogicError("Failed to refresh Cloudflare Turnstile token (Timeout)")
  692. def book(self, slot_info: VSQueryResult, user_inputs) -> VSBookResult:
  693. """
  694. 执行完整的预约流程
  695. """
  696. self._log("Starting booking process...")
  697. user_email = user_inputs.get('email')
  698. user_inputs['alias_email'] = get_alias_email(user_email, new_domain="text.skin")
  699. res = VSBookResult()
  700. app_type = slot_info.apt_type
  701. from_date = slot_info.earliest_date.strftime("%Y-%m-%d") if slot_info.earliest_date else datetime.now().strftime("%Y-%m-%d")
  702. expected_start = user_inputs.get("expected_start_date", "2000-01-01")
  703. expected_end = user_inputs.get("expected_end_date", "2100-01-01")
  704. if from_date >= expected_end:
  705. self._log("No valid slots in date preference.")
  706. res.success = False
  707. return res
  708. apt_config = self.free_config.get("apt_configs", {}).get(app_type.routing_key)
  709. if not apt_config:
  710. raise NotFoundError(message="Book: Config missing for this routing key.")
  711. self._fetch_configurations(apt_config)
  712. sub_cc = apt_config.get("subcategory_code")
  713. sub_conf = self.subcategory_conf.get(sub_cc, {})
  714. ocr_enabled = sub_conf.get("isOCREnable", False)
  715. if ocr_enabled:
  716. self._log("OCR Enabled, uploading documents...")
  717. upload_res = self._upload_applicant_documents(apt_config, user_inputs)
  718. user_inputs["applicant_image"] = upload_res.get("passportImageFilename")
  719. user_inputs["applicant_image_data"] = upload_res.get("passportImageFileBytes")
  720. user_inputs["guid"] = upload_res.get("uploadDocumentGUID")
  721. enable_reference_number = sub_conf.get("enableReferenceNumber", False)
  722. final_urn = None
  723. is_waitlist = (slot_info.availability_status == AvailabilityStatus.Waitlist)
  724. if not self.booking_wait_applied:
  725. self._log("pre-booking wait: sleeping 20s before booking to avoid risk control")
  726. time.sleep(20)
  727. self.booking_wait_applied = True
  728. MAX_RETRY = 2
  729. for i in range(MAX_RETRY):
  730. try:
  731. final_urn = self._add_primary_applicant(apt_config, user_inputs, is_waitlist, ocr_enabled, enable_reference_number)
  732. break
  733. except BizLogicError as e:
  734. err_msg = str(e)
  735. self._log(f"Add Applicant retry {i+1}/{MAX_RETRY}: {err_msg}")
  736. if 'Capping has exceeded' in err_msg:
  737. raise e
  738. time.sleep(10.0)
  739. if not final_urn:
  740. raise BizLogicError(message="Failed to add primary applicant (Slot likely taken or API error)")
  741. self._log(f"Applicant Added. URN: {final_urn}")
  742. otp_enabled = sub_conf.get("isApplicantOTPEnabled", False)
  743. if otp_enabled:
  744. self._log("Applicant OTP Required.")
  745. now_utc = datetime.utcnow()
  746. sent_at = now_utc.strftime("%Y-%m-%d %H:%M:%S")
  747. if not self._applicant_otp_send(apt_config, final_urn):
  748. raise BizLogicError(message='Applicant OTP send failed')
  749. otp_code = self._read_otp_email(sent_at=sent_at)
  750. if not self._applicant_otp_verify(apt_config, final_urn, otp_code):
  751. raise BizLogicError(message='Applicant OTP verify failed')
  752. if is_waitlist:
  753. if self._confirm_waitlist(apt_config, final_urn):
  754. res.success = True
  755. res.urn = final_urn
  756. res.account = self.config.account.username
  757. self._log("Waitlist confirmed.")
  758. return res
  759. raise BizLogicError(message='Confirm waitlist failed')
  760. months = self._get_filtered_covered_months(expected_start, expected_end, from_date)
  761. self._log(f"Scanning months: {months} (Start looking from: {from_date})")
  762. selected_slot_id = ""
  763. selected_slot_date = ""
  764. selected_slot_time_range = ""
  765. all_ads = set()
  766. forbidden_dates = set()
  767. found_slot = False
  768. for m_str in months:
  769. self._log(f"Checking calendar for {m_str}...")
  770. ads = self._query_slot_calendar(apt_config, final_urn, m_str)
  771. new_ads = [d for d in ads if d not in all_ads]
  772. all_ads.update(new_ads)
  773. for _ in range(3):
  774. avail_candidates = [d for d in list(all_ads) if d not in forbidden_dates]
  775. sel_dates = self._filter_dates(avail_candidates, expected_start, expected_end)
  776. if not sel_dates:
  777. break
  778. tmp_date = sel_dates[0]
  779. forbidden_dates.add(tmp_date)
  780. if not self._saveuseractionaudit(apt_config, final_urn, tmp_date):
  781. self._log(f"Audit failed for {tmp_date}, skipping...")
  782. time.sleep(1)
  783. continue
  784. ats = self._query_slot_time(apt_config, final_urn, tmp_date)
  785. if not ats:
  786. self._log(f"No timeslots for {tmp_date}")
  787. continue
  788. sel_tm = random.choice(ats)
  789. selected_slot_id = sel_tm.get("allocationId")
  790. selected_slot_date = tmp_date
  791. selected_slot_time_range = sel_tm.get("slot")
  792. found_slot = True
  793. break
  794. if found_slot:
  795. break
  796. if not found_slot:
  797. self._log("No valid slots found after scanning.")
  798. res.success = False
  799. return res
  800. self._log(f"Slot Selected: {selected_slot_date} {selected_slot_time_range} (ID: {selected_slot_id})")
  801. self._submit_no_addition_service(final_urn)
  802. amount, currency = self._query_fee(apt_config, final_urn)
  803. self._log("Submitting schedule...")
  804. schedule_res = self._schedule(apt_config, final_urn, amount, currency, selected_slot_id)
  805. if not schedule_res.get("IsAppointmentBooked"):
  806. self._log(f"Booking failed: {schedule_res}")
  807. res.success = False
  808. return res
  809. res.success = True
  810. res.account = self.config.account.username
  811. res.book_date = selected_slot_date
  812. res.book_time = selected_slot_time_range
  813. res.urn = final_urn
  814. if schedule_res.get("IsPaymentRequired", False):
  815. payload = schedule_res.get("payLoad", "")
  816. if payload:
  817. self._log("Processing payment link...")
  818. payment_url = self._pay_request(payload)
  819. if payment_url:
  820. res.payment_link = payment_url
  821. return res
  822. def _get_application(self):
  823. url = f"https://{self.vfs_api_domain}/appointment/application"
  824. headers = self._get_common_headers(with_auth=True)
  825. data = {
  826. 'countryCode': self.free_config.get("country_code"),
  827. 'missionCode': self.free_config.get("mission_code"),
  828. 'loginUser': self.config.account.username,
  829. 'languageCode': 'en-US',
  830. }
  831. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  832. return resp.json()
  833. def _upload_applicant_documents(self, apt_config, user_inputs) -> Dict:
  834. """上传图片:根据前缀判断是下载外部图片还是读取本地文件,并上传到 VFS"""
  835. import requests as standard_requests
  836. url = f"https://{self.vfs_api_domain}/appointment/UploadApplicantDocument"
  837. passport_path_or_url = user_inputs.get("passport_image_url")
  838. if not passport_path_or_url:
  839. raise NotFoundError(message="Missing passport_image_url")
  840. if passport_path_or_url.startswith("http://") or passport_path_or_url.startswith("https://"):
  841. img_resp = standard_requests.get(passport_path_or_url, timeout=30)
  842. if img_resp.status_code != 200:
  843. raise BizLogicError(message=f"Failed to download passport image: {img_resp.status_code}")
  844. img_data = img_resp.content
  845. else:
  846. if not os.path.exists(passport_path_or_url):
  847. raise BizLogicError(message=f"Local passport image file not found: {passport_path_or_url}")
  848. with open(passport_path_or_url, "rb") as f:
  849. img_data = f.read()
  850. b64_str = base64.b64encode(img_data).decode('utf-8')
  851. headers = self._get_common_headers(with_auth=True)
  852. data = {
  853. "missioncode": self.free_config.get("mission_code"),
  854. "countryCode": self.free_config.get("country_code"),
  855. "centerCode": apt_config.get("vac_code"),
  856. "loginUser": self.config.account.username,
  857. "languageCode": "en-US",
  858. "visaCategoryCode": apt_config.get("subcategory_code"),
  859. "fileBytes": b64_str,
  860. "selfiImageFileBytes": ""
  861. }
  862. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  863. result = resp.json()
  864. result["passportImageFilename"] = "passport_img.jpg"
  865. result["passportImageFileBytes"] = b64_str
  866. return result
  867. def _add_primary_applicant(self, apt_config: Dict[str, Any], user_inputs: Dict[str, Any],
  868. is_waitlist: bool, ocr_enabled: bool, enable_ref: bool) -> str:
  869. """构造申请人 payload 并提交"""
  870. url = f"https://{self.vfs_api_domain}/appointment/applicants"
  871. headers = self._get_common_headers(with_auth=True)
  872. gender_str = str(user_inputs.get("gender", "")).lower()
  873. gender_code = 1 if gender_str == "male" else 2
  874. raw_dial = user_inputs.get("phone_country_code", "86")
  875. dial_code = str(raw_dial)
  876. # 日期格式转换 YYYY-MM-DD -> DD/MM/YYYY
  877. def _to_ddmmyyyy(d_str):
  878. try:
  879. return datetime.strptime(str(d_str), "%Y-%m-%d").strftime("%d/%m/%Y")
  880. except:
  881. return str(d_str)
  882. dob = _to_ddmmyyyy(user_inputs.get("birthday", ""))
  883. ppt_exp = _to_ddmmyyyy(user_inputs.get("passport_expiry_date", ""))
  884. applicant = {
  885. "urn": "",
  886. "arn": "",
  887. "loginUser": self.config.account.username,
  888. "firstName": str(user_inputs.get("first_name", "")).strip().upper(),
  889. "middleName": "",
  890. "lastName": str(user_inputs.get("last_name", "")).strip().upper(),
  891. "employerFirstName": "",
  892. "employerLastName": "",
  893. "salutation": "",
  894. "Subclasscode": None,
  895. "VisaToken": None,
  896. "centerClassCode": None,
  897. "dateOfApplication": None,
  898. "selectedSubvisaCategory": None,
  899. "gender": gender_code,
  900. "contactNumber": str(user_inputs.get("phone", "")).strip().lstrip("0"),
  901. "dialCode": dial_code,
  902. "employerContactNumber": "",
  903. "employerDialCode": "",
  904. "emailId": str(user_inputs.get("alias_email", "")).strip().upper(),
  905. "employerEmailId": "",
  906. "passportNumber": str(user_inputs.get("passport_no", "")).strip().upper(),
  907. "confirmPassportNumber": "",
  908. "passportExpirtyDate": ppt_exp,
  909. "dateOfBirth": dob,
  910. "nationalId": None,
  911. "nationalityCode": get_country_iso3(str(user_inputs.get("nationality", ""))),
  912. "state": user_inputs.get("state"),
  913. "city": user_inputs.get("city"),
  914. "addressline1": user_inputs.get("addressline1"),
  915. "addressline2": user_inputs.get("addressline2"),
  916. "pincode": None,
  917. "isEndorsedChild": False,
  918. "applicantType": 0,
  919. "vlnNumber": None,
  920. "applicantGroupId": 0,
  921. "parentPassportNumber": "",
  922. "parentPassportExpiry": "",
  923. "dateOfDeparture": None,
  924. "entryType": "",
  925. "eoiVisaType": "",
  926. "passportType": "",
  927. "vfsReferenceNumber": "",
  928. "familyReunificationCerificateNumber": "",
  929. "PVRequestRefNumber": "",
  930. "PVStatus": "",
  931. "PVStatusDescription": "",
  932. "PVCanAllowRetry": True,
  933. "PVisVerified": False,
  934. "eefRegistrationNumber": "",
  935. "isAutoRefresh": True,
  936. "helloVerifyNumber": "",
  937. "OfflineCClink": "",
  938. "idenfystatuscheck": False,
  939. "vafStatus": None,
  940. "SpecialAssistance": "",
  941. "AdditionalRefNo": None,
  942. "juridictionCode": "",
  943. "canInitiateVAF": False,
  944. "canEditVAF": False,
  945. "canDeleteVAF": False,
  946. "canDownloadVAF": False,
  947. "Retryleft": "",
  948. # 这里的 IP 应该已经在 create_session 时获取到了
  949. "ipAddress": self.real_ip
  950. }
  951. if enable_ref:
  952. applicant["referenceNumber"] = str(user_inputs.get("cover_letter_id", "")).strip()
  953. else:
  954. applicant["referenceNumber"] = None
  955. if ocr_enabled:
  956. applicant["applicantImage"] = str(user_inputs.get("applicant_image", ""))
  957. applicant["applicantImageData"] = str(user_inputs.get("applicant_image_data", ""))
  958. applicant["GUID"] = str(user_inputs.get("guid", ""))
  959. payload = {
  960. "countryCode": self.free_config.get("country_code"),
  961. "missionCode": self.free_config.get("mission_code"),
  962. "centerCode": apt_config.get("vac_code"),
  963. "loginUser": self.config.account.username,
  964. "visaCategoryCode": apt_config.get("subcategory_code"),
  965. "applicantList": [applicant],
  966. "languageCode": "en-US",
  967. "isWaitlist": is_waitlist,
  968. "isEdit": False,
  969. "feeEntryTypeCode": None, "feeExemptionTypeCode": None,
  970. "feeExemptionDetailsCode": None, "juridictionCode": None, "regionCode": None
  971. }
  972. resp = self._perform_request("POST", url, headers=headers, json_data=payload)
  973. urn = resp.json().get("urn")
  974. if not urn:
  975. err_msg = resp.json().get('error')
  976. raise BizLogicError(message=str(err_msg))
  977. return urn
  978. def _applicant_otp_send(self, apt_config, urn) -> bool:
  979. url = f"https://{self.vfs_api_domain}/appointment/applicantotp"
  980. headers = self._get_common_headers(with_auth=True)
  981. data = {
  982. "urn": urn,
  983. "loginUser": self.config.account.username,
  984. "missionCode": self.free_config.get("mission_code"),
  985. "countryCode": self.free_config.get("country_code"),
  986. "centerCode": apt_config.get("vac_code"),
  987. "OTP": "",
  988. "otpAction": "GENERATE",
  989. "languageCode": "en-US"
  990. }
  991. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  992. return resp.json().get("isOTPGenerated", False)
  993. def _applicant_otp_verify(self, apt_config, urn, otp) -> bool:
  994. url = f"https://{self.vfs_api_domain}/appointment/applicantotp"
  995. headers = self._get_common_headers(with_auth=True)
  996. # VFS 这里的 header 有时需要 datacenter,原代码有就加上
  997. headers["datacenter"] = "GERMANY"
  998. data = {
  999. "urn": urn,
  1000. "loginUser": self.config.account.username,
  1001. "missionCode": self.free_config.get("mission_code"),
  1002. "countryCode": self.free_config.get("country_code"),
  1003. "centerCode": apt_config.get("vac_code"),
  1004. "OTP": otp,
  1005. "otpAction": "VALIDATE",
  1006. "languageCode": "en-US"
  1007. }
  1008. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  1009. return resp.json().get("isOTPValidated", False)
  1010. def _query_slot_calendar(self, apt_config, urn, from_date) -> List:
  1011. url = f"https://{self.vfs_api_domain}/appointment/calendar"
  1012. headers = self._get_common_headers(with_auth=True)
  1013. # 将 YYYY-MM-DD 转为 DD/MM/YYYY 用于 API
  1014. dt_m = datetime.strptime(from_date, "%Y-%m-%d")
  1015. converted_date = dt_m.strftime("%d/%m/%Y")
  1016. data = {
  1017. "missionCode": self.free_config.get("mission_code"),
  1018. "countryCode": self.free_config.get("country_code"),
  1019. "centerCode": apt_config.get("vac_code"),
  1020. "loginUser": self.config.account.username,
  1021. "visaCategoryCode": apt_config.get("subcategory_code"),
  1022. "fromDate": converted_date,
  1023. "urn": urn,
  1024. "payCode": ""
  1025. }
  1026. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  1027. calendars = resp.json().get("calendars")
  1028. ads_out = []
  1029. if calendars:
  1030. for item in calendars:
  1031. # 通常是 MM/DD/YYYY
  1032. raw = item.get("date")
  1033. ads_out.append(to_yyyymmdd(raw, "%m/%d/%Y"))
  1034. return ads_out
  1035. def _query_slot_time(self, apt_config, urn, slot_date) -> List:
  1036. url = f"https://{self.vfs_api_domain}/appointment/timeslot"
  1037. headers = self._get_common_headers(with_auth=True)
  1038. dt_m = datetime.strptime(slot_date, "%Y-%m-%d")
  1039. converted_date = dt_m.strftime("%d/%m/%Y")
  1040. data = {
  1041. "missionCode": self.free_config.get("mission_code"),
  1042. "countryCode": self.free_config.get("country_code"),
  1043. "centerCode": apt_config.get("vac_code"),
  1044. "loginUser": self.config.account.username,
  1045. "visaCategoryCode": apt_config.get("subcategory_code"),
  1046. "slotDate": converted_date,
  1047. "urn": urn
  1048. }
  1049. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  1050. return resp.json().get("slots", [])
  1051. def _saveuseractionaudit(self, apt_config, urn, earliest_date) -> bool:
  1052. url = f"https://{self.vfs_api_domain}/appointment/saveuseractionaudit"
  1053. headers = self._get_common_headers(with_auth=True)
  1054. dt = datetime.strptime(earliest_date, "%Y-%m-%d")
  1055. data = {
  1056. "missionCode": self.free_config.get("mission_code"),
  1057. "countryCode": self.free_config.get("country_code"),
  1058. "centerCode": apt_config.get("vac_code"),
  1059. "loginUser": self.config.account.username,
  1060. "urn": urn,
  1061. "firstEarliestSlotDate": dt.strftime("%d/%m/%Y"),
  1062. "action": "schedule",
  1063. "ipAddress": self.real_ip,
  1064. "eadAppointmentDetail": dt.strftime("%Y-%m-%dT%H:%M:%S")
  1065. }
  1066. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  1067. return resp.json().get("isSavedSuccess", False)
  1068. def _submit_no_addition_service(self, urn):
  1069. url = f"https://{self.vfs_api_domain}/vas/mapvas"
  1070. headers = self._get_common_headers(with_auth=True)
  1071. data = {
  1072. "loginUser": self.config.account.username,
  1073. "missionCode": self.free_config.get("mission_code"),
  1074. "countryCode": self.free_config.get("country_code"),
  1075. "urn": urn,
  1076. "applicants": []
  1077. }
  1078. self._perform_request("POST", url, headers=headers, json_data=data)
  1079. def _query_fee(self, apt_config, urn) -> Tuple[float, str]:
  1080. url = f"https://{self.vfs_api_domain}/appointment/fees"
  1081. headers = self._get_common_headers(with_auth=True)
  1082. data = {
  1083. "missionCode": self.free_config.get("mission_code"),
  1084. "countryCode": self.free_config.get("country_code"),
  1085. "centerCode": apt_config.get("vac_code"),
  1086. "loginUser": self.config.account.username,
  1087. "urn": urn,
  1088. "languageCode": "en-US"
  1089. }
  1090. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  1091. j = resp.json()
  1092. total = j.get("totalamount", 0.0)
  1093. currency = "EUR"
  1094. if j.get("feeDetails"):
  1095. currency = j["feeDetails"][0].get("currency", "EUR")
  1096. return total, currency
  1097. def _schedule(self, apt_config, urn, amount, currency, slot_id) -> Dict:
  1098. url = f"https://{self.vfs_api_domain}/appointment/schedule"
  1099. headers = self._get_common_headers(with_auth=True)
  1100. data = {
  1101. "missionCode": self.free_config.get("mission_code"),
  1102. "countryCode": self.free_config.get("country_code"),
  1103. "centerCode": apt_config.get("vac_code"),
  1104. "loginUser": self.config.account.username,
  1105. "urn": urn,
  1106. "notificationType": "none",
  1107. "paymentdetails": {
  1108. "paymentmode": "Online",
  1109. "RequestRefNo": "",
  1110. "clientId": "",
  1111. "merchantId": "",
  1112. "amount": amount,
  1113. "currency": currency
  1114. },
  1115. "allocationId": str(slot_id),
  1116. "CanVFSReachoutToApplicant": True
  1117. }
  1118. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  1119. return resp.json()
  1120. def _pay_request(self, payload) -> str:
  1121. """
  1122. 解析支付重定向 URL (DrissionPage 新标签页版)
  1123. """
  1124. start_url = f"https://online.vfsglobal.com/PG-Component/Payment/PayRequest?payLoad={payload}"
  1125. final_url = ""
  1126. try:
  1127. self._log("Resolving payment redirect...")
  1128. pay_tab = self.page.new_tab(start_url)
  1129. pay_tab.wait.url_change(start_url, timeout=15)
  1130. final_url = pay_tab.url
  1131. self._log(f"Payment URL resolved: {final_url}")
  1132. pay_tab.close()
  1133. except Exception as e:
  1134. self._log(f"[WARN] Failed to resolve payment URL: {e}")
  1135. try:
  1136. pay_tab.close()
  1137. except:
  1138. pass
  1139. return final_url
  1140. def _confirm_waitlist(self, apt_config: Dict[str, Any], urn: str) -> bool:
  1141. url = f"https://{self.vfs_api_domain}/appointment/ConfirmWaitlist"
  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. "notificationType": "none",
  1150. "CanVFSReachoutToApplicant": True
  1151. }
  1152. resp = self._perform_request("POST", url, headers=headers, json_data=data)
  1153. return resp.json().get("isConfirmed", False)
  1154. def _filter_dates(self, dates: List[str], start_str: str, end_str: str) -> List[str]:
  1155. if not start_str or not end_str:
  1156. return dates
  1157. valid_dates = []
  1158. try:
  1159. s_date = datetime.strptime(start_str[:10], "%Y-%m-%d")
  1160. e_date = datetime.strptime(end_str[:10], "%Y-%m-%d")
  1161. for date_str in dates:
  1162. curr_date = datetime.strptime(date_str, "%Y-%m-%d")
  1163. if s_date <= curr_date <= e_date:
  1164. valid_dates.append(date_str)
  1165. random.shuffle(valid_dates)
  1166. return valid_dates
  1167. except:
  1168. return dates
  1169. def _get_filtered_covered_months(self, start_date, end_date, from_date) -> List[str]:
  1170. fmt = "%Y-%m-%d"
  1171. try:
  1172. dt_start = datetime.strptime(start_date, fmt) if start_date else datetime.now()
  1173. dt_end = datetime.strptime(end_date, fmt) if end_date else datetime.now().replace(year=datetime.now().year + 1)
  1174. try:
  1175. dt_from = datetime.strptime(from_date, fmt)
  1176. except:
  1177. dt_from = datetime.now()
  1178. except:
  1179. return []
  1180. dt_start = dt_start.replace(day=1)
  1181. dt_end = dt_end.replace(day=1)
  1182. dt_from = dt_from.replace(day=1)
  1183. curr = max(dt_start, dt_from)
  1184. months = []
  1185. while curr <= dt_end:
  1186. months.append(curr.strftime(fmt))
  1187. if curr.month == 12:
  1188. curr = curr.replace(year=curr.year + 1, month=1)
  1189. else:
  1190. curr = curr.replace(month=curr.month + 1)
  1191. return months
  1192. def cleanup(self):
  1193. """
  1194. 销毁浏览器并彻底删除临时文件
  1195. """
  1196. if self.page:
  1197. try:
  1198. self.page.quit()
  1199. except Exception:
  1200. pass
  1201. self.page = None
  1202. if os.path.exists(self.root_workspace):
  1203. for _ in range(3):
  1204. try:
  1205. time.sleep(0.2)
  1206. shutil.rmtree(self.root_workspace, ignore_errors=True)
  1207. break
  1208. except Exception as e:
  1209. self._log(f"Cleanup retry: {e}")
  1210. time.sleep(0.5)
  1211. if os.path.exists(self.root_workspace):
  1212. self._log(f"[WARN] Failed to fully remove workspace: {self.root_workspace}")
  1213. if self.tunnel:
  1214. try: self.tunnel.stop()
  1215. except: pass
  1216. self.tunnel = None
  1217. def get_plugin_status(self) -> PluginStatus:
  1218. account_name = self.config.account.username if self.config and self.config.account else ""
  1219. proxy_info = f"{self.config.proxy.ip}:{self.config.proxy.port}" if self.config and self.config.proxy and self.config.proxy.ip else ""
  1220. current_url = ""
  1221. if self.page:
  1222. try:
  1223. current_url = self.page.url
  1224. except Exception:
  1225. pass
  1226. session_age = int(time.time() - self.session_create_time) if self.session_create_time > 0 else 0
  1227. state = "running" if self.is_healthy else "unhealthy"
  1228. if not self.jwt_token and self.page:
  1229. state = "logging_in"
  1230. return PluginStatus(
  1231. instance_id=self.instance_id,
  1232. is_healthy=self.is_healthy,
  1233. state=state,
  1234. current_url=current_url,
  1235. account=account_name,
  1236. proxy=proxy_info,
  1237. session_age_sec=session_age,
  1238. last_active_at=utc_now_str(),
  1239. )
  1240. def __del__(self):
  1241. """
  1242. 析构函数:当对象被垃圾回收时自动调用
  1243. """
  1244. self.cleanup()