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
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Agent 笔记:远程开发连接窗口——已保存的连接与测试连接

状态:已实现

## 先说结论

欢迎页“更多”菜单和标题栏项目菜单里的“远程开发”会打开一个窗口:左边是保存的 SSH 连接,右边是选中连接的表单、“测试连接”和测试到的主机信息。连接列表由 Rust Core 定义格式和规则(`remote.connections`、`remote.editConnections`),桌面端只负责读文件、把 Core 生成的整份文档原子写回。窗口里不保存任何秘密:密码、私钥口令和主机指纹确认都由 OpenSSH 在测试时经 AskPass(Lithe 自己充当 `SSH_ASKPASS` 程序,把问题转回主程序)提出,和 Git 认证共用同一个提问框,答完即丢。

## 问题

Issue #2231 要求远程开发先能“告诉 Lithe 连哪台机器、怎么登录”,并在打开项目前确认这台机器能用。旧客户端做过一版(见已归档的 `archived/feature/2026-10-24-remote-ssh-connections.md`),但它把密码记进钥匙串、把整张列表当工作副本编辑,而且字段规则写在 Swift 里。Viva 桌面端重新实现时要解决:

- 字段规则(主机不能带 `user@`、端口范围、名称去重、最多 128 项)放在哪里,换 UI 壳子时不用重写;
- 保存时如果文件已被另一个窗口或手工改过,不能把别人的修改覆盖掉;
- 设计 S12 规定秘密只出现在 AskPass 的问题和答案里,S15 规定 Lithe 不读私钥内容。

## 决策

### 连接规则在 Core,文件读写在桌面端

- 字段的规范化、逐字段检查和文档格式都在 Core 的 `lithe_remote::client::connections`。Core 拒绝时返回 `{ field, reason }`,窗口把原因显示在对应字段下面,不弹通用错误框。
- `desktop/internal/remoteconn` 只做两件事:按 Core 给出的相对路径和大小上限,从平台偏好目录读取 `remote/connections.json`;把 Core 编辑后返回的整份文档,以“读取时的字节”为前提原子写回(`configfile`)。文件在读取之后被改过,写入就失败(`ErrConflict`),窗口提示重新读取,不会覆盖。
- 每次点“保存”或确认“删除”都立即写一次,并且写完重新读取文件来显示,不维护一份和磁盘不同的工作副本。打开窗口只读不写,没有文件时不会创建。

### 秘密不经过窗口

- 表单只有“认证方式”和私钥路径,没有密码框。私钥只保存路径,Lithe 不打开、不读取私钥文件,也不接受粘贴的私钥。
- “测试连接”调用 Core 的 `RemoteProbe`,Core 运行 PATH 里的 OpenSSH。OpenSSH 的每个问题(密码、口令、验证码、首次连接时的主机指纹确认)通过 AskPass 回到窗口,进入和 Git 认证相同的队列(`gitAuthState`),问题带 `Remote` 标记,只换标题、提示和回答用的 Core 方法(`RemoteAuthRespond`)。
- 取消提问、点“停止”或关闭窗口都会取消测试;被取消的测试不再追问,也不显示结果。

### 日志只记动作

窗口只记录 `remote.connections.open`、`remote.connection.new/select/save/remove/test/test.stop` 和 `remote.authentication.answer/retry/cancel` 等动作,以及 Core 的固定状态或错误码。主机、用户名、私钥路径、OpenSSH 输出、提问原文和答案都不进日志,`remote_test.go` 用 `loggedAny` 检查。

### 列表选中行跟着表单走

Viva 的 `ListState.Select` 只能选到列表上一帧已经声明过的行。保存一个新连接后,表单立刻编辑它,但列表这一帧还不认识新行,选中会被截到旧的最后一行。因此:行高亮直接按“表单正在编辑的连接”画;列表声明完行之后,再调用 `syncListSelection` 把键盘选中行同步过去。这是 Viva 的既定行为,不是缺陷,不在 Viva 侧改。

## 考虑过的备选方案

- **桌面端自己校验字段、自己定义 JSON 格式**:Go 里写一遍,将来换 UI 壳子还要再写一遍,和“通用逻辑进 Rust Core”的约定冲突。没有采用。
- **沿用旧客户端的“记住到钥匙串”**:用户少输一次密码,但秘密会离开 AskPass 的问答,违反 S12;而且记错的密码会被重复发送。没有采用。
- **把整张列表当工作副本编辑,关窗口时统一保存**:可以批量修改,但关窗口时要么丢修改,要么要再问一次;和“保存后重新读取显示真实来源”的设置规则也不一致。没有采用。
- **为远程连接单独做一个认证弹窗**:两个弹窗可能同时出现、互相遮挡。共用 Git 的队列后,同一时间只显示一个问题,按提出顺序逐个回答。

