Introducing a new way to review your PRs.

Read the announcement

4gray / iptvnator

:tv: Cross-platform IPTV player application with multiple features, such as support of m3u and m3u8 playlists, favorites, TV guide, TV archive/catchup and more.

View on GitHub

26 documents · Updated Nov 23, 2026

docs / hosted-server-pairing-and-sessions

Hosted server pairing and sessions

Hosted-server mode lets the Electron app publish one shared IPTVnator library for browsers and Android devices. The host selects a local or remote access path, the web backend protects that path, and each client proves its right to use the library before it receives a credential. This page expands the hosted server and device access model across Electron, the browser backend, and the TV shell runtime.

Hosted server role

The hosted server is a shared library service owned by the desktop host. It keeps server data and device access under one instance identity while clients use the same source, playback, and sync APIs.

The host process starts the web backend as a separate worker. The worker opens the server database and serves both the web app and its API. Electron keeps the worker session in memory, so a worker restart creates a new instance boundary for clients that use the local endpoint.

The main actors have distinct duties:

ActorResponsibility
Electron hostSelects the access mode, starts or stops the worker, and manages invites and paired devices.
Web backendServes the hosted app, checks ingress, owns pairing state, and issues or checks device credentials.
Browser clientDiscovers the server, completes browser pairing, and stores its connection for later use.
Android TV shellDiscovers the server, runs native pairing or Access sign-in, and proxies remote API traffic from its WebView.

Mode selection

Hosted mode chooses where clients connect and which trust boundary protects that connection. The settings surface presents Home network, custom HTTPS, and Cloudflare Access tunnel modes; a PC-only local listener remains available when no sharing mode is selected. The self-hosted source synchronization page describes how a selected server becomes the source of shared data.

Access modes

The access modes provide different reach and trust properties:

ModeReachMain trust step
Local onlyThe host computerElectron supplies a local access code to its own web client.
Home networkA selected private LAN addressThe device must pair. Open LAN profiles are upgraded to paired access.
Custom HTTPSA configured HTTPS origin, either directly or through an external proxyTLS and the exact configured origin protect the route.
Cloudflare AccessA public HTTPS origin behind AccessEdge sign-in comes first. A registered device key then identifies the client.

The selected LAN address must be a current private IPv4 interface. HTTPS origins must be a bare HTTPS origin without a path or embedded credentials. Cloudflare mode may use the bundled connector, but a connector marked running does not prove that the public route is ready.

Listener and ingress boundaries

The worker serves a local listener for the host and adds only the listener needed by the selected mode. A custom HTTPS route uses a second loopback listener when an external proxy owns TLS. Cloudflare mode uses a loopback listener that is released to the edge only after Access checks pass.

Every request is classified by the listener, not by client headers alone. A LAN request must come from the selected private subnet. A proxied request must carry the configured host and origin pair. Requests that do not match either boundary receive a denial before the hosted app or API handles them.

flowchart LR
    Host[Electron host] --> Worker[Hosted web worker]
    Worker --> Local[Local listener]
    Worker --> LAN[Private LAN listener]
    Worker --> Loopback[Loopback HTTPS listener]
    LAN --> Pairing[Paired device]
    Loopback --> Proxy[HTTPS proxy or Cloudflare Access]
    Proxy --> Remote[Remote browser or Android shell]
    Local --> Desktop[Host web client]

References: apps/…/hosted-server.service.ts• L82-199 apps/…/hosted-mode-config.ts• L35-96 apps/…/main.ts• L97-161 apps/…/hosted-listener.ts• L11-78

Device discovery and approval

Discovery separates “this is an IPTVnator server” from “this device may use its data.” Clients first inspect the instance endpoint without sending a pairing credential. The response identifies the server instance and its access capabilities, so the client can choose direct use, token entry, ordinary pairing, or Cloudflare sign-in.

Ordinary device pairing

Ordinary pairing is a short, two-party approval flow for browsers and Android devices on a LAN or custom HTTPS route. The host creates a one-use invitation. The new device turns that invitation into a pending claim, shows a six-digit code, and waits while the host approves the matching request.

The claim secret stays with the new device. The host sees the claim name and confirmation code, not the secret. After approval, the new device polls with its claim secret and receives a device token plus the server instance ID.

sequenceDiagram
    participant H as Host settings
    participant S as Hosted backend
    participant D as New device
    H->>S: Create invitation
    S-->>H: Short-lived pairing link
    H-->>D: QR code or pairing link
    D->>S: Claim invitation
    S-->>D: Claim secret and confirmation code
    D-->>H: Show code in pending request
    H->>S: Approve matching claim
    loop Until approved or expired
        D->>S: Complete claim
        S-->>D: Pending or device token
    end

Browser pairing saves the token and instance-bound connection in browser storage. Android saves the token in its encrypted credential store. Revoking the device removes ordinary server access and requires a new invitation.

Cloudflare device registration

