Repository navigation
feat(remote): 端口页的端口转发:开始/停止/列出转发,监听端口带上所属进程(S28/S29,#2231) - #147
Merged
Merged
Conversation
- ports.listening 在 Linux(/proc)和 macOS(lsof)上带上 pid、process, 同一地址和端口只保留一条 - crates/remote 新增 ForwardSet:同一目标只建一个转发、每个连接最多 64 个、 本机端口被占用时改用空闲端口并标出、非回环目标必须确认(S28)、 会话结束释放全部转发(S29) - RemoteSession::on_closed 在会话关闭时运行一次回调 - Core 新增 remote.startForward、remote.stopForward、remote.forwards, 新增错误码 forward_target_not_confirmed、forward_limit_reached - lithe-workspace 的测试专用项只按 test-support 特性编译, 单独测试该 crate 不再报 dead_code
This was referenced Nov 21, 2026
七个文件冲突,都是两边各自在同一列表末尾追加条目,按并集保留: - protocol::ErrorCode 与 application-boundary.md:保留 forward_target_not_confirmed、forward_limit_reached,再加 main 的 remote_server_invalid;connection_closed 用本分支补全的说明。 - RemoteCommand、命令名映射、分发器与 rust-core-api.md 方法表:remote.startForward、remote.stopForward、remote.forwards 之后接 remote.connections、remote.editConnections。 - workspace lib.rs:connections 与 forwards 两个模块都保留。 - sessions.rs 的 import:同时引入 ArtifactError 与 ForwardSet。 端口转发经已有连接的服务端协议完成,不另起 ssh 进程,与 #148 的环境变量白名单不交叉。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
关联 Issue
1lck/Lithe-IDEA#2231(远程开发完整设计)的 R1 端口页,对应安全要求 S28(转发到主机以外的机器必须确认)和 S29(会话结束释放全部转发)。桌面端端口页会在后续 PR 里调用这里的三个命令。
类型
ports.listening带上监听端口的所属进程改动说明
问题
原来
crates/remote只有单个PortForward:调用方自己管理生命周期,同一目标点两次会开两个本机端口,会话断开后本机端口还开着,目标可以是主机网络里的任何机器且无需确认。Core 也没有对应命令,端口页无从调用。ports.listening只返回地址和端口,用户看不出是哪个程序在监听。ports.listening带上所属进程(crates/remote/src/server)ListeningPort新增可选字段pid、process(protocol/methods.rs)。服务端用户能查看所属进程时才填,旧客户端忽略这两个字段。platform/linux.rs):从/proc/net/tcp{,6}读出监听套接字的 inode(inode 为 0 时视为未知),再扫描/proc/<pid>/fd里socket:[N]形式的链接,找到所属进程,名字取自/proc/<pid>/comm。读不了的进程(别的用户的)直接跳过,不报错。platform/macos.rs,用于本机测试):lsof -Fpcn按字段输出,解析出 pid、命令名、地址和端口。ports.rs:子进程继承同一个监听套接字、或者多个套接字用SO_REUSEPORT共用端口时,同一地址和端口会出现多次。现在每个地址和端口只保留一条:优先已知所属进程的,其次 pid 最小的(通常是打开套接字的父进程)。ForwardSet(新文件crates/remote/src/client/forward_set.rs)一个会话的全部端口转发,由它开始、列出、停止并持有:
localhost。方括号只允许包住 IPv6 地址,[db.internal]这类写法会被拒绝。主机名最长 253 字节;以-开头等格式不对的主机名、端口 0 返回InvalidTarget(Host | Port),请求不会发给主机。ports.listening报告的0.0.0.0、::表示监听所有网卡,转发时换成同一地址族的回环地址(127.0.0.1、::1),结果里的target_host是实际发给主机的地址。allow_non_loopback_target才会开始,否则返回TargetNotConfirmed,什么都不建。ForwardInfo::loopback_target用来在列表里区分这两种转发。127.0.0.1。请求的端口在本机127.0.0.1或::1上已有程序接受连接时(250 ms 探测),改用空闲端口,requested_local_port保留原请求,local_port_changed为true。这样用户看到的是该打开哪个地址,而不是一个错误。created为false,重复点击不会再开本机端口。每个会话最多 64 个转发(MAX_FORWARDS),超出返回LimitReached;已有转发的目标在上限时仍然返回原转发。on_closed关闭全部本机端口;会话已结束时调用返回SessionClosed。is_loopback_host原来在connection_log.rs里,现在两处都要用,移到forward_set.rs共用,删掉旧的副本和它的测试(新位置有覆盖同样情况的测试)。RemoteSession::on_closed(client/session.rs)会话关闭时只运行一次回调。回调在锁内取出、锁外执行,所以回调里可以再调用会话的方法,不会死锁;等待中的请求先失败,回调后运行。已经关闭的会话注册回调时立即运行。
PortForward::connections()(client/forward.rs)返回已接受的本机连接数,端口页用来显示。
Core 命令(
crates/workspace/src/forwards.rs、crates/core)remote.startForward{ sessionId, targetHost, targetPort, localPort?, allowNonLoopbackTarget? }{ forward, created }remote.stopForward{ sessionId, forwardId }{ stopped },未知 id 返回falseremote.forwards{ sessionId }{ forwards },按id排序请求都是
deny_unknown_fields、camelCase。ForwardError到错误码的对应:ForwardErrorInvalidTargetinvalid_request,details是{"field":"targetHost"}或{"field":"targetPort"}TargetNotConfirmedforward_target_not_confirmed(新)LimitReachedforward_limit_reached(新),details是{"maximum":64}SessionClosedconnection_closed,同时忘掉这个连接Listen(本机端口完全打不开)unknown,details是{"io":"<kind>"}crates/workspace/src/sessions.rs:Connection持有Arc<RemoteSession>和ForwardSet,由Connection::new创建;close()先关转发再关会话。新增forget(id),会话已结束的连接被忘掉,下一次请求返回connection_closed,端口页据此重新连接,而不是显示空列表。原来request_error里Disconnected分支自己删连接,现在也改用forget。remote.forwards加入请求追踪的高频方法(request_trace::is_hot):端口页显示时会定时刷新连接数,高频方法不记录请求追踪,避免淹没用户操作。remote.startForward仍然记录。为什么新增两个错误码
错误码必须一码一义。“需要用户确认”和“已达上限”的处理方式不同(前者弹确认后带
allowNonLoopbackTarget: true重试,后者提示先停掉一些转发),也都不是invalid_request(请求本身格式正确),所以各用一个新码。本机端口完全打不开是罕见的系统错误,没有用户能做的特定处理,归入unknown,details.io给出原因。顺手清理
lithe-workspace里测试专用的stat_requests_for_test、ConnectionTransport::InProcess原来按cfg(any(test, feature = "test-support"))编译,而唯一的使用方test_support模块只按test-support特性编译。单独运行cargo test -p lithe-workspace时这两项报 dead_code。现在统一只按特性编译,五处一起改。不在
crates/remote里的改动包结构要求远程代码都在 remote 包;以下几处是命令入口、错误码和文档,只做接线:
crates/protocol/src/error.rs:新增ForwardTargetNotConfirmed、ForwardLimitReached。所有 Core 命令共用同一个错误码枚举,只能加在这里。crates/workspace/src/forwards.rs(新)、sessions.rs、test_support.rs、lib.rs、fs.rs:请求结构、错误码转换、连接持有转发,以及上面的测试特性清理。crates/core/src/protocol/command.rs、runtime/dispatcher/remote.rs、runtime/request_trace.rs:三个命令的名字、路由和高频方法登记。crates/core/src/tests/remote_sessions.rs:命令级端到端测试。rust/contracts/rust-core-api.md:命令表新增三行,新增“Remote port forwards”一节(请求、结果、转发对象字段,目标、确认、本机端口、同一目标只建一个、生命周期)。rust/contracts/remote-workspace-protocol-v1.md:ports.listening一行补上pid、process。rust/contracts/application-boundary.md:错误码列表加上connection_closed、forward_target_not_confirmed、forward_limit_reached。兼容性
pid、process是可选字段,没有时不序列化;旧版本的客户端和服务端互相兼容,主机端启动脚本和协议版本没有变化。sdk-e2e.mjs。remote_server_invalid,命令枚举、命令名映射、分发器和rust-core-api.md方法表在三个转发命令之后接remote.connections、remote.editConnections,workspace 同时保留connections和forwards模块。端口转发经已有连接完成,不另起 ssh 进程,与 feat(remote): ssh 子进程只保留它自己需要的环境变量(S26,#2231) #148 的环境变量白名单不交叉。合并后本地重跑了下面全部检查(cargo test为 125 组 2450 项,全部通过)。测试
crates/remote/src/client/forward_set.rs新增 5 项单元测试:0.0.0.0、::、[::]、::ffff:0.0.0.0)和 IPv4 映射的回环地址换成主机的回环地址;127.0.0.2、LocalHost算回环。db.internal、localhost.example.com)不算回环目标。-或.开头、含空格、冒号或换行、方括号不完整、方括号包住主机名或 IPv4、超过 253 字节。client/session.rs:closed_hooks_run_once_after_waiting_requests_fail,回调运行时等待中的请求都已失败,读写两侧各关闭一次,回调只运行一次。server/platform/linux.rs:only_socket_descriptors_name_an_inode、listening_sockets_of_the_users_own_processes_name_their_owner。server/platform/macos.rs:lsof_field_output_yields_addresses_ports_and_owners、listening_sockets_of_the_users_own_processes_name_their_owner。crates/remote/tests/tunnels.rs:listening_ports_include_a_local_listener增加断言:所属进程是测试进程自己。test-stability: allow注释。crates/core/src/tests/remote_sessions.rs:remote_port_forwards_start_once_per_target_and_end_with_the_connection,走完整的execute_json命令路径:通配地址转发并回显数据、再次开始返回同一个转发、未确认的私网目标返回forward_target_not_confirmed、确认后开始、各类格式错误带details.field、上限和details.maximum、停止两次、未知连接和会话结束后返回connection_closed。本地验证(macOS arm64,Rust 1.100.0):
cargo fmt --all -- --checkcargo clippy --locked --workspace --all-targets --all-features -- -D warningslithe-remote、lithe-workspace、lithe-protocol、lithe-core四个包:--target x86_64-pc-windows-msvc --all-targets(默认特性)和远端服务的--target aarch64-unknown-linux-musl --all-targets --all-features,-D warningscargo test --workspace --features lithe-remote/server:全部通过cargo test --locked -p lithe-workspace(不开特性):22 项通过,没有警告cargo test --locked -p lithe-core --features lithe-remote/server --lib remote:56 项通过node scripts/rust-core/affected-crates.mjs、node scripts/rust-core/verify-platform-tests.mjsscripts/rust-core/verify-rust-core-comments.sh remote workspace protocol corenode scripts/logging/generate-log-events.mjs --checkLinux arm64(本机原生 arm64 容器
rust:1.100-slim,docker run --init):cargo test --locked -p lithe-remote --all-features:全部通过。必须加--init,否则容器里没有进程回收僵尸进程,kill(pid, 0)对僵尸进程也返回成功,persistence.rs的emptied_sessions_are_forgotten_without_further_requests会失败;这是测试环境问题,与本次改动无关。cargo test --locked -p lithe-workspace:22 项通过。remote_port_forwards_start_once_per_target_and_end_with_the_connection:通过。lithe-core里依赖 git 的远程测试会失败(git should be available: NotFound),与本次改动无关;不依赖 git 的远程测试全部通过。