## 后果

- 收益:字段规则只有 Core 一份;Windows 和 macOS 用同一份 Go 代码;秘密只在 OpenSSH 和 AskPass 之间流动;并发修改不会互相覆盖。
- 代价:每次保存都是一次完整的读、改、写和重新读取;Core 没有返回字段的错误只能显示在窗口底部。
- Windows 上 OpenSSH 能否通过 GUI 子系统的 `lithe` 助手读到答案仍待实机验证;打开远程项目、文件夹选择和远端服务部署在后续阶段实现。

## 验证

- `desktop/internal/remoteconn/remoteconn_test.go`:读取不创建文件、冲突时不写入、损坏文件报 `ErrDamaged`。
- `desktop/internal/workbench/remote_test.go`:保存、改名、删除前确认;字段被拒绝时显示在字段下且不写文件;新保存的连接是列表选中行;测试时在窗口中回答密码并显示主机信息;停止和关窗口都会取消测试且不再追问;日志不含主机和用户名。
- `desktop/internal/workbench/remote_connections_visual_test.go` 生成 `docs/assets/remote-connections/` 下的截图。

```bash
scripts/desktop/desktop-go.sh test ./internal/remoteconn ./internal/workbench -run 'Remote|GitAuth' -count=1
```

## 适用范围

`desktop/internal/remoteconn/`、`desktop/internal/workbench/remote.go`、`desktop/internal/workbench/remote_view.go`,以及 `desktop/internal/workbench/git_auth.go` 中带 `Remote` 标记的提问。后续“打开远程工作区”“记住最近文件夹”都要通过同一个 `remoteconn` 和 Core 的 `remote.editConnections`,不要另存一份连接信息。
3 changes: 3 additions & 0 deletions desktop/internal/core/gitauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type GitAuthChallenge struct {
Attempt int
// WorkingDirectory is the directory the Git process runs in.
WorkingDirectory string
// Remote says the question comes from the SSH client of a remote connection (RemoteProbe),
// not from Git: it is answered with RemoteAuthRespond.
Remote bool
// Done is closed once the request that asked has returned; no answer reaches it after that.
Done <-chan struct{}
}
Expand Down
249 changes: 249 additions & 0 deletions desktop/internal/core/remote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
package core

import (
"context"
"encoding/json"
"errors"
)

// Remote development connections: the SSH connections the user saved, and the probe that checks
// one reaches a Linux host a remote server build exists for. Core validates every entry, picks
// ids and applies one edit at a time against a machine-local document the host keeps in its
// preference directory; it never stores a secret (design S08–S10, S12, S15). Questions OpenSSH
// asks during a probe (passwords, key passphrases, one-time codes, unknown host keys) reach the
// host as authentication challenges, like Git's.

// Authentication modes of an SSHConnection. The empty mode is RemoteAuthenticationAuto.
const (
// RemoteAuthenticationAuto lets OpenSSH and the user's ~/.ssh/config choose.
RemoteAuthenticationAuto = "auto"
// RemoteAuthenticationAgent uses only the keys of the running SSH agent.
RemoteAuthenticationAgent = "agent"
// RemoteAuthenticationPrivateKey uses only IdentityFile; Lithe passes its path to OpenSSH and
// never reads the key.
RemoteAuthenticationPrivateKey = "privateKey"
// RemoteAuthenticationPassword asks for a password or one-time code during the connection.
RemoteAuthenticationPassword = "password"
)

// SSHConnection is how OpenSSH reaches a host. Empty and zero fields are left to OpenSSH and the
// user's ~/.ssh/config, so Host may be an alias defined there.
type SSHConnection struct {
// Host is a host name, an address or a ~/.ssh/config alias, without a user or a port.
Host string `json:"host"`
// Port is the SSH port; 0 leaves it to OpenSSH.
Port uint16 `json:"port,omitempty"`
// User is the login name; empty leaves it to OpenSSH.
User string `json:"user,omitempty"`
// Authentication is one of the RemoteAuthentication modes; empty is RemoteAuthenticationAuto.
Authentication string `json:"authentication,omitempty"`
// IdentityFile is the private key path, required by RemoteAuthenticationPrivateKey and
// dropped by Core in every other mode.
IdentityFile string `json:"identityFile,omitempty"`
// ProxyJump is OpenSSH's jump host list, for example "bastion" or "user@a,user@b:2222".
ProxyJump string `json:"proxyJump,omitempty"`
// ConnectTimeoutSeconds bounds the TCP connection, 1 to 600; 0 is Core's default of 15.
ConnectTimeoutSeconds uint32 `json:"connectTimeoutSeconds,omitempty"`
// KeepAliveSeconds sends a keep-alive this often, 1 to 3600; 0 sends none.
KeepAliveSeconds uint32 `json:"keepAliveSeconds,omitempty"`
}

