bel_plugin.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. import time
  2. import json
  3. import random
  4. import re
  5. import os
  6. import uuid
  7. import shutil
  8. import base64
  9. import socket
  10. import requests
  11. from datetime import datetime
  12. from typing import List, Dict, Optional, Any, Callable
  13. from urllib.parse import urljoin, urlparse, urlencode
  14. # DrissionPage 核心
  15. from DrissionPage import ChromiumPage, ChromiumOptions
  16. from vs_plg import IVSPlg
  17. from vs_types import VSPlgConfig, AppointmentType, VSQueryResult, VSBookResult, AvailabilityStatus, TimeSlot, DateAvailability, NotFoundError, PermissionDeniedError, RateLimiteddError, SessionExpiredOrInvalidError, BizLogicError, PluginStatus, utc_now_str
  18. from toolkit.vs_cloud_api import VSCloudApi
  19. from toolkit.proxy_tunnel import ProxyTunnel
  20. class BrowserResponse:
  21. def __init__(self, result_dict):
  22. result_dict = result_dict or {}
  23. self.status_code = result_dict.get('status', 0)
  24. self.text = result_dict.get('body', '')
  25. self.headers = result_dict.get('headers', {})
  26. self.url = result_dict.get('url', '')
  27. self._json = None
  28. def json(self):
  29. if self._json is None:
  30. if not self.text: return {}
  31. try: self._json = json.loads(self.text)
  32. except: self._json = {}
  33. return self._json
  34. def to_yyyymmdd(data_str: str, date_str_format: str, target_format: str="%Y-%m-%d"):
  35. dt = datetime.strptime(data_str, date_str_format)
  36. return dt.strftime("%Y-%m-%d")
  37. def get_alias_email(email: str, new_domain: str = "text.skin") -> str:
  38. if "@" not in email: raise ValueError(f"Invalid email: {email}")
  39. local_part, _ = email.rsplit("@", 1)
  40. return f"{local_part}@{new_domain}"
  41. class BelPlugin(IVSPlg):
  42. """
  43. Belgium (https://visaonweb.diplomatie.be/en) 签证预约插件 (Browser + Tunnel Mode)
  44. """
  45. name = "bel_plugin"
  46. version = "1.0.0"
  47. description = "比利时签证预约插件 (Visa On Web / 浏览器与隧道模式)"
  48. def __init__(self, group_id: str):
  49. self.group_id = group_id
  50. self.config: Optional[VSPlgConfig] = None
  51. self.free_config: Dict[str, Any] = {}
  52. self.logger = None
  53. # 浏览器实例
  54. self.page: Optional[ChromiumPage] = None
  55. # 资源隔离
  56. self.instance_id = uuid.uuid4().hex[:8]
  57. self.root_workspace = os.path.abspath(os.path.join("data/temp_browser_data", f"{self.group_id}.{self.instance_id}"))
  58. self.user_data_path = os.path.join(self.root_workspace, "user_data")
  59. if not os.path.exists(self.root_workspace):
  60. os.makedirs(self.root_workspace)
  61. self.applicants = []
  62. self.tunnel = None # 代理隧道
  63. self.is_healthy = True
  64. self.session_create_time: float = 0
  65. def set_log(self, logger: Callable[[str], None]) -> None:
  66. self.logger = logger
  67. def _log(self, message):
  68. if self.logger:
  69. self.logger(f'[BelPlugin] [{self.group_id}] {message}')
  70. else:
  71. print(f'[BelPlugin] [{self.group_id}] {message}')
  72. def set_config(self, config: VSPlgConfig):
  73. self.config = config
  74. self.free_config = config.free_config or {}
  75. def keep_alive(self):
  76. pass
  77. def health_check(self) -> bool:
  78. if not self.is_healthy:
  79. return False
  80. if not self.page:
  81. return False
  82. try:
  83. if not self.page.run_js("return 1;"):
  84. return False
  85. except:
  86. return False
  87. if self.config.session_max_life > 0:
  88. if time.time() - self.session_create_time > self.config.session_max_life:
  89. self._log("Session expired.")
  90. return False
  91. return True
  92. def create_session(self):
  93. """
  94. 创建会话:启动浏览器 -> 代理隧道 -> 提取 Captcha -> 本地识别 -> 提交 -> 获取 Context
  95. """
  96. self._log(f"Initializing Session (ID: {self.instance_id})...")
  97. co = ChromiumOptions()
  98. def get_free_port():
  99. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  100. s.bind(('', 0)); return s.getsockname()[1]
  101. co.set_local_port(get_free_port())
  102. co.set_user_data_path(self.user_data_path)
  103. chrome_path = os.getenv("CHROME_BIN")
  104. if chrome_path and os.path.exists(chrome_path):
  105. co.set_paths(browser_path=chrome_path)
  106. if self.config.proxy and self.config.proxy.ip:
  107. p = self.config.proxy
  108. if p.username and p.password:
  109. self._log(f"Starting Tunnel for {p.ip}...")
  110. self.tunnel = ProxyTunnel(p.ip, p.port, p.username, p.password)
  111. local_proxy = self.tunnel.start()
  112. self._log(f"Tunnel started at {local_proxy}")
  113. co.set_argument(f'--proxy-server={local_proxy}')
  114. else:
  115. proxy_str = f"{p.proto}://{p.ip}:{p.port}"
  116. co.set_argument(f'--proxy-server={proxy_str}')
  117. else:
  118. self._log("[WARN] No proxy configured!")
  119. co.headless(False)
  120. co.set_argument('--no-sandbox')
  121. co.set_argument('--disable-gpu')
  122. co.set_argument('--disable-dev-shm-usage')
  123. co.set_argument('--window-size=1920,1080')
  124. co.set_argument('--disable-blink-features=AutomationControlled')
  125. co.set_argument('--ignore-certificate-errors')
  126. try:
  127. self.page = ChromiumPage(co)
  128. url_home = "https://visaonweb.diplomatie.be/Account/Login?ReturnUrl=%2Fen"
  129. self._log(f"Navigating to {url_home}")
  130. self.page.get(url_home)
  131. self.page.wait.doc_loaded()
  132. self.page.wait.ele_displayed('tag:button@@type=submit', timeout=10)
  133. self.page.ele('tag:input@@name=UserName').input(self.config.account.username)
  134. self.page.ele('tag:input@@name=Password').input(self.config.account.password)
  135. self.page.ele('tag:button@@type=submit').click()
  136. self.page.listen.start('/VisaApplication/MyList')
  137. self.page.get('https://visaonweb.diplomatie.be/en/VisaApplication/IndexByUserId')
  138. packet = self.page.listen.wait(timeout=10)
  139. if not packet:
  140. raise BizLogicError(message='Get userList failed')
  141. self.page.listen.stop()
  142. resp_body = packet.response.body
  143. for dat in resp_body.get('data', []):
  144. if dat.get('St') == 'Submitted':
  145. applicant = {
  146. "id": dat.get('Id'),
  147. "first_name": dat.get('FName'),
  148. "last_name": dat.get('LName'),
  149. "vow_id": dat.get('VOWId'),
  150. "os_id": dat.get('OSId'),
  151. "sub_group_id": dat.get('SubGroupId'),
  152. "vac": dat.get('Vac'),
  153. }
  154. self.applicants.append(applicant)
  155. self.session_create_time = time.time()
  156. self._log("Session created successfully.")
  157. except Exception as e:
  158. self._log(f"Session Create Failed: {e}")
  159. time.sleep(3600)
  160. self.cleanup()
  161. raise e
  162. def query(self, apt_type: AppointmentType) -> VSQueryResult:
  163. res = VSQueryResult()
  164. res.success = False
  165. if not self.applicants:
  166. self._log("No submitted applicants available to query appointment.")
  167. res.availability_status = AvailabilityStatus.NoneAvailable
  168. return res
  169. applicant = random.choice(self.applicants)
  170. applicant_id = applicant.get('id')
  171. url = 'https://visaonweb.diplomatie.be/Common/GetEAppointmentUrl'
  172. params = {
  173. 'id': applicant_id
  174. }
  175. headers = {
  176. "cache-control": "max-age=0",
  177. "if-modified-since": "0",
  178. "x-requested-with": "XMLHttpRequest",
  179. }
  180. resp = self._perform_request('GET', url, headers=headers, params=params)
  181. appointment_url = resp.json().get('url')
  182. if not appointment_url:
  183. self._log("GetEAppointmentUrl returned empty URL.")
  184. res.availability_status = AvailabilityStatus.NoneAvailable
  185. return res
  186. tab = self.page.new_tab(appointment_url)
  187. try:
  188. tab.wait.doc_loaded()
  189. site_key = '5f64399c-14a8-415e-ad1a-7ebccdc4943a'
  190. capsolver_key = self.free_config.get('capsolver_key') or os.getenv('CAPSOLVER_KEY', '')
  191. token = self.get_captcha_solution(capsolver_key, site_key, appointment_url)
  192. inject_script = f"""
  193. (function() {{
  194. var token = "{token}";
  195. var textareas = [
  196. document.querySelector('textarea[name="h-captcha-response"]'),
  197. document.querySelector('textarea[name="g-recaptcha-response"]'),
  198. document.querySelector('[name="h-captcha-response"]'),
  199. document.querySelector('[name="g-recaptcha-response"]')
  200. ];
  201. textareas.forEach(function(el) {{
  202. if (el) {{
  203. el.value = token;
  204. el.innerHTML = token;
  205. el.dispatchEvent(new Event('input', {{ bubbles: true }}));
  206. el.dispatchEvent(new Event('change', {{ bubbles: true }}));
  207. }}
  208. }});
  209. if (typeof window.hcaptcha !== 'undefined' && typeof window.hcaptcha.setData === 'function') {{
  210. try {{ window.hcaptcha.setData(null, {{ response: token }}); }} catch(e) {{}}
  211. }}
  212. if (typeof window.onHCaptchaSuccess === 'function') {{
  213. window.onHCaptchaSuccess(token);
  214. }}
  215. }})();
  216. """
  217. tab.run_js(inject_script)
  218. time.sleep(2)
  219. btn = tab.ele('tag:button@@type=submit', timeout=2) or tab.ele('tag:input@@type=submit', timeout=2)
  220. if btn:
  221. btn.click()
  222. tab.wait.doc_loaded()
  223. time.sleep(3)
  224. self._log(f"Page title after hCaptcha submit: {tab.title}")
  225. available_dates = []
  226. date_eles = tab.eles('.available-date, .calendar-day-available, [data-date]')
  227. for d_ele in date_eles:
  228. d_str = d_ele.attr('data-date') or d_ele.text
  229. if d_str and re.match(r'^\d{4}-\d{2}-\d{2}$', d_str.strip()):
  230. available_dates.append(d_str.strip())
  231. if available_dates:
  232. res.success = True
  233. res.availability_status = AvailabilityStatus.Available
  234. earliest_date = available_dates[0]
  235. earliest_dt = datetime.strptime(earliest_date, "%Y-%m-%d")
  236. res.earliest_date = earliest_dt
  237. res.availability = [
  238. DateAvailability(
  239. date=datetime.strptime(d, "%Y-%m-%d"),
  240. times=[],
  241. )
  242. for d in available_dates
  243. ]
  244. else:
  245. res.success = False
  246. res.availability_status = AvailabilityStatus.NoneAvailable
  247. res.availability = []
  248. finally:
  249. try:
  250. tab.close()
  251. except Exception:
  252. pass
  253. return res
  254. def get_captcha_solution(self, api_key: str, site_key: str, site_url: str) -> str:
  255. """
  256. 调用 Capsolver 解决 hCaptcha (HCaptchaTaskProxyLess)
  257. """
  258. if not api_key:
  259. api_key = self.free_config.get('capsolver_key') or os.getenv('CAPSOLVER_KEY', '')
  260. if not api_key:
  261. raise BizLogicError(message="Capsolver API key missing in free_config or environment")
  262. payload = {
  263. "clientKey": api_key,
  264. "task": {
  265. "type": "HCaptchaTaskProxyLess",
  266. "websiteKey": site_key,
  267. "websiteURL": site_url
  268. }
  269. }
  270. self._log("Submitting hCaptcha task to Capsolver...")
  271. with requests.Session() as session:
  272. res = session.post("https://api.capsolver.com/createTask", json=payload, timeout=20)
  273. res_json = res.json()
  274. if res_json.get("errorId", 0) != 0:
  275. raise BizLogicError(message=f"Failed to create Capsolver task: {res.text}")
  276. task_id = res_json.get("taskId")
  277. if not task_id:
  278. raise BizLogicError(message=f"No taskId returned from Capsolver: {res.text}")
  279. self._log(f"Waiting for hCaptcha solution (TaskId: {task_id})...")
  280. start_time = time.time()
  281. while time.time() - start_time < 120:
  282. time.sleep(3)
  283. res = session.post(
  284. "https://api.capsolver.com/getTaskResult",
  285. json={"clientKey": api_key, "taskId": task_id},
  286. timeout=20
  287. )
  288. result = res.json()
  289. status = result.get("status")
  290. if status == "ready":
  291. solution = result.get("solution", {})
  292. token = solution.get("gRecaptchaResponse") or solution.get("token") or solution.get("response")
  293. self._log("✅ hCaptcha solved successfully.")
  294. return token
  295. if status == "failed":
  296. raise BizLogicError(message=f"hCaptcha solve failed: {res.text}")
  297. raise BizLogicError(message="Capsolver task timed out (120s)")
  298. def book(self, slot_info: VSQueryResult, user_inputs: Dict) -> VSBookResult:
  299. res = VSBookResult()
  300. return res
  301. def _perform_request(self, method, url, headers=None, data=None, json_data=None, params=None, retry_count=0):
  302. if not self.page:
  303. raise BizLogicError("Browser not init")
  304. req_url = url
  305. if params:
  306. sep = '&' if '?' in req_url else '?'
  307. req_url += sep + urlencode(params)
  308. fetch_opts = { "method": method.upper(), "headers": headers or {}, "credentials": "include" }
  309. if json_data:
  310. fetch_opts['body'] = json.dumps(json_data)
  311. fetch_opts['headers']['Content-Type'] = 'application/json'
  312. elif data:
  313. if isinstance(data, dict):
  314. fetch_opts['body'] = urlencode(data)
  315. fetch_opts['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
  316. else:
  317. fetch_opts['body'] = data
  318. js = f"""
  319. return fetch("{req_url}", {json.dumps(fetch_opts)})
  320. .then(async r => {{
  321. const h = {{}}; r.headers.forEach((v, k) => h[k] = v);
  322. return {{ status: r.status, body: await r.text(), headers: h, url: r.url }};
  323. }}).catch(e => {{ return {{ status: 0, body: e.toString() }}; }});
  324. """
  325. resp = BrowserResponse(self.page.run_js(js, timeout=60))
  326. if resp.status_code == 200:
  327. return resp
  328. elif resp.status_code == 403:
  329. if "Just a moment" in resp.text and retry_count < 2:
  330. self._log("Cloudflare 403. Refreshing...")
  331. if self._refresh_firewall_session():
  332. return self._perform_request(method, url, headers, data, json_data, params, retry_count+1)
  333. raise PermissionDeniedError(f"HTTP 403: {resp.text[:100]}")
  334. elif resp.status_code == 429:
  335. self.is_healthy = False
  336. raise RateLimiteddError()
  337. elif resp.status_code in [401, 419]:
  338. self.is_healthy = False
  339. raise SessionExpiredOrInvalidError()
  340. else:
  341. raise BizLogicError(f"HTTP {resp.status_code}: {resp.text[:100]}")
  342. def _filter_dates(self, dates, start, end):
  343. if not start or not end: return dates
  344. valid = []
  345. s = datetime.strptime(start[:10], "%Y-%m-%d")
  346. e = datetime.strptime(end[:10], "%Y-%m-%d")
  347. for d in dates:
  348. c = datetime.strptime(d, "%Y-%m-%d")
  349. if s <= c <= e: valid.append(d)
  350. random.shuffle(valid)
  351. return valid
  352. def cleanup(self):
  353. if self.page:
  354. try: self.page.quit()
  355. except: pass
  356. self.page = None
  357. if os.path.exists(self.root_workspace):
  358. for _ in range(3):
  359. try: time.sleep(0.2); shutil.rmtree(self.root_workspace, ignore_errors=True); break
  360. except: time.sleep(0.5)
  361. if self.tunnel:
  362. try: self.tunnel.stop()
  363. except: pass
  364. self.tunnel = None
  365. def get_plugin_status(self) -> PluginStatus:
  366. account_name = self.config.account.username if self.config and self.config.account else ""
  367. proxy_info = f"{self.config.proxy.ip}:{self.config.proxy.port}" if self.config and self.config.proxy and self.config.proxy.ip else ""
  368. current_url = ""
  369. if self.page:
  370. try:
  371. current_url = self.page.url
  372. except Exception:
  373. pass
  374. state = "running" if getattr(self, "is_healthy", True) else "unhealthy"
  375. return PluginStatus(
  376. instance_id=getattr(self, "instance_id", self.group_id),
  377. is_healthy=getattr(self, "is_healthy", True),
  378. state=state,
  379. current_url=current_url,
  380. account=account_name,
  381. proxy=proxy_info,
  382. session_age_sec=0,
  383. last_active_at=utc_now_str(),
  384. )
  385. def __del__(self):
  386. self.cleanup()