| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548 |
- import time
- import json
- import random
- import socket
- import uuid
- import shutil
- import re
- import os
- import stat
- from dataclasses import dataclass
- from datetime import datetime
- from pathlib import Path
- from typing import Dict, Optional, Any, Callable
- from urllib.parse import parse_qs, urlparse
- try:
- import fcntl
- except ImportError: # pragma: no cover - persistent local profiles require POSIX.
- fcntl = None
- from DrissionPage import ChromiumPage, ChromiumOptions
- from DrissionPage.errors import PageDisconnectedError
- import configure
- from plugins.ita_calendar_book import (
- CalendarBookError,
- book_date_preferences,
- read_calendar_service,
- recheck_calendar_before_final,
- )
- from plugins.ita_calendar_query import (
- CALENDAR_PATH,
- CalendarAccessDeniedError,
- CalendarAuthenticationExpiredError,
- CalendarGateError,
- CalendarQueryScanResult,
- CalendarRateLimitedError,
- CalendarTransientError,
- guard_calendar,
- same_origin_path,
- scan_until_first_available_month,
- )
- from plugins.ita_protocol import (
- ItaCapability,
- ItaProtocolError,
- ItaRouteConfig,
- resolve_route_config,
- )
- from vs_plg import IVSPlg
- from vs_types import VSPlgConfig, AppointmentType, VSQueryResult, VSBookResult, DateAvailability, AvailabilityStatus, SessionExpiredOrInvalidError, RateLimiteddError, PermissionDeniedError, BizLogicError, PluginStatus, utc_now_str
- from toolkit.mihomo_tunnel import MihomoTunnel
- from utils.mouse import HumanMouse
- from utils.keyboard import HumanKeyboard
- from utils.fingerprint_utils import FingerprintGenerator
- BOOKING_FORM_DISCOVERY_STOP_LABEL = "booking-form-ready-before-avanti"
- BOOKING_RECHECK_NONE_AVAILABLE_LABEL = "no-availability-after-user-recheck"
- BOOKING_CALENDAR_PREFINAL_STOP_LABEL = "calendar-ready-before-final"
- PERSISTENT_BROWSER_PROFILE_KEY = "persistent_browser_profile_path"
- RADWARE_VALIDATION_HOSTS = frozenset({"validate.perfdrive.com"})
- SERVICE_LINK_WAIT_SECONDS = 15
- SERVICES_PROBE_QUERY_MODE = "services_probe"
- CALENDAR_QUERY_MODE = "calendar"
- SERVICE_LIST_DIAGNOSTIC_SCRIPT = r"""
- /* coordinate:ita-service-list-structure */
- const targetPath = '__TARGET_PATH__';
- const tables = Array.from(document.querySelectorAll('table'));
- const bookingLinks = Array.from(
- document.querySelectorAll('a[href^="/Services/Booking/"]')
- );
- // Page-level diagnostic only: a row without its booking link exposes no
- // verified service-id binding. Never turn this count into a booking result.
- const normalizeNotice = (value) => String(value || '').replace(/\s+/g, ' ')
- .trim().toLowerCase().replace(/[.。!!]+$/, '');
- const alreadyBookedNotice = 'prenotazione già effettuata per questo servizio';
- const alreadyBookedRows = tables.flatMap((table) =>
- Array.from(table.querySelectorAll('tbody tr')),
- ).filter((row) => Array.from(row.cells).some((cell) => {
- const style = window.getComputedStyle(cell);
- return cell.getClientRects().length > 0 && style.visibility === 'visible'
- && normalizeNotice(cell.innerText) === alreadyBookedNotice;
- }));
- return {
- path: window.location.pathname,
- ready_state: document.readyState,
- table_count: tables.length,
- row_count: tables.reduce(
- (count, table) => count + table.querySelectorAll('tbody tr').length,
- 0,
- ),
- booking_link_count: bookingLinks.length,
- target_link_count: bookingLinks.filter(
- (link) => link.getAttribute('href') === targetPath,
- ).length,
- already_booked_notice_row_count: alreadyBookedRows.length,
- };
- """
- class ItaAuthenticationChallengeError(BizLogicError):
- """Raised when website authentication requires an external security challenge."""
- class ItaTargetServiceLinkMissingError(BizLogicError):
- """Raised when the authenticated Services page lacks the configured link."""
- class ItaCalendarPreparationRequiredError(BizLogicError):
- """Read-only recovery cannot restore Calendar; ITA-OTP-01 remains pending."""
- class ItaCalendarTransientError(BizLogicError):
- """A bounded read-only retry is possible without repeating preparation."""
- @dataclass(frozen=True, slots=True, repr=False)
- class CalendarQueryCheckpoint:
- """In-memory comparison reference, never authentication proof or a signal.
- Kept only by the owner of the prepared Query session. It must not be
- serialized, sent to Book, or used for a different account/profile/route.
- """
- route: ItaRouteConfig
- username: str
- profile_path: str
- calendar_url: str
- runtime_service: str
- BOOKING_FORM_DISCOVERY_SCRIPT = r"""
- const form = document.querySelector('#bookingForm');
- if (!form) {
- return null;
- }
- const toPath = (rawUrl) => {
- try {
- return new URL(rawUrl, window.location.origin).pathname;
- } catch (_error) {
- return null;
- }
- };
- const allFields = Array.from(
- form.querySelectorAll('input, select, textarea')
- );
- const allButtons = Array.from(
- form.querySelectorAll('button, input[type="submit"], input[type="button"]')
- );
- return {
- path: window.location.pathname,
- form: {
- method: (form.getAttribute('method') || 'GET').toUpperCase(),
- action_path: toPath(form.getAttribute('action') || window.location.href),
- },
- field_count: allFields.length,
- fields_truncated: allFields.length > 100,
- fields: allFields.slice(0, 100).map((element) => ({
- tag: element.tagName.toLowerCase(),
- type: element.tagName === 'INPUT'
- ? (element.getAttribute('type') || 'text').toLowerCase()
- : null,
- id: element.getAttribute('id'),
- name: element.getAttribute('name'),
- required: Boolean(element.required),
- })),
- button_count: allButtons.length,
- buttons_truncated: allButtons.length > 50,
- buttons: allButtons.slice(0, 50).map((element) => ({
- tag: element.tagName.toLowerCase(),
- type: (element.getAttribute('type') || 'button').toLowerCase(),
- id: element.getAttribute('id'),
- name: element.getAttribute('name'),
- text: element.tagName === 'BUTTON'
- ? (element.textContent || '').trim()
- : (element.getAttribute('aria-label') || '').trim(),
- })),
- };
- """
- # ==========================================
- # 2. ItaPlugin 核心逻辑
- # ==========================================
- class ItaPlugin(IVSPlg):
- name = "ita_plugin"
- version = "1.0.0"
- description = "意大利Prenot@Mi领事预约插件 (支持拟人化交互与多领区调度)"
- _KNOWN_NO_AVAILABILITY_NOTICES = frozenset(
- {
- "stante l'elevata richiesta i posti disponibili per il servizio "
- "scelto sono esauriti",
- }
- )
- def __init__(self, group_id: str):
- self.group_id = group_id
- self.config: Optional[VSPlgConfig] = None
- self.free_config: Dict[str, Any] = {}
- self.is_healthy = True
- self._cleanup_attempted = False
- self.logger = None
- self.page: Optional[ChromiumPage] = None
-
- # Prenotami 特有配置
- self.ita_url = 'https://prenotami.esteri.it'
-
- self.instance_id = uuid.uuid4().hex[:8]
- self.root_workspace = os.path.abspath(os.path.join("data/temp_browser_data", f"{self.group_id}.{self.instance_id}"))
- self._transient_user_data_path = os.path.join(self.root_workspace, "user_data")
- self.user_data_path = self._transient_user_data_path
- self._persistent_profile_path: Optional[Path] = None
- self._profile_lock_handle = None
-
- if not os.path.exists(self.root_workspace):
- os.makedirs(self.root_workspace)
- self.tunnel = None
- self.session_create_time: float = 0
- self.last_booking_form_structure: Optional[Dict[str, Any]] = None
- self.last_service_list_structure: Optional[Dict[str, Any]] = None
- self.last_calendar_query_scan: Optional[CalendarQueryScanResult] = None
- self._calendar_query_route_key: Optional[str] = None
- self._calendar_query_checkpoint: Optional[CalendarQueryCheckpoint] = None
- self._calendar_query_page = None
- self._calendar_query_generation = None
- self._calendar_query_tab_id = None
- self._query_stop_reason = None
- self._reset_calendar_book_state()
-
- def set_log(self, logger: Callable[[str], None]) -> None:
- self.logger = logger
-
- def _log(self, message):
- if self.logger:
- self.logger(f'[ItaPlugin] [{self.group_id}] {message}')
- else:
- print(f'[ItaPlugin] [{self.group_id}] {message}')
- @staticmethod
- def _url_for_log(raw_url: str) -> str:
- parsed = urlparse(str(raw_url or ""))
- path = parsed.path or "/"
- if path.startswith("/signin/") and path != "/signin/":
- path = "/signin/<redacted>"
- if (parsed.hostname or "").lower() in RADWARE_VALIDATION_HOSTS:
- path = "/<redacted-challenge>"
- if parsed.scheme and parsed.netloc:
- return f"{parsed.scheme}://{parsed.netloc}{path}"
- return path
- @staticmethod
- def _normalize_service_notice(message: str) -> str:
- normalized = str(message or "").replace("’", "'").replace("‘", "'")
- normalized = re.sub(r"\s+", " ", normalized).strip().casefold()
- return normalized.rstrip(".。!!")
- @classmethod
- def _classify_service_notice(
- cls,
- message: str,
- ) -> Optional[AvailabilityStatus]:
- """Classify only service notices backed by current website evidence."""
- normalized = cls._normalize_service_notice(message)
- if normalized in cls._KNOWN_NO_AVAILABILITY_NOTICES:
- return AvailabilityStatus.NoneAvailable
- return None
- def _random_sleep(self, min_sec=30, max_sec=60):
- sleep_time = random.uniform(min_sec, max_sec)
- self._log(f"Anti-Rate-Limit: Sleeping for {sleep_time:.2f} seconds...")
- time.sleep(sleep_time)
-
- def set_config(self, config: VSPlgConfig):
- self.config = config
- self.free_config = config.free_config or {}
- query_mode = self.free_config.get(
- "query_mode",
- SERVICES_PROBE_QUERY_MODE,
- )
- if query_mode not in {
- SERVICES_PROBE_QUERY_MODE,
- CALENDAR_QUERY_MODE,
- }:
- raise BizLogicError(
- "Italy query_mode must be services_probe or calendar"
- )
- otp_mode = self.free_config.get("otp_mode")
- if otp_mode not in (None, "manual", "cloud"):
- raise BizLogicError("Italy otp_mode must be manual, cloud, or omitted")
- if not self._book_final_configuration_valid():
- raise BizLogicError(
- "Italy final submission requires explicit OTP mode on an applicant Book configuration"
- )
- self.user_data_path = self._transient_user_data_path
- self._persistent_profile_path = None
- self._clear_calendar_query_state()
- self._reset_calendar_book_state()
- configured_profile = self.free_config.get(PERSISTENT_BROWSER_PROFILE_KEY)
- if configured_profile is not None:
- if not isinstance(configured_profile, str) or not configured_profile.strip():
- raise BizLogicError(
- "Italy persistent browser profile path must be a non-empty string"
- )
- candidate = Path(configured_profile).expanduser()
- if candidate.is_symlink():
- raise BizLogicError(
- "Italy persistent browser profile path must not be a symlink"
- )
- try:
- resolved_profile = candidate.resolve(strict=True)
- except OSError as exc:
- raise BizLogicError(
- "Italy persistent browser profile directory does not exist"
- ) from exc
- if not resolved_profile.is_dir():
- raise BizLogicError(
- "Italy persistent browser profile path must be a directory"
- )
- if os.name == "posix":
- profile_mode = stat.S_IMODE(resolved_profile.stat().st_mode)
- if profile_mode != 0o700:
- raise BizLogicError(
- "Italy persistent browser profile permissions must be 700"
- )
- transient_root = Path(self.root_workspace).resolve()
- if (
- resolved_profile == transient_root
- or resolved_profile.is_relative_to(transient_root)
- ):
- raise BizLogicError(
- "Italy persistent browser profile must stay outside the temporary workspace"
- )
- self._persistent_profile_path = resolved_profile
- self.user_data_path = str(resolved_profile)
- def _resolve_route(self, apt_type: AppointmentType) -> ItaRouteConfig:
- """Resolve the current route through the shared pure protocol core."""
- if not isinstance(apt_type, AppointmentType):
- raise BizLogicError("Italy route requires an AppointmentType")
- try:
- return resolve_route_config(
- self.free_config.get("apt_configs", {}),
- apt_type.routing_key,
- )
- except ItaProtocolError as exc:
- raise BizLogicError(
- f"Italy route configuration rejected ({exc.code})"
- ) from exc
- @staticmethod
- def _booking_path(route: ItaRouteConfig) -> str:
- return f"/Services/Booking/{route.service_id}"
- def _record_calendar_query_prepared_route(
- self,
- route: ItaRouteConfig,
- ) -> None:
- """Record route continuity only after a validated AVANTI transition."""
- if not isinstance(route, ItaRouteConfig):
- raise BizLogicError(
- "Italy Calendar preparation requires an ItaRouteConfig"
- )
- current_path, _ = same_origin_path(self.page.url)
- if current_path != CALENDAR_PATH:
- raise BizLogicError(
- "Italy Calendar route cannot be recorded outside Calendar"
- )
- self._calendar_query_route_key = route.routing_key
- def _clear_calendar_query_state(self) -> None:
- self._calendar_query_route_key = None
- self.last_calendar_query_scan = None
- self._calendar_query_checkpoint = None
- self._calendar_query_page = None
- self._calendar_query_generation = None
- self._calendar_query_tab_id = None
- def _invalidate_query_session(self, reason: str) -> None:
- # Same health -> cleanup -> creator contract as VFS/BLS. The caller
- # owns the retry budget; Query itself never logs in or sends an OTP.
- self.is_healthy = False
- self._query_stop_reason = reason
- self._clear_calendar_query_state()
- def _check_query_checkpoint_owner(
- self, apt_type: AppointmentType, checkpoint: CalendarQueryCheckpoint,
- ) -> None:
- account = self.config.account if self.config else None
- username = account.username.strip().casefold() if account else ""
- if (
- not isinstance(checkpoint, CalendarQueryCheckpoint)
- or not username
- or username != checkpoint.username
- or getattr(self, "_persistent_profile_path", None) is None
- or str(self._persistent_profile_path) != checkpoint.profile_path
- or self.user_data_path != checkpoint.profile_path
- or self._resolve_route(apt_type) != checkpoint.route
- or self.free_config.get("query_mode") != CALENDAR_QUERY_MODE
- or self.free_config.get("final_submit_enabled", False) is not False
- or getattr(self, "_calendar_book_prepared_context", None) is not None
- ):
- raise BizLogicError("Italy Query recovery account, profile, or route does not match")
- def capture_calendar_query_checkpoint(
- self, apt_type: AppointmentType,
- ) -> CalendarQueryCheckpoint:
- """Read a recovery reference only from this already-prepared Calendar."""
- route = self._resolve_route(apt_type)
- if (
- not self.page or not self.is_healthy or self.session_create_time <= 0
- or getattr(self, "_calendar_query_route_key", None) != route.routing_key
- or route.capability is not ItaCapability.SINGLE_SCHENGEN_TOURISM
- or route.calendar_scan is None
- ):
- raise BizLogicError("Italy Query recovery requires a prepared current Session")
- self._validate_calendar_query_url(self.page.url)
- guard_calendar(self.page)
- checkpoint = CalendarQueryCheckpoint(
- route=route,
- username=self.config.account.username.strip().casefold(),
- profile_path=str(getattr(self, "_persistent_profile_path", "")),
- calendar_url=str(self.page.url),
- runtime_service=read_calendar_service(self.page),
- )
- self._check_query_checkpoint_owner(apt_type, checkpoint)
- tab_id = self.page.tab_id
- if not isinstance(tab_id, str) or not tab_id:
- raise BizLogicError("Italy Query recovery requires a current browser tab")
- self._calendar_query_checkpoint = checkpoint
- self._calendar_query_page = self.page
- self._calendar_query_generation = self.session_create_time
- self._calendar_query_tab_id = tab_id
- return checkpoint
- @staticmethod
- def _validate_calendar_query_url(raw_url: str) -> None:
- path, parsed = same_origin_path(raw_url)
- query = parse_qs(parsed.query, keep_blank_values=True)
- if (
- path != CALENDAR_PATH or parsed.fragment
- or set(query) - {"selectedService"}
- or any(len(values) != 1 or not values[0] for values in query.values())
- ):
- raise BizLogicError("Italy Query recovery requires the observed Calendar URL shape")
- def _check_current_query_checkpoint(self, apt_type: AppointmentType) -> None:
- checkpoint = getattr(self, "_calendar_query_checkpoint", None)
- if checkpoint is None:
- return # Existing CS3A callers attest the same-instance route.
- self._check_query_checkpoint_owner(apt_type, checkpoint)
- if (
- self.page is not self._calendar_query_page
- or self.session_create_time != self._calendar_query_generation
- or self.page.tab_id != self._calendar_query_tab_id
- or read_calendar_service(self.page) != checkpoint.runtime_service
- ):
- raise BizLogicError("Italy Query current Calendar identity changed")
- def _check_calendar_access(self, booking_path: str) -> None:
- parsed = urlparse(str(self.page.url or ""))
- if parsed.hostname == "iam.esteri.it":
- raise SessionExpiredOrInvalidError("Italy session is no longer authenticated")
- if parsed.hostname in RADWARE_VALIDATION_HOSTS:
- raise PermissionDeniedError("Italy Calendar requires browser verification")
- path, _ = same_origin_path(self.page.url)
- if self.page.ele("#pingid-button", timeout=0.5):
- raise SessionExpiredOrInvalidError("Italy session is no longer authenticated")
- if path != CALENDAR_PATH:
- if path in {"/UserArea", "/Services", booking_path}:
- raise ItaCalendarPreparationRequiredError(
- "Italy Calendar requires preparation; automatic OTP is pending (ITA-OTP-01)"
- )
- raise CalendarGateError("Italy Calendar reached an unexpected page")
- def restore_calendar_query(
- self, apt_type: AppointmentType, checkpoint: CalendarQueryCheckpoint,
- ) -> None:
- """Try one observed read-only navigation after same-account login.
- A checkpoint only supplies comparison values. New page/route/service
- evidence is required before the new instance may query again.
- """
- self._clear_calendar_query_state()
- self._check_query_checkpoint_owner(apt_type, checkpoint)
- if not self.page or not self.is_healthy or self.session_create_time <= 0:
- raise SessionExpiredOrInvalidError("Italy Query recovery requires a new authenticated Session")
- self._validate_calendar_query_url(checkpoint.calendar_url)
- try:
- self.page.get(checkpoint.calendar_url)
- self.page.wait.doc_loaded()
- self._check_calendar_access(self._booking_path(checkpoint.route))
- guard_calendar(self.page)
- if read_calendar_service(self.page) != checkpoint.runtime_service:
- raise BizLogicError("Italy Query restored Calendar service does not match")
- self._record_calendar_query_prepared_route(checkpoint.route)
- self.capture_calendar_query_checkpoint(apt_type)
- self._query_stop_reason = None
- except SessionExpiredOrInvalidError:
- self._invalidate_query_session("session_expired")
- raise
- except PageDisconnectedError:
- self._invalidate_query_session("browser_disconnected")
- raise
- except Exception:
- self._clear_calendar_query_state()
- self._query_stop_reason = "needs_manual"
- raise
- def _reset_calendar_book_state(self) -> None:
- self._calendar_book_prepared_context = None
- self._calendar_book_attempted = False
- self.last_booking_precommit = None
- def _record_calendar_book_prepared_route(self, route: ItaRouteConfig) -> None:
- """Attest Book preparation after the caller validates this route's AVANTI.
- This private hook does not perform or infer preparation. It must not be
- called merely because a persisted browser happens to show Calendar.
- """
- self._calendar_book_prepared_context = None
- self.last_booking_precommit = None
- if (not isinstance(route, ItaRouteConfig)
- or route.capability is not ItaCapability.SINGLE_SCHENGEN_TOURISM
- or route.calendar_scan is None):
- raise BizLogicError("Italy Book preparation requires a single-tourism Calendar route")
- configured = self._resolve_route(AppointmentType(routing_key=route.routing_key))
- if route != configured or getattr(self, "_calendar_query_route_key", None) is not None:
- raise BizLogicError("Italy Book cannot reuse a Query preparation or another route")
- username = self.config.account.username.strip().casefold() if self.config else ""
- if (not username or not self.page or self.session_create_time <= 0
- or not self._book_final_configuration_valid()):
- raise BizLogicError("Italy Book preparation requires an authenticated user Session and final disabled")
- try:
- runtime_service = read_calendar_service(self.page)
- tab_id = self.page.tab_id
- if not isinstance(tab_id, str) or not tab_id:
- raise CalendarBookError("Book Calendar tab identity was unavailable")
- except Exception:
- raise BizLogicError("Italy Book preparation did not identify the current Calendar service") from None
- self._calendar_book_prepared_context = (
- id(self), self.page, username, self.session_create_time, route, runtime_service, tab_id,
- )
- self._calendar_book_attempted = False
- def _calendar_book_context_matches(self, route: ItaRouteConfig, username: str) -> bool:
- context = getattr(self, "_calendar_book_prepared_context", None)
- try:
- return (
- context is not None
- and len(context) == 7
- and context[1] is self.page
- and context[6] == self.page.tab_id
- and context[:5] == (id(self), self.page, username, self.session_create_time, route)
- and self.config.account.username.strip().casefold() == username
- and getattr(self, "_calendar_query_route_key", None) is None
- and self._book_final_configuration_valid()
- and self._resolve_route(AppointmentType(routing_key=route.routing_key)) == route
- )
- except Exception:
- return False
- def _book_prepared_calendar(
- self, slot_info: VSQueryResult, route: ItaRouteConfig, user_inputs: Dict,
- ) -> VSBookResult:
- """Recheck only the prepared applicant Session; stop before final."""
- if getattr(self, "_calendar_book_attempted", False):
- raise BizLogicError("Italy Book Calendar was already attempted; fresh preparation is required")
- if not isinstance(user_inputs, dict) or not isinstance(user_inputs.get("username"), str):
- raise BizLogicError("Italy Calendar Book requires user_inputs.username for applicant binding")
- username = user_inputs["username"].strip().casefold()
- if not username or not self._calendar_book_context_matches(route, username):
- raise BizLogicError("Italy Book Calendar preparation does not match the current applicant and route")
- try:
- start_day, end_day = book_date_preferences(user_inputs)
- if read_calendar_service(self.page) != self._calendar_book_prepared_context[5]:
- raise CalendarBookError("Book Calendar no longer matches the prepared service")
- self._calendar_book_attempted = True
- approval = recheck_calendar_before_final(
- self.page,
- lookahead_months=route.calendar_scan.lookahead_months,
- start_day=start_day, end_day=end_day,
- preferred_day=slot_info.earliest_date.date() if slot_info.earliest_date else None,
- session_is_current=lambda: self._calendar_book_context_matches(route, username),
- log=self._log,
- )
- if not self._calendar_book_context_matches(route, username):
- raise CalendarBookError("Book Session changed before publishing the pre-final checkpoint")
- except CalendarGateError as exc:
- self._calendar_book_prepared_context = None
- self.last_booking_precommit = None
- if getattr(exc, "http_status", None) == 401 or urlparse(str(self.page.url or "")).hostname == "iam.esteri.it":
- raise SessionExpiredOrInvalidError("Italy Book Session is no longer authenticated") from None
- if getattr(exc, "http_status", None) == 429:
- raise RateLimiteddError("Italy Book Calendar is rate limited") from None
- raise BizLogicError(str(exc)) from None
- except Exception:
- self._calendar_book_prepared_context = None
- self.last_booking_precommit = None
- raise BizLogicError("Italy Book Calendar failed before final confirmation") from None
- self.last_booking_precommit = approval
- result = VSBookResult(success=False)
- if approval is None:
- result.label = BOOKING_RECHECK_NONE_AVAILABLE_LABEL
- else:
- result.label = BOOKING_CALENDAR_PREFINAL_STOP_LABEL
- self._log(
- "Italy Book selected current user Calendar slot "
- f"date={approval.candidate.day.isoformat()} time={approval.candidate.time_label}; "
- "stopped before final confirmation."
- )
- return result
- def _acquire_persistent_profile_lock(self) -> None:
- profile_path = self._persistent_profile_path
- if profile_path is None or self._profile_lock_handle is not None:
- return
- if fcntl is None:
- raise BizLogicError(
- "Italy persistent browser profiles require POSIX file locking"
- )
- lock_path = profile_path / ".coordinate-profile.lock"
- try:
- lock_handle = lock_path.open("a+", encoding="utf-8")
- lock_path.chmod(0o600)
- fcntl.flock(lock_handle.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
- except BlockingIOError as exc:
- try:
- lock_handle.close()
- except UnboundLocalError:
- pass
- raise BizLogicError(
- "Italy persistent browser profile is already in use"
- ) from exc
- except OSError as exc:
- try:
- lock_handle.close()
- except UnboundLocalError:
- pass
- raise BizLogicError(
- f"Cannot lock Italy persistent browser profile ({type(exc).__name__})"
- ) from exc
- self._profile_lock_handle = lock_handle
- def _release_persistent_profile_lock(self) -> None:
- lock_handle = getattr(self, "_profile_lock_handle", None)
- if lock_handle is None:
- return
- try:
- if fcntl is not None:
- fcntl.flock(lock_handle.fileno(), fcntl.LOCK_UN)
- finally:
- lock_handle.close()
- self._profile_lock_handle = None
-
- def keep_alive(self):
- pass
- def health_check(self) -> bool:
- if not self.is_healthy:
- return False
- if not self.page:
- self._invalidate_query_session("browser_disconnected")
- return False
- try:
- if not self.page.run_js("return 1;"):
- self._invalidate_query_session("browser_disconnected")
- return False
- except Exception:
- self._invalidate_query_session("browser_disconnected")
- return False
- if self.config.session_max_life > 0:
- if time.time() - self.session_create_time > self.config.session_max_life:
- self._log("Session expired.")
- self._invalidate_query_session("configured_lifetime_reached")
- return False
- return True
- def create_session(self):
- """
- 创建认证浏览器会话,并在登录后的 /UserArea 停止。
- """
- self._log(f"Initializing Session (ID: {self.instance_id})...")
- self._cleanup_attempted = False
- self.is_healthy = False
- self._query_stop_reason = None
- self._clear_calendar_query_state()
- self._reset_calendar_book_state()
- co = ChromiumOptions()
- def get_free_port():
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
- s.bind(('', 0))
- return s.getsockname()[1]
- debug_port = get_free_port()
- self._log(f"Assigned Debug Port: {debug_port}")
- self._log(f"Account id={self.config.account.id}, proxy id={self.config.proxy.id}")
- self._acquire_persistent_profile_lock()
- if self._persistent_profile_path is not None:
- self._log(
- "Browser profile mode=persistent "
- f"id={self._persistent_profile_path.name}"
- )
- else:
- self._log("Browser profile mode=transient")
- co.set_local_port(debug_port)
- co.set_user_data_path(self.user_data_path)
- chrome_path = configure.CHROME_PATH
- if not chrome_path:
- chrome_path = os.getenv("CHROME_BIN")
- if chrome_path and os.path.exists(chrome_path):
- co.set_paths(browser_path=chrome_path)
- if self.config.proxy and self.config.proxy.ip:
- p = self.config.proxy
- if p.username and p.password:
- self._log(f"Starting Proxy Tunnel for {p.ip}...")
- exit_node = {
- "name": "ExitNode",
- "type": p.proto,
- "server": p.ip,
- "port": p.port,
- "username": p.username,
- "password": p.password
- }
- relay_node = None
- if configure.MIHOMO_RELAY_NODES:
- relay_node = random.choice(configure.MIHOMO_RELAY_NODES)
- mihomo_path = configure.MIHOMO_BIN_PATH
- if not mihomo_path:
- mihomo_path = os.getenv("MIHOMO_BIN")
- if not mihomo_path:
- raise BizLogicError(message='Mihomo path is null, You need set mihomo bin path in configure or os env')
- self.tunnel = MihomoTunnel(mihomo_path, exit_node=exit_node, relay_node=relay_node)
- local_proxy = self.tunnel.start()
- self._log(f"Tunnel started at {local_proxy}")
- co.set_argument(f'--proxy-server={local_proxy}')
- else:
- proxy_str = f"{p.proto}://{p.ip}:{p.port}"
- co.set_argument(f'--proxy-server={proxy_str}')
- else:
- self._log("[WARN] No proxy configured!")
- specific_fp = FingerprintGenerator().generate(self.config.account.username)
- fp_seed = specific_fp.get("seed")
- fp_platform = specific_fp.get("platform")
- fp_brand = specific_fp.get("brand")
- self._log(f'browser fingerprint seed={fp_seed}')
- co.headless(False)
- co.set_argument('--no-sandbox')
- co.set_argument('--disable-dev-shm-usage')
- co.set_argument('--window-size=1920,1080')
- co.set_argument('--disable-blink-features=AutomationControlled')
- co.set_argument(f"--fingerprint={fp_seed}")
- co.set_argument(f"--fingerprint-platform={fp_platform}")
- co.set_argument(f"--fingerprint-brand={fp_brand}")
-
- self.page = ChromiumPage(co)
- if self.config.debug:
- self.page.get('https://example.com')
- js_script = """
- function getFingerprint() {
- let webglVendor = 'Unknown';
- let webglRenderer = 'Unknown';
- try {
- let canvas = document.createElement('canvas');
- let gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
- if (gl) {
- let debugInfo = gl.getExtension('WEBGL_debug_renderer_info');
- if (debugInfo) {
- webglVendor = gl.getParameter(debugInfo.UNMASKED_VENDOR_WEBGL);
- webglRenderer = gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL);
- }
- }
- } catch(e) {}
- return {
- "User-Agent": navigator.userAgent,
- "Platform": navigator.userAgentData ? navigator.userAgentData.platform : navigator.platform,
- "Brands": navigator.userAgentData ? navigator.userAgentData.brands.map(b => b.brand).join(', ') : 'Not Supported',
- "CPU Cores": navigator.hardwareConcurrency,
- "Language": navigator.language,
- "Timezone": Intl.DateTimeFormat().resolvedOptions().timeZone,
- "WebGL Vendor": webglVendor,
- "WebGL Renderer": webglRenderer
- };
- }
- return getFingerprint();
- """
- fp_data = self.page.run_js(js_script)
- self._log("================ 预检浏览器指纹数据 ================")
- self._log(json.dumps(fp_data, indent=4, ensure_ascii=False))
- self._log("====================================================")
- self.page = ChromiumPage(co)
- ita_url = self.ita_url
- self._log(f"Navigating to {ita_url}")
- self.page.get(ita_url)
-
- self._log("Init humanize tools...")
- self.mouse = HumanMouse(self.page, debug=True)
- self.keyboard = HumanKeyboard(self.page)
- self._log("Random mouse start position...")
- viewport_width = self.page.rect.viewport_size[0]
- viewport_height = self.page.rect.viewport_size[1]
- init_x = random.randint(10, viewport_width - 10)
- init_y = random.randint(10, viewport_height - 10)
- self.mouse.move(init_x, init_y)
- max_steps = 15
- stuck_counter = 0
- last_url = ""
- session_created = False
- has_clicked_login = False
- has_submitted_login = False
- username = self.config.account.username
- password = self.config.account.password
- for step in range(max_steps):
- self.page.wait.doc_loaded()
- time.sleep(1)
- current_url = self.page.url
- current_parsed_url = urlparse(current_url)
- current_host = (current_parsed_url.hostname or "").lower()
- current_path = urlparse(current_url).path.rstrip("/") or "/"
- current_title = self.page.title.lower()
- current_html_content = self.page.html
- self._log(
- f"--- [Router Step {step+1}] Current URL: "
- f"{self._url_for_log(current_url)} ---"
- )
- if current_host in RADWARE_VALIDATION_HOSTS:
- raise ItaAuthenticationChallengeError(
- "Italy authentication requires Radware browser validation; "
- "automatic retry is disabled"
- )
- # --- [异常处理与反爬对抗层] ---
- if current_url == last_url:
- stuck_counter += 1
- else:
- last_url = current_url
- stuck_counter = 0
- if stuck_counter >= 3:
- if current_path == "/UserArea":
- # The URL can stay stable while authenticated DOM finishes
- # rendering; let the success predicate below inspect it.
- pass
- elif has_clicked_login or has_submitted_login:
- self._log(
- "[WARN] Authentication flow is still on the same page; "
- "waiting without refresh or repeated submission."
- )
- time.sleep(2)
- continue
- self._log("[WARN] Page stucked before authentication, try to refresh...")
- self.page.refresh()
- self._random_sleep(5, 8)
- stuck_counter = 0
- continue
- server_error_indicators = ["502 Bad Gateway", "503 Service Temporarily Unavailable", "error 1020"]
- if any(err in current_html_content for err in server_error_indicators):
- if has_clicked_login or has_submitted_login:
- raise BizLogicError(
- "Italy authentication reached a server error; "
- "refusing to refresh or repeat the login attempt"
- )
- self._log(f"[WARN] Server network error, try to refresh (Step: {step})...")
- self.page.refresh()
- self._random_sleep(10, 15)
- continue
- # Cloudflare 拦截
- cloudflare_blocked_indicators = [
- "Sorry, you have been blocked",
- "You are being rate limited",
- "Cloudflare Ray ID"
- ]
- if any(indicator in current_html_content for indicator in cloudflare_blocked_indicators):
- raise BizLogicError(message="Blocked by Cloudflare WAF. Need to change IP or browser fingerprint.")
- # 遇到五秒盾先绕盾 (保留你原有的逻辑)
- if "just a moment" in current_title or "cloudflare" in current_title:
- # 假设你有 cf_bypasser 实例
- # if not cf_bypasser.bypass(max_retry=3): continue
- self._log("[State] CF 5-second shield detected. Waiting...")
- time.sleep(5)
- continue
- # 状态 0: 登录成功后的用户控制台。
- if (
- current_path == "/UserArea"
- and self.page.ele('.app-menu', timeout=1)
- and self.page.ele('@href=/Services', timeout=1)
- ):
- self._log("Successfully reached authenticated /UserArea.")
- self.session_create_time = time.time()
- self.is_healthy = True
- session_created = True
- break
- # 状态 1: 登录前的初始首页
- elif self.page.ele('#pingid-button', timeout=1):
- if has_clicked_login:
- self._log(
- "[State] Login entry was already clicked; "
- "waiting without clicking it again."
- )
- time.sleep(2)
- continue
- self._log("[State] Initial Landing Page detected. Clicking login button...")
- login_btn = self.page.ele('#pingid-button')
- self.mouse.human_click_ele(login_btn)
- has_clicked_login = True
- self._log("Redirecting to PingID...")
- self._random_sleep(3, 5)
- continue
- # 状态 2: 真正的表单登录页 (PingID)
- elif self.page.ele('@name=callback_1', timeout=1):
- if has_submitted_login:
- self._log(
- "[State] Login form is still present after submission; "
- "waiting without resubmitting credentials."
- )
- time.sleep(2)
- continue
- self._log("[State] PingID Login Form detected. Submitting credentials...")
- user_input = self.page.ele('@name=callback_1')
- pwd_input = self.page.ele('@name=callback_2')
- submit_btn = self.page.ele('tag:button@type=submit')
- if not user_input or not pwd_input or not submit_btn:
- raise BizLogicError(
- "Italy IAM login form is missing callback_1, "
- "callback_2, or the submit button"
- )
- self._log("Entering username...")
- self.mouse.human_click_ele(user_input)
- user_input.input(username, clear=True)
- time.sleep(1)
- self._log("Entering password...")
- self.mouse.human_click_ele(pwd_input)
- pwd_input.input(password, clear=True)
- time.sleep(1)
- self._log("Clicking submit login button...")
- self.mouse.human_click_ele(submit_btn)
- has_submitted_login = True
- self._log("Login form submitted. Waiting for dashboard to load...")
- self._random_sleep(5, 7)
- continue
- # /UserArea 只有在认证 DOM 同时就绪时才能成功。
- elif current_path == "/UserArea":
- self._log(
- "[State] /UserArea loaded without complete authentication "
- "markers; waiting."
- )
- time.sleep(2)
- continue
- else:
- self._log("[State] In unknown or transitional state. Waiting for next polling cycle...")
- time.sleep(2)
- if not session_created:
- raise BizLogicError(
- f"Failed to create authenticated /UserArea session after "
- f"{max_steps} navigation steps. Stuck at: "
- f"{self._url_for_log(self.page.url)}"
- )
- # -------------------------------------------------------------
- # 2. Query Availability
- # -------------------------------------------------------------
- def _inspect_service_list_structure(
- self,
- booking_path: str,
- ) -> Optional[Dict[str, Any]]:
- """Return counts, including known notices; never export row text or IDs."""
- if not self.page:
- return None
- script = SERVICE_LIST_DIAGNOSTIC_SCRIPT.replace(
- "__TARGET_PATH__",
- booking_path,
- )
- raw = self.page.run_js(script)
- if not isinstance(raw, dict):
- self.last_service_list_structure = None
- return None
- def bounded_count(value: object) -> int:
- try:
- parsed = int(value)
- except (TypeError, ValueError):
- parsed = 0
- return max(0, min(parsed, 10000))
- path = str(raw.get("path") or "").strip()[:120] or None
- ready_state = str(raw.get("ready_state") or "").strip().lower()
- if ready_state not in {"loading", "interactive", "complete"}:
- ready_state = "unknown"
- structure = {
- "path": path,
- "ready_state": ready_state,
- "table_count": bounded_count(raw.get("table_count")),
- "row_count": bounded_count(raw.get("row_count")),
- "booking_link_count": bounded_count(
- raw.get("booking_link_count")
- ),
- "target_link_count": bounded_count(raw.get("target_link_count")),
- "already_booked_notice_row_count": (
- bounded_count(raw["already_booked_notice_row_count"])
- if type(raw.get("already_booked_notice_row_count")) is int else 0
- ),
- }
- self.last_service_list_structure = structure
- return structure
- def query(self, apt_type: AppointmentType) -> VSQueryResult:
- """Preserve the framework's typed failures and unhealthy handoff."""
- self.last_calendar_query_scan = None
- if getattr(self, "is_healthy", True) is False:
- raise SessionExpiredOrInvalidError("Italy Query cannot reuse an unhealthy Session")
- try:
- if (self.page and self.free_config.get("otp_mode") in {"manual", "cloud"}
- and self.free_config.get("query_mode") == CALENDAR_QUERY_MODE
- and urlparse(self.page.url or "").hostname == "prenotami.esteri.it"):
- from plugins.ita_workflow import prepare_query_if_needed
- entry = prepare_query_if_needed(self, apt_type)
- if entry is not None:
- return entry
- return self._query_once(apt_type)
- except SessionExpiredOrInvalidError:
- self._invalidate_query_session("session_expired")
- raise
- except PageDisconnectedError:
- self._invalidate_query_session("browser_disconnected")
- raise
- except RateLimiteddError:
- self._invalidate_query_session("rate_limited")
- raise
- except PermissionDeniedError:
- self._invalidate_query_session("needs_manual")
- raise
- except ItaCalendarPreparationRequiredError:
- self._clear_calendar_query_state()
- self._query_stop_reason = "needs_manual"
- raise
- def _query_once(self, apt_type: AppointmentType, *, query_mode=None) -> VSQueryResult:
- # Diagnostic evidence is per call, never a reusable availability cache.
- self.last_calendar_query_scan = None
- if not self.page:
- raise BizLogicError("Italy query requires an initialized browser session")
- route = self._resolve_route(apt_type)
- query_mode = query_mode or self.free_config.get(
- "query_mode",
- SERVICES_PROBE_QUERY_MODE,
- )
- current_url = self.page.url or ""
- current_parsed_url = urlparse(current_url)
- current_path = current_parsed_url.path.rstrip("/") or "/"
- if query_mode == CALENDAR_QUERY_MODE:
- if route.capability is not ItaCapability.SINGLE_SCHENGEN_TOURISM:
- raise BizLogicError(
- "Italy calendar query requires the "
- "single_schengen_tourism capability"
- )
- if (
- current_parsed_url.netloc.lower() == "iam.esteri.it"
- or self.page.ele("#pingid-button", timeout=0.5)
- ):
- raise SessionExpiredOrInvalidError(
- "Italy session is no longer authenticated"
- )
- same_origin_path(current_url)
- if current_path != CALENDAR_PATH:
- raise ItaCalendarPreparationRequiredError(
- "Italy calendar query requires a route-confirmed "
- "Calendar Session; preparation is not complete (ITA-OTP-01)"
- )
- if getattr(self, "_calendar_query_route_key", None) != route.routing_key:
- raise BizLogicError(
- "Italy calendar query route continuity is not confirmed"
- )
- self._check_current_query_checkpoint(apt_type)
- return self._query_calendar(apt_type, route)
- service_id = route.service_id
- result = VSQueryResult(
- success=False,
- apt_type=apt_type,
- availability_status=AvailabilityStatus.NoneAvailable,
- )
- self.last_service_list_structure = None
- services_path = "/Services"
- services_url = f"{self.ita_url}{services_path}"
- booking_path = self._booking_path(route)
- if current_path not in {"/UserArea", services_path, booking_path}:
- if (
- current_parsed_url.netloc.lower() == "iam.esteri.it"
- or self.page.ele("#pingid-button", timeout=0.5)
- ):
- raise SessionExpiredOrInvalidError(
- "Italy session is no longer authenticated"
- )
- raise BizLogicError(
- "Italy query expected /UserArea or /Services, "
- f"got {current_path}"
- )
- # Always reload Services before probing. The no-slot response leaves a
- # modal over the service list, so reusing the old DOM can block the next
- # Sentinel iteration even though the URL is still /Services.
- self.page.get(services_url)
- self.page.wait.doc_loaded()
- current_url = self.page.url or ""
- current_parsed_url = urlparse(current_url)
- current_path = current_parsed_url.path.rstrip("/") or "/"
- if (
- current_parsed_url.netloc.lower() == "iam.esteri.it"
- or self.page.ele("#pingid-button", timeout=0.5)
- ):
- raise SessionExpiredOrInvalidError(
- "Italy session expired while loading the service list"
- )
- if current_path != services_path:
- raise BizLogicError(
- f"Italy service-list navigation reached {current_path}"
- )
- self._log(f"Checking configured Italy service id={service_id}...")
- booking_link = self.page.ele(
- f"tag:a@@href={booking_path}",
- timeout=SERVICE_LINK_WAIT_SECONDS,
- )
- if not booking_link:
- structure = self._inspect_service_list_structure(booking_path)
- if structure is not None:
- self._log(
- "Italy service-list structure after missing target link: "
- + json.dumps(structure, sort_keys=True)
- )
- message = (
- "Italy target service link is missing after the bounded wait: "
- f"{booking_path}"
- )
- if structure and structure["already_booked_notice_row_count"] > 0:
- message += (
- "; Services contains an already-booked notice "
- "(Prenotazione già effettuata per questo servizio). "
- "Check the target service row and existing reservations; "
- "the notice is not bound to the configured service ID."
- )
- raise ItaTargetServiceLinkMissingError(message)
- self.mouse.human_click_ele(booking_link)
- self.page.wait.load_start()
- self.page.wait.doc_loaded()
- current_url = self.page.url or ""
- current_parsed_url = urlparse(current_url)
- current_path = current_parsed_url.path.rstrip("/") or "/"
- if (
- current_parsed_url.netloc.lower() == "iam.esteri.it"
- or self.page.ele("#pingid-button", timeout=0.5)
- ):
- raise SessionExpiredOrInvalidError(
- "Italy session expired while checking the target service"
- )
- not_available = self.page.ele("#WNotAvailable", timeout=2)
- not_available_message = ""
- if not_available:
- not_available_message = (
- not_available.attr("value")
- or getattr(not_available, "value", "")
- or ""
- ).strip()
- visible_notice = self.page.ele(".jconfirm-content", timeout=2)
- visible_notice_message = ""
- if visible_notice:
- visible_notice_message = (visible_notice.text or "").strip()
- visible_notice_status = self._classify_service_notice(
- visible_notice_message
- )
- if current_path == services_path and not_available_message:
- self._log("Target Italy service explicitly reported no availability.")
- return result
- if (
- current_path == services_path
- and visible_notice_status == AvailabilityStatus.NoneAvailable
- ):
- self._log(
- "Target Italy service showed the verified no-availability notice."
- )
- return result
- if current_path == services_path:
- if visible_notice_message:
- raise BizLogicError(
- "Italy service returned to /Services with an unrecognized "
- "visible service notice"
- )
- raise BizLogicError(
- "Italy service returned to /Services without a non-empty "
- "#WNotAvailable marker or a recognized no-availability notice"
- )
- booking_form = self.page.ele("#bookingForm", timeout=1)
- if current_path == booking_path and booking_form:
- self._log(
- "Target Italy service accepted entry; returning a weak signal "
- "without form, calendar, or OTP actions."
- )
- result.success = True
- return result
- if current_path == booking_path:
- raise BizLogicError(
- "Italy target service reached the booking path without #bookingForm"
- )
- if booking_form:
- raise BizLogicError(
- "Italy #bookingForm appeared on an unexpected path"
- )
- raise BizLogicError(
- f"Italy query reached an unrecognized path: {current_path}"
- )
- def _query_calendar(
- self,
- apt_type: AppointmentType,
- route: ItaRouteConfig,
- ) -> VSQueryResult:
- """Publish only the earliest day from the first positive month."""
- if route.calendar_scan is None:
- raise BizLogicError(
- "Italy calendar query route is missing calendar_scan"
- )
- try:
- scan = scan_until_first_available_month(
- self.page,
- lookahead_months=route.calendar_scan.lookahead_months,
- log=self._log,
- )
- except CalendarAuthenticationExpiredError:
- raise SessionExpiredOrInvalidError("Italy Calendar authentication expired") from None
- except CalendarRateLimitedError:
- raise RateLimiteddError("Italy Calendar request was rate limited") from None
- except CalendarAccessDeniedError:
- raise PermissionDeniedError("Italy Calendar access requires operator attention") from None
- except (CalendarTransientError, TimeoutError):
- # A refresh may have returned to IAM without producing the XHR
- # being listened for. Inspect current page before calling it a
- # transient network error; never repeat preparation here.
- self._check_calendar_access(self._booking_path(route))
- raise ItaCalendarTransientError("Italy Calendar read temporarily failed") from None
- except CalendarGateError as exc:
- raise BizLogicError(
- "Italy Calendar query failed closed"
- ) from exc
- self._check_current_query_checkpoint(apt_type)
- self.last_calendar_query_scan = scan
- result = VSQueryResult(
- success=False,
- apt_type=apt_type,
- availability_status=AvailabilityStatus.NoneAvailable,
- )
- earliest = scan.earliest_available_date
- if earliest is None:
- self._log(
- "Italy Calendar scan completed without a positive month."
- )
- return result
- earliest_dt = datetime.combine(earliest, datetime.min.time())
- result.success = True
- result.availability_status = AvailabilityStatus.Available
- result.earliest_date = earliest_dt
- result.availability = [
- DateAvailability(date=earliest_dt, times=[])
- ]
- self._log(
- "Italy Calendar found the first positive month; "
- f"earliest_date={earliest.isoformat()}."
- )
- return result
- # -------------------------------------------------------------
- # 3. Book
- # -------------------------------------------------------------
- def _inspect_booking_form_structure(
- self,
- booking_path: str,
- ) -> Dict[str, Any]:
- """Read structural metadata only; never read values or submit the form."""
- if not self.page:
- raise BizLogicError(
- "Italy booking-form inspection requires an initialized browser session"
- )
- raw = self.page.run_js(BOOKING_FORM_DISCOVERY_SCRIPT)
- if not isinstance(raw, dict):
- raise BizLogicError(
- "Italy booking form disappeared before structural inspection"
- )
- raw_form = raw.get("form") if isinstance(raw.get("form"), dict) else {}
- raw_fields = raw.get("fields") if isinstance(raw.get("fields"), list) else []
- raw_buttons = (
- raw.get("buttons") if isinstance(raw.get("buttons"), list) else []
- )
- def metadata_text(value: object) -> Optional[str]:
- if value is None:
- return None
- text = re.sub(r"\s+", " ", str(value)).strip()
- return text[:120] or None
- def bounded_count(value: object, fallback: int, maximum: int) -> int:
- try:
- parsed = int(value)
- except (TypeError, ValueError):
- parsed = fallback
- return max(0, min(parsed, maximum))
- structure = {
- "path": metadata_text(raw.get("path")),
- "form": {
- "method": metadata_text(raw_form.get("method")),
- "action_path": metadata_text(raw_form.get("action_path")),
- },
- "field_count": bounded_count(
- raw.get("field_count"), len(raw_fields), 10000
- ),
- "fields_truncated": bool(
- raw.get("fields_truncated") or len(raw_fields) > 100
- ),
- "fields": [
- {
- "tag": metadata_text(item.get("tag")),
- "type": metadata_text(item.get("type")),
- "id": metadata_text(item.get("id")),
- "name": metadata_text(item.get("name")),
- "required": bool(item.get("required")),
- }
- for item in raw_fields[:100]
- if isinstance(item, dict)
- ],
- "button_count": bounded_count(
- raw.get("button_count"), len(raw_buttons), 10000
- ),
- "buttons_truncated": bool(
- raw.get("buttons_truncated") or len(raw_buttons) > 50
- ),
- "buttons": [
- {
- "tag": metadata_text(item.get("tag")),
- "type": metadata_text(item.get("type")),
- "id": metadata_text(item.get("id")),
- "name": metadata_text(item.get("name")),
- "text": metadata_text(item.get("text")),
- }
- for item in raw_buttons[:50]
- if isinstance(item, dict)
- ],
- }
- self.last_booking_form_structure = structure
- if structure["path"] != booking_path:
- raise BizLogicError(
- "Italy booking form appeared on an unexpected path during Book recheck"
- )
- if not structure["form"]["method"] or not structure["form"]["action_path"]:
- raise BizLogicError(
- "Italy booking form is missing method or action metadata"
- )
- return structure
- def book(self, slot_info: VSQueryResult, user_inputs: Dict = None) -> VSBookResult:
- if self.free_config.get("otp_mode") in {"manual", "cloud"}:
- from plugins.ita_workflow import book
- if not self.page or not isinstance(slot_info, VSQueryResult):
- raise BizLogicError("Italy Book requires a browser Session and Query trigger")
- return book(self, slot_info, user_inputs)
- return self._book_discovery_or_prepared(slot_info, user_inputs)
- def _book_final_configuration_valid(self) -> bool:
- value = self.free_config.get("final_submit_enabled", False)
- return value is False or (
- value is True and self.free_config.get("otp_mode") in ("manual", "cloud")
- and self.free_config.get("query_mode", SERVICES_PROBE_QUERY_MODE) == SERVICES_PROBE_QUERY_MODE
- )
- def _book_discovery_or_prepared(self, slot_info: VSQueryResult, user_inputs: Dict = None) -> VSBookResult:
- """Recheck the user's Session; prepared Calendar stops before final."""
- res = VSBookResult()
- res.success = False
- self.last_booking_form_structure = None
- self.last_booking_precommit = None
- if not self.page:
- raise BizLogicError("Italy Book requires an initialized browser session")
- if not isinstance(slot_info, VSQueryResult):
- raise BizLogicError("Italy Book requires a VSQueryResult trigger")
- self._log(
- "Rechecking the target Italy service in the user's authenticated Session..."
- )
- route = self._resolve_route(slot_info.apt_type)
- current_path = urlparse(str(self.page.url or "")).path.rstrip("/") or "/"
- if (current_path == CALENDAR_PATH
- or getattr(self, "_calendar_book_prepared_context", None) is not None):
- return self._book_prepared_calendar(slot_info, route, user_inputs)
- recheck = self.query(slot_info.apt_type)
- if not recheck.success:
- self._log("Target Italy service is no longer available for this user Session.")
- res.label = BOOKING_RECHECK_NONE_AVAILABLE_LABEL
- return res
- self._inspect_booking_form_structure(self._booking_path(route))
- res.label = BOOKING_FORM_DISCOVERY_STOP_LABEL
- self._log(
- "Italy booking form reached; stopped before AVANTI for field verification."
- )
- return res
- # --- 资源清理核心方法 ---
- def cleanup(self):
- """
- 销毁浏览器并彻底删除临时文件
- """
- self._cleanup_attempted = True
- cleanup_failed = False
- # 1. 关闭浏览器
- if self.page:
- try:
- self.page.quit() # 这会关闭 Chrome 进程
- except Exception:
- # DrissionPage already handles an ordinary disconnection in
- # quit(). An additional failure must not silently permit a
- # second browser to take ownership of this persistent profile.
- cleanup_failed = True
- self.page = None
- self.is_healthy = False
- self._clear_calendar_query_state()
- self._reset_calendar_book_state()
- # 2. 删除文件
- # 注意:Chrome 关闭后可能需要几百毫秒释放文件锁,稍微等待
- if os.path.exists(self.root_workspace):
- for _ in range(3):
- try:
- time.sleep(0.2)
- shutil.rmtree(self.root_workspace, ignore_errors=True)
- break
- except Exception as e:
- # 如果删除失败(通常是Windows文件占用),重试
- self._log(f"Cleanup retry: {e}")
- time.sleep(0.5)
- # 如果依然存在,打印警告(虽然 ignore_errors=True 会掩盖报错,但可以 check exists)
- if os.path.exists(self.root_workspace):
- self._log(f"[WARN] Failed to fully remove workspace: {self.root_workspace}")
- cleanup_failed = True
- # 3. [新增] 关闭代理隧道
- if self.tunnel:
- try: self.tunnel.stop()
- except Exception: cleanup_failed = True
- self.tunnel = None
- self._release_persistent_profile_lock()
- if cleanup_failed:
- raise BizLogicError("Italy cleanup did not complete; automatic recreation is unsafe")
-
- def get_plugin_status(self) -> PluginStatus:
- account_name = self.config.account.username if self.config and self.config.account else ""
- proxy_info = f"{self.config.proxy.ip}:{self.config.proxy.port}" if self.config and self.config.proxy and self.config.proxy.ip else ""
- current_url = ""
- if self.page:
- try:
- current_url = self.page.url
- except Exception:
- pass
- state = getattr(self, "_query_stop_reason", None) or ("running" if self.is_healthy else "unhealthy")
- return PluginStatus(
- instance_id=self.instance_id,
- is_healthy=self.is_healthy,
- state=state,
- current_url=self._url_for_log(current_url) if current_url else "",
- account=account_name,
- proxy=proxy_info,
- session_age_sec=0,
- last_active_at=utc_now_str(),
- )
- def __del__(self):
- """
- 析构函数:当对象被垃圾回收时自动调用
- """
- try:
- if not getattr(self, "_cleanup_attempted", False):
- self.cleanup()
- except Exception:
- # Explicit lifecycle owners report cleanup errors. A finalizer
- # must not retry business actions or emit unraisable tracebacks.
- pass
|