// SavedRemoteConnection is one entry of the saved connections.
type SavedRemoteConnection struct {
// ID is Core's stable identifier of the entry.
ID string `json:"id"`
// Name is the user's label.
Name string `json:"name"`
// Connection is how to reach the host.
Connection SSHConnection `json:"connection"`
// RecentFolders are the host folders opened over the entry, the latest first.
RecentFolders []string `json:"recentFolders,omitempty"`
}

// RemoteConnectionsStorage says where the host keeps the document: RelativePath inside the
// platform preference directory, at most MaximumBytes long.
type RemoteConnectionsStorage struct {
RelativePath string `json:"relativePath"`
MaximumBytes int64 `json:"maximumBytes"`
}

// RemoteConnectionsState is the document as Core reads it.
type RemoteConnectionsState struct {
Connections []SavedRemoteConnection `json:"connections"`
Storage RemoteConnectionsStorage `json:"storage"`
}

// RemoteConnectionSave adds a connection, or replaces the entry ID when ID is set.
type RemoteConnectionSave struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Connection SSHConnection `json:"connection"`
}

// RemoteConnectionID names an entry to remove.
type RemoteConnectionID struct {
ID string `json:"id"`
}

// RemoteConnectionFolder records a host folder opened over the entry ID.
type RemoteConnectionFolder struct {
ID string `json:"id"`
Path string `json:"path"`
}

// RemoteConnectionEdit is exactly one of Save, Remove and RememberFolder.
type RemoteConnectionEdit struct {
Save *RemoteConnectionSave `json:"save,omitempty"`
Remove *RemoteConnectionID `json:"remove,omitempty"`
RememberFolder *RemoteConnectionFolder `json:"rememberFolder,omitempty"`
}

// RemoteConnectionsPublication is the outcome of an edit. Document is the text to publish, nil
// when the edit changed nothing; ID names the entry a save wrote.
type RemoteConnectionsPublication struct {
Changed bool `json:"changed"`
Document *string `json:"document"`
Connections []SavedRemoteConnection `json:"connections"`
ID string `json:"id"`
}

// RemoteConnections reads the saved connections from document, the text of the source or nil
// when it does not exist. A document Core cannot decode fails with CodeParseFailed, with the
// field and the reason in RemoteConnectionFieldError.
func (c *Client) RemoteConnections(ctx context.Context, document *string) (RemoteConnectionsState, error) {
var state RemoteConnectionsState
err := c.Execute(ctx, "remote.connections", struct {
Document *string `json:"document"`
}{document}, &state)
return state, err
}

// EditRemoteConnections applies edit to document. A refused entry fails with CodeInvalidRequest,
// with the field and the reason in RemoteConnectionFieldError.
func (c *Client) EditRemoteConnections(ctx context.Context, document *string, edit RemoteConnectionEdit) (RemoteConnectionsPublication, error) {
var publication RemoteConnectionsPublication
err := c.Execute(ctx, "remote.editConnections", struct {
Document *string `json:"document"`
RemoteConnectionEdit
}{document, edit}, &publication)
return publication, err
}

// RemoteConnectionFieldError reads the field and the reason Core named when it refused a
// connection or a document: field is a JSON name such as "host" or "connectTimeoutSeconds", and
// reason one of "required", "invalid", "tooLong", "duplicate", "full" and "unknown".
func RemoteConnectionFieldError(err error) (field, reason string, ok bool) {
var coreErr *Error
if !errors.As(err, &coreErr) || coreErr.Details == "" {
return "", "", false
}
if coreErr.Code != CodeInvalidRequest && coreErr.Code != CodeParseFailed {
return "", "", false
}
var details struct {
Field string `json:"field"`
Reason string `json:"reason"`
}
if json.Unmarshal([]byte(coreErr.Details), &details) != nil || details.Field == "" || details.Reason == "" {
return "", "", false
}
return details.Field, details.Reason, true
}

// RemoteProbeStatus is the outcome of a probe. A host that cannot be reached is an outcome, not
// an error.
type RemoteProbeStatus string

