feat(remote)!: make remote serve WebSocket-only and refuse older clients with CLIENT_OUTDATED

This commit is contained in:
imSp4rky
2026-12-05 09:47:22 -07:00
parent 641623590d
commit 00ad15527c
62 changed files with 657 additions and 2098 deletions
+26 -116
View File
@@ -24,9 +24,6 @@ in the wire form of `unshackle/core/wire.py`. To set up `dl --remote`, read
| `GET` | `/api/tunnel` | [The WebSocket of a `--local-proxy` tunnel](#get-apitunnel). |
| `GET` | `/api/tunnel/check` | [The exit IP address through a tunnel](#get-apitunnelcheck). |
| `GET` | `/api/session/{session_id}/channel` | [The remote session channel](#get-apisessionsession_idchannel): the messages the server starts, and the client's answers. |
| `GET` | `/api/session/{session_id}/prompt` | [The login state and a waiting prompt](#get-apisessionsession_idprompt), the polling fallback of the remote session channel. |
| `POST` | `/api/session/{session_id}/prompt` | [Answer a prompt](#post-apisessionsession_idprompt), the polling fallback. |
| `GET` | `/api/session/{session_id}/logs` | [Service log records](#get-apisessionsession_idlogs), the polling fallback. |
| `GET` | `/api/session/{session_id}/titles` | [The titles](#get-apisessionsession_idtitles). |
| `POST` | `/api/session/{session_id}/tracks` | [The tracks, chapters and manifests of a title](#post-apisessionsession_idtracks). |
| `POST` | `/api/session/{session_id}/segments` | [The URL, DRM, headers and cookies of the selected tracks](#post-apisessionsession_idsegments). |
@@ -63,9 +60,9 @@ DELETE /api/session/{id} → the end, with the changed cache files
`error_code` from `APIErrorCode`. The server never answers with a `5xx` status.
- **Body size.** The create request and `POST /api/search` take a body of up to 8 MiB, the
`samples` route up to 16 MiB, and every other route up to 1 MiB.
- **A route that the server lacks.** A client counts an optional call (the segment filter, a bad
content key report, the log poll) as empty only when the server answers `404` with no
`error_code`. Any other error stops the call.
- **Older clients.** The server accepts only the current client flow. A create without
`session_id`, and a service route of a remote session whose client never opened the remote session
channel, get `426 CLIENT_OUTDATED`.
### Slow responses
@@ -91,15 +88,14 @@ The login state (`AuthStatus`) tells where the login of a remote session stands:
| `authenticated` | The login succeeded. The service routes answer. |
| `failed` | The login failed. `error` gives the reason, and each service route answers `AUTH_FAILED`. |
`pending_input` is not a login state: the prompt route reports it while a prompt or a CDM call
waits for the client. The login state stays `authenticating` during that time. Each service route
A prompt or a CDM call that waits for the client does not change the login state: it stays
`authenticating` during that time. Each service route
(`titles`, `tracks`, `segments`, `segment_filter`, `samples`, `license`, `keys`, `keys/bad`) answers
`400 INVALID_INPUT` while the login state is `authenticating`, and `401 AUTH_FAILED` after a failed
login.
A prompt waits up to 600 seconds for its answer. When the time ends, the login fails. A prompt after
the login (in the titles, tracks or licence step) goes to the client on an open remote session channel.
Without the channel it fails at once, because the client polls for prompts only during the login.
the login (in the titles, tracks or licence step) goes to the client on the remote session channel.
A CDM call goes to the client in any login state. A call that no client takes within 10 seconds fails,
because no client listens. A call that a client took waits 60 seconds for its answer. An answer to a
@@ -115,7 +111,7 @@ create request, for the whole remote session:
| Server CDM | `server_cdm` | The server's device, live, up to the licence cap of the API key. The server vault answers first. |
| Client device | `client_device` | The client's own device, through the licence relay. The service sees a stand-in for that device. With `server_vault`, the server vault answers first. |
| Lent device | `lent_device` | Nobody licenses live: the service runs on the server's device, and the content keys come only from a vault. |
| No device | `no_device` | No server CDM, and no client device that the service can see: the create had no `cdm_type`, or no `cdm_relay: true`. A client with a CDM licenses through the licence relay. |
| No device | `no_device` | No server CDM, and no client device that the service can see: the create had no `cdm_type`. A client with a CDM licenses through the licence relay. |
The create response gives the mode in four fields, and the client reads them back:
@@ -126,15 +122,14 @@ The create response gives the mode in four fields, and the client reads them bac
| `server_vault` | `false` | the grant | the grant | `false` |
| `server_cdm_max_height` | the licence cap, or `null` | the licence cap, or `null` | the licence cap, or `null` | the licence cap, or `null` |
The client reads the no-device mode, and a create response without `server_cdm` and
`server_device`, as the client-device mode.
The client reads the no-device mode as the client-device mode.
The server picks the server CDM when the API key has the `server_cdm` grant for the service, unless
the client sends `server_cdm: false` with a `cdm_type`, or `quality` asks for more than the licence
cap with a `cdm_type`. Above the licence cap without a `cdm_type`, the server refuses the create
with `403 SERVER_CDM_CAPPED`. A request that sets one of the service's `SERVER_DEVICE_OPTIONS` keeps the
server CDM. Without the server CDM, a create with `cdm_type` and `cdm_relay: true` (or with a
licence cap) gets the client-device mode, and any other create gets the no-device mode. A service
server CDM. Without the server CDM, a create with `cdm_type` gets the client-device mode, and any other
create gets the no-device mode. A service
that calls `lend_server_device()` turns a client-device mode into the lent-device mode.
The user's view of each mode is in [Who licenses the tracks](../../guide/remote-sessions.md#who-licenses-the-tracks).
@@ -147,8 +142,7 @@ limit, the server ends the remote session.
| State | When | Limit |
|---|---|---|
| awaited | The client gave the remote session its ID in the create request, and has sent no request and opened no remote session channel since | 150s |
| polling | The client has had no remote session channel | The TTL rule only |
| awaited | The client has opened no remote session channel since the create request | 150s |
| present | The remote session channel is open | None |
| away | The remote session channel closed with any code except 1000, the connection broke, or the server closed the channel | 180s, or half of `session_ttl` when that is more |
| left | The client closed the remote session channel with code 1000 | 90s |
@@ -187,7 +181,7 @@ the cache directory of the remote session. A request that waits for the remote s
Before a restart the operator can start a drain. A create request then gets `409 SERVER_DRAINING`,
and the requests of open remote sessions continue. The server tells each client with the `drain`
message of the remote session channel and the `draining` field of the info route.
message of the remote session channel.
---
@@ -203,7 +197,7 @@ the background, and answers at once with the login state `authenticating`. The b
|---|---|
| `service` | Required. The service tag, in any case, or an alias. |
| `title_id` | Required. The title ID or URL, as for `dl`. |
| `session_id` | The ID that the client gives the remote session: the canonical text of a version 4 UUID. Without it, the server makes the ID. |
| `session_id` | Required. The ID that the client gives the remote session: the canonical text of a version 4 UUID. Without it, the create gets `426 CLIENT_OUTDATED`. |
| `credentials` | `{username, password, extra?}` for the profile. |
| `cookies` | The Netscape cookie file of the profile, zlib-compressed and base64-encoded. |
| `cache` | The cache files of the service: a map of cache key to the zlib-compressed, base64-encoded file. See [Cache files](#cache-files). |
@@ -214,7 +208,6 @@ the background, and answers at once with the login state `authenticating`. The b
| `proxy_region` | The country code that the client resolved `proxy` from. For a server-account service, the server matches it against its own accounts. |
| `server_cdm` | `false` when the client declines the server CDM. |
| `cdm_type` | The DRM system of the client's CDM (`widevine`, `playready`), or the id of a DRM plugin. |
| `cdm_relay` | `true` when the client answers CDM calls. A plugin `cdm_type` gets no relay. |
| `cdm_security_level`, `cdm_system_id`, `cdm_device_type` | The device facts of the client's CDM. `cdm_system_id` and `cdm_device_type` are for Widevine only. |
| `service_config` | The identity config keys that the service lists in `client_config`, and the client's `preferences` block. At most 16384 bytes of JSON. |
| `plugins` | The DRM plugins of the client: id to `{version, device?}`. `device` holds the plain values of the device attributes that the plugin names in `device_facts`. At most 16384 bytes of JSON. |
@@ -226,8 +219,8 @@ the background, and answers at once with the login state `authenticating`. The b
`session_id` lets the client delete the remote session before the create answers. A second create
with the same ID from the same API key and IP address ends the first one if the first one has not
answered, and replaces its remote session when the new one is stored. A second create that fails
leaves that remote session open. A remote session with an ID from the client ends when the client
sends no request for it and opens no remote session channel within 150 seconds of the create.
leaves that remote session open. A remote session ends when the client opens no remote session channel within 150 seconds of the
create.
`dl_params` holds `lang`, `v_lang`, `a_lang` and `s_lang` (arrays of language strings), `acodec` (an
array of audio codec names or `dl -a` aliases such as `ddp`), and `forced_subs` and `exact_lang`
@@ -293,6 +286,7 @@ keys from track data, and keeps the cache on the server.
| `400` | `INVALID_SERVICE` | An unknown service, or one that the API key may not use. |
| `403` | `FORBIDDEN` | Another API key or another IP address has the `session_id`. |
| `404` | `SESSION_NOT_FOUND` | A `DELETE` or a newer create with the same `session_id` ended this create. |
| `426` | `CLIENT_OUTDATED` | No `session_id`: the client is older than the server accepts. |
| `409` | `SERVER_DRAINING` | The server drains before a restart. |
### `GET /api/tunnel`
@@ -348,27 +342,26 @@ Each message is a JSON text frame with a `type` field (`ChannelMessage`):
|---|---|---|---|
| `prompt` | server | `id`, `prompt`, `qr` | The service asks the user for input. `qr`, when present, is the text of a QR code to draw with it. |
| `cdm_call` | server | `id`, `call` | The service calls the client's CDM. `call` has the shape of [`cdm_call`](#cdm-calls). |
| `log` | server | `seq`, `level`, `message`, `ts`, `qr` | One record of the service log, as the logs route gives it. `qr`, when present, is the text of a QR code to draw under the message. |
| `log` | server | `seq`, `level`, `message`, `ts`, `qr` | One record of the service log. `qr`, when present, is the text of a QR code to draw under the message. |
| `status` | server | `status`, `error` | The login state, on attach and at each change. |
| `drain` | server | `draining` | The operator started or stopped a drain before a restart. |
| `answer` | client | `id`, `response`, `error` | The answer to the `prompt` or `cdm_call` with this `id`. The answer to a CDM call is its JSON object as a string. `error` says why the client cannot answer a prompt, and the service stops at once. |
When the client attaches, 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 remote session channel, or the prompt route.
twice after a reconnect: the client skips a `seq` it already has. A prompt or CDM call waits on the
server while the remote session channel is down, and goes out when the client attaches again.
The server closes the remote session channel with code `4404` when the remote session ends. The client then
stops reconnecting, and its next request finds the remote session gone. The server sends a ping after
20 seconds without data.
The client uses the polling fallback (the prompt and logs routes, and a keep-alive request to the info
route at a third of `expires_in`) in these cases:
- A gateway in front of the server refuses the WebSocket upgrade, or the connection fails, at the
create. The client then polls for the life of the process.
- The remote session channel drops. The client polls until it reconnects, with a backoff from 1 to 30
seconds.
The client opens the remote session channel right after the create answers, and keeps it open for the
life of the remote session. When the channel drops, the client connects again with a backoff from 1 to
30 seconds. A refused upgrade at the start ends the run with an error that names the host and the
status. A service route (`titles`, `tracks`, `segments`, `segment_filter`, `samples`, `license`,
`keys` and `keys/bad`) of a remote session whose client never opened the channel gets
`426 CLIENT_OUTDATED`.
| Status | Error code | Meaning |
| --- | --- | --- |
@@ -376,82 +369,6 @@ route at a third of `expires_in`) in these cases:
| `403` | `FORBIDDEN` | The remote session belongs to another API key or IP address. |
| `404` | `SESSION_NOT_FOUND` | No such remote session. |
### `GET /api/session/{session_id}/prompt`
The polling fallback of the remote session channel: the login state, and the prompt or CDM call that waits
for the client (`PromptReply`). A client without the remote session channel polls it every 2 seconds during
the login, and every 0.5 seconds while one of its requests runs service code, so that it takes each
CDM call within the 10 seconds of the pickup.
```json
{ "status": "authenticating" }
{ "status": "pending_input", "prompt": "Enter code: " }
{ "status": "pending_input", "prompt": "Scan the code, then press Enter", "qr": "https://login.example/scan" }
{ "status": "pending_input", "prompt": "...", "cdm_call": { "op": "challenge", "drm": "widevine", "init_data": "...", "license_type": "OFFLINE", "privacy_mode": true, "service_certificate": "..." } }
{ "status": "authenticated" }
{ "status": "failed", "error": "Invalid credentials" }
```
#### CDM calls
A `cdm_call` comes first, in any login state. Its `op` is `challenge`, `keys` (parse a licence and return its keys) or `close`. The
client answers it with a JSON object in `response`: `{"session": "...", "challenge": "<base64>"}` for
`challenge`, `{"keys": [{"kid", "type", "key", "permissions"}]}` for `keys`, `{}` for `close`, or
`{"error": "..."}` when its CDM refuses. A `keys` answer holds only the Widevine `OPERATOR_SESSION` and
`SIGNING` keys, never a content key, and the client refuses a PlayReady `keys` call. An answered call
stops waiting at once, so a client that polls again at once does not get it two times.
| Status | Error code | Meaning |
| --- | --- | --- |
| `200` | - | The login state, and the waiting prompt or CDM call. |
| `403` | `FORBIDDEN` | The remote session belongs to another API key or IP address. |
| `404` | `SESSION_NOT_FOUND` | No such remote session. |
### `POST /api/session/{session_id}/prompt`
Answer the waiting prompt or CDM call. Body: `{ "response": "..." }` (`PromptAnswer`).
=== "Response `200`"
```json
{ "status": "accepted" }
```
| Status | Error code | Meaning |
| --- | --- | --- |
| `200` | - | Answer accepted. |
| `400` | `INVALID_INPUT` | No `response`, or no prompt waits. |
| `404` | `SESSION_NOT_FOUND` | No such remote session. |
### `GET /api/session/{session_id}/logs`
The polling fallback of the remote session channel: the records of the service's own log (`LogsReply`).
Pass `since=<seq>` to get only the records after the last sequence number. The route answers in each
login state, `failed` too.
=== "Response `200`"
```json
{
"session_id": "d4e5f6a7-...",
"logs": [
{ "seq": 1, "level": "INFO", "message": "Signed in as profile 'Main'", "ts": 1724990000.1 },
{ "seq": 2, "level": "WARNING", "message": " - The service returned no streams", "ts": 1724990002.7 }
],
"last_seq": 2
}
```
The buffer keeps the newest 500 records. Only the logger of the service is copied: the core loggers
stay on the server, and the HTTP requests that the server makes for the service never reach the
client. A server-account remote session sends no log records, because the output of its login can
carry the identity of the operator's account.
| Status | Error code | Meaning |
| --- | --- | --- |
| `200` | - | Records, oldest first (an empty list when nothing is new). |
| `404` | `SESSION_NOT_FOUND` | No such remote session. |
### `GET /api/session/{session_id}/titles`
The titles of an authenticated remote session (`TitlesReply`), each in the wire form. A slow answer
@@ -741,8 +658,7 @@ client never learns the server vault names.
### `GET /api/session/{session_id}`
Remote session info (`SessionInfo`). The client also uses it as the keep-alive of the polling
fallback.
Remote session info (`SessionInfo`).
=== "Response `200`"
@@ -751,17 +667,11 @@ fallback.
"session_id": "d4e5f6a7-...",
"service": "EXAMPLE",
"valid": true,
"expires_in": 300,
"draining": false,
"track_count": 4,
"title_count": 10
}
```
`expires_in` is the configured `session_ttl`, not the seconds that remain for this remote session.
`draining` is `true` after the operator starts a drain before a restart. The client checks it before
each title.
| Status | Error code | Meaning |
| --- | --- | --- |
| `200` | - | Info returned. |