| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603 |
- """Shared Italy form preparation and passive request audit.
- Extracted from the real-tested Calendar gate. Production and diagnostic tools
- use the same protocol; no tool imports, credentials, or network calls on import.
- """
- from __future__ import annotations
- import json
- import time
- from enum import Enum
- from threading import Lock
- from urllib.parse import parse_qs, urljoin, urlparse
- from plugins.ita_calendar_query import (
- AVAILABILITY_PATH, CALENDAR_PATH, CalendarGateError,
- header_value as _header, same_origin_path as _same_origin_path,
- )
- from plugins.ita_protocol import ItaRouteConfig
- OTP_PATH = "/BookingCalendar/GenerateOTP"
- def booking_preflight_state(raw: object) -> str:
- """Classify observed readiness, not the unknown cause of a widget failure."""
- if not isinstance(raw, dict):
- return "snapshot_unavailable"
- token, disabled = raw.get("turnstileTokenPresent"), raw.get("avantiDisabled")
- if type(token) is not bool or type(disabled) is not bool:
- return "invalid_snapshot"
- if not token:
- return "turnstile_and_avanti_pending" if disabled else "turnstile_token_pending"
- return "avanti_disabled" if disabled else "ready"
- def safe_booking_preflight_diagnostic(raw: object, *, stage, outcome: str) -> dict:
- """Only bounded booleans/counts and fixed categories; never token/value/HTML."""
- snapshot = raw if isinstance(raw, dict) else {}
- result = {
- "contract": "ita-book-preflight-v2",
- "stage": stage.value if isinstance(stage, BookingPreflightStage) else "invalid",
- "outcome": outcome if outcome in {"ready", "waiting", "rejected", "timeout", "read_failed"} else "invalid",
- "state": booking_preflight_state(raw),
- }
- for key in ("pathMatches", "formPresent", "privacyChecked", "otpDisabled", "otpValuePresent",
- "turnstileTokenPresent", "avantiDisabled", "turnstileApiPresent",
- "turnstileScriptPresent", "documentVisible"):
- value = snapshot.get(key)
- result[key] = value if type(value) is bool else None
- for key in ("turnstileCount", "avantiButtonCount", "unknownRequiredCount"):
- value = snapshot.get(key)
- result[key] = value if type(value) is int and 0 <= value <= 1000 else None
- return result
- def _print_booking_preflight_diagnostic(raw: object, *, stage, outcome: str) -> None:
- print("ITA_BOOK_PREFLIGHT_DIAGNOSTIC=" + json.dumps(
- safe_booking_preflight_diagnostic(raw, stage=stage, outcome=outcome), sort_keys=True,
- ), flush=True)
- def wait_for_booking_preflight(
- page: object,
- *,
- script: str,
- stage: BookingPreflightStage = None,
- timeout: float = 120,
- diagnostic=None,
- wait_action=None,
- check_current=None,
- ) -> dict[str, object]:
- """Wait without relaxing structure; optional bounded form interaction.
- An interaction return value is never evidence of readiness: only the next
- validated site snapshot can permit OTP/AVANTI. No recovery is enabled by
- default; callers must explicitly supply their preparation recovery.
- """
- stage = stage if stage is not None else BookingPreflightStage.BEFORE_OTP_SEND
- report = diagnostic or _print_booking_preflight_diagnostic
- started = time.monotonic()
- deadline = started + timeout
- announced = False
- raw = None
- last_state = None
- next_report = started
- while (now := time.monotonic()) < deadline:
- if check_current is not None:
- check_current()
- try:
- raw = page.run_js(script)
- except Exception:
- report(None, stage=stage, outcome="read_failed")
- raise CalendarGateError("Italy booking preflight read_failed") from None
- if check_current is not None:
- check_current()
- try:
- _validate_preflight_except_dynamic_token(raw, stage=stage)
- except CalendarGateError:
- report(
- raw,
- stage=stage,
- outcome="rejected",
- )
- raise
- if raw.get("turnstileTokenPresent") and not raw.get("avantiDisabled"):
- try:
- validate_booking_preflight(raw, stage=stage)
- except CalendarGateError:
- report(
- raw,
- stage=stage,
- outcome="rejected",
- )
- raise
- report(
- raw,
- stage=stage,
- outcome="ready",
- )
- return raw
- if not announced:
- print("ITA_BOOK_WAITING_FOR_FORM: watch the opened browser; complete any human verification. "
- "Do not click OTP, AVANTI, or final Prenota.", flush=True)
- announced = True
- state = booking_preflight_state(raw)
- if state != last_state or now >= next_report:
- report(raw, stage=stage, outcome="waiting")
- last_state, next_report = state, now + 15
- if wait_action is not None:
- wait_action(raw, stage=stage, elapsed=now - started, remaining=deadline - now)
- time.sleep(0.5)
- report(raw, stage=stage, outcome="timeout")
- raise CalendarGateError(f"Italy booking preflight timed out: {booking_preflight_state(raw)}")
- def guarded_preflight_script(route: ItaRouteConfig, booking_path: str) -> str:
- """Check scope before the reused script changes any visible form control."""
- prefix = """
- /* coordinate:ita-book-gate-form-boundary */
- const bookGateForms = document.querySelectorAll('#bookingForm');
- const bookGateForm = bookGateForms.length === 1 ? bookGateForms[0] : null;
- let bookGateAction = null;
- try { bookGateAction = bookGateForm ? new URL(bookGateForm.action, location.href) : null; }
- catch (_) {}
- if (location.origin !== 'https://prenotami.esteri.it' ||
- location.pathname !== BOOK_GATE_PATH || !bookGateForm ||
- bookGateForm.method.toUpperCase() !== 'POST' ||
- bookGateForm.enctype !== 'multipart/form-data' ||
- !bookGateAction || bookGateAction.origin !== location.origin ||
- bookGateAction.pathname !== BOOK_GATE_PATH || bookGateAction.search || bookGateAction.hash ||
- bookGateForm.querySelectorAll('input[type="file"]').length !== 0) {
- return {pathMatches: false, formPresent: false};
- }
- """.replace("BOOK_GATE_PATH", json.dumps(booking_path))
- return prefix + _booking_preflight_script(route, booking_path)
- class BookingPreflightStage(Enum):
- BEFORE_OTP_SEND = "before_otp_send"
- AFTER_OTP_SEND = "after_otp_send"
- class CalendarRequestAudit:
- """Passively count business requests across refreshes; never retain bodies.
- The production page.listen owns a separate CDP connection. This diagnostic
- callback uses the page driver and refuses to replace an existing handler.
- It never enables Fetch interception or changes any browser request.
- """
- EVENT = "Network.requestWillBeSent"
- def __init__(self, page: object, *, include_time_slots: bool = False) -> None:
- self.page = page
- self.driver = page.driver
- self._callback = self._on_request
- self._lock = Lock()
- self._started = False
- self._invalid_event = False
- self._counts = {
- "otpRequests": 0,
- "avantiPosts": 0,
- "availabilityRequests": 0,
- "finalRequests": 0,
- }
- if include_time_slots:
- self._counts["timeSlotRequests"] = 0
- def enable_time_slot_counting(self) -> None:
- """Opt in for the soak; existing gate counter shapes stay unchanged."""
- with self._lock:
- self._counts.setdefault("timeSlotRequests", 0)
- def start(self) -> None:
- if self._started:
- raise CalendarGateError("Passive request audit was already started")
- for name in ("event_handlers", "immediate_event_handlers"):
- handlers = getattr(self.driver, name, None)
- if not isinstance(handlers, dict) or self.EVENT in handlers:
- raise CalendarGateError(
- "Passive request audit cannot replace an existing callback"
- )
- self.driver.set_callback(self.EVENT, self._callback, immediate=True)
- self._started = True
- try:
- self.page.run_cdp("Network.enable")
- except Exception:
- self.stop()
- raise
- def stop(self) -> None:
- if not self._started:
- return
- if self.driver.immediate_event_handlers.get(self.EVENT) is not self._callback:
- raise CalendarGateError("Passive request audit callback changed")
- self.driver.set_callback(self.EVENT, None, immediate=True)
- self._started = False
- def _on_request(self, **event: object) -> None:
- request = event.get("request")
- if (
- not isinstance(request, dict)
- or not isinstance(request.get("url"), str)
- or not isinstance(request.get("method"), str)
- ):
- with self._lock:
- self._invalid_event = True
- return
- try:
- path, _ = _same_origin_path(request["url"])
- except CalendarGateError:
- return
- method = request["method"].upper()
- key = None
- if path == OTP_PATH:
- key = "otpRequests"
- elif path == "/BookingCalendar/InsertNewBooking":
- key = "finalRequests"
- elif method == "POST" and path.startswith("/Services/Booking/"):
- key = "avantiPosts"
- elif path == AVAILABILITY_PATH:
- key = "availabilityRequests"
- elif path == "/BookingCalendar/RetrieveTimeSlots" and "timeSlotRequests" in self._counts:
- key = "timeSlotRequests"
- if key is not None:
- with self._lock:
- self._counts[key] += 1
- def require_active_on(self, page: object) -> None:
- """Validate a borrowed audit without registering or removing callbacks."""
- if self.page is not page or self.driver is not getattr(page, "driver", None):
- raise CalendarGateError("Passive request audit belongs to another page or driver")
- if not self._started or self.driver.is_running is not True:
- raise CalendarGateError("Passive request audit is not connected")
- immediate = getattr(self.driver, "immediate_event_handlers", None)
- regular = getattr(self.driver, "event_handlers", None)
- if (not isinstance(immediate, dict) or immediate.get(self.EVENT) is not self._callback
- or not isinstance(regular, dict) or self.EVENT in regular):
- raise CalendarGateError("Passive request audit callback changed")
- def snapshot(self) -> dict[str, int]:
- self.require_active_on(self.page)
- with self._lock:
- if self._invalid_event:
- raise CalendarGateError("Passive request audit received an invalid event")
- return dict(self._counts)
- def retained_snapshot(self) -> dict[str, int]:
- """Best-effort last observed counters, not proof after a disconnect."""
- with self._lock:
- if self._invalid_event:
- raise CalendarGateError("Passive request audit received an invalid event")
- return dict(self._counts)
- def settled_snapshot(self, *, timeout: float = 3) -> dict[str, int]:
- # A read-only CDP round trip plus a bounded stable-count window lets
- # already-arriving callbacks catch up before a per-query comparison.
- self.page.run_cdp("Page.getFrameTree")
- deadline = time.monotonic() + timeout
- previous = self.snapshot()
- stable_since = time.monotonic()
- while time.monotonic() < deadline:
- time.sleep(0.1)
- current = self.snapshot()
- if current != previous:
- previous = current
- stable_since = time.monotonic()
- elif time.monotonic() - stable_since >= 0.3:
- return current
- raise CalendarGateError("Passive business-request counts did not settle")
- def validate_otp_packet(packet: object, *, service_id: int) -> None:
- path, parsed = _same_origin_path(packet.url)
- if str(packet.method).upper() != "GET" or path != OTP_PATH:
- raise CalendarGateError("OTP request method or path changed")
- if parse_qs(parsed.query) != {"ServiceID": [str(service_id)]}:
- raise CalendarGateError("OTP request service id changed")
- if packet.response.status != 200:
- raise CalendarGateError("OTP response status was not 200")
- raw_body = packet.response.raw_body
- if not isinstance(raw_body, str):
- raise CalendarGateError("OTP response body type changed")
- try:
- decoded = json.loads(raw_body)
- except json.JSONDecodeError as exc:
- raise CalendarGateError("OTP response was not the observed JSON string") from exc
- if decoded != "SENT":
- raise CalendarGateError("OTP response did not confirm SENT")
- def validate_avanti_packet(
- packet: object,
- *,
- booking_path: str,
- final_page_url: str,
- ) -> str:
- path, request_url = _same_origin_path(packet.url)
- if (
- str(packet.method).upper() != "POST"
- or path != booking_path
- or request_url.query
- or request_url.fragment
- ):
- raise CalendarGateError("AVANTI request method or path changed")
- status = packet.response.status
- if status == 302:
- raw_location = _header(packet.response.headers, "location")
- redirect_path, _ = _same_origin_path(
- urljoin("https://prenotami.esteri.it", raw_location)
- )
- if redirect_path != CALENDAR_PATH:
- raise CalendarGateError("AVANTI redirect did not target Calendar")
- transition_mode = "direct_302"
- elif status == 200:
- response_path, _ = _same_origin_path(
- getattr(packet.response, "url", "")
- )
- if response_path != CALENDAR_PATH:
- raise CalendarGateError(
- "AVANTI followed response did not target Calendar"
- )
- transition_mode = "followed_redirect_200"
- else:
- raise CalendarGateError(
- "AVANTI response status was not an allowed redirect representation"
- )
- final_path, _ = _same_origin_path(final_page_url)
- if final_path != CALENDAR_PATH:
- raise CalendarGateError("AVANTI browser did not finish on Calendar")
- return transition_mode
- def validate_booking_preflight(
- raw: object,
- *,
- stage: BookingPreflightStage = BookingPreflightStage.BEFORE_OTP_SEND,
- ) -> None:
- """Validate shared form shape and the stage-sensitive OTP-send control."""
- if not isinstance(raw, dict):
- raise CalendarGateError("Booking preflight did not return an object")
- if not isinstance(stage, BookingPreflightStage):
- raise CalendarGateError("Booking preflight stage is invalid")
- true_fields = (
- "pathMatches",
- "formPresent",
- "bookingTypeOptionMatched",
- "purposeOptionMatched",
- "bookingVisibleMatches",
- "bookingHiddenMatches",
- "companionsHiddenIsZero",
- "purposeVisibleMatches",
- "purposeHiddenIsPreSubmit",
- "privacyChecked",
- "turnstileTokenPresent",
- )
- for field_name in true_fields:
- if raw.get(field_name) is not True:
- raise CalendarGateError(
- f"Booking preflight rejected {field_name}"
- )
- for field_name in (
- "otpInputCount",
- "otpButtonCount",
- "avantiButtonCount",
- "turnstileCount",
- ):
- if raw.get(field_name) != 1:
- raise CalendarGateError(
- f"Booking preflight rejected {field_name}"
- )
- otp_disabled = raw.get("otpDisabled")
- if not isinstance(otp_disabled, bool) or (
- stage is BookingPreflightStage.BEFORE_OTP_SEND
- and otp_disabled
- ):
- raise CalendarGateError(
- "Booking preflight rejected otpDisabled"
- )
- if raw.get("avantiDisabled") is not False:
- raise CalendarGateError(
- "Booking preflight rejected avantiDisabled"
- )
- otp_value_present = raw.get("otpValuePresent")
- expected_otp_value_present = (
- stage is BookingPreflightStage.AFTER_OTP_SEND
- )
- if (
- not isinstance(otp_value_present, bool)
- or otp_value_present is not expected_otp_value_present
- ):
- raise CalendarGateError(
- "Booking preflight rejected otpValuePresent"
- )
- if raw.get("unknownRequiredCount") != 0:
- raise CalendarGateError(
- "Booking preflight rejected unknownRequiredCount"
- )
- def _booking_preflight_script(
- route: ItaRouteConfig,
- booking_path: str,
- ) -> str:
- if route.booking_type is None or route.purpose is None:
- raise CalendarGateError("Q-CAL route is missing form controls")
- config = {
- "bookingPath": booking_path,
- "bookingTypeId": route.booking_type.control_id,
- "bookingTypeValue": route.booking_type.value,
- "bookingTypeLabels": list(route.booking_type.labels),
- "purposeId": route.purpose.control_id,
- "purposeValue": route.purpose.value,
- "purposeLabels": list(route.purpose.labels),
- }
- config_json = json.dumps(config, ensure_ascii=False)
- return f"""
- /* coordinate:ita-qcal-booking-preflight */
- const cfg = {config_json};
- const normalize = (value) => String(value || '')
- .replace(/\\s+/g, ' ').trim().toLocaleLowerCase();
- const form = document.querySelector('#bookingForm');
- const booking = document.getElementById(cfg.bookingTypeId);
- const purpose = document.getElementById(cfg.purposeId);
- const bookingOption = booking
- ? Array.from(booking.options).find(
- (option) => option.value === cfg.bookingTypeValue
- )
- : null;
- const purposeOption = purpose
- ? Array.from(purpose.options).find(
- (option) => option.value === cfg.purposeValue
- )
- : null;
- const bookingLabels = cfg.bookingTypeLabels.map(normalize);
- const purposeLabels = cfg.purposeLabels.map(normalize);
- const bookingTypeOptionMatched = Boolean(
- bookingOption && bookingLabels.includes(normalize(bookingOption.textContent))
- );
- const purposeOptionMatched = Boolean(
- purposeOption && purposeLabels.includes(normalize(purposeOption.textContent))
- );
- if (bookingTypeOptionMatched && booking.value !== cfg.bookingTypeValue) {{
- booking.value = cfg.bookingTypeValue;
- booking.dispatchEvent(new Event('change', {{bubbles: true}}));
- }}
- if (purposeOptionMatched && purpose.value !== cfg.purposeValue) {{
- purpose.value = cfg.purposeValue;
- purpose.dispatchEvent(new Event('change', {{bubbles: true}}));
- }}
- const privacy = document.getElementById('PrivacyCheck');
- if (privacy && !privacy.checked) privacy.click();
- const allowedRequiredIds = new Set([
- cfg.bookingTypeId,
- cfg.purposeId,
- 'otp-input',
- 'PrivacyCheck',
- 'hiddenTipoPrenotazione',
- 'numAccSelected',
- 'hiddenSelezionato_0',
- ]);
- const allowedRequiredNames = new Set([
- 'otp-input',
- 'PrivacyCheck',
- 'cf-turnstile-response',
- '__RequestVerificationToken',
- ]);
- const unknownRequired = form
- ? Array.from(form.querySelectorAll('[required]')).filter((element) =>
- !allowedRequiredIds.has(element.id) &&
- !allowedRequiredNames.has(element.name)
- )
- : [];
- const bookingHidden = document.getElementById('hiddenTipoPrenotazione');
- const companionsHidden = document.getElementById('numAccSelected');
- const purposeHidden = document.getElementById('hiddenSelezionato_0');
- const otpInputs = document.querySelectorAll('#otp-input');
- const otpButtons = document.querySelectorAll('#otp-send');
- const avantiButtons = document.querySelectorAll('#btnAvanti');
- const turnstile = document.querySelectorAll(
- 'input[name="cf-turnstile-response"]'
- );
- return {{
- pathMatches: window.location.pathname === cfg.bookingPath,
- formPresent: Boolean(form),
- bookingTypeOptionMatched,
- purposeOptionMatched,
- bookingVisibleMatches: Boolean(
- booking && booking.value === cfg.bookingTypeValue
- ),
- bookingHiddenMatches: Boolean(
- bookingHidden && bookingHidden.value === cfg.bookingTypeValue
- ),
- companionsHiddenIsZero: Boolean(
- companionsHidden && companionsHidden.value === '0'
- ),
- purposeVisibleMatches: Boolean(
- purpose && purpose.value === cfg.purposeValue
- ),
- purposeHiddenIsPreSubmit: Boolean(
- purposeHidden && ['0', cfg.purposeValue].includes(purposeHidden.value)
- ),
- privacyChecked: Boolean(privacy && privacy.checked),
- otpInputCount: otpInputs.length,
- otpValuePresent: Boolean(
- otpInputs.length === 1 && String(otpInputs[0].value || '').trim()
- ),
- otpButtonCount: otpButtons.length,
- otpDisabled: otpButtons.length === 1 ? otpButtons[0].disabled : null,
- avantiButtonCount: avantiButtons.length,
- avantiDisabled:
- avantiButtons.length === 1 ? avantiButtons[0].disabled : null,
- turnstileCount: turnstile.length,
- turnstileTokenPresent: Boolean(
- turnstile.length === 1 && turnstile[0].value
- ),
- turnstileApiPresent: Boolean(window.turnstile),
- turnstileScriptPresent: Boolean(document.querySelector(
- 'script[src^="https://challenges.cloudflare.com/turnstile/"]'
- )),
- documentVisible: document.visibilityState === 'visible',
- unknownRequiredCount: unknownRequired.length,
- }};
- """
- def _validate_preflight_except_dynamic_token(
- raw: object,
- *,
- stage: BookingPreflightStage,
- ) -> None:
- if not isinstance(raw, dict):
- raise CalendarGateError("Booking preflight did not return an object")
- for key in ("turnstileTokenPresent", "avantiDisabled"):
- if type(raw.get(key)) is not bool:
- raise CalendarGateError(f"Booking preflight rejected {key}")
- candidate = dict(raw)
- candidate["turnstileTokenPresent"] = True
- candidate["avantiDisabled"] = False
- validate_booking_preflight(candidate, stage=stage)
- def _wait_for_calendar(page: object, timeout: float = 30) -> None:
- deadline = time.monotonic() + timeout
- while time.monotonic() < deadline:
- if (urlparse(str(page.url or "")).path.rstrip("/") or "/") == CALENDAR_PATH:
- page.wait.doc_loaded()
- return
- time.sleep(0.25)
- raise CalendarGateError("AVANTI did not reach Calendar before timeout")
- def _element_or_fail(page: object, selector: str) -> object:
- element = page.ele(selector, timeout=5)
- if not element:
- raise CalendarGateError(f"Required element is missing: {selector}")
- return element
- def _read_manual_otp() -> str:
- otp = input(
- "Italy booking OTP "
- "(visible; terminal scrollback may retain it): "
- ).strip()
- if not otp.isdigit() or not 4 <= len(otp) <= 12:
- raise CalendarGateError("OTP must be 4-12 digits")
- return otp
- def _enter_otp(plugin: object, otp: str) -> None:
- """Focus, type, and compare in-page without returning the OTP value."""
- otp_input = _element_or_fail(plugin.page, "#otp-input")
- plugin.mouse.human_click_ele(otp_input)
- otp_input.input(otp, clear=True)
- value_matches = otp_input.run_js(
- "return this.value === arguments[0];",
- otp,
- )
- if value_matches is not True:
- raise CalendarGateError("OTP DOM write verification failed")
- print("ITA_QCAL_OTP_DOM_WRITE_VERIFIED matched=true")
|