mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-01 21:45:10 +00:00
fix(remote): end a remote session from before its create to after its DELETE
This commit is contained in:
@@ -987,7 +987,9 @@ the newlines must go out at once.
|
||||
|
||||
### `POST /api/session/create`
|
||||
|
||||
Make a remote session for a service and title. Authentication runs asynchronously in the background. This call returns immediately with `status: "authenticating"`, and you then poll the prompt endpoint. The body accepts `service` and `title_id` (both required). It also accepts a broad set of optional keys, because the body allows `additionalProperties`. These are `credentials` (`{username, password, extra?}`), `cookies` (base64 of zlib-compressed Netscape cookie file), `proxy`, `no_proxy`, `profile`, `cache` (map of forwarded cache files, keyed by the file path relative to the service cache directory with `/` separators and no `.json` suffix, so a nested key such as `session_web/<sha1>` lands in a subdirectory), `client_region`, `proxy_region`, `cdm_type`, `range_`, `vcodec`, `quality`, `best_available`, `dl_params`, `client`, and any service CLI options. `proxy_region` is the country code the client resolved `proxy` from; the server matches it against its own accounts. `client` is a freeform object the dashboard shows as sent (the CLI puts `version`, `code_hash`, `platform` and a redacted `argv` in it); the server ignores it above 4096 bytes of JSON.
|
||||
Make a remote session for a service and title. Authentication runs asynchronously in the background. This call returns immediately with `status: "authenticating"`, and you then poll the prompt endpoint. The body accepts `service` and `title_id` (both required). It also accepts a broad set of optional keys, because the body allows `additionalProperties`. These are `session_id`, `credentials` (`{username, password, extra?}`), `cookies` (base64 of zlib-compressed Netscape cookie file), `proxy`, `no_proxy`, `profile`, `cache` (map of forwarded cache files, keyed by the file path relative to the service cache directory with `/` separators and no `.json` suffix, so a nested key such as `session_web/<sha1>` lands in a subdirectory), `client_region`, `proxy_region`, `cdm_type`, `range_`, `vcodec`, `quality`, `best_available`, `dl_params`, `client`, and any service CLI options. `proxy_region` is the country code the client resolved `proxy` from; the server matches it against its own accounts. `client` is a freeform object the dashboard shows as sent (the CLI puts `version`, `code_hash`, `platform` and a redacted `argv` in it); the server ignores it above 4096 bytes of JSON.
|
||||
|
||||
`session_id` is the ID that you give the remote session, so that you can delete it before this call answers. It must be the canonical text of a version 4 UUID. Without it, the server makes the ID. A second create with the same ID from the same API key and IP 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.
|
||||
|
||||
`dl_params` is an object with the `dl` track selection: `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` (booleans). The service reads these values from `ctx.parent.params`. The server uses the `dl` default for each absent value. It refuses a malformed value, an unknown codec, range or vcodec name, and a height of 0 or less with `INVALID_PARAMETERS`. The flat `range_`, `vcodec`, `quality`, `best_available` and `generate_dv` keys are `dl` values on this route. On the list and search routes they are `dl` values only when the body has `service_params`.
|
||||
|
||||
@@ -1011,8 +1013,10 @@ The `proxy` value must be a full proxy URI, unless the operator gives your API k
|
||||
| Status | Error code | Meaning |
|
||||
| --- | --- | --- |
|
||||
| `200` | - | Session created; authentication in progress. |
|
||||
| `400` | `INVALID_INPUT` | Missing `service` or `title_id`. |
|
||||
| `400` | `INVALID_INPUT` | Missing `service` or `title_id`, or a `session_id` that is not the canonical text of a version 4 UUID. |
|
||||
| `400` | `INVALID_PARAMETERS` | Invalid `range_`, `vcodec`, `quality` or `dl_params` value. |
|
||||
| `403` | `FORBIDDEN` | Another API key or another IP has the `session_id`. |
|
||||
| `404` | `SESSION_NOT_FOUND` | A `DELETE` or a newer create with the same `session_id` ended this create. |
|
||||
| `400` | `INVALID_SERVICE` | Unknown or disallowed service. |
|
||||
|
||||
### `GET /api/session/{session_id}/channel`
|
||||
@@ -1307,7 +1311,7 @@ Return remote session metadata and validity.
|
||||
|
||||
### `DELETE /api/session/{session_id}`
|
||||
|
||||
Tear down a remote session. The server cancels its input bridge, harvests any updated cache files, and deletes the remote session and its cache directory.
|
||||
Tear down a remote session. The server cancels its input bridge, harvests any updated cache files, and deletes the remote session and its cache directory. A create of the same API key and IP that still runs under this ID ends too: it makes no remote session and answers `404 SESSION_NOT_FOUND`. If no remote session exists yet, the `DELETE` answers `200` with no `cache`.
|
||||
|
||||
=== "Response `200`"
|
||||
|
||||
|
||||
@@ -246,7 +246,8 @@ hood the client walks a remote session through its lifecycle.
|
||||
[remote session channel](#the-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`
|
||||
deletes the remote session (see [When the client ends a remote session](#when-the-client-ends-a-remote-session)).
|
||||
If the login belongs to the client, the `DELETE`
|
||||
request returns the updated cache files (for example a refreshed token) and the
|
||||
client saves them locally, so the **next** remote session can skip interactive
|
||||
authentication. The login belongs to the client when it succeeds and the
|
||||
@@ -480,7 +481,9 @@ The server mounts all these routes, even in `--remote-only` mode. Paths use the
|
||||
|
||||
### Create: request and response
|
||||
|
||||
`POST /api/session/create` requires `service` and `title_id`. It also accepts
|
||||
`POST /api/session/create` requires `service` and `title_id`. It also accepts `session_id` (the ID of the remote session,
|
||||
as the canonical text of a version 4 UUID; a create of the same API key and IP with the same ID replaces that remote
|
||||
session when the new one is stored, and the server answers the ID of another API key or IP with `403 FORBIDDEN`),
|
||||
`credentials`, `cookies` (base64 of zlib-compressed Netscape cookie file), `proxy`,
|
||||
`no_proxy`, `profile`, `cache` (a map of `filename → base64(zlib(bytes))`),
|
||||
`client_region`, `server_cdm` (`false` when the user picked the local CDM), `cdm_type`, `cdm_security_level`, `cdm_relay` (`true` when the client
|
||||
@@ -614,6 +617,8 @@ Source: `unshackle/core/api/session_store.py`. A singleton you get from
|
||||
- **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.
|
||||
- **`creating()`** holds the ID of a remote session while its `create` handler works, and refuses an ID that another API
|
||||
key or IP has. **`end_pending()`** ends such a create for a `DELETE`, and then `create()` stores nothing.
|
||||
- **`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.
|
||||
- **Expiry.** `get()` and `cleanup_expired()` use one rule. A remote session that no request holds
|
||||
@@ -635,7 +640,7 @@ Source: `unshackle/core/api/session_store.py`. A singleton you get from
|
||||
|
||||
| Field | Description |
|
||||
|---|---|
|
||||
| `session_id` | UUID4 string |
|
||||
| `session_id` | UUID4 string: the one of the create request, else one that the server makes |
|
||||
| `service_tag` | Normalized service tag |
|
||||
| `service_instance` | The authenticated service object kept alive between calls |
|
||||
| `titles` / `title_map` | Result of `get_titles()` and a `title_id → Title` map |
|
||||
@@ -651,9 +656,12 @@ Source: `unshackle/core/api/session_store.py`. A singleton you get from
|
||||
The `create` handler builds a `Cacher` namespaced as:
|
||||
|
||||
```text
|
||||
_sessions/<pbkdf2_hmac(sha256, X-Secret-Key, "unshackle-session-ns", 100000)[:12]>/<session_id>/<service>
|
||||
_sessions/<pbkdf2_hmac(sha256, X-Secret-Key, "unshackle-session-ns", 100000)[:12]>/<uuid4>/<service>
|
||||
```
|
||||
|
||||
The server makes a new UUID for each `create`, so a `create` that takes over a session ID never shares a directory
|
||||
with the one before it.
|
||||
|
||||
The handler writes forwarded `cache` files into that directory before authentication.
|
||||
On `DELETE`, when the login belongs to the client (`client_auth`) and the remote
|
||||
session does not run on the server's device, the handler harvests updated cache files from the whole directory tree (compressing each
|
||||
@@ -732,10 +740,12 @@ the error codes and the request and reply shapes that it shares with the server
|
||||
(merging returned `session_headers`/`session_cookie_jar`, re-parsing `manifests`) →
|
||||
get the licence in proxy mode or `server_cdm` mode → `close()` (`DELETE`, saving any returned
|
||||
`cache`).
|
||||
- **ID.** The client gives the remote session its ID: a version 4 UUID in the `session_id` of `create`. It uses
|
||||
the ID of the reply, so a server that ignores the field also works.
|
||||
- **Server restarts.** The server keeps remote sessions in memory only, so a restart
|
||||
loses them all. The client recovers in two ways:
|
||||
- A drain announced through `draining`: the client finishes the current title, then
|
||||
closes its remote session, so the drain can end and the refreshed cache comes back.
|
||||
deletes its remote session, so the drain can end and the refreshed cache comes back.
|
||||
It waits until `/api/health` reports the restarted server without `draining`,
|
||||
opens a new remote session, and continues with the next title.
|
||||
- A lost remote session (`SESSION_NOT_FOUND`, a connection error, an answer cut off
|
||||
@@ -743,7 +753,8 @@ the error codes and the request and reply shapes that it shares with the server
|
||||
for `/api/health`, then checks the old remote session. A live remote session takes the
|
||||
call again only after a connection error, because a timed-out call can already have run.
|
||||
A cut-off answer counts as a connection error, so the server can run that call twice. A lost one is replaced: the
|
||||
client opens a new remote session, sends `titles` and the current title's `tracks`
|
||||
client deletes the old one when the server did not answer `SESSION_NOT_FOUND` for the call, and
|
||||
saves the cache that comes back. It then opens a new remote session, sends `titles` and the current title's `tracks`
|
||||
again, and then sends the failed call again. A title or track that the new remote session
|
||||
gives another ID keeps its old ID on the client, and the client sends the new ID. The client matches a title by season and episode number, and a track by its
|
||||
attributes.
|
||||
@@ -760,6 +771,31 @@ the error codes and the request and reply shapes that it shares with the server
|
||||
with no `error_code`. Any other error stops the call: a segment filter that fails on the
|
||||
server stops the track download, and the client never keeps every segment in its place.
|
||||
|
||||
### When the client ends a remote session
|
||||
|
||||
Source: `unshackle/core/remote_lifetime.py`. One owner holds the remote session, its tunnel and its channel
|
||||
from before the first request until the server answers the `DELETE`. It closes the channel, sends the `DELETE`,
|
||||
saves the returned cache and then stops the tunnel. It does so:
|
||||
|
||||
- when `dl` has finished every title, before the run post-scripts
|
||||
- when the process exits: `--list`, an error, or Ctrl+C in any step, also before `create` answers
|
||||
- when the login or any other step of the opening fails, or `create` times out or its answer is cut off
|
||||
- when a serve that relays for a REST client closes its pooled remote session
|
||||
|
||||
The `DELETE` has a connect and read timeout of 15s each. The client sends it again only once, when a gateway answers with
|
||||
an error page or asks for the API key in the other header. It does not wait for a rate limit. A process that ends without its exit hooks (`SIGHUP`,
|
||||
`SIGTERM`, `SIGKILL`, a closed console window) sends no `DELETE`, so the remote session stays until its TTL. Ctrl+C
|
||||
waits for the download threads before it ends the remote session.
|
||||
|
||||
While the client ends a remote session, it ignores the first Ctrl+C, so it does not lose the returned cache. The next
|
||||
Ctrl+C stops the wait at once, and the process then waits for no other end. The count of Ctrl+C is one for the process.
|
||||
When the run itself called the end, after the last title, that Ctrl+C also stops the run. On Windows, a Ctrl+C
|
||||
counts when the network read that blocks returns, so the 15s timeout limits the wait. After the end, no thread of the
|
||||
client opens a remote session again.
|
||||
|
||||
The `DELETE` can arrive while the server still works on `create`. The server then answers `200` with no cache, and
|
||||
the `create` makes no remote session, starts no login and answers `404 SESSION_NOT_FOUND`.
|
||||
|
||||
---
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
Reference in New Issue
Block a user