mirror of
https://github.com/kunkundi/crossdesk.git
synced 2026-11-03 20:47:43 +08:00
Compare commits
26
Commits
cli-v0.1.4
..
mcp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fc1529ee2b | ||
|
|
e1ae449d36 | ||
|
|
f57b4cc4e3 | ||
|
|
2fab61871e | ||
|
|
d776889562 | ||
|
|
30d409ff49 | ||
|
|
6de7165fb0 | ||
|
|
28398c0067 | ||
|
|
0188f2e860 | ||
|
|
597c4971cf | ||
|
|
b1a0dcdb92 | ||
|
|
3382792bfd | ||
|
|
bfa743fd1b | ||
|
|
d051dbb278 | ||
|
|
425698e22e | ||
|
|
adcb0f17dd | ||
|
|
49717f0f68 | ||
|
|
bb48551d59 | ||
|
|
ea4db45dbf | ||
|
|
489491008a | ||
|
|
e15e2c33bd | ||
|
|
b999a01e66 | ||
|
|
53a3df6133 | ||
|
|
3597fd4b3e | ||
|
|
c2bfce03f2 | ||
|
|
86d3a8cf72 |
@@ -97,7 +97,7 @@ jobs:
|
||||
uses: actions/cache/restore@v5
|
||||
with:
|
||||
path: ${{ env.XMAKE_PKG_INSTALLDIR }}
|
||||
key: cli-deps-v1-${{ matrix.runner }}-${{ matrix.target }}-image${{ steps.runner-image.outputs.version }}-xmake${{ env.XMAKE_VERSION }}-${{ hashFiles('xmake.lua', 'apps/cli/xmake.lua', 'apps/desktop/xmake/options.lua', 'deps/thirdparty/openssl/**', 'deps/submodules/minirtc/xmake.lua', 'deps/submodules/minirtc/thirdparty/**', 'deps/patches/**', '.github/workflows/build-cli.yml') }}
|
||||
key: cli-deps-v1-${{ matrix.runner }}-${{ matrix.target }}-image${{ steps.runner-image.outputs.version }}-xmake${{ env.XMAKE_VERSION }}-${{ hashFiles('xmake.lua', 'apps/cli/xmake.lua', 'apps/desktop/xmake/options.lua', 'deps/thirdparty/openssl/**', 'deps/submodules/minirtc/xmake.lua', 'deps/submodules/minirtc/thirdparty/**', '.github/workflows/build-cli.yml') }}
|
||||
- name: Configure macOS
|
||||
if: runner.os == 'macOS'
|
||||
run: xmake f -y -p macosx -a ${{ matrix.arch }} -m release --target_minver=13.0 --CROSSDESK_BUILD_DESKTOP=n --CROSSDESK_BUILD_CLI=y --USE_CUDA=n
|
||||
|
||||
@@ -276,7 +276,8 @@ jobs:
|
||||
if [[ "${CROSSDESK_RELEASE_BUILD:-NO}" == "YES" ]]; then
|
||||
VERIFY_FLAGS+=(--require-release)
|
||||
fi
|
||||
python3 apps/ios/scripts/verify_app_bundle.py "${APP_PATH}" "${VERIFY_FLAGS[@]}"
|
||||
python3 apps/ios/scripts/verify_app_bundle.py "${APP_PATH}" \
|
||||
--dsym-dir "${DERIVED_DATA}/Build/Products/Release-iphoneos" "${VERIFY_FLAGS[@]}"
|
||||
xcrun lipo -info "${EXECUTABLE_PATH}"
|
||||
echo "Minimum iOS version: ${MINIMUM_OS}"
|
||||
|
||||
@@ -293,10 +294,10 @@ jobs:
|
||||
cp apps/ios/licenses/OPEN_SOURCE_NOTICE.txt "${PACKAGE_DIR}/OPEN_SOURCE_NOTICE.txt"
|
||||
cp "${APP_PATH}/SourceMetadata.json" "${PACKAGE_DIR}/SourceMetadata.json"
|
||||
|
||||
DSYM_PATH="${DERIVED_DATA}/Build/Products/Release-iphoneos/CrossDeskMobile.app.dSYM"
|
||||
if [[ -d "${DSYM_PATH}" ]]; then
|
||||
for DSYM_NAME in CrossDeskMobile.app.dSYM OpenSSL.framework.dSYM; do
|
||||
DSYM_PATH="${DERIVED_DATA}/Build/Products/Release-iphoneos/${DSYM_NAME}"
|
||||
cp -R "${DSYM_PATH}" "${PACKAGE_DIR}/"
|
||||
fi
|
||||
done
|
||||
|
||||
ditto -c -k --sequesterRsrc --keepParent "${PACKAGE_DIR}" "${PACKAGE_FILE}"
|
||||
echo "PACKAGE_FILE=${PACKAGE_FILE}" >> "${GITHUB_ENV}"
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
name: MCP package
|
||||
|
||||
on:
|
||||
push:
|
||||
paths: ['apps/mcp/**', 'apps/cli/package.json', 'apps/cli/npm/platforms.json', '.github/workflows/build-mcp.yml']
|
||||
pull_request:
|
||||
paths: ['apps/mcp/**', 'apps/cli/package.json', 'apps/cli/npm/platforms.json', '.github/workflows/build-mcp.yml']
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
package:
|
||||
runs-on: ubuntu-22.04
|
||||
timeout-minutes: 15
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
- uses: actions/setup-node@v6
|
||||
with:
|
||||
node-version: 22
|
||||
cache: npm
|
||||
cache-dependency-path: apps/mcp/package-lock.json
|
||||
- run: npm ci
|
||||
working-directory: apps/mcp
|
||||
- run: npm run build
|
||||
working-directory: apps/mcp
|
||||
- run: npm run pack:npm
|
||||
working-directory: apps/mcp
|
||||
- uses: actions/upload-artifact@v6
|
||||
with:
|
||||
name: mcp-package
|
||||
path: build/npm-mcp/*.tgz
|
||||
if-no-files-found: error
|
||||
@@ -22,9 +22,9 @@
|
||||
|
||||
</div>
|
||||
|
||||
CrossDesk 让你从电脑、浏览器、手机或终端连接远端电脑,也能通过 **桌面墙(DeskHub)** 集中查看与操控多台设备。项目基于 [MiniRTC](https://github.com/kunkundi/minirtc),支持自托管。
|
||||
CrossDesk 让你从电脑、浏览器、手机或终端(桌面 CLI)连接远端电脑,也能通过桌面墙(DeskHub)集中查看与操控多台设备。项目基于 [MiniRTC](https://github.com/kunkundi/minirtc),支持自托管。
|
||||
|
||||
[主要功能](#features) · [下载与平台支持](#download) · [快速连接](#quick-start) · [终端 CLI](#cli) · [IP 直连](#lan-direct) · [界面预览](#preview) · [使用指南](docs/USER_GUIDE.md) · [自托管](#self-hosting) · [构建与反馈](#development) · [赞助](#sponsor)
|
||||
[主要功能](#features) · [下载与平台支持](#download) · [快速连接](#quick-start) · [终端 CLI](#cli) · [IP 直连](#lan-direct) · [可信设备](#trusted-devices) · [界面预览](#preview) · [使用指南](docs/USER_GUIDE.md) · [自托管](#self-hosting) · [构建与反馈](#development) · [赞助](#sponsor)
|
||||
|
||||
<a id="features"></a>
|
||||
|
||||
@@ -32,10 +32,11 @@ CrossDesk 让你从电脑、浏览器、手机或终端连接远端电脑,也
|
||||
|
||||
- **跨平台远控:** Windows、macOS、Linux 桌面互控,浏览器及原生 iOS / Android 客户端访问电脑;鸿蒙 PC 与移动端处于开发阶段。
|
||||
- **局域网 IP 直连:** 桌面客户端通过 IP、端口和六位密码连接,无需云端信令服务;支持独立端口、记住密码及按模式显示近期设备。
|
||||
- **终端主控:** 在终端中通过设备 ID 观看和操控远程桌面,支持 ANSI / Kitty 画面、局部缩放与平移、持续本地命令模式及 Pi 扩展,可单独构建。
|
||||
- **桌面墙:** 多设备实时预览、分组、放大操控、群控和批量文件分发。
|
||||
- **终端主控:** 在终端中通过设备 ID 观看和操控远程桌面,支持 ANSI / Kitty / Sixel 画面、局部缩放与平移、持续本地命令模式及 Pi 扩展,可单独构建。
|
||||
- 桌面墙:多设备实时预览、分组、放大操控、群控和批量文件分发。
|
||||
- **画面与声音:** H.264 / AV1、硬件编解码、远端音频,以及会话内画质和帧率调整;可用能力随平台与设备而异。
|
||||
- **日常协作:** 多显示器切换、键鼠与组合键、文本剪贴板、文件传输。
|
||||
- **可信设备:** 桌面端可选开启可信设备限制,在连接密码之外校验设备身份;支持复制、粘贴配对信息,提前添加允许访问本机的设备。
|
||||
- **部署与运维:** P2P 直连、TURN 中继、SRTP 媒体加密、自托管,以及 Windows 登录前远控和 Linux 无头运行。
|
||||
|
||||
<a id="download"></a>
|
||||
@@ -72,7 +73,7 @@ CrossDesk 让你从电脑、浏览器、手机或终端连接远端电脑,也
|
||||
2. **发起连接。** 在另一台电脑的 CrossDesk 中输入被控电脑的 9 位 ID,点击“连接”;也可使用原生移动端或 [Web 客户端](https://web.crossdesk.cn/)。
|
||||
3. **验证密码。** 输入被控电脑当前的连接密码,等待远程画面出现。桌面端可勾选“记住密码”,后续从“近期连接”重连。
|
||||
|
||||
使用自建服务器时,两端需要接入同一服务。更多连接、密码及浏览器操作见 [使用指南](docs/USER_GUIDE.md#quick-start);连接失败、黑屏或无法操作键鼠时先查阅 [常见问题](docs/FAQ.md)。
|
||||
使用自建服务器时,两端需要接入同一服务。新版桌面端使用设备密钥登录服务器,请先升级服务端:新 ID 注册时绑定公钥,旧 ID 首次升级时验证原密码后绑定;绑定后,针对该 ID 的错误密码尝试不会阻塞正常签名登录。远程控制仍需输入连接密码;新版控制端也验证设备签名,密码失败按控制端及“控制端 → 目标”计数,不再累计被控端的总失败次数,避免他人的错误尝试锁住目标。身份备份与兼容要求见 [设备登录说明](docs/connection-architecture.md#41-id-模式)。更多连接、密码及浏览器操作见 [使用指南](docs/USER_GUIDE.md#quick-start);连接失败、黑屏或无法操作键鼠时先查阅 [常见问题](docs/FAQ.md)。
|
||||
|
||||
<a id="lan-direct"></a>
|
||||
|
||||
@@ -84,6 +85,24 @@ CrossDesk 让你从电脑、浏览器、手机或终端连接远端电脑,也
|
||||
|
||||
两台电脑需要在局域网内可互相访问;网络可以有外网,也可以没有外网,直连会话不依赖云端信令服务。主窗口可直接切换本机 IP、修改端口和密码,默认开启所有 IP 接口监听,支持 IPv4 和 IPv6,支持通过 VPN 等接口连接,可在设置的“网络连接”中调整;关闭设置中的“启用 IP 直连”会停止监听、断开 IP 直连会话并隐藏模式按钮。详见 [IP 直连使用指南](docs/USER_GUIDE.md#lan-direct)。
|
||||
|
||||
<a id="trusted-devices"></a>
|
||||
|
||||
### 仅允许可信设备连接(可选)
|
||||
|
||||
桌面端的可信设备限制**默认关闭**,适用于 ID 连接和 IP 直连。开启后,控制端仍需通过原有密码验证,并持有可信名单中设备的配对私钥;仅知道 ID 和密码不能通过这项限制。
|
||||
|
||||
1. **在控制端复制配对信息。** 打开 **设置 → 常规 → 隐私保护 → 管理可信设备**,在 **本机配对信息** 中点击 **复制配对信息**。
|
||||
2. **在被控端添加控制端。** 打开同一管理界面,将收到的信息粘贴到 **添加可信设备** 输入框,点击 **添加** 即可加入,无需再次确认,也无需先建立远程连接。
|
||||
3. **在被控端开启限制。** 返回设置,勾选 **仅允许可信设备控制** 并保存。之后照常通过 ID 或 IP 发起连接、验证密码。
|
||||
|
||||
配对信息只包含公钥、设备名称和 ID,不含私钥或密码,应通过可信渠道传递。添加后可点击 **查看指纹** 与控制端核对;也允许添加本机,同一公钥不会重复加入。信任是单向的,如需互相控制,应在双方分别添加对方。
|
||||
|
||||
添加和移除会立即保存;开启限制时,**移除信任** 会断开对应设备的会话,名单为空则拒绝所有控制端。也可以从管理界面的已连接设备中添加完成身份验证的控制端。
|
||||
|
||||
**兼容范围:** 两端均需使用支持可信设备认证的客户端。不支持该协议的旧版客户端无法连接开启限制的被控端;当前 CLI、iOS、Android 和鸿蒙移动端尚未实现该认证。关闭限制时沿用原有连接方式。
|
||||
|
||||
设备登录与配对共用缓存目录 `identity.json` 中的私钥,可信名单保存在配置目录的 `trusted-devices/devices.json`。迁移本机身份时应同时备份 ID/密码缓存与 `identity.json`;已绑定的 ID 无法仅凭密码恢复登录。清空整个缓存目录后需重新登记 ID,并让其他设备重新信任本机。备份、迁移及认证机制见 [可信设备技术说明](docs/connection-architecture.md#43-可选可信设备认证)。
|
||||
|
||||
<a id="preview"></a>
|
||||
|
||||
## 界面预览
|
||||
@@ -121,7 +140,7 @@ CrossDesk 让你从电脑、浏览器、手机或终端连接远端电脑,也
|
||||
|
||||
## 会话操作与设置
|
||||
|
||||
远程会话支持显示器切换、画质与帧率调整、声音、文本剪贴板同步和文件传输。工具栏还可查看网络状态、控制隐私屏;桌面设置中可配置 IP 直连开关、编码、中继、自托管与桌面墙画面预算。
|
||||
远程会话支持显示器切换、画质与帧率调整、声音、文本剪贴板同步和文件传输。工具栏还可查看网络状态、控制隐私屏;桌面设置中可配置 IP 直连开关、可信设备限制、编码、中继、自托管与桌面墙画面预算。
|
||||
|
||||
- [会话操作](docs/USER_GUIDE.md#session):工具栏、文本剪贴板、文件保存和隐私屏。
|
||||
- [客户端设置与更新](docs/USER_GUIDE.md#settings):常规设置、桌面墙传输及版本检查。
|
||||
@@ -131,14 +150,14 @@ CrossDesk 让你从电脑、浏览器、手机或终端连接远端电脑,也
|
||||
|
||||
## 终端客户端 CLI
|
||||
|
||||
`crossdesk-cli` 在终端中通过 9 位设备 ID 连接被控电脑,默认只读、静音。普通终端使用 ANSI 字符画面,支持 Kitty 图形协议的终端可显示像素画面;可切换显示器、调整画质、放大细节和平移,也可明确开启键鼠控制。CLI 可以独立于桌面 GUI 构建,被控电脑仍需运行 CrossDesk 并提供可捕获的图形会话,信令服务需支持 `cli` 临时身份注册。
|
||||
`crossdesk-cli` 在终端中通过 9 位设备 ID 连接被控电脑,默认只读、静音。普通终端使用 ANSI 字符画面,支持 Kitty 或 Sixel 图形协议的终端可显示像素画面;可切换显示器、调整画质、放大细节和平移,也可明确开启键鼠控制。CLI 可以独立于桌面 GUI 构建,被控电脑仍需运行 CrossDesk 并提供可捕获的图形会话,信令服务需支持 `cli` 临时身份注册。
|
||||
|
||||
### 画面预览
|
||||
|
||||
| ANSI 字符画面 | Kitty 协议像素画面 |
|
||||
| --- | --- |
|
||||
| <img src="docs/images/cli-ansi-overview.png" width="480" alt="CrossDesk CLI 的 ANSI 四象限全景:顶部显示只读模式和连接状态,底部显示操作快捷键" /> | <img src="docs/images/cli-kitty-overview.png" width="480" alt="CrossDesk CLI 的 Kitty 协议全景:同一远程桌面以像素画面显示,保留状态栏和操作快捷键" /> |
|
||||
| 用彩色字符显示桌面轮廓,适合普通终端;查看局部可按 `d` 放大。 | 直接显示像素画面,适合支持 Kitty 图形协议的终端,如 Ghostty。 |
|
||||
| 用彩色字符显示桌面轮廓,适合普通终端;查看局部可用 `+/-` 或滚轮以鼠标位置缩放。 | 直接显示像素画面,适合支持 Kitty 图形协议的终端,如 Ghostty。 |
|
||||
|
||||
两图采集于 **2026-10-31**,来自同一授权设备的实际只读连接,以 128 × 40 字符视口还原 CLI 的 ANSI / Kitty 协议输出。图中 FPS 是采集时的实际输出计数,静止画面下 ANSI 无变化可显示 `FPS 0`。图示为输出渲染,终端实窗效果受字体、窗口大小及协议支持影响;采集方法见 [图示来源](docs/images/README.md#cli-captures)。
|
||||
|
||||
@@ -183,7 +202,7 @@ xmake run crossdesk-cli 123456789
|
||||
|
||||
### 选择画面与连接参数
|
||||
|
||||
默认 `--renderer auto` 自动探测 Kitty 图形协议,未获支持时回退为 ANSI。macOS「终端」等普通终端可显式选择 `ansi`;Ghostty 等支持该协议的终端可选择 `kitty`。显式指定 `kitty` 但探测失败时会报错,此时改用 `ansi`。
|
||||
默认 `--renderer auto` 按 Kitty → Sixel → ANSI 的顺序选择。macOS「终端」等普通终端可显式选择 `ansi`;支持对应图形协议的终端可选择 `kitty` 或 `sixel`。显式像素模式探测失败时会报错,此时改用 `ansi`。Xshell 是否能显示像素图片取决于终端自身的协议支持,CLI 无法单方面补齐。
|
||||
|
||||
以下示例使用 npm 全局安装后的命令;从源码构建时,可在仓库根目录将 `crossdesk-cli` 替换为 `xmake run crossdesk-cli`。
|
||||
|
||||
@@ -200,7 +219,7 @@ crossdesk-cli 123456789 --quality balanced --fps 8
|
||||
|
||||
| 参数 | 用途 |
|
||||
| --- | --- |
|
||||
| `--renderer auto\|ansi\|kitty` | 自动选择、字符画面或像素画面;默认 `auto` |
|
||||
| `--renderer auto\|ansi\|kitty\|sixel` | 自动选择、字符画面或像素画面;默认 `auto` |
|
||||
| `--zoom native` | 从原始细节模式开始;`--zoom 2` 表示相对全景放大 2 倍 |
|
||||
| `--quality low\|balanced\|high\|ultra` | 请求最高 360p / 720p / 1080p / 2160p;默认 `high`,实际画质由被控端决定 |
|
||||
| `--fps 8` | 固定本地输出目标和相应远端预览请求;允许 1–30,默认本地目标为 30,未指定时远端供帧会自适应 |
|
||||
@@ -208,7 +227,7 @@ crossdesk-cli 123456789 --quality balanced --fps 8
|
||||
| `--control` | 连接后直接启用键鼠控制;省略时默认只读 |
|
||||
| `--server your-server.example --port 9099` | 使用自建信令服务,两端需接入同一服务 |
|
||||
|
||||
ANSI 全景受字符网格限制,提高画质不能让整屏小字变得清晰;可扩大终端、缩小字体,再按 `d` 查看局部,按 `f` 回到全景。顶部状态栏显示模式、连接进度、渲染器、缩放、实时输出 FPS 和网络信息,窄窗口会省略部分字段。通过 SSH 使用时分配 PTY(`ssh -t`);tmux / screen 使用 ANSI。更多显示、鼠标报告和兼容性说明见 [终端使用指南](apps/cli/docs/terminal-client.md)。
|
||||
ANSI 全景受字符网格限制,提高画质不能让整屏小字变得清晰;可扩大终端、缩小字体,再用 `+/-` 或滚轮查看局部,按 `f` 回到全景并重置平移。顶部状态栏显示模式、连接进度、渲染器、缩放、实时输出 FPS 和网络信息,窄窗口会省略部分字段。通过 SSH 使用时分配 PTY(`ssh -t`);tmux / screen 使用 ANSI。更多显示、鼠标报告和兼容性说明见 [终端使用指南](apps/cli/docs/terminal-client.md)。
|
||||
|
||||
### 控制、查看与退出
|
||||
|
||||
@@ -218,10 +237,13 @@ ANSI 全景受字符网格限制,提高画质不能让整屏小字变得清晰
|
||||
| 控制模式 | 键鼠输入传给远端;`Ctrl+]` 进入本地命令模式并暂停远端输入 |
|
||||
| 本地命令模式 | 执行命令后持续保持;`c` 选择手动返回后是否启用控制,`q` 退出 CLI |
|
||||
| `Esc` / `Ctrl+C` | 在本地命令模式中手动返回所选的控制或只读模式 |
|
||||
| `+` / `=` / `-` | 在只读或本地命令模式中放大 / 缩小 |
|
||||
| `d` / `f` | 在只读或本地命令模式中切换原始细节 / 完整桌面;只读时也可点击目标进入细节 |
|
||||
| `+` / `=` / `-` | 在只读或本地命令模式中以鼠标位置放大 / 缩小 |
|
||||
| 滚轮 / 点击 | 只读模式中,滚轮以鼠标位置缩放;点击目标进入原始细节 |
|
||||
| `f` | 在只读或本地命令模式中恢复完整桌面,同时重置倍率、平移和细节模式 |
|
||||
| 方向键 / `i j k l` | 在只读或本地命令模式中平移;`i` 上、`j` 左、`k` 下、`l` 右 |
|
||||
|
||||
鼠标下的画面内容在缩放时尽量保持原位,到达桌面边缘时限制裁剪范围;没有有效鼠标位置时沿用当前中心。本地命令模式中鼠标仅更新缩放锚点,点击和滚轮不执行视图操作。
|
||||
|
||||
**控制模式下退出:先按 `Ctrl+]`,再按 `q`。** 普通 `c`、`q` 和 `Ctrl+C` 在控制模式中会发送到远端。
|
||||
|
||||
`Ctrl+]` 模式不会因为缩放、平移或按 `c` 自动结束。状态栏中的 `CONTROL PAUSED` / `READ ONLY` 表示返回后是否启用控制;按 `Esc` / `Ctrl+C` 手动返回。再次按 `Ctrl+]` 也可返回,若返回控制模式则向远端发送一次该组合键。
|
||||
@@ -237,7 +259,7 @@ pi -e ./apps/cli/plugins/pi/index.ts
|
||||
|
||||
其他平台将 `CROSSDESK_CLI` 改为原生可执行文件的绝对路径。在 Pi 中输入 `/crossdesk` 或 `/crossdesk 123456789`,按提示输入密码。安装要求与验证范围见 [Pi 扩展说明](apps/cli/docs/terminal-client.md#pi-扩展)。
|
||||
|
||||
CLI 当前仅提供 ID 连接与远程观看 / 输入,不提供 IP 直连、音频播放、文件传输或剪贴板同步。macOS arm64 已完成本地构建和连接验证;Linux / Windows 完整运行、SSH / Kitty 实机显示及真实 Pi TUI 的验收范围见 [验证记录](apps/cli/docs/validation.md)。平台构建与 npm 发布不代表全部平台已完成实机验收。
|
||||
CLI 当前仅提供 ID 连接与远程观看 / 输入,不提供 IP 直连、音频播放、文件传输或剪贴板同步,也暂不支持连接开启[可信设备限制](#trusted-devices)的被控端。macOS arm64 已完成本地构建和连接验证;Linux / Windows 完整运行、SSH / Kitty / Sixel 实机显示及真实 Pi TUI 的验收范围见 [验证记录](apps/cli/docs/validation.md)。平台构建与 npm 发布不代表全部平台已完成实机验收。
|
||||
|
||||
[CLI 文档入口](apps/cli/README.md) · [终端使用与故障排查](apps/cli/docs/terminal-client.md) · [桥接协议](apps/cli/docs/bridge-protocol.md) · [npm 打包与分发](apps/cli/docs/npm-distribution.md)
|
||||
|
||||
|
||||
+36
-14
@@ -22,9 +22,9 @@
|
||||
|
||||
</div>
|
||||
|
||||
CrossDesk connects your computers from desktop, browser, mobile, and terminal clients, with **DeskHub** for viewing and controlling multiple devices together. It is built on [MiniRTC](https://github.com/kunkundi/minirtc) and supports self-hosting.
|
||||
CrossDesk connects your computers from desktop, browser, mobile, and terminal clients, with DeskHub for viewing and controlling multiple devices together. It is built on [MiniRTC](https://github.com/kunkundi/minirtc) and supports self-hosting.
|
||||
|
||||
[Features](#features) · [Download and platforms](#download) · [Quick start](#quick-start) · [Terminal CLI](#cli) · [Direct IP](#lan-direct) · [Preview](#preview) · [User guide](docs/USER_GUIDE_EN.md) · [Self-hosting](#self-hosting) · [Build and feedback](#development) · [Sponsor](#sponsor)
|
||||
[Features](#features) · [Download and platforms](#download) · [Quick start](#quick-start) · [Terminal CLI](#cli) · [Direct IP](#lan-direct) · [Trusted devices](#trusted-devices) · [Preview](#preview) · [User guide](docs/USER_GUIDE_EN.md) · [Self-hosting](#self-hosting) · [Build and feedback](#development) · [Sponsor](#sponsor)
|
||||
|
||||
<a id="features"></a>
|
||||
|
||||
@@ -32,10 +32,11 @@ CrossDesk connects your computers from desktop, browser, mobile, and terminal cl
|
||||
|
||||
- **Cross-platform access:** control Windows, macOS, and Linux desktops from computers, browsers, or native iOS / Android clients. HarmonyOS PC and mobile clients are in development.
|
||||
- **Direct IP over LAN:** connect desktop clients using an IP address, port, and six-character password without a cloud signaling service. Each local IP has its own port; passwords can be remembered and recent devices are separated by mode.
|
||||
- **Terminal controller:** view and control remote desktops by device ID, with ANSI / Kitty rendering, zoom and pan, persistent local command mode, a Pi extension, and a build independent of the desktop GUI.
|
||||
- **DeskHub:** live multi-device previews, groups, enlarged control, group input, and batch file distribution.
|
||||
- **Terminal controller:** view and control remote desktops by device ID, with ANSI / Kitty / Sixel rendering, zoom and pan, persistent local command mode, a Pi extension, and a build independent of the desktop GUI.
|
||||
- DeskHub: live multi-device previews, groups, enlarged control, group input, and batch file distribution.
|
||||
- **Video and audio:** H.264 / AV1, hardware codecs, remote audio, and in-session quality and frame-rate controls. Capabilities depend on the platform and device.
|
||||
- **Everyday controls:** monitor switching, keyboard and mouse input, shortcuts, text clipboard, and file transfer.
|
||||
- **Trusted devices:** optionally restrict desktop access to trusted device identities in addition to the connection password. Copy and paste pairing information to approve devices before they connect.
|
||||
- **Deployment:** direct P2P connections, TURN relay, SRTP media encryption, self-hosting, Windows access before sign-in, and Linux headless operation.
|
||||
|
||||
<a id="download"></a>
|
||||
@@ -72,7 +73,7 @@ Requirements follow the current source and build configuration. Check the releas
|
||||
2. **Connect.** On another computer, enter the host's 9-digit ID and click **Connect**. Native mobile clients and the [Web Client](https://web.crossdesk.cn/) can also connect.
|
||||
3. **Verify the password.** Enter the host's current connection password and wait for the desktop to appear. On desktop, select **Remember password** to reconnect later from **Recent Connections**.
|
||||
|
||||
For self-hosting, both ends must use the same service. See the [user guide](docs/USER_GUIDE_EN.md#quick-start) for connection, password, and browser controls, or the [FAQ](docs/FAQ.md#english) for failed connections, blank video, and input problems.
|
||||
For self-hosting, both ends must use the same service. Upgrade the server before using the new desktop client: device login uses a private key, with its public key bound at registration or after a one-time password check for an existing ID. Once bound, wrong-password attempts against that ID cannot block valid signature login. Remote control still requires the connection password. Signed controllers have separate per-controller and controller-to-destination failure budgets; failures from one controller no longer lock the destination for others. See the [device login guide](docs/connection-architecture.md#41-id-模式) (Chinese) for identity backups and compatibility, the [user guide](docs/USER_GUIDE_EN.md#quick-start) for controls, or the [FAQ](docs/FAQ.md#english) for connection problems.
|
||||
|
||||
<a id="lan-direct"></a>
|
||||
|
||||
@@ -84,6 +85,24 @@ For self-hosting, both ends must use the same service. See the [user guide](docs
|
||||
|
||||
Both computers must be reachable over the LAN. Internet access may be available or absent; direct sessions do not depend on a cloud signaling service. The main window lets you select the local IP, edit its port, and change the password. **Listen on all IP interfaces** is enabled by default for IPv4, IPv6, VPN, and other local interfaces on a shared port; change it under **Settings → General → Network**. Disabling **Enable direct IP** stops listeners, disconnects direct IP sessions, and hides the mode button. See the [direct IP guide](docs/USER_GUIDE_EN.md#lan-direct).
|
||||
|
||||
<a id="trusted-devices"></a>
|
||||
|
||||
### Allow only trusted devices (optional)
|
||||
|
||||
The desktop trusted-device restriction is **off by default** and applies to both ID and direct IP connections. When enabled, the controller must pass the existing password verification and prove possession of a pairing private key recognized by the host. Knowing the ID and password alone does not satisfy this restriction.
|
||||
|
||||
1. **Copy pairing information on the controller.** Open **Settings → General → Privacy → Manage trusted devices**. Under **This device’s pairing info**, click **Copy pairing info**.
|
||||
2. **Add the controller on the host.** Open the same management dialog, paste the information into **Add a trusted device**, and click **Add**. It is saved immediately, with no second confirmation or prior remote connection required.
|
||||
3. **Enable the restriction on the host.** Return to settings, select **Only allow trusted devices**, and save. Connect by ID or IP and verify the password as usual.
|
||||
|
||||
Pairing information contains only the public key, device name, and ID, with no private key or password. Exchange it through a trusted channel. After adding a device, use **View fingerprint** to compare it with the controller. You can also add the local device; the same public key is not added twice. Trust is one-way: to control each other, add the other device on both computers.
|
||||
|
||||
Adding or removing a device takes effect immediately. With the restriction enabled, **Remove trust** disconnects that device’s sessions, and an empty list rejects all controllers. The dialog also lets you add connected controllers that have completed identity verification.
|
||||
|
||||
**Compatibility:** both clients must support trusted-device authentication. Older clients without the protocol cannot connect to a restricted host. The CLI, iOS, Android, and HarmonyOS mobile clients do not currently implement it. With the restriction disabled, the existing connection behavior applies.
|
||||
|
||||
Device login and pairing share the private key in the cache directory's `identity.json`; the trusted list is stored in `trusted-devices/devices.json` in the configuration directory. Back up the ID/password cache and `identity.json` together when migrating a device: a bound ID cannot be recovered with its password alone. Clearing the entire cache requires a new ID and renewed trust from other devices. See the [trusted-device technical guide](docs/connection-architecture.md#43-可选可信设备认证) (Chinese) for details.
|
||||
|
||||
<a id="preview"></a>
|
||||
|
||||
## Preview
|
||||
@@ -121,7 +140,7 @@ Group control sends input to all connected devices; closing DeskHub disconnects
|
||||
|
||||
## Session controls and settings
|
||||
|
||||
Remote sessions support monitor switching, video quality and frame-rate controls, audio, text clipboard synchronization, and file transfer. The toolbar also provides network statistics and privacy screen controls. Desktop settings configure direct IP availability, codecs, relay, self-hosting, and the DeskHub video budget.
|
||||
Remote sessions support monitor switching, video quality and frame-rate controls, audio, text clipboard synchronization, and file transfer. The toolbar also provides network statistics and privacy screen controls. Desktop settings configure direct IP availability, trusted-device restrictions, codecs, relay, self-hosting, and the DeskHub video budget.
|
||||
|
||||
- [Session controls](docs/USER_GUIDE_EN.md#session): toolbar, text clipboard, received files, and privacy screens.
|
||||
- [Client settings and updates](docs/USER_GUIDE_EN.md#settings): general options, DeskHub video settings, and version checks.
|
||||
@@ -131,14 +150,14 @@ Remote sessions support monitor switching, video quality and frame-rate controls
|
||||
|
||||
## Terminal client CLI
|
||||
|
||||
`crossdesk-cli` connects to a host by its 9-digit device ID and starts in read-only, silent mode. Standard terminals use ANSI character rendering; terminals supporting the Kitty graphics protocol can display pixel graphics. The CLI supports monitor selection, quality settings, detail zoom, panning, and optional keyboard/mouse control. It builds independently of the desktop GUI. The host still needs CrossDesk and a capturable graphical session, and the signaling service must support temporary `cli` identities.
|
||||
`crossdesk-cli` connects to a host by its 9-digit device ID and starts in read-only, silent mode. Standard terminals use ANSI character rendering; terminals supporting the Kitty or Sixel graphics protocol can display pixel graphics. The CLI supports monitor selection, quality settings, detail zoom, panning, and optional keyboard/mouse control. It builds independently of the desktop GUI. The host still needs CrossDesk and a capturable graphical session, and the signaling service must support temporary `cli` identities.
|
||||
|
||||
### Display preview
|
||||
|
||||
| ANSI character rendering | Kitty pixel graphics |
|
||||
| --- | --- |
|
||||
| <img src="docs/images/cli-ansi-overview.png" width="480" alt="CrossDesk CLI ANSI quadrant overview, with read-only mode and connection status at the top and shortcuts at the bottom" /> | <img src="docs/images/cli-kitty-overview.png" width="480" alt="CrossDesk CLI Kitty protocol overview of the same remote desktop, rendered as pixels with the status bar and shortcuts" /> |
|
||||
| Colored characters show the desktop in standard terminals; press `d` to inspect a smaller region. | Pixel graphics display the desktop in terminals supporting the Kitty graphics protocol, such as Ghostty. |
|
||||
| Colored characters show the desktop in standard terminals; use `+/-` or the wheel to zoom at the mouse position. | Pixel graphics display the desktop in terminals supporting the Kitty graphics protocol, such as Ghostty. |
|
||||
|
||||
Both images were captured on **2026-10-31** from real read-only connections to the same authorized host. They render the CLI's ANSI / Kitty protocol output in a 128 × 40 character viewport. FPS counts actual output during capture; an unchanged ANSI view can show `FPS 0`. Actual terminal appearance depends on the font, window size, and protocol support. See [capture notes](docs/images/README.md#cli-captures) for the method.
|
||||
|
||||
@@ -183,7 +202,7 @@ For an existing checkout, run the dependency preparation and subsequent commands
|
||||
|
||||
### Rendering and connection options
|
||||
|
||||
The default `--renderer auto` probes for Kitty graphics support and falls back to ANSI. Use `ansi` explicitly in standard terminals such as macOS Terminal, or `kitty` in compatible terminals such as Ghostty. Explicit `kitty` mode reports an error if the probe fails; use `ansi` in that case.
|
||||
The default `--renderer auto` selects Kitty, then Sixel, then ANSI according to terminal capability replies. Use `ansi` in standard terminals such as macOS Terminal, or `kitty` / `sixel` in terminals supporting the corresponding protocol. Explicit pixel modes report an error if the probe fails; use `ansi` in that case. Sixel output alone does not guarantee Xshell compatibility: the terminal itself must support the protocol.
|
||||
|
||||
These examples use the command installed globally by npm. For a source build, replace `crossdesk-cli` with `xmake run crossdesk-cli` and run it from the repository root.
|
||||
|
||||
@@ -200,7 +219,7 @@ crossdesk-cli 123456789 --quality balanced --fps 8
|
||||
|
||||
| Option | Purpose |
|
||||
| --- | --- |
|
||||
| `--renderer auto\|ansi\|kitty` | Automatic selection, character rendering, or pixel graphics; default: `auto` |
|
||||
| `--renderer auto\|ansi\|kitty\|sixel` | Automatic selection, character rendering, or pixel graphics; default: `auto` |
|
||||
| `--zoom native` | Start in native detail mode; `--zoom 2` zooms to twice the full-desktop scale |
|
||||
| `--quality low\|balanced\|high\|ultra` | Request up to 360p / 720p / 1080p / 2160p; default: `high`. The host determines actual quality |
|
||||
| `--fps 8` | Fix the local output target and corresponding remote preview request; accepts 1–30. The default local target is 30, with adaptive remote requests when omitted |
|
||||
@@ -208,7 +227,7 @@ crossdesk-cli 123456789 --quality balanced --fps 8
|
||||
| `--control` | Enable keyboard/mouse control immediately after connecting; omitted by default for read-only viewing |
|
||||
| `--server your-server.example --port 9099` | Use a self-hosted signaling service; both ends must use the same service |
|
||||
|
||||
ANSI overviews are limited by the character grid: higher video quality alone cannot make full-desktop text legible. Enlarge the terminal or reduce its font size, press `d` for detail, and `f` to return to the full desktop. The top bar shows input mode, connection progress, renderer, zoom, live output FPS, and network information; narrow windows omit some fields. Allocate a PTY over SSH (`ssh -t`), and use ANSI inside tmux / screen. See the [terminal guide](apps/cli/docs/terminal-client.md) (Chinese) for rendering, mouse reporting, and compatibility details.
|
||||
ANSI overviews are limited by the character grid: higher video quality alone cannot make full-desktop text legible. Enlarge the terminal or reduce its font size, use `+/-` or the wheel for detail, and `f` to restore the full desktop and reset panning. The top bar shows input mode, connection progress, renderer, zoom, live output FPS, and network information; narrow windows omit some fields. Allocate a PTY over SSH (`ssh -t`), and use ANSI inside tmux / screen. See the [terminal guide](apps/cli/docs/terminal-client.md) (Chinese) for rendering, mouse reporting, and compatibility details.
|
||||
|
||||
### Control, navigation, and exit
|
||||
|
||||
@@ -218,10 +237,13 @@ ANSI overviews are limited by the character grid: higher video quality alone can
|
||||
| Control mode | Keyboard/mouse input goes to the host; `Ctrl+]` enters local command mode and pauses remote input |
|
||||
| Local command mode | Stays active after commands; `c` selects whether control resumes when you leave; `q` exits the CLI |
|
||||
| `Esc` / `Ctrl+C` | Manually leave local command mode and return to the selected control or read-only mode |
|
||||
| `+` / `=` / `-` | Zoom in / out in read-only or local command mode |
|
||||
| `d` / `f` | Switch to native detail / full-desktop view in read-only or local command mode; clicking in read-only mode also opens detail at that point |
|
||||
| `+` / `=` / `-` | Zoom in / out at the mouse position in read-only or local command mode |
|
||||
| Wheel / click | In read-only mode, zoom at the mouse position with the wheel; click a point to open native detail |
|
||||
| `f` | Restore the full desktop and reset zoom, pan, and native detail mode in read-only or local command mode |
|
||||
| Arrow keys / `i j k l` | Pan in read-only or local command mode: `i` up, `j` left, `k` down, `l` right |
|
||||
|
||||
Zoom keeps the content under the pointer in place where possible, clamping the crop at desktop edges. Without a valid mouse position, zoom uses the current center. In local command mode, mouse reports only update the zoom anchor; clicks and the wheel do not change the view.
|
||||
|
||||
**To quit while controlling the host, press `Ctrl+]`, then `q`.** Ordinary `c`, `q`, and `Ctrl+C` go to the host in control mode.
|
||||
|
||||
Zooming, panning, or pressing `c` does not end `Ctrl+]` mode. The status bar shows `CONTROL PAUSED` / `READ ONLY` to indicate whether control will resume; press `Esc` / `Ctrl+C` to return manually. Pressing `Ctrl+]` again also leaves local mode and forwards that shortcut if control resumes.
|
||||
@@ -237,7 +259,7 @@ pi -e ./apps/cli/plugins/pi/index.ts
|
||||
|
||||
On other platforms, set `CROSSDESK_CLI` to the absolute path of the native executable. Enter `/crossdesk` or `/crossdesk 123456789` in Pi and follow the password prompt. See the [Pi extension guide](apps/cli/docs/terminal-client.md#pi-扩展) (Chinese) for requirements and validation scope.
|
||||
|
||||
The CLI currently provides ID-based viewing and input, without direct IP connections, audio playback, file transfer, or clipboard synchronization. Local builds and remote connections have been validated on macOS arm64. See the [validation record](apps/cli/docs/validation.md) (Chinese) for the remaining Linux / Windows, SSH / Kitty terminal, and actual Pi TUI checks. Platform builds and npm publication do not establish that every platform has been validated on real devices.
|
||||
The CLI currently provides ID-based viewing and input, without direct IP connections, audio playback, file transfer, or clipboard synchronization. It also cannot yet connect to hosts with [trusted-device restrictions](#trusted-devices) enabled. Local builds and remote connections have been validated on macOS arm64. See the [validation record](apps/cli/docs/validation.md) (Chinese) for the remaining Linux / Windows, SSH / Kitty / Sixel terminal, and actual Pi TUI checks. Platform builds and npm publication do not establish that every platform has been validated on real devices.
|
||||
|
||||
[CLI documentation](apps/cli/README.md) · [Terminal usage and troubleshooting](apps/cli/docs/terminal-client.md) · [Bridge protocol](apps/cli/docs/bridge-protocol.md) · [npm packaging and distribution](apps/cli/docs/npm-distribution.md) (detailed guides in Chinese)
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ public final class MainActivity extends Activity implements NativeSession.Listen
|
||||
private View settingsUpdateDot, aboutUpdateDot, settingsUpdateButton, aboutUpdateRow;
|
||||
private TextView aboutUpdateLabel;
|
||||
private boolean navigatingBack;
|
||||
private FrameLayout canvas;
|
||||
private FrameLayout canvas, safeArea;
|
||||
private LinearLayout root;
|
||||
private EditText remote;
|
||||
private TextView status, signalBadge, identitySummary;
|
||||
@@ -133,10 +133,11 @@ public final class MainActivity extends Activity implements NativeSession.Listen
|
||||
settingsUpdateDot=null;aboutUpdateDot=null;settingsUpdateButton=null;aboutUpdateRow=null;aboutUpdateLabel=null;
|
||||
previewSwitch=null;clearPreviewsButton=null;previewCleanupError=null;previewCards.clear();recentGrid=null;
|
||||
canvas=new FrameLayout(this);canvas.setBackgroundColor(dark?Color.BLACK:BACKGROUND);
|
||||
FrameLayout safeArea=new FrameLayout(this);canvas.addView(safeArea,new FrameLayout.LayoutParams(-1,-1));
|
||||
safeArea=new FrameLayout(this);canvas.addView(safeArea,new FrameLayout.LayoutParams(-1,-1));
|
||||
root=ui.pageColumn(dark?0:nextPage.equals("home")?1200:720);
|
||||
safeArea.addView(root,new FrameLayout.LayoutParams(-1,-1,Gravity.TOP|Gravity.CENTER_HORIZONTAL));
|
||||
safeArea.setOnApplyWindowInsetsListener((view,insets)->{
|
||||
if(dark&&(pictureInPictureUi||isInPictureInPictureMode())){view.setPadding(0,0,0,0);return insets;}
|
||||
if(dark&&android.os.Build.VERSION.SDK_INT>=30){
|
||||
android.graphics.Insets bars=insets.getInsets(WindowInsets.Type.systemBars()|WindowInsets.Type.displayCutout());
|
||||
view.setPadding(bars.left,bars.top,bars.right,bars.bottom);
|
||||
@@ -328,7 +329,10 @@ public final class MainActivity extends Activity implements NativeSession.Listen
|
||||
dialog.setContentView(card);dialog.setCanceledOnTouchOutside(false);dialog.setOnCancelListener(d->end(""));Window w=dialog.getWindow();if(w!=null){w.setBackgroundDrawableResource(android.R.color.transparent);w.setDimAmount(.08f);}dialog.show();if(w!=null)w.setLayout(dp(292),-2);
|
||||
}
|
||||
private void sessionScreen(boolean autoLandscape){
|
||||
installRoot("session",true);remoteOrientation.begin(autoLandscape,!isTablet()&&!isInMultiWindowMode());video=new RemoteVideoView(this,session);video.setRelative(preferences.getBoolean("relativeMouse",true));canvas.addView(video,0,new FrameLayout.LayoutParams(-1,-1));
|
||||
installRoot("session",true);remoteOrientation.begin(autoLandscape,!isTablet()&&!isInMultiWindowMode());video=new RemoteVideoView(this,session);video.setRelative(preferences.getBoolean("relativeMouse",true));
|
||||
// Video, cursor and touch input must share the area inside system bars
|
||||
// and display cutouts; fitting to the outer canvas hides image edges.
|
||||
safeArea.addView(video,0,new FrameLayout.LayoutParams(-1,-1));
|
||||
controls=new SessionControls(this,ui,new SessionControls.Actions(){
|
||||
public void disconnect(){confirmDisconnect();}public void display(){selectDisplay();}public void clipboard(){MainActivity.this.clipboard();}
|
||||
public void sendFile(){chooseFile();}public void saveFile(){saveReceivedFile();}public void secureAttention(){MainActivity.this.secureAttention();}
|
||||
@@ -803,6 +807,7 @@ public final class MainActivity extends Activity implements NativeSession.Listen
|
||||
if(value){releaseKeys();hideKeyboard();pageHost.finish();if(disconnectDialog!=null)disconnectDialog.dismiss();if(remoteUpdateDialog!=null)remoteUpdateDialog.dismiss();}
|
||||
if(controls!=null)controls.setPictureInPicture(value);
|
||||
if(video!=null)video.setPictureInPicture(value);
|
||||
if(safeArea!=null)safeArea.requestApplyInsets();
|
||||
}
|
||||
@Override protected void onUserLeaveHint(){
|
||||
super.onUserLeaveHint();
|
||||
|
||||
@@ -42,6 +42,9 @@ final class RemoteViewport {
|
||||
void zoomBy(float factor, float focusX, float focusY) {
|
||||
if (baseWidth == 0 || baseHeight == 0 || !Float.isFinite(factor) || factor <= 0) return;
|
||||
float nextScale = clamp(scale * factor, 1, MAXIMUM_SCALE);
|
||||
// Returning to fit must clear cursor-edge panning too, including a
|
||||
// pinch that starts at 1x with an already shifted image.
|
||||
if (nextScale == 1) { reset(); return; }
|
||||
float ratio = nextScale / scale;
|
||||
float focusFromCenterX = focusX - (baseLeft + baseWidth / 2f);
|
||||
float focusFromCenterY = focusY - (baseTop + baseHeight / 2f);
|
||||
@@ -75,10 +78,9 @@ final class RemoteViewport {
|
||||
}
|
||||
|
||||
void finishGesture() {
|
||||
if (!isZoomed()) {
|
||||
scale = 1;
|
||||
constrainOffset();
|
||||
}
|
||||
// Ordinary pointer gestures at 1x retain edge panning. Only a zoom
|
||||
// snapping back to its minimum restores the complete centered image.
|
||||
if (scale > 1 && !isZoomed()) reset();
|
||||
}
|
||||
|
||||
void reset() { scale = 1; offsetX = offsetY = 0; edgePanned = false; }
|
||||
|
||||
+14
-7
@@ -1,6 +1,6 @@
|
||||
# CrossDesk CLI
|
||||
|
||||
CrossDesk 的终端主控客户端,源码位于本仓库的 `apps/cli`。通过真实 MiniRTC ID 会话观看远程桌面;默认只读、静音。包含 ANSI 象限字符画面、局部放大和平移、Kitty 图形后端,以及 Pi 自定义 TUI 扩展。
|
||||
CrossDesk 的终端主控客户端,源码位于本仓库的 `apps/cli`。通过真实 MiniRTC ID 会话观看远程桌面;默认只读、静音。包含 ANSI 象限字符画面、局部放大和平移、Kitty / Sixel 图形后端,以及 Pi 自定义 TUI 扩展。
|
||||
|
||||
## 画面预览
|
||||
|
||||
@@ -8,7 +8,7 @@ CrossDesk 的终端主控客户端,源码位于本仓库的 `apps/cli`。通
|
||||
| --- | --- |
|
||||
| <img src="../../docs/images/cli-ansi-overview.png" width="480" alt="CrossDesk CLI 的 ANSI 全景输出,包含只读状态栏与快捷键提示" /> | <img src="../../docs/images/cli-kitty-overview.png" width="480" alt="CrossDesk CLI 的 Kitty 像素全景输出,显示同一远程桌面" /> |
|
||||
|
||||
图示来自 **2026-10-31** 的实际只读连接,通过伪终端采集并渲染协议输出;来源与边界见 [图示说明](../../docs/images/README.md#cli-captures)。默认 `auto` 探测 Kitty 支持,失败时使用 ANSI;普通终端可指定 `--renderer ansi`,Ghostty 等支持 Kitty 图形协议的终端可指定 `--renderer kitty`。ANSI 全景适合查看桌面轮廓,阅读小字需进入细节模式。
|
||||
图示来自 **2026-10-31** 的实际只读连接,通过伪终端采集并渲染协议输出;来源与边界见 [图示说明](../../docs/images/README.md#cli-captures)。默认 `auto` 依次选择 Kitty、Sixel、ANSI;普通终端可指定 `--renderer ansi`,支持对应协议的终端可指定 `--renderer kitty` 或 `--renderer sixel`。显式像素模式需要通过能力探测;增加 Sixel 输出不代表所有 Xshell 版本均可显示图片,终端本身也必须支持该协议。ANSI 全景适合查看桌面轮廓,阅读小字需进入细节模式。
|
||||
|
||||
## 安装与连接
|
||||
|
||||
@@ -53,28 +53,35 @@ xmake run crossdesk-cli 123456789 --quality high --zoom native
|
||||
|
||||
```sh
|
||||
crossdesk-cli 123456789 --renderer kitty
|
||||
crossdesk-cli 123456789 --renderer sixel --fps 8
|
||||
crossdesk-cli 123456789 --renderer ansi --zoom native
|
||||
crossdesk-cli 123456789 --quality balanced --fps 8
|
||||
crossdesk-cli 123456789 --display 1
|
||||
```
|
||||
|
||||
默认请求 `high` 画质(最高 1920 × 1080),`--display` 的索引从 `0` 开始。交互 CLI 默认本地输出目标为 30 FPS,远端供帧会随输出耗时自适应;显式 `--fps` 固定目标和相应预览请求,实际帧率仍取决于设备、终端与链路。更多参数见 [主 README](../../README.md#cli) 和 [终端使用指南](docs/terminal-client.md)。
|
||||
默认请求 `high` 画质(最高 1920 × 1080)和清晰度优先策略,`--display` 的索引从 `0` 开始。交互 CLI 默认本地输出目标为 30 FPS,远端供帧会随输出耗时自适应;显式 `--fps` 固定目标和相应预览请求,各帧率下均优先保持分辨率,实际画质与帧率仍取决于设备、终端与链路。更多参数见 [主 README](../../README.md#cli) 和 [终端使用指南](docs/terminal-client.md)。
|
||||
|
||||
## 查看、控制与退出
|
||||
|
||||
| 操作 | 按键 |
|
||||
| --- | --- |
|
||||
| 开启远端控制 | 只读模式下按 `c` |
|
||||
| 放大局部 / 回到全景 | 只读或本地命令模式下按 `d` / `f`;只读时也可点击目标进入细节 |
|
||||
| 缩放 / 平移 | 只读或本地命令模式下用 `+/-` 或滚轮缩放;方向键或 `i` 上、`j` 左、`k` 下、`l` 右平移 |
|
||||
| 缩放 | 只读或本地命令模式下用 `+` / `=` / `-` 以鼠标位置缩放;只读时也可用滚轮 |
|
||||
| 回到全景 | 只读或本地命令模式下按 `f`,同时重置倍率、平移和细节模式 |
|
||||
| 平移 | 只读或本地命令模式下用方向键或 `i` 上、`j` 左、`k` 下、`l` 右 |
|
||||
| 原始细节 | 只读时点击目标位置,或以 `--zoom native` 启动 |
|
||||
| 暂停远端输入并进入本地命令模式 | `Ctrl+]` |
|
||||
| 选择返回后的控制状态 | 本地命令模式下按 `c`,再按 `Esc` / `Ctrl+C` 返回 |
|
||||
| 退出连接 | 只读模式下按 `q` / `Ctrl+C`;控制模式下先按 `Ctrl+]`,再按 `q` |
|
||||
|
||||
本地命令模式在缩放、平移和按 `c` 后持续保持,直到手动返回。控制模式中的 `Ctrl+T`、`Ctrl+C` 和普通按键均传给远端。再次按 `Ctrl+]` 也可离开本地命令模式,若返回控制模式则同时向远端发送该组合键。
|
||||
缩放尽量保持鼠标下的画面内容原位;没有有效鼠标位置时沿用当前中心。底部提示随模式和窗口宽度调整,`Zoom at mouse` 表示鼠标锚点缩放,`Full view` / `Full` 表示恢复全景。
|
||||
|
||||
本地命令模式在缩放、平移和按 `c` 后持续保持,直到手动返回。此模式中鼠标仅更新缩放锚点,点击和滚轮不执行视图操作。控制模式中的 `Ctrl+T`、`Ctrl+C` 和普通按键均传给远端。再次按 `Ctrl+]` 也可离开本地命令模式,若返回控制模式则同时向远端发送该组合键。
|
||||
|
||||
## 更多文档与功能范围
|
||||
|
||||
- [MCP 服务与实时面板](../mcp/README.md):Agent 截图和结构化远程操作,不要求控制端安装桌面客户端。
|
||||
- [原生自动化协议](docs/automation-protocol.md):独立 `agent --stdio` 模式。
|
||||
- [构建、使用、Pi 安装和故障排查](docs/terminal-client.md)
|
||||
- [私有桥接协议](docs/bridge-protocol.md)
|
||||
- [实际验证记录](docs/validation.md)
|
||||
@@ -82,4 +89,4 @@ crossdesk-cli 123456789 --display 1
|
||||
|
||||
CLI 仅作为控制端,每次连接通过服务端获得新的 `cli-xxxxxxxxx` 临时 ID。使用前须部署支持 `cli` 注册的 crossdesk-server;旧服务端不会回退为普通设备身份。
|
||||
|
||||
终端 I/O 包含 POSIX(Linux/macOS)和 Windows Console / ConPTY 实现。macOS arm64 已完整构建,之前已连接真实被控端验证 ANSI 观看与预览协商。Linux / Windows 全量构建和真实连接、SSH/Kitty 实机显示及真实 Pi TUI 仍需验收;交叉编译检查不等于完整运行验证。此项目不创建被控端虚拟桌面,不提供音频播放、文件传输、剪贴板同步或 MCP 服务。
|
||||
终端 I/O 包含 POSIX(Linux/macOS)和 Windows Console / ConPTY 实现。macOS arm64 已完整构建,之前已连接真实被控端验证 ANSI 观看与预览协商。Linux / Windows 全量构建和真实连接、SSH/Kitty/Sixel 实机显示及真实 Pi TUI 仍需验收;交叉编译检查不等于完整运行验证。CLI 不创建被控端虚拟桌面,不提供音频播放、文件传输或剪贴板同步;MCP 服务由独立的 `apps/mcp` 包提供。
|
||||
|
||||
@@ -0,0 +1,835 @@
|
||||
#include "agent.h"
|
||||
#include "automation_protocol.h"
|
||||
#include "terminal_io.h"
|
||||
#include "video_server.h"
|
||||
#include <algorithm>
|
||||
#include <cerrno>
|
||||
#include <cmath>
|
||||
#include <deque>
|
||||
#include <frame_image.h>
|
||||
#include <future>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <terminal_renderer.h>
|
||||
|
||||
namespace crossdesk::cli {
|
||||
namespace {
|
||||
using nlohmann::json;
|
||||
using namespace automation;
|
||||
using Clock = std::chrono::steady_clock;
|
||||
using Ms = std::chrono::milliseconds;
|
||||
constexpr size_t kMaxResponse = 4 * 1024 * 1024;
|
||||
int64_t Age(Clock::time_point queued) {
|
||||
return std::chrono::duration_cast<Ms>(Clock::now() - queued).count();
|
||||
}
|
||||
class Responses {
|
||||
public:
|
||||
explicit Responses(int fd) : fd_(fd), mode_(fd, true) {}
|
||||
void Put(json j) {
|
||||
auto line = j.dump(-1, ' ', false, json::error_handler_t::replace) + '\n';
|
||||
if (line.size() > kMaxResponse || queue_.size() >= 64 ||
|
||||
bytes_ + line.size() > 8 * 1024 * 1024)
|
||||
throw std::runtime_error("OUTPUT_BACKPRESSURE");
|
||||
if (queue_.empty())
|
||||
progressed_ = Clock::now();
|
||||
bytes_ += line.size();
|
||||
queue_.push_back(std::move(line));
|
||||
}
|
||||
bool Empty() const { return queue_.empty(); }
|
||||
void Pump() {
|
||||
if (Empty())
|
||||
return;
|
||||
const auto &s = queue_.front();
|
||||
auto n = Write(fd_, s.data() + offset_,
|
||||
std::min(size_t(16384), s.size() - offset_));
|
||||
if (n > 0) {
|
||||
offset_ += n;
|
||||
bytes_ -= n;
|
||||
progressed_ = Clock::now();
|
||||
if (offset_ == s.size()) {
|
||||
queue_.pop_front();
|
||||
offset_ = 0;
|
||||
}
|
||||
} else if (n < 0 && errno != EAGAIN && errno != EINTR)
|
||||
throw std::runtime_error("OUTPUT_CLOSED");
|
||||
if (Clock::now() - progressed_ > std::chrono::seconds(10))
|
||||
throw std::runtime_error("OUTPUT_TIMEOUT");
|
||||
}
|
||||
|
||||
private:
|
||||
int fd_;
|
||||
NonBlocking mode_;
|
||||
std::deque<std::string> queue_;
|
||||
size_t offset_ = 0, bytes_ = 0;
|
||||
Clock::time_point progressed_ = Clock::now();
|
||||
};
|
||||
struct Observation {
|
||||
std::string id;
|
||||
uint64_t generation, epoch;
|
||||
int width, height, source_width, source_height;
|
||||
ImageRegion region;
|
||||
Clock::time_point created;
|
||||
};
|
||||
struct Capture {
|
||||
json id, request;
|
||||
bool preview = false;
|
||||
Clock::time_point deadline;
|
||||
};
|
||||
struct Encoding {
|
||||
Capture capture;
|
||||
std::shared_ptr<const DecodedFrame> frame;
|
||||
uint64_t epoch;
|
||||
bool cancelled = false;
|
||||
std::future<FrameImage> result;
|
||||
};
|
||||
struct TerminalEncoding {
|
||||
json id;
|
||||
uint64_t generation, epoch;
|
||||
Clock::time_point queued;
|
||||
bool cancelled = false;
|
||||
std::future<terminal::Grid> result;
|
||||
};
|
||||
struct RunningAction {
|
||||
json id;
|
||||
std::string operation;
|
||||
uint64_t generation = 0, epoch = 0;
|
||||
std::vector<Step> steps;
|
||||
size_t next = 0;
|
||||
bool submitted = false;
|
||||
Clock::time_point started;
|
||||
};
|
||||
struct Record {
|
||||
std::string operation, arguments;
|
||||
json result;
|
||||
Clock::time_point created;
|
||||
};
|
||||
class Automation {
|
||||
public:
|
||||
Automation(int output, SessionOptions options, int password, bool video)
|
||||
: output_(output), options_(std::move(options)), password_fd_(password),
|
||||
video_(video ? std::make_unique<VideoServer>() : nullptr) {}
|
||||
~Automation() { session_.Disconnect(); }
|
||||
int Run() {
|
||||
NonBlocking input(0);
|
||||
std::string buffer;
|
||||
bool eof = false, quit = false;
|
||||
auto until = Clock::time_point::max();
|
||||
while (!interrupted) {
|
||||
Tick();
|
||||
output_.Pump();
|
||||
PumpVideo();
|
||||
if (!quit) {
|
||||
char bytes[8192];
|
||||
const auto n = Read(0, bytes, sizeof(bytes));
|
||||
if (n > 0)
|
||||
buffer.append(bytes, size_t(n));
|
||||
else if (!n)
|
||||
eof = true;
|
||||
else if (errno != EAGAIN && errno != EINTR)
|
||||
eof = true;
|
||||
for (int count = 0; count < 16 && !quit; ++count) {
|
||||
const auto end = buffer.find('\n');
|
||||
if ((end == std::string::npos && buffer.size() > 65536) ||
|
||||
(end != std::string::npos && end > 65536)) {
|
||||
Error(nullptr, "MESSAGE_TOO_LARGE");
|
||||
quit = true;
|
||||
break;
|
||||
}
|
||||
if (end == std::string::npos)
|
||||
break;
|
||||
auto request = ParseBoundedJson(buffer.substr(0, end));
|
||||
buffer.erase(0, end + 1);
|
||||
quit = Handle(request);
|
||||
}
|
||||
if (!quit && buffer.size() > 128 * 1024) {
|
||||
Error(nullptr, "INPUT_BACKPRESSURE");
|
||||
quit = true;
|
||||
}
|
||||
if (eof && buffer.find('\n') == std::string::npos) {
|
||||
if (!buffer.empty())
|
||||
Error(nullptr, "INCOMPLETE_MESSAGE");
|
||||
quit = true;
|
||||
}
|
||||
}
|
||||
if (quit && until == Clock::time_point::max()) {
|
||||
Stop("CANCELLED");
|
||||
session_.Disconnect();
|
||||
PumpVideo();
|
||||
until = Clock::now() + std::chrono::seconds(2);
|
||||
}
|
||||
if (quit && (output_.Empty() || Clock::now() >= until))
|
||||
break;
|
||||
WaitIo(quit ? -1 : 0, output_.Empty() ? -1 : output_fd(), 5);
|
||||
}
|
||||
return interrupted ? 130 : 0;
|
||||
}
|
||||
void SetOutputFd(int fd) { output_fd_ = fd; }
|
||||
|
||||
private:
|
||||
int output_fd() const { return output_fd_; }
|
||||
json Status() const {
|
||||
auto result = session_.Status();
|
||||
result["video"] = {{"decode_path", decode_path_}, {"decoder", decoder_}};
|
||||
result["video_output"]["enabled"] = video_ && video_->Enabled();
|
||||
result["video_output"]["protocol"] =
|
||||
decode_path_ == "minirtc" ? "nv12/1" : "h264-annexb/1";
|
||||
if (video_) result["video_output"].update(video_->Status());
|
||||
if (decode_path_ == "minirtc") {
|
||||
result["video_output"]["received"] = frame_ ? frame_->frame_id : 0;
|
||||
result["video_output"]["waiting_keyframe"] = false;
|
||||
result["video_output"].erase("has_sps");
|
||||
result["video_output"].erase("has_pps");
|
||||
}
|
||||
result["transport_state"] = result["state"];
|
||||
result["geometry_epoch"] = epoch_;
|
||||
result["cursor"] = Cursor();
|
||||
result["human_paused"] = paused_;
|
||||
result["control_allowed"] = allowed_;
|
||||
result["frame_age_ms"] = frame_ ? json(Age(frame_->queued)) : json(nullptr);
|
||||
result["source_size"] =
|
||||
frame_ ? json{{"width", frame_->width}, {"height", frame_->height}}
|
||||
: json(nullptr);
|
||||
if (session_.Connected())
|
||||
result["state"] = frame_ && frame_->generation == session_.Generation()
|
||||
? "ready"
|
||||
: "waiting_frame";
|
||||
return result;
|
||||
}
|
||||
json Cursor() const {
|
||||
const bool visible = cursor_visible_ && frame_ && session_.Connected() &&
|
||||
frame_->generation == session_.Generation() && allowed_ && !paused_ &&
|
||||
session_.ControlEnabled();
|
||||
return {{"type", "cursor"}, {"visible", visible},
|
||||
{"x", cursor_x_}, {"y", cursor_y_},
|
||||
{"generation", session_.Generation()}, {"geometry_epoch", epoch_},
|
||||
{"width", frame_ ? frame_->width : 0},
|
||||
{"height", frame_ ? frame_->height : 0}};
|
||||
}
|
||||
void PumpVideo() {
|
||||
if (!video_) return;
|
||||
const auto generation = session_.Generation();
|
||||
const bool connected = session_.Connected();
|
||||
const bool decoded = decode_path_ == "minirtc";
|
||||
if (generation != video_generation_ || connected != video_connected_) {
|
||||
video_->Bind(generation, decoded, connected);
|
||||
video_generation_ = generation;
|
||||
video_connected_ = connected;
|
||||
last_video_frame_ = 0;
|
||||
}
|
||||
const auto cursor = Cursor();
|
||||
if (cursor != video_cursor_) {
|
||||
video_->Cursor(cursor);
|
||||
video_cursor_ = cursor;
|
||||
}
|
||||
const bool enabled = video_->Enabled();
|
||||
const bool key = video_->TakeKeyframeRequest();
|
||||
if (enabled != video_enabled_ || key) {
|
||||
session_.SetVideoOutput(enabled && !decoded, key && !decoded);
|
||||
video_enabled_ = enabled;
|
||||
last_video_frame_ = 0;
|
||||
}
|
||||
if (!enabled) return;
|
||||
if (decoded) {
|
||||
if (frame_ && frame_->generation == generation && Age(frame_->queued) <= 2000 &&
|
||||
frame_->frame_id != last_video_frame_) {
|
||||
video_->Submit(frame_);
|
||||
last_video_frame_ = frame_->frame_id;
|
||||
}
|
||||
} else {
|
||||
for (int count = 0; count < 8; ++count) {
|
||||
auto frame = session_.TakeEncodedVideo();
|
||||
if (!frame) break;
|
||||
video_->Submit(std::move(*frame));
|
||||
}
|
||||
}
|
||||
}
|
||||
void Reply(const json &id, json value) {
|
||||
output_.Put({{"id", id}, {"ok", true}, {"result", std::move(value)}});
|
||||
}
|
||||
void Error(const json &id, const std::string &code,
|
||||
bool side_effect = false) {
|
||||
output_.Put({{"id", id},
|
||||
{"ok", false},
|
||||
{"error",
|
||||
{{"code", code},
|
||||
{"retryable", !side_effect && (code == "FRAME_TIMEOUT" ||
|
||||
code == "BUSY")},
|
||||
{"side_effect_possible", side_effect}}}});
|
||||
}
|
||||
void FinishAction(const char *error = nullptr) {
|
||||
if (!action_)
|
||||
return;
|
||||
session_.ReleaseInput();
|
||||
json result = {
|
||||
{"status", error ? (action_->submitted ? "outcome_unknown"
|
||||
: "failed_before_submit")
|
||||
: "submitted"},
|
||||
{"operation_id", action_->operation},
|
||||
{"generation", action_->generation},
|
||||
{"after_frame_id", frame_ ? frame_->frame_id : 0},
|
||||
{"side_effect_possible", action_->submitted},
|
||||
{"retryable", false}};
|
||||
if (error)
|
||||
result["error"] = error;
|
||||
for (auto &record : records_)
|
||||
if (record.operation == action_->operation) {
|
||||
record.result = result;
|
||||
break;
|
||||
}
|
||||
Reply(action_->id, result);
|
||||
action_.reset();
|
||||
}
|
||||
void Stop(const char *code) {
|
||||
FinishAction(code);
|
||||
for (const auto &capture : captures_)
|
||||
Error(capture.id, code);
|
||||
captures_.clear();
|
||||
if (encoding_ && !encoding_->cancelled) {
|
||||
Error(encoding_->capture.id, code);
|
||||
encoding_->cancelled = true;
|
||||
}
|
||||
if (terminal_encoding_ && !terminal_encoding_->cancelled) {
|
||||
Error(terminal_encoding_->id, code);
|
||||
terminal_encoding_->cancelled = true;
|
||||
}
|
||||
observations_.clear();
|
||||
preview_ = json();
|
||||
}
|
||||
void Tick() {
|
||||
session_.Tick();
|
||||
// Recycle only uniquely owned frames; an encoder/video worker may still be
|
||||
// reading the old pixels. Save geometry before Take can swap its storage.
|
||||
if (auto f = session_.TakeFrame(frame_ && frame_.use_count() == 1 ? frame_.get() : nullptr)) {
|
||||
if (!frame_ || f->generation != frame_->generation ||
|
||||
f->width != frame_->width || f->height != frame_->height) {
|
||||
++epoch_;
|
||||
cursor_visible_ = false;
|
||||
observations_.clear();
|
||||
preview_ = json();
|
||||
}
|
||||
frame_ = std::make_shared<DecodedFrame>(std::move(*f));
|
||||
}
|
||||
if (frame_ && frame_->generation != session_.Generation()) {
|
||||
frame_.reset();
|
||||
++epoch_;
|
||||
cursor_visible_ = false;
|
||||
observations_.clear();
|
||||
preview_ = json();
|
||||
}
|
||||
if (action_) {
|
||||
if (!session_.Connected() || !frame_ || paused_ || !allowed_ ||
|
||||
action_->generation != session_.Generation() ||
|
||||
action_->epoch != epoch_)
|
||||
FinishAction("SESSION_CHANGED");
|
||||
while (action_ && action_->next < action_->steps.size() &&
|
||||
Age(action_->started) >= action_->steps[action_->next].delay) {
|
||||
const auto &step = action_->steps[action_->next++];
|
||||
action_->submitted =
|
||||
true; // A failed send may still have reached the peer.
|
||||
const bool sent =
|
||||
step.kind == StepKind::Pointer
|
||||
? session_.Pointer(step.x, step.y, step.flag, step.wheel)
|
||||
: step.kind == StepKind::Key ? session_.Key(step.key, step.down)
|
||||
: session_.Text(step.text);
|
||||
if (!sent) {
|
||||
FinishAction("ACTION_OUTCOME_UNKNOWN");
|
||||
break;
|
||||
}
|
||||
if (step.kind == StepKind::Pointer) {
|
||||
cursor_x_ = step.x;
|
||||
cursor_y_ = step.y;
|
||||
cursor_visible_ = true;
|
||||
}
|
||||
action_->submitted = true;
|
||||
}
|
||||
if (action_ && action_->next == action_->steps.size())
|
||||
FinishAction();
|
||||
}
|
||||
if (terminal_encoding_ &&
|
||||
terminal_encoding_->result.wait_for(Ms(0)) == std::future_status::ready) {
|
||||
auto job = std::move(terminal_encoding_);
|
||||
try {
|
||||
auto grid = job->result.get();
|
||||
if (job->cancelled) {
|
||||
// Consume the worker result without replying twice.
|
||||
} else if (!session_.Connected() || job->generation != session_.Generation() ||
|
||||
job->epoch != epoch_)
|
||||
Error(job->id, "STALE_OBSERVATION");
|
||||
else if (Age(job->queued) > 2000)
|
||||
Error(job->id, "FRAME_TIMEOUT");
|
||||
else {
|
||||
json cursor = nullptr;
|
||||
if (Cursor()["visible"].get<bool>()) {
|
||||
const int x = grid.content.x + std::clamp(
|
||||
int(cursor_x_ * grid.content.width), 0, grid.content.width - 1);
|
||||
const int y = grid.content.y + std::clamp(
|
||||
int(cursor_y_ * grid.content.height), 0, grid.content.height - 1);
|
||||
cursor = {{"column", x / grid.samples_x}, {"row", y / grid.samples_y}};
|
||||
}
|
||||
Reply(job->id, {{"columns", grid.viewport.columns}, {"rows", grid.viewport.rows},
|
||||
{"lines", terminal::Lines(grid)}, {"cursor", cursor},
|
||||
{"generation", job->generation}, {"geometry_epoch", job->epoch}});
|
||||
}
|
||||
} catch (...) { if (!job->cancelled) Error(job->id, "FRAME_RENDER_FAILED"); }
|
||||
}
|
||||
if (encoding_ &&
|
||||
encoding_->result.wait_for(Ms(0)) == std::future_status::ready) {
|
||||
auto job = std::move(encoding_);
|
||||
try {
|
||||
auto image = job->result.get();
|
||||
if (!job->cancelled) {
|
||||
if (!frame_ || job->epoch != epoch_ ||
|
||||
job->frame->generation != session_.Generation())
|
||||
Error(job->capture.id, "STALE_OBSERVATION");
|
||||
else if (Age(job->frame->queued) > 2000)
|
||||
Error(job->capture.id, "FRAME_TIMEOUT");
|
||||
else {
|
||||
const auto &f = *job->frame;
|
||||
json meta = {
|
||||
{"generation", f.generation},
|
||||
{"geometry_epoch", job->epoch},
|
||||
{"frame_id", f.frame_id},
|
||||
{"display_index", session_.Status()["display"]},
|
||||
{"source_size", {{"width", f.width}, {"height", f.height}}},
|
||||
{"source_rect",
|
||||
{{"x", image.region.x},
|
||||
{"y", image.region.y},
|
||||
{"width", image.region.width},
|
||||
{"height", image.region.height}}},
|
||||
{"image_size",
|
||||
{{"width", image.width}, {"height", image.height}}},
|
||||
{"frame_age_ms", Age(f.queued)}};
|
||||
json result = {{"mime_type", "image/png"},
|
||||
{"data", ImageBase64(image.png)},
|
||||
{"metadata", meta}};
|
||||
if (job->capture.preview) {
|
||||
preview_ = result;
|
||||
preview_time_ = f.queued;
|
||||
preview_edge_ =
|
||||
Number(job->capture.request, "max_edge", 64, 960, 960);
|
||||
} else {
|
||||
const auto id = "obs_" + std::to_string(f.generation) + "_" +
|
||||
std::to_string(++observation_sequence_);
|
||||
result["metadata"]["observation_id"] = id;
|
||||
if (observations_.size() >= 32)
|
||||
observations_.pop_front();
|
||||
observations_.push_back(
|
||||
{id, f.generation, job->epoch, image.width, image.height,
|
||||
int(f.width), int(f.height), image.region, Clock::now()});
|
||||
}
|
||||
Reply(job->capture.id, std::move(result));
|
||||
}
|
||||
}
|
||||
} catch (...) {
|
||||
if (!job->cancelled)
|
||||
Error(job->capture.id, "IMAGE_ENCODING_FAILED");
|
||||
}
|
||||
}
|
||||
for (auto it = captures_.begin(); it != captures_.end();) {
|
||||
if (it->request.contains("generation") &&
|
||||
Sequence(it->request, "generation") != session_.Generation()) {
|
||||
Error(it->id, "STALE_OBSERVATION");
|
||||
it = captures_.erase(it);
|
||||
} else if (Clock::now() > it->deadline || session_.Finished()) {
|
||||
Error(it->id, session_.Finished() ? "NOT_READY" : "FRAME_TIMEOUT");
|
||||
it = captures_.erase(it);
|
||||
} else
|
||||
++it;
|
||||
}
|
||||
if (!encoding_ && !captures_.empty() && frame_ && session_.Connected() &&
|
||||
Age(frame_->queued) <= 2000) {
|
||||
auto it = std::find_if(
|
||||
captures_.begin(), captures_.end(), [&](const Capture &c) {
|
||||
return !c.request.contains("after_frame_id") ||
|
||||
frame_->frame_id > Sequence(c.request, "after_frame_id");
|
||||
});
|
||||
if (it != captures_.end()) {
|
||||
auto capture = *it;
|
||||
const auto capture_id = capture.id;
|
||||
captures_.erase(it);
|
||||
try {
|
||||
Encode(std::move(capture));
|
||||
} catch (...) {
|
||||
Error(capture_id, "INVALID_REGION");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void Encode(Capture capture) {
|
||||
ImageRegion region;
|
||||
if (capture.request.contains("region")) {
|
||||
const auto &r = capture.request["region"];
|
||||
Fields(r, {"x", "y", "width", "height"});
|
||||
region = {Number(r, "x", 0, int(frame_->width) - 1, -1),
|
||||
Number(r, "y", 0, int(frame_->height) - 1, -1),
|
||||
Number(r, "width", 1, frame_->width, -1),
|
||||
Number(r, "height", 1, frame_->height, -1)};
|
||||
if (region.x < 0 || region.y < 0 || region.width < 1 ||
|
||||
region.height < 1 || region.x > int(frame_->width) - region.width ||
|
||||
region.y > int(frame_->height) - region.height)
|
||||
throw std::invalid_argument("INVALID_REGION");
|
||||
}
|
||||
const int edge =
|
||||
Number(capture.request, "max_edge", 64, capture.preview ? 960 : 2048,
|
||||
capture.preview ? 960 : 1280);
|
||||
auto job = std::make_unique<Encoding>();
|
||||
job->capture = capture;
|
||||
job->frame = frame_;
|
||||
job->epoch = epoch_;
|
||||
job->result = std::async(std::launch::async, [frame = frame_, region, edge,
|
||||
preview = capture.preview] {
|
||||
return EncodeFrameImage(*frame, region, edge,
|
||||
preview ? 256 * 1024 : 2 * 1024 * 1024);
|
||||
});
|
||||
encoding_ = std::move(job);
|
||||
}
|
||||
void CaptureFrame(const json &j, const json &id, bool preview) {
|
||||
Fields(j, {"id", "method", "region", "max_edge", "after_frame_id",
|
||||
"generation", "timeout_ms"});
|
||||
if (!session_.Connected())
|
||||
throw std::invalid_argument("NOT_READY");
|
||||
Number(j, "max_edge", 64, preview ? 960 : 2048, preview ? 960 : 1280);
|
||||
if (preview && j.contains("region"))
|
||||
throw std::invalid_argument("INVALID_REQUEST");
|
||||
if (j.contains("after_frame_id") && !j.contains("generation"))
|
||||
throw std::invalid_argument("GENERATION_REQUIRED");
|
||||
Sequence(j, "after_frame_id");
|
||||
if (j.contains("generation") &&
|
||||
Sequence(j, "generation") != session_.Generation())
|
||||
throw std::invalid_argument("STALE_OBSERVATION");
|
||||
if (preview && frame_ && j.contains("after_frame_id") &&
|
||||
Sequence(j, "after_frame_id") >= frame_->frame_id) {
|
||||
Reply(id, {{"unchanged", true},
|
||||
{"generation", frame_->generation},
|
||||
{"frame_id", frame_->frame_id},
|
||||
{"frame_age_ms", Age(frame_->queued)},
|
||||
{"stale", Age(frame_->queued) > 2000}});
|
||||
return;
|
||||
}
|
||||
if (preview && frame_ && !preview_.is_null() &&
|
||||
Age(preview_time_) <= 2000 &&
|
||||
preview_edge_ == Number(j, "max_edge", 64, 960, 960) &&
|
||||
preview_["metadata"]["generation"] == frame_->generation &&
|
||||
preview_["metadata"]["frame_id"] == frame_->frame_id) {
|
||||
auto cached = preview_;
|
||||
cached["metadata"]["frame_age_ms"] = Age(preview_time_);
|
||||
Reply(id, cached);
|
||||
return;
|
||||
}
|
||||
if (captures_.size() >= 16)
|
||||
throw std::invalid_argument("BUSY");
|
||||
const int timeout = Number(j, "timeout_ms", 0, 5000, 2000);
|
||||
Capture c{id, j, preview, Clock::now() + Ms(timeout)};
|
||||
// timeout_ms bounds waiting for a suitable frame, not PNG encoding.
|
||||
// A zero timeout can capture an already available frame immediately.
|
||||
const bool available = frame_ && frame_->generation == session_.Generation() &&
|
||||
Age(frame_->queued) <= 2000 &&
|
||||
(!j.contains("after_frame_id") || frame_->frame_id > Sequence(j, "after_frame_id"));
|
||||
if (!encoding_ && captures_.empty() && available) {
|
||||
Encode(std::move(c));
|
||||
return;
|
||||
}
|
||||
if (timeout == 0)
|
||||
throw std::invalid_argument(encoding_ || !captures_.empty() ? "BUSY" : "FRAME_TIMEOUT");
|
||||
if (preview)
|
||||
captures_.push_back(c);
|
||||
else
|
||||
captures_.insert(std::find_if(captures_.begin(), captures_.end(),
|
||||
[](const Capture &queued) { return queued.preview; }), c);
|
||||
}
|
||||
const Observation &Observe(const json &j) {
|
||||
auto name = String(j, "observation_id");
|
||||
auto it = std::find_if(observations_.begin(), observations_.end(),
|
||||
[&](const Observation &o) { return o.id == name; });
|
||||
if (it == observations_.end() || it->generation != session_.Generation() ||
|
||||
it->epoch != epoch_ || Age(it->created) > 30000)
|
||||
throw std::invalid_argument("STALE_OBSERVATION");
|
||||
return *it;
|
||||
}
|
||||
void StartAction(const json &j, const json &id) {
|
||||
Fields(j, {"id", "method", "operation_id", "observation_id", "action"});
|
||||
const auto operation = String(j, "operation_id");
|
||||
const auto arguments = json{
|
||||
{"observation_id", j.at("observation_id")},
|
||||
{"action",
|
||||
j.at("action")}}.dump();
|
||||
for (const auto &r : records_)
|
||||
if (r.operation == operation) {
|
||||
if (r.arguments != arguments)
|
||||
throw std::invalid_argument("OPERATION_ID_CONFLICT");
|
||||
Reply(id, r.result);
|
||||
return;
|
||||
}
|
||||
if (!allowed_ || paused_ || !session_.ControlEnabled())
|
||||
throw std::invalid_argument("CONTROL_DENIED");
|
||||
if (!session_.Connected() || !frame_ || Age(frame_->queued) > 2000)
|
||||
throw std::invalid_argument("NOT_READY");
|
||||
if (action_)
|
||||
throw std::invalid_argument("BUSY");
|
||||
const auto &o = Observe(j);
|
||||
const auto &a = j.at("action");
|
||||
const auto type = String(a, "type", 32);
|
||||
auto running = std::make_unique<RunningAction>();
|
||||
running->id = id;
|
||||
running->operation = operation;
|
||||
running->generation = o.generation;
|
||||
running->epoch = o.epoch;
|
||||
if (type == "type_text" && !session_.Status()["text_input"].get<bool>())
|
||||
throw std::invalid_argument("TEXT_INPUT_UNSUPPORTED");
|
||||
running->steps = automation::BuildInputSteps(a,
|
||||
{o.width, o.height, o.source_width, o.source_height, o.region});
|
||||
while (records_.size() >= 128 ||
|
||||
(!records_.empty() && Age(records_.front().created) > 300000))
|
||||
records_.pop_front();
|
||||
records_.push_back(
|
||||
{operation,
|
||||
arguments,
|
||||
{{"status", "in_progress"}, {"operation_id", operation}},
|
||||
Clock::now()});
|
||||
running->started = Clock::now();
|
||||
action_ = std::move(running);
|
||||
}
|
||||
std::string Password() {
|
||||
if (password_fd_ < 3 || password_fd_ > 1024)
|
||||
throw std::invalid_argument("CREDENTIAL_CHANNEL_INVALID");
|
||||
std::string secret;
|
||||
const auto deadline = Clock::now() + std::chrono::seconds(5);
|
||||
while (!interrupted && Clock::now() < deadline) {
|
||||
if (!WaitReadable(password_fd_, 10))
|
||||
continue;
|
||||
char c;
|
||||
const auto n = Read(password_fd_, &c, 1);
|
||||
if (!n || (n == 1 && c == '\n'))
|
||||
return secret;
|
||||
if (n < 0) {
|
||||
if (errno == EINTR || errno == EAGAIN)
|
||||
continue;
|
||||
throw std::invalid_argument("CREDENTIAL_CHANNEL_FAILED");
|
||||
}
|
||||
if (!c || secret.size() >= 256)
|
||||
throw std::invalid_argument("INVALID_CREDENTIAL");
|
||||
secret += c;
|
||||
}
|
||||
std::fill(secret.begin(), secret.end(), '\0');
|
||||
throw std::invalid_argument("CREDENTIAL_TIMEOUT");
|
||||
}
|
||||
bool Handle(const json &j) {
|
||||
json id = nullptr;
|
||||
try {
|
||||
if (!j.is_object() || !j.contains("id") || !j["id"].is_number_integer() ||
|
||||
j["id"].get<int64_t>() < 0)
|
||||
throw std::invalid_argument("INVALID_REQUEST");
|
||||
id = j["id"];
|
||||
const auto method = String(j, "method", 32);
|
||||
if (method == "hello") {
|
||||
Fields(j, {"id", "method", "protocol"});
|
||||
if (String(j, "protocol") != "crossdesk-automation/1")
|
||||
throw std::invalid_argument("VERSION_MISMATCH");
|
||||
hello_ = true;
|
||||
Reply(id, {{"protocol", "crossdesk-automation/1"},
|
||||
{"image_formats", {"image/png"}},
|
||||
{"video_protocols", {"h264-annexb/1", "nv12/1"}},
|
||||
{"video_decoders", {"auto", "software"}},
|
||||
{"video_transport", "loopback-websocket/1"},
|
||||
{"terminal_protocol", "ansi/1"},
|
||||
{"action_completion", "submitted"},
|
||||
{"max_request_bytes", 65536},
|
||||
{"max_response_bytes", kMaxResponse},
|
||||
{"operation_cache_entries", 128},
|
||||
{"operation_cache_ms", 300000},
|
||||
{"human_pause", true}});
|
||||
return false;
|
||||
}
|
||||
if (!hello_)
|
||||
throw std::invalid_argument("HELLO_REQUIRED");
|
||||
if (method == "connect") {
|
||||
Fields(j, {"id", "method", "device", "display", "mode",
|
||||
"decode_path", "decoder"});
|
||||
if (connected_once_)
|
||||
throw std::invalid_argument("NEW_PROCESS_REQUIRED");
|
||||
const auto device = String(j, "device", 9);
|
||||
if (device.size() != 9 ||
|
||||
device.find_first_not_of("0123456789") != std::string::npos)
|
||||
throw std::invalid_argument("INVALID_DEVICE");
|
||||
const auto mode = j.contains("mode") ? String(j, "mode", 8) : "observe";
|
||||
if (mode != "observe" && mode != "control")
|
||||
throw std::invalid_argument("INVALID_MODE");
|
||||
options_.display = Number(j, "display", 0, 63, 0);
|
||||
const auto path = j.contains("decode_path")
|
||||
? String(j, "decode_path", 16) : "browser";
|
||||
const auto decoder = j.contains("decoder")
|
||||
? String(j, "decoder", 16) : "auto";
|
||||
if ((path != "browser" && path != "minirtc") ||
|
||||
(decoder != "auto" && decoder != "software"))
|
||||
throw std::invalid_argument("INVALID_VIDEO_CONFIG");
|
||||
decode_path_ = path;
|
||||
decoder_ = decoder;
|
||||
options_.hardware_acceleration = decoder != "software";
|
||||
auto password = Password();
|
||||
connected_once_ = true;
|
||||
paused_ = false;
|
||||
allowed_ = mode == "control";
|
||||
session_.Connect(options_, device, std::move(password));
|
||||
session_.SetControl(allowed_);
|
||||
} else if (method == "screenshot" || method == "preview") {
|
||||
CaptureFrame(j, id, method == "preview");
|
||||
return false;
|
||||
} else if (method == "action") {
|
||||
StartAction(j, id);
|
||||
return false;
|
||||
} else if (method == "terminal_frame") {
|
||||
Fields(j, {"id", "method", "columns", "rows", "colors", "cell_aspect"});
|
||||
if (terminal_encoding_) throw std::invalid_argument("BUSY");
|
||||
if (!session_.Connected() || !frame_ ||
|
||||
frame_->generation != session_.Generation() || Age(frame_->queued) > 2000)
|
||||
throw std::invalid_argument("NOT_READY");
|
||||
terminal::Viewport viewport;
|
||||
viewport.columns = Number(j, "columns", 1, 160, 80);
|
||||
viewport.rows = Number(j, "rows", 1, 60, 22);
|
||||
viewport.colors = Number(j, "colors", 8, 256, 24);
|
||||
if (viewport.colors != 8 && viewport.colors != 24 && viewport.colors != 256)
|
||||
throw std::invalid_argument("INVALID_REQUEST");
|
||||
viewport.cell_aspect = j.value("cell_aspect", .5);
|
||||
if (!std::isfinite(viewport.cell_aspect) || viewport.cell_aspect < .2 || viewport.cell_aspect > 2)
|
||||
throw std::invalid_argument("INVALID_REQUEST");
|
||||
viewport.detail = terminal::AnsiDetail::Quadrant;
|
||||
auto job = std::make_unique<TerminalEncoding>();
|
||||
job->id = id;
|
||||
job->generation = frame_->generation;
|
||||
job->epoch = epoch_;
|
||||
job->queued = frame_->queued;
|
||||
job->result = std::async(std::launch::async, [frame = frame_, viewport] {
|
||||
return terminal::Convert(*frame, viewport);
|
||||
});
|
||||
terminal_encoding_ = std::move(job);
|
||||
return false;
|
||||
} else if (method == "control") {
|
||||
Fields(j, {"id", "method", "enabled"});
|
||||
const bool enable = Flag(j, "enabled");
|
||||
if (enable && (!allowed_ || paused_))
|
||||
throw std::invalid_argument("CONTROL_DENIED");
|
||||
if (!enable)
|
||||
FinishAction("CONTROL_DISABLED");
|
||||
session_.SetControl(enable);
|
||||
} else if (method == "viewer_control") {
|
||||
Fields(j, {"id", "method", "operation"});
|
||||
const auto operation = String(j, "operation", 16);
|
||||
if (operation == "pause") {
|
||||
paused_ = true;
|
||||
FinishAction("USER_PAUSED");
|
||||
session_.SetControl(false);
|
||||
} else if (operation == "resume") {
|
||||
if (!allowed_)
|
||||
throw std::invalid_argument("CONTROL_DENIED");
|
||||
paused_ = false;
|
||||
session_.SetControl(true);
|
||||
} else if (operation == "disconnect") {
|
||||
Stop("CANCELLED");
|
||||
session_.Disconnect();
|
||||
} else
|
||||
throw std::invalid_argument("INVALID_REQUEST");
|
||||
} else if (method == "display") {
|
||||
Fields(j, {"id", "method", "index"});
|
||||
const int index = Number(j, "index", 0, 63, -1);
|
||||
if (!session_.Connected() || index < 0 ||
|
||||
size_t(index) >= session_.Status()["displays"].size())
|
||||
throw std::invalid_argument("DISPLAY_UNAVAILABLE");
|
||||
Stop("SESSION_CHANGED");
|
||||
if (!session_.SelectDisplay(index))
|
||||
throw std::invalid_argument("DISPLAY_UNAVAILABLE");
|
||||
frame_.reset();
|
||||
++epoch_;
|
||||
cursor_visible_ = false;
|
||||
} else if (method == "cancel") {
|
||||
Fields(j, {"id", "method", "request_id"});
|
||||
if (!j.contains("request_id") || !j["request_id"].is_number_integer())
|
||||
throw std::invalid_argument("INVALID_REQUEST");
|
||||
const auto target = j["request_id"];
|
||||
if (action_ && action_->id == target)
|
||||
FinishAction("CANCELLED");
|
||||
if (encoding_ && !encoding_->cancelled &&
|
||||
encoding_->capture.id == target) {
|
||||
encoding_->cancelled = true;
|
||||
Error(target, "CANCELLED");
|
||||
}
|
||||
if (terminal_encoding_ && !terminal_encoding_->cancelled && terminal_encoding_->id == target) {
|
||||
terminal_encoding_->cancelled = true;
|
||||
Error(target, "CANCELLED");
|
||||
}
|
||||
for (auto it = captures_.begin(); it != captures_.end();) {
|
||||
if (it->id == target) {
|
||||
Error(target, "CANCELLED");
|
||||
it = captures_.erase(it);
|
||||
} else
|
||||
++it;
|
||||
}
|
||||
} else if (method == "disconnect" || method == "exit") {
|
||||
Fields(j, {"id", "method"});
|
||||
Stop("CANCELLED");
|
||||
session_.Disconnect();
|
||||
frame_.reset();
|
||||
} else if (method == "video_open") {
|
||||
Fields(j, {"id", "method", "ticket", "view", "view_generation",
|
||||
"session", "origin", "apps"});
|
||||
if (!video_) throw std::invalid_argument("VIDEO_CHANNEL_UNAVAILABLE");
|
||||
if (!session_.Connected()) throw std::invalid_argument("NOT_READY");
|
||||
PumpVideo();
|
||||
video_->Grant({String(j, "ticket", 32), String(j, "view", 32),
|
||||
String(j, "session", 32), String(j, "origin", 256),
|
||||
Sequence(j, "view_generation"), Flag(j, "apps")});
|
||||
Reply(id, {{"url", video_->Url()}, {"transport", "loopback-websocket/1"},
|
||||
{"protocol", decode_path_ == "minirtc" ? "nv12/1" : "h264-annexb/1"},
|
||||
{"decoder", decoder_}, {"expires_in_ms", 15000}});
|
||||
return false;
|
||||
} else if (method == "video_revoke") {
|
||||
Fields(j, {"id", "method", "view"});
|
||||
if (video_) video_->Revoke(j.contains("view") ? String(j, "view", 32) : "");
|
||||
} else if (method == "status")
|
||||
Fields(j, {"id", "method"});
|
||||
else
|
||||
throw std::invalid_argument("UNSUPPORTED_METHOD");
|
||||
PumpVideo();
|
||||
Reply(id, Status());
|
||||
return method == "exit";
|
||||
} catch (const std::invalid_argument &e) {
|
||||
Error(id, e.what());
|
||||
} catch (const json::exception &) {
|
||||
Error(id, "INVALID_REQUEST");
|
||||
} catch (...) {
|
||||
Error(id, "OPERATION_FAILED");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
ControllerSession session_;
|
||||
Responses output_;
|
||||
SessionOptions options_;
|
||||
std::string decode_path_ = "browser", decoder_ = "auto";
|
||||
int password_fd_, output_fd_ = -1;
|
||||
bool hello_ = false, connected_once_ = false, allowed_ = false,
|
||||
paused_ = false;
|
||||
uint64_t epoch_ = 0, observation_sequence_ = 0;
|
||||
std::shared_ptr<DecodedFrame> frame_;
|
||||
std::deque<Observation> observations_;
|
||||
std::deque<Capture> captures_;
|
||||
std::unique_ptr<Encoding> encoding_;
|
||||
std::unique_ptr<TerminalEncoding> terminal_encoding_;
|
||||
std::unique_ptr<RunningAction> action_;
|
||||
std::deque<Record> records_;
|
||||
json preview_;
|
||||
int preview_edge_ = 960;
|
||||
Clock::time_point preview_time_;
|
||||
std::unique_ptr<VideoServer> video_;
|
||||
uint64_t video_generation_ = 0, last_video_frame_ = 0;
|
||||
bool video_connected_ = false, video_enabled_ = false;
|
||||
bool cursor_visible_ = false;
|
||||
float cursor_x_ = 0, cursor_y_ = 0;
|
||||
json video_cursor_;
|
||||
};
|
||||
} // namespace
|
||||
int Agent(int output_fd, SessionOptions options, int password_fd,
|
||||
bool video_websocket) {
|
||||
Automation automation(output_fd, std::move(options), password_fd, video_websocket);
|
||||
automation.SetOutputFd(output_fd);
|
||||
return automation.Run();
|
||||
}
|
||||
} // namespace crossdesk::cli
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-11-02
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _AGENT_H_
|
||||
#define _AGENT_H_
|
||||
|
||||
#include <controller_session.h>
|
||||
|
||||
namespace crossdesk::cli {
|
||||
int Agent(int output_fd, SessionOptions options, int password_fd,
|
||||
bool video_websocket = false);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,262 @@
|
||||
#include "automation_protocol.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace crossdesk::cli::automation {
|
||||
void Fields(const json &j, std::initializer_list<const char *> names) {
|
||||
if (!j.is_object())
|
||||
throw std::invalid_argument("INVALID_REQUEST");
|
||||
for (auto it = j.begin(); it != j.end(); ++it)
|
||||
if (std::none_of(names.begin(), names.end(),
|
||||
[&](const char *s) { return it.key() == s; }))
|
||||
throw std::invalid_argument("UNKNOWN_FIELD");
|
||||
}
|
||||
int Number(const json &j, const char *key, int low, int high, int fallback) {
|
||||
if (!j.contains(key))
|
||||
return fallback;
|
||||
if (!j[key].is_number_integer())
|
||||
throw std::invalid_argument("INVALID_REQUEST");
|
||||
const auto n = j[key].get<int64_t>();
|
||||
if (n < low || n > high)
|
||||
throw std::invalid_argument("INVALID_REQUEST");
|
||||
return int(n);
|
||||
}
|
||||
uint64_t Sequence(const json &j, const char *key, uint64_t fallback) {
|
||||
if (!j.contains(key))
|
||||
return fallback;
|
||||
if (!j[key].is_number_unsigned())
|
||||
throw std::invalid_argument("INVALID_REQUEST");
|
||||
return j[key].get<uint64_t>();
|
||||
}
|
||||
bool Flag(const json &j, const char *key, bool fallback) {
|
||||
if (!j.contains(key))
|
||||
return fallback;
|
||||
if (!j[key].is_boolean())
|
||||
throw std::invalid_argument("INVALID_REQUEST");
|
||||
return j[key].get<bool>();
|
||||
}
|
||||
std::string String(const json &j, const char *key, size_t limit) {
|
||||
if (!j.contains(key) || !j[key].is_string())
|
||||
throw std::invalid_argument("INVALID_REQUEST");
|
||||
auto s = j[key].get<std::string>();
|
||||
if (s.empty() || s.size() > limit || s.find('\0') != std::string::npos)
|
||||
throw std::invalid_argument("INVALID_REQUEST");
|
||||
return s;
|
||||
}
|
||||
namespace {
|
||||
int KeyCode(const std::string &key) {
|
||||
if (key.size() == 1 &&
|
||||
((key[0] >= 'A' && key[0] <= 'Z') || (key[0] >= '0' && key[0] <= '9')))
|
||||
return key[0];
|
||||
static const std::map<std::string, int> keys = {{"BACKSPACE", 8},
|
||||
{"TAB", 9},
|
||||
{"ENTER", 13},
|
||||
{"SHIFT", 16},
|
||||
{"CTRL", 17},
|
||||
{"ALT", 18},
|
||||
{"PAUSE", 19},
|
||||
{"CAPS_LOCK", 20},
|
||||
{"ESC", 27},
|
||||
{"SPACE", 32},
|
||||
{"PAGE_UP", 33},
|
||||
{"PAGE_DOWN", 34},
|
||||
{"END", 35},
|
||||
{"HOME", 36},
|
||||
{"LEFT", 37},
|
||||
{"UP", 38},
|
||||
{"RIGHT", 39},
|
||||
{"DOWN", 40},
|
||||
{"INSERT", 45},
|
||||
{"DELETE", 46},
|
||||
{"META", 91},
|
||||
{"F1", 112},
|
||||
{"F2", 113},
|
||||
{"F3", 114},
|
||||
{"F4", 115},
|
||||
{"F5", 116},
|
||||
{"F6", 117},
|
||||
{"F7", 118},
|
||||
{"F8", 119},
|
||||
{"F9", 120},
|
||||
{"F10", 121},
|
||||
{"F11", 122},
|
||||
{"F12", 123},
|
||||
{"SEMICOLON", 186},
|
||||
{"EQUAL", 187},
|
||||
{"COMMA", 188},
|
||||
{"MINUS", 189},
|
||||
{"PERIOD", 190},
|
||||
{"SLASH", 191},
|
||||
{"BACKQUOTE", 192},
|
||||
{"BRACKET_LEFT", 219},
|
||||
{"BACKSLASH", 220},
|
||||
{"BRACKET_RIGHT", 221},
|
||||
{"QUOTE", 222}};
|
||||
auto it = keys.find(key);
|
||||
if (it == keys.end())
|
||||
throw std::invalid_argument("INVALID_KEY");
|
||||
return it->second;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::vector<Step> BuildInputSteps(const json &a, const InputGeometry &o) {
|
||||
const auto type = String(a, "type", 32);
|
||||
std::vector<Step> steps;
|
||||
auto point = [&](const json &p, int delay, int flag = 0, int wheel = 0) {
|
||||
auto coordinate = [&](const char *key, int size, int offset, int span,
|
||||
int source) {
|
||||
if (!p.contains(key) || !p[key].is_number())
|
||||
throw std::invalid_argument("INVALID_COORDINATE");
|
||||
const auto value = p[key].get<double>();
|
||||
if (!std::isfinite(value) || value < 0 || value >= size)
|
||||
throw std::invalid_argument("INVALID_COORDINATE");
|
||||
const auto center = std::min(value + .5, double(size) - .5);
|
||||
return float(std::clamp((offset + center * span / size) / source, 0.0, 1.0));
|
||||
};
|
||||
Step step;
|
||||
step.delay = delay;
|
||||
step.flag = flag;
|
||||
step.wheel = wheel;
|
||||
step.x =
|
||||
coordinate("x", o.width, o.region.x, o.region.width, o.source_width);
|
||||
step.y =
|
||||
coordinate("y", o.height, o.region.y, o.region.height, o.source_height);
|
||||
steps.push_back(std::move(step));
|
||||
};
|
||||
auto key = [&](int code, bool down, int delay) {
|
||||
Step step;
|
||||
step.kind = StepKind::Key;
|
||||
step.key = code;
|
||||
step.down = down;
|
||||
step.delay = delay;
|
||||
steps.push_back(std::move(step));
|
||||
};
|
||||
if (type == "move" || type == "click" || type == "double_click") {
|
||||
Fields(a, type == "move"
|
||||
? std::initializer_list<const char *>{"type", "x", "y",
|
||||
"modifiers"}
|
||||
: std::initializer_list<const char *>{"type", "x", "y",
|
||||
"button", "modifiers"});
|
||||
point(a, 0);
|
||||
if (type != "move") {
|
||||
const auto button =
|
||||
a.contains("button") ? String(a, "button", 8) : "left";
|
||||
const int flag = button == "left" ? 1
|
||||
: button == "right" ? 3
|
||||
: button == "middle" ? 5
|
||||
: -1;
|
||||
if (flag < 0)
|
||||
throw std::invalid_argument("INVALID_BUTTON");
|
||||
point(a, 10, flag);
|
||||
point(a, 40, flag + 1);
|
||||
if (type == "double_click") {
|
||||
point(a, 110, flag);
|
||||
point(a, 140, flag + 1);
|
||||
}
|
||||
}
|
||||
} else if (type == "scroll") {
|
||||
Fields(a, {"type", "x", "y", "direction", "ticks", "modifiers"});
|
||||
const auto direction = String(a, "direction", 8);
|
||||
if (direction != "up" && direction != "down" && direction != "left" &&
|
||||
direction != "right")
|
||||
throw std::invalid_argument("INVALID_DIRECTION");
|
||||
const int ticks = Number(a, "ticks", 1, 10, 1);
|
||||
point(a, 0);
|
||||
point(a, 10, direction == "left" || direction == "right" ? 8 : 7,
|
||||
(direction == "up" || direction == "right" ? 120 : -120) * ticks);
|
||||
} else if (type == "drag") {
|
||||
Fields(a, {"type", "path", "duration_ms", "button", "modifiers"});
|
||||
const auto button = a.contains("button") ? String(a, "button", 8) : "left";
|
||||
const int flag = button == "left" ? 1
|
||||
: button == "right" ? 3
|
||||
: button == "middle" ? 5
|
||||
: -1;
|
||||
if (flag < 0)
|
||||
throw std::invalid_argument("INVALID_BUTTON");
|
||||
const auto &path = a.at("path");
|
||||
if (!path.is_array() || path.size() < 2 || path.size() > 128)
|
||||
throw std::invalid_argument("INVALID_PATH");
|
||||
const int duration = Number(a, "duration_ms", 100, 2000, 500);
|
||||
for (const auto &p : path)
|
||||
Fields(p, {"x", "y"});
|
||||
point(path.front(), 0);
|
||||
point(path.front(), 10, flag);
|
||||
for (size_t i = 1; i < path.size(); ++i)
|
||||
point(path[i], 10 + int(i * duration / (path.size() - 1)));
|
||||
point(path.back(), duration + 30, flag + 1);
|
||||
} else if (type == "key_press") {
|
||||
Fields(a, {"type", "key", "modifiers"});
|
||||
const auto code = KeyCode(String(a, "key", 16));
|
||||
key(code, true, 0);
|
||||
key(code, false, 40);
|
||||
} else if (type == "hotkey") {
|
||||
Fields(a, {"type", "keys"});
|
||||
const auto &keys = a.at("keys");
|
||||
if (!keys.is_array() || keys.size() < 2 || keys.size() > 6)
|
||||
throw std::invalid_argument("INVALID_KEYS");
|
||||
std::set<int> seen;
|
||||
std::vector<int> codes;
|
||||
for (const auto &item : keys) {
|
||||
if (!item.is_string())
|
||||
throw std::invalid_argument("INVALID_KEY");
|
||||
int code = KeyCode(item.get<std::string>());
|
||||
if (!seen.insert(code).second)
|
||||
throw std::invalid_argument("DUPLICATE_KEY");
|
||||
codes.push_back(code);
|
||||
}
|
||||
int delay = 0;
|
||||
for (int code : codes) {
|
||||
key(code, true, delay);
|
||||
delay += 15;
|
||||
}
|
||||
delay += 30;
|
||||
for (auto it = codes.rbegin(); it != codes.rend(); ++it) {
|
||||
key(*it, false, delay);
|
||||
delay += 15;
|
||||
}
|
||||
} else if (type == "type_text") {
|
||||
Fields(a, {"type", "text"});
|
||||
Step step;
|
||||
step.kind = StepKind::Text;
|
||||
step.text = String(a, "text", 16384);
|
||||
steps.push_back(std::move(step));
|
||||
} else
|
||||
throw std::invalid_argument("UNSUPPORTED_ACTION");
|
||||
if (a.contains("modifiers")) {
|
||||
const auto &modifiers = a["modifiers"];
|
||||
if (!modifiers.is_array() || modifiers.size() > 4)
|
||||
throw std::invalid_argument("INVALID_KEYS");
|
||||
std::set<int> seen;
|
||||
std::vector<Step> prefix;
|
||||
for (const auto &item : modifiers) {
|
||||
if (!item.is_string())
|
||||
throw std::invalid_argument("INVALID_KEY");
|
||||
const auto name = item.get<std::string>();
|
||||
if (name != "CTRL" && name != "ALT" && name != "SHIFT" && name != "META")
|
||||
throw std::invalid_argument("INVALID_KEY");
|
||||
const int code = KeyCode(name);
|
||||
if (!seen.insert(code).second ||
|
||||
(type == "key_press" && String(a, "key", 16) == name))
|
||||
throw std::invalid_argument("DUPLICATE_KEY");
|
||||
Step step;
|
||||
step.kind = StepKind::Key;
|
||||
step.key = code;
|
||||
step.down = true;
|
||||
step.delay = int(prefix.size()) * 15;
|
||||
prefix.push_back(step);
|
||||
}
|
||||
const int offset = int(prefix.size()) * 15;
|
||||
for (auto &step : steps)
|
||||
step.delay += offset;
|
||||
steps.insert(steps.begin(), prefix.begin(), prefix.end());
|
||||
int delay = steps.back().delay + 15;
|
||||
for (auto it = prefix.rbegin(); it != prefix.rend(); ++it, delay += 15)
|
||||
key(it->key, false, delay);
|
||||
}
|
||||
return steps;
|
||||
}
|
||||
} // namespace crossdesk::cli::automation
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-11-03
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _AUTOMATION_PROTOCOL_H_
|
||||
#define _AUTOMATION_PROTOCOL_H_
|
||||
|
||||
#include <frame_image.h>
|
||||
#include <initializer_list>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace crossdesk::cli::automation {
|
||||
using nlohmann::json;
|
||||
void Fields(const json &, std::initializer_list<const char *> names);
|
||||
int Number(const json &, const char *key, int low, int high, int fallback);
|
||||
uint64_t Sequence(const json &, const char *key, uint64_t fallback = 0);
|
||||
bool Flag(const json &, const char *key, bool fallback = false);
|
||||
std::string String(const json &, const char *key, size_t limit = 128);
|
||||
|
||||
struct InputGeometry {
|
||||
int width, height, source_width, source_height;
|
||||
ImageRegion region;
|
||||
};
|
||||
enum class StepKind { Pointer, Key, Text };
|
||||
struct Step {
|
||||
StepKind kind = StepKind::Pointer;
|
||||
int delay = 0, key = 0, flag = 0, wheel = 0;
|
||||
float x = 0, y = 0;
|
||||
bool down = false;
|
||||
std::string text;
|
||||
};
|
||||
|
||||
// Pure validation and translation: no session access or input side effects.
|
||||
std::vector<Step> BuildInputSteps(const json &action,
|
||||
const InputGeometry &geometry);
|
||||
} // namespace crossdesk::cli::automation
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,123 @@
|
||||
# Native automation protocol
|
||||
|
||||
`crossdesk-cli agent --stdio --password-fd 3 --video-ws` is the private native adapter used by CrossDesk MCP. It is separate from the existing terminal-oriented `bridge` protocol. This is **not MCP on stdin**: the TypeScript service owns the public MCP transport and tool schemas.
|
||||
|
||||
Optional native arguments are `--server HOST`, `--port PORT`, `--quality PRESET` and `--log-dir DIRECTORY`. The caller creates the log directory. Diagnostic stdout is redirected to stderr; only protocol responses use the preserved output descriptor. Passwords travel over descriptor 3, terminated by newline or EOF, and never appear in JSON requests. `--video-ws` starts an authenticated, native loopback WebSocket listener on an ephemeral port. There is no video file descriptor. Agent mode selects the high-frame-rate preview tier at up to 1920 × 1080 by default; negotiated remote capabilities and network conditions determine the delivered rate. Each process accepts one connection attempt; retry authentication with a new process and credential pipe.
|
||||
|
||||
## Envelope
|
||||
|
||||
One UTF-8 JSON object per line, maximum request 64 KiB and nesting depth 32. Use nonnegative integer request IDs. Responses may complete out of order; correlate by ID.
|
||||
|
||||
```json
|
||||
{"id":1,"method":"hello","protocol":"crossdesk-automation/1"}
|
||||
{"id":1,"ok":true,"result":{"protocol":"crossdesk-automation/1","image_formats":["image/png"],"action_completion":"submitted","human_pause":true,"max_request_bytes":65536,"max_response_bytes":4194304,"operation_cache_entries":128,"operation_cache_ms":300000}}
|
||||
```
|
||||
|
||||
Unknown fields and malformed arguments are rejected. Error responses contain a stable code, retryability and whether input may already have been submitted:
|
||||
|
||||
```json
|
||||
{"id":2,"ok":false,"error":{"code":"CONTROL_DENIED","retryable":false,"side_effect_possible":false}}
|
||||
```
|
||||
|
||||
The output queue is bounded to 64 messages and 8 MiB; a blocked output pipe terminates after 10 seconds without progress. EOF, signals and `exit` disconnect and release held input. A caller that needs responses must keep stdin open until they arrive.
|
||||
|
||||
## Methods
|
||||
|
||||
| Method | Parameters in addition to `id`/`method` |
|
||||
| --- | --- |
|
||||
| `hello` | `protocol: "crossdesk-automation/1"` |
|
||||
| `connect` | `device` (9 digits), `mode: "observe" | "control"`, `display` (0–63), optional `decode_path: "browser" / "minirtc"`, `decoder: "auto" / "software"` |
|
||||
| `status` | none |
|
||||
| `screenshot` | optional `region: {x,y,width,height}`, `max_edge` (64–2048), `after_frame_id`, `generation`, `timeout_ms` (0–5000) |
|
||||
| `video_open` | `ticket`, `view`, `view_generation`, `session`, `origin`, `apps`; requires `--video-ws` and a connected session |
|
||||
| `video_revoke` | optional `view`; omit to revoke all video grants and sockets |
|
||||
| `preview` | optional `max_edge` (64–960), `after_frame_id`, `generation`, `timeout_ms` (0–5000); no region |
|
||||
| `action` | `operation_id`, `observation_id`, `action` |
|
||||
| `control` | `enabled` boolean; cannot override observe-only permission or human pause |
|
||||
| `viewer_control` | `operation: "pause" | "resume" | "disconnect"`; restricted to trusted UI by the MCP adapter |
|
||||
| `terminal_frame` | `columns` (1–160), `rows` (1–60), `colors` (8/24/256), `cell_aspect` (0.2–2); private terminal viewer only |
|
||||
| `display` | `index` (0–63) |
|
||||
| `cancel` | `request_id`; cancels queued/in-flight captures or input sequences |
|
||||
| `disconnect`, `exit` | none; `exit` also ends the process |
|
||||
|
||||
`connect` returns immediately after initiating transport. Poll `status` until `state == "ready"`; this requires a frame from the current display generation. Status includes `transport_state`, `geometry_epoch`, `frame_age_ms`, `source_size`, `control_allowed`, `human_paused`, host platform, displays and transport diagnostics.
|
||||
|
||||
## Live video
|
||||
|
||||
`doctor --json` advertises `automation_video_transport: "loopback-websocket/1"`, `automation_video_protocols: ["h264-annexb/1", "nv12/1"]`, and `automation_video_decoders: ["auto", "software"]`. `hello` repeats these without the `automation_` prefix. The MCP adapter requires the transport capability, so an old pipe-only CLI is rejected. Node spawns only stdin, stdout, stderr, and the credential pipe; live video never enters Node.
|
||||
|
||||
The terminal viewer additionally requires `automation_terminal_protocol: "ansi/1"` in `doctor` and `terminal_protocol: "ansi/1"` in `hello`. `terminal_frame` asynchronously converts the current decoded frame using the existing ANSI quadrant renderer and returns `columns`, `rows`, `lines`, `generation`, `geometry_epoch`, and a nullable `cursor` with zero-based terminal `column`/`row`. Only one render may be in flight; obsolete geometry or frames older than two seconds are rejected. These rendered text rows travel through the private authenticated terminal IPC, never through public MCP tools or model content. The viewer requests at most 8 FPS and waits for terminal writes before requesting another frame. It shares the automation connection; no second media session is opened.
|
||||
|
||||
Status includes `cursor` with normalized full-frame `x`/`y`, `visible`, `generation`, `geometry_epoch`, `width`, and `height`. The native layer updates it after each successfully sent pointer step, including drag steps, using the same ROI-to-source mapping as remote input. It hides on pause/control disable and clears on geometry changes. It is an Agent position indicator, not remote cursor telemetry or execution confirmation; observation PNGs and source video remain unchanged.
|
||||
|
||||
Authenticated video sockets also receive small JSON text messages with `type: "cursor"` and these fields, independently of binary video frames. Pointer updates are coalesced to at most 30 Hz, retained for newly authorized viewers, and deferred under socket backpressure; they require no video ACK. Browser/Apps render a separate transparent overlay with the same aspect-ratio fitting as the video, including when no new video frame arrives.
|
||||
|
||||
`connect.decode_path` defaults to `browser` (compressed H.264), or selects `minirtc` (decoded NV12). `connect.decoder` defaults to `auto` (hardware preferred, software when unavailable); `software` selects the native software decoder. Both paths maintain native screenshot frames with the selected decoder. These choices are fixed for the connection. Status exposes `video` and the reported `native_video_decoder` backend name. PNG encoding runs only when requested. The legacy `preview` image method remains available to private protocol callers but is not used by MCP panels.
|
||||
|
||||
### Authorization and transport
|
||||
|
||||
The trusted adapter generates 24 random bytes as a 32-character base64url `ticket` and sends `video_open` over private JSON-RPC. `view` and `session` are also 32-character base64url IDs; `view_generation` is positive. `origin` is the Node panel's exact `http://127.0.0.1:PORT` origin. `apps` permits an opaque (`null`) or HTTPS MCP Apps origin, but still requires the same one-time ticket. No password is included.
|
||||
|
||||
The response contains `{url, transport, protocol, decoder, expires_in_ms:15000}`. Node supplies the ticket to the authorized UI. The browser opens the returned `ws://127.0.0.1:PORT/api/video` directly and sends `{"ticket":"..."}` as its first text message within 3 seconds. Tickets expire after 15 seconds, are consumed once, and are bound to a view/session plus the native display generation. The native service checks the exact Host and path, RFC 6455 version, and authorized Origin. It does not serve HTTP content or accept remote input commands. Logs are disabled for this listener; tickets never go in URLs or model-visible results.
|
||||
|
||||
The listener uses its own I/O thread, at most 16 WebSocket candidates and 8 authorized viewers. Client messages are limited to 1024 bytes. Revocation, disconnect, native generation changes, or shutdown cancel sockets and invalidate tickets, including partially sent frames. Pause only disables Agent control; viewing continues. The adapter explicitly revokes a removed or replaced view, and terminates the native session if revocation cannot be confirmed. Native processes own ephemeral ports, so panel CSP allows `ws://127.0.0.1:*`, restricted to the literal loopback address; the control HTTP origin remains exact.
|
||||
|
||||
### Binary messages and flow control
|
||||
|
||||
Each binary WebSocket message contains one complete frame, **without the old pipe length prefix**. Integers are big-endian. For `h264-annexb/1`:
|
||||
|
||||
| Offset | Type | Value |
|
||||
| --- | --- | --- |
|
||||
| 0 | u32 | `0x43445631` (`CDV1`) |
|
||||
| 4 | u32 | Display/connection generation |
|
||||
| 8 | u32 | Receive sequence; gaps require a new keyframe |
|
||||
| 12 | u64 | Local monotonic receive timestamp in microseconds |
|
||||
| 20 | u32 | Flags: 1 = IDR keyframe, 0 = delta |
|
||||
| 24 | bytes | Annex B H.264; keyframes include SPS and PPS |
|
||||
|
||||
Access units are capped at 4 MiB. Both the receive tap and the I/O handoff are bounded to 60 frames / 8 MiB. Overflow discards dependent frames until an IDR. After receiving each message the browser sends `{"type":"ack","generation":N,"sequence":N}` directly to the native socket, in order. This acknowledges receipt, not decode, presentation, or remote execution. Per-viewer unacknowledged output is capped at 8 frames; at 1 MiB outstanding, further frames are skipped until space is available (a single keyframe may exceed that threshold). Dropping a dependent picture requires a new keyframe; `{"type":"keyframe"}` requests recovery, rate-limited to once per second.
|
||||
|
||||
For `nv12/1`, magic is `0x43444e31` (`CDN1`). Offsets 4, 8, 12 and 20 remain generation, decoded sequence, local monotonic decoded-frame queue time in microseconds, and flags (always 1). Offsets 24 and 28 are u32 width and height; offset 32 starts tightly packed Y followed by interleaved UV. Dimensions must be even, at most 8192 per axis / 16777216 pixels. Message size must equal `32 + width * height * 3 / 2` (at most 25165856 bytes). Color is limited-range BT.601, matching the existing PNG conversion. Sequence gaps are allowed.
|
||||
|
||||
NV12 retains one latest picture in the I/O handoff and at most one unacknowledged picture per viewer. New pictures are skipped for a slow viewer without blocking others. Each prepared WebSocket message is immutable and shared between recipients, avoiding per-viewer payload copies. There is still a native copy into the WebSocket payload and raw pixel transfer to the browser; this is not GPU zero-copy. A viewer whose oldest outstanding frame remains unacknowledged for 10 seconds is disconnected. Hidden/unloaded pages close their native socket. The browser replaces pending raw pictures and does not decode H.264 in this mode.
|
||||
|
||||
`status.video_output` includes `transport`, `viewers`, I/O handoff `queued` / `queued_bytes`, aggregate `unacknowledged_bytes`, and `dropped` (handoff replacements plus per-viewer congestion skips). These diagnostics contain no video payload. The original receive-tap SPS/PPS and keyframe state remain available for H.264.
|
||||
|
||||
The MiniRTC `Params` structure appends `on_receive_encoded_video`, changing its ABI size. Rebuild consumers and MiniRTC together; zero-initialize `Params`. The callback borrows frame bytes on the receive/decode queue and must not retain the pointer. Returning nonzero requests a remote keyframe. No remote wire-protocol change is required.
|
||||
|
||||
## Images and observations
|
||||
|
||||
Images return `{mime_type:"image/png",data:"<base64>",metadata:{...}}`. Metadata includes `generation`, `geometry_epoch`, `frame_id`, `display_index`, `source_size`, `source_rect`, `image_size` and `frame_age_ms`; only screenshots create `observation_id`. Preview images cannot authorize model input.
|
||||
|
||||
NV12 frames must be tightly packed, even dimensions, at most 8192 per axis and 16 million pixels. Conversion/PNG encoding runs on one worker with no `ControllerSession` calls. The owner loop continues processing cancellation and input release. Screenshots have priority over queued previews. An encoding from an obsolete generation or geometry epoch is discarded. Frames older than two seconds are not used for a new image. PNG limits are 2 MiB for observations and 256 KiB for previews; the encoder reduces dimensions when necessary.
|
||||
|
||||
`after_frame_id` requires `generation`. A screenshot waits up to `timeout_ms` for a newer frame; a preview can return `{unchanged:true, frame_age_ms, stale, generation, frame_id}`. Neither path treats an image as an input acknowledgement.
|
||||
|
||||
`timeout_ms` bounds frame/worker availability, not the subsequent PNG encoding. With `timeout_ms: 0`, a fresh eligible frame is encoded immediately if the worker and queue are idle; otherwise return `FRAME_TIMEOUT` or `BUSY`. Captures of the same priority retain arrival order. Encoding still rejects obsolete geometry and frames that become older than two seconds. Terminal rendering also honors cancellation without sending a second result when the worker finishes.
|
||||
|
||||
## Actions
|
||||
|
||||
All coordinates refer to the immutable returned observation image. The native layer maps them through its source rectangle to normalized remote coordinates, without terminal layout padding. Coordinates must satisfy `0 <= x < image_width` and `0 <= y < image_height`. Observations expire after 30 seconds, retain at most 32 entries, and are invalidated by geometry/display generation changes.
|
||||
|
||||
```json
|
||||
{"id":10,"method":"action","operation_id":"click-1","observation_id":"obs_2_1","action":{"type":"click","x":120,"y":80,"button":"left"}}
|
||||
```
|
||||
|
||||
Supported payloads:
|
||||
|
||||
- `move`: `x`, `y`.
|
||||
- `click`, `double_click`: `x`, `y`, optional `button` (`left`, `right`, `middle`).
|
||||
- `scroll`: `x`, `y`, `direction` (`up`, `down`, `left`, `right`), optional `ticks` (1–10).
|
||||
- `drag`: `path` of 2–128 `{x,y}` points, optional `duration_ms` (100–2000), optional `button` (`left`, `right`, `middle`; default `left`).
|
||||
- `key_press`: `key`; `hotkey`: 2–6 unique `keys`.
|
||||
- `type_text`: `text`, at most 16 KiB UTF-8; requires negotiated remote text-input capability.
|
||||
|
||||
All pointer actions and `key_press` accept optional `modifiers`, an array of at most four unique names from `CTRL`, `ALT`, `SHIFT`, `META`. They are pressed before the action and released afterwards in reverse order; a `key_press` cannot also list its own key as a modifier. `hotkey` already describes the complete chord and does not accept `modifiers`. Punctuation VK names are `SEMICOLON`, `EQUAL`, `COMMA`, `MINUS`, `PERIOD`, `SLASH`, `BACKQUOTE`, `BRACKET_LEFT`, `BACKSLASH`, `BRACKET_RIGHT`, `QUOTE`; these name keys, not layout-independent text.
|
||||
|
||||
Only one input sequence runs at once. Timed steps are advanced by the owner loop; cancellation, disconnect, display changes and pause release held buttons/keys. A failed send is conservatively considered potentially submitted.
|
||||
|
||||
New actions require a current frame no older than two seconds as well as a valid observation. Cached operation lookups remain available with a stalled feed or paused control. Failed key/button releases remain tracked for subsequent cleanup, including after control is disabled; a successful local send still does not prove remote execution. Invalid display indices leave existing observations and input sequences intact.
|
||||
|
||||
Results use `status: submitted | in_progress | failed_before_submit | outcome_unknown`, with `operation_id`, `retryable`, `side_effect_possible` and, for completed actions, `generation`/`after_frame_id`. The same operation ID and arguments retrieve the cached result; different arguments conflict. Retention is bounded to 128 entries, with entries older than five minutes eligible for removal on a new action. Never retry an uncertain effect with a new ID. `submitted` does not claim remote execution.
|
||||
|
||||
The native process trusts its parent. The parent must authenticate panel-only `viewer_control` calls and protect descriptor 3; this private pipe does not isolate arbitrary programs running as the same OS user.
|
||||
@@ -12,7 +12,7 @@
|
||||
| connect | device:9位数字字符串, display:0..63(默认0) | 从专用 password fd 读取至换行/EOF(最多256字节、等待5秒),开始异步注册/连接;ok不是远端已连接 |
|
||||
| status | 无 | 返回当前状态 |
|
||||
| control | enabled:boolean | 显式启停控制;关闭时释放输入 |
|
||||
| input | data:string,最多16KiB | 终端原始按键/UTF-8/bracketed paste/SGR鼠标;只读时 c 开启控制、q/Ctrl+C 退出,d/鼠标点击进入细节,+/-/滚轮缩放,方向键或 i 上、j 左、k 下、l 右平移,f 全景;Ctrl+] 打开本地命令,随后 c 选择返回后的控制状态、q 退出或视图键连续调整画面,Esc/Ctrl+C 手动返回;控制模式中 Ctrl+T/Ctrl+C 传给远端,本地模式中再按 Ctrl+] 手动返回,并在返回控制模式时传递远端 Ctrl+] |
|
||||
| input | data:string,最多16KiB | 终端原始按键/UTF-8/bracketed paste/SGR鼠标;只读时 c 开启控制、q/Ctrl+C 退出,鼠标点击进入细节,+/-/滚轮以鼠标位置缩放,方向键或 i 上、j 左、k 下、l 右平移,f 恢复全景并重置倍率与平移;Ctrl+] 打开本地命令,随后 c 选择返回后的控制状态、q 退出或视图键连续调整画面,Esc/Ctrl+C 手动返回;控制模式中 Ctrl+T/Ctrl+C 传给远端,本地模式中再按 Ctrl+] 手动返回,并在返回控制模式时传递远端 Ctrl+] |
|
||||
| display | index:0..63 | 选择已广告的零起始显示器,提升 generation |
|
||||
| ack | frame_id、generation:非负整数 | 确认宿主消费帧;匹配当前等待帧才开放下一帧 |
|
||||
| disconnect | 无 | 释放输入、结束当前媒体会话 |
|
||||
|
||||
@@ -39,7 +39,7 @@ npx @chunkdash/crossdesk-cli@latest 123456789
|
||||
|
||||
这些是构建与分发目标,不代表均已实机通过。当前完整构建与 npm 运行验证只覆盖 macOS arm64;CLI 主程序与平台 I/O 已通过 Windows x64 与 Linux x64、arm64 的交叉编译检查。其他平台的完整 MiniRTC 链接、实际视频连接、键鼠、终端恢复仍需对应机器验收。
|
||||
|
||||
普通终端使用 ANSI 渲染,按能力选择 TrueColor / 256 色 / 基础色;Kitty 图形协议是可选增强。macOS Terminal、iTerm2、Linux 常用终端、Windows Terminal 和 VS Code 集成终端属于目标场景。SSH 应分配 PTY(`ssh -t`);tmux / screen 使用 ANSI。不支持 `TERM=dumb` 或把交互观看输出重定向到文件;非交互宿主使用 `bridge --stdio`。Alpine / musl 当前没有预编译包。Windows 独立 mintty 窗口可能不提供 Console / ConPTY,应使用 Windows Terminal 承载 Git Bash。
|
||||
普通终端使用 ANSI 渲染,按能力选择 TrueColor / 256 色 / 基础色;Kitty / Sixel 图形协议是可选增强,需终端自身支持并通过能力探测。macOS Terminal、iTerm2、Linux 常用终端、Windows Terminal 和 VS Code 集成终端属于目标场景。SSH 应分配 PTY(`ssh -t`);tmux / screen 使用 ANSI。不支持 `TERM=dumb` 或把交互观看输出重定向到文件;非交互宿主使用 `bridge --stdio`。Alpine / musl 当前没有预编译包。Windows 独立 mintty 窗口可能不提供 Console / ConPTY,应使用 Windows Terminal 承载 Git Bash。
|
||||
|
||||
CI 的 macOS 构建配置指定 `--target_minver=13.0`,但最终必须检查实际产物及依赖。当前开发机的本地二进制最低要求为 **macOS 27.0**,不应将这个本地产物宣称为旧 macOS 通用版。
|
||||
|
||||
@@ -57,7 +57,7 @@ codesign --force --sign - build/macosx/arm64/release/crossdesk-cli
|
||||
npm --prefix apps/cli run pack:npm
|
||||
```
|
||||
|
||||
脚本读取 `apps/cli/package.json` 与 `apps/cli/xmake.lua` 的版本,检查二者一致;确认源码中的 MiniRTC 日志脱敏补丁已应用,再检查当前平台二进制格式、CPU、动态链接依赖,并运行离线 `doctor --json`。macOS 只接受系统动态库,Linux 只接受列明的基础系统运行库,Windows 拒绝未随产品处理的 DLL 和动态 MSVC 运行库。Windows 在含 `dumpbin.exe` 的 Visual Studio 开发者环境中运行打包。脚本预先检查两个输出文件是否存在,并在临时目录中完成两个包后再写入输出目录;写入失败时清理本次已写入的包,已有同名产物不会被覆盖。
|
||||
脚本读取 `apps/cli/package.json` 与 `apps/cli/xmake.lua` 的版本,检查二者一致;检查当前平台二进制格式、CPU、动态链接依赖,并运行离线 `doctor --json`。macOS 只接受系统动态库,Linux 只接受列明的基础系统运行库,Windows 拒绝未随产品处理的 DLL 和动态 MSVC 运行库。Windows 在含 `dumpbin.exe` 的 Visual Studio 开发者环境中运行打包。脚本预先检查两个输出文件是否存在,并在临时目录中完成两个包后再写入输出目录;写入失败时清理本次已写入的包,已有同名产物不会被覆盖。
|
||||
|
||||
默认从 `build/<platform>/<arch>/release/` 读取原生程序,将两个 `.tgz` 写入 `build/npm/`。可指定路径:
|
||||
|
||||
@@ -67,7 +67,7 @@ npm --prefix apps/cli run pack:npm -- --binary /absolute/path/crossdesk-cli --ou
|
||||
|
||||
每台构建机生成当前平台的原生包与相同版本的通用启动包。以 macOS arm64、版本 0.1.0 为例,文件名为 `chunkdash-crossdesk-cli-darwin-arm64-0.1.0.tgz` 和 `chunkdash-crossdesk-cli-0.1.0.tgz`;npm 将 `@scope/name` 的 tarball 文件名写作 `scope-name-version.tgz`。五个 CI artifact 各含一份入口包,最终需要五个平台包和一份入口包,共六个 tarball。
|
||||
|
||||
`apps/cli/npm/platforms.json` 集中定义五个平台;脚本校验平台包名合法且不重复,启动包通过精确版本的 `optionalDependencies` 让 npm 仅安装匹配系统和 CPU 的原生包。生成的六个包都包含作者、源码仓库、主页、问题反馈地址,以及面向 npm 官方注册表的公开发布配置。原生包包含二进制、运行要求、SHA-256,以及当前控制端和已应用修补的 MiniRTC 源码快照。外部 Xmake 依赖的许可证、对应源码和重链接材料需要随正式版本一并提供,参见 [源码来源](source-provenance.md)。源码快照已含 MiniRTC,解包后无需运行 `prepare-deps.sh`;在 `source/` 目录按上述构建说明显式关闭桌面、开启 CLI,再构建 `crossdesk-cli`。源码包仅包含 CLI 及其依赖,不包含桌面或移动应用。
|
||||
`apps/cli/npm/platforms.json` 集中定义五个平台;脚本校验平台包名合法且不重复,启动包通过精确版本的 `optionalDependencies` 让 npm 仅安装匹配系统和 CPU 的原生包。生成的六个包都包含作者、源码仓库、主页、问题反馈地址,以及面向 npm 官方注册表的公开发布配置。原生包包含二进制、运行要求、SHA-256,以及当前控制端和 MiniRTC 源码快照。外部 Xmake 依赖的许可证、对应源码和重链接材料需要随正式版本一并提供,参见 [源码来源](source-provenance.md)。源码快照已含 MiniRTC,解包后无需运行 `prepare-deps.sh`;在 `source/` 目录按上述构建说明显式关闭桌面、开启 CLI,再构建 `crossdesk-cli`。源码包仅包含 CLI 及其依赖,不包含桌面或移动应用。
|
||||
|
||||
`apps/cli/package.json` 的 `private: true` 表示源码目录不是发布产物。只发布打包脚本生成的 tarball;生成的包不带 private 标记。
|
||||
|
||||
|
||||
@@ -7,9 +7,11 @@ CLI 从独立开发目录整合到 CrossDesk,入口、打包脚本、npm 启
|
||||
- `ControllerSession` 的流名称、HostInformation 和输入参考 Android `rtc_bridge.cpp`;身份使用单 Peer 的 `cli` 临时注册协议;终端适配不依赖 JNI、Surface 或 GuiRuntime。
|
||||
- 帧缓存遵守 `MiniRtcVideoFrame` 回调借用契约,直接复制到有界 CPU 存储,不跨回调保留 native 指针。
|
||||
- PreviewSubscription 复用 wire 实现与对端 capability 字段,不新增被控端协议。
|
||||
- MCP 实时面板新增 MiniRTC 的 `on_receive_encoded_video` 回调,转发解码前的完整 H.264 Annex B 帧;回调及关键帧请求由当前子模块源码提供。`Params` 新增字段改变 ABI 大小,CLI 与 MiniRTC 必须一起重建;发布时同步记录包含该改动的子模块提交。
|
||||
- 自动化图像编码复用 MiniRTC 已有的 libyuv 2025.8.14,并通过 Xmake 固定静态 libpng v1.6.59;libpng 的压缩依赖沿用平台 zlib。新增依赖的许可证和对应源码也应纳入正式发行的依赖材料。
|
||||
|
||||
`deps/patches/minirtc-credential-logs.patch` 是 CLI 所需的 MiniRTC 修补:Join、排队 Join、取消 Join、Leave 四个日志点只记录 `@` 前的 ID,不记录密码。`apps/cli/scripts/prepare-deps.sh` 按主仓库记录初始化子模块并幂等应用补丁,不另行校验提交哈希;更新 MiniRTC 时无需修改脚本中的版本。CLI 构建检查补丁已存在。共享子模块中的脱敏同样适用于桌面程序;补丁没有改变鉴权、TLS、TURN 或媒体协议。
|
||||
MiniRTC 已在提交 `b9ee1fa7fe038fdf771f5dec1b1d561d78273f15` 中合入凭据日志脱敏:Join、排队 Join、取消 Join、Leave 四个日志点只记录 `@` 前的 ID,不记录密码。`apps/cli/scripts/prepare-deps.sh` 仅按主仓库记录初始化子模块,不再应用独立补丁;更新 MiniRTC 时只需更新子模块提交。共享子模块中的脱敏同样适用于桌面程序;该修改没有改变鉴权、TLS、TURN 或媒体协议。
|
||||
|
||||
npm 原生包携带 CLI、共享库、构建配置和已修补 MiniRTC 的源码快照,不复制桌面、移动端、测试或本机构建缓存。源码包保留仓库目录结构,在 `source/` 中配置 `--CROSSDESK_BUILD_DESKTOP=n --CROSSDESK_BUILD_CLI=y` 后构建。
|
||||
npm 原生包携带 CLI、共享库、构建配置和 MiniRTC 的源码快照,不复制桌面、移动端、测试或本机构建缓存。源码包保留仓库目录结构,在 `source/` 中配置 `--CROSSDESK_BUILD_DESKTOP=n --CROSSDESK_BUILD_CLI=y` 后构建。
|
||||
|
||||
LICENSE 沿用 CrossDesk GPL-3.0;MiniRTC 子模块保留其 LGPL-3.0 和第三方声明。外部 Xmake 依赖的许可证、对应源码及重链接材料仍需随正式版本提供。
|
||||
|
||||
@@ -11,7 +11,7 @@ xmake build crossdesk-cli
|
||||
xmake run crossdesk-cli doctor --json
|
||||
```
|
||||
|
||||
`prepare-deps.sh` 按主仓库记录的提交初始化 MiniRTC 子模块,并应用凭据日志脱敏补丁,可重复执行。脚本不另行校验提交哈希,升级 MiniRTC 时只需更新子模块提交;如果补丁与新版源码不兼容,准备步骤仍会失败,需要同步调整补丁。构建会检查补丁,缺失时失败。仓库默认开启桌面构建、关闭 CLI。上述参数只构建 CLI;需要同时构建桌面和 CLI 时,将 `CROSSDESK_BUILD_DESKTOP` 设为 `y`。恢复默认桌面配置使用 `xmake f -y --CROSSDESK_BUILD_DESKTOP=y --CROSSDESK_BUILD_CLI=n`。所有命令均从仓库根目录执行。
|
||||
`prepare-deps.sh` 按主仓库记录的提交初始化 MiniRTC 子模块,可重复执行。凭据日志脱敏已合入 MiniRTC,无需额外应用补丁;升级 MiniRTC 时只需更新子模块提交。仓库默认开启桌面构建、关闭 CLI。上述参数只构建 CLI;需要同时构建桌面和 CLI 时,将 `CROSSDESK_BUILD_DESKTOP` 设为 `y`。恢复默认桌面配置使用 `xmake f -y --CROSSDESK_BUILD_DESKTOP=y --CROSSDESK_BUILD_CLI=n`。所有命令均从仓库根目录执行。
|
||||
|
||||
构建产物在 `build/<platform>/<arch>/<mode>/crossdesk-cli`。可把对应目录加入 PATH 或复制可执行文件到自己的 bin 目录。Pi 需要直接启动该可执行文件,不能使用会输出构建日志的 `xmake run` 作为桥接程序。
|
||||
|
||||
@@ -41,7 +41,7 @@ crossdesk-cli 123456789 --server your-server.example --port 9099
|
||||
|
||||
保留 MiniRTC WSS 证书链与主机名验证、TURN 临时凭据下发及 SRTP 协商。未提供跳过证书检查的参数。SRTP 是否实际协商成功通过状态统计呈现;沿用现有 MiniRTC 对旧端的兼容性,不宣称所有旧端必然启用 SRTP。自建证书问题应修复证书/系统信任链。
|
||||
|
||||
## ANSI、Kitty 与 SSH
|
||||
## ANSI、Kitty、Sixel 与 SSH
|
||||
|
||||
ANSI 默认使用四象限字符:每个字符从 2×2 样本选择误差最小的两个颜色和象限形状,比原先上下半方块多一倍水平采样点;每个字符仍只有前景/背景两色。使用 `--ansi-detail half` 可回到上下半方块以兼容字体。缩小时采用面积加权滤波,避免最近邻采样漏掉细线。支持 TrueColor、256 色和基础 8 色,256 色选择实际颜色立方体或灰阶条中更接近的颜色。环境的 COLORTERM/TERM 决定颜色模式;非 UTF-8 locale 使用背景色空格。TERM=dumb、非 TTY 输出明确拒绝交互连接,不输出全屏转义序列。
|
||||
|
||||
@@ -57,13 +57,13 @@ Windows 的可见传统控制台优先读取当前字体尺寸;Windows Termina
|
||||
|
||||
缩放在完整输出边界清理旧画面。上限 240 列、100 画面行;默认顶部预留一行状态栏、底部预留两行快捷键,另预留一列避免自动换行。顶部状态栏用反色区分,左侧为控制模式和连接进度,右侧为画面参数及网络信息;底部两行分别为会话和视图快捷键。视频和等待提示从顶部状态栏下方开始,鼠标坐标按视频区域映射,点击状态栏不会传给远端。按键分组使用统一列宽和左右留白,窄窗口自动减少列数、合并按键组并省略次要参数,累计帧数和输出字节数保留在日志中。控制模式下仅提示本地命令入口及后续操作,进入本地命令模式后才显示平移和缩放按键;终端高度不足时优先收起底部提示行。
|
||||
|
||||
快捷键行使用 `KEYS |`、`VIEW |` 等固定宽度标题,标题和按键组之间用竖线分隔;键名用 `<c>`、`<+/->` 等标签标识。宽窗口中的分组分隔线按列对齐,窄窗口保留标题和完整按键标签,按可用宽度合并或减少分组。
|
||||
快捷键行使用 `KEYS |`、`VIEW |` 等固定宽度标题,标题和按键组之间用竖线分隔;键名用 `<c>`、`<+/->` 等标签标识。`Zoom at mouse` 表示鼠标锚点缩放,`Full view` / `Full` 表示恢复全景,`Pan` 表示平移。只读模式提示滚轮缩放,宽窗口还显示点击进入细节;本地命令模式提示返回、控制切换及键盘视图操作,宽窗口显示鼠标选择锚点和 `=` 放大。宽窗口中的分组分隔线按列对齐,窄窗口保留完整按键标签,优先保留缩放和全景,按可用宽度合并或减少分组。
|
||||
|
||||
顶部状态栏以整数显示实时帧率(例如 `FPS 30`),约每秒按最近统计区间内完整输出到终端的帧数计算;没有新画面输出时更新为 `FPS 0`。该数值是实际提交速率,不是 `--fps` 目标值或终端 GPU 呈现频率;窄窗口优先省略画面参数和网络信息,保留帧率。
|
||||
|
||||
ANSI 在完整桌面缩略图中仍无法保留普通字号的文字。先增加终端列数(缩小终端字体或扩大窗口),阅读局部时用 `--zoom native`。只读全景中点击目标位置可直接进入该区域的细节模式;按 `d` 则以当前中心进入。细节模式依据视口和字符宽高比选择源区域,避免为了完整装入桌面而把文字缩小;ANSI 视野较小,借助方向键平移阅读。`--zoom 1..32` 是相对整桌面的倍率;在细节模式中 `+/-` 是相对原始细节尺寸的倍率。画面缩放完全在本地进行,控制鼠标坐标映射回原始桌面的正确区域。
|
||||
ANSI 在完整桌面缩略图中仍无法保留普通字号的文字。先增加终端列数(缩小终端字体或扩大窗口),阅读局部时用 `--zoom native`。只读时可用 `+/-` 或滚轮以鼠标位置缩放,也可点击目标位置直接进入该区域的细节模式。细节模式依据视口和字符宽高比选择源区域,避免为了完整装入桌面而把文字缩小;ANSI 视野较小,借助方向键平移阅读。`--zoom 1..32` 是相对整桌面的倍率;在细节模式中 `+/-` 是相对原始细节尺寸的倍率。画面缩放完全在本地进行,控制鼠标坐标映射回原始桌面的正确区域。
|
||||
|
||||
auto 模式发送 Kitty 查询与设备属性查询,等待最多 250ms;没有有效 Kitty 回应就用 ANSI。显式 kitty 模式探测失败时报错。tmux/screen 中 auto 使用 ANSI,显式 kitty 报不支持;不修改用户终端配置。
|
||||
auto 模式发送 Kitty 查询与主设备属性查询(DA1),等待最多 250ms;优先选择 Kitty,否则在 DA1 的能力参数中找到 `4` 时选择 Sixel,再回退为 ANSI。显式 `kitty` 或 `sixel` 模式探测失败时报错,不强制向未知终端输出图像。tmux/screen 中 auto 使用 ANSI,显式像素模式报不支持;不修改用户终端配置。
|
||||
|
||||
本地 macOS/Linux 的 Kitty 模式会另用一个 1×1 像素的共享内存对象探测 `t=s`,最多等待 250ms;探测成功后,将未压缩 RGB 写入权限为 0600 的 POSIX 共享内存,仅通过终端字节流发送名字和显示命令。每次使用唯一名字,最多保留一个待确认的共享缓冲区(1920×1080×3 字节)。收到上传成功确认后才显示新图并释放缓冲区;上传报错、确认超过 500ms 或本地分配失败时,本次会话退回内联传输,继续保留旧画面。终端和 CLI 都会清理共享对象,退出时也会释放未确认的对象。SSH 中不尝试共享内存;Windows 暂用内联传输。
|
||||
|
||||
@@ -71,16 +71,26 @@ auto 模式发送 Kitty 查询与设备属性查询,等待最多 250ms;没
|
||||
|
||||
依据终端报告的像素尺寸生成画面(未知时按 10×20 像素字符估算),上限 1920×1080。保留面积滤波、黑边、宽高比和裁剪坐标;原始细节模式按像素视口裁取源区域。共享内存路径使用可复用水平行计算的整数面积权重,避免逐像素重复浮点运算,也不再因 Base64 字节预算额外降采样。没有可靠终端像素尺寸时使用估算值,因此不承诺显示设备的物理像素一一对应。内联压缩结果超出单次输出预算时仍逐级降低尺寸;高熵视频的实际帧率可能低于目标。RGB 传输和压缩不丢失像素,不代表面积采样或上游视频编码无损。
|
||||
|
||||
Sixel 使用终端字节流传图,无需额外图像库或临时图片文件:每六行像素按颜色平面编码,使用重复压缩。每帧最多 248 色(RGB 色立方体加灰阶),复杂画面超过约 1.9MB 时逐级降低到 43 色、16 色,必要时降到 4 级灰阶,以保留原有 2MiB 单消息上限。Sixel 有调色板量化损失;它没有 Kitty 的图像缩放 placement,因此依据字符像素尺寸限制画面行列,使图像保持在 1920×1080 内,缩放、细节模式和鼠标映射使用相同视口。每帧覆盖整个图像区域(包括黑边),最后不足六行的部分不会绘制到状态栏;通过保存/恢复 DECSDM 模式使图像从视频区光标位置开始,退出时恢复。未收到像素尺寸时按 10×20 字符估算,实际对齐仍取决于终端尺寸报告。高熵画面或慢速 SSH 建议先用 `--fps 8`。
|
||||
|
||||
```sh
|
||||
crossdesk-cli 123456789 --renderer sixel --fps 8
|
||||
```
|
||||
|
||||
CLI 支持 Sixel 不等于 Xshell 必然支持。查阅的 [Xshell 官方完整功能列表](https://www.netsarang.com/en/xshell-all-features/) 与 [更新记录](https://www.netsarang.com/en/xshell-update-history/) 尚未提供 Sixel 支持说明;以终端实际能力回应及显示测试为准,不能仅修改 `TERM` 就获得像素图像支持。无能力回应时可继续使用 `--renderer ansi --zoom native`。Sixel 协议与 DA1 能力位依据 [XTerm 控制序列文档](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Sixel-Graphics)。
|
||||
|
||||
```sh
|
||||
ssh -t your-server
|
||||
crossdesk-cli 123456789 --renderer ansi --fps 8
|
||||
```
|
||||
|
||||
用户本地终端负责显示 SSH 字节流。ANSI、Kitty over SSH 和 SSH+tmux 的实机互通尚未验证。无 GUI 指主控客户端;被控端仍需要可捕获的图形会话。
|
||||
用户本地终端负责显示 SSH 字节流。ANSI、Kitty / Sixel over SSH 和 SSH+tmux 的实机互通尚未验证。无 GUI 指主控客户端;被控端仍需要可捕获的图形会话。
|
||||
|
||||
## 预览与背压
|
||||
|
||||
`--fps 1..30` 是本地输出目标。交互 CLI 的 ANSI、内联 Kitty 和共享内存 Kitty 默认均为 30 FPS;共享内存失败回退时保持该目标。bridge 仍按宿主协议默认使用 8 FPS。
|
||||
`--fps 1..30` 是本地输出目标。交互 CLI 的 ANSI、Sixel、内联 Kitty 和共享内存 Kitty 默认均为 30 FPS;共享内存失败回退时保持该目标。bridge 仍按宿主协议默认使用 8 FPS。
|
||||
|
||||
独立 CLI 与 bridge 的远端预览统一请求清晰度优先(保持分辨率),包括默认 30 FPS、显式 `--fps` 和自动切换预览帧率。提高帧率不会将编码策略切回帧率优先;实际分辨率仍受画质上限、源画面尺寸、被控端支持及编码/传输条件限制。
|
||||
|
||||
未显式指定 `--fps` 时,远端预览从 30 FPS 开始,根据完整一帧从转换开始到输出完成的耗时自动选择 8/15/30 FPS 请求;共享内存的上传确认等待也计入,等待源帧的空闲时间不计入。使用平滑估计、不同升降档阈值和至少 8 个新帧样本,持续变慢 2 秒才降档、持续恢复 4 秒才升档,保留供帧余量。15 FPS 仅在对端声明支持时使用;预览未获接受时不追加自动调节。视口变化、显示器切换和共享内存回退会重置耗时估计。显式 `--fps` 固定本地目标和对应的远端预览请求,关闭自动调节;例如 `--fps 30` 可用于与自动策略对比。
|
||||
|
||||
@@ -99,7 +109,7 @@ crossdesk-cli 123456789 --renderer ansi --fps 8
|
||||
|
||||
媒体回调仅同步复制借用的 NV12 数据,校验尺寸、容量、native 描述符与 stride;不保留借用指针。上限单帧 16,777,216 像素、每边 8192、偶数尺寸。最多一个待转换帧,替换前先完成校验与复制,无效帧不会挤掉已有画面;播放器另保留最近转换的一帧用于静止桌面的本地放大/重绘,并在接收新帧时回收上一帧缓冲区供后续复制使用。只丢弃解码后图像。显示器切换/关闭提升 generation,旧帧失效。
|
||||
|
||||
转换与输出在拥有会话的事件循环,媒体回调独立。输出至多一个完整消息(2MiB),按令牌预算分块非阻塞写入,突发额度上限仍为 1MiB。本地交互 ANSI 和 Kitty 使用 8MiB/s;存在 `SSH_CONNECTION`、`SSH_CLIENT` 或 `SSH_TTY` 时,两者保持 1MiB/s,Pi 桥接始终保持 1MiB/s。不能开始新画面时保留最新解码帧。ANSI 差分按终端实际使用的颜色与字形比较,忽略量化后相同的颜色和空格不可见的前景色;同一行可输出多个变化片段,并按光标定位、颜色切换和字符的实际字节成本合并短间隔。差分基准只在整帧输出完成后提交。Pi 桥接另要求消费帧 ACK,最多一帧等待 ACK,输入映射只在对应 ACK 后切换到新网格。状态栏优先在完整画面输出之间更新,避免大图持续刷新时状态饥饿;可写事件唤醒短写重试,仍遵守字节预算。控制事件至多 64 条,协议输入 64KiB/嵌套 32 层,输入粘贴缓冲 32KiB,文本单次最多 16KiB。
|
||||
转换与输出在拥有会话的事件循环,媒体回调独立。输出至多一个完整消息(2MiB),按令牌预算分块非阻塞写入,突发额度上限仍为 1MiB。本地交互 ANSI、Kitty 和 Sixel 使用 8MiB/s;存在 `SSH_CONNECTION`、`SSH_CLIENT` 或 `SSH_TTY` 时,三者保持 1MiB/s,Pi 桥接始终保持 1MiB/s。不能开始新画面时保留最新解码帧。ANSI 差分按终端实际使用的颜色与字形比较,忽略量化后相同的颜色和空格不可见的前景色;同一行可输出多个变化片段,并按光标定位、颜色切换和字符的实际字节成本合并短间隔。差分基准只在整帧输出完成后提交。Pi 桥接另要求消费帧 ACK,最多一帧等待 ACK,输入映射只在对应 ACK 后切换到新网格。状态栏优先在完整画面输出之间更新,避免大图持续刷新时状态饥饿;可写事件唤醒短写重试,仍遵守字节预算。控制事件至多 64 条,协议输入 64KiB/嵌套 32 层,输入粘贴缓冲 32KiB,文本单次最多 16KiB。
|
||||
|
||||
测量像素渲染帧率时使用 release 构建;debug 的 `-O0` 面积采样开销明显更高。共享内存中的像素不经过终端字节预算,但仍受转换和终端消费速度限制;8MiB/s 是内联路径的输出上限,不是帧率保证。解码、像素转换、压缩、终端消费速度和帧大小都会影响结果。退出时,未完成画面最多继续写入 2s;取消图像传输及恢复屏幕的短写最多另重试 500ms。POSIX 立即恢复 termios 并清理输入,不等待输出排空;如果终端始终不消费输出,屏幕清理只能尽力发送。
|
||||
|
||||
@@ -117,12 +127,13 @@ macOS 自带 Terminal 还需要在当前窗口勾选“显示 → 允许鼠标
|
||||
| 退出 CLI | `q` 或 `Ctrl+C` | `Ctrl+]`,再按 `q` |
|
||||
| 进入本地命令模式 | `?` 或 `Ctrl+]` | `Ctrl+]` |
|
||||
| 放大/缩小 | `+` / `=` / `-` | `Ctrl+]`,再按对应键 |
|
||||
| 原始细节/完整画面 | `d`(detail)/ `f`(fit),相邻字母键 | `Ctrl+]`,再按对应键 |
|
||||
| 恢复完整画面 | `f`,重置倍率、平移和细节模式 | `Ctrl+]`,再按 `f` |
|
||||
| 鼠标查看 | 滚轮以鼠标位置缩放;点击进入原始细节 | 进入本地命令模式后仅更新缩放锚点,使用缩放键调整 |
|
||||
| 平移 | 方向键或 `i` 上、`j` 左、`k` 下、`l` 右 | `Ctrl+]`,再按对应键 |
|
||||
|
||||
进入本地命令模式时暂停远端输入,并释放已记录按键/鼠标。执行缩放、平移、`c` 或未知命令后均保持此模式;`c` 切换手动返回后是否启用远端控制,状态栏显示 `CONTROL PAUSED` 或 `READ ONLY`。`Esc` 或 `Ctrl+C` 手动返回所选模式;`q` 退出 CLI。再次按 `Ctrl+]` 也会返回,并在所选模式为控制时向远端发送一次 `Ctrl+]`。命令模式中的鼠标事件被忽略,bracketed paste 内容被丢弃,均不会退出模式或触发快捷键。窗口失焦或输入缓冲区溢出时继续保持本地命令模式,但将返回模式设为只读。控制模式下普通 `c/q/?`、`Ctrl+T`、`Ctrl+C` 和单独 `Esc` 均传给远端。目前不支持重绑定。
|
||||
进入本地命令模式时暂停远端输入,并释放已记录按键/鼠标。执行缩放、平移、`c` 或未知命令后均保持此模式;`c` 切换手动返回后是否启用远端控制,状态栏显示 `CONTROL PAUSED` 或 `READ ONLY`。`Esc` 或 `Ctrl+C` 手动返回所选模式;`q` 退出 CLI。再次按 `Ctrl+]` 也会返回,并在所选模式为控制时向远端发送一次 `Ctrl+]`。命令模式中的鼠标事件仅更新本地缩放锚点,不执行点击、拖动或滚轮操作;bracketed paste 内容被丢弃,均不会退出模式或触发快捷键。窗口失焦或输入缓冲区溢出时继续保持本地命令模式,但将返回模式设为只读。控制模式下普通 `c/q/?`、`Ctrl+T`、`Ctrl+C` 和单独 `Esc` 均传给远端。目前不支持重绑定。
|
||||
|
||||
只读时,`d` 或在画面中单击进入该位置的原始细节模式;`+` / `=` 放大、`-` 缩小(1~32倍),滚轮以鼠标指向为中心调整倍率。方向键或 `i/j/k/l` 分别向上/左/下/右平移,`f` 恢复完整画面。黑边点击被忽略。这些按键和鼠标在控制模式下仍传给远端;进入本地命令模式后可连续调整视图,直至手动返回。放大、平移和只读鼠标定位不会向远端发送操作,也适用于静止画面。
|
||||
只读时,在画面中单击进入该位置的原始细节模式;`+` / `=` 放大、`-` 缩小(1~32倍)。缩放键与滚轮均以鼠标位置为锚点,尽量保持鼠标下的画面内容在原位置;到达桌面边缘时限制裁剪范围。没有收到鼠标位置,或鼠标位于黑边、状态栏时,缩放键沿用当前中心;窗口失焦会清除鼠标位置。方向键或 `i/j/k/l` 分别向上/左/下/右平移,`f` 恢复完整画面并重置倍率、平移和细节模式。黑边点击被忽略。这些按键和鼠标在控制模式下仍传给远端;进入本地命令模式后可移动鼠标选择锚点并连续按缩放键调整视图,直至手动返回。放大、平移和只读鼠标定位不会向远端发送操作,也适用于静止画面。
|
||||
|
||||
支持 SGR 和传统 `CSI M` 鼠标报告,包括移动、左右中键、拖动和滚轮;完整缓冲跨读取分片的传统报告,坐标字节不会作为按键执行。启动时依次启用普通、拖动和全移动报告,兼容只支持部分跟踪模式的终端;退出时关闭这些模式。传统报告的坐标上限为 223,较大窗口应使用支持 SGR 的终端。协议格式依据 [XTerm 鼠标协议](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Mouse-Tracking)。
|
||||
|
||||
@@ -145,7 +156,7 @@ export CROSSDESK_CLI="$PWD/build/macosx/arm64/release/crossdesk-cli"
|
||||
pi -e ./apps/cli/plugins/pi/index.ts
|
||||
```
|
||||
|
||||
在 Pi 输入 `/crossdesk` 或 `/crossdesk 123456789`。先输入 ID,再在自定义遮蔽组件输入密码。扩展使用独立 fd 3 写密码,随后关闭;不写配置、聊天、工具结果或模型上下文。查看组件与独立 CLI 使用相同的 `Ctrl+]` 命令前缀和上述模式按键,由 bridge 统一解析。插件默认请求 high 预览并显示 ANSI 象限字符行,支持同样的只读细节/放大/平移键,颜色深度依据终端环境回退,由 Pi 自身布局和刷新;不直接占用 TTY,不发送 Kitty 图形或自行打开鼠标报告。
|
||||
在 Pi 输入 `/crossdesk` 或 `/crossdesk 123456789`。先输入 ID,再在自定义遮蔽组件输入密码。扩展使用独立 fd 3 写密码,随后关闭;不写配置、聊天、工具结果或模型上下文。查看组件与独立 CLI 使用相同的 `Ctrl+]` 命令前缀和上述模式按键,由 bridge 统一解析。插件默认请求 high 预览并显示 ANSI 象限字符行,支持同样的缩放、全景和平移键,颜色深度依据终端环境回退,由 Pi 自身布局和刷新;不直接占用 TTY,不发送 Kitty 图形或自行打开鼠标报告。
|
||||
|
||||
Pi 扩展读取宿主的字符尺寸,并通过宿主终端写入/输入监听接口查询字符尺寸或完整文本区尺寸,每两秒及窗口变化时刷新;尺寸回复不会传给远端。得到的宽高比通过 bridge 的 `viewport.cell_aspect` 传入,比例变化时重绘静止画面,并丢弃仍在途的旧比例帧。自动检测不可用或不准确时,可在启动 Pi 前设置环境变量 `CROSSDESK_CELL_ASPECT=0.45`(宽/高,允许 0.2~2;`auto` 恢复自动检测)。视图关闭或 bridge 停止时移除查询定时器和输入监听。
|
||||
|
||||
@@ -169,9 +180,9 @@ Pi 扩展读取宿主的字符尺寸,并通过宿主终端写入/输入监
|
||||
- 日志在进程创建的私有临时目录(POSIX `/tmp/crossdesk-cli-*`、Windows `%TEMP%\crossdesk-cli-*`)。交互连接的摘要在 client.log,包含收到的终端鼠标报告数 `mouse_reports`、其中的传统报告数 `mouse_legacy_reports`、成功交给传输层的鼠标事件数 `mouse_forwarded`、发送失败数 `mouse_send_failures`。一次点击报告可能生成定位和按键两条事件,所以发送数可能大于报告数;发送成功不表示被控端已执行。transport.log 只记录白名单中的 ICE/DTLS 状态及本地控制开关,不记录密码、SDP 或原始信令。使用完可删除。错误退出信息在恢复终端后显示,并给出本次日志目录。
|
||||
- `connection_failed` 表示连接建立失败;`connection_lost` 表示连接成功后传输失效。xmake 随后的 `execv ... failed(1)` 表示程序以错误码 1 退出,本身不代表进程崩溃。遇到控制失效时,对照 transport.log 中的控制开关和连接状态;`mouse_reports` 为 0 表示终端没有向 CLI 提供可识别的鼠标事件。报告数增长但 `mouse_forwarded` 为 0 时,检查是否仍为只读/本地命令模式、是否已有画面,以及点击是否落在视频区域;`mouse_send_failures` 增长时检查连接状态。
|
||||
- 如果整个 macOS Terminal 窗口退出且 CLI 记录 `output closed`,还应检查系统 DiagnosticReports 中的 Terminal 报告。已观察到 Terminal 2.15 在创建标签页并调整窗口尺寸时发生 `TTLogicalScreen expandUnicharOffsetCacheToLine:` 行缓存越界;这与 CLI 的 `connection_failed` 不同。临时可在独立窗口运行,避开该标签页操作,不能据此认为 Terminal 缺陷已修复。
|
||||
- `doctor --json` 是环境报告,不会联网登录、主动探测 Kitty 或证明远端可连接。 `input_shortcuts=local_commands_v2` 表示支持本文的持续本地命令模式;启动日志同时记录实际执行路径,以便区分不同工程的构建产物。
|
||||
- `doctor --json` 是环境报告,不会联网登录、主动探测 Kitty / Sixel 或证明远端可连接。 `input_shortcuts=local_commands_v2` 表示支持本文的持续本地命令模式;启动日志同时记录实际执行路径,以便区分不同工程的构建产物。
|
||||
- 画面比例异常时查看 `client.log` 摘要的 `terminal_geometry`:`cell_aspect` 为实际采用的字符宽高比,`reported` 表示收到有效的终端尺寸报告,`manual_aspect` 表示使用了手动覆盖。`doctor` 中的比例和像素尺寸只反映系统 TTY 信息,不主动查询终端,可能与交互连接自动检测后的值不同。
|
||||
|
||||
第一轮无音频播放或音频订阅 API,SSH 服务器上的声音不会传回本地。无后台守护进程、共享媒体 Broker、IP 直连、MCP 或自动截图。其他宿主可以使用 [私有桥接](bridge-protocol.md),没有可靠自定义 UI 时打开独立终端或给出播放器命令。
|
||||
终端模式无音频播放或音频订阅 API,SSH 服务器上的声音不会传回本地。无后台守护进程、共享媒体 Broker 或 IP 直连。终端宿主可以使用 [私有桥接](bridge-protocol.md),没有可靠自定义 UI 时打开独立终端。Agent 的 PNG 截图和结构化操作由独立的 [自动化模式](automation-protocol.md) 和 [MCP 服务](../../mcp/README.md) 提供。
|
||||
|
||||
每个播放器/桥接进程只拥有自己的一个媒体会话,不向模型消息发送视频。
|
||||
|
||||
@@ -18,16 +18,23 @@
|
||||
- Release 构建、实际二进制 bridge/PTY 回归通过。ASan/UBSan 检查覆盖原有会话和渲染行为,以及无效帧保留、NV12 缓冲复用、预览参数去重、ACK 状态、重复断开和重连网络统计复位。
|
||||
- 生产 bridge 配合工程外假传输验证静止画面重绘、细节模式、质量切换、窗口变化、无效配置原子性和帧 ACK 背压,通过。
|
||||
- ANSI 渲染前后对比覆盖 8/24/256 色、UTF-8/ASCII、half/quadrant、全景/细节模式,输出校验值一致。在本机 240×100 网格连续执行 200 次差分生成,静止网格约 447.4 ms → 3.6 ms,单行变化约 422.8 ms → 9.5 ms。该结果只衡量差分文本生成,不包含视频解码、图像缩放和终端显示时间。
|
||||
- npm 真实打包、离线安装及源码包重建通过;工程外故障注入确认已有产物保留、第二个包生成失败时无半套输出、缺失脱敏补丁时拒绝打包。
|
||||
- npm 真实打包、离线安装及源码包重建通过;工程外故障注入确认已有产物保留、第二个包生成失败时无半套输出。
|
||||
|
||||
新增回归代码、基准、假传输和验证产物均位于工程外临时目录,未向工程或子模块添加测试。此次完整构建与运行验证覆盖 macOS arm64,未重新进行 Linux/Windows 实机验证。
|
||||
|
||||
## 控制按键调整(2026-10-30)
|
||||
## 鼠标锚点缩放与快捷键提示(2026-11-02)
|
||||
|
||||
- 移除 `d` 本地快捷键,保留鼠标点击进入细节及 `--zoom native`。`f` 恢复完整桌面并重置倍率、平移和细节模式;`+/-`、`=` 和只读滚轮使用鼠标锚点缩放。
|
||||
- 工程外 ASan/UBSan 检查通过 900 次缩放几何检查,覆盖 ASCII、ANSI half/quadrant、Kitty/Sixel 视口、全景/细节、连续输入、边缘限制、SGR/传统鼠标报告、失焦与模式切换;确认 `d` 不再改变本地视图,控制模式仍转发普通字母。
|
||||
- 四种控制/本地模式组合、1~240 列共 960 组状态栏布局检查通过,覆盖完整按键标签、缩放/全景提示及模式可用操作。Pi 提示通过 Node.js 语法检查;没有进行真实 Pi TUI 或远端设备连接验证。
|
||||
- 新增验证代码及产物均保存在工程外临时目录。
|
||||
|
||||
## 控制按键调整(2026-10-30,历史记录)
|
||||
|
||||
- 当前桌面与 CLI 同时启用的 debug 配置下,`xmake build crossdesk-cli`、`xmake run crossdesk-cli doctor --json` 和命令帮助检查通过。
|
||||
- 工程外 ASan/UBSan 检查覆盖本地命令前缀、只读快捷键、远端 Ctrl+T/Ctrl+C/Ctrl+]、鼠标释放、取消命令、粘贴隔离、UTF-8/CSI 分片、失焦与退出;原有会话/渲染检查通过。
|
||||
- 平移字母键采用 `i` 上、`j` 左、`k` 下、`l` 右;工程外 ASan/UBSan 检查确认四个方向与普通/应用模式方向键一致,控制模式中普通字母仍传给远端。
|
||||
- 细节/全景采用相邻的 `d`/`f`;工程外 ASan/UBSan 检查确认细节保留中心、全景复位缩放与中心、前缀命令和鼠标点击行为正确,控制模式中普通字母仍传给远端。
|
||||
- 当时细节/全景采用相邻的 `d`/`f`(`d` 已于 2026-11-02 移除);工程外 ASan/UBSan 检查确认细节保留中心、全景复位缩放与中心、前缀命令和鼠标点击行为正确,控制模式中普通字母仍传给远端。
|
||||
- 实际二进制 bridge 验证命令状态、错误恢复和退出响应;生产 view 配合假传输的 PTY 检查验证快捷键、模式提示、隐藏密码和终端恢复。实际二进制的 bridge 与 PTY 回归同样通过。
|
||||
- Pi 1.4.0 SDK 严格类型检查和模拟组件运行通过,覆盖控制按键转发、命令提示、取消、缩放、退出与 bridge 启动失败后的关闭操作。
|
||||
|
||||
@@ -132,8 +139,16 @@
|
||||
- 暂停 PTY 读取 1.3s,并在暂停期间发送退出命令,恢复读取后完整帧计数与提交计数一致,约 1.17s 内退出,备用屏幕、termios 和非阻塞标志恢复。没有连接真实远端,也未验证真实终端的 GPU 绘制、SSH 或 tmux 实际显示;上述帧率为合成源的完整输出接收率。
|
||||
- 额外检查源码、脚本、数据和产物均位于工程外系统临时目录,未新增或修改工程内测试文件/用例。
|
||||
|
||||
## Sixel 图形输出(2026-11-02)
|
||||
|
||||
- 新增 `--renderer sixel`;auto 按 Kitty → Sixel → ANSI 选择,Sixel 通过 DA1 能力参数 `4` 探测。显式像素模式没有有效回应或处于 tmux/screen 时拒绝启动;`doctor --json` 增加 `sixel=probe_on_view`,不主动探测。
|
||||
- macOS arm64 CLI debug 构建、帮助信息和 `doctor --json` 通过。工程外 ASan/UBSan 检查覆盖能力回复解析、20,000 组随机尺寸的视口限制及重复计算一致性、7 组合成 NV12 图像(纯黑、纯白、渐变、彩色、部分六行、裁剪缩放和大视口)。
|
||||
- 工程外独立 Sixel 解码器验证完整像素覆盖、颜色量化误差、图像边界和光标保存/恢复;1920×1080 高熵样本触发 4 级灰阶回退,输出约 1.30MB,保持原尺寸且低于单消息上限。
|
||||
- 生产 CLI/终端 I/O 配合工程外假会话的 11 组 POSIX PTY 检查通过:Kitty 优先、Sixel 自动及显式选择、分片 DA1 回应、错误/无回应回退、显式模式失败、tmux/screen 限制、窗口变化和细节缩放;SSH 字节预算保持 1MiB/s,退出后 termios 与备用屏幕恢复,Sixel 路径发送 DECSDM 恢复命令。
|
||||
- 未连接真实远端,未进行 Xshell、其他 Sixel 终端或真实 SSH 显示验收,不保证任何 Xshell 版本支持该协议。额外测试源码、脚本、数据及产物均保存在工程外系统临时目录,未新增或修改工程内测试文件/用例。
|
||||
|
||||
## 实测边界
|
||||
|
||||
本地检查不涵盖 Linux / Windows 完整构建、跨平台远程控制、Kitty/SSH 实际终端显示、真实 Pi TUI 或长时间资源占用。五个平台 CI 配置与 npm 包名均不代表已发布或已验收。
|
||||
本地检查不涵盖 Linux / Windows 完整构建、跨平台远程控制、Kitty/Sixel/SSH 实际终端显示、真实 Pi TUI 或长时间资源占用。五个平台 CI 配置与 npm 包名均不代表已发布或已验收。
|
||||
|
||||
CLI 需要支持 `cli` 临时注册的服务端。`doctor --json` 仅报告本机环境,不会联网登录或证明远端可连接。
|
||||
|
||||
+61
-21
@@ -1,4 +1,5 @@
|
||||
#include "terminal_io.h"
|
||||
#include "agent.h"
|
||||
#include <algorithm>
|
||||
#include <cerrno>
|
||||
#include <clocale>
|
||||
@@ -143,6 +144,7 @@ private:
|
||||
};
|
||||
struct Tty {
|
||||
int out;
|
||||
bool sixel = false;
|
||||
RawTerminal terminal;
|
||||
Tty(int input, int output) : out(output), terminal(input, output) {
|
||||
// Fall back to button/drag reporting if all-motion tracking is unsupported.
|
||||
@@ -153,6 +155,7 @@ struct Tty {
|
||||
~Tty() {
|
||||
// Cancel any incomplete string/control sequence before restoration.
|
||||
std::string end = "\030\033\\" + std::string(kKittyDelete) +
|
||||
(sixel ? "\033[?80r" : "") +
|
||||
"\033[0m\033[?1003l\033[?1002l\033[?1000l"
|
||||
"\033[?1006l\033[?1004l\033[?2004l\033["
|
||||
"?7h\033[?25h\033[?1049l";
|
||||
@@ -332,9 +335,9 @@ int View(SessionOptions options, const std::string &device,
|
||||
return 130;
|
||||
Tty tty(0, fd);
|
||||
Output output(fd);
|
||||
bool kitty = false;
|
||||
bool kitty = false, sixel = false;
|
||||
if (renderer != "ansi" && Env("TMUX").empty() && Env("STY").empty()) {
|
||||
output.Put(std::string(kKittyQuery) + "\033[c");
|
||||
output.Put((renderer == "sixel" ? std::string{} : kKittyQuery) + "\033[c");
|
||||
std::string reply;
|
||||
auto until = Clock::now() + std::chrono::milliseconds(250);
|
||||
while (Clock::now() < until && !interrupted) {
|
||||
@@ -345,11 +348,21 @@ int View(SessionOptions options, const std::string &device,
|
||||
if (n > 0 && reply.size() < 8192)
|
||||
reply.append(b, n);
|
||||
}
|
||||
kitty = KittyReply(reply);
|
||||
kitty = renderer != "sixel" && KittyReply(reply);
|
||||
sixel = renderer != "kitty" && !kitty && SixelReply(reply);
|
||||
}
|
||||
if (renderer == "kitty" && !kitty)
|
||||
throw std::runtime_error(
|
||||
"Kitty probe failed or multiplexer active; use --renderer ansi");
|
||||
if (renderer == "sixel" && !sixel)
|
||||
throw std::runtime_error(
|
||||
"Sixel probe failed or multiplexer active; use --renderer ansi");
|
||||
if (sixel) {
|
||||
// DECSDM reset places graphics at the cursor. Save the user's setting
|
||||
// and restore it on exit, including interrupted image writes.
|
||||
output.Put("\033[?80s\033[?80l");
|
||||
tty.sixel = true;
|
||||
}
|
||||
// Both pixel frames and large true-colour character grids need local
|
||||
// throughput. Keep SSH conservative and retain the one-message/burst limits
|
||||
// and nonblocking writes so slow terminals still apply backpressure.
|
||||
@@ -520,10 +533,12 @@ int View(SessionOptions options, const std::string &device,
|
||||
auto v = Size(&status_rows, &input.terminal_metrics, cell_aspect);
|
||||
v.detail = detail;
|
||||
v.native = input.native;
|
||||
v.pixel_mode = kitty;
|
||||
v.pixel_mode = kitty || sixel;
|
||||
v.zoom = input.zoom;
|
||||
v.center_x = input.center_x;
|
||||
v.center_y = input.center_y;
|
||||
if (sixel)
|
||||
v = SixelViewport(v);
|
||||
if (input.view_revision != view_revision) {
|
||||
view_revision = input.view_revision;
|
||||
redraw = true;
|
||||
@@ -591,9 +606,10 @@ int View(SessionOptions options, const std::string &device,
|
||||
bar.command_mode = input.command_mode;
|
||||
bar.fps = "FPS " + std::to_string(output_fps);
|
||||
bar.view = std::string(kitty ? "KITTY"
|
||||
: sixel ? "SIXEL"
|
||||
: detail == AnsiDetail::Quadrant ? "QUAD"
|
||||
: "HALF") +
|
||||
(input.native ? " | Detail x" : " | Fit x") +
|
||||
(input.native ? " | Detail x" : " | Zoom x") +
|
||||
std::to_string(int(input.zoom)) +
|
||||
(last_frame ? " | " + std::to_string(last_frame->width) + "x" +
|
||||
std::to_string(last_frame->height)
|
||||
@@ -651,15 +667,15 @@ int View(SessionOptions options, const std::string &device,
|
||||
now - last_frame->queued)
|
||||
.count();
|
||||
}
|
||||
pending = kitty ? Layout(*last_frame, v) : Convert(*last_frame, v);
|
||||
// Clear waiting text and Kitty margins without erasing the status bars.
|
||||
pending = v.pixel_mode ? Layout(*last_frame, v) : Convert(*last_frame, v);
|
||||
// Clear waiting text and image margins without erasing the status bars.
|
||||
std::string clear;
|
||||
if (waiting_visible)
|
||||
for (int row = 0; row < v.rows; ++row)
|
||||
clear += "\033[" + std::to_string(v.row_offset + row + 1) +
|
||||
";1H\033[0m\033[2K";
|
||||
if (shared_enabled) {
|
||||
const auto pixels = KittyPixels(*last_frame, v);
|
||||
const auto pixels = ImagePixels(*last_frame, v);
|
||||
if (shared_image.Assign(pixels.rgb.data(), pixels.rgb.size())) {
|
||||
output.Put(KittySharedUpload(shared_image.Name(), pixels.width,
|
||||
pixels.height, submitted % 2 != 0));
|
||||
@@ -673,7 +689,8 @@ int View(SessionOptions options, const std::string &device,
|
||||
if (fresh && !frame_started)
|
||||
frame_started = Clock::now();
|
||||
output.Put(clear + (kitty ? Kitty(*last_frame, v, submitted % 2 != 0)
|
||||
: Difference(*pending, displayed)));
|
||||
: sixel ? Sixel(*last_frame, v)
|
||||
: Difference(*pending, displayed)));
|
||||
waiting_visible = false;
|
||||
pending_frame = true;
|
||||
}
|
||||
@@ -729,7 +746,7 @@ int View(SessionOptions options, const std::string &device,
|
||||
{"reported", input.terminal_metrics.Known()},
|
||||
{"manual_aspect", cell_aspect > 0}}},
|
||||
{"submitted", submitted},
|
||||
{"renderer", kitty ? "kitty" : "ansi"},
|
||||
{"renderer", kitty ? "kitty" : sixel ? "sixel" : "ansi"},
|
||||
{"kitty_transport", !kitty ? "none"
|
||||
: shared_enabled ? "shared_memory"
|
||||
: "inline"},
|
||||
@@ -1011,27 +1028,34 @@ int Bridge(int fd, SessionOptions options, int password_fd) {
|
||||
s.Disconnect();
|
||||
return interrupted ? 130 : 0;
|
||||
}
|
||||
const char *help = R"(crossdesk-cli <9-digit-device-id> [--renderer auto|ansi|kitty] [--fps 1..30]
|
||||
const char *help = R"(crossdesk-cli <9-digit-device-id> [--renderer auto|ansi|kitty|sixel] [--fps 1..30]
|
||||
[--display 0..63] [--control] [--server host] [--port 9099]
|
||||
[--quality low|balanced|high|ultra] [--ansi-detail quadrant|half] [--zoom 1..32|native]
|
||||
[--cell-aspect auto|0.2..2]
|
||||
crossdesk-cli view <9-digit-device-id> [options] (compatibility alias)
|
||||
crossdesk-cli doctor [--json]
|
||||
crossdesk-cli bridge --stdio [--password-fd 3] [--server host] [--port 9099]
|
||||
crossdesk-cli agent --stdio [--password-fd 3] [--video-ws] [--server host] [--port 9099]
|
||||
|
||||
Connect by device ID: read-only and silent by default; hidden password prompt on the local terminal.
|
||||
Read-only: c enables control; q or Ctrl+C quits; ? shows local commands.
|
||||
Ctrl+] enters persistent local mode: c selects control on return; q quits; Esc/Ctrl+C returns.
|
||||
View keys: click/d native detail, wheel/+/- zoom, arrows/ijkl pan (i up, j left, k down, l right), f fit.
|
||||
View keys (read-only/local): +/- zoom at mouse (= also zooms in); f restores the full desktop and resets pan.
|
||||
Pan: arrows or i up, j left, k down, l right. Read-only mouse: wheel zooms at pointer; click opens native detail.
|
||||
Local mode: move the mouse to choose the zoom anchor; clicks and wheel do not change the view.
|
||||
Zoom uses the current center when no mouse position is available or the pointer is outside the picture.
|
||||
Local mode stays active after commands, paste or focus loss; remote input remains paused.
|
||||
Ctrl+T and Ctrl+C pass through in control mode; another Ctrl+] leaves local mode and forwards Ctrl+] if control is enabled.
|
||||
Default ANSI detail: quadrant. Default quality: high (1920x1080); ultra requests up to 3840x2160.
|
||||
Remote previews prioritize resolution at every frame rate.
|
||||
Cell aspect is character width/height: auto detects terminal geometry; use --cell-aspect to calibrate.
|
||||
Display index is zero-based. ANSI cannot reproduce full desktop text at fit scale.
|
||||
Auto rendering prefers Kitty, then Sixel, then ANSI. Explicit pixel renderers require a successful probe.
|
||||
--fps controls local output (default 30). Without it, remote preview adapts to measured output speed.
|
||||
Explicit --fps keeps a fixed preview request. Kitty transport fallback keeps the local target unchanged.
|
||||
Preview requests above 15 use the negotiated interactive 30 FPS stream; lower requests use 1/8/15 FPS.
|
||||
bridge: private NDJSON v1, default 8 FPS, credentials on a separate inherited descriptor.
|
||||
agent: crossdesk-automation/1, structured actions and PNG screenshots; no terminal required.
|
||||
No IP direct connection, audio playback, clipboard or file transfer support.
|
||||
)";
|
||||
} // namespace
|
||||
@@ -1064,13 +1088,20 @@ int main(int argc, char **argv) {
|
||||
{"multiplexer", !Env("TMUX").empty() || !Env("STY").empty()},
|
||||
{"software_decode", true},
|
||||
{"kitty", "probe_on_view"},
|
||||
{"sixel", "probe_on_view"},
|
||||
{"audio_playback", false},
|
||||
{"bridge_version", 1}};
|
||||
{"bridge_version", 1},
|
||||
{"automation_terminal_protocol", "ansi/1"},
|
||||
{"automation_protocol", "crossdesk-automation/1"},
|
||||
{"automation_video", "h264-annexb/1"},
|
||||
{"automation_video_transport", "loopback-websocket/1"},
|
||||
{"automation_video_protocols", {"h264-annexb/1", "nv12/1"}},
|
||||
{"automation_video_decoders", {"auto", "software"}}};
|
||||
std::cout << d.dump(argc == 3 ? -1 : 2) << "\n";
|
||||
return 0;
|
||||
}
|
||||
int i = 2;
|
||||
if (command != "view" && command != "bridge") {
|
||||
if (command != "view" && command != "bridge" && command != "agent") {
|
||||
if (command.find_first_not_of("0123456789") != std::string::npos)
|
||||
throw std::invalid_argument(
|
||||
"expected a 9-digit device ID or a command; use --help");
|
||||
@@ -1083,6 +1114,8 @@ int main(int argc, char **argv) {
|
||||
double zoom = 1, cell_aspect = 0;
|
||||
bool control = false, stdio = false, native = false, fps_explicit = false;
|
||||
int secret_fd = 3;
|
||||
bool video_websocket = false;
|
||||
if (command == "agent") options.fps = 30;
|
||||
if (command == "view") {
|
||||
if (i >= argc)
|
||||
throw std::invalid_argument("device ID required");
|
||||
@@ -1102,8 +1135,10 @@ int main(int argc, char **argv) {
|
||||
std::string a = argv[i];
|
||||
if (a == "--control" && command == "view")
|
||||
control = true;
|
||||
else if (a == "--stdio" && command == "bridge")
|
||||
else if (a == "--stdio" && command != "view")
|
||||
stdio = true;
|
||||
else if (a == "--video-ws" && command == "agent")
|
||||
video_websocket = true;
|
||||
else {
|
||||
if (i + 1 >= argc)
|
||||
throw std::invalid_argument("option value missing");
|
||||
@@ -1112,6 +1147,8 @@ int main(int argc, char **argv) {
|
||||
options.host = value;
|
||||
else if (a == "--port")
|
||||
options.port = number(value, 1, 65535);
|
||||
else if (a == "--log-dir" && command == "agent")
|
||||
options.log_directory = value;
|
||||
else if (a == "--fps" && command == "view") {
|
||||
options.fps = number(value, 1, 30);
|
||||
fps_explicit = true;
|
||||
@@ -1136,22 +1173,23 @@ int main(int argc, char **argv) {
|
||||
} else if (a == "--zoom" && command == "view") {
|
||||
native = value == "native";
|
||||
zoom = native ? 1 : number(value, 1, 32);
|
||||
} else if (a == "--password-fd" && command == "bridge")
|
||||
} else if (a == "--password-fd" && command != "view")
|
||||
secret_fd = number(value, 3, 1024);
|
||||
else
|
||||
throw std::invalid_argument("unsupported option");
|
||||
}
|
||||
}
|
||||
if (renderer != "auto" && renderer != "ansi" && renderer != "kitty")
|
||||
if (renderer != "auto" && renderer != "ansi" && renderer != "kitty" &&
|
||||
renderer != "sixel")
|
||||
throw std::invalid_argument("unknown renderer");
|
||||
if (command == "bridge" && !stdio)
|
||||
throw std::invalid_argument("bridge requires --stdio");
|
||||
if (command != "view" && !stdio)
|
||||
throw std::invalid_argument("automation and bridge require --stdio");
|
||||
Descriptor output(Duplicate(1));
|
||||
if (output.value < 0)
|
||||
throw std::runtime_error("cannot preserve output channel");
|
||||
// All library stdout writes go to diagnostics. Only Output uses the saved
|
||||
// fd.
|
||||
options.log_directory = Logs();
|
||||
if (options.log_directory.empty()) options.log_directory = Logs();
|
||||
if (command == "view") {
|
||||
Descriptor logs(OpenLog(options.log_directory + "/client.log"));
|
||||
if (logs.value < 0 || Redirect(logs.value, 2) < 0)
|
||||
@@ -1170,7 +1208,9 @@ int main(int argc, char **argv) {
|
||||
return command == "view" ? View(options, device, renderer, control,
|
||||
output.value, detail, zoom, native,
|
||||
cell_aspect, fps_explicit)
|
||||
: Bridge(output.value, options, secret_fd);
|
||||
: command == "agent"
|
||||
? Agent(output.value, options, secret_fd, video_websocket)
|
||||
: Bridge(output.value, options, secret_fd);
|
||||
} catch (const std::exception &e) {
|
||||
if (command == "view") {
|
||||
std::string msg = "crossdesk-cli: " + SafeText(e.what()) + "\r\n";
|
||||
|
||||
+13
-11
@@ -36,17 +36,19 @@ function main() {
|
||||
return;
|
||||
}
|
||||
const stdio = ['inherit', 'inherit', 'inherit'];
|
||||
if (args[0] === 'bridge') {
|
||||
const index = args.lastIndexOf('--password-fd');
|
||||
const fd = index === -1 ? 3 : Number(args[index + 1]);
|
||||
if (Number.isInteger(fd) && fd >= 3 && fd <= 1024) {
|
||||
try {
|
||||
const stat = fs.fstatSync(fd);
|
||||
if (stat.isFIFO() || stat.isSocket() || stat.isFile()) {
|
||||
while (stdio.length <= fd) stdio.push('ignore');
|
||||
stdio[fd] = fd;
|
||||
}
|
||||
} catch { /* The native bridge reports a missing credential descriptor. */ }
|
||||
if (args[0] === 'bridge' || args[0] === 'agent') {
|
||||
for (const [option, fallback] of [['--password-fd', 3]]) {
|
||||
const index = args.lastIndexOf(option);
|
||||
const fd = index === -1 ? fallback : Number(args[index + 1]);
|
||||
if (Number.isInteger(fd) && fd >= 3 && fd <= 1024) {
|
||||
try {
|
||||
const stat = fs.fstatSync(fd);
|
||||
if (stat.isFIFO() || stat.isSocket() || stat.isFile()) {
|
||||
while (stdio.length <= fd) stdio.push('ignore');
|
||||
stdio[fd] = fd;
|
||||
}
|
||||
} catch { /* Native code reports unavailable descriptors. */ }
|
||||
}
|
||||
}
|
||||
}
|
||||
const child = spawn(binary, args, { stdio, shell: false });
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@chunkdash/crossdesk-cli",
|
||||
"version": "0.1.4",
|
||||
"version": "0.2.0",
|
||||
"private": true,
|
||||
"description": "CrossDesk remote desktop controller for the terminal",
|
||||
"license": "GPL-3.0-only",
|
||||
|
||||
@@ -227,10 +227,10 @@ export default function crossdesk(pi: ExtensionAPI) {
|
||||
const ack = frame; frame = undefined;
|
||||
if (ack) queueMicrotask(() => send("ack", ack)); // Host consumed the frame; not a physical display timestamp.
|
||||
const hint = bridgeStopped ? "q / Esc / Ctrl+C close"
|
||||
: commandMode ? "Esc/Ctrl+C return | c toggle q quit | arrows/ijkl pan +/- zoom f fit d detail"
|
||||
: commandMode ? "Esc/Ctrl+C return | c toggle | q quit | +/- zoom | f full | arrows/ijkl pan"
|
||||
: control ? "Keys -> remote | Ctrl+] then c release, q quit"
|
||||
: "c control q quit ? help | i up j left k down l right | +/- zoom f fit d detail";
|
||||
return [truncateToWidth(`CrossDesk ${device} | ${status} ${native ? "detail" : "fit"} x${zoom} | Ctrl+] commands`, width),
|
||||
: "c control | q quit | +/- zoom | f full | arrows/ijkl pan | ? commands";
|
||||
return [truncateToWidth(`CrossDesk ${device} | ${status} ${native ? "detail" : "zoom"} x${zoom} | Ctrl+] commands`, width),
|
||||
...lines.slice(0, h).map(line => truncateToWidth(line, width)),
|
||||
truncateToWidth(`${hint}${error ? " | " + error : lines.length ? "" : " | Waiting for remote video…"}`, width)];
|
||||
},
|
||||
|
||||
@@ -84,6 +84,8 @@ function inspect(binary, target) {
|
||||
}
|
||||
const doctor = JSON.parse(run(binary, ['doctor', '--json'], { timeout: 15000 }));
|
||||
if (doctor.bridge_version !== 1) throw new Error('Native binary does not report the expected bridge version.');
|
||||
if (doctor.automation_protocol !== 'crossdesk-automation/1')
|
||||
throw new Error('Native binary does not report the expected automation protocol.');
|
||||
return info;
|
||||
}
|
||||
function pack(directory, output) {
|
||||
@@ -132,8 +134,6 @@ try {
|
||||
const filename = tarballName(name, version);
|
||||
if (fs.existsSync(path.join(output, filename))) throw new Error(`Output already exists: ${filename}`);
|
||||
}
|
||||
run('git', ['-C', path.join(root, 'deps/submodules/minirtc'), 'apply', '--reverse', '--check',
|
||||
path.join(root, 'deps/patches/minirtc-credential-logs.patch')]);
|
||||
const executable = process.platform === 'win32' ? 'crossdesk-cli.exe' : 'crossdesk-cli';
|
||||
binary ||= path.join(root, 'build', platform.xmakePlatform, platform.xmakeArch, 'release', executable);
|
||||
const info = inspect(binary, target);
|
||||
@@ -144,7 +144,7 @@ try {
|
||||
fs.copyFileSync(binary, path.join(native, 'bin', executable));
|
||||
fs.chmodSync(path.join(native, 'bin', executable), 0o755);
|
||||
copy('LICENSE', path.join(native, 'LICENSE'));
|
||||
// Include the controller and patched MiniRTC sources used by this checkout.
|
||||
// Include the controller and MiniRTC sources used by this checkout.
|
||||
// External Xmake dependency source/licenses still belong to the release's
|
||||
// dependency bundle; see source/apps/cli/docs/source-provenance.md.
|
||||
// Keep desktop build definitions for Xmake's initial option discovery;
|
||||
@@ -153,11 +153,11 @@ try {
|
||||
'libs/controller/include',
|
||||
'libs/controller/src', 'libs/terminal', 'libs/wire/include',
|
||||
'libs/wire/src', 'deps/submodules/minirtc',
|
||||
'deps/submodules/xmake.lua', 'deps/thirdparty', 'deps/patches', 'xmake.lua',
|
||||
'deps/submodules/xmake.lua', 'deps/thirdparty', 'xmake.lua',
|
||||
'LICENSE', '.gitmodules'])
|
||||
copy(entry, path.join(native, 'source', entry));
|
||||
json(path.join(native, 'build-info.json'), { version, ...info });
|
||||
fs.writeFileSync(path.join(native, 'README.md'), `# ${platform.packageName}\n\nNative ${target} dependency for [${project.name}](https://www.npmjs.com/package/${project.name}) ${version}, published under the ${platform.packageName.split('/')[0]} scope.\n\nInstall the cross-platform entry package with \`npm install -g ${project.name}\` or run \`npx ${project.name} doctor --json\`. This platform package contains the native binary and does not register a shell command.\n\nSource repository: [CrossDesk](${project.repository.url.replace(/^git\+/, '').replace(/\.git$/, '')}). Report problems at ${project.bugs.url}.\n\nRuntime requirements and linked libraries: [build-info.json](build-info.json).\n\nController and patched MiniRTC sources are included in source/. From source/, configure with xmake f -y --CROSSDESK_BUILD_DESKTOP=n --CROSSDESK_BUILD_CLI=y --USE_CUDA=n -m release, then run xmake build crossdesk-cli. External dependency notices and corresponding sources must accompany the product release; see source/apps/cli/docs/source-provenance.md.\n`);
|
||||
fs.writeFileSync(path.join(native, 'README.md'), `# ${platform.packageName}\n\nNative ${target} dependency for [${project.name}](https://www.npmjs.com/package/${project.name}) ${version}, published under the ${platform.packageName.split('/')[0]} scope.\n\nInstall the cross-platform entry package with \`npm install -g ${project.name}\` or run \`npx ${project.name} doctor --json\`. This platform package contains the native binary and does not register a shell command.\n\nSource repository: [CrossDesk](${project.repository.url.replace(/^git\+/, '').replace(/\.git$/, '')}). Report problems at ${project.bugs.url}.\n\nRuntime requirements and linked libraries: [build-info.json](build-info.json).\n\nController and MiniRTC sources are included in source/. From source/, configure with xmake f -y --CROSSDESK_BUILD_DESKTOP=n --CROSSDESK_BUILD_CLI=y --USE_CUDA=n -m release, then run xmake build crossdesk-cli. External dependency notices and corresponding sources must accompany the product release; see source/apps/cli/docs/source-provenance.md.\n`);
|
||||
const metadata = {
|
||||
license: project.license, author: project.author, repository: project.repository,
|
||||
homepage: project.homepage, bugs: project.bugs,
|
||||
|
||||
@@ -2,9 +2,3 @@
|
||||
set -eu
|
||||
cd "$(dirname "$0")/../../.."
|
||||
git submodule update --init --recursive
|
||||
patch_file="$(pwd)/deps/patches/minirtc-credential-logs.patch"
|
||||
if git -C deps/submodules/minirtc apply --reverse --check "$patch_file" 2>/dev/null; then
|
||||
exit 0
|
||||
fi
|
||||
git -C deps/submodules/minirtc apply --check "$patch_file"
|
||||
git -C deps/submodules/minirtc apply "$patch_file"
|
||||
|
||||
@@ -0,0 +1,507 @@
|
||||
#include "video_server.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <deque>
|
||||
#include <future>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <websocketpp/config/asio_no_tls.hpp>
|
||||
#include <websocketpp/server.hpp>
|
||||
|
||||
namespace crossdesk::cli {
|
||||
namespace {
|
||||
using Clock = std::chrono::steady_clock;
|
||||
using namespace std::chrono_literals;
|
||||
using json = nlohmann::json;
|
||||
using Handle = websocketpp::connection_hdl;
|
||||
using Server = websocketpp::server<websocketpp::config::asio>;
|
||||
constexpr size_t kMaxEncoded = 4 * 1024 * 1024;
|
||||
constexpr size_t kMaxQueued = 8 * 1024 * 1024;
|
||||
|
||||
bool Token(const std::string &s) {
|
||||
return s.size() == 32 &&
|
||||
s.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw"
|
||||
"xyz0123456789-_") == std::string::npos;
|
||||
}
|
||||
bool AppOrigin(const std::string &s) {
|
||||
return s == "null" ||
|
||||
(s.size() > 8 && s.size() <= 256 && s.compare(0, 8, "https://") == 0 &&
|
||||
s.find_first_of("/?#@\\ \t\r\n", 8) == std::string::npos);
|
||||
}
|
||||
bool BrowserOrigin(const std::string &s) {
|
||||
const std::string prefix = "http://127.0.0.1:";
|
||||
if (s.compare(0, prefix.size(), prefix) || s.size() > prefix.size() + 5 ||
|
||||
s.size() == prefix.size() ||
|
||||
s.find_first_not_of("0123456789", prefix.size()) != std::string::npos)
|
||||
return false;
|
||||
const auto port = std::stoul(s.substr(prefix.size()));
|
||||
return port > 0 && port <= 65535;
|
||||
}
|
||||
void Number(std::string &out, uint64_t value, int bytes) {
|
||||
for (int n = bytes - 1; n >= 0; --n)
|
||||
out.push_back(char(value >> (n * 8)));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
struct VideoServer::State {
|
||||
struct Ticket {
|
||||
VideoGrant grant;
|
||||
Clock::time_point expires;
|
||||
};
|
||||
struct Receipt {
|
||||
uint32_t generation, sequence;
|
||||
size_t bytes;
|
||||
Clock::time_point sent;
|
||||
};
|
||||
struct Client {
|
||||
std::string origin, view, session;
|
||||
uint64_t view_generation = 0;
|
||||
uint64_t cursor_revision = 0;
|
||||
bool authorized = false, waiting_key = true;
|
||||
size_t bytes = 0;
|
||||
Clock::time_point opened = Clock::now(), pinged = Clock::now();
|
||||
std::deque<Receipt> receipts;
|
||||
};
|
||||
struct Packet {
|
||||
std::shared_ptr<const DecodedFrame> raw;
|
||||
EncodedVideoFrame encoded;
|
||||
size_t Size() const {
|
||||
return raw ? 32 + raw->nv12.size() : 24 + encoded.data.size();
|
||||
}
|
||||
uint64_t Generation() const {
|
||||
return raw ? raw->generation : encoded.generation;
|
||||
}
|
||||
uint64_t Sequence() const { return raw ? raw->frame_id : encoded.sequence; }
|
||||
bool Key() const { return raw || encoded.key; }
|
||||
};
|
||||
|
||||
Server server;
|
||||
Server::timer_ptr timer;
|
||||
std::thread thread;
|
||||
std::string authority;
|
||||
std::map<Handle, Client, std::owner_less<Handle>> clients;
|
||||
std::map<std::string, Ticket> tickets;
|
||||
std::atomic<size_t> viewers{0}, outstanding{0}, dropped{0};
|
||||
std::atomic<bool> key_requested{false};
|
||||
uint64_t generation = 0;
|
||||
bool connected = false, decoded = false;
|
||||
Clock::time_point last_key_request{};
|
||||
|
||||
// One posted drain, not one posted handler per picture. Raw frames replace
|
||||
// each other; the compressed handoff is bounded and recovers at an IDR.
|
||||
mutable std::mutex mutex;
|
||||
std::deque<Packet> queue;
|
||||
size_t queued_bytes = 0;
|
||||
bool scheduled = false, wait_key = true;
|
||||
json cursor;
|
||||
uint64_t cursor_revision = 0;
|
||||
|
||||
State() {
|
||||
server.clear_access_channels(websocketpp::log::alevel::all);
|
||||
server.clear_error_channels(websocketpp::log::elevel::all);
|
||||
server.init_asio();
|
||||
server.set_open_handshake_timeout(3000);
|
||||
server.set_close_handshake_timeout(1000);
|
||||
server.set_pong_timeout(10000);
|
||||
server.set_max_message_size(1024);
|
||||
server.set_max_http_body_size(0);
|
||||
server.set_socket_init_handler([](Handle, asio::ip::tcp::socket &socket) {
|
||||
asio::error_code ec;
|
||||
socket.set_option(asio::ip::tcp::no_delay(true), ec);
|
||||
socket.set_option(asio::socket_base::send_buffer_size(256 * 1024), ec);
|
||||
});
|
||||
server.set_validate_handler([this](Handle h) {
|
||||
auto c = server.get_con_from_hdl(h);
|
||||
if (clients.size() >= 16 || c->get_resource() != "/api/video" ||
|
||||
c->get_request_header("Host") != authority ||
|
||||
c->get_request_header("Sec-WebSocket-Version") != "13" ||
|
||||
c->get_origin().empty() || c->get_origin().size() > 256) {
|
||||
c->set_status(websocketpp::http::status_code::forbidden);
|
||||
return false;
|
||||
}
|
||||
Client client;
|
||||
client.origin = c->get_origin();
|
||||
clients.emplace(h, std::move(client));
|
||||
return true;
|
||||
});
|
||||
server.set_message_handler([this](Handle h, Server::message_ptr message) {
|
||||
try {
|
||||
Message(h, message);
|
||||
} catch (...) {
|
||||
Drop(h);
|
||||
}
|
||||
});
|
||||
server.set_close_handler([this](Handle h) { Remove(h); });
|
||||
server.set_fail_handler([this](Handle h) { Remove(h); });
|
||||
server.set_pong_timeout_handler(
|
||||
[this](Handle h, const std::string &) { Drop(h); });
|
||||
server.listen(asio::ip::tcp::endpoint(asio::ip::address_v4::loopback(), 0));
|
||||
asio::error_code ec;
|
||||
authority =
|
||||
"127.0.0.1:" + std::to_string(server.get_local_endpoint(ec).port());
|
||||
if (ec)
|
||||
throw std::runtime_error("VIDEO_CHANNEL_UNAVAILABLE");
|
||||
server.start_accept();
|
||||
Maintain();
|
||||
thread = std::thread([this] { server.run(); });
|
||||
}
|
||||
~State() {
|
||||
Call([this] {
|
||||
websocketpp::lib::error_code ec;
|
||||
server.stop_listening(ec);
|
||||
if (timer)
|
||||
timer->cancel();
|
||||
Revoke("");
|
||||
});
|
||||
server.stop();
|
||||
thread.join();
|
||||
}
|
||||
template <class F> void Call(F f) {
|
||||
auto task = std::make_shared<std::packaged_task<void()>>(std::move(f));
|
||||
auto result = task->get_future();
|
||||
server.get_io_service().post([task] { (*task)(); });
|
||||
result.get();
|
||||
}
|
||||
void Remove(Handle h) {
|
||||
auto it = clients.find(h);
|
||||
if (it == clients.end())
|
||||
return;
|
||||
if (it->second.authorized)
|
||||
--viewers;
|
||||
outstanding -= it->second.bytes;
|
||||
clients.erase(it);
|
||||
}
|
||||
void Drop(Handle h) {
|
||||
// Cancel even a partially sent raw picture on revocation. Waiting for a
|
||||
// WebSocket close frame behind megabytes of video would keep it alive.
|
||||
Remove(h);
|
||||
websocketpp::lib::error_code ec;
|
||||
auto c = server.get_con_from_hdl(h, ec);
|
||||
if (!ec) {
|
||||
asio::error_code ignored;
|
||||
c->get_raw_socket().cancel(ignored);
|
||||
c->get_raw_socket().close(ignored);
|
||||
}
|
||||
}
|
||||
void Revoke(const std::string &view) {
|
||||
for (auto it = tickets.begin(); it != tickets.end();) {
|
||||
if (view.empty() || it->second.grant.view == view)
|
||||
it = tickets.erase(it);
|
||||
else
|
||||
++it;
|
||||
}
|
||||
std::vector<Handle> close;
|
||||
for (const auto &[h, c] : clients)
|
||||
if (view.empty() || c.view == view)
|
||||
close.push_back(h);
|
||||
for (auto h : close)
|
||||
Drop(h);
|
||||
}
|
||||
void RequestKey() {
|
||||
const auto now = Clock::now();
|
||||
if (now - last_key_request < 1s)
|
||||
return;
|
||||
last_key_request = now;
|
||||
key_requested = true;
|
||||
}
|
||||
void Message(Handle h, const Server::message_ptr &message) {
|
||||
auto it = clients.find(h);
|
||||
if (it == clients.end())
|
||||
return;
|
||||
auto &c = it->second;
|
||||
auto j = json::parse(message->get_payload(), nullptr, false);
|
||||
if (message->get_opcode() != websocketpp::frame::opcode::TEXT ||
|
||||
!j.is_object())
|
||||
return Drop(h);
|
||||
if (!c.authorized) {
|
||||
if (j.size() != 1 || !j.contains("ticket") || !j["ticket"].is_string())
|
||||
return Drop(h);
|
||||
auto ticket = tickets.find(j["ticket"].get<std::string>());
|
||||
if (ticket == tickets.end())
|
||||
return Drop(h);
|
||||
const auto grant = std::move(ticket->second);
|
||||
tickets.erase(ticket);
|
||||
if (!connected || grant.expires <= Clock::now() || viewers >= 8 ||
|
||||
(c.origin != grant.grant.origin &&
|
||||
!(grant.grant.apps && AppOrigin(c.origin))))
|
||||
return Drop(h);
|
||||
c.authorized = true;
|
||||
c.view = grant.grant.view;
|
||||
c.view_generation = grant.grant.view_generation;
|
||||
c.session = grant.grant.session;
|
||||
++viewers;
|
||||
// Also prompts a raw viewer to receive the current fresh picture.
|
||||
key_requested = true;
|
||||
return;
|
||||
}
|
||||
if (j.value("type", "") == "keyframe" && j.size() == 1 && !decoded) {
|
||||
c.waiting_key = true;
|
||||
RequestKey();
|
||||
return;
|
||||
}
|
||||
if (j.value("type", "") != "ack" || j.size() != 3 ||
|
||||
!j.contains("generation") || !j["generation"].is_number_unsigned() ||
|
||||
!j.contains("sequence") || !j["sequence"].is_number_unsigned() ||
|
||||
c.receipts.empty() ||
|
||||
j["generation"] != c.receipts.front().generation ||
|
||||
j["sequence"] != c.receipts.front().sequence)
|
||||
return Drop(h);
|
||||
c.bytes -= c.receipts.front().bytes;
|
||||
outstanding -= c.receipts.front().bytes;
|
||||
c.receipts.pop_front();
|
||||
}
|
||||
void Maintain() {
|
||||
timer = server.set_timer(
|
||||
33, [this](const websocketpp::lib::error_code &error) {
|
||||
if (error)
|
||||
return;
|
||||
const auto now = Clock::now();
|
||||
std::string pointer;
|
||||
uint64_t revision;
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
revision = cursor_revision;
|
||||
if (!cursor.is_null()) pointer = cursor.dump();
|
||||
}
|
||||
for (auto it = tickets.begin(); it != tickets.end();)
|
||||
if (it->second.expires <= now)
|
||||
it = tickets.erase(it);
|
||||
else
|
||||
++it;
|
||||
std::vector<Handle> close;
|
||||
for (auto &[h, c] : clients) {
|
||||
if (c.authorized && c.cursor_revision != revision && !pointer.empty()) {
|
||||
websocketpp::lib::error_code ec;
|
||||
auto connection = server.get_con_from_hdl(h, ec);
|
||||
// Coalesce pointer changes while a viewer is backpressured.
|
||||
if (!ec && connection->get_buffered_amount() < 256 * 1024) {
|
||||
server.send(h, pointer, websocketpp::frame::opcode::TEXT, ec);
|
||||
if (!ec) c.cursor_revision = revision;
|
||||
}
|
||||
if (ec) close.push_back(h);
|
||||
}
|
||||
if ((!c.authorized && now - c.opened >= 3s) ||
|
||||
(!c.receipts.empty() && now - c.receipts.front().sent >= 10s)) {
|
||||
close.push_back(h);
|
||||
} else if (c.authorized && now - c.pinged >= 10s) {
|
||||
c.pinged = now;
|
||||
websocketpp::lib::error_code ec;
|
||||
server.ping(h, "", ec);
|
||||
if (ec)
|
||||
close.push_back(h);
|
||||
}
|
||||
}
|
||||
for (auto h : close)
|
||||
Drop(h);
|
||||
Maintain();
|
||||
});
|
||||
}
|
||||
void Enqueue(Packet p) {
|
||||
std::lock_guard lock(mutex);
|
||||
if (p.raw) {
|
||||
dropped += queue.size();
|
||||
queue.clear();
|
||||
queued_bytes = 0;
|
||||
} else {
|
||||
if (queue.size() >= 60 || queued_bytes + p.Size() > kMaxQueued) {
|
||||
dropped += queue.size();
|
||||
queue.clear();
|
||||
queued_bytes = 0;
|
||||
wait_key = true;
|
||||
key_requested = true;
|
||||
}
|
||||
if (wait_key && !p.Key()) {
|
||||
++dropped;
|
||||
return;
|
||||
}
|
||||
wait_key = false;
|
||||
}
|
||||
queued_bytes += p.Size();
|
||||
queue.push_back(std::move(p));
|
||||
if (!scheduled) {
|
||||
scheduled = true;
|
||||
server.get_io_service().post([this] { Drain(); });
|
||||
}
|
||||
}
|
||||
void Drain() {
|
||||
Packet p;
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
if (queue.empty()) {
|
||||
scheduled = false;
|
||||
return;
|
||||
}
|
||||
p = std::move(queue.front());
|
||||
queued_bytes -= p.Size();
|
||||
queue.pop_front();
|
||||
}
|
||||
if (connected && p.Generation() == generation && bool(p.raw) == decoded)
|
||||
Publish(p);
|
||||
// Let authorization, reads, and timers run between large pictures.
|
||||
server.get_io_service().post([this] { Drain(); });
|
||||
}
|
||||
void Publish(const Packet &p) {
|
||||
std::vector<Handle> ready;
|
||||
for (auto &[h, c] : clients) {
|
||||
if (!c.authorized)
|
||||
continue;
|
||||
// ACKs are flow-control hints, not proof that a client read the socket.
|
||||
// Also bound the actual send queue against forged early receipts.
|
||||
const auto buffered = server.get_con_from_hdl(h)->get_buffered_amount();
|
||||
if ((decoded && (!c.receipts.empty() || buffered > 0)) ||
|
||||
(!decoded && (c.receipts.size() >= 8 || c.bytes >= 1024 * 1024 ||
|
||||
buffered >= 1024 * 1024))) {
|
||||
++dropped;
|
||||
c.waiting_key = true;
|
||||
if (!decoded)
|
||||
RequestKey();
|
||||
continue;
|
||||
}
|
||||
if (!decoded && c.waiting_key && !p.Key()) {
|
||||
RequestKey();
|
||||
continue;
|
||||
}
|
||||
ready.push_back(h);
|
||||
}
|
||||
if (ready.empty())
|
||||
return;
|
||||
// Prepare once and share the immutable WebSocket message across viewers.
|
||||
// No length-prefixed pipe, Node Buffer, or per-viewer payload copy.
|
||||
auto message =
|
||||
server.get_con_from_hdl(ready.front())
|
||||
->get_message(websocketpp::frame::opcode::BINARY, p.Size());
|
||||
auto &bytes = message->get_raw_payload();
|
||||
Number(bytes, decoded ? 0x43444e31 : 0x43445631, 4);
|
||||
Number(bytes, p.Generation(), 4);
|
||||
Number(bytes, p.Sequence(), 4);
|
||||
const auto time =
|
||||
p.raw ? std::chrono::duration_cast<std::chrono::microseconds>(
|
||||
p.raw->queued.time_since_epoch())
|
||||
.count()
|
||||
: p.encoded.timestamp_us;
|
||||
Number(bytes, time, 8);
|
||||
Number(bytes, p.Key() ? 1 : 0, 4);
|
||||
if (p.raw) {
|
||||
Number(bytes, p.raw->width, 4);
|
||||
Number(bytes, p.raw->height, 4);
|
||||
message->append_payload(p.raw->nv12.data(), p.raw->nv12.size());
|
||||
} else
|
||||
message->append_payload(p.encoded.data.data(), p.encoded.data.size());
|
||||
message->set_header(websocketpp::frame::prepare_header(
|
||||
websocketpp::frame::basic_header(websocketpp::frame::opcode::BINARY,
|
||||
bytes.size(), true, false),
|
||||
websocketpp::frame::extended_header(bytes.size())));
|
||||
message->set_prepared(true);
|
||||
for (auto h : ready) {
|
||||
auto &c = clients.at(h);
|
||||
websocketpp::lib::error_code ec;
|
||||
server.send(h, message, ec);
|
||||
if (ec) {
|
||||
Drop(h);
|
||||
continue;
|
||||
}
|
||||
c.waiting_key = false;
|
||||
c.bytes += bytes.size();
|
||||
outstanding += bytes.size();
|
||||
c.receipts.push_back({uint32_t(p.Generation()), uint32_t(p.Sequence()),
|
||||
bytes.size(), Clock::now()});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
VideoServer::VideoServer() : state_(std::make_unique<State>()) {}
|
||||
VideoServer::~VideoServer() = default;
|
||||
std::string VideoServer::Url() const {
|
||||
return "ws://" + state_->authority + "/api/video";
|
||||
}
|
||||
void VideoServer::Bind(uint64_t generation, bool decoded, bool connected) {
|
||||
auto &s = *state_;
|
||||
// Only the automation owner calls Bind; the I/O thread reads these values.
|
||||
s.Call([&s, generation, decoded, connected] {
|
||||
if (s.generation == generation && s.decoded == decoded &&
|
||||
s.connected == connected)
|
||||
return;
|
||||
s.Revoke("");
|
||||
s.generation = generation;
|
||||
s.decoded = decoded;
|
||||
s.connected = connected;
|
||||
s.key_requested = false;
|
||||
std::lock_guard lock(s.mutex);
|
||||
s.queue.clear();
|
||||
s.queued_bytes = 0;
|
||||
s.wait_key = true;
|
||||
});
|
||||
}
|
||||
void VideoServer::Grant(VideoGrant grant) {
|
||||
if (!Token(grant.ticket) || !Token(grant.view) || !Token(grant.session) ||
|
||||
!grant.view_generation || !BrowserOrigin(grant.origin))
|
||||
throw std::invalid_argument("INVALID_VIDEO_GRANT");
|
||||
auto &s = *state_;
|
||||
s.Call([&s, grant = std::move(grant)] {
|
||||
if (!s.connected)
|
||||
throw std::invalid_argument("NOT_READY");
|
||||
for (auto it = s.tickets.begin(); it != s.tickets.end();)
|
||||
if (it->second.expires <= Clock::now())
|
||||
it = s.tickets.erase(it);
|
||||
else
|
||||
++it;
|
||||
if (s.tickets.size() >= 32)
|
||||
throw std::invalid_argument("VIDEO_LIMIT");
|
||||
// A replaced view cannot retain a socket or consume an old grant.
|
||||
bool changed = false;
|
||||
for (const auto &[h, c] : s.clients)
|
||||
changed |=
|
||||
c.view == grant.view && (c.view_generation != grant.view_generation ||
|
||||
c.session != grant.session);
|
||||
for (const auto &[ticket, t] : s.tickets)
|
||||
changed |= t.grant.view == grant.view &&
|
||||
(t.grant.view_generation != grant.view_generation ||
|
||||
t.grant.session != grant.session);
|
||||
if (changed)
|
||||
s.Revoke(grant.view);
|
||||
if (!s.tickets
|
||||
.emplace(grant.ticket, State::Ticket{grant, Clock::now() + 15s})
|
||||
.second)
|
||||
throw std::invalid_argument("INVALID_VIDEO_GRANT");
|
||||
});
|
||||
}
|
||||
void VideoServer::Revoke(const std::string &view) {
|
||||
state_->Call([&] { state_->Revoke(view); });
|
||||
}
|
||||
bool VideoServer::Enabled() const { return state_->viewers.load() > 0; }
|
||||
bool VideoServer::TakeKeyframeRequest() {
|
||||
return state_->key_requested.exchange(false);
|
||||
}
|
||||
void VideoServer::Submit(std::shared_ptr<const DecodedFrame> frame) {
|
||||
if (!frame || !frame->width || !frame->height || (frame->width & 1) ||
|
||||
(frame->height & 1) || frame->width > 8192 || frame->height > 8192 ||
|
||||
uint64_t(frame->width) * frame->height > 16777216 ||
|
||||
frame->nv12.size() != size_t(frame->width) * frame->height * 3 / 2)
|
||||
throw std::invalid_argument("INVALID_VIDEO_FRAME");
|
||||
if (Enabled())
|
||||
state_->Enqueue({std::move(frame), {}});
|
||||
}
|
||||
void VideoServer::Submit(EncodedVideoFrame frame) {
|
||||
if (frame.data.empty() || frame.data.size() > kMaxEncoded)
|
||||
throw std::invalid_argument("INVALID_VIDEO_FRAME");
|
||||
if (Enabled())
|
||||
state_->Enqueue({{}, std::move(frame)});
|
||||
}
|
||||
void VideoServer::Cursor(json cursor) {
|
||||
auto &s = *state_;
|
||||
std::lock_guard lock(s.mutex);
|
||||
s.cursor = std::move(cursor);
|
||||
++s.cursor_revision;
|
||||
}
|
||||
json VideoServer::Status() const {
|
||||
const auto &s = *state_;
|
||||
std::lock_guard lock(s.mutex);
|
||||
return {{"transport", "loopback-websocket/1"},
|
||||
{"viewers", s.viewers.load()},
|
||||
{"queued", s.queue.size()},
|
||||
{"queued_bytes", s.queued_bytes},
|
||||
{"unacknowledged_bytes", s.outstanding.load()},
|
||||
{"dropped", s.dropped.load()}};
|
||||
}
|
||||
} // namespace crossdesk::cli
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-11-03
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _VIDEO_SERVER_H_
|
||||
#define _VIDEO_SERVER_H_
|
||||
|
||||
#include <controller_session.h>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace crossdesk::cli {
|
||||
|
||||
struct VideoGrant {
|
||||
std::string ticket, view, session, origin;
|
||||
uint64_t view_generation = 0;
|
||||
bool apps = false;
|
||||
};
|
||||
|
||||
// Owns the loopback listener and its I/O thread. JSON-RPC carries only grants
|
||||
// and diagnostics; encoded and decoded pictures stay in the native process.
|
||||
class VideoServer {
|
||||
public:
|
||||
VideoServer();
|
||||
~VideoServer();
|
||||
VideoServer(const VideoServer &) = delete;
|
||||
VideoServer &operator=(const VideoServer &) = delete;
|
||||
|
||||
std::string Url() const;
|
||||
void Bind(uint64_t generation, bool decoded, bool connected);
|
||||
void Grant(VideoGrant grant);
|
||||
void Revoke(const std::string &view = "");
|
||||
bool Enabled() const;
|
||||
bool TakeKeyframeRequest();
|
||||
void Submit(std::shared_ptr<const DecodedFrame> frame);
|
||||
void Submit(EncodedVideoFrame frame);
|
||||
void Cursor(nlohmann::json cursor);
|
||||
nlohmann::json Status() const;
|
||||
|
||||
private:
|
||||
struct State;
|
||||
std::unique_ptr<State> state_;
|
||||
};
|
||||
|
||||
} // namespace crossdesk::cli
|
||||
|
||||
#endif
|
||||
+7
-8
@@ -2,9 +2,10 @@ local source_dir = path.absolute("../..", os.scriptdir())
|
||||
local controller_dir = path.join(source_dir, "libs/controller")
|
||||
local terminal_dir = path.join(source_dir, "libs/terminal")
|
||||
local minirtc_dir = path.join(source_dir, "deps/submodules/minirtc")
|
||||
local credential_patch = path.join(source_dir, "deps/patches/minirtc-credential-logs.patch")
|
||||
|
||||
add_requires("spdlog 1.14.1", {system = false})
|
||||
add_requires("asio 1.32.0", "websocketpp 0.8.2", {system = false})
|
||||
add_requires("libpng v1.6.59", {system = false, configs = {shared = false}})
|
||||
if is_plat("windows") then
|
||||
add_requires("zlib 1.3.1", {system = false, configs = {shared = false}})
|
||||
end
|
||||
@@ -12,15 +13,11 @@ end
|
||||
target("crossdesk_controller")
|
||||
set_kind("static")
|
||||
add_deps("minirtc", "crossdesk_wire")
|
||||
add_packages("spdlog")
|
||||
add_packages("spdlog", "libpng", "libyuv")
|
||||
add_includedirs(path.join(controller_dir, "include"),
|
||||
path.join(minirtc_dir, "src/api"), {public = true})
|
||||
add_includedirs(path.join(minirtc_dir, "src/log"))
|
||||
add_files(path.join(controller_dir, "src/*.cpp"))
|
||||
before_build(function ()
|
||||
-- Require the same credential redaction in checkouts and source bundles.
|
||||
os.vrunv("git", {"-C", minirtc_dir, "apply", "--reverse", "--check", credential_patch})
|
||||
end)
|
||||
target_end()
|
||||
|
||||
target("crossdesk_terminal")
|
||||
@@ -37,12 +34,14 @@ target_end()
|
||||
|
||||
target("crossdesk-cli")
|
||||
set_kind("binary")
|
||||
set_version("0.1.4")
|
||||
set_version("0.2.0")
|
||||
add_deps("crossdesk_terminal")
|
||||
add_files("main.cpp")
|
||||
add_files("agent.cpp", "automation_protocol.cpp", "video_server.cpp")
|
||||
add_packages("asio", "websocketpp")
|
||||
add_files(is_plat("windows") and "terminal_io_windows.cpp" or "terminal_io_posix.cpp")
|
||||
if is_plat("windows") then
|
||||
add_syslinks("advapi32", "user32")
|
||||
add_syslinks("advapi32", "user32", "ws2_32")
|
||||
add_defines("NOMINMAX", "WIN32_LEAN_AND_MEAN")
|
||||
elseif is_plat("linux") then
|
||||
add_syslinks("rt")
|
||||
|
||||
@@ -0,0 +1,244 @@
|
||||
#include "private_file.h"
|
||||
|
||||
#include <openssl/rand.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <Aclapi.h>
|
||||
#include <sddl.h>
|
||||
#else
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace crossdesk::private_file {
|
||||
namespace {
|
||||
void Require(bool ok, const char* error) {
|
||||
if (!ok) throw std::runtime_error(error);
|
||||
}
|
||||
std::string RandomSuffix() {
|
||||
unsigned char bytes[16];
|
||||
Require(RAND_bytes(bytes, sizeof(bytes)) == 1, "Secure random source unavailable");
|
||||
constexpr char hex[] = "0123456789abcdef";
|
||||
std::string value;
|
||||
for (auto byte : bytes) { value += hex[byte >> 4]; value += hex[byte & 15]; }
|
||||
return value;
|
||||
}
|
||||
#ifdef _WIN32
|
||||
std::vector<unsigned char> CurrentUser() {
|
||||
HANDLE token = nullptr;
|
||||
Require(OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token),
|
||||
"Cannot open user token");
|
||||
DWORD size = 0;
|
||||
GetTokenInformation(token, TokenUser, nullptr, 0, &size);
|
||||
std::vector<unsigned char> data(size);
|
||||
bool ok = GetTokenInformation(token, TokenUser, data.data(), size, &size);
|
||||
CloseHandle(token);
|
||||
Require(ok, "Cannot read user token");
|
||||
return data;
|
||||
}
|
||||
PSECURITY_DESCRIPTOR PrivateSecurityDescriptor(bool directory) {
|
||||
auto user = CurrentUser();
|
||||
LPWSTR sid = nullptr;
|
||||
Require(ConvertSidToStringSidW(
|
||||
reinterpret_cast<TOKEN_USER*>(user.data())->User.Sid, &sid),
|
||||
"Cannot read user SID");
|
||||
// An elevated token can default to Administrators as the owner even when
|
||||
// its DACL grants access only to TokenUser. Always set both explicitly.
|
||||
const auto acl = L"O:" + std::wstring(sid) + L"D:P(A;" +
|
||||
(directory ? L"OICI" : L"") + L";FA;;;" + sid + L")";
|
||||
LocalFree(sid);
|
||||
PSECURITY_DESCRIPTOR descriptor = nullptr;
|
||||
Require(ConvertStringSecurityDescriptorToSecurityDescriptorW(
|
||||
acl.c_str(), SDDL_REVISION_1, &descriptor, nullptr),
|
||||
"Cannot protect private file");
|
||||
return descriptor;
|
||||
}
|
||||
#endif
|
||||
} // namespace
|
||||
|
||||
void Write(const std::filesystem::path& path, const std::string& text) {
|
||||
const auto temporary = path.parent_path() / (".write-" + RandomSuffix());
|
||||
#ifdef _WIN32
|
||||
auto descriptor = PrivateSecurityDescriptor(false);
|
||||
SECURITY_ATTRIBUTES security{sizeof(security), descriptor, FALSE};
|
||||
HANDLE file = CreateFileW(temporary.c_str(), GENERIC_WRITE, 0, &security,
|
||||
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
LocalFree(descriptor);
|
||||
Require(file != INVALID_HANDLE_VALUE, "Cannot create private file");
|
||||
DWORD written = 0;
|
||||
bool ok = WriteFile(file, text.data(), static_cast<DWORD>(text.size()),
|
||||
&written, nullptr) &&
|
||||
written == text.size() && FlushFileBuffers(file);
|
||||
CloseHandle(file);
|
||||
if (!ok || !MoveFileExW(temporary.c_str(), path.c_str(),
|
||||
MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) {
|
||||
DeleteFileW(temporary.c_str());
|
||||
throw std::runtime_error("Cannot persist private file");
|
||||
}
|
||||
#else
|
||||
int fd =
|
||||
open(temporary.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
|
||||
Require(fd >= 0, "Cannot create private file");
|
||||
size_t offset = 0;
|
||||
while (offset < text.size()) {
|
||||
const auto count = write(fd, text.data() + offset, text.size() - offset);
|
||||
if (count <= 0) break;
|
||||
offset += static_cast<size_t>(count);
|
||||
}
|
||||
bool ok = offset == text.size() && fsync(fd) == 0;
|
||||
close(fd);
|
||||
if (!ok || rename(temporary.c_str(), path.c_str()) != 0) {
|
||||
unlink(temporary.c_str());
|
||||
throw std::runtime_error("Cannot persist private file");
|
||||
}
|
||||
const int directory = open(path.parent_path().c_str(), O_RDONLY | O_DIRECTORY | O_NOFOLLOW);
|
||||
const bool durable = directory >= 0 && fsync(directory) == 0;
|
||||
if (directory >= 0) close(directory);
|
||||
Require(durable, "Cannot persist private directory entry");
|
||||
#endif
|
||||
}
|
||||
namespace {
|
||||
#ifdef _WIN32
|
||||
using FileHandle = std::unique_ptr<void, decltype(&CloseHandle)>;
|
||||
FileHandle OpenPrivatePath(const std::filesystem::path& path, DWORD access) {
|
||||
// Keep the object open without FILE_SHARE_DELETE so validation and legacy
|
||||
// owner repair cannot operate on a replacement at the same path.
|
||||
HANDLE file = CreateFileW(
|
||||
path.c_str(), access, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
|
||||
OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,
|
||||
nullptr);
|
||||
Require(file != INVALID_HANDLE_VALUE, "Cannot open private path");
|
||||
return {file, CloseHandle};
|
||||
}
|
||||
void CheckPrivateWindowsPath(const std::filesystem::path& path) {
|
||||
auto file = OpenPrivatePath(path, READ_CONTROL);
|
||||
BY_HANDLE_FILE_INFORMATION info{};
|
||||
Require(GetFileInformationByHandle(file.get(), &info) &&
|
||||
!(info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT),
|
||||
"private path cannot be a reparse point");
|
||||
auto user = CurrentUser();
|
||||
auto sid = reinterpret_cast<TOKEN_USER*>(user.data())->User.Sid;
|
||||
PSID owner = nullptr;
|
||||
PACL acl = nullptr;
|
||||
PSECURITY_DESCRIPTOR descriptor = nullptr;
|
||||
Require(GetSecurityInfo(
|
||||
file.get(), SE_FILE_OBJECT,
|
||||
OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, &owner,
|
||||
nullptr, &acl, nullptr, &descriptor) == ERROR_SUCCESS,
|
||||
"Cannot read private file permissions");
|
||||
std::unique_ptr<void, decltype(&LocalFree)> security(descriptor, LocalFree);
|
||||
bool ok = acl && acl->AceCount > 0;
|
||||
// Only the exact protected, full-access DACL written by older versions is
|
||||
// eligible for owner repair. Never adopt a shared or foreign-user profile.
|
||||
SECURITY_DESCRIPTOR_CONTROL control{};
|
||||
DWORD revision = 0;
|
||||
bool legacy = owner && IsWellKnownSid(owner, WinBuiltinAdministratorsSid) &&
|
||||
GetSecurityDescriptorControl(descriptor, &control, &revision) &&
|
||||
(control & SE_DACL_PROTECTED) && acl && acl->AceCount == 1;
|
||||
for (DWORD i = 0; ok && i < acl->AceCount; ++i) {
|
||||
void* raw = nullptr;
|
||||
if (!GetAce(acl, i, &raw)) {
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
const auto* ace = static_cast<ACCESS_ALLOWED_ACE*>(raw);
|
||||
ok = ace->Header.AceType == ACCESS_ALLOWED_ACE_TYPE &&
|
||||
EqualSid(const_cast<DWORD*>(&ace->SidStart), sid);
|
||||
const BYTE flags = (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
||||
? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE
|
||||
: 0;
|
||||
legacy =
|
||||
legacy && ace->Mask == FILE_ALL_ACCESS && ace->Header.AceFlags == flags;
|
||||
}
|
||||
Require(ok && owner && (EqualSid(owner, sid) || legacy),
|
||||
"private files must be accessible only by the current user");
|
||||
if (legacy) {
|
||||
auto writable = OpenPrivatePath(path, WRITE_OWNER);
|
||||
BY_HANDLE_FILE_INFORMATION current{};
|
||||
Require(GetFileInformationByHandle(writable.get(), ¤t) &&
|
||||
current.dwVolumeSerialNumber == info.dwVolumeSerialNumber &&
|
||||
current.nFileIndexHigh == info.nFileIndexHigh &&
|
||||
current.nFileIndexLow == info.nFileIndexLow &&
|
||||
SetSecurityInfo(writable.get(), SE_FILE_OBJECT,
|
||||
OWNER_SECURITY_INFORMATION, sid, nullptr,
|
||||
nullptr, nullptr) == ERROR_SUCCESS,
|
||||
"Cannot repair private file owner");
|
||||
}
|
||||
}
|
||||
void CreatePrivateDirectory(const std::filesystem::path& path, bool protect_existing) {
|
||||
if (!std::filesystem::exists(path)) {
|
||||
auto descriptor = PrivateSecurityDescriptor(true);
|
||||
SECURITY_ATTRIBUTES security{sizeof(security), descriptor, FALSE};
|
||||
bool ok = CreateDirectoryW(path.c_str(), &security);
|
||||
LocalFree(descriptor);
|
||||
Require(ok, "Cannot create private directory");
|
||||
}
|
||||
if (protect_existing) {
|
||||
// The cache may predate identity storage and inherit a normal profile ACL.
|
||||
// Elevated older builds may have used Administrators as the owner. The
|
||||
// private-path check below repairs that owner after applying the user ACL.
|
||||
auto file = OpenPrivatePath(path, READ_CONTROL | WRITE_DAC);
|
||||
BY_HANDLE_FILE_INFORMATION info{};
|
||||
Require(GetFileInformationByHandle(file.get(), &info) &&
|
||||
(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
|
||||
!(info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT),
|
||||
"Invalid cache directory");
|
||||
PSID owner = nullptr;
|
||||
PSECURITY_DESCRIPTOR original = nullptr;
|
||||
Require(GetSecurityInfo(file.get(), SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION,
|
||||
&owner, nullptr, nullptr, nullptr, &original) == ERROR_SUCCESS,
|
||||
"Cannot read cache directory owner");
|
||||
std::unique_ptr<void, decltype(&LocalFree)> original_guard(original, LocalFree);
|
||||
auto user = CurrentUser();
|
||||
Require(owner && (EqualSid(owner, reinterpret_cast<TOKEN_USER*>(user.data())->User.Sid) ||
|
||||
IsWellKnownSid(owner, WinBuiltinAdministratorsSid)),
|
||||
"Cache directory belongs to another user");
|
||||
auto descriptor = PrivateSecurityDescriptor(true);
|
||||
std::unique_ptr<void, decltype(&LocalFree)> descriptor_guard(descriptor, LocalFree);
|
||||
BOOL present = FALSE, defaulted = FALSE;
|
||||
PACL acl = nullptr;
|
||||
Require(GetSecurityDescriptorDacl(descriptor, &present, &acl, &defaulted) && present &&
|
||||
SetSecurityInfo(file.get(), SE_FILE_OBJECT,
|
||||
DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION,
|
||||
nullptr, nullptr, acl, nullptr) == ERROR_SUCCESS,
|
||||
"Cannot protect cache directory");
|
||||
}
|
||||
CheckPrivateWindowsPath(path);
|
||||
}
|
||||
#endif
|
||||
} // namespace
|
||||
|
||||
std::string Read(const std::filesystem::path& path) {
|
||||
#ifdef _WIN32
|
||||
CheckPrivateWindowsPath(path);
|
||||
#else
|
||||
struct stat status{};
|
||||
Require(lstat(path.c_str(), &status) == 0 && S_ISREG(status.st_mode) &&
|
||||
status.st_uid == getuid() && (status.st_mode & 0077) == 0,
|
||||
"private file must be owned by this user with mode 0600");
|
||||
#endif
|
||||
Require(std::filesystem::file_size(path) <= 64 * 1024, "private file too large");
|
||||
std::ifstream input(path, std::ios::binary);
|
||||
Require(input.good(), "Cannot read private file");
|
||||
return {std::istreambuf_iterator<char>(input), {}};
|
||||
}
|
||||
|
||||
void EnsureDirectory(const std::filesystem::path& directory, bool protect_existing) {
|
||||
std::filesystem::create_directories(directory.parent_path());
|
||||
#ifdef _WIN32
|
||||
CreatePrivateDirectory(directory, protect_existing);
|
||||
#else
|
||||
std::filesystem::create_directories(directory);
|
||||
struct stat status{};
|
||||
Require(lstat(directory.c_str(), &status) == 0 && S_ISDIR(status.st_mode) &&
|
||||
status.st_uid == getuid(), "Invalid private directory owner or type");
|
||||
Require(chmod(directory.c_str(), 0700) == 0, "Cannot protect private directory");
|
||||
#endif
|
||||
}
|
||||
} // namespace crossdesk::private_file
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-11-01
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _PRIVATE_FILE_H_
|
||||
#define _PRIVATE_FILE_H_
|
||||
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
namespace crossdesk::private_file {
|
||||
// User-only files, atomic replacement, and rejection of shared/linked stores.
|
||||
void EnsureDirectory(const std::filesystem::path& directory,
|
||||
bool protect_existing = false);
|
||||
std::string Read(const std::filesystem::path& path);
|
||||
void Write(const std::filesystem::path& path, const std::string& text);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -36,6 +36,14 @@ ConfigCenter::~ConfigCenter() {}
|
||||
int ConfigCenter::Load() {
|
||||
SI_Error rc = ini_->LoadFile(config_path_.c_str());
|
||||
if (rc < 0) {
|
||||
std::error_code error;
|
||||
if (std::filesystem::exists(config_path_, error) || error) {
|
||||
// An unreadable existing configuration must not silently disable access
|
||||
// restrictions or overwrite the user's settings with default values.
|
||||
trusted_devices_only_.store(true);
|
||||
LOG_ERROR("Cannot read existing configuration; restricting incoming connections");
|
||||
return -1;
|
||||
}
|
||||
Save();
|
||||
return -1;
|
||||
}
|
||||
@@ -141,6 +149,9 @@ int ConfigCenter::Load() {
|
||||
ini_->GetBoolValue(section_, "privacy_local_unlock_allowed", true));
|
||||
lock_on_disconnect_.store(
|
||||
ini_->GetBoolValue(section_, "lock_on_disconnect", true));
|
||||
trusted_devices_only_.store(
|
||||
ini_->GetValue(section_, "trusted_devices_only", nullptr) != nullptr &&
|
||||
ini_->GetBoolValue(section_, "trusted_devices_only", true));
|
||||
save_remote_previews_.store(
|
||||
ini_->GetBoolValue(section_, "save_remote_previews", true));
|
||||
portable_service_prompt_suppressed_ =
|
||||
@@ -239,6 +250,7 @@ int ConfigCenter::Save() {
|
||||
ini_->SetBoolValue(section_, "privacy_local_unlock_allowed",
|
||||
privacy_local_unlock_allowed_.load());
|
||||
ini_->SetBoolValue(section_, "lock_on_disconnect", lock_on_disconnect_.load());
|
||||
ini_->SetBoolValue(section_, "trusted_devices_only", trusted_devices_only_.load());
|
||||
ini_->SetBoolValue(section_, "save_remote_previews",
|
||||
save_remote_previews_.load());
|
||||
ini_->SetBoolValue(section_, "portable_service_prompt_suppressed",
|
||||
@@ -425,6 +437,13 @@ bool ConfigCenter::IsPrivacyLocalUnlockAllowed() const {
|
||||
return privacy_local_unlock_allowed_.load();
|
||||
}
|
||||
|
||||
int ConfigCenter::SetTrustedDevicesOnly(bool enabled) {
|
||||
if (StoreValues({{"trusted_devices_only", enabled ? "true" : "false"}}) != 0) return -1;
|
||||
trusted_devices_only_.store(enabled);
|
||||
return 0;
|
||||
}
|
||||
bool ConfigCenter::IsTrustedDevicesOnly() const { return trusted_devices_only_.load(); }
|
||||
|
||||
int ConfigCenter::SetLockOnDisconnect(bool enabled) {
|
||||
if (StoreValues({{"lock_on_disconnect", enabled ? "true" : "false"}}) != 0)
|
||||
return -1;
|
||||
|
||||
@@ -55,6 +55,7 @@ class ConfigCenter {
|
||||
int SetPrivacyScreen(bool enable_privacy_screen);
|
||||
int SetPrivacyLocalUnlock(bool allowed);
|
||||
int SetLockOnDisconnect(bool enabled);
|
||||
int SetTrustedDevicesOnly(bool enabled);
|
||||
int SetSaveRemotePreviews(bool enabled);
|
||||
int SetPortableServicePromptSuppressed(bool suppressed);
|
||||
int SetSponsorshipPromptDismissedVersion(const std::string& version);
|
||||
@@ -80,6 +81,7 @@ class ConfigCenter {
|
||||
bool IsEnablePrivacyScreen() const;
|
||||
bool IsPrivacyLocalUnlockAllowed() const;
|
||||
bool IsLockOnDisconnect() const;
|
||||
bool IsTrustedDevicesOnly() const;
|
||||
bool IsSaveRemotePreviews() const;
|
||||
bool IsPortableServicePromptSuppressed() const;
|
||||
std::string GetSponsorshipPromptDismissedVersion() const;
|
||||
@@ -113,6 +115,7 @@ class ConfigCenter {
|
||||
std::atomic<bool> enable_privacy_screen_{true};
|
||||
std::atomic<bool> privacy_local_unlock_allowed_{true};
|
||||
std::atomic<bool> lock_on_disconnect_{true};
|
||||
std::atomic<bool> trusted_devices_only_{false};
|
||||
std::atomic<bool> save_remote_previews_{true};
|
||||
bool portable_service_prompt_suppressed_ = false;
|
||||
std::string sponsorship_prompt_dismissed_version_;
|
||||
|
||||
@@ -366,6 +366,19 @@ slint::SharedString UiText(std::string_view value) {
|
||||
return slint::SharedString(value);
|
||||
}
|
||||
|
||||
slint::SharedString TrustFingerprint(std::string fingerprint) {
|
||||
for (size_t i = 8; i < fingerprint.size(); i += 9) fingerprint.insert(i, " ");
|
||||
return UiText(fingerprint);
|
||||
}
|
||||
|
||||
ui::TrustedDeviceEntry TrustRow(const TrustedDevices::Device& device,
|
||||
const std::string& remote = {}) {
|
||||
auto id = device.device_id;
|
||||
if (id.rfind("C-", 0) == 0) id.erase(0, 2);
|
||||
return {UiText(remote), UiText(device.public_key),
|
||||
UiText(device.name.empty() ? id : device.name), UiText(id), {}};
|
||||
}
|
||||
|
||||
void RegisterFontAwesome(slint::Window& window) {
|
||||
if (const auto error = window.window_handle().register_font_from_data(
|
||||
fa_solid_900_ttf, fa_solid_900_ttf_len)) {
|
||||
@@ -1065,6 +1078,11 @@ struct GuiApplication::SlintUi {
|
||||
std::string tab_model_signature;
|
||||
std::vector<std::string> controller_ids;
|
||||
std::string controller_model_signature;
|
||||
std::string trusted_device_signature;
|
||||
std::shared_ptr<slint::VectorModel<ui::TrustedDeviceEntry>> trusted_device_model =
|
||||
std::make_shared<slint::VectorModel<ui::TrustedDeviceEntry>>();
|
||||
std::shared_ptr<slint::VectorModel<ui::TrustedDeviceEntry>> trust_candidate_model =
|
||||
std::make_shared<slint::VectorModel<ui::TrustedDeviceEntry>>();
|
||||
std::string connection_dialog_remote_id;
|
||||
bool connection_password_initialized = false;
|
||||
bool connection_dialog_gallery = false;
|
||||
@@ -1153,6 +1171,7 @@ int GuiApplication::Run() {
|
||||
announcements_.SetStorageDirectory(path_manager_->GetCachePath() /
|
||||
"announcement_reads");
|
||||
config_center_ = std::make_unique<ConfigCenter>(cache_path_ + "/config.ini");
|
||||
InitializeTrustedDevices(path_manager_->GetConfigPath() / "trusted-devices");
|
||||
|
||||
LOG_INFO("CrossDesk version: {} (Slint UI)", CROSSDESK_VERSION);
|
||||
|
||||
@@ -1559,6 +1578,7 @@ void GuiApplication::ResetSettingsUi(bool gallery_window) {
|
||||
config_center_->IsEnablePrivacyScreen());
|
||||
main->set_privacy_local_unlock_allowed(config_center_->IsPrivacyLocalUnlockAllowed());
|
||||
main->set_lock_on_disconnect_enabled(config_center_->IsLockOnDisconnect());
|
||||
main->set_trusted_devices_only(config_center_->IsTrustedDevicesOnly());
|
||||
#ifdef _WIN32
|
||||
main->set_capture_method_visible(true);
|
||||
main->set_capture_method_index(
|
||||
@@ -1932,6 +1952,7 @@ void GuiApplication::BindMainCallbacks(bool gallery_window) {
|
||||
main->set_file_save_path(folder);
|
||||
}
|
||||
});
|
||||
BindTrustedDeviceCallbacks(gallery_window);
|
||||
main->on_save_settings([this, main] {
|
||||
return SaveSettingsFromUi(main->get_gallery_mode());
|
||||
});
|
||||
@@ -2472,6 +2493,7 @@ void GuiApplication::Tick() {
|
||||
return;
|
||||
}
|
||||
TickLan();
|
||||
TickControllerTrust();
|
||||
if (LanAvailable()) {
|
||||
const auto address = lan_->identity().settings().value("address", "");
|
||||
ui_->main->set_lan_listen_all(lan_->identity().listen_all());
|
||||
@@ -2702,6 +2724,7 @@ void GuiApplication::Tick() {
|
||||
}
|
||||
SyncStreamWindow();
|
||||
SyncServerWindow();
|
||||
SyncTrustedDevices();
|
||||
SyncFileTransferNotifications();
|
||||
}
|
||||
|
||||
@@ -2960,6 +2983,7 @@ bool GuiApplication::RequestPasswordChange(const std::string &password) {
|
||||
password_change_result_ready_ = false;
|
||||
password_change_succeeded_ = false;
|
||||
password_change_result_uncertain_ = false;
|
||||
password_change_recovery_ = false;
|
||||
password_change_requested_at_ = std::chrono::steady_clock::now();
|
||||
pending_password_change_request_id_ = request_id;
|
||||
pending_local_password_ = password;
|
||||
@@ -3039,6 +3063,7 @@ void GuiApplication::HandlePasswordChangeResult() {
|
||||
password_change_result_ready_ = false;
|
||||
password_change_succeeded_ = false;
|
||||
password_change_result_uncertain_ = false;
|
||||
password_change_recovery_ = false;
|
||||
pending_password_change_request_id_.clear();
|
||||
pending_local_password_.clear();
|
||||
password_change_error_.clear();
|
||||
@@ -3085,23 +3110,53 @@ void GuiApplication::HandlePasswordChangeResult() {
|
||||
}
|
||||
|
||||
void GuiApplication::HandleCredentialRecovery() {
|
||||
bool retry_active = false;
|
||||
bool promote_pending = false;
|
||||
bool clear_pending = false;
|
||||
CredentialRecoveryAction action;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(password_change_mutex_);
|
||||
retry_active = credential_recovery_retry_active_;
|
||||
promote_pending = credential_recovery_promote_pending_;
|
||||
clear_pending = credential_recovery_clear_pending_;
|
||||
credential_recovery_retry_active_ = false;
|
||||
credential_recovery_promote_pending_ = false;
|
||||
credential_recovery_clear_pending_ = false;
|
||||
if (retry_active) {
|
||||
action = std::exchange(credential_recovery_action_, CredentialRecoveryAction::Idle);
|
||||
if (action == CredentialRecoveryAction::RetryActive) {
|
||||
credential_recovery_attempt_pending_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (retry_active) {
|
||||
if (action == CredentialRecoveryAction::ResumePending) {
|
||||
// Key authentication proves device ownership, not which password committed.
|
||||
// Replay the durable request and promote only its successful server reply.
|
||||
const bool self_hosted = config_center_->IsSelfHosted();
|
||||
std::string request_id;
|
||||
const auto identity = settings_.PendingPasswordChangeIdentity(self_hosted,
|
||||
self_hosted ? config_center_->GetSignalServerHost() : config_center_->GetDefaultServerHost(),
|
||||
self_hosted ? config_center_->GetSignalServerPort() : config_center_->GetDefaultSignalServerPort(),
|
||||
&request_id);
|
||||
const auto at = identity.find('@');
|
||||
if (!peer_ || !signal_connected_ || at == std::string::npos || request_id.empty() ||
|
||||
identity.substr(0, at) != client_id_) return;
|
||||
const auto password = identity.substr(at + 1);
|
||||
{
|
||||
std::lock_guard lock(password_change_mutex_);
|
||||
if (password_change_pending_) return;
|
||||
password_change_pending_ = true;
|
||||
password_change_result_ready_ = false;
|
||||
password_change_succeeded_ = false;
|
||||
password_change_result_uncertain_ = false;
|
||||
password_change_recovery_ = true;
|
||||
password_change_requested_at_ = std::chrono::steady_clock::now();
|
||||
pending_password_change_request_id_ = request_id;
|
||||
pending_local_password_ = password;
|
||||
password_change_error_.clear();
|
||||
}
|
||||
const auto message = nlohmann::json{{"type", "change_password"},
|
||||
{"request_id", request_id}, {"new_password", password}}.dump();
|
||||
if (SendSignalMessage(peer_, message.data(), message.size()) != 0) {
|
||||
std::lock_guard lock(password_change_mutex_);
|
||||
password_change_result_ready_ = true;
|
||||
password_change_result_uncertain_ = true;
|
||||
password_change_error_ = "Password recovery connection unavailable";
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (action == CredentialRecoveryAction::RetryActive) {
|
||||
LOG_INFO("Pending credential was not accepted; retrying active credential");
|
||||
if (peer_) {
|
||||
CloseConnectionPeer();
|
||||
@@ -3109,7 +3164,7 @@ void GuiApplication::HandleCredentialRecovery() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (promote_pending) {
|
||||
if (action == CredentialRecoveryAction::PromotePending) {
|
||||
#if defined(__linux__) && !defined(__OHOS__)
|
||||
HeadlessConsole::Instance().NotifyKey("console_password_recovered");
|
||||
#endif
|
||||
@@ -3118,7 +3173,7 @@ void GuiApplication::HandleCredentialRecovery() {
|
||||
} else {
|
||||
LOG_INFO("Recovered password change with pending credential");
|
||||
}
|
||||
} else if (clear_pending) {
|
||||
} else if (action == CredentialRecoveryAction::ClearPending) {
|
||||
#if defined(__linux__) && !defined(__OHOS__)
|
||||
HeadlessConsole::Instance().NotifyKey("console_password_retained");
|
||||
#endif
|
||||
@@ -4258,6 +4313,113 @@ void GuiApplication::PositionServerWindows() {
|
||||
}
|
||||
}
|
||||
|
||||
void GuiApplication::BindTrustedDeviceCallbacks(bool gallery_window) {
|
||||
const auto main = ui_->Window(gallery_window);
|
||||
main->on_trust_action([this, main](
|
||||
slint::SharedString action, slint::SharedString id, slint::SharedString key) {
|
||||
const auto message = [this](const char* text) {
|
||||
return UiText(localization::Literal(text, localization_language_index_));
|
||||
};
|
||||
const bool copying = action == "copy";
|
||||
const auto set_error = [&](const char* text) {
|
||||
if (copying) main->set_trust_copy_error(message(text));
|
||||
else main->set_trusted_devices_error(message(text));
|
||||
};
|
||||
if (copying || action == "open" || action == "close") {
|
||||
main->set_trust_copy_error("");
|
||||
main->set_trust_copy_notice("");
|
||||
}
|
||||
if (!copying) {
|
||||
main->set_trusted_devices_error("");
|
||||
main->set_trusted_devices_notice("");
|
||||
}
|
||||
if (action == "open" || action == "close") {
|
||||
main->set_trust_pairing_input("");
|
||||
SyncTrustedDevices();
|
||||
return;
|
||||
}
|
||||
if (!trusted_devices_) {
|
||||
set_error("无法读取设备身份或可信列表,请修复后再启用。");
|
||||
return;
|
||||
}
|
||||
if (action == "edit") return;
|
||||
try {
|
||||
if (copying) {
|
||||
const auto text = trusted_devices_->PairingInfo(client_id_, GetHostName());
|
||||
if (SDL_SetClipboardText(text.c_str()))
|
||||
main->set_trust_copy_notice(message("配对信息已复制。"));
|
||||
else set_error("无法访问剪贴板,请重试。");
|
||||
} else if (action == "import") {
|
||||
const auto input = std::string(main->get_trust_pairing_input());
|
||||
if (input.empty()) return;
|
||||
const auto device = TrustedDevices::ParsePairingInfo(input);
|
||||
if (!device) {
|
||||
set_error("配对信息无效,请从控制端重新复制。");
|
||||
return;
|
||||
}
|
||||
// The Add action approves the current input. Import only updates the
|
||||
// list; a live connection must still complete its own authentication.
|
||||
if (trusted_devices_->Contains(device->public_key)) {
|
||||
main->set_trusted_devices_notice(message("已在可信列表中"));
|
||||
} else {
|
||||
trusted_devices_->Add(*device);
|
||||
main->set_trusted_devices_notice(message("已加入可信设备。"));
|
||||
}
|
||||
main->set_trust_pairing_input("");
|
||||
} else if ((action == "add" && !TrustController(std::string(id), std::string(key))) ||
|
||||
(action == "remove" && !RemoveTrustedDevice(std::string(key)))) {
|
||||
set_error("无法更新可信设备,请检查列表容量和存储权限后重试。");
|
||||
}
|
||||
} catch (const std::exception&) {
|
||||
set_error(copying ? "无法复制配对信息,请重试。"
|
||||
: "无法更新可信设备,请检查列表容量和存储权限后重试。");
|
||||
}
|
||||
SyncTrustedDevices();
|
||||
});
|
||||
}
|
||||
|
||||
void GuiApplication::SyncTrustedDevices() {
|
||||
if (!ui_->main->get_trusted_devices_open() && !ui_->wall->get_trusted_devices_open()) return;
|
||||
std::vector<ui::TrustedDeviceEntry> saved, candidates;
|
||||
if (trusted_devices_) {
|
||||
for (const auto& device : trusted_devices_->List()) saved.push_back(TrustRow(device));
|
||||
for (const auto& id : ConnectedControllers())
|
||||
if (const auto device = VerifiedController(id);
|
||||
device && !trusted_devices_->Contains(device->public_key))
|
||||
candidates.push_back(TrustRow(*device, id));
|
||||
}
|
||||
std::sort(candidates.begin(), candidates.end(), [](const auto& a, const auto& b) {
|
||||
return std::string(a.remote_id) < std::string(b.remote_id);
|
||||
});
|
||||
std::string signature = trusted_devices_ ? "ready" : "unavailable";
|
||||
for (const auto* rows : {&saved, &candidates}) {
|
||||
signature += "|";
|
||||
for (const auto& device : *rows)
|
||||
signature += std::string(device.remote_id) + '\0' + std::string(device.public_key) + '\0' +
|
||||
std::string(device.display_name) + '\0' + std::string(device.device_id) + '\0';
|
||||
}
|
||||
if (signature != ui_->trusted_device_signature) {
|
||||
// Tick runs frequently. Hash keys and update Slint only when the rows change.
|
||||
for (auto* rows : {&saved, &candidates})
|
||||
for (auto& device : *rows)
|
||||
device.fingerprint = TrustFingerprint(TrustedDevices::Fingerprint(std::string(device.public_key)));
|
||||
ui_->trusted_device_model->set_vector(std::move(saved));
|
||||
ui_->trust_candidate_model->set_vector(std::move(candidates));
|
||||
ui_->trusted_device_signature = std::move(signature);
|
||||
const auto fingerprint = TrustFingerprint(trusted_devices_ ? trusted_devices_->Fingerprint() : "");
|
||||
for (auto& window : {ui_->main, ui_->wall}) {
|
||||
window->set_trusted_devices(ui_->trusted_device_model);
|
||||
window->set_trust_candidates(ui_->trust_candidate_model);
|
||||
window->set_device_fingerprint(fingerprint);
|
||||
}
|
||||
}
|
||||
if (!trusted_devices_) {
|
||||
for (auto& window : {ui_->main, ui_->wall})
|
||||
window->set_trusted_devices_error(UiText(localization::Literal(
|
||||
"无法读取设备身份或可信列表,请修复后再启用。", localization_language_index_)));
|
||||
}
|
||||
}
|
||||
|
||||
void GuiApplication::SyncServerWindow() {
|
||||
// The connection map is authoritative. Lifecycle flags record callback
|
||||
// intent, but callbacks for different controllers can cross each other, so
|
||||
@@ -4610,6 +4772,11 @@ bool GuiApplication::SaveSettingsFromUi(bool gallery_window) {
|
||||
const auto previous_connection_mode = config_center_->GetConnectionMode();
|
||||
auto& main = ui_->Window(gallery_window);
|
||||
main->set_settings_save_error("");
|
||||
if (main->get_trusted_devices_only() && !trusted_devices_) {
|
||||
main->set_settings_save_error(UiText(localization::Literal(
|
||||
"无法读取设备身份或可信列表,请修复后再启用。", localization_language_index_)));
|
||||
return false;
|
||||
}
|
||||
const bool has_endpoint = main->get_self_hosted_enabled() ||
|
||||
!std::string(main->get_server_host()).empty() ||
|
||||
!std::string(main->get_server_port()).empty();
|
||||
@@ -4769,6 +4936,12 @@ bool GuiApplication::SaveSettingsFromUi(bool gallery_window) {
|
||||
props->preview_needs_sync_ = true;
|
||||
ui_->gallery_synced = {};
|
||||
}
|
||||
if (config_center_->SetTrustedDevicesOnly(main->get_trusted_devices_only()) != 0) {
|
||||
main->set_settings_save_error(UiText(localization::Literal(
|
||||
"无法保存可信设备设置,请重试。", localization_language_index_)));
|
||||
return false;
|
||||
}
|
||||
TickControllerTrust();
|
||||
ApplySettingsFromConfig(previous_server !=
|
||||
GetSignalServerSettings(*config_center_));
|
||||
ResetSettingsUi(gallery_window);
|
||||
|
||||
@@ -54,6 +54,7 @@ private:
|
||||
void ShowMainWindow(const std::string& activation_token = {});
|
||||
bool MinimizeMainWindowToTray();
|
||||
void BindMainCallbacks(bool gallery_window = false);
|
||||
void BindTrustedDeviceCallbacks(bool gallery_window = false);
|
||||
void BindStreamCallbacks();
|
||||
void BindServerCallbacks();
|
||||
void Tick();
|
||||
@@ -92,6 +93,7 @@ private:
|
||||
void SyncStreamKeyboardFocus();
|
||||
void SetStreamKeyboardFocus(bool focused);
|
||||
void SyncServerWindow();
|
||||
void SyncTrustedDevices();
|
||||
void SyncFileTransferNotifications();
|
||||
void CancelFileTransfer(uint32_t file_id);
|
||||
void SetServerPanelExpanded(bool expanded);
|
||||
|
||||
@@ -386,7 +386,7 @@ inline constexpr TranslationRow kTranslationRows[] = {
|
||||
{"设置(修改后立即保存)", {"设置(修改后立即保存)", "Settings (each change is saved immediately)", "Настройки (изменения сохраняются сразу)", "設定(修改後立即儲存)", "設定 (各変更はすぐに保存されます)", "설정(각 변경 사항은 즉시 저장됩니다)", "Einstellungen (jede Änderung wird sofort gespeichert)", "Paramètres (chaque modification est enregistrée immédiatement)", "Configuración (cada cambio se guarda inmediatamente)", "Configurações (cada alteração é salva imediatamente)"}},
|
||||
{"访问官网: ", {"访问官网: ", "Access Website: ", "Официальный сайт: ", "訪問官網: ", "ウェブサイトにアクセス: ", "웹사이트 접속: ", "Zugriff auf die Website: ", "Accéder au site Web : ", "Acceder al sitio web: ", "Acesse o site: "}},
|
||||
{"访问密码必须为 6 位英文字母或数字,区分大小写。", {"访问密码必须为 6 位英文字母或数字,区分大小写。", "Use exactly 6 letters (A–Z, a–z) or digits (0–9). Case-sensitive.", "Пароль: ровно 6 латинских букв или цифр. Регистр учитывается.", "訪問密碼必須為 6 位英文字母或數字,區分大小寫。", "正確に 6 つの文字 (A ~ Z、a ~ z) または数字 (0 ~ 9) を使用してください。大文字と小文字を区別。", "정확히 6개의 문자(A–Z, a–z) 또는 숫자(0–9)를 사용하세요. 대소문자를 구분합니다.", "Verwenden Sie genau 6 Buchstaben (A–Z, a–z) oder Ziffern (0–9). Groß- und Kleinschreibung beachten.", "Utilisez exactement 6 lettres (A–Z, a–z) ou chiffres (0–9). Sensible aux majuscules et minuscules.", "Utilice exactamente 6 letras (A–Z, a–z) o dígitos (0–9). Distingue mayúsculas y minúsculas.", "Use exatamente 6 letras (A–Z, a–z) ou dígitos (0–9). Maiúsculas e minúsculas."}},
|
||||
{"证书验证失败,请重新安装自托管根证书", {"证书验证失败,请重新安装自托管根证书", "Certificate verification failed. Reinstall the self-hosted root certificate.", "Ошибка проверки сертификата. Переустановите корневой сертификат собственного сервера.", "證書驗證失敗,請重新安裝自託管根證書", "証明書の検証に失敗しました。セルフホスト型ルート証明書を再インストールします。", "인증서 확인에 실패했습니다. 자체 호스팅 루트 인증서를 다시 설치합니다.", "Die Zertifikatsüberprüfung ist fehlgeschlagen. Installieren Sie das selbstgehostete Stammzertifikat neu.", "La vérification du certificat a échoué. Réinstallez le certificat racine auto-hébergé.", "Falló la verificación del certificado. Reinstale el certificado raíz autohospedado.", "Falha na verificação do certificado. Reinstale o certificado raiz auto-hospedado."}},
|
||||
{"服务器证书无效或已过期,正在自动重试", {"服务器证书无效或已过期,正在自动重试", "Server certificate invalid or expired. Retrying automatically.", "Сертификат сервера недействителен или просрочен. Повторяем подключение автоматически.", "伺服器憑證無效或已過期,正在自動重試", "サーバー証明書が無効か期限切れです。自動的に再試行します。", "서버 인증서가 유효하지 않거나 만료되었습니다. 자동으로 재시도합니다.", "Serverzertifikat ungültig oder abgelaufen. Automatischer Neuversuch.", "Certificat du serveur invalide ou expiré. Nouvelle tentative automatique.", "Certificado del servidor no válido o caducado. Reintentando automáticamente.", "Certificado do servidor inválido ou expirado. Tentando novamente automaticamente."}},
|
||||
{"该分组暂无近期连接", {"该分组暂无近期连接", "No recent connections in this group", "В этой группе нет недавних подключений", "該分組暫無近期連線", "このグループには最近の接続がありません", "이 그룹에는 최근 연결이 없습니다.", "Keine aktuellen Verbindungen in dieser Gruppe", "Aucune connexion récente dans ce groupe", "No hay conexiones recientes en este grupo.", "Nenhuma conexão recente neste grupo"}},
|
||||
{"该应用需要授权以下权限:", {"该应用需要授权以下权限:", "The application requires the following permissions:", "Для работы приложения требуются следующие разрешения:", "該應用需要授權以下許可權:", "アプリケーションには次の権限が必要です。", "애플리케이션에는 다음 권한이 필요합니다.", "Die Anwendung erfordert die folgenden Berechtigungen:", "L'application nécessite les autorisations suivantes :", "La aplicación requiere los siguientes permisos:", "O aplicativo requer as seguintes permissões:"}},
|
||||
{"该设备正在连接,请稍候", {"该设备正在连接,请稍候", "This device is already connecting", "Устройство уже подключается", "該裝置正在連線,請稍候", "このデバイスはすでに接続されています", "이 장치는 이미 연결되어 있습니다", "Dieses Gerät stellt bereits eine Verbindung her", "Cet appareil est déjà connecté", "Este dispositivo ya se está conectando", "Este dispositivo já está conectado"}},
|
||||
@@ -494,6 +494,31 @@ inline constexpr TranslationRow kTranslationRows[] = {
|
||||
{":同步键鼠到所有已连接设备", {":同步键鼠到所有已连接设备", ": mirror input to all connected devices", ": ввод на всех подключённых устройствах", ":同步鍵鼠到所有已連線裝置", ": 接続されているすべてのデバイスへのミラー入力", ": 연결된 모든 장치에 대한 미러 입력", ": Eingang auf alle angeschlossenen Geräte spiegeln", ": entrée miroir sur tous les appareils connectés", ": entrada espejo para todos los dispositivos conectados", ": entrada espelhada para todos os dispositivos conectados"}},
|
||||
{"监听所有 IP 接口", {"监听所有 IP 接口", "Listen on all IP interfaces", "Все IP-интерфейсы", "監聽所有 IP 介面", "すべての IP で待ち受け", "모든 IP 인터페이스 수신", "Alle IP-Schnittstellen", "Toutes les interfaces IP", "Todas las interfaces IP", "Todas as interfaces IP"}},
|
||||
{"所有接口共用一个端口,包括 VPN。分享时请选择实际 IP;关闭后恢复各 IP 的独立端口。", {"所有接口共用一个端口,包括 VPN。分享时请选择实际 IP;关闭后恢复各 IP 的独立端口。", "Use one port on all interfaces, including VPNs. Select an actual IP to share. Turn off to restore separate ports per IP.", "Один порт для всех интерфейсов, включая VPN. Для подключения выберите реальный IP. Отключение вернёт отдельные порты для каждого IP.", "所有介面共用一個連接埠,包括 VPN。分享時請選擇實際 IP;關閉後恢復各 IP 的獨立連接埠。", "VPN を含む全インターフェースで同じポートを使います。共有には実際の IP を選択してください。オフにすると IP ごとのポートに戻ります。", "VPN을 포함한 모든 인터페이스가 하나의 포트를 사용합니다. 공유할 실제 IP를 선택하세요. 끄면 IP별 포트가 복원됩니다.", "Ein Port für alle Schnittstellen, einschließlich VPN. Zum Teilen eine tatsächliche IP wählen. Ausschalten stellt die Ports pro IP wieder her.", "Un port commun à toutes les interfaces, VPN inclus. Choisissez une IP réelle à partager. Désactivez pour rétablir les ports propres à chaque IP.", "Un puerto para todas las interfaces, incluidas las VPN. Seleccione una IP real para compartir. Desactive para restaurar los puertos de cada IP.", "Uma porta para todas as interfaces, incluindo VPNs. Selecione um IP real para compartilhar. Desative para restaurar as portas de cada IP."}},
|
||||
{"可信设备", {"可信设备", "Trusted devices", "Доверенные устройства", "可信裝置", "信頼済みデバイス", "신뢰하는 기기", "Vertrauenswürdige Geräte", "Appareils de confiance", "Dispositivos de confianza", "Dispositivos confiáveis"}},
|
||||
{"仅允许可信设备控制", {"仅允许可信设备控制", "Only allow trusted devices", "Разрешать только доверенные устройства", "僅允許可信裝置控制", "信頼済みデバイスのみ許可", "신뢰하는 기기만 제어 허용", "Nur vertrauenswürdige Geräte zulassen", "Autoriser uniquement les appareils de confiance", "Permitir solo dispositivos de confianza", "Permitir apenas dispositivos confiáveis"}},
|
||||
{"管理可信设备", {"管理可信设备", "Manage trusted devices", "Управление доверенными устройствами", "管理可信裝置", "信頼済みデバイスを管理", "신뢰하는 기기 관리", "Vertrauenswürdige Geräte verwalten", "Gérer les appareils de confiance", "Administrar dispositivos de confianza", "Gerenciar dispositivos confiáveis"}},
|
||||
{"可添加的已连接设备", {"可添加的已连接设备", "Connected devices available to trust", "Подключённые устройства для добавления", "可新增的已連線裝置", "追加可能な接続中のデバイス", "추가 가능한 연결된 기기", "Verbundene Geräte zum Hinzufügen", "Appareils connectés à ajouter", "Dispositivos conectados que puedes añadir", "Dispositivos conectados disponíveis"}},
|
||||
{"信任此设备", {"信任此设备", "Trust this device", "Доверять устройству", "信任此裝置", "このデバイスを信頼", "이 기기 신뢰", "Diesem Gerät vertrauen", "Faire confiance à cet appareil", "Confiar en este dispositivo", "Confiar neste dispositivo"}},
|
||||
{"移除信任", {"移除信任", "Remove trust", "Отозвать доверие", "移除信任", "信頼を解除", "신뢰 해제", "Vertrauen entfernen", "Retirer la confiance", "Retirar confianza", "Remover confiança"}},
|
||||
{"无法读取设备身份或可信列表,请修复后再启用。", {"无法读取设备身份或可信列表,请修复后再启用。", "Could not load the device identity or trusted list. Repair the local data before enabling this restriction.", "Не удалось загрузить идентификатор устройства или список доверия. Восстановите локальные данные перед включением ограничения.", "無法讀取裝置身分或可信清單,請修復後再啟用。", "デバイス情報または信頼リストを読み込めません。ローカルデータを修復してから制限を有効にしてください。", "기기 ID 또는 신뢰 목록을 불러올 수 없습니다. 로컬 데이터를 복구한 후 제한을 켜세요.", "Geräteidentität oder Vertrauensliste konnte nicht geladen werden. Lokale Daten vor dem Aktivieren reparieren.", "Impossible de charger l’identité ou la liste de confiance. Réparez les données locales avant d’activer la restriction.", "No se pudo cargar la identidad o la lista de confianza. Repara los datos locales antes de activar la restricción.", "Não foi possível carregar a identidade ou a lista confiável. Repare os dados locais antes de ativar a restrição."}},
|
||||
{"无法保存可信设备设置,请重试。", {"无法保存可信设备设置,请重试。", "Could not save trusted device settings. Try again.", "Не удалось сохранить настройки доверенных устройств. Повторите попытку.", "無法儲存可信裝置設定,請重試。", "信頼済みデバイスの設定を保存できません。再試行してください。", "신뢰하는 기기 설정을 저장할 수 없습니다. 다시 시도하세요.", "Einstellungen für vertrauenswürdige Geräte konnten nicht gespeichert werden. Erneut versuchen.", "Impossible d’enregistrer les paramètres de confiance. Réessayez.", "No se pudo guardar la configuración de confianza. Inténtalo de nuevo.", "Não foi possível salvar as configurações de confiança. Tente novamente."}},
|
||||
{"粘贴", {"粘贴", "Paste", "Вставить", "貼上", "貼り付け", "붙여넣기", "Einfügen", "Coller", "Pegar", "Colar"}},
|
||||
{"粘贴控制端的配对信息", {"粘贴控制端的配对信息", "Paste the controller’s pairing information", "Вставьте данные сопряжения управляющего устройства", "貼上控制端的配對資訊", "操作側のペアリング情報を貼り付け", "제어 장치의 페어링 정보 붙여넣기", "Kopplungsdaten des steuernden Geräts einfügen", "Collez les informations d’association de l’appareil de contrôle", "Pegue la información de emparejamiento del dispositivo de control", "Cole as informações de pareamento do dispositivo controlador"}},
|
||||
{"已在可信列表中", {"已在可信列表中", "Already trusted", "Уже доверенное", "已在可信清單中", "信頼済み", "이미 신뢰하는 장치", "Bereits vertrauenswürdig", "Déjà approuvé", "Ya es de confianza", "Já é confiável"}},
|
||||
{"无法访问剪贴板,请重试。", {"无法访问剪贴板,请重试。", "Cannot access the clipboard. Please try again.", "Не удалось получить доступ к буферу обмена. Повторите попытку.", "無法存取剪貼簿,請重試。", "クリップボードにアクセスできません。もう一度お試しください。", "클립보드에 접근할 수 없습니다. 다시 시도하세요.", "Kein Zugriff auf die Zwischenablage. Bitte erneut versuchen.", "Impossible d’accéder au presse-papiers. Réessayez.", "No se puede acceder al portapapeles. Inténtelo de nuevo.", "Não foi possível acessar a área de transferência. Tente novamente."}},
|
||||
{"配对信息无效,请从控制端重新复制。", {"配对信息无效,请从控制端重新复制。", "Invalid pairing information. Copy it again from the controller.", "Недопустимые данные сопряжения. Скопируйте их заново с управляющего устройства.", "配對資訊無效,請從控制端重新複製。", "ペアリング情報が無効です。操作側からコピーし直してください。", "유효하지 않은 페어링 정보입니다. 제어 장치에서 다시 복사하세요.", "Ungültige Kopplungsdaten. Kopieren Sie sie erneut vom steuernden Gerät.", "Informations d’association invalides. Copiez-les à nouveau depuis l’appareil de contrôle.", "Información de emparejamiento no válida. Cópiela de nuevo del dispositivo de control.", "Informações de pareamento inválidas. Copie-as novamente do dispositivo controlador."}},
|
||||
{"已加入可信设备。", {"已加入可信设备。", "Device added to the trusted list.", "Устройство добавлено в список доверенных.", "已加入可信裝置。", "信頼済みデバイスに追加しました。", "신뢰 장치 목록에 추가했습니다.", "Gerät zur Vertrauensliste hinzugefügt.", "Appareil ajouté à la liste de confiance.", "Dispositivo añadido a la lista de confianza.", "Dispositivo adicionado à lista de confiança."}},
|
||||
{"无法更新可信设备,请检查列表容量和存储权限后重试。", {"无法更新可信设备,请检查列表容量和存储权限后重试。", "Cannot update trusted devices. Check the list capacity and storage permissions, then try again.", "Не удалось обновить список доверенных устройств. Проверьте его размер и права доступа к хранилищу.", "無法更新可信裝置,請檢查清單容量和儲存權限後重試。", "信頼済みデバイスを更新できません。リストの上限と保存先の権限を確認してください。", "신뢰 장치를 업데이트할 수 없습니다. 목록 용량과 저장 권한을 확인한 후 다시 시도하세요.", "Vertrauensliste konnte nicht aktualisiert werden. Prüfen Sie die Listenbegrenzung und Speicherberechtigungen.", "Impossible de mettre à jour les appareils de confiance. Vérifiez la capacité de la liste et les autorisations de stockage.", "No se pueden actualizar los dispositivos de confianza. Compruebe la capacidad de la lista y los permisos de almacenamiento.", "Não foi possível atualizar os dispositivos confiáveis. Verifique a capacidade da lista e as permissões de armazenamento."}},
|
||||
{"复制配对信息", {"复制配对信息", "Copy pairing info", "Копировать данные", "複製配對資訊", "ペアリング情報をコピー", "페어링 정보 복사", "Kopplungsdaten kopieren", "Copier les informations", "Copiar información", "Copiar informações"}},
|
||||
{"添加可信设备", {"添加可信设备", "Add a trusted device", "Добавить доверенное устройство", "新增可信裝置", "信頼するデバイスを追加", "신뢰 장치 추가", "Vertrauenswürdiges Gerät hinzufügen", "Ajouter un appareil de confiance", "Añadir dispositivo de confianza", "Adicionar dispositivo confiável"}},
|
||||
{"已信任的设备", {"已信任的设备", "Trusted devices", "Доверенные устройства", "已信任的裝置", "信頼済みデバイス", "신뢰하는 장치", "Vertrauenswürdige Geräte", "Appareils de confiance", "Dispositivos de confianza", "Dispositivos confiáveis"}},
|
||||
{"本机配对信息", {"本机配对信息", "This device’s pairing info", "Данные этого устройства", "本機配對資訊", "このデバイスのペアリング情報", "이 장치의 페어링 정보", "Kopplungsdaten dieses Geräts", "Informations de cet appareil", "Información de este dispositivo", "Informações deste dispositivo"}},
|
||||
{"查看指纹", {"查看指纹", "View fingerprint", "Показать отпечаток", "查看指紋", "フィンガープリントを表示", "지문 보기", "Fingerabdruck anzeigen", "Afficher l’empreinte", "Ver huella", "Ver impressão digital"}},
|
||||
{"收起指纹", {"收起指纹", "Hide fingerprint", "Скрыть отпечаток", "收起指紋", "フィンガープリントを隠す", "지문 숨기기", "Fingerabdruck ausblenden", "Masquer l’empreinte", "Ocultar huella", "Ocultar impressão digital"}},
|
||||
{"暂无可信设备", {"暂无可信设备", "No trusted devices", "Нет доверенных устройств", "暫無可信裝置", "信頼済みデバイスはありません", "신뢰하는 장치 없음", "Keine vertrauenswürdigen Geräte", "Aucun appareil de confiance", "No hay dispositivos de confianza", "Nenhum dispositivo confiável"}},
|
||||
{"请核对控制端指纹。", {"请核对控制端指纹。", "Check the fingerprint on the controller.", "Сверьте отпечаток с управляющим устройством.", "請核對控制端指紋。", "操作側のフィンガープリントと照合してください。", "제어 장치의 지문과 대조하세요.", "Mit dem Fingerabdruck des steuernden Geräts vergleichen.", "Vérifiez l’empreinte sur l’appareil de contrôle.", "Verifique la huella en el dispositivo de control.", "Confira a impressão digital no dispositivo controlador."}},
|
||||
{"配对信息已复制。", {"配对信息已复制。", "Pairing information copied.", "Данные сопряжения скопированы.", "配對資訊已複製。", "ペアリング情報をコピーしました。", "페어링 정보를 복사했습니다.", "Kopplungsdaten kopiert.", "Informations d’association copiées.", "Información de emparejamiento copiada.", "Informações de pareamento copiadas."}},
|
||||
{"无法复制配对信息,请重试。", {"无法复制配对信息,请重试。", "Unable to copy pairing information. Please try again.", "Не удалось скопировать данные сопряжения. Повторите попытку.", "無法複製配對資訊,請重試。", "ペアリング情報をコピーできません。もう一度お試しください。", "페어링 정보를 복사할 수 없습니다. 다시 시도하세요.", "Kopplungsdaten konnten nicht kopiert werden. Bitte erneut versuchen.", "Impossible de copier les informations d’association. Réessayez.", "No se pudo copiar la información de emparejamiento. Inténtelo de nuevo.", "Não foi possível copiar as informações de pareamento. Tente novamente."}},
|
||||
};
|
||||
|
||||
struct NamedTranslation { const char* name; const char* literal; };
|
||||
@@ -676,7 +701,7 @@ inline constexpr NamedTranslation kNamedTranslations[] = {
|
||||
{"signal_disconnected", "未连接服务器"},
|
||||
{"signal_reconnect", "重新连接"},
|
||||
{"signal_reconnecting", "正在重新连接..."},
|
||||
{"signal_tls_cert_error", "证书验证失败,请重新安装自托管根证书"},
|
||||
{"signal_tls_cert_error", "服务器证书无效或已过期,正在自动重试"},
|
||||
{"p2p_connected", "对等连接已建立"},
|
||||
{"p2p_disconnected", "对等连接已断开"},
|
||||
{"p2p_connecting", "正在建立对等连接..."},
|
||||
|
||||
@@ -99,8 +99,9 @@ void FileTransferManager::ProcessQueue(std::shared_ptr<RemoteSession> props) {
|
||||
peer = events ? events->DirectPeer() : nullptr;
|
||||
}
|
||||
auto session = std::make_shared<file_transfer::Session>(
|
||||
[peer, events, file, props](const char*, const char* data, size_t size) {
|
||||
if (!peer || !events || (props && props->closing_)) return -1;
|
||||
[this, peer, events, file, props](const char*, const char* data, size_t size) {
|
||||
if (!peer || !events || (props && props->closing_) ||
|
||||
(!props && !owner_.IsAuthorizedController(file.remote_id))) return -1;
|
||||
auto callback = events->EnterCallback();
|
||||
if (!callback || !events->DataAllowed()) return -1;
|
||||
const auto wire_id = events->WireId(file.remote_id);
|
||||
@@ -341,7 +342,8 @@ void FileTransferManager::Receive(const char* data, size_t size,
|
||||
(const file_transfer::ReceiveState& progress) mutable {
|
||||
UpdateReceiveProgress(progress, notifications, remote);
|
||||
});
|
||||
entry.session->SetOnSendAck([peer, events, remote](const FileTransferAck& ack) {
|
||||
entry.session->SetOnSendAck([this, peer, events, remote, props](const FileTransferAck& ack) {
|
||||
if (!props && !owner_.IsAuthorizedController(remote)) return -1;
|
||||
auto callback = events->EnterCallback();
|
||||
if (!callback || !events->DataAllowed()) return -1;
|
||||
const auto bytes = EncodeFileTransferAck(ack);
|
||||
|
||||
@@ -593,14 +593,17 @@ bool SettingsManager::StagePendingPasswordChange(
|
||||
}
|
||||
|
||||
std::string SettingsManager::PendingPasswordChangeIdentity(
|
||||
bool self_hosted, const std::string &server_host, int server_port) const {
|
||||
bool self_hosted, const std::string &server_host, int server_port,
|
||||
std::string* request_id) const {
|
||||
std::lock_guard<std::mutex> lock(cache_mutex_);
|
||||
if (request_id) request_id->clear();
|
||||
if (cache_v3_.pending_identity[0] == '\0' ||
|
||||
cache_v3_.pending_self_hosted != self_hosted ||
|
||||
cache_v3_.pending_server_port != server_port ||
|
||||
server_host != cache_v3_.pending_server_host) {
|
||||
return {};
|
||||
}
|
||||
if (request_id) *request_id = cache_v3_.pending_request_id;
|
||||
return cache_v3_.pending_identity;
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ public:
|
||||
const std::string &request_id);
|
||||
std::string PendingPasswordChangeIdentity(
|
||||
bool self_hosted, const std::string &server_host,
|
||||
int server_port) const;
|
||||
int server_port, std::string* request_id = nullptr) const;
|
||||
bool PromotePendingPasswordChange();
|
||||
bool ClearPendingPasswordChange();
|
||||
|
||||
|
||||
@@ -443,6 +443,12 @@ void GuiRuntime::CloseConnectionPeer() {
|
||||
announcements_.Reset();
|
||||
return;
|
||||
}
|
||||
{
|
||||
std::lock_guard lock(controller_trust_mutex_);
|
||||
std::erase_if(controller_trust_, [this](const auto& item) {
|
||||
return item.second.events == peer_events_.get();
|
||||
});
|
||||
}
|
||||
peer_events_->StopAccepting();
|
||||
transfers_.Cancel(peer_events_.get());
|
||||
announcements_.Reset();
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
#include "runtime/device_identity_store.h"
|
||||
|
||||
#include <openssl/crypto.h>
|
||||
|
||||
#include <stdexcept>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <fcntl.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "private_file.h"
|
||||
|
||||
namespace crossdesk::device_identity_store {
|
||||
namespace {
|
||||
void Require(bool ok) {
|
||||
if (!ok) throw std::runtime_error("Device identity store unavailable or invalid");
|
||||
}
|
||||
|
||||
// Keep the lock file's inode stable; it contains no credentials.
|
||||
class StoreLock {
|
||||
public:
|
||||
explicit StoreLock(const std::filesystem::path& path) {
|
||||
#ifdef _WIN32
|
||||
handle_ = CreateFileW(path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr,
|
||||
OPEN_ALWAYS, FILE_FLAG_OPEN_REPARSE_POINT, nullptr);
|
||||
BY_HANDLE_FILE_INFORMATION info{};
|
||||
if (handle_ == INVALID_HANDLE_VALUE || !GetFileInformationByHandle(handle_, &info) ||
|
||||
(info.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT))) {
|
||||
if (handle_ != INVALID_HANDLE_VALUE) CloseHandle(handle_);
|
||||
throw std::runtime_error("Device identity store unavailable");
|
||||
}
|
||||
#else
|
||||
handle_ = open(path.c_str(), O_CREAT | O_RDWR | O_NOFOLLOW | O_CLOEXEC, 0600);
|
||||
struct stat info{};
|
||||
if (handle_ < 0 || fstat(handle_, &info) != 0 || !S_ISREG(info.st_mode) ||
|
||||
info.st_uid != getuid() || (info.st_mode & 0077) != 0 ||
|
||||
flock(handle_, LOCK_EX | LOCK_NB) != 0) {
|
||||
if (handle_ >= 0) close(handle_);
|
||||
throw std::runtime_error("Device identity store unavailable");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
~StoreLock() {
|
||||
#ifdef _WIN32
|
||||
CloseHandle(handle_);
|
||||
#else
|
||||
close(handle_);
|
||||
#endif
|
||||
}
|
||||
StoreLock(const StoreLock&) = delete;
|
||||
StoreLock& operator=(const StoreLock&) = delete;
|
||||
|
||||
private:
|
||||
#ifdef _WIN32
|
||||
HANDLE handle_ = INVALID_HANDLE_VALUE;
|
||||
#else
|
||||
int handle_ = -1;
|
||||
#endif
|
||||
};
|
||||
|
||||
struct PrivateText {
|
||||
std::string value;
|
||||
~PrivateText() { OPENSSL_cleanse(value.data(), value.size()); }
|
||||
};
|
||||
|
||||
void Validate(const nlohmann::json& document) {
|
||||
Require(document.is_object() && document.at("version") == 1);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void Update(const std::filesystem::path& cache,
|
||||
const std::function<void(nlohmann::json&)>& update) {
|
||||
private_file::EnsureDirectory(cache, true);
|
||||
StoreLock lock(cache / "identity.json.lock");
|
||||
const auto path = cache / "identity.json";
|
||||
const bool exists = std::filesystem::symlink_status(path).type() !=
|
||||
std::filesystem::file_type::not_found;
|
||||
nlohmann::json saved = {{"version", 1}};
|
||||
if (exists) {
|
||||
PrivateText text{private_file::Read(path)};
|
||||
saved = nlohmann::json::parse(text.value, nullptr, false);
|
||||
Require(!saved.is_discarded());
|
||||
}
|
||||
Validate(saved);
|
||||
PrivateText before{saved.dump()};
|
||||
update(saved);
|
||||
Validate(saved);
|
||||
PrivateText after{saved.dump()};
|
||||
Require(after.value.size() <= 64 * 1024);
|
||||
if (!exists || before.value != after.value) private_file::Write(path, after.value);
|
||||
}
|
||||
|
||||
} // namespace crossdesk::device_identity_store
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-11-01
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _DEVICE_IDENTITY_STORE_H_
|
||||
#define _DEVICE_IDENTITY_STORE_H_
|
||||
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace crossdesk::device_identity_store {
|
||||
|
||||
// The shared device signing key lives in cache/identity.json. Read the latest
|
||||
// document under a process lock, apply an update, then atomically persist it.
|
||||
// Throw on corruption, unsafe permissions, lock contention or persistence error.
|
||||
void Update(const std::filesystem::path& cache,
|
||||
const std::function<void(nlohmann::json&)>& update);
|
||||
|
||||
} // namespace crossdesk::device_identity_store
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,70 @@
|
||||
#include "runtime/device_login.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
#include "runtime/trusted_devices.h"
|
||||
|
||||
namespace crossdesk {
|
||||
int DeviceLogin::Exchange(SignalExchangeStage stage, const nlohmann::json& message,
|
||||
const TrustedDevices* identity, char* response, size_t capacity) {
|
||||
auto output = [&](const nlohmann::json& reply) {
|
||||
const auto text = reply.dump();
|
||||
if (text.empty() || text.size() > capacity) return -1;
|
||||
std::memcpy(response, text.data(), text.size());
|
||||
return static_cast<int>(text.size());
|
||||
};
|
||||
if (stage == SignalExchangeStage::PrepareLogin) {
|
||||
phase_ = Phase::Idle;
|
||||
used_password_ = false;
|
||||
password_.clear();
|
||||
request_ = message;
|
||||
const auto login = message.at("user_id").get<std::string>();
|
||||
if (!identity) return -1; // Never fall back when local key storage fails.
|
||||
const auto at = login.find('@');
|
||||
request_["user_id"] = login.substr(0, at);
|
||||
if (at != std::string::npos) password_ = login.substr(at + 1);
|
||||
request_["device_key"] = {{"version", 1}, {"public_key", identity->PublicKey()}};
|
||||
request_.erase("reconnect_token");
|
||||
request_.erase("reconnect_version");
|
||||
phase_ = Phase::AwaitingChallenge;
|
||||
return output(request_);
|
||||
}
|
||||
if (!Pending()) return 0;
|
||||
const auto type = message.value("type", "");
|
||||
if (type == "login_challenge") {
|
||||
if (!identity || phase_ != Phase::AwaitingChallenge || message.at("device_key_version") != 1 ||
|
||||
message.at("user_id") != request_.at("user_id")) return -1;
|
||||
const auto nonce = message.at("nonce").get<std::string>();
|
||||
if (nonce.size() != 64 || !std::all_of(nonce.begin(), nonce.end(),
|
||||
[](char c) { return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'); }))
|
||||
return -1;
|
||||
const auto id = request_.at("user_id").get<std::string>();
|
||||
auto reply = request_;
|
||||
used_password_ = message.at("password_required").get<bool>();
|
||||
if (used_password_) {
|
||||
if (id.empty() || id.rfind("C-", 0) == 0 || password_.empty()) return -1;
|
||||
reply["user_id"] = id + "@" + password_;
|
||||
}
|
||||
const auto transcript = nlohmann::json::array(
|
||||
{"crossdesk.device-login.v1", id, identity->PublicKey(), nonce}).dump();
|
||||
reply["device_key"]["nonce"] = nonce;
|
||||
reply["device_key"]["signature"] = identity->Sign(transcript);
|
||||
phase_ = Phase::AwaitingResult;
|
||||
password_.clear();
|
||||
return output(reply);
|
||||
}
|
||||
if (type == "login") {
|
||||
if (message.at("status") == "success") {
|
||||
if (phase_ != Phase::AwaitingResult || message.value("device_key_version", 0) != 1) return -1;
|
||||
const auto assigned = message.at("user_id").get<std::string>();
|
||||
const auto expected = request_.at("user_id").get<std::string>();
|
||||
if (assigned.empty() || (!expected.empty() && assigned != expected)) return -1;
|
||||
}
|
||||
password_.clear();
|
||||
request_.clear();
|
||||
phase_ = Phase::Idle;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-11-02
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _DEVICE_LOGIN_H_
|
||||
#define _DEVICE_LOGIN_H_
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "minirtc.h"
|
||||
|
||||
namespace crossdesk {
|
||||
class TrustedDevices;
|
||||
|
||||
// One signaling connection's application authentication, owned by its callbacks.
|
||||
class DeviceLogin {
|
||||
public:
|
||||
int Exchange(SignalExchangeStage stage, const nlohmann::json& message,
|
||||
const TrustedDevices* identity, char* response, size_t capacity);
|
||||
bool Pending() const { return phase_ != Phase::Idle; }
|
||||
bool UsedPassword() const { return used_password_; }
|
||||
|
||||
private:
|
||||
enum class Phase { Idle, AwaitingChallenge, AwaitingResult };
|
||||
nlohmann::json request_;
|
||||
std::string password_;
|
||||
Phase phase_ = Phase::Idle;
|
||||
bool used_password_ = false;
|
||||
};
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,192 @@
|
||||
#include "runtime/gui_runtime.h"
|
||||
|
||||
#include "platform.h"
|
||||
#include "rd_log.h"
|
||||
|
||||
namespace crossdesk {
|
||||
void GuiRuntime::InitializeTrustedDevices(const std::filesystem::path& directory) {
|
||||
try {
|
||||
trusted_devices_ = std::make_unique<TrustedDevices>(directory, cache_path_);
|
||||
} catch (const std::exception& error) {
|
||||
LOG_ERROR("Cannot load trusted devices: {}", error.what());
|
||||
// The independent persisted switch stays enabled on identity/store failure.
|
||||
}
|
||||
}
|
||||
|
||||
bool GuiRuntime::TrustedControllerAllowed(const std::string& id) {
|
||||
if (!config_center_ || !config_center_->IsTrustedDevicesOnly()) return true;
|
||||
std::lock_guard lock(controller_trust_mutex_);
|
||||
const auto it = controller_trust_.find(id);
|
||||
return it != controller_trust_.end() && it->second.IsTrusted(trusted_devices_.get());
|
||||
}
|
||||
|
||||
bool GuiRuntime::BeginControllerTrust(const std::string& id, PeerEventHandler* events) {
|
||||
const bool required = config_center_->IsTrustedDevicesOnly();
|
||||
std::lock_guard lock(controller_trust_mutex_);
|
||||
const auto previous = controller_trust_.find(id);
|
||||
// Non-connected statuses clear this state. Duplicate Connected notifications
|
||||
// in the same application session must not restart the challenge or timeout.
|
||||
if (previous != controller_trust_.end() && previous->second.events == events) {
|
||||
auto& state = previous->second;
|
||||
const bool allowed = !required || state.IsTrusted(trusted_devices_.get());
|
||||
state.pending = !allowed;
|
||||
return allowed;
|
||||
}
|
||||
ControllerTrust state;
|
||||
state.events = events;
|
||||
state.pending = required;
|
||||
state.deadline = std::chrono::steady_clock::now() + std::chrono::seconds(10);
|
||||
try {
|
||||
if (!trusted_devices_) state.invalid = true;
|
||||
else state.nonce = TrustedDevices::Nonce();
|
||||
} catch (...) { state.invalid = true; }
|
||||
controller_trust_[id] = std::move(state);
|
||||
return !required;
|
||||
}
|
||||
|
||||
void GuiRuntime::ForgetControllerTrust(const std::string& id, PeerEventHandler* events) {
|
||||
std::lock_guard lock(controller_trust_mutex_);
|
||||
const auto it = controller_trust_.find(id);
|
||||
if (it != controller_trust_.end() && it->second.events == events)
|
||||
controller_trust_.erase(it);
|
||||
}
|
||||
|
||||
int GuiRuntime::SendTrustMessage(const std::string& id, const nlohmann::json& message) {
|
||||
const auto text = message.dump();
|
||||
return WithController(id, [&](PeerPtr* peer, const std::string& wire) {
|
||||
return SendReliableDataFrameToPeer(peer, text.data(), text.size(),
|
||||
control_data_label_.c_str(), wire.data(), wire.size());
|
||||
}, false);
|
||||
}
|
||||
|
||||
bool GuiRuntime::HandleDeviceTrustMessage(const std::string& id, PeerEventHandler* events,
|
||||
const char* data, size_t size) {
|
||||
if (!data || size > 4096) return false;
|
||||
const auto message = nlohmann::json::parse(data, data + size, nullptr, false);
|
||||
if (!message.is_object()) return false;
|
||||
const auto type_it = message.find("type");
|
||||
if (type_it == message.end()) return false;
|
||||
if (*type_it == static_cast<int>(ControlType::host_infomation)) {
|
||||
// Advertise through an existing message so disabled restrictions send no
|
||||
// unfamiliar protocol messages to legacy clients. Retain only bounded,
|
||||
// inert metadata until the connection's proof has been accepted.
|
||||
std::lock_guard lock(controller_trust_mutex_);
|
||||
const auto it = controller_trust_.find(id);
|
||||
if (it != controller_trust_.end() && it->second.events == events) {
|
||||
const auto capability = message.find("device_trust_version");
|
||||
if (capability != message.end() && *capability == 1) it->second.capable = true;
|
||||
if (it->second.pending) {
|
||||
it->second.early_host_info.assign(data, size);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (!type_it->is_string()) return false;
|
||||
const auto type = type_it->get<std::string>();
|
||||
if (type.rfind("device_trust_", 0) != 0) return false;
|
||||
if (!trusted_devices_) return true;
|
||||
try {
|
||||
if (type == "device_trust_challenge") {
|
||||
const auto props = FindRemoteSession(id);
|
||||
if (!props || props->closing_ || props->peer_events_.get() != events ||
|
||||
message.at("version") != 1) return true;
|
||||
const auto wire = events->WireId(id);
|
||||
const auto proof = trusted_devices_->Prove(message.at("nonce"),
|
||||
props->local_id_, GetHostName()).dump();
|
||||
SendReliableDataFrameToPeer(props->peer_, proof.data(), proof.size(),
|
||||
control_data_label_.c_str(), wire.data(), wire.size());
|
||||
} else if (type == "device_trust_proof") {
|
||||
std::lock_guard lock(controller_trust_mutex_);
|
||||
const auto it = controller_trust_.find(id);
|
||||
if (it == controller_trust_.end() || it->second.events != events) return true;
|
||||
auto& state = it->second;
|
||||
if (state.invalid || state.verified || std::chrono::steady_clock::now() >= state.deadline)
|
||||
return true;
|
||||
// Accept once per fresh challenge; malformed/replayed proofs grant nothing.
|
||||
state.verified = TrustedDevices::Verify(message, state.nonce);
|
||||
if (!state.verified) state.invalid = true;
|
||||
}
|
||||
} catch (const std::exception&) {
|
||||
// Untrusted protocol input never escapes the transport callback.
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void GuiRuntime::TickControllerTrust() {
|
||||
struct Action {
|
||||
std::string id;
|
||||
PeerEventHandler* events;
|
||||
std::string nonce;
|
||||
int kind;
|
||||
std::string host_info;
|
||||
};
|
||||
std::vector<Action> actions;
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
const bool required = config_center_->IsTrustedDevicesOnly();
|
||||
{
|
||||
std::lock_guard lock(controller_trust_mutex_);
|
||||
for (auto& [id, state] : controller_trust_) {
|
||||
if (state.closing) continue;
|
||||
const bool trusted = state.IsTrusted(trusted_devices_.get());
|
||||
if (required && (state.invalid || (state.verified && !trusted) ||
|
||||
(!state.verified && now >= state.deadline))) {
|
||||
state.invalid = true;
|
||||
state.closing = true;
|
||||
actions.push_back({id, state.events, {}, -1});
|
||||
} else if (state.pending && (!required || trusted)) {
|
||||
state.pending = false;
|
||||
actions.push_back({id, state.events, {}, 1, std::exchange(state.early_host_info, {})});
|
||||
}
|
||||
if ((required || state.capable) && !state.invalid && !state.verified &&
|
||||
now < state.deadline && now >= state.next_challenge) {
|
||||
state.next_challenge = now + std::chrono::seconds(1);
|
||||
actions.push_back({id, state.events, state.nonce, 0});
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const auto& action : actions) {
|
||||
const auto events = ControllerEvents(action.id);
|
||||
if (!events || events.get() != action.events) continue;
|
||||
auto callback = events->EnterCallback();
|
||||
if (!callback) continue;
|
||||
if (action.kind < 0) {
|
||||
LOG_WARN("Reject controller [{}]: trusted device verification required", action.id);
|
||||
transfers_.Cancel(events.get(), action.id);
|
||||
remote_input_.Disconnect(action.id);
|
||||
CloseServerController(action.id);
|
||||
} else if (action.kind > 0) {
|
||||
events->ApplyConnectionStatus(ConnectionStatus::Connected, action.id);
|
||||
if (!action.host_info.empty()) {
|
||||
const auto wire = events->WireId(action.id);
|
||||
PeerEventHandler::OnReceiveDataBuffer(action.host_info.data(), action.host_info.size(),
|
||||
wire.data(), wire.size(), control_data_label_.data(), control_data_label_.size(), events.get());
|
||||
}
|
||||
} else {
|
||||
SendTrustMessage(action.id, {{"type", "device_trust_challenge"},
|
||||
{"version", 1}, {"nonce", action.nonce}});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<TrustedDevices::Device> GuiRuntime::VerifiedController(const std::string& id) {
|
||||
std::lock_guard lock(controller_trust_mutex_);
|
||||
const auto it = controller_trust_.find(id);
|
||||
return it == controller_trust_.end() || it->second.invalid
|
||||
? std::nullopt : it->second.verified;
|
||||
}
|
||||
bool GuiRuntime::TrustController(const std::string& id, const std::string& expected_key) {
|
||||
if (!trusted_devices_ || !IsAuthorizedController(id)) return false;
|
||||
const auto device = VerifiedController(id);
|
||||
if (!device || device->public_key != expected_key) return false;
|
||||
try { trusted_devices_->Add(*device); return true; }
|
||||
catch (const std::exception& error) { LOG_ERROR("Cannot trust device: {}", error.what()); return false; }
|
||||
}
|
||||
bool GuiRuntime::RemoveTrustedDevice(const std::string& public_key) {
|
||||
if (!trusted_devices_) return false;
|
||||
try { trusted_devices_->Remove(public_key); }
|
||||
catch (const std::exception& error) { LOG_ERROR("Cannot remove trusted device: {}", error.what()); return false; }
|
||||
TickControllerTrust();
|
||||
return true;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
@@ -190,9 +190,7 @@ int GuiRuntime::CreateConnectionPeer() {
|
||||
if (pending_identity.empty()) {
|
||||
credential_recovery_in_progress_ = false;
|
||||
credential_recovery_attempt_pending_ = false;
|
||||
credential_recovery_retry_active_ = false;
|
||||
credential_recovery_promote_pending_ = false;
|
||||
credential_recovery_clear_pending_ = false;
|
||||
credential_recovery_action_ = CredentialRecoveryAction::Idle;
|
||||
} else if (!credential_recovery_in_progress_) {
|
||||
credential_recovery_in_progress_ = true;
|
||||
credential_recovery_attempt_pending_ = true;
|
||||
@@ -270,6 +268,7 @@ int GuiRuntime::CreateConnectionPeer() {
|
||||
|
||||
params_.on_signal_status = PeerEventHandler::OnSignalStatus;
|
||||
params_.on_signal_message = PeerEventHandler::OnSignalMessage;
|
||||
params_.on_signal_exchange = PeerEventHandler::OnSignalExchange;
|
||||
params_.on_connection_status = PeerEventHandler::OnConnectionStatus;
|
||||
params_.on_net_status_report = PeerEventHandler::OnNetStatusReport;
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "runtime/announcement_inbox.h"
|
||||
#include "runtime/peer_event_handler.h"
|
||||
#include "runtime/privacy_session_policy.h"
|
||||
#include "runtime/trusted_devices.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
@@ -63,7 +64,8 @@ class GuiRuntime : protected gui_detail::GuiState {
|
||||
std::string password, bool remember_password = false);
|
||||
int WithController(
|
||||
const std::string& id,
|
||||
const std::function<int(PeerPtr*, const std::string&)>& operation);
|
||||
const std::function<int(PeerPtr*, const std::string&)>& operation,
|
||||
bool require_trusted = true);
|
||||
int SendControllerData(const std::string& id, const char* data, size_t size,
|
||||
const char* label);
|
||||
std::shared_ptr<PeerEventHandler> ControllerEvents(const std::string& id);
|
||||
@@ -105,6 +107,32 @@ class GuiRuntime : protected gui_detail::GuiState {
|
||||
void HandlePrivacy();
|
||||
void HandleScreenLock();
|
||||
bool IsAuthorizedController(const std::string& remote_id);
|
||||
void InitializeTrustedDevices(const std::filesystem::path& directory);
|
||||
bool TrustedControllerAllowed(const std::string& remote_id);
|
||||
bool BeginControllerTrust(const std::string& remote_id, PeerEventHandler* events);
|
||||
void ForgetControllerTrust(const std::string& remote_id, PeerEventHandler* events);
|
||||
bool HandleDeviceTrustMessage(const std::string& remote_id, PeerEventHandler* events,
|
||||
const char* data, size_t size);
|
||||
void TickControllerTrust();
|
||||
std::optional<TrustedDevices::Device> VerifiedController(const std::string& remote_id);
|
||||
bool TrustController(const std::string& remote_id, const std::string& expected_key);
|
||||
bool RemoveTrustedDevice(const std::string& public_key);
|
||||
int SendTrustMessage(const std::string& remote_id, const nlohmann::json& message);
|
||||
std::unique_ptr<TrustedDevices> trusted_devices_;
|
||||
struct ControllerTrust {
|
||||
PeerEventHandler* events = nullptr;
|
||||
std::string nonce;
|
||||
std::optional<TrustedDevices::Device> verified;
|
||||
std::string early_host_info;
|
||||
std::chrono::steady_clock::time_point deadline{}, next_challenge{};
|
||||
bool pending = false, invalid = false, closing = false, capable = false;
|
||||
bool IsTrusted(const TrustedDevices* devices) const {
|
||||
return devices && !invalid && !closing && verified &&
|
||||
devices->Contains(verified->public_key);
|
||||
}
|
||||
};
|
||||
std::mutex controller_trust_mutex_;
|
||||
std::map<std::string, ControllerTrust> controller_trust_;
|
||||
void SetControllerPrivacySupport(const std::string& remote_id, bool supported);
|
||||
// Caller holds connection_status_mutex_ exclusively. Turns privacy off when
|
||||
// any connected controller lacks support, or on for the first automatic one.
|
||||
|
||||
@@ -180,7 +180,9 @@ std::shared_ptr<PeerEventHandler> GuiRuntime::ControllerEvents(
|
||||
}
|
||||
int GuiRuntime::WithController(
|
||||
const std::string& id,
|
||||
const std::function<int(PeerPtr*, const std::string&)>& operation) {
|
||||
const std::function<int(PeerPtr*, const std::string&)>& operation,
|
||||
bool require_trusted) {
|
||||
if (require_trusted && !TrustedControllerAllowed(id)) return -1;
|
||||
std::shared_lock lock(remote_sessions_mutex_);
|
||||
auto it = lan_controllers_.find(id);
|
||||
auto events = it == lan_controllers_.end() ? peer_events_ : it->second.events;
|
||||
|
||||
@@ -133,6 +133,12 @@ void PeerEventHandler::OnReceiveDataBuffer(
|
||||
GuiRuntime* runtime = &handler->owner_;
|
||||
|
||||
std::string source_id = std::string(src_id, src_id_size);
|
||||
const auto remote_id = handler->ConnectionKey(user_id, user_id_size);
|
||||
if (source_id == runtime->control_data_label_ &&
|
||||
runtime->HandleDeviceTrustMessage(remote_id, handler, data, size)) return;
|
||||
const auto viewer = runtime->FindRemoteSession(remote_id);
|
||||
if (viewer && viewer->peer_events_.get() != handler) return;
|
||||
if (!viewer && !runtime->TrustedControllerAllowed(remote_id)) return;
|
||||
// The service-owned host runs as SYSTEM. Do not expose filesystem or
|
||||
// clipboard operations in that security context.
|
||||
if (runtime->unattended_host_ &&
|
||||
@@ -143,34 +149,30 @@ void PeerEventHandler::OnReceiveDataBuffer(
|
||||
return;
|
||||
if (source_id == web_clipboard::kStream) {
|
||||
runtime->clipboard_.ReceiveWeb(
|
||||
data, size, handler->ConnectionKey(user_id, user_id_size), handler);
|
||||
data, size, remote_id, handler);
|
||||
return;
|
||||
}
|
||||
if (source_id == runtime->file_label_) {
|
||||
runtime->transfers_.Receive(data, size, handler,
|
||||
handler->ConnectionKey(user_id, user_id_size));
|
||||
runtime->transfers_.Receive(data, size, handler, remote_id);
|
||||
return;
|
||||
} else if (source_id == runtime->clipboard_label_) {
|
||||
if (size > 0) {
|
||||
std::string remote_user_id =
|
||||
handler->ConnectionKey(user_id, user_id_size);
|
||||
auto props =
|
||||
runtime->FindRemoteSession(remote_user_id);
|
||||
if (props && !props->clipboard_allowed_.load()) {
|
||||
if (viewer && !viewer->clipboard_allowed_.load()) {
|
||||
return;
|
||||
}
|
||||
|
||||
runtime->clipboard_.QueueRemoteText(
|
||||
data, size, props ? remote_user_id : std::string{});
|
||||
data, size, viewer ? remote_id : std::string{}, {},
|
||||
viewer ? std::function<bool()>{} : [runtime, remote_id] {
|
||||
return runtime->IsAuthorizedController(remote_id);
|
||||
});
|
||||
}
|
||||
return;
|
||||
} else if (source_id == runtime->file_feedback_label_) {
|
||||
runtime->transfers_.HandleAck(
|
||||
data, size, handler, handler->ConnectionKey(user_id, user_id_size));
|
||||
runtime->transfers_.HandleAck(data, size, handler, remote_id);
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string remote_id = handler->ConnectionKey(user_id, user_id_size);
|
||||
const std::string wire_id = handler->WireId(remote_id);
|
||||
PeerPtr* bound_peer =
|
||||
handler->IsDirect() ? handler->DirectPeer() : runtime->peer_;
|
||||
|
||||
@@ -84,6 +84,24 @@ PeerEventHandler::PeerEventHandler(GuiRuntime& owner) : owner_(owner) {
|
||||
owner_.transfers_.Connected(this, "");
|
||||
}
|
||||
|
||||
int PeerEventHandler::OnSignalExchange(SignalExchangeStage stage, const char* message,
|
||||
size_t size, char* response, size_t capacity, void* user_data) {
|
||||
auto* handler = static_cast<PeerEventHandler*>(user_data);
|
||||
if (!handler || !message) return -1;
|
||||
auto callback = handler->EnterCallback();
|
||||
if (!callback) return -1;
|
||||
if (stage == SignalExchangeStage::IncomingMessage &&
|
||||
!handler->device_login_.Pending()) return 0;
|
||||
try {
|
||||
return handler->device_login_.Exchange(stage,
|
||||
nlohmann::json::parse(message, message + size),
|
||||
handler->owner_.trusted_devices_.get(), response, capacity);
|
||||
} catch (const std::exception&) {
|
||||
LOG_ERROR("Device login protocol or identity unavailable");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
void PeerEventHandler::OnSignalMessage(const char* message, size_t size,
|
||||
void* user_data) {
|
||||
auto* handler = static_cast<PeerEventHandler*>(user_data);
|
||||
@@ -184,13 +202,16 @@ void PeerEventHandler::OnSignalMessage(const char* message, size_t size,
|
||||
j.contains("reason") && j["reason"].is_string()
|
||||
? j["reason"].get<std::string>()
|
||||
: "Password change failed";
|
||||
runtime->password_change_result_uncertain_ = false;
|
||||
// A rejected replay cannot tell us whether the original request committed.
|
||||
runtime->password_change_result_uncertain_ = runtime->password_change_recovery_ &&
|
||||
!runtime->password_change_succeeded_;
|
||||
runtime->password_change_result_ready_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
void PeerEventHandler::OnSignalStatus(SignalStatus status, const char* user_id,
|
||||
size_t user_id_size, void* user_data) {
|
||||
using RecoveryAction = GuiRuntime::CredentialRecoveryAction;
|
||||
auto* handler = static_cast<PeerEventHandler*>(user_data);
|
||||
if (!handler) return;
|
||||
auto callback = handler->EnterCallback();
|
||||
@@ -212,10 +233,12 @@ void PeerEventHandler::OnSignalStatus(SignalStatus status, const char* user_id,
|
||||
SendClientInfo(runtime->peer_, client_id);
|
||||
std::lock_guard<std::mutex> lock(runtime->password_change_mutex_);
|
||||
if (runtime->credential_recovery_in_progress_) {
|
||||
if (runtime->credential_recovery_attempt_pending_) {
|
||||
runtime->credential_recovery_promote_pending_ = true;
|
||||
if (!handler->device_login_.UsedPassword()) {
|
||||
runtime->credential_recovery_action_ = RecoveryAction::ResumePending;
|
||||
} else if (runtime->credential_recovery_attempt_pending_) {
|
||||
runtime->credential_recovery_action_ = RecoveryAction::PromotePending;
|
||||
} else {
|
||||
runtime->credential_recovery_clear_pending_ = true;
|
||||
runtime->credential_recovery_action_ = RecoveryAction::ClearPending;
|
||||
}
|
||||
}
|
||||
} else if (SignalStatus::SignalFailed == status) {
|
||||
@@ -223,7 +246,7 @@ void PeerEventHandler::OnSignalStatus(SignalStatus status, const char* user_id,
|
||||
std::lock_guard<std::mutex> lock(runtime->password_change_mutex_);
|
||||
if (runtime->credential_recovery_in_progress_ &&
|
||||
runtime->credential_recovery_attempt_pending_) {
|
||||
runtime->credential_recovery_retry_active_ = true;
|
||||
runtime->credential_recovery_action_ = RecoveryAction::RetryActive;
|
||||
}
|
||||
} else if (SignalStatus::SignalClosed == status) {
|
||||
runtime->signal_connected_ = false;
|
||||
@@ -300,6 +323,14 @@ void PeerEventHandler::DispatchConnectionStatus(ConnectionStatus status,
|
||||
}
|
||||
}
|
||||
|
||||
if (!props) {
|
||||
if (status == ConnectionStatus::Connected) {
|
||||
if (!runtime->BeginControllerTrust(remote_id, handler)) return;
|
||||
} else {
|
||||
runtime->ForgetControllerTrust(remote_id, handler);
|
||||
}
|
||||
}
|
||||
|
||||
if (props) {
|
||||
runtime->is_client_mode_ = true;
|
||||
if (!props->wall_managed_) runtime->show_connection_status_window_ = true;
|
||||
@@ -332,7 +363,9 @@ void PeerEventHandler::DispatchConnectionStatus(ConnectionStatus status,
|
||||
LOG_INFO("Local display [{}:{}]", i + 1,
|
||||
remote_action.i.display_list[i]);
|
||||
}
|
||||
std::string msg = remote_action.to_json();
|
||||
auto information = nlohmann::json::parse(remote_action.to_json());
|
||||
information["device_trust_version"] = 1;
|
||||
std::string msg = information.dump();
|
||||
const int ret =
|
||||
SendReliableDataFrame(props->peer_, msg.data(), msg.size(),
|
||||
runtime->control_data_label_.c_str());
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
|
||||
#include "minirtc.h"
|
||||
#include "runtime/session_lifecycle.h"
|
||||
#include "runtime/device_login.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
@@ -63,6 +64,9 @@ public:
|
||||
size_t user_id_size, void *user_data);
|
||||
static void OnSignalMessage(const char *message, size_t size,
|
||||
void *user_data);
|
||||
static int OnSignalExchange(SignalExchangeStage stage, const char* message,
|
||||
size_t size, char* response, size_t capacity,
|
||||
void* user_data);
|
||||
static void OnConnectionStatus(ConnectionStatus status, const char *user_id,
|
||||
size_t user_id_size, void *user_data);
|
||||
static void OnNetStatusReport(const char *client_id, size_t client_id_size,
|
||||
@@ -85,6 +89,7 @@ private:
|
||||
size_t early_data_bytes_ = 0;
|
||||
|
||||
GuiRuntime& owner_;
|
||||
DeviceLogin device_login_;
|
||||
gui_detail::SessionCallbackGate callbacks_;
|
||||
};
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
namespace crossdesk {
|
||||
|
||||
bool GuiRuntime::IsAuthorizedController(const std::string& remote_id) {
|
||||
if (!TrustedControllerAllowed(remote_id)) return false;
|
||||
if (remote_id.rfind("lan/", 0) == 0) {
|
||||
const auto events = ControllerEvents(remote_id);
|
||||
if (!events || !events->DataAllowed()) return false;
|
||||
@@ -59,6 +60,7 @@ void GuiRuntime::ApplyPrivacyAdmission(bool automatic_enable) {
|
||||
|
||||
void GuiRuntime::QueuePrivacyCommand(const std::string& remote_id,
|
||||
const PrivacyCommand& command) {
|
||||
if (!TrustedControllerAllowed(remote_id)) return;
|
||||
std::shared_lock session_lock(connection_status_mutex_);
|
||||
const auto found = connection_status_.find(remote_id);
|
||||
if (found == connection_status_.end() ||
|
||||
@@ -86,6 +88,7 @@ void GuiRuntime::HandlePrivacy() {
|
||||
commands.swap(privacy_commands_);
|
||||
}
|
||||
for (const auto& [remote_id, command] : commands) {
|
||||
if (!TrustedControllerAllowed(remote_id)) continue;
|
||||
// Keep admission and activation under the connection lock so an unknown
|
||||
// controller cannot join between the capability check and enabling.
|
||||
std::shared_lock lock(connection_status_mutex_);
|
||||
|
||||
@@ -167,21 +167,23 @@ struct UserSettingsState {
|
||||
};
|
||||
|
||||
struct PasswordChangeState {
|
||||
enum class CredentialRecoveryAction {
|
||||
Idle, RetryActive, PromotePending, ClearPending, ResumePending
|
||||
};
|
||||
std::mutex password_change_mutex_;
|
||||
uint64_t next_password_change_request_id_ = 0;
|
||||
bool password_change_pending_ = false;
|
||||
bool password_change_result_ready_ = false;
|
||||
bool password_change_succeeded_ = false;
|
||||
bool password_change_result_uncertain_ = false;
|
||||
bool password_change_recovery_ = false;
|
||||
std::chrono::steady_clock::time_point password_change_requested_at_;
|
||||
std::string pending_password_change_request_id_;
|
||||
std::string pending_local_password_;
|
||||
std::string password_change_error_;
|
||||
bool credential_recovery_in_progress_ = false;
|
||||
bool credential_recovery_attempt_pending_ = false;
|
||||
bool credential_recovery_retry_active_ = false;
|
||||
bool credential_recovery_promote_pending_ = false;
|
||||
bool credential_recovery_clear_pending_ = false;
|
||||
CredentialRecoveryAction credential_recovery_action_ = CredentialRecoveryAction::Idle;
|
||||
};
|
||||
|
||||
struct ConnectionState {
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
#include "runtime/trusted_devices.h"
|
||||
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/sha.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "private_file.h"
|
||||
#include "runtime/device_identity_store.h"
|
||||
|
||||
namespace crossdesk {
|
||||
namespace {
|
||||
constexpr size_t kMaxTrustedDevices = 128;
|
||||
void Require(bool ok) {
|
||||
if (!ok) throw std::runtime_error("Invalid or unavailable trusted device identity");
|
||||
}
|
||||
std::string Hex(const uint8_t* data, size_t size) {
|
||||
constexpr char digits[] = "0123456789abcdef";
|
||||
std::string result;
|
||||
result.reserve(size * 2);
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
result += digits[data[i] >> 4]; result += digits[data[i] & 15];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
bool IsHex(const std::string& value, size_t bytes) {
|
||||
return value.size() == bytes * 2 && std::all_of(value.begin(), value.end(),
|
||||
[](char c) { return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'); });
|
||||
}
|
||||
std::vector<uint8_t> Unhex(const std::string& text, size_t bytes) {
|
||||
Require(IsHex(text, bytes));
|
||||
auto digit = [](char c) { return c <= '9' ? c - '0' : c - 'a' + 10; };
|
||||
std::vector<uint8_t> result(bytes);
|
||||
for (size_t i = 0; i < bytes; ++i)
|
||||
result[i] = (digit(text[2 * i]) << 4) | digit(text[2 * i + 1]);
|
||||
return result;
|
||||
}
|
||||
bool Label(const std::string& value) {
|
||||
return value.size() <= 128 && std::none_of(value.begin(), value.end(),
|
||||
[](unsigned char c) { return c < 32 || c == 127; });
|
||||
}
|
||||
std::string Transcript(const std::string& nonce, const TrustedDevices::Device& device) {
|
||||
// Canonical encoding and a domain tag prevent ambiguous or cross-protocol use.
|
||||
return nlohmann::json::array({"crossdesk.device-trust.v1", nonce, device.public_key,
|
||||
device.device_id, device.name}).dump();
|
||||
}
|
||||
using Context = std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)>;
|
||||
struct PrivateText {
|
||||
std::string value;
|
||||
~PrivateText() { OPENSSL_cleanse(value.data(), value.size()); }
|
||||
};
|
||||
}
|
||||
|
||||
TrustedDevices::TrustedDevices(const std::filesystem::path& directory,
|
||||
const std::filesystem::path& cache)
|
||||
: path_(directory / "devices.json") {
|
||||
private_file::EnsureDirectory(directory);
|
||||
const bool exists = std::filesystem::symlink_status(path_).type() !=
|
||||
std::filesystem::file_type::not_found;
|
||||
PrivateText legacy_key;
|
||||
if (exists) {
|
||||
PrivateText text{private_file::Read(path_)};
|
||||
auto saved = nlohmann::json::parse(text.value, nullptr, false);
|
||||
Require(!saved.is_discarded());
|
||||
const bool legacy = saved.at("version") == 1;
|
||||
Require((legacy || saved.at("version") == 2) && saved.at("devices").is_array() &&
|
||||
saved.at("devices").size() <= kMaxTrustedDevices);
|
||||
if (legacy) {
|
||||
legacy_key.value = saved.at("private_key").get<std::string>();
|
||||
auto& encoded = saved["private_key"].get_ref<std::string&>();
|
||||
OPENSSL_cleanse(encoded.data(), encoded.size());
|
||||
Require(IsHex(legacy_key.value, 32));
|
||||
} else Require(!saved.contains("private_key"));
|
||||
for (const auto& row : saved.at("devices")) {
|
||||
Device device{row.at("public_key"), row.at("name"), row.at("device_id")};
|
||||
Require(IsHex(device.public_key, 32) && Label(device.name) && Label(device.device_id));
|
||||
Require(devices_.emplace(device.public_key, device).second);
|
||||
}
|
||||
}
|
||||
|
||||
device_identity_store::Update(cache, [&](nlohmann::json& identity) {
|
||||
if (!identity.contains("trust_private_key") && !legacy_key.value.empty())
|
||||
identity["trust_private_key"] = legacy_key.value;
|
||||
if (identity.contains("trust_private_key")) {
|
||||
const auto& encoded = identity.at("trust_private_key").get_ref<const std::string&>();
|
||||
// Interrupted migration may leave both copies. Never replace either key
|
||||
// on a mismatch: that would silently invalidate existing pairings.
|
||||
Require(legacy_key.value.empty() || encoded == legacy_key.value);
|
||||
auto secret = Unhex(encoded, 32);
|
||||
key_.reset(EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, nullptr,
|
||||
secret.data(), secret.size()));
|
||||
OPENSSL_cleanse(secret.data(), secret.size());
|
||||
} else {
|
||||
key_.reset(EVP_PKEY_Q_keygen(nullptr, nullptr, "ED25519"));
|
||||
Require(bool(key_));
|
||||
std::array<uint8_t, 32> secret{};
|
||||
size_t size = secret.size();
|
||||
Require(EVP_PKEY_get_raw_private_key(key_.get(), secret.data(), &size) == 1 && size == 32);
|
||||
identity["trust_private_key"] = Hex(secret.data(), secret.size());
|
||||
OPENSSL_cleanse(secret.data(), secret.size());
|
||||
}
|
||||
Require(bool(key_));
|
||||
});
|
||||
|
||||
std::array<uint8_t, 32> public_key{};
|
||||
size_t size = public_key.size();
|
||||
Require(EVP_PKEY_get_raw_public_key(key_.get(), public_key.data(), &size) == 1 && size == 32);
|
||||
public_key_ = Hex(public_key.data(), public_key.size());
|
||||
// The identity update is durable before removing the legacy secret. If this
|
||||
// write fails, reopening can safely retry with the same key and device list.
|
||||
if (!exists || !legacy_key.value.empty()) Save(devices_);
|
||||
}
|
||||
|
||||
std::string TrustedDevices::Fingerprint(const std::string& public_key) {
|
||||
const auto bytes = Unhex(public_key, 32);
|
||||
std::array<uint8_t, SHA256_DIGEST_LENGTH> hash{};
|
||||
SHA256(bytes.data(), bytes.size(), hash.data());
|
||||
return Hex(hash.data(), hash.size());
|
||||
}
|
||||
std::string TrustedDevices::Fingerprint() const { return Fingerprint(public_key_); }
|
||||
|
||||
std::string TrustedDevices::PairingInfo(const std::string& device_id,
|
||||
const std::string& name) const {
|
||||
Require(Label(device_id) && Label(name));
|
||||
// Export public information only. Connection admission still requires a fresh
|
||||
// proof of private-key possession; importing this document grants no bypass.
|
||||
return nlohmann::json{{"type", "crossdesk-pairing"}, {"version", 1},
|
||||
{"public_key", public_key_}, {"device_id", device_id}, {"name", name}}.dump();
|
||||
}
|
||||
|
||||
std::optional<TrustedDevices::Device> TrustedDevices::ParsePairingInfo(std::string_view text) {
|
||||
if (text.empty() || text.size() > kMaxPairingInfoSize) return std::nullopt;
|
||||
try {
|
||||
const auto info = nlohmann::json::parse(text.begin(), text.end(), nullptr, false);
|
||||
if (!info.is_object() || info.size() != 5 || info.at("type") != "crossdesk-pairing" ||
|
||||
!info.at("version").is_number_integer() || info.at("version") != 1)
|
||||
return std::nullopt;
|
||||
Device device{info.at("public_key"), info.at("name"), info.at("device_id")};
|
||||
if (IsHex(device.public_key, 32) && Label(device.name) && Label(device.device_id))
|
||||
return device;
|
||||
} catch (const std::exception&) {}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::string TrustedDevices::Nonce() {
|
||||
std::array<uint8_t, 32> bytes{};
|
||||
Require(RAND_bytes(bytes.data(), bytes.size()) == 1);
|
||||
return Hex(bytes.data(), bytes.size());
|
||||
}
|
||||
|
||||
nlohmann::json TrustedDevices::Prove(const std::string& nonce,
|
||||
const std::string& device_id, const std::string& name) const {
|
||||
Require(IsHex(nonce, 32) && Label(device_id) && Label(name));
|
||||
const Device device{public_key_, name, device_id};
|
||||
const auto text = Transcript(nonce, device);
|
||||
return {{"type", "device_trust_proof"}, {"version", 1}, {"nonce", nonce},
|
||||
{"public_key", public_key_}, {"device_id", device_id}, {"name", name},
|
||||
{"signature", Sign(text)}};
|
||||
}
|
||||
|
||||
std::string TrustedDevices::Sign(std::string_view text) const {
|
||||
Context context(EVP_MD_CTX_new(), EVP_MD_CTX_free);
|
||||
std::array<uint8_t, 64> signature{};
|
||||
size_t size = signature.size();
|
||||
Require(context && EVP_DigestSignInit(context.get(), nullptr, nullptr, nullptr, key_.get()) == 1 &&
|
||||
EVP_DigestSign(context.get(), signature.data(), &size,
|
||||
reinterpret_cast<const uint8_t*>(text.data()), text.size()) == 1 && size == 64);
|
||||
return Hex(signature.data(), signature.size());
|
||||
}
|
||||
|
||||
std::optional<TrustedDevices::Device> TrustedDevices::Verify(
|
||||
const nlohmann::json& proof, const std::string& nonce) {
|
||||
try {
|
||||
if (proof.at("version") != 1 || proof.at("nonce") != nonce || !IsHex(nonce, 32))
|
||||
return std::nullopt;
|
||||
Device device{proof.at("public_key"), proof.at("name"), proof.at("device_id")};
|
||||
if (!Label(device.name) || !Label(device.device_id)) return std::nullopt;
|
||||
const auto public_key = Unhex(device.public_key, 32);
|
||||
const auto signature = Unhex(proof.at("signature"), 64);
|
||||
std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)> key(
|
||||
EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, nullptr, public_key.data(), public_key.size()),
|
||||
EVP_PKEY_free);
|
||||
Context context(EVP_MD_CTX_new(), EVP_MD_CTX_free);
|
||||
const auto text = Transcript(nonce, device);
|
||||
if (key && context && EVP_DigestVerifyInit(context.get(), nullptr, nullptr, nullptr, key.get()) == 1 &&
|
||||
EVP_DigestVerify(context.get(), signature.data(), signature.size(),
|
||||
reinterpret_cast<const uint8_t*>(text.data()), text.size()) == 1)
|
||||
return device;
|
||||
} catch (const std::exception&) {}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
bool TrustedDevices::Contains(const std::string& public_key) const {
|
||||
std::lock_guard lock(mutex_);
|
||||
return devices_.count(public_key) != 0;
|
||||
}
|
||||
std::vector<TrustedDevices::Device> TrustedDevices::List() const {
|
||||
std::lock_guard lock(mutex_);
|
||||
std::vector<Device> result;
|
||||
for (const auto& [key, device] : devices_) result.push_back(device);
|
||||
return result;
|
||||
}
|
||||
void TrustedDevices::Save(const std::map<std::string, Device>& devices) const {
|
||||
nlohmann::json saved{{"version", 2}, {"devices", nlohmann::json::array()}};
|
||||
for (const auto& [key, device] : devices)
|
||||
saved["devices"].push_back({{"public_key", key}, {"name", device.name}, {"device_id", device.device_id}});
|
||||
const auto text = saved.dump(2);
|
||||
Require(text.size() <= 64 * 1024);
|
||||
private_file::Write(path_, text);
|
||||
}
|
||||
void TrustedDevices::Add(const Device& device) {
|
||||
Require(IsHex(device.public_key, 32) && Label(device.name) && Label(device.device_id));
|
||||
std::lock_guard lock(mutex_);
|
||||
const auto existing = devices_.find(device.public_key);
|
||||
if (existing != devices_.end() && existing->second == device) return;
|
||||
Require(existing != devices_.end() || devices_.size() < kMaxTrustedDevices);
|
||||
auto next = devices_;
|
||||
next[device.public_key] = device;
|
||||
Save(next);
|
||||
devices_.swap(next);
|
||||
}
|
||||
void TrustedDevices::Remove(const std::string& public_key) {
|
||||
std::lock_guard lock(mutex_);
|
||||
if (!devices_.contains(public_key)) return;
|
||||
auto next = devices_;
|
||||
next.erase(public_key);
|
||||
Save(next);
|
||||
devices_.swap(next);
|
||||
}
|
||||
} // namespace crossdesk
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-11-01
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _TRUSTED_DEVICES_H_
|
||||
#define _TRUSTED_DEVICES_H_
|
||||
|
||||
#include <openssl/evp.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class TrustedDevices {
|
||||
public:
|
||||
struct Device {
|
||||
std::string public_key, name, device_id;
|
||||
bool operator==(const Device&) const = default;
|
||||
};
|
||||
|
||||
TrustedDevices(const std::filesystem::path& directory,
|
||||
const std::filesystem::path& cache);
|
||||
std::string Fingerprint() const;
|
||||
const std::string& PublicKey() const { return public_key_; }
|
||||
// Callers supply a canonical, purpose-specific transcript.
|
||||
std::string Sign(std::string_view transcript) const;
|
||||
static std::string Fingerprint(const std::string& public_key);
|
||||
static std::string Nonce();
|
||||
nlohmann::json Prove(const std::string& nonce,
|
||||
const std::string& device_id, const std::string& name) const;
|
||||
static std::optional<Device> Verify(const nlohmann::json& proof,
|
||||
const std::string& nonce);
|
||||
static constexpr size_t kMaxPairingInfoSize = 2048;
|
||||
std::string PairingInfo(const std::string& device_id, const std::string& name) const;
|
||||
static std::optional<Device> ParsePairingInfo(std::string_view text);
|
||||
bool Contains(const std::string& public_key) const;
|
||||
std::vector<Device> List() const;
|
||||
// Call only after a local add action for a verified peer or valid pairing info.
|
||||
void Add(const Device& device);
|
||||
void Remove(const std::string& public_key);
|
||||
|
||||
private:
|
||||
void Save(const std::map<std::string, Device>& devices) const;
|
||||
std::filesystem::path path_;
|
||||
std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)> key_{nullptr, EVP_PKEY_free};
|
||||
std::string public_key_;
|
||||
mutable std::mutex mutex_;
|
||||
std::map<std::string, Device> devices_;
|
||||
};
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -12,3 +12,5 @@ export { GalleryStrings } from "device_strings.slint";
|
||||
export { LanStrings } from "lan_strings.slint";
|
||||
export { GalleryNetworkDevice } from "gallery_network_dialog.slint";
|
||||
export { DeviceGroupOption } from "device_group_dialog.slint";
|
||||
|
||||
export { TrustedDeviceEntry, TrustStrings } from "trusted_devices.slint";
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { SurfaceSeparator, FlatSurface, UiSurface } from "ui_surface.slint";
|
||||
import { TrustedDevicesDialog, TrustedDeviceEntry, TrustStrings } from "trusted_devices.slint";
|
||||
import { LanStrings } from "lan_strings.slint";
|
||||
import { AboutPanel, AboutPage, AboutComponent, AboutStrings, AboutNavigationRow, AboutNavigationGroup, AboutRowSeparator } from "about_panel.slint";
|
||||
import { UiRadius } from "ui_radius.slint";
|
||||
@@ -93,7 +94,7 @@ export global UiStrings {
|
||||
in-out property <string> license: "Licensed under GNU GPL v3.";
|
||||
in-out property <string> signal-connected: "Signal server connected";
|
||||
in-out property <string> signal-disconnected: "Signal server disconnected";
|
||||
in-out property <string> tls-error: "Signal server TLS certificate error";
|
||||
in-out property <string> tls-error: "Server certificate invalid or expired. Retrying automatically.";
|
||||
in-out property <string> signal-reconnect: "Reconnect";
|
||||
in-out property <string> signal-reconnecting: "Reconnecting...";
|
||||
in-out property <string> update-available: "A new version is available";
|
||||
@@ -660,6 +661,17 @@ export component MainWindow inherits Window {
|
||||
in-out property <bool> privacy-local-unlock-allowed: true;
|
||||
in property <bool> lock-on-disconnect-available: false;
|
||||
in-out property <bool> lock-on-disconnect-enabled: true;
|
||||
in-out property <bool> trusted-devices-only: false;
|
||||
in-out property <bool> trusted-devices-open: false;
|
||||
in property <[TrustedDeviceEntry]> trusted-devices;
|
||||
in property <[TrustedDeviceEntry]> trust-candidates;
|
||||
in property <string> device-fingerprint;
|
||||
in property <string> trusted-devices-error;
|
||||
in property <string> trusted-devices-notice;
|
||||
in property <string> trust-copy-error;
|
||||
in property <string> trust-copy-notice;
|
||||
in-out property <string> trust-pairing-input;
|
||||
callback trust-action(string, string, string);
|
||||
in-out property <string> file-save-path: "";
|
||||
in-out property <string> server-host: "";
|
||||
in-out property <string> server-port: "";
|
||||
@@ -1087,7 +1099,7 @@ export component MainWindow inherits Window {
|
||||
if event.text == Key.Escape { root.messages-open = false; }
|
||||
return accept;
|
||||
}
|
||||
if root.settings-open && !root.self-host-settings-open {
|
||||
if root.settings-open && !root.self-host-settings-open && !root.trusted-devices-open {
|
||||
if event.text == Key.Escape { root.cancel-settings(); root.settings-open = false; }
|
||||
return accept;
|
||||
}
|
||||
@@ -1923,7 +1935,7 @@ export component MainWindow inherits Window {
|
||||
text: root.current-version;
|
||||
font-size: 11px; color: WorkbenchStyle.muted; vertical-alignment: center;
|
||||
}
|
||||
if !root.lan-mode && !root.signal-connected && !root.signal-tls-error: WorkbenchButton {
|
||||
if !root.lan-mode && !root.signal-connected: WorkbenchButton {
|
||||
x: parent.width - self.width - 20px;
|
||||
y: (parent.height - self.height) / 2;
|
||||
width: max(64px, reconnect-label-metrics.preferred-width + 22px);
|
||||
@@ -2753,15 +2765,21 @@ export component MainWindow inherits Window {
|
||||
}
|
||||
}
|
||||
}
|
||||
Text {
|
||||
text: GalleryStrings.texts[119] + "\n" + GalleryStrings.texts[207];
|
||||
font-size: 11px; color: WorkbenchStyle.muted; wrap: word-wrap;
|
||||
}
|
||||
SettingsGroup {
|
||||
title: GalleryStrings.texts[120]; rows: 3;
|
||||
title: GalleryStrings.texts[120]; rows: 5;
|
||||
SettingsToggleRow { label: UiStrings.privacy-on-connect; checked <=> root.privacy-on-connect-enabled; enabled: root.privacy-setting-available; }
|
||||
SettingsToggleRow { label: UiStrings.privacy-local-unlock; checked <=> root.privacy-local-unlock-allowed; enabled: root.privacy-setting-available; }
|
||||
SettingsToggleRow { label: UiStrings.lock-on-disconnect; checked <=> root.lock-on-disconnect-enabled; enabled: root.lock-on-disconnect-available; divider: false; }
|
||||
SettingsToggleRow { label: UiStrings.lock-on-disconnect; checked <=> root.lock-on-disconnect-enabled; enabled: root.lock-on-disconnect-available; }
|
||||
SettingsToggleRow { label: TrustStrings.only; checked <=> root.trusted-devices-only; }
|
||||
SettingsRow {
|
||||
label: TrustStrings.title; divider: false;
|
||||
SettingsButton {
|
||||
x: parent.width - self.width - 12px; y: 4px;
|
||||
width: max(104px, self.label-width); height: 28px;
|
||||
text: TrustStrings.manage;
|
||||
clicked => { root.trusted-devices-open = true; root.trust-action("open", "", ""); }
|
||||
}
|
||||
}
|
||||
}
|
||||
Text {
|
||||
text: GalleryStrings.texts[121];
|
||||
@@ -2871,6 +2889,20 @@ export component MainWindow inherits Window {
|
||||
}
|
||||
}
|
||||
|
||||
if root.trusted-devices-open: TrustedDevicesDialog {
|
||||
z: 51;
|
||||
devices: root.trusted-devices;
|
||||
candidates: root.trust-candidates;
|
||||
local-fingerprint: root.device-fingerprint;
|
||||
error: root.trusted-devices-error;
|
||||
notice: root.trusted-devices-notice;
|
||||
copy-error: root.trust-copy-error;
|
||||
copy-notice: root.trust-copy-notice;
|
||||
pairing-input <=> root.trust-pairing-input;
|
||||
close => { root.trust-action("close", "", ""); root.trusted-devices-open = false; }
|
||||
action(action, id, key) => { root.trust-action(action, id, key); }
|
||||
}
|
||||
|
||||
if root.self-host-settings-open: Rectangle {
|
||||
background: #15223835;
|
||||
z: 49;
|
||||
|
||||
@@ -6,11 +6,13 @@ import { WorkbenchStyle } from "workbench_controls.slint";
|
||||
// Shared visual treatment for the global settings pages.
|
||||
export component SettingsButton inherits FlatSurface {
|
||||
in property <string> text;
|
||||
in property <length> text-size: 12px;
|
||||
in property <image> icon;
|
||||
in property <bool> primary: false;
|
||||
in property <bool> quiet: false;
|
||||
in property <bool> align-left: false;
|
||||
in property <bool> enabled: true;
|
||||
in property <bool> destructive: false;
|
||||
out property <length> label-width: button-label.preferred-width + 2 * root.content-padding + (root.has-icon ? 24px : 0px);
|
||||
private property <bool> has-icon: root.icon.width > 0;
|
||||
private property <length> content-padding: root.has-icon ? 8px : 12px;
|
||||
@@ -18,7 +20,8 @@ export component SettingsButton inherits FlatSurface {
|
||||
width: 88px; height: 32px; border-radius: UiRadius.control;
|
||||
background: !root.enabled ? ControlStyle.disabled
|
||||
: root.primary ? (touch.pressed ? #1e4fa3 : touch.has-hover ? #2866c9 : #3275dc)
|
||||
: touch.pressed ? ButtonStyle.pressed : touch.has-hover ? ButtonStyle.hover
|
||||
: touch.pressed ? (root.destructive ? #f6babb : ButtonStyle.pressed)
|
||||
: touch.has-hover ? (root.destructive ? #fbe0e0 : ButtonStyle.hover)
|
||||
: root.quiet ? transparent : ButtonStyle.surface;
|
||||
outline-enabled: !root.quiet && !root.primary;
|
||||
outline-color: !root.enabled ? ButtonStyle.disabled-border
|
||||
@@ -44,8 +47,9 @@ export component SettingsButton inherits FlatSurface {
|
||||
button-label := Text {
|
||||
x: root.has-icon ? 24px : 0px;
|
||||
width: parent.width - self.x; height: parent.height;
|
||||
text: root.text; font-size: 12px; font-weight: root.primary ? 600 : 400;
|
||||
color: !root.enabled ? #98a2b3 : root.primary ? white : root.quiet ? #65758b : #3c4d63;
|
||||
text: root.text; font-size: root.text-size; font-weight: root.primary ? 600 : 400;
|
||||
color: !root.enabled ? #98a2b3 : root.primary ? white
|
||||
: root.destructive ? #c43c43 : root.quiet ? #65758b : #3c4d63;
|
||||
horizontal-alignment: root.align-left ? left : center; vertical-alignment: center; overflow: elide;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,264 @@
|
||||
import { SettingsDialog, SettingsButton } from "settings_controls.slint";
|
||||
import { WorkbenchStyle } from "workbench_controls.slint";
|
||||
import { ScrollView } from "std-widgets.slint";
|
||||
import { FlatSurface, UiSurface } from "ui_surface.slint";
|
||||
import { UiRadius } from "ui_radius.slint";
|
||||
import { RoundedLineEdit } from "rounded_line_edit.slint";
|
||||
|
||||
export struct TrustedDeviceEntry {
|
||||
remote-id: string,
|
||||
public-key: string,
|
||||
display-name: string,
|
||||
device-id: string,
|
||||
fingerprint: string,
|
||||
}
|
||||
|
||||
export global TrustStrings {
|
||||
in property <string> title: "Trusted devices";
|
||||
in property <string> only: "Only allow trusted devices";
|
||||
in property <string> manage: "Manage";
|
||||
in property <string> copy: "Copy pairing info";
|
||||
in property <string> import-title: "Add a trusted device";
|
||||
in property <string> add: "Add";
|
||||
in property <string> placeholder: "Paste the controller's pairing information";
|
||||
in property <string> confirm: "Check the fingerprint on the controller.";
|
||||
in property <string> empty: "No trusted devices";
|
||||
in property <string> candidates: "Connected devices available to trust";
|
||||
in property <string> trust: "Trust";
|
||||
in property <string> remove: "Remove";
|
||||
in property <string> saved: "Trusted devices";
|
||||
in property <string> sharing: "This device’s pairing info";
|
||||
in property <string> show-fingerprint: "View fingerprint";
|
||||
in property <string> hide-fingerprint: "Hide fingerprint";
|
||||
in property <string> close: "Close";
|
||||
}
|
||||
|
||||
component FingerprintBlock inherits Rectangle {
|
||||
in property <string> fingerprint;
|
||||
background: #f0f4fa; border-radius: UiRadius.control;
|
||||
height: content.preferred-height;
|
||||
content := VerticalLayout {
|
||||
width: parent.width; height: self.preferred-height;
|
||||
padding: 10px;
|
||||
Text { text: root.fingerprint; color: #465a76; font-size: 11px; wrap: word-wrap; }
|
||||
}
|
||||
}
|
||||
|
||||
component DeviceRow inherits FlatSurface {
|
||||
in property <TrustedDeviceEntry> device;
|
||||
in property <string> action-text;
|
||||
in property <bool> confirming: false;
|
||||
private property <bool> fingerprint-open: false;
|
||||
callback action;
|
||||
height: content.preferred-height;
|
||||
background: white; outline-color: UiSurface.edge;
|
||||
border-radius: UiRadius.control;
|
||||
content := VerticalLayout {
|
||||
width: parent.width; height: self.preferred-height;
|
||||
padding: 12px; spacing: 8px;
|
||||
padding-top: root.confirming ? 12px : 8px;
|
||||
padding-bottom: root.confirming ? 12px : 8px;
|
||||
HorizontalLayout {
|
||||
spacing: 6px;
|
||||
VerticalLayout {
|
||||
spacing: 2px; horizontal-stretch: 1; min-width: 0px;
|
||||
Text {
|
||||
text: root.device.display-name;
|
||||
font-size: root.confirming ? 13px : 12px; font-weight: root.confirming ? 600 : 500;
|
||||
color: WorkbenchStyle.text; overflow: elide;
|
||||
}
|
||||
Text {
|
||||
text: "ID: " + (root.device.device-id == "" ? "—" : root.device.device-id);
|
||||
font-size: 11px; color: WorkbenchStyle.muted; overflow: elide;
|
||||
}
|
||||
}
|
||||
if !root.confirming: SettingsButton {
|
||||
width: min(144px, self.label-width); height: 28px; quiet: true;
|
||||
text-size: 11px;
|
||||
text: root.fingerprint-open ? TrustStrings.hide-fingerprint : TrustStrings.show-fingerprint;
|
||||
clicked => { root.fingerprint-open = !root.fingerprint-open; }
|
||||
}
|
||||
if !root.confirming: SettingsButton {
|
||||
width: min(128px, max(64px, self.label-width)); height: 28px;
|
||||
text-size: 11px;
|
||||
quiet: true; text: root.action-text;
|
||||
destructive: true;
|
||||
clicked => { root.action(); }
|
||||
}
|
||||
}
|
||||
if root.confirming || root.fingerprint-open: FingerprintBlock {
|
||||
fingerprint: root.device.fingerprint;
|
||||
}
|
||||
if root.confirming: HorizontalLayout {
|
||||
spacing: 12px;
|
||||
Text {
|
||||
text: TrustStrings.confirm; font-size: 11px;
|
||||
color: WorkbenchStyle.muted; wrap: word-wrap;
|
||||
horizontal-stretch: 1; min-width: 0px; vertical-alignment: center;
|
||||
}
|
||||
SettingsButton {
|
||||
width: min(parent.width * 0.6, max(112px, self.label-width)); height: 30px;
|
||||
primary: true; text: root.action-text;
|
||||
clicked => { root.action(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export component TrustedDevicesDialog inherits Rectangle {
|
||||
in property <[TrustedDeviceEntry]> devices;
|
||||
in property <[TrustedDeviceEntry]> candidates;
|
||||
in property <string> local-fingerprint;
|
||||
in property <string> error;
|
||||
in property <string> notice;
|
||||
in property <string> copy-error;
|
||||
in property <string> copy-notice;
|
||||
in-out property <string> pairing-input;
|
||||
private property <bool> local-fingerprint-open: false;
|
||||
callback close;
|
||||
callback action(string, string, string);
|
||||
background: #15223835;
|
||||
TouchArea { }
|
||||
forward-focus: focus;
|
||||
focus := FocusScope {
|
||||
key-pressed(event) => {
|
||||
if event.text == Key.Escape { root.close(); return accept; }
|
||||
return reject;
|
||||
}
|
||||
}
|
||||
dialog := SettingsDialog {
|
||||
// Reserve the full 14px Fluent scrollbar plus an 8px content gap.
|
||||
private property <length> scrollbar-gutter: 22px;
|
||||
x: (parent.width - self.width) / 2; y: (parent.height - self.height) / 2;
|
||||
width: min(540px, parent.width - 32px);
|
||||
height: min(400px, parent.height - 32px);
|
||||
min-width: 0px; max-width: parent.width - 32px;
|
||||
min-height: 0px; max-height: parent.height - 32px;
|
||||
title: TrustStrings.title; close-label: TrustStrings.close;
|
||||
close => { root.close(); }
|
||||
ScrollView {
|
||||
x: 24px; y: 64px;
|
||||
// Extend into the right margin to preserve the existing card width.
|
||||
width: parent.width - 56px + dialog.scrollbar-gutter;
|
||||
height: parent.height - 84px;
|
||||
min-width: 0px; min-height: 0px;
|
||||
horizontal-scrollbar-policy: always-off;
|
||||
vertical-scrollbar-policy: as-needed;
|
||||
viewport-width: self.visible-width;
|
||||
viewport-height: content.preferred-height;
|
||||
content := VerticalLayout {
|
||||
x: 0px; y: 0px;
|
||||
width: max(0px, parent.visible-width - dialog.scrollbar-gutter); height: self.preferred-height;
|
||||
spacing: 16px; padding: 0px; alignment: start;
|
||||
FlatSurface {
|
||||
background: #f1f6ff; outline-color: #d7e3f7; border-radius: UiRadius.surface;
|
||||
height: add-content.preferred-height;
|
||||
add-content := VerticalLayout {
|
||||
width: parent.width; height: self.preferred-height; padding: 14px; spacing: 10px;
|
||||
Text {
|
||||
text: TrustStrings.import-title; font-size: 14px; font-weight: 600;
|
||||
color: WorkbenchStyle.text; min-width: 0px; wrap: word-wrap;
|
||||
}
|
||||
HorizontalLayout {
|
||||
spacing: 8px;
|
||||
RoundedLineEdit {
|
||||
horizontal-stretch: 1; height: 32px; text <=> root.pairing-input;
|
||||
placeholder-text: TrustStrings.placeholder; surrounding-background: #f1f6ff;
|
||||
edited => { root.action("edit", "", ""); }
|
||||
accepted => { root.action("import", "", ""); }
|
||||
}
|
||||
SettingsButton {
|
||||
width: max(64px, self.label-width); height: 32px; primary: true;
|
||||
text: TrustStrings.add; enabled: root.pairing-input != "" && root.local-fingerprint != "";
|
||||
clicked => { root.action("import", "", ""); }
|
||||
}
|
||||
}
|
||||
if root.error != "": Text {
|
||||
text: root.error; color: #b33c39; font-size: 11px; wrap: word-wrap;
|
||||
}
|
||||
if root.notice != "": Text {
|
||||
text: root.notice; color: #28734a; font-size: 11px; wrap: word-wrap;
|
||||
}
|
||||
}
|
||||
}
|
||||
VerticalLayout {
|
||||
spacing: 6px;
|
||||
Rectangle {
|
||||
// Align mixed font sizes by their baselines rather than their top edges.
|
||||
private property <length> baseline: max(saved-title.font-metrics.ascent, saved-count.font-metrics.ascent);
|
||||
height: max(saved-title.y + saved-title.preferred-height, saved-count.y + saved-count.preferred-height);
|
||||
saved-title := Text {
|
||||
x: 0px; width: self.preferred-width; height: self.preferred-height;
|
||||
y: parent.baseline - self.font-metrics.ascent;
|
||||
text: TrustStrings.saved; font-size: 13px; font-weight: 600; color: WorkbenchStyle.text;
|
||||
}
|
||||
saved-count := Text {
|
||||
x: saved-title.preferred-width + 6px;
|
||||
width: self.preferred-width; height: self.preferred-height;
|
||||
y: parent.baseline - self.font-metrics.ascent;
|
||||
text: "" + root.devices.length; font-size: 12px; color: WorkbenchStyle.muted;
|
||||
}
|
||||
}
|
||||
if root.devices.length == 0: FlatSurface {
|
||||
height: 48px; background: white;
|
||||
outline-color: UiSurface.edge; border-radius: UiRadius.control;
|
||||
Text {
|
||||
x: 14px; width: parent.width - 28px; height: parent.height;
|
||||
text: TrustStrings.empty; font-size: 12px; color: WorkbenchStyle.muted;
|
||||
horizontal-alignment: center; vertical-alignment: center; wrap: word-wrap;
|
||||
}
|
||||
}
|
||||
for device in root.devices: DeviceRow {
|
||||
device: device; action-text: TrustStrings.remove;
|
||||
action => { root.action("remove", device.remote-id, device.public-key); }
|
||||
}
|
||||
}
|
||||
if root.candidates.length > 0: VerticalLayout {
|
||||
spacing: 8px;
|
||||
Text { text: TrustStrings.candidates; font-size: 13px; font-weight: 600; color: WorkbenchStyle.text; wrap: word-wrap; }
|
||||
for device in root.candidates: DeviceRow {
|
||||
device: device; confirming: true; action-text: TrustStrings.trust;
|
||||
action => { root.action("add", device.remote-id, device.public-key); }
|
||||
}
|
||||
}
|
||||
FlatSurface {
|
||||
height: sharing-content.preferred-height; background: white;
|
||||
outline-color: UiSurface.edge; border-radius: UiRadius.control;
|
||||
sharing-content := VerticalLayout {
|
||||
width: parent.width; height: self.preferred-height; padding: 14px; spacing: 8px;
|
||||
HorizontalLayout {
|
||||
spacing: 12px;
|
||||
Text {
|
||||
horizontal-stretch: 1; min-width: 0px;
|
||||
text: TrustStrings.sharing; font-size: 13px; font-weight: 600;
|
||||
color: WorkbenchStyle.text; wrap: word-wrap; vertical-alignment: center;
|
||||
}
|
||||
SettingsButton {
|
||||
width: min(160px, self.label-width); height: 30px; text: TrustStrings.copy;
|
||||
enabled: root.local-fingerprint != ""; clicked => { root.action("copy", "", ""); }
|
||||
}
|
||||
}
|
||||
HorizontalLayout {
|
||||
spacing: 8px;
|
||||
SettingsButton {
|
||||
width: self.label-width; height: 22px; quiet: true; align-left: true;
|
||||
text: root.local-fingerprint-open ? TrustStrings.hide-fingerprint : TrustStrings.show-fingerprint;
|
||||
enabled: root.local-fingerprint != "";
|
||||
clicked => { root.local-fingerprint-open = !root.local-fingerprint-open; }
|
||||
}
|
||||
Text {
|
||||
text: root.copy-error != "" ? root.copy-error : root.copy-notice;
|
||||
color: root.copy-error != "" ? #b33c39 : #28734a; font-size: 11px;
|
||||
horizontal-stretch: 1; min-width: 0px; wrap: word-wrap;
|
||||
horizontal-alignment: right; vertical-alignment: center;
|
||||
}
|
||||
}
|
||||
if root.local-fingerprint-open: FingerprintBlock {
|
||||
fingerprint: root.local-fingerprint;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,6 +55,24 @@ inline int ApplyMainWindowStrings(
|
||||
languages.push_back(Text(option.display_name));
|
||||
languages.push_back(Text(localization::Literal("跟随系统", language)));
|
||||
window->set_language_options(std::make_shared<slint::VectorModel<slint::SharedString>>(languages));
|
||||
auto& trust = window->global<ui::TrustStrings>();
|
||||
trust.set_title(Text(localization::Literal("可信设备", language)));
|
||||
trust.set_only(Text(localization::Literal("仅允许可信设备控制", language)));
|
||||
trust.set_manage(Text(localization::Literal("管理可信设备", language)));
|
||||
trust.set_copy(Text(localization::Literal("复制配对信息", language)));
|
||||
trust.set_import_title(Text(localization::Literal("添加可信设备", language)));
|
||||
trust.set_add(Text(localization::Literal("添加", language)));
|
||||
trust.set_placeholder(Text(localization::Literal("粘贴控制端的配对信息", language)));
|
||||
trust.set_confirm(Text(localization::Literal("请核对控制端指纹。", language)));
|
||||
trust.set_saved(Text(localization::Literal("已信任的设备", language)));
|
||||
trust.set_sharing(Text(localization::Literal("本机配对信息", language)));
|
||||
trust.set_show_fingerprint(Text(localization::Literal("查看指纹", language)));
|
||||
trust.set_hide_fingerprint(Text(localization::Literal("收起指纹", language)));
|
||||
trust.set_empty(Text(localization::Literal("暂无可信设备", language)));
|
||||
trust.set_candidates(Text(localization::Literal("可添加的已连接设备", language)));
|
||||
trust.set_trust(Text(localization::Literal("信任此设备", language)));
|
||||
trust.set_remove(Text(localization::Literal("移除信任", language)));
|
||||
trust.set_close(Text(localization::message_close[language]));
|
||||
auto& strings = window->global<ui::UiStrings>();
|
||||
strings.set_window_minimize(Text(localization::window_minimize[language]));
|
||||
strings.set_window_close(Text(localization::window_close[language]));
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "lan_identity.h"
|
||||
#include "private_file.h"
|
||||
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/pem.h>
|
||||
@@ -8,7 +9,6 @@
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <charconv>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string_view>
|
||||
@@ -16,23 +16,18 @@
|
||||
#include "direct_address.h"
|
||||
#include "lan_password.h"
|
||||
#ifdef _WIN32
|
||||
// Winsock must precede windows.h; the networking/ACL headers need its types.
|
||||
// Winsock must precede windows.h; the networking headers need its types.
|
||||
// clang-format off
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <Aclapi.h>
|
||||
#include <iphlpapi.h>
|
||||
#include <netioapi.h>
|
||||
#include <sddl.h>
|
||||
// clang-format on
|
||||
#else
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <net/if.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#ifdef __APPLE__
|
||||
#include <net/if_dl.h>
|
||||
#include <net/if_types.h>
|
||||
@@ -59,166 +54,8 @@ bool IsHex(const std::string& text, size_t size) {
|
||||
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
|
||||
});
|
||||
}
|
||||
#ifdef _WIN32
|
||||
std::vector<unsigned char> CurrentUser() {
|
||||
HANDLE token = nullptr;
|
||||
Require(OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token),
|
||||
"Cannot open user token");
|
||||
DWORD size = 0;
|
||||
GetTokenInformation(token, TokenUser, nullptr, 0, &size);
|
||||
std::vector<unsigned char> data(size);
|
||||
bool ok = GetTokenInformation(token, TokenUser, data.data(), size, &size);
|
||||
CloseHandle(token);
|
||||
Require(ok, "Cannot read user token");
|
||||
return data;
|
||||
}
|
||||
PSECURITY_DESCRIPTOR PrivateSecurityDescriptor(bool directory) {
|
||||
auto user = CurrentUser();
|
||||
LPWSTR sid = nullptr;
|
||||
Require(ConvertSidToStringSidW(
|
||||
reinterpret_cast<TOKEN_USER*>(user.data())->User.Sid, &sid),
|
||||
"Cannot read user SID");
|
||||
// An elevated token can default to Administrators as the owner even when
|
||||
// its DACL grants access only to TokenUser. Always set both explicitly.
|
||||
const auto acl = L"O:" + std::wstring(sid) + L"D:P(A;" +
|
||||
(directory ? L"OICI" : L"") + L";FA;;;" + sid + L")";
|
||||
LocalFree(sid);
|
||||
PSECURITY_DESCRIPTOR descriptor = nullptr;
|
||||
Require(ConvertStringSecurityDescriptorToSecurityDescriptorW(
|
||||
acl.c_str(), SDDL_REVISION_1, &descriptor, nullptr),
|
||||
"Cannot protect LAN file");
|
||||
return descriptor;
|
||||
}
|
||||
#endif
|
||||
void PrivateWrite(const std::filesystem::path& path, const std::string& text) {
|
||||
const auto temporary = path.parent_path() / (".write-" + RandomHex(16));
|
||||
#ifdef _WIN32
|
||||
auto descriptor = PrivateSecurityDescriptor(false);
|
||||
SECURITY_ATTRIBUTES security{sizeof(security), descriptor, FALSE};
|
||||
HANDLE file = CreateFileW(temporary.c_str(), GENERIC_WRITE, 0, &security,
|
||||
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
LocalFree(descriptor);
|
||||
Require(file != INVALID_HANDLE_VALUE, "Cannot create LAN file");
|
||||
DWORD written = 0;
|
||||
bool ok = WriteFile(file, text.data(), static_cast<DWORD>(text.size()),
|
||||
&written, nullptr) &&
|
||||
written == text.size() && FlushFileBuffers(file);
|
||||
CloseHandle(file);
|
||||
if (!ok || !MoveFileExW(temporary.c_str(), path.c_str(),
|
||||
MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) {
|
||||
DeleteFileW(temporary.c_str());
|
||||
throw std::runtime_error("Cannot persist LAN file");
|
||||
}
|
||||
#else
|
||||
int fd =
|
||||
open(temporary.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
|
||||
Require(fd >= 0, "Cannot create private LAN file");
|
||||
size_t offset = 0;
|
||||
while (offset < text.size()) {
|
||||
const auto count = write(fd, text.data() + offset, text.size() - offset);
|
||||
if (count <= 0) break;
|
||||
offset += static_cast<size_t>(count);
|
||||
}
|
||||
bool ok = offset == text.size() && fsync(fd) == 0;
|
||||
close(fd);
|
||||
if (!ok || rename(temporary.c_str(), path.c_str()) != 0) {
|
||||
unlink(temporary.c_str());
|
||||
throw std::runtime_error("Cannot persist LAN file");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#ifdef _WIN32
|
||||
using FileHandle = std::unique_ptr<void, decltype(&CloseHandle)>;
|
||||
FileHandle OpenLanPath(const std::filesystem::path& path, DWORD access) {
|
||||
// Keep the object open without FILE_SHARE_DELETE so validation and legacy
|
||||
// owner repair cannot operate on a replacement at the same path.
|
||||
HANDLE file = CreateFileW(
|
||||
path.c_str(), access, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
|
||||
OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,
|
||||
nullptr);
|
||||
Require(file != INVALID_HANDLE_VALUE, "Cannot open LAN path");
|
||||
return {file, CloseHandle};
|
||||
}
|
||||
void CheckPrivateWindowsPath(const std::filesystem::path& path) {
|
||||
auto file = OpenLanPath(path, READ_CONTROL);
|
||||
BY_HANDLE_FILE_INFORMATION info{};
|
||||
Require(GetFileInformationByHandle(file.get(), &info) &&
|
||||
!(info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT),
|
||||
"LAN path cannot be a reparse point");
|
||||
auto user = CurrentUser();
|
||||
auto sid = reinterpret_cast<TOKEN_USER*>(user.data())->User.Sid;
|
||||
PSID owner = nullptr;
|
||||
PACL acl = nullptr;
|
||||
PSECURITY_DESCRIPTOR descriptor = nullptr;
|
||||
Require(GetSecurityInfo(
|
||||
file.get(), SE_FILE_OBJECT,
|
||||
OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, &owner,
|
||||
nullptr, &acl, nullptr, &descriptor) == ERROR_SUCCESS,
|
||||
"Cannot read LAN file permissions");
|
||||
std::unique_ptr<void, decltype(&LocalFree)> security(descriptor, LocalFree);
|
||||
bool ok = acl && acl->AceCount > 0;
|
||||
// Only the exact protected, full-access DACL written by older versions is
|
||||
// eligible for owner repair. Never adopt a shared or foreign-user profile.
|
||||
SECURITY_DESCRIPTOR_CONTROL control{};
|
||||
DWORD revision = 0;
|
||||
bool legacy = owner && IsWellKnownSid(owner, WinBuiltinAdministratorsSid) &&
|
||||
GetSecurityDescriptorControl(descriptor, &control, &revision) &&
|
||||
(control & SE_DACL_PROTECTED) && acl && acl->AceCount == 1;
|
||||
for (DWORD i = 0; ok && i < acl->AceCount; ++i) {
|
||||
void* raw = nullptr;
|
||||
if (!GetAce(acl, i, &raw)) {
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
const auto* ace = static_cast<ACCESS_ALLOWED_ACE*>(raw);
|
||||
ok = ace->Header.AceType == ACCESS_ALLOWED_ACE_TYPE &&
|
||||
EqualSid(const_cast<DWORD*>(&ace->SidStart), sid);
|
||||
const BYTE flags = (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
||||
? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE
|
||||
: 0;
|
||||
legacy =
|
||||
legacy && ace->Mask == FILE_ALL_ACCESS && ace->Header.AceFlags == flags;
|
||||
}
|
||||
Require(ok && owner && (EqualSid(owner, sid) || legacy),
|
||||
"LAN files must be accessible only by the current user");
|
||||
if (legacy) {
|
||||
auto writable = OpenLanPath(path, WRITE_OWNER);
|
||||
BY_HANDLE_FILE_INFORMATION current{};
|
||||
Require(GetFileInformationByHandle(writable.get(), ¤t) &&
|
||||
current.dwVolumeSerialNumber == info.dwVolumeSerialNumber &&
|
||||
current.nFileIndexHigh == info.nFileIndexHigh &&
|
||||
current.nFileIndexLow == info.nFileIndexLow &&
|
||||
SetSecurityInfo(writable.get(), SE_FILE_OBJECT,
|
||||
OWNER_SECURITY_INFORMATION, sid, nullptr,
|
||||
nullptr, nullptr) == ERROR_SUCCESS,
|
||||
"Cannot repair LAN file owner");
|
||||
}
|
||||
}
|
||||
void CreatePrivateDirectory(const std::filesystem::path& path) {
|
||||
if (!std::filesystem::exists(path)) {
|
||||
auto descriptor = PrivateSecurityDescriptor(true);
|
||||
SECURITY_ATTRIBUTES security{sizeof(security), descriptor, FALSE};
|
||||
bool ok = CreateDirectoryW(path.c_str(), &security);
|
||||
LocalFree(descriptor);
|
||||
Require(ok, "Cannot create LAN directory");
|
||||
}
|
||||
CheckPrivateWindowsPath(path);
|
||||
}
|
||||
#endif
|
||||
std::string Read(const std::filesystem::path& path) {
|
||||
#ifdef _WIN32
|
||||
CheckPrivateWindowsPath(path);
|
||||
#else
|
||||
struct stat status{};
|
||||
Require(lstat(path.c_str(), &status) == 0 && S_ISREG(status.st_mode) &&
|
||||
status.st_uid == getuid() && (status.st_mode & 0077) == 0,
|
||||
"LAN file must be owned by this user with mode 0600");
|
||||
#endif
|
||||
Require(std::filesystem::file_size(path) <= 64 * 1024, "LAN file too large");
|
||||
std::ifstream input(path, std::ios::binary);
|
||||
Require(input.good(), "Cannot read LAN file");
|
||||
return {std::istreambuf_iterator<char>(input), {}};
|
||||
}
|
||||
using private_file::Read;
|
||||
using private_file::Write;
|
||||
std::string BioText(BIO* bio) {
|
||||
char* data = nullptr;
|
||||
long size = BIO_get_mem_data(bio, &data);
|
||||
@@ -245,7 +82,7 @@ std::string PasswordKey(const std::filesystem::path& directory, bool create) {
|
||||
Require(
|
||||
RAND_bytes(reinterpret_cast<unsigned char*>(key.value.data()), 32) == 1,
|
||||
"Cannot generate LAN password storage key");
|
||||
PrivateWrite(path, key.value);
|
||||
Write(path, key.value);
|
||||
}
|
||||
auto key = Read(path);
|
||||
Require(key.size() == 32, "Invalid LAN password storage key");
|
||||
@@ -358,11 +195,11 @@ void CreateCertificate(const std::filesystem::path& key_path,
|
||||
Require(PEM_write_bio_PrivateKey(bio.get(), key.get(), nullptr, nullptr, 0,
|
||||
nullptr, nullptr) == 1,
|
||||
"Cannot encode private key");
|
||||
PrivateWrite(key_path, BioText(bio.get()));
|
||||
Write(key_path, BioText(bio.get()));
|
||||
(void)BIO_reset(bio.get());
|
||||
Require(PEM_write_bio_X509(bio.get(), cert.get()) == 1,
|
||||
"Cannot encode certificate");
|
||||
PrivateWrite(cert_path, BioText(bio.get()));
|
||||
Write(cert_path, BioText(bio.get()));
|
||||
}
|
||||
} // namespace
|
||||
unsigned ParsePort(const std::string& value) {
|
||||
@@ -539,18 +376,7 @@ Identity::Identity(std::filesystem::path directory)
|
||||
: directory_(std::move(directory)) {}
|
||||
void Identity::Load() {
|
||||
DevicePassword::CheckSupport();
|
||||
std::filesystem::create_directories(directory_.parent_path());
|
||||
#ifdef _WIN32
|
||||
CreatePrivateDirectory(directory_);
|
||||
#else
|
||||
std::filesystem::create_directories(directory_);
|
||||
struct stat directory_status{};
|
||||
Require(lstat(directory_.c_str(), &directory_status) == 0 &&
|
||||
S_ISDIR(directory_status.st_mode) &&
|
||||
directory_status.st_uid == getuid(),
|
||||
"Invalid LAN directory owner or type");
|
||||
Require(chmod(directory_.c_str(), 0700) == 0, "Cannot protect LAN directory");
|
||||
#endif
|
||||
private_file::EnsureDirectory(directory_);
|
||||
const auto store = directory_ / "identity.json";
|
||||
if (!std::filesystem::exists(store)) {
|
||||
Require(!std::filesystem::exists(private_key()) &&
|
||||
@@ -642,7 +468,7 @@ void Identity::Load() {
|
||||
state_.erase("password_attempts");
|
||||
}
|
||||
void Identity::Save(const nlohmann::json& state) {
|
||||
PrivateWrite(directory_ / "identity.json", state.dump(2));
|
||||
Write(directory_ / "identity.json", state.dump(2));
|
||||
}
|
||||
std::pair<std::string, nlohmann::json> Identity::GeneratePassword() {
|
||||
constexpr std::string_view alphabet =
|
||||
|
||||
@@ -24,6 +24,7 @@ int GuiApplication::RunUnattended(void* stop_event) {
|
||||
dll_log_path_ = exec_log_path_;
|
||||
InitializeLogger();
|
||||
config_center_ = std::make_unique<ConfigCenter>(cache_path_ + "/config.ini");
|
||||
InitializeTrustedDevices(root / "trusted-devices");
|
||||
if (config_center_->SetPrivacyScreen(false) != 0 ||
|
||||
config_center_->SetSaveRemotePreviews(false) != 0)
|
||||
return 1;
|
||||
@@ -44,6 +45,7 @@ int GuiApplication::RunUnattended(void* stop_event) {
|
||||
next_peer_attempt = now + 3000;
|
||||
if (CreateConnectionPeer() != 0) CloseConnectionPeer();
|
||||
}
|
||||
TickControllerTrust();
|
||||
HandleConnectionStatusChange();
|
||||
HandleScreenLock();
|
||||
HandleWindowsServiceIntegration();
|
||||
|
||||
@@ -82,12 +82,13 @@ function setup_targets()
|
||||
target("desktop_common")
|
||||
set_kind("object")
|
||||
add_deps("rd_log", "crossdesk_wire")
|
||||
add_packages("libyuv")
|
||||
add_packages("libyuv", "openssl3")
|
||||
add_packages("concurrentqueue")
|
||||
add_files("apps/desktop/src/common/*.cpp")
|
||||
if is_os("windows") then
|
||||
add_files("apps/desktop/src/platform/windows/system_info.cpp",
|
||||
"apps/desktop/src/platform/windows/screen_lock.cpp")
|
||||
add_syslinks("Advapi32", {public = true})
|
||||
elseif is_os("macosx") then
|
||||
add_files("apps/desktop/src/platform/macos/system_info.cpp",
|
||||
"apps/desktop/src/platform/macos/screen_lock.cpp")
|
||||
|
||||
@@ -222,6 +222,7 @@
|
||||
300000000000000000000001 /* Frameworks */,
|
||||
600000000000000000000002 /* Resources */,
|
||||
300000000000000000000002 /* Embed Frameworks */,
|
||||
700000000000000000000002 /* Generate OpenSSL symbols */,
|
||||
);
|
||||
buildRules = (
|
||||
);
|
||||
@@ -318,6 +319,24 @@
|
||||
shellPath = /bin/zsh;
|
||||
shellScript = "set -e\npython3 \"${PROJECT_DIR}/scripts/prepare_source_metadata.py\" --output \"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/SourceMetadata.json\"\n\"${PROJECT_DIR}/scripts/build_minirtc_ios.sh\"\npython3 \"${PROJECT_DIR}/scripts/verify_native_build.py\" --inputs \"${PROJECT_DIR}/Vendor/${PLATFORM_NAME}/${CONFIGURATION}/libCrossDeskMiniRTC.a.inputs.sha256\" --xmake-version \"${PROJECT_DIR}/Vendor/${PLATFORM_NAME}/${CONFIGURATION}/xmake-version.txt\"\npython3 \"${PROJECT_DIR}/scripts/package_openssl_ios.py\" --inputs \"${PROJECT_DIR}/Vendor/${PLATFORM_NAME}/${CONFIGURATION}/libCrossDeskMiniRTC.a.inputs.sha256\" --output \"${BUILT_PRODUCTS_DIR}/OpenSSL.framework\" --platform \"${PLATFORM_NAME}\"\n";
|
||||
};
|
||||
700000000000000000000002 /* Generate OpenSSL symbols */ = {
|
||||
isa = PBXShellScriptBuildPhase;
|
||||
alwaysOutOfDate = 1;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
);
|
||||
inputPaths = (
|
||||
"$(TARGET_BUILD_DIR)/$(FRAMEWORKS_FOLDER_PATH)/OpenSSL.framework/OpenSSL",
|
||||
"$(PROJECT_DIR)/scripts/generate_openssl_symbols.py",
|
||||
);
|
||||
name = "Generate OpenSSL symbols";
|
||||
outputPaths = (
|
||||
"$(DWARF_DSYM_FOLDER_PATH)/OpenSSL.framework.dSYM/Contents/Resources/DWARF/OpenSSL",
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
shellPath = /bin/zsh;
|
||||
shellScript = "set -e\nif [[ \"${DEBUG_INFORMATION_FORMAT}\" == \"dwarf-with-dsym\" ]]; then\n python3 -B \"${PROJECT_DIR}/scripts/generate_openssl_symbols.py\" --binary \"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/OpenSSL.framework/OpenSSL\" --output \"${DWARF_DSYM_FOLDER_PATH}/OpenSSL.framework.dSYM\"\nfi\n";
|
||||
};
|
||||
/* End PBXShellScriptBuildPhase section */
|
||||
|
||||
/* Begin PBXSourcesBuildPhase section */
|
||||
@@ -496,7 +515,7 @@
|
||||
buildSettings = {
|
||||
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
CURRENT_PROJECT_VERSION = 3;
|
||||
CURRENT_PROJECT_VERSION = 4;
|
||||
ENABLE_BITCODE = NO;
|
||||
ENABLE_USER_SCRIPT_SANDBOXING = NO;
|
||||
FRAMEWORK_SEARCH_PATHS = (
|
||||
@@ -548,7 +567,7 @@
|
||||
buildSettings = {
|
||||
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
CURRENT_PROJECT_VERSION = 3;
|
||||
CURRENT_PROJECT_VERSION = 4;
|
||||
ENABLE_BITCODE = NO;
|
||||
ENABLE_USER_SCRIPT_SANDBOXING = NO;
|
||||
FRAMEWORK_SEARCH_PATHS = (
|
||||
|
||||
@@ -323,6 +323,7 @@ final class RemoteKeyboardInputBar: UIView {
|
||||
let computerButton = UIButton(type: .system)
|
||||
computerButton.setTitle(NSLocalizedString("电脑键盘", comment: ""), for: .normal)
|
||||
computerButton.titleLabel?.font = .systemFont(ofSize: 15, weight: .medium)
|
||||
computerButton.setContentHuggingPriority(.defaultHigh, for: .horizontal)
|
||||
computerButton.accessibilityLabel = NSLocalizedString("切换到电脑键盘", comment: "")
|
||||
computerButton.addTarget(self, action: #selector(switchToComputer), for: .touchUpInside)
|
||||
|
||||
@@ -337,7 +338,8 @@ final class RemoteKeyboardInputBar: UIView {
|
||||
row.translatesAutoresizingMaskIntoConstraints = false
|
||||
addSubview(row)
|
||||
NSLayoutConstraint.activate([
|
||||
computerButton.widthAnchor.constraint(equalToConstant: 80),
|
||||
// Keep the minimum touch width while allowing longer translations.
|
||||
computerButton.widthAnchor.constraint(greaterThanOrEqualToConstant: 80),
|
||||
dismissButton.widthAnchor.constraint(equalToConstant: RemoteKeyboardDismissButton.sideLength),
|
||||
row.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 4),
|
||||
row.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor, constant: -4),
|
||||
|
||||
@@ -384,6 +384,15 @@ final class RemoteTouchSurface: UIView, UIGestureRecognizerDelegate {
|
||||
let newScale = min(max(oldScale * recognizer.scale, 1),
|
||||
maximumViewportScale)
|
||||
recognizer.scale = 1
|
||||
if newScale == 1 {
|
||||
// Pinching back to fit also ends any cursor-edge pan, even
|
||||
// when the gesture starts at 1x with an off-center image.
|
||||
viewportScale = 1
|
||||
viewportOffset = .zero
|
||||
edgePanned = false
|
||||
publishViewport()
|
||||
return
|
||||
}
|
||||
guard abs(newScale - oldScale) > 0.0001 else { return }
|
||||
|
||||
let center = CGPoint(x: bounds.midX, y: bounds.midY)
|
||||
@@ -402,7 +411,8 @@ final class RemoteTouchSurface: UIView, UIGestureRecognizerDelegate {
|
||||
pinchActive = false
|
||||
if viewportScale <= 1.001 {
|
||||
viewportScale = 1
|
||||
viewportOffset = constrainedViewportOffset(viewportOffset, scale: 1)
|
||||
viewportOffset = .zero
|
||||
edgePanned = false
|
||||
publishViewport()
|
||||
}
|
||||
default:
|
||||
|
||||
@@ -140,8 +140,11 @@ struct RemoteSessionView: View {
|
||||
|
||||
}
|
||||
.frame(width: proxy.size.width, height: proxy.size.height)
|
||||
.clipped()
|
||||
}
|
||||
.ignoresSafeArea()
|
||||
// Fit video and input to the same unobscured area. Only the black
|
||||
// background extends beneath the home indicator and display cutout.
|
||||
.ignoresSafeArea(.keyboard)
|
||||
.transaction { transaction in
|
||||
transaction.animation = nil
|
||||
transaction.disablesAnimations = true
|
||||
@@ -855,7 +858,8 @@ private struct RemoteComputerKeyboard: View {
|
||||
switchKeyboard()
|
||||
}
|
||||
.font(.system(size: 15, weight: .medium))
|
||||
.frame(width: 80, height: 44)
|
||||
.fixedSize(horizontal: true, vertical: false)
|
||||
.frame(minWidth: 80, minHeight: 44)
|
||||
.accessibilityLabel(NSLocalizedString("切换到系统键盘", comment: ""))
|
||||
if verticalSizeClass == .compact {
|
||||
pageTabs
|
||||
|
||||
+31
-4
@@ -150,11 +150,28 @@ Commit and push the intended signing settings and shared scheme to the branch
|
||||
selected by the workflow before starting a build.
|
||||
|
||||
Xcode Cloud automatically runs `ci_scripts/ci_post_clone.sh` next to the Xcode
|
||||
project. It installs Xmake, CMake, and Python 3.13 with Homebrew, prepares an
|
||||
project. It installs Xmake, CMake, pkgconf, and Python 3.13 with Homebrew, prepares an
|
||||
isolated host Python environment with pip/setuptools/wheel, verifies the minimum
|
||||
Xmake version, and initializes the pinned MiniRTC submodules. The native build
|
||||
phase selects these tools again and preserves Cloud's proxy settings for package
|
||||
downloads. No manual workflow PATH setting or `sudo` command is required.
|
||||
Xmake version and host `pkg-config`, and initializes the pinned MiniRTC submodules.
|
||||
It exposes both `pkgconf` and `pkg-config` from the Homebrew formula in the host
|
||||
tool directory, allowing GLib's Meson build to resolve Xmake's iOS PCRE2 and
|
||||
libffi packages instead of compiling bundled fallback versions. The native build
|
||||
phase checks and selects these tools again and preserves Cloud's proxy settings
|
||||
for package downloads. No manual workflow PATH setting or `sudo` command is required.
|
||||
|
||||
For **Archive - iOS**, `ci_scripts/ci_pre_xcodebuild.sh` builds the native
|
||||
dependencies before `xcodebuild`, using `Release / iphoneos / arm64` to match the
|
||||
shared scheme. It streams verbose build output and prints a heartbeat with
|
||||
process status every minute, avoiding the archive's 30-minute inactivity timeout
|
||||
while Xcode buffers Run Script output. The archive's native build phase reuses
|
||||
the same dependency cache and libraries, retaining its input and toolchain
|
||||
validation. Other actions keep their existing native build behavior.
|
||||
|
||||
The prebuild records its output under `.xmake/xcode-cloud-logs` and copies package
|
||||
installation and Meson logs there on failure. These files are temporary, so the
|
||||
script also prints the complete GLib install log and the last 200 lines of other
|
||||
failed package logs and GLib's Meson log into the retained Cloud step output.
|
||||
Inspect **Run ci_pre_xcodebuild.sh script** when diagnosing native build failures.
|
||||
|
||||
The first archive compiles native dependencies from source and may take longer
|
||||
than later builds. After changing the setup script, start a build from the new
|
||||
@@ -586,6 +603,16 @@ Xcode removes the static archive from the embedded framework (it may insert a
|
||||
small resource-framework stub) and preserves `PrivacyInfo.xcprivacy`, source
|
||||
metadata and license texts. The application does not load a dynamic OpenSSL copy.
|
||||
|
||||
For builds using `DWARF with dSYM File`, the **Generate OpenSSL symbols** phase
|
||||
runs after embedding. It verifies that Xcode's placeholder has no executable
|
||||
code, generates `OpenSSL.framework.dSYM` from that exact binary, and checks its
|
||||
UUIDs. This companion describes the empty resource stub, not the statically
|
||||
linked OpenSSL implementation. Xcode includes it alongside the app's dSYM in
|
||||
the archive so Organizer can upload symbols for every embedded Mach-O binary.
|
||||
`verify_app_bundle.py` checks both UUID matches when given an archived app;
|
||||
for an ordinary Release build, pass `--dsym-dir /path/to/Release-iphoneos`.
|
||||
The CI ZIP includes both dSYM bundles.
|
||||
|
||||
The iOS-specific OpenSSL recipe disables POSIX metadata calls, automatic
|
||||
`openssl.cnf` loading, and the unused file-URI store provider. It selects the
|
||||
upstream `getrandom` entropy backend, which uses Darwin's `getentropy` on the
|
||||
|
||||
@@ -13,8 +13,9 @@ if ! command -v brew >/dev/null 2>&1; then
|
||||
fi
|
||||
|
||||
echo "Installing Xcode Cloud native build tools"
|
||||
brew install xmake cmake python@3.13
|
||||
brew install xmake cmake pkgconf python@3.13
|
||||
BREW_PREFIX="$(brew --prefix)"
|
||||
PKGCONF_PREFIX="$(brew --prefix pkgconf)"
|
||||
PYTHON_PREFIX="$(brew --prefix python@3.13)"
|
||||
|
||||
# Match GitHub Actions' host Python dependencies without modifying Homebrew's
|
||||
@@ -25,6 +26,14 @@ PYTHON_PREFIX="$(brew --prefix python@3.13)"
|
||||
for tool in xmake xrepo cmake ctest cpack; do
|
||||
ln -sf "${BREW_PREFIX}/bin/${tool}" "${HOST_TOOLS_DIR}/bin/${tool}"
|
||||
done
|
||||
# Meson needs the host pkg-config executable to find Xmake's iOS packages.
|
||||
# Without it, GLib falls back to bundled PCRE2/libffi sources. Use the formula's
|
||||
# prefix so an older pkg-config elsewhere on PATH cannot shadow this tool.
|
||||
for tool in pkgconf pkg-config; do
|
||||
ln -sf "${PKGCONF_PREFIX}/bin/${tool}" "${HOST_TOOLS_DIR}/bin/${tool}"
|
||||
done
|
||||
echo "Verifying Xcode Cloud host pkg-config"
|
||||
"${HOST_TOOLS_DIR}/bin/pkg-config" --version
|
||||
|
||||
"${HOST_TOOLS_DIR}/bin/xmake" --version > "${HOST_TOOLS_DIR}/xmake-version.txt"
|
||||
"${HOST_TOOLS_DIR}/bin/python3" -B "${IOS_DIR}/scripts/verify_native_build.py" \
|
||||
|
||||
Executable
+85
@@ -0,0 +1,85 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# The shared CrossDeskMobile scheme archives Release for physical iOS devices.
|
||||
# Leave other actions to their existing Xcode build phase and SDK selection.
|
||||
if [[ "${CI_XCODEBUILD_ACTION:-}" != "archive" || "${CI_PRODUCT_PLATFORM:-}" != "iOS" ]]; then
|
||||
echo "Skipping native prebuild for ${CI_XCODEBUILD_ACTION:-unknown} (${CI_PRODUCT_PLATFORM:-unknown})"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
|
||||
IOS_DIR="$(dirname "$SCRIPT_DIR")"
|
||||
LOG_DIR="${IOS_DIR}/.xmake/xcode-cloud-logs"
|
||||
mkdir -p "$LOG_DIR"
|
||||
|
||||
collect_dependency_logs() {
|
||||
local log_root="$1"
|
||||
local label="$2"
|
||||
local log_file relative_path destination
|
||||
[[ -d "$log_root" ]] || return 0
|
||||
|
||||
while IFS= read -r -d '' log_file; do
|
||||
relative_path="${log_file#"${log_root}/"}"
|
||||
destination="${LOG_DIR}/dependencies/${label}/${relative_path}"
|
||||
mkdir -p "$(dirname "$destination")"
|
||||
cp "$log_file" "$destination"
|
||||
|
||||
# Cloud does not publish arbitrary files as downloadable artifacts.
|
||||
# Include GLib's complete install log in the retained step output, plus
|
||||
# the tail of Meson and other failed package logs for context.
|
||||
if [[ "$log_file" == */glib/*/logs/install.txt ]]; then
|
||||
echo "GLib install log: ${log_file}"
|
||||
cat "$log_file"
|
||||
elif [[ "$log_file" == */installdir.failed/logs/* || \
|
||||
"$log_file" == */glib/*/meson-logs/meson-log.txt ]]; then
|
||||
echo "Last 200 lines: ${log_file}"
|
||||
tail -n 200 "$log_file"
|
||||
fi
|
||||
done < <(find "$log_root" -type f \( \
|
||||
-path '*/logs/*.txt' -o -path '*/meson-logs/meson-log.txt' \) -print0)
|
||||
}
|
||||
|
||||
MONITOR_PID=""
|
||||
finish() {
|
||||
local build_exit_code=$?
|
||||
trap - EXIT
|
||||
set +e
|
||||
if [[ -n "$MONITOR_PID" ]]; then
|
||||
kill "$MONITOR_PID" 2>/dev/null
|
||||
wait "$MONITOR_PID" 2>/dev/null
|
||||
fi
|
||||
if [[ "$build_exit_code" -ne 0 ]]; then
|
||||
echo "iOS native prebuild failed with exit code ${build_exit_code}"
|
||||
collect_dependency_logs "${XMAKE_GLOBALDIR:-${HOME}}/.xmake/cache/packages" cache
|
||||
collect_dependency_logs "${IOS_DIR}/.xmake/packages" installed
|
||||
echo "Native build diagnostics: ${LOG_DIR}"
|
||||
fi
|
||||
exit "$build_exit_code"
|
||||
}
|
||||
trap finish EXIT
|
||||
trap 'exit 130' INT
|
||||
trap 'exit 143' TERM
|
||||
|
||||
# Run outside xcodebuild so downloads and compiler output are streamed instead
|
||||
# of buffered by a Run Script phase. Keep the step active during quiet commands.
|
||||
(
|
||||
SLEEP_PID=""
|
||||
trap 'kill "${SLEEP_PID}" 2>/dev/null || true; wait "${SLEEP_PID}" 2>/dev/null || true; exit 0' TERM INT
|
||||
while true; do
|
||||
sleep 60 &
|
||||
SLEEP_PID=$!
|
||||
wait "$SLEEP_PID" || exit 0
|
||||
echo "iOS native prebuild still running at $(date -u +%FT%TZ)"
|
||||
ps -axo pid,ppid,state,etime,pcpu,comm | \
|
||||
awk 'NR == 1 || /xmake|clang|cmake|ninja|meson|python|git|curl|make|zsh/' || true
|
||||
done
|
||||
) &
|
||||
MONITOR_PID=$!
|
||||
|
||||
echo "Prebuilding iOS native dependencies for Release / iphoneos / arm64"
|
||||
CONFIGURATION=Release PLATFORM_NAME=iphoneos CURRENT_ARCH=arm64 \
|
||||
CROSSDESK_NATIVE_VERBOSE=1 \
|
||||
"${IOS_DIR}/scripts/build_minirtc_ios.sh" 2>&1 | tee "${LOG_DIR}/ios-native-build.log"
|
||||
echo "iOS native prebuild finished; Archive will reuse the native build products"
|
||||
@@ -50,10 +50,12 @@ fi
|
||||
# tools again here instead of relying on exported PATH changes surviving.
|
||||
if [[ "${CI_XCODE_CLOUD:-}" == "TRUE" ]]; then
|
||||
CLOUD_HOST_BIN="${IOS_DIR}/.xmake/xcode-cloud-host/bin"
|
||||
if [[ ! -x "${CLOUD_HOST_BIN}/xmake" || ! -x "${CLOUD_HOST_BIN}/python3" ]]; then
|
||||
print -u2 "Xcode Cloud build tools are missing. Run ci_scripts/ci_post_clone.sh before building."
|
||||
exit 69
|
||||
fi
|
||||
for host_tool in xmake python3 pkg-config; do
|
||||
if [[ ! -x "${CLOUD_HOST_BIN}/${host_tool}" ]]; then
|
||||
print -u2 "Xcode Cloud host tool ${host_tool} is missing. Run ci_scripts/ci_post_clone.sh before building."
|
||||
exit 69
|
||||
fi
|
||||
done
|
||||
export PATH="${CLOUD_HOST_BIN}:${PATH}"
|
||||
fi
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Generate matching symbols for Xcode's embedded, codeless OpenSSL framework."""
|
||||
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
def uuids(path):
|
||||
output = subprocess.check_output(["xcrun", "dwarfdump", "--uuid", str(path)], text=True)
|
||||
result = dict((arch, uuid.upper()) for uuid, arch in
|
||||
re.findall(r"UUID: ([0-9A-Fa-f-]{36}) \(([^)]+)\)", output))
|
||||
if not result:
|
||||
raise ValueError(f"No Mach-O UUIDs found: {path}")
|
||||
return result
|
||||
|
||||
|
||||
def verify_symbols(binary, dsym):
|
||||
dwarf = dsym / "Contents/Resources/DWARF" / binary.name
|
||||
if not dwarf.is_file():
|
||||
raise ValueError(f"Missing dSYM DWARF file: {dwarf}")
|
||||
if uuids(binary) != uuids(dwarf):
|
||||
raise ValueError(f"dSYM UUIDs do not match {binary}: {dwarf}")
|
||||
|
||||
|
||||
def generate(binary, output):
|
||||
# This is only for Xcode's resource placeholder. Real OpenSSL code remains
|
||||
# statically linked into the app and must not receive an empty dSYM here.
|
||||
commands = subprocess.check_output(["xcrun", "otool", "-l", str(binary)], text=True)
|
||||
text_sizes = re.findall(
|
||||
r"sectname __text\s+segname __TEXT\s+addr \S+\s+size (0x[0-9a-fA-F]+)", commands)
|
||||
if not text_sizes or any(int(size, 16) for size in text_sizes):
|
||||
raise ValueError(f"Expected a codeless OpenSSL resource stub: {binary}")
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
# No function-level debug info exists in this empty stub. dsymutil still
|
||||
# emits a valid UUID-matched DWARF companion; quiet suppresses that expected
|
||||
# warning only after verifying that there is no executable code to lose.
|
||||
subprocess.run(["xcrun", "dsymutil", "--quiet", str(binary), "-o", str(output)], check=True)
|
||||
verify_symbols(binary, output)
|
||||
subprocess.run(["xcrun", "dwarfdump", "--verify", str(output)], check=True)
|
||||
print(f"Generated UUID-matched OpenSSL resource symbols: {output}")
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--binary", required=True, type=Path)
|
||||
parser.add_argument("--output", required=True, type=Path)
|
||||
args = parser.parse_args()
|
||||
generate(args.binary, args.output)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except (OSError, ValueError, subprocess.CalledProcessError) as error:
|
||||
print(f"OpenSSL symbols: {error}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
@@ -10,8 +10,10 @@ import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from generate_openssl_symbols import verify_symbols
|
||||
|
||||
def verify(app, require_release=False):
|
||||
|
||||
def verify(app, require_release=False, dsym_dir=None):
|
||||
info = plistlib.loads((app / "Info.plist").read_bytes())
|
||||
binary = app / info["CFBundleExecutable"]
|
||||
subprocess.run(["xcrun", "lipo", str(binary), "-verify_arch", "arm64"], check=True)
|
||||
@@ -63,6 +65,14 @@ def verify(app, require_release=False):
|
||||
sdk_symbols = subprocess.check_output(["xcrun", "nm", "-gU", str(embedded_binary)], text=True)
|
||||
if re.search(r"\b_(?:SSL_new|OPENSSL_init_crypto)\b", sdk_symbols):
|
||||
raise ValueError("Unexpected dynamic OpenSSL implementation in resource framework")
|
||||
# Archive validation includes the exact files that Organizer uploads.
|
||||
if dsym_dir is None and app.parents[2].suffix == ".xcarchive":
|
||||
dsym_dir = app.parents[2] / "dSYMs"
|
||||
if dsym_dir is not None:
|
||||
verify_symbols(binary, dsym_dir / f"{app.name}.dSYM")
|
||||
if embedded_binary.exists():
|
||||
verify_symbols(embedded_binary, dsym_dir / "OpenSSL.framework.dSYM")
|
||||
print("Verified app and embedded OpenSSL dSYM UUIDs")
|
||||
linked = subprocess.check_output(["xcrun", "otool", "-L", str(binary)], text=True)
|
||||
if "OpenSSL.framework" in linked:
|
||||
raise ValueError("OpenSSL must be statically linked into the application")
|
||||
@@ -79,8 +89,9 @@ def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("app", type=Path)
|
||||
parser.add_argument("--require-release", action="store_true")
|
||||
parser.add_argument("--dsym-dir", type=Path)
|
||||
args = parser.parse_args()
|
||||
verify(args.app, args.require_release)
|
||||
verify(args.app.resolve(), args.require_release, args.dsym_dir)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
dist/
|
||||
@@ -0,0 +1,231 @@
|
||||
# CrossDesk MCP
|
||||
|
||||
让本机 Agent 通过 MCP 观察并操作远端 CrossDesk,同时为用户提供浏览器、终端或 MCP Apps 监看面板。控制端不需要安装 CrossDesk 桌面客户端;被控端仍需运行兼容的 CrossDesk,并连接支持 `cli` 注册的信令服务器。
|
||||
|
||||
此目录实现 MCP 包 `@chunkdash/crossdesk-mcp` 0.1.0,绑定原生 CLI 包 0.2.0。**源码实现和本地打包不代表这些版本已经发布到 npm。** 发布后的安装示例需要以实际可用版本为准。
|
||||
|
||||
## 从源码运行
|
||||
|
||||
仓库根目录构建原生程序(已有工作区可保留原有桌面构建选项):
|
||||
|
||||
```sh
|
||||
sh apps/cli/scripts/prepare-deps.sh
|
||||
xmake f -y --CROSSDESK_BUILD_DESKTOP=n --CROSSDESK_BUILD_CLI=y --USE_CUDA=n -m release
|
||||
xmake build crossdesk-cli
|
||||
cd apps/mcp
|
||||
npm ci
|
||||
npm run build
|
||||
```
|
||||
|
||||
Node.js 要求 22 或以上。在 Agent 宿主中配置本地 stdio 进程,例如 macOS arm64 源码构建:
|
||||
|
||||
```json
|
||||
{
|
||||
"mcpServers": {
|
||||
"crossdesk": {
|
||||
"command": "node",
|
||||
"args": [
|
||||
"/absolute/path/crossdesk/apps/mcp/dist/main.js",
|
||||
"serve", "--stdio", "--panel=auto",
|
||||
"--native-binary", "/absolute/path/crossdesk/build/macosx/arm64/release/crossdesk-cli"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Windows 和 Linux 使用各自构建产物路径;`--native-binary` 必须指向可信原生可执行程序,不接受 shell 命令。也可以通过 `CROSSDESK_NATIVE_BINARY` 显式指定开发产物。未指定时只从包内的固定版本平台依赖解析,不搜索 PATH。服务启动不连接远端、不打开浏览器;连接时才检查 `doctor` 协议并启动原生子进程。
|
||||
|
||||
发布包的配置形式:
|
||||
|
||||
```json
|
||||
{
|
||||
"mcpServers": {
|
||||
"crossdesk": {
|
||||
"command": "npx",
|
||||
"args": ["-y", "@chunkdash/crossdesk-mcp@<已发布版本>", "serve", "--stdio", "--panel=auto"]
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
平台依赖覆盖 macOS arm64/x64、glibc Linux arm64/x64 和 Windows x64。安装时保留 optional dependencies;不需要另装全局 CLI。原生运行要求沿用 [CLI 分发文档](../cli/docs/npm-distribution.md)。`npm ci` 仅安装此源码目录的开发依赖,开发运行需使用 `--native-binary`;生成的发布包才包含固定平台依赖。
|
||||
|
||||
## 用户连接流程
|
||||
|
||||
1. 对 Agent 说“打开 CrossDesk”。`crossdesk_open` 创建 `view_id`,返回面板选择和等待用户状态。
|
||||
2. 在面板选择已保存设备,或输入 9 位设备 ID;密码填写在面板里。可选择仅查看或允许 Agent 控制。
|
||||
3. “保存设备”“记住密码”“允许以后自动连接”分别控制设备簿、系统凭据库、持久权限。后两项要求保存设备,默认均关闭。保存密码不自动授予控制权限。
|
||||
4. 有待执行的远程任务时,Agent 在打开面板后立即调用 `crossdesk_get_session({view_id, wait_ms: 25000})` 等待连接。连接就绪后,工具直接返回会话 ID 和权限状态,Agent 继续任务;用户无需回到对话发送连接确认,也无需点击继续按钮。浏览器和 Apps 面板使用同一流程。
|
||||
5. Agent 获取独立截图,用截图的 `observation_id` 和图像坐标提交动作。面板预览不消耗模型截图序列。
|
||||
|
||||
`crossdesk_open` 立即返回,让 Apps 宿主有机会渲染面板;等待连接在后续 `crossdesk_get_session` 调用中完成。按 `view_id` 查询默认最多等待 25 秒,`wait_ms: 0` 则立即查询;未就绪时返回 `wait_timed_out: true`,Agent 在原任务仍待执行时继续等待。按 `session_id` 查询仍立即返回。取消等待只取消本次查询,不断开用户正在建立或使用的连接。用户主动断开后返回 `closed`,Agent 停止等待,不自动重连。
|
||||
|
||||
面板发起的连接失败后,按 `view_id` 等待也会返回 `failed` 和错误码,避免 Agent 一直停留在等待用户状态。断开正在建立的连接会取消整个连接尝试。多个面板和 Agent 同时查询状态时共用一次原生查询,单次查询取消或暂时繁忙不会断开会话。`crossdesk_connect` 在连接成功但面板初始化失败时仍返回有效的 `session_id`,并附加 `panel_error`。
|
||||
|
||||
自动继续依赖宿主保持当前 Agent 任务及工具调用流程。它不通过网页伪造用户消息,也不承诺从已经结束的对话中主动唤醒 Agent;此类唤醒由宿主负责。
|
||||
|
||||
人工暂停由原生层锁定,模型不能通过 `set_control(true)` 恢复;只有当前授权面板的恢复按钮可以恢复。仅查看连接不能升级为控制连接,需重新连接并授权。关闭/隐藏面板仅停止预览,远程会话保留;断开按钮或 `crossdesk_disconnect` 才结束会话。
|
||||
|
||||
同一 MCP 进程默认只保留一个活动远程会话。相同配置目录中的进程用设备锁避免同时连接同一目标;它不协调另一个系统用户、另一份配置目录或独立桌面客户端。
|
||||
|
||||
## 面板策略
|
||||
|
||||
`--panel` 优先于设备配置中的 `panel`,默认 `auto`。
|
||||
|
||||
| 模式 | 行为 |
|
||||
| --- | --- |
|
||||
| `auto` | 有本机 GUI 时打开浏览器;无 GUI 且检测到终端环境时提供 CLI 画面入口;两者均无则使用 `off` |
|
||||
| `apps` | 只使用 MCP Apps;能力不匹配返回 `PANEL_UNSUPPORTED` |
|
||||
| `browser` | 只使用本地浏览器,不附加 Apps 工具资源元数据 |
|
||||
| `terminal` | 提供独立终端 `view` 命令,共用当前 MCP 会话,不启动网页服务 |
|
||||
| `off` | 不启动网页服务,保留截图和操作工具,凭据通过独立 `setup` 配置 |
|
||||
|
||||
`auto` 不再优先选择 MCP Apps;需要宿主内嵌面板时显式使用 `--panel=apps`。Apps 使用 `extensions["io.modelcontextprotocol/ui"].mimeTypes` 中的 `text/html;profile=mcp-app` 判断能力。旧协议读取 initialize 能力;2026-07-28 协议读取当前请求的能力,不按宿主产品名猜测支持情况。Apps 的卡片、侧栏或窗口形式由宿主决定。
|
||||
|
||||
Linux 根据 `DISPLAY` / `WAYLAND_DISPLAY` 判断 GUI;macOS 检查当前用户的桌面登录会话,SSH 会话不自动打开远端 Mac 的浏览器;Windows 排除 SSH 和 Services 会话。终端判断包含 TTY、`SSH_TTY` 和非 `dumb` 的 `TERM`,因为 MCP 的 stdin/stdout 通常是管道。环境变量被宿主清除或识别不适用时,可显式设置 `--panel=terminal` 或 `--panel=browser`。
|
||||
|
||||
终端模式的 `crossdesk_open` / `crossdesk_connect` 返回 `terminal_command`。用户在同一主机的另一个交互终端执行(源码运行时将 `crossdesk-mcp` 替换为 `node /absolute/path/apps/mcp/dist/main.js`):
|
||||
|
||||
```sh
|
||||
crossdesk-mcp view --view <view_id> --config-dir <相同的配置目录>
|
||||
```
|
||||
|
||||
该命令提供设备选择、隐藏密码输入和本次控制授权;已建立会话则直接显示画面。终端通过受限配置目录中的运行描述符和认证本地 IPC 复用 MCP 的原生连接,不启动第二个远程连接,也不占用 Agent 的终端或 MCP stdio。Agent 应展示命令供用户执行,再通过 `crossdesk_get_session` 等待;不能在工具输出捕获通道中运行 `view`。无交互终端时 `view` 返回 `VIEW_REQUIRES_TERMINAL`。
|
||||
|
||||
终端画面采用原生 CLI 的 ANSI 四分块渲染器,目标 8 FPS,最多 160 列 × 60 行,根据终端环境使用 8/256/真彩色。默认字符宽高比 0.5,可用 `--cell-aspect`(0.2–2)调整。此入口暂不使用 Kitty/Sixel。`p` 暂停 Agent、`r` 恢复已授权控制、`d` 断开、`q` / Ctrl+C 仅关闭画面并恢复终端;关闭画面后会话保留。暂停仍由原生层锁定,Agent 不能自行恢复。
|
||||
|
||||
浏览器/Apps 与终端均绘制 Agent 最后成功发送的鼠标位置,支持移动、点击、滚动和拖拽;坐标来自原生层对截图缩放及裁剪的映射。暂停或关闭控制时隐藏,切换显示器或图像尺寸变化时清除。该指针是 Agent 的操作位置提示,不代表远端输入执行 ACK,也不跟踪远端用户自己的鼠标。它不写入模型截图或源视频。
|
||||
|
||||
Apps 资源为 `ui://crossdesk/viewer.html`,工具只在允许使用 Apps 且当前请求支持时暴露 UI 元数据和面板专用接口。页面回报 `crossdesk_ui_ready` 后才标记就绪;10 秒未回报显示 `not_confirmed`,不自动弹第二个浏览器。用户明确要求回退时调用 `crossdesk_open_browser`;该工具不附带 Apps 元数据,`apps`/`off` 策略拒绝覆盖。
|
||||
|
||||
浏览器面板只监听随机端口的 `127.0.0.1`,与 MCP 服务同一进程。它不是 MCP Streamable HTTP,也不向局域网提供 MCP 接入。MCP、CLI、用户浏览器应在同一控制端电脑上运行;SSH 容器/远程宿主的浏览器转发不属于此本地接入模式。
|
||||
|
||||
页面标题为 CrossDesk MCP,仅保留正式应用标志,使用文字按钮和简洁状态提示。布局填满浏览器或宿主面板的可用宽高,远程画面保持原比例自动缩放;连接设置在高度不足时独立滚动。连接后侧栏显示设备与授权信息,暂停、恢复和断开按钮位于画面工具栏。「放大画面」收起侧栏,按 Esc 还原;窄屏默认显示连接表单,连接后显示画面,可用「连接信息」展开设备详情。界面资源均可离线使用,保存设备与持久授权仍由用户分别选择。
|
||||
|
||||
首次打开使用 URL fragment 中的短期一次性 bootstrap,页面立即清除 fragment,并换取 HttpOnly/SameSite cookie 和 CSRF 令牌。模型只收到无特权的网页地址。复制无凭据地址到另一浏览器不能获得会话控制权。
|
||||
|
||||
无法自动打开浏览器时,在自己的终端运行工具结果 `pairing_command` 中的命令和参数。例如:
|
||||
|
||||
```sh
|
||||
crossdesk-mcp setup --open-panel --view <view_id> --config-dir <相同的配置目录>
|
||||
```
|
||||
|
||||
通过源码运行时用 `node /absolute/path/apps/mcp/dist/main.js` 替换 `crossdesk-mcp`。配对通过本用户配置目录中的受限运行描述符和本地 IPC 完成,不允许匿名 HTTP 请求签发控制凭证。`setup` 使用用户终端的桌面环境打开浏览器;仍无法打开时,只在该交互终端显示 60 秒有效的一次性地址,不将它返回 MCP 或发送到聊天。面板最多保留 32 个视图;再次打开时会清理超过 30 分钟未使用的视图。浏览器会话最长 12 小时,失效后重新打开。
|
||||
|
||||
## 无面板配置与凭据
|
||||
|
||||
在用户自己的交互终端执行(不能在宿主 stdio 通道中交互输入密码):
|
||||
|
||||
```sh
|
||||
crossdesk-mcp setup
|
||||
```
|
||||
|
||||
配置默认存放于 `~/.config/crossdesk/mcp/`,可用 `--config-dir` 或 `CROSSDESK_MCP_CONFIG_DIR` 覆盖。`devices.json` 包含设备、面板/视频选项及凭据引用;密码保存到 macOS Keychain、Windows 凭据库或 Linux Secret Service。凭据库不可用就返回明确错误,没有明文文件降级。面板的临时连接仍可使用用户现场输入的密码。持久保存失败会保留已建立连接,并在面板显示错误。
|
||||
|
||||
也可在 `setup` 中指定外部密码环境变量名称,服务读取该变量作为凭据提供器。示例配置(没有密码值):
|
||||
|
||||
```json
|
||||
{
|
||||
"version": 1,
|
||||
"panel": "off",
|
||||
"server": "api.crossdesk.cn",
|
||||
"port": 9099,
|
||||
"devices": [{
|
||||
"alias": "work-pc",
|
||||
"device_id": "123456789",
|
||||
"server": "api.crossdesk.cn",
|
||||
"port": 9099,
|
||||
"authorization": "control",
|
||||
"credential": "environment",
|
||||
"environment_variable": "MY_CROSSDESK_PASSWORD"
|
||||
}]
|
||||
}
|
||||
```
|
||||
|
||||
授权范围和可用凭据缺一不可。模型不能提供任意 `device_id` 绕过面板确认;模型侧 `crossdesk_connect` 仅接受已授权设备别名。不要把密码发给 Agent,也不要把包含密码的命令行写进宿主配置。
|
||||
|
||||
Apps 依赖宿主遵守 app-only 工具可见性和 `_meta` 的模型不可见约定;服务另检查每个视图的随机 UI 能力令牌和 generation。app-only 标记本身不是可信身份凭据。能够读取同用户进程/文件或执行任意本机代码的 Agent 不受此边界隔离。
|
||||
|
||||
## 模型工具
|
||||
|
||||
| 工具 | 作用 |
|
||||
| --- | --- |
|
||||
| `crossdesk_open` | 打开设备选择/连接面板,返回 `view_id` |
|
||||
| `crossdesk_open_browser` | 用户显式请求的浏览器入口/回退 |
|
||||
| `crossdesk_list_devices` | 本机设备簿和授权摘要,不提供云发现 |
|
||||
| `crossdesk_connect` | 通过别名连接已经授权且有凭据的设备 |
|
||||
| `crossdesk_get_session` | `session_id`、`view_id` 严格二选一;按 `view_id` 可等待连接,`wait_ms` 范围 0–25000 |
|
||||
| `crossdesk_screenshot` | 返回 PNG image content 和观测元数据 |
|
||||
| `crossdesk_action` | 结构化输入,可附带动作之后的截图 |
|
||||
| `crossdesk_set_control` | 在已授权范围内启停 Agent 输入 |
|
||||
| `crossdesk_select_display` | 切换显示器,使旧观测失效 |
|
||||
| `crossdesk_disconnect` | 断开并释放输入状态、子进程和设备锁 |
|
||||
|
||||
动作包括 `move`、`click`、`double_click`、`scroll`、`drag`、`key_press`、`hotkey`、`type_text`。不提供 shell、任意键按下保持、剪贴板或文件操作。按键使用 Windows VK 对应的标准名称:`A`–`Z`、`0`–`9`、`F1`–`F12`、`CTRL`、`ALT`、`SHIFT`、`META`、`ENTER`、`ESC`、方向键等;应用快捷键仍需根据远端系统选择。
|
||||
|
||||
鼠标动作和 `key_press` 支持可选的 `modifiers: ["CTRL", "ALT", "SHIFT", "META"]`(选择需要的键,不能重复),用于组合点击、选择、拖拽和快捷键。修饰键仅在本次动作期间保持,结束、取消或人工暂停时释放。`drag` 可指定 `button: "left" | "right" | "middle"`,默认左键。标点键支持 `SEMICOLON`、`EQUAL`、`COMMA`、`MINUS`、`PERIOD`、`SLASH`、`BACKQUOTE`、`BRACKET_LEFT`、`BACKSLASH`、`BRACKET_RIGHT`、`QUOTE`;这些是 VK 键位名称,实际字符取决于远端键盘布局,输入文字应使用 `type_text`。例如:
|
||||
|
||||
```json
|
||||
{"session_id":"…","operation_id":"select-item-1","observation_id":"…","action":{"type":"click","x":120,"y":80,"modifiers":["SHIFT"]}}
|
||||
```
|
||||
|
||||
截图默认最长边 1280,最大 2048;允许指定源帧像素区域。返回 `source_size`、`source_rect`、`image_size`、`generation`、`geometry_epoch`、`frame_id`、`frame_age_ms` 和 `observation_id`。动作坐标使用返回图像坐标,不使用浏览器 CSS 像素。观测保留最多 32 个,30 秒过期;切换显示器、连接代际或图像几何变化立即失效。截图使用不超过 2 秒的帧,PNG 不超过 2 MiB,必要时继续缩小输出并返回真实尺寸。
|
||||
|
||||
`timeout_ms` 限制等待可用帧的时间,PNG 编码独立执行;`0` 表示只使用当前已就绪的新鲜帧,无帧返回 `FRAME_TIMEOUT`,编码器繁忙返回 `BUSY`。截图按到达顺序处理,并优先于预览。视频超过 2 秒未更新时拒绝新的键鼠输入,恢复新鲜画面后可继续;已提交操作的去重查询仍可读取原结果。
|
||||
|
||||
`operation_id` 在当前原生会话中缓存最近 128 个操作,至少在未被容量淘汰的 5 分钟窗口内支持同参数查询;不同参数复用 ID 返回冲突。`submitted` 仅说明本地发送完成,远端没有执行 ACK。超时、断网、取消或发送失败可能返回 `outcome_unknown`/`side_effect_possible`,禁止自动重放。`observe_after` 默认开启,后续图像只证明取得更新帧,不证明操作执行成功。
|
||||
|
||||
## 实时视频与按需截图
|
||||
|
||||
实时视频提供两条路径,默认 `browser`,两条路径均由原生 CLI 的本机 WebSocket 直接发送给浏览器。Node 只处理会话、授权票据和 MCP 工具,不读取或转发实时视频字节;不再创建视频管道,不经过 MCP stdio 或模型上下文,不逐帧转 PNG、不重新编码视频。
|
||||
|
||||
| `video.decode_path` | 画面路径 |
|
||||
| --- | --- |
|
||||
| `browser` | MiniRTC 输出原始 H.264 Annex B → 浏览器 WebCodecs 解码 → canvas |
|
||||
| `minirtc` | MiniRTC 解码 → 紧凑 NV12 原始帧 → 浏览器 VideoFrame / canvas;浏览器不再解码 H.264 |
|
||||
|
||||
`video.decoder` 默认 `auto`:优先使用可用硬件解码器,不支持时使用软件。`software` 禁用本程序的硬件选择与硬件回退。浏览器路径使用 WebCodecs 的 `prefer-hardware` / `prefer-software`;这些值是浏览器 API 的偏好,浏览器可以忽略,不能据此宣称已确认物理硬件或软件后端。原生路径由 MiniRTC 的解码器工厂选择,本机 macOS 使用 VideoToolbox 或 OpenH264;其他平台使用 MiniRTC 已支持的后端。状态中的 `native_video_decoder` 返回 MiniRTC 报告的后端名称(统计可用前为空),不代表浏览器后端。
|
||||
|
||||
在 `~/.config/crossdesk/mcp/devices.json` 的根对象中加入:
|
||||
|
||||
```json
|
||||
"video": {
|
||||
"decode_path": "minirtc",
|
||||
"decoder": "auto"
|
||||
}
|
||||
```
|
||||
|
||||
也可在 Agent 宿主的 MCP 启动参数中配置,命令行优先于文件:
|
||||
|
||||
```sh
|
||||
crossdesk-mcp serve --stdio --video-path browser --video-decoder auto
|
||||
crossdesk-mcp serve --stdio --video-path minirtc --video-decoder software
|
||||
```
|
||||
|
||||
修改后重启 MCP,再建立连接。选项适用于整个 MCP 进程,两条路径均将解码选项传给原生端;浏览器路径中的原生解码用于截图缓存。截图工具或动作后的截图请求才触发 PNG 编码。这里配置的是控制端解码器,不更改远端编码器。原有配置文件无需新增字段即可继续使用。
|
||||
|
||||
Agent 模式默认请求高帧率档位,分辨率上限 1920 × 1080;实际分辨率和帧率由远端能力、网络和本机解码性能决定。面板显示实际尺寸与呈现 FPS。浏览器路径需要 WebCodecs H.264 解码能力;MiniRTC 路径仅需浏览器支持从 NV12 创建 VideoFrame 并绘制。能力不足时明确报错,不自动退回 PNG 轮询。NV12 不压缩,1080p / 30 FPS 约需 93 MB/s 本机传输,4K / 30 FPS 约需 373 MB/s;它不增加远端到控制端的 H.264 网络码率。移除 Node 转发没有消除 NV12 自身的本机传输与浏览器像素处理开销;这不是 GPU 零拷贝。
|
||||
|
||||
视频 WebSocket 在原生 CLI 的独立 I/O 线程中运行,只监听随机 `127.0.0.1` 端口,与 Node 面板 HTTP 端口不同。浏览器面板通过 cookie/CSRF 授权获取视频票据,Apps 通过 app-only `crossdesk_video_open` 和视图令牌获取。票据 15 秒内一次性有效,绑定视图、会话和代际,通过 WebSocket 第一条消息提交,不写入 URL、日志或模型可见工具结果。WebSocket 只提供画面、收帧 ACK 和关键帧恢复请求,不能执行远端输入操作。最多 8 个活动观看者。H.264 丢帧后等待关键帧恢复;NV12 每帧独立,每个观看者最多一帧未确认,原生发送队列只保留最新画面;H.264 最多 8 帧未确认并受字节阈值限制。10 秒未确认最早一帧则关闭该视频通道,不阻塞其他观看者或 MCP 控制。视图撤销、显示器/连接代际变化、断开和进程退出会关闭对应视频连接;暂停 Agent 控制仍继续播放。页面隐藏或卸载时释放视频订阅,恢复可见时重新订阅。
|
||||
|
||||
浏览器页面和 MCP Apps 资源的 CSP 允许原生服务使用的 `ws://127.0.0.1:*`(仅动态端口),HTTP 控制仍限制到 Node 面板的确切地址。原生服务另外校验确切 Host、Origin 和一次性票据。宿主仍可能限制 loopback、WebSocket、WebCodecs 或 iframe 权限;视频失败时提供重试和浏览器入口,`--panel=apps` 的严格策略仍禁止浏览器回退。浏览器和 Apps 共用同一播放器,具体宿主需要独立验收。
|
||||
|
||||
## 打包
|
||||
|
||||
```sh
|
||||
npm run build
|
||||
npm run pack:npm -- --out-dir /absolute/path/artifacts
|
||||
```
|
||||
|
||||
脚本生成可发布 tarball,包含面板、运行代码、源代码、许可和固定版本的五个平台可选依赖。CI 的 `MCP package` 工作流仅构建并上传 `mcp-package`,不发布 npm。原生包继续由 CLI 工作流构建,`doctor` 必须同时声明自动化协议和 `automation_video_protocols: ["h264-annexb/1", "nv12/1"]` 和 `automation_video_decoders: ["auto", "software"]`,以及 `automation_video_transport: "loopback-websocket/1"`、`automation_terminal_protocol: "ansi/1"`;先发布对应 0.2.0 平台包,再发布 MCP tarball。尚未进行真实跨平台发布/远端验收时,不应把构建成功等同于各宿主和设备上的联调完成。
|
||||
|
||||
从发布 tarball 重建 TypeScript/面板时,将包中的 `build-package.json` 复制为 `package.json`,运行 `npm ci && npm run build`。原生源码和依赖来源随平台包提供,详见 [源码来源](../cli/docs/source-provenance.md)。
|
||||
|
||||
## 本地审阅验证(2026-11-03)
|
||||
|
||||
动作参数校验和输入步骤生成集中在 `apps/cli/automation_protocol.*`,主循环负责调度、取消和权限检查。截图按色度网格对齐后仅转换所需区域,PNG 在容量受限的缓冲区中单次压缩;原生帧仅在没有其他读取者时复用内存。Node 按行累计响应字节,在完整响应到达时解码,避免反复扫描和复制大截图。
|
||||
|
||||
本机 macOS arm64 完成 TypeScript/面板构建、原生 CLI 构建、现有 wire/keyboard-state 测试,以及工程外的 MCP、会话和原生动作回归检查。ASan/UBSan 检查覆盖动作调度和输入清理;140 组随机图像覆盖完整帧、奇偶裁剪边界、缩放及容量回退,优化前后 PNG 字节一致。
|
||||
|
||||
使用 1920 × 1080 合成 NV12、最长边 1280、每项 6 次的 `-O2` 本地基准:完整画面编码约 22.5 → 11.5 ms,321 × 241 裁剪约 2.1 → 0.9 ms。数字仅衡量图像转换与编码,不包含网络、解码、远端执行或宿主渲染;未连接真实远端或完成 Windows/Linux 验收。额外测试代码、脚本、数据和产物均存放于工程外系统临时目录。
|
||||
Generated
+977
@@ -0,0 +1,977 @@
|
||||
{
|
||||
"name": "@chunkdash/crossdesk-mcp",
|
||||
"version": "0.1.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@chunkdash/crossdesk-mcp",
|
||||
"version": "0.1.0",
|
||||
"license": "GPL-3.0-only",
|
||||
"dependencies": {
|
||||
"@modelcontextprotocol/client": "2.4.0",
|
||||
"@modelcontextprotocol/core": "2.4.0",
|
||||
"@modelcontextprotocol/ext-apps": "2.0.4",
|
||||
"@modelcontextprotocol/server": "2.4.0",
|
||||
"@napi-rs/keyring": "2.1.2",
|
||||
"zod": "4.6.7"
|
||||
},
|
||||
"bin": {
|
||||
"crossdesk-mcp": "dist/main.js"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "22.19.15",
|
||||
"esbuild": "0.28.2",
|
||||
"typescript": "5.9.3"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=22"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/aix-ppc64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.28.2.tgz",
|
||||
"integrity": "sha512-XExcO+dvLKvVtNTibSTBej1NCAbaGhWn9Ww1ZPx80qsahhPFe/8jgWP0IchNe0F3HwkU7n8ejhH8bjonqht8mQ==",
|
||||
"cpu": [
|
||||
"ppc64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"aix"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/android-arm": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.28.2.tgz",
|
||||
"integrity": "sha512-kXXoiPVVGQcnIYGOeaovwOURpniDBpSq4A03qkQ+BMQqtGG6HYap3xne9C1O1yo4TR3qxlCX5IqqmX6fFo2Lqg==",
|
||||
"cpu": [
|
||||
"arm"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"android"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/android-arm64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.28.2.tgz",
|
||||
"integrity": "sha512-5YfKeeI8qWfBZIX+u2xZC3Zlb3Os/gLS2sbEKM+I4ZOcsWmHS2WLysCcQZDAFRslDUU5Oiq44gf6PYN1vGwG5A==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"android"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/android-x64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.28.2.tgz",
|
||||
"integrity": "sha512-O387ite7SzUyCcy3JQX4P4bLtEA7bLLkx+esve5JHnyYfNTxcVpXZo9jhdB0lTKN44gztELTdU7nS8Nr16Fs1Q==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"android"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/darwin-arm64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.28.2.tgz",
|
||||
"integrity": "sha512-n4KqkOQrraxHJcgjM1RvwbigfQKIKJVpM7xp+KsxiyUSrRdIXnt73VhrPAx0fV44hgfmIVKjxMN9J1t5jySVkw==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"darwin"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/darwin-x64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.28.2.tgz",
|
||||
"integrity": "sha512-uq6suIWYP37qzGddBKPw5QEQPi6HiLGsO7UmkpfyaYNQ3D+rN6w6WfwH+nuqcGXWvawGwxOEroO4YGnFh95azw==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"darwin"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/freebsd-arm64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.28.2.tgz",
|
||||
"integrity": "sha512-n+I0BTSRIoy+d6RPKnEVwql5UwBJolytvY4mAOIEJorKlqgPII8ix6slVVrfZ5Tnj7glIZvloylbB/EJPMWEXw==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"freebsd"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/freebsd-x64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.28.2.tgz",
|
||||
"integrity": "sha512-78XJTJkvPs0kz2w61301PJjXl4g7q3JqiYMZ/M/yVI73EHBrCRTgkhu9oqG7vPqq+a/yadEW8aD+agKlk5xrmg==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"freebsd"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/linux-arm": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.28.2.tgz",
|
||||
"integrity": "sha512-XlDnu2q5yoqems+xay6wSAcg9DDD7K9RLKZEBOMZm3ckNpJBvOX20tSfby8KfrrhINDyv9V2YVZKY/SpoGJI8w==",
|
||||
"cpu": [
|
||||
"arm"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/linux-arm64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.28.2.tgz",
|
||||
"integrity": "sha512-pW4AC0P3it8c7do9MVM4p51FzHzdM/TZrerurgRcHJ2WTa1VQ1CIq18xncfpBJw4ojkiZZrKW2yIBWBP92j6Ug==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/linux-ia32": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.28.2.tgz",
|
||||
"integrity": "sha512-CYbnj78HsIeA+DhgUKgFCfvNsTHFhMMrinUrMZpDXJXKN8T3XViTZ/+wtHeVxEWY8ewSzTFN+nRmSwO2tZaLUQ==",
|
||||
"cpu": [
|
||||
"ia32"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/linux-loong64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.28.2.tgz",
|
||||
"integrity": "sha512-buwkd8nsph4R+ajRvw0qM5Hja/TXQow3ptzWO2EbG/cqcIkHloRrdlBtQlshyYGTNFvfkfJ5tpPLVkY4DtsPfQ==",
|
||||
"cpu": [
|
||||
"loong64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/linux-mips64el": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.28.2.tgz",
|
||||
"integrity": "sha512-ZVykbDyk7519VwiNb9Lcj9m8XM6v5V9uKPvrEMkkEedVewf+0itkhahp4HDpgERXhwLRpWFypsGbG/J8s0QjJA==",
|
||||
"cpu": [
|
||||
"mips64el"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/linux-ppc64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.28.2.tgz",
|
||||
"integrity": "sha512-CAXl+Dtd9UUuJd8pKKdwh6MLm3MUMiqMPmhZ3tTSXPqfyQ3vDl6R5hZdZ/kYojK4ofXtdfSv1tFq8XzWx3heNQ==",
|
||||
"cpu": [
|
||||
"ppc64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/linux-riscv64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.28.2.tgz",
|
||||
"integrity": "sha512-GeXCej4IQtU1B+QlDV8W/RRvbzI3O/Stss+/bCXv4lZls5WGRtu2a+3JkA3i4qIUlMXpcHebWpF8AkJhATowuA==",
|
||||
"cpu": [
|
||||
"riscv64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/linux-s390x": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.28.2.tgz",
|
||||
"integrity": "sha512-3H1weTYZPxt/WOhByszQZybS9w5lKzUn1FDMsgEChbHWQwHYQQRfBxgCcZvPhjHfKyJjIievvMmEUawJrdY9Dg==",
|
||||
"cpu": [
|
||||
"s390x"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/linux-x64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.28.2.tgz",
|
||||
"integrity": "sha512-4xTZr1FUmSoQW4XIWmit3tzQrUTZM+N3P0XV8xROKYF50XfI7xeO90+1bZvNwxIufQ9hDQVRJH5YhgPVF8A/HQ==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/netbsd-arm64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.28.2.tgz",
|
||||
"integrity": "sha512-sSATRjPeDBg3pdgHoQfoYBob11Kk1FGa9lui5RIHZCoCkJa9QKlvl3/vKz2usCmYYjs7ymJR/2Nnsqe+Hjt5nw==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"netbsd"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/netbsd-x64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.28.2.tgz",
|
||||
"integrity": "sha512-lqnzCV+mM0gIADaKihiCg6ifgfU2L3h5E33rNQBN1Y4MaVGnzryzmvvf7UHxprpQdE8hpqLolJ9Rl+SkIRDpyw==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"netbsd"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/openbsd-arm64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.28.2.tgz",
|
||||
"integrity": "sha512-AL2qJILH7lNjrDmCQDvdxMfAUIv8KMNZOvrwAQ8i8//ntL9FflhOyMJ8OZSMBb8/AWXe3/5v5S20y3zCoZWKoQ==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"openbsd"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/openbsd-x64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.28.2.tgz",
|
||||
"integrity": "sha512-QtiuPytchRyC4rwUKhexJdQKvDuZ6hWloi3igqPQNUJCS1/v9EiO3UTOXR6A3FoMo4fnAKbWJdqaIwhOzh8qEw==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"openbsd"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/openharmony-arm64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.28.2.tgz",
|
||||
"integrity": "sha512-WkhYDmpTjLvGlScA1rwjRUmhl4k8oXR3cIbtqWmELgU/dFeHHlEllxDvdWcNJV9rbzCexB5vz8gtNewWLgCT7Q==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"openharmony"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/sunos-x64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.28.2.tgz",
|
||||
"integrity": "sha512-GPMSkTOtMnv2U2F8gxe4Io6qmVs+YKyp832Etqqxr0hFngmXQ3rzwytelm3GIn7T4VviRUlf3sOgBOiTdvaf7g==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"sunos"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/win32-arm64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.28.2.tgz",
|
||||
"integrity": "sha512-PIhhEkE9uPBleRBrQEJpUn7MBnibZzbGzYWPmY3x+YoVg/95zbjB4CxPPOQ8l5tYYM4mMaCthF8/1DIfBQQyWQ==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"win32"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/win32-ia32": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.28.2.tgz",
|
||||
"integrity": "sha512-YmJbfTlvU7Sdn9BB+4PRES4oB6pxgS37MAONj+hBr/cpXS1aBPKXxNnDbu+QCWPj0o9dgyxeq79g6c5P8KeuYA==",
|
||||
"cpu": [
|
||||
"ia32"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"win32"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/win32-x64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.28.2.tgz",
|
||||
"integrity": "sha512-5ebpxr3nWMzrL/rnUI755Jkuee0bHL/Gq0WTF9lvcpv73wAp5eu8MfBUgWK9bhWvZjj7yX8etf/8tI8Ney695g==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"win32"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@modelcontextprotocol/client": {
|
||||
"version": "2.4.0",
|
||||
"resolved": "https://registry.npmjs.org/@modelcontextprotocol/client/-/client-2.4.0.tgz",
|
||||
"integrity": "sha512-LgrD2OphHuG12vfKFKz6SOnq7bKHQmhGJIAYdD+MZBn2npHEyry6uD8zvktodtl5CQM85MO0anUyD9JwmUmxww==",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@modelcontextprotocol/core": "2.4.0",
|
||||
"cross-spawn": "^7.0.5",
|
||||
"eventsource": "^3.0.2",
|
||||
"eventsource-parser": "^3.0.8",
|
||||
"jose": "^6.1.3",
|
||||
"pkce-challenge": "^5.0.0",
|
||||
"zod": "^4.2.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20"
|
||||
}
|
||||
},
|
||||
"node_modules/@modelcontextprotocol/core": {
|
||||
"version": "2.4.0",
|
||||
"resolved": "https://registry.npmjs.org/@modelcontextprotocol/core/-/core-2.4.0.tgz",
|
||||
"integrity": "sha512-z8QBKqdfXLPzkLQuG4xp8hrVEMm1PZ1IgxMN2sMbJiFiAKOWXnAzogXn2W4aqIBlP1ea6U5yiqVquvUNM1Cj6Q==",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"zod": "^4.2.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20"
|
||||
}
|
||||
},
|
||||
"node_modules/@modelcontextprotocol/ext-apps": {
|
||||
"version": "2.0.4",
|
||||
"resolved": "https://registry.npmjs.org/@modelcontextprotocol/ext-apps/-/ext-apps-2.0.4.tgz",
|
||||
"integrity": "sha512-B+PcbuGiaf0e5I3uy4iO5tS3Abo+5w8JvZFzHMzQdq9yVwP836zJkw2+KCGOQ0r4TM1fuQi4JdnbuWjkS4aBvg==",
|
||||
"license": "MIT",
|
||||
"workspaces": [
|
||||
"examples/*"
|
||||
],
|
||||
"dependencies": {
|
||||
"@standard-schema/spec": "^1.1.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@modelcontextprotocol/client": "^2.0.0",
|
||||
"@modelcontextprotocol/core": "^2.0.0",
|
||||
"@modelcontextprotocol/server": "^2.0.0",
|
||||
"react": "^17.0.0 || ^18.0.0 || ^19.0.0",
|
||||
"react-dom": "^17.0.0 || ^18.0.0 || ^19.0.0",
|
||||
"zod": "^4.2.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@modelcontextprotocol/server": {
|
||||
"optional": true
|
||||
},
|
||||
"react": {
|
||||
"optional": true
|
||||
},
|
||||
"react-dom": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@modelcontextprotocol/server": {
|
||||
"version": "2.4.0",
|
||||
"resolved": "https://registry.npmjs.org/@modelcontextprotocol/server/-/server-2.4.0.tgz",
|
||||
"integrity": "sha512-rgR9yxSaAJ+bcsi/XroT/S2AfPiIWloXAnrdfYSCms9ivVju0ishu/wW0J21Ho6+ZOR1r7RyeRbjiRuF87ix5Q==",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@modelcontextprotocol/core": "2.4.0",
|
||||
"zod": "^4.2.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20"
|
||||
}
|
||||
},
|
||||
"node_modules/@napi-rs/keyring": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@napi-rs/keyring/-/keyring-2.1.2.tgz",
|
||||
"integrity": "sha512-9i1SR6tYeQ3o1beLeVMxhN6Fq513m9TZlTs+46+4JgvSNkaFoW9S4WrpV5MjzE+0CzVoC+dZ4RfZDIgW6+afnQ==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">= 10"
|
||||
},
|
||||
"funding": {
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/Brooooooklyn"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"@napi-rs/keyring-darwin-arm64": "2.1.2",
|
||||
"@napi-rs/keyring-darwin-x64": "2.1.2",
|
||||
"@napi-rs/keyring-freebsd-x64": "2.1.2",
|
||||
"@napi-rs/keyring-linux-arm-gnueabihf": "2.1.2",
|
||||
"@napi-rs/keyring-linux-arm64-gnu": "2.1.2",
|
||||
"@napi-rs/keyring-linux-arm64-musl": "2.1.2",
|
||||
"@napi-rs/keyring-linux-riscv64-gnu": "2.1.2",
|
||||
"@napi-rs/keyring-linux-x64-gnu": "2.1.2",
|
||||
"@napi-rs/keyring-linux-x64-musl": "2.1.2",
|
||||
"@napi-rs/keyring-win32-arm64-msvc": "2.1.2",
|
||||
"@napi-rs/keyring-win32-ia32-msvc": "2.1.2",
|
||||
"@napi-rs/keyring-win32-x64-msvc": "2.1.2"
|
||||
}
|
||||
},
|
||||
"node_modules/@napi-rs/keyring-darwin-arm64": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@napi-rs/keyring-darwin-arm64/-/keyring-darwin-arm64-2.1.2.tgz",
|
||||
"integrity": "sha512-vsInUWUzWS6/WzR/TFNwmdtmwKWjnnR8TOpvZ/pjjxDAkJB/waz37PWzp2b8AFcw1aCcq2aQ7k2vvbLQy8pTrw==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"darwin"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">= 10"
|
||||
}
|
||||
},
|
||||
"node_modules/@napi-rs/keyring-darwin-x64": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@napi-rs/keyring-darwin-x64/-/keyring-darwin-x64-2.1.2.tgz",
|
||||
"integrity": "sha512-wQihYroaQ3r++R6u5c9Gj0TCw1l1F53LCb5aLOXfVvT4DsnDnpe7p7VGYo4/33bLe0iqVevtAj+ET3yKCtOlJQ==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"darwin"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">= 10"
|
||||
}
|
||||
},
|
||||
"node_modules/@napi-rs/keyring-freebsd-x64": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@napi-rs/keyring-freebsd-x64/-/keyring-freebsd-x64-2.1.2.tgz",
|
||||
"integrity": "sha512-Ve5ha7MbJsq+4TYyXYiTy4IeZOnYxkBB05uH2+nIrEKBkHmyfHttF814EfWAfoJa63KqBhYRXdYJo0o1WVL2sg==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"freebsd"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">= 10"
|
||||
}
|
||||
},
|
||||
"node_modules/@napi-rs/keyring-linux-arm-gnueabihf": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@napi-rs/keyring-linux-arm-gnueabihf/-/keyring-linux-arm-gnueabihf-2.1.2.tgz",
|
||||
"integrity": "sha512-zxijTnVnTApEyJMjgnLdVQL1g1DAudku/5brrWNDRWj4J0q0nFhUZEtzUA+nY2FvgDjl4s+2YCZYXSTH4TFMUg==",
|
||||
"cpu": [
|
||||
"arm"
|
||||
],
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">= 10"
|
||||
}
|
||||
},
|
||||
"node_modules/@napi-rs/keyring-linux-arm64-gnu": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@napi-rs/keyring-linux-arm64-gnu/-/keyring-linux-arm64-gnu-2.1.2.tgz",
|
||||
"integrity": "sha512-PM+SAcl/yGzgGbeeCsKrnlG4T5DBIDjmDqtKm5an1bJz0o0gwSzR3BMiXf04JenkcqascZpdcIReoEvLj0cvCw==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"libc": [
|
||||
"glibc"
|
||||
],
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">= 10"
|
||||
}
|
||||
},
|
||||
"node_modules/@napi-rs/keyring-linux-arm64-musl": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@napi-rs/keyring-linux-arm64-musl/-/keyring-linux-arm64-musl-2.1.2.tgz",
|
||||
"integrity": "sha512-+ERS9oyInPCALN957cViBDbYJc724/Q+rZ8ZAz4rOHj4B4v0VR4OAWThdYZgZqD54sZVV0MdyX24qva97hYoAQ==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"libc": [
|
||||
"musl"
|
||||
],
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">= 10"
|
||||
}
|
||||
},
|
||||
"node_modules/@napi-rs/keyring-linux-riscv64-gnu": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@napi-rs/keyring-linux-riscv64-gnu/-/keyring-linux-riscv64-gnu-2.1.2.tgz",
|
||||
"integrity": "sha512-pGADtqeT3U1SaFZGjkVsmZVjeNOQx0fr77S+NWszXjZboyaUOLlGKgqG6ssAWzcwxv8uPhKc9Ij17ujNZhSqLw==",
|
||||
"cpu": [
|
||||
"riscv64"
|
||||
],
|
||||
"libc": [
|
||||
"glibc"
|
||||
],
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">= 10"
|
||||
}
|
||||
},
|
||||
"node_modules/@napi-rs/keyring-linux-x64-gnu": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@napi-rs/keyring-linux-x64-gnu/-/keyring-linux-x64-gnu-2.1.2.tgz",
|
||||
"integrity": "sha512-Fj5TmTnl2oriuCT5DDEYlhgxYes20uGfgOg10LK9PyPVsI8/PQ1m8I4rq/4A5hkrMtY7wvFb70YmX/yPHDYWvQ==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"libc": [
|
||||
"glibc"
|
||||
],
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">= 10"
|
||||
}
|
||||
},
|
||||
"node_modules/@napi-rs/keyring-linux-x64-musl": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@napi-rs/keyring-linux-x64-musl/-/keyring-linux-x64-musl-2.1.2.tgz",
|
||||
"integrity": "sha512-C+KhAeU6x2A43FV3dOmMvUraJb8zsC9VZfsunjZ1cgtuAb208TALaUuAY0jThOphXqhyhBJOs2xTCQhOrRkJIw==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"libc": [
|
||||
"musl"
|
||||
],
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">= 10"
|
||||
}
|
||||
},
|
||||
"node_modules/@napi-rs/keyring-win32-arm64-msvc": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@napi-rs/keyring-win32-arm64-msvc/-/keyring-win32-arm64-msvc-2.1.2.tgz",
|
||||
"integrity": "sha512-kBq80ZZfjXoa5wHG9kgAVhYYjaPOLgHh5ngt7r1ftwFXPXDPXKH7oM2r2j2Q64cs0pVtmodVtn5Q9e6xCwzQUQ==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"win32"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">= 10"
|
||||
}
|
||||
},
|
||||
"node_modules/@napi-rs/keyring-win32-ia32-msvc": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@napi-rs/keyring-win32-ia32-msvc/-/keyring-win32-ia32-msvc-2.1.2.tgz",
|
||||
"integrity": "sha512-bhgKvVbWlZ6iR/tmvb2ht6/o0OzarUX9W7hjg+G+eVdGjR1prwtQ9lYo5ZESdn/+zyF1MIFvHkOCGja9YqDcww==",
|
||||
"cpu": [
|
||||
"ia32"
|
||||
],
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"win32"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">= 10"
|
||||
}
|
||||
},
|
||||
"node_modules/@napi-rs/keyring-win32-x64-msvc": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@napi-rs/keyring-win32-x64-msvc/-/keyring-win32-x64-msvc-2.1.2.tgz",
|
||||
"integrity": "sha512-yQtFdfuwqM0ezKlYO6r2n44in2LGzd4MC679Ddhvojip3t1hn+q/4I+gJU+cIJQVd5FAZSKNrK4tGfew3UTpAQ==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"win32"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">= 10"
|
||||
}
|
||||
},
|
||||
"node_modules/@standard-schema/spec": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/@standard-schema/spec/-/spec-1.1.0.tgz",
|
||||
"integrity": "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/node": {
|
||||
"version": "22.19.15",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.19.15.tgz",
|
||||
"integrity": "sha512-F0R/h2+dsy5wJAUe3tAU6oqa2qbWY5TpNfL/RGmo1y38hiyO1w3x2jPtt76wmuaJI4DQnOBu21cNXQ2STIUUWg==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"undici-types": "~6.21.0"
|
||||
}
|
||||
},
|
||||
"node_modules/cross-spawn": {
|
||||
"version": "7.0.6",
|
||||
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
|
||||
"integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"path-key": "^3.1.0",
|
||||
"shebang-command": "^2.0.0",
|
||||
"which": "^2.0.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 8"
|
||||
}
|
||||
},
|
||||
"node_modules/esbuild": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.28.2.tgz",
|
||||
"integrity": "sha512-HKVLS8dvII+xoKW9kmqxbRKrnWEXfJJr/FZhhJmiqIB0e053QNYFqOBouTMO/k5sID4MvCiUCvv8b9M4h32wIA==",
|
||||
"dev": true,
|
||||
"hasInstallScript": true,
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
"esbuild": "bin/esbuild"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"@esbuild/aix-ppc64": "0.28.2",
|
||||
"@esbuild/android-arm": "0.28.2",
|
||||
"@esbuild/android-arm64": "0.28.2",
|
||||
"@esbuild/android-x64": "0.28.2",
|
||||
"@esbuild/darwin-arm64": "0.28.2",
|
||||
"@esbuild/darwin-x64": "0.28.2",
|
||||
"@esbuild/freebsd-arm64": "0.28.2",
|
||||
"@esbuild/freebsd-x64": "0.28.2",
|
||||
"@esbuild/linux-arm": "0.28.2",
|
||||
"@esbuild/linux-arm64": "0.28.2",
|
||||
"@esbuild/linux-ia32": "0.28.2",
|
||||
"@esbuild/linux-loong64": "0.28.2",
|
||||
"@esbuild/linux-mips64el": "0.28.2",
|
||||
"@esbuild/linux-ppc64": "0.28.2",
|
||||
"@esbuild/linux-riscv64": "0.28.2",
|
||||
"@esbuild/linux-s390x": "0.28.2",
|
||||
"@esbuild/linux-x64": "0.28.2",
|
||||
"@esbuild/netbsd-arm64": "0.28.2",
|
||||
"@esbuild/netbsd-x64": "0.28.2",
|
||||
"@esbuild/openbsd-arm64": "0.28.2",
|
||||
"@esbuild/openbsd-x64": "0.28.2",
|
||||
"@esbuild/openharmony-arm64": "0.28.2",
|
||||
"@esbuild/sunos-x64": "0.28.2",
|
||||
"@esbuild/win32-arm64": "0.28.2",
|
||||
"@esbuild/win32-ia32": "0.28.2",
|
||||
"@esbuild/win32-x64": "0.28.2"
|
||||
}
|
||||
},
|
||||
"node_modules/eventsource": {
|
||||
"version": "3.0.7",
|
||||
"resolved": "https://registry.npmjs.org/eventsource/-/eventsource-3.0.7.tgz",
|
||||
"integrity": "sha512-CRT1WTyuQoD771GW56XEZFQ/ZoSfWid1alKGDYMmkt2yl8UXrVR4pspqWNEcqKvVIzg6PAltWjxcSSPrboA4iA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"eventsource-parser": "^3.0.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/eventsource-parser": {
|
||||
"version": "3.1.1",
|
||||
"resolved": "https://registry.npmjs.org/eventsource-parser/-/eventsource-parser-3.1.1.tgz",
|
||||
"integrity": "sha512-EKN1vKAMcZ8MlYMpaNuxN6R9yakzH6uajHcHVTqWJzvu5pWw9DyhbP35HH8MVBQ+dZjAfDxk+A8NiR9KWaXiyQ==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=18.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/isexe": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
|
||||
"integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==",
|
||||
"license": "ISC"
|
||||
},
|
||||
"node_modules/jose": {
|
||||
"version": "6.2.15",
|
||||
"resolved": "https://registry.npmjs.org/jose/-/jose-6.2.15.tgz",
|
||||
"integrity": "sha512-qdF6ewcZARtef63fJ9AKKZPgxw+t7USH6HLXYcD4CrdmSvDp79rpJSPFEKc90QBeGS63u1KQtN2MZQHC0yDIzA==",
|
||||
"license": "MIT",
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/panva"
|
||||
}
|
||||
},
|
||||
"node_modules/path-key": {
|
||||
"version": "3.1.1",
|
||||
"resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz",
|
||||
"integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/pkce-challenge": {
|
||||
"version": "5.0.1",
|
||||
"resolved": "https://registry.npmjs.org/pkce-challenge/-/pkce-challenge-5.0.1.tgz",
|
||||
"integrity": "sha512-wQ0b/W4Fr01qtpHlqSqspcj3EhBvimsdh0KlHhH8HRZnMsEa0ea2fTULOXOS9ccQr3om+GcGRk4e+isrZWV8qQ==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=16.20.0"
|
||||
}
|
||||
},
|
||||
"node_modules/shebang-command": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz",
|
||||
"integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"shebang-regex": "^3.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/shebang-regex": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz",
|
||||
"integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/typescript": {
|
||||
"version": "5.9.3",
|
||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz",
|
||||
"integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
|
||||
"dev": true,
|
||||
"license": "Apache-2.0",
|
||||
"bin": {
|
||||
"tsc": "bin/tsc",
|
||||
"tsserver": "bin/tsserver"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.17"
|
||||
}
|
||||
},
|
||||
"node_modules/undici-types": {
|
||||
"version": "6.21.0",
|
||||
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz",
|
||||
"integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/which": {
|
||||
"version": "2.0.2",
|
||||
"resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
|
||||
"integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"isexe": "^2.0.0"
|
||||
},
|
||||
"bin": {
|
||||
"node-which": "bin/node-which"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 8"
|
||||
}
|
||||
},
|
||||
"node_modules/zod": {
|
||||
"version": "4.6.7",
|
||||
"resolved": "https://registry.npmjs.org/zod/-/zod-4.6.7.tgz",
|
||||
"integrity": "sha512-g8W2D2kU6a8untc053fX1uVBKzM7yqAxW/3zwk83+6RyF2+SKOuV+c0wBDhUwzV1PaGapmZH+rmcUzPf+3CGwg==",
|
||||
"license": "MIT",
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/colinhacks"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"name": "@chunkdash/crossdesk-mcp",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"description": "Local CrossDesk MCP tools and remote desktop panels",
|
||||
"license": "GPL-3.0-only",
|
||||
"author": "DI JUNKUN",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/kunkundi/crossdesk.git",
|
||||
"directory": "apps/mcp"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=22"
|
||||
},
|
||||
"bin": {
|
||||
"crossdesk-mcp": "dist/main.js"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc && tsc -p tsconfig.ui.json && node scripts/build-ui.mjs",
|
||||
"check": "tsc --noEmit && tsc -p tsconfig.ui.json",
|
||||
"pack:npm": "node scripts/pack.mjs"
|
||||
},
|
||||
"dependencies": {
|
||||
"@modelcontextprotocol/client": "2.4.0",
|
||||
"@modelcontextprotocol/core": "2.4.0",
|
||||
"@modelcontextprotocol/ext-apps": "2.0.4",
|
||||
"@modelcontextprotocol/server": "2.4.0",
|
||||
"@napi-rs/keyring": "2.1.2",
|
||||
"zod": "4.6.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "22.19.15",
|
||||
"esbuild": "0.28.2",
|
||||
"typescript": "5.9.3"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { build } from "esbuild";
|
||||
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { dirname, resolve } from "node:path";
|
||||
|
||||
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
// Unmodified apps/desktop/resources/linux/crossdesk_128x128.png.
|
||||
// Keep a local copy so the npm source package rebuilds without desktop assets.
|
||||
const logo = "data:image/png;base64," +
|
||||
(await readFile(resolve(root, "ui/assets/crossdesk.png"))).toString("base64");
|
||||
await mkdir(resolve(root, "dist"), { recursive: true });
|
||||
for (const mode of ["apps", "browser"]) {
|
||||
const result = await build({
|
||||
entryPoints: [resolve(root, "ui/viewer.ts")],
|
||||
bundle: true,
|
||||
minify: true,
|
||||
platform: "browser",
|
||||
target: "es2022",
|
||||
outfile: "viewer.js",
|
||||
write: false,
|
||||
define: {
|
||||
CROSSDESK_APPS: String(mode === "apps"),
|
||||
CROSSDESK_LOGO: JSON.stringify(logo),
|
||||
},
|
||||
});
|
||||
const script = result.outputFiles
|
||||
.find((f) => f.path.endsWith(".js"))
|
||||
.text.replaceAll("</script", "<\\/script");
|
||||
const css = result.outputFiles.find((f) => f.path.endsWith(".css")).text;
|
||||
const nonce = mode === "browser" ? ' nonce="__CROSSDESK_NONCE__"' : "";
|
||||
await writeFile(
|
||||
resolve(root, `dist/viewer-${mode}.html`),
|
||||
`<!doctype html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="theme-color" content="#ffffff"><link rel="icon" type="image/png" href="${logo}"><title>CrossDesk MCP</title><style${nonce}>${css}</style></head><body><div id="app"></div><script${nonce}>${script}</script></body></html>`,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
import {
|
||||
mkdtemp,
|
||||
readFile,
|
||||
writeFile,
|
||||
mkdir,
|
||||
cp,
|
||||
chmod,
|
||||
rm,
|
||||
access,
|
||||
} from "node:fs/promises";
|
||||
import { dirname, resolve, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { tmpdir } from "node:os";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { parseArgs } from "node:util";
|
||||
|
||||
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
const repository = resolve(root, "../..");
|
||||
const { values } = parseArgs({ options: { "out-dir": { type: "string" } } });
|
||||
const output = resolve(values["out-dir"] ?? join(repository, "build/npm-mcp"));
|
||||
const project = JSON.parse(await readFile(join(root, "package.json"), "utf8"));
|
||||
const cli = JSON.parse(
|
||||
await readFile(join(root, "../cli/package.json"), "utf8"),
|
||||
);
|
||||
const platforms = JSON.parse(
|
||||
await readFile(join(root, "../cli/npm/platforms.json"), "utf8"),
|
||||
);
|
||||
const native = await import(new URL("../dist/native.js", import.meta.url));
|
||||
if (native.NATIVE_VERSION !== cli.version)
|
||||
throw new Error("MCP and CLI native versions must match.");
|
||||
for (const file of ["main.js", "viewer-browser.html", "viewer-apps.html"])
|
||||
await access(join(root, "dist", file));
|
||||
const staging = await mkdtemp(join(tmpdir(), "crossdesk-mcp-pack-"));
|
||||
try {
|
||||
for (const entry of [
|
||||
"dist",
|
||||
"src",
|
||||
"ui",
|
||||
"scripts",
|
||||
"tsconfig.json",
|
||||
"tsconfig.ui.json",
|
||||
"package-lock.json",
|
||||
"README.md",
|
||||
])
|
||||
await cp(join(root, entry), join(staging, entry), { recursive: true });
|
||||
await cp(join(repository, "LICENSE"), join(staging, "LICENSE"));
|
||||
await writeFile(
|
||||
join(staging, "build-package.json"),
|
||||
JSON.stringify(project, null, 2) + "\n",
|
||||
);
|
||||
const manifest = {
|
||||
...project,
|
||||
private: false,
|
||||
files: [
|
||||
"dist",
|
||||
"src",
|
||||
"ui",
|
||||
"scripts/build-ui.mjs",
|
||||
"tsconfig.json",
|
||||
"tsconfig.ui.json",
|
||||
"build-package.json",
|
||||
"package-lock.json",
|
||||
"README.md",
|
||||
"LICENSE",
|
||||
],
|
||||
optionalDependencies: Object.fromEntries(
|
||||
Object.values(platforms).map((p) => [p.packageName, cli.version]),
|
||||
),
|
||||
publishConfig: { access: "public" },
|
||||
};
|
||||
delete manifest.scripts;
|
||||
delete manifest.devDependencies;
|
||||
await writeFile(
|
||||
join(staging, "package.json"),
|
||||
JSON.stringify(manifest, null, 2) + "\n",
|
||||
);
|
||||
await chmod(join(staging, "dist/main.js"), 0o755);
|
||||
await mkdir(output, { recursive: true });
|
||||
const filename = `${project.name.replace(/^@/, "").replace("/", "-")}-${project.version}.tgz`;
|
||||
try {
|
||||
await access(join(output, filename));
|
||||
throw new Error(`Output already exists: ${filename}`);
|
||||
} catch (error) {
|
||||
if (error.code !== "ENOENT") throw error;
|
||||
}
|
||||
const args = [
|
||||
"pack",
|
||||
"--json",
|
||||
"--ignore-scripts",
|
||||
"--pack-destination",
|
||||
output,
|
||||
];
|
||||
if (process.platform === "win32" && !process.env.npm_execpath)
|
||||
throw new Error("On Windows use npm run pack:npm.");
|
||||
const result = execFileSync(
|
||||
process.env.npm_execpath ? process.execPath : "npm",
|
||||
process.env.npm_execpath ? [process.env.npm_execpath, ...args] : args,
|
||||
{ cwd: staging, encoding: "utf8" },
|
||||
);
|
||||
const parsed = JSON.parse(result);
|
||||
const packages = Array.isArray(parsed) ? parsed : Object.values(parsed);
|
||||
if (packages.length !== 1 || packages[0].filename !== filename)
|
||||
throw new Error("Unexpected package archive.");
|
||||
process.stdout.write(join(output, filename) + "\n");
|
||||
} finally {
|
||||
await rm(staging, { recursive: true, force: true });
|
||||
}
|
||||
@@ -0,0 +1,403 @@
|
||||
import {
|
||||
createServer,
|
||||
type IncomingMessage,
|
||||
type ServerResponse,
|
||||
type Server,
|
||||
} from "node:http";
|
||||
import {
|
||||
createServer as createIPC,
|
||||
createConnection,
|
||||
type Server as IPCServer,
|
||||
} from "node:net";
|
||||
import { spawn } from "node:child_process";
|
||||
import { readFile, writeFile, unlink, chmod, readdir } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { Failure, token, equal, failure, type Data } from "./common.js";
|
||||
import { Sessions, type View } from "./sessions.js";
|
||||
import { uiAction } from "./ui-actions.js";
|
||||
import { displayEnvironment } from "./environment.js";
|
||||
|
||||
const uiFile = fileURLToPath(new URL("./viewer-browser.html", import.meta.url));
|
||||
interface BrowserSession {
|
||||
csrf: string;
|
||||
views: Set<string>;
|
||||
expires: number;
|
||||
}
|
||||
export async function openURL(url: string): Promise<boolean> {
|
||||
if (!displayEnvironment().gui) return false;
|
||||
const command =
|
||||
process.platform === "darwin"
|
||||
? "open"
|
||||
: process.platform === "win32"
|
||||
? "rundll32.exe"
|
||||
: "xdg-open";
|
||||
const args =
|
||||
process.platform === "win32" ? ["url.dll,FileProtocolHandler", url] : [url];
|
||||
return new Promise((resolve) => {
|
||||
const child = spawn(command, args, {
|
||||
stdio: "ignore",
|
||||
shell: false,
|
||||
windowsHide: true,
|
||||
});
|
||||
const timeout = setTimeout(() => {
|
||||
child.unref();
|
||||
resolve(true);
|
||||
}, 1500);
|
||||
child.once("error", () => {
|
||||
clearTimeout(timeout);
|
||||
resolve(false);
|
||||
});
|
||||
child.once("exit", (code) => {
|
||||
clearTimeout(timeout);
|
||||
resolve(code === 0);
|
||||
});
|
||||
});
|
||||
}
|
||||
export class BrowserPanel {
|
||||
private closed = false;
|
||||
private server?: Server;
|
||||
private starting?: Promise<void>;
|
||||
private ipc?: IPCServer;
|
||||
private ipcPath?: string;
|
||||
private descriptor?: string;
|
||||
private origin = "";
|
||||
private cookie = "crossdesk_" + token();
|
||||
private grants = new Map<string, { view: string; expires: number }>();
|
||||
private clients = new Map<string, BrowserSession>();
|
||||
private html = "";
|
||||
constructor(private sessions: Sessions) {}
|
||||
async connectDomains() {
|
||||
await this.start();
|
||||
// Native processes own ephemeral video ports, including after reconnect.
|
||||
// Restrict the wildcard to ports on the literal loopback address.
|
||||
return [this.origin, "ws://127.0.0.1:*"];
|
||||
}
|
||||
async video(view: View) {
|
||||
await this.start();
|
||||
if (this.closed) throw new Failure("PANEL_CLOSED");
|
||||
return this.sessions.openVideo(view, this.origin);
|
||||
}
|
||||
async open(view: View): Promise<Data> {
|
||||
if (this.closed) throw new Failure("PANEL_CLOSED");
|
||||
await this.start();
|
||||
if (this.closed) throw new Failure("PANEL_CLOSED");
|
||||
const opened = await openURL(this.bootstrapURL(view));
|
||||
return {
|
||||
...this.sessions.describe(view),
|
||||
browser_opened: opened,
|
||||
browser_url: this.origin,
|
||||
pairing_command: {
|
||||
command: "crossdesk-mcp",
|
||||
args: [
|
||||
"setup",
|
||||
"--open-panel",
|
||||
"--view",
|
||||
view.id,
|
||||
"--config-dir",
|
||||
this.sessions.store.directory,
|
||||
],
|
||||
},
|
||||
};
|
||||
}
|
||||
private bootstrapURL(view: View): string {
|
||||
if (this.closed) throw new Failure("PANEL_CLOSED");
|
||||
for (const [key, value] of this.grants)
|
||||
if (value.expires < Date.now()) this.grants.delete(key);
|
||||
if (this.grants.size >= 32) throw new Failure("VIEW_LIMIT");
|
||||
const grant = token();
|
||||
this.grants.set(grant, { view: view.id, expires: Date.now() + 60000 });
|
||||
return `${this.origin}/#bootstrap=${grant}`;
|
||||
}
|
||||
private async start() {
|
||||
this.starting ??= (async () => {
|
||||
this.html = await readFile(uiFile, "utf8");
|
||||
if (this.closed) throw new Failure("PANEL_CLOSED");
|
||||
this.server = createServer((req, res) => {
|
||||
void this.handle(req, res).catch(() => {
|
||||
if (!res.headersSent)
|
||||
this.respond(res, 500, { error: { code: "INTERNAL_ERROR" } });
|
||||
else res.end();
|
||||
});
|
||||
});
|
||||
// No upgrade handler or video bytes in the Node HTTP server.
|
||||
this.server.requestTimeout = 45000;
|
||||
this.server.headersTimeout = 10000;
|
||||
this.server.maxHeadersCount = 32;
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
this.server!.once("error", reject);
|
||||
this.server!.listen(0, "127.0.0.1", resolve);
|
||||
});
|
||||
this.origin = `http://127.0.0.1:${(this.server.address() as import("node:net").AddressInfo).port}`;
|
||||
if (this.closed) throw new Failure("PANEL_CLOSED");
|
||||
await this.startIPC();
|
||||
if (this.closed) throw new Failure("PANEL_CLOSED");
|
||||
})().catch(async (error) => {
|
||||
await this.close(false);
|
||||
this.starting = undefined;
|
||||
throw error;
|
||||
});
|
||||
return this.starting;
|
||||
}
|
||||
private respond(res: ServerResponse, code: number, value: Data) {
|
||||
res.writeHead(code, { "Content-Type": "application/json; charset=utf-8" });
|
||||
res.end(JSON.stringify(value));
|
||||
}
|
||||
private client(req: IncomingMessage): BrowserSession | undefined {
|
||||
const value = req.headers.cookie
|
||||
?.split(";")
|
||||
.map((s) => s.trim())
|
||||
.find((s) => s.startsWith(this.cookie + "="))
|
||||
?.slice(this.cookie.length + 1);
|
||||
const client = value ? this.clients.get(value) : undefined;
|
||||
return client && client.expires > Date.now() ? client : undefined;
|
||||
}
|
||||
private async body(req: IncomingMessage): Promise<Data> {
|
||||
if (req.headers["content-type"] !== "application/json")
|
||||
throw new Failure("CONTENT_TYPE_REQUIRED");
|
||||
let bytes = 0;
|
||||
const chunks: Buffer[] = [];
|
||||
for await (const chunk of req) {
|
||||
bytes += chunk.length;
|
||||
if (bytes > 65536) throw new Failure("MESSAGE_TOO_LARGE");
|
||||
chunks.push(chunk);
|
||||
}
|
||||
try {
|
||||
const value = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
||||
if (!value || Array.isArray(value) || typeof value !== "object")
|
||||
throw new Error();
|
||||
return value;
|
||||
} catch {
|
||||
throw new Failure("INVALID_ARGUMENT");
|
||||
}
|
||||
}
|
||||
private async handle(req: IncomingMessage, res: ServerResponse) {
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
res.setHeader("Referrer-Policy", "no-referrer");
|
||||
res.setHeader("X-Content-Type-Options", "nosniff");
|
||||
res.setHeader("X-Frame-Options", "DENY");
|
||||
const authority = this.origin.slice("http://".length);
|
||||
if (
|
||||
req.headers.host !== authority ||
|
||||
(req.headers.origin && req.headers.origin !== this.origin)
|
||||
)
|
||||
return this.respond(res, 403, { error: { code: "ORIGIN_DENIED" } });
|
||||
if (req.url === "/" && req.method === "GET") {
|
||||
const nonce = token();
|
||||
res.setHeader(
|
||||
"Content-Security-Policy",
|
||||
`default-src 'none'; script-src 'nonce-${nonce}'; style-src 'nonce-${nonce}'; img-src data:; connect-src 'self' ws://127.0.0.1:*; base-uri 'none'; form-action 'none'; frame-ancestors 'none'`,
|
||||
);
|
||||
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
|
||||
res.end(this.html.replaceAll("__CROSSDESK_NONCE__", nonce));
|
||||
return;
|
||||
}
|
||||
if (req.url === "/api/context" && req.method === "GET") {
|
||||
const client = this.client(req);
|
||||
if (!client)
|
||||
return this.respond(res, 401, { error: { code: "PAIRING_REQUIRED" } });
|
||||
for (const id of client.views)
|
||||
if (!this.sessions.views.has(id)) client.views.delete(id);
|
||||
if (!client.views.size)
|
||||
return this.respond(res, 401, { error: { code: "PAIRING_REQUIRED" } });
|
||||
return this.respond(res, 200, {
|
||||
csrf: client.csrf,
|
||||
views: [...client.views].map((v) =>
|
||||
this.sessions.describe(this.sessions.view(v)),
|
||||
),
|
||||
});
|
||||
}
|
||||
if (req.method !== "POST" || req.headers.origin !== this.origin)
|
||||
return this.respond(res, 403, { error: { code: "ORIGIN_DENIED" } });
|
||||
try {
|
||||
const body = await this.body(req);
|
||||
if (req.url === "/api/bootstrap") {
|
||||
const grant =
|
||||
typeof body.token === "string"
|
||||
? this.grants.get(body.token)
|
||||
: undefined;
|
||||
if (!grant || grant.expires < Date.now())
|
||||
throw new Failure("PAIRING_REQUIRED");
|
||||
this.grants.delete(body.token);
|
||||
for (const [id, client] of this.clients)
|
||||
if (client.expires < Date.now()) this.clients.delete(id);
|
||||
const existing = this.client(req);
|
||||
if (!existing && this.clients.size >= 32)
|
||||
throw new Failure("VIEW_LIMIT");
|
||||
const session = token(),
|
||||
client = existing ?? {
|
||||
csrf: token(),
|
||||
views: new Set<string>(),
|
||||
expires: 0,
|
||||
};
|
||||
client.views.add(grant.view);
|
||||
client.expires = Date.now() + 12 * 60 * 60 * 1000;
|
||||
for (const [key, previous] of this.clients)
|
||||
if (previous === client) this.clients.delete(key);
|
||||
this.clients.set(session, client);
|
||||
res.setHeader(
|
||||
"Set-Cookie",
|
||||
`${this.cookie}=${session}; HttpOnly; SameSite=Strict; Path=/api`,
|
||||
);
|
||||
return this.respond(res, 200, {
|
||||
csrf: client.csrf,
|
||||
...this.sessions.describe(this.sessions.view(grant.view)),
|
||||
});
|
||||
}
|
||||
const client = this.client(req);
|
||||
if (
|
||||
!client ||
|
||||
typeof req.headers["x-crossdesk-csrf"] !== "string" ||
|
||||
!equal(client.csrf, req.headers["x-crossdesk-csrf"])
|
||||
)
|
||||
throw new Failure("PANEL_UNAUTHORIZED");
|
||||
if (typeof body.view_id !== "string" || !client.views.has(body.view_id))
|
||||
throw new Failure("VIEW_UNAUTHORIZED");
|
||||
const name = req.url?.match(
|
||||
/^\/api\/(ready|devices|status|connect|video_open|control)$/,
|
||||
)?.[1];
|
||||
if (!name) throw new Failure("UNKNOWN_ACTION");
|
||||
const { view_id, ...args } = body;
|
||||
const controller = new AbortController();
|
||||
res.once("close", () => {
|
||||
if (!res.writableEnded) controller.abort();
|
||||
});
|
||||
const result = name === "video_open" ? await this.video(this.sessions.view(view_id)) : await uiAction(
|
||||
this.sessions,
|
||||
this.sessions.view(view_id),
|
||||
name,
|
||||
args,
|
||||
controller.signal,
|
||||
);
|
||||
this.respond(res, 200, result);
|
||||
} catch (error) {
|
||||
this.respond(res, 400, { error: failure(error) });
|
||||
}
|
||||
}
|
||||
private async startIPC() {
|
||||
const secret = token();
|
||||
const suffix = token().slice(0, 20);
|
||||
this.ipcPath =
|
||||
process.platform === "win32"
|
||||
? `\\\\.\\pipe\\crossdesk-mcp-${suffix}`
|
||||
: join(tmpdir(), `cd-mcp-${suffix}.sock`);
|
||||
this.ipc = createIPC((socket) => {
|
||||
socket.setTimeout(5000, () => socket.destroy());
|
||||
let data = "",
|
||||
consumed = false;
|
||||
socket.on("error", () => {});
|
||||
socket.on("data", (part) => {
|
||||
data += part.toString("utf8");
|
||||
if (data.length > 2048) {
|
||||
socket.destroy();
|
||||
return;
|
||||
}
|
||||
if (consumed || !data.includes("\n")) return;
|
||||
consumed = true;
|
||||
void (async () => {
|
||||
try {
|
||||
const p = JSON.parse(data);
|
||||
if (
|
||||
typeof p.secret !== "string" ||
|
||||
!equal(p.secret, secret) ||
|
||||
typeof p.view_id !== "string"
|
||||
)
|
||||
throw new Failure("PAIRING_DENIED");
|
||||
const v = this.sessions.view(p.view_id);
|
||||
if (
|
||||
v.panel !== "browser" ||
|
||||
!["auto", "browser"].includes(this.sessions.policy)
|
||||
)
|
||||
throw new Failure("PANEL_POLICY_DENIED");
|
||||
// Only the authenticated local setup process receives this URL.
|
||||
// It uses its own desktop environment to open the browser.
|
||||
socket.end(
|
||||
JSON.stringify({ ok: true, url: this.bootstrapURL(v) }) + "\n",
|
||||
);
|
||||
} catch {
|
||||
socket.end('{"ok":false}\n');
|
||||
}
|
||||
})();
|
||||
});
|
||||
});
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
this.ipc!.once("error", reject);
|
||||
this.ipc!.listen(this.ipcPath!, resolve);
|
||||
});
|
||||
if (process.platform !== "win32") await chmod(this.ipcPath, 0o600);
|
||||
this.descriptor = join(
|
||||
this.sessions.store.directory,
|
||||
`run-${process.pid}-${suffix}.json`,
|
||||
);
|
||||
await writeFile(
|
||||
this.descriptor,
|
||||
JSON.stringify({ pid: process.pid, endpoint: this.ipcPath, secret }),
|
||||
{ mode: 0o600, flag: "wx" },
|
||||
);
|
||||
}
|
||||
async close(final = true) {
|
||||
if (final) this.closed = true;
|
||||
await this.sessions.revokeVideo();
|
||||
this.grants.clear();
|
||||
this.clients.clear();
|
||||
this.server?.closeAllConnections();
|
||||
if (this.server?.listening) this.server.close();
|
||||
if (this.ipc?.listening) this.ipc.close();
|
||||
if (this.descriptor) await unlink(this.descriptor).catch(() => {});
|
||||
if (this.ipcPath && process.platform !== "win32")
|
||||
await unlink(this.ipcPath).catch(() => {});
|
||||
}
|
||||
}
|
||||
export async function pairBrowser(
|
||||
directory: string,
|
||||
view: string,
|
||||
): Promise<string | undefined> {
|
||||
for (const file of await readdir(directory)) {
|
||||
if (!/^run-\d+-[a-zA-Z0-9_-]+\.json$/.test(file)) continue;
|
||||
try {
|
||||
const descriptor = JSON.parse(
|
||||
await readFile(join(directory, file), "utf8"),
|
||||
);
|
||||
const url = await new Promise<string | undefined>((resolve) => {
|
||||
const socket = createConnection(descriptor.endpoint);
|
||||
let data = "";
|
||||
socket.setTimeout(5000, () => {
|
||||
socket.destroy();
|
||||
resolve(undefined);
|
||||
});
|
||||
socket.once("error", () => resolve(undefined));
|
||||
socket.once("close", () => resolve(undefined));
|
||||
socket.once("connect", () =>
|
||||
socket.write(
|
||||
JSON.stringify({ secret: descriptor.secret, view_id: view }) + "\n",
|
||||
),
|
||||
);
|
||||
socket.on("data", (part) => {
|
||||
data += part;
|
||||
if (data.length > 2048) socket.destroy();
|
||||
});
|
||||
socket.once("end", () => {
|
||||
try {
|
||||
const result = JSON.parse(data);
|
||||
resolve(
|
||||
result.ok &&
|
||||
typeof result.url === "string" &&
|
||||
/^http:\/\/127\.0\.0\.1:\d+\/#bootstrap=[a-zA-Z0-9_-]+$/.test(
|
||||
result.url,
|
||||
)
|
||||
? result.url
|
||||
: undefined,
|
||||
);
|
||||
} catch {
|
||||
resolve(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
if (url) return url;
|
||||
} catch {
|
||||
/* Another MCP instance may have exited. */
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
import { randomBytes, timingSafeEqual } from "node:crypto";
|
||||
import { z } from "zod";
|
||||
|
||||
export type Data = Record<string, any>;
|
||||
export const token = () => randomBytes(24).toString("base64url");
|
||||
export const equal = (a: string, b: string) => {
|
||||
const left = Buffer.from(a),
|
||||
right = Buffer.from(b);
|
||||
return left.length === right.length && timingSafeEqual(left, right);
|
||||
};
|
||||
export class Failure extends Error {
|
||||
constructor(
|
||||
public code: string,
|
||||
public sideEffect = false,
|
||||
public retryable = false,
|
||||
) {
|
||||
super(code);
|
||||
}
|
||||
}
|
||||
export function failure(error: unknown): Data {
|
||||
const e = error instanceof Failure ? error : new Failure("INTERNAL_ERROR");
|
||||
return {
|
||||
code: e.code,
|
||||
retryable: e.retryable,
|
||||
side_effect_possible: e.sideEffect,
|
||||
};
|
||||
}
|
||||
export function parse<T extends z.ZodType>(
|
||||
schema: T,
|
||||
value: unknown,
|
||||
): z.output<T> {
|
||||
const result = schema.safeParse(value);
|
||||
if (!result.success) throw new Failure("INVALID_ARGUMENT");
|
||||
return result.data;
|
||||
}
|
||||
export const id = z
|
||||
.string()
|
||||
.min(1)
|
||||
.max(128)
|
||||
.regex(/^[a-zA-Z0-9_-]+$/);
|
||||
export const alias = z
|
||||
.string()
|
||||
.min(1)
|
||||
.max(80)
|
||||
.regex(/^[\p{L}\p{N} _.-]+$/u);
|
||||
export const mode = z.enum(["observe", "control"]);
|
||||
export const panel = z.enum(["auto", "apps", "browser", "terminal", "off"]);
|
||||
export type Panel = z.infer<typeof panel>;
|
||||
export const videoOptions = z.strictObject({
|
||||
decode_path: z.enum(["browser", "minirtc"]).default("browser"),
|
||||
decoder: z.enum(["auto", "software"]).default("auto"),
|
||||
});
|
||||
export type VideoOptions = z.infer<typeof videoOptions>;
|
||||
export const host = z
|
||||
.string()
|
||||
.min(1)
|
||||
.max(253)
|
||||
.regex(/^[a-zA-Z0-9.:-]+$/);
|
||||
export const point = z.strictObject({
|
||||
x: z.number().finite().min(0).max(8192),
|
||||
y: z.number().finite().min(0).max(8192),
|
||||
});
|
||||
const button = z.enum(["left", "right", "middle"]).optional();
|
||||
const unique = (keys: string[]) => new Set(keys).size === keys.length;
|
||||
const modifiers = z.array(z.enum(["CTRL", "ALT", "SHIFT", "META"])).max(4).refine(unique)
|
||||
.describe("Modifiers held during this action and released afterwards, including on cancellation.").optional();
|
||||
const key = z.enum([
|
||||
..."ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split(""),
|
||||
"BACKSPACE", "TAB", "ENTER", "SHIFT", "CTRL", "ALT", "PAUSE", "CAPS_LOCK",
|
||||
"ESC", "SPACE", "PAGE_UP", "PAGE_DOWN", "END", "HOME", "LEFT", "UP", "RIGHT", "DOWN",
|
||||
"INSERT", "DELETE", "META", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12",
|
||||
"SEMICOLON", "EQUAL", "COMMA", "MINUS", "PERIOD", "SLASH", "BACKQUOTE", "BRACKET_LEFT", "BACKSLASH", "BRACKET_RIGHT", "QUOTE",
|
||||
]);
|
||||
export const action = z.discriminatedUnion("type", [
|
||||
point.extend({ type: z.literal("move"), modifiers }),
|
||||
point.extend({ type: z.literal("click"), button, modifiers }),
|
||||
point.extend({ type: z.literal("double_click"), button, modifiers }),
|
||||
point.extend({
|
||||
type: z.literal("scroll"),
|
||||
direction: z.enum(["up", "down", "left", "right"]),
|
||||
ticks: z.int().min(1).max(10).optional(),
|
||||
modifiers,
|
||||
}),
|
||||
z.strictObject({
|
||||
type: z.literal("drag"),
|
||||
path: z.array(point).min(2).max(128),
|
||||
duration_ms: z.int().min(100).max(2000).optional(),
|
||||
button,
|
||||
modifiers,
|
||||
}),
|
||||
z.strictObject({ type: z.literal("key_press"), key, modifiers })
|
||||
.refine((p) => !p.modifiers?.some((modifier) => modifier === p.key)),
|
||||
z.strictObject({
|
||||
type: z.literal("hotkey"),
|
||||
keys: z.array(key).min(2).max(6).refine(unique),
|
||||
}),
|
||||
z.strictObject({
|
||||
type: z.literal("type_text"),
|
||||
text: z
|
||||
.string()
|
||||
.min(1)
|
||||
.max(16384)
|
||||
.refine((s) => Buffer.byteLength(s) <= 16384 && !s.includes("\0")),
|
||||
}),
|
||||
]);
|
||||
export const screenshot = z.strictObject({
|
||||
session_id: id,
|
||||
region: z
|
||||
.strictObject({
|
||||
x: z.int().min(0),
|
||||
y: z.int().min(0),
|
||||
width: z.int().min(1).max(8192),
|
||||
height: z.int().min(1).max(8192),
|
||||
})
|
||||
.optional(),
|
||||
max_edge: z.int().min(64).max(2048).optional(),
|
||||
after_frame_id: z.int().min(0).optional(),
|
||||
generation: z.int().min(0).optional(),
|
||||
timeout_ms: z.int().min(0).max(5000).optional(),
|
||||
});
|
||||
export const uiConnect = z.strictObject({
|
||||
device_alias: alias.optional(),
|
||||
device_id: z
|
||||
.string()
|
||||
.regex(/^\d{9}$/)
|
||||
.optional(),
|
||||
label: alias.optional(),
|
||||
password: z
|
||||
.string()
|
||||
.max(256)
|
||||
.refine((s) => Buffer.byteLength(s) <= 256 && !/[\0\r\n]/.test(s))
|
||||
.optional(),
|
||||
mode: mode.default("observe"),
|
||||
display_index: z.int().min(0).max(63).default(0),
|
||||
server: host.optional(),
|
||||
port: z.int().min(1).max(65535).optional(),
|
||||
save_device: z.boolean().default(false),
|
||||
remember_password: z.boolean().default(false),
|
||||
persistent_authorization: z.boolean().default(false),
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import { statSync } from "node:fs";
|
||||
|
||||
// MCP stdio is normally piped even when its host is an interactive terminal.
|
||||
// Detect the desktop independently, without opening or taking over /dev/tty.
|
||||
export function displayEnvironment(
|
||||
platform = process.platform,
|
||||
env = process.env,
|
||||
tty = !!(process.stdin.isTTY || process.stdout.isTTY || process.stderr.isTTY),
|
||||
): { gui: boolean; terminal: boolean } {
|
||||
const ssh = !!(env.SSH_CONNECTION || env.SSH_TTY || env.SSH_CLIENT);
|
||||
let gui = false;
|
||||
if (platform === "linux") gui = !!(env.DISPLAY || env.WAYLAND_DISPLAY);
|
||||
else if (platform === "darwin" && !ssh) {
|
||||
try {
|
||||
const uid = statSync("/dev/console").uid;
|
||||
gui = uid !== 0 && uid === process.getuid?.();
|
||||
} catch { /* No logged-in desktop session. */ }
|
||||
} else if (platform === "win32") {
|
||||
gui = !ssh && env.SESSIONNAME?.toLowerCase() !== "services";
|
||||
}
|
||||
return {
|
||||
gui,
|
||||
terminal: tty || !!env.SSH_TTY || (!!env.TERM && env.TERM !== "dumb"),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
#!/usr/bin/env node
|
||||
import { parseArgs } from "node:util";
|
||||
import { createInterface } from "node:readline/promises";
|
||||
import { Writable } from "node:stream";
|
||||
import {
|
||||
serveStdio,
|
||||
StdioServerTransport,
|
||||
} from "@modelcontextprotocol/server/stdio";
|
||||
import { DeviceStore, type Device } from "./store.js";
|
||||
import { Sessions } from "./sessions.js";
|
||||
import { BrowserPanel, pairBrowser, openURL } from "./browser.js";
|
||||
import { createServer } from "./server.js";
|
||||
import { TerminalPanel } from "./terminal.js";
|
||||
import { terminalView } from "./terminal-view.js";
|
||||
import {
|
||||
panel,
|
||||
parse,
|
||||
Failure,
|
||||
failure,
|
||||
uiConnect,
|
||||
videoOptions,
|
||||
type Panel,
|
||||
} from "./common.js";
|
||||
|
||||
async function setup(store: DeviceStore, view?: string) {
|
||||
if (!process.stdin.isTTY || !process.stdout.isTTY)
|
||||
throw new Failure("SETUP_REQUIRES_TERMINAL");
|
||||
if (view) {
|
||||
const url = await pairBrowser(store.directory, view);
|
||||
if (!url) throw new Failure("PANEL_NOT_FOUND");
|
||||
if (await openURL(url)) process.stdout.write("浏览器配对入口已打开。\n");
|
||||
else
|
||||
process.stdout.write(
|
||||
`请在本机浏览器打开下方一次性链接(60 秒有效,不要发送到聊天):\n${url}\n`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
let hidden = false;
|
||||
const output = new Writable({
|
||||
write(chunk, _encoding, done) {
|
||||
if (!hidden) process.stdout.write(chunk);
|
||||
done();
|
||||
},
|
||||
});
|
||||
const rl = createInterface({
|
||||
input: process.stdin,
|
||||
output,
|
||||
terminal: true,
|
||||
historySize: 0,
|
||||
});
|
||||
const question = async (label: string) => (await rl.question(label)).trim();
|
||||
try {
|
||||
const config = await store.config();
|
||||
process.stdout.write(
|
||||
"CrossDesk MCP · 本地设备配置\n密码只保存到系统凭据库,不写入设备配置。\n",
|
||||
);
|
||||
const name = await question("设备别名:");
|
||||
const previous = config.devices.find((d) => d.alias === name);
|
||||
const device = previous?.device_id ?? (await question("9 位设备 ID:"));
|
||||
const server =
|
||||
previous?.server ??
|
||||
((await question(`服务器 [${config.server}]:`)) || config.server);
|
||||
const port =
|
||||
previous?.port ??
|
||||
Number((await question(`端口 [${config.port}]:`)) || config.port);
|
||||
const permission = await question(
|
||||
"允许未来自动连接?[n / observe / control]:",
|
||||
);
|
||||
const environment = await question(
|
||||
"使用现有密码环境变量(留空使用系统凭据库):",
|
||||
);
|
||||
let password: string | undefined;
|
||||
if (!environment) {
|
||||
hidden = true;
|
||||
process.stdout.write("访问密码(隐藏输入,可为空):");
|
||||
try {
|
||||
password = await rl.question("");
|
||||
} finally {
|
||||
hidden = false;
|
||||
process.stdout.write("\n");
|
||||
}
|
||||
}
|
||||
parse(uiConnect, {
|
||||
device_id: device,
|
||||
label: name,
|
||||
server,
|
||||
port,
|
||||
password,
|
||||
});
|
||||
const d: Device = {
|
||||
alias: name,
|
||||
device_id: device,
|
||||
server,
|
||||
port,
|
||||
...(permission === "observe" || permission === "control"
|
||||
? { authorization: permission }
|
||||
: {}),
|
||||
...(environment
|
||||
? { credential: "environment", environment_variable: environment }
|
||||
: {}),
|
||||
};
|
||||
await store.save(d, password);
|
||||
password = undefined;
|
||||
process.stdout.write(
|
||||
"已保存。Agent 可调用 crossdesk_list_devices;自动连接仍需有效授权和凭据。\n",
|
||||
);
|
||||
} finally {
|
||||
hidden = false;
|
||||
rl.close();
|
||||
}
|
||||
}
|
||||
async function main() {
|
||||
const { values, positionals } = parseArgs({
|
||||
allowPositionals: true,
|
||||
strict: true,
|
||||
options: {
|
||||
stdio: { type: "boolean" },
|
||||
panel: { type: "string" },
|
||||
"video-path": { type: "string" },
|
||||
"video-decoder": { type: "string" },
|
||||
"config-dir": { type: "string" },
|
||||
"native-binary": { type: "string" },
|
||||
"open-panel": { type: "boolean" },
|
||||
view: { type: "string" },
|
||||
"cell-aspect": { type: "string" },
|
||||
help: { type: "boolean" },
|
||||
version: { type: "boolean" },
|
||||
},
|
||||
});
|
||||
if (values.version) {
|
||||
process.stdout.write("0.1.0\n");
|
||||
return;
|
||||
}
|
||||
if (values.help || !positionals.length) {
|
||||
process.stdout.write(
|
||||
"crossdesk-mcp serve --stdio [--panel auto|apps|browser|terminal|off] [--video-path browser|minirtc] [--video-decoder auto|software] [--config-dir directory]\ncrossdesk-mcp view --view VIEW_ID [--config-dir directory] [--cell-aspect 0.5]\ncrossdesk-mcp setup [--config-dir directory]\ncrossdesk-mcp setup --open-panel --view VIEW_ID [--config-dir directory]\nDevelopment: --native-binary /absolute/path/to/crossdesk-cli\n",
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (positionals.length !== 1 || !["serve", "setup", "view"].includes(positionals[0]!))
|
||||
throw new Failure("INVALID_COMMAND");
|
||||
const store = new DeviceStore(
|
||||
values["config-dir"] ?? process.env.CROSSDESK_MCP_CONFIG_DIR,
|
||||
);
|
||||
await store.initialize();
|
||||
if (positionals[0] === "view") {
|
||||
if (!values.view) throw new Failure("VIEW_REQUIRED");
|
||||
await terminalView(store.directory, values.view, Number(values["cell-aspect"] ?? 0.5));
|
||||
return;
|
||||
}
|
||||
if (positionals[0] === "setup") {
|
||||
if (values["open-panel"] && !values.view)
|
||||
throw new Failure("VIEW_REQUIRED");
|
||||
await setup(store, values["open-panel"] ? values.view : undefined);
|
||||
return;
|
||||
}
|
||||
if (!values.stdio) throw new Failure("STDIO_REQUIRED");
|
||||
const config = await store.config();
|
||||
const policy: Panel = parse(
|
||||
panel,
|
||||
values.panel ?? config.panel,
|
||||
);
|
||||
const sessions = new Sessions(
|
||||
store,
|
||||
policy,
|
||||
values["native-binary"] ?? process.env.CROSSDESK_NATIVE_BINARY,
|
||||
parse(videoOptions, {
|
||||
decode_path: values["video-path"] ?? config.video.decode_path,
|
||||
decoder: values["video-decoder"] ?? config.video.decoder,
|
||||
}),
|
||||
);
|
||||
const browser = new BrowserPanel(sessions);
|
||||
const terminal = new TerminalPanel(sessions);
|
||||
const handle = serveStdio(() => createServer(sessions, browser, terminal), {
|
||||
transport: new StdioServerTransport(),
|
||||
onerror: () => process.stderr.write("crossdesk-mcp: protocol error\n"),
|
||||
});
|
||||
let closing = false;
|
||||
const close = async () => {
|
||||
if (closing) return;
|
||||
closing = true;
|
||||
await terminal.close();
|
||||
await browser.close();
|
||||
await sessions.close();
|
||||
await handle.close();
|
||||
};
|
||||
process.stdin.once("end", () => {
|
||||
void close();
|
||||
});
|
||||
for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"] as const)
|
||||
process.once(signal, () => {
|
||||
void close().finally(() => {
|
||||
process.exitCode = 0;
|
||||
});
|
||||
});
|
||||
process.once("uncaughtException", () => {
|
||||
process.stderr.write("crossdesk-mcp: INTERNAL_ERROR\n");
|
||||
void close().finally(() => {
|
||||
process.exitCode = 1;
|
||||
});
|
||||
});
|
||||
}
|
||||
main().catch((error) => {
|
||||
process.stderr.write(`crossdesk-mcp: ${failure(error).code}\n`);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
@@ -0,0 +1,286 @@
|
||||
import { spawn, execFile, type ChildProcess } from "node:child_process";
|
||||
import { createRequire } from "node:module";
|
||||
import { dirname, join, resolve } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
import type { Writable } from "node:stream";
|
||||
import { Failure, type Data, type VideoOptions } from "./common.js";
|
||||
|
||||
export const NATIVE_VERSION = "0.2.0";
|
||||
const run = promisify(execFile);
|
||||
const require = createRequire(import.meta.url);
|
||||
function nativeEnvironment(): NodeJS.ProcessEnv {
|
||||
const env: NodeJS.ProcessEnv = {};
|
||||
for (const key of [
|
||||
"PATH",
|
||||
"HOME",
|
||||
"USERPROFILE",
|
||||
"SystemRoot",
|
||||
"SYSTEMROOT",
|
||||
"WINDIR",
|
||||
"TEMP",
|
||||
"TMP",
|
||||
"TMPDIR",
|
||||
"LANG",
|
||||
"LC_ALL",
|
||||
"SSL_CERT_FILE",
|
||||
"SSL_CERT_DIR",
|
||||
])
|
||||
if (process.env[key] !== undefined) env[key] = process.env[key];
|
||||
return env;
|
||||
}
|
||||
export async function nativeBinary(override?: string): Promise<string> {
|
||||
let binary: string;
|
||||
if (override) binary = resolve(override);
|
||||
else {
|
||||
const target = `${process.platform === "win32" ? "windows" : process.platform}-${process.arch}`;
|
||||
if (
|
||||
![
|
||||
"darwin-arm64",
|
||||
"darwin-x64",
|
||||
"linux-arm64",
|
||||
"linux-x64",
|
||||
"windows-x64",
|
||||
].includes(target)
|
||||
)
|
||||
throw new Failure("PLATFORM_UNSUPPORTED");
|
||||
if (
|
||||
process.platform === "linux" &&
|
||||
!(process.report.getReport() as Data).header.glibcVersionRuntime
|
||||
)
|
||||
throw new Failure("GLIBC_REQUIRED");
|
||||
try {
|
||||
const manifest = require.resolve(
|
||||
`@chunkdash/crossdesk-cli-${target}/package.json`,
|
||||
);
|
||||
if (require(manifest).version !== NATIVE_VERSION)
|
||||
throw new Failure("NATIVE_VERSION_MISMATCH");
|
||||
binary = join(
|
||||
dirname(manifest),
|
||||
"bin",
|
||||
process.platform === "win32" ? "crossdesk-cli.exe" : "crossdesk-cli",
|
||||
);
|
||||
} catch (error) {
|
||||
throw error instanceof Failure
|
||||
? error
|
||||
: new Failure("NATIVE_PACKAGE_MISSING");
|
||||
}
|
||||
}
|
||||
try {
|
||||
const { stdout } = await run(binary, ["doctor", "--json"], {
|
||||
timeout: 5000,
|
||||
maxBuffer: 65536,
|
||||
windowsHide: true,
|
||||
env: nativeEnvironment(),
|
||||
});
|
||||
const doctor = JSON.parse(stdout);
|
||||
if (doctor.automation_protocol !== "crossdesk-automation/1" ||
|
||||
doctor.automation_terminal_protocol !== "ansi/1" ||
|
||||
doctor.automation_video_transport !== "loopback-websocket/1" ||
|
||||
!["h264-annexb/1", "nv12/1"].every((p) => doctor.automation_video_protocols?.includes(p)) ||
|
||||
!["auto", "software"].every((p) => doctor.automation_video_decoders?.includes(p)))
|
||||
throw new Failure("NATIVE_PROTOCOL_MISMATCH");
|
||||
} catch (error) {
|
||||
throw error instanceof Failure ? error : new Failure("NATIVE_START_FAILED");
|
||||
}
|
||||
return binary;
|
||||
}
|
||||
|
||||
type Pending = {
|
||||
resolve: (value: Data) => void;
|
||||
reject: (error: Failure) => void;
|
||||
clean: () => void;
|
||||
action: boolean;
|
||||
};
|
||||
export class NativeClient {
|
||||
private child: ChildProcess;
|
||||
private pending = new Map<number, Pending>();
|
||||
private sequence = 0;
|
||||
private chunks: Buffer[] = [];
|
||||
private bufferedBytes = 0;
|
||||
private closed = false;
|
||||
private exited = false;
|
||||
private closing?: Promise<void>;
|
||||
constructor(binary: string, server: string, port: number, logs: string,
|
||||
readonly videoOptions: VideoOptions = { decode_path: "browser", decoder: "auto" }) {
|
||||
this.child = spawn(
|
||||
binary,
|
||||
[
|
||||
"agent",
|
||||
"--stdio",
|
||||
"--password-fd",
|
||||
"3",
|
||||
"--video-ws",
|
||||
"--server",
|
||||
server,
|
||||
"--port",
|
||||
String(port),
|
||||
"--log-dir",
|
||||
logs,
|
||||
],
|
||||
{
|
||||
stdio: ["pipe", "pipe", "ignore", "pipe"],
|
||||
shell: false,
|
||||
windowsHide: true,
|
||||
env: nativeEnvironment(),
|
||||
},
|
||||
);
|
||||
this.child.stdout!.on("data", (part: Buffer) => this.receive(part));
|
||||
this.child.stdin!.on("error", () => this.fail());
|
||||
this.child.stdio[3]!.on("error", () => this.fail());
|
||||
this.child.once("error", () => this.fail());
|
||||
// Process exit can precede the final stdout data. Drain replies until EOF
|
||||
// or close before rejecting requests that have no response.
|
||||
this.child.once("close", () => { this.exited = true; this.fail(); });
|
||||
this.child.stdout!.once("end", () => this.fail());
|
||||
}
|
||||
async connect(
|
||||
device: string,
|
||||
password: string,
|
||||
mode: string,
|
||||
display: number,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Data> {
|
||||
if (Buffer.byteLength(password) > 256 || /[\0\r\n]/.test(password))
|
||||
throw new Failure("INVALID_CREDENTIAL");
|
||||
const hello = await this.request(
|
||||
"hello",
|
||||
{ protocol: "crossdesk-automation/1" },
|
||||
5000,
|
||||
signal,
|
||||
);
|
||||
if (hello.protocol !== "crossdesk-automation/1" ||
|
||||
hello.terminal_protocol !== "ansi/1" ||
|
||||
hello.video_transport !== "loopback-websocket/1" ||
|
||||
!hello.video_protocols?.includes(this.videoOptions.decode_path === "minirtc" ? "nv12/1" : "h264-annexb/1") ||
|
||||
!hello.video_decoders?.includes(this.videoOptions.decoder))
|
||||
throw new Failure("NATIVE_PROTOCOL_MISMATCH");
|
||||
const secret = Buffer.from(password + "\n");
|
||||
password = "";
|
||||
(this.child.stdio[3] as Writable).end(secret, () => secret.fill(0));
|
||||
return this.request("connect", { device, mode, display, ...this.videoOptions }, 7000, signal);
|
||||
}
|
||||
request(
|
||||
method: string,
|
||||
args: Data = {},
|
||||
timeout = 7000,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Data> {
|
||||
if (this.closed || (this.closing && method !== "disconnect"))
|
||||
return Promise.reject(new Failure("SESSION_CLOSED"));
|
||||
if (signal?.aborted) return Promise.reject(new Failure("CANCELLED"));
|
||||
if (this.pending.size >= 32 || this.child.stdin!.writableLength > 262144)
|
||||
return Promise.reject(new Failure("BUSY", false, true));
|
||||
const id = ++this.sequence;
|
||||
const line = JSON.stringify({ ...args, id, method }) + "\n";
|
||||
if (Buffer.byteLength(line) > 65536)
|
||||
return Promise.reject(new Failure("MESSAGE_TOO_LARGE"));
|
||||
return new Promise((resolve, reject) => {
|
||||
const cancel = (code: string) => {
|
||||
const p = this.pending.get(id);
|
||||
if (!p) return;
|
||||
this.pending.delete(id);
|
||||
p.clean();
|
||||
// Cancellation is best effort; a timed-out action is never replayed.
|
||||
this.child.stdin?.write(
|
||||
JSON.stringify({
|
||||
id: ++this.sequence,
|
||||
method: "cancel",
|
||||
request_id: id,
|
||||
}) + "\n",
|
||||
);
|
||||
reject(new Failure(code, method === "action"));
|
||||
};
|
||||
const abort = () => cancel("CANCELLED");
|
||||
const timer = setTimeout(() => cancel("NATIVE_TIMEOUT"), timeout);
|
||||
signal?.addEventListener("abort", abort, { once: true });
|
||||
this.pending.set(id, {
|
||||
resolve,
|
||||
reject,
|
||||
action: method === "action",
|
||||
clean: () => {
|
||||
clearTimeout(timer);
|
||||
signal?.removeEventListener("abort", abort);
|
||||
},
|
||||
});
|
||||
this.child.stdin!.write(line);
|
||||
});
|
||||
}
|
||||
private receive(part: Buffer) {
|
||||
if (this.closed) return;
|
||||
for (let start = 0; start < part.length;) {
|
||||
const end = part.indexOf(10, start);
|
||||
const chunk = part.subarray(start, end < 0 ? part.length : end);
|
||||
this.bufferedBytes += chunk.length;
|
||||
if (this.bufferedBytes > 4 * 1024 * 1024) {
|
||||
this.fail();
|
||||
this.child.kill();
|
||||
return;
|
||||
}
|
||||
this.chunks.push(chunk);
|
||||
if (end < 0) return;
|
||||
// One allocation and UTF-8 decode per response, including split UTF-8
|
||||
// codepoints. Do not repeatedly scan/copy a multi-megabyte PNG response.
|
||||
const line = this.chunks.length === 1 ? chunk : Buffer.concat(this.chunks, this.bufferedBytes);
|
||||
this.chunks = [];
|
||||
this.bufferedBytes = 0;
|
||||
start = end + 1;
|
||||
try {
|
||||
const reply = JSON.parse(line.toString("utf8"));
|
||||
if (!reply || !Number.isSafeInteger(reply.id) || reply.id < 0 || typeof reply.ok !== "boolean" ||
|
||||
(reply.ok && (!reply.result || typeof reply.result !== "object" || Array.isArray(reply.result))) ||
|
||||
(!reply.ok && (!reply.error || typeof reply.error.code !== "string")))
|
||||
throw new Error();
|
||||
const p = this.pending.get(reply.id);
|
||||
if (!p) continue;
|
||||
this.pending.delete(reply.id);
|
||||
p.clean();
|
||||
if (reply.ok) p.resolve(reply.result);
|
||||
else
|
||||
p.reject(
|
||||
new Failure(
|
||||
/^[A-Z_]+$/.test(reply.error?.code)
|
||||
? reply.error.code
|
||||
: "NATIVE_ERROR",
|
||||
!!reply.error?.side_effect_possible,
|
||||
!!reply.error?.retryable,
|
||||
),
|
||||
);
|
||||
} catch {
|
||||
this.fail();
|
||||
this.child.kill();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
private fail() {
|
||||
this.closed = true;
|
||||
this.chunks = [];
|
||||
this.bufferedBytes = 0;
|
||||
for (const p of this.pending.values()) {
|
||||
p.clean();
|
||||
p.reject(new Failure("SESSION_CLOSED", p.action));
|
||||
}
|
||||
this.pending.clear();
|
||||
}
|
||||
close(): Promise<void> {
|
||||
if (this.closing) return this.closing;
|
||||
this.closing = (async () => {
|
||||
if (!this.closed)
|
||||
await this.request("disconnect", {}, 1000).catch(() => {});
|
||||
this.child.stdin?.end();
|
||||
if (!this.exited && this.child.exitCode === null && this.child.signalCode === null)
|
||||
await new Promise<void>((resolve) => {
|
||||
const kill = setTimeout(() => {
|
||||
this.child.kill("SIGKILL");
|
||||
}, 2000);
|
||||
// Keep the device lock until the child really exits.
|
||||
this.child.once("close", () => {
|
||||
clearTimeout(kill);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
this.fail();
|
||||
})();
|
||||
return this.closing;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,412 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { z } from "zod";
|
||||
import { McpServer } from "@modelcontextprotocol/server";
|
||||
import type {
|
||||
CallToolResult,
|
||||
ClientCapabilities,
|
||||
Tool,
|
||||
ServerContext,
|
||||
} from "@modelcontextprotocol/server";
|
||||
import {
|
||||
getUiCapability,
|
||||
registerAppResource,
|
||||
registerAppTool,
|
||||
RESOURCE_MIME_TYPE,
|
||||
} from "@modelcontextprotocol/ext-apps/server";
|
||||
import { Sessions, type View } from "./sessions.js";
|
||||
import { BrowserPanel } from "./browser.js";
|
||||
import { TerminalPanel } from "./terminal.js";
|
||||
import { uiAction, uiSchemas, type UIAction } from "./ui-actions.js";
|
||||
import {
|
||||
id,
|
||||
alias,
|
||||
mode,
|
||||
action,
|
||||
screenshot,
|
||||
Failure,
|
||||
failure,
|
||||
parse,
|
||||
token,
|
||||
type Data,
|
||||
} from "./common.js";
|
||||
|
||||
const resource = "ui://crossdesk/viewer.html";
|
||||
const uiNames: Record<UIAction, string> = {
|
||||
ready: "crossdesk_ui_ready",
|
||||
devices: "crossdesk_ui_list_devices",
|
||||
status: "crossdesk_ui_get_session",
|
||||
connect: "crossdesk_ui_connect",
|
||||
video_open: "crossdesk_video_open",
|
||||
control: "crossdesk_viewer_control",
|
||||
};
|
||||
function output(value: Data, meta?: Data): CallToolResult {
|
||||
const data = { ...value };
|
||||
const content: any[] = [];
|
||||
if (data.mime_type === "image/png" && typeof data.data === "string") {
|
||||
content.push({ type: "image", mimeType: data.mime_type, data: data.data });
|
||||
delete data.data;
|
||||
}
|
||||
if (data.observation?.data) {
|
||||
content.push({
|
||||
type: "image",
|
||||
mimeType: data.observation.mime_type,
|
||||
data: data.observation.data,
|
||||
});
|
||||
data.observation = { ...data.observation };
|
||||
delete data.observation.data;
|
||||
}
|
||||
content.unshift({ type: "text", text: JSON.stringify(data) });
|
||||
return { content, structuredContent: data, ...(meta ? { _meta: meta } : {}) };
|
||||
}
|
||||
export function createServer(
|
||||
sessions: Sessions,
|
||||
browser: BrowserPanel,
|
||||
terminal: TerminalPanel,
|
||||
): McpServer {
|
||||
const server = new McpServer(
|
||||
{ name: "crossdesk-mcp", version: "0.1.0" },
|
||||
{
|
||||
capabilities: { tools: {}, resources: {} },
|
||||
instructions:
|
||||
"Remote computer control. Open a panel for the user to select and authorize a device. Never request passwords in chat. When a remote task is pending, immediately follow crossdesk_open with crossdesk_get_session(view_id, wait_ms: 25000) in the same turn. Repeat bounded waits on wait_timed_out until ready or the user cancels; do not ask the user to type a connection confirmation or click Continue. On ready, continue the requested task within current authorization. On closed/failed, stop waiting and do not automatically reconnect. Obtain a screenshot before actions and pass its observation_id; coordinates refer to that returned image. Submitted actions are not remote execution acknowledgements. Never replay actions after uncertain outcomes. Human pause can only be resumed in the user panel.",
|
||||
},
|
||||
);
|
||||
type Definition = {
|
||||
name: string;
|
||||
schema: z.ZodType;
|
||||
description: string;
|
||||
ui?: boolean;
|
||||
app?: boolean;
|
||||
readOnly?: boolean;
|
||||
run: (p: Data, ctx: ServerContext) => Promise<CallToolResult>;
|
||||
};
|
||||
const definitions = new Map<string, Definition>();
|
||||
const supported = (ctx: ServerContext) => {
|
||||
const envelope = ctx.mcpReq.envelope as Data | undefined;
|
||||
const caps = envelope
|
||||
? envelope["io.modelcontextprotocol/clientCapabilities"]
|
||||
: server.server.getClientCapabilities();
|
||||
return (
|
||||
getUiCapability((caps ?? {}) as ClientCapabilities)?.mimeTypes?.includes(
|
||||
RESOURCE_MIME_TYPE,
|
||||
) ?? false
|
||||
);
|
||||
};
|
||||
const apps = (ctx: ServerContext) =>
|
||||
sessions.policy === "apps" && supported(ctx);
|
||||
const openPanel = async (v: View) => v.panel === "browser" ? browser.open(v)
|
||||
: v.panel === "terminal" ? terminal.open(v) : sessions.describe(v);
|
||||
const viewResult = (v: View, result: Data) =>
|
||||
output(
|
||||
result,
|
||||
v.panel === "apps"
|
||||
? {
|
||||
"crossdesk/ui": {
|
||||
view_id: v.id,
|
||||
view_generation: v.generation,
|
||||
ui_token: v.secret,
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
);
|
||||
const add = (
|
||||
name: string,
|
||||
schema: z.ZodType,
|
||||
description: string,
|
||||
run: Definition["run"],
|
||||
options: Partial<Definition> = {},
|
||||
) => {
|
||||
const d = { name, schema, description, run, ...options };
|
||||
definitions.set(name, d);
|
||||
const config = {
|
||||
description,
|
||||
inputSchema: schema,
|
||||
annotations: {
|
||||
readOnlyHint: !!d.readOnly,
|
||||
destructiveHint: d.name === "crossdesk_action",
|
||||
openWorldHint: true,
|
||||
},
|
||||
...(d.ui || d.app
|
||||
? {
|
||||
_meta: {
|
||||
ui: {
|
||||
...(d.ui ? { resourceUri: resource } : {}),
|
||||
visibility: d.app
|
||||
? ["app" as const]
|
||||
: ["model" as const, "app" as const],
|
||||
},
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
};
|
||||
if (d.ui || d.app)
|
||||
registerAppTool(
|
||||
server,
|
||||
name,
|
||||
{ ...config, _meta: config._meta! },
|
||||
(p, ctx) => run(p as Data, ctx),
|
||||
);
|
||||
else server.registerTool(name, config, (p, ctx) => run(p as Data, ctx));
|
||||
};
|
||||
add(
|
||||
"crossdesk_open",
|
||||
z.strictObject({ device_alias: alias.optional() }),
|
||||
"Open CrossDesk so the user can choose a device, enter its password, and authorize this session. GUI environments use a browser; headless terminal environments return terminal_command, which you must show to the user to run in a separate terminal. Never run the terminal viewer in a captured tool-output channel. Returns immediately. To continue a pending remote task, call crossdesk_get_session with the returned view_id and wait_ms: 25000; no chat confirmation is needed.",
|
||||
async (p, ctx) => {
|
||||
if (p.device_alias) await sessions.store.device(p.device_alias);
|
||||
const v = sessions.create(
|
||||
sessions.choose(supported(ctx)),
|
||||
p.device_alias,
|
||||
);
|
||||
return viewResult(
|
||||
v,
|
||||
await openPanel(v),
|
||||
);
|
||||
},
|
||||
{ ui: true },
|
||||
);
|
||||
add(
|
||||
"crossdesk_open_browser",
|
||||
z.strictObject({ view_id: id.optional() }),
|
||||
"Explicitly open the local browser panel after the user requests browser fallback. Does not create another remote connection.",
|
||||
async (p) => {
|
||||
if (!["auto", "browser"].includes(sessions.policy))
|
||||
throw new Failure("PANEL_POLICY_DENIED");
|
||||
const v = p.view_id
|
||||
? sessions.view(p.view_id)
|
||||
: sessions.create("browser");
|
||||
if (v.panel !== "browser") {
|
||||
await sessions.revokeVideo(v);
|
||||
v.panel = "browser";
|
||||
v.generation++;
|
||||
v.secret = token();
|
||||
v.ready = false;
|
||||
v.created = Date.now();
|
||||
}
|
||||
return output(await browser.open(v));
|
||||
},
|
||||
);
|
||||
add(
|
||||
"crossdesk_list_devices",
|
||||
z.strictObject({}),
|
||||
"List the local saved device book and persistent authorization, without credentials. This is not online discovery.",
|
||||
async () => output({ devices: await sessions.store.list() }),
|
||||
{ readOnly: true },
|
||||
);
|
||||
add(
|
||||
"crossdesk_connect",
|
||||
z.strictObject({
|
||||
device_alias: alias,
|
||||
mode: mode.default("observe"),
|
||||
display_index: z.int().min(0).max(63).default(0),
|
||||
}),
|
||||
"Connect only a saved, previously authorized device with available credentials. AUTH_REQUIRED means the user must authorize through the panel or setup.",
|
||||
async (p, ctx) => {
|
||||
const selected = sessions.choose(supported(ctx));
|
||||
// Reserve panel capacity before starting a connection. Once connected,
|
||||
// panel setup errors must not hide the live session from the caller.
|
||||
const v = sessions.create(selected, p.device_alias);
|
||||
let result: Data;
|
||||
try {
|
||||
result = await sessions.connectSaved(
|
||||
p.device_alias,
|
||||
p.mode,
|
||||
p.display_index,
|
||||
ctx.mcpReq.signal,
|
||||
);
|
||||
} catch (error) {
|
||||
sessions.views.delete(v.id);
|
||||
throw error;
|
||||
}
|
||||
v.session = result.session_id;
|
||||
let panelResult: Data;
|
||||
try { panelResult = await openPanel(v); }
|
||||
catch (error) { panelResult = { ...sessions.describe(v), panel_error: failure(error) }; }
|
||||
return viewResult(v, {
|
||||
...result,
|
||||
...panelResult,
|
||||
state: result.state,
|
||||
});
|
||||
},
|
||||
{ ui: true },
|
||||
);
|
||||
add(
|
||||
"crossdesk_get_session",
|
||||
z.strictObject({
|
||||
session_id: id.optional(),
|
||||
view_id: id.optional(),
|
||||
wait_ms: z.int().min(0).max(25000).optional(),
|
||||
}),
|
||||
"Get connection, display geometry, frame age and control state. Supply exactly one of session_id or view_id. With view_id, wait for connection (default 25000 ms; 0 returns immediately). On wait_timed_out, repeat while the remote task is pending. On ready, continue without asking the user to send a message. Waiting never connects or grants control. session_id returns immediately and requires wait_ms to be 0 or omitted.",
|
||||
async (p, ctx) => {
|
||||
if (!!p.session_id === !!p.view_id)
|
||||
throw new Failure("SELECT_ONE_SESSION");
|
||||
if (p.session_id && p.wait_ms) throw new Failure("INVALID_ARGUMENT");
|
||||
return output(p.view_id
|
||||
? await sessions.waitForConnection(p.view_id, p.wait_ms ?? 25000, ctx.mcpReq.signal)
|
||||
: await sessions.status(p.session_id, undefined, ctx.mcpReq.signal));
|
||||
},
|
||||
{ readOnly: true },
|
||||
);
|
||||
add(
|
||||
"crossdesk_screenshot",
|
||||
screenshot,
|
||||
"Capture the remote display as a PNG image and immutable observation metadata. Coordinates in subsequent actions use this returned image. after_frame_id requires generation.",
|
||||
async (p, ctx) => output(await sessions.screenshot(p, ctx.mcpReq.signal)),
|
||||
{ readOnly: true },
|
||||
);
|
||||
add(
|
||||
"crossdesk_action",
|
||||
z.strictObject({
|
||||
session_id: id,
|
||||
operation_id: id,
|
||||
observation_id: id,
|
||||
action,
|
||||
observe_after: z.boolean().default(true),
|
||||
}),
|
||||
"Submit one structured action against a recent observation. Reuse operation_id only to retrieve that same operation, never replay uncertain effects with a new ID. Human pause prevents control. A post-action screenshot is not an execution acknowledgement.",
|
||||
async (p, ctx) => output(await sessions.action(p, ctx.mcpReq.signal)),
|
||||
);
|
||||
add(
|
||||
"crossdesk_set_control",
|
||||
z.strictObject({ session_id: id, enabled: z.boolean() }),
|
||||
"Enable or disable control within the user-authorized scope. Cannot override human pause or upgrade an observe-only connection.",
|
||||
async (p) => output(await sessions.control(p.session_id, p.enabled)),
|
||||
);
|
||||
add(
|
||||
"crossdesk_select_display",
|
||||
z.strictObject({ session_id: id, display_index: z.int().min(0).max(63) }),
|
||||
"Select a remote display. Invalidates previous observations; capture a new screenshot before any further input.",
|
||||
async (p) => output(await sessions.display(p.session_id, p.display_index)),
|
||||
);
|
||||
add(
|
||||
"crossdesk_disconnect",
|
||||
z.strictObject({ session_id: id }),
|
||||
"Disconnect the remote session, release input state and stop associated previews.",
|
||||
async (p) => output(await sessions.disconnect(p.session_id)),
|
||||
);
|
||||
for (const [name, schema] of Object.entries(uiSchemas)) {
|
||||
add(
|
||||
uiNames[name as UIAction],
|
||||
schema.extend({
|
||||
view_id: id,
|
||||
view_generation: z.int().min(1),
|
||||
ui_token: id,
|
||||
}),
|
||||
`CrossDesk panel only: ${name}.`,
|
||||
async (p, ctx) => {
|
||||
if (!apps(ctx)) throw new Failure("PANEL_UNSUPPORTED");
|
||||
const { view_id, view_generation, ui_token, ...args } = p;
|
||||
const v = sessions.authenticate(view_id, view_generation, ui_token);
|
||||
if (v.panel !== "apps") throw new Failure("VIEW_UNAUTHORIZED");
|
||||
// Video grants stay in the authenticated app-only response.
|
||||
const result = name === "video_open" ? await browser.video(v) : await uiAction(
|
||||
sessions,
|
||||
v,
|
||||
name,
|
||||
args,
|
||||
ctx.mcpReq.signal,
|
||||
);
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: name === "video_open" ? "Video viewer connection" : JSON.stringify(result),
|
||||
},
|
||||
],
|
||||
structuredContent: result,
|
||||
};
|
||||
},
|
||||
{ app: true },
|
||||
);
|
||||
}
|
||||
registerAppResource(
|
||||
server,
|
||||
"CrossDesk viewer",
|
||||
resource,
|
||||
{ mimeType: RESOURCE_MIME_TYPE },
|
||||
async (uri, ctx) => {
|
||||
if (!apps(ctx)) throw new Failure("PANEL_UNSUPPORTED");
|
||||
return {
|
||||
contents: [
|
||||
{
|
||||
uri: uri.href,
|
||||
mimeType: RESOURCE_MIME_TYPE,
|
||||
text: await readFile(
|
||||
new URL("./viewer-apps.html", import.meta.url),
|
||||
"utf8",
|
||||
),
|
||||
_meta: {
|
||||
ui: {
|
||||
prefersBorder: true,
|
||||
csp: { connectDomains: await browser.connectDomains(), resourceDomains: [] },
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
},
|
||||
);
|
||||
// Request-scoped projection avoids global tool mutations when modern clients
|
||||
// send concurrent calls with different capability envelopes.
|
||||
server.server.setRequestHandler("tools/list", async (_req, ctx) => ({
|
||||
tools: [...definitions.values()]
|
||||
.filter((d) => !d.app || apps(ctx))
|
||||
.map((d) => {
|
||||
const inputSchema = z.toJSONSchema(d.schema, {
|
||||
io: "input",
|
||||
}) as Tool["inputSchema"];
|
||||
if (d.name === "crossdesk_get_session")
|
||||
inputSchema.oneOf = [
|
||||
{ required: ["session_id"] },
|
||||
{ required: ["view_id"] },
|
||||
];
|
||||
return {
|
||||
name: d.name,
|
||||
description: d.description,
|
||||
inputSchema,
|
||||
annotations: {
|
||||
readOnlyHint: !!d.readOnly,
|
||||
destructiveHint: d.name === "crossdesk_action",
|
||||
openWorldHint: true,
|
||||
},
|
||||
...((d.ui || d.app) && apps(ctx)
|
||||
? {
|
||||
_meta: {
|
||||
ui: {
|
||||
...(d.ui ? { resourceUri: resource } : {}),
|
||||
visibility: d.app ? ["app"] : ["model", "app"],
|
||||
},
|
||||
...(d.ui ? { "ui/resourceUri": resource } : {}),
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
};
|
||||
}),
|
||||
}));
|
||||
server.server.setRequestHandler("tools/call", async (req, ctx) => {
|
||||
try {
|
||||
const d = definitions.get(req.params.name);
|
||||
if (!d || (d.app && !apps(ctx))) throw new Failure("UNKNOWN_TOOL");
|
||||
if (Buffer.byteLength(JSON.stringify(req.params.arguments ?? {})) > 65536)
|
||||
throw new Failure("MESSAGE_TOO_LARGE");
|
||||
const p = parse(d.schema, req.params.arguments ?? {}) as Data;
|
||||
return await d.run(p, ctx);
|
||||
} catch (error) {
|
||||
const value = { error: failure(error) };
|
||||
return { ...output(value), isError: true };
|
||||
}
|
||||
});
|
||||
server.server.setRequestHandler("resources/list", async (_req, ctx) => ({
|
||||
resources: apps(ctx)
|
||||
? [
|
||||
{
|
||||
name: "CrossDesk viewer",
|
||||
uri: resource,
|
||||
mimeType: RESOURCE_MIME_TYPE,
|
||||
},
|
||||
]
|
||||
: [],
|
||||
}));
|
||||
return server;
|
||||
}
|
||||
@@ -0,0 +1,485 @@
|
||||
import { join } from "node:path";
|
||||
import { setTimeout as delay } from "node:timers/promises";
|
||||
import {
|
||||
Failure,
|
||||
failure,
|
||||
token,
|
||||
equal,
|
||||
parse,
|
||||
uiConnect,
|
||||
type Data,
|
||||
type Panel,
|
||||
type VideoOptions,
|
||||
} from "./common.js";
|
||||
import { DeviceStore, type Device } from "./store.js";
|
||||
import { nativeBinary, NativeClient } from "./native.js";
|
||||
import { displayEnvironment } from "./environment.js";
|
||||
|
||||
export interface View {
|
||||
id: string;
|
||||
generation: number;
|
||||
secret: string;
|
||||
panel: Exclude<Panel, "auto">;
|
||||
created: number;
|
||||
ready: boolean;
|
||||
lastSeen: number;
|
||||
alias?: string;
|
||||
session?: string;
|
||||
disconnected?: boolean;
|
||||
connectionError?: Data;
|
||||
}
|
||||
interface Session {
|
||||
id: string;
|
||||
device: Device;
|
||||
native: NativeClient;
|
||||
status: Data;
|
||||
release: () => Promise<void>;
|
||||
connecting: boolean;
|
||||
statusRequest?: Promise<Data>;
|
||||
closing?: Promise<void>;
|
||||
}
|
||||
export class Sessions {
|
||||
readonly views = new Map<string, View>();
|
||||
private current?: Session;
|
||||
private connecting?: AbortController;
|
||||
private connectionDone?: Promise<void>;
|
||||
private closing = false;
|
||||
private binary?: Promise<string>;
|
||||
constructor(
|
||||
readonly store: DeviceStore,
|
||||
readonly policy: Panel,
|
||||
private nativePath?: string,
|
||||
private videoOptions?: VideoOptions,
|
||||
) {}
|
||||
choose(apps: boolean): View["panel"] {
|
||||
if (this.policy === "apps" && !apps) throw new Failure("PANEL_UNSUPPORTED");
|
||||
if (this.policy !== "auto") return this.policy;
|
||||
const environment = displayEnvironment();
|
||||
return environment.gui ? "browser" : environment.terminal ? "terminal" : "off";
|
||||
}
|
||||
create(panel: View["panel"], alias?: string): View {
|
||||
for (const [id, view] of this.views)
|
||||
if (Date.now() - view.lastSeen > 30 * 60 * 1000) {
|
||||
void this.revokeVideo(view);
|
||||
this.views.delete(id);
|
||||
}
|
||||
if (this.views.size >= 32) throw new Failure("VIEW_LIMIT");
|
||||
const view: View = {
|
||||
id: token(),
|
||||
generation: 1,
|
||||
secret: token(),
|
||||
panel,
|
||||
created: Date.now(),
|
||||
lastSeen: Date.now(),
|
||||
ready: false,
|
||||
alias,
|
||||
session: this.current?.id,
|
||||
};
|
||||
this.views.set(view.id, view);
|
||||
return view;
|
||||
}
|
||||
view(id: string): View {
|
||||
const v = this.views.get(id);
|
||||
if (!v) throw new Failure("VIEW_NOT_FOUND");
|
||||
return v;
|
||||
}
|
||||
authenticate(id: string, generation: number, secret: string): View {
|
||||
const v = this.view(id);
|
||||
if (v.generation !== generation || !equal(v.secret, secret))
|
||||
throw new Failure("VIEW_UNAUTHORIZED");
|
||||
return v;
|
||||
}
|
||||
describe(v: View): Data {
|
||||
return {
|
||||
view_id: v.id,
|
||||
view_generation: v.generation,
|
||||
panel: v.panel,
|
||||
panel_status: v.ready
|
||||
? "ready"
|
||||
: Date.now() - v.created > 10000
|
||||
? "not_confirmed"
|
||||
: "waiting",
|
||||
session_id: v.session ?? null,
|
||||
state: v.session ? "session_available" : v.disconnected ? "closed" : v.connectionError ? "failed" : "awaiting_user",
|
||||
...(v.connectionError ? { error: v.connectionError } : {}),
|
||||
device_alias: v.alias ?? null,
|
||||
next_step: v.session
|
||||
? "Call crossdesk_get_session with view_id."
|
||||
: v.disconnected || v.connectionError
|
||||
? "The session was disconnected. Do not automatically reconnect."
|
||||
: v.panel === "off"
|
||||
? "Use setup to configure credentials and authorization, then crossdesk_connect with the saved device alias. Never send passwords in chat."
|
||||
: "The user selects and authorizes a device in the panel. To continue a pending remote task, immediately call crossdesk_get_session with this view_id and wait_ms: 25000; repeat on wait_timed_out. Do not ask the user to send a connection confirmation message.",
|
||||
};
|
||||
}
|
||||
private session(id: string): Session {
|
||||
if (!this.current || this.current.id !== id)
|
||||
throw new Failure("SESSION_NOT_FOUND");
|
||||
return this.current;
|
||||
}
|
||||
async status(sessionId?: string, viewId?: string, signal?: AbortSignal): Promise<Data> {
|
||||
const view = viewId ? this.view(viewId) : undefined;
|
||||
const id = sessionId ?? view?.session;
|
||||
if (!id && view) return this.describe(view);
|
||||
const s = this.session(id!);
|
||||
// A panel connection may still be performing the native hello handshake.
|
||||
// Reading native status here would race initialization and could close it.
|
||||
if (s.connecting) return {
|
||||
...(view ? this.describe(view) : {}),
|
||||
session_id: s.id,
|
||||
device_alias: s.device.alias,
|
||||
state: "connecting",
|
||||
control: false,
|
||||
};
|
||||
// Concurrent panel/model polls share one native read. A cancelled waiter
|
||||
// must not cancel another caller's status request or disconnect the session.
|
||||
if (signal?.aborted) throw new Failure("CANCELLED");
|
||||
if (!["closed", "failed"].includes(s.status.state)) {
|
||||
s.statusRequest ??= s.native.request("status", {}, 5000)
|
||||
.then((status) => (s.status = status))
|
||||
.catch((error) => {
|
||||
if (!(error instanceof Failure) || error.code !== "SESSION_CLOSED") throw error;
|
||||
return (s.status = { state: "closed", control: false });
|
||||
})
|
||||
.finally(() => { s.statusRequest = undefined; });
|
||||
await this.waitStatus(s.statusRequest, signal);
|
||||
}
|
||||
// The user may have disconnected or replaced the session during the read.
|
||||
if (this.current !== s || (view && view.session !== id))
|
||||
return view ? this.describe(view) : { session_id: id, state: "closed", control: false };
|
||||
if (["closed", "failed"].includes(s.status.state)) {
|
||||
await this.dispose(s);
|
||||
}
|
||||
return {
|
||||
...s.status,
|
||||
session_id: s.id,
|
||||
device_alias: s.device.alias,
|
||||
...(view ? this.describe(view) : {}),
|
||||
state: s.status.state,
|
||||
...(s.status.state === "ready" ? {
|
||||
next_step: "Connection ready. Continue the user's pending task within the current authorization. Obtain a screenshot before input; respect human_paused and control. No user confirmation message is needed.",
|
||||
} : ["closed", "failed", "disconnected"].includes(s.status.state) ? {
|
||||
next_step: "The connection ended. Stop waiting and do not automatically reconnect or replay actions.",
|
||||
} : {}),
|
||||
};
|
||||
}
|
||||
private async waitStatus(request: Promise<Data>, signal?: AbortSignal) {
|
||||
if (!signal) return request;
|
||||
let abort: () => void = () => {};
|
||||
try {
|
||||
return await Promise.race([request, new Promise<never>((_, reject) => {
|
||||
abort = () => reject(new Failure("CANCELLED"));
|
||||
signal.addEventListener("abort", abort, { once: true });
|
||||
if (signal.aborted) abort();
|
||||
})]);
|
||||
} finally {
|
||||
signal.removeEventListener("abort", abort);
|
||||
}
|
||||
}
|
||||
private dispose(s: Session): Promise<void> {
|
||||
return s.closing ??= s.native.close().finally(() => s.release());
|
||||
}
|
||||
async waitForConnection(viewId: string, waitMs: number, signal?: AbortSignal): Promise<Data> {
|
||||
const deadline = Date.now() + waitMs;
|
||||
let result = this.describe(this.view(viewId));
|
||||
const timedOut = () => ({
|
||||
...result,
|
||||
wait_timed_out: true,
|
||||
next_step: "Still waiting for the user to connect in the panel. Call crossdesk_get_session again with the same view_id and wait_ms: 25000 while the remote task is pending. Do not ask the user to type a connection confirmation.",
|
||||
});
|
||||
const timeout = new AbortController();
|
||||
const timer = waitMs > 0 ? setTimeout(() => timeout.abort(), waitMs) : undefined;
|
||||
const waitSignal = signal ? AbortSignal.any([signal, timeout.signal]) : timeout.signal;
|
||||
try {
|
||||
for (;;) {
|
||||
if (this.closing || signal?.aborted) throw new Failure("CANCELLED");
|
||||
const view = this.view(viewId);
|
||||
view.lastSeen = Date.now();
|
||||
result = await this.status(undefined, viewId, waitSignal);
|
||||
if (waitMs === 0 || view.panel === "off" ||
|
||||
["ready", "closed", "failed", "disconnected"].includes(result.state))
|
||||
return result;
|
||||
const remaining = deadline - Date.now();
|
||||
if (remaining <= 0) return timedOut();
|
||||
await delay(Math.min(250, remaining), undefined, { signal: waitSignal });
|
||||
}
|
||||
} catch (error) {
|
||||
if (this.closing || signal?.aborted) throw new Failure("CANCELLED");
|
||||
if (timeout.signal.aborted) return timedOut();
|
||||
throw error;
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
async connectSaved(
|
||||
name: string,
|
||||
mode: string,
|
||||
display: number,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Data> {
|
||||
const d = await this.store.device(name);
|
||||
if (
|
||||
!d.authorization ||
|
||||
(mode === "control" && d.authorization !== "control")
|
||||
)
|
||||
throw new Failure("AUTH_REQUIRED");
|
||||
return this.connect(
|
||||
d,
|
||||
await this.store.password(d),
|
||||
mode,
|
||||
display,
|
||||
undefined,
|
||||
signal,
|
||||
);
|
||||
}
|
||||
async connectUI(v: View, args: unknown, signal?: AbortSignal): Promise<Data> {
|
||||
const p = parse(uiConnect, args);
|
||||
if (!!p.device_alias === !!p.device_id)
|
||||
throw new Failure("SELECT_ONE_DEVICE");
|
||||
const config = await this.store.config();
|
||||
const d: Device = p.device_alias
|
||||
? { ...(await this.store.device(p.device_alias)) }
|
||||
: {
|
||||
alias: p.label ?? p.device_id!,
|
||||
device_id: p.device_id!,
|
||||
server: p.server ?? config.server,
|
||||
port: p.port ?? config.port,
|
||||
};
|
||||
if (p.device_alias && (p.server || p.port || p.label))
|
||||
throw new Failure("INVALID_ARGUMENT");
|
||||
if ((p.remember_password || p.persistent_authorization) && !p.save_device)
|
||||
throw new Failure("SAVE_DEVICE_REQUIRED");
|
||||
let password = p.password ?? (await this.store.password(d));
|
||||
p.password = undefined;
|
||||
// Persist only after successful authentication, avoiding poisoned saved passwords.
|
||||
const result = await this.connect(
|
||||
d,
|
||||
password,
|
||||
p.mode,
|
||||
p.display_index,
|
||||
v,
|
||||
signal,
|
||||
);
|
||||
if (p.save_device) {
|
||||
d.authorization = p.persistent_authorization ? p.mode : undefined;
|
||||
try {
|
||||
await this.store.save(d, p.remember_password ? password : undefined);
|
||||
} catch (error) {
|
||||
result.persistence_error =
|
||||
error instanceof Failure ? error.code : "CONFIG_WRITE_FAILED";
|
||||
}
|
||||
}
|
||||
password = "";
|
||||
return result;
|
||||
}
|
||||
private async connect(
|
||||
d: Device,
|
||||
password: string,
|
||||
mode: string,
|
||||
display: number,
|
||||
view?: View,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Data> {
|
||||
if (this.closing) throw new Failure("SESSION_CLOSED");
|
||||
if (this.connecting) throw new Failure("BUSY");
|
||||
// Reserve the single connection slot before the first await.
|
||||
const controller = this.connecting = new AbortController();
|
||||
signal = signal ? AbortSignal.any([signal, controller.signal]) : controller.signal;
|
||||
let complete!: () => void;
|
||||
this.connectionDone = new Promise<void>((resolve) => { complete = resolve; });
|
||||
let release: (() => Promise<void>) | undefined;
|
||||
let native: NativeClient | undefined;
|
||||
let session: Session | undefined;
|
||||
try {
|
||||
if (this.current && !["closed", "failed", "disconnected"].includes(
|
||||
(await this.status(this.current.id, undefined, signal)).state,
|
||||
)) throw new Failure("SESSION_LIMIT");
|
||||
if (this.current) await this.disconnect(this.current.id);
|
||||
if (view) {
|
||||
view.disconnected = false;
|
||||
view.connectionError = undefined;
|
||||
}
|
||||
release = await this.store.lock(
|
||||
`device:${d.server}:${d.port}:${d.device_id}`,
|
||||
);
|
||||
this.binary ??= nativeBinary(this.nativePath).catch((e) => {
|
||||
this.binary = undefined;
|
||||
throw e;
|
||||
});
|
||||
const binary = await this.binary;
|
||||
if (this.closing || signal?.aborted) throw new Failure("CANCELLED");
|
||||
native = new NativeClient(
|
||||
binary,
|
||||
d.server,
|
||||
d.port,
|
||||
join(this.store.directory, "logs"),
|
||||
this.videoOptions ?? (await this.store.config()).video,
|
||||
);
|
||||
const s: Session = session = {
|
||||
id: token(),
|
||||
device: d,
|
||||
native,
|
||||
status: {},
|
||||
release,
|
||||
connecting: true,
|
||||
};
|
||||
this.current = s;
|
||||
if (view) {
|
||||
view.session = s.id;
|
||||
view.alias = d.alias;
|
||||
view.disconnected = false;
|
||||
}
|
||||
s.status = await native.connect(
|
||||
d.device_id,
|
||||
password,
|
||||
mode,
|
||||
display,
|
||||
signal,
|
||||
);
|
||||
password = "";
|
||||
const deadline = Date.now() + 35000;
|
||||
while (Date.now() < deadline) {
|
||||
if (this.closing || signal?.aborted) throw new Failure("CANCELLED");
|
||||
s.status = await native.request("status", {}, 5000, signal);
|
||||
if (this.current !== s || signal.aborted) throw new Failure("CANCELLED");
|
||||
if (s.status.state === "ready" && typeof s.status.frame_age_ms === "number" && s.status.frame_age_ms <= 2000) {
|
||||
s.connecting = false;
|
||||
return { ...s.status, session_id: s.id, device_alias: s.device.alias };
|
||||
}
|
||||
if (["failed", "closed", "disconnected"].includes(s.status.state))
|
||||
throw new Failure(
|
||||
s.status.error === "incorrect_password"
|
||||
? "AUTH_FAILED"
|
||||
: "CONNECTION_FAILED",
|
||||
);
|
||||
await delay(100, undefined, { signal });
|
||||
}
|
||||
throw new Failure("CONNECTION_TIMEOUT");
|
||||
} catch (error) {
|
||||
if (session) await this.dispose(session);
|
||||
else {
|
||||
await native?.close();
|
||||
await release?.();
|
||||
}
|
||||
if (this.current?.native === native) this.current = undefined;
|
||||
const reason = signal.aborted ? new Failure("CANCELLED") : error;
|
||||
if (view && (!view.session || view.session === session?.id)) {
|
||||
view.session = undefined;
|
||||
if (!view.disconnected) view.connectionError = failure(reason);
|
||||
}
|
||||
throw reason;
|
||||
} finally {
|
||||
password = "";
|
||||
this.connecting = undefined;
|
||||
this.connectionDone = undefined;
|
||||
complete();
|
||||
}
|
||||
}
|
||||
async screenshot(p: Data, signal?: AbortSignal): Promise<Data> {
|
||||
const { session_id, ...args } = p;
|
||||
if (args.after_frame_id !== undefined && args.generation === undefined)
|
||||
throw new Failure("GENERATION_REQUIRED");
|
||||
return this.session(session_id).native.request(
|
||||
"screenshot",
|
||||
args,
|
||||
(args.timeout_ms ?? 2000) + 3000,
|
||||
signal,
|
||||
);
|
||||
}
|
||||
async action(p: Data, signal?: AbortSignal): Promise<Data> {
|
||||
const { session_id, observe_after = true, ...args } = p;
|
||||
const s = this.session(session_id);
|
||||
const result = await s.native.request("action", args, 5000, signal);
|
||||
if (observe_after && result.status === "submitted") {
|
||||
try {
|
||||
result.observation = await this.screenshot(
|
||||
{
|
||||
session_id,
|
||||
generation: result.generation,
|
||||
after_frame_id: result.after_frame_id,
|
||||
timeout_ms: 2000,
|
||||
},
|
||||
signal,
|
||||
);
|
||||
} catch (error) {
|
||||
result.observation_error =
|
||||
error instanceof Failure ? error.code : "OBSERVATION_FAILED";
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
async control(id: string, enabled: boolean) {
|
||||
return this.session(id).native.request("control", { enabled });
|
||||
}
|
||||
async display(id: string, index: number) {
|
||||
return this.session(id).native.request("display", { index });
|
||||
}
|
||||
async disconnect(id: string) {
|
||||
const s = this.session(id);
|
||||
if (s.connecting) this.connecting?.abort();
|
||||
for (const v of this.views.values())
|
||||
if (v.session === id) {
|
||||
v.session = undefined;
|
||||
v.disconnected = true;
|
||||
v.connectionError = undefined;
|
||||
}
|
||||
await this.dispose(s);
|
||||
if (this.current === s) this.current = undefined;
|
||||
return { session_id: id, state: "closed" };
|
||||
}
|
||||
async viewerControl(v: View, operation: string) {
|
||||
if (!v.session) throw new Failure("NOT_READY");
|
||||
if (operation === "disconnect") return this.disconnect(v.session);
|
||||
return this.session(v.session).native.request("viewer_control", {
|
||||
operation,
|
||||
});
|
||||
}
|
||||
async terminalFrame(v: View, args: Data, signal?: AbortSignal) {
|
||||
if (v.panel !== "terminal" || !v.session || this.connecting)
|
||||
throw new Failure("NOT_READY");
|
||||
const s = this.session(v.session);
|
||||
return s.native.request("terminal_frame", args, 5000, signal);
|
||||
}
|
||||
async openVideo(v: View, origin: string): Promise<Data> {
|
||||
if (!v.session || this.views.get(v.id) !== v) throw new Failure("NOT_READY");
|
||||
const s = this.session(v.session);
|
||||
if (this.closing || this.connecting || ["closed", "failed"].includes(s.status.state))
|
||||
throw new Failure("NOT_READY");
|
||||
const generation = v.generation, session = v.session;
|
||||
const ticket = token();
|
||||
const result = await s.native.request("video_open", {
|
||||
ticket, view: v.id, view_generation: generation, session, origin, apps: v.panel === "apps",
|
||||
});
|
||||
if (this.current !== s || this.views.get(v.id) !== v ||
|
||||
v.generation !== generation || v.session !== session) {
|
||||
await s.native.request("video_revoke", { view: v.id }).catch(() => {});
|
||||
throw new Failure("SESSION_CHANGED");
|
||||
}
|
||||
if (result.transport !== "loopback-websocket/1" ||
|
||||
typeof result.url !== "string" || !/^ws:\/\/127\.0\.0\.1:\d+\/api\/video$/.test(result.url) ||
|
||||
result.protocol !== (s.native.videoOptions.decode_path === "minirtc" ? "nv12/1" : "h264-annexb/1") ||
|
||||
result.decoder !== s.native.videoOptions.decoder)
|
||||
throw new Failure("NATIVE_PROTOCOL_MISMATCH");
|
||||
return { ...result, ticket };
|
||||
}
|
||||
async revokeVideo(view?: View) {
|
||||
const s = this.current;
|
||||
if (!s || (view && view.session !== s.id)) return;
|
||||
try {
|
||||
await s.native.request("video_revoke", view ? { view: view.id } : {});
|
||||
} catch {
|
||||
// Losing the control channel must not leave an authorized video socket
|
||||
// alive after its view was removed or replaced.
|
||||
if (this.current === s) await this.disconnect(s.id);
|
||||
else await s.native.close();
|
||||
}
|
||||
}
|
||||
async close() {
|
||||
this.closing = true;
|
||||
this.connecting?.abort();
|
||||
const pending = this.connectionDone;
|
||||
if (this.current) await this.disconnect(this.current.id);
|
||||
await pending;
|
||||
this.views.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import {
|
||||
mkdir,
|
||||
readFile,
|
||||
writeFile,
|
||||
rename,
|
||||
unlink,
|
||||
open,
|
||||
chmod,
|
||||
} from "node:fs/promises";
|
||||
import { homedir } from "node:os";
|
||||
import { join, resolve } from "node:path";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
alias,
|
||||
host,
|
||||
mode,
|
||||
panel,
|
||||
videoOptions,
|
||||
Failure,
|
||||
parse,
|
||||
token,
|
||||
type Data,
|
||||
} from "./common.js";
|
||||
|
||||
const deviceSchema = z.strictObject({
|
||||
alias,
|
||||
device_id: z.string().regex(/^\d{9}$/),
|
||||
server: host,
|
||||
port: z.int().min(1).max(65535),
|
||||
authorization: mode.optional(),
|
||||
credential: z.enum(["keyring", "environment"]).optional(),
|
||||
environment_variable: z
|
||||
.string()
|
||||
.regex(/^[A-Z][A-Z0-9_]{0,127}$/)
|
||||
.optional(),
|
||||
});
|
||||
export type Device = z.infer<typeof deviceSchema>;
|
||||
const configSchema = z.strictObject({
|
||||
version: z.literal(1),
|
||||
panel: panel.default("auto"),
|
||||
video: videoOptions.prefault({}),
|
||||
server: host.default("api.crossdesk.cn"),
|
||||
port: z.int().min(1).max(65535).default(9099),
|
||||
devices: z.array(deviceSchema).max(256),
|
||||
});
|
||||
export type Config = z.infer<typeof configSchema>;
|
||||
export class DeviceStore {
|
||||
readonly directory: string;
|
||||
readonly file: string;
|
||||
constructor(directory?: string) {
|
||||
this.directory = resolve(
|
||||
directory ?? join(homedir(), ".config", "crossdesk", "mcp"),
|
||||
);
|
||||
this.file = join(this.directory, "devices.json");
|
||||
}
|
||||
async initialize() {
|
||||
await mkdir(this.directory, { recursive: true, mode: 0o700 });
|
||||
if (process.platform !== "win32") await chmod(this.directory, 0o700);
|
||||
await mkdir(join(this.directory, "logs"), { recursive: true, mode: 0o700 });
|
||||
}
|
||||
async config(): Promise<Config> {
|
||||
try {
|
||||
return parse(configSchema, JSON.parse(await readFile(this.file, "utf8")));
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === "ENOENT")
|
||||
return configSchema.parse({ version: 1, devices: [] });
|
||||
throw new Failure("CONFIG_INVALID");
|
||||
}
|
||||
}
|
||||
async list(): Promise<Data[]> {
|
||||
return (await this.config()).devices.map((d) => ({
|
||||
device_alias: d.alias,
|
||||
device_id: d.device_id,
|
||||
server: d.server,
|
||||
port: d.port,
|
||||
authorization: d.authorization ?? "none",
|
||||
credential_provider: d.credential ?? "none",
|
||||
}));
|
||||
}
|
||||
async device(name: string): Promise<Device> {
|
||||
const d = (await this.config()).devices.find((d) => d.alias === name);
|
||||
if (!d) throw new Failure("DEVICE_NOT_FOUND");
|
||||
return d;
|
||||
}
|
||||
private account(d: Device) {
|
||||
return createHash("sha256")
|
||||
.update(JSON.stringify([this.directory, d.server, d.port, d.device_id]))
|
||||
.digest("hex");
|
||||
}
|
||||
private async entry(d: Device) {
|
||||
try {
|
||||
const { AsyncEntry } = await import("@napi-rs/keyring");
|
||||
return new AsyncEntry("crossdesk-mcp", this.account(d), {
|
||||
linux: { store: "secret-service" },
|
||||
});
|
||||
} catch {
|
||||
throw new Failure("CREDENTIAL_STORE_UNAVAILABLE");
|
||||
}
|
||||
}
|
||||
async password(d: Device): Promise<string> {
|
||||
if (d.credential === "environment") {
|
||||
const secret =
|
||||
d.environment_variable && process.env[d.environment_variable];
|
||||
if (secret !== undefined) return secret;
|
||||
} else if (d.credential === "keyring") {
|
||||
try {
|
||||
const secret = await (await this.entry(d)).getPassword();
|
||||
if (secret !== null) return secret;
|
||||
} catch {
|
||||
throw new Failure("CREDENTIAL_STORE_UNAVAILABLE");
|
||||
}
|
||||
}
|
||||
throw new Failure("AUTH_REQUIRED");
|
||||
}
|
||||
async save(d: Device, password?: string) {
|
||||
parse(deviceSchema, d);
|
||||
const lock = await this.lock("config");
|
||||
try {
|
||||
const config = await this.config();
|
||||
const previous = config.devices.find((item) => item.alias === d.alias);
|
||||
if (
|
||||
previous &&
|
||||
(previous.device_id !== d.device_id ||
|
||||
previous.server !== d.server ||
|
||||
previous.port !== d.port)
|
||||
)
|
||||
throw new Failure("DEVICE_ALIAS_CONFLICT");
|
||||
if (password !== undefined) {
|
||||
try {
|
||||
await (await this.entry(d)).setPassword(password);
|
||||
d.credential = "keyring";
|
||||
delete d.environment_variable;
|
||||
} catch {
|
||||
throw new Failure("CREDENTIAL_STORE_UNAVAILABLE");
|
||||
}
|
||||
}
|
||||
config.devices = [
|
||||
...config.devices.filter((item) => item.alias !== d.alias),
|
||||
d,
|
||||
];
|
||||
parse(configSchema, config);
|
||||
const temporary = `${this.file}.${token()}.tmp`;
|
||||
try {
|
||||
await writeFile(temporary, JSON.stringify(config, null, 2) + "\n", {
|
||||
mode: 0o600,
|
||||
flag: "wx",
|
||||
});
|
||||
await rename(temporary, this.file);
|
||||
} finally {
|
||||
await unlink(temporary).catch(() => {});
|
||||
}
|
||||
} finally {
|
||||
await lock();
|
||||
}
|
||||
}
|
||||
async lock(name: string): Promise<() => Promise<void>> {
|
||||
const path = join(
|
||||
this.directory,
|
||||
`lock-${createHash("sha256").update(name).digest("hex").slice(0, 24)}`,
|
||||
);
|
||||
for (let attempt = 0; attempt < 2; ++attempt) {
|
||||
try {
|
||||
const handle = await open(path, "wx", 0o600);
|
||||
await handle.writeFile(String(process.pid));
|
||||
await handle.close();
|
||||
let released = false;
|
||||
return async () => {
|
||||
if (released) return;
|
||||
released = true;
|
||||
await unlink(path).catch(() => {});
|
||||
};
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code !== "EEXIST")
|
||||
throw new Failure("CONFIG_WRITE_FAILED");
|
||||
const pid = Number(await readFile(path, "utf8").catch(() => "0"));
|
||||
if (!Number.isSafeInteger(pid) || pid < 1)
|
||||
throw new Failure("DEVICE_BUSY");
|
||||
try {
|
||||
process.kill(pid, 0);
|
||||
throw new Failure("DEVICE_BUSY");
|
||||
} catch (e) {
|
||||
if ((e as NodeJS.ErrnoException).code !== "ESRCH")
|
||||
throw new Failure("DEVICE_BUSY");
|
||||
}
|
||||
await unlink(path).catch(() => {});
|
||||
}
|
||||
}
|
||||
throw new Failure("DEVICE_BUSY");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
import { createInterface } from "node:readline/promises";
|
||||
import { emitKeypressEvents } from "node:readline";
|
||||
import { Writable } from "node:stream";
|
||||
import { setTimeout as delay } from "node:timers/promises";
|
||||
import { Failure, failure, type Data } from "./common.js";
|
||||
import { TerminalClient } from "./terminal.js";
|
||||
|
||||
async function connect(client: TerminalClient) {
|
||||
let hidden = false;
|
||||
const output = new Writable({ write(chunk, _encoding, done) {
|
||||
if (!hidden) process.stdout.write(chunk);
|
||||
done();
|
||||
} });
|
||||
const rl = createInterface({ input: process.stdin, output, terminal: true, historySize: 0 });
|
||||
const ask = async (label: string) => (await rl.question(label)).trim();
|
||||
try {
|
||||
const { devices, defaults } = await client.call("devices");
|
||||
const status = await client.call("status");
|
||||
process.stdout.write("CrossDesk MCP · 终端画面\n");
|
||||
for (const d of devices)
|
||||
process.stdout.write(`${d.device_alias} (${d.device_id})\n`);
|
||||
const selected = (await ask(`设备别名或 9 位 ID${status.device_alias ? ` [${status.device_alias}]` : ""}:`)) || status.device_alias;
|
||||
const device = devices.find((d: Data) => d.device_alias === selected);
|
||||
const args: Data = device ? { device_alias: selected } : {
|
||||
device_id: selected,
|
||||
server: (await ask(`服务器 [${defaults.server}]:`)) || defaults.server,
|
||||
port: Number((await ask(`端口 [${defaults.port}]:`)) || defaults.port),
|
||||
};
|
||||
args.mode = (await ask("允许 Agent 操作?[y/N]:")).toLowerCase() === "y" ? "control" : "observe";
|
||||
args.display_index = Number((await ask("显示器索引 [0]:")) || "0");
|
||||
const saved = device?.credential_provider !== "none" && !!device;
|
||||
if (!saved || (await ask("使用已保存凭据?[Y/n]:")).toLowerCase() === "n") {
|
||||
process.stdout.write("访问密码(隐藏输入,可为空):");
|
||||
hidden = true;
|
||||
try { args.password = await rl.question(""); }
|
||||
finally { hidden = false; process.stdout.write("\n"); }
|
||||
}
|
||||
process.stdout.write("正在连接…\n");
|
||||
try { await client.call("connect", args); }
|
||||
finally { args.password = undefined; }
|
||||
} finally { rl.close(); }
|
||||
}
|
||||
|
||||
export async function terminalView(directory: string, view: string, cellAspect = 0.5) {
|
||||
if (!process.stdin.isTTY || !process.stdout.isTTY || process.env.TERM === "dumb")
|
||||
throw new Failure("VIEW_REQUIRES_TERMINAL");
|
||||
if (!Number.isFinite(cellAspect) || cellAspect < 0.2 || cellAspect > 2)
|
||||
throw new Failure("INVALID_ARGUMENT");
|
||||
const client = await TerminalClient.attach(directory, view);
|
||||
let raw = false, stopped = false, controls = false, message = "";
|
||||
const previousRaw = process.stdin.isRaw;
|
||||
const stop = () => { stopped = true; };
|
||||
const keypress = (_text: string, key: { name?: string; ctrl?: boolean; meta?: boolean; sequence?: string }) => {
|
||||
if (key.name === "q" || (key.ctrl && key.name === "c")) { stop(); return; }
|
||||
if (controls || key.ctrl || key.meta || !["p", "r", "d"].includes(key.sequence ?? "")) return;
|
||||
controls = true;
|
||||
void client.call("control", { operation: key.sequence === "p" ? "pause" : key.sequence === "r" ? "resume" : "disconnect" })
|
||||
.then(() => { message = ""; if (key.sequence === "d") stopped = true; })
|
||||
.catch((error) => { message = failure(error).code; })
|
||||
.finally(() => { controls = false; });
|
||||
};
|
||||
const write = (value: string) => new Promise<void>((resolve, reject) => {
|
||||
process.stdout.write(value, (error) => error ? reject(error) : resolve());
|
||||
});
|
||||
try {
|
||||
let status = await client.call("status");
|
||||
if (!status.session_id) await connect(client);
|
||||
process.stdin.setRawMode(true);
|
||||
raw = true;
|
||||
emitKeypressEvents(process.stdin);
|
||||
process.stdin.on("keypress", keypress);
|
||||
process.stdin.resume();
|
||||
for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"] as const) process.on(signal, stop);
|
||||
await write("\x1b[?1049h\x1b[?25l\x1b[?7l\x1b[2J");
|
||||
let statusAt = 0, size = "";
|
||||
const colors = /truecolor|24bit/i.test(process.env.COLORTERM ?? "") ? 24
|
||||
: /256color/i.test(process.env.TERM ?? "") ? 256 : 8;
|
||||
while (!stopped) {
|
||||
const started = Date.now();
|
||||
if (started - statusAt >= 500) {
|
||||
status = await client.call("status", {}, 7000);
|
||||
statusAt = Date.now();
|
||||
if (["closed", "failed", "disconnected"].includes(status.state)) break;
|
||||
}
|
||||
// Leave a spare column so terminals with unusual wrap behavior stay safe.
|
||||
const columns = Math.min(160, Math.max(1, (process.stdout.columns || 80) - 1));
|
||||
const rows = Math.min(60, Math.max(1, (process.stdout.rows || 24) - 2));
|
||||
const nextSize = `${columns}:${rows}`;
|
||||
let screen = nextSize !== size ? "\x1b[2J" : "";
|
||||
size = nextSize;
|
||||
try {
|
||||
const frame = await client.call("frame", { columns, rows, colors, cell_aspect: cellAspect }, 7000);
|
||||
if (frame.columns !== columns || frame.rows !== rows || !Array.isArray(frame.lines) || frame.lines.length !== rows)
|
||||
throw new Failure("INVALID_FRAME");
|
||||
// Only renderer-generated SGR sequences and single-width block glyphs.
|
||||
for (const [index, line] of frame.lines.entries()) {
|
||||
if (typeof line !== "string" || !/^(?:\x1b\[[0-9;]*m|[ \u2580-\u259f])*$/u.test(line))
|
||||
throw new Failure("INVALID_FRAME");
|
||||
screen += `\x1b[${index + 1};1H${line}\x1b[0m\x1b[K`;
|
||||
}
|
||||
const cursor = frame.cursor;
|
||||
if (cursor && Number.isInteger(cursor.column) && Number.isInteger(cursor.row) &&
|
||||
cursor.column >= 0 && cursor.column < columns && cursor.row >= 0 && cursor.row < rows)
|
||||
screen += `\x1b[${cursor.row + 1};${cursor.column + 1}H\x1b[97;44m↖\x1b[0m`;
|
||||
} catch (error) {
|
||||
if (!["NOT_READY", "BUSY", "FRAME_TIMEOUT", "STALE_OBSERVATION"].includes(failure(error).code)) throw error;
|
||||
}
|
||||
const label = status.human_paused ? "PAUSED" : status.control ? "AGENT CONTROL" : "OBSERVE";
|
||||
const bar = `${label} | p pause / r resume / d disconnect / q close view${message ? ` | ${message}` : ""}`;
|
||||
// No remote strings or credentials are interpolated into terminal control output.
|
||||
screen += `\x1b[${rows + 1};1H\x1b[0m${bar.replace(/[^\x20-\x7e]/g, "").slice(0, columns)}\x1b[K`;
|
||||
await write(screen);
|
||||
await delay(Math.max(0, 125 - (Date.now() - started)));
|
||||
}
|
||||
} finally {
|
||||
process.stdin.off("keypress", keypress);
|
||||
for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"] as const) process.off(signal, stop);
|
||||
if (raw) {
|
||||
process.stdin.setRawMode(previousRaw);
|
||||
process.stdin.pause();
|
||||
process.stdout.write("\x1b[0m\x1b[?7h\x1b[?25h\x1b[?1049l");
|
||||
}
|
||||
await client.call("detach", {}, 1000).catch(() => {});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
import { createServer, createConnection, type Server, type Socket } from "node:net";
|
||||
import { chmod, readFile, readdir, unlink, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { z } from "zod";
|
||||
import { Failure, equal, failure, id, parse, token, type Data } from "./common.js";
|
||||
import { Sessions, type View } from "./sessions.js";
|
||||
import { uiAction } from "./ui-actions.js";
|
||||
|
||||
const frameSchema = z.strictObject({
|
||||
columns: z.int().min(1).max(160),
|
||||
rows: z.int().min(1).max(60),
|
||||
colors: z.union([z.literal(8), z.literal(24), z.literal(256)]),
|
||||
cell_aspect: z.number().finite().min(0.2).max(2),
|
||||
});
|
||||
|
||||
// A private local channel, separate from MCP stdio and browser HTTP. Only the
|
||||
// user's terminal receives rendered frames and connection credentials.
|
||||
export class TerminalPanel {
|
||||
private server?: Server;
|
||||
private starting?: Promise<void>;
|
||||
private endpoint?: string;
|
||||
private descriptor?: string;
|
||||
private closed = false;
|
||||
private sockets = new Set<Socket>();
|
||||
private frames = new Set<string>();
|
||||
private frameTimes = new Map<string, number>();
|
||||
constructor(private sessions: Sessions) {}
|
||||
|
||||
async open(view: View): Promise<Data> {
|
||||
await this.start();
|
||||
for (const id of this.frameTimes.keys())
|
||||
if (!this.sessions.views.has(id)) this.frameTimes.delete(id);
|
||||
return {
|
||||
...this.sessions.describe(view),
|
||||
terminal_command: {
|
||||
command: "crossdesk-mcp",
|
||||
args: ["view", "--view", view.id, "--config-dir", this.sessions.store.directory],
|
||||
},
|
||||
next_step: "Show terminal_command to the user to run in a separate interactive terminal on this host. It displays the existing MCP session and accepts credentials locally. Never run it inside MCP stdio or capture its video into chat. Then wait with crossdesk_get_session(view_id, wait_ms: 25000).",
|
||||
};
|
||||
}
|
||||
|
||||
private async start() {
|
||||
if (this.closed) throw new Failure("PANEL_CLOSED");
|
||||
this.starting ??= (async () => {
|
||||
const secret = token(), suffix = token().slice(0, 20);
|
||||
this.endpoint = process.platform === "win32"
|
||||
? `\\\\.\\pipe\\crossdesk-terminal-${suffix}`
|
||||
: join(tmpdir(), `cd-term-${suffix}.sock`);
|
||||
this.server = createServer((socket) => {
|
||||
this.sockets.add(socket);
|
||||
const controller = new AbortController();
|
||||
socket.on("error", () => {});
|
||||
socket.once("close", () => {
|
||||
this.sockets.delete(socket);
|
||||
controller.abort();
|
||||
});
|
||||
socket.setTimeout(45000, () => socket.destroy());
|
||||
let buffer = Buffer.alloc(0), consumed = false;
|
||||
socket.on("data", (chunk: Buffer) => {
|
||||
if (consumed) return socket.destroy();
|
||||
buffer = Buffer.concat([buffer, chunk]);
|
||||
if (buffer.length > 65536) return socket.destroy();
|
||||
if (!buffer.includes(10)) return;
|
||||
consumed = true;
|
||||
void (async () => {
|
||||
try {
|
||||
const p = JSON.parse(buffer.toString("utf8"));
|
||||
buffer = Buffer.alloc(0);
|
||||
if (typeof p.secret !== "string" || !equal(secret, p.secret))
|
||||
throw new Failure("PANEL_UNAUTHORIZED");
|
||||
const view = this.sessions.view(parse(id, p.view_id));
|
||||
if (view.panel !== "terminal" || !["auto", "terminal"].includes(this.sessions.policy))
|
||||
throw new Failure("PANEL_POLICY_DENIED");
|
||||
view.lastSeen = Date.now();
|
||||
let result: Data;
|
||||
if (p.method === "frame") {
|
||||
const args = parse(frameSchema, p.args);
|
||||
if (this.frames.has(view.id) || Date.now() - (this.frameTimes.get(view.id) ?? 0) < 100)
|
||||
throw new Failure("BUSY", false, true);
|
||||
this.frames.add(view.id);
|
||||
this.frameTimes.set(view.id, Date.now());
|
||||
try { result = await this.sessions.terminalFrame(view, args, controller.signal); }
|
||||
finally { this.frames.delete(view.id); }
|
||||
} else if (p.method === "detach") {
|
||||
view.ready = false;
|
||||
result = this.sessions.describe(view);
|
||||
} else {
|
||||
result = await uiAction(this.sessions, view, p.method, p.args, controller.signal);
|
||||
}
|
||||
socket.end(JSON.stringify({ result }) + "\n");
|
||||
} catch (error) {
|
||||
socket.end(JSON.stringify({ error: failure(error) }) + "\n");
|
||||
}
|
||||
})();
|
||||
});
|
||||
});
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
this.server!.once("error", reject);
|
||||
this.server!.listen(this.endpoint!, resolve);
|
||||
});
|
||||
if (process.platform !== "win32") await chmod(this.endpoint, 0o600);
|
||||
this.descriptor = join(this.sessions.store.directory, `terminal-${process.pid}-${suffix}.json`);
|
||||
await writeFile(this.descriptor, JSON.stringify({ endpoint: this.endpoint, secret }), { mode: 0o600, flag: "wx" });
|
||||
if (this.closed) throw new Failure("PANEL_CLOSED");
|
||||
})().catch(async (error) => {
|
||||
await this.cleanup();
|
||||
this.starting = undefined;
|
||||
throw error;
|
||||
});
|
||||
return this.starting;
|
||||
}
|
||||
|
||||
private async cleanup() {
|
||||
for (const socket of this.sockets) socket.destroy();
|
||||
if (this.server?.listening) this.server.close();
|
||||
if (this.descriptor) await unlink(this.descriptor).catch(() => {});
|
||||
if (this.endpoint && process.platform !== "win32") await unlink(this.endpoint).catch(() => {});
|
||||
}
|
||||
async close() {
|
||||
this.closed = true;
|
||||
await this.starting?.catch(() => {});
|
||||
await this.cleanup();
|
||||
}
|
||||
}
|
||||
|
||||
export class TerminalClient {
|
||||
private constructor(private endpoint: string, private secret: string, private view: string) {}
|
||||
static async attach(directory: string, view: string) {
|
||||
parse(id, view);
|
||||
for (const file of await readdir(directory)) {
|
||||
if (!/^terminal-\d+-[a-zA-Z0-9_-]+\.json$/.test(file)) continue;
|
||||
try {
|
||||
const d = JSON.parse(await readFile(join(directory, file), "utf8"));
|
||||
const client = new TerminalClient(d.endpoint, d.secret, view);
|
||||
await client.call("ready", {}, 2000);
|
||||
return client;
|
||||
} catch { /* An unrelated or exited MCP instance. */ }
|
||||
}
|
||||
throw new Failure("PANEL_NOT_FOUND");
|
||||
}
|
||||
call(method: string, args: Data = {}, timeout = 45000): Promise<Data> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const socket = createConnection(this.endpoint);
|
||||
let buffer = Buffer.alloc(0), settled = false;
|
||||
const finish = (error?: Failure, result?: Data) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timer);
|
||||
socket.destroy();
|
||||
if (error) reject(error); else resolve(result!);
|
||||
};
|
||||
const timer = setTimeout(() => finish(new Failure("PANEL_TIMEOUT")), timeout);
|
||||
socket.on("error", () => finish(new Failure("PANEL_CLOSED")));
|
||||
socket.once("close", () => finish(new Failure("PANEL_CLOSED")));
|
||||
socket.once("connect", () => socket.write(JSON.stringify({ secret: this.secret, view_id: this.view, method, args }) + "\n"));
|
||||
socket.on("data", (chunk: Buffer) => {
|
||||
buffer = Buffer.concat([buffer, chunk]);
|
||||
if (buffer.length > 2 * 1024 * 1024) return finish(new Failure("MESSAGE_TOO_LARGE"));
|
||||
if (!buffer.includes(10)) return;
|
||||
try {
|
||||
const reply = JSON.parse(buffer.toString("utf8"));
|
||||
if (reply.error) finish(new Failure(reply.error.code));
|
||||
else if (reply.result && typeof reply.result === "object") finish(undefined, reply.result);
|
||||
else finish(new Failure("INVALID_RESPONSE"));
|
||||
} catch { finish(new Failure("INVALID_RESPONSE")); }
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { z } from "zod";
|
||||
import { parse, uiConnect, Failure, type Data } from "./common.js";
|
||||
import { Sessions, type View } from "./sessions.js";
|
||||
|
||||
export const uiSchemas = {
|
||||
ready: z.strictObject({}),
|
||||
devices: z.strictObject({}),
|
||||
status: z.strictObject({}),
|
||||
connect: uiConnect,
|
||||
video_open: z.strictObject({}),
|
||||
control: z.strictObject({
|
||||
operation: z.enum(["pause", "resume", "disconnect"]),
|
||||
}),
|
||||
};
|
||||
export type UIAction = keyof typeof uiSchemas;
|
||||
export async function uiAction(
|
||||
sessions: Sessions,
|
||||
view: View,
|
||||
name: string,
|
||||
args: unknown,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Data> {
|
||||
if (!Object.hasOwn(uiSchemas, name)) throw new Failure("UNKNOWN_ACTION");
|
||||
const p = parse(uiSchemas[name as UIAction], args) as Data;
|
||||
view.lastSeen = Date.now();
|
||||
switch (name) {
|
||||
case "ready":
|
||||
view.ready = true;
|
||||
return sessions.describe(view);
|
||||
case "devices": {
|
||||
const config = await sessions.store.config();
|
||||
return {
|
||||
devices: await sessions.store.list(),
|
||||
defaults: { server: config.server, port: config.port },
|
||||
};
|
||||
}
|
||||
case "status":
|
||||
return sessions.status(undefined, view.id, signal);
|
||||
case "connect":
|
||||
return sessions.connectUI(view, p, signal);
|
||||
case "control":
|
||||
return sessions.viewerControl(view, p.operation);
|
||||
default:
|
||||
throw new Failure("UNKNOWN_ACTION");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"strict": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"outDir": "dist",
|
||||
"rootDir": "src",
|
||||
"skipLibCheck": true,
|
||||
"lib": ["ES2022", "DOM"]
|
||||
},
|
||||
"include": ["src/**/*.ts"]
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Bundler",
|
||||
"strict": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"noEmit": true,
|
||||
"skipLibCheck": true,
|
||||
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
||||
"types": []
|
||||
},
|
||||
"include": ["ui/**/*.ts"]
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 7.5 KiB |
@@ -0,0 +1,301 @@
|
||||
type FrameInfo = { width: number; height: number; fps: number; frames: number };
|
||||
type Cursor = { type: "cursor"; visible: boolean; x: number; y: number;
|
||||
generation: number; geometry_epoch: number; width: number; height: number };
|
||||
function parameters(data: Uint8Array) {
|
||||
let codec = "", signature = "";
|
||||
for (let i = 0; i + 4 < data.length; i++) {
|
||||
if (data[i] || data[i + 1]) continue;
|
||||
const prefix = data[i + 2] === 1 ? 3 : data[i + 2] === 0 && data[i + 3] === 1 ? 4 : 0;
|
||||
if (!prefix) continue;
|
||||
const start = i + prefix, type = data[start]! & 31;
|
||||
if (type !== 7 && type !== 8) continue;
|
||||
let end = start + 1;
|
||||
while (end + 3 < data.length && !(data[end] === 0 && data[end + 1] === 0 &&
|
||||
(data[end + 2] === 1 || (data[end + 2] === 0 && data[end + 3] === 1)))) end++;
|
||||
if (end + 3 >= data.length) end = data.length;
|
||||
if (end - start > 65536) throw new Error("VIDEO_INVALID");
|
||||
const hex = [...data.subarray(start, end)].map((n) => n.toString(16).padStart(2, "0")).join("");
|
||||
signature += hex;
|
||||
if (type === 7 && end >= start + 4) codec = "avc1." + hex.slice(2, 8);
|
||||
i = end - 1;
|
||||
}
|
||||
return { codec, signature };
|
||||
}
|
||||
|
||||
export class VideoPlayer {
|
||||
private socket?: WebSocket;
|
||||
private decoder?: VideoDecoder;
|
||||
private stopped = false;
|
||||
private failed = false;
|
||||
private waitingKey = true;
|
||||
private generation = -1;
|
||||
private sequence = -1;
|
||||
private signature = "";
|
||||
private queue: ArrayBuffer[] = [];
|
||||
private queuedBytes = 0;
|
||||
private pumping = false;
|
||||
private keyRequested = 0;
|
||||
private firstFrame?: ReturnType<typeof setTimeout>;
|
||||
private latest?: VideoFrame;
|
||||
private drawing = 0;
|
||||
private count = 0;
|
||||
private samples = 0;
|
||||
private sampledAt = performance.now();
|
||||
private fps = 0;
|
||||
private raw = false;
|
||||
private software = false;
|
||||
private automatic = true;
|
||||
private cursor?: Cursor;
|
||||
private cursorDrawn = "";
|
||||
private resize?: ResizeObserver;
|
||||
constructor(private canvas: HTMLCanvasElement,
|
||||
private onFrame: (info: FrameInfo) => void,
|
||||
private onError: (error: Error) => void,
|
||||
private overlay?: HTMLCanvasElement) {
|
||||
if (overlay) {
|
||||
this.resize = new ResizeObserver(() => this.drawCursor());
|
||||
this.resize.observe(overlay);
|
||||
}
|
||||
}
|
||||
|
||||
start(url: string, ticket: string, protocol = "h264-annexb/1", decoder = "auto") {
|
||||
if (!["h264-annexb/1", "nv12/1"].includes(protocol) ||
|
||||
!["auto", "software"].includes(decoder)) throw new Error("VIDEO_INVALID");
|
||||
this.raw = protocol === "nv12/1";
|
||||
this.automatic = decoder === "auto";
|
||||
this.software = !this.automatic;
|
||||
if (typeof VideoFrame === "undefined" || (!this.raw && typeof VideoDecoder === "undefined"))
|
||||
throw new Error("VIDEO_UNSUPPORTED");
|
||||
if (!/^ws:\/\/127\.0\.0\.1:\d+\/api\/video$/.test(url)) throw new Error("VIDEO_INVALID");
|
||||
const socket = this.socket = new WebSocket(url);
|
||||
socket.binaryType = "arraybuffer";
|
||||
socket.onopen = () => socket.send(JSON.stringify({ ticket }));
|
||||
socket.onerror = () => this.fail(new Error("VIDEO_UNAVAILABLE"));
|
||||
socket.onclose = () => { if (!this.stopped) this.fail(new Error("VIDEO_UNAVAILABLE")); };
|
||||
this.firstFrame = setTimeout(() => this.fail(new Error("VIDEO_UNAVAILABLE")), 10000);
|
||||
socket.onmessage = (event) => {
|
||||
if (this.stopped) return;
|
||||
if (typeof event.data === "string") {
|
||||
try {
|
||||
if (event.data.length > 1024) throw new Error();
|
||||
const p = JSON.parse(event.data) as Cursor;
|
||||
if (p.type !== "cursor" || typeof p.visible !== "boolean" ||
|
||||
!Number.isFinite(p.x) || !Number.isFinite(p.y) || p.x < 0 || p.x > 1 || p.y < 0 || p.y > 1 ||
|
||||
!Number.isSafeInteger(p.generation) || p.generation < 0 ||
|
||||
!Number.isSafeInteger(p.geometry_epoch) || p.geometry_epoch < 0 ||
|
||||
!Number.isInteger(p.width) || !Number.isInteger(p.height) ||
|
||||
p.width < 0 || p.width > 8192 || p.height < 0 || p.height > 8192)
|
||||
throw new Error();
|
||||
this.cursor = p;
|
||||
this.drawCursor();
|
||||
} catch { this.fail(new Error("VIDEO_INVALID")); }
|
||||
return;
|
||||
}
|
||||
if (!(event.data instanceof ArrayBuffer)) return;
|
||||
const data = event.data;
|
||||
if (data.byteLength < (this.raw ? 38 : 25) ||
|
||||
data.byteLength > (this.raw ? 16777216 * 3 / 2 + 32 : 4 * 1024 * 1024 + 24))
|
||||
return this.fail(new Error("VIDEO_INVALID"));
|
||||
const header = new DataView(data);
|
||||
if (header.getUint32(0) !== (this.raw ? 0x43444e31 : 0x43445631))
|
||||
return this.fail(new Error("VIDEO_INVALID"));
|
||||
// Receipt, not presentation: bounds native writes even when the decoder
|
||||
// or the canvas is slow. This message goes directly to the native CLI.
|
||||
if (socket.readyState === WebSocket.OPEN) socket.send(JSON.stringify({
|
||||
type: "ack", generation: header.getUint32(4), sequence: header.getUint32(8),
|
||||
}));
|
||||
if (this.raw || this.queue.length >= 8 || this.queuedBytes + data.byteLength > 8 * 1024 * 1024) {
|
||||
this.queue = [];
|
||||
this.queuedBytes = 0;
|
||||
this.waitingKey = true;
|
||||
this.requestKey();
|
||||
}
|
||||
this.queue.push(data);
|
||||
this.queuedBytes += data.byteLength;
|
||||
void this.pump();
|
||||
};
|
||||
}
|
||||
private requestKey() {
|
||||
if (this.raw) return;
|
||||
if (Date.now() - this.keyRequested < 1000 || this.socket?.readyState !== WebSocket.OPEN) return;
|
||||
this.keyRequested = Date.now();
|
||||
this.socket.send(JSON.stringify({ type: "keyframe" }));
|
||||
}
|
||||
private async pump() {
|
||||
if (this.pumping) return;
|
||||
this.pumping = true;
|
||||
try {
|
||||
while (!this.stopped && this.queue.length) {
|
||||
const bytes = this.queue.shift()!;
|
||||
this.queuedBytes -= bytes.byteLength;
|
||||
const packet = new DataView(bytes);
|
||||
if (packet.getUint32(0) !== (this.raw ? 0x43444e31 : 0x43445631) || packet.getUint32(20) > 1)
|
||||
throw new Error("VIDEO_INVALID");
|
||||
const generation = packet.getUint32(4), sequence = packet.getUint32(8);
|
||||
const key = packet.getUint32(20) === 1;
|
||||
if (generation < this.generation) continue;
|
||||
const timestamp = Number(packet.getBigUint64(12));
|
||||
if (!Number.isSafeInteger(timestamp)) throw new Error("VIDEO_INVALID");
|
||||
if (this.raw) {
|
||||
const width = packet.getUint32(24), height = packet.getUint32(28);
|
||||
if (!key || !width || !height || (width & 1) || (height & 1) ||
|
||||
width > 8192 || height > 8192 || width * height > 16777216 ||
|
||||
bytes.byteLength !== 32 + width * height * 3 / 2)
|
||||
throw new Error("VIDEO_INVALID");
|
||||
if (generation === this.generation && sequence <= this.sequence) continue;
|
||||
this.generation = generation;
|
||||
this.sequence = sequence;
|
||||
// Match the native NV12 screenshot conversion's limited-range BT.601.
|
||||
this.present(new VideoFrame(new Uint8Array(bytes, 32), {
|
||||
format: "NV12", codedWidth: width, codedHeight: height, timestamp,
|
||||
colorSpace: { matrix: "smpte170m", primaries: "smpte170m", transfer: "smpte170m", fullRange: false },
|
||||
transfer: [bytes],
|
||||
} as VideoFrameBufferInit & { transfer: ArrayBuffer[] }));
|
||||
continue;
|
||||
}
|
||||
if (generation !== this.generation || (this.sequence >= 0 && sequence !== this.sequence + 1))
|
||||
this.waitingKey = true;
|
||||
this.generation = generation;
|
||||
this.sequence = sequence;
|
||||
if ((this.decoder?.decodeQueueSize ?? 0) >= 4) this.waitingKey = true;
|
||||
if (this.waitingKey && !key) { this.requestKey(); continue; }
|
||||
const data = new Uint8Array(bytes, 24);
|
||||
if (key) {
|
||||
const { codec, signature } = parameters(data);
|
||||
if (!codec) { this.waitingKey = true; this.requestKey(); continue; }
|
||||
if (!this.decoder || this.waitingKey || signature !== this.signature) {
|
||||
const config: VideoDecoderConfig = { codec, optimizeForLatency: true,
|
||||
hardwareAcceleration: this.software ? "prefer-software" : "prefer-hardware" };
|
||||
if (!(await VideoDecoder.isConfigSupported(config)).supported) {
|
||||
if (this.software) throw new Error("VIDEO_UNSUPPORTED");
|
||||
this.software = true;
|
||||
config.hardwareAcceleration = "prefer-software";
|
||||
if (!(await VideoDecoder.isConfigSupported(config)).supported) throw new Error("VIDEO_UNSUPPORTED");
|
||||
}
|
||||
if (this.stopped) break;
|
||||
if (this.decoder && this.decoder.state !== "closed") this.decoder.close();
|
||||
const decoder = new VideoDecoder({
|
||||
output: (frame) => {
|
||||
if (this.stopped || this.decoder !== decoder) { frame.close(); return; }
|
||||
this.present(frame);
|
||||
},
|
||||
error: () => {
|
||||
if (this.stopped || this.decoder !== decoder) return;
|
||||
this.decoder = undefined;
|
||||
if (this.software || !this.automatic) {
|
||||
this.fail(new Error("VIDEO_UNAVAILABLE"));
|
||||
return;
|
||||
}
|
||||
this.software = true;
|
||||
this.waitingKey = true;
|
||||
this.requestKey();
|
||||
},
|
||||
});
|
||||
this.decoder = decoder;
|
||||
decoder.configure(config);
|
||||
this.signature = signature;
|
||||
}
|
||||
this.waitingKey = false;
|
||||
}
|
||||
this.decoder?.decode(new EncodedVideoChunk({ type: key ? "key" : "delta", timestamp, data }));
|
||||
}
|
||||
} catch (error) {
|
||||
this.fail(error instanceof Error ? error : new Error("VIDEO_INVALID"));
|
||||
} finally { this.pumping = false; }
|
||||
}
|
||||
private present(frame: VideoFrame) {
|
||||
this.latest?.close();
|
||||
this.latest = frame;
|
||||
if (!this.drawing) this.drawing = requestAnimationFrame(() => this.draw());
|
||||
}
|
||||
private draw() {
|
||||
this.drawing = 0;
|
||||
const frame = this.latest;
|
||||
this.latest = undefined;
|
||||
if (!frame) return;
|
||||
try {
|
||||
if (this.stopped) return;
|
||||
const width = frame.displayWidth, height = frame.displayHeight;
|
||||
if (width > 8192 || height > 8192 || width * height > 16777216) throw new Error("VIDEO_INVALID");
|
||||
if (this.canvas.width !== width || this.canvas.height !== height) {
|
||||
this.canvas.width = width;
|
||||
this.canvas.height = height;
|
||||
}
|
||||
this.canvas.getContext("2d", { alpha: false })!.drawImage(frame, 0, 0);
|
||||
clearTimeout(this.firstFrame);
|
||||
this.count++;
|
||||
this.drawCursor();
|
||||
this.samples++;
|
||||
const elapsed = performance.now() - this.sampledAt;
|
||||
if (elapsed >= 1000) {
|
||||
this.fps = this.samples * 1000 / elapsed;
|
||||
this.samples = 0;
|
||||
this.sampledAt = performance.now();
|
||||
}
|
||||
this.canvas.dataset.frames = String(this.count);
|
||||
this.onFrame({ width, height, frames: this.count, fps: this.fps });
|
||||
} catch { this.fail(new Error("VIDEO_INVALID")); }
|
||||
finally { frame.close(); }
|
||||
}
|
||||
private drawCursor() {
|
||||
const overlay = this.overlay;
|
||||
if (!overlay) return;
|
||||
if (overlay.width !== this.canvas.width) overlay.width = this.canvas.width;
|
||||
if (overlay.height !== this.canvas.height) overlay.height = this.canvas.height;
|
||||
const p = this.cursor;
|
||||
const state = JSON.stringify([this.stopped, !!this.count, this.generation,
|
||||
overlay.width, overlay.height, overlay.clientWidth, overlay.clientHeight, p]);
|
||||
if (state === this.cursorDrawn) return;
|
||||
this.cursorDrawn = state;
|
||||
const context = overlay.getContext("2d")!;
|
||||
context.clearRect(0, 0, overlay.width, overlay.height);
|
||||
if (this.stopped || !this.count || !p?.visible || p.generation !== this.generation ||
|
||||
p.width !== overlay.width || p.height !== overlay.height) return;
|
||||
// Match the video canvas's object-fit: contain, including letterboxing.
|
||||
const scale = Math.min(overlay.clientWidth / overlay.width, overlay.clientHeight / overlay.height);
|
||||
if (!scale) return;
|
||||
context.save();
|
||||
context.translate(Math.min(overlay.width - 1, p.x * overlay.width), Math.min(overlay.height - 1, p.y * overlay.height));
|
||||
context.scale(1 / scale, 1 / scale);
|
||||
context.beginPath();
|
||||
context.moveTo(0, 0);
|
||||
context.lineTo(0, 22);
|
||||
context.lineTo(6, 17);
|
||||
context.lineTo(10, 26);
|
||||
context.lineTo(14, 24);
|
||||
context.lineTo(10, 15);
|
||||
context.lineTo(18, 15);
|
||||
context.closePath();
|
||||
context.fillStyle = "#3269e8";
|
||||
context.strokeStyle = "#fff";
|
||||
context.lineWidth = 2;
|
||||
context.lineJoin = "round";
|
||||
context.shadowColor = "#0009";
|
||||
context.shadowBlur = 3;
|
||||
context.fill();
|
||||
context.stroke();
|
||||
context.restore();
|
||||
}
|
||||
private fail(error: Error) {
|
||||
if (this.failed || this.stopped) return;
|
||||
this.failed = true;
|
||||
this.stop();
|
||||
this.onError(error);
|
||||
}
|
||||
stop() {
|
||||
this.stopped = true;
|
||||
this.cursor = undefined;
|
||||
this.resize?.disconnect();
|
||||
this.drawCursor();
|
||||
clearTimeout(this.firstFrame);
|
||||
this.socket?.close();
|
||||
if (this.decoder && this.decoder.state !== "closed") this.decoder.close();
|
||||
this.decoder = undefined;
|
||||
cancelAnimationFrame(this.drawing);
|
||||
this.drawing = 0;
|
||||
this.latest?.close();
|
||||
this.latest = undefined;
|
||||
this.queue = [];
|
||||
this.queuedBytes = 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
:root {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
|
||||
color: #292d33;
|
||||
background: #fff;
|
||||
font-size: 13px;
|
||||
color-scheme: light;
|
||||
--accent: #3269b7;
|
||||
--muted: #777d85;
|
||||
--line: #e4e6e9;
|
||||
}
|
||||
* { box-sizing: border-box; }
|
||||
html, body { width: 100%; height: 100%; margin: 0; }
|
||||
body { overflow: hidden; }
|
||||
[hidden] { display: none !important; }
|
||||
button, input, select { font: inherit; }
|
||||
button, summary, input, select { -webkit-tap-highlight-color: transparent; }
|
||||
:focus-visible { outline: 2px solid #7194c6; outline-offset: 2px; }
|
||||
#app { width: 100%; height: 100vh; height: 100dvh; display: flex; flex-direction: column; }
|
||||
.app-header { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 8px 16px; padding: 12px 20px; border-bottom: 1px solid var(--line); flex: none; }
|
||||
.brand, .header-right { display: flex; align-items: center; gap: 10px; min-width: 0; }
|
||||
.brand-logo { display: block; width: 28px; height: 28px; }
|
||||
h1 { margin: 0; font-size: 16px; font-weight: 600; line-height: 1.5; white-space: nowrap; }
|
||||
h2 { margin: 0; font-size: 13px; font-weight: 600; }
|
||||
.connection { color: var(--muted); font-size: 12px; white-space: nowrap; }
|
||||
.connection[data-state="online"] { color: #397557; }
|
||||
.connection[data-state="connecting"] { color: #8a682d; }
|
||||
.connection[data-state="offline"] { color: #a15149; }
|
||||
#details-toggle { display: none; }
|
||||
main { display: grid; grid-template-columns: 270px minmax(0, 1fr); flex: 1; min-height: 0; }
|
||||
aside { min-width: 0; min-height: 0; overflow: auto; overscroll-behavior: contain; padding: 22px 20px; border-right: 1px solid var(--line); }
|
||||
#sidebar-title { margin-bottom: 20px; }
|
||||
fieldset { border: 0; margin: 0; padding: 0; min-width: 0; }
|
||||
label { display: block; margin: 16px 0 7px; font-size: 12px; }
|
||||
fieldset > label:first-child { margin-top: 0; }
|
||||
.optional { color: var(--muted); margin-left: 4px; font-size: 11px; }
|
||||
input:not([type="checkbox"]), select { display: block; width: 100%; height: 36px; min-width: 0; border: 1px solid #d7dade; border-radius: 4px; padding: 7px 9px; background: #fff; color: inherit; }
|
||||
input:not([type="checkbox"]):focus, select:focus { border-color: var(--accent); outline: 1px solid var(--accent); }
|
||||
input:disabled, select:disabled { background: #f5f5f5; color: #999; }
|
||||
input::placeholder { color: #92969c; }
|
||||
#device-id, #session-device-id { font-variant-numeric: tabular-nums; }
|
||||
.password-field { position: relative; }
|
||||
.password-field input { padding-right: 48px; }
|
||||
.password-field button { position: absolute; right: 2px; top: 2px; height: 32px; min-height: 32px; padding: 0 8px; border: 0; background: transparent; color: var(--muted); }
|
||||
.field-hint, .privacy, .session-note { color: var(--muted); font-size: 11px; line-height: 1.7; margin: 7px 0 0; }
|
||||
.preferences { border-top: 1px solid var(--line); margin-top: 20px; padding-top: 7px; }
|
||||
details { font-size: 12px; }
|
||||
summary { padding: 9px 0; cursor: pointer; color: #666d76; }
|
||||
summary::marker { font-size: 10px; }
|
||||
.checks { padding-bottom: 10px; }
|
||||
.checks label { display: flex; align-items: flex-start; gap: 8px; margin-top: 13px; font-size: 12px; line-height: 1.5; cursor: pointer; }
|
||||
.checks label:has(input:disabled) { color: #999; cursor: default; }
|
||||
.checks small { display: block; margin-top: 2px; color: var(--muted); font-size: 11px; }
|
||||
input[type="checkbox"] { width: 14px; height: 14px; flex: none; margin: 2px 0 0; accent-color: var(--accent); }
|
||||
.row { display: flex; gap: 12px; }
|
||||
.row > div { flex: 1; min-width: 0; }
|
||||
button { display: inline-flex; align-items: center; justify-content: center; min-height: 32px; padding: 6px 10px; border: 1px solid #d9dce0; border-radius: 4px; background: #fff; color: #4d535c; font-size: 12px; cursor: pointer; white-space: nowrap; }
|
||||
button:not(:disabled):hover { background: #f3f4f5; border-color: #bfc4cb; }
|
||||
button:not(:disabled):active { background: #e9ebee; }
|
||||
button:disabled { cursor: default; color: #a3a7ad; background: #fafafa; border-color: #e7e9eb; }
|
||||
.primary { width: 100%; min-height: 36px; background: var(--accent); color: #fff; border-color: var(--accent); margin-top: 20px; }
|
||||
.primary:not(:disabled):hover { background: #285b9f; border-color: #285b9f; }
|
||||
.privacy { margin-top: 12px; }
|
||||
#error { flex: none; max-height: 25%; overflow: auto; padding: 10px 20px; background: #fff5f2; border-bottom: 1px solid #edddd6; color: #964a37; font-size: 12px; line-height: 1.6; overflow-wrap: anywhere; }
|
||||
.device-card strong { display: block; font-size: 14px; font-weight: 500; overflow-wrap: anywhere; }
|
||||
.device-card > span { display: block; margin-top: 5px; color: var(--muted); font-size: 12px; }
|
||||
.session-details { margin: 20px 0 0; font-size: 12px; }
|
||||
.session-details > div { display: flex; justify-content: space-between; gap: 12px; padding: 11px 0; border-bottom: 1px solid #eee; }
|
||||
.session-details dt { color: var(--muted); flex: none; }
|
||||
.session-details dd { margin: 0; text-align: right; overflow-wrap: anywhere; }
|
||||
#session-agent[data-paused="true"] { color: #956e2d; }
|
||||
.session-note { margin-top: 18px; }
|
||||
.workspace { display: flex; flex-direction: column; gap: 10px; min-width: 0; min-height: 0; overflow: auto; padding: 16px; background: #f6f7f8; }
|
||||
.viewer-card { display: flex; flex-direction: column; flex: 1; min-height: 160px; min-width: 0; background: #fff; border: 1px solid var(--line); border-radius: 5px; overflow: hidden; }
|
||||
.viewer-toolbar { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px 12px; padding: 9px 12px; flex: none; }
|
||||
.viewer-title { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
|
||||
.viewer-title h2 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
#video-state { color: var(--muted); font-size: 11px; white-space: nowrap; flex: none; }
|
||||
#video-state[data-state="stale"] { color: #956e2d; }
|
||||
.viewer-controls { display: flex; gap: 6px; flex-wrap: wrap; }
|
||||
.danger:not(:disabled) { color: #9a4e47; }
|
||||
.danger:not(:disabled):hover { border-color: #d6b4ae; background: #fff6f4; }
|
||||
/* The screen takes the remaining panel height; object-fit preserves the remote aspect ratio. */
|
||||
.screen { position: relative; flex: 1; min-height: 60px; width: 100%; display: flex; align-items: center; justify-content: center; overflow: hidden; isolation: isolate; border-radius: 8px; background: #17191c; }
|
||||
.screen canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
|
||||
#agent-cursor { pointer-events: none; z-index: 4; }
|
||||
/* Overlay the control indicator so toggling control never resizes the video. */
|
||||
.control-halo { display: none; }
|
||||
#app[data-controlling="true"] .screen::before,
|
||||
#app[data-controlling="true"] .screen::after { content: ""; position: absolute; inset: 0; border-radius: inherit; pointer-events: none; }
|
||||
#app[data-controlling="true"] .screen::before { z-index: 1; box-shadow: inset 0 0 10px #3976ff40, inset 0 0 3px #8ccaff70; }
|
||||
#app[data-controlling="true"] .control-halo {
|
||||
--halo-spread: 14px;
|
||||
display: block;
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
z-index: 2;
|
||||
overflow: hidden;
|
||||
border-radius: inherit;
|
||||
pointer-events: none;
|
||||
/* Fade the light inward at the edges, leaving the desktop itself clear. */
|
||||
mask-image:
|
||||
linear-gradient(to bottom, #000, #0007 4px, transparent var(--halo-spread), transparent calc(100% - var(--halo-spread)), #0007 calc(100% - 4px), #000),
|
||||
linear-gradient(to right, #000, #0007 4px, transparent var(--halo-spread), transparent calc(100% - var(--halo-spread)), #0007 calc(100% - 4px), #000);
|
||||
}
|
||||
.control-halo::before {
|
||||
content: "";
|
||||
position: absolute;
|
||||
inset: -22%;
|
||||
background:
|
||||
radial-gradient(ellipse 32% 38% at 24% 26%, #d5f3ff 0%, #76c4ff 20%, #427bff80 50%, transparent 100%),
|
||||
radial-gradient(ellipse 38% 40% at 78% 75%, #c4d7ff 0%, #637cff 24%, #436bff70 60%, transparent 100%),
|
||||
linear-gradient(125deg, #3c8aff70, #687dff50 55%, #4ab5ff80);
|
||||
filter: blur(8px);
|
||||
opacity: .9;
|
||||
/* Move a cached light layer rather than repainting an animated gradient. */
|
||||
animation: control-halo-drift 9s ease-in-out infinite alternate;
|
||||
will-change: transform, opacity;
|
||||
}
|
||||
#app[data-controlling="true"] .screen::after {
|
||||
z-index: 3;
|
||||
padding: 1px;
|
||||
background: linear-gradient(125deg, #dbf6ffb0, #5e98ff70 30%, #c2e9ffc0 53%, #7e9dff80 75%, #b0e6ffb0);
|
||||
-webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
|
||||
-webkit-mask-composite: xor;
|
||||
mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
|
||||
mask-composite: exclude;
|
||||
}
|
||||
@keyframes control-halo-drift {
|
||||
0% { transform: translate3d(-7%, -5%, 0) scale(1); opacity: .7; }
|
||||
50% { opacity: 1; }
|
||||
100% { transform: translate3d(7%, 5%, 0) scale(1.05); opacity: .85; }
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.control-halo::before { animation: none; will-change: auto; }
|
||||
}
|
||||
#empty { text-align: center; padding: 16px; max-width: 100%; color: #b4b7bd; }
|
||||
#empty h3 { font-size: 14px; font-weight: 400; margin: 0 0 8px; }
|
||||
#empty p { color: #7e848e; font-size: 12px; line-height: 1.7; margin: 0; }
|
||||
.stale { position: absolute; left: 12px; bottom: 12px; padding: 7px 10px; background: #25272cdd; color: #e2c495; border-radius: 3px; font-size: 11px; }
|
||||
.telemetry { display: flex; align-items: center; flex-wrap: wrap; gap: 7px 18px; padding: 9px 12px; flex: none; color: var(--muted); font-size: 11px; }
|
||||
.stream-metrics { display: flex; gap: 14px; font-variant-numeric: tabular-nums; }
|
||||
#permission { margin-left: auto; }
|
||||
#permission[data-state="paused"] { color: #956e2d; }
|
||||
.panel-status { flex: none; font-size: 12px; line-height: 1.6; color: var(--muted); margin: 0; }
|
||||
.video-options { display: flex; flex-wrap: wrap; gap: 8px; flex: none; }
|
||||
.video-options:not(:has(button:not([hidden]))) { display: none; }
|
||||
.focus-mode main { grid-template-columns: minmax(0, 1fr); }
|
||||
.focus-mode .app-header { display: none; }
|
||||
.focus-mode aside, .focus-mode #details-toggle { display: none !important; }
|
||||
@media (max-width: 760px) {
|
||||
.app-header { padding: 10px 14px; }
|
||||
main { display: flex; flex-direction: column; }
|
||||
aside { flex: 1; border-right: 0; padding: 20px; }
|
||||
#app:not([data-connected="true"]) .workspace { display: none; }
|
||||
#app[data-connected="true"] aside { display: none; flex: none; max-height: 40%; border-bottom: 1px solid var(--line); }
|
||||
#app[data-connected="true"].details-open aside { display: block; }
|
||||
#app[data-connected="true"] #details-toggle { display: inline-flex; }
|
||||
#app[data-connected="true"] .workspace { flex: 1; }
|
||||
.workspace { padding: 10px; }
|
||||
#sidebar-title { margin-bottom: 16px; }
|
||||
.session-details { margin-top: 12px; }
|
||||
}
|
||||
@media (max-width: 480px) {
|
||||
#app[data-controlling="true"] .control-halo { --halo-spread: 10px; }
|
||||
.app-header { gap: 8px; padding: 10px 12px; }
|
||||
.brand, .header-right { gap: 7px; }
|
||||
.brand-logo { width: 24px; height: 24px; }
|
||||
h1 { font-size: 15px; }
|
||||
.connection { font-size: 11px; }
|
||||
#details-toggle { padding: 5px 7px; min-height: 28px; font-size: 11px; }
|
||||
.workspace { padding: 8px; gap: 8px; }
|
||||
.viewer-toolbar { padding: 9px; }
|
||||
.viewer-title, .viewer-controls { width: 100%; flex: auto; }
|
||||
.viewer-controls button { flex: 1; min-height: 36px; }
|
||||
.telemetry { padding: 8px 9px; gap: 6px 10px; }
|
||||
#session-info { display: none; }
|
||||
.stream-metrics { gap: 10px; }
|
||||
#permission { font-size: 10px; }
|
||||
}
|
||||
@media (max-height: 460px) and (min-width: 481px) {
|
||||
.app-header { padding-top: 6px; padding-bottom: 6px; }
|
||||
.workspace { padding: 8px; gap: 6px; }
|
||||
.viewer-toolbar { padding-top: 5px; padding-bottom: 5px; }
|
||||
.telemetry { padding-top: 6px; padding-bottom: 6px; }
|
||||
}
|
||||
@@ -0,0 +1,547 @@
|
||||
import type { App } from "@modelcontextprotocol/ext-apps";
|
||||
import "./viewer.css";
|
||||
import { VideoPlayer } from "./video-player.js";
|
||||
|
||||
declare const CROSSDESK_APPS: boolean;
|
||||
declare const CROSSDESK_LOGO: string;
|
||||
type Data = Record<string, any>;
|
||||
type Invoke = (name: string, args?: Data) => Promise<Data>;
|
||||
const root = document.querySelector<HTMLDivElement>("#app")!;
|
||||
root.innerHTML = `
|
||||
<header class="app-header">
|
||||
<div class="brand"><img class="brand-logo" src="${CROSSDESK_LOGO}" alt="" width="28" height="28"><h1>CrossDesk MCP</h1></div>
|
||||
<div class="header-right"><button id="details-toggle" aria-expanded="false" aria-controls="sidebar">连接信息</button><span class="connection" id="connection" role="status"><span id="connection-label">等待连接</span></span></div>
|
||||
</header>
|
||||
<div id="error" role="alert" hidden></div>
|
||||
<main>
|
||||
<aside id="sidebar" aria-labelledby="sidebar-title">
|
||||
<h2 id="sidebar-title">连接设备</h2>
|
||||
<div id="connect-form" role="form" aria-label="连接远程电脑">
|
||||
<fieldset id="connection-fields">
|
||||
<label for="device">设备</label><select id="device"><option value="">连接新设备</option></select>
|
||||
<div id="new-device">
|
||||
<label for="device-id">设备 ID</label><input id="device-id" inputmode="numeric" autocomplete="off" placeholder="输入 9 位设备 ID" maxlength="11">
|
||||
</div>
|
||||
<label for="password">访问密码</label><div class="password-field"><input id="password" type="password" autocomplete="off" maxlength="256" placeholder="输入远端访问密码"><button type="button" id="password-toggle" aria-label="显示密码" aria-pressed="false">显示</button></div>
|
||||
<label for="mode">授权范围</label><select id="mode" aria-describedby="mode-description"><option value="observe">仅查看画面</option><option value="control">允许 Agent 操作</option></select>
|
||||
<p class="field-hint" id="mode-description">Agent 可以查看屏幕,无法操作键鼠。</p>
|
||||
<details class="preferences"><summary>保存与授权</summary>
|
||||
<div id="new-device-label"><label for="label">设备备注 <span class="optional">可选</span></label><input id="label" placeholder="例如:工作电脑" maxlength="80"></div>
|
||||
<div class="checks">
|
||||
<label><input id="save" type="checkbox"><span>保存设备</span></label>
|
||||
<label><input id="remember" type="checkbox" disabled><span>记住密码<small>保存在本机系统凭据库</small></span></label>
|
||||
<label><input id="authorize" type="checkbox" disabled><span>允许下次自动连接<small>沿用本次授权范围</small></span></label>
|
||||
</div>
|
||||
</details>
|
||||
<details id="advanced"><summary>高级设置</summary>
|
||||
<label for="server">服务器</label><input id="server" value="api.crossdesk.cn" autocomplete="off">
|
||||
<div class="row"><div><label for="port">端口</label><input id="port" type="number" min="1" max="65535" value="9099"></div><div><label for="display">显示器索引</label><input id="display" type="number" min="0" max="63" value="0"></div></div>
|
||||
</details>
|
||||
</fieldset>
|
||||
<button id="connect" type="button" class="primary" disabled><span id="connect-label">连接</span></button>
|
||||
<p class="privacy">密码不会发送到 Agent 对话中。</p>
|
||||
</div>
|
||||
<div id="session-card" hidden>
|
||||
<div class="device-card"><strong id="session-device">远程电脑</strong><span id="session-platform">已连接</span></div>
|
||||
<dl class="session-details"><div id="session-device-id-row" hidden><dt>设备 ID</dt><dd id="session-device-id"></dd></div><div><dt>访问权限</dt><dd id="session-access"></dd></div><div><dt>显示器</dt><dd id="session-display"></dd></div><div><dt>Agent 状态</dt><dd id="session-agent"></dd></div></dl>
|
||||
<p class="session-note">关闭页面不会断开连接。</p>
|
||||
</div>
|
||||
</aside>
|
||||
<section class="workspace" aria-label="远程画面与会话控制">
|
||||
<div class="viewer-card">
|
||||
<div class="viewer-toolbar">
|
||||
<div class="viewer-title"><h2 id="device-name">远程画面</h2><span id="video-state">待连接</span></div>
|
||||
<div class="viewer-controls"><button id="pause" disabled><span id="pause-label">暂停控制</span></button><button id="disconnect" class="danger" disabled>断开连接</button><button id="focus-view" aria-label="放大画面" aria-pressed="false" disabled>放大画面</button></div>
|
||||
</div>
|
||||
<div class="screen" id="screen">
|
||||
<canvas id="frame" role="img" aria-label="远程桌面实时画面" hidden></canvas>
|
||||
<canvas id="agent-cursor" aria-hidden="true"></canvas>
|
||||
<div class="control-halo" aria-hidden="true"></div>
|
||||
<div id="empty"><h3 id="empty-title">未连接设备</h3><p id="empty-description">选择设备并输入访问密码。</p></div>
|
||||
<div class="stale" id="stale" role="status" hidden>画面暂未更新</div>
|
||||
</div>
|
||||
<div class="telemetry"><div class="stream-metrics"><span id="resolution">—</span><span id="fps">—</span></div><span id="session-info">等待设备连接</span><span id="permission">等待授权</span></div>
|
||||
</div>
|
||||
<div class="video-options"><button id="video-retry" hidden>重新加载画面</button><button id="video-browser" hidden>在浏览器中观看</button></div>
|
||||
<p id="panel-status" class="panel-status" role="status" hidden></p>
|
||||
</section>
|
||||
</main>`;
|
||||
const element = <T extends HTMLElement = HTMLElement>(id: string) =>
|
||||
document.getElementById(id) as T;
|
||||
const input = (id: string) => element<HTMLInputElement>(id);
|
||||
const select = (id: string) => element<HTMLSelectElement>(id);
|
||||
let invoke: Invoke;
|
||||
let app: App | undefined;
|
||||
let context: Data | undefined;
|
||||
let status: Data = {};
|
||||
let savedDevices: Data[] = [];
|
||||
let controlBusy = false;
|
||||
let disconnectedByUser = false;
|
||||
let frameSize = "", frameRate = "";
|
||||
let player: VideoPlayer | undefined;
|
||||
let videoStarting = false, videoFailed = false, videoEpoch = 0;
|
||||
let videoSession: string | undefined;
|
||||
let videoGeneration: number | undefined;
|
||||
let imageAt = 0;
|
||||
let busy = false,
|
||||
polling = false,
|
||||
started = false;
|
||||
const messages: Record<string, string> = {
|
||||
VIDEO_UNSUPPORTED: "当前浏览器不支持此视频格式,请使用支持 WebCodecs 的浏览器。",
|
||||
VIDEO_UNAVAILABLE: "视频通道暂不可用。可重新加载画面;内嵌面板受限时请在浏览器中观看。",
|
||||
VIDEO_INVALID: "视频格式发生变化,请重新加载画面。",
|
||||
VIDEO_LIMIT: "观看面板数量已达上限,请关闭其他面板。",
|
||||
AUTH_FAILED: "访问密码错误,请重新输入。",
|
||||
AUTH_REQUIRED: "请输入访问密码,或在本机 setup 中配置凭据。",
|
||||
CONNECTION_FAILED: "连接失败,请检查设备是否在线及服务器设置。",
|
||||
CONNECTION_TIMEOUT: "等待远端画面超时,请检查远端状态后重试。",
|
||||
CONNECTION_LOST: "远程连接已中断。请检查远端状态并重新连接。",
|
||||
SESSION_LIMIT: "已有活动连接,请先断开当前电脑。",
|
||||
DEVICE_BUSY: "另一实例正在使用此设备。",
|
||||
CREDENTIAL_STORE_UNAVAILABLE: "系统凭据库不可用,密码未保存。",
|
||||
PAIRING_REQUIRED:
|
||||
"请在终端运行 Agent 工具结果中的 setup --open-panel 配对命令,再打开面板。",
|
||||
PANEL_UNAUTHORIZED: "面板授权已失效,请重新打开。",
|
||||
VIEW_UNAUTHORIZED: "此面板已失效,请使用最新打开的面板。",
|
||||
CONTROL_DENIED: "当前会话没有控制权限,或已被人工暂停。",
|
||||
NATIVE_PACKAGE_MISSING:
|
||||
"缺少对应平台的 CLI 运行组件,请检查包安装是否启用了 optional dependencies。",
|
||||
INVALID_ARGUMENT: "请检查设备 ID、服务器和输入内容。",
|
||||
DEVICE_ALIAS_CONFLICT: "这个备注已属于另一台设备,请换一个备注。",
|
||||
};
|
||||
function text(id: string, value: string) {
|
||||
if (element(id).textContent !== value) element(id).textContent = value;
|
||||
}
|
||||
function platformName(platform?: string) {
|
||||
return ({ macos: "macOS", windows: "Windows", linux: "Linux", android: "Android", ios: "iOS" } as Record<string, string>)[platform ?? ""] ?? "远程电脑";
|
||||
}
|
||||
function resetMetrics() {
|
||||
imageAt = 0;
|
||||
frameSize = frameRate = "";
|
||||
text("resolution", "—");
|
||||
text("fps", "—");
|
||||
element<HTMLButtonElement>("focus-view").disabled = true;
|
||||
}
|
||||
function setFocus(focused: boolean) {
|
||||
root.classList.toggle("focus-mode", focused);
|
||||
const button = element<HTMLButtonElement>("focus-view");
|
||||
const label = focused ? "还原布局" : "放大画面";
|
||||
button.textContent = label;
|
||||
if (focused) setDetails(false);
|
||||
button.setAttribute("aria-pressed", String(focused));
|
||||
button.setAttribute("aria-label", label);
|
||||
button.title = label;
|
||||
}
|
||||
function setDetails(open: boolean) {
|
||||
root.classList.toggle("details-open", open);
|
||||
element("details-toggle").setAttribute("aria-expanded", String(open));
|
||||
text("details-toggle", open ? "收起信息" : "连接信息");
|
||||
}
|
||||
element("details-toggle").onclick = () => setDetails(!root.classList.contains("details-open"));
|
||||
element("focus-view").onclick = () => setFocus(!root.classList.contains("focus-mode"));
|
||||
window.addEventListener("keydown", (event) => {
|
||||
if (event.key === "Escape" && root.classList.contains("focus-mode")) setFocus(false);
|
||||
});
|
||||
function hidePassword() {
|
||||
input("password").type = "password";
|
||||
text("password-toggle", "显示");
|
||||
element("password-toggle").setAttribute("aria-label", "显示密码");
|
||||
element("password-toggle").setAttribute("aria-pressed", "false");
|
||||
}
|
||||
element("password-toggle").onclick = () => {
|
||||
if (input("password").type === "text") return hidePassword();
|
||||
input("password").type = "text";
|
||||
text("password-toggle", "隐藏");
|
||||
element("password-toggle").setAttribute("aria-label", "隐藏密码");
|
||||
element("password-toggle").setAttribute("aria-pressed", "true");
|
||||
};
|
||||
select("mode").onchange = () => {
|
||||
text("mode-description", select("mode").value === "control"
|
||||
? "Agent 可以操作键鼠,你可以随时暂停。"
|
||||
: "Agent 可以查看屏幕,无法操作键鼠。");
|
||||
};
|
||||
function showError(error: unknown) {
|
||||
const code = error instanceof Error ? error.message : "INTERNAL_ERROR";
|
||||
element("error").textContent =
|
||||
messages[code] ??
|
||||
`操作未完成(${/^[A-Z_]+$/.test(code) ? code : "CONNECTION_ERROR"})`;
|
||||
element("error").hidden = false;
|
||||
}
|
||||
function check(result: Data): Data {
|
||||
if (result.error) throw new Error(result.error.code ?? "INTERNAL_ERROR");
|
||||
return result;
|
||||
}
|
||||
async function devices() {
|
||||
const result = await invoke("devices");
|
||||
if (!started && result.defaults) {
|
||||
input("server").value = result.defaults.server;
|
||||
input("port").value = String(result.defaults.port);
|
||||
}
|
||||
const chosen = select("device").value || context?.device_alias || "";
|
||||
select("device").replaceChildren(new Option("连接新设备", ""));
|
||||
savedDevices = result.devices ?? [];
|
||||
for (const d of savedDevices)
|
||||
select("device").add(
|
||||
new Option(`${d.device_alias} · ${d.device_id}`, d.device_alias),
|
||||
);
|
||||
select("device").value = [...select("device").options].some(
|
||||
(o) => o.value === chosen,
|
||||
)
|
||||
? chosen
|
||||
: "";
|
||||
deviceChanged();
|
||||
}
|
||||
function deviceChanged() {
|
||||
const saved = !!select("device").value;
|
||||
element("new-device").hidden = saved;
|
||||
element("new-device-label").hidden = saved;
|
||||
input("server").disabled = input("port").disabled = saved;
|
||||
input("password").placeholder = saved
|
||||
? "留空使用已保存的密码"
|
||||
: "输入远端访问密码";
|
||||
}
|
||||
select("device").onchange = deviceChanged;
|
||||
input("save").onchange = () => {
|
||||
for (const name of ["remember", "authorize"]) {
|
||||
input(name).disabled = !input("save").checked;
|
||||
if (!input("save").checked) input(name).checked = false;
|
||||
}
|
||||
};
|
||||
function renderStatus(s: Data) {
|
||||
const connected = s.state === "ready" || s.state === "waiting_frame";
|
||||
const lost = ["closed", "failed"].includes(s.state);
|
||||
if (lost && !disconnectedByUser && ["ready", "waiting_frame"].includes(status.state)) showError(new Error("CONNECTION_LOST"));
|
||||
const changedSession = s.session_id !== status.session_id;
|
||||
status = s;
|
||||
const connecting = busy || s.state === "waiting_frame";
|
||||
root.dataset.connected = String(connected);
|
||||
root.dataset.connecting = String(connecting);
|
||||
root.dataset.controlling = String(connected && s.control === true && s.control_allowed === true && !s.human_paused);
|
||||
element("connection").dataset.state = connected ? "online" : connecting ? "connecting" : lost && !disconnectedByUser ? "offline" : "idle";
|
||||
text("connection-label", connected ? "已连接" : connecting ? "正在连接" : lost ? "未连接" : "等待连接");
|
||||
element("connect-form").hidden = connected;
|
||||
element("session-card").hidden = !connected;
|
||||
text("sidebar-title", connected ? "连接信息" : "连接设备");
|
||||
text("device-name", connected ? s.device_alias || "远程电脑" : "远程画面");
|
||||
if (!connected || changedSession) setDetails(false);
|
||||
text("permission", connected ? s.human_paused ? "控制已暂停" : s.control_allowed ? "允许 Agent 操作" : "仅查看画面" : "等待授权");
|
||||
element("permission").dataset.state = !connected ? "idle" : s.human_paused ? "paused" : "active";
|
||||
const platform = platformName(s.platform);
|
||||
text("session-info", connected ? `${platform} · 显示器 ${(s.display ?? 0) + 1}` : "等待设备连接");
|
||||
text("session-device", s.device_alias || "远程电脑");
|
||||
text("session-platform", platform);
|
||||
const device = savedDevices.find((d) => d.device_alias === s.device_alias);
|
||||
element("session-device-id-row").hidden = !device;
|
||||
text("session-device-id", device ? String(device.device_id).replace(/(\d{3})(?=\d)/g, "$1 ") : "");
|
||||
text("session-access", s.control_allowed ? "查看与操作" : "仅查看");
|
||||
text("session-display", `显示器 ${(s.display ?? 0) + 1}`);
|
||||
text("session-agent", s.human_paused ? "已暂停" : s.control_allowed ? "允许操作" : "仅可观察");
|
||||
element("session-agent").dataset.paused = String(!!s.human_paused);
|
||||
element<HTMLButtonElement>("pause").disabled = !connected || !s.control_allowed || controlBusy;
|
||||
const pauseLabel = s.human_paused ? "恢复控制" : "暂停控制";
|
||||
text("pause-label", pauseLabel);
|
||||
element<HTMLButtonElement>("disconnect").disabled = !s.session_id || controlBusy || busy;
|
||||
element<HTMLButtonElement>("connect").disabled = busy || connected || !invoke;
|
||||
element<HTMLFieldSetElement>("connection-fields").disabled = busy;
|
||||
element("connect").setAttribute("aria-busy", String(busy));
|
||||
text("connect-label", busy ? "正在连接…" : "连接");
|
||||
if (!connected) {
|
||||
element("frame").hidden = true;
|
||||
element("empty").hidden = false;
|
||||
element("stale").hidden = true;
|
||||
resetMetrics();
|
||||
if (root.classList.contains("focus-mode")) setFocus(false);
|
||||
} else if (changedSession) {
|
||||
resetMetrics();
|
||||
element("frame").hidden = true;
|
||||
element("empty").hidden = false;
|
||||
}
|
||||
if (!imageAt && !videoFailed) {
|
||||
text("empty-title", connecting || connected ? "正在建立远程连接" : lost ? "会话已结束" : "未连接设备");
|
||||
text("empty-description", connecting || connected ? "正在等待远端画面,请稍候。" : lost ? "请重新连接设备。" : "选择设备并输入访问密码。");
|
||||
text("video-state", connecting || connected ? "等待画面" : "待连接");
|
||||
element("video-state").dataset.state = "idle";
|
||||
}
|
||||
}
|
||||
async function connect() {
|
||||
if (!invoke || busy || ["ready", "waiting_frame"].includes(status.state)) return;
|
||||
element("error").hidden = true;
|
||||
disconnectedByUser = false;
|
||||
busy = true;
|
||||
renderStatus(status);
|
||||
const saved = select("device").value;
|
||||
let args: Data = {
|
||||
mode: select("mode").value,
|
||||
display_index: Number(input("display").value),
|
||||
save_device: input("save").checked,
|
||||
remember_password: input("remember").checked,
|
||||
persistent_authorization: input("authorize").checked,
|
||||
...(saved
|
||||
? { device_alias: saved }
|
||||
: {
|
||||
device_id: input("device-id").value.replace(/\s/g, ""),
|
||||
server: input("server").value,
|
||||
port: Number(input("port").value),
|
||||
...(input("label").value ? { label: input("label").value } : {}),
|
||||
}),
|
||||
};
|
||||
if (!saved || input("password").value)
|
||||
args.password = input("password").value;
|
||||
input("password").value = "";
|
||||
hidePassword();
|
||||
try {
|
||||
const result = await invoke("connect", args);
|
||||
args = {};
|
||||
renderStatus(result);
|
||||
if (result.persistence_error)
|
||||
showError(new Error(result.persistence_error));
|
||||
await devices();
|
||||
} catch (error) {
|
||||
args = {};
|
||||
showError(error);
|
||||
} finally {
|
||||
busy = false;
|
||||
renderStatus(status);
|
||||
}
|
||||
}
|
||||
element("connect").onclick = () => {
|
||||
void connect();
|
||||
};
|
||||
element("connect-form").addEventListener("keydown", (event) => {
|
||||
if (event.key === "Enter" && event.target instanceof HTMLInputElement) {
|
||||
event.preventDefault();
|
||||
void connect();
|
||||
}
|
||||
});
|
||||
element("pause").onclick = async () => {
|
||||
if (controlBusy) return;
|
||||
controlBusy = true;
|
||||
renderStatus(status);
|
||||
try {
|
||||
await invoke("control", {
|
||||
operation: status.human_paused ? "resume" : "pause",
|
||||
});
|
||||
renderStatus(await invoke("status"));
|
||||
} catch (e) {
|
||||
showError(e);
|
||||
} finally {
|
||||
controlBusy = false;
|
||||
renderStatus(status);
|
||||
}
|
||||
};
|
||||
element("disconnect").onclick = async () => {
|
||||
if (controlBusy) return;
|
||||
controlBusy = true;
|
||||
disconnectedByUser = true;
|
||||
renderStatus(status);
|
||||
try {
|
||||
await closePreview();
|
||||
await invoke("control", { operation: "disconnect" });
|
||||
element("error").hidden = true;
|
||||
resetMetrics();
|
||||
renderStatus(await invoke("status"));
|
||||
} catch (e) {
|
||||
disconnectedByUser = false;
|
||||
showError(e);
|
||||
} finally {
|
||||
controlBusy = false;
|
||||
renderStatus(status);
|
||||
}
|
||||
};
|
||||
async function closePreview() {
|
||||
++videoEpoch;
|
||||
player?.stop();
|
||||
player = undefined;
|
||||
videoSession = undefined;
|
||||
videoGeneration = undefined;
|
||||
videoStarting = videoFailed = false;
|
||||
element("video-retry").hidden = true;
|
||||
element("video-browser").hidden = true;
|
||||
}
|
||||
async function startPreview() {
|
||||
if (player || videoStarting || videoFailed || document.hidden) return;
|
||||
const epoch = ++videoEpoch;
|
||||
videoSession = status.session_id;
|
||||
videoGeneration = status.generation;
|
||||
videoStarting = true;
|
||||
const failed = (error: Error) => {
|
||||
if (epoch !== videoEpoch) return;
|
||||
videoFailed = true;
|
||||
showError(error);
|
||||
element("video-retry").hidden = false;
|
||||
element("video-browser").hidden = !CROSSDESK_APPS;
|
||||
element("stale").hidden = !imageAt;
|
||||
text("video-state", "画面不可用");
|
||||
element("video-state").dataset.state = "stale";
|
||||
if (!imageAt) {
|
||||
text("empty-title", "暂时无法显示画面");
|
||||
text("empty-description", "会话仍然保留。请尝试重新加载画面。");
|
||||
}
|
||||
};
|
||||
try {
|
||||
const grant = await invoke("video_open");
|
||||
if (epoch !== videoEpoch || document.hidden) return;
|
||||
player = new VideoPlayer(element<HTMLCanvasElement>("frame"), (info) => {
|
||||
if (epoch !== videoEpoch) return;
|
||||
imageAt = Date.now();
|
||||
element("frame").hidden = false;
|
||||
element("empty").hidden = true;
|
||||
element("stale").hidden = true;
|
||||
const size = `${info.width} × ${info.height}`;
|
||||
const rate = info.fps > 0 ? `${info.fps.toFixed(1)} FPS` : "测量中";
|
||||
if (size !== frameSize) { frameSize = size; text("resolution", size); }
|
||||
if (rate !== frameRate) { frameRate = rate; text("fps", rate); }
|
||||
element<HTMLButtonElement>("focus-view").disabled = false;
|
||||
text("video-state", "实时");
|
||||
element("video-state").dataset.state = "live";
|
||||
}, failed, element<HTMLCanvasElement>("agent-cursor"));
|
||||
player.start(grant.url, grant.ticket, grant.protocol, grant.decoder);
|
||||
} catch (error) {
|
||||
failed(error instanceof Error ? error : new Error("VIDEO_UNAVAILABLE"));
|
||||
} finally {
|
||||
if (epoch === videoEpoch) videoStarting = false;
|
||||
}
|
||||
}
|
||||
element("video-retry").onclick = async () => {
|
||||
await closePreview();
|
||||
element("error").hidden = true;
|
||||
await startPreview();
|
||||
};
|
||||
element("video-browser").onclick = async () => {
|
||||
try {
|
||||
const result = await app!.callServerTool({name: "crossdesk_open_browser", arguments: {view_id: context!.view_id}});
|
||||
if (result.isError) throw new Error("VIDEO_UNAVAILABLE");
|
||||
started = false;
|
||||
await closePreview();
|
||||
element("error").hidden = true;
|
||||
for (const name of ["connect", "pause", "disconnect"])
|
||||
element<HTMLButtonElement>(name).disabled = true;
|
||||
element("panel-status").hidden = false;
|
||||
element("panel-status").textContent = "浏览器观看面板已打开。";
|
||||
} catch (error) { showError(error); }
|
||||
};
|
||||
async function poll() {
|
||||
if (polling || !invoke || !started || document.hidden || busy || controlBusy) return;
|
||||
polling = true;
|
||||
try {
|
||||
renderStatus(await invoke("status"));
|
||||
if (videoSession && (videoSession !== status.session_id ||
|
||||
videoGeneration !== status.generation)) {
|
||||
if (videoFailed) element("error").hidden = true;
|
||||
await closePreview();
|
||||
}
|
||||
if (status.state !== "ready" || document.hidden) {
|
||||
await closePreview();
|
||||
return;
|
||||
}
|
||||
await startPreview();
|
||||
const stale = !!imageAt && Date.now() - imageAt > 2000;
|
||||
element("stale").hidden = !stale;
|
||||
if (stale) {
|
||||
text("video-state", "等待画面");
|
||||
element("video-state").dataset.state = "stale";
|
||||
text("fps", "—");
|
||||
frameRate = "";
|
||||
}
|
||||
} catch (error) { showError(error); }
|
||||
finally { polling = false; }
|
||||
}
|
||||
document.addEventListener("visibilitychange", () => {
|
||||
if (document.hidden) void closePreview();
|
||||
else void poll();
|
||||
});
|
||||
window.addEventListener("pagehide", () => {
|
||||
void closePreview();
|
||||
});
|
||||
setInterval(() => {
|
||||
void poll();
|
||||
}, 1000);
|
||||
async function ready(next: Data) {
|
||||
if (
|
||||
started &&
|
||||
context?.view_id === next.view_id &&
|
||||
context?.view_generation === next.view_generation
|
||||
)
|
||||
return;
|
||||
if (started) await closePreview();
|
||||
context = next;
|
||||
element("panel-status").hidden = true;
|
||||
started = false;
|
||||
await invoke("ready");
|
||||
await devices();
|
||||
started = true;
|
||||
element<HTMLButtonElement>("connect").disabled = false;
|
||||
await poll();
|
||||
}
|
||||
async function start() {
|
||||
if (CROSSDESK_APPS) {
|
||||
const { App } = await import("@modelcontextprotocol/ext-apps");
|
||||
app = new App({ name: "CrossDesk MCP", version: "0.1.0" }, {});
|
||||
const names: Record<string, string> = {
|
||||
ready: "crossdesk_ui_ready",
|
||||
devices: "crossdesk_ui_list_devices",
|
||||
status: "crossdesk_ui_get_session",
|
||||
connect: "crossdesk_ui_connect",
|
||||
video_open: "crossdesk_video_open",
|
||||
control: "crossdesk_viewer_control",
|
||||
};
|
||||
let connected = false,
|
||||
initial: Data | undefined;
|
||||
invoke = async (name, args = {}) => {
|
||||
const result = await app!.callServerTool({
|
||||
name: names[name]!,
|
||||
arguments: {
|
||||
...args,
|
||||
view_id: context!.view_id,
|
||||
view_generation: context!.view_generation,
|
||||
ui_token: context!.ui_token,
|
||||
},
|
||||
});
|
||||
const data =
|
||||
result.structuredContent ??
|
||||
JSON.parse((result.content?.[0] as Data)?.text ?? "{}");
|
||||
return check(data);
|
||||
};
|
||||
app.ontoolresult = (result) => {
|
||||
const meta = result._meta?.["crossdesk/ui"] as Data | undefined;
|
||||
if (!meta) return;
|
||||
initial = { ...(result.structuredContent ?? {}), ...meta };
|
||||
if (connected) void ready(initial).catch(showError);
|
||||
};
|
||||
app.onteardown = async () => {
|
||||
await closePreview();
|
||||
return {};
|
||||
};
|
||||
await app.connect();
|
||||
connected = true;
|
||||
if (initial) await ready(initial);
|
||||
} else {
|
||||
let csrf = "";
|
||||
const request = async (path: string, body?: Data) =>
|
||||
check(
|
||||
await (
|
||||
await fetch("/api/" + path, {
|
||||
method: body ? "POST" : "GET",
|
||||
credentials: "same-origin",
|
||||
headers: body
|
||||
? { "Content-Type": "application/json", "X-CrossDesk-CSRF": csrf }
|
||||
: {},
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
cache: "no-store",
|
||||
})
|
||||
).json(),
|
||||
);
|
||||
const bootstrap = new URLSearchParams(location.hash.slice(1)).get(
|
||||
"bootstrap",
|
||||
);
|
||||
history.replaceState(null, "", location.pathname);
|
||||
const result = bootstrap
|
||||
? await request("bootstrap", { token: bootstrap })
|
||||
: await request("context");
|
||||
csrf = result.csrf;
|
||||
invoke = (name, args = {}) =>
|
||||
request(name, { ...args, view_id: context!.view_id });
|
||||
await ready(bootstrap ? result : result.views.at(-1));
|
||||
}
|
||||
}
|
||||
void start().catch(showError);
|
||||
-24
@@ -1,24 +0,0 @@
|
||||
diff --git a/src/pc/peer_connection.cpp b/src/pc/peer_connection.cpp
|
||||
index bb9eb49..eda133f 100644
|
||||
--- a/src/pc/peer_connection.cpp
|
||||
+++ b/src/pc/peer_connection.cpp
|
||||
@@ -459,3 +459,3 @@ int PeerConnection::SendJoinRequestLocked(
|
||||
LOG_INFO("[{}] sends join transmission [{}] request", user_id_,
|
||||
- transmission_id);
|
||||
+ transmission_id.substr(0, transmission_id.find('@')));
|
||||
}
|
||||
@@ -481,3 +481,3 @@ int PeerConnection::Join(const std::string& transmission_id) {
|
||||
LOG_INFO("Queue join transmission [{}] until signal login completes",
|
||||
- transmission_id);
|
||||
+ transmission_id.substr(0, transmission_id.find('@')));
|
||||
return 0;
|
||||
@@ -498,3 +498,3 @@ int PeerConnection::Leave(const std::string& transmission_id) {
|
||||
LOG_INFO("Cancel queued join transmission [{}]",
|
||||
- *pending_join_transmission_id_);
|
||||
+ pending_join_transmission_id_->substr(0, pending_join_transmission_id_->find('@')));
|
||||
pending_join_transmission_id_.reset();
|
||||
@@ -508,3 +508,3 @@ int PeerConnection::Leave(const std::string& transmission_id) {
|
||||
LOG_INFO("[{}] leave transmission [{}] notification queued",
|
||||
- user_id_, transmission_id);
|
||||
+ user_id_, transmission_id.substr(0, transmission_id.find('@')));
|
||||
}
|
||||
Vendored
+1
-1
Submodule deps/submodules/minirtc updated: 613cfe8bc5...9bdb5a775c
@@ -4,6 +4,8 @@
|
||||
|
||||
本文描述当前已实现的桌面客户端架构,代码依据为 CrossDesk `cc48eb65`、MiniRTC `040114d`,核对日期为 **2026-10-25**。ID 模式的服务端行为按客户端协议说明;服务端内部实现由独立的 CrossDesk Server 项目维护。
|
||||
|
||||
可信设备认证及配对身份文件的说明于 **2026-11-01** 按当前工作区更新;服务器登录沿用原有 ID、密码及重连令牌流程。
|
||||
|
||||
## 1. 两种模式的边界
|
||||
|
||||
两种模式复用桌面采集、编码、音视频传输、键鼠、文件和剪贴板处理。差别主要在**如何找到设备、在哪里验证密码,以及通过什么通道交换连接参数**。
|
||||
@@ -178,11 +180,17 @@ WSS 在连接成功后继续承担心跳和会话关闭消息。当前心跳约
|
||||
|
||||
| 场景 | 登录身份及职责 |
|
||||
| --- | --- |
|
||||
| 本机常驻 Peer | 使用本机缓存身份;已有 ID 与密码时,普通登录提交 `本机ID@本机密码`,用于本机上线与接受远控 |
|
||||
| 控制端会话 Peer | 以 `C-<本机ID>` 登录;目标设备的密码在后续 Join 中提交,不附加在该登录身份中 |
|
||||
| 自托管首次身份分配 | 无缓存身份时,常驻 Peer 以空 `user_id` 请求服务端分配身份,不能假定首次登录时已经具备 ID 和密码 |
|
||||
| 本机常驻 Peer | 使用缓存 ID 和本地 Ed25519 私钥完成挑战签名,服务端按该 ID 绑定的公钥验签 |
|
||||
| 控制端会话 Peer | 以 `C-<本机ID>` 完成同一私钥的挑战签名,服务端核对原 ID 的公钥;目标密码在后续 Join 中提交 |
|
||||
| 首次身份分配 | 无缓存身份时,以空 `user_id` 和本机公钥发起挑战;证明私钥持有权后,服务端原子分配 ID、远控密码并绑定公钥 |
|
||||
|
||||
服务端支持重连令牌且客户端持有有效令牌时,后续登录可使用不带密码的 ID 与令牌;令牌被拒绝后可回退到原登录身份。连接目标设备时,控制端会话 Peer 将 `目标ID@目标密码` 放入 Join,由服务端返回成功、凭据错误、限流或繁忙结果。客户端解析结果并驱动密码弹窗与重试;服务端保存密码的具体方式及配额以配套服务端实现为准。
|
||||
每次登录和重连都先请求 `login_challenge`,再签署规范 JSON 数组 `["crossdesk.device-login.v1", ID, 公钥, 随机挑战]`。挑战为 32 字节随机数,只属于当前 WSS 连接,有效期 10 秒,验证尝试时即消费。已绑定 ID 的签名登录不进入密码计算队列,也不读取密码失败计数;伪造签名不能锁定该 ID。服务端另外限制挑战来源和待处理数量,仍受连接及消息资源上限约束。
|
||||
|
||||
旧 ID 首次升级时,客户端先完成签名证明,再按服务端要求提交原有密码;密码验证成功后仅允许绑定一次公钥,保留 ID 和密码。绑定后撤销旧重连令牌并拒绝密码登录,不能用密码覆盖公钥;尚未完成绑定的旧 ID 按来源 IP 及“来源 IP → 目标 ID”限流,不保留目标 ID 的总失败次数。部署时先升级服务端;新桌面客户端要求签名协议,不自动回退到旧服务端的密码登录。混部或回滚到忽略公钥绑定的旧服务端会失去这项保护。
|
||||
|
||||
MiniRTC 的可选 `OnSignalExchange` 回调提供登录请求替换、入站消息处理及回复发送能力,不包含设备密钥、可信名单或 CrossDesk 协议字段。应用层负责签名和协议状态;未配置回调的使用方保留原有登录及内存重连令牌行为。改密中断后,签名登录不代表待定密码已生效,客户端重发持久保存的改密请求,收到成功回复后才更新本地密码。
|
||||
|
||||
连接目标设备时,控制端会话 Peer 仍将 `目标ID@目标密码` 放入 Join,由服务端返回成功、凭据错误、限流或繁忙结果。设备签名登录仅证明本机 ID 所有权,不授予控制其他设备的权限。服务端按已验证控制端 B 和“B → A”组合累计失败(分别为 20/5 次),不保留 A 的总失败次数;B 的失败不会触发 C 连接 A 的密码冷却。认证队列和计算额度也按已验证控制端隔离。未验证的旧客户端、Web/CLI 临时身份按来源 IP 处理,无法通过自称 `C-B` 获得 B 的预算。已绑定 B 的 `C-B` 登录必须支持签名协议;同一 NAT 下旧客户端仍共享 IP 限制,显式 IP 风控与服务器资源上限仍然适用。
|
||||
|
||||
信令 TLS 与远控数据加密属于两条路径。原生远控的 DTLS 握手和 SRTP 处理位于 MiniRTC,不能把“信令使用 TLS”等同于“画面经 TLS WebSocket 传输”。
|
||||
|
||||
@@ -196,7 +204,7 @@ WSS 在连接成功后继续承担心跳和会话关闭消息。当前心跳约
|
||||
|
||||
该表适用于 `MiniRtcConnection` 原生传输。浏览器使用 `DataChannelConnection` 的 WebRTC 适配路径,不按此表推断其加密行为。
|
||||
|
||||
对应代码:[常驻身份初始化](../apps/desktop/src/gui/runtime/gui_runtime.cpp)、[控制端会话登录与 Join](../apps/desktop/src/gui/runtime/connection_runtime.cpp)、[重连令牌](../deps/submodules/minirtc/src/pc/reconnect_login.h)、[SRTP 能力协商](../deps/submodules/minirtc/src/transport/ice_transport.cpp)。
|
||||
对应代码:[应用层登录认证](../apps/desktop/src/gui/runtime/device_login.cpp)、[常驻身份初始化](../apps/desktop/src/gui/runtime/gui_runtime.cpp)、[控制端会话登录与 Join](../apps/desktop/src/gui/runtime/connection_runtime.cpp)、[通用信令回调](../deps/submodules/minirtc/src/api/minirtc.h)、[SRTP 能力协商](../deps/submodules/minirtc/src/transport/ice_transport.cpp)。
|
||||
|
||||
### 4.2 IP 模式
|
||||
|
||||
@@ -217,6 +225,24 @@ IP 直连采用 **TLS 内提交密码,由被控端本地验证**:
|
||||
|
||||
对应代码:[密码验证](../apps/desktop/src/network/lan/lan_password.cpp)、[本地身份和凭据存储](../apps/desktop/src/network/lan/lan_identity.cpp)、[认证限流](../apps/desktop/src/network/lan/lan_auth_limiter.cpp)、[DTLS/SRTP 就绪处理](../deps/submodules/minirtc/src/transport/ice_transport.cpp)。
|
||||
|
||||
### 4.3 可选可信设备认证
|
||||
|
||||
可信设备限制默认关闭,认证及授权全部由 CrossDesk 应用层完成。被控端为每次新会话生成 32 字节随机挑战,控制端签署协议域 `crossdesk.device-trust.v1`、挑战随机数、本机公钥、设备 ID 和名称。被控端验证签名后,还必须确认公钥在本地可信名单中,才开放控制权限;相同 ID 或名称不能替代公钥匹配。
|
||||
|
||||
首次添加支持离线配对:在控制端复制本机配对信息,通过双方认可的渠道交给被控端;被控端粘贴后点击“添加”即校验并写入可信名单,也支持添加本机,无需预览和二次确认。已存在的公钥不会重复添加,完整指纹可在列表中查看。复制结果显示在“本机配对信息”栏目,添加结果显示在添加区域。配对信息只包含公钥和展示信息,不含私钥或密码,也不需要先建立远程连接或关闭可信设备限制。导入仅更新名单,后续连接仍须完成原有密码验证及实时签名认证;配对信息本身不能证明来源,用户通过传递渠道确认其来源。也可以继续从已连接且完成身份验证的控制端中选择添加。
|
||||
|
||||
挑战有效期为 10 秒,只接受一次成功证明。重复的已连接通知沿用当前挑战及截止时间;断开或重新进入连接阶段时清除认证状态,下次连接重新生成挑战。开启限制时,未完成证明、签名无效或公钥不在名单中的设备不能获得控制权限;不支持该应用层协议的旧客户端同样不能通过。关闭限制时保持原连接行为。
|
||||
|
||||
MiniRTC 仅通过现有可靠数据通道收发这些应用消息,不提供可信设备专用接口,也不参与名单、签名或授权判断。认证证明不包含 DTLS 连接绑定:随机挑战可防止历史证明重放,但不防护攻击者在两条连接之间实时转发挑战和签名的情形。原有传输加密及其兼容规则仍由第 4.1、4.2 节描述。
|
||||
|
||||
设备私钥保存在缓存目录中的 `identity.json`,服务器登录与可信设备认证复用这一份密钥,通过不同协议域区分签名用途。macOS/Linux 文件权限为 `0600`,Windows 使用当前用户 ACL;身份存储通过 `identity.json.lock` 保护读写,并基于最新内容原子更新。锁文件不含凭据,无需备份。
|
||||
|
||||
可信列表保存在配置目录的 `trusted-devices/devices.json`,新版格式只含可信设备的公钥、名称和 ID,可包含本机。旧格式中的本机私钥会自动迁入 `identity.json`,持久写入成功后才从列表文件移除,因此原有指纹和配对关系保持不变。迁移中断可重试;发现两处密钥冲突时停止加载,不自动覆盖。Windows 无人值守服务使用其独立资料目录中的同一布局。
|
||||
|
||||
只删除普通 ID/密码缓存不会删除私钥,但单凭私钥不能找回已分配的 ID,客户端会请求新 ID。保留旧 ID 而丢失私钥后无法登录已绑定的身份,需要恢复原私钥备份,或清理旧 ID 缓存重新登记。复制 ID 缓存及私钥会复制登录身份,不具备硬件绑定;备份时应一起保护这些文件。清空整个缓存目录还会改变配对指纹,需要其他设备重新信任本机。保留允许控制本机的设备名单还应备份可信列表。身份文件损坏或权限不安全时停止加载和设备登录;已开启的可信设备限制不会自动关闭。
|
||||
|
||||
对应代码:[设备签名与名单存储](../apps/desktop/src/gui/runtime/trusted_devices.cpp)、[应用层认证及授权](../apps/desktop/src/gui/runtime/device_trust_runtime.cpp)、[连接状态与认证生命周期](../apps/desktop/src/gui/runtime/peer_event_handler.cpp)。
|
||||
|
||||
## 5. 运行时复用、线程与关闭
|
||||
|
||||
```mermaid
|
||||
|
||||
@@ -19,6 +19,11 @@
|
||||
#include <vector>
|
||||
|
||||
namespace crossdesk {
|
||||
struct EncodedVideoFrame {
|
||||
std::vector<uint8_t> data;
|
||||
uint64_t generation = 0, sequence = 0, timestamp_us = 0;
|
||||
bool key = false;
|
||||
};
|
||||
struct DecodedFrame {
|
||||
unsigned width = 0, height = 0;
|
||||
uint64_t generation = 0, frame_id = 0;
|
||||
@@ -46,6 +51,7 @@ struct SessionOptions {
|
||||
int port = 9099, display = 0, fps = 8;
|
||||
int preview_width = 1920, preview_height = 1080;
|
||||
int quality = 2, max_bitrate_bps = 6000000;
|
||||
bool hardware_acceleration = false;
|
||||
std::chrono::seconds timeout{30};
|
||||
};
|
||||
// All public methods except the destructor run on one owner thread. Call Tick
|
||||
@@ -59,15 +65,19 @@ public:
|
||||
void Tick();
|
||||
void Disconnect();
|
||||
bool SelectDisplay(int index);
|
||||
void SetVideoOutput(bool enabled, bool request_keyframe = false);
|
||||
std::optional<EncodedVideoFrame> TakeEncodedVideo();
|
||||
void SetPreview(int fps, int width, int height, int quality = -1,
|
||||
int max_bitrate_bps = -1);
|
||||
void SetControl(bool enabled);
|
||||
void ReleaseInput();
|
||||
bool Text(const std::string &utf8);
|
||||
bool Key(int key, bool down);
|
||||
bool Pointer(float x, float y, int flag, int wheel = 0);
|
||||
std::optional<DecodedFrame> TakeFrame(DecodedFrame *recycle = nullptr);
|
||||
nlohmann::json Status() const;
|
||||
bool Connected() const { return state_ == "connected"; }
|
||||
bool ControlEnabled() const { return control_; }
|
||||
bool PreviewAccepted() const { return preview_accepted_; }
|
||||
bool SupportsPreviewFps15() const { return fps15_; }
|
||||
bool Finished() const { return state_ == "failed" || state_ == "closed"; }
|
||||
@@ -84,13 +94,14 @@ private:
|
||||
Params Parameters();
|
||||
bool Send(const char *stream, const std::string &payload);
|
||||
void Preview();
|
||||
void ReleaseInput();
|
||||
static void Signal(SignalStatus, const char *, size_t, void *);
|
||||
static void Connection(ConnectionStatus, const char *, size_t, void *);
|
||||
static void Data(const char *, size_t, const char *, size_t, const char *,
|
||||
size_t, void *);
|
||||
static void Video(const MiniRtcVideoFrame *, const char *, size_t,
|
||||
const char *, size_t, void *);
|
||||
static int EncodedVideo(const uint8_t *, size_t, int64_t, const char *,
|
||||
size_t, const char *, size_t, void *);
|
||||
static void Network(const char *, size_t, TraversalMode,
|
||||
const MiniRtcNetTrafficStats *, const char *, size_t,
|
||||
void *);
|
||||
@@ -102,6 +113,7 @@ private:
|
||||
bool control_ = false, preview_supported_ = false, resolution_ = false;
|
||||
bool fps15_ = false, bandwidth_ = false, text_supported_ = false;
|
||||
bool preview_accepted_ = false;
|
||||
HostPlatform host_platform_ = HostPlatform::Unknown;
|
||||
std::vector<HostDisplay> displays_;
|
||||
std::set<int> keys_, buttons_;
|
||||
float pointer_x_ = .5f, pointer_y_ = .5f;
|
||||
@@ -114,6 +126,12 @@ private:
|
||||
MiniRtcNetTrafficStats network_{};
|
||||
TraversalMode traversal_ = UnknownMode;
|
||||
LatestFrame frames_;
|
||||
bool video_output_ = false, video_wait_key_ = true, video_request_key_ = false;
|
||||
size_t video_bytes_ = 0;
|
||||
uint64_t video_generation_ = 0, video_sequence_ = 0;
|
||||
std::deque<EncodedVideoFrame> video_frames_;
|
||||
std::vector<uint8_t> video_sps_, video_pps_;
|
||||
std::chrono::steady_clock::time_point video_key_requested_{};
|
||||
};
|
||||
nlohmann::json ParseBoundedJson(const std::string &text);
|
||||
std::string SafeText(const std::string &text, size_t limit = 160);
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-11-02
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _FRAME_IMAGE_H_
|
||||
#define _FRAME_IMAGE_H_
|
||||
|
||||
#include <controller_session.h>
|
||||
|
||||
namespace crossdesk {
|
||||
struct ImageRegion {
|
||||
int x = 0, y = 0, width = 0, height = 0;
|
||||
};
|
||||
struct FrameImage {
|
||||
int width = 0, height = 0;
|
||||
ImageRegion region;
|
||||
std::vector<uint8_t> png;
|
||||
};
|
||||
// Pure bounded conversion; safe on an encoding worker, never calls the session.
|
||||
FrameImage EncodeFrameImage(const DecodedFrame &frame, ImageRegion region,
|
||||
int max_edge, size_t max_bytes);
|
||||
std::string ImageBase64(const std::vector<uint8_t> &bytes);
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -77,7 +77,7 @@ Params ControllerSession::Parameters() {
|
||||
std::snprintf(p.log_path, sizeof(p.log_path), "%s",
|
||||
options_.log_directory.c_str());
|
||||
p.signal_server_port = options_.port;
|
||||
p.hardware_acceleration = false;
|
||||
p.hardware_acceleration = options_.hardware_acceleration;
|
||||
p.native_video_output = true;
|
||||
p.av1_encoding = false;
|
||||
p.turn_mode = TurnAutoUdpTcp;
|
||||
@@ -92,6 +92,7 @@ Params ControllerSession::Parameters() {
|
||||
p.on_net_status_report = Network;
|
||||
p.on_receive_data_buffer = Data;
|
||||
p.on_receive_video_frame = Video;
|
||||
p.on_receive_encoded_video = EncodedVideo;
|
||||
// No audio callback, clipboard receiver, file writer or incoming input
|
||||
// handler.
|
||||
p.user_id = "cli";
|
||||
@@ -118,6 +119,7 @@ void ControllerSession::Connect(SessionOptions options, std::string device,
|
||||
state_ = "registering";
|
||||
error_.clear();
|
||||
displays_.clear();
|
||||
host_platform_ = HostPlatform::Unknown;
|
||||
preview_supported_ = resolution_ = fps15_ = bandwidth_ = text_supported_ =
|
||||
false;
|
||||
preview_request_ = preview_ack_ = 0;
|
||||
@@ -238,6 +240,7 @@ void ControllerSession::Tick() {
|
||||
if (!a.from_json(e.data))
|
||||
continue;
|
||||
if (a.type == host_infomation) {
|
||||
host_platform_ = a.i.platform;
|
||||
displays_.clear();
|
||||
for (size_t i = 0; i < std::min(a.i.display_num, size_t(64)); ++i)
|
||||
displays_.push_back({SafeText(a.i.display_list[i]), a.i.left[i],
|
||||
@@ -285,6 +288,8 @@ void ControllerSession::Fail(const char *reason) {
|
||||
state_ = "failed";
|
||||
control_ = false;
|
||||
ClosePeers();
|
||||
keys_.clear();
|
||||
buttons_.clear();
|
||||
}
|
||||
void ControllerSession::Disconnect() {
|
||||
if (state_ == "closed" && !control_)
|
||||
@@ -292,6 +297,8 @@ void ControllerSession::Disconnect() {
|
||||
ReleaseInput();
|
||||
control_ = false;
|
||||
ClosePeers();
|
||||
keys_.clear();
|
||||
buttons_.clear();
|
||||
state_ = "closed";
|
||||
}
|
||||
bool ControllerSession::SelectDisplay(int index) {
|
||||
@@ -342,6 +349,9 @@ void ControllerSession::Preview() {
|
||||
// 60 is the existing interactive-mode sentinel, not a 60 FPS request.
|
||||
// The encoder cadence is independently specified by frame_rate.
|
||||
s.frame_rate = 30;
|
||||
// Preserve desktop text detail at every cadence, including 30 FPS previews.
|
||||
s.preference =
|
||||
static_cast<int>(VideoDegradationPreference::MaintainResolution);
|
||||
s.audio = false;
|
||||
s.quality = options_.quality;
|
||||
if (resolution_) {
|
||||
@@ -361,30 +371,34 @@ void ControllerSession::SetControl(bool enabled) {
|
||||
control_ = enabled;
|
||||
}
|
||||
void ControllerSession::ReleaseInput() {
|
||||
auto keys = keys_;
|
||||
for (int k : keys)
|
||||
Key(k, false);
|
||||
// Release buttons before modifiers, so interrupted modified drags do not
|
||||
// turn into an unmodified drop. Failed releases remain tracked for cleanup.
|
||||
auto buttons = buttons_;
|
||||
for (int b : buttons)
|
||||
Pointer(pointer_x_, pointer_y_, b + 1);
|
||||
keys_.clear();
|
||||
buttons_.clear();
|
||||
auto keys = keys_;
|
||||
for (auto it = keys.rbegin(); it != keys.rend(); ++it)
|
||||
Key(*it, false);
|
||||
}
|
||||
bool ControllerSession::Key(int key, bool down) {
|
||||
if (!control_ || !Connected() || key < 0 || key > 255)
|
||||
if ((!control_ && (down || !keys_.count(key))) || !Connected() || key < 0 ||
|
||||
key > 255)
|
||||
return false;
|
||||
RemoteAction a{};
|
||||
a.type = keyboard;
|
||||
a.k = {size_t(key), 0, false, down ? key_down : key_up};
|
||||
if (down)
|
||||
keys_.insert(key);
|
||||
else
|
||||
const bool sent = Send(kKeyboardStream, a.to_json());
|
||||
if (sent && !down)
|
||||
keys_.erase(key);
|
||||
return Send(kKeyboardStream, a.to_json());
|
||||
return sent;
|
||||
}
|
||||
bool ControllerSession::Pointer(float x, float y, int flag, int wheel) {
|
||||
if (!control_ || !Connected() || !std::isfinite(x) || !std::isfinite(y) ||
|
||||
x < 0 || y < 0 || x > 1 || y > 1 || flag < 0 || flag > 8 ||
|
||||
const bool releasing =
|
||||
flag >= 2 && flag <= 6 && !(flag & 1) && buttons_.count(flag - 1);
|
||||
if ((!control_ && !releasing) || !Connected() || !std::isfinite(x) ||
|
||||
!std::isfinite(y) || x < 0 || y < 0 || x > 1 || y > 1 || flag < 0 || flag > 8 ||
|
||||
std::abs(int64_t(wheel)) > 1200)
|
||||
return false;
|
||||
RemoteAction a{};
|
||||
@@ -392,13 +406,12 @@ bool ControllerSession::Pointer(float x, float y, int flag, int wheel) {
|
||||
a.m = {x, y, wheel, static_cast<MouseFlag>(flag)};
|
||||
pointer_x_ = x;
|
||||
pointer_y_ = y;
|
||||
if (flag >= 1 && flag <= 6) {
|
||||
if (flag & 1)
|
||||
buttons_.insert(flag);
|
||||
else
|
||||
buttons_.erase(flag - 1);
|
||||
}
|
||||
return Send(kMouseStream, a.to_json());
|
||||
if (flag >= 1 && flag <= 6 && (flag & 1))
|
||||
buttons_.insert(flag);
|
||||
const bool sent = Send(kMouseStream, a.to_json());
|
||||
if (sent && flag >= 2 && flag <= 6 && !(flag & 1))
|
||||
buttons_.erase(flag - 1);
|
||||
return sent;
|
||||
}
|
||||
bool ControllerSession::Text(const std::string &text) {
|
||||
if (!control_ || !Connected() || !text_supported_ || text.empty() ||
|
||||
@@ -419,7 +432,9 @@ std::optional<DecodedFrame> ControllerSession::TakeFrame(DecodedFrame *recycle)
|
||||
json ControllerSession::Status() const {
|
||||
json ds = json::array();
|
||||
for (size_t i = 0; i < displays_.size(); ++i)
|
||||
ds.push_back({{"index", i}, {"name", displays_[i].name}});
|
||||
ds.push_back({{"index", i}, {"name", displays_[i].name},
|
||||
{"left", displays_[i].left}, {"top", displays_[i].top},
|
||||
{"right", displays_[i].right}, {"bottom", displays_[i].bottom}});
|
||||
std::lock_guard lock(mutex_);
|
||||
return {{"state", state_},
|
||||
{"client_id", client_id_},
|
||||
@@ -427,6 +442,7 @@ json ControllerSession::Status() const {
|
||||
{"generation", generation_},
|
||||
{"control", control_},
|
||||
{"displays", ds},
|
||||
{"platform", HostPlatformName(host_platform_)},
|
||||
{"display", options_.display},
|
||||
{"text_input", text_supported_},
|
||||
{"preview_supported", preview_supported_},
|
||||
@@ -437,7 +453,19 @@ json ControllerSession::Status() const {
|
||||
{"preview_height", options_.preview_height},
|
||||
{"preview_quality", options_.quality},
|
||||
{"preview_bitrate_bps", options_.max_bitrate_bps},
|
||||
{"native_video_decoder", SafeText(std::string(
|
||||
network_.video_decoder,
|
||||
std::find(network_.video_decoder,
|
||||
network_.video_decoder + sizeof(network_.video_decoder), '\0')))},
|
||||
{"frames", frames_.Stats()},
|
||||
{"video_output",
|
||||
{{"enabled", video_output_},
|
||||
{"received", video_sequence_},
|
||||
{"queued", video_frames_.size()},
|
||||
{"queued_bytes", video_bytes_},
|
||||
{"waiting_keyframe", video_wait_key_},
|
||||
{"has_sps", !video_sps_.empty()},
|
||||
{"has_pps", !video_pps_.empty()}}},
|
||||
{"network",
|
||||
{{"video_bps", network_.video_inbound_stats.bitrate},
|
||||
{"rtt_ms", network_.rtt_ms},
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user