Skip to content

feat(remote): 主机侧与反向端口转发:远端服务监听端口,Core 新增两个转发命令与反向开关 (Refs 1lck/Lithe-IDEA#2231) - #159

Merged
xiaoyumuxi merged 1 commit into
mainfrom
feat/2231-host-forward
Nov 21, 2026
Merged

xiaoyumuxi merged 1 commit into
mainfrom
feat/2231-host-forward

Conversation

@xiaoyumuxi

Copy link
Copy Markdown
Contributor

关联 Issue

1lck/Lithe-IDEA#2231(远程开发完整设计)D10 转发规则 T3、T4:让 Linux 主机自己监听端口并转到另一台服务器(主机侧转发),以及让主机回环端口连回本机(反向转发)。本 PR 是 Core 部分,桌面端端口页的“添加转发规则…”和设置开关另开 PR。

类型

  • feat:远端服务新增 listener.* 监听方法组(feature listener);Core 新增 remote.startHostForward、remote.startReverseForward,远程工作区设置新增“允许反向转发”,新增四个错误码

改动说明

远端服务:监听(新文件 crates/remote/src/server/listener.rs,接线在 connection.rs、session.rs、tunnel.rs、protocol/methods.rs)

  • listener.open { listenerId, host, port, relay? } 返回 { port }。host 只接受 IP,不解析主机名,所以主机不会去解析客户端发来的地址:
    • 带 relay(一个 tunnel.open 目标)是主机侧规则:主机自己连目标、在主机上复制字节,数据不经过连接;host 是回环地址,或在用户确认监听全部网卡时为 0.0.0.0 / ::。
    • 不带 relay 是反向规则:host 必须是回环地址;每个接入的连接先挂起,发 listener.accepted { listenerId, acceptId },客户端订阅一条新隧道后用 listener.adopt { listenerId, acceptId, tunnelId } 接管,之后和客户端自己开的隧道一样。
  • listener.close 返回最终状态并关闭端口,以及它转发或挂起的所有连接;最多等 1 秒让端口释放,规则改到同一端口能马上重新绑定。listener.list 列出 listenerId、port、connections、failedConnections。
  • 错误:端口被占用是新的 ADDRESS_IN_USE,系统拒绝(如普通用户绑 1024 以下)是 PERMISSION_DENIED,主机名或不允许的地址是 INVALID_REQUEST,接管一个已不在等待的连接是 UNKNOWN_RESOURCE。
  • 上限:每个会话 64 个监听;每个监听同时转发 64 个连接,更多的留在内核队列;整个会话同时转发 256 个,超出的关闭并记为失败;每个反向监听最多挂起 16 个,10 秒没被接管的关闭并记为失败。
  • 监听属于打开它的连接,持久会话里也一样,连接结束就关闭;断线重连后客户端不会自己重新打开,主机端口只在用户的规则存在时开着。

客户端:主机侧与反向转发(新文件 crates/remote/src/client/host_forward.rs,改 forward_set.rs、forward.rs、api.rs)

  • HostForward 对应主机侧规则;ReverseForward 收到 listener.accepted 后在本机只连 127.0.0.1、再试 ::1,主机上的用户无法借它访问本机所在的局域网。本机连接失败计入 failedConnections。
  • ForwardSet 统一管理三种转发:64 个的上限三种合计;先占位、不持锁启动、提交时去重,重复点击不会开第二个端口:本地按目标,主机侧按主机端口(未指定时任意端口)+ 网卡 + 目标,反向按主机端口 + 本机端口。
  • 目标不是主机回环地址时,主机侧规则和本地转发一样需要用户确认(allowNonLoopbackTarget),否则返回 forward_target_not_confirmed。
  • 转发信息新增 kind(local / host / reverse)、hostListenHost、hostListenPort、requestedHostPort、allInterfaces、failedConnections;不适用于该种类的字段不出现。
  • 日志:新增 remote.forward.host_started、remote.forward.host_removed 两个事件(logging/events/remote.json,已重新生成),只记录种类、端口、是否全部网卡和计数,不记录地址、网址或进程名。

设置(client/preferences.rs、workspace/preferences.rs)

  • 新增 allowReverseForwarding,默认 false(自动 = 不允许),因为反向转发打开的本机端口主机上的所有用户都能访问。
  • remote.preferences 新增 reverseForwarding { source, allowed };remote.editPreferences 新增 reverseForwarding: { allowed: boolean|null }。一次只能改一个值,一个都不给或给多个都按 { field: "operation" } 拒绝。

命令(workspace/forwards.rs,接线在 command.rs、dispatcher/remote.rs、request_trace.rs)

  • remote.startHostForward { sessionId, targetHost, targetPort, listenPort?, allInterfaces?, allowNonLoopbackTarget? } → { forward, created }。默认只监听主机回环地址;allInterfaces 由桌面端每次询问用户,Core 不记住。
  • remote.startReverseForward { sessionId, localPort, listenPort?, preferences } → { forward, created }。请求带上当前设置文档(和 remote.connect 带 SSH 程序的方式一样),Core 判断不允许时返回 reverse_forward_disabled,所有客户端执行同一条规则。localPort 为 0 是 invalid_request,details.field 为 localPort。
  • 新错误码(protocol/error.rs,诊断目录已重新生成),一码一义:forward_host_port_in_use、forward_host_port_denied、forward_host_unsupported(远端服务没有 listener 功能)、reverse_forward_disabled。其他远端错误沿用 sessions::request_error 的映射。
  • 两个命令都不属于高频请求,已加入 request_trace 的非高频测试列表。

契约文档

  • contracts/remote-workspace-protocol-v1.md:方法表加四行、事件表加 listener.accepted;新增“Listeners”一节;“服务器从不监听网络端口”改为只为用户添加的规则监听;错误码列表加 ADDRESS_IN_USE;“连接结束即结束”的资源加上监听。
  • contracts/rust-core-api.md:命令表两行;设置一节改为三个值;转发一节重写:三种转发、种类对照表、主机端口规则与错误、反向默认关闭、按规则去重、合计上限、重连后不恢复。
  • contracts/application-boundary.md:错误码列表加四项。

验证

  • cargo fmt --all
  • cargo clippy -p lithe-remote -p lithe-workspace -p lithe-core -p lithe-protocol --all-targets --all-features -- -D warnings:无警告
  • cargo test -p lithe-remote --features server:全部通过(tests/tunnels.rs 17 个,含主机侧转发、反向转发接管、端口占用、超时记失败、会话结束关闭监听)
  • cargo test -p lithe-workspace:59 个通过(新增反向开关往返与 reverse_forwarding_allowed,多字段编辑被拒)
  • cargo test -p lithe-core:889 个通过;新增 remote_host_and_reverse_forwards_follow_their_own_rules,经过分发器验证主机侧转发回显、非回环目标需确认、端口占用、未开启时拒绝反向、开启后反向回显与去重、localPort 为 0、列表种类和停止
  • cargo test -p lithe-diagnostics -p lithe-protocol:通过
  • node scripts/logging/generate-log-events.mjs --check:148 个事件,生成结果一致
  • node shared/plugins/scripts/verify-docs.mjs:通过

…r (Refs 1lck/Lithe-IDEA#2231)

The server can now listen on a host port for a forwarding rule the user
added (listener.open/adopt/close/list, listener.accepted, feature
"listener"). Host rules relay every accepted connection on the host;
reverse rules hand each connection to the client as a tunnel that reaches
this machine's loopback interface.

Core adds remote.startHostForward and remote.startReverseForward. Host
forwards listen on the host's loopback interface unless the request asks
for every interface; reverse forwards bind loopback only and start only
when the new remote preference allowReverseForwarding is on (off by
default). All kinds share the 64-forward limit, report kind and
failedConnections, and are never reopened after a reconnect. New error
codes: forward_host_port_in_use, forward_host_port_denied,
forward_host_unsupported, reverse_forward_disabled.
@xiaoyumuxi
xiaoyumuxi marked this pull request as ready for review November 21, 2026 17:21
@xiaoyumuxi
xiaoyumuxi merged commit 62d44ce into main Nov 21, 2026
6 of 7 checks passed
xiaoyumuxi added a commit to 1lck/Lithe-IDEA that referenced this pull request Nov 22, 2026
- 端口窗口标题栏“+”打开“添加转发规则”:主机端口 → 另一台服务器(T3),
  主机端口 → 本机(T4 反向转发,只在设置允许时提供);默认只监听主机回环地址,
  监听全部网卡每次都询问、不记住,目标不是主机回环地址时先询问(S28)
- 端口窗口新增“转发规则”一节:监听地址(全部网卡用警告色)、目标、连接数和状态,
  可逐条停止,全部停止也停止规则;规则随会话结束,重连后不自动恢复
- 设置 › 工具 › 远程工作区新增“端口转发 › 反向转发”,默认自动(不允许)
- Core 客户端接上 remote.startHostForward / remote.startReverseForward 和四个新错误码
- 固定 Core main 62d44ce(Lithe-IDEA/Lithe-core-rust#159)
- 中英词条、无头截图、功能矩阵和远程开发 Note 同步更新
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant