Feature: Report activity history on Linux clients

Implement COMMAND_QUERY_ACTIVITY -> TOKEN_REPORT_ACTIVITY with two backends
picked at startup. With a desktop, a sampler thread reads the foreground
window every second and books dwell time like the Windows client: under
five seconds is dropped, five idle minutes close the window, the last 500
records stay newest first, each line being "[YYYY-MM-DD HH:MM:SS] [title]
duration". It uses its own ActivityChecker because an Xlib connection
cannot be shared with the heartbeat thread, and it is stopped before main's
locals go away.

A reachable X server is not enough: the window backend is only chosen when
the root window carries _NET_SUPPORTING_WM_CHECK, so Xvfb or a bare X
without an EWMH window manager falls back to the headless backend instead
of sampling a window id that never appears.

Without a desktop the answer is computed on demand: logins from
"last -n 100 -F" plus bash/zsh history of root and /home/*, tagged [登录] or
[命令] after a [来源] header. Both halves are merged and sorted by
timestamp; three fifths of the budget goes to logins and reboot records are
capped at ten, since a WSL or container writes one reboot per start.
activity_commands=0 drops the command half.

A single detached thread builds and sends it, only while the main link is up.

The MCP gate now admits Linux for get_activity_history and documents both
shapes. test/unit/linux covers the parsers, ordering and UTF-8 truncation;
15 cases pass.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-08 15:22:09 +01:00
1 parent 5f40f3726c
commit d8048979e3
6 files changed
+940 -14

No files matched your search

+33
View File
@@ -23,6 +23,7 @@ SimpleRemoter 的 Linux 客户端,支持远程桌面、远程终端、文件
| 远程终端 | `PTYHandler` (main.cpp) | `ConPTYManager.cpp` | ✅ 完整 |
| 心跳/RTT | main.cpp | `KernelManager.cpp` | ✅ 完整 |
| 用户活动检测 | `ActivityChecker` | `ActivityWindow` | ✅ 完整 |
| 历史活动 | `LinuxActivityHistory.h` | `ActivityHistory.cpp` | ✅ 双后端(有桌面:X11 前台窗口驻留,格式与 Windows 一致;无桌面:`last` 登录记录 + bash/zsh 命令历史,`activity_commands=0` 可关闭命令采集) |
| 系统信息采集 | main.cpp | `LoginServer.cpp` | ✅ 完整 |
| 守护进程 | daemonize() | Windows 服务 | ✅ 完整 |
| 剪贴板同步 | `ClipboardHandler.h` | `ScreenManager.cpp` | ✅ 服务端↔Linux |
@@ -265,6 +266,37 @@ macOS 可直接复用。
所以分享到一台不在当前信任锚覆盖范围内的主控时,子进程会按 fail-closed 直接
握手失败、反复重连;要么先给这台主机装好包含该主控 CA 的信任锚,要么用明文分享。
## 历史活动
服务端「客户管理 → 历史活动」与 MCP `get_activity_history` 取的是同一次上报
(`COMMAND_QUERY_ACTIVITY` → `TOKEN_REPORT_ACTIVITY`)。客户端在**启动时**按有没有
桌面挑一个后端,之后不再切换:
| 后端 | 触发条件 | 内容 |
|------|---------|------|
| 窗口 | `DISPLAY` 可用且能连上 X 服务器 | 后台每秒采样前台窗口,格式与 Windows 客户端一致:`[YYYY-MM-DD HH:MM:SS] [窗口标题] 时长`。驻留不足 5 秒不记录,空闲 5 分钟算打断,内存保留最近 500 条,最新在前;重启客户端后重新累计 |
| 无头 | 没有 `DISPLAY` / 连不上 X(服务器、容器、systemd 服务) | 查询时现算:登录记录来自 `last -n 100 -F`,命令历史来自 root 和 `/home/*` 下的 `.bash_history`、`.zsh_history`,每行带 `[登录]` / `[命令]` 标签,首行 `[来源]` 说明 |
无头后端把额度按 6:4 分给登录记录和命令历史(登录至少 10 行),并且 `reboot`
记录最多保留 10 条——WSL/容器每启动一次就写一条 `reboot`,不设上限会把真正的
登录和命令全挤掉。
无头后端的登录记录和命令历史合并后按时间戳统一排序(新的在前,时间未知的排最后),
所以服务端/MCP 按 offset 分页时「offset=0」真的是最近的那些。
窗口后端有两个已知边界:
- **X 起来了但没有 EWMH 窗口管理器**(Xvfb、裸 X):`_NET_ACTIVE_WINDOW` 永远读不到。
启动时用根窗口上的 `_NET_SUPPORTING_WM_CHECK` 判一下,没有就直接按无头后端走。
- **没装 `libXss`**(很少见):拿不到空闲时间,一直按「在用」算,同一窗口会一路累加
时长,锁屏也不会打断。装了 `libxss1` 就正常。
命令历史的时间戳取决于 shell 配置:bash 需要 `HISTTIMEFORMAT`,zsh 需要
`EXTENDED_HISTORY`,否则只能拿到命令本身,显示为 `[时间未知]`。
**隐私开关**:在 `~/.config/ghost/config.conf` 里写 `activity_commands=0`,无头后端就只上报
登录记录、不读任何 shell 历史。每次查询都重新读配置,改完不用重启。
## 常见问题
### Q: 远程桌面功能不工作
@@ -369,6 +401,7 @@ loginctl show-session $(loginctl | grep $(whoami) | awk '{print $1}') -p Type
| `QualityLevel` | 屏幕质量等级 | `2` (Good / H264 1080P) |
| `transfer` | 主机转移记录(空 = 未转移),见「主机转移」 | `1.2.3.4:443:6:0:1790000000` |
| `share_list` | 主机分享记录(空 = 未分享),竖线分隔,见「主机分享」 | `1.2.3.4:443:0:6` |
| `activity_commands` | 无头后端是否采集 shell 命令历史(1 = 采集,0 = 只报登录记录),见「历史活动」 | `1` |
### 质量等级说明