de_plugin.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. import time
  2. import json
  3. import random
  4. import re
  5. import os
  6. import uuid
  7. import base64
  8. from datetime import datetime
  9. from typing import List, Dict, Optional, Any, Callable
  10. from DrissionPage import ChromiumPage
  11. from vs_plg import IVSPlg
  12. from vs_types import (
  13. VSPlgConfig,
  14. AppointmentType,
  15. VSQueryResult,
  16. VSBookResult,
  17. AvailabilityStatus,
  18. TimeSlot,
  19. DateAvailability,
  20. NotFoundError,
  21. PermissionDeniedError,
  22. RateLimiteddError,
  23. SessionExpiredOrInvalidError,
  24. BizLogicError,
  25. PluginStatus,
  26. utc_now_str
  27. )
  28. from toolkit.vs_cloud_api import VSCloudApi
  29. from utils.cloudflare_bypass_for_scraping import CloudflareBypasser
  30. from toolkit.ocr_engine import DddOcrEngine
  31. from utils.plugin_utils import (
  32. BrowserResponse,
  33. init_chromium_options,
  34. perform_browser_fetch,
  35. filter_dates_in_range,
  36. cleanup_browser_workspace
  37. )
  38. def get_alias_email(email: str, new_domain: str = "text.skin") -> str:
  39. if "@" not in email:
  40. raise ValueError(f"Invalid email: {email}")
  41. local_part, _ = email.rsplit("@", 1)
  42. return f"{local_part}@{new_domain}"
  43. class DePlugin(IVSPlg):
  44. """
  45. Germany (Visametric) 签证预约插件 (Browser + Tunnel Mode)
  46. """
  47. name = "de_plugin"
  48. version = "1.0.0"
  49. description = "德国Visametric签证预约插件 (支持Cloudflare穿透与OCR验证)"
  50. def __init__(self, group_id: str):
  51. self.group_id = group_id
  52. self.config: Optional[VSPlgConfig] = None
  53. self.free_config: Dict[str, Any] = {}
  54. self.logger = None
  55. # 浏览器实例
  56. self.page: Optional[ChromiumPage] = None
  57. # 资源隔离
  58. self.instance_id = uuid.uuid4().hex[:8]
  59. self.root_workspace = os.path.abspath(os.path.join("data/temp_browser_data", f"{self.group_id}.{self.instance_id}"))
  60. self.user_data_path = os.path.join(self.root_workspace, "user_data")
  61. if not os.path.exists(self.root_workspace):
  62. os.makedirs(self.root_workspace)
  63. self.tunnel = None # 代理隧道
  64. self.is_healthy = True
  65. self.session_create_time: float = 0
  66. # 字符识别引擎
  67. self.ocr_engine: Optional[DddOcrEngine] = None
  68. # 业务状态
  69. self.base_url = "https://ie-appointment.visametric.com"
  70. self.csrf_token = ""
  71. self.personal_info_val = ""
  72. self.email_val_control = ""
  73. def set_log(self, logger: Callable[[str], None]) -> None:
  74. self.logger = logger
  75. def _log(self, message):
  76. if self.logger:
  77. self.logger(f'[DePlugin] [{self.group_id}] [{self.instance_id}] {message}')
  78. else:
  79. print(f'[DePlugin] [{self.group_id}] [{self.instance_id}] {message}')
  80. def set_config(self, config: VSPlgConfig):
  81. self.config = config
  82. self.free_config = config.free_config or {}
  83. if self.free_config.get("base_url"):
  84. self.base_url = self.free_config["base_url"].rstrip('/')
  85. def keep_alive(self):
  86. pass
  87. def health_check(self) -> bool:
  88. if not self.is_healthy or not self.page:
  89. return False
  90. try:
  91. if not self.page.run_js("return 1;"):
  92. return False
  93. except Exception:
  94. return False
  95. if self.config.session_max_life > 0:
  96. if time.time() - self.session_create_time > self.config.session_max_life:
  97. self._log("Session expired.")
  98. return False
  99. return True
  100. def create_session(self):
  101. """
  102. 创建会话:启动浏览器 -> 代理隧道 -> 过盾 -> 提取 Captcha -> 本地识别 -> 提交 -> 获取 Context
  103. """
  104. self._log(f"Initializing Session (ID: {self.instance_id})...")
  105. self.ocr_engine = DddOcrEngine()
  106. co, self.tunnel = init_chromium_options(
  107. user_data_path=self.user_data_path,
  108. username_seed=self.config.account.username if self.config and self.config.account else None,
  109. proxy=self.config.proxy if self.config else None,
  110. headless=False,
  111. window_size="1920,1080",
  112. logger=self._log
  113. )
  114. co.set_argument('--ignore-certificate-errors')
  115. try:
  116. self.page = ChromiumPage(co)
  117. # 1. 访问首页
  118. url_home = f"{self.base_url}/en"
  119. self._log(f"Navigating to {url_home}")
  120. self.page.get(url_home)
  121. # 2. Cloudflare 过盾
  122. cf = CloudflareBypasser(self.page, log=self.config.debug if self.config else False)
  123. if not cf.bypass(max_retry=15):
  124. if "access denied" in self.page.title.lower():
  125. raise BizLogicError("Cloudflare Access Denied")
  126. raise BizLogicError("Cloudflare bypass timeout")
  127. # 3. 提取 CSRF 和 验证码
  128. meta_ele = self.page.ele('xpath://meta[@name="csrf-token"]', timeout=30)
  129. if not meta_ele:
  130. self.page.get_screenshot(path='csrf_not_found.jpg')
  131. raise NotFoundError("CSRF Token meta tag not found (Page load failed?)")
  132. self.csrf_token = meta_ele.attr('content')
  133. # 提取验证码图片 (Visametric Base64)
  134. html = self.page.html
  135. match = re.search(r'"data:image/png;base64,"\s*\+\s*"(.*?)"', html)
  136. if not match:
  137. try:
  138. img_ele = self.page.ele('xpath://img[contains(@src, "data:image")]')
  139. if img_ele:
  140. b64_src = img_ele.attr('src')
  141. captcha_b64 = b64_src.split(',')[1]
  142. else:
  143. raise NotFoundError("Captcha image not found")
  144. except Exception:
  145. raise NotFoundError("Captcha image not found (Regex failed)")
  146. else:
  147. captcha_b64 = match.group(1)
  148. image_bytes = base64.b64decode(captcha_b64)
  149. # 4. 识别验证码 (本地 OCR 服务)
  150. captcha_code = self.ocr_engine.inference_captcha(image_bytes)
  151. # 5. 提交验证码 (获取 PersonalInfo)
  152. self._submit_captcha(captcha_code)
  153. self.session_create_time = time.time()
  154. self._log("Session created successfully.")
  155. except Exception as e:
  156. self._log(f"Session Create Failed: {e}")
  157. self.cleanup()
  158. raise e
  159. def _submit_captcha(self, code: str):
  160. """
  161. 提交验证码,获取 personalinfo 和 emailValControl
  162. """
  163. url = f"{self.base_url}/en/appointment-form"
  164. payload = {
  165. '_token': self.csrf_token,
  166. 'cpJvnsControl': '',
  167. 'mailConfirmCode': code
  168. }
  169. resp = self._perform_request('POST', url, data=payload, headers={
  170. 'X-Requested-With': 'XMLHttpRequest'
  171. })
  172. html = resp.text
  173. match_pi = re.search(r"personalinfo:\s*'([^']*)'", html)
  174. if match_pi:
  175. self.personal_info_val = match_pi.group(1)
  176. match_ev = re.search(r"emailValControl:\s*'([^']*)'", html)
  177. if match_ev:
  178. self.email_val_control = match_ev.group(1)
  179. if not self.personal_info_val:
  180. raise NotFoundError(message="Personalinfo not found in captcha response")
  181. m = re.search(r'name="csrf-token" content="([^"]+)"', html)
  182. if m:
  183. self.csrf_token = m.group(1)
  184. def query(self, apt_type: AppointmentType) -> VSQueryResult:
  185. res = VSQueryResult()
  186. res.success = False
  187. consular_id = self.free_config.get("consularid", "1")
  188. url = f"{self.base_url}/en/getdate"
  189. payload = {
  190. "consularid": consular_id,
  191. "exitid": "1",
  192. "servicetypeid": "1",
  193. "calendarType": "2",
  194. "totalperson": "1"
  195. }
  196. headers = {
  197. 'X-CSRF-TOKEN': self.csrf_token,
  198. 'X-Requested-With': 'XMLHttpRequest'
  199. }
  200. try:
  201. resp = self._perform_request('POST', url, data=payload, headers=headers, retry_count=1)
  202. except Exception as e:
  203. self._log(f"Query Error: {e}")
  204. raise e
  205. j = resp.json()
  206. dates = j.get("getDateEnable", [])
  207. if dates:
  208. res.success = True
  209. res.availability_status = AvailabilityStatus.Available
  210. earliest_date = dates[0]
  211. earliest_dt = datetime.strptime(earliest_date, "%d-%m-%Y")
  212. res.earliest_date = earliest_dt
  213. res.availability = [
  214. DateAvailability(date=datetime.strptime(d, "%d-%m-%Y"), times=[])
  215. for d in dates
  216. ]
  217. else:
  218. res.availability_status = AvailabilityStatus.NoneAvailable
  219. return res
  220. def book(self, slot_info: VSQueryResult, user_inputs: Dict = None) -> VSBookResult:
  221. res = VSBookResult()
  222. user_inputs = user_inputs or {}
  223. exp_start = user_inputs.get('expected_start_date', '')
  224. exp_end = user_inputs.get('expected_end_date', '')
  225. available_dates_str = [
  226. da.date.strftime("%Y-%m-%d")
  227. for da in slot_info.availability
  228. ]
  229. valid_dates = filter_dates_in_range(available_dates_str, exp_start, exp_end, shuffle=True)
  230. if not valid_dates:
  231. raise NotFoundError("No dates match constraints")
  232. target_date = random.choice(valid_dates)
  233. self._log(f"Selected date: {target_date}")
  234. # 1. 获取时间 Slot
  235. time_slot = self._get_slot_time(target_date)
  236. # 2. 发送邮件流程
  237. alias_email = get_alias_email(user_inputs.get("email", ""), new_domain='text.skin')
  238. self._send_email_step1(alias_email)
  239. self._send_email_step2("0")
  240. # 3. 读取 OTP
  241. otp_code = self._read_otp_email(alias_email)
  242. # 4. 提交确认
  243. book_res_html = self._confirm_appointment(target_date, time_slot, user_inputs, otp_code, alias_email)
  244. if "complete all required fields" in book_res_html.lower():
  245. raise BizLogicError("Incomplete fields response")
  246. match = re.search(r'https:\/\/checkout\.stripe\.com\/c\/pay\/[^\s"]+', book_res_html)
  247. res.success = True
  248. res.book_date = target_date
  249. res.book_time = time_slot['time']
  250. if match:
  251. res.payment_link = match.group(0)
  252. self._log(f"Payment Link: {res.payment_link}")
  253. return res
  254. def _perform_request(self, method, url, headers=None, data=None, json_data=None, params=None, retry_count=0):
  255. """在浏览器上下文中注入执行 Fetch"""
  256. if not self.page:
  257. raise BizLogicError("Browser not initialized")
  258. resp = perform_browser_fetch(
  259. self.page,
  260. method=method,
  261. url=url,
  262. headers=headers,
  263. data=data,
  264. json_data=json_data,
  265. params=params
  266. )
  267. if resp.status_code == 200:
  268. return resp
  269. elif resp.status_code == 403:
  270. if "Just a moment" in resp.text and retry_count < 2:
  271. self._log("Cloudflare 403. Refreshing...")
  272. if self._refresh_firewall_session():
  273. return self._perform_request(method, url, headers, data, json_data, params, retry_count+1)
  274. raise PermissionDeniedError(f"HTTP 403: {resp.text[:100]}")
  275. elif resp.status_code == 429:
  276. self.is_healthy = False
  277. raise RateLimiteddError()
  278. elif resp.status_code in (401, 419):
  279. self.is_healthy = False
  280. raise SessionExpiredOrInvalidError()
  281. else:
  282. raise BizLogicError(f"HTTP {resp.status_code}: {resp.text[:100]}")
  283. def _refresh_firewall_session(self) -> bool:
  284. try:
  285. self.page.refresh()
  286. cf = CloudflareBypasser(self.page, log=self.config.debug if self.config else False)
  287. return cf.bypass(max_retry=10)
  288. except Exception:
  289. return False
  290. def _get_slot_time(self, date: str) -> Dict:
  291. url = f"{self.base_url}/en/senddate"
  292. dt_m = datetime.strptime(date, "%Y-%m-%d")
  293. converted_date = dt_m.strftime("%d-%m-%Y")
  294. payload = {
  295. "fulldate": converted_date,
  296. "totalperson": "1",
  297. "set_new_consular_id": self.free_config.get("consularid", "1"),
  298. "set_new_exit_office_id": "1",
  299. "calendarType": "2",
  300. "set_new_service_type_id": "1",
  301. "personalinfo": self.personal_info_val
  302. }
  303. headers = {'X-CSRF-TOKEN': self.csrf_token, 'X-Requested-With': 'XMLHttpRequest'}
  304. resp = self._perform_request('POST', url, data=payload, headers=headers)
  305. times = []
  306. for m in re.finditer(r'data-id="([^"]+)"[^>]*data-all="([^"]+)"[^>]*>.*?<i>(.*?)</i>', resp.text, re.DOTALL):
  307. times.append({'data_id': m.group(1), 'data_all': m.group(2), 'time': m.group(3).strip()})
  308. if not times:
  309. raise NotFoundError("No time slots")
  310. return random.choice(times)
  311. def _send_email_step1(self, email: str):
  312. url = f"{self.base_url}/en/jky45fgd"
  313. payload = {"emailCheck": email, "personalinfo": self.personal_info_val}
  314. headers = {'X-CSRF-TOKEN': self.csrf_token, 'X-Requested-With': 'XMLHttpRequest'}
  315. self._perform_request('POST', url, data=payload, headers=headers)
  316. def _send_email_step2(self, code_val: str):
  317. url = f"{self.base_url}/en/confirmCodeSendMail"
  318. payload = {"confirmCode": code_val, "emailValControl": self.email_val_control}
  319. headers = {'X-CSRF-TOKEN': self.csrf_token, 'X-Requested-With': 'XMLHttpRequest'}
  320. self._perform_request('POST', url, data=payload, headers=headers)
  321. def _read_otp_email(self, recipient: str) -> str:
  322. master_email = "hujiarui8@gmail.com"
  323. sender = 'Visametric - verify at visametric.com'
  324. now_utc = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
  325. for _ in range(12):
  326. c = VSCloudApi.Instance().fetch_mail_content(master_email, sender, recipient, 'Verification Code', 'Verification code', now_utc, 300)
  327. if c:
  328. m = re.search(r'\b\d{6}\b', c)
  329. if m:
  330. return m.group(0)
  331. time.sleep(5)
  332. raise NotFoundError("OTP timeout")
  333. def _confirm_appointment(self, date, slot_data, user_inputs, otp, alias_email):
  334. url = f"{self.base_url}/en/personal/appointment/create"
  335. def _get_dob(d):
  336. try:
  337. return datetime.strptime(d[:10], "%Y-%m-%d")
  338. except Exception:
  339. return datetime.now()
  340. dob = _get_dob(user_inputs.get('birthday', ''))
  341. payload = {
  342. "_token": self.csrf_token,
  343. "country": str(self.free_config.get("consularid", "1")),
  344. "visitingcountry": str(self.free_config.get("consularid", "1")),
  345. "city": "6",
  346. "office": "1",
  347. "officetype": "1",
  348. "totalPerson": "1",
  349. "name1": user_inputs.get('first_name', '').upper(),
  350. "surname1": user_inputs.get('last_name', '').upper(),
  351. "nationality1": "2",
  352. "birthday1": str(dob.day),
  353. "birthmonth1": str(dob.month),
  354. "birthyear1": str(dob.year),
  355. "passport1": user_inputs.get('passport_no'),
  356. "passportExpirationDate1": datetime.strptime(user_inputs.get('passport_expiry_date', '')[:10], "%Y-%m-%d").strftime("%d-%m-%Y"),
  357. "email1": alias_email,
  358. "phone1": user_inputs.get('phone_no'),
  359. "alternativephone1": "",
  360. "mailConfirmCode": otp,
  361. "ctval": slot_data['data_id'],
  362. "qtallvert": slot_data['data_all'],
  363. "oldofficetype": "1",
  364. "oldtotalperson": "1",
  365. "rePaymentControl": "0",
  366. "view_set_app_country": "Schengen - Tourism/Family&Friend Visit/Transit Visa/Other Purposes",
  367. "view_set_app_office": "Dublin",
  368. "view_set_app_service_type": "NORMAL",
  369. "cargoactive": "0",
  370. "setnewcalendarstatus": "2",
  371. "availableDaycontrol": "0",
  372. "travelStartDate": datetime.strptime(user_inputs.get('travel_date', '')[:10], "%Y-%m-%d").strftime("%d-%m-%Y"),
  373. "personalapproveTerms": "1"
  374. }
  375. for i in range(2, 5):
  376. payload.update({
  377. f"name{i}": "", f"surname{i}": "", f"nationality{i}": "0", f"birthday{i}": "0", f"birthmonth{i}": "0", f"birthyear{i}": "0", f"passport{i}": "", f"passportExpirationDate{i}": "", f"email{i}": alias_email, f"phone{i}": user_inputs.get('phone_no'), f"alternativephone{i}": ""
  378. })
  379. headers = {'X-Requested-With': 'XMLHttpRequest'}
  380. return self._perform_request('POST', url, data=payload, headers=headers).text
  381. def cleanup(self):
  382. """销毁浏览器并彻底删除临时文件"""
  383. cleanup_browser_workspace(self.page, self.root_workspace, self.tunnel, self._log)
  384. self.page = None
  385. self.tunnel = None
  386. def get_plugin_status(self) -> PluginStatus:
  387. account_name = self.config.account.username if self.config and self.config.account else ""
  388. proxy_info = f"{self.config.proxy.ip}:{self.config.proxy.port}" if self.config and self.config.proxy and self.config.proxy.ip else ""
  389. current_url = ""
  390. if self.page:
  391. try:
  392. current_url = self.page.url
  393. except Exception:
  394. pass
  395. session_age = int(time.time() - self.session_create_time) if getattr(self, "session_create_time", 0) > 0 else 0
  396. state = "running" if getattr(self, "is_healthy", True) else "unhealthy"
  397. return PluginStatus(
  398. instance_id=self.instance_id,
  399. is_healthy=self.is_healthy,
  400. state=state,
  401. current_url=current_url,
  402. account=account_name,
  403. proxy=proxy_info,
  404. session_age_sec=session_age,
  405. last_active_at=utc_now_str(),
  406. )
  407. def __del__(self):
  408. """
  409. 析构函数:当对象被垃圾回收时自动调用
  410. """
  411. self.cleanup()