ita_plugin.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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 utils.mouse import HumanMouse
  31. from utils.keyboard import HumanKeyboard
  32. from utils.flow_engine import BaseFlow, on, guard, done, fatal
  33. from utils.plugin_utils import (
  34. BrowserResponse,
  35. init_chromium_options,
  36. perform_browser_fetch,
  37. filter_dates_in_range,
  38. cleanup_browser_workspace
  39. )
  40. class ItaLoginFlow(BaseFlow):
  41. """
  42. 基于 BaseFlow 注解状态机驱动的意大利 Prenot@Mi 会话创建与自动导航流
  43. """
  44. def __init__(self, page, plugin: 'ItaPlugin', interval: float = 1.0):
  45. super().__init__(
  46. page=page,
  47. config=plugin.config,
  48. context={"plugin": plugin},
  49. interval=interval,
  50. logger=plugin._log,
  51. cf_bypasser=CloudflareBypasser(page, log=plugin.config.debug if plugin.config else False)
  52. )
  53. self.plugin = plugin
  54. self.has_submitted_login = False
  55. # --- 异常守卫与自愈 (Priority 5~7) ---
  56. @guard(text="502 Bad Gateway", priority=5)
  57. @guard(text="503 Service Temporarily Unavailable", priority=5)
  58. @guard(text="error 1020", priority=5)
  59. def on_server_network_error(self):
  60. self.log("[WARN] 检测到服务器网络错误 (502/503/1020),等待并刷新自愈...", level="warning")
  61. self.plugin._random_sleep(10, 15)
  62. self.page.refresh()
  63. try:
  64. self.page.wait.doc_loaded()
  65. except Exception:
  66. pass
  67. @guard(text="Sorry, you have been blocked", priority=6)
  68. @guard(text="You are being rate limited", priority=6)
  69. @guard(text="Cloudflare Ray ID", priority=6)
  70. @fatal("Blocked by Cloudflare WAF. Need to change IP or browser fingerprint.")
  71. def on_cloudflare_blocked(self):
  72. pass
  73. @guard(title="just a moment", priority=7)
  74. def on_cloudflare_turnstile_challenge(self):
  75. self.log("🛡️ 检测到 Cloudflare 5秒盾挑战,正在尝试穿透...")
  76. if self.cf_bypasser.bypass(max_retry=3):
  77. self.plugin._random_sleep(3, 5)
  78. self.cf_bypasser.handle_waiting_room()
  79. @guard(title="waitting room", priority=7)
  80. def on_cloudflare_waiting_room(self):
  81. self.log("🛡️ 检测到 Cloudflare 等候室,正在等待自动排队刷新...")
  82. self.cf_bypasser.handle_waiting_room()
  83. time.sleep(2)
  84. # --- 状态流转与表单处理 (Priority 35~45) ---
  85. @on(ele="#pingid-button", priority=45)
  86. def handle_initial_landing_page(self):
  87. self.log("[State] Initial Landing Page detected. Clicking login button...")
  88. login_btn = self.page.ele('#pingid-button')
  89. if login_btn:
  90. self.plugin.mouse.human_click_ele(login_btn)
  91. self.log("Redirecting to PingID...")
  92. self.plugin._random_sleep(3, 5)
  93. @on(ele="@name=callback_1", priority=40)
  94. def handle_pingid_login_form(self):
  95. if self.has_submitted_login:
  96. return
  97. self.log("[State] PingID Login Form detected. Submitting credentials...")
  98. username = self.plugin.config.account.username
  99. password = self.plugin.config.account.password
  100. self.log("Entering username...")
  101. user_input = self.page.ele('@name=callback_1')
  102. self.plugin.mouse.human_click_ele(user_input)
  103. user_input.input(username, clear=True)
  104. time.sleep(1)
  105. self.log("Entering password...")
  106. pwd_input = self.page.ele('@type=password')
  107. self.plugin.mouse.human_click_ele(pwd_input)
  108. pwd_input.input(password, clear=True)
  109. time.sleep(1)
  110. self.log("Clicking submit login button...")
  111. submit_btn = self.page.ele('tag:button@type=submit')
  112. if submit_btn:
  113. self.plugin.mouse.human_click_ele(submit_btn)
  114. self.has_submitted_login = True
  115. self.log("Login form submitted. Waiting for dashboard to load...")
  116. self.plugin._random_sleep(5, 7)
  117. @on(ele=".app-menu", priority=35)
  118. def handle_dashboard_menu(self):
  119. self.log("[State] Dashboard Menu detected.")
  120. switch_en_btn = self.page.ele('tag:a@@href=/Language/ChangeLanguage?lang=2', timeout=0.5)
  121. if switch_en_btn:
  122. self.log("Found 'English' language switch button. Clicking to change language...")
  123. self.plugin.mouse.human_click_ele(switch_en_btn)
  124. self.plugin._random_sleep(4, 6)
  125. return
  126. book_btn = self.page.ele('@href=/Services', timeout=0.5)
  127. if book_btn:
  128. self.log("Found 'Book' menu element. Clicking to proceed to reservation...")
  129. self.plugin.mouse.human_click_ele(book_btn)
  130. self.plugin._random_sleep(4, 6)
  131. return
  132. self.log("[WARN] On Dashboard, but neither 'English' nor 'Book' button was found. Waiting...")
  133. time.sleep(2)
  134. # --- 成功终态到达 (Priority 10) ---
  135. @on(url="/Services", ele=".app-menu", priority=10)
  136. @done
  137. def on_reached_services_page(self):
  138. self.plugin.session_create_time = time.time()
  139. self.log("🎉 Successfully reached the Booking Services page! Session created successfully!")
  140. class ItaPlugin(IVSPlg):
  141. name = "ita_plugin"
  142. version = "1.0.0"
  143. description = "意大利Prenot@Mi领事预约插件 (支持拟人化交互与多领区调度)"
  144. def __init__(self, group_id: str):
  145. self.group_id = group_id
  146. self.config: Optional[VSPlgConfig] = None
  147. self.free_config: Dict[str, Any] = {}
  148. self.is_healthy = True
  149. self.logger = None
  150. self.page: Optional[ChromiumPage] = None
  151. # Prenotami 特有配置
  152. self._service_id = 0
  153. self.ita_url = 'https://prenotami.esteri.it'
  154. self._host = self.ita_url
  155. self.instance_id = uuid.uuid4().hex[:8]
  156. self.root_workspace = os.path.abspath(os.path.join("data/temp_browser_data", f"{self.group_id}.{self.instance_id}"))
  157. self.user_data_path = os.path.join(self.root_workspace, "user_data")
  158. if not os.path.exists(self.root_workspace):
  159. os.makedirs(self.root_workspace)
  160. self.tunnel = None
  161. self.session_create_time: float = 0
  162. def set_log(self, logger: Callable[[str], None]) -> None:
  163. self.logger = logger
  164. def _log(self, message):
  165. if self.logger:
  166. self.logger(f'[ItaPlugin] [{self.group_id}] [{self.instance_id}] {message}')
  167. else:
  168. print(f'[ItaPlugin] [{self.group_id}] [{self.instance_id}] {message}')
  169. def _random_sleep(self, min_sec=3, max_sec=5):
  170. sleep_time = random.uniform(min_sec, max_sec)
  171. time.sleep(sleep_time)
  172. def set_config(self, config: VSPlgConfig):
  173. self.config = config
  174. self.free_config = config.free_config or {}
  175. # Service ID (e.g., 1321 for Ireland, 5059 for Guangzhou)
  176. self._service_id = self.free_config.get('service_id', 0)
  177. def keep_alive(self):
  178. pass
  179. def health_check(self) -> bool:
  180. if not self.is_healthy or not self.page:
  181. return False
  182. try:
  183. if not self.page.run_js("return 1;"):
  184. return False
  185. except Exception:
  186. return False
  187. if self.config.session_max_life > 0:
  188. if time.time() - self.session_create_time > self.config.session_max_life:
  189. self._log("Session expired.")
  190. return False
  191. return True
  192. def create_session(self):
  193. """
  194. 全浏览器会话创建:过盾 -> JS注入登录 -> 状态机自动路由导航 -> 到达目标页
  195. """
  196. self._log(f"Initializing Session (ID: {self.instance_id})...")
  197. self._log(f"Account id={self.config.account.id}, proxy id={self.config.proxy.id if self.config and self.config.proxy else 'None'}")
  198. co, self.tunnel = init_chromium_options(
  199. user_data_path=self.user_data_path,
  200. username_seed=self.config.account.username if self.config and self.config.account else None,
  201. proxy=self.config.proxy if self.config else None,
  202. headless=False,
  203. window_size="1920,1080",
  204. logger=self._log
  205. )
  206. self.page = ChromiumPage(co)
  207. ita_url = self.ita_url
  208. self._log(f"Navigating to {ita_url}")
  209. self.page.get(ita_url)
  210. self._log("Init humanize tools...")
  211. self.mouse = HumanMouse(self.page, debug=False)
  212. self.keyboard = HumanKeyboard(self.page)
  213. viewport_width = self.page.rect.viewport_size[0]
  214. viewport_height = self.page.rect.viewport_size[1]
  215. init_x = random.randint(10, viewport_width - 10)
  216. init_y = random.randint(10, viewport_height - 10)
  217. self.mouse.move(init_x, init_y)
  218. # 启动 ItaLoginFlow 注解流状态机
  219. login_flow = ItaLoginFlow(
  220. self.page,
  221. plugin=self,
  222. interval=1.0
  223. )
  224. login_flow.run(max_steps=20)
  225. self.session_create_time = time.time()
  226. self._log("🎉 ItaLoginFlow completed successfully. Session created.")
  227. def query(self, apt_type: AppointmentType) -> VSQueryResult:
  228. res = VSQueryResult()
  229. res.success = False
  230. res.availability_status = AvailabilityStatus.NoneAvailable
  231. TARGET_SERVICE = self.free_config.get('target_service', "National and Schengen Visas")
  232. REASON_FOR_VISIT = self.free_config.get('reason_for_visit', "Tourism")
  233. # 步骤 1:在服务列表中寻找并点击目标服务的 Book
  234. self._log("Waiting for service list to load...")
  235. self.page.wait.eles_loaded('@aria-controls=dataTableServices', timeout=10)
  236. self._log(f"Searching for [{TARGET_SERVICE}] Book button...")
  237. target_book_btn_xpath = f'xpath://tr[contains(., "{TARGET_SERVICE}")]//a[contains(text(), "Book")]'
  238. book_btn = self.page.ele(target_book_btn_xpath)
  239. if not book_btn:
  240. self._log(f"Target [{TARGET_SERVICE}] Book button not found, currently no slots available.")
  241. return res
  242. self.mouse.human_click_ele(book_btn)
  243. self.page.wait.load_start()
  244. time.sleep(3)
  245. # 步骤 2:填写预约表单
  246. self._log("Waiting for booking form to load...")
  247. self.page.wait.eles_loaded('#bookingForm', timeout=10)
  248. self._log("Selecting booking type & reason...")
  249. typeofbooking_ddl = self.page.ele('#typeofbookingddl')
  250. if typeofbooking_ddl:
  251. typeofbooking_ddl.select('Individual booking')
  252. time.sleep(1)
  253. reason_ddl = self.page.ele('#ddls_0')
  254. if reason_ddl:
  255. reason_ddl.select(REASON_FOR_VISIT)
  256. time.sleep(1)
  257. notes_input = self.page.ele('#BookingNotes')
  258. if notes_input:
  259. notes_input.clear()
  260. notes_input.input('N/A')
  261. time.sleep(1)
  262. # 步骤 3:处理 OTP 验证码
  263. self._log("Sending OTP code...")
  264. otp_send_btn = self.page.ele('#otp-send')
  265. if otp_send_btn:
  266. self.mouse.human_click_ele(otp_send_btn)
  267. self.page.wait.ele_displayed('#IdOtpSent', timeout=15)
  268. self._log("OTP code sent successfully.")
  269. otp_input = self.page.ele('#otp-input')
  270. if otp_input:
  271. self.mouse.human_click_ele(otp_input)
  272. otp_input.input('123456')
  273. time.sleep(1)
  274. # 步骤 4:勾选隐私政策并提交
  275. privacy_checkbox = self.page.ele('#PrivacyCheck')
  276. if privacy_checkbox and not privacy_checkbox.states.is_checked:
  277. self.mouse.human_click_ele(privacy_checkbox)
  278. time.sleep(1)
  279. self._log("Submitting forward form...")
  280. forward_btn = self.page.ele('#btnAvanti')
  281. if forward_btn:
  282. self.mouse.human_click_ele(forward_btn)
  283. self.page.wait.load_start()
  284. self._log(f"Form submitted. Current page: {self.page.url}")
  285. time.sleep(3)
  286. return res
  287. def book(self, slot_info: VSQueryResult, user_inputs: Dict = None) -> VSBookResult:
  288. res = VSBookResult()
  289. res.success = False
  290. user_inputs = user_inputs or {}
  291. if not slot_info.availability:
  292. raise NotFoundError("No slots to book")
  293. target_dt = slot_info.availability[0].date
  294. target_date = target_dt.strftime("%Y-%m-%d") if target_dt else ""
  295. target_slot = slot_info.availability[0].times[0] if slot_info.availability[0].times else None
  296. slot_id = target_slot.label if target_slot else ""
  297. slot_text = target_slot.time if target_slot else ""
  298. # 1. 获取 OTP (GenerateOTP)
  299. self._log("Requesting OTP...")
  300. otp_url = f"{self._host}/BookingCalendar/GenerateOTP?ServiceID={self._service_id}"
  301. self._perform_request("POST", otp_url)
  302. # 2. 等待并读取邮件
  303. self._log("Waiting for email code...")
  304. time.sleep(10)
  305. email_account = self.config.account.username if self.config and self.config.account else ""
  306. otp_code = VSCloudApi.Instance().get_email_verify_code(email_account)
  307. if not otp_code:
  308. raise BizLogicError("Failed to retrieve OTP code")
  309. self._log(f"Got OTP: {otp_code}")
  310. # 3. 提交详细信息 (Fill User Info)
  311. self._log("Submitting User Details & Files...")
  312. passport_pdf_path = user_inputs.get('passport_pdf_path')
  313. irp_pdf_path = user_inputs.get('irp_pdf_path')
  314. def file_to_b64(path):
  315. if not path or not os.path.exists(path):
  316. return ""
  317. with open(path, "rb") as f:
  318. return base64.b64encode(f.read()).decode('utf-8')
  319. ppt_b64 = file_to_b64(passport_pdf_path)
  320. irp_b64 = file_to_b64(irp_pdf_path)
  321. submit_url = f"{self._host}/Services/Booking/{self._service_id}"
  322. js_submit = f"""
  323. const url = "{submit_url}";
  324. const fd = new FormData();
  325. fd.append('ServizioDescrizione', 'D Visa Application');
  326. fd.append('MessaggioRassicuranteWaitingList', 'True');
  327. fd.append('isWaitingListEnabled', 'False');
  328. fd.append('IDServizioConsolare', '35');
  329. fd.append('IDServizioErogato', '{self._service_id}');
  330. fd.append('IdTipoPrenotazione', '1');
  331. fd.append('NumMaxAccompagnatori', '3');
  332. fd.append('NumAccompagnatoriSelected', '0');
  333. fd.append('DatiAddizionaliPrenotante[0]._Descrizione', 'Other citizenship/s');
  334. fd.append('DatiAddizionaliPrenotante[0]._testo', '{user_inputs.get("citizen", "China")}');
  335. fd.append('DatiAddizionaliPrenotante[0]._Obbligatorio', 'False');
  336. fd.append('DatiAddizionaliPrenotante[0]._Id', '61738');
  337. fd.append('DatiAddizionaliPrenotante[0]._TipoDatoAddizionale.IDTipoDatoAddizionale', '26');
  338. fd.append('DatiAddizionaliPrenotante[0]._TipoDatoAddizionale.IDTipoControllo', '2');
  339. fd.append('DatiAddizionaliPrenotante[1]._Descrizione', 'Full residence address');
  340. fd.append('DatiAddizionaliPrenotante[1]._testo', '{user_inputs.get("address", "")}');
  341. fd.append('DatiAddizionaliPrenotante[1]._Obbligatorio', 'True');
  342. fd.append('DatiAddizionaliPrenotante[1]._Id', '61739');
  343. fd.append('DatiAddizionaliPrenotante[1]._TipoDatoAddizionale.IDTipoDatoAddizionale', '25');
  344. fd.append('DatiAddizionaliPrenotante[1]._TipoDatoAddizionale.IDTipoControllo', '2');
  345. fd.append('DatiAddizionaliPrenotante[2]._Descrizione', 'Passport number');
  346. fd.append('DatiAddizionaliPrenotante[2]._testo', '{user_inputs.get("passport", "")}');
  347. fd.append('DatiAddizionaliPrenotante[2]._Obbligatorio', 'True');
  348. fd.append('DatiAddizionaliPrenotante[2]._Id', '61740');
  349. fd.append('DatiAddizionaliPrenotante[2]._TipoDatoAddizionale.IDTipoDatoAddizionale', '2');
  350. fd.append('DatiAddizionaliPrenotante[2]._TipoDatoAddizionale.IDTipoControllo', '2');
  351. fd.append('DatiAddizionaliPrenotante[3]._Descrizione', 'Reason for visit');
  352. fd.append('DatiAddizionaliPrenotante[3]._Obbligatorio', 'True');
  353. fd.append('DatiAddizionaliPrenotante[3]._Id', '61741');
  354. fd.append('DatiAddizionaliPrenotante[3]._TipoDatoAddizionale.IDTipoDatoAddizionale', '34');
  355. fd.append('DatiAddizionaliPrenotante[3]._TipoDatoAddizionale.IDTipoControllo', '3');
  356. fd.append('DatiAddizionaliPrenotante[3]._idSelezionato', '42');
  357. fd.append('otp-input', '{otp_code}');
  358. fd.append('PrivacyCheck', 'true');
  359. async function addFile(b64, name, filename) {{
  360. if(!b64) return;
  361. const res = await fetch(`data:application/pdf;base64,${{b64}}`);
  362. const blob = await res.blob();
  363. fd.append(name, blob, filename);
  364. }}
  365. await Promise.all([
  366. addFile('{ppt_b64}', 'File_0', 'passport.pdf'),
  367. addFile('{irp_b64}', 'File_1', 'irp.pdf')
  368. ]);
  369. return fetch(url, {{
  370. method: 'POST',
  371. body: fd
  372. }}).then(async r => {{
  373. return {{ status: r.status, url: r.url, text: await r.text() }};
  374. }}).catch(e => {{ return {{ status: 0, text: e.toString() }}; }});
  375. """
  376. result_dict = self.page.run_js(js_submit)
  377. resp = BrowserResponse(result_dict)
  378. if resp.status_code == 302 or "BookingCalendar" in resp.url:
  379. self._log("User Info Submitted Successfully.")
  380. else:
  381. self._log(f"User Info Submit Failed: {resp.text[:100]}")
  382. if "Codice errato" in resp.text:
  383. raise BizLogicError("Invalid OTP Code")
  384. return res
  385. # 4. 最终确认预约 (InsertNewBooking)
  386. self._log("Finalizing Booking...")
  387. final_url = f"{self._host}/BookingCalendar/InsertNewBooking"
  388. final_payload = {
  389. "idCalendarioGiornaliero": slot_id,
  390. "selectedDay": target_date,
  391. "selectedHour": slot_text
  392. }
  393. resp_final = self._perform_request("POST", final_url, data=final_payload)
  394. if resp_final.status_code == 200:
  395. self._log("Booking Confirmed!")
  396. res.success = True
  397. res.book_date = target_date
  398. res.book_time = slot_text
  399. else:
  400. self._log(f"Final Booking Failed: {resp_final.status_code}")
  401. return res
  402. def _perform_request(self, method, url, headers=None, data=None, json_data=None):
  403. """在浏览器上下文中注入执行 Fetch"""
  404. if not self.page:
  405. raise BizLogicError("Browser not initialized")
  406. return perform_browser_fetch(
  407. self.page,
  408. method=method,
  409. url=url,
  410. headers=headers,
  411. data=data,
  412. json_data=json_data
  413. )
  414. def _parse_valid_days(self, text: str) -> List[str]:
  415. days = []
  416. try:
  417. matches = re.findall(r'{"DateLibere":"(.*?)".*?"SlotRimanenti":(-?\d+)}', text)
  418. for d_str, rem in matches:
  419. if int(rem) != -1:
  420. dt = datetime.strptime(d_str[:10], "%d/%m/%Y")
  421. days.append(dt.strftime("%Y-%m-%d"))
  422. except Exception:
  423. pass
  424. return days
  425. def _parse_time_slots(self, text: str) -> List[Dict[str, Any]]:
  426. slots = []
  427. try:
  428. raw_list = json.loads(text)
  429. for item in raw_list:
  430. remain = item.get('SlotRimanenti', -1)
  431. if remain > 0:
  432. start = item['OrarioInizioFascia']
  433. end = item['OrarioFineFascia']
  434. s_time = f"{start['Hours']:02d}:{start['Minutes']:02d}"
  435. e_time = f"{end['Hours']:02d}:{end['Minutes']:02d}"
  436. slots.append({
  437. 'id': item['IDCalendarioServizioGiornaliero'],
  438. 'start': s_time,
  439. 'end': e_time,
  440. 'remain': remain
  441. })
  442. except Exception:
  443. pass
  444. return slots
  445. def cleanup(self):
  446. """销毁浏览器并彻底删除临时文件"""
  447. cleanup_browser_workspace(self.page, self.root_workspace, self.tunnel, self._log)
  448. self.page = None
  449. self.tunnel = None
  450. def get_plugin_status(self) -> PluginStatus:
  451. account_name = self.config.account.username if self.config and self.config.account else ""
  452. proxy_info = f"{self.config.proxy.ip}:{self.config.proxy.port}" if self.config and self.config.proxy and self.config.proxy.ip else ""
  453. current_url = ""
  454. if self.page:
  455. try:
  456. current_url = self.page.url
  457. except Exception:
  458. pass
  459. session_age = int(time.time() - self.session_create_time) if self.session_create_time > 0 else 0
  460. state = "running" if self.is_healthy else "unhealthy"
  461. return PluginStatus(
  462. instance_id=self.instance_id,
  463. is_healthy=self.is_healthy,
  464. state=state,
  465. current_url=current_url,
  466. account=account_name,
  467. proxy=proxy_info,
  468. session_age_sec=session_age,
  469. last_active_at=utc_now_str(),
  470. )
  471. def __del__(self):
  472. """
  473. 析构函数:当对象被垃圾回收时自动调用
  474. """
  475. self.cleanup()