Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions rust/contracts/application-boundary.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ meaning; `lithe_protocol::ErrorCode` documents when it is reported:
- `connection_closed`: the remote connection the request names closed or was lost
- `forward_target_not_confirmed`: a port forward reaches another machine through the remote host and the user has not confirmed it
- `forward_limit_reached`: the remote connection keeps the most port forwards it allows
- `forward_host_port_in_use`: another program on the remote host already uses the port a host or reverse forward asks for
- `forward_host_port_denied`: the remote host does not allow listening on the port a host or reverse forward asks for
- `forward_host_unsupported`: the remote server cannot listen on host ports for host or reverse forwards
- `reverse_forward_disabled`: the remote workspace settings do not allow reverse forwards
- `remote_server_invalid`: the server build `remote.connect` would send cannot
be read or is not the build its SHA-256 names
- `unknown`
Expand Down
46 changes: 41 additions & 5 deletions rust/contracts/remote-workspace-protocol-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ Other clients should do the same.
bytes of UTF-8 JSON. Frames larger than 16 MiB are rejected.
3. Closing the client-to-server stream is a disconnect.

The server never listens on a network port; all traffic, including tunnels,
uses this stream. Local port forwards are the client's: the reference client
All traffic, including tunnels, uses this stream. The server listens on a
network port only for a forwarding rule the user added (`listener.open`, see
"Listeners"); such a port belongs to the connection that opened it and
closes with it. Local port forwards are the client's: the reference client
listens on loopback addresses only, because a remote development server
usually has no authentication of its own, and bridges at most 64 connections
per forward, leaving further ones in the system's listen queue until one
Expand Down Expand Up @@ -154,6 +156,10 @@ subscribe before creating the resource.
| `tunnel.write` | `tunnelId`, `data` | `null`; `BUSY` when the peer stops reading |
| `tunnel.shutdownWrite` | `tunnelId` | `null`; queued data is sent, then the write side is shut down; replies keep arriving |
| `tunnel.close` | `tunnelId` | `null`; aborts both directions, queued data is discarded |
| `listener.open` | `listenerId`, `host` (an IP address), `port` (`0`: the host chooses), `relay?` (a `tunnel.open` target) | `port`; `ADDRESS_IN_USE` when another socket holds the port, `PERMISSION_DENIED` when the host refuses it, `BUSY` while the session has 64 listeners |
| `listener.adopt` | `listenerId`, `acceptId`, `tunnelId` | `null`; the waiting connection becomes that tunnel; `UNKNOWN_RESOURCE` when it no longer waits |
| `listener.close` | `listenerId` | `listenerId`, `port`, `connections`, `failedConnections`; closes the port and every connection it relays or still holds |
| `listener.list` | — | `listeners` by `listenerId`: `listenerId`, `port`, `connections`, `failedConnections` |
| `ports.listening` | — | `ports` sorted by `address` and `port`, one entry for each: `address`, `port`, and `pid` and `process` (the command name) when the server's user can inspect the owning process |

`fs.copy` refuses, with `INVALID_REQUEST`, to copy a file onto itself —
Expand Down Expand Up @@ -257,10 +263,40 @@ environment and caches it for the session. `process.spawn` with
| `walk.done` | `walkId`, `total`, `truncated`, `cancelled`, `skippedNonUtf8`, `unreadableDirectories` |
| `tunnel.data` | `tunnelId`, `data` |
| `tunnel.closed` | `tunnelId`, `error?` — the last event of the tunnel |
| `listener.accepted` | `listenerId`, `acceptId` — a reverse listener holds a connection for `listener.adopt` |

`watch.changed` batches are coalesced and sorted by path. With `overflow`
changes were lost and the client must rescan the watched tree.

## Listeners

The `listener` feature lets a client make the host listen on a TCP port for
a forwarding rule the user added:

- With `relay`, a **host rule**: the host connects every accepted connection
to that target itself and copies the bytes on the host, so nothing
crosses the stream. `host` is a loopback address, or `0.0.0.0` or `::`
when the user confirmed listening on every interface. At most 64
connections of one listener and 256 of the session are relayed at once;
further ones wait in the kernel's backlog, or are closed and counted as
failed when the session is at its limit.
- Without `relay`, a **reverse rule**: `host` must be a loopback address.
Every accepted connection waits, at most 16 at once per listener, for the
client, which receives `listener.accepted`, subscribes to a new tunnel and
calls `listener.adopt`; the connection then behaves as a tunnel the client
opened. A connection not adopted within 10 seconds is closed and counted
as failed.

Host names are refused, so the host never resolves what the client sends as
an address to bind. `failedConnections` counts connections that never
reached their target: a relay target that refused, a connection not adopted
in time, or one the host had no thread or tunnel left for. A listener
belongs to the connection that opened it, also in a persistent session, and
closes when that connection ends; clients do not reopen one after a
reconnect on their own, so a host port is open only while the user's rule
is. `listener.close` waits up to one second for the port to be released, so
a rule edited to the same port can bind it again at once.

