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
Expand Up @@ -106,7 +106,7 @@ Linux 静态版用 Rust 自带的 `rust-lld` 链接,在 macOS 和 Linux 上都
- **路径**:协议里的路径是远端主机上的绝对路径。这一层是文件系统和进程的适配器,工具链、终端和调试器都需要真实的主机路径;以工作区为根的相对标识在这一层之上构造。
- **错误**:错误码稳定(例如 `NOT_FOUND`、`CONFLICT`、`DIRECTORY_NOT_EMPTY`),客户端按错误码分支。`message` 只给人看,OS 错误号放在 `details` 里。
- **兼容**:不兼容的修改才提升协议版本号。新增方法通过 `features` 列表声明,客户端先查列表再调用。
- **只走标准输入输出**:服务的协议通道不监听任何 TCP 端口,所以共享主机上的其他用户连不进协议。端口转发也走这条流。唯一的例外是 #2231 §8 设计的主机侧转发规则(T3 主机端口到其他服务器、T4 反向转发,尚未实现):服务只在用户建立并确认的规则存在期间按规则打开监听,默认只绑定主机回环地址(#2231 S20、S27、S29)。
- **只走标准输入输出**:服务的协议通道不监听任何 TCP 端口,所以共享主机上的其他用户连不进协议。端口转发也走这条流。唯一的例外是 #2231 §8 设计的主机侧转发规则(T3 主机端口到其他服务器、T4 反向转发):服务只在用户建立并确认的规则存在期间按规则打开监听,默认只绑定主机回环地址(#2231 S20、S27、S29)。

### 文件和进程的语义