Cloudflare mode adds two checks to ordinary discovery: the public request must pass Cloudflare Access, and the client must have a registered device key. A browser receives a durable device cookie. Android creates a device key and binds it to its Access credential.

The first Access sign-in uses the configured invitation when a new device must be registered. A paired Cloudflare device with approval permission can create a registration invite for another device. The host can grant or remove that approval permission without granting it to every paired device.

After registration, the client uses its Access session and device key for library requests. A revoked key stays distinguishable from an unknown key, so the client can show “revoked” rather than silently treating the problem as a network failure.

Android TV approval

The TV shell starts a claim when the user chooses a hosted library that is not already paired. It shows the confirmation code and a QR approval link when the server can provide one. A paired phone can scan and approve the TV; an HTTPS browser can approve it with the phone approval password; the host computer remains the final fallback approver.

The TV never receives an approver credential. It keeps only its own claim secret while it polls for approval. A successful poll stores the TV device credential and selects the hosted library. Cancelling or replacing the flow leaves the current library selection unchanged.

References: apps/…/device-pairing-store.ts• L31-122 apps/…/device-pairing-router.ts• L94-114 apps/…/cloudflare-device-access.ts• L44-105 tv-shell/…/HostedPairingFlow.java• L83-177

Credential lifecycle

Credentials have different lifetimes because they serve different trust tasks. A pairing claim is temporary. A paired device token or Cloudflare refresh credential represents continued access. An instance ID binds that credential to the server database that issued it.

Host and browser credentials

The Electron host keeps the local access code and worker request path in the current worker session. It does not expose that local code as a remote device credential. Host settings call the worker directly when possible, then use the local API boundary for legacy local control.

The browser receives an ordinary device token only after approval. The backend stores a digest of that token. The browser stores the token beside the instance-bound API connection, then sends it for later source requests. Clearing browser storage or forgetting the server removes the local copy; revoking the device removes the server copy.

HTTPS browser approval uses a separate phone approval password. The backend stores a password hash and gives a successful browser sign-in a short-lived secure session cookie. That session can approve the pending claim but does not become a library token.

Android credentials

The Android shell encrypts ordinary device records with a non-exportable Android Keystore key. Each record is tied to the server origin and instance ID. A token from another origin or instance is not a valid reconnect credential.

Cloudflare Android access stores the Access token set, refresh data, instance identity, and device key as one protected record. The shell refreshes the access token when it expires or when the edge rejects it. It replaces the record only if the stored pairing has not changed, which prevents an old Activity from overwriting a newer sign-in.

Revocation and replacement

The host can revoke a device or change whether it may approve other Cloudflare devices. A revoked ordinary token stops working immediately. A revoked Cloudflare device reports its state to the shell, which forgets the current record and returns the user to connection settings. An unknown Cloudflare device must use a new invitation.

The instance ID protects against stale credentials after the host database or worker changes. Electron rejects a changed local instance, and Android asks the user to pair again instead of attaching the old token to a different library.

References: apps/…/hosted-server-session.ts• L8-43 apps/…/hosted-device-control.ts• L54-203 apps/…/device-pairing-store.ts• L124-199 tv-shell/…/PairedCredentialStore.java• L17-79 tv-shell/…/HostedAccessSession.java• L41-110

Pairing transports

The same approval model travels over different paths because the host, browser, and TV shell do not share one runtime. Each path limits where secrets can appear and keeps the active document or connection tied to the selected origin.

Electron and worker transport

Electron sends hosted control commands over a main-frame IPC boundary. The hosted worker handles API requests in its own process and returns bounded replies to Electron. Lifecycle operations are serialized, so start, stop, ingress changes, and device actions do not race one another.

This path lets the desktop UI manage pairing without putting the host access code into a public page. It also lets the host use the same backend pairing routes that remote devices use, while retaining a trusted local management path.

Browser transport

The ordinary browser follows a no-store pairing page. The invitation remains in the URL fragment until the page turns it into a claim; the fragment is then removed from the address bar. The browser polls the claim endpoint until the host approves it, stores the resulting token locally, and reloads the hosted app.

Cloudflare browser pairing uses the public HTTPS page and its device cookie. The page registers that browser only after the user presses the connect action. It does not place the device key or library token in the URL.

Android shell transport

The Android shell uses bounded JSON requests with redirects disabled. It confirms the exact origin before it sends a claim, and it sends approver credentials only to the dedicated approval routes. For a hosted WebView, hosted-access-api.js routes same-origin API calls through the native bridge. The native client then applies the stored ordinary token or Cloudflare Access session.

The bridge is scoped to the active main-frame document. When that document ends, pending requests stop. This keeps a previous hosted page from continuing to use a later page's session.

References: apps/…/hosted-server.service.ts• L319-400 apps/…/hosted-worker-client.ts• L9-35 apps/…/hosted-device-page.ts• L32-118 apps/…/hosted-cloudflare-device-page.ts• L18-73 tv-shell/…/PairingTransport.java• L11-73 tv-shell/…/hosted-access-api.js• L10-141

