Feature: Share a Linux host to another master

Add COMMAND_SHARE (SHARE_TYPE_YAMA) with the Windows semantics: the
record goes into share_list and a second connection is opened to the
target master while the main link stays where it is. main()'s connection
state is all global, so each record is served by a forked
"--share <entry>" child that reuses the whole login, heartbeat and
command path; the parent only starts, tracks and reaps them.
COMMAND_SHARE_CANCEL clears the list and stops the children right away
(Linux can; Windows does it on the next start).

Transfer now opens its connection immediately too, matching the Windows
fallthrough that spawns a shared instance, and that temporary connection
is dropped once the main link has actually moved to the target or when
the transfer expires.

common/host_endpoint.h holds the ip/port/shine/transport word list the
two commands share; common/host_share.h holds the share payload,
share_list encoding and the per-master dedup.

Config writes now take a cross-process flock and replace the file by
rename. The parent and every child write the same config.conf from their
own snapshot, and a truncating overwrite could drop the other side's
keys. Children ignore the commands that only mean something on the main
link: share, transfer, upgrade, TLS anchor and group.

Verified in WSL against fake masters: share start, cancel and restore,
re-sharing with a different transport, share plus transfer on one master,
and the transfer child disappearing after the main link moved.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-07 23:21:44 +01:00
1 parent 7240879792
commit 2a97471b51
6 files changed
+743 -85

No files matched your search

