Skip to content

Commit 867c368

Browse files
authored
feat(remote): 保存 SSH 连接,并把连接规则集中到 remote 包(#2231) (#145)
- 新增 lithe_remote::client::connections:SshConnection 的字段、规范化、逐字段检查和转成 SshHop 的规则都在这里;新增 proxyJump;拒绝时给出 { field, reason }。 - 新增已保存连接文档(remote/connections.json,版本 1,最多 128 项、每项最多 10 个最近文件夹),以及 remote.connections / remote.editConnections(save / remove / rememberFolder 三选一)。 - probe 和 connect 改用同一份 SshConnection;端口 0、超时 0 等越界值不再按默认值处理,而是拒绝并指出字段。
1 parent 7c1cd55 commit 867c368

13 files changed

Lines changed: 1509 additions & 258 deletions

File tree

‎rust/contracts/rust-core-api.md‎

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,8 @@ or removing a command fails until the table changes in the same commit.
816816
| `remote.hostInfo` | Return cached handshake capabilities of one connection |
817817
| `remote.workspaceTrust` | Read remote workspace trust decisions and one open workspace's trust |
818818
| `remote.editWorkspaceTrust` | Record or remove one remote workspace trust decision |
819+
| `remote.connections` | Read the SSH connections the user saved |
820+
| `remote.editConnections` | Save, remove or record a folder on one saved SSH connection |
819821
| `git.historyRewritePreview` | Review undo, message edit, squash, or drop with complete messages, eligibility, and an immutable checkout expectation |
820822
| `git.remotes` | Return configured fetch remote names and URLs |
821823
| `git.tags` | Return tag names, tag object IDs, target IDs, dates, and annotations |
@@ -5661,10 +5663,28 @@ place.
56615663

56625664
`remote.probe` checks a saved SSH connection before a remote workspace is
56635665
opened. The request is `{ connection: { host, port?, user?, authentication?,
5664-
identityFile?, connectTimeoutSeconds?, keepAliveSeconds? } }`; unknown fields,
5665-
including any password, are rejected. `authentication` is `auto` (default),
5666-
`agent`, `privateKey` (requires `identityFile`), or `password`. `host` is a
5667-
name, address, or SSH configuration alias without a user.
5666+
identityFile?, proxyJump?, connectTimeoutSeconds?, keepAliveSeconds? } }`;
5667+
unknown fields, including any password, are rejected. `authentication` is
5668+
`auto` (default), `agent`, `privateKey` (requires `identityFile`), or
5669+
`password`. `host` is a name, address, or SSH configuration alias without a
5670+
user. Text fields are trimmed and empty optional fields dropped first;
5671+
`identityFile` is dropped unless `authentication` is `privateKey`. Then:
5672+
5673+
| Field | Accepted |
5674+
| --- | --- |
5675+
| `host` | 1 to 1024 bytes of ASCII letters, digits and `._-:%`, not starting with `-` |
5676+
| `port` | 1 to 65535; absent keeps the SSH configuration's port |
5677+
| `user` | at most 1024 bytes of ASCII letters, digits and `._-`, starting with a letter, digit or `_` |
5678+
| `identityFile` | at most 1024 bytes, no control characters; quoted for OpenSSH when it has spaces |
5679+
| `proxyJump` | `none`, or comma-separated `[user@]host[:port]`; absent keeps the SSH configuration's jump hosts |
5680+
| `connectTimeoutSeconds` | 1 to 600; absent is 15 |
5681+
| `keepAliveSeconds` | 1 to 3600; absent keeps the transport's default |
5682+
5683+
A refused connection is `invalid_request` whose `details` is the JSON
5684+
`{ field, reason }`, where `reason` is `required`, `invalid` or `tooLong`.
5685+
Every field becomes an allowlisted SSH option; options that run a local
5686+
command, such as `ProxyCommand`, are only read from the user's own SSH
5687+
configuration.
56685688

56695689
Core runs the system `ssh` with the probe of
56705690
[`remote-workspace-protocol-v1`](remote-workspace-protocol-v1.md)'s bootstrap
@@ -5689,7 +5709,7 @@ The result is `{ status, host, diagnostics }`:
56895709
`host` is `{ system, machine, homeDirectory, serverTarget }` from `uname -s`,
56905710
`uname -m` and `$HOME`; `serverTarget` is the server's Rust target or `null`.
56915711
`diagnostics` is the redacted end of OpenSSH's standard error (at most
5692-
4 KiB). A malformed connection is `invalid_request`, a missing `ssh` is
5712+
4 KiB). A malformed connection is `invalid_request` as above, a missing `ssh` is
56935713
`runtime_missing`, and cancellation or the five-minute bound are `cancelled`
56945714
and `timed_out`.
56955715

@@ -5698,6 +5718,48 @@ and `timed_out`.
56985718
`diskTotalBytes` and `diskAvailableBytes` (the file system holding `$HOME`),
56995719
and `uptimeSeconds`.
57005720

5721+
#### Saved connections
5722+
5723+
The connection window lists the SSH connections the user saved. Native
5724+
clients keep them in one machine-local document, read it, and publish the
5725+
`document` an edit returns atomically, as for the trust decisions; Core
5726+
validates the document and applies every edit.
5727+
5728+
`remote.connections` accepts `{ document: string|null }` and returns
5729+
`{ connections, storage: { relativePath: "remote/connections.json",
5730+
maximumBytes: 262144 } }`. `connections` is the list in the user's order;
5731+
each entry is `{ id, name, connection, recentFolders? }`, where `connection`
5732+
is the object `remote.probe` takes and `recentFolders` holds at most 10
5733+
normalized absolute host paths, most recent first.
5734+
5735+
`remote.editConnections` accepts `{ document }` with exactly one of:
5736+
5737+
- `save: { id?, name, connection }` normalizes and checks the connection,
5738+
then adds an entry with a new id or replaces the entry `id` in place.
5739+
`name` is trimmed, 1 to 128 characters without control characters, and
5740+
unique among the entries regardless of case. Replacing an entry with
5741+
another `host`, `port`, `user` or `proxyJump` clears its recent folders,
5742+
which belong to the target and not to the name.
5743+
- `remove: { id }` removes the entry; an absent id changes nothing.
5744+
- `rememberFolder: { id, path }` moves `path` to the front of the entry's
5745+
recent folders and keeps 10.
5746+
5747+
The result is `{ changed, document: string|null, connections, id? }`; `id`
5748+
names the entry a save wrote. An unchanged edit returns a `null` document and
5749+
must not create a source. Requests never carry a secret: a password field is
5750+
an unknown field and is rejected.
5751+
5752+
Bounds: 256 KiB per document and 128 entries. A document that cannot be
5753+
decoded, has duplicate keys, unknown fields, another version, a malformed
5754+
entry, or an id or name used twice is `parse_failed`; a malformed request or a
5755+
refused edit is `invalid_request`. Both carry `details` as the JSON
5756+
`{ field, reason }`, where `reason` is `required`, `invalid`, `tooLong`,
5757+
`duplicate`, `full` (128 entries) or `unknown` (no entry has the id).
5758+
5759+
Native clients keep the document below their platform user-preference
5760+
directory, never on a host, in a workspace or in an installed package. This
5761+
user-settings resource does not affect code signatures or update deltas.
5762+
57015763
#### Remote connections that stay open
57025764

57035765
`remote.connect` takes `{ connection, servers: [{ target, path }],

‎rust/crates/core/src/protocol/command.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,11 @@ pub enum RemoteCommand {
10091009
/// Records or removes one remote workspace trust decision
10101010
/// (`remote.editWorkspaceTrust`).
10111011
EditWorkspaceTrust,
1012+
/// Reads the SSH connections the user saved (`remote.connections`).
1013+
Connections,
1014+
/// Saves, removes or records a folder on one saved SSH connection
1015+
/// (`remote.editConnections`).
1016+
EditConnections,
10121017
}
10131018

10141019
#[derive(Debug, Clone)]
@@ -1573,6 +1578,8 @@ impl CoreCommand {
15731578
"remote.hostInfo" => Some(Self::Remote(RemoteCommand::HostInfo)),
15741579
"remote.workspaceTrust" => Some(Self::Remote(RemoteCommand::WorkspaceTrust)),
15751580
"remote.editWorkspaceTrust" => Some(Self::Remote(RemoteCommand::EditWorkspaceTrust)),
1581+
"remote.connections" => Some(Self::Remote(RemoteCommand::Connections)),
1582+
"remote.editConnections" => Some(Self::Remote(RemoteCommand::EditConnections)),
15761583
"git.historyRewritePreview" => Some(Self::Git(GitCommand::HistoryRewritePreview)),
15771584
"git.remotes" => Some(Self::Git(GitCommand::Remotes)),
15781585
"git.tags" => Some(Self::Git(GitCommand::Tags)),

‎rust/crates/core/src/runtime/dispatcher/remote.rs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,36 @@ pub(super) fn dispatch(command: RemoteCommand, id: Option<String>, payload: Valu
166166
Err(error) => CoreResponse::failure(id, error),
167167
}
168168
}
169+
RemoteCommand::Connections => {
170+
match serde_json::from_value(payload)
171+
.map_err(|error| {
172+
CoreError::new(
173+
ErrorCode::InvalidRequest,
174+
"Invalid remote connections request",
175+
)
176+
.with_details(error.to_string())
177+
})
178+
.and_then(lithe_workspace::connections::list)
179+
{
180+
Ok(value) => CoreResponse::success(id, value),
181+
Err(error) => CoreResponse::failure(id, error),
182+
}
183+
}
184+
RemoteCommand::EditConnections => {
185+
match serde_json::from_value(payload)
186+
.map_err(|error| {
187+
CoreError::new(
188+
ErrorCode::InvalidRequest,
189+
"Invalid remote connections request",
190+
)
191+
.with_details(error.to_string())
192+
})
193+
.and_then(lithe_workspace::connections::edit)
194+
{
195+
Ok(value) => CoreResponse::success(id, value),
196+
Err(error) => CoreResponse::failure(id, error),
197+
}
198+
}
169199
RemoteCommand::Disconnect => {
170200
match serde_json::from_value::<lithe_workspace::sessions::DisconnectRequest>(payload) {
171201
Ok(request) => CoreResponse::success(

0 commit comments

Comments
 (0)