Recovery behavior

Recovery treats a failed attempt as separate from the selected library whenever possible. Expired claims, failed routes, stale credentials, and interrupted sign-in flows produce a new discovery or pairing step rather than silently changing the user's library.

Expired and superseded pairing

Invitations, claims, and browser approval sessions are short-lived. The Android shell also bounds its foreground pairing attempt. If a code expires, polling fails, or the user starts another attempt, the old flow is discarded and the user must create or scan a fresh link.

The host device list polls for pending claims and removes an invitation when its claim is consumed. A failed or cancelled attempt does not revoke an existing device and does not alter the current library connection.

Worker and ingress failure

Electron reports distinct stopped, starting, running, and error states for the worker. A missing port, invalid TLS identity, failed worker start, or failed managed connector prevents a partially configured route from being presented as ready. If a listener setup step fails, the worker closes the listeners and storage it already opened.

Cloudflare route status is checked after startup. An unverified or still-provisioning route can leave the worker running while pairing remains unavailable. The user must restore the Access route before creating a new public invite.

Client reconnect failure

Android first probes the instance endpoint without credentials. It can report Access sign-in, missing pairing, revoked device, wrong library, incompatible server, certificate, or network failure as separate outcomes. If a remote hosted page cannot load, the shell can fall back to its embedded local library while preserving the selected remote origin for a later retry.

When an Access token expires, the shell refreshes it and retries the request once. An Access denial opens sign-in again. A revoked device clears the record; a device that needs registration starts the invitation flow. The settings and parental controls page is the place to connect this recovery behavior to user-facing settings.

References: apps/…/hosted-server.service.ts• L127-217 apps/…/hosted-server.service.ts• L220-267 apps/…/device-pairing-store.ts• L64-77 tv-shell/…/HostedConnection.java• L30-93 tv-shell/…/MainActivity.java• L289-315 tv-shell/…/MainActivity.java• L670-725

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the mode schema, listener binding, public-route check, and settings labels aligned. A mode can appear selectable while its listener or pairing route is not usable.
  • Keep the instance ID contract aligned across /api/v1/instance, ordinary device tokens, Android records, and source-sync connections. A changed instance must cause re-pairing rather than reuse.
  • Keep invitation URLs and approval URLs aligned with their transport. LAN approval must remain bound to the paired private origin; public approval must remain HTTPS and Access-protected.
  • Keep ordinary device approval separate from Cloudflare device registration. The two stores have different revocation and approver rules.
  • Keep the native hosted API bridge scoped to the same origin and active document. Changes to request streaming or page navigation can otherwise leak requests across hosted sessions.
  • Keep token rotation and record replacement atomic on Android. A refresh from an old Activity must not replace a newer pairing.

Important failure modes

TriggerConsequenceGuard
The selected LAN address is no longer availableThe worker cannot expose the intended network routeValidate against private interfaces and report startup state before pairing.
A custom HTTPS certificate or key is invalidHTTPS ingress does not startValidate the TLS identity before marking the mode running.
Cloudflare Access or its public route is not readyThe worker may run, but public pairing is unavailableCheck the Access challenge and keep pairing disabled until the route is protected.
A pairing invitation or claim expiresThe device receives no credentialPrune pending state and require a fresh invitation.
A device is revokedExisting requests fail and the device must register againStore only token digests and report revoked state to the client.
The server instance changesA saved token points at the wrong libraryCompare the instance ID before reconnecting.
The Android hosted page is replaced or hiddenPending native API requests can outlive the pageEnd the bridge document session and cancel its pending work.
Access sign-in is denied or its token expiresHosted API requests cannot reach the libraryRefresh once, then require a new sign-in without changing the selected library.

Key files

FileWhy to read it
apps/…/hosted-server.service.tsShows how Electron selects, starts, stops, and reports the hosted worker.
apps/…/hosted-mode-config.tsDefines the accepted access modes and public-route checks.
apps/…/main.tsShows which listeners and ingress checks each mode creates.
apps/…/hosted-listener.tsDefines the network boundary for local, LAN, and proxied requests.
apps/…/device-pairing-store.tsExplains invitation, claim, approval, expiry, token digest, and revocation state.
apps/…/device-pairing-router.tsMaps browser, device, and host approval actions to the pairing contract.
apps/…/settings-hosted-server.component.tsShows the user-facing mode selection and route readiness model.
apps/…/settings-hosted-devices.component.tsShows invite creation, pending approval, device permissions, and revocation.
tv-shell/…/HostedPairingFlow.javaShows the Android TV and phone pairing lifecycle.
tv-shell/…/HostedAccessSession.javaShows Cloudflare credential refresh and library verification.
tv-shell/…/PairedCredentialStore.javaShows how Android protects and replaces saved credentials.
tv-shell/…/HostedAccessBridge.javaShows how hosted WebView API requests use the native session.