## Event delivery and persistent sessions

`server.hello` returns `session` with `persistent`, `resumed`,
Expand Down Expand Up @@ -291,8 +327,8 @@ events after `afterSeq` were dropped; the client re-reads state and rescans
watches.

While no client is attached, a persistent session keeps processes, terminals
and watches running. Tunnels and walks belong to the connection that opened
them and end with it. After `server.shutdown` a persistent session is no
and watches running. Tunnels, walks and listeners belong to the connection
that opened them and end with it. After `server.shutdown` a persistent session is no
longer reachable, even while it is still terminating its processes; the next
connection with the same key starts a fresh session.

Expand All @@ -318,7 +354,7 @@ the field.
`NOT_FOUND`, `PERMISSION_DENIED`, `ALREADY_EXISTS`, `NOT_A_DIRECTORY`,
`IS_A_DIRECTORY`, `DIRECTORY_NOT_EMPTY`, `CONFLICT`, `TOO_LARGE`,
`UNKNOWN_RESOURCE`, `RESOURCE_ID_IN_USE`, `SPAWN_FAILED`, `CONNECT_FAILED`,
`BUSY`, `UNSUPPORTED`, `IO`. Clients additionally produce `TIMEOUT` and
`ADDRESS_IN_USE`, `BUSY`, `UNSUPPORTED`, `IO`. Clients additionally produce `TIMEOUT` and
`DISCONNECTED` locally. Messages are for people; OS error numbers stay in
`details`. `BUSY` always means a capacity limit that may clear: retrying
later is safe because the failed request changed nothing.
Expand Down
98 changes: 75 additions & 23 deletions rust/contracts/rust-core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,9 @@ or removing a command fails until the table changes in the same commit.
| `remote.workspaceTrust` | Read remote workspace trust decisions and one open workspace's trust |
| `remote.editWorkspaceTrust` | Record or remove one remote workspace trust decision |
| `remote.startForward` | Forward a local port to a port as the remote host sees it |
| `remote.stopForward` | Stop one port forward and close its local port |
| `remote.startHostForward` | Make the remote host listen on a port and connect it to another server |
| `remote.startReverseForward` | Make the remote host listen on a loopback port that reaches a port on this machine |
| `remote.stopForward` | Stop one port forward and close the port it listens on |
| `remote.forwards` | List the running port forwards of one connection |
| `remote.parseForwardAddress` | Read a port, `host:port` or URL typed into the ports page into a forward target |
| `remote.connections` | Read the SSH connections the user saved |
Expand All @@ -829,7 +831,7 @@ or removing a command fails until the table changes in the same commit.
| `remote.mottyHosts` | List the SSH hosts saved in Motty for an import |
| `remote.listWslDistributions` | List the WSL distributions installed on this Windows machine |
| `remote.preferences` | Read the remote workspace settings and what each automatic value resolves to |
| `remote.editPreferences` | Set the SSH program or the reconnect limit, or make one automatic again |
| `remote.editPreferences` | Set the SSH program, the reconnect limit or whether reverse forwards are allowed, or make one automatic again |
| `git.historyRewritePreview` | Review undo, message edit, squash, or drop with complete messages, eligibility, and an immutable checkout expectation |
| `git.remotes` | Return configured fetch remote names and URLs |
| `git.tags` | Return tag names, tag object IDs, target IDs, dates, and annotations |
Expand Down Expand Up @@ -5927,17 +5929,18 @@ the distributions each time it opens.

#### Remote workspace preferences

The settings page "Tools › Remote Workspace" edits two values that apply to
The settings page "Tools › Remote Workspace" edits three values that apply to
every connection. Native clients keep them in one machine-local document,
read it, and publish the `document` an edit returns atomically, as for the
saved connections; Core validates the document, applies every edit and
tells what each automatic value resolves to.

`remote.preferences` accepts `{ document: string|null, homeDirectory? }`
and returns `{ preferences, ssh, reconnect, storage, sshConfig? }`:
and returns `{ preferences, ssh, reconnect, reverseForwarding, storage,
sshConfig? }`:

