# Remote sessions A remote session runs a service on a remote unshackle server and the download on your machine. The server logs in to the service, lists the titles, gets the tracks and their manifests, and can license the tracks. Your machine selects the tracks, downloads and decrypts them, and muxes the result. !!! note "When to use a remote session" - The server has service accounts, cookies or a CDM that you do not want to copy to each machine. - You want to download on a fast machine and keep the logins and the licensing in one place. - A service is available only in the region of the server. The server is an `unshackle serve` instance. The routes, fields and message shapes between your machine and the server are on the [remote session protocol](../dev/rest-api/remote-sessions.md) page. The server settings are under [`serve`](../reference/configuration/services.md#serve). ## Configure the client On your machine (the client), add a `remote_services` block to `unshackle.yaml`. Each entry names a server and gives its URL and API key: ```yaml title="unshackle.yaml (client)" remote_services: my_server: url: "https://my-box.example:8786" api_key: "your-api-key" server_cdm: true # optional: the server CDM licenses the tracks services: EXAMPLE: server_cdm: false # optional: your own CDM licenses EXAMPLE title_map: "0ABCDEF": "The Show (Renamed Locally)" ``` [`remote_services`](../reference/configuration/services.md#remote_services) in the configuration reference gives each setting of a server entry. ### Select a server When you configure one server, unshackle uses it: ```bash unshackle dl --remote EXAMPLE 0ABCDEF ``` When you configure more than one server, select one with `--server`: ```bash unshackle dl --remote --server my_server EXAMPLE 0ABCDEF ``` With no `remote_services` block, unshackle stops with an error that tells you to add one. ### Make a service remote by default { #making-a-service-remote-by-default } To run a service on the server without `--remote`, set `remote` in the [`services`](../reference/configuration/services.md#services) block, not in the `services` sub-dict of `remote_services`. Add `server` to select one of several servers: ```yaml title="unshackle.yaml (client)" services: EXAMPLE: dl: remote: true server: my_server ``` `unshackle dl EXAMPLE 0ABCDEF` and `unshackle search EXAMPLE "query"` then run on `my_server`. `--local` runs the local service code for one run. The [`services`](../reference/configuration/services.md#services) reference gives the other rules. --- ## What happens during a download The command looks like an ordinary download. The client takes the remote session through these steps. === "1. Log in" The client creates the remote session and sends what it can, so that the server does not have to prompt: - The local **credentials** and **cookies** of the service and profile. - The local **cache files** of the service (for example refreshed tokens), including the files in subdirectories of the service cache directory, but only the files of the active profile. The client withholds a file whose path, in any directory or file name, embeds a hash of another credential or the name of another profile. Thus the tokens of one profile never reach the server while you use a different profile. At worst, a withheld file makes the server log in again. - A resolved **proxy**, or, if you set none, your detected **region**. Without the [`server_proxy`](../reference/configuration/services.md#serve) grant on your API key, the proxy must be a full proxy URI on a public host, and the server rejects the remote session when it is in a different region from yours: pass `--proxy` with your own proxy, or `--no-proxy` to accept the server's own connection. For Control D, see [Control D](proxies-and-vpn.md#control-d). The client also downloads playlists and segments through the same proxy, as a local run does. `--no-proxy-download` and `--proxy-download` change that for downloads only. - With `--local-proxy`, the client opens a tunnel first, and the server sends the service requests through your machine's own connection. See [Send the server's requests through your machine](proxies-and-vpn.md#send-the-servers-requests-through-your-machine). - Your track selection (`-r`, `-v`, `-q`, `--best-available`, `--generate-dv`) and your language and audio codec selection (`-l`, `-vl`, `-al`, `-a`, `-fs`), so that the server fetches the right manifests. Some services fetch one manifest for each audio language. The server uses its own accounts only when the operator lists the service in `serve.server_accounts` and gives your API key `server_accounts`. For any other service, a client that sends no credentials or cookies logs in with nothing. For a listed service the client sends no credentials, cookies or cache: the server picks one of its own accounts that works in your region, and rotates through them. `--proxy ca` picks a `ca` account. The service listing of the server gives the regions that those accounts cover. When your region is not one of them and you set no `--proxy`, the client resolves a proxy for the first of those regions itself, so both sides are in a region that the account works in. This is a bare region query, so it skips Control D. A server-account service does not accept a Control D resolver from the client. === "2. Prompts" Some services need a one-time code, a PIN or a device confirmation. When the login on the server asks for input, the server sends the prompt to the client. The client shows it to you, and sends your answer back. ```text Enter the 6-digit code sent to your email: 483920 ``` The server also sends each record of the service's own log to the client, which prints it, so you see the reason when a step fails on the server. === "3. Titles and tracks" The client gets the title list, then the tracks of the title that you select. The server sends the manifests, and the client parses them again to download the segments itself. The client uses the HTTP headers and cookies that the server used. Each cookie keeps its domain, path and `secure` flag, so it goes only to its own hosts. === "4. Licensing" For a DRM-protected title, the [licensing mode](#who-licenses-the-tracks) of the remote session decides who licenses each track. === "5. Download and end" The client downloads, decrypts and muxes locally. While it works, the remote session stays open, so a long download does not let it expire. When the run ends, also early (`--list`, Ctrl+C, an error), the client ends the remote session (see [When the client ends a remote session](#when-the-client-ends-a-remote-session)). When the login belongs to the client, the server returns the changed cache files (for example a refreshed token), and the client saves them, so the **next** remote session can skip the interactive login. The login belongs to the client when it succeeds and the remote session uses no server account. This includes a pair code that you approve on another device. A server-account login never belongs to the client, and neither does a remote session on the server's device (see below). The client saves only the returned files that pass the same profile check as before it sent them, so a file for another profile cannot overwrite that profile's local file. !!! tip "Rename remote titles locally" To rename titles of a remote service that you do not have installed locally, add a `title_map` under that service in `remote_services..services`. The server sends the titles exactly as the service returns them, and applies no `title_map` of its own. The client applies your map, so the output name stays under your control. --- ## Who licenses the tracks When the server creates the remote session, it fixes the licensing mode: who licenses the tracks for the whole remote session. There are four licensing modes: | Licensing mode | Who licenses | When | |---|---|---| | Server CDM | The server's device, up to the licence cap of your API key. The client needs no CDM. | Your API key has the `server_cdm` grant for the service, and your client did not decline it. | | Client device | Your local CDM, through a licence relay on the server. With the [`server_vault`](../reference/configuration/services.md#serve) grant (or `server_cdm`), the server vault answers first, and a track that no server vault holds goes back to your device. | Your client declined the server CDM, the API key has no server CDM for the service, or `-q` asks for more than the licence cap. | | Lent device | Nobody licenses live. The content keys come from the server vault (with the grant) or from your own vaults. | A service option runs the remote session on the server's device. | | No device | Nobody licenses: only your own key vaults can give the content keys. The service on the server sees no device. | No server CDM, and your client has no local CDM. | ### The server CDM The server checks its key vaults, loads the device for your API key, licenses, and returns the content keys. When the server's `cdm` entry for the service has a `widevine` and a `playready` device, the server loads the device of your local CDM's DRM system. See [`cdm`](../reference/configuration/drm.md). - Set `server_cdm: true` in the server entry to ask for it. - A content key from a server vault is unproven. The client decrypts with it and checks that the output decodes, and reports a wrong pair to the server. The server flags the pair, and the next licence for that KID reaches the CDM. The client never learns the server vault names. - A content key in your own vaults goes first, also when the server already answered. The client proves it, and a server key waits as the next candidate. - When the server licence fails, or the server gives a track no content key, your own device licenses the track only when the `cdm` entry for the service gives the loaded device (see [`remote_services`](../reference/configuration/services.md#remote_services)). Otherwise, or when that device also fails, the run stops at the licence step of that track with one `DRM_ERROR` line. !!! note "The licence cap" An API key with `server_cdm_max_height` gets the server CDM only up to that height. Above it, the client reads its own key vaults first, then licenses the track with its own local CDM. When the track has no DRM of that CDM's system, the client uses the device that its own `cdm` entry names for the DRM system of the track. With no such device, the download of that track stops. See [`server_cdm_max_height`](../reference/configuration/services.md#serve). ### Your own device Your local CDM makes the licence challenge. The server sends it to the service, and returns the licence for your CDM to parse. - Your client declines the server CDM in the cases that [`remote_services`](../reference/configuration/services.md#remote_services) gives (`server_cdm: false`, `dl --cdm`, a `cdm` entry for the service). The server then builds the service on your device, also when the API key has the server CDM. - A service option that the service lists in `SERVER_DEVICE_OPTIONS` keeps the server CDM, and the service gets the server's device whatever device you have. The client shows a warning when the server did not apply its choice. - The service on the server sees your device, not the server's. See [Your own device identity](#your-own-device-identity). ### The lent device A service can offer an option that builds the remote session on the server's own device and identity, for the quality that device reaches. The remote session then makes no live licence: each content key comes from the server vault or your own vaults, and a content key that no vault holds stops the download with an error that names its KID, when the client knows it. The service uses the server's own config: your identity values do not apply, but your preferences do (see [Your preferences](#your-preferences)). The client reads the server vault only when the API key has the `server_vault` grant (or `server_cdm`). The login tokens of such a remote session hold the server's device identity, so the server keeps them and returns no cache files. !!! note "A DRM type that the client does not know" The client rebuilds the DRM of each track from the track list of the server. When the server sends a DRM type that the client does not know, the client stops the title with one `DRM_ERROR` line that names the type. Update the client to the version of the server. ### Your own device identity A service can need more than a licence challenge from your device: an ESN, model keys, or a session-key exchange that runs on the CDM. The device never leaves your machine. Only these cross to the server: - **Device facts.** The client sends its DRM system, security level, system ID and device type. The service sees a stand-in with these facts. - **CDM calls.** When the service calls the CDM, the server sends the 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 tells the client, because a CDM holds only a few sessions. - **Session keys only.** The client returns only the Widevine `OPERATOR_SESSION` and `SIGNING` keys. It never returns a content key, and it refuses a PlayReady keys call. Thus a server cannot use your device to license titles for itself. - **Identity config.** A service lists the config keys that a client may send (for example a device serial, device keys, or a map of serials). The client sends only those keys, from its own `services: :` block in `unshackle.yaml`. When the client sends an identity, the server drops its own value for each of those keys, so the two identities never mix. A mapping merges the client's entries over the server's. A client entry replaces a server entry with the same key, also when the server's key is a number. The server keeps these values in the memory of the remote session only. ### Your preferences The `preferences` block of a service is the place for user choices, such as a metadata language or a subtitle format. It is not an identity, so it crosses `--remote` in every licensing mode, whatever CDM the client has, and no service declares anything for it: - The client sends each entry of its own `services: : preferences:` block in `unshackle.yaml` that is a string, a number or a boolean. It leaves out other entries and logs one debug line for each. - The server merges the entries key by key over its own `preferences` block for that service. An entry that the client sends replaces the server's value, and a config key that the client does not send keeps the server's value. The entries never count as an identity, and they stay when the service builds the remote session on the server's device. - The server refuses a `preferences` value that is not an object of strings, numbers and booleans with `INVALID_INPUT`. A service reads its preferences from `self.config["preferences"]`, locally and over `--remote` alike. Keep a value that changes the server's account (a profile, an account type) out of `preferences` unless every client of the server may choose it: a server account is shared, and the service can store the choice in its cache. --- ## When the server restarts The server keeps remote sessions in memory only, so a restart loses them all. The client recovers in two ways: - **A drain.** The operator can announce a restart. The client then finishes the current title, ends its remote session so that the refreshed cache comes back, waits until the server is back, opens a new remote session and continues with the next title. - **A lost remote session.** After a connection error, an answer cut off after its headers, a timeout, a closed tunnel or a gateway error page, the client waits for the server and checks the old remote session. A live remote session takes the call again only after a connection error, because a call that timed out can already have run. A cut-off answer counts as a connection error, so the server can run that call twice. The client replaces a lost remote session: it opens a new one, gets the titles and the tracks of the current title again, and sends the failed call again. It matches a title by season and episode number, and a track by its attributes, so a new ID on the server changes nothing for the run. The client waits up to 30 minutes for the server, which covers the drain of a deploy. A Ctrl+C ends this wait. The client stops with an error when the new remote session licenses differently from the old one, for example after a change to the `server_cdm` grant of the API key. An interactive login asks for its input again. ## When the client ends a remote session The client ends the remote session, closes its tunnel, and saves the returned cache: - 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 the server answers the create request - when a signal or a closed console stops the process, or the cancel of a download job stops its worker - when the login or any other step of the opening fails, or the create request times out or its answer is cut off - when a server that relays for a REST client closes its pooled remote session, or stops The end request 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 Ctrl+C during the downloads cancels the run. The client waits for the download threads and for the content keys that still go to the vaults, and then ends the remote session. The cancel ends each wait of a download thread within 0.5s: the wait for a server that restarts, for a rate limit and for a login. A cancelled thread opens no new remote session. The cancel does not stop a request that the server already has: the client waits for its answer. A failed track is not a cancel, so the next title can open a new 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. On Windows, a Ctrl+C counts when the network read that blocks returns, so the 15s timeout limits the wait. `SIGHUP` (a closed terminal) and `SIGTERM` stop `dl`, `search` and `tunnel-test` as Ctrl+C does, with an exit code other than 0. Three things are different: - The end request goes first: before the wait for the download threads, for a CDM call and for the remote session channel. - The process ends 8s after the first signal at the latest, also when the server does not answer. - A later signal changes nothing, and Ctrl+C keeps its own count. After `SIGHUP` the process writes nothing more to a terminal. A redirected output gets each line. `nohup` keeps its effect. On Windows, a closed console window and Ctrl+Break send the end request and then end the process, without the rest of the Ctrl+C steps. The system gives a closed window about 5s. A kill (`SIGKILL`, a Windows task end, the cancel of a download job on Windows) sends no end request, so only the server ends the remote session, at its own limits (see [Remote session lifetime](../dev/rest-api/remote-sessions.md#remote-session-lifetime)). A cancelled job that uses a remote server sends the end request for at most 4s, then its worker ends. --- ## Troubleshooting !!! example "\"Could not connect to remote server at ...\"" The client could not reach the URL. Make sure that `unshackle serve` runs on the server, that the `url` and port in `remote_services` are correct (default port `8786`), and that each firewall or reverse proxy allows the connection. !!! example "`Remote server rejected the API key (HTTP 401)`" The server does not know the `api_key` of the `remote_services` entry. Ask the operator for a valid API key, or set `auth_headers` when a gateway in front of the server reads another header. !!! example "`403 FORBIDDEN` during a download" The request came from a different IP address than the one that created the remote session. Remote sessions are bound to that address. Do not move between networks (or NAT egress addresses) during a remote download. !!! example "The login times out or the prompt never resolves" An interactive login has a time limit (see [Login states](../dev/rest-api/remote-sessions.md#login-states)). After that time the login fails and the server ends the remote session. Start the download again. If the server never prompts you, make sure that the credentials and cookies that the client sent are valid for the service. !!! example "\"Multiple remote services configured, pick one with --server: ...\"" You have more than one entry under `remote_services`. Pass `--server ` to select one.