Overview
Session Synchronization is the critical validation phase that ensures captured protocol tokens are active and ready for use. After Protocol Interception captures authentication tokens, APITHON must verify that the session is “hot” by triggering actual backend communication.Why Synchronization Is Needed
Simply capturing tokens from network traffic is insufficient for two key reasons:- Token Validation: The tokens may be stale, expired, or incomplete
- Backend Activation: The target service may require an initial “handshake” to establish streaming readiness
- Context Initialization: The internal context token needs to be confirmed as actively tracking conversation state
The Synchronization Function
Thesincronizar_tunel function (lines 54-100 in apithon.py) orchestrates the entire synchronization process:
The Three Phases
Based on the README (lines 28-32), APITHON operates through three critical phases:Phase 1: Interception (Sniffing)
Using Playwright, the system captures tokens and dynamic context from the target’s data flow.
Phase 2: Validation (Emulated)
The script automates sending a validation signal (”.” character) to immediately activate backend packet exchange.This is the core synchronization mechanism.
Phase 3: Bridging (Gateway)
A Flask server translates standard REST requests to the captured internal protocol, enabling interoperability.See Gateway Mode for details on this phase.
Automated Validation Process
The validation process automatically triggers backend communication to capture active session tokens.Input Detection
APItHON uses a flexible CSS selector to find the chat input field:- Content-editable divs: Modern rich-text chat interfaces
- Textareas: Traditional multi-line input fields
- Input elements: Simple single-line text boxes
The 15-second timeout gives the page time to fully load and authenticate before attempting validation.
The Validation Message
APItHON sends a single period (.) as the validation message:
- Minimal data payload
- Universally accepted as valid input
- Triggers backend processing without generating meaningful conversation
- Easy to identify in logs or chat history
Fallback to Manual Input
If automation fails (due to CAPTCHA, unusual UI, or timing issues), the user is notified:Session Monitoring Loop
After sending the validation message, APITHON enters a polling loop waiting for token capture:- Checks the
status_readyflag every 500ms - Continues until the
protocol_sniffersuccessfully captures all tokens - Prevents the gateway from starting prematurely
The synchronization is complete when
SESS["status_ready"] becomes True, which happens inside the protocol_sniffer function after all tokens are captured.Cookie and Context Management
Cookie Retrieval
During synchronization, all browser cookies for the target domain are extracted:Context Persistence
Theinternal_context token is the most critical piece captured during synchronization:
- Starts with
!character - Is 100+ characters long
- Contains alphanumeric characters, underscores, and hyphens
- Persists conversation state across multiple requests
Session State Storage
All synchronized session data is stored in the globalSESS dictionary:
In-Memory Only
Session data is stored exclusively in RAM and never written to disk.
Process Lifetime
Tokens are valid only for the current process lifetime. They’re lost when APITHON terminates.
URL Normalization
Before synchronization begins, the input URL is normalized:app.example.com→https://app.example.comhttps://app.example.com→https://app.example.com(unchanged)
Error Handling
Input Selector Not Found
If the input field isn’t detected within 15 seconds:Synchronization Timeout
If tokens are never captured (due to network issues, incorrect URL, or authentication failure), the loop continues indefinitely:Ctrl+C to abort and restart with corrections.
Synchronization Success
When synchronization completes successfully:- All four tokens are captured and stored
- The browser instance is closed (resources freed)
- The
status_readyflag isTrue - The system is ready to enter Gateway Mode
Diagram: Synchronization Flow
Next Steps
After successful synchronization, the captured tokens are ready to be used in Gateway Mode to translate external API requests into the target’s internal protocol.Protocol Interception
Review how tokens are captured during synchronization
Gateway Mode
Learn how synchronized sessions power the API gateway