Expand Down Expand Up @@ -178,7 +178,7 @@ inotify 的坑:目录是逐个注册的,新建目录要在收到创建事件
- **终端**:用伪终端(PTY)运行,被启动的程序成为新会话的首进程,并把这个终端设为控制终端,和 SSH 登录时完全一样。所以作业控制、`Ctrl-C`、窗口大小变化(内核会发 `SIGWINCH`)都符合用户习惯。默认程序是用户的登录 shell,按 POSIX 惯例在 `argv[0]` 前加 `-`,sh、bash、zsh、fish、csh 都认。关闭终端相当于关掉终端窗口:向它的整个会话发 `SIGHUP`,2 秒后还在的发 `SIGKILL`。
- **登录环境**:经 SSH 或 `docker exec` 执行的命令,拿到的环境往往很少,nvm、SDKMAN、Homebrew 加进 `PATH` 的路径都不在里面。服务会用用户自己的 shell 以交互式登录 shell 启动一次,让它运行 `lithe-remote-server --print-environment`,读取它最终得到的环境,并在会话内缓存。用服务自己的二进制输出环境,可以避开 GNU 和 BSD `env` 的参数差异;每次尝试最多等 10 秒,profile 卡住(例如启动 tmux)也不会挂住服务;交互式失败就退回非交互登录 shell,再失败就用服务自己的环境,并在 `diagnostics` 里说明原因。`process.spawn` 指定 `environment: login` 就会用这个环境作为基础。
- **复制**:`fs.copy` 拒绝把文件复制到它自己身上(同一路径、经过符号链接的任一方向、或者是硬链接),也拒绝在解析链接之后把目录复制进它自己。覆盖写入会在读取源文件之前先截断目标,第一版因此会把源文件清空。socket、FIFO 和设备文件从不复制:作为复制对象本身时返回 `UNSUPPORTED`,在目录树里时跳过并计数,因为打开 FIFO 读取可能永远阻塞。
- **端口**:隧道把远端的 TCP 端口或 Unix socket(Docker、数据库的 socket)接到协议流里。客户端的 `PortForward` 在本地监听,每个连接开一条隧道,所以浏览器访问 `127.0.0.1:<本地端口>` 就能到达只监听在远端回环地址上的服务。本地客户端发完请求后半关闭时(HTTP/1.0、`nc -N`),转发用 `tunnel.shutdownWrite`:先把排队的数据写完,再只关闭写入端,远端的响应照常返回;`tunnel.close` 只用于中止。`ports.listening` 列出远端正在监听的端口,桌面端的“端口”工具窗口用它列出主机上的端口,默认不自动转发。转发目标的输入(端口、`主机:端口`、网址)由 Core 的 `remote.parseForwardAddress` 解析。转发按“在哪一侧监听、由哪一侧连接哪个目标”建模为四类规则(#2231 §8 D10):T1 本机到主机回环、T2 本机到主机能访问的其他服务器(已实现);T3 主机端口到其他服务器、T4 反向转发(已设计,未实现)。T3 在主机上完成转发,数据不绕回本机;T4 默认关闭。
- **端口**:隧道把远端的 TCP 端口或 Unix socket(Docker、数据库的 socket)接到协议流里。客户端的 `PortForward` 在本地监听,每个连接开一条隧道,所以浏览器访问 `127.0.0.1:<本地端口>` 就能到达只监听在远端回环地址上的服务。本地客户端发完请求后半关闭时(HTTP/1.0、`nc -N`),转发用 `tunnel.shutdownWrite`:先把排队的数据写完,再只关闭写入端,远端的响应照常返回;`tunnel.close` 只用于中止。`ports.listening` 列出远端正在监听的端口,桌面端的“端口”工具窗口用它列出主机上的端口,默认不自动转发。转发目标的输入(端口、`主机:端口`、网址)由 Core 的 `remote.parseForwardAddress` 解析。转发按“在哪一侧监听、由哪一侧连接哪个目标”建模为四类规则(#2231 §8 D10):T1 本机到主机回环、T2 本机到主机能访问的其他服务器(`remote.startForward`);T3 主机端口到其他服务器(`remote.startHostForward`)、T4 反向转发(`remote.startReverseForward`)。T3 在主机上完成转发,数据不绕回本机,默认只监听主机回环地址,监听全部网卡要逐次确认;T4 默认关闭,只有偏好里允许反向转发时 Core 才接受,主机只在回环地址监听、本机只连接回环地址。四类共用会话转发上限;T3、T4 随会话结束,重连后不自动恢复。

### 后续阶段

Expand Down
10 changes: 10 additions & 0 deletions desktop/internal/core/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ const (
// CodeForwardLimitReached means the session already runs as many forwards as Core allows;
// details carry the maximum.
CodeForwardLimitReached ErrorCode = "forward_limit_reached"
// CodeForwardHostPortInUse means the host port a host or reverse forward asked for is used by
// another program of the host; unlike a local port it is never replaced by another one.
CodeForwardHostPortInUse ErrorCode = "forward_host_port_in_use"
// CodeForwardHostPortDenied means the host refused to listen on the port, such as a port
// below 1024 for an unprivileged user.
CodeForwardHostPortDenied ErrorCode = "forward_host_port_denied"
// CodeForwardHostUnsupported means the remote server cannot listen on host ports.
CodeForwardHostUnsupported ErrorCode = "forward_host_unsupported"
// CodeReverseForwardDisabled means the remote preferences do not allow reverse forwards.
CodeReverseForwardDisabled ErrorCode = "reverse_forward_disabled"
// CodeRemoteServerInvalid means the remote server build for the host's platform could not be
// read, or its SHA-256 is not the one the application's manifest names; nothing was sent to
// the host.
Expand Down
128 changes: 114 additions & 14 deletions desktop/internal/core/remote_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,61 @@ package core

import "context"

// RemoteForward is one forward of a session: a listener on this computer's loopback interface
// whose connections Core carries over the session to a port as the host sees it. Only this
// computer's processes can reach the listener (S27), and the host never reaches back (S29).
// RemoteForwardKind is which way a forward carries connections.
type RemoteForwardKind string

const (
// RemoteForwardLocal listens on this computer's loopback interface and reaches a port as the
// host sees it (remote.startForward).
RemoteForwardLocal RemoteForwardKind = "local"
// RemoteForwardHost makes the host listen on a port and connect every accepted connection to
// another server itself; the bytes never cross the session (remote.startHostForward).
RemoteForwardHost RemoteForwardKind = "host"
// RemoteForwardReverse makes the host listen on its loopback interface and brings every
// accepted connection back to a port on this computer's loopback interface
// (remote.startReverseForward).
RemoteForwardReverse RemoteForwardKind = "reverse"
)

// RemoteForward is one forward of a session. A local forward listens on this computer's loopback
// interface and carries its connections over the session to a port as the host sees it; only this
// computer's processes can reach the listener (S27), and the host never reaches back (S29). A host
// or reverse forward listens on the host instead. Fields that do not apply to Kind are zero.
type RemoteForward struct {
// ID names the forward to RemoteStopForward; it is unique within the session.
ID uint64 `json:"id"`
// LocalHost and LocalPort are where the forward listens on this computer; LocalHost is
// always a loopback address.
LocalHost string `json:"localHost"`
LocalPort uint16 `json:"localPort"`
// RequestedLocalPort is the local port the forward was asked for, absent when any was
// Kind is local, host or reverse; a Core that predates the kinds sends none, which is local.
Kind RemoteForwardKind `json:"kind,omitempty"`
// LocalHost and LocalPort are where a local forward listens on this computer, and the port
// on this computer a reverse forward reaches; LocalHost is always a loopback address.
LocalHost string `json:"localHost,omitempty"`
LocalPort uint16 `json:"localPort,omitempty"`
// RequestedLocalPort is the local port a local forward was asked for, absent when any was
// accepted; LocalPortChanged reports that it was taken and the system chose LocalPort.
RequestedLocalPort *uint16 `json:"requestedLocalPort,omitempty"`
LocalPortChanged bool `json:"localPortChanged"`
// TargetHost and TargetPort are the forward's target as the host resolves it, and
// LoopbackTarget reports that the target is on the host's own loopback interface.
TargetHost string `json:"targetHost"`
TargetPort uint16 `json:"targetPort"`
// HostListenHost and HostListenPort are where a host or reverse forward listens on the host,
// and RequestedHostPort the host port it was asked for, absent when any was accepted.
// AllInterfaces reports a host forward that listens on every interface of the host.
HostListenHost string `json:"hostListenHost,omitempty"`
HostListenPort uint16 `json:"hostListenPort,omitempty"`
RequestedHostPort *uint16 `json:"requestedHostPort,omitempty"`
AllInterfaces bool `json:"allInterfaces"`
// TargetHost and TargetPort are a local or host forward's target as the host resolves it,
// and LoopbackTarget reports that the target is on the host's own loopback interface.
TargetHost string `json:"targetHost,omitempty"`
TargetPort uint16 `json:"targetPort,omitempty"`
LoopbackTarget bool `json:"loopbackTarget"`
// Connections is the number of connections the forward carries now.
Connections uint64 `json:"connections"`
// Connections is the number of connections the forward carries now, and FailedConnections
// those of a host or reverse forward that could not reach their target.
Connections uint64 `json:"connections"`
FailedConnections uint64 `json:"failedConnections"`
}

// IsLocal reports whether the forward listens on this computer: a local forward, or one from a
// Core that predates the kinds.
func (f RemoteForward) IsLocal() bool {
return f.Kind == "" || f.Kind == RemoteForwardLocal
}

// RemoteForwardTarget is what RemoteStartForward forwards to.
Expand Down Expand Up @@ -70,6 +104,72 @@ func (c *Client) RemoteStartForward(ctx context.Context, sessionID string, targe
return result.Forward, result.Created, nil
}

// RemoteHostForwardRule is what RemoteStartHostForward makes the host listen on and reach.
type RemoteHostForwardRule struct {
// Host and Port are the target as the host resolves it, as RemoteForwardTarget names one.
Host string `json:"targetHost"`
Port uint16 `json:"targetPort"`
// ListenPort is the host port to listen on; 0 lets the host choose one. A taken port is never
// replaced: it fails with CodeForwardHostPortInUse.
ListenPort uint16 `json:"listenPort,omitempty"`
// AllInterfaces makes the host listen on every interface instead of its loopback interface;
// the window asks the user every time and never remembers it.
AllInterfaces bool `json:"allInterfaces,omitempty"`
// AllowNonLoopback records that the user confirmed a target outside the host's loopback
// interface (S28), as for RemoteStartForward.
AllowNonLoopback bool `json:"allowNonLoopbackTarget,omitempty"`
}

// RemoteStartHostForward makes the host of the session sessionID listen on a port and connect
// every accepted connection to rule's target. created is false when the same rule already ran:
// forward is then that forward, unchanged. Besides RemoteStartForward's failures, a host port in
// use fails with CodeForwardHostPortInUse, one the host refuses with CodeForwardHostPortDenied
// and a server that cannot listen with CodeForwardHostUnsupported.
func (c *Client) RemoteStartHostForward(ctx context.Context, sessionID string, rule RemoteHostForwardRule) (forward RemoteForward, created bool, err error) {
var result struct {
Forward RemoteForward `json:"forward"`
Created bool `json:"created"`
}
err = c.Execute(ctx, "remote.startHostForward", struct {
SessionID string `json:"sessionId"`
RemoteHostForwardRule
}{sessionID, rule}, &result)
if err != nil {
return RemoteForward{}, false, err
}
return result.Forward, result.Created, nil
}

// RemoteReverseForwardRule is what RemoteStartReverseForward makes the host listen on and reach.
type RemoteReverseForwardRule struct {
// LocalPort is the port on this computer's loopback interface each connection reaches.
LocalPort uint16 `json:"localPort"`
// ListenPort is the port on the host's loopback interface; 0 lets the host choose one.
ListenPort uint16 `json:"listenPort,omitempty"`
}

// RemoteStartReverseForward makes the host of the session sessionID listen on its loopback
// interface and bring every accepted connection back to rule's port on this computer.
// preferences is the remote preference document, nil when there is none; unless it allows
// reverse forwards the request fails with CodeReverseForwardDisabled. created is false when the
// same rule already ran. A LocalPort of 0 fails with CodeInvalidRequest naming localPort; the
// other failures are RemoteStartHostForward's.
func (c *Client) RemoteStartReverseForward(ctx context.Context, sessionID string, rule RemoteReverseForwardRule, preferences *string) (forward RemoteForward, created bool, err error) {
var result struct {
Forward RemoteForward `json:"forward"`
Created bool `json:"created"`
}
err = c.Execute(ctx, "remote.startReverseForward", struct {
SessionID string `json:"sessionId"`
RemoteReverseForwardRule
Preferences *string `json:"preferences"`
}{sessionID, rule, preferences}, &result)
if err != nil {
return RemoteForward{}, false, err
}
return result.Forward, result.Created, nil
}

// RemoteStopForward stops the forward id of the session sessionID and closes its connections;
// stopped is false when the session has no such forward.
func (c *Client) RemoteStopForward(ctx context.Context, sessionID string, id uint64) (stopped bool, err error) {
Expand Down
21 changes: 19 additions & 2 deletions desktop/internal/core/remote_preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const (
type RemotePreferenceValues struct {
SSHProgram *string `json:"sshProgram"`
ReconnectLimitSeconds *uint32 `json:"reconnectLimitSeconds"`
// AllowReverseForwarding is whether the Ports window may start reverse forwards.
AllowReverseForwarding *bool `json:"allowReverseForwarding"`
}

// RemoteSSHProgram is the OpenSSH client Core starts. Path is nil only when the automatic
Expand All @@ -51,6 +53,13 @@ type RemoteReconnect struct {
DelaysMilliseconds []uint64 `json:"delaysMilliseconds"`
}

// RemoteReverseForwarding is whether reverse forwards may start. The automatic value refuses
// them, because a port a reverse forward opens is open to every user of the host.
type RemoteReverseForwarding struct {
Source RemotePreferenceSource `json:"source"`
Allowed bool `json:"allowed"`
}

// RemotePreferenceStorage is where the document lives below the application's preference root.
type RemotePreferenceStorage struct {
RelativePath string `json:"relativePath"`
Expand All @@ -69,6 +78,7 @@ type RemotePreferences struct {
Preferences RemotePreferenceValues `json:"preferences"`
SSH RemoteSSHProgram `json:"ssh"`
Reconnect RemoteReconnect `json:"reconnect"`
Reverse RemoteReverseForwarding `json:"reverseForwarding"`
Storage RemotePreferenceStorage `json:"storage"`
SSHConfig *RemoteSSHConfigFile `json:"sshConfig"`
}
Expand All @@ -90,8 +100,15 @@ func (c *Client) RemotePreferences(ctx context.Context, document *string, homeDi
// RemotePreferenceEdit changes one value: set exactly one field. A nil Path or Seconds makes the
// value automatic again.
type RemotePreferenceEdit struct {
SSHProgram *RemoteSSHProgramEdit `json:"sshProgram,omitempty"`
ReconnectLimit *RemoteReconnectLimitEdit `json:"reconnectLimit,omitempty"`
SSHProgram *RemoteSSHProgramEdit `json:"sshProgram,omitempty"`
ReconnectLimit *RemoteReconnectLimitEdit `json:"reconnectLimit,omitempty"`
Reverse *RemoteReverseForwardingEdit `json:"reverseForwarding,omitempty"`
}

// RemoteReverseForwardingEdit allows or refuses reverse forwards; a nil Allowed is automatic,
// which refuses them.
type RemoteReverseForwardingEdit struct {
Allowed *bool `json:"allowed"`
}

// RemoteSSHProgramEdit sets the SSH program to an absolute path of an executable file.
Expand Down
Loading
Loading