tls_plugin.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. import time
  2. import json
  3. import random
  4. import re
  5. import os
  6. from datetime import datetime
  7. from typing import List, Dict, Optional, Any
  8. from urllib.parse import urljoin, urlparse
  9. from curl_cffi import requests, const
  10. from bs4 import BeautifulSoup
  11. from vs_plg import IVSPlg
  12. from vs_types import VSPlgConfig, VSQueryResult, VSBookResult, AvailabilityStatus, NotFoundError, PermissionDeniedError, RateLimiteddError, SessionExpiredOrInvalidError, BizLogicError
  13. from vs_log_macros import VSC_INFO, VSC_ERROR, VSC_DEBUG, VSC_WARN
  14. from toolkit.vs_cloud_api import VSCloudApi
  15. class TlsPlugin(IVSPlg):
  16. """
  17. TLSContact 签证预约插件
  18. 适配法国签证 (FR) 流程
  19. """
  20. def __init__(self, group_id: str):
  21. self.group_id = group_id
  22. self.config: Optional[VSPlgConfig] = None
  23. self.free_config: Dict[str, Any] = {}
  24. self.is_healthy = True
  25. # 会话相关
  26. self.session: Optional[requests.Session] = None
  27. self.travel_group: Optional[Dict] = None
  28. self.user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
  29. def get_group_id(self) -> str:
  30. return self.group_id
  31. def set_config(self, config: VSPlgConfig):
  32. self.config = config
  33. try:
  34. self.free_config = json.loads(config.free_config) if config.free_config else {}
  35. except:
  36. self.free_config = {}
  37. def health_check(self) -> bool:
  38. return self.is_healthy
  39. def create_session(self):
  40. """
  41. 创建会话:处理 Cloudflare -> 登录 -> 获取 Travel Group
  42. """
  43. # 1. 初始化 Session
  44. curlopt = {
  45. const.CurlOpt.MAXAGE_CONN: 1800,
  46. const.CurlOpt.MAXLIFETIME_CONN: 1800,
  47. const.CurlOpt.VERBOSE: self.config.debug,
  48. }
  49. self.session = requests.Session(
  50. proxy=self._get_proxy_url(),
  51. impersonate="chrome124",
  52. curl_options=curlopt,
  53. use_thread_local_curl=False,
  54. http_version=const.CurlHttpVersion.V2TLS
  55. )
  56. embassy = self.free_config.get('center', {})
  57. if not embassy:
  58. raise NotFoundError(message="center not found in free config")
  59. # 2. 解决 Cloudflare 5s 盾
  60. self._solve_cloudflare5S_challenge()
  61. # 3. 获取登录页面参数 (OIDC)
  62. login_page = "https://visas-fr.tlscontact.com/en-us/login"
  63. params = {
  64. "issuerId": embassy["code"],
  65. "country": embassy["country"],
  66. "vac": embassy["code"],
  67. "redirect": f"/en-us/country/{embassy['country']}/vac/{embassy['code']}"
  68. }
  69. headers = {
  70. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  71. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
  72. 'Referer': f'https://visas-fr.tlscontact.com/en-us/country/{embassy["country"]}/vac/{embassy["code"]}',
  73. 'User-Agent': self.user_agent,
  74. }
  75. resp = self._perform_request("GET", login_page, headers=headers, params=params)
  76. if self.config.debug:
  77. self._save_debug_html(resp.text, prefix='Tls_Login_Page')
  78. # 解析 Keycloak 登录地址
  79. soup = BeautifulSoup(resp.text, 'html.parser')
  80. form = soup.find('form')
  81. if not form:
  82. raise NotFoundError(message="Login form not found")
  83. action = form.get('action')
  84. authenticate_url = action if action.startswith('http') else urljoin(resp.url, action)
  85. # 4. 解决 ReCaptcha V2 (登录验证码)
  86. api_token = self.free_config.get("capsolver_key", "")
  87. if not api_token:
  88. raise NotFoundError(message="Missing 'capsolver_key' in free_config, captcha might fail.")
  89. rc_params = {
  90. "type": "ReCaptchaV2TaskProxyLess",
  91. "page": authenticate_url,
  92. "siteKey": "6LcDpXcfAAAAAM7wOEsF_38DNsL20tTvPTKxpyn0",
  93. "apiToken": api_token,
  94. "proxy": self._get_proxy_url()
  95. }
  96. g_token = self._solve_recaptcha(rc_params)
  97. # 5. 提交登录
  98. payload = {
  99. 'username': self.config.account.username,
  100. 'password': self.config.account.password,
  101. 'g-recaptcha-response': g_token
  102. }
  103. headers['Content-Type'] = 'application/x-www-form-urlencoded'
  104. resp = self._perform_request("POST", authenticate_url, headers=headers, data=payload)
  105. if self.config.debug:
  106. self._save_debug_html(resp.text, prefix='Tls_Travel_Groups_Page')
  107. # 6. 解析 Travel Groups
  108. self._check_page_is_session_expired_or_invalid("My travel group", resp.text)
  109. groups = self._parse_travel_groups(resp.text)
  110. # 选择匹配城市的 Group
  111. target_city = embassy['city'].lower()
  112. for g in groups:
  113. if g['location'].lower() == target_city:
  114. self.travel_group = g
  115. break
  116. if not self.travel_group:
  117. raise NotFoundError(message=f"No matched group found for city {target_city}")
  118. VSC_INFO("tls_plg", "[%s] Session created. Group: %s", self.group_id, self.travel_group['group_number'])
  119. def query(self) -> VSQueryResult:
  120. res = VSQueryResult()
  121. res.success = False
  122. embassy = self.free_config.get('center', {})
  123. group_num = self.travel_group['group_number']
  124. interest_month = self.free_config.get("interest_month", time.strftime("%m-%Y"))
  125. max_retries = self.free_config.get("max_retries", 2)
  126. url = f'https://visas-fr.tlscontact.com/en-us/{group_num}/workflow/appointment-booking'
  127. params = {
  128. 'location': embassy["code"],
  129. 'month': interest_month,
  130. }
  131. headers = {
  132. 'accept': '*/*',
  133. 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
  134. 'referer': f'{url}?location={embassy["code"]}',
  135. 'user-agent': self.user_agent,
  136. }
  137. for attempt in range(1, max_retries + 1):
  138. try:
  139. resp = self._perform_request("GET", url, headers=headers, params=params)
  140. if self.config.debug:
  141. self._save_debug_html(resp.text, prefix='Tls_Query_Slot_Page')
  142. break # ✅ 请求成功,跳出重试循环
  143. except PermissionDeniedError:
  144. VSC_WARN(
  145. "tls_plg",
  146. "[TLS] Query Appointment-booking blocked (403), attempt %d/%d",
  147. attempt, max_retries
  148. )
  149. # 最后一次就不再绕盾了
  150. if attempt >= max_retries:
  151. raise PermissionDeniedError()
  152. self._solve_cloudflare5S_challenge()
  153. VSC_INFO("tls_plg", "[TLS] Cloudflare bypass success, retrying...")
  154. continue
  155. self._check_page_is_session_expired_or_invalid('Book your appointment', resp.text)
  156. # 3. 解析 Slots
  157. all_slots = self._parse_appointment_slots(resp.text)
  158. target_labels = self.free_config.get("target_labels", ["", "pta"])
  159. available = [s for s in all_slots if s.get("label") in target_labels]
  160. res.city = self.free_config.get('city', '')
  161. res.country = self.free_config.get('country', '')
  162. res.visa_type = self.free_config.get('visa_type', '')
  163. res.routing_key = self.free_config.get('routing_key', '')
  164. if available:
  165. res.success = True
  166. res.availability_status = AvailabilityStatus.Available
  167. res.earliest_date = available[0]['date']
  168. date_map = {}
  169. for s in available:
  170. d = s['date']
  171. date_map.setdefault(d, [])
  172. ts = VSQueryResult.DateAvailability.TimeSlot()
  173. ts.time = s['time']
  174. ts.label = f"{s['type']}"
  175. date_map[d].append(ts)
  176. for d, slots in date_map.items():
  177. da = VSQueryResult.DateAvailability()
  178. da.date = d
  179. da.times = slots
  180. res.availability.append(da)
  181. else:
  182. res.success = False
  183. res.availability_status = AvailabilityStatus.NoneAvailable
  184. return res
  185. def book(self, slot_info: VSQueryResult, user_input: Dict = None) -> VSBookResult:
  186. res = VSBookResult()
  187. res.success = False
  188. target_date = slot_info.availability[0].date
  189. target_time = slot_info.availability[0].times[0].time
  190. target_label = ""
  191. embassy = self.free_config.get('center', {})
  192. group_num = self.travel_group['group_number']
  193. interest_month = self.free_config.get("interest_month", time.strftime("%m-%Y"))
  194. # 1. 解决 ReCaptcha V3
  195. page_url = f'https://visas-fr.tlscontact.com/en-us/{group_num}/workflow/appointment-booking?location={embassy["code"]}&month={interest_month}'
  196. api_token = self.free_config.get("capsolver_key", "")
  197. rc_params = {
  198. "type": "ReCaptchaV3Task",
  199. "page": page_url,
  200. "action": "book",
  201. "siteKey": "6LcTpXcfAAAAAM3VojNhyV-F1z92ADJIvcSZ39Y9",
  202. "apiToken": api_token,
  203. "proxy": self._get_proxy_url()
  204. }
  205. g_token = self._solve_recaptcha(rc_params)
  206. # 2. 构造请求
  207. url = f'https://visas-fr.tlscontact.com/en-us/{group_num}/workflow/appointment-booking'
  208. next_action = '601f284bf7ee33b6578ad0fad426fae18c232707f2'
  209. next_state = '%5B%22%22%2C%7B%22children%22%3A%5B%5B%22lang%22%2C%22en-us%22%2C%22d%22%5D%2C%7B%22children%22%3A%5B%5B%22groupId%22%2C%22$GROUPID$%22%2C%22d%22%5D%2C%7B%22children%22%3A%5B%22workflow%22%2C%7B%22children%22%3A%5B%22appointment-booking%22%2C%7B%22children%22%3A%5B%22__PAGE__%22%2C%7B%7D%2Cnull%2Cnull%5D%7D%2Cnull%2Cnull%2Ctrue%5D%7D%2Cnull%2Cnull%5D%7D%2Cnull%2Cnull%5D%7D%2Cnull%2Cnull%2Ctrue%5D%7D%2Cnull%2Cnull%5D'
  210. headers = {
  211. 'Next-Action': next_action,
  212. 'Referer': page_url,
  213. 'Next-Router-State-Tree': next_state.replace("$GROUPID$", group_num),
  214. 'Accept': 'text/x-component',
  215. 'User-Agent': self.user_agent,
  216. }
  217. params = {
  218. 'location': embassy["code"],
  219. 'month': interest_month,
  220. }
  221. boundary = "----WebKitFormBoundary" + "".join(
  222. random.choices("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", k=16)
  223. )
  224. headers["Content-Type"] = f"multipart/form-data; boundary={boundary}"
  225. form_fields = {
  226. '1_formGroupId': str(group_num),
  227. '1_lang': 'en-us',
  228. '1_process': 'APPOINTMENT',
  229. '1_location': embassy["code"],
  230. '1_date': target_date,
  231. '1_time': target_time,
  232. '1_appointmentLabel': target_label,
  233. '1_captcha_token': g_token,
  234. '0': '[{"status":"IDLE"},"$K1"]'
  235. }
  236. body_parts = []
  237. for name, value in form_fields.items():
  238. body_parts.append(f"--{boundary}\r\n")
  239. body_parts.append(f'Content-Disposition: form-data; name="{name}"\r\n')
  240. body_parts.append("\r\n")
  241. body_parts.append(f"{value}\r\n")
  242. body_parts.append(f"--{boundary}--\r\n")
  243. body = "".join(body_parts).encode("utf-8")
  244. resp = self.session.post(url, params=params, headers=headers, data=body, allow_redirects=False)
  245. if self.config.debug:
  246. self._save_debug_html(resp.text, prefix='Tls_Book_Appointment_Page')
  247. if resp.status_code == 303:
  248. res.success = True
  249. res.book_date = target_date
  250. res.book_time = target_time
  251. return res
  252. else:
  253. VSC_WARN('tls_plg', 'Expected Status is 303, but got {resp.status_code}')
  254. res.success = False
  255. return res
  256. def _save_debug_html(self, content: str, prefix: str = "debug"):
  257. save_dir = "debug_pages"
  258. if not os.path.exists(save_dir):
  259. os.makedirs(save_dir)
  260. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  261. filename = f"{save_dir}/{prefix}_{timestamp}.html"
  262. with open(filename, "w", encoding="utf-8") as f:
  263. f.write(content)
  264. VSC_INFO("tls_plg", "[%s] HTML saved to: %s", self.group_id, filename)
  265. def _get_proxy_url(self):
  266. # 构造代理
  267. proxy_url = ""
  268. if self.config.proxy.ip:
  269. s = self.config.proxy
  270. if s.username:
  271. proxy_url = f"{s.scheme}://{s.username}:{s.password}@{s.ip}:{s.port}"
  272. else:
  273. proxy_url = f"{s.scheme}://{s.ip}:{s.port}"
  274. return proxy_url
  275. def _perform_request(self, method, url, headers=None, data=None, json_data=None, params=None):
  276. """
  277. 统一 HTTP 请求封装,严格复刻 C++ 逻辑:
  278. 1. 发送 OPTIONS 请求
  279. 2. 发送实际请求
  280. """
  281. resp = self.session.request(method, url, headers=headers, data=data, json=json_data, params=params, timeout=30)
  282. if self.config.debug:
  283. VSC_INFO('tls_plg', f'[perform request] Response={resp.text}\nMethod={method}, Url={url}, Data={data}, JsonData={json_data}, Params={params}')
  284. if resp.status_code == 200:
  285. return resp
  286. elif resp.status_code == 401:
  287. self.is_healthy = False
  288. raise SessionExpiredOrInvalidError()
  289. elif resp.status_code == 403:
  290. raise PermissionDeniedError()
  291. elif resp.status_code == 429:
  292. self.is_healthy = False
  293. raise RateLimiteddError()
  294. else:
  295. raise BizLogicError(message=f"HTTP Error {resp.status_code}: {resp.text[:100]}")
  296. def _solve_cloudflare5S_challenge(self):
  297. """
  298. 解决 Cloudflare 5s 盾
  299. """
  300. VSC_INFO("tls_plg", f"[{self.group_id}] Solving Cloudflare 5s...")
  301. embassy = self.free_config.get('center', {})
  302. website_url = f'https://visas-fr.tlscontact.com/en-us/country/{embassy["country"]}'
  303. # 1. 格式化代理字符串, 这里的接口要求格式通常是: host:port:user:pass (根据你的脚本示例)
  304. p = self.config.proxy
  305. if p.username:
  306. proxy_str = f"{p.ip}:{p.port}:{p.username}:{p.password}"
  307. else:
  308. proxy_str = f"{p.ip}:{p.port}"
  309. # 2. 提交任务
  310. task = VSCloudApi.Instance().submit_anticloudflare_task(proxy_str, website_url)
  311. # 3. 等待结果
  312. task_id = str(task['id'])
  313. result = VSCloudApi.Instance().get_anticloudflare_result(task_id)
  314. parsed = json.loads(result.get('result', '{}'))
  315. cookies_list = parsed.get('cookies', [])
  316. for cookie in cookies_list:
  317. if cookie['name'] in ['__cf_bm', 'cf_clearance']:
  318. self.session.cookies.set(
  319. cookie['name'],
  320. cookie['value'],
  321. domain=cookie['domain'],
  322. path='/'
  323. )
  324. ua = parsed.get('userAgent')
  325. if ua:
  326. self.user_agent = ua
  327. self.session.headers['User-Agent'] = ua
  328. VSC_INFO("tls_plg", "[%s] Cloudflare 5s challenge solved.", self.group_id)
  329. def _solve_recaptcha(self, params) -> str:
  330. """
  331. 调用 Capsolver
  332. """
  333. key = params.get("apiToken")
  334. if not key:
  335. raise NotFoundError(message="Api-token is required for recaptcha solver")
  336. submit_url = "https://api.capsolver.com/createTask"
  337. task = {
  338. "type": params.get("type"),
  339. "websiteURL": params.get("page"),
  340. "websiteKey": params.get("siteKey"),
  341. }
  342. if params.get("action"):
  343. task["pageAction"] = params.get("action")
  344. if params.get("proxy"):
  345. p = urlparse(params.get("proxy"))
  346. task["proxyType"] = p.scheme
  347. task["proxyAddress"] = p.hostname
  348. task["proxyPort"] = p.port
  349. if p.username:
  350. task["proxyLogin"] = p.username
  351. task["proxyPassword"] = p.password
  352. payload = {"clientKey": key, "task": task}
  353. r = requests.post(submit_url, json=payload, timeout=20)
  354. if r.status_code != 200:
  355. raise BizLogicError(message="Failed to submit capsolver task")
  356. task_id = r.json().get("taskId")
  357. for _ in range(20):
  358. r = requests.post("https://api.capsolver.com/getTaskResult", json={"clientKey": key, "taskId": task_id}, timeout=20)
  359. if r.status_code == 200:
  360. d = r.json()
  361. if d.get("status") == "ready":
  362. return d["solution"]["gRecaptchaResponse"]
  363. time.sleep(3)
  364. raise BizLogicError(message="Capsolver task timeout")
  365. def _parse_travel_groups(self, html: str) -> List[Dict]:
  366. groups = []
  367. js_pattern = r'\\"travelGroups\\":\s*(\[.*?\]),\\"availableCountriesToCreateGroups'
  368. js_match = re.search(js_pattern, html, re.DOTALL)
  369. if js_match:
  370. json_str = js_match.group(1).replace(r'\"', '"')
  371. data = json.loads(json_str)
  372. for g in data:
  373. groups.append({
  374. 'group_name': g.get('groupName'),
  375. 'group_number': g.get('formGroupId'),
  376. 'location': g.get('vacName')
  377. })
  378. else:
  379. VSC_WARN('tls_plg', 'Parsed travel group page, but not found travelGroups')
  380. return groups
  381. def _parse_appointment_slots(self, html: str) -> List[Dict]:
  382. slots = []
  383. pattern = r'"availableAppointments\\":\s*(\[.*\]),\\"showFlexiAppointment'
  384. match = re.search(pattern, html, re.DOTALL)
  385. if match:
  386. json_str = match.group(1).replace(r'\"', '"')
  387. data = json.loads(json_str)
  388. for day in data:
  389. d_str = day.get('day')
  390. for s in day.get('slots', []):
  391. labels = s.get('labels', [])
  392. lbl = ""
  393. stype = ""
  394. cost = ""
  395. if 'pta' in labels:
  396. lbl = 'pta'
  397. stype = "Prime"
  398. elif 'ptaw' in labels:
  399. lbl = 'ptaw'
  400. stype = "Prime Weekend"
  401. elif '' in labels:
  402. lbl = ''
  403. stype = "Standard"
  404. if lbl or not labels:
  405. slots.append({
  406. 'date': d_str,
  407. 'time': s.get('time'),
  408. 'label': lbl,
  409. 'type': stype,
  410. 'cost': cost
  411. })
  412. return slots
  413. else:
  414. VSC_WARN('tls_plg', 'Parsed appointment slot page, but not found availableAppointments')
  415. return slots
  416. def _check_page_is_session_expired_or_invalid(self, keyword, html: str) -> bool:
  417. if not html:
  418. self.is_healthy = False
  419. raise SessionExpiredOrInvalidError()
  420. if keyword not in html:
  421. if 'redirected automatically' in html.lower():
  422. self.is_healthy = False
  423. raise SessionExpiredOrInvalidError()
  424. if 'login' in html.lower() and 'password' in html.lower():
  425. self.is_healthy = False
  426. raise SessionExpiredOrInvalidError()
  427. if 'session expired!' in html.lower() and 'for security reasons, your session has expired. please log in again to continue.' in html.lower() and 'you will be redirected automatically in 10 seconds.' in html.lower():
  428. self.is_healthy = False
  429. raise SessionExpiredOrInvalidError()
  430. if 'temporarily blocked!' in html.lower() and 'Your session has been temporarily suspended due to the high number of your access to this page.' in html.lower() and 'You can try to access your account again in 2 hours.' in html.lower():
  431. self.is_healthy = False
  432. raise SessionExpiredOrInvalidError()