- `preferences` is `{ sshProgram, reconnectLimitSeconds }`, the stored
values; `null` is automatic.
- `preferences` is `{ sshProgram, reconnectLimitSeconds,
allowReverseForwarding }`, the stored values; `null` is automatic.
- `ssh` is `{ source, path, status }`, the OpenSSH client every connection
starts. `source` is `setting` for a stored program and `automatic`
otherwise, which is the first executable `ssh` (`ssh.exe` on Windows) in an
Expand All @@ -5951,6 +5954,9 @@ and returns `{ preferences, ssh, reconnect, storage, sshConfig? }`:
double up to 30 seconds and add up to the limit; the last one is shortened
to fit. A limit of 0 never reconnects, so the list is empty. Clients wait
these pauses and decide nothing else about the schedule.
- `reverseForwarding` is `{ source, allowed }`: whether the ports page may
start reverse forwards (automatic: not allowed), because every user of the
host can reach the local port one opens.
- `storage` is `{ relativePath: "remote/preferences.json", maximumBytes:
16384 }`.
- `sshConfig` is `{ path, exists }` for `.ssh/config` below `homeDirectory`,
Expand All @@ -5963,10 +5969,12 @@ and returns `{ preferences, ssh, reconnect, storage, sshConfig? }`:
existing executable file, or makes the program automatic with `null`.
- `reconnectLimit: { seconds: number|null }` stores 0 to 3600 seconds, or
makes the limit automatic with `null`.
- `reverseForwarding: { allowed: boolean|null }` allows or refuses reverse
forwards, or makes the switch automatic with `null`.

The result is `{ changed, document: string|null }`; an unchanged edit
returns a `null` document and must not create a source. Neither or both
fields is `invalid_request` with `{ field: "operation", reason: "invalid"
returns a `null` document and must not create a source. No field or more
than one is `invalid_request` with `{ field: "operation", reason: "invalid"
}`; a refused value is `invalid_request` with `{ field, reason }`, where
`field` is `sshProgram` or `reconnectLimitSeconds` and `reason` is
`invalid` (empty, surrounding spaces or control characters), `tooLong`
Expand Down Expand Up @@ -6111,29 +6119,54 @@ deletion of inputs used by surviving processes.

#### Remote port forwards

The ports page forwards a local port to a TCP port as the remote host sees
it. It lists candidates with `remote.hostRequest` and `ports.listening`
The ports page runs three kinds of forwards over one connection:

- a **local** forward listens on this machine and reaches a TCP port as the
remote host sees it;
- a **host** forward makes the host listen on a port and connect every
accepted connection to another server itself, as `ssh -L` on a jump host
does; the bytes never cross the connection;
- a **reverse** forward makes the host listen on its loopback interface and
brings every accepted connection back to a port on this machine's loopback
interface, as `ssh -R 127.0.0.1:…` does.

The page lists candidates with `remote.hostRequest` and `ports.listening`
(`{ ports: [{ address, port, pid?, process? }] }`, one entry for each address
and port; `pid` and `process` only when the server's user can inspect the
owning process). Forwards belong to one connection:

| Command | Request | Result |
| --- | --- | --- |
| `remote.startForward` | `{ sessionId, targetHost, targetPort, localPort?, allowNonLoopbackTarget? }` | `{ forward, created }` |
| `remote.startHostForward` | `{ sessionId, targetHost, targetPort, listenPort?, allInterfaces?, allowNonLoopbackTarget? }` | `{ forward, created }` |
| `remote.startReverseForward` | `{ sessionId, localPort, listenPort?, preferences }` | `{ forward, created }` |
| `remote.stopForward` | `{ sessionId, forwardId }` | `{ stopped }` |
| `remote.forwards` | `{ sessionId }` | `{ forwards }`, by `id` |
| `remote.parseForwardAddress` | `{ text }` | `{ targetHost, targetPort, scheme?, path? }` |

A forward is `{ id, localHost, localPort, requestedLocalPort?,
localPortChanged, targetHost, targetPort, loopbackTarget, connections }`.
A forward is `{ id, kind, localHost?, localPort?, requestedLocalPort?,
localPortChanged, hostListenHost?, hostListenPort?, requestedHostPort?,
allInterfaces, targetHost?, targetPort?, loopbackTarget, connections,
failedConnections }`. `kind` is `local`, `host` or `reverse`; a field that
does not apply to the kind is absent:

| Kind | Listens on | Reaches |
| --- | --- | --- |
| `local` | `localHost:localPort` | `targetHost:targetPort` as the host sees it |
| `host` | `hostListenHost:hostListenPort` | `targetHost:targetPort` as the host resolves it |
| `reverse` | `hostListenHost:hostListenPort` (always `127.0.0.1`) | `localHost:localPort` (always `127.0.0.1`) on this machine |

`connections` counts accepted connections and `failedConnections` those
that could not reach their target (always 0 for a local forward).

- **Target.** `targetHost` is an IP address (IPv6 with or without brackets)
or a host name the remote host resolves, such as `localhost`. A wildcard
address (`0.0.0.0`, `::`), as `ports.listening` reports a program that
listens on every interface, becomes the host's loopback address of the
same family; `targetHost` in the result is the address sent to the host.
A malformed host or port `0` is `invalid_request` with `details.field`
(`targetHost` or `targetPort`).
(`targetHost`, `targetPort`, or `localPort` for a reverse forward's port
`0`).
- **Typed address.** `remote.parseForwardAddress` reads what the user typed
into the page, without a connection and without resolving anything: a
port alone (`3000`, on `localhost`), `host:port` with brackets around an
Expand All @@ -6153,23 +6186,42 @@ localPortChanged, targetHost, targetPort, loopbackTarget, connections }`.
`allowNonLoopbackTarget: true`, which the native client sets after the
user confirmed that target. Without it the request fails with
`forward_target_not_confirmed` and nothing starts. `loopbackTarget` tells
the two apart in the list.
the two apart in the list. The rule is the same for host forwards.
- **Local port.** The forward listens on `127.0.0.1`. `localPort` (absent or
`0` for a free one) is used when no local program accepts connections on
it, on `127.0.0.1` or `::1`; otherwise the forward listens on a free port,
`requestedLocalPort` keeps the asked one and `localPortChanged` is `true`.
A local port that cannot be opened at all is `unknown` with
`details.io`.
- **Once per target.** Starting a target that already has a forward returns
that forward with `created: false`, so a repeated click opens no second
local port. A connection keeps at most 64 forwards; one more is
`forward_limit_reached` with `details.maximum`.
- **Lifetime.** `remote.stopForward` closes the local port; connections it
already accepted run until either side closes them, and an unknown id
returns `stopped: false`. `remote.disconnect`, `remote.closeAll` and a
lost connection close every local port of the connection. Requests on a
- **Host port.** Host and reverse forwards listen on the host port
`listenPort` (absent or `0` for one the host chooses) and never move to
another one: a port another host program uses is
`forward_host_port_in_use`, and one the host refuses, such as a port below
1024 for an unprivileged user, is `forward_host_port_denied`. A host
forward listens on the host's loopback interface unless `allInterfaces` is
`true`; native clients ask the user every time and never remember it,
because every machine that reaches the host can then use the port. A
server that cannot listen is `forward_host_unsupported`.
- **Reverse forwards are off by default.** `remote.startReverseForward`
takes the current remote preferences `document` as `preferences` and is
`reverse_forward_disabled` unless they allow reverse forwards; native
clients also ask the user before each one starts. The local side only ever
connects to `127.0.0.1`, then `::1`, so a host user cannot reach the local
network through it.
- **Once per rule.** Starting a rule that already runs returns that forward
with `created: false`, so a repeated click opens no second port: a local
forward by target, a host forward by host port (or any port when none is
asked), interfaces and target, a reverse forward by host port and local
port. A connection keeps at most 64 forwards of all kinds together; one
more is `forward_limit_reached` with `details.maximum`.
- **Lifetime.** `remote.stopForward` closes the port the forward listens
on; connections it already accepted run until either side closes them,
except a host forward's, which the host closes with its port. An unknown
id returns `stopped: false`. `remote.disconnect`, `remote.closeAll` and a
lost connection close every port the connection opened on either side,
and a reconnect reopens none of them. Requests on a
connection whose session ended are `connection_closed` and forget it, as
for the other commands. `connections` counts accepted local connections.
for the other commands.