const (
// RemoteProbeConnected means a shell ran on the host and a server build exists for it.
RemoteProbeConnected RemoteProbeStatus = "connected"
// RemoteProbeUnsupportedPlatform means the host answered, but no server build exists for it.
RemoteProbeUnsupportedPlatform RemoteProbeStatus = "unsupportedPlatform"
// RemoteProbeAuthenticationFailed means the server rejected every offered credential.
RemoteProbeAuthenticationFailed RemoteProbeStatus = "authenticationFailed"
// RemoteProbeHostKeyRejected means the host key is unknown and was declined, or changed.
RemoteProbeHostKeyRejected RemoteProbeStatus = "hostKeyRejected"
// RemoteProbeUnreachable means the name did not resolve or the host did not answer in time.
RemoteProbeUnreachable RemoteProbeStatus = "unreachable"
// RemoteProbeFailed means the connection failed for another reason; see Diagnostics.
RemoteProbeFailed RemoteProbeStatus = "failed"
)

// RemoteHostDetails are facts about a probed host for display; each is empty or zero when the
// host did not report it.
type RemoteHostDetails struct {
HostName string `json:"hostName"`
OperatingSystem string `json:"operatingSystem"`
Kernel string `json:"kernel"`
CPUCount uint32 `json:"cpuCount"`
MemoryBytes uint64 `json:"memoryBytes"`
DiskTotalBytes uint64 `json:"diskTotalBytes"`
DiskAvailableBytes uint64 `json:"diskAvailableBytes"`
UptimeSeconds uint64 `json:"uptimeSeconds"`
}

// RemoteProbedHost is what the host's shell reported.
type RemoteProbedHost struct {
// System is `uname -s`, for example "Linux".
System string `json:"system"`
// Machine is `uname -m`, for example "x86_64" or "aarch64".
Machine string `json:"machine"`
// HomeDirectory is the login's home directory, empty when unknown.
HomeDirectory string `json:"homeDirectory"`
// ServerTarget is the Rust target of the server build for the host, empty when none exists.
ServerTarget string `json:"serverTarget"`
// Details are facts for display.
Details RemoteHostDetails `json:"details"`
}

// RemoteProbeResult is the outcome of a probe, and the host when its shell ran.
type RemoteProbeResult struct {
Status RemoteProbeStatus `json:"status"`
Host *RemoteProbedHost `json:"host"`
// Diagnostics is the redacted end of OpenSSH's standard error. It is upstream text, shown
// as it is, and may still name the host, so it is never logged.
Diagnostics string `json:"diagnostics"`
}

// RemoteProbe connects to the host of connection with the system OpenSSH client and reports its
// platform. Each question OpenSSH asks reaches the prompt WithGitAuthentication put in ctx as a
// challenge with Remote set, answered with RemoteAuthRespond; without a prompt each question is
// cancelled at once. Core bounds a probe, answers included, to five minutes. The probe changes
// nothing, so an observation failure is reported only when the request itself succeeded.
func (c *Client) RemoteProbe(ctx context.Context, connection SSHConnection) (RemoteProbeResult, error) {
prompt, _ := ctx.Value(gitAuthenticationKey{}).(func(GitAuthChallenge))
if prompt == nil {
prompt = func(challenge GitAuthChallenge) {
// The answer must not wait on the request's goroutine, which delivers the event.
go func() { _, _ = c.RemoteAuthRespond(context.Background(), challenge.RequestID, nil) }()
}
}
returned := make(chan struct{})
defer close(returned)
observer := gitAuthenticationObserver(func(challenge GitAuthChallenge) {
challenge.Remote = true
prompt(challenge)
}, returned, nil)
var result RemoteProbeResult
observationErr, err := c.ExecuteWithEvents(ctx, "remote.probe", struct {
Connection SSHConnection `json:"connection"`
}{connection}, &result, observer)
if err != nil {
return RemoteProbeResult{}, err
}
if observationErr != nil {
return RemoteProbeResult{}, observationErr
}
return result, nil
}

// RemoteAuthRespond answers the Remote challenge requestID with the text the user typed, or nil
// to cancel. accepted is false when the challenge no longer waits. The answer is never logged.
// Core answers at once, so the call may run on any goroutine, but not on the UI thread.
func (c *Client) RemoteAuthRespond(ctx context.Context, requestID string, answer *string) (accepted bool, err error) {
var result struct {
Accepted bool `json:"accepted"`
}
err = c.Execute(ctx, "remote.authRespond", gitAuthReply{RequestID: requestID, Answer: answer}, &result)
return result.Accepted, err
}
Loading
Loading