diff --git a/dev/rest-api/authentication/index.html b/dev/rest-api/authentication/index.html index 37adf191..6a2cf3c0 100644 --- a/dev/rest-api/authentication/index.html +++ b/dev/rest-api/authentication/index.html @@ -2696,7 +2696,9 @@ The CORS headers expose these headers and Retry-After to a browser

A --remote client that gets a 429 with Retry-After shows a countdown, waits that time -(one hour at most), and then sends the request again. It stops after 3 waits for one request.

+(one hour at most), and then sends the request again. It stops after 3 waits for one request. +The keep-alive, the poll for CDM calls, the health check and the drain check do not wait: each +tries again at its next interval.

429 is not the RATE_LIMITED error body

RATE_LIMITED in Errors is a service rate limit that a handler raises, and it diff --git a/dev/rest-api/endpoints/index.html b/dev/rest-api/endpoints/index.html index e06aa545..6cd54e7f 100644 --- a/dev/rest-api/endpoints/index.html +++ b/dev/rest-api/endpoints/index.html @@ -1960,6 +1960,17 @@ + + +

  • + + + + GET /api/session/{session_id}/channel + + + +
  • @@ -2875,6 +2886,17 @@ +
  • + +
  • + + + + GET /api/session/{session_id}/channel + + + +
  • @@ -4896,15 +4918,16 @@ answers the prompt within 10 minutes.

    The server holds remote sessions in memory. They expire after serve.session_ttl (default 300s) without a request, except while awaiting interactive authentication input, which uses a 600s window. A remote session binds to the IP that made it, so the server rejects a request from a different IP with 403 FORBIDDEN.

    Remote session lifecycle overview

    POST /api/session/create        → returns session_id, status "authenticating"
    -GET  /api/session/{id}/prompt   → poll until authenticated (submit answers via POST)
    -GET  /api/session/{id}/titles   → list titles
    -POST /api/session/{id}/tracks   → tracks + chapters (with URLs) for one title
    -POST /api/session/{id}/segments → resolve segment URLs / DRM / headers per track
    -POST /api/session/{id}/segment_filter → HLS segments the service drops (ads, bumpers)
    -POST /api/session/{id}/samples  → decrypt cbcs samples of a track on the server
    -POST /api/session/{id}/license  → proxy or server-CDM DRM licensing
    -POST /api/session/{id}/keys/bad → report a server-vault content key that did not decrypt
    -DELETE /api/session/{id}        → tear down, harvest updated cache
    +GET  /api/session/{id}/channel  → WebSocket: prompts, CDM calls, logs, login state, drain
    +GET  /api/session/{id}/prompt   → without the channel: poll until authenticated (submit answers via POST)
    +GET  /api/session/{id}/titles   → list titles
    +POST /api/session/{id}/tracks   → tracks + chapters (with URLs) for one title
    +POST /api/session/{id}/segments → resolve segment URLs / DRM / headers per track
    +POST /api/session/{id}/segment_filter → HLS segments the service drops (ads, bumpers)
    +POST /api/session/{id}/samples  → decrypt cbcs samples of a track on the server
    +POST /api/session/{id}/license  → proxy or server-CDM DRM licensing
    +POST /api/session/{id}/keys/bad → report a server-vault content key that did not decrypt
    +DELETE /api/session/{id}        → tear down, harvest updated cache
     

    Slow responses

    The titles, tracks and license routes can take minutes on a large catalogue. When the @@ -4965,8 +4988,36 @@ the newlines must go out at once.

    +

    GET /api/session/{session_id}/channel

    +

    Open the remote session channel: a WebSocket that carries the prompts, CDM calls, log records, login state and drain state of the remote session, and the client's answers. Pass since=<seq> to get only the log records newer than that sequence number. The server closes the WebSocket with code 4404 when the remote session ends. See The remote session channel for the messages.

    + + + + + + + + + + + + + + + + + + + + + + + + + +
    StatusError codeMeaning
    101-WebSocket open.
    403FORBIDDENRequest IP differs from the remote session creator.
    404SESSION_NOT_FOUNDNo such session.

    GET /api/session/{session_id}/prompt

    -

    Poll for the authentication status and any pending interactive prompt (OTP, PIN, device code). Response variants:

    +

    The polling fallback of the remote session channel. Poll for the authentication status and any pending interactive prompt (OTP, PIN, device code). Response variants:

    @@ -5050,7 +5101,7 @@ the newlines must go out at once.

    GET /api/session/{session_id}/logs

    -

    Drain the service's server-side log output for this remote session. The server mirrors every log call the service makes (self.log.info, warnings, errors) into a bounded per-session buffer, so a remote client can see why an auth, title, or license step failed. Pass since=<seq> to get only records newer than the last drained sequence number. Works in every auth state, including failed.

    +

    The polling fallback of the remote session channel. Drain the service's server-side log output for this remote session. The server mirrors every log call the service makes (self.log.info, warnings, errors) into a bounded per-session buffer, so a remote client can see why an auth, title, or license step failed. Pass since=<seq> to get only records newer than the last drained sequence number. Works in every auth state, including failed.

    diff --git a/dev/rest-api/errors/index.html b/dev/rest-api/errors/index.html index 9d30af28..ef59ebcc 100644 --- a/dev/rest-api/errors/index.html +++ b/dev/rest-api/errors/index.html @@ -2311,8 +2311,9 @@ reference. Each endpoint lists the specific codes it raises, and this page gives meanings of those codes.

    Where this comes from

    -

    unshackle/core/api/errors.py defines the error format, and the wrapper -that turns raised errors into responses lives in unshackle/core/api/routes.py. +

    unshackle/core/api/errors.py defines the error format, unshackle/core/remote_protocol.py +defines the error codes, and the wrapper that turns raised errors into responses lives in +unshackle/core/api/routes.py. Nothing on this page is invented. It reflects the actual server behaviour.

    The standard error shape

    diff --git a/dev/rest-api/index.html b/dev/rest-api/index.html index 88393bfd..3c2ed3af 100644 --- a/dev/rest-api/index.html +++ b/dev/rest-api/index.html @@ -2499,6 +2499,11 @@ request and response in full.

    GET +/api/session/{session_id}/channel +WebSocket of the remote session channel: prompts, CDM calls, logs, login state. + + +GET /api/session/{session_id}/logs Drain the service's server-side log output. diff --git a/dev/rest-api/remote-sessions/index.html b/dev/rest-api/remote-sessions/index.html index fd8db1c4..e3d2a9a4 100644 --- a/dev/rest-api/remote-sessions/index.html +++ b/dev/rest-api/remote-sessions/index.html @@ -1834,6 +1834,17 @@ +
  • + +
  • + + + + The remote session channel + + + +
  • @@ -2339,6 +2350,17 @@ +
  • + +
  • + + + + The remote session channel + + + +
  • @@ -2608,8 +2630,8 @@ runs in the background on the server.

    Some services need a one-time code, PIN, or device confirmation. When the server's auth flow asks for input, the remote session enters pending_input. The -client polls, displays the prompt to you locally, collects your answer, and -sends it back. The server's auth thread resumes with your response.

    +server sends the prompt to the client, the client shows it to you locally, collects +your answer, and sends it back. The server's auth thread resumes with your response.

    Enter the 6-digit code sent to your email: 483920
     

    A client older than the service_params change sends service options as @@ -2619,8 +2641,7 @@ profile) no longer reaches the service; the server logs a warning naming the dropped key. Update the client to send service options under service_params.

    The server also mirrors the service's own log output (self.log) into a -per-session buffer. The client drains it through -GET /api/session/{id}/logs after each step and prints the records +per-session buffer and sends each record to the client, which prints the records locally, so you see the server-side reason when a step fails.

    @@ -2649,9 +2670,9 @@ and return the content keys directly. See Server-side vs. proxied CDM below.

    -

    The client downloads, decrypts, and muxes locally. While it works, it sends a -keep-alive request to the remote session at an interval of one third of -session_ttl, so a long download does not let the remote session expire. On +

    The client downloads, decrypts, and muxes locally. While it works, the open +remote session channel keeps the remote session alive, +so a long download does not let the remote session expire. On completion, and also when dl exits early (--list, Ctrl+C, an error), it deletes the remote session. If the login belongs to the client, the DELETE request returns the updated cache files (for example a refreshed token) and the @@ -2755,8 +2776,9 @@ the wire:

  • CDM calls. When the service calls the CDM, the server sends a cdm_call to the client and waits. The client makes the challenge, or parses the licence, on its own CDM. When the service closes a CDM session, the server sends a close call, because a CDM holds - only a few sessions. The client polls for calls during login and while one of its own - requests runs service code, so an idle remote session sends nothing extra.
  • + only a few sessions. The server sends each call on the remote session channel. Without + the channel, the client polls for calls during login and while one of its own requests + runs service code, so an idle remote session sends nothing extra.
  • Session keys only. A keys call returns Widevine OPERATOR_SESSION and SIGNING keys. It never returns a content key, and the client refuses a PlayReady keys call. So a server cannot use your device to license titles for itself.
  • @@ -2802,12 +2824,12 @@ generally never touch this directly, but it explains behaviour you might observe Idle session TTL 300s (5 min) default -Refreshed on every request to the session and when a titles or tracks request ends. A remote session does not expire while a request works on it +Refreshed on every request to the session and when a titles or tracks request ends. A remote session does not expire while a request works on it or while its remote session channel is open Max concurrent sessions 100 default -Oldest (least recently used) is evicted when full. A remote session that a request works on goes last +When full, the least recently used remote session of the API key that holds the most is evicted. A remote session that a request works on goes last Auth/input timeout @@ -2942,18 +2964,23 @@ from a browser origin, not when you want only the local HTTP API.

    GET +/api/session/{id}/channel +WebSocket of the remote session channel: the messages the server starts, and the client's answers; ?since= is the last log seq the client has + + +GET /api/session/{id}/logs -Drain the service's server-side log output +Drain the service's server-side log output (polling fallback) GET /api/session/{id}/prompt -Poll interactive auth status / pending prompt +Poll interactive auth status / pending prompt (polling fallback) POST /api/session/{id}/prompt -Submit an answer to a pending prompt +Submit an answer to a pending prompt (polling fallback) GET @@ -3005,8 +3032,9 @@ a per-profile ESN.

    licenses. server_device tells it that the service runs on the server's device, so every content key comes from a vault.

    Authentication runs on a background thread (asyncio.to_thread(authenticate, ...)). -The remote session starts in AUTHENTICATING. The client must poll the prompt -endpoint until it reaches authenticated (or failed).

    +The remote session starts in AUTHENTICATING. The client opens the remote session +channel and waits on it until the login state is authenticated (or failed). A client +without the channel polls the prompt endpoint for that state.

    Interactive auth: the InputBridge

    When the service calls its input function during authenticate() on the server thread, an InputBridge pauses that thread and exposes the prompt to the HTTP @@ -3015,16 +3043,16 @@ layer.

  • AuthStatus values: authenticating, pending_input, authenticated, failed.
  • InputBridge.request_input(prompt, timeout=600) blocks the sync auth thread on a threading.Event until submit_response() or cancel() fires.
  • -
  • A timeout raises TimeoutError and marks the remote session FAILED.
  • -
  • A prompt after authentication (in get_titles(), get_tracks() or licensing) raises - RuntimeError at once. The client polls for prompts only during authentication, so - no answer can come.
  • +
  • A timeout raises TimeoutError. The remote session becomes FAILED when that error ends the login.
  • +
  • A prompt after authentication (in get_titles(), get_tracks() or licensing) goes to + the client on an open remote session channel. Without the channel it raises RuntimeError + at once: the client polls for prompts only during authentication, so no answer can come.
  • A CDM call (InputBridge.request_cdm) works during and after authentication, because the - client also polls while its own requests run. A call that no client fetches within 10 seconds + channel carries it, or the client polls while its own requests run. A call that no client fetches within 10 seconds fails then, because no client is listening. A fetched call waits 60 seconds for its answer. The call leaves the auth status as it was. An answer never marks the remote session as client-authenticated.
  • AUTH_INPUT_TIMEOUT = 600.0 seconds. This is also the TTL granted to - AUTHENTICATING / PENDING_INPUT sessions in the store.
  • + AUTHENTICATING sessions in the store. A remote session that waits on a prompt stays AUTHENTICATING.

    GET /api/session/{id}/prompt returns one of:

    { "status": "authenticated" }
    @@ -3043,6 +3071,74 @@ straight away does not see it twice.

    403 FORBIDDEN. POST /api/session/{id}/prompt takes { "response": "..." } and returns { "status": "accepted" }. A post with no pending prompt is an INVALID_INPUT error.

    +

    The remote session channel

    +

    GET /api/session/{id}/channel is a WebSocket that carries only the messages the server +starts. Titles, tracks, licences, samples, the segment filter, bad content key reports, create +and delete stay HTTP requests. The route takes the same API key and ownership checks as the +other remote session routes.

    +

    Each message is a JSON text frame with a type field:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    typeFromFieldsMeaning
    promptserverid, promptThe service asks the user for input
    cdm_callserverid, callThe service calls the client's CDM; call has the shape of the prompt route's cdm_call
    logserverseq, level, message, tsOne record of the service log, with the same redaction as GET /logs
    statusserverstatus, errorThe login state, as the status of GET /prompt
    drainserverdrainingThe operator started or stopped a drain before a restart
    answerclientid, response, errorThe answer to the prompt or cdm_call with this id; error says why the client cannot answer a prompt, and the service stops at once
    +

    When the client connects, the server first sends the log records after since, the drain +state, the prompts and CDM calls that the client did not answer, and the login state. A log +record can arrive twice, after a reconnect; the client skips a seq it already has. Each +prompt and CDM call goes to one path only: the channel, or the prompt routes.

    +

    The server closes the channel with code 4404 when the remote session ends: a delete, an +expiry, an eviction, or a server shutdown. The client then stops reconnecting, and its next +request finds the remote session gone.

    +

    The client polls the prompt and logs routes, and sends keep-alive requests, in these cases:

    +
      +
    • A gateway in front of the server refuses the WebSocket upgrade, or the connection fails, when + the client creates the remote session. The client polls for the life of the process and logs + the refusal at debug level only.
    • +
    • The channel drops. The client polls until it reconnects, with a backoff from 1 to 30 seconds.
    • +

    SessionStore internals

    Source: unshackle/core/api/session_store.py. A singleton you get from get_session_store().

    @@ -3050,12 +3146,11 @@ returns { "status": "accepted" }. A post with no pending prompt is
  • Config-driven limits. serve.session_ttl (default 300) and serve.max_sessions (default 100) are read as properties, so config changes take effect without recreating the store.
  • -
  • create() evicts the least-recently-accessed remote session when at capacity, - then stores a new SessionEntry. It defaults auth_status to AUTHENTICATED, and - the create handler overrides it to AUTHENTICATING.
  • +
  • create() evicts the least-recently-accessed remote session of the API key that holds the most when at capacity, + then stores a complete SessionEntry from the fields of the create handler and publishes the create event.
  • get() refreshes last_accessed through touch(). It returns None (and deletes the entry) if an authenticated remote session has been idle longer than the - TTL. Remote sessions in AUTHENTICATING / PENDING_INPUT are exempt from TTL expiry.
  • + TTL. Remote sessions in AUTHENTICATING are exempt from TTL expiry.
  • cleanup_expired() runs every 60s: authenticated remote sessions expire at ttl, in-flight-auth remote sessions expire at AUTH_INPUT_TIMEOUT. The removal of a remote session cancels its InputBridge and deletes its cache directory (pruning @@ -3106,7 +3201,7 @@ returns { "status": "accepted" }. A post with no pending prompt is auth_status / auth_error -Current AuthStatus and last error message +Current AuthStatus, read from input_bridge (AUTHENTICATED without one), and last error message created_at / last_accessed @@ -3138,7 +3233,7 @@ suffix, which is the Cacher cache key the service reads it with:

    Both sides reject a cache key with a drive letter, a root, a .. segment, an empty segment, or more than 8 segments before it becomes a path, so a peer cannot write outside the cache directory. Both sides also refuse a cache map with more than -64 entries. The server skips a cache key it cannot write, such as one that names an +256 entries, and the server refuses the request body of a remote session creation or of a search when it is larger than 8 MiB. The client gives a warning when it has more than 256 cache files to send. The server skips a cache key it cannot write, such as one that names an existing file as a directory, and logs a warning.

    Remote session info response

    GET /api/session/{id}:

    @@ -3152,8 +3247,9 @@ existing file as a directory, and logs a warning.

    "title_count": 40 }
  • -

    draining is true after the operator starts a drain before a restart. The client reads -it before each title, so it knows of the restart before it occurs.

    +

    draining is true after the operator starts a drain before a restart. The client checks +it before each title, so it knows of the restart before it occurs. With an open remote session +channel the client reads the drain message and sends no request.

    expires_in is the configured TTL, not time remaining

    The expires_in value reports the store's configured session_ttl, not the @@ -3161,7 +3257,9 @@ seconds left before this specific remote session expires.

    Reference client: RemoteService

    Source: unshackle/core/remote_service.py. This is the canonical consumer of the -remote session API and a good template for any client.

    +remote session API and a good template for any client. The routes, the AuthStatus values, +the error codes and the request and reply shapes that it shares with the server are in +unshackle/core/remote_protocol.py.