ita_plugin.py 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  1. import time
  2. import json
  3. import random
  4. import socket
  5. import uuid
  6. import shutil
  7. import re
  8. import os
  9. import stat
  10. from dataclasses import dataclass
  11. from datetime import datetime
  12. from pathlib import Path
  13. from typing import Dict, Optional, Any, Callable
  14. from urllib.parse import parse_qs, urlparse
  15. try:
  16. import fcntl
  17. except ImportError: # pragma: no cover - persistent local profiles require POSIX.
  18. fcntl = None
  19. from DrissionPage import ChromiumPage, ChromiumOptions
  20. from DrissionPage.errors import PageDisconnectedError
  21. import configure
  22. from plugins.ita_calendar_book import (
  23. CalendarBookError,
  24. book_date_preferences,
  25. read_calendar_service,
  26. recheck_calendar_before_final,
  27. )
  28. from plugins.ita_calendar_query import (
  29. CALENDAR_PATH,
  30. CalendarAccessDeniedError,
  31. CalendarAuthenticationExpiredError,
  32. CalendarGateError,
  33. CalendarQueryScanResult,
  34. CalendarRateLimitedError,
  35. CalendarTransientError,
  36. guard_calendar,
  37. same_origin_path,
  38. scan_until_first_available_month,
  39. )
  40. from plugins.ita_protocol import (
  41. ItaCapability,
  42. ItaProtocolError,
  43. ItaRouteConfig,
  44. resolve_route_config,
  45. )
  46. from vs_plg import IVSPlg
  47. from vs_types import VSPlgConfig, AppointmentType, VSQueryResult, VSBookResult, DateAvailability, AvailabilityStatus, SessionExpiredOrInvalidError, RateLimiteddError, PermissionDeniedError, BizLogicError, PluginStatus, utc_now_str
  48. from toolkit.mihomo_tunnel import MihomoTunnel
  49. from utils.mouse import HumanMouse
  50. from utils.keyboard import HumanKeyboard
  51. from utils.fingerprint_utils import FingerprintGenerator
  52. BOOKING_FORM_DISCOVERY_STOP_LABEL = "booking-form-ready-before-avanti"
  53. BOOKING_RECHECK_NONE_AVAILABLE_LABEL = "no-availability-after-user-recheck"
  54. BOOKING_CALENDAR_PREFINAL_STOP_LABEL = "calendar-ready-before-final"
  55. PERSISTENT_BROWSER_PROFILE_KEY = "persistent_browser_profile_path"
  56. RADWARE_VALIDATION_HOSTS = frozenset({"validate.perfdrive.com"})
  57. SERVICE_LINK_WAIT_SECONDS = 15
  58. SERVICES_PROBE_QUERY_MODE = "services_probe"
  59. CALENDAR_QUERY_MODE = "calendar"
  60. SERVICE_LIST_DIAGNOSTIC_SCRIPT = r"""
  61. /* coordinate:ita-service-list-structure */
  62. const targetPath = '__TARGET_PATH__';
  63. const tables = Array.from(document.querySelectorAll('table'));
  64. const bookingLinks = Array.from(
  65. document.querySelectorAll('a[href^="/Services/Booking/"]')
  66. );
  67. // Page-level diagnostic only: a row without its booking link exposes no
  68. // verified service-id binding. Never turn this count into a booking result.
  69. const normalizeNotice = (value) => String(value || '').replace(/\s+/g, ' ')
  70. .trim().toLowerCase().replace(/[.。!!]+$/, '');
  71. const alreadyBookedNotice = 'prenotazione già effettuata per questo servizio';
  72. const alreadyBookedRows = tables.flatMap((table) =>
  73. Array.from(table.querySelectorAll('tbody tr')),
  74. ).filter((row) => Array.from(row.cells).some((cell) => {
  75. const style = window.getComputedStyle(cell);
  76. return cell.getClientRects().length > 0 && style.visibility === 'visible'
  77. && normalizeNotice(cell.innerText) === alreadyBookedNotice;
  78. }));
  79. return {
  80. path: window.location.pathname,
  81. ready_state: document.readyState,
  82. table_count: tables.length,
  83. row_count: tables.reduce(
  84. (count, table) => count + table.querySelectorAll('tbody tr').length,
  85. 0,
  86. ),
  87. booking_link_count: bookingLinks.length,
  88. target_link_count: bookingLinks.filter(
  89. (link) => link.getAttribute('href') === targetPath,
  90. ).length,
  91. already_booked_notice_row_count: alreadyBookedRows.length,
  92. };
  93. """
  94. class ItaAuthenticationChallengeError(BizLogicError):
  95. """Raised when website authentication requires an external security challenge."""
  96. class ItaTargetServiceLinkMissingError(BizLogicError):
  97. """Raised when the authenticated Services page lacks the configured link."""
  98. class ItaCalendarPreparationRequiredError(BizLogicError):
  99. """Read-only recovery cannot restore Calendar; ITA-OTP-01 remains pending."""
  100. class ItaCalendarTransientError(BizLogicError):
  101. """A bounded read-only retry is possible without repeating preparation."""
  102. @dataclass(frozen=True, slots=True, repr=False)
  103. class CalendarQueryCheckpoint:
  104. """In-memory comparison reference, never authentication proof or a signal.
  105. Kept only by the owner of the prepared Query session. It must not be
  106. serialized, sent to Book, or used for a different account/profile/route.
  107. """
  108. route: ItaRouteConfig
  109. username: str
  110. profile_path: str
  111. calendar_url: str
  112. runtime_service: str
  113. BOOKING_FORM_DISCOVERY_SCRIPT = r"""
  114. const form = document.querySelector('#bookingForm');
  115. if (!form) {
  116. return null;
  117. }
  118. const toPath = (rawUrl) => {
  119. try {
  120. return new URL(rawUrl, window.location.origin).pathname;
  121. } catch (_error) {
  122. return null;
  123. }
  124. };
  125. const allFields = Array.from(
  126. form.querySelectorAll('input, select, textarea')
  127. );
  128. const allButtons = Array.from(
  129. form.querySelectorAll('button, input[type="submit"], input[type="button"]')
  130. );
  131. return {
  132. path: window.location.pathname,
  133. form: {
  134. method: (form.getAttribute('method') || 'GET').toUpperCase(),
  135. action_path: toPath(form.getAttribute('action') || window.location.href),
  136. },
  137. field_count: allFields.length,
  138. fields_truncated: allFields.length > 100,
  139. fields: allFields.slice(0, 100).map((element) => ({
  140. tag: element.tagName.toLowerCase(),
  141. type: element.tagName === 'INPUT'
  142. ? (element.getAttribute('type') || 'text').toLowerCase()
  143. : null,
  144. id: element.getAttribute('id'),
  145. name: element.getAttribute('name'),
  146. required: Boolean(element.required),
  147. })),
  148. button_count: allButtons.length,
  149. buttons_truncated: allButtons.length > 50,
  150. buttons: allButtons.slice(0, 50).map((element) => ({
  151. tag: element.tagName.toLowerCase(),
  152. type: (element.getAttribute('type') || 'button').toLowerCase(),
  153. id: element.getAttribute('id'),
  154. name: element.getAttribute('name'),
  155. text: element.tagName === 'BUTTON'
  156. ? (element.textContent || '').trim()
  157. : (element.getAttribute('aria-label') || '').trim(),
  158. })),
  159. };
  160. """
  161. # ==========================================
  162. # 2. ItaPlugin 核心逻辑
  163. # ==========================================
  164. class ItaPlugin(IVSPlg):
  165. name = "ita_plugin"
  166. version = "1.0.0"
  167. description = "意大利Prenot@Mi领事预约插件 (支持拟人化交互与多领区调度)"
  168. _KNOWN_NO_AVAILABILITY_NOTICES = frozenset(
  169. {
  170. "stante l'elevata richiesta i posti disponibili per il servizio "
  171. "scelto sono esauriti",
  172. }
  173. )
  174. def __init__(self, group_id: str):
  175. self.group_id = group_id
  176. self.config: Optional[VSPlgConfig] = None
  177. self.free_config: Dict[str, Any] = {}
  178. self.is_healthy = True
  179. self._cleanup_attempted = False
  180. self.logger = None
  181. self.page: Optional[ChromiumPage] = None
  182. # Prenotami 特有配置
  183. self.ita_url = 'https://prenotami.esteri.it'
  184. self.instance_id = uuid.uuid4().hex[:8]
  185. self.root_workspace = os.path.abspath(os.path.join("data/temp_browser_data", f"{self.group_id}.{self.instance_id}"))
  186. self._transient_user_data_path = os.path.join(self.root_workspace, "user_data")
  187. self.user_data_path = self._transient_user_data_path
  188. self._persistent_profile_path: Optional[Path] = None
  189. self._profile_lock_handle = None
  190. if not os.path.exists(self.root_workspace):
  191. os.makedirs(self.root_workspace)
  192. self.tunnel = None
  193. self.session_create_time: float = 0
  194. self.last_booking_form_structure: Optional[Dict[str, Any]] = None
  195. self.last_service_list_structure: Optional[Dict[str, Any]] = None
  196. self.last_calendar_query_scan: Optional[CalendarQueryScanResult] = None
  197. self._calendar_query_route_key: Optional[str] = None
  198. self._calendar_query_checkpoint: Optional[CalendarQueryCheckpoint] = None
  199. self._calendar_query_page = None
  200. self._calendar_query_generation = None
  201. self._calendar_query_tab_id = None
  202. self._query_stop_reason = None
  203. self._reset_calendar_book_state()
  204. def set_log(self, logger: Callable[[str], None]) -> None:
  205. self.logger = logger
  206. def _log(self, message):
  207. if self.logger:
  208. self.logger(f'[ItaPlugin] [{self.group_id}] {message}')
  209. else:
  210. print(f'[ItaPlugin] [{self.group_id}] {message}')
  211. @staticmethod
  212. def _url_for_log(raw_url: str) -> str:
  213. parsed = urlparse(str(raw_url or ""))
  214. path = parsed.path or "/"
  215. if path.startswith("/signin/") and path != "/signin/":
  216. path = "/signin/<redacted>"
  217. if (parsed.hostname or "").lower() in RADWARE_VALIDATION_HOSTS:
  218. path = "/<redacted-challenge>"
  219. if parsed.scheme and parsed.netloc:
  220. return f"{parsed.scheme}://{parsed.netloc}{path}"
  221. return path
  222. @staticmethod
  223. def _normalize_service_notice(message: str) -> str:
  224. normalized = str(message or "").replace("’", "'").replace("‘", "'")
  225. normalized = re.sub(r"\s+", " ", normalized).strip().casefold()
  226. return normalized.rstrip(".。!!")
  227. @classmethod
  228. def _classify_service_notice(
  229. cls,
  230. message: str,
  231. ) -> Optional[AvailabilityStatus]:
  232. """Classify only service notices backed by current website evidence."""
  233. normalized = cls._normalize_service_notice(message)
  234. if normalized in cls._KNOWN_NO_AVAILABILITY_NOTICES:
  235. return AvailabilityStatus.NoneAvailable
  236. return None
  237. def _random_sleep(self, min_sec=30, max_sec=60):
  238. sleep_time = random.uniform(min_sec, max_sec)
  239. self._log(f"Anti-Rate-Limit: Sleeping for {sleep_time:.2f} seconds...")
  240. time.sleep(sleep_time)
  241. def set_config(self, config: VSPlgConfig):
  242. self.config = config
  243. self.free_config = config.free_config or {}
  244. query_mode = self.free_config.get(
  245. "query_mode",
  246. SERVICES_PROBE_QUERY_MODE,
  247. )
  248. if query_mode not in {
  249. SERVICES_PROBE_QUERY_MODE,
  250. CALENDAR_QUERY_MODE,
  251. }:
  252. raise BizLogicError(
  253. "Italy query_mode must be services_probe or calendar"
  254. )
  255. otp_mode = self.free_config.get("otp_mode")
  256. if otp_mode not in (None, "manual", "cloud"):
  257. raise BizLogicError("Italy otp_mode must be manual, cloud, or omitted")
  258. if not self._book_final_configuration_valid():
  259. raise BizLogicError(
  260. "Italy final submission requires explicit OTP mode on an applicant Book configuration"
  261. )
  262. self.user_data_path = self._transient_user_data_path
  263. self._persistent_profile_path = None
  264. self._clear_calendar_query_state()
  265. self._reset_calendar_book_state()
  266. configured_profile = self.free_config.get(PERSISTENT_BROWSER_PROFILE_KEY)
  267. if configured_profile is not None:
  268. if not isinstance(configured_profile, str) or not configured_profile.strip():
  269. raise BizLogicError(
  270. "Italy persistent browser profile path must be a non-empty string"
  271. )
  272. candidate = Path(configured_profile).expanduser()
  273. if candidate.is_symlink():
  274. raise BizLogicError(
  275. "Italy persistent browser profile path must not be a symlink"
  276. )
  277. try:
  278. resolved_profile = candidate.resolve(strict=True)
  279. except OSError as exc:
  280. raise BizLogicError(
  281. "Italy persistent browser profile directory does not exist"
  282. ) from exc
  283. if not resolved_profile.is_dir():
  284. raise BizLogicError(
  285. "Italy persistent browser profile path must be a directory"
  286. )
  287. if os.name == "posix":
  288. profile_mode = stat.S_IMODE(resolved_profile.stat().st_mode)
  289. if profile_mode != 0o700:
  290. raise BizLogicError(
  291. "Italy persistent browser profile permissions must be 700"
  292. )
  293. transient_root = Path(self.root_workspace).resolve()
  294. if (
  295. resolved_profile == transient_root
  296. or resolved_profile.is_relative_to(transient_root)
  297. ):
  298. raise BizLogicError(
  299. "Italy persistent browser profile must stay outside the temporary workspace"
  300. )
  301. self._persistent_profile_path = resolved_profile
  302. self.user_data_path = str(resolved_profile)
  303. def _resolve_route(self, apt_type: AppointmentType) -> ItaRouteConfig:
  304. """Resolve the current route through the shared pure protocol core."""
  305. if not isinstance(apt_type, AppointmentType):
  306. raise BizLogicError("Italy route requires an AppointmentType")
  307. try:
  308. return resolve_route_config(
  309. self.free_config.get("apt_configs", {}),
  310. apt_type.routing_key,
  311. )
  312. except ItaProtocolError as exc:
  313. raise BizLogicError(
  314. f"Italy route configuration rejected ({exc.code})"
  315. ) from exc
  316. @staticmethod
  317. def _booking_path(route: ItaRouteConfig) -> str:
  318. return f"/Services/Booking/{route.service_id}"
  319. def _record_calendar_query_prepared_route(
  320. self,
  321. route: ItaRouteConfig,
  322. ) -> None:
  323. """Record route continuity only after a validated AVANTI transition."""
  324. if not isinstance(route, ItaRouteConfig):
  325. raise BizLogicError(
  326. "Italy Calendar preparation requires an ItaRouteConfig"
  327. )
  328. current_path, _ = same_origin_path(self.page.url)
  329. if current_path != CALENDAR_PATH:
  330. raise BizLogicError(
  331. "Italy Calendar route cannot be recorded outside Calendar"
  332. )
  333. self._calendar_query_route_key = route.routing_key
  334. def _clear_calendar_query_state(self) -> None:
  335. self._calendar_query_route_key = None
  336. self.last_calendar_query_scan = None
  337. self._calendar_query_checkpoint = None
  338. self._calendar_query_page = None
  339. self._calendar_query_generation = None
  340. self._calendar_query_tab_id = None
  341. def _invalidate_query_session(self, reason: str) -> None:
  342. # Same health -> cleanup -> creator contract as VFS/BLS. The caller
  343. # owns the retry budget; Query itself never logs in or sends an OTP.
  344. self.is_healthy = False
  345. self._query_stop_reason = reason
  346. self._clear_calendar_query_state()
  347. def _check_query_checkpoint_owner(
  348. self, apt_type: AppointmentType, checkpoint: CalendarQueryCheckpoint,
  349. ) -> None:
  350. account = self.config.account if self.config else None
  351. username = account.username.strip().casefold() if account else ""
  352. if (
  353. not isinstance(checkpoint, CalendarQueryCheckpoint)
  354. or not username
  355. or username != checkpoint.username
  356. or getattr(self, "_persistent_profile_path", None) is None
  357. or str(self._persistent_profile_path) != checkpoint.profile_path
  358. or self.user_data_path != checkpoint.profile_path
  359. or self._resolve_route(apt_type) != checkpoint.route
  360. or self.free_config.get("query_mode") != CALENDAR_QUERY_MODE
  361. or self.free_config.get("final_submit_enabled", False) is not False
  362. or getattr(self, "_calendar_book_prepared_context", None) is not None
  363. ):
  364. raise BizLogicError("Italy Query recovery account, profile, or route does not match")
  365. def capture_calendar_query_checkpoint(
  366. self, apt_type: AppointmentType,
  367. ) -> CalendarQueryCheckpoint:
  368. """Read a recovery reference only from this already-prepared Calendar."""
  369. route = self._resolve_route(apt_type)
  370. if (
  371. not self.page or not self.is_healthy or self.session_create_time <= 0
  372. or getattr(self, "_calendar_query_route_key", None) != route.routing_key
  373. or route.capability is not ItaCapability.SINGLE_SCHENGEN_TOURISM
  374. or route.calendar_scan is None
  375. ):
  376. raise BizLogicError("Italy Query recovery requires a prepared current Session")
  377. self._validate_calendar_query_url(self.page.url)
  378. guard_calendar(self.page)
  379. checkpoint = CalendarQueryCheckpoint(
  380. route=route,
  381. username=self.config.account.username.strip().casefold(),
  382. profile_path=str(getattr(self, "_persistent_profile_path", "")),
  383. calendar_url=str(self.page.url),
  384. runtime_service=read_calendar_service(self.page),
  385. )
  386. self._check_query_checkpoint_owner(apt_type, checkpoint)
  387. tab_id = self.page.tab_id
  388. if not isinstance(tab_id, str) or not tab_id:
  389. raise BizLogicError("Italy Query recovery requires a current browser tab")
  390. self._calendar_query_checkpoint = checkpoint
  391. self._calendar_query_page = self.page
  392. self._calendar_query_generation = self.session_create_time
  393. self._calendar_query_tab_id = tab_id
  394. return checkpoint
  395. @staticmethod
  396. def _validate_calendar_query_url(raw_url: str) -> None:
  397. path, parsed = same_origin_path(raw_url)
  398. query = parse_qs(parsed.query, keep_blank_values=True)
  399. if (
  400. path != CALENDAR_PATH or parsed.fragment
  401. or set(query) - {"selectedService"}
  402. or any(len(values) != 1 or not values[0] for values in query.values())
  403. ):
  404. raise BizLogicError("Italy Query recovery requires the observed Calendar URL shape")
  405. def _check_current_query_checkpoint(self, apt_type: AppointmentType) -> None:
  406. checkpoint = getattr(self, "_calendar_query_checkpoint", None)
  407. if checkpoint is None:
  408. return # Existing CS3A callers attest the same-instance route.
  409. self._check_query_checkpoint_owner(apt_type, checkpoint)
  410. if (
  411. self.page is not self._calendar_query_page
  412. or self.session_create_time != self._calendar_query_generation
  413. or self.page.tab_id != self._calendar_query_tab_id
  414. or read_calendar_service(self.page) != checkpoint.runtime_service
  415. ):
  416. raise BizLogicError("Italy Query current Calendar identity changed")
  417. def _check_calendar_access(self, booking_path: str) -> None:
  418. parsed = urlparse(str(self.page.url or ""))
  419. if parsed.hostname == "iam.esteri.it":
  420. raise SessionExpiredOrInvalidError("Italy session is no longer authenticated")
  421. if parsed.hostname in RADWARE_VALIDATION_HOSTS:
  422. raise PermissionDeniedError("Italy Calendar requires browser verification")
  423. path, _ = same_origin_path(self.page.url)
  424. if self.page.ele("#pingid-button", timeout=0.5):
  425. raise SessionExpiredOrInvalidError("Italy session is no longer authenticated")
  426. if path != CALENDAR_PATH:
  427. if path in {"/UserArea", "/Services", booking_path}:
  428. raise ItaCalendarPreparationRequiredError(
  429. "Italy Calendar requires preparation; automatic OTP is pending (ITA-OTP-01)"
  430. )
  431. raise CalendarGateError("Italy Calendar reached an unexpected page")
  432. def restore_calendar_query(
  433. self, apt_type: AppointmentType, checkpoint: CalendarQueryCheckpoint,
  434. ) -> None:
  435. """Try one observed read-only navigation after same-account login.
  436. A checkpoint only supplies comparison values. New page/route/service
  437. evidence is required before the new instance may query again.
  438. """
  439. self._clear_calendar_query_state()
  440. self._check_query_checkpoint_owner(apt_type, checkpoint)
  441. if not self.page or not self.is_healthy or self.session_create_time <= 0:
  442. raise SessionExpiredOrInvalidError("Italy Query recovery requires a new authenticated Session")
  443. self._validate_calendar_query_url(checkpoint.calendar_url)
  444. try:
  445. self.page.get(checkpoint.calendar_url)
  446. self.page.wait.doc_loaded()
  447. self._check_calendar_access(self._booking_path(checkpoint.route))
  448. guard_calendar(self.page)
  449. if read_calendar_service(self.page) != checkpoint.runtime_service:
  450. raise BizLogicError("Italy Query restored Calendar service does not match")
  451. self._record_calendar_query_prepared_route(checkpoint.route)
  452. self.capture_calendar_query_checkpoint(apt_type)
  453. self._query_stop_reason = None
  454. except SessionExpiredOrInvalidError:
  455. self._invalidate_query_session("session_expired")
  456. raise
  457. except PageDisconnectedError:
  458. self._invalidate_query_session("browser_disconnected")
  459. raise
  460. except Exception:
  461. self._clear_calendar_query_state()
  462. self._query_stop_reason = "needs_manual"
  463. raise
  464. def _reset_calendar_book_state(self) -> None:
  465. self._calendar_book_prepared_context = None
  466. self._calendar_book_attempted = False
  467. self.last_booking_precommit = None
  468. def _record_calendar_book_prepared_route(self, route: ItaRouteConfig) -> None:
  469. """Attest Book preparation after the caller validates this route's AVANTI.
  470. This private hook does not perform or infer preparation. It must not be
  471. called merely because a persisted browser happens to show Calendar.
  472. """
  473. self._calendar_book_prepared_context = None
  474. self.last_booking_precommit = None
  475. if (not isinstance(route, ItaRouteConfig)
  476. or route.capability is not ItaCapability.SINGLE_SCHENGEN_TOURISM
  477. or route.calendar_scan is None):
  478. raise BizLogicError("Italy Book preparation requires a single-tourism Calendar route")
  479. configured = self._resolve_route(AppointmentType(routing_key=route.routing_key))
  480. if route != configured or getattr(self, "_calendar_query_route_key", None) is not None:
  481. raise BizLogicError("Italy Book cannot reuse a Query preparation or another route")
  482. username = self.config.account.username.strip().casefold() if self.config else ""
  483. if (not username or not self.page or self.session_create_time <= 0
  484. or not self._book_final_configuration_valid()):
  485. raise BizLogicError("Italy Book preparation requires an authenticated user Session and final disabled")
  486. try:
  487. runtime_service = read_calendar_service(self.page)
  488. tab_id = self.page.tab_id
  489. if not isinstance(tab_id, str) or not tab_id:
  490. raise CalendarBookError("Book Calendar tab identity was unavailable")
  491. except Exception:
  492. raise BizLogicError("Italy Book preparation did not identify the current Calendar service") from None
  493. self._calendar_book_prepared_context = (
  494. id(self), self.page, username, self.session_create_time, route, runtime_service, tab_id,
  495. )
  496. self._calendar_book_attempted = False
  497. def _calendar_book_context_matches(self, route: ItaRouteConfig, username: str) -> bool:
  498. context = getattr(self, "_calendar_book_prepared_context", None)
  499. try:
  500. return (
  501. context is not None
  502. and len(context) == 7
  503. and context[1] is self.page
  504. and context[6] == self.page.tab_id
  505. and context[:5] == (id(self), self.page, username, self.session_create_time, route)
  506. and self.config.account.username.strip().casefold() == username
  507. and getattr(self, "_calendar_query_route_key", None) is None
  508. and self._book_final_configuration_valid()
  509. and self._resolve_route(AppointmentType(routing_key=route.routing_key)) == route
  510. )
  511. except Exception:
  512. return False
  513. def _book_prepared_calendar(
  514. self, slot_info: VSQueryResult, route: ItaRouteConfig, user_inputs: Dict,
  515. ) -> VSBookResult:
  516. """Recheck only the prepared applicant Session; stop before final."""
  517. if getattr(self, "_calendar_book_attempted", False):
  518. raise BizLogicError("Italy Book Calendar was already attempted; fresh preparation is required")
  519. if not isinstance(user_inputs, dict) or not isinstance(user_inputs.get("username"), str):
  520. raise BizLogicError("Italy Calendar Book requires user_inputs.username for applicant binding")
  521. username = user_inputs["username"].strip().casefold()
  522. if not username or not self._calendar_book_context_matches(route, username):
  523. raise BizLogicError("Italy Book Calendar preparation does not match the current applicant and route")
  524. try:
  525. start_day, end_day = book_date_preferences(user_inputs)
  526. if read_calendar_service(self.page) != self._calendar_book_prepared_context[5]:
  527. raise CalendarBookError("Book Calendar no longer matches the prepared service")
  528. self._calendar_book_attempted = True
  529. approval = recheck_calendar_before_final(
  530. self.page,
  531. lookahead_months=route.calendar_scan.lookahead_months,
  532. start_day=start_day, end_day=end_day,
  533. preferred_day=slot_info.earliest_date.date() if slot_info.earliest_date else None,
  534. session_is_current=lambda: self._calendar_book_context_matches(route, username),
  535. log=self._log,
  536. )
  537. if not self._calendar_book_context_matches(route, username):
  538. raise CalendarBookError("Book Session changed before publishing the pre-final checkpoint")
  539. except CalendarGateError as exc:
  540. self._calendar_book_prepared_context = None
  541. self.last_booking_precommit = None
  542. if getattr(exc, "http_status", None) == 401 or urlparse(str(self.page.url or "")).hostname == "iam.esteri.it":
  543. raise SessionExpiredOrInvalidError("Italy Book Session is no longer authenticated") from None
  544. if getattr(exc, "http_status", None) == 429:
  545. raise RateLimiteddError("Italy Book Calendar is rate limited") from None
  546. raise BizLogicError(str(exc)) from None
  547. except Exception:
  548. self._calendar_book_prepared_context = None
  549. self.last_booking_precommit = None
  550. raise BizLogicError("Italy Book Calendar failed before final confirmation") from None
  551. self.last_booking_precommit = approval
  552. result = VSBookResult(success=False)
  553. if approval is None:
  554. result.label = BOOKING_RECHECK_NONE_AVAILABLE_LABEL
  555. else:
  556. result.label = BOOKING_CALENDAR_PREFINAL_STOP_LABEL
  557. self._log(
  558. "Italy Book selected current user Calendar slot "
  559. f"date={approval.candidate.day.isoformat()} time={approval.candidate.time_label}; "
  560. "stopped before final confirmation."
  561. )
  562. return result
  563. def _acquire_persistent_profile_lock(self) -> None:
  564. profile_path = self._persistent_profile_path
  565. if profile_path is None or self._profile_lock_handle is not None:
  566. return
  567. if fcntl is None:
  568. raise BizLogicError(
  569. "Italy persistent browser profiles require POSIX file locking"
  570. )
  571. lock_path = profile_path / ".coordinate-profile.lock"
  572. try:
  573. lock_handle = lock_path.open("a+", encoding="utf-8")
  574. lock_path.chmod(0o600)
  575. fcntl.flock(lock_handle.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
  576. except BlockingIOError as exc:
  577. try:
  578. lock_handle.close()
  579. except UnboundLocalError:
  580. pass
  581. raise BizLogicError(
  582. "Italy persistent browser profile is already in use"
  583. ) from exc
  584. except OSError as exc:
  585. try:
  586. lock_handle.close()
  587. except UnboundLocalError:
  588. pass
  589. raise BizLogicError(
  590. f"Cannot lock Italy persistent browser profile ({type(exc).__name__})"
  591. ) from exc
  592. self._profile_lock_handle = lock_handle
  593. def _release_persistent_profile_lock(self) -> None:
  594. lock_handle = getattr(self, "_profile_lock_handle", None)
  595. if lock_handle is None:
  596. return
  597. try:
  598. if fcntl is not None:
  599. fcntl.flock(lock_handle.fileno(), fcntl.LOCK_UN)
  600. finally:
  601. lock_handle.close()
  602. self._profile_lock_handle = None
  603. def keep_alive(self):
  604. pass
  605. def health_check(self) -> bool:
  606. if not self.is_healthy:
  607. return False
  608. if not self.page:
  609. self._invalidate_query_session("browser_disconnected")
  610. return False
  611. try:
  612. if not self.page.run_js("return 1;"):
  613. self._invalidate_query_session("browser_disconnected")
  614. return False
  615. except Exception:
  616. self._invalidate_query_session("browser_disconnected")
  617. return False
  618. if self.config.session_max_life > 0:
  619. if time.time() - self.session_create_time > self.config.session_max_life:
  620. self._log("Session expired.")
  621. self._invalidate_query_session("configured_lifetime_reached")
  622. return False
  623. return True
  624. def create_session(self):
  625. """
  626. 创建认证浏览器会话,并在登录后的 /UserArea 停止。
  627. """
  628. self._log(f"Initializing Session (ID: {self.instance_id})...")
  629. self._cleanup_attempted = False
  630. self.is_healthy = False
  631. self._query_stop_reason = None
  632. self._clear_calendar_query_state()
  633. self._reset_calendar_book_state()
  634. co = ChromiumOptions()
  635. def get_free_port():
  636. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  637. s.bind(('', 0))
  638. return s.getsockname()[1]
  639. debug_port = get_free_port()
  640. self._log(f"Assigned Debug Port: {debug_port}")
  641. self._log(f"Account id={self.config.account.id}, proxy id={self.config.proxy.id}")
  642. self._acquire_persistent_profile_lock()
  643. if self._persistent_profile_path is not None:
  644. self._log(
  645. "Browser profile mode=persistent "
  646. f"id={self._persistent_profile_path.name}"
  647. )
  648. else:
  649. self._log("Browser profile mode=transient")
  650. co.set_local_port(debug_port)
  651. co.set_user_data_path(self.user_data_path)
  652. chrome_path = configure.CHROME_PATH
  653. if not chrome_path:
  654. chrome_path = os.getenv("CHROME_BIN")
  655. if chrome_path and os.path.exists(chrome_path):
  656. co.set_paths(browser_path=chrome_path)
  657. if self.config.proxy and self.config.proxy.ip:
  658. p = self.config.proxy
  659. if p.username and p.password:
  660. self._log(f"Starting Proxy Tunnel for {p.ip}...")
  661. exit_node = {
  662. "name": "ExitNode",
  663. "type": p.proto,
  664. "server": p.ip,
  665. "port": p.port,
  666. "username": p.username,
  667. "password": p.password
  668. }
  669. relay_node = None
  670. if configure.MIHOMO_RELAY_NODES:
  671. relay_node = random.choice(configure.MIHOMO_RELAY_NODES)
  672. mihomo_path = configure.MIHOMO_BIN_PATH
  673. if not mihomo_path:
  674. mihomo_path = os.getenv("MIHOMO_BIN")
  675. if not mihomo_path:
  676. raise BizLogicError(message='Mihomo path is null, You need set mihomo bin path in configure or os env')
  677. self.tunnel = MihomoTunnel(mihomo_path, exit_node=exit_node, relay_node=relay_node)
  678. local_proxy = self.tunnel.start()
  679. self._log(f"Tunnel started at {local_proxy}")
  680. co.set_argument(f'--proxy-server={local_proxy}')
  681. else:
  682. proxy_str = f"{p.proto}://{p.ip}:{p.port}"
  683. co.set_argument(f'--proxy-server={proxy_str}')
  684. else:
  685. self._log("[WARN] No proxy configured!")
  686. specific_fp = FingerprintGenerator().generate(self.config.account.username)
  687. fp_seed = specific_fp.get("seed")
  688. fp_platform = specific_fp.get("platform")
  689. fp_brand = specific_fp.get("brand")
  690. self._log(f'browser fingerprint seed={fp_seed}')
  691. co.headless(False)
  692. co.set_argument('--no-sandbox')
  693. co.set_argument('--disable-dev-shm-usage')
  694. co.set_argument('--window-size=1920,1080')
  695. co.set_argument('--disable-blink-features=AutomationControlled')
  696. co.set_argument(f"--fingerprint={fp_seed}")
  697. co.set_argument(f"--fingerprint-platform={fp_platform}")
  698. co.set_argument(f"--fingerprint-brand={fp_brand}")
  699. self.page = ChromiumPage(co)
  700. if self.config.debug:
  701. self.page.get('https://example.com')
  702. js_script = """
  703. function getFingerprint() {
  704. let webglVendor = 'Unknown';
  705. let webglRenderer = 'Unknown';
  706. try {
  707. let canvas = document.createElement('canvas');
  708. let gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
  709. if (gl) {
  710. let debugInfo = gl.getExtension('WEBGL_debug_renderer_info');
  711. if (debugInfo) {
  712. webglVendor = gl.getParameter(debugInfo.UNMASKED_VENDOR_WEBGL);
  713. webglRenderer = gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL);
  714. }
  715. }
  716. } catch(e) {}
  717. return {
  718. "User-Agent": navigator.userAgent,
  719. "Platform": navigator.userAgentData ? navigator.userAgentData.platform : navigator.platform,
  720. "Brands": navigator.userAgentData ? navigator.userAgentData.brands.map(b => b.brand).join(', ') : 'Not Supported',
  721. "CPU Cores": navigator.hardwareConcurrency,
  722. "Language": navigator.language,
  723. "Timezone": Intl.DateTimeFormat().resolvedOptions().timeZone,
  724. "WebGL Vendor": webglVendor,
  725. "WebGL Renderer": webglRenderer
  726. };
  727. }
  728. return getFingerprint();
  729. """
  730. fp_data = self.page.run_js(js_script)
  731. self._log("================ 预检浏览器指纹数据 ================")
  732. self._log(json.dumps(fp_data, indent=4, ensure_ascii=False))
  733. self._log("====================================================")
  734. self.page = ChromiumPage(co)
  735. ita_url = self.ita_url
  736. self._log(f"Navigating to {ita_url}")
  737. self.page.get(ita_url)
  738. self._log("Init humanize tools...")
  739. self.mouse = HumanMouse(self.page, debug=True)
  740. self.keyboard = HumanKeyboard(self.page)
  741. self._log("Random mouse start position...")
  742. viewport_width = self.page.rect.viewport_size[0]
  743. viewport_height = self.page.rect.viewport_size[1]
  744. init_x = random.randint(10, viewport_width - 10)
  745. init_y = random.randint(10, viewport_height - 10)
  746. self.mouse.move(init_x, init_y)
  747. max_steps = 15
  748. stuck_counter = 0
  749. last_url = ""
  750. session_created = False
  751. has_clicked_login = False
  752. has_submitted_login = False
  753. username = self.config.account.username
  754. password = self.config.account.password
  755. for step in range(max_steps):
  756. self.page.wait.doc_loaded()
  757. time.sleep(1)
  758. current_url = self.page.url
  759. current_parsed_url = urlparse(current_url)
  760. current_host = (current_parsed_url.hostname or "").lower()
  761. current_path = urlparse(current_url).path.rstrip("/") or "/"
  762. current_title = self.page.title.lower()
  763. current_html_content = self.page.html
  764. self._log(
  765. f"--- [Router Step {step+1}] Current URL: "
  766. f"{self._url_for_log(current_url)} ---"
  767. )
  768. if current_host in RADWARE_VALIDATION_HOSTS:
  769. raise ItaAuthenticationChallengeError(
  770. "Italy authentication requires Radware browser validation; "
  771. "automatic retry is disabled"
  772. )
  773. # --- [异常处理与反爬对抗层] ---
  774. if current_url == last_url:
  775. stuck_counter += 1
  776. else:
  777. last_url = current_url
  778. stuck_counter = 0
  779. if stuck_counter >= 3:
  780. if current_path == "/UserArea":
  781. # The URL can stay stable while authenticated DOM finishes
  782. # rendering; let the success predicate below inspect it.
  783. pass
  784. elif has_clicked_login or has_submitted_login:
  785. self._log(
  786. "[WARN] Authentication flow is still on the same page; "
  787. "waiting without refresh or repeated submission."
  788. )
  789. time.sleep(2)
  790. continue
  791. self._log("[WARN] Page stucked before authentication, try to refresh...")
  792. self.page.refresh()
  793. self._random_sleep(5, 8)
  794. stuck_counter = 0
  795. continue
  796. server_error_indicators = ["502 Bad Gateway", "503 Service Temporarily Unavailable", "error 1020"]
  797. if any(err in current_html_content for err in server_error_indicators):
  798. if has_clicked_login or has_submitted_login:
  799. raise BizLogicError(
  800. "Italy authentication reached a server error; "
  801. "refusing to refresh or repeat the login attempt"
  802. )
  803. self._log(f"[WARN] Server network error, try to refresh (Step: {step})...")
  804. self.page.refresh()
  805. self._random_sleep(10, 15)
  806. continue
  807. # Cloudflare 拦截
  808. cloudflare_blocked_indicators = [
  809. "Sorry, you have been blocked",
  810. "You are being rate limited",
  811. "Cloudflare Ray ID"
  812. ]
  813. if any(indicator in current_html_content for indicator in cloudflare_blocked_indicators):
  814. raise BizLogicError(message="Blocked by Cloudflare WAF. Need to change IP or browser fingerprint.")
  815. # 遇到五秒盾先绕盾 (保留你原有的逻辑)
  816. if "just a moment" in current_title or "cloudflare" in current_title:
  817. # 假设你有 cf_bypasser 实例
  818. # if not cf_bypasser.bypass(max_retry=3): continue
  819. self._log("[State] CF 5-second shield detected. Waiting...")
  820. time.sleep(5)
  821. continue
  822. # 状态 0: 登录成功后的用户控制台。
  823. if (
  824. current_path == "/UserArea"
  825. and self.page.ele('.app-menu', timeout=1)
  826. and self.page.ele('@href=/Services', timeout=1)
  827. ):
  828. self._log("Successfully reached authenticated /UserArea.")
  829. self.session_create_time = time.time()
  830. self.is_healthy = True
  831. session_created = True
  832. break
  833. # 状态 1: 登录前的初始首页
  834. elif self.page.ele('#pingid-button', timeout=1):
  835. if has_clicked_login:
  836. self._log(
  837. "[State] Login entry was already clicked; "
  838. "waiting without clicking it again."
  839. )
  840. time.sleep(2)
  841. continue
  842. self._log("[State] Initial Landing Page detected. Clicking login button...")
  843. login_btn = self.page.ele('#pingid-button')
  844. self.mouse.human_click_ele(login_btn)
  845. has_clicked_login = True
  846. self._log("Redirecting to PingID...")
  847. self._random_sleep(3, 5)
  848. continue
  849. # 状态 2: 真正的表单登录页 (PingID)
  850. elif self.page.ele('@name=callback_1', timeout=1):
  851. if has_submitted_login:
  852. self._log(
  853. "[State] Login form is still present after submission; "
  854. "waiting without resubmitting credentials."
  855. )
  856. time.sleep(2)
  857. continue
  858. self._log("[State] PingID Login Form detected. Submitting credentials...")
  859. user_input = self.page.ele('@name=callback_1')
  860. pwd_input = self.page.ele('@name=callback_2')
  861. submit_btn = self.page.ele('tag:button@type=submit')
  862. if not user_input or not pwd_input or not submit_btn:
  863. raise BizLogicError(
  864. "Italy IAM login form is missing callback_1, "
  865. "callback_2, or the submit button"
  866. )
  867. self._log("Entering username...")
  868. self.mouse.human_click_ele(user_input)
  869. user_input.input(username, clear=True)
  870. time.sleep(1)
  871. self._log("Entering password...")
  872. self.mouse.human_click_ele(pwd_input)
  873. pwd_input.input(password, clear=True)
  874. time.sleep(1)
  875. self._log("Clicking submit login button...")
  876. self.mouse.human_click_ele(submit_btn)
  877. has_submitted_login = True
  878. self._log("Login form submitted. Waiting for dashboard to load...")
  879. self._random_sleep(5, 7)
  880. continue
  881. # /UserArea 只有在认证 DOM 同时就绪时才能成功。
  882. elif current_path == "/UserArea":
  883. self._log(
  884. "[State] /UserArea loaded without complete authentication "
  885. "markers; waiting."
  886. )
  887. time.sleep(2)
  888. continue
  889. else:
  890. self._log("[State] In unknown or transitional state. Waiting for next polling cycle...")
  891. time.sleep(2)
  892. if not session_created:
  893. raise BizLogicError(
  894. f"Failed to create authenticated /UserArea session after "
  895. f"{max_steps} navigation steps. Stuck at: "
  896. f"{self._url_for_log(self.page.url)}"
  897. )
  898. # -------------------------------------------------------------
  899. # 2. Query Availability
  900. # -------------------------------------------------------------
  901. def _inspect_service_list_structure(
  902. self,
  903. booking_path: str,
  904. ) -> Optional[Dict[str, Any]]:
  905. """Return counts, including known notices; never export row text or IDs."""
  906. if not self.page:
  907. return None
  908. script = SERVICE_LIST_DIAGNOSTIC_SCRIPT.replace(
  909. "__TARGET_PATH__",
  910. booking_path,
  911. )
  912. raw = self.page.run_js(script)
  913. if not isinstance(raw, dict):
  914. self.last_service_list_structure = None
  915. return None
  916. def bounded_count(value: object) -> int:
  917. try:
  918. parsed = int(value)
  919. except (TypeError, ValueError):
  920. parsed = 0
  921. return max(0, min(parsed, 10000))
  922. path = str(raw.get("path") or "").strip()[:120] or None
  923. ready_state = str(raw.get("ready_state") or "").strip().lower()
  924. if ready_state not in {"loading", "interactive", "complete"}:
  925. ready_state = "unknown"
  926. structure = {
  927. "path": path,
  928. "ready_state": ready_state,
  929. "table_count": bounded_count(raw.get("table_count")),
  930. "row_count": bounded_count(raw.get("row_count")),
  931. "booking_link_count": bounded_count(
  932. raw.get("booking_link_count")
  933. ),
  934. "target_link_count": bounded_count(raw.get("target_link_count")),
  935. "already_booked_notice_row_count": (
  936. bounded_count(raw["already_booked_notice_row_count"])
  937. if type(raw.get("already_booked_notice_row_count")) is int else 0
  938. ),
  939. }
  940. self.last_service_list_structure = structure
  941. return structure
  942. def query(self, apt_type: AppointmentType) -> VSQueryResult:
  943. """Preserve the framework's typed failures and unhealthy handoff."""
  944. self.last_calendar_query_scan = None
  945. if getattr(self, "is_healthy", True) is False:
  946. raise SessionExpiredOrInvalidError("Italy Query cannot reuse an unhealthy Session")
  947. try:
  948. if (self.page and self.free_config.get("otp_mode") in {"manual", "cloud"}
  949. and self.free_config.get("query_mode") == CALENDAR_QUERY_MODE
  950. and urlparse(self.page.url or "").hostname == "prenotami.esteri.it"):
  951. from plugins.ita_workflow import prepare_query_if_needed
  952. entry = prepare_query_if_needed(self, apt_type)
  953. if entry is not None:
  954. return entry
  955. return self._query_once(apt_type)
  956. except SessionExpiredOrInvalidError:
  957. self._invalidate_query_session("session_expired")
  958. raise
  959. except PageDisconnectedError:
  960. self._invalidate_query_session("browser_disconnected")
  961. raise
  962. except RateLimiteddError:
  963. self._invalidate_query_session("rate_limited")
  964. raise
  965. except PermissionDeniedError:
  966. self._invalidate_query_session("needs_manual")
  967. raise
  968. except ItaCalendarPreparationRequiredError:
  969. self._clear_calendar_query_state()
  970. self._query_stop_reason = "needs_manual"
  971. raise
  972. def _query_once(self, apt_type: AppointmentType, *, query_mode=None) -> VSQueryResult:
  973. # Diagnostic evidence is per call, never a reusable availability cache.
  974. self.last_calendar_query_scan = None
  975. if not self.page:
  976. raise BizLogicError("Italy query requires an initialized browser session")
  977. route = self._resolve_route(apt_type)
  978. query_mode = query_mode or self.free_config.get(
  979. "query_mode",
  980. SERVICES_PROBE_QUERY_MODE,
  981. )
  982. current_url = self.page.url or ""
  983. current_parsed_url = urlparse(current_url)
  984. current_path = current_parsed_url.path.rstrip("/") or "/"
  985. if query_mode == CALENDAR_QUERY_MODE:
  986. if route.capability is not ItaCapability.SINGLE_SCHENGEN_TOURISM:
  987. raise BizLogicError(
  988. "Italy calendar query requires the "
  989. "single_schengen_tourism capability"
  990. )
  991. if (
  992. current_parsed_url.netloc.lower() == "iam.esteri.it"
  993. or self.page.ele("#pingid-button", timeout=0.5)
  994. ):
  995. raise SessionExpiredOrInvalidError(
  996. "Italy session is no longer authenticated"
  997. )
  998. same_origin_path(current_url)
  999. if current_path != CALENDAR_PATH:
  1000. raise ItaCalendarPreparationRequiredError(
  1001. "Italy calendar query requires a route-confirmed "
  1002. "Calendar Session; preparation is not complete (ITA-OTP-01)"
  1003. )
  1004. if getattr(self, "_calendar_query_route_key", None) != route.routing_key:
  1005. raise BizLogicError(
  1006. "Italy calendar query route continuity is not confirmed"
  1007. )
  1008. self._check_current_query_checkpoint(apt_type)
  1009. return self._query_calendar(apt_type, route)
  1010. service_id = route.service_id
  1011. result = VSQueryResult(
  1012. success=False,
  1013. apt_type=apt_type,
  1014. availability_status=AvailabilityStatus.NoneAvailable,
  1015. )
  1016. self.last_service_list_structure = None
  1017. services_path = "/Services"
  1018. services_url = f"{self.ita_url}{services_path}"
  1019. booking_path = self._booking_path(route)
  1020. if current_path not in {"/UserArea", services_path, booking_path}:
  1021. if (
  1022. current_parsed_url.netloc.lower() == "iam.esteri.it"
  1023. or self.page.ele("#pingid-button", timeout=0.5)
  1024. ):
  1025. raise SessionExpiredOrInvalidError(
  1026. "Italy session is no longer authenticated"
  1027. )
  1028. raise BizLogicError(
  1029. "Italy query expected /UserArea or /Services, "
  1030. f"got {current_path}"
  1031. )
  1032. # Always reload Services before probing. The no-slot response leaves a
  1033. # modal over the service list, so reusing the old DOM can block the next
  1034. # Sentinel iteration even though the URL is still /Services.
  1035. self.page.get(services_url)
  1036. self.page.wait.doc_loaded()
  1037. current_url = self.page.url or ""
  1038. current_parsed_url = urlparse(current_url)
  1039. current_path = current_parsed_url.path.rstrip("/") or "/"
  1040. if (
  1041. current_parsed_url.netloc.lower() == "iam.esteri.it"
  1042. or self.page.ele("#pingid-button", timeout=0.5)
  1043. ):
  1044. raise SessionExpiredOrInvalidError(
  1045. "Italy session expired while loading the service list"
  1046. )
  1047. if current_path != services_path:
  1048. raise BizLogicError(
  1049. f"Italy service-list navigation reached {current_path}"
  1050. )
  1051. self._log(f"Checking configured Italy service id={service_id}...")
  1052. booking_link = self.page.ele(
  1053. f"tag:a@@href={booking_path}",
  1054. timeout=SERVICE_LINK_WAIT_SECONDS,
  1055. )
  1056. if not booking_link:
  1057. structure = self._inspect_service_list_structure(booking_path)
  1058. if structure is not None:
  1059. self._log(
  1060. "Italy service-list structure after missing target link: "
  1061. + json.dumps(structure, sort_keys=True)
  1062. )
  1063. message = (
  1064. "Italy target service link is missing after the bounded wait: "
  1065. f"{booking_path}"
  1066. )
  1067. if structure and structure["already_booked_notice_row_count"] > 0:
  1068. message += (
  1069. "; Services contains an already-booked notice "
  1070. "(Prenotazione già effettuata per questo servizio). "
  1071. "Check the target service row and existing reservations; "
  1072. "the notice is not bound to the configured service ID."
  1073. )
  1074. raise ItaTargetServiceLinkMissingError(message)
  1075. self.mouse.human_click_ele(booking_link)
  1076. self.page.wait.load_start()
  1077. self.page.wait.doc_loaded()
  1078. current_url = self.page.url or ""
  1079. current_parsed_url = urlparse(current_url)
  1080. current_path = current_parsed_url.path.rstrip("/") or "/"
  1081. if (
  1082. current_parsed_url.netloc.lower() == "iam.esteri.it"
  1083. or self.page.ele("#pingid-button", timeout=0.5)
  1084. ):
  1085. raise SessionExpiredOrInvalidError(
  1086. "Italy session expired while checking the target service"
  1087. )
  1088. not_available = self.page.ele("#WNotAvailable", timeout=2)
  1089. not_available_message = ""
  1090. if not_available:
  1091. not_available_message = (
  1092. not_available.attr("value")
  1093. or getattr(not_available, "value", "")
  1094. or ""
  1095. ).strip()
  1096. visible_notice = self.page.ele(".jconfirm-content", timeout=2)
  1097. visible_notice_message = ""
  1098. if visible_notice:
  1099. visible_notice_message = (visible_notice.text or "").strip()
  1100. visible_notice_status = self._classify_service_notice(
  1101. visible_notice_message
  1102. )
  1103. if current_path == services_path and not_available_message:
  1104. self._log("Target Italy service explicitly reported no availability.")
  1105. return result
  1106. if (
  1107. current_path == services_path
  1108. and visible_notice_status == AvailabilityStatus.NoneAvailable
  1109. ):
  1110. self._log(
  1111. "Target Italy service showed the verified no-availability notice."
  1112. )
  1113. return result
  1114. if current_path == services_path:
  1115. if visible_notice_message:
  1116. raise BizLogicError(
  1117. "Italy service returned to /Services with an unrecognized "
  1118. "visible service notice"
  1119. )
  1120. raise BizLogicError(
  1121. "Italy service returned to /Services without a non-empty "
  1122. "#WNotAvailable marker or a recognized no-availability notice"
  1123. )
  1124. booking_form = self.page.ele("#bookingForm", timeout=1)
  1125. if current_path == booking_path and booking_form:
  1126. self._log(
  1127. "Target Italy service accepted entry; returning a weak signal "
  1128. "without form, calendar, or OTP actions."
  1129. )
  1130. result.success = True
  1131. return result
  1132. if current_path == booking_path:
  1133. raise BizLogicError(
  1134. "Italy target service reached the booking path without #bookingForm"
  1135. )
  1136. if booking_form:
  1137. raise BizLogicError(
  1138. "Italy #bookingForm appeared on an unexpected path"
  1139. )
  1140. raise BizLogicError(
  1141. f"Italy query reached an unrecognized path: {current_path}"
  1142. )
  1143. def _query_calendar(
  1144. self,
  1145. apt_type: AppointmentType,
  1146. route: ItaRouteConfig,
  1147. ) -> VSQueryResult:
  1148. """Publish only the earliest day from the first positive month."""
  1149. if route.calendar_scan is None:
  1150. raise BizLogicError(
  1151. "Italy calendar query route is missing calendar_scan"
  1152. )
  1153. try:
  1154. scan = scan_until_first_available_month(
  1155. self.page,
  1156. lookahead_months=route.calendar_scan.lookahead_months,
  1157. log=self._log,
  1158. )
  1159. except CalendarAuthenticationExpiredError:
  1160. raise SessionExpiredOrInvalidError("Italy Calendar authentication expired") from None
  1161. except CalendarRateLimitedError:
  1162. raise RateLimiteddError("Italy Calendar request was rate limited") from None
  1163. except CalendarAccessDeniedError:
  1164. raise PermissionDeniedError("Italy Calendar access requires operator attention") from None
  1165. except (CalendarTransientError, TimeoutError):
  1166. # A refresh may have returned to IAM without producing the XHR
  1167. # being listened for. Inspect current page before calling it a
  1168. # transient network error; never repeat preparation here.
  1169. self._check_calendar_access(self._booking_path(route))
  1170. raise ItaCalendarTransientError("Italy Calendar read temporarily failed") from None
  1171. except CalendarGateError as exc:
  1172. raise BizLogicError(
  1173. "Italy Calendar query failed closed"
  1174. ) from exc
  1175. self._check_current_query_checkpoint(apt_type)
  1176. self.last_calendar_query_scan = scan
  1177. result = VSQueryResult(
  1178. success=False,
  1179. apt_type=apt_type,
  1180. availability_status=AvailabilityStatus.NoneAvailable,
  1181. )
  1182. earliest = scan.earliest_available_date
  1183. if earliest is None:
  1184. self._log(
  1185. "Italy Calendar scan completed without a positive month."
  1186. )
  1187. return result
  1188. earliest_dt = datetime.combine(earliest, datetime.min.time())
  1189. result.success = True
  1190. result.availability_status = AvailabilityStatus.Available
  1191. result.earliest_date = earliest_dt
  1192. result.availability = [
  1193. DateAvailability(date=earliest_dt, times=[])
  1194. ]
  1195. self._log(
  1196. "Italy Calendar found the first positive month; "
  1197. f"earliest_date={earliest.isoformat()}."
  1198. )
  1199. return result
  1200. # -------------------------------------------------------------
  1201. # 3. Book
  1202. # -------------------------------------------------------------
  1203. def _inspect_booking_form_structure(
  1204. self,
  1205. booking_path: str,
  1206. ) -> Dict[str, Any]:
  1207. """Read structural metadata only; never read values or submit the form."""
  1208. if not self.page:
  1209. raise BizLogicError(
  1210. "Italy booking-form inspection requires an initialized browser session"
  1211. )
  1212. raw = self.page.run_js(BOOKING_FORM_DISCOVERY_SCRIPT)
  1213. if not isinstance(raw, dict):
  1214. raise BizLogicError(
  1215. "Italy booking form disappeared before structural inspection"
  1216. )
  1217. raw_form = raw.get("form") if isinstance(raw.get("form"), dict) else {}
  1218. raw_fields = raw.get("fields") if isinstance(raw.get("fields"), list) else []
  1219. raw_buttons = (
  1220. raw.get("buttons") if isinstance(raw.get("buttons"), list) else []
  1221. )
  1222. def metadata_text(value: object) -> Optional[str]:
  1223. if value is None:
  1224. return None
  1225. text = re.sub(r"\s+", " ", str(value)).strip()
  1226. return text[:120] or None
  1227. def bounded_count(value: object, fallback: int, maximum: int) -> int:
  1228. try:
  1229. parsed = int(value)
  1230. except (TypeError, ValueError):
  1231. parsed = fallback
  1232. return max(0, min(parsed, maximum))
  1233. structure = {
  1234. "path": metadata_text(raw.get("path")),
  1235. "form": {
  1236. "method": metadata_text(raw_form.get("method")),
  1237. "action_path": metadata_text(raw_form.get("action_path")),
  1238. },
  1239. "field_count": bounded_count(
  1240. raw.get("field_count"), len(raw_fields), 10000
  1241. ),
  1242. "fields_truncated": bool(
  1243. raw.get("fields_truncated") or len(raw_fields) > 100
  1244. ),
  1245. "fields": [
  1246. {
  1247. "tag": metadata_text(item.get("tag")),
  1248. "type": metadata_text(item.get("type")),
  1249. "id": metadata_text(item.get("id")),
  1250. "name": metadata_text(item.get("name")),
  1251. "required": bool(item.get("required")),
  1252. }
  1253. for item in raw_fields[:100]
  1254. if isinstance(item, dict)
  1255. ],
  1256. "button_count": bounded_count(
  1257. raw.get("button_count"), len(raw_buttons), 10000
  1258. ),
  1259. "buttons_truncated": bool(
  1260. raw.get("buttons_truncated") or len(raw_buttons) > 50
  1261. ),
  1262. "buttons": [
  1263. {
  1264. "tag": metadata_text(item.get("tag")),
  1265. "type": metadata_text(item.get("type")),
  1266. "id": metadata_text(item.get("id")),
  1267. "name": metadata_text(item.get("name")),
  1268. "text": metadata_text(item.get("text")),
  1269. }
  1270. for item in raw_buttons[:50]
  1271. if isinstance(item, dict)
  1272. ],
  1273. }
  1274. self.last_booking_form_structure = structure
  1275. if structure["path"] != booking_path:
  1276. raise BizLogicError(
  1277. "Italy booking form appeared on an unexpected path during Book recheck"
  1278. )
  1279. if not structure["form"]["method"] or not structure["form"]["action_path"]:
  1280. raise BizLogicError(
  1281. "Italy booking form is missing method or action metadata"
  1282. )
  1283. return structure
  1284. def book(self, slot_info: VSQueryResult, user_inputs: Dict = None) -> VSBookResult:
  1285. if self.free_config.get("otp_mode") in {"manual", "cloud"}:
  1286. from plugins.ita_workflow import book
  1287. if not self.page or not isinstance(slot_info, VSQueryResult):
  1288. raise BizLogicError("Italy Book requires a browser Session and Query trigger")
  1289. return book(self, slot_info, user_inputs)
  1290. return self._book_discovery_or_prepared(slot_info, user_inputs)
  1291. def _book_final_configuration_valid(self) -> bool:
  1292. value = self.free_config.get("final_submit_enabled", False)
  1293. return value is False or (
  1294. value is True and self.free_config.get("otp_mode") in ("manual", "cloud")
  1295. and self.free_config.get("query_mode", SERVICES_PROBE_QUERY_MODE) == SERVICES_PROBE_QUERY_MODE
  1296. )
  1297. def _book_discovery_or_prepared(self, slot_info: VSQueryResult, user_inputs: Dict = None) -> VSBookResult:
  1298. """Recheck the user's Session; prepared Calendar stops before final."""
  1299. res = VSBookResult()
  1300. res.success = False
  1301. self.last_booking_form_structure = None
  1302. self.last_booking_precommit = None
  1303. if not self.page:
  1304. raise BizLogicError("Italy Book requires an initialized browser session")
  1305. if not isinstance(slot_info, VSQueryResult):
  1306. raise BizLogicError("Italy Book requires a VSQueryResult trigger")
  1307. self._log(
  1308. "Rechecking the target Italy service in the user's authenticated Session..."
  1309. )
  1310. route = self._resolve_route(slot_info.apt_type)
  1311. current_path = urlparse(str(self.page.url or "")).path.rstrip("/") or "/"
  1312. if (current_path == CALENDAR_PATH
  1313. or getattr(self, "_calendar_book_prepared_context", None) is not None):
  1314. return self._book_prepared_calendar(slot_info, route, user_inputs)
  1315. recheck = self.query(slot_info.apt_type)
  1316. if not recheck.success:
  1317. self._log("Target Italy service is no longer available for this user Session.")
  1318. res.label = BOOKING_RECHECK_NONE_AVAILABLE_LABEL
  1319. return res
  1320. self._inspect_booking_form_structure(self._booking_path(route))
  1321. res.label = BOOKING_FORM_DISCOVERY_STOP_LABEL
  1322. self._log(
  1323. "Italy booking form reached; stopped before AVANTI for field verification."
  1324. )
  1325. return res
  1326. # --- 资源清理核心方法 ---
  1327. def cleanup(self):
  1328. """
  1329. 销毁浏览器并彻底删除临时文件
  1330. """
  1331. self._cleanup_attempted = True
  1332. cleanup_failed = False
  1333. # 1. 关闭浏览器
  1334. if self.page:
  1335. try:
  1336. self.page.quit() # 这会关闭 Chrome 进程
  1337. except Exception:
  1338. # DrissionPage already handles an ordinary disconnection in
  1339. # quit(). An additional failure must not silently permit a
  1340. # second browser to take ownership of this persistent profile.
  1341. cleanup_failed = True
  1342. self.page = None
  1343. self.is_healthy = False
  1344. self._clear_calendar_query_state()
  1345. self._reset_calendar_book_state()
  1346. # 2. 删除文件
  1347. # 注意:Chrome 关闭后可能需要几百毫秒释放文件锁,稍微等待
  1348. if os.path.exists(self.root_workspace):
  1349. for _ in range(3):
  1350. try:
  1351. time.sleep(0.2)
  1352. shutil.rmtree(self.root_workspace, ignore_errors=True)
  1353. break
  1354. except Exception as e:
  1355. # 如果删除失败(通常是Windows文件占用),重试
  1356. self._log(f"Cleanup retry: {e}")
  1357. time.sleep(0.5)
  1358. # 如果依然存在,打印警告(虽然 ignore_errors=True 会掩盖报错,但可以 check exists)
  1359. if os.path.exists(self.root_workspace):
  1360. self._log(f"[WARN] Failed to fully remove workspace: {self.root_workspace}")
  1361. cleanup_failed = True
  1362. # 3. [新增] 关闭代理隧道
  1363. if self.tunnel:
  1364. try: self.tunnel.stop()
  1365. except Exception: cleanup_failed = True
  1366. self.tunnel = None
  1367. self._release_persistent_profile_lock()
  1368. if cleanup_failed:
  1369. raise BizLogicError("Italy cleanup did not complete; automatic recreation is unsafe")
  1370. def get_plugin_status(self) -> PluginStatus:
  1371. account_name = self.config.account.username if self.config and self.config.account else ""
  1372. proxy_info = f"{self.config.proxy.ip}:{self.config.proxy.port}" if self.config and self.config.proxy and self.config.proxy.ip else ""
  1373. current_url = ""
  1374. if self.page:
  1375. try:
  1376. current_url = self.page.url
  1377. except Exception:
  1378. pass
  1379. state = getattr(self, "_query_stop_reason", None) or ("running" if self.is_healthy else "unhealthy")
  1380. return PluginStatus(
  1381. instance_id=self.instance_id,
  1382. is_healthy=self.is_healthy,
  1383. state=state,
  1384. current_url=self._url_for_log(current_url) if current_url else "",
  1385. account=account_name,
  1386. proxy=proxy_info,
  1387. session_age_sec=0,
  1388. last_active_at=utc_now_str(),
  1389. )
  1390. def __del__(self):
  1391. """
  1392. 析构函数:当对象被垃圾回收时自动调用
  1393. """
  1394. try:
  1395. if not getattr(self, "_cleanup_attempted", False):
  1396. self.cleanup()
  1397. except Exception:
  1398. # Explicit lifecycle owners report cleanup errors. A finalizer
  1399. # must not retry business actions or emit unraisable tracebacks.
  1400. pass