#### Remote workspace trust

Expand Down
8 changes: 8 additions & 0 deletions rust/crates/core/src/protocol/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,12 @@ pub enum RemoteCommand {
/// Forwards a local port to a port as the remote host sees it
/// (`remote.startForward`).
StartForward,
/// Makes the remote host listen on a port and connect it to another
/// server (`remote.startHostForward`).
StartHostForward,
/// Makes the remote host listen on a loopback port that reaches a port
/// on this machine (`remote.startReverseForward`).
StartReverseForward,
/// Stops one port forward and closes its local port (`remote.stopForward`).
StopForward,
/// Lists the running port forwards of one connection (`remote.forwards`).
Expand Down Expand Up @@ -1613,6 +1619,8 @@ impl CoreCommand {
"remote.workspaceTrust" => Some(Self::Remote(RemoteCommand::WorkspaceTrust)),
"remote.editWorkspaceTrust" => Some(Self::Remote(RemoteCommand::EditWorkspaceTrust)),
"remote.startForward" => Some(Self::Remote(RemoteCommand::StartForward)),
"remote.startHostForward" => Some(Self::Remote(RemoteCommand::StartHostForward)),
"remote.startReverseForward" => Some(Self::Remote(RemoteCommand::StartReverseForward)),
"remote.stopForward" => Some(Self::Remote(RemoteCommand::StopForward)),
"remote.forwards" => Some(Self::Remote(RemoteCommand::Forwards)),
"remote.parseForwardAddress" => Some(Self::Remote(RemoteCommand::ParseForwardAddress)),
Expand Down
Loading
Loading