+42 -4
View File
@@ -29,7 +29,8 @@ SimpleRemoter 的 Linux 客户端,支持远程桌面、远程终端、文件
| 屏幕预览(缩略图) | `ScreenPreview.cpp` | `client/ScreenPreview.cpp` | ✅ 完整(X11 抓屏 → 缩放 → 原始 BGRA 上报,由服务端统一编码 JPEG;双击预览、列表缩略图、MCP `get_screenshot` 均可用。无 `DISPLAY` 时不声明能力位,服务端不会发请求) |
| V2 文件传输 | `FileTransferV2.h` | `file_upload.h` | ✅ 服务端↔Linux(含 MCP `upload_file`/`download_file`:发送完成包带流式 SHA-256,路径不存在/无权限回 `TOKEN_DOWNLOAD_ERROR`) |
| C2C 文件接收 | `FileTransferV2.h` | `file_upload.h` | ✅ Windows/MCP → Linux(目标目录由服务端 `dst_dir` 指定,落盘后回执) |
| 主机转移 | `common/host_transfer.h` + main.cpp | `KernelManager.cpp` | ✅ 完整(`COMMAND_ASSIGN_MASTER`,重连生效;到期自动回原主控。主机分享未实现) |
| 主机转移 | `common/host_transfer.h` + main.cpp | `KernelManager.cpp` | ✅ 完整(`COMMAND_ASSIGN_MASTER`,下发后立即另起一条连接,主连接重连时生效;到期自动回原主控并收掉那条连接) |
| 主机分享 | `common/host_share.h` + main.cpp | `KernelManager.cpp` / `ClientDll.cpp` | ✅ 完整(每条分享记录 fork 一个 `--share` 子进程,主连接不受影响;取消分享立即断开,重启自动恢复) |
| 配置持久化 | `LinuxConfig` | INI 文件 | ✅ 完整 |
| MCP 远程管理 | 复用 `SystemManager.h` / `PTYHandler.h` / `ScreenHandler.h` | 服务端 MCP | ✅ 档 1:`kill_process`、`exec_command`、`terminal_open/exec/close`、`remote_open/close/mouse/keyboard/clipboard`(服务端放开门禁,客户端协议本已实现) |
@@ -217,17 +218,53 @@ Windows 客户端的同名命令没有摘要(走内存加载器/更新器)
服务端选中一台主机后点「转移主机」,选目标主控和有效天数,客户端会:
1. 把目标写进 `~/.config/ghost/config.conf` 的 `transfer` 键(`ip:port:transport:shine:valid_to`);
2. **不立刻断开**,当前连接照常维持,下一次重连(断线重连、客户端重启)才连到新主控;
3. 过了有效天数自动回到转移前的原主控——原地址只记在内存里(启动参数或编译期默认),
不写回配置,所以重启后仍然有效。
2. **立即另起一条通往新主控的连接**(与分享同一个 fork+exec 机制),新主控马上就能看到
这台主机上线;它与老主控之间的主连接原样保留,不会断开;
3. 下一次重连(断线重连、客户端重启)时主连接才搬到新主控,那条临时连接随之消失;
4. 过了有效天数自动回到转移前的原主控——原地址只记在内存里(启动参数或编译期默认),
不写回配置,所以重启后仍然有效;到期时那条临时连接一并收掉。
第 2 步与 Windows 客户端现有的实际行为一致(`KernelManager.cpp` 的转移分支会贯穿到分享
分支、额外起一个 shared 实例);差别只在到期处理:Windows 那条连接要等进程重启才消失。
注意事项:
- 目标是永久转移(天数填 0)时客户端会拒绝,避免把主机改到一个不可达的地址后无法回退;
- 有效期内重复下发会被忽略(与 Windows 客户端一致);
- 转移期间切换传输方式(明文 / WS / WSS)会跟随服务端下发的 `transport` 段;
- 「取消分享」不会影响转移建立的临时连接;
- 只想把主机要回来,直接把 `transfer` 键清空并重启客户端即可。
## 主机分享
服务端选中一台主机后点「分享主机」,填目标主控地址,客户端会:
1. 把记录追加进 `~/.config/ghost/config.conf` 的 `share_list` 键
(多条用 `|` 分隔,每条 `ip:port[:shine[:transport]]`,与 Windows 的
`settings/share_list` 格式一致);
2. **不动主连接**,另起一个子进程 `<exe> --share <记录>` 连到目标主控。
子进程复用同一份登录/心跳/命令处理代码,有自己的日志文件
(`log_<月份>_<pid>.txt`)和自己的重连循环。
跨平台部分在 `common/host_share.h`(命令解析、记录编解码、按 `ip:port` 去重),
macOS 可直接复用。
注意事项:
- 同一个 `ip:port` 只保留一条记录,换了传输方式会替换旧记录并重启对应的子进程;
完全相同的记录重复下发会被忽略,不会断开已有分享连接;
- 分享实例不再响应「分享主机」「转移主机」「升级程序」——分享链不能一层套一层,
二进制也不能几个进程同时替换;
- 「取消分享」在 Linux 上**立即生效**(Windows 是清空配置、下次启动才生效),
会杀掉所有分享子进程并清空 `share_list`;「转移主机」建立的那条连接不属于分享,
不受影响;
- 主实例退出(含被 `kill -9`)时子进程跟着退出(`PR_SET_PDEATHSIG`);
- 分享链路的传输方式在分享那一刻固定,之后主连接切 TCP/WS/WSS 不会影响它;
- **TLS 信任锚是整机一份**(`TlsEnabledFor` 只看进程,不看目标主控),而
`COMMAND_TLS_ENABLE` / `COMMAND_TLS_DISABLE` 只作用于主连接、分享实例不处理。
所以分享到一台不在当前信任锚覆盖范围内的主控时,子进程会按 fail-closed 直接
握手失败、反复重连;要么先给这台主机装好包含该主控 CA 的信任锚,要么用明文分享。
## 常见问题
### Q: 远程桌面功能不工作
@@ -331,6 +368,7 @@ loginctl show-session $(loginctl | grep $(whoami) | awk '{print $1}') -p Type
| `GeoLocation` | 地理位置缓存 | `北京市` |
| `QualityLevel` | 屏幕质量等级 | `2` (Good / H264 1080P) |
| `transfer` | 主机转移记录(空 = 未转移),见「主机转移」 | `1.2.3.4:443:6:0:1790000000` |
| `share_list` | 主机分享记录(空 = 未分享),竖线分隔,见「主机分享」 | `1.2.3.4:443:0:6` |
### 质量等级说明