mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-16 16:50:41 +01:00
Compare commits
No files matched your search
@@ -6,6 +6,16 @@ on:
|
|||||||
pull_request:
|
pull_request:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
test-macos-ssh-paste:
|
||||||
|
runs-on: macos-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- run: python3 -m unittest discover -s contrib/macos-ssh -p 'test_*.py' -v
|
||||||
|
- name: Test file-URL clipboard reader on a private pasteboard
|
||||||
|
run: |
|
||||||
|
swiftc -O contrib/macos-ssh/clipboard-files.swift -o "$RUNNER_TEMP/clipboard-files"
|
||||||
|
"$RUNNER_TEMP/clipboard-files" --self-test
|
||||||
|
|
||||||
test:
|
test:
|
||||||
runs-on: ubuntu-22.04
|
runs-on: ubuntu-22.04
|
||||||
steps:
|
steps:
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ on:
|
|||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
contents: write
|
||||||
|
packages: write
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
release:
|
release:
|
||||||
@@ -37,3 +38,23 @@ jobs:
|
|||||||
files: |
|
files: |
|
||||||
uu-remote-linux-*.tar.gz
|
uu-remote-linux-*.tar.gz
|
||||||
uu-remote-linux-*.tar.gz.sha256
|
uu-remote-linux-*.tar.gz.sha256
|
||||||
|
|
||||||
|
# Headless NAS image (docker/), built from the release tarball published by the job above.
|
||||||
|
docker:
|
||||||
|
needs: release
|
||||||
|
runs-on: ubuntu-22.04
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.actor }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
- uses: docker/build-push-action@v6
|
||||||
|
with:
|
||||||
|
context: docker
|
||||||
|
build-args: VERSION=${{ github.ref_name }}
|
||||||
|
push: true
|
||||||
|
tags: |
|
||||||
|
ghcr.io/vallierdev/uu-remote-linux:${{ github.ref_name }}
|
||||||
|
ghcr.io/vallierdev/uu-remote-linux:latest
|
||||||
@@ -146,7 +146,7 @@ every 60 s.
|
|||||||
| `c_engine/x11_engine.c`, `c_engine/clipboard.c` | X11 capture/input/selection |
|
| `c_engine/x11_engine.c`, `c_engine/clipboard.c` | X11 capture/input/selection |
|
||||||
| `c_engine/wayland_engine.c`, `c_engine/portal_engine.c` | Wayland and portal engines |
|
| `c_engine/wayland_engine.c`, `c_engine/portal_engine.c` | Wayland and portal engines |
|
||||||
| `src/assist.rs`, `c_engine/assist_window.c` | remote assistance (guest + account) and the unified GTK main window (assist + account/QR card) |
|
| `src/assist.rs`, `c_engine/assist_window.c` | remote assistance (guest + account) and the unified GTK main window (assist + account/QR card) |
|
||||||
| `src/viewer_main.rs`, `src/viewer/` | experimental viewer (`--features native-viewer,viewer-sdl`): cloud join, WebRTC offer side, SDL desktop window (ffmpeg CLI decode), files/terminal/portmap/power/assist tools, GTK launcher |
|
| `src/viewer_main.rs`, `src/viewer/` | experimental viewer (`--features native-viewer,viewer-sdl`): cloud join, WebRTC offer side, SDL desktop window (ffmpeg CLI decode), files/terminal/portmap/power/assist tools, device removal (`POST /api/v1/device/{id}/unbind`, verified live; shared devices use `/api/v1/device/share/{id}/unbind`, strings only), GTK launcher |
|
||||||
| `c_engine/viewer_window.c`, `c_engine/viewer_tools.c` | viewer GTK launcher and tool windows (files, port mapping, terminal list) |
|
| `c_engine/viewer_window.c`, `c_engine/viewer_tools.c` | viewer GTK launcher and tool windows (files, port mapping, terminal list) |
|
||||||
|
|
||||||
Diagnostic subcommands: `login [--no-gui]`, `unlock-check` (password on stdin, prints only accepted/rejected),
|
Diagnostic subcommands: `login [--no-gui]`, `unlock-check` (password on stdin, prints only accepted/rejected),
|
||||||
|
|||||||
Generated
+1
-1
@@ -2741,7 +2741,7 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "uu-remote-linux"
|
name = "uu-remote-linux"
|
||||||
version = "0.2.1"
|
version = "0.2.8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"base64 0.22.1",
|
"base64 0.22.1",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "uu-remote-linux"
|
name = "uu-remote-linux"
|
||||||
version = "0.2.1"
|
version = "0.2.8"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
default-run = "uuremote-server"
|
default-run = "uuremote-server"
|
||||||
authors = ["uu-remote-linux contributors"]
|
authors = ["uu-remote-linux contributors"]
|
||||||
|
|||||||
+43
-9
@@ -1,4 +1,4 @@
|
|||||||
# uu-remote-linux · Unofficial Linux host for NetEase UU Remote
|
# uu-remote-linux · Unofficial Linux host and viewer for NetEase UU Remote
|
||||||
|
|
||||||
[简体中文](README.md) | **English**
|
[简体中文](README.md) | **English**
|
||||||
|
|
||||||
@@ -7,13 +7,19 @@
|
|||||||
> NetEase UU Remote (网易 UU 远程) is a great remote-desktop tool, but there is no Linux version on its
|
> NetEase UU Remote (网易 UU 远程) is a great remote-desktop tool, but there is no Linux version on its
|
||||||
> official website. So, with the help of Claude and Codex, I wrote this open-source Linux host: install it,
|
> official website. So, with the help of Claude and Codex, I wrote this open-source Linux host: install it,
|
||||||
> and you can control your Linux machine from the official Mac / Windows / mobile UU Remote clients.
|
> and you can control your Linux machine from the official Mac / Windows / mobile UU Remote clients.
|
||||||
|
> It now also ships an **experimental Linux viewer**, `uuremote-viewer`, that controls official Windows / Mac hosts or this
|
||||||
|
> project's host from Linux (desktop, file transfer, terminal, port mapping, remote assistance; see
|
||||||
|
> [below](#experimental-native-viewer-uuremote-viewer)).
|
||||||
>
|
>
|
||||||
> This is a personal hobby project. **It is not affiliated with NetEase and is not an official client.**
|
> This is a personal hobby project. **It is not affiliated with NetEase and is not an official client.**
|
||||||
> If NetEase considers this project infringing or inappropriate, please DM me on X
|
> If NetEase considers this project infringing or inappropriate, please DM me on X
|
||||||
> **[@ai_fengshou](https://x.com/ai_fengshou)** and I will take it down right away.
|
> **[@ai_fengshou](https://x.com/ai_fengshou)** and I will take it down right away.
|
||||||
|
|
||||||
|
<p align="center"><img src="docs/images/viewer-window.png" width="720" alt="Linux viewer launcher: device list (device names redacted)"></p>
|
||||||
|
<p align="center"><sub>Linux viewer: device list (device names redacted)</sub></p>
|
||||||
|
|
||||||
<p align="center"><img src="docs/images/main-window.png" width="620" alt="Host main window: remote assistance and account"></p>
|
<p align="center"><img src="docs/images/main-window.png" width="620" alt="Host main window: remote assistance and account"></p>
|
||||||
<p align="center"><sub>The QR login window shown on first run (QR code blurred). The UI is in Chinese, like the official app.</sub></p>
|
<p align="center"><sub>Linux host main window: remote assistance and account (device ID, code and account redacted). The UI is in Chinese, like the official app.</sub></p>
|
||||||
|
|
||||||
<!-- Screenshot placeholder: the Linux desktop controlled from the official client. Take your own
|
<!-- Screenshot placeholder: the Linux desktop controlled from the official client. Take your own
|
||||||
screenshot, remove account/device details and NetEase logos, save it as docs/images/remote-desktop.png
|
screenshot, remove account/device details and NetEase logos, save it as docs/images/remote-desktop.png
|
||||||
@@ -230,6 +236,16 @@ service and run `loginctl enable-linger $USER`, so the systemd user manager (wit
|
|||||||
starts at boot without a login. The viewer's remote desktop keeps showing "connecting" in this mode; that is expected.
|
starts at boot without a login. The viewer's remote desktop keeps showing "connecting" in this mode; that is expected.
|
||||||
Log in once with `uuremote-server login --no-gui` (QR in the terminal).
|
Log in once with `uuremote-server login --no-gui` (QR in the terminal).
|
||||||
|
|
||||||
|
**Synology / NAS (Docker)**: run headless mode in a container on a desktop-less NAS (file transfer, terminal, port
|
||||||
|
mapping): image `ghcr.io/vallierdev/uu-remote-linux`, see [docker/README.md](docker/README.md). Verified on Synology DSM 7.3 (x86_64).
|
||||||
|
|
||||||
|
**Paste files from a Mac terminal**: when using Ghostty over UU's SSH port mapping,
|
||||||
|
the optional [Mac SSH paste companion](contrib/macos-ssh/README.md) uploads files
|
||||||
|
copied in Finder before inserting their remote paths. Text and mouse reports pass
|
||||||
|
through. Ghostty's title and progress bar show upload progress, transferred size,
|
||||||
|
speed and elapsed time. It does not change the official UU terminal; folders, drag-and-drop and
|
||||||
|
raw screenshot clipboard data are not supported.
|
||||||
|
|
||||||
**Virtual desktop**: for a full remote desktop, run a desktop session (Xfce recommended) on an Xvfb screen:
|
**Virtual desktop**: for a full remote desktop, run a desktop session (Xfce recommended) on an Xvfb screen:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
@@ -411,33 +427,51 @@ hosts or this project's Linux host), and can start remote assistance with someon
|
|||||||
separate from the host `uuremote-server`; the default host build and behaviour are unchanged.
|
separate from the host `uuremote-server`; the default host build and behaviour are unchanged.
|
||||||
|
|
||||||
**Get it**: release tarballs include `uuremote-viewer` (x86_64) from this version on. Runtime needs GTK3, SDL2 (Ubuntu/Debian:
|
**Get it**: release tarballs include `uuremote-viewer` (x86_64) from this version on. Runtime needs GTK3, SDL2 (Ubuntu/Debian:
|
||||||
`libsdl2-2.0-0`) and the `ffmpeg` command; clipboard needs `wl-clipboard` (Wayland) or `xclip`/`xsel` (X11); the terminal needs a local
|
`libsdl2-2.0-0`) and the `ffmpeg` command; SDL desktop windows synchronize text clipboard directly on Wayland/X11 without extra clipboard tools; the terminal needs a local
|
||||||
terminal emulator (`TERMINAL` selects one). From source: `sudo apt install libsdl2-dev`, then
|
terminal emulator (`TERMINAL` selects one). From source: `sudo apt install libsdl2-dev`, then
|
||||||
`cargo build --release --locked --features native-viewer,viewer-sdl --bin uuremote-viewer`.
|
`cargo build --release --locked --features native-viewer,viewer-sdl --bin uuremote-viewer`.
|
||||||
|
|
||||||
|
SDL clipboard access reuses the desktop window to avoid temporary-window Dock churn on GNOME Wayland (#37).
|
||||||
|
Synchronization runs only while that window is focused and keeps the 30 KiB text limit. Selections without a supported
|
||||||
|
text format are skipped; empty-text detection depends on the SDL2 backend. Builds without SDL retain the command backend.
|
||||||
|
SDL2 reads selections synchronously, so a slow selection owner can temporarily block the window; the bridge timeout cannot interrupt it.
|
||||||
|
GNOME-to-official-Mac bidirectional clipboard synchronization is still unverified; this change does not resolve host compatibility.
|
||||||
|
|
||||||
**Use**:
|
**Use**:
|
||||||
- `uuremote-viewer login`: QR login (reuses the sibling `uuremote-server` login window; `--no-gui` prints a terminal QR). The viewer
|
- `uuremote-viewer login`: QR login (reuses the sibling `uuremote-server` login window; `--no-gui` prints a terminal QR). The viewer
|
||||||
shares `~/.config/uuremote/config.json` with the host (`UU_CONFIG` selects another), so it **cannot connect to a host on the same
|
shares `~/.config/uuremote/config.json` with the host (`UU_CONFIG` selects another), so it **cannot connect to a host on the same
|
||||||
machine with the same identity**; other devices are fine.
|
machine with the same identity**; other devices are fine.
|
||||||
- `uuremote-viewer ui`: launcher listing your devices: desktop, view-only, files, terminal, port mapping, reboot/shutdown, assistance.
|
- `uuremote-viewer ui`: launcher listing your devices: desktop, view-only, files, terminal, port mapping, reboot/shutdown, assistance.
|
||||||
- CLI: `connect DEVICE false - echo legacy [--view-only]` (desktop), `files DEVICE ls|get|put …`, `terminal DEVICE [SESSION]`,
|
- CLI: `connect DEVICE false - echo legacy [--view-only]` (desktop), `files DEVICE ls|get|put …`, `terminal DEVICE [SESSION]`,
|
||||||
`terminal-ui DEVICE NAME`, `portmap DEVICE LOCAL_PORT HOST PORT` (listens on 127.0.0.1 only), `power DEVICE reboot|shutdown`,
|
`terminal-ui DEVICE NAME`, `portmap DEVICE LOCAL_PORT HOST PORT` (listens on 127.0.0.1 only), `power DEVICE reboot|shutdown`, `unbind DEVICE` (remove a device from the account; also under "More tools" in the launcher),
|
||||||
`assist CONNECT_ID` (the code is read from one stdin line, never an argument). See `uuremote-viewer --help`.
|
`assist CONNECT_ID` (the code is read from one stdin line, never an argument). See `uuremote-viewer --help`.
|
||||||
- Ctrl+Alt+F in the desktop window opens the files window on the same connection. `UU_VIEWER_AUDIO=0` disables sound,
|
- Ctrl+Alt+F in the desktop window opens the files window on the same connection. `UU_VIEWER_AUDIO=0` disables sound,
|
||||||
`UU_VIEWER_CLIPBOARD=0` the clipboard, `UU_VIEWER_VIEW_ONLY=1` input; `UU_VIEWER_FFMPEG` selects the ffmpeg binary.
|
`UU_VIEWER_CLIPBOARD=0` the clipboard, `UU_VIEWER_VIEW_ONLY=1` input; `UU_VIEWER_FFMPEG` selects the ffmpeg binary.
|
||||||
|
- Desktop offers H.264 / H.265; the host chooses the codec. `UU_VIEWER_CODEC=h264` limits both SDP and decoder capabilities to H.264.
|
||||||
|
The default request stays 4:2:0; `UU_VIEWER_CHROMA=444` requests true color (4:4:4) after CONTROL opens. The host may fall back to
|
||||||
|
4:2:0, as reported by CaptureSettingSync. FFmpeg converts either chroma format to RGB24 for display.
|
||||||
|
|
||||||
|
Mac targets use native macOS keycodes; Windows / Linux targets retain Windows VK. Keys follow physical positions, without
|
||||||
|
automatically translating Ctrl to Command; on a Mac target the PC Super / Windows key acts as Command. Only verified target
|
||||||
|
platform values 1 (Windows / this project's Linux host) and 4 (Mac) are accepted; unknown values fail explicitly. Account
|
||||||
|
connections look up the selected device only when the platform is absent; assistance requires it in the join response.
|
||||||
|
View-only sessions skip this lookup. Media keys and some JIS-specific keys are not mapped; Mac platform detection for remote
|
||||||
|
assistance and ISO / JIS keyboards have not been tested live.
|
||||||
|
|
||||||
**Tested live** (author's setup, not full acceptance):
|
**Tested live** (author's setup, not full acceptance):
|
||||||
| Feature | Official Windows host | Official Mac host | This project's Linux host |
|
| Feature | Official Windows host | Official Mac host | This project's Linux host |
|
||||||
|---|---|---|---|
|
|---|---|---|---|
|
||||||
| Desktop video (H.264) | ✅ | ✅ | ✅ |
|
| Desktop video (H.264) | ✅ | ✅ | ✅ |
|
||||||
|
| H.265, 4:4:4 true color | — | — | ✅ (VM, libx265) |
|
||||||
| Keyboard, mouse | ✅ | — | ✅ |
|
| Keyboard, mouse | ✅ | — | ✅ |
|
||||||
| File list / download / upload | ✅ list, download | — | ✅ |
|
| File list / download / upload | ✅ | — | ✅ |
|
||||||
| Port mapping, terminal | — | — | ✅ |
|
| Port mapping, terminal | ✅ (PowerShell terminal) | — | ✅ |
|
||||||
| Clipboard (text, both ways), sound | — | — | ✅ (VM) |
|
| Clipboard (text) | host → viewer only; viewer → Windows not applied yet | — | ✅ both ways (VM) |
|
||||||
|
| Sound | unconfirmed (no audio packets while the host is silent) | — | ✅ (VM) |
|
||||||
| Remote assistance (ID + code) | — | — | ✅ (VM, guest host) |
|
| Remote assistance (ID + code) | — | — | ✅ (VM, guest host) |
|
||||||
| Terminal session list (create, double-click reattach, end) | — | — | ✅ (VM) |
|
| Terminal session list (create, double-click reattach, end) | — | — | ✅ (VM) |
|
||||||
| Reboot, shutdown | — | — | ✅ (VM) |
|
| Reboot, shutdown | — | — | ✅ (VM) |
|
||||||
| Wayland desktop window and clipboard (Hyprland) | — | — | ✅ (VM) |
|
| Wayland desktop window and clipboard | — | — | ✅ Hyprland, KDE; GNOME window works; clipboard switched to SDL in v0.2.3 (#37, final revision not re-verified live) |
|
||||||
|
|
||||||
"—" = not tested yet. Only use reboot/shutdown on machines you can restart. **Not tested live**: GNOME/KDE Wayland windows and clipboard, H.265 and 4:4:4 (only H.264 4:2:0 is decoded), multiple monitors. Logs carry field
|
"—" = not tested yet. Only use reboot/shutdown on machines you can restart. **Not tested live**: H.265 / 4:4:4 with official hosts, multiple monitors. Logs carry field
|
||||||
numbers, sizes and states only, never keystrokes, clipboard or file contents, codes or credentials.
|
numbers, sizes and states only, never keystrokes, clipboard or file contents, codes or credentials.
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
# uu-remote-linux · UU 远程 Linux 被控端(非官方)
|
# uu-remote-linux · UU 远程 Linux 被控端 + 控制端(非官方)
|
||||||
|
|
||||||
**简体中文** | [English](README.en.md)
|
**简体中文** | [English](README.en.md)
|
||||||
|
|
||||||
@@ -6,12 +6,17 @@
|
|||||||
>
|
>
|
||||||
> 网易 UU 远程非常好用,但官网一直没有 Linux 版本。于是我借助 Claude 和 Codex,写了这个开源的 Linux 被控端:
|
> 网易 UU 远程非常好用,但官网一直没有 Linux 版本。于是我借助 Claude 和 Codex,写了这个开源的 Linux 被控端:
|
||||||
> 装上之后,就能用官方的 Mac / Windows / 手机客户端远程控制你的 Linux 电脑。
|
> 装上之后,就能用官方的 Mac / Windows / 手机客户端远程控制你的 Linux 电脑。
|
||||||
|
> 现在还附带一个**实验性 Linux 控制端** `uuremote-viewer`:在 Linux 上用 UU 账号远控官方 Windows / Mac 被控端或本项目主机
|
||||||
|
> (桌面、文件传输、终端、端口映射、远程协助,详见[下文](#实验性原生控制端uuremote-viewer))。
|
||||||
>
|
>
|
||||||
> 这是个人兴趣项目,**与网易无关,也不是官方客户端**。如果网易认为本项目侵权或有不妥,
|
> 这是个人兴趣项目,**与网易无关,也不是官方客户端**。如果网易认为本项目侵权或有不妥,
|
||||||
> 请在推特私信我 **[@ai_fengshou](https://x.com/ai_fengshou)**,我会第一时间自行下架。
|
> 请在推特私信我 **[@ai_fengshou](https://x.com/ai_fengshou)**,我会第一时间自行下架。
|
||||||
|
|
||||||
|
<p align="center"><img src="docs/images/viewer-window.png" width="720" alt="Linux 控制端启动窗口:设备列表(设备名已打码)"></p>
|
||||||
|
<p align="center"><sub>Linux 控制端:设备列表(设备名已打码)</sub></p>
|
||||||
|
|
||||||
<p align="center"><img src="docs/images/main-window.png" width="620" alt="Linux 主机主窗口:远程协助与账号"></p>
|
<p align="center"><img src="docs/images/main-window.png" width="620" alt="Linux 主机主窗口:远程协助与账号"></p>
|
||||||
<p align="center"><sub>首次运行时弹出的扫码登录窗口(二维码已打码)</sub></p>
|
<p align="center"><sub>Linux 被控端主窗口:远程协助与账号(设备 ID、验证码和账号已打码)</sub></p>
|
||||||
|
|
||||||
<!-- 截图占位:在官方客户端里远控 Linux 桌面的效果图。请自行截图并去掉账号、设备名等信息后放到
|
<!-- 截图占位:在官方客户端里远控 Linux 桌面的效果图。请自行截图并去掉账号、设备名等信息后放到
|
||||||
docs/images/remote-desktop.png,再取消下面这行的注释。注意不要截入网易的图标和品牌素材。 -->
|
docs/images/remote-desktop.png,再取消下面这行的注释。注意不要截入网易的图标和品牌素材。 -->
|
||||||
@@ -204,6 +209,15 @@ install -Dm644 contrib/desktop/uu-remote-linux.desktop ~/.local/share/applicatio
|
|||||||
再执行 `loginctl enable-linger $USER`,这样开机后不用登录,systemd 用户服务(以及 `XDG_RUNTIME_DIR`、用户 D-Bus)也会自动启动。
|
再执行 `loginctl enable-linger $USER`,这样开机后不用登录,systemd 用户服务(以及 `XDG_RUNTIME_DIR`、用户 D-Bus)也会自动启动。
|
||||||
无头模式下控制端点「远程桌面」会一直显示正在连接,这是正常的。首次登录用 `uuremote-server login --no-gui` 在终端里扫码。
|
无头模式下控制端点「远程桌面」会一直显示正在连接,这是正常的。首次登录用 `uuremote-server login --no-gui` 在终端里扫码。
|
||||||
|
|
||||||
|
**群晖 / NAS(Docker)**:没有桌面的 NAS 可以用容器运行无头模式(文件传输、终端、端口映射),
|
||||||
|
镜像 `ghcr.io/vallierdev/uu-remote-linux`,步骤见 [docker/README.md](docker/README.md)。已在群晖 DSM 7.3(x86_64)上实测三项功能。
|
||||||
|
|
||||||
|
**Mac 终端粘贴文件**:使用 Ghostty 通过 UU 端口映射连接 SSH 时,可安装
|
||||||
|
[Mac SSH 粘贴助手](contrib/macos-ssh/README.md)。在 Finder 复制文件后按 Command-V,
|
||||||
|
助手先通过同一个 SSH 别名上传文件,再插入服务器路径;普通文本和鼠标报告正常透传。
|
||||||
|
Ghostty 标题栏和进度条显示上传进度、已传大小、速度和耗时。
|
||||||
|
这是可选的 Mac 端工具,不会改变官方 UU 内置终端;暂不支持文件夹、拖放或原始截图剪贴板。
|
||||||
|
|
||||||
**虚拟桌面**:需要完整的远程桌面时,用 Xvfb 创建一块虚拟屏幕,再在上面跑一个桌面环境(推荐 Xfce):
|
**虚拟桌面**:需要完整的远程桌面时,用 Xvfb 创建一块虚拟屏幕,再在上面跑一个桌面环境(推荐 Xfce):
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
@@ -377,31 +391,45 @@ xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`。
|
|||||||
也可以凭对方的设备 ID 和验证码发起远程协助。它是实验性功能,与主机 `uuremote-server` 相互独立,默认主机构建和行为不变。
|
也可以凭对方的设备 ID 和验证码发起远程协助。它是实验性功能,与主机 `uuremote-server` 相互独立,默认主机构建和行为不变。
|
||||||
|
|
||||||
**获取**:Release 包从本版本起附带 `uuremote-viewer`(x86_64)。运行时需要 GTK3、SDL2(Ubuntu/Debian:`libsdl2-2.0-0`)和
|
**获取**:Release 包从本版本起附带 `uuremote-viewer`(x86_64)。运行时需要 GTK3、SDL2(Ubuntu/Debian:`libsdl2-2.0-0`)和
|
||||||
`ffmpeg` 命令;剪贴板需要 `wl-clipboard`(Wayland)或 `xclip`/`xsel`(X11);终端功能需要本机终端程序(可用 `TERMINAL` 指定)。
|
`ffmpeg` 命令;SDL 桌面窗口直接同步文本剪贴板(Wayland/X11),无需额外剪贴板工具;终端功能需要本机终端程序(可用 `TERMINAL` 指定)。
|
||||||
从源码构建:`sudo apt install libsdl2-dev`,然后 `cargo build --release --locked --features native-viewer,viewer-sdl --bin uuremote-viewer`。
|
从源码构建:`sudo apt install libsdl2-dev`,然后 `cargo build --release --locked --features native-viewer,viewer-sdl --bin uuremote-viewer`。
|
||||||
|
|
||||||
|
SDL 剪贴板复用桌面窗口,避免 GNOME Wayland 上临时窗口引起的 Dock 跳动(#37)。仅在桌面窗口获得焦点时同步,
|
||||||
|
保留 30 KiB 文本上限;跳过不含支持的文本格式的本地选择,空文本的识别取决于 SDL2 后端。不带 SDL 的构建保留命令后端。
|
||||||
|
SDL2 同步读取选择内容,慢速的剪贴板提供者可能暂时阻塞窗口,桥接超时无法中断该原生调用。
|
||||||
|
GNOME 与官方 Mac 被控端的双向剪贴板尚未验证通过;此修改不解决被控端兼容问题。
|
||||||
|
|
||||||
**使用**:
|
**使用**:
|
||||||
- `uuremote-viewer login`:扫码登录(复用同目录下 `uuremote-server` 的登录窗口;`--no-gui` 为终端二维码)。控制端与主机共用
|
- `uuremote-viewer login`:扫码登录(复用同目录下 `uuremote-server` 的登录窗口;`--no-gui` 为终端二维码)。控制端与主机共用
|
||||||
`~/.config/uuremote/config.json`(可用 `UU_CONFIG` 另指定),因此**不能用同一身份连接本机上的主机**;控制其他设备不受影响。
|
`~/.config/uuremote/config.json`(可用 `UU_CONFIG` 另指定),因此**不能用同一身份连接本机上的主机**;控制其他设备不受影响。
|
||||||
- `uuremote-viewer ui`:启动窗口,列出账号下的设备,可进入桌面、只看模式、文件传输、终端、端口映射、重启/关机、远程协助。
|
- `uuremote-viewer ui`:启动窗口,列出账号下的设备,可进入桌面、只看模式、文件传输、终端、端口映射、重启/关机、远程协助。
|
||||||
- 命令行:`connect DEVICE false - echo legacy [--view-only]`(桌面)、`files DEVICE ls|get|put …`、`terminal DEVICE [SESSION]`、
|
- 命令行:`connect DEVICE false - echo legacy [--view-only]`(桌面)、`files DEVICE ls|get|put …`、`terminal DEVICE [SESSION]`、
|
||||||
`terminal-ui DEVICE NAME`、`portmap DEVICE LOCAL_PORT HOST PORT`(只监听 127.0.0.1)、`power DEVICE reboot|shutdown`、
|
`terminal-ui DEVICE NAME`、`portmap DEVICE LOCAL_PORT HOST PORT`(只监听 127.0.0.1)、`power DEVICE reboot|shutdown`、`unbind DEVICE`(从账号删除设备,启动窗口「更多工具」里也有)、
|
||||||
`assist CONNECT_ID`(验证码从 stdin 读取一行,不能作为参数传入)。`uuremote-viewer --help` 查看全部参数。
|
`assist CONNECT_ID`(验证码从 stdin 读取一行,不能作为参数传入)。`uuremote-viewer --help` 查看全部参数。
|
||||||
- 桌面窗口中按 Ctrl+Alt+F 打开文件窗口(复用同一连接)。`UU_VIEWER_AUDIO=0` 关闭声音,`UU_VIEWER_CLIPBOARD=0` 关闭剪贴板,
|
- 桌面窗口中按 Ctrl+Alt+F 打开文件窗口(复用同一连接)。`UU_VIEWER_AUDIO=0` 关闭声音,`UU_VIEWER_CLIPBOARD=0` 关闭剪贴板,
|
||||||
`UU_VIEWER_VIEW_ONLY=1` 只看不控,`UU_VIEWER_FFMPEG` 指定 ffmpeg 路径。
|
`UU_VIEWER_VIEW_ONLY=1` 只看不控,`UU_VIEWER_FFMPEG` 指定 ffmpeg 路径。
|
||||||
|
- 桌面同时提供 H.264 / H.265,由主机协商选择;`UU_VIEWER_CODEC=h264` 将 SDP 和解码能力限制为 H.264。
|
||||||
|
默认请求 4:2:0;`UU_VIEWER_CHROMA=444` 在 CONTROL 打开后请求真彩(4:4:4)。主机可能回落到 4:2:0,
|
||||||
|
以 CaptureSettingSync 回报为准。两种色度均由 FFmpeg 转为 RGB24 显示。
|
||||||
|
|
||||||
|
Mac 目标使用 macOS 原生键码,Windows / Linux 目标保留 Windows VK。键盘按物理键位置转发,不自动把 Ctrl 改成 Command;
|
||||||
|
控制 Mac 时,PC 键盘的 Super / Win 键对应 Command。仅接受已验证的目标平台值 1(Windows / 本项目 Linux 主机)和 4(Mac),
|
||||||
|
未知值会报错,不猜测键码。账号连接缺少平台字段时查询目标设备;远程协助必须由连接响应提供平台。只看模式不需要此识别。
|
||||||
|
媒体键和部分 JIS 专用键尚未映射;远程协助的 Mac 平台识别及 ISO / JIS 键盘尚未实机验收。
|
||||||
|
|
||||||
**已实测**(作者环境,非全面验收):
|
**已实测**(作者环境,非全面验收):
|
||||||
| 功能 | 官方 Windows 被控端 | 官方 Mac 被控端 | 本项目 Linux 主机 |
|
| 功能 | 官方 Windows 被控端 | 官方 Mac 被控端 | 本项目 Linux 主机 |
|
||||||
|---|---|---|---|
|
|---|---|---|---|
|
||||||
| 桌面画面(H.264) | ✅ | ✅ | ✅ |
|
| 桌面画面(H.264) | ✅ | ✅ | ✅ |
|
||||||
|
| H.265、4:4:4 真彩 | — | — | ✅(虚拟机,libx265) |
|
||||||
| 键盘、鼠标 | ✅ | — | ✅ |
|
| 键盘、鼠标 | ✅ | — | ✅ |
|
||||||
| 文件列表 / 下载 / 上传 | ✅ 列表、下载 | — | ✅ |
|
| 文件列表 / 下载 / 上传 | ✅ | — | ✅ |
|
||||||
| 端口映射、终端 | — | — | ✅ |
|
| 端口映射、终端 | ✅(终端为 PowerShell) | — | ✅ |
|
||||||
| 剪贴板(文本,双向)、声音 | — | — | ✅(虚拟机) |
|
| 剪贴板(文本) | 仅被控端 → 控制端;控制端 → Windows 未生效 | — | ✅ 双向(虚拟机) |
|
||||||
|
| 声音 | 未确认(被控端未播放声音时没有音频包) | — | ✅(虚拟机) |
|
||||||
| 远程协助(ID + 验证码) | — | — | ✅(虚拟机,访客主机) |
|
| 远程协助(ID + 验证码) | — | — | ✅(虚拟机,访客主机) |
|
||||||
| 终端会话列表(新建、双击接回、结束) | — | — | ✅(虚拟机) |
|
| 终端会话列表(新建、双击接回、结束) | — | — | ✅(虚拟机) |
|
||||||
| 重启、关机 | — | — | ✅(虚拟机) |
|
| 重启、关机 | — | — | ✅(虚拟机) |
|
||||||
| Wayland 下的桌面窗口与剪贴板(Hyprland) | — | — | ✅(虚拟机) |
|
| Wayland 下的桌面窗口与剪贴板 | — | — | ✅ Hyprland、KDE;GNOME 窗口正常,剪贴板已于 v0.2.3 改为 SDL 直连(#37,最终版本未重新实机验收) |
|
||||||
|
|
||||||
「—」表示尚未测试。重启/关机只应在可以重启的机器上使用。**未实测**:GNOME/KDE Wayland 下的窗口与剪贴板、
|
「—」表示尚未测试。重启/关机只应在可以重启的机器上使用。**未实测**:官方被控端的 H.265 / 4:4:4、多显示器。日志只记录字段号、大小和状态,不记录键盘、剪贴板、文件内容、验证码或凭据。
|
||||||
H.265 与 4:4:4(目前只解码 H.264 4:2:0)、多显示器。日志只记录字段号、大小和状态,不记录键盘、剪贴板、文件内容、验证码或凭据。
|
|
||||||
@@ -475,6 +475,8 @@ static void build_window(void) {
|
|||||||
static void *ui_main(void *arg) {
|
static void *ui_main(void *arg) {
|
||||||
(void)arg;
|
(void)arg;
|
||||||
int ok = gtk_init_check(NULL, NULL);
|
int ok = gtk_init_check(NULL, NULL);
|
||||||
|
// The CSS hard-codes a light palette; a dark system theme would turn unstyled text white on white (#43).
|
||||||
|
if (ok) g_object_set(gtk_settings_get_default(), "gtk-theme-name", "Adwaita", "gtk-application-prefer-dark-theme", FALSE, NULL);
|
||||||
if (ok) build_window();
|
if (ok) build_window();
|
||||||
pthread_mutex_lock(&init_lock);
|
pthread_mutex_lock(&init_lock);
|
||||||
init_state = ok ? 1 : -1;
|
init_state = ok ? 1 : -1;
|
||||||
|
|||||||
@@ -474,6 +474,8 @@ static gboolean show_window(gpointer d) {
|
|||||||
static void *ui_thread(void *d) {
|
static void *ui_thread(void *d) {
|
||||||
(void)d;
|
(void)d;
|
||||||
if (!gtk_init_check(NULL, NULL)) { pthread_mutex_lock(&init_lock); init_state = -1; pthread_cond_signal(&init_cond); pthread_mutex_unlock(&init_lock); return NULL; }
|
if (!gtk_init_check(NULL, NULL)) { pthread_mutex_lock(&init_lock); init_state = -1; pthread_cond_signal(&init_cond); pthread_mutex_unlock(&init_lock); return NULL; }
|
||||||
|
// Light palette in CSS; keep a dark system theme from making unstyled text white on white (#43).
|
||||||
|
g_object_set(gtk_settings_get_default(), "gtk-theme-name", "Adwaita", "gtk-application-prefer-dark-theme", FALSE, NULL);
|
||||||
show_window(NULL); gtk_main(); return NULL;
|
show_window(NULL); gtk_main(); return NULL;
|
||||||
}
|
}
|
||||||
/* Hidden windows retain their widgets so queued idle callbacks stay valid. */
|
/* Hidden windows retain their widgets so queued idle callbacks stay valid. */
|
||||||
|
|||||||
@@ -117,6 +117,15 @@ static void more_item(GtkButton *b, gpointer kind) {
|
|||||||
if (ok) { connect_kind(k); show_toast("正在发送电源指令…"); }
|
if (ok) { connect_kind(k); show_toast("正在发送电源指令…"); }
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!strcmp(k, "unbind")) {
|
||||||
|
GtkWidget *d = gtk_message_dialog_new(GTK_WINDOW(dialog), GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_NONE,
|
||||||
|
"确定要从账号中删除该设备吗?");
|
||||||
|
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(d), "删除后需要在该设备上重新登录才能再次远控。");
|
||||||
|
gtk_dialog_add_buttons(GTK_DIALOG(d), "取消", GTK_RESPONSE_CANCEL, "删除", GTK_RESPONSE_OK, NULL);
|
||||||
|
int ok = gtk_dialog_run(GTK_DIALOG(d)) == GTK_RESPONSE_OK; gtk_widget_destroy(d);
|
||||||
|
if (ok) { gtk_widget_destroy(dialog); connect_kind(k); show_toast("正在删除设备…"); }
|
||||||
|
return;
|
||||||
|
}
|
||||||
gtk_widget_destroy(dialog);
|
gtk_widget_destroy(dialog);
|
||||||
on_tool(NULL, kind);
|
on_tool(NULL, kind);
|
||||||
}
|
}
|
||||||
@@ -126,9 +135,9 @@ static void open_more(void) {
|
|||||||
GtkWidget *area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)), *box = vbox(10);
|
GtkWidget *area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)), *box = vbox(10);
|
||||||
gtk_container_set_border_width(GTK_CONTAINER(box), 22);
|
gtk_container_set_border_width(GTK_CONTAINER(box), 22);
|
||||||
gtk_box_pack_start(GTK_BOX(box), label("更多工具", "h2"), FALSE, FALSE, 6);
|
gtk_box_pack_start(GTK_BOX(box), label("更多工具", "h2"), FALSE, FALSE, 6);
|
||||||
const char *names[] = {"🗂 文件传输", "▶ 观看模式", "⌨ 终端", "⇄ 端口映射", "⏻ 远程开机", "↻ 重启", "⏻ 关机"};
|
const char *names[] = {"🗂 文件传输", "▶ 观看模式", "⌨ 终端", "⇄ 端口映射", "⏻ 远程开机", "↻ 重启", "⏻ 关机", "🗑 删除设备"};
|
||||||
const char *kinds[] = {"file", "watch", "terminal", "portmap", "wol", "reboot", "shutdown"};
|
const char *kinds[] = {"file", "watch", "terminal", "portmap", "wol", "reboot", "shutdown", "unbind"};
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
GtkWidget *b = gtk_button_new(), *line = hbox(10); add_class(b, "cardbtn");
|
GtkWidget *b = gtk_button_new(), *line = hbox(10); add_class(b, "cardbtn");
|
||||||
gtk_box_pack_start(GTK_BOX(line), label(names[i], "devname"), TRUE, TRUE, 0);
|
gtk_box_pack_start(GTK_BOX(line), label(names[i], "devname"), TRUE, TRUE, 0);
|
||||||
gtk_box_pack_end(GTK_BOX(line), label("›", "hint"), FALSE, FALSE, 0);
|
gtk_box_pack_end(GTK_BOX(line), label("›", "hint"), FALSE, FALSE, 0);
|
||||||
@@ -581,6 +590,8 @@ static gboolean on_delete(GtkWidget *w, GdkEvent *e, gpointer d) { (void)w; (voi
|
|||||||
static void *run(void *arg) {
|
static void *run(void *arg) {
|
||||||
(void)arg;
|
(void)arg;
|
||||||
int ok = gtk_init_check(NULL, NULL);
|
int ok = gtk_init_check(NULL, NULL);
|
||||||
|
// The CSS hard-codes a light palette; a dark system theme would turn unstyled text white on white (#43).
|
||||||
|
if (ok) g_object_set(gtk_settings_get_default(), "gtk-theme-name", "Adwaita", "gtk-application-prefer-dark-theme", FALSE, NULL);
|
||||||
if (ok) {
|
if (ok) {
|
||||||
GtkCssProvider *css = gtk_css_provider_new();
|
GtkCssProvider *css = gtk_css_provider_new();
|
||||||
gtk_css_provider_load_from_data(css, CSS, -1, NULL);
|
gtk_css_provider_load_from_data(css, CSS, -1, NULL);
|
||||||
@@ -658,6 +669,9 @@ int uu_viewer_ui_start(void) {
|
|||||||
/// Lines of "id\tname\tplatform\tonline\tcurrent".
|
/// Lines of "id\tname\tplatform\tonline\tcurrent".
|
||||||
void uu_viewer_ui_set_devices(const char *tsv) {
|
void uu_viewer_ui_set_devices(const char *tsv) {
|
||||||
pthread_mutex_lock(&d_lock);
|
pthread_mutex_lock(&d_lock);
|
||||||
|
/* A refresh (every 30 s) must keep the open device page usable: re-select by id, not reset. */
|
||||||
|
char keep[sizeof devices[0].id] = "";
|
||||||
|
if (selected >= 0 && selected < device_count) snprintf(keep, sizeof keep, "%s", devices[selected].id);
|
||||||
device_count = 0;
|
device_count = 0;
|
||||||
char *copy = strdup(tsv ? tsv : ""), *save = NULL;
|
char *copy = strdup(tsv ? tsv : ""), *save = NULL;
|
||||||
for (char *line = strtok_r(copy, "\n", &save); line && device_count < 64; line = strtok_r(NULL, "\n", &save)) {
|
for (char *line = strtok_r(copy, "\n", &save); line && device_count < 64; line = strtok_r(NULL, "\n", &save)) {
|
||||||
@@ -670,6 +684,7 @@ void uu_viewer_ui_set_devices(const char *tsv) {
|
|||||||
}
|
}
|
||||||
free(copy);
|
free(copy);
|
||||||
selected = -1;
|
selected = -1;
|
||||||
|
for (int i = 0; keep[0] && i < device_count; i++) if (!strcmp(devices[i].id, keep)) { selected = i; break; }
|
||||||
pthread_mutex_unlock(&d_lock);
|
pthread_mutex_unlock(&d_lock);
|
||||||
if (init_state == 1) g_idle_add(apply_devices, NULL);
|
if (init_state == 1) g_idle_add(apply_devices, NULL);
|
||||||
}
|
}
|
||||||
@@ -699,7 +714,8 @@ static int replace_result;
|
|||||||
static gboolean confirm_replace(gpointer d) {
|
static gboolean confirm_replace(gpointer d) {
|
||||||
(void)d;
|
(void)d;
|
||||||
GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
|
GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||||
GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL, "打开后将断开当前与该设备的连接,继续吗?");
|
GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, "打开后将断开当前与该设备的连接,继续吗?");
|
||||||
|
gtk_dialog_add_buttons(GTK_DIALOG(dialog), "取消", GTK_RESPONSE_CANCEL, "继续", GTK_RESPONSE_OK, NULL);
|
||||||
int accepted = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK;
|
int accepted = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK;
|
||||||
gtk_widget_destroy(dialog);
|
gtk_widget_destroy(dialog);
|
||||||
pthread_mutex_lock(&init_lock); replace_result = accepted; pthread_cond_signal(&init_cond); pthread_mutex_unlock(&init_lock);
|
pthread_mutex_lock(&init_lock); replace_result = accepted; pthread_cond_signal(&init_cond); pthread_mutex_unlock(&init_lock);
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
* Zero-Wine dependency, zero-overhead, thread-safe.
|
* Zero-Wine dependency, zero-overhead, thread-safe.
|
||||||
*/
|
*/
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/shm.h>
|
#include <sys/shm.h>
|
||||||
@@ -218,6 +219,54 @@ int x11_key_symbol(unsigned long symbol, int is_press) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Type UTF-8 text the way xdotool does: bind each character's keysym to a spare (unmapped) keycode, press it,
|
||||||
|
* then restore the keycode. Works for CJK text from mobile input methods. Never logs the text. */
|
||||||
|
int x11_type_utf8(const char *text) {
|
||||||
|
if (!g_display || !text) return -1;
|
||||||
|
int min_kc = 0, max_kc = 0, per = 0;
|
||||||
|
XDisplayKeycodes(g_display, &min_kc, &max_kc);
|
||||||
|
KeySym *map = XGetKeyboardMapping(g_display, (KeyCode)min_kc, max_kc - min_kc + 1, &per);
|
||||||
|
if (!map) return -2;
|
||||||
|
int spare = 0;
|
||||||
|
for (int kc = max_kc; kc >= min_kc && !spare; kc--) {
|
||||||
|
int empty = 1;
|
||||||
|
for (int i = 0; i < per; i++) if (map[(kc - min_kc) * per + i] != NoSymbol) { empty = 0; break; }
|
||||||
|
if (empty) spare = kc;
|
||||||
|
}
|
||||||
|
XFree(map);
|
||||||
|
if (!spare) return -3;
|
||||||
|
int typed = 0;
|
||||||
|
const unsigned char *p = (const unsigned char *)text;
|
||||||
|
while (*p) {
|
||||||
|
unsigned cp; int n;
|
||||||
|
if (*p < 0x80) { cp = *p; n = 1; }
|
||||||
|
else if ((*p & 0xE0) == 0xC0 && (p[1] & 0xC0) == 0x80) { cp = ((p[0] & 0x1F) << 6) | (p[1] & 0x3F); n = 2; }
|
||||||
|
else if ((*p & 0xF0) == 0xE0 && (p[1] & 0xC0) == 0x80 && (p[2] & 0xC0) == 0x80) { cp = ((p[0] & 0x0F) << 12) | ((p[1] & 0x3F) << 6) | (p[2] & 0x3F); n = 3; }
|
||||||
|
else if ((*p & 0xF8) == 0xF0 && (p[1] & 0xC0) == 0x80 && (p[2] & 0xC0) == 0x80 && (p[3] & 0xC0) == 0x80) { cp = ((p[0] & 0x07) << 18) | ((p[1] & 0x3F) << 12) | ((p[2] & 0x3F) << 6) | (p[3] & 0x3F); n = 4; }
|
||||||
|
else { p++; continue; }
|
||||||
|
p += n;
|
||||||
|
KeySym sym;
|
||||||
|
if (cp == '\n') sym = 0xff0d; /* Return */
|
||||||
|
else if (cp == '\t') sym = 0xff09; /* Tab */
|
||||||
|
else if (cp < 0x20 || cp == 0x7f || (cp >= 0x80 && cp < 0xa0)) continue;
|
||||||
|
else if (cp < 0x100) sym = cp; /* Latin-1 keysyms equal the code point */
|
||||||
|
else sym = 0x1000000 | cp; /* Unicode keysym */
|
||||||
|
KeySym syms[2] = { sym, sym };
|
||||||
|
XChangeKeyboardMapping(g_display, spare, 2, syms, 1);
|
||||||
|
XSync(g_display, False);
|
||||||
|
usleep(8000); /* let clients see the MappingNotify before the key */
|
||||||
|
XTestFakeKeyEvent(g_display, spare, True, CurrentTime);
|
||||||
|
XTestFakeKeyEvent(g_display, spare, False, CurrentTime);
|
||||||
|
XSync(g_display, False);
|
||||||
|
usleep(8000);
|
||||||
|
typed++;
|
||||||
|
}
|
||||||
|
KeySym none[2] = { NoSymbol, NoSymbol };
|
||||||
|
XChangeKeyboardMapping(g_display, spare, 2, none, 1);
|
||||||
|
XSync(g_display, False);
|
||||||
|
return typed;
|
||||||
|
}
|
||||||
|
|
||||||
int x11_key_event(int keycode, int is_press) {
|
int x11_key_event(int keycode, int is_press) {
|
||||||
if (!g_display) return -1;
|
if (!g_display) return -1;
|
||||||
if (is_press) track_injected_key(keycode, 1);
|
if (is_press) track_injected_key(keycode, 1);
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
__pycache__/
|
||||||
@@ -0,0 +1,103 @@
|
|||||||
|
# Paste copied files into a UU-mapped SSH terminal on macOS
|
||||||
|
|
||||||
|
This optional Mac companion uploads files copied in Finder when you paste them
|
||||||
|
into Ghostty, then inserts shell-quoted **remote** paths into the terminal.
|
||||||
|
For example, copy an image in Finder, connect with `uu-ssh-paste`, and press
|
||||||
|
Command-V at a remote shell prompt or in a terminal application's text field.
|
||||||
|
The file is available on the server before its path is inserted. No Enter is sent.
|
||||||
|
|
||||||
|
The connection is Ghostty → OpenSSH → your existing UU TCP mapping → the Linux
|
||||||
|
host's SSH server. Both terminal traffic and uploads use the same SSH alias.
|
||||||
|
The companion does not create a mapping or fall back to a public network route.
|
||||||
|
The official Mac UU application's built-in terminal is unaffected.
|
||||||
|
|
||||||
|
## Install and connect
|
||||||
|
|
||||||
|
On the controlling Mac, install Python 3 and Apple's command-line developer
|
||||||
|
tools (for `swiftc`), then run from this repository:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sh contrib/macos-ssh/install.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
Enable port mapping on your Linux UU host (`UU_PORT_MAPPING=1`); in the official
|
||||||
|
Mac UU app, map a **local loopback** port to `127.0.0.1:22` on that host. Keep
|
||||||
|
the mapping connected. Configure an SSH alias for that local port, using your
|
||||||
|
existing key and the server's verified host key. For example:
|
||||||
|
|
||||||
|
```sshconfig
|
||||||
|
Host uu-server
|
||||||
|
HostName 127.0.0.1
|
||||||
|
Port 22243
|
||||||
|
User your-linux-user
|
||||||
|
IdentityFile ~/.ssh/id_ed25519
|
||||||
|
IdentitiesOnly yes
|
||||||
|
HostKeyAlias your-existing-server-host-key-name
|
||||||
|
StrictHostKeyChecking yes
|
||||||
|
ProxyCommand none
|
||||||
|
ProxyJump none
|
||||||
|
```
|
||||||
|
|
||||||
|
In Ghostty, connect using that alias:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
uu-ssh-paste uu-server
|
||||||
|
# Or use a separate SSH config:
|
||||||
|
uu-ssh-paste -F ~/.config/your-uu-ssh.conf uu-server
|
||||||
|
```
|
||||||
|
|
||||||
|
Copy one or several files in Finder with Command-C, switch to the connected
|
||||||
|
terminal, and press Command-V. Each paste creates a unique mode-0700 directory
|
||||||
|
under `~/.local/share/uuremote/uploads/` on the server. Files remain there until
|
||||||
|
you remove them. Filenames with spaces, Unicode and shell metacharacters are
|
||||||
|
supported. Existing files are never overwritten by another paste batch.
|
||||||
|
|
||||||
|
Text paste, SSH exit statuses, terminal resizing and mouse reports pass through.
|
||||||
|
The remote application still controls whether it receives bracketed paste and
|
||||||
|
mouse movement. Ctrl-C during a transfer cancels that paste. Input typed while
|
||||||
|
the upload is running is queued, so Enter cannot overtake the uploaded path.
|
||||||
|
|
||||||
|
Ghostty's title bar shows the connection phase, upload percentage, transferred
|
||||||
|
size, transfer speed and elapsed time. Its progress indicator tracks the upload
|
||||||
|
too. Progress is derived from OpenSSH's transfer meter, aggregated across copied
|
||||||
|
files; it is not a simulated timer. The original terminal title is restored when
|
||||||
|
the upload finishes, fails or is cancelled, and no terminal cells are overwritten.
|
||||||
|
Filename and error output from SCP stays private; only numeric progress is shown.
|
||||||
|
|
||||||
|
## Scope and failure behavior
|
||||||
|
|
||||||
|
- Only file-URL clipboard entries copied in Finder are uploaded, and only when
|
||||||
|
they match the complete pasted path list. Pasting a textual local path or a
|
||||||
|
command does not upload anything. The clipboard is never modified or polled
|
||||||
|
in the background; file URLs are read only during a potential file paste.
|
||||||
|
- Folder paste, drag-and-drop and raw screenshot/image clipboard data are not
|
||||||
|
supported. Save an image as a file and copy that file in Finder first.
|
||||||
|
- The SSH server must support SFTP. Upload authentication is noninteractive
|
||||||
|
(`BatchMode=yes`); the alias must already work with a key or SSH agent.
|
||||||
|
- Missing files, duplicate basenames in a batch, control characters in filenames,
|
||||||
|
failed uploads and clipboard-reader failures do not insert a misleading path.
|
||||||
|
Failed transfer directories are removed on a best-effort basis. No file names,
|
||||||
|
clipboard contents, file contents, terminal input or SSH error text are logged.
|
||||||
|
- A paste is limited to 256 files and 8 MiB of pasted path/text data. Transfers
|
||||||
|
time out after five minutes. File sizes themselves are not limited to 8 MiB.
|
||||||
|
- The companion keeps local bracketed-paste framing enabled while the session
|
||||||
|
runs and removes framing for remote applications that have not requested it.
|
||||||
|
No global Ghostty configuration, remote desktop or Wine component is needed.
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
|
||||||
|
```sh
|
||||||
|
python3 -m unittest discover -s contrib/macos-ssh -p 'test_*.py' -v
|
||||||
|
swiftc -O contrib/macos-ssh/clipboard-files.swift -o /tmp/clipboard-files
|
||||||
|
/tmp/clipboard-files --self-test
|
||||||
|
# Optional real SSH/SFTP acceptance test over an already connected UU mapping:
|
||||||
|
UU_PASTE_TEST_HOST=uu-server UU_PASTE_TEST_CONFIG="$HOME/.config/your-uu-ssh.conf" \
|
||||||
|
python3 -m unittest discover -s contrib/macos-ssh -p 'test_*.py' -v
|
||||||
|
```
|
||||||
|
|
||||||
|
The Swift test uses a private pasteboard and never touches the user's clipboard.
|
||||||
|
Python tests exercise fragmented paste framing, shell quoting, plain text,
|
||||||
|
mouse reports, failed uploads, queued Enter, PTY restoration and exit status.
|
||||||
|
The optional live tests transfer synthetic binary files and verify hashes,
|
||||||
|
including a throttled upload that must report intermediate byte progress.
|
||||||
|
Physical Command-C/Command-V behavior in Finder/Ghostty remains a manual check.
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import AppKit
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
// Only file URLs are read. Text, images and the general pasteboard are never
|
||||||
|
// written, and the result is consumed privately by the SSH companion.
|
||||||
|
func filePaths(_ board: NSPasteboard) -> [String] {
|
||||||
|
let options: [NSPasteboard.ReadingOptionKey: Any] = [.urlReadingFileURLsOnly: true]
|
||||||
|
return (board.readObjects(forClasses: [NSURL.self], options: options) as? [URL] ?? [])
|
||||||
|
.filter { $0.isFileURL }.map { $0.path }
|
||||||
|
}
|
||||||
|
|
||||||
|
if CommandLine.arguments.dropFirst().first == "--self-test" {
|
||||||
|
// An isolated pasteboard: tests never read or overwrite the user's clipboard.
|
||||||
|
let board = NSPasteboard.withUniqueName()
|
||||||
|
defer { board.releaseGlobally() }
|
||||||
|
let paths = ["/tmp/space and 中文.png", "/tmp/quote'$(literal).txt"]
|
||||||
|
board.writeObjects(paths.map { NSURL(fileURLWithPath: $0) })
|
||||||
|
precondition(filePaths(board) == paths)
|
||||||
|
board.clearContents()
|
||||||
|
board.setString("/tmp/text-is-not-a-file", forType: .string)
|
||||||
|
precondition(filePaths(board).isEmpty)
|
||||||
|
board.clearContents()
|
||||||
|
board.writeObjects([NSURL(string: "https://example.invalid/file")!])
|
||||||
|
precondition(filePaths(board).isEmpty)
|
||||||
|
print("Private pasteboard tests passed")
|
||||||
|
} else {
|
||||||
|
let data = try JSONSerialization.data(withJSONObject: filePaths(.general))
|
||||||
|
FileHandle.standardOutput.write(data)
|
||||||
|
}
|
||||||
Executable
+27
@@ -0,0 +1,27 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
if [ "$(uname -s)" != Darwin ]; then
|
||||||
|
echo 'Install this companion on the controlling Mac.' >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
command -v python3 >/dev/null
|
||||||
|
source_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||||
|
data_dir="$HOME/.local/share/uuremote/macos-ssh"
|
||||||
|
bin_dir="$HOME/.local/bin"
|
||||||
|
mkdir -p "$data_dir" "$bin_dir"
|
||||||
|
scratch=$(mktemp -d)
|
||||||
|
trap 'rm -rf "$scratch"' EXIT HUP INT TERM
|
||||||
|
swiftc -O "$source_dir/clipboard-files.swift" -o "$scratch/clipboard-files"
|
||||||
|
"$scratch/clipboard-files" --self-test
|
||||||
|
install -m 755 "$scratch/clipboard-files" "$data_dir/clipboard-files"
|
||||||
|
install -m 755 "$source_dir/uu-ssh-paste" "$data_dir/uu-ssh-paste"
|
||||||
|
if [ -e "$bin_dir/uu-ssh-paste" ] && [ ! -L "$bin_dir/uu-ssh-paste" ]; then
|
||||||
|
echo 'An existing uu-ssh-paste file was preserved; inspect it before replacing it.' >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if [ -L "$bin_dir/uu-ssh-paste" ] && [ "$(readlink "$bin_dir/uu-ssh-paste")" != "$data_dir/uu-ssh-paste" ]; then
|
||||||
|
echo 'An unrelated uu-ssh-paste symlink was preserved; inspect it before replacing it.' >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
ln -sfn "$data_dir/uu-ssh-paste" "$bin_dir/uu-ssh-paste"
|
||||||
|
echo 'Installed uu-ssh-paste. Connect using your existing UU SSH alias.'
|
||||||
@@ -0,0 +1,472 @@
|
|||||||
|
"""Synthetic fixtures only; never inspect or change the general pasteboard."""
|
||||||
|
|
||||||
|
import base64
|
||||||
|
import errno
|
||||||
|
import hashlib
|
||||||
|
import importlib.machinery
|
||||||
|
import importlib.util
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import pty
|
||||||
|
import select
|
||||||
|
import shlex
|
||||||
|
import signal
|
||||||
|
import struct
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import termios
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import unittest
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
|
||||||
|
SCRIPT = Path(__file__).with_name("uu-ssh-paste")
|
||||||
|
loader = importlib.machinery.SourceFileLoader("uu_paste", str(SCRIPT))
|
||||||
|
spec = importlib.util.spec_from_loader(loader.name, loader)
|
||||||
|
paste = importlib.util.module_from_spec(spec)
|
||||||
|
loader.exec_module(paste)
|
||||||
|
|
||||||
|
|
||||||
|
class FramingTests(unittest.TestCase):
|
||||||
|
def test_paste_at_every_fragment_boundary_and_multiple_pastes(self):
|
||||||
|
wire = b"key" + paste.START + b"hello\nworld" + paste.END + b"\r"
|
||||||
|
expected = [("data", b"key"), ("paste", b"hello\nworld"), ("data", b"\r")]
|
||||||
|
for split in range(len(wire) + 1):
|
||||||
|
frames = paste.InputFrames()
|
||||||
|
events = frames.feed(wire[:split]) + frames.feed(wire[split:])
|
||||||
|
merged = []
|
||||||
|
for kind, data in events:
|
||||||
|
if kind == "data" and merged and merged[-1][0] == "data":
|
||||||
|
merged[-1] = (kind, merged[-1][1] + data)
|
||||||
|
else:
|
||||||
|
merged.append((kind, data))
|
||||||
|
self.assertEqual(merged, expected)
|
||||||
|
frames = paste.InputFrames()
|
||||||
|
self.assertEqual(frames.feed(paste.START + b"a" + paste.END + paste.START + b"b" + paste.END),
|
||||||
|
[("paste", b"a"), ("paste", b"b")])
|
||||||
|
|
||||||
|
def test_mouse_and_arrow_bytes_and_standalone_escape(self):
|
||||||
|
raw = b"\x1b[<2;12;4M\x1b[<2;12;4m\x1b[<35;13;5M\x1b[A\x1b"
|
||||||
|
frames = paste.InputFrames()
|
||||||
|
output = b"".join(data for kind, data in frames.feed(raw) if kind == "data")
|
||||||
|
self.assertEqual(output + frames.flush_key(), raw)
|
||||||
|
|
||||||
|
def test_oversized_paste_is_discarded_and_parser_recovers(self):
|
||||||
|
frames = paste.InputFrames()
|
||||||
|
with mock.patch.object(paste, "MAX_PASTE", 16):
|
||||||
|
self.assertEqual(frames.feed(paste.START + b"x" * 40), [])
|
||||||
|
self.assertEqual(frames.feed(paste.END + b"next")[-1], ("data", b"next"))
|
||||||
|
self.assertFalse(frames.pasting)
|
||||||
|
self.assertLess(len(frames.buffer), 16)
|
||||||
|
|
||||||
|
def test_remote_modes_track_split_sequences_without_disabling_local_framing(self):
|
||||||
|
for split in range(len(paste.DISABLE) + 1):
|
||||||
|
modes = paste.OutputModes()
|
||||||
|
modes.bracketed = True
|
||||||
|
result = modes.feed(paste.DISABLE[:split]) + modes.feed(paste.DISABLE[split:])
|
||||||
|
self.assertEqual(result, paste.ENABLE)
|
||||||
|
self.assertFalse(modes.bracketed)
|
||||||
|
modes = paste.OutputModes()
|
||||||
|
self.assertEqual(modes.feed(b"\x1b[?1;2004h"), b"\x1b[?1h" + paste.ENABLE)
|
||||||
|
self.assertTrue(modes.bracketed)
|
||||||
|
self.assertEqual(modes.feed(b"\x1b[?2004;25l"), b"\x1b[?25l" + paste.ENABLE)
|
||||||
|
self.assertFalse(modes.bracketed)
|
||||||
|
self.assertEqual(modes.feed(b"\x1bc"), b"\x1bc" + paste.ENABLE)
|
||||||
|
raw = b"\x1b[31mhello\x1b[0m\x1b]0;title\x07"
|
||||||
|
self.assertEqual(modes.feed(raw), raw)
|
||||||
|
|
||||||
|
|
||||||
|
class FileTests(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.temp = tempfile.TemporaryDirectory()
|
||||||
|
self.addCleanup(self.temp.cleanup)
|
||||||
|
self.root = Path(self.temp.name)
|
||||||
|
self.files = [self.root / "space 中文.png", self.root / "quote'$(literal);.txt"]
|
||||||
|
for path in self.files:
|
||||||
|
path.write_bytes(os.urandom(1025))
|
||||||
|
self.paths = [str(p) for p in self.files]
|
||||||
|
# Ghostty uses backslash escaping rather than POSIX single quotes.
|
||||||
|
escape = "\\ ()[]{}<>\"'`!#$&;|*?\t"
|
||||||
|
self.payload = " ".join("".join("\\" + c if c in escape else c for c in p)
|
||||||
|
for p in self.paths).encode()
|
||||||
|
|
||||||
|
def test_finder_files_match_ghostty_escaping(self):
|
||||||
|
self.assertEqual(paste.copied_files(self.payload, lambda: self.paths), self.paths)
|
||||||
|
|
||||||
|
def test_plain_text_and_pasted_commands_do_not_upload(self):
|
||||||
|
clipboard = mock.Mock(return_value=[])
|
||||||
|
for value in (b"hello\nworld", b"cat /tmp/file", b"\xff", b"'unterminated"):
|
||||||
|
self.assertIsNone(paste.copied_files(value, clipboard))
|
||||||
|
clipboard.assert_not_called()
|
||||||
|
self.assertIsNone(paste.copied_files(self.payload, lambda: []))
|
||||||
|
self.assertIsNone(paste.copied_files(self.payload, lambda: self.paths[::-1]))
|
||||||
|
|
||||||
|
def test_missing_files_folders_duplicate_names_and_control_characters_rejected(self):
|
||||||
|
cases = [[str(self.root)], [str(self.root / "missing")]]
|
||||||
|
newline = self.root / "bad\nname"
|
||||||
|
newline.write_text("synthetic")
|
||||||
|
cases.append([str(newline)])
|
||||||
|
folder = self.root / "another"
|
||||||
|
folder.mkdir()
|
||||||
|
duplicate = folder / self.files[0].name
|
||||||
|
duplicate.write_text("synthetic")
|
||||||
|
cases.append([self.paths[0], str(duplicate)])
|
||||||
|
for paths in cases:
|
||||||
|
with self.assertRaises(paste.PasteError):
|
||||||
|
paste.copied_files(shlex.join(paths).encode(), lambda: paths)
|
||||||
|
|
||||||
|
def test_upload_paths_are_quoted_and_failed_batch_is_cleaned(self):
|
||||||
|
uploader = paste.Uploader(self.root / "config with spaces", "uu-test")
|
||||||
|
directory = b"/home/test user/.local/share/uuremote/uploads/paste.ABCD1234\n"
|
||||||
|
with mock.patch.object(paste, "run_private", side_effect=[directory, b""]) as run:
|
||||||
|
result = uploader.upload(self.paths, threading.Event())
|
||||||
|
self.assertEqual(shlex.split(result.decode()),
|
||||||
|
[directory.decode().strip() + "/" + p.name for p in self.files])
|
||||||
|
self.assertEqual(run.call_args_list[1].args[0][-3:-1], self.paths)
|
||||||
|
self.assertNotIn("-O", run.call_args_list[1].args[0])
|
||||||
|
self.assertIn("BatchMode=yes", run.call_args_list[1].args[0])
|
||||||
|
with mock.patch.object(paste, "run_private", side_effect=[directory, paste.PasteError("failed"), b""]) as run:
|
||||||
|
with self.assertRaises(paste.PasteError):
|
||||||
|
uploader.upload(self.paths, threading.Event())
|
||||||
|
cleanup = run.call_args_list[2].args[0][-1]
|
||||||
|
self.assertEqual(shlex.split(cleanup), ["rm", "-rf", "--", directory.decode().strip()])
|
||||||
|
|
||||||
|
def test_bad_destination_and_bad_clipboard_fail_without_content_in_error(self):
|
||||||
|
uploader = paste.Uploader(None, "uu-test")
|
||||||
|
for output in (b"relative", b"/tmp/invalid", b"/tmp/paste.abc\nextra", b"\xff"):
|
||||||
|
with mock.patch.object(paste, "run_private", return_value=output):
|
||||||
|
with self.assertRaises(paste.PasteError):
|
||||||
|
uploader.upload(self.paths, threading.Event())
|
||||||
|
with mock.patch.object(paste.subprocess, "run", return_value=subprocess.CompletedProcess([], 0, b"{}")):
|
||||||
|
with self.assertRaises(paste.PasteError):
|
||||||
|
paste.clipboard_paths("helper")
|
||||||
|
|
||||||
|
def test_cancellation_stops_transfer(self):
|
||||||
|
cancel = threading.Event()
|
||||||
|
cancel.set()
|
||||||
|
with self.assertRaisesRegex(paste.PasteError, "cancelled"):
|
||||||
|
paste.run_private([sys.executable, "-c", "import time;time.sleep(30)"], cancel, 3)
|
||||||
|
|
||||||
|
|
||||||
|
class ProgressTests(unittest.TestCase):
|
||||||
|
def test_numeric_meter_handles_fragments_multiple_files_and_rounding(self):
|
||||||
|
for fragment in (1, 7, 9999):
|
||||||
|
reports = []
|
||||||
|
meter = paste.SCPMeter([1048576, 1025], lambda *args: reports.append(args))
|
||||||
|
wire = (b"\rprivate-file 0% 0 0.0KB/s --:-- ETA"
|
||||||
|
b"\rprivate-file 50% 512KB 128.0KB/s 00:04 ETA"
|
||||||
|
b"\rprivate-file 100% 1024KB 128.0KB/s 00:08\r\n"
|
||||||
|
b"\rsecret-other 0% 0 0.0KB/s --:-- ETA"
|
||||||
|
b"\rsecret-other 100% 1025 64.0KB/s 00:00\r\n")
|
||||||
|
for offset in range(0, len(wire), fragment):
|
||||||
|
meter.feed(wire[offset:offset + fragment])
|
||||||
|
self.assertIn(("uploading", 524288, 1049601, 131072), reports)
|
||||||
|
self.assertEqual(reports[-1], ("uploading", 1049601, 1049601, 65536))
|
||||||
|
self.assertEqual(meter.index, 2)
|
||||||
|
self.assertEqual([p[1] for p in reports], sorted(p[1] for p in reports))
|
||||||
|
self.assertNotIn("private-file", repr(reports))
|
||||||
|
self.assertNotIn("secret-other", repr(reports))
|
||||||
|
|
||||||
|
def test_unrecognized_output_is_not_displayed_and_buffer_is_bounded(self):
|
||||||
|
reports = []
|
||||||
|
meter = paste.SCPMeter([10], lambda *args: reports.append(args))
|
||||||
|
meter.feed(b"private error " * 10000 + b" 999% 5 1.0KB/s 00:00\n")
|
||||||
|
self.assertEqual(reports, [])
|
||||||
|
self.assertLessEqual(len(meter.buffer), 512)
|
||||||
|
|
||||||
|
def test_metered_process_captures_progress_privately_and_preserves_failure_cancellation(self):
|
||||||
|
reports = []
|
||||||
|
code = ("import os;assert os.isatty(1);assert os.tcgetpgrp(1)==os.getpgrp();"
|
||||||
|
"os.write(1,b'\\rprivate-file 100% 1025 64.0KB/s 00:00\\n')")
|
||||||
|
paste.run_metered([sys.executable, "-c", code], threading.Event(), 5,
|
||||||
|
paste.SCPMeter([1025], lambda *args: reports.append(args)))
|
||||||
|
self.assertEqual(reports[-1], ("uploading", 1025, 1025, 65536))
|
||||||
|
with self.assertRaises(paste.PasteError):
|
||||||
|
paste.run_metered([sys.executable, "-c", "raise SystemExit(2)"], threading.Event(), 5,
|
||||||
|
paste.SCPMeter([1], lambda *_: None))
|
||||||
|
cancel = threading.Event()
|
||||||
|
cancel.set()
|
||||||
|
with self.assertRaises(paste.PasteError):
|
||||||
|
paste.run_metered([sys.executable, "-c", "import time;time.sleep(30)"], cancel, 5,
|
||||||
|
paste.SCPMeter([1], lambda *_: None))
|
||||||
|
|
||||||
|
def test_display_restores_split_remote_title_and_never_changes_terminal_cells(self):
|
||||||
|
output = []
|
||||||
|
display = paste.UploadDisplay(output.append)
|
||||||
|
for byte in b"\x1b]2;remote app title\x1b\\":
|
||||||
|
display.observe(bytes([byte]))
|
||||||
|
with mock.patch.object(paste.time, "monotonic", return_value=10):
|
||||||
|
display.start(1048576)
|
||||||
|
with mock.patch.object(paste.time, "monotonic", return_value=13):
|
||||||
|
display.update("uploading", 524288, 1048576, 131072)
|
||||||
|
display.render()
|
||||||
|
display.render() # Throttled, not a second display update.
|
||||||
|
self.assertEqual(len(output), 2)
|
||||||
|
self.assertIn(b"Connecting", output[0])
|
||||||
|
self.assertIn(b"Uploading 50% | 512.0 KiB/1.0 MiB | 128.0 KiB/s | 3s", output[1])
|
||||||
|
self.assertIn(b"\x1b]9;4;1;50\x1b\\", output[1])
|
||||||
|
display.observe(b"\x1b]0;new remote title\x07")
|
||||||
|
display.finish()
|
||||||
|
self.assertEqual(output[-1], b"\x1b]9;4;0\x1b\\\x1b]2;new remote title\x1b\\")
|
||||||
|
display.finish()
|
||||||
|
self.assertEqual(len(output), 3)
|
||||||
|
self.assertTrue(all(b"\n" not in item and b"\r" not in item for item in output))
|
||||||
|
|
||||||
|
|
||||||
|
HARNESS = r'''
|
||||||
|
import os, runpy, sys, time, json
|
||||||
|
h = runpy.run_path(sys.argv[1])
|
||||||
|
paths, remote, mode, fail = json.loads(sys.argv[2])
|
||||||
|
class Upload:
|
||||||
|
def upload(self, files, cancel, progress):
|
||||||
|
assert files == paths
|
||||||
|
progress('uploading',512,1024,256)
|
||||||
|
time.sleep(0.1)
|
||||||
|
if fail == 'cancel':
|
||||||
|
assert cancel.wait(5)
|
||||||
|
if fail:
|
||||||
|
raise h['PasteError']('synthetic failure')
|
||||||
|
return remote.encode()
|
||||||
|
child = """import os,tty,base64
|
||||||
|
tty.setraw(0)
|
||||||
|
os.write(1, %r + b'CHILD_READY')
|
||||||
|
data=b''
|
||||||
|
while not data.endswith(b'\\r'):
|
||||||
|
part=os.read(0,65536)
|
||||||
|
if not part: break
|
||||||
|
data+=part
|
||||||
|
os.write(1,b'RECEIVED:' + base64.b64encode(data) + b':DONE')
|
||||||
|
raise SystemExit(7)
|
||||||
|
""" % (h['ENABLE'] if mode else h['DISABLE'])
|
||||||
|
raise SystemExit(h['relay']([sys.executable,'-c',child], lambda: paths, Upload()))
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class RelayTests(unittest.TestCase):
|
||||||
|
def test_resize_and_signal_shutdown_restore_terminal(self):
|
||||||
|
child = r'''
|
||||||
|
import fcntl,os,signal,struct,termios,tty
|
||||||
|
tty.setraw(0)
|
||||||
|
def resized(*_):
|
||||||
|
rows,cols,_,_=struct.unpack('HHHH',fcntl.ioctl(0,termios.TIOCGWINSZ,bytes(8)))
|
||||||
|
os.write(1,('SIZE:%d,%d;'%(rows,cols)).encode())
|
||||||
|
signal.signal(signal.SIGWINCH,resized)
|
||||||
|
resized()
|
||||||
|
while True: signal.pause()
|
||||||
|
'''
|
||||||
|
harness = "import runpy,sys;h=runpy.run_path(sys.argv[1]);h['relay']([sys.executable,'-c',sys.argv[2]],lambda:[],None)"
|
||||||
|
master, slave = pty.openpty()
|
||||||
|
original = termios.tcgetattr(slave)
|
||||||
|
import fcntl
|
||||||
|
fcntl.ioctl(slave, termios.TIOCSWINSZ, struct.pack("HHHH", 24, 80, 0, 0))
|
||||||
|
proc = subprocess.Popen([sys.executable, "-c", harness, str(SCRIPT), child],
|
||||||
|
stdin=slave, stdout=slave, stderr=slave)
|
||||||
|
output = bytearray()
|
||||||
|
|
||||||
|
def until(marker):
|
||||||
|
deadline = time.monotonic() + 5
|
||||||
|
while marker not in output:
|
||||||
|
self.assertLess(time.monotonic(), deadline, "Resize was not forwarded")
|
||||||
|
if select.select([master], [], [], 0.1)[0]:
|
||||||
|
output.extend(os.read(master, 65536))
|
||||||
|
try:
|
||||||
|
until(b"SIZE:24,80;")
|
||||||
|
fcntl.ioctl(slave, termios.TIOCSWINSZ, struct.pack("HHHH", 31, 110, 0, 0))
|
||||||
|
proc.send_signal(signal.SIGWINCH)
|
||||||
|
until(b"SIZE:31,110;")
|
||||||
|
proc.send_signal(signal.SIGTERM)
|
||||||
|
self.assertEqual(proc.wait(timeout=5), 128 + signal.SIGTERM)
|
||||||
|
restored = termios.tcgetattr(slave)
|
||||||
|
restored[3] &= ~getattr(termios, "PENDIN", 0)
|
||||||
|
original[3] &= ~getattr(termios, "PENDIN", 0)
|
||||||
|
self.assertEqual(restored, original)
|
||||||
|
finally:
|
||||||
|
if proc.poll() is None:
|
||||||
|
proc.kill()
|
||||||
|
proc.wait()
|
||||||
|
os.close(master)
|
||||||
|
os.close(slave)
|
||||||
|
|
||||||
|
def test_real_pty_upload_order_mouse_text_failure_exit_status_and_restoration(self):
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
file = Path(tmp) / "paste space 中文.txt"
|
||||||
|
file.write_text("synthetic fixture")
|
||||||
|
mouse = b"\x1b[<2;2;3M\x1b[<2;2;3m\x1b[<35;3;4M"
|
||||||
|
for mode, fail, ordinary in ((False, False, False), (True, False, False),
|
||||||
|
(True, True, False), (False, False, True),
|
||||||
|
(True, False, True), (True, 'cancel', False)):
|
||||||
|
with self.subTest(mode=mode, fail=fail, ordinary=ordinary):
|
||||||
|
master, slave = pty.openpty()
|
||||||
|
original = termios.tcgetattr(slave)
|
||||||
|
remote = shlex.quote("/home/test/.local/share/uuremote/uploads/paste.test/space 中文.txt")
|
||||||
|
config = json.dumps([[str(file)], remote, mode, fail])
|
||||||
|
proc = subprocess.Popen([sys.executable, "-c", HARNESS, str(SCRIPT), config],
|
||||||
|
stdin=slave, stdout=slave, stderr=slave)
|
||||||
|
output = bytearray()
|
||||||
|
try:
|
||||||
|
deadline = time.monotonic() + 10
|
||||||
|
while b"CHILD_READY" not in output:
|
||||||
|
self.assertLess(time.monotonic(), deadline, "PTY harness did not start")
|
||||||
|
if select.select([master], [], [], 0.1)[0]:
|
||||||
|
output.extend(os.read(master, 65536))
|
||||||
|
payload = b"ordinary\ntext" if ordinary else shlex.quote(str(file)).encode()
|
||||||
|
wire = mouse + paste.START + payload + paste.END
|
||||||
|
wire += b"\x03\r" if fail == 'cancel' else b"\r"
|
||||||
|
for byte in wire:
|
||||||
|
os.write(master, bytes([byte]))
|
||||||
|
while proc.poll() is None or select.select([master], [], [], 0)[0]:
|
||||||
|
self.assertLess(time.monotonic(), deadline, "PTY relay did not exit")
|
||||||
|
if select.select([master], [], [], 0.1)[0]:
|
||||||
|
try:
|
||||||
|
chunk = os.read(master, 65536)
|
||||||
|
except OSError as error:
|
||||||
|
if error.errno == errno.EIO:
|
||||||
|
break
|
||||||
|
raise
|
||||||
|
if not chunk:
|
||||||
|
break
|
||||||
|
output.extend(chunk)
|
||||||
|
self.assertEqual(proc.wait(timeout=2), 7)
|
||||||
|
encoded = bytes(output).split(b"RECEIVED:", 1)[1].split(b":DONE", 1)[0]
|
||||||
|
expected = b"" if fail else paste.paste_bytes(payload if ordinary else remote.encode(), mode)
|
||||||
|
interrupt = b"\x03" if fail == 'cancel' else b""
|
||||||
|
self.assertEqual(base64.b64decode(encoded), mouse + expected + interrupt + b"\r")
|
||||||
|
restored = termios.tcgetattr(slave)
|
||||||
|
# macOS sets PENDIN when restoring canonical input. It
|
||||||
|
# is kernel bookkeeping, not an unrestored user mode.
|
||||||
|
restored[3] &= ~getattr(termios, "PENDIN", 0)
|
||||||
|
original[3] &= ~getattr(termios, "PENDIN", 0)
|
||||||
|
self.assertEqual(restored, original)
|
||||||
|
if fail:
|
||||||
|
self.assertNotIn(str(file).encode(), output)
|
||||||
|
if not ordinary:
|
||||||
|
self.assertIn(b"UU upload: Connecting", output)
|
||||||
|
self.assertIn(b"\x1b]9;4;0\x1b\\", output)
|
||||||
|
if fail == 'cancel':
|
||||||
|
self.assertIn(b"UU upload: Cancelling", output)
|
||||||
|
finally:
|
||||||
|
if proc.poll() is None:
|
||||||
|
proc.kill()
|
||||||
|
proc.wait()
|
||||||
|
os.close(master)
|
||||||
|
os.close(slave)
|
||||||
|
|
||||||
|
|
||||||
|
@unittest.skipUnless(os.environ.get("UU_PASTE_TEST_HOST"), "set UU_PASTE_TEST_HOST for a real mapped SSH test")
|
||||||
|
class MappedSSHTests(unittest.TestCase):
|
||||||
|
def test_live_upload_reports_intermediate_progress_and_verified_completion(self):
|
||||||
|
host = os.environ["UU_PASTE_TEST_HOST"]
|
||||||
|
uploader = paste.Uploader(os.environ.get("UU_PASTE_TEST_CONFIG"), host)
|
||||||
|
uploader.scp.extend(["-l", "1024"]) # Deliberately slow a synthetic transfer.
|
||||||
|
reports = []
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
source = Path(tmp) / "private progress 中文.bin"
|
||||||
|
source.write_bytes(os.urandom(1024 * 1024))
|
||||||
|
paths = shlex.split(uploader.upload([str(source)], threading.Event(),
|
||||||
|
lambda *args: reports.append(args)).decode())
|
||||||
|
try:
|
||||||
|
total = source.stat().st_size
|
||||||
|
self.assertEqual(reports[0], ("connecting", 0, total, 0))
|
||||||
|
self.assertTrue(any(0 < done < total and speed > 0
|
||||||
|
for stage, done, size, speed in reports if stage == "uploading"))
|
||||||
|
self.assertEqual(reports[-1], ("finishing", total, total, 0))
|
||||||
|
self.assertNotIn(source.name, repr(reports))
|
||||||
|
remote = paste.run_private([*uploader.ssh, "sha256sum -- " + shlex.quote(paths[0])],
|
||||||
|
threading.Event(), 30)
|
||||||
|
self.assertEqual(remote.split()[0].decode(), hashlib.sha256(source.read_bytes()).hexdigest())
|
||||||
|
finally:
|
||||||
|
paste.run_private([*uploader.ssh, "rm -rf -- " + shlex.quote(str(Path(paths[0]).parent))],
|
||||||
|
threading.Event(), 30)
|
||||||
|
|
||||||
|
def test_paste_through_real_pty_and_uu_ssh_opens_uploaded_files(self):
|
||||||
|
host = os.environ["UU_PASTE_TEST_HOST"]
|
||||||
|
config = os.environ.get("UU_PASTE_TEST_CONFIG")
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
files = [Path(tmp) / "copied file 中文.bin", Path(tmp) / "literal'$(echo nope);.bin"]
|
||||||
|
for file in files:
|
||||||
|
file.write_bytes(os.urandom(65569))
|
||||||
|
hashes = [hashlib.sha256(f.read_bytes()).hexdigest() for f in files]
|
||||||
|
mouse = b"\x1b[<2;2;3M\x1b[<2;2;3m\x1b[<35;3;4M"
|
||||||
|
remote = r'''
|
||||||
|
import os,tty,shlex,hashlib,json,shutil
|
||||||
|
from pathlib import Path
|
||||||
|
tty.setraw(0)
|
||||||
|
os.write(1,b'\x1b[?2004hLIVE_READY')
|
||||||
|
data=b''
|
||||||
|
while not data.endswith(b'\r'):
|
||||||
|
data+=os.read(0,65536)
|
||||||
|
mouse=b'\x1b[<2;2;3M\x1b[<2;2;3m\x1b[<35;3;4M'
|
||||||
|
assert data.startswith(mouse+b'\x1b[200~') and data.endswith(b'\x1b[201~\r')
|
||||||
|
paths=shlex.split(data[len(mouse)+6:-7].decode())
|
||||||
|
assert len(paths)==2
|
||||||
|
hashes=[hashlib.sha256(Path(p).read_bytes()).hexdigest() for p in paths]
|
||||||
|
for directory in {Path(p).parent for p in paths}:
|
||||||
|
assert directory.parent==Path.home()/'.local/share/uuremote/uploads'
|
||||||
|
assert directory.name.startswith('paste.')
|
||||||
|
shutil.rmtree(directory)
|
||||||
|
os.write(1,b'LIVE_HASHES:'+json.dumps(hashes).encode()+b':DONE')
|
||||||
|
'''
|
||||||
|
harness = r'''
|
||||||
|
import runpy,sys,json
|
||||||
|
h=runpy.run_path(sys.argv[1])
|
||||||
|
host,config,paths,remote=json.loads(sys.argv[2])
|
||||||
|
uploader=h['Uploader'](config,host)
|
||||||
|
common=['-F',config] if config else []
|
||||||
|
raise SystemExit(h['relay'](['/usr/bin/ssh',*common,'-tt',host,remote],lambda:paths,uploader))
|
||||||
|
'''
|
||||||
|
args = json.dumps([host, config, [str(p) for p in files], "python3 -c " + shlex.quote(remote)])
|
||||||
|
master, slave = pty.openpty()
|
||||||
|
process = subprocess.Popen([sys.executable, "-c", harness, str(SCRIPT), args],
|
||||||
|
stdin=slave, stdout=slave, stderr=slave)
|
||||||
|
output = bytearray()
|
||||||
|
try:
|
||||||
|
deadline = time.monotonic() + 60
|
||||||
|
sent = False
|
||||||
|
while process.poll() is None or select.select([master], [], [], 0)[0]:
|
||||||
|
self.assertLess(time.monotonic(), deadline, "Live paste timed out")
|
||||||
|
if select.select([master], [], [], 0.1)[0]:
|
||||||
|
output.extend(os.read(master, 65536))
|
||||||
|
if b"LIVE_READY" in output and not sent:
|
||||||
|
payload = shlex.join([str(p) for p in files]).encode()
|
||||||
|
os.write(master, mouse + paste.START + payload + paste.END + b"\r")
|
||||||
|
sent = True
|
||||||
|
self.assertEqual(process.wait(timeout=2), 0)
|
||||||
|
received = bytes(output).split(b"LIVE_HASHES:", 1)[1].split(b":DONE", 1)[0]
|
||||||
|
self.assertEqual(json.loads(received), hashes)
|
||||||
|
finally:
|
||||||
|
if process.poll() is None:
|
||||||
|
process.kill()
|
||||||
|
process.wait()
|
||||||
|
os.close(master)
|
||||||
|
os.close(slave)
|
||||||
|
|
||||||
|
def test_real_upload_roundtrip_binary_unicode_and_shell_metacharacters(self):
|
||||||
|
host = os.environ["UU_PASTE_TEST_HOST"]
|
||||||
|
config = os.environ.get("UU_PASTE_TEST_CONFIG")
|
||||||
|
uploader = paste.Uploader(config, host)
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
root = Path(tmp)
|
||||||
|
files = [root / "space 中文.bin", root / "literal'$(echo nope);.bin"]
|
||||||
|
for file in files:
|
||||||
|
file.write_bytes(os.urandom(65569))
|
||||||
|
paths = shlex.split(uploader.upload([str(f) for f in files], threading.Event()).decode())
|
||||||
|
try:
|
||||||
|
for index, (source, remote) in enumerate(zip(files, paths)):
|
||||||
|
dest = root / ("download-" + str(index))
|
||||||
|
paste.run_private([*uploader.scp, f"{host}:{remote}", str(dest)], threading.Event(), 60)
|
||||||
|
self.assertEqual(hashlib.sha256(source.read_bytes()).digest(),
|
||||||
|
hashlib.sha256(dest.read_bytes()).digest())
|
||||||
|
directory = str(Path(paths[0]).parent)
|
||||||
|
mode = paste.run_private([*uploader.ssh, "stat -c %a -- " + shlex.quote(directory)],
|
||||||
|
threading.Event(), 30)
|
||||||
|
self.assertEqual(mode.strip(), b"700")
|
||||||
|
finally:
|
||||||
|
paste.run_private([*uploader.ssh, "rm -rf -- " + shlex.quote(str(Path(paths[0]).parent))],
|
||||||
|
threading.Event(), 30)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Executable
+594
@@ -0,0 +1,594 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Paste Finder files into an SSH terminal through an existing UU TCP mapping."""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import collections
|
||||||
|
import errno
|
||||||
|
import fcntl
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import pty
|
||||||
|
import queue
|
||||||
|
import re
|
||||||
|
import select
|
||||||
|
import shlex
|
||||||
|
import signal
|
||||||
|
import stat
|
||||||
|
import struct
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import termios
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import tty
|
||||||
|
|
||||||
|
START, END = b"\x1b[200~", b"\x1b[201~"
|
||||||
|
ENABLE, DISABLE = b"\x1b[?2004h", b"\x1b[?2004l"
|
||||||
|
MAX_PASTE = 8 * 1024 * 1024
|
||||||
|
|
||||||
|
|
||||||
|
class PasteError(Exception):
|
||||||
|
"""Messages must not include clipboard contents, paths or subprocess output."""
|
||||||
|
|
||||||
|
|
||||||
|
def prefix_tail(data, marker):
|
||||||
|
for length in range(min(len(data), len(marker) - 1), 0, -1):
|
||||||
|
if data.endswith(marker[:length]):
|
||||||
|
return length
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
class InputFrames:
|
||||||
|
"""Recognize bracketed pastes across arbitrary PTY read boundaries."""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.buffer = b""
|
||||||
|
self.pasting = False
|
||||||
|
self.overflow = False
|
||||||
|
|
||||||
|
def feed(self, data):
|
||||||
|
self.buffer += data
|
||||||
|
events = []
|
||||||
|
while self.buffer:
|
||||||
|
if self.pasting:
|
||||||
|
end = self.buffer.find(END)
|
||||||
|
if end < 0:
|
||||||
|
if len(self.buffer) > MAX_PASTE:
|
||||||
|
self.overflow = True
|
||||||
|
tail = prefix_tail(self.buffer, END)
|
||||||
|
self.buffer = self.buffer[-tail:] if tail else b""
|
||||||
|
break
|
||||||
|
payload, self.buffer = self.buffer[:end], self.buffer[end + len(END):]
|
||||||
|
events.append(("error" if self.overflow or len(payload) > MAX_PASTE else "paste", payload))
|
||||||
|
self.pasting = self.overflow = False
|
||||||
|
else:
|
||||||
|
start = self.buffer.find(START)
|
||||||
|
if start >= 0:
|
||||||
|
if start:
|
||||||
|
events.append(("data", self.buffer[:start]))
|
||||||
|
self.buffer = self.buffer[start + len(START):]
|
||||||
|
self.pasting = True
|
||||||
|
else:
|
||||||
|
tail = prefix_tail(self.buffer, START)
|
||||||
|
size = len(self.buffer) - tail
|
||||||
|
if size:
|
||||||
|
events.append(("data", self.buffer[:size]))
|
||||||
|
self.buffer = self.buffer[size:]
|
||||||
|
break
|
||||||
|
return events
|
||||||
|
|
||||||
|
def flush_key(self):
|
||||||
|
# A standalone Escape must not wait indefinitely for another key.
|
||||||
|
if not self.pasting:
|
||||||
|
result, self.buffer = self.buffer, b""
|
||||||
|
return result
|
||||||
|
return b""
|
||||||
|
|
||||||
|
|
||||||
|
class OutputModes:
|
||||||
|
"""Keep local paste framing on; retain the remote app's requested mode."""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.buffer = b""
|
||||||
|
self.bracketed = False
|
||||||
|
|
||||||
|
def feed(self, data):
|
||||||
|
self.buffer += data
|
||||||
|
output = bytearray()
|
||||||
|
while self.buffer:
|
||||||
|
index = self.buffer.find(b"\x1b")
|
||||||
|
if index < 0:
|
||||||
|
output.extend(self.buffer)
|
||||||
|
self.buffer = b""
|
||||||
|
break
|
||||||
|
output.extend(self.buffer[:index])
|
||||||
|
self.buffer = self.buffer[index:]
|
||||||
|
if self.buffer.startswith(b"\x1bc"):
|
||||||
|
output.extend(b"\x1bc" + ENABLE)
|
||||||
|
self.bracketed = False
|
||||||
|
self.buffer = self.buffer[2:]
|
||||||
|
continue
|
||||||
|
match = re.match(rb"\x1b\[\?([0-9;]+)([hl])", self.buffer)
|
||||||
|
if match:
|
||||||
|
params = match[1].split(b";")
|
||||||
|
if b"2004" in params:
|
||||||
|
self.bracketed = match[2] == b"h"
|
||||||
|
others = [p for p in params if p != b"2004"]
|
||||||
|
if others:
|
||||||
|
output.extend(b"\x1b[?" + b";".join(others) + match[2])
|
||||||
|
output.extend(ENABLE)
|
||||||
|
else:
|
||||||
|
output.extend(match[0])
|
||||||
|
self.buffer = self.buffer[match.end():]
|
||||||
|
elif len(self.buffer) < 128 and re.fullmatch(rb"\x1b(?:\[(?:\?[0-9;]*)?)?", self.buffer):
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
output.extend(self.buffer[:1])
|
||||||
|
self.buffer = self.buffer[1:]
|
||||||
|
return bytes(output)
|
||||||
|
|
||||||
|
|
||||||
|
def paste_bytes(data, bracketed):
|
||||||
|
return START + data + END if bracketed else data
|
||||||
|
|
||||||
|
|
||||||
|
def clipboard_paths(helper):
|
||||||
|
try:
|
||||||
|
result = subprocess.run([str(helper)], capture_output=True, timeout=3, check=True)
|
||||||
|
paths = json.loads(result.stdout)
|
||||||
|
if not isinstance(paths, list) or not all(isinstance(p, str) for p in paths):
|
||||||
|
raise ValueError()
|
||||||
|
return paths
|
||||||
|
except (OSError, ValueError, subprocess.SubprocessError):
|
||||||
|
raise PasteError("Cannot check copied files; paste was not sent.") from None
|
||||||
|
|
||||||
|
|
||||||
|
def copied_files(data, read_clipboard):
|
||||||
|
try:
|
||||||
|
text = data.decode("utf-8")
|
||||||
|
paths = shlex.split(text)
|
||||||
|
except (UnicodeError, ValueError):
|
||||||
|
return None
|
||||||
|
if not paths or not all(p.startswith("/") for p in paths):
|
||||||
|
return None
|
||||||
|
# Ordinary text that happens to name a local file must never upload it.
|
||||||
|
# Ghostty 1.3.1 getOpinionatedStringContents escapes Finder file paths;
|
||||||
|
# require the entire pasted path list to match actual file-URL clipboard data.
|
||||||
|
if paths != read_clipboard():
|
||||||
|
return None
|
||||||
|
if len(paths) > 256:
|
||||||
|
raise PasteError("Too many copied files (maximum 256).")
|
||||||
|
for path in paths:
|
||||||
|
if any(ord(c) < 32 or ord(c) == 127 for c in path):
|
||||||
|
raise PasteError("A copied filename contains unsupported control characters.")
|
||||||
|
try:
|
||||||
|
if not stat.S_ISREG(os.stat(path).st_mode):
|
||||||
|
raise PasteError("Copy files individually; folder pasting is not supported.")
|
||||||
|
except OSError:
|
||||||
|
raise PasteError("A copied file is no longer readable.") from None
|
||||||
|
if len({Path(p).name for p in paths}) != len(paths):
|
||||||
|
raise PasteError("Copied files must have distinct filenames; paste them separately.")
|
||||||
|
return paths
|
||||||
|
|
||||||
|
|
||||||
|
class SCPMeter:
|
||||||
|
"""Reduce OpenSSH's private progress output to numeric batch counters."""
|
||||||
|
|
||||||
|
# OpenSSH progressmeter.c writes: filename percent size rate time [ETA].
|
||||||
|
# Anchor at the numeric suffix; filenames and errors are never displayed.
|
||||||
|
record = re.compile(rb" (\d{1,3})%\s+(\d{1,20}(?:[KMGT]B)?)\s+"
|
||||||
|
rb"(\d{1,20}(?:\.\d{1,10})?[KMGT]B)/s\s+"
|
||||||
|
rb"(?:\d+:\d\d(?::\d\d)?(?: ETA)?|--:-- ETA|- stalled -)\s*$")
|
||||||
|
|
||||||
|
def __init__(self, sizes, report):
|
||||||
|
self.sizes, self.report = sizes, report
|
||||||
|
self.buffer, self.index, self.finished = b"", 0, 0
|
||||||
|
self.current, self.percent = 0, 0
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def amount(token):
|
||||||
|
match = re.fullmatch(rb"(\d+(?:\.\d+)?)([KMGT])?B?", token)
|
||||||
|
return int(float(match[1]) * 1024 ** (b" KMGT".index(match[2]) if match[2] else 0))
|
||||||
|
|
||||||
|
def parse(self, line):
|
||||||
|
match = self.record.search(line)
|
||||||
|
if not match or self.index >= len(self.sizes):
|
||||||
|
return
|
||||||
|
percent = int(match[1])
|
||||||
|
if percent > 100:
|
||||||
|
return
|
||||||
|
self.percent = max(self.percent, percent)
|
||||||
|
size = self.sizes[self.index]
|
||||||
|
self.current = max(self.current, min(size, self.amount(match[2])))
|
||||||
|
if percent == 100:
|
||||||
|
self.current = size
|
||||||
|
self.report("uploading", self.finished + self.current, sum(self.sizes), self.amount(match[3]))
|
||||||
|
|
||||||
|
def feed(self, data):
|
||||||
|
for piece in re.split(rb"([\r\n])", data):
|
||||||
|
if piece in (b"\r", b"\n"):
|
||||||
|
self.parse(self.buffer)
|
||||||
|
self.buffer = b""
|
||||||
|
if piece == b"\n" and self.percent == 100:
|
||||||
|
self.finished += self.sizes[self.index]
|
||||||
|
self.index += 1
|
||||||
|
self.current = self.percent = 0
|
||||||
|
else:
|
||||||
|
# Keep only enough for a numeric suffix, not unlimited errors.
|
||||||
|
self.buffer = (self.buffer + piece)[-512:]
|
||||||
|
self.parse(self.buffer)
|
||||||
|
|
||||||
|
|
||||||
|
def run_metered(args, cancel, timeout, meter):
|
||||||
|
master, slave = pty.openpty()
|
||||||
|
fcntl.ioctl(slave, termios.TIOCSWINSZ, struct.pack("HHHH", 24, 120, 0, 0))
|
||||||
|
|
||||||
|
try:
|
||||||
|
# The upload runs on a worker thread, so avoid Python preexec_fn after
|
||||||
|
# fork. Acquire the private controlling TTY in a fresh interpreter.
|
||||||
|
launch = ("import fcntl,os,sys,termios;fcntl.ioctl(1,termios.TIOCSCTTY,0);"
|
||||||
|
"os.execv(sys.argv[1],sys.argv[1:])")
|
||||||
|
with subprocess.Popen([sys.executable, "-c", launch, *args],
|
||||||
|
stdin=subprocess.DEVNULL, stdout=slave, stderr=slave,
|
||||||
|
start_new_session=True) as process:
|
||||||
|
os.close(slave)
|
||||||
|
slave = None
|
||||||
|
deadline = time.monotonic() + timeout
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
if cancel.is_set() or time.monotonic() > deadline:
|
||||||
|
raise PasteError("Upload cancelled or timed out; paste was not sent.")
|
||||||
|
if select.select([master], [], [], 0.1)[0]:
|
||||||
|
try:
|
||||||
|
data = os.read(master, 65536)
|
||||||
|
except OSError as error:
|
||||||
|
if error.errno != errno.EIO:
|
||||||
|
raise
|
||||||
|
data = b""
|
||||||
|
if not data:
|
||||||
|
break
|
||||||
|
meter.feed(data)
|
||||||
|
elif process.poll() is not None:
|
||||||
|
break
|
||||||
|
try:
|
||||||
|
status = process.wait(timeout=max(0.1, deadline - time.monotonic()))
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
raise PasteError("Upload timed out; paste was not sent.") from None
|
||||||
|
if status:
|
||||||
|
raise PasteError("Upload failed; paste was not sent.")
|
||||||
|
finally:
|
||||||
|
if process.poll() is None:
|
||||||
|
os.killpg(process.pid, signal.SIGKILL)
|
||||||
|
process.wait()
|
||||||
|
finally:
|
||||||
|
os.close(master)
|
||||||
|
if slave is not None:
|
||||||
|
os.close(slave)
|
||||||
|
return b""
|
||||||
|
|
||||||
|
|
||||||
|
def run_private(args, cancel, timeout, meter=None):
|
||||||
|
# Neither SSH errors nor SCP output may disclose paths, clipboard/file data,
|
||||||
|
# or credentials. Authentication never reads from the interactive terminal.
|
||||||
|
if meter is not None:
|
||||||
|
return run_metered(args, cancel, timeout, meter)
|
||||||
|
with subprocess.Popen(args, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.DEVNULL, start_new_session=True) as process:
|
||||||
|
deadline = time.monotonic() + timeout
|
||||||
|
while True:
|
||||||
|
if cancel.is_set() or time.monotonic() > deadline:
|
||||||
|
os.killpg(process.pid, signal.SIGKILL)
|
||||||
|
process.communicate()
|
||||||
|
raise PasteError("Upload cancelled." if cancel.is_set() else "Upload timed out; paste was not sent.")
|
||||||
|
try:
|
||||||
|
stdout, _ = process.communicate(timeout=0.1)
|
||||||
|
if process.returncode:
|
||||||
|
raise PasteError("Upload failed; check the UU mapping and SSH access. Paste was not sent.")
|
||||||
|
return stdout
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Uploader:
|
||||||
|
def __init__(self, config, host):
|
||||||
|
common = ["-F", str(config)] if config else []
|
||||||
|
common += ["-o", "BatchMode=yes", "-o", "ConnectTimeout=15"]
|
||||||
|
self.ssh = ["/usr/bin/ssh", *common, host]
|
||||||
|
self.scp = ["/usr/bin/scp", "-q", *common]
|
||||||
|
self.host = host
|
||||||
|
|
||||||
|
def upload(self, paths, cancel, progress=None):
|
||||||
|
sizes = [os.stat(path).st_size for path in paths] if progress else []
|
||||||
|
if progress:
|
||||||
|
progress("connecting", 0, sum(sizes), 0)
|
||||||
|
script = ('umask 077; d="$HOME/.local/share/uuremote/uploads"; '
|
||||||
|
'mkdir -p "$d" && mktemp -d "$d/paste.XXXXXXXX"')
|
||||||
|
try:
|
||||||
|
directory = run_private([*self.ssh, script], cancel, 30).decode("utf-8").strip()
|
||||||
|
except UnicodeError:
|
||||||
|
raise PasteError("Invalid upload destination; paste was not sent.") from None
|
||||||
|
if (not directory.startswith("/") or len(directory) > 4096 or
|
||||||
|
any(ord(c) < 32 or ord(c) == 127 for c in directory) or
|
||||||
|
not re.fullmatch(r"paste\.[A-Za-z0-9]+", directory.rsplit("/", 1)[-1])):
|
||||||
|
raise PasteError("Invalid upload destination; paste was not sent.")
|
||||||
|
try:
|
||||||
|
# Use modern SCP's SFTP mode, never legacy SCP (-O). Unique batch
|
||||||
|
# directories prevent overwrites; all local paths are absolute.
|
||||||
|
if progress:
|
||||||
|
progress("uploading", 0, sum(sizes), 0)
|
||||||
|
scp = self.scp[:1] + self.scp[2:] # Remove only our own -q.
|
||||||
|
run_private([*scp, *paths, f"{self.host}:{directory}/"], cancel, 300,
|
||||||
|
SCPMeter(sizes, progress))
|
||||||
|
progress("finishing", sum(sizes), sum(sizes), 0)
|
||||||
|
else:
|
||||||
|
run_private([*self.scp, *paths, f"{self.host}:{directory}/"], cancel, 300)
|
||||||
|
except (PasteError, OSError):
|
||||||
|
# Clean only this batch, with quoted remote shell syntax. Cancellation
|
||||||
|
# of one upload must not stop another SSH/UU session.
|
||||||
|
try:
|
||||||
|
run_private([*self.ssh, "rm -rf -- " + shlex.quote(directory)], threading.Event(), 10)
|
||||||
|
except (PasteError, OSError):
|
||||||
|
pass
|
||||||
|
raise
|
||||||
|
return " ".join(shlex.quote(directory + "/" + Path(p).name) for p in paths).encode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
def write_all(fd, data):
|
||||||
|
view = memoryview(data)
|
||||||
|
while view:
|
||||||
|
view = view[os.write(fd, view):]
|
||||||
|
|
||||||
|
|
||||||
|
def display_size(size):
|
||||||
|
for unit in ("B", "KiB", "MiB", "GiB", "TiB"):
|
||||||
|
if size < 1024 or unit == "TiB":
|
||||||
|
return f"{size:.0f} {unit}" if unit == "B" else f"{size:.1f} {unit}"
|
||||||
|
size /= 1024
|
||||||
|
|
||||||
|
|
||||||
|
class UploadDisplay:
|
||||||
|
"""Session-local title and OSC 9;4 progress; terminal cells stay untouched."""
|
||||||
|
|
||||||
|
# https://ghostty.org/docs/vt/osc/conemu: states 0=clear, 1=percent, 3=busy.
|
||||||
|
|
||||||
|
def __init__(self, emit):
|
||||||
|
self.emit, self.active = emit, False
|
||||||
|
self.title, self.pending = b"UU SSH", b""
|
||||||
|
self.state = ("connecting", 0, 0, 0)
|
||||||
|
self.started = self.rendered = 0
|
||||||
|
|
||||||
|
def observe(self, data):
|
||||||
|
self.pending = (self.pending + data)[-8192:]
|
||||||
|
for match in re.finditer(rb"\x1b\][02];([^\x07\x1b]*)(?:\x07|\x1b\\)", self.pending):
|
||||||
|
self.title = match[1]
|
||||||
|
# Retain only an incomplete OSC title across output reads.
|
||||||
|
index = self.pending.rfind(b"\x1b]")
|
||||||
|
if index < 0:
|
||||||
|
self.pending = b"\x1b" if self.pending.endswith(b"\x1b") else b""
|
||||||
|
else:
|
||||||
|
tail = self.pending[index:]
|
||||||
|
self.pending = tail if b"\x07" not in tail and b"\x1b\\" not in tail else b""
|
||||||
|
|
||||||
|
def start(self, total):
|
||||||
|
self.active = True
|
||||||
|
self.started = time.monotonic()
|
||||||
|
self.state = ("connecting", 0, total, 0)
|
||||||
|
self.render(force=True)
|
||||||
|
|
||||||
|
def update(self, stage, done, total, speed):
|
||||||
|
self.state = stage, done, total, speed
|
||||||
|
|
||||||
|
def render(self, force=False):
|
||||||
|
now = time.monotonic()
|
||||||
|
if not self.active or (not force and now - self.rendered < 0.2):
|
||||||
|
return
|
||||||
|
self.rendered = now
|
||||||
|
stage, done, total, speed = self.state
|
||||||
|
elapsed = int(now - self.started)
|
||||||
|
if stage in ("connecting", "cancelling"):
|
||||||
|
label = "Connecting" if stage == "connecting" else "Cancelling"
|
||||||
|
text = f"UU upload: {label} | {display_size(total)} | {elapsed}s"
|
||||||
|
indicator = b"\x1b]9;4;3\x1b\\"
|
||||||
|
else:
|
||||||
|
percent = min(100, int(done * 100 / total)) if total else 100
|
||||||
|
label = "Finishing" if stage == "finishing" else "Uploading"
|
||||||
|
text = (f"UU upload: {label} {percent}% | {display_size(done)}/{display_size(total)}"
|
||||||
|
f" | {display_size(speed)}/s | {elapsed}s")
|
||||||
|
indicator = f"\x1b]9;4;1;{percent}\x1b\\".encode()
|
||||||
|
self.emit(b"\x1b]2;" + text.encode() + b"\x1b\\" + indicator)
|
||||||
|
|
||||||
|
def finish(self):
|
||||||
|
if self.active:
|
||||||
|
self.active = False
|
||||||
|
self.emit(b"\x1b]9;4;0\x1b\\\x1b]2;" + self.title + b"\x1b\\")
|
||||||
|
|
||||||
|
|
||||||
|
def relay(command, read_clipboard, uploader):
|
||||||
|
original = termios.tcgetattr(0)
|
||||||
|
master, slave = pty.openpty()
|
||||||
|
|
||||||
|
def resize(*_):
|
||||||
|
try:
|
||||||
|
size = fcntl.ioctl(0, termios.TIOCGWINSZ, bytes(8))
|
||||||
|
fcntl.ioctl(master, termios.TIOCSWINSZ, size)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
resize()
|
||||||
|
|
||||||
|
def child_tty():
|
||||||
|
os.setsid()
|
||||||
|
fcntl.ioctl(0, termios.TIOCSCTTY, 0)
|
||||||
|
|
||||||
|
child = subprocess.Popen(command, stdin=slave, stdout=slave, stderr=slave, preexec_fn=child_tty)
|
||||||
|
os.close(slave)
|
||||||
|
previous_resize = signal.signal(signal.SIGWINCH, resize)
|
||||||
|
|
||||||
|
def shutdown(signum, _frame):
|
||||||
|
raise SystemExit(128 + signum)
|
||||||
|
|
||||||
|
previous_signals = {sig: signal.signal(sig, shutdown)
|
||||||
|
for sig in (signal.SIGHUP, signal.SIGTERM, signal.SIGINT)}
|
||||||
|
frames, modes = InputFrames(), OutputModes()
|
||||||
|
events, completed = collections.deque(), queue.Queue()
|
||||||
|
updates = queue.Queue()
|
||||||
|
display = UploadDisplay(lambda data: write_all(1, data))
|
||||||
|
cancel, uploading, interrupted = threading.Event(), False, False
|
||||||
|
worker = None
|
||||||
|
last_input = time.monotonic()
|
||||||
|
|
||||||
|
def upload_worker(paths):
|
||||||
|
try:
|
||||||
|
completed.put((uploader.upload(paths, cancel, lambda *args: updates.put(args)), None))
|
||||||
|
except (OSError, PasteError):
|
||||||
|
completed.put((None, "Upload failed or cancelled; paste was not sent."))
|
||||||
|
|
||||||
|
def notice(message):
|
||||||
|
write_all(2, ("\r\n[UU paste] " + message + "\r\n").encode())
|
||||||
|
|
||||||
|
def interrupt_upload():
|
||||||
|
nonlocal interrupted, frames
|
||||||
|
if not uploading:
|
||||||
|
return
|
||||||
|
pending = list(events)
|
||||||
|
positions = [(i, payload.rfind(b"\x03")) for i, (kind, payload) in enumerate(pending)
|
||||||
|
if kind == "data" and b"\x03" in payload]
|
||||||
|
if positions:
|
||||||
|
index, offset = positions[-1]
|
||||||
|
cancel.set()
|
||||||
|
interrupted = True
|
||||||
|
_, done, total, _ = display.state
|
||||||
|
display.update("cancelling", done, total, 0)
|
||||||
|
display.render(force=True)
|
||||||
|
frames = InputFrames()
|
||||||
|
events.clear()
|
||||||
|
events.extend(frames.feed(pending[index][1][offset + 1:]))
|
||||||
|
events.extend(pending[index + 1:])
|
||||||
|
write_all(master, b"\x03")
|
||||||
|
|
||||||
|
try:
|
||||||
|
tty.setraw(0)
|
||||||
|
write_all(1, ENABLE)
|
||||||
|
while True:
|
||||||
|
interrupt_upload()
|
||||||
|
while not updates.empty():
|
||||||
|
update = updates.get_nowait()
|
||||||
|
if not interrupted:
|
||||||
|
display.update(*update)
|
||||||
|
if uploading:
|
||||||
|
display.render()
|
||||||
|
try:
|
||||||
|
data, error = completed.get_nowait()
|
||||||
|
except queue.Empty:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
uploading = False
|
||||||
|
display.finish()
|
||||||
|
if error:
|
||||||
|
notice(error)
|
||||||
|
elif not interrupted:
|
||||||
|
write_all(master, paste_bytes(data, modes.bracketed))
|
||||||
|
while events and not uploading:
|
||||||
|
kind, data = events.popleft()
|
||||||
|
if kind == "error":
|
||||||
|
notice("Paste exceeds 8 MiB; paste was not sent.")
|
||||||
|
continue
|
||||||
|
if kind == "data":
|
||||||
|
write_all(master, data)
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
files = copied_files(data, read_clipboard)
|
||||||
|
except PasteError as error:
|
||||||
|
notice(str(error))
|
||||||
|
continue
|
||||||
|
if files is None:
|
||||||
|
write_all(master, paste_bytes(data, modes.bracketed))
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
total = sum(os.stat(path).st_size for path in files)
|
||||||
|
except OSError:
|
||||||
|
notice("A copied file is no longer readable; paste was not sent.")
|
||||||
|
continue
|
||||||
|
cancel.clear()
|
||||||
|
uploading, interrupted = True, False
|
||||||
|
display.start(total)
|
||||||
|
worker = threading.Thread(target=upload_worker, args=(files,), daemon=True)
|
||||||
|
worker.start()
|
||||||
|
interrupt_upload()
|
||||||
|
readable, _, _ = select.select([0, master], [], [], 0.025)
|
||||||
|
if master in readable:
|
||||||
|
try:
|
||||||
|
data = os.read(master, 65536)
|
||||||
|
except OSError as error:
|
||||||
|
if error.errno != errno.EIO:
|
||||||
|
raise
|
||||||
|
data = b""
|
||||||
|
if not data:
|
||||||
|
break
|
||||||
|
display.observe(data)
|
||||||
|
write_all(1, modes.feed(data))
|
||||||
|
if 0 in readable:
|
||||||
|
data = os.read(0, 65536)
|
||||||
|
if not data:
|
||||||
|
break
|
||||||
|
last_input = time.monotonic()
|
||||||
|
events.extend(frames.feed(data))
|
||||||
|
if sum(len(payload) for _, payload in events) > MAX_PASTE:
|
||||||
|
events.clear()
|
||||||
|
notice("Input queue is full; queued input was discarded.")
|
||||||
|
elif time.monotonic() - last_input > 0.025:
|
||||||
|
data = frames.flush_key()
|
||||||
|
if data:
|
||||||
|
events.append(("data", data))
|
||||||
|
if modes.buffer:
|
||||||
|
write_all(1, modes.buffer)
|
||||||
|
finally:
|
||||||
|
cancel.set()
|
||||||
|
signal.signal(signal.SIGWINCH, previous_resize)
|
||||||
|
for sig, previous in previous_signals.items():
|
||||||
|
signal.signal(sig, previous)
|
||||||
|
termios.tcsetattr(0, termios.TCSADRAIN, original)
|
||||||
|
display.finish()
|
||||||
|
write_all(1, DISABLE)
|
||||||
|
os.close(master)
|
||||||
|
if child.poll() is None:
|
||||||
|
try:
|
||||||
|
child.wait(timeout=2)
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
os.killpg(child.pid, signal.SIGTERM)
|
||||||
|
try:
|
||||||
|
child.wait(timeout=2)
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
os.killpg(child.pid, signal.SIGKILL)
|
||||||
|
status = child.wait()
|
||||||
|
if worker is not None:
|
||||||
|
worker.join(timeout=12)
|
||||||
|
return status if status >= 0 else 128 - status
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
|
parser.add_argument("-F", "--ssh-config", type=Path)
|
||||||
|
parser.add_argument("host", help="SSH alias for an existing UU loopback port mapping")
|
||||||
|
parser.add_argument("command", nargs=argparse.REMAINDER)
|
||||||
|
args = parser.parse_args()
|
||||||
|
if not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9_.@-]*", args.host):
|
||||||
|
parser.error("use an SSH host alias, without options or a port")
|
||||||
|
common = ["-F", str(args.ssh_config)] if args.ssh_config else []
|
||||||
|
command = ["/usr/bin/ssh", *common]
|
||||||
|
if not (os.isatty(0) and os.isatty(1)):
|
||||||
|
os.execv(command[0], [*command, args.host, *args.command])
|
||||||
|
helper = Path(__file__).resolve().with_name("clipboard-files")
|
||||||
|
if not helper.is_file():
|
||||||
|
parser.error("run contrib/macos-ssh/install.sh on your Mac first")
|
||||||
|
return relay([*command, "-tt", args.host, *args.command],
|
||||||
|
lambda: clipboard_paths(helper), Uploader(args.ssh_config, args.host))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
sys.exit(main())
|
||||||
|
except (OSError, PasteError):
|
||||||
|
print("UU SSH companion failed; check the UU mapping and SSH configuration.", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
# Unofficial UU Remote Linux host in a container, for NAS boxes (Synology DSM, etc.) without a desktop.
|
||||||
|
# Headless mode: file transfer, terminal and port mapping; no remote desktop. Uses the published release binary.
|
||||||
|
FROM ubuntu:24.04
|
||||||
|
ARG VERSION=v0.2.8
|
||||||
|
LABEL org.opencontainers.image.source="https://github.com/VallierDev/uu-remote-linux" \
|
||||||
|
org.opencontainers.image.description="Unofficial UU Remote Linux host, headless (file transfer, terminal, port mapping)" \
|
||||||
|
org.opencontainers.image.licenses="GPL-3.0"
|
||||||
|
RUN apt-get update \
|
||||||
|
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
|
||||||
|
ca-certificates curl bash libx11-6 libxext6 libxtst6 libxfixes3 libgtk-3-0 \
|
||||||
|
&& cd /tmp \
|
||||||
|
&& base="https://github.com/VallierDev/uu-remote-linux/releases/download/${VERSION}/uu-remote-linux-${VERSION}-x86_64-linux.tar.gz" \
|
||||||
|
&& curl -fsSLO "$base" && curl -fsSLO "$base.sha256" && sha256sum -c "uu-remote-linux-${VERSION}-x86_64-linux.tar.gz.sha256" \
|
||||||
|
&& tar xzf "uu-remote-linux-${VERSION}-x86_64-linux.tar.gz" \
|
||||||
|
&& install -m755 "$(find /tmp -name uuremote-server -type f | head -1)" /usr/local/bin/uuremote-server \
|
||||||
|
&& rm -rf /tmp/* /var/lib/apt/lists/*
|
||||||
|
# Credentials live in /config (keep it private); /data is what file transfer and the terminal see as home.
|
||||||
|
ENV UU_CONFIG=/config/config.json UU_DESKTOP_BACKEND=none UU_LOGIN_WINDOW=0 UU_AUDIO=0 RUST_LOG=info HOME=/data
|
||||||
|
VOLUME ["/config", "/data"]
|
||||||
|
WORKDIR /data
|
||||||
|
ENTRYPOINT ["uuremote-server"]
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
# 群晖 / NAS 容器部署(无桌面)
|
||||||
|
|
||||||
|
**简体中文** | [English](#english)
|
||||||
|
|
||||||
|
在没有桌面的 NAS(群晖 DSM 等,x86_64)上,用容器运行本项目主机,供官方 UU 远程客户端使用:
|
||||||
|
|
||||||
|
- **文件传输**:收发 `/data` 里的文件(把你的共享文件夹挂到 `/data`)。
|
||||||
|
- **终端**:进入容器内的 bash,当前目录为 `/data`。
|
||||||
|
- **端口映射**:在外网经 UU 访问家里局域网的服务(NAS 管理页、路由器、其他电脑……)。
|
||||||
|
|
||||||
|
不提供远程桌面(NAS 没有图形界面)。容器里运行的是 GitHub 发布包中的同一个 `uuremote-server`。
|
||||||
|
|
||||||
|
## 安装
|
||||||
|
|
||||||
|
在 NAS 上(SSH 登录后用 root 或 `sudo`),进入本目录:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
mkdir -p config data && chmod 700 config
|
||||||
|
docker compose build # 或者直接使用 ghcr.io/vallierdev/uu-remote-linux 镜像
|
||||||
|
```
|
||||||
|
|
||||||
|
**首次登录**(只需一次):在你自己的终端里运行,用手机 UU 远程扫描终端里显示的二维码并确认。
|
||||||
|
二维码等同于登录凭据,不要截图或发给别人。
|
||||||
|
|
||||||
|
```sh
|
||||||
|
docker compose run --rm uu-remote login --no-gui
|
||||||
|
```
|
||||||
|
|
||||||
|
登录信息保存在 `config/config.json`(权限 0600),请妥善保管 `config` 目录。然后启动:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
docker compose up -d
|
||||||
|
docker compose logs -f # 看到 availability=Some("ready") 即可在客户端设备列表里找到它
|
||||||
|
```
|
||||||
|
|
||||||
|
**群晖 Container Manager**:也可以在「项目」里新建项目,选本目录的 `compose.yaml`;首次登录仍需在 SSH 里执行上面的 `login` 命令。
|
||||||
|
|
||||||
|
## 说明
|
||||||
|
|
||||||
|
- `network_mode: host` 让 WebRTC 走直连;改用默认桥接网络也能用,但更常走中转,延迟更高。
|
||||||
|
- 把 `compose.yaml` 里的 `./data` 改成要开放的共享文件夹,例如 `/volume1/homes/你的用户名`。只挂需要远程访问的目录。
|
||||||
|
- 终端操作的是容器而不是 NAS 系统本身;需要操作 NAS 本机时请用 DSM 自带的 SSH。
|
||||||
|
- 升级:重新 `docker compose build --pull`(或拉取新镜像)后 `docker compose up -d`;`config` 保留即可免再登录。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## English
|
||||||
|
|
||||||
|
Run this project's host in a container on a desktop-less NAS (Synology DSM, x86_64) for the official UU Remote clients:
|
||||||
|
file transfer (`/data`), terminal (bash inside the container) and port mapping into your LAN. No remote desktop.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
mkdir -p config data && chmod 700 config
|
||||||
|
docker compose build
|
||||||
|
docker compose run --rm uu-remote login --no-gui # scan the terminal QR with the UU mobile app (the QR is a credential)
|
||||||
|
docker compose up -d
|
||||||
|
docker compose logs -f # wait for availability=Some("ready")
|
||||||
|
```
|
||||||
|
|
||||||
|
Credentials are kept in `config/config.json`. Mount only the folders you want to reach as `/data`.
|
||||||
|
Host networking gives WebRTC direct paths; bridge networking works but relays more often.
|
||||||
|
|
||||||
|
**Unofficial project, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
# docker compose up -d (first run: log in once, see README.md in this directory)
|
||||||
|
services:
|
||||||
|
uu-remote:
|
||||||
|
image: ghcr.io/vallierdev/uu-remote-linux:latest
|
||||||
|
build: .
|
||||||
|
container_name: uu-remote
|
||||||
|
# Host networking gives WebRTC direct UDP paths; bridge mode also works but falls back to relays more often.
|
||||||
|
network_mode: host
|
||||||
|
restart: unless-stopped
|
||||||
|
volumes:
|
||||||
|
- ./config:/config
|
||||||
|
# Folder offered to file transfer and the terminal (change to your share, e.g. /volume1/homes/you).
|
||||||
|
- ./data:/data
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 51 KiB After Width: | Height: | Size: 161 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 255 KiB |
@@ -0,0 +1,30 @@
|
|||||||
|
控制端:支持 H.265 / 4:4:4,可对官方 Windows 被控端上传、端口映射和终端。/ Viewer: H.265 / 4:4:4, and upload, port mapping and terminal with the official Windows host.
|
||||||
|
|
||||||
|
### 控制端新增 / Viewer — added
|
||||||
|
- **H.265 与 4:4:4 真彩**:桌面同时提供 H.264 / H.265,由被控端选择;`UU_VIEWER_CHROMA=444` 请求真彩,`UU_VIEWER_CODEC=h264` 只用 H.264。
|
||||||
|
已在虚拟机中对本项目主机实测(libx265 4:2:0 与 4:4:4、强制 H.264 均 0 解码错误);⚠️ 官方被控端的 H.265 / 4:4:4 未实测。
|
||||||
|
H.265 and 4:4:4 true color (tested against this project's host in a VM; not yet with official hosts).
|
||||||
|
|
||||||
|
### 控制端修复 / Viewer — fixed(均为实测发现并验证 / found and verified live)
|
||||||
|
- **官方 Windows 被控端上传文件**:之前上传会被拒绝(错误码 2)。现在按官方客户端的格式发送(Windows 路径、修改时间、压缩文件列表),
|
||||||
|
64 KiB 与 1 MiB 文件上传后再下载回来 sha256 一致。下载也不再拒绝大于 32 KiB 的数据块(错误码 3)。
|
||||||
|
- **官方 Windows 被控端端口映射**:之前每个连接都超时;现在可正常转发(经被控端访问 HTTP 返回 200)。
|
||||||
|
- **官方 Windows 被控端终端**:之前连上后没有任何输出;现在可打开 PowerShell 并交互。
|
||||||
|
- **启动窗口**:设备列表每 30 秒刷新后,已打开的设备页按钮全部失效的问题;「断开当前连接」确认框改为中文按钮。
|
||||||
|
- **连接官方 Mac 被控端时键盘错位**(#39,感谢 @wanglz111,#40):按 1 出空格、Backspace 出 c。Mac 目标改用 macOS 原生键码
|
||||||
|
(按物理位置转发,PC 的 Win 键对应 Command),Windows 与本项目主机仍用 Windows VK;无法确定目标平台时拒绝控制而不是乱发键码。
|
||||||
|
贡献者已在官方 Mac 被控端实机确认;⚠️ ISO/JIS 键盘、媒体键与 Mac 远程协助未验收。
|
||||||
|
- **FFmpeg 4.4 下没有画面**(#40):v0.2.0 / v0.2.1 的控制端解码参数 `-fps_mode` 不被 FFmpeg 4.4(如 Ubuntu 22.04 自带版本)识别,
|
||||||
|
解码进程直接退出。现改用新旧版本都支持的参数。
|
||||||
|
- 对本项目主机的上传、端口映射、终端新建/接回均已回归验证。
|
||||||
|
- Upload, port mapping and terminal now work with the official Windows host; launcher device page stays usable after list refreshes.
|
||||||
|
|
||||||
|
### 已知问题 / Known issues
|
||||||
|
- 控制端 → 官方 Windows 被控端的剪贴板:被控端确认收到文本,但未出现在其桌面剪贴板中,仍在排查(被控端 → 控制端正常)。
|
||||||
|
- GNOME Wayland 下控制端剪贴板不可用,且轮询会让 Dock 图标跳动(#37,修复中);窗口本身正常。KDE、Hyprland 正常。
|
||||||
|
- Viewer → Windows host clipboard is acknowledged but not applied yet; GNOME clipboard (#37).
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.2.2-x86_64-linux.tar.gz`:主机 `uuremote-server`(X11 版)与控制端 `uuremote-viewer`(x86_64,glibc ≥ 2.35;控制端需 SDL2、GTK3、ffmpeg),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
控制端:GNOME Wayland 下剪贴板不再让 Dock 图标跳动。/ Viewer: clipboard no longer makes GNOME Wayland Dock icons jump.
|
||||||
|
|
||||||
|
### 控制端修复 / Viewer — fixed
|
||||||
|
- **GNOME Wayland 剪贴板与 Dock 跳动**(#37,感谢 @wanglz111,#41):之前每 500 ms 调用一次 `wl-paste`,在缺少 data-control 的 GNOME 上
|
||||||
|
每次都会创建临时窗口,导致 Dock 图标持续跳动。现在桌面窗口直接通过 SDL 读写文本剪贴板,不再需要 `wl-clipboard` / `xclip`。
|
||||||
|
只在桌面窗口获得焦点时同步,仍保留 30 KiB 上限、`UU_VIEWER_CLIPBOARD=0` 关闭开关和原有协议。
|
||||||
|
早期版本已由报告者在 GNOME Wayland 实机确认 Dock 不再跳动;⚠️ 最终的焦点门控版本只有自动化测试,尚未重新实机验收。
|
||||||
|
Clipboard now goes through the existing SDL window (focused only). An earlier revision was confirmed live by the reporter;
|
||||||
|
the final focus-gated revision is covered by automated tests only.
|
||||||
|
|
||||||
|
### 已知问题 / Known issues
|
||||||
|
- 剪贴板只在控制端窗口获得焦点时同步;窗口在后台时收到的被控端剪贴板变化不会写入本机。
|
||||||
|
- 剪贴板内容提供方响应很慢时,可能短暂卡住控制端窗口(SDL2 同步读取)。
|
||||||
|
- 控制端 → 官方 Windows 被控端的剪贴板:被控端确认收到文本,但未出现在其桌面剪贴板中,仍在排查;与官方 Mac 被控端的双向剪贴板未验证。
|
||||||
|
- Clipboard syncs only while the viewer window is focused; viewer → official Windows host clipboard is still unresolved.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.2.3-x86_64-linux.tar.gz`:主机 `uuremote-server`(X11 版)与控制端 `uuremote-viewer`(x86_64,glibc ≥ 2.35;控制端需 SDL2、GTK3、ffmpeg),附带 `contrib/`。主机端与 v0.2.2 相同。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
控制端:修复 FFmpeg 9 下没有画面;新增「删除设备」。/ Viewer: fix no video with FFmpeg 9; add device removal.
|
||||||
|
|
||||||
|
### 控制端修复 / Viewer — fixed
|
||||||
|
- **FFmpeg 9 下桌面没有画面**(v0.2.2、v0.2.3 的回归):为兼容 FFmpeg 4.4 改用的 `-vsync 0` 已被 FFmpeg 9 删除,解码进程一启动就退出,
|
||||||
|
所有帧都解码失败。现在启动时探测当前 ffmpeg 支持的参数:新版本用 `-fps_mode passthrough`,旧版本(如 Ubuntu 22.04 的 4.4)用 `-vsync 0`。
|
||||||
|
已在 FFmpeg 9.0.2 上连接官方 Mac 被控端实测:0 解码错误。
|
||||||
|
Fixed: with FFmpeg 9 every frame failed to decode (`-vsync` was removed). Verified against the official Mac host on FFmpeg 9.0.2.
|
||||||
|
|
||||||
|
### 控制端新增 / Viewer — added
|
||||||
|
- **删除设备**:启动窗口「更多工具 → 删除设备」(带确认),或命令行 `uuremote-viewer unbind DEVICE`,把设备从账号中移除,
|
||||||
|
与官方客户端的「删除设备」相同。命令行方式已对一台测试设备实测删除成功;⚠️ 启动窗口里的按钮尚未实机点击验收;
|
||||||
|
分享给你的设备(`--shared`)只有字符串依据,未实测。
|
||||||
|
Remove a device from the account (CLI verified live on a test device; launcher button and `--shared` not yet verified).
|
||||||
|
|
||||||
|
### 已知问题 / Known issues
|
||||||
|
- 剪贴板只在控制端窗口获得焦点时同步;控制端 → 官方 Windows 被控端的剪贴板仍在排查。
|
||||||
|
- Clipboard syncs only while the viewer window is focused; viewer → official Windows host clipboard is still unresolved.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.2.4-x86_64-linux.tar.gz`:主机 `uuremote-server`(X11 版)与控制端 `uuremote-viewer`(x86_64,glibc ≥ 2.35;控制端需 SDL2、GTK3、ffmpeg),附带 `contrib/`。主机端与 v0.2.3 相同。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
窗口颜色修复;控制端可接收官方 Mac 被控端的剪贴板。/ Window colors fixed; viewer receives the official Mac host's clipboard.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- **深色系统主题下窗口文字看不见**(#43):协助窗口、控制端启动窗口和工具窗口使用固定浅色背景,但部分文字跟随系统深色主题变成了白色。
|
||||||
|
现在这些窗口固定使用浅色主题。已在虚拟机中设置深色偏好后截图确认。
|
||||||
|
GTK windows now stay on the light theme, so text is readable under dark system themes.
|
||||||
|
- **控制端 ↔ 官方 Mac 被控端剪贴板**:Mac 被控端以 `public.utf8-plain-text`(UTF-8)提供文本,而不是 Windows 的 `CF_UNICODETEXT`,
|
||||||
|
之前控制端不会请求,剪贴板始终不同步。现在两种格式都支持;主机端发给官方客户端的格式不变。
|
||||||
|
Mac → 控制端已实机验证;⚠️ 控制端 → Mac 尚未验证。
|
||||||
|
The viewer now handles the Mac host's UTF-8 text format (Mac → viewer verified live; viewer → Mac not yet verified).
|
||||||
|
|
||||||
|
### 已验证 / Verified with the official Mac host(FFmpeg 9.0.2)
|
||||||
|
- 画面 0 解码错误,键盘输入正确,文件上传/下载/列目录 sha256 一致。
|
||||||
|
|
||||||
|
### 已知问题 / Known issues
|
||||||
|
- 剪贴板只在控制端窗口获得焦点时同步;控制端 → 官方 Windows 被控端的剪贴板仍在排查。
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.2.5-x86_64-linux.tar.gz`:主机 `uuremote-server`(X11 版)与控制端 `uuremote-viewer`(x86_64,glibc ≥ 2.35;控制端需 SDL2、GTK3、ffmpeg),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
控制端可打开官方 Mac 被控端的终端;输入问题诊断日志。/ Viewer terminal works with the official Mac host; input diagnostics.
|
||||||
|
|
||||||
|
### 控制端修复 / Viewer — fixed
|
||||||
|
- **连接官方 Mac 被控端的终端**:之前一连上就断开(「Terminal channel closed」)。Mac 被控端把终端的应答发在终端通道上,
|
||||||
|
控制端现在能识别。已实机验证:可打开终端并正常回显。Mac 被控端的端口映射也已实测可用。
|
||||||
|
Terminal sessions with the official Mac host now open (verified live); port mapping with it was verified too.
|
||||||
|
|
||||||
|
### 主机端诊断 / Host diagnostics(#46)
|
||||||
|
- 收到不认识的输入事件时记一条日志(只有动作名和字段名,**不含输入内容**),用于排查手机输入法打字无反应。
|
||||||
|
- 小键盘按键记录被控端当前的 NumLock 状态(只记开/关,不记按键),用于排查小键盘不出数字。
|
||||||
|
- Logs unsupported input actions (names only, never typed text) and the server's NumLock state for numpad keys.
|
||||||
|
|
||||||
|
### 其他 / Other
|
||||||
|
- README 标题与开头加入控制端介绍并放上截图(#44)。
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.2.6-x86_64-linux.tar.gz`:主机 `uuremote-server`(X11 版)与控制端 `uuremote-viewer`(x86_64,glibc ≥ 2.35;控制端需 SDL2、GTK3、ffmpeg),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
手机输入法打字(实验);控制端 → 官方 Windows 被控端剪贴板修复。/ Mobile input-method text (experimental); viewer → Windows host clipboard fixed.
|
||||||
|
|
||||||
|
### 主机端 / Host
|
||||||
|
- **手机自带输入法打字**(#13、#46):官方 Windows 被控端用 `text_input` 动作接收手机输入法的文字。本版主机端支持这个动作,
|
||||||
|
在 X11 上把文字(含中文)直接输入到当前窗口;不论它从哪个数据通道到达都会处理。
|
||||||
|
已用手机 UU 远程实测:手机自带键盘输入的英文正确打出(手机在 CONTROL 通道发送 `text_input`);中文注入已在 X11 上
|
||||||
|
单独实测正确,但尚未用手机中文输入法实测。Wayland / 门户后端暂不支持,会记录一次警告。
|
||||||
|
诊断:非 CONTROL 通道收到的输入动作会记录动作名(不含内容)。
|
||||||
|
- Mobile input-method text via the official `text_input {content}` action, typed on X11. Verified with the UU mobile app
|
||||||
|
(English); Chinese injection verified on X11 separately, not yet with a phone IME.
|
||||||
|
|
||||||
|
### 控制端修复 / Viewer — fixed
|
||||||
|
- **控制端 → 官方 Windows 被控端剪贴板**:之前被控端确认收到,但粘贴为空。原因是控制端同时发送的「富剪贴板格式通告」
|
||||||
|
会覆盖刚写入的文本,而 Windows 被控端之后从不请求数据。现在按被控端平台发送:Windows 及本项目主机只发文本变更,
|
||||||
|
Mac 只发格式通告,平台未知时两者都发。已实测:Windows 被控端「运行」框可粘贴,官方 Mac 被控端正常。
|
||||||
|
Viewer → official Windows host clipboard now works (verified live); Mac unchanged and verified.
|
||||||
|
|
||||||
|
### 社区贡献 / Community
|
||||||
|
- **Mac SSH 粘贴助手**(#45,感谢 @jo32):`contrib/macos-ssh/`,在 Ghostty 里经 UU 端口映射用 SSH 时粘贴 Finder 文件会先上传再插入路径。
|
||||||
|
可选工具,维护者尚未实机验收。Optional contrib tool, not verified by the maintainer.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.2.7-x86_64-linux.tar.gz`:主机 `uuremote-server`(X11 版)与控制端 `uuremote-viewer`(x86_64,glibc ≥ 2.35;控制端需 SDL2、GTK3、ffmpeg),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
群晖 / NAS 容器镜像。/ Docker image for Synology and other NAS boxes.
|
||||||
|
|
||||||
|
### 新增 / Added
|
||||||
|
- **群晖 / NAS(Docker,无桌面)**:`ghcr.io/vallierdev/uu-remote-linux`,在没有图形界面的 NAS 上运行本项目主机,
|
||||||
|
供官方客户端使用文件传输、终端(容器内 bash)和端口映射(经 UU 访问家里局域网的服务)。不提供远程桌面。
|
||||||
|
已在群晖 DSM 7.3.2(x86_64)上实测:文件上传下载 sha256 一致、终端回显正常、端口映射访问 DSM 管理页返回 200。
|
||||||
|
安装与首次扫码登录见 [docker/README.md](https://github.com/VallierDev/uu-remote-linux/blob/main/docker/README.md)。
|
||||||
|
- Headless NAS container image (file transfer, terminal, port mapping), verified on Synology DSM 7.3.2 (x86_64).
|
||||||
|
|
||||||
|
主机端与控制端程序与 v0.2.7 相同。/ Host and viewer binaries are unchanged from v0.2.7.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.2.8-x86_64-linux.tar.gz`(主机 `uuremote-server` + 控制端 `uuremote-viewer`,附带 `contrib/`),或 `docker pull ghcr.io/vallierdev/uu-remote-linux:v0.2.8`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
+73
-12
@@ -5,11 +5,37 @@ use crate::protocol::{decode_wire,WireValue};
|
|||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
const UNICODE_TEXT:u64=13;
|
const UNICODE_TEXT:u64=13;
|
||||||
|
/// The official Mac host offers its pasteboard text as format id 0 named `public.utf8-plain-text` (observed live).
|
||||||
|
const MAC_TEXT:&str="public.utf8-plain-text";
|
||||||
|
#[derive(Clone,Copy,PartialEq,Eq,Debug)]
|
||||||
|
enum TextFormat {Windows,Mac}
|
||||||
|
impl TextFormat {
|
||||||
|
fn of(format:&[u8])->Option<Self>{
|
||||||
|
if number(format,1)==Ok(UNICODE_TEXT){return Some(Self::Windows)}
|
||||||
|
let named=|n|text_field(format,n).is_ok_and(|s|s==MAC_TEXT);
|
||||||
|
(named(2)||named(3)).then_some(Self::Mac)
|
||||||
|
}
|
||||||
|
fn id(self)->u64{match self{Self::Windows=>UNICODE_TEXT,Self::Mac=>0}}
|
||||||
|
fn name(self)->&'static str{match self{Self::Windows=>"CF_UNICODETEXT",Self::Mac=>MAC_TEXT}}
|
||||||
|
fn encode(self,text:&str)->Vec<u8>{match self{Self::Windows=>encode_windows(text),Self::Mac=>text.as_bytes().to_vec()}}
|
||||||
|
fn decode(self,data:&[u8])->Result<String,&'static str>{
|
||||||
|
match self{
|
||||||
|
Self::Windows=>decode_windows(data),
|
||||||
|
Self::Mac=>{
|
||||||
|
let data=data.strip_suffix(&[0]).unwrap_or(data);
|
||||||
|
if data.len()>MAX_TEXT{return Err("clipboard text exceeds limit");}
|
||||||
|
let text=std::str::from_utf8(data).map_err(|_|"invalid UTF-8 clipboard text")?;
|
||||||
|
if text.contains('\0'){return Err("embedded NUL in clipboard text");}
|
||||||
|
Ok(text.to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
const BLOCK_BYTES:usize=0x20000;
|
const BLOCK_BYTES:usize=0x20000;
|
||||||
const MAX_BYTES:usize=MAX_TEXT*2+2;
|
const MAX_BYTES:usize=MAX_TEXT*2+2;
|
||||||
pub struct ResultMessages {pub messages:Vec<(u32,Vec<u8>)>,pub text:Option<String>}
|
pub struct ResultMessages {pub messages:Vec<(u32,Vec<u8>)>,pub text:Option<String>}
|
||||||
impl Default for ResultMessages {fn default()->Self{Self{messages:Vec::new(),text:None}}}
|
impl Default for ResultMessages {fn default()->Self{Self{messages:Vec::new(),text:None}}}
|
||||||
struct Pending {key:String, count:Option<usize>, blocks:BTreeMap<usize,Vec<u8>>}
|
struct Pending {key:String, format:TextFormat, count:Option<usize>, blocks:BTreeMap<usize,Vec<u8>>}
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct State {local:Option<String>, pending:Option<Pending>}
|
pub struct State {local:Option<String>, pending:Option<Pending>}
|
||||||
pub fn format_ids(rpc:&[u8])->Vec<u64>{
|
pub fn format_ids(rpc:&[u8])->Vec<u64>{
|
||||||
@@ -62,12 +88,15 @@ fn decode_windows(data:&[u8])->Result<String,&'static str>{
|
|||||||
if text.len()>MAX_TEXT{return Err("clipboard text exceeds limit");}Ok(text)
|
if text.len()>MAX_TEXT{return Err("clipboard text exceeds limit");}Ok(text)
|
||||||
}
|
}
|
||||||
impl State{
|
impl State{
|
||||||
pub fn offer(&mut self,id:u64,text:String)->Result<Vec<u8>,&'static str>{
|
pub fn offer(&mut self,id:u64,text:String)->Result<Vec<u8>,&'static str>{self.offer_formats(id,text,&[TextFormat::Windows])}
|
||||||
|
/// Viewer offers: Windows hosts pick CF_UNICODETEXT, the official Mac host its UTF-8 type.
|
||||||
|
pub fn offer_any_host(&mut self,id:u64,text:String)->Result<Vec<u8>,&'static str>{self.offer_formats(id,text,&[TextFormat::Windows,TextFormat::Mac])}
|
||||||
|
fn offer_formats(&mut self,id:u64,text:String,formats:&[TextFormat])->Result<Vec<u8>,&'static str>{
|
||||||
if text.len()>MAX_TEXT{return Err("clipboard text exceeds limit");}
|
if text.len()>MAX_TEXT{return Err("clipboard text exceeds limit");}
|
||||||
if text.contains('\0')||encode_windows(&text).len()>MAX_BYTES{return Err("clipboard encoded text exceeds limit");}
|
if text.contains('\0')||encode_windows(&text).len()>MAX_BYTES{return Err("clipboard encoded text exceeds limit");}
|
||||||
self.local=Some(text);
|
self.local=Some(text);
|
||||||
let mut format=Vec::new();int(1,UNICODE_TEXT,&mut format);string(2,"CF_UNICODETEXT",&mut format);
|
let mut list=Vec::new();
|
||||||
let mut list=Vec::new();nested(1,&format,&mut list);
|
for kind in formats {let mut format=Vec::new();int(1,kind.id(),&mut format);string(2,kind.name(),&mut format);nested(1,&format,&mut list);}
|
||||||
Ok(request(id,1,&list))
|
Ok(request(id,1,&list))
|
||||||
}
|
}
|
||||||
pub fn request(&mut self,rpc:&[u8],next:&mut impl FnMut()->u64)->Result<ResultMessages,&'static str>{
|
pub fn request(&mut self,rpc:&[u8],next:&mut impl FnMut()->u64)->Result<ResultMessages,&'static str>{
|
||||||
@@ -78,22 +107,30 @@ impl State{
|
|||||||
if let Some(list)=bytes(clip,1)?{
|
if let Some(list)=bytes(clip,1)?{
|
||||||
out.messages.push((22,response(h,1,&[])));
|
out.messages.push((22,response(h,1,&[])));
|
||||||
let fields=decode_wire(list).map_err(|_|"invalid clipboard format list")?;
|
let fields=decode_wire(list).map_err(|_|"invalid clipboard format list")?;
|
||||||
let supports_text=fields.iter().filter(|f|f.number==1).any(|f|match f.value{WireValue::Bytes(format)=>number(format,1)==Ok(UNICODE_TEXT),_=>false});
|
let offered:Vec<TextFormat>=fields.iter().filter(|f|f.number==1).filter_map(|f|match f.value{WireValue::Bytes(format)=>TextFormat::of(format),_=>None}).collect();
|
||||||
|
let chosen=if offered.contains(&TextFormat::Windows){Some(TextFormat::Windows)}else{offered.first().copied()};
|
||||||
|
let supports_text=chosen.is_some();
|
||||||
self.pending=None;
|
self.pending=None;
|
||||||
if supports_text{
|
if !supports_text{
|
||||||
|
// Format ids/names only (never data), to learn non-Windows peers' text formats.
|
||||||
|
let formats:Vec<(u64,String)>=fields.iter().filter(|f|f.number==1).filter_map(|f|match f.value{WireValue::Bytes(format)=>Some(format),_=>None})
|
||||||
|
.take(8).map(|format|(number(format,1).unwrap_or(u64::MAX),decode_wire(format).ok().and_then(|x|x.into_iter().find_map(|y|match (y.number,y.value){(n,WireValue::Bytes(s)) if n!=1=>std::str::from_utf8(s).ok().map(|s|s.chars().take(48).collect()),_=>None})).unwrap_or_default())).collect();
|
||||||
|
log::info!("Clipboard offer without a supported text format: formats={formats:?}");
|
||||||
|
}
|
||||||
|
if let Some(format)=chosen{
|
||||||
let id=next();
|
let id=next();
|
||||||
let epoch=std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs() as u32;
|
let epoch=std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs() as u32;
|
||||||
let key=format!("{epoch:08x}-0000-4000-8000-{:012x}",id & 0xffffffffffff);
|
let key=format!("{epoch:08x}-0000-4000-8000-{:012x}",id & 0xffffffffffff);
|
||||||
let mut ask=Vec::new();int(1,UNICODE_TEXT,&mut ask);string(2,&key,&mut ask);string(3,"CF_UNICODETEXT",&mut ask);
|
let mut ask=Vec::new();int(1,format.id(),&mut ask);string(2,&key,&mut ask);string(3,format.name(),&mut ask);
|
||||||
self.pending=Some(Pending{key,count:None,blocks:BTreeMap::new()});
|
self.pending=Some(Pending{key,format,count:None,blocks:BTreeMap::new()});
|
||||||
out.messages.push((21,request(id,2,&ask)));
|
out.messages.push((21,request(id,2,&ask)));
|
||||||
}
|
}
|
||||||
}else if let Some(ask)=bytes(clip,2)?{
|
}else if let Some(ask)=bytes(clip,2)?{
|
||||||
let key=text_field(ask,2)?;
|
let key=text_field(ask,2)?;
|
||||||
if key.len()>256||key.is_empty(){return Err("invalid clipboard block key");}
|
if key.len()>256||key.is_empty(){return Err("invalid clipboard block key");}
|
||||||
let available=number(ask,1)?==UNICODE_TEXT&&self.local.is_some();
|
let format=if number(ask,1)?==UNICODE_TEXT{Some(TextFormat::Windows)}else if text_field(ask,3).is_ok_and(|s|s==MAC_TEXT){Some(TextFormat::Mac)}else{None};
|
||||||
let mut confirm=Vec::new();int(1,if available{1}else{2},&mut confirm);string(2,key,&mut confirm);
|
let data=format.zip(self.local.as_ref()).map(|(format,text)|format.encode(text));
|
||||||
let data=if available{Some(encode_windows(self.local.as_ref().unwrap()))}else{None};
|
let mut confirm=Vec::new();int(1,if data.is_some(){1}else{2},&mut confirm);string(2,key,&mut confirm);
|
||||||
if let Some(data)=&data{int(3,data.chunks(BLOCK_BYTES).len() as u64,&mut confirm);}
|
if let Some(data)=&data{int(3,data.chunks(BLOCK_BYTES).len() as u64,&mut confirm);}
|
||||||
out.messages.push((22,response(h,2,&confirm)));
|
out.messages.push((22,response(h,2,&confirm)));
|
||||||
if let Some(data)=data{
|
if let Some(data)=data{
|
||||||
@@ -144,13 +181,37 @@ impl State{
|
|||||||
else if (1..=count).all(|i|pending.blocks.contains_key(&i)){1}
|
else if (1..=count).all(|i|pending.blocks.contains_key(&i)){1}
|
||||||
else{return Ok(None);};
|
else{return Ok(None);};
|
||||||
let data:Vec<u8>=(first..first+count).flat_map(|i|pending.blocks[&i].iter().copied()).collect();
|
let data:Vec<u8>=(first..first+count).flat_map(|i|pending.blocks[&i].iter().copied()).collect();
|
||||||
self.pending=None;decode_windows(&data).map(Some)
|
let format=pending.format;self.pending=None;format.decode(&data).map(Some)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests{
|
mod tests{
|
||||||
use super::*;
|
use super::*;
|
||||||
|
#[test] fn mac_utf8_text_round_trips_both_directions(){
|
||||||
|
// Mac host offer: format list {1 id 0, 2 "public.utf8-plain-text"} (observed live).
|
||||||
|
let mut mac=Vec::new();int(1,0,&mut mac);string(2,MAC_TEXT,&mut mac);
|
||||||
|
let mut list=Vec::new();nested(1,&mac,&mut list);
|
||||||
|
let mut viewer=State::default();let mut id=200;let mut next=||{id+=1;id};
|
||||||
|
let asked=viewer.request(&request(7,1,&list),&mut next).unwrap();
|
||||||
|
assert_eq!(asked.messages.len(),2);
|
||||||
|
let ask_rpc=&asked.messages[1].1;let clip=bytes(ask_rpc,9).unwrap().unwrap();let ask=bytes(clip,2).unwrap().unwrap();
|
||||||
|
assert_eq!(number(ask,1),Ok(0));assert_eq!(text_field(ask,3),Ok(MAC_TEXT));
|
||||||
|
let key=text_field(ask,2).unwrap().to_owned();
|
||||||
|
let mut confirm=Vec::new();int(1,1,&mut confirm);string(2,&key,&mut confirm);int(3,1,&mut confirm);
|
||||||
|
assert_eq!(viewer.response(&response(header(ask_rpc).unwrap(),2,&confirm)).unwrap(),None);
|
||||||
|
let mut block=Vec::new();string(1,&key,&mut block);int(2,0,&mut block);nested(3,"中文 text\n".as_bytes(),&mut block);
|
||||||
|
assert_eq!(viewer.request(&request(8,3,&block),&mut next).unwrap().text.as_deref(),Some("中文 text\n"));
|
||||||
|
// Viewer offer lists both formats; a Mac-style ask gets UTF-8 bytes back.
|
||||||
|
let offer=viewer.offer_any_host(9,"hi".into()).unwrap();
|
||||||
|
assert_eq!(format_ids(&offer),vec![13,0]);
|
||||||
|
let mut ask=Vec::new();int(1,0,&mut ask);string(2,"k",&mut ask);string(3,MAC_TEXT,&mut ask);
|
||||||
|
let out=viewer.request(&request(10,2,&ask),&mut next).unwrap();
|
||||||
|
let data=bytes(bytes(bytes(&out.messages[1].1,9).unwrap().unwrap(),3).unwrap().unwrap(),3).unwrap().unwrap();
|
||||||
|
assert_eq!(data,b"hi");
|
||||||
|
// Host-side offers are unchanged: CF_UNICODETEXT only.
|
||||||
|
assert_eq!(format_ids(&State::default().offer(1,"hi".into()).unwrap()),vec![13]);
|
||||||
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn desktop_clipboard_negotiates_and_buffers_unicode_until_confirmation(){
|
fn desktop_clipboard_negotiates_and_buffers_unicode_until_confirmation(){
|
||||||
let text="UU-test-中文🙂\n".repeat(900);
|
let text="UU-test-中文🙂\n".repeat(900);
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ pub enum ControlInput {
|
|||||||
MouseAbsolute { x: f64, y: f64 },
|
MouseAbsolute { x: f64, y: f64 },
|
||||||
MouseRelative { dx: i32, dy: i32 },
|
MouseRelative { dx: i32, dy: i32 },
|
||||||
MouseScroll { dx: i32, dy: i32 },
|
MouseScroll { dx: i32, dy: i32 },
|
||||||
|
/// Official Windows host `text_input {content}` (KeyboardMouseRunner::execTextInput): text typed by the
|
||||||
|
/// mobile clients' own input method. Never logged.
|
||||||
|
TextInput { text: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
@@ -58,6 +61,11 @@ pub fn decode_control(data: &[u8]) -> Result<ControlMessage, String> {
|
|||||||
"mouse_move_relative" => ControlInput::MouseRelative {
|
"mouse_move_relative" => ControlInput::MouseRelative {
|
||||||
dx: integer(&v, "delta_x")?, dy: integer(&v, "delta_y")?,
|
dx: integer(&v, "delta_x")?, dy: integer(&v, "delta_y")?,
|
||||||
},
|
},
|
||||||
|
"text_input" => {
|
||||||
|
let text = v.get("content").and_then(Value::as_str).ok_or("CONTROL field content must be a string")?;
|
||||||
|
if text.len() > 16 * 1024 { return Err("text_input content exceeds 16 KiB".into()); }
|
||||||
|
ControlInput::TextInput { text: text.to_owned() }
|
||||||
|
}
|
||||||
"mouse_scroll" => ControlInput::MouseScroll {
|
"mouse_scroll" => ControlInput::MouseScroll {
|
||||||
dx: integer(&v, "delta_x")?, dy: integer(&v, "delta_y")?,
|
dx: integer(&v, "delta_x")?, dy: integer(&v, "delta_y")?,
|
||||||
},
|
},
|
||||||
@@ -137,6 +145,9 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn does_not_invent_actions_for_unknown_json() {
|
fn does_not_invent_actions_for_unknown_json() {
|
||||||
assert!(matches!(decode_control(br#"{"action":"unverified"}"#).unwrap(), ControlMessage::Unsupported(_)));
|
assert!(matches!(decode_control(br#"{"action":"unverified"}"#).unwrap(), ControlMessage::Unsupported(_)));
|
||||||
|
assert_eq!(decode_control("{\"action\":\"text_input\",\"content\":\"你好 a\"}".as_bytes()).unwrap(),
|
||||||
|
ControlMessage::Input(ControlInput::TextInput { text: "你好 a".into() }));
|
||||||
|
assert!(decode_control(br#"{"action":"text_input"}"#).is_err());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,12 @@ extern "C" {
|
|||||||
fn x11_key_event(keycode: libc::c_int, is_press: libc::c_int) -> libc::c_int;
|
fn x11_key_event(keycode: libc::c_int, is_press: libc::c_int) -> libc::c_int;
|
||||||
fn x11_release_all() -> libc::c_int;
|
fn x11_release_all() -> libc::c_int;
|
||||||
fn x11_terminal_shortcut_state() -> libc::c_int;
|
fn x11_terminal_shortcut_state() -> libc::c_int;
|
||||||
|
fn x11_type_utf8(text: *const libc::c_char) -> libc::c_int;
|
||||||
}
|
}
|
||||||
#[cfg(not(target_os = "linux"))]
|
#[cfg(not(target_os = "linux"))]
|
||||||
unsafe fn x11_terminal_shortcut_state() -> libc::c_int { 0 }
|
unsafe fn x11_terminal_shortcut_state() -> libc::c_int { 0 }
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
|
unsafe fn x11_type_utf8(_: *const libc::c_char) -> libc::c_int { 0 }
|
||||||
|
|
||||||
#[cfg(not(target_os = "linux"))]
|
#[cfg(not(target_os = "linux"))]
|
||||||
unsafe fn x11_mouse_move(_: libc::c_int, _: libc::c_int) -> libc::c_int { 0 }
|
unsafe fn x11_mouse_move(_: libc::c_int, _: libc::c_int) -> libc::c_int { 0 }
|
||||||
@@ -248,6 +251,14 @@ impl InputInjector {
|
|||||||
if AUDIT.fetch_add(1,std::sync::atomic::Ordering::Relaxed) < 8 {
|
if AUDIT.fetch_add(1,std::sync::atomic::Ordering::Relaxed) < 8 {
|
||||||
log::info!("Desktop keyboard injection outcome: result={result}");
|
log::info!("Desktop keyboard injection outcome: result={result}");
|
||||||
}
|
}
|
||||||
|
// KP_0..KP_9 type digits only with NumLock on (#46); record the server's NumLock state, not the key.
|
||||||
|
static NUMPAD: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||||
|
if (0x60..=0x6f).contains(&key) && is_press && self.backend==crate::desktop_backend::DesktopBackend::X11
|
||||||
|
&& NUMPAD.fetch_add(1,std::sync::atomic::Ordering::Relaxed) < 4 {
|
||||||
|
let (mut x,mut y,mut mask)=(0,0,0u32);
|
||||||
|
let state=unsafe { x11_pointer_state(&mut x,&mut y,&mut mask) };
|
||||||
|
log::info!("Desktop numpad key: result={result} numlock={}", state==0 && mask & 0x10 != 0);
|
||||||
|
}
|
||||||
if result == 0 {
|
if result == 0 {
|
||||||
let mut held=self.held.lock().unwrap();
|
let mut held=self.held.lock().unwrap();
|
||||||
if is_press {held.keys.insert(symbol);} else {held.keys.remove(&symbol);}
|
if is_press {held.keys.insert(symbol);} else {held.keys.remove(&symbol);}
|
||||||
@@ -349,6 +360,20 @@ impl InputInjector {
|
|||||||
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::key(symbol,pressed) }
|
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::key(symbol,pressed) }
|
||||||
else { unsafe { x11_key_symbol(symbol as libc::c_ulong,i32::from(pressed)) } }
|
else { unsafe { x11_key_symbol(symbol as libc::c_ulong,i32::from(pressed)) } }
|
||||||
}
|
}
|
||||||
|
/// Mobile input-method text (`text_input`). X11 only for now; other backends log once.
|
||||||
|
pub fn text_input(&self, text: &str) {
|
||||||
|
let _serial=self.serial.lock().unwrap();
|
||||||
|
let Ok(c_text)=std::ffi::CString::new(text.replace('\0',"")) else { return };
|
||||||
|
if self.backend==crate::desktop_backend::DesktopBackend::X11 {
|
||||||
|
let typed=unsafe { x11_type_utf8(c_text.as_ptr()) };
|
||||||
|
log::info!("Desktop text input: chars={} result={}", text.chars().count(), typed);
|
||||||
|
} else {
|
||||||
|
static WARNED:std::sync::atomic::AtomicBool=std::sync::atomic::AtomicBool::new(false);
|
||||||
|
if !WARNED.swap(true,std::sync::atomic::Ordering::Relaxed) {
|
||||||
|
log::warn!("Mobile text input is not supported on the {:?} backend yet", self.backend);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn key_event(&self, keycode: i32, is_press: bool) {
|
pub fn key_event(&self, keycode: i32, is_press: bool) {
|
||||||
let _serial=self.serial.lock().unwrap();
|
let _serial=self.serial.lock().unwrap();
|
||||||
if self.backend!=crate::desktop_backend::DesktopBackend::X11 { return; }
|
if self.backend!=crate::desktop_backend::DesktopBackend::X11 { return; }
|
||||||
|
|||||||
+130
-5
@@ -14,9 +14,16 @@ pub struct JoinData {
|
|||||||
#[serde(default)] pub report_server_address: String,
|
#[serde(default)] pub report_server_address: String,
|
||||||
#[serde(default)] pub streamer_retry_delta_ms: i32,
|
#[serde(default)] pub streamer_retry_delta_ms: i32,
|
||||||
#[serde(default)] pub international_connect: bool,
|
#[serde(default)] pub international_connect: bool,
|
||||||
|
#[serde(default)] pub publisher_platform: Option<i32>,
|
||||||
|
}
|
||||||
|
fn device_platform(data:&Value,device:&str)->Option<i64>{
|
||||||
|
["my_binded_devices","others_shared_devices"].into_iter()
|
||||||
|
.filter_map(|key|data.get(key).and_then(Value::as_array)).flatten()
|
||||||
|
.find(|d|d.get("device_id").and_then(Value::as_str)==Some(device))
|
||||||
|
.and_then(|d|d.get("platform")).and_then(Value::as_i64)
|
||||||
}
|
}
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum Error { AuthenticationExpired, Api(i32, String), InvalidResponse, Transport, MissingRoom, InvalidDevice, InvalidAssist }
|
pub enum Error { AuthenticationExpired, Api(i32, String), InvalidResponse, Transport, MissingRoom, InvalidDevice, InvalidAssist, UnknownKeyboardPlatform }
|
||||||
impl std::fmt::Display for Error {
|
impl std::fmt::Display for Error {
|
||||||
// The server's short message is user-facing (e.g. 2002 「被控端正在被远控中,无法发起连接」).
|
// The server's short message is user-facing (e.g. 2002 「被控端正在被远控中,无法发起连接」).
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
@@ -27,6 +34,7 @@ impl std::fmt::Display for Error {
|
|||||||
Error::InvalidAssist => write!(f, "请输入对方的设备 ID 和有效验证码"),
|
Error::InvalidAssist => write!(f, "请输入对方的设备 ID 和有效验证码"),
|
||||||
Error::InvalidResponse => write!(f, "服务器响应格式无效"),
|
Error::InvalidResponse => write!(f, "服务器响应格式无效"),
|
||||||
Error::MissingRoom => write!(f, "服务器未返回可用的房间或信令信息"),
|
Error::MissingRoom => write!(f, "服务器未返回可用的房间或信令信息"),
|
||||||
|
Error::UnknownKeyboardPlatform => write!(f, "无法确定目标键盘平台,不能启用控制"),
|
||||||
Error::Api(code, _) => write!(f, "连接请求失败(错误码 {code})"),
|
Error::Api(code, _) => write!(f, "连接请求失败(错误码 {code})"),
|
||||||
_ => write!(f, "viewer API: {self:?}"),
|
_ => write!(f, "viewer API: {self:?}"),
|
||||||
}
|
}
|
||||||
@@ -34,6 +42,18 @@ impl std::fmt::Display for Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Mirrors Windows 0x140b77cd0 / 0x140b7a6d0, not host RoomData.
|
// Mirrors Windows 0x140b77cd0 / 0x140b7a6d0, not host RoomData.
|
||||||
|
/// Treat the caller's device string as one opaque path segment, never a URL/query.
|
||||||
|
fn path_segment(device: &str) -> Result<String, Error> {
|
||||||
|
if device.is_empty() || device=="." || device==".." { return Err(Error::InvalidDevice); }
|
||||||
|
Ok(device.bytes().map(|b|if b.is_ascii_alphanumeric() || b"-._~".contains(&b) {(b as char).to_string()}else{format!("%{b:02X}")}).collect())
|
||||||
|
}
|
||||||
|
fn parse_ack(status: u16, body: Value) -> Result<(), Error> {
|
||||||
|
let code = body.get("code").and_then(Value::as_i64).unwrap_or(-1);
|
||||||
|
if code == 1120 { return Err(Error::AuthenticationExpired); }
|
||||||
|
if status == 200 && code == 0 { return Ok(()); }
|
||||||
|
let msg = body.get("msg").and_then(Value::as_str).unwrap_or("").chars().take(120).collect();
|
||||||
|
Err(Error::Api(i32::try_from(code).unwrap_or(-1), msg))
|
||||||
|
}
|
||||||
pub fn parse_join(status: u16, value: Value) -> Result<JoinData, Error> {
|
pub fn parse_join(status: u16, value: Value) -> Result<JoinData, Error> {
|
||||||
let code = value.get("code").and_then(Value::as_i64).and_then(|v| i32::try_from(v).ok()).ok_or(Error::InvalidResponse)?;
|
let code = value.get("code").and_then(Value::as_i64).and_then(|v| i32::try_from(v).ok()).ok_or(Error::InvalidResponse)?;
|
||||||
let msg = value.get("msg").and_then(Value::as_str).ok_or(Error::InvalidResponse)?.chars().take(120).collect::<String>();
|
let msg = value.get("msg").and_then(Value::as_str).ok_or(Error::InvalidResponse)?.chars().take(120).collect::<String>();
|
||||||
@@ -102,10 +122,7 @@ impl Api {
|
|||||||
Ok((status, response.json().await.map_err(|_| Error::InvalidResponse)?))
|
Ok((status, response.json().await.map_err(|_| Error::InvalidResponse)?))
|
||||||
}
|
}
|
||||||
pub async fn join(&self, device: &str, force_join: bool, retry: bool) -> Result<JoinData, Error> {
|
pub async fn join(&self, device: &str, force_join: bool, retry: bool) -> Result<JoinData, Error> {
|
||||||
// Treat the caller's device string as one opaque path segment, never a URL/query.
|
let path = format!("/api/v1/room/join/by_device/{}", path_segment(device)?);
|
||||||
if device.is_empty() || device=="." || device==".." { return Err(Error::InvalidDevice); }
|
|
||||||
let encoded=device.bytes().map(|b|if b.is_ascii_alphanumeric() || b"-._~".contains(&b) {(b as char).to_string()}else{format!("%{b:02X}")}).collect::<String>();
|
|
||||||
let path = format!("/api/v1/room/join/by_device/{encoded}");
|
|
||||||
let mut retries = 0;
|
let mut retries = 0;
|
||||||
loop {
|
loop {
|
||||||
let result = match self.request(reqwest::Method::POST, &path, Some(json!({"force_join":force_join}))).await {
|
let result = match self.request(reqwest::Method::POST, &path, Some(json!({"force_join":force_join}))).await {
|
||||||
@@ -135,6 +152,15 @@ impl Api {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// Remove a device from the account (official 「删除设备」). Paths from the Windows host strings
|
||||||
|
/// `/api/v1/device/%s/unbind` and `/api/v1/device/share/%s/unbind` (own vs. shared-with-me device).
|
||||||
|
/// POST verified live against an own device; the shared-device path is from strings only.
|
||||||
|
pub async fn unbind(&self, device: &str, shared: bool) -> Result<(), Error> {
|
||||||
|
let segment = path_segment(device)?;
|
||||||
|
let path = if shared { format!("/api/v1/device/share/{segment}/unbind") } else { format!("/api/v1/device/{segment}/unbind") };
|
||||||
|
let (status, body) = self.request(reqwest::Method::POST, &path, None).await?;
|
||||||
|
parse_ack(status, body)
|
||||||
|
}
|
||||||
/// Structure-only probe of the device list (status, JSON keys/types/lengths, never values).
|
/// Structure-only probe of the device list (status, JSON keys/types/lengths, never values).
|
||||||
pub async fn devices_shape(&self, method: reqwest::Method) -> Result<String, Error> {
|
pub async fn devices_shape(&self, method: reqwest::Method) -> Result<String, Error> {
|
||||||
let path = "/api/v1/device/list";
|
let path = "/api/v1/device/list";
|
||||||
@@ -154,6 +180,21 @@ impl Api {
|
|||||||
if status != 200 || body.get("code").and_then(Value::as_i64) != Some(0) { return Err(Error::InvalidResponse); }
|
if status != 200 || body.get("code").and_then(Value::as_i64) != Some(0) { return Err(Error::InvalidResponse); }
|
||||||
body.get("data").cloned().ok_or(Error::InvalidResponse)
|
body.get("data").cloned().ok_or(Error::InvalidResponse)
|
||||||
}
|
}
|
||||||
|
pub async fn target_is_mac(&self,room:&JoinData,device:Option<&str>)->Result<bool,Error>{
|
||||||
|
let platform=match room.publisher_platform {
|
||||||
|
Some(platform)=>Some(i64::from(platform)),
|
||||||
|
None=>match device {
|
||||||
|
Some(device)=>device_platform(&self.device_list().await?,device),
|
||||||
|
None=>None,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
// Observed device/share platforms: 1 = Windows (also our Linux host), 4 = Mac.
|
||||||
|
match platform {
|
||||||
|
Some(1)=>Ok(false),
|
||||||
|
Some(4)=>Ok(true),
|
||||||
|
_=>Err(Error::UnknownKeyboardPlatform),
|
||||||
|
}
|
||||||
|
}
|
||||||
/// Method and wire-key mapping remain unverified. Caller must select both explicitly.
|
/// Method and wire-key mapping remain unverified. Caller must select both explicitly.
|
||||||
pub async fn devices(&self, method: reqwest::Method, array_pointer: &str) -> Result<Vec<Value>, Error> {
|
pub async fn devices(&self, method: reqwest::Method, array_pointer: &str) -> Result<Vec<Value>, Error> {
|
||||||
let (status, body) = self.request(method, "/api/v1/device/list", None).await?;
|
let (status, body) = self.request(method, "/api/v1/device/list", None).await?;
|
||||||
@@ -165,8 +206,27 @@ impl Api {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
#[test] fn unbind_ack_and_device_segment(){
|
||||||
|
use super::*;
|
||||||
|
assert_eq!(parse_ack(200,json!({"code":0,"msg":"ok"})),Ok(()));
|
||||||
|
assert_eq!(parse_ack(422,json!({"code":1120})),Err(Error::AuthenticationExpired));
|
||||||
|
assert_eq!(parse_ack(200,json!({"code":5003,"msg":"no such device"})),Err(Error::Api(5003,"no such device".into())));
|
||||||
|
assert_eq!(parse_ack(404,json!({})),Err(Error::Api(-1,String::new())));
|
||||||
|
assert_eq!(path_segment("a/b?c").unwrap(),"a%2Fb%3Fc");
|
||||||
|
for bad in ["",".",".."]{assert_eq!(path_segment(bad),Err(Error::InvalidDevice));}
|
||||||
|
}
|
||||||
use super::*;
|
use super::*;
|
||||||
#[test]
|
#[test]
|
||||||
|
fn target_platform_matches_only_the_selected_device(){
|
||||||
|
let data=json!({"my_binded_devices":[{"device_id":"windows","platform":1},{"device_id":"mac","platform":4}],
|
||||||
|
"others_shared_devices":[{"device_id":"shared-mac","platform":4}]});
|
||||||
|
assert_eq!(device_platform(&data,"windows"),Some(1));
|
||||||
|
assert_eq!(device_platform(&data,"mac"),Some(4));
|
||||||
|
assert_eq!(device_platform(&data,"shared-mac"),Some(4));
|
||||||
|
assert_eq!(device_platform(&data,"missing"),None);
|
||||||
|
assert_eq!(device_platform(&json!({}),"mac"),None);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
fn assistance_response_validation_and_safe_user_errors() {
|
fn assistance_response_validation_and_safe_user_errors() {
|
||||||
let mut fixture:Value=serde_json::from_str(include_str!("../../tests/fixtures/viewer/join.json")).unwrap();
|
let mut fixture:Value=serde_json::from_str(include_str!("../../tests/fixtures/viewer/join.json")).unwrap();
|
||||||
fixture["data"]["publisher_device_id"]=json!("synthetic-publisher");
|
fixture["data"]["publisher_device_id"]=json!("synthetic-publisher");
|
||||||
@@ -188,6 +248,11 @@ mod tests {
|
|||||||
let fixture: Value = serde_json::from_str(include_str!("../../tests/fixtures/viewer/join.json")).unwrap();
|
let fixture: Value = serde_json::from_str(include_str!("../../tests/fixtures/viewer/join.json")).unwrap();
|
||||||
let data = parse_join(200, fixture.clone()).unwrap();
|
let data = parse_join(200, fixture.clone()).unwrap();
|
||||||
assert_eq!(data.signaling_list.len(), 1);
|
assert_eq!(data.signaling_list.len(), 1);
|
||||||
|
assert_eq!(data.publisher_platform,None);
|
||||||
|
let mut empty=fixture.clone();empty["data"]["publisher_platform"]=Value::Null;
|
||||||
|
assert_eq!(parse_join(200,empty).unwrap().publisher_platform,None);
|
||||||
|
let mut mac=fixture.clone();mac["data"]["publisher_platform"]=json!(4);
|
||||||
|
assert_eq!(parse_join(200,mac).unwrap().publisher_platform,Some(4));
|
||||||
for key in ["report_token", "report_url"] {
|
for key in ["report_token", "report_url"] {
|
||||||
let mut v = fixture.clone(); v["data"].as_object_mut().unwrap().remove(key);
|
let mut v = fixture.clone(); v["data"].as_object_mut().unwrap().remove(key);
|
||||||
assert!(matches!(parse_join(200,v), Err(Error::InvalidResponse)));
|
assert!(matches!(parse_join(200,v), Err(Error::InvalidResponse)));
|
||||||
@@ -242,6 +307,62 @@ mod http_tests {
|
|||||||
(url,job)
|
(url,job)
|
||||||
}
|
}
|
||||||
fn signer()->UUAuthClient {UUAuthClient::new(Credentials{token:"synthetic-auth".into(),sign_key:"synthetic-key".into(),..Default::default()})}
|
fn signer()->UUAuthClient {UUAuthClient::new(Credentials{token:"synthetic-auth".into(),sign_key:"synthetic-key".into(),..Default::default()})}
|
||||||
|
fn room(platform:Option<i32>)->JoinData {
|
||||||
|
let mut room=parse_join(200,serde_json::from_str(include_str!("../../tests/fixtures/viewer/join.json")).unwrap()).unwrap();
|
||||||
|
room.publisher_platform=platform;room
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn keyboard_platform_uses_room_metadata_without_device_lookup(){
|
||||||
|
let api=Api::new(signer(),"http://127.0.0.1:1");
|
||||||
|
for device in [None,Some("synthetic-device")] {
|
||||||
|
assert_eq!(api.target_is_mac(&room(Some(1)),device).await,Ok(false));
|
||||||
|
assert_eq!(api.target_is_mac(&room(Some(4)),device).await,Ok(true));
|
||||||
|
for platform in [0,-1,2,3,5,999,i32::MIN,i32::MAX] {
|
||||||
|
assert_eq!(api.target_is_mac(&room(Some(platform)),device).await,Err(Error::UnknownKeyboardPlatform));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert_eq!(api.target_is_mac(&room(None),None).await,Err(Error::UnknownKeyboardPlatform));
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn keyboard_platform_looks_up_only_the_selected_account_device(){
|
||||||
|
for list in ["my_binded_devices","others_shared_devices"] {
|
||||||
|
for (platform,mac) in [(1,false),(4,true)] {
|
||||||
|
let data=json!({list:[{"device_id":"unrelated","platform":4},
|
||||||
|
{"device_id":"synthetic-device","platform":platform}]});
|
||||||
|
let(url,job)=server(vec![(200,json!({"code":0,"data":data}))]).await;
|
||||||
|
assert_eq!(Api::new(signer(),&url).target_is_mac(&room(None),Some("synthetic-device")).await,Ok(mac));
|
||||||
|
let requests=job.await.unwrap();assert_eq!(requests.len(),1);
|
||||||
|
assert!(requests[0].starts_with("GET /api/v1/device/list "));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn keyboard_platform_rejects_unknown_or_malformed_device_metadata(){
|
||||||
|
let mut devices=vec![json!({"device_id":"unrelated","platform":4}),json!({"device_id":"synthetic-device"})];
|
||||||
|
for platform in [json!(0),json!(-1),json!(999),json!("4"),json!(true),json!(4.5),json!(null),json!(u64::MAX)] {
|
||||||
|
devices.push(json!({"device_id":"synthetic-device","platform":platform}));
|
||||||
|
}
|
||||||
|
for device in devices {
|
||||||
|
let data=json!({"my_binded_devices":[device]});
|
||||||
|
let(url,job)=server(vec![(200,json!({"code":0,"data":data}))]).await;
|
||||||
|
assert_eq!(Api::new(signer(),&url).target_is_mac(&room(None),Some("synthetic-device")).await,Err(Error::UnknownKeyboardPlatform));
|
||||||
|
assert_eq!(job.await.unwrap().len(),1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn keyboard_platform_propagates_device_lookup_failures(){
|
||||||
|
for (status,body,error) in [
|
||||||
|
(422,json!({"code":1120}),Error::AuthenticationExpired),
|
||||||
|
(503,json!({"code":5000}),Error::InvalidResponse),
|
||||||
|
(200,json!({"code":0}),Error::InvalidResponse),
|
||||||
|
] {
|
||||||
|
let(url,job)=server(vec![(status,body)]).await;
|
||||||
|
assert_eq!(Api::new(signer(),&url).target_is_mac(&room(None),Some("synthetic-device")).await,Err(error));
|
||||||
|
assert_eq!(job.await.unwrap().len(),1);
|
||||||
|
}
|
||||||
|
let api=Api::new(signer(),"http://127.0.0.1:1");
|
||||||
|
assert_eq!(api.target_is_mac(&room(None),Some("synthetic-device")).await,Err(Error::Transport));
|
||||||
|
}
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn signed_join_and_explicit_device_list_use_only_loopback_http() {
|
async fn signed_join_and_explicit_device_list_use_only_loopback_http() {
|
||||||
let fixture=serde_json::from_str(include_str!("../../tests/fixtures/viewer/join.json")).unwrap();
|
let fixture=serde_json::from_str(include_str!("../../tests/fixtures/viewer/join.json")).unwrap();
|
||||||
@@ -325,6 +446,10 @@ mod strict_tests {
|
|||||||
for (key,bad) in [("token",json!(false)),("signaling_list",json!([7])),("ws_connect_timeout_ms",json!(2147483648u64)),("streamer_retry_delta_ms",json!(null)),("international_connect",json!(1)),("report_token",json!(null)),("report_url",json!(7)),("report_server_address",json!([]))] {
|
for (key,bad) in [("token",json!(false)),("signaling_list",json!([7])),("ws_connect_timeout_ms",json!(2147483648u64)),("streamer_retry_delta_ms",json!(null)),("international_connect",json!(1)),("report_token",json!(null)),("report_url",json!(7)),("report_server_address",json!([]))] {
|
||||||
let mut v=fixture.clone();v["data"][key]=bad;assert!(matches!(parse_join(200,v),Err(Error::InvalidResponse)));
|
let mut v=fixture.clone();v["data"][key]=bad;assert!(matches!(parse_join(200,v),Err(Error::InvalidResponse)));
|
||||||
}
|
}
|
||||||
|
for platform in [json!("4"),json!(true),json!(4.5),json!(2147483648u64),json!(-2147483649i64)] {
|
||||||
|
let mut v=fixture.clone();v["data"]["publisher_platform"]=platform;
|
||||||
|
assert!(matches!(parse_join(200,v),Err(Error::InvalidResponse)));
|
||||||
|
}
|
||||||
for key in ["code","msg"] {
|
for key in ["code","msg"] {
|
||||||
let mut v=fixture.clone();v.as_object_mut().unwrap().remove(key);assert!(matches!(parse_join(200,v),Err(Error::InvalidResponse)));
|
let mut v=fixture.clone();v.as_object_mut().unwrap().remove(key);assert!(matches!(parse_join(200,v),Err(Error::InvalidResponse)));
|
||||||
}
|
}
|
||||||
|
|||||||
+141
-22
@@ -12,7 +12,19 @@ const POLL:Duration=Duration::from_millis(500);
|
|||||||
static WARNED:AtomicBool=AtomicBool::new(false);
|
static WARNED:AtomicBool=AtomicBool::new(false);
|
||||||
fn warn(){if !WARNED.swap(true,Ordering::Relaxed){log::warn!("Viewer text clipboard unavailable; synchronization disabled");}}
|
fn warn(){if !WARNED.swap(true,Ordering::Relaxed){log::warn!("Viewer text clipboard unavailable; synchronization disabled");}}
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Backend {read:PathBuf,read_args:Vec<&'static str>,write:PathBuf,write_args:Vec<&'static str>}
|
struct CommandBackend {read:PathBuf,read_args:Vec<&'static str>,write:PathBuf,write_args:Vec<&'static str>}
|
||||||
|
#[derive(Clone)]
|
||||||
|
enum Backend {
|
||||||
|
Command(CommandBackend),
|
||||||
|
#[cfg(feature="viewer-sdl")]
|
||||||
|
Sdl(mpsc::Sender<ClipboardRequest>),
|
||||||
|
}
|
||||||
|
#[cfg(feature="viewer-sdl")]
|
||||||
|
pub enum ClipboardRequest {
|
||||||
|
Read(tokio::sync::oneshot::Sender<Result<Option<String>,()>>),
|
||||||
|
// false means the unfocused window skipped the write.
|
||||||
|
Write(String,tokio::sync::oneshot::Sender<Result<bool,()>>),
|
||||||
|
}
|
||||||
fn executable(name:&str)->Option<PathBuf>{
|
fn executable(name:&str)->Option<PathBuf>{
|
||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::PermissionsExt;
|
||||||
std::env::var_os("PATH").and_then(|p|std::env::split_paths(&p).map(|d|d.join(name))
|
std::env::var_os("PATH").and_then(|p|std::env::split_paths(&p).map(|d|d.join(name))
|
||||||
@@ -23,9 +35,38 @@ impl Backend {
|
|||||||
if std::env::var("UU_VIEWER_CLIPBOARD").as_deref()==Ok("0"){return None}
|
if std::env::var("UU_VIEWER_CLIPBOARD").as_deref()==Ok("0"){return None}
|
||||||
// Unit sessions must never inspect or change the developer's real clipboard.
|
// Unit sessions must never inspect or change the developer's real clipboard.
|
||||||
if !cfg!(target_os="linux") || cfg!(test){return None}
|
if !cfg!(target_os="linux") || cfg!(test){return None}
|
||||||
let selected=Self::select(std::env::var_os("WAYLAND_DISPLAY").is_some_and(|v|!v.is_empty()),executable);
|
let selected=CommandBackend::select(std::env::var_os("WAYLAND_DISPLAY").is_some_and(|v|!v.is_empty()),executable).map(Self::Command);
|
||||||
if selected.is_none(){warn();}selected
|
if selected.is_none(){warn();}selected
|
||||||
}
|
}
|
||||||
|
async fn read(&self)->Result<Option<String>,()>{
|
||||||
|
match self {
|
||||||
|
Self::Command(backend)=>backend.read().await,
|
||||||
|
#[cfg(feature="viewer-sdl")]
|
||||||
|
Self::Sdl(tx)=>{
|
||||||
|
let(reply,rx)=tokio::sync::oneshot::channel();
|
||||||
|
tokio::time::timeout(Duration::from_secs(2),async {
|
||||||
|
tx.send(ClipboardRequest::Read(reply)).await.map_err(|_|())?;
|
||||||
|
rx.await.map_err(|_|())?
|
||||||
|
}).await.map_err(|_|())?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async fn write(&self,value:&str)->Result<bool,()>{
|
||||||
|
if value.len()>MAX_TEXT || value.contains('\0'){return Err(())}
|
||||||
|
match self {
|
||||||
|
Self::Command(backend)=>backend.write(value).await.map(|()|true),
|
||||||
|
#[cfg(feature="viewer-sdl")]
|
||||||
|
Self::Sdl(tx)=>{
|
||||||
|
let(reply,rx)=tokio::sync::oneshot::channel();
|
||||||
|
tokio::time::timeout(Duration::from_secs(2),async {
|
||||||
|
tx.send(ClipboardRequest::Write(value.into(),reply)).await.map_err(|_|())?;
|
||||||
|
rx.await.map_err(|_|())?
|
||||||
|
}).await.map_err(|_|())?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl CommandBackend {
|
||||||
fn select(wayland:bool,find:impl Fn(&str)->Option<PathBuf>)->Option<Self>{
|
fn select(wayland:bool,find:impl Fn(&str)->Option<PathBuf>)->Option<Self>{
|
||||||
let selected=if wayland{
|
let selected=if wayland{
|
||||||
find("wl-paste").zip(find("wl-copy")).map(|(read,write)|Self{read,write,read_args:vec!["--no-newline","--type","text"],write_args:vec!["--type","text/plain;charset=utf-8"]})
|
find("wl-paste").zip(find("wl-copy")).map(|(read,write)|Self{read,write,read_args:vec!["--no-newline","--type","text"],write_args:vec!["--type","text/plain;charset=utf-8"]})
|
||||||
@@ -45,7 +86,6 @@ impl Backend {
|
|||||||
tokio::time::timeout(Duration::from_secs(2),operation).await.map_err(|_|())?
|
tokio::time::timeout(Duration::from_secs(2),operation).await.map_err(|_|())?
|
||||||
}
|
}
|
||||||
async fn write(&self,value:&str)->Result<(),()>{
|
async fn write(&self,value:&str)->Result<(),()>{
|
||||||
if value.len()>MAX_TEXT{return Err(())}
|
|
||||||
let mut child=tokio::process::Command::new(&self.write).args(&self.write_args).stdin(Stdio::piped()).stdout(Stdio::null()).stderr(Stdio::null()).kill_on_drop(true).spawn().map_err(|_|())?;
|
let mut child=tokio::process::Command::new(&self.write).args(&self.write_args).stdin(Stdio::piped()).stdout(Stdio::null()).stderr(Stdio::null()).kill_on_drop(true).spawn().map_err(|_|())?;
|
||||||
let operation=async{
|
let operation=async{
|
||||||
let mut stdin=child.stdin.take().ok_or(())?;stdin.write_all(value.as_bytes()).await.map_err(|_|())?;drop(stdin);
|
let mut stdin=child.stdin.take().ok_or(())?;stdin.write_all(value.as_bytes()).await.map_err(|_|())?;drop(stdin);
|
||||||
@@ -55,18 +95,30 @@ impl Backend {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct SyncState {last:Option<String>,assembly:text::Assembly,rich:rich::State,seq:u64}
|
// target_mac: Some(true) official Mac host, Some(false) Windows-type host (official or this project), None unknown.
|
||||||
|
struct SyncState {last:Option<String>,assembly:text::Assembly,rich:rich::State,seq:u64,target_mac:Option<bool>}
|
||||||
impl SyncState {
|
impl SyncState {
|
||||||
async fn apply(&mut self,backend:&Backend,value:String)->Result<(),()>{
|
async fn apply(&mut self,backend:&Backend,value:String)->Result<(),()>{
|
||||||
backend.write(&value).await?;
|
if backend.write(&value).await? {
|
||||||
log::info!("Viewer remote clipboard applied: bytes={}",value.len());
|
log::info!("Viewer remote clipboard applied: bytes={}",value.len());
|
||||||
self.last=Some(value);Ok(())
|
self.last=Some(value);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
fn next(&mut self)->u64{self.seq=self.seq.wrapping_add(1).max(100);self.seq}
|
fn next(&mut self)->u64{self.seq=self.seq.wrapping_add(1).max(100);self.seq}
|
||||||
fn local(&mut self,value:String)->Result<Option<Vec<u8>>,&'static str>{
|
/// Two requests per change: the rich format-list offer (this project's host asks for the data) and
|
||||||
|
/// RpcRequest case 10 ClipboardTextChangeRequest carrying the text, which the official Windows host applies
|
||||||
|
/// (handle_text_change_request -> notifyReceiveText); it only acknowledges the rich offer. Each host
|
||||||
|
/// ignores the other form.
|
||||||
|
fn local(&mut self,value:String)->Result<Option<Vec<Vec<u8>>>,&'static str>{
|
||||||
if self.last.as_ref()==Some(&value){return Ok(None)}
|
if self.last.as_ref()==Some(&value){return Ok(None)}
|
||||||
let id=self.next();let payload=self.rich.offer(id,value.clone())?;
|
let id=self.next();let payload=self.rich.offer_any_host(id,value.clone())?;
|
||||||
self.last=Some(value);Ok(Some(wire::envelope(self.next(),21,&payload)))
|
let rich=wire::envelope(self.next(),21,&payload);
|
||||||
|
let text=wire::envelope(self.next(),21,&crate::clipboard_protocol::text_request(self.next(),&value));
|
||||||
|
self.last=Some(value);
|
||||||
|
// Live-verified: the official Windows host writes case-10 text, then a rich format list replaces it with a
|
||||||
|
// placeholder it never asks us to render (paste is empty). The Mac host only takes the rich offer.
|
||||||
|
Ok(Some(match self.target_mac {Some(true)=>vec![rich],Some(false)=>vec![text],None=>vec![rich,text]}))
|
||||||
}
|
}
|
||||||
fn receive(&mut self,data:&[u8])->Result<(Vec<Vec<u8>>,Option<String>),&'static str>{
|
fn receive(&mut self,data:&[u8])->Result<(Vec<Vec<u8>>,Option<String>),&'static str>{
|
||||||
if data.len()>MAX_TEXT*2+1024{return Err("clipboard message exceeds limit")}
|
if data.len()>MAX_TEXT*2+1024{return Err("clipboard message exceeds limit")}
|
||||||
@@ -85,29 +137,42 @@ impl SyncState {
|
|||||||
value=Some(text);replies.push(wire::envelope(self.next(),22,&text::text_response(&header)));
|
value=Some(text);replies.push(wire::envelope(self.next(),22,&text::text_response(&header)));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
22=>if let Some(text)=self.rich.response(payload)?{value=Some(text)},
|
22=>{
|
||||||
|
// TextChangeResponse (case 6 {1 err}, CB_SUCCEEDED=1) answers our case 10 request: status only.
|
||||||
|
if let Some(status)=decode_wire(payload).ok().and_then(|f|f.into_iter().find_map(|x|match (x.number,x.value){(6,WireValue::Bytes(b))=>Some(b.to_vec()),_=>None})){
|
||||||
|
let err=decode_wire(&status).ok().and_then(|f|f.into_iter().find_map(|x|match (x.number,x.value){(1,WireValue::Varint(v))=>Some(v),_=>None})).unwrap_or(0);
|
||||||
|
log::info!("Viewer clipboard text change acknowledged: err={err}");
|
||||||
|
}
|
||||||
|
if let Some(text)=self.rich.response(payload)?{value=Some(text)}
|
||||||
|
},
|
||||||
_=>{},
|
_=>{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if value.as_ref().is_some_and(|text|text.contains('\0')){return Err("clipboard text contains NUL")}
|
||||||
Ok((replies,value))
|
Ok((replies,value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async fn send(channel:&RTCDataChannel,message:Vec<u8>)->Result<(),()>{
|
async fn send(channel:&RTCDataChannel,message:Vec<u8>)->Result<(),()>{
|
||||||
tokio::time::timeout(Duration::from_secs(2),channel.send(&bytes::Bytes::from(message))).await.map_err(|_|())?.map_err(|_|())?;Ok(())
|
tokio::time::timeout(Duration::from_secs(2),channel.send(&bytes::Bytes::from(message))).await.map_err(|_|())?.map_err(|_|())?;Ok(())
|
||||||
}
|
}
|
||||||
pub fn available()->bool{Backend::detect().is_some()}
|
pub struct Bridge {backend:Option<Backend>,pub enabled:bool,tx:Option<mpsc::Sender<Vec<u8>>>,task:Option<tokio::task::JoinHandle<()>>,target_mac:Option<bool>}
|
||||||
pub struct Bridge {backend:Option<Backend>,pub enabled:bool,tx:Option<mpsc::Sender<Vec<u8>>>,task:Option<tokio::task::JoinHandle<()>>}
|
|
||||||
impl Bridge {
|
impl Bridge {
|
||||||
pub fn new(desktop:bool)->Self{
|
pub fn new(desktop:bool)->Self{
|
||||||
let backend=if desktop{Backend::detect()}else{None};let enabled=backend.is_some();
|
let backend=if desktop{Backend::detect()}else{None};let enabled=backend.is_some();
|
||||||
Self{backend,enabled,tx:None,task:None}
|
Self{backend,enabled,tx:None,task:None,target_mac:None}
|
||||||
}
|
}
|
||||||
|
#[cfg(feature="viewer-sdl")]
|
||||||
|
pub fn sdl(tx:mpsc::Sender<ClipboardRequest>)->Self{
|
||||||
|
let backend=(std::env::var("UU_VIEWER_CLIPBOARD").as_deref()!=Ok("0")).then_some(Backend::Sdl(tx));
|
||||||
|
Self{enabled:backend.is_some(),backend,tx:None,task:None,target_mac:None}
|
||||||
|
}
|
||||||
|
pub fn set_target_mac(&mut self,mac:Option<bool>){self.target_mac=mac;}
|
||||||
pub fn start(&mut self,channel:&Arc<RTCDataChannel>,control:&Arc<RTCDataChannel>,observed:Arc<Observed>){
|
pub fn start(&mut self,channel:&Arc<RTCDataChannel>,control:&Arc<RTCDataChannel>,observed:Arc<Observed>){
|
||||||
let Some(backend)=self.backend.take() else{return};
|
let Some(backend)=self.backend.take() else{return};let target_mac=self.target_mac;
|
||||||
let (tx,mut rx)=mpsc::channel::<Vec<u8>>(16);self.tx=Some(tx);
|
let (tx,mut rx)=mpsc::channel::<Vec<u8>>(16);self.tx=Some(tx);
|
||||||
let channel=Arc::downgrade(channel);let control=Arc::downgrade(control);
|
let channel=Arc::downgrade(channel);let control=Arc::downgrade(control);
|
||||||
self.task=Some(tokio::spawn(async move{
|
self.task=Some(tokio::spawn(async move{
|
||||||
let mut state=SyncState::default();let mut tick=tokio::time::interval(POLL);
|
let mut state=SyncState{target_mac,..SyncState::default()};let mut tick=tokio::time::interval(POLL);
|
||||||
let mut retries=0;let mut last_probe=std::time::Instant::now();
|
let mut retries=0;let mut last_probe=std::time::Instant::now();
|
||||||
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||||
loop {
|
loop {
|
||||||
@@ -143,7 +208,7 @@ impl Bridge {
|
|||||||
if channel.ready_state()!=webrtc::data_channel::data_channel_state::RTCDataChannelState::Open{continue}
|
if channel.ready_state()!=webrtc::data_channel::data_channel_state::RTCDataChannelState::Open{continue}
|
||||||
match backend.read().await {
|
match backend.read().await {
|
||||||
Ok(Some(value))=>{let length=value.len();match state.local(value){
|
Ok(Some(value))=>{let length=value.len();match state.local(value){
|
||||||
Ok(Some(message))=>{if send(&channel,message).await.is_err(){break}log::info!("Viewer local clipboard offered: bytes={length}");},
|
Ok(Some(messages))=>{let mut failed=false;for m in messages{if send(&channel,m).await.is_err(){failed=true;break}}if failed{break}log::info!("Viewer local clipboard offered: bytes={length}");},
|
||||||
Ok(None)=>{},Err(_)=>{},
|
Ok(None)=>{},Err(_)=>{},
|
||||||
}},
|
}},
|
||||||
Ok(None)=>{},Err(())=>{warn();break},
|
Ok(None)=>{},Err(())=>{warn();break},
|
||||||
@@ -160,8 +225,22 @@ impl Drop for Bridge{fn drop(&mut self){self.stop();}}
|
|||||||
|
|
||||||
#[cfg(test)] mod tests {
|
#[cfg(test)] mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
#[cfg(feature="viewer-sdl")]
|
||||||
|
#[tokio::test] async fn sdl_backend_requests_and_invalid_text(){
|
||||||
|
let(tx,mut rx)=mpsc::channel(8);let backend=Backend::Sdl(tx);
|
||||||
|
let ui=tokio::spawn(async move{
|
||||||
|
let Some(ClipboardRequest::Write(text,reply))=rx.recv().await else{panic!()};
|
||||||
|
assert_eq!(text,"text\n");reply.send(Ok(true)).unwrap();
|
||||||
|
let Some(ClipboardRequest::Read(reply))=rx.recv().await else{panic!()};
|
||||||
|
reply.send(Ok(Some("text\n".into()))).unwrap();
|
||||||
|
});
|
||||||
|
assert!(backend.write("nul\0text").await.is_err());
|
||||||
|
assert!(backend.write(&"a".repeat(MAX_TEXT+1)).await.is_err());
|
||||||
|
backend.write("text\n").await.unwrap();assert_eq!(backend.read().await.unwrap().as_deref(),Some("text\n"));
|
||||||
|
ui.await.unwrap();assert!(backend.read().await.is_err());
|
||||||
|
}
|
||||||
#[test] fn backend_priority_requires_a_complete_wayland_pair_and_falls_back(){
|
#[test] fn backend_priority_requires_a_complete_wayland_pair_and_falls_back(){
|
||||||
let select=|wayland,names:&[&str]|Backend::select(wayland,|n|names.contains(&n).then(||PathBuf::from(n)));
|
let select=|wayland,names:&[&str]|CommandBackend::select(wayland,|n|names.contains(&n).then(||PathBuf::from(n)));
|
||||||
let all=["wl-copy","wl-paste","xclip","xsel"];
|
let all=["wl-copy","wl-paste","xclip","xsel"];
|
||||||
assert_eq!(select(true,&all).unwrap().write,PathBuf::from("wl-copy"));
|
assert_eq!(select(true,&all).unwrap().write,PathBuf::from("wl-copy"));
|
||||||
assert_eq!(select(false,&all).unwrap().write,PathBuf::from("xclip"));
|
assert_eq!(select(false,&all).unwrap().write,PathBuf::from("xclip"));
|
||||||
@@ -169,6 +248,17 @@ impl Drop for Bridge{fn drop(&mut self){self.stop();}}
|
|||||||
assert_eq!(select(true,&["xsel"]).unwrap().write,PathBuf::from("xsel"));
|
assert_eq!(select(true,&["xsel"]).unwrap().write,PathBuf::from("xsel"));
|
||||||
assert!(select(true,&[]).is_none());
|
assert!(select(true,&[]).is_none());
|
||||||
}
|
}
|
||||||
|
#[cfg(feature="viewer-sdl")]
|
||||||
|
#[tokio::test] async fn unfocused_write_does_not_mark_remote_text_as_applied(){
|
||||||
|
let(tx,mut rx)=mpsc::channel(8);let backend=Backend::Sdl(tx);
|
||||||
|
let ui=tokio::spawn(async move{
|
||||||
|
let Some(ClipboardRequest::Write(_,reply))=rx.recv().await else{panic!()};
|
||||||
|
reply.send(Ok(false)).unwrap();
|
||||||
|
});
|
||||||
|
let mut state=SyncState::default();state.last=Some("local".into());
|
||||||
|
state.apply(&backend,"remote".into()).await.unwrap();
|
||||||
|
assert_eq!(state.last.as_deref(),Some("local"));ui.await.unwrap();
|
||||||
|
}
|
||||||
#[test] fn limits_match_host_and_unchanged_or_remote_text_is_not_reoffered(){
|
#[test] fn limits_match_host_and_unchanged_or_remote_text_is_not_reoffered(){
|
||||||
assert!(include_str!("../clipboard.rs").contains("pub const MAX_TEXT: usize = 30 * 1024;"));
|
assert!(include_str!("../clipboard.rs").contains("pub const MAX_TEXT: usize = 30 * 1024;"));
|
||||||
assert!(POLL>=Duration::from_millis(500));
|
assert!(POLL>=Duration::from_millis(500));
|
||||||
@@ -199,11 +289,20 @@ impl Drop for Bridge{fn drop(&mut self){self.stop();}}
|
|||||||
assert!(state.receive(&wire::envelope(1,17,&[18,1,255])).is_err());
|
assert!(state.receive(&wire::envelope(1,17,&[18,1,255])).is_err());
|
||||||
assert!(state.receive(&wire::envelope(1,17,&text::encode(1,&"a".repeat(MAX_TEXT+1),false))).is_err());
|
assert!(state.receive(&wire::envelope(1,17,&text::encode(1,&"a".repeat(MAX_TEXT+1),false))).is_err());
|
||||||
}
|
}
|
||||||
|
#[test] fn rejected_nul_does_not_stop_later_valid_updates(){
|
||||||
|
let mut state=SyncState::default();
|
||||||
|
for kind in [17,21]{
|
||||||
|
let payload=if kind==17{text::encode(1,"nul\0text",false)}else{text::text_request(73,"nul\0text")};
|
||||||
|
assert!(state.receive(&wire::envelope(1,kind,&payload)).is_err());
|
||||||
|
let(_,value)=state.receive(&wire::envelope(2,17,&text::encode(2,"valid",false))).unwrap();
|
||||||
|
assert_eq!(value.as_deref(),Some("valid"));
|
||||||
|
}
|
||||||
|
}
|
||||||
// Match webrtc.rs: CONTROL and TEXT have separate rich States, and only TEXT has a desktop sink.
|
// Match webrtc.rs: CONTROL and TEXT have separate rich States, and only TEXT has a desktop sink.
|
||||||
fn native_host_exchange(outbound:usize,reverse:bool)->(Option<String>,bool){
|
fn native_host_exchange(outbound:usize,reverse:bool)->(Option<String>,bool){
|
||||||
let mut viewer=SyncState::default();let mut host=[rich::State::default(),rich::State::default()];let mut id=1000;
|
let mut viewer=SyncState::default();let mut host=[rich::State::default(),rich::State::default()];let mut id=1000;
|
||||||
let value="clipboard-中文🙂\n".to_owned();
|
let value="clipboard-中文🙂\n".to_owned();
|
||||||
let offer=if reverse{wire::envelope(1,21,&host[1].offer(1,value.clone()).unwrap())}else{viewer.local(value.clone()).unwrap().unwrap()};
|
let offer=if reverse{wire::envelope(1,21,&host[1].offer(1,value.clone()).unwrap())}else{viewer.local(value.clone()).unwrap().unwrap().remove(0)};
|
||||||
let mut queue=std::collections::VecDeque::from([(reverse,if reverse{1}else{outbound},offer)]);
|
let mut queue=std::collections::VecDeque::from([(reverse,if reverse{1}else{outbound},offer)]);
|
||||||
let mut received=None;let mut unavailable=false;let mut steps=0;
|
let mut received=None;let mut unavailable=false;let mut steps=0;
|
||||||
while let Some((to_viewer,channel,packet))=queue.pop_front(){
|
while let Some((to_viewer,channel,packet))=queue.pop_front(){
|
||||||
@@ -231,6 +330,22 @@ impl Drop for Bridge{fn drop(&mut self){self.stop();}}
|
|||||||
if reverse && received.is_some(){viewer.last=Some(value.clone());assert!(viewer.local(value).unwrap().is_none());}
|
if reverse && received.is_some(){viewer.last=Some(value.clone());assert!(viewer.local(value).unwrap().is_none());}
|
||||||
(received,unavailable)
|
(received,unavailable)
|
||||||
}
|
}
|
||||||
|
#[test] fn local_change_follows_target_platform(){
|
||||||
|
for (mac,kinds) in [(Some(true),vec![9]),(Some(false),vec![10]),(None,vec![9,10])]{
|
||||||
|
let mut viewer=SyncState{target_mac:mac,..SyncState::default()};
|
||||||
|
let cases:Vec<u32>=viewer.local("text".into()).unwrap().unwrap().iter().map(|m|{
|
||||||
|
let rpc=decode_wire(m).unwrap().into_iter().find_map(|f|match (f.number,f.value){(21,WireValue::Bytes(b))=>Some(b.to_vec()),_=>None}).unwrap();
|
||||||
|
decode_wire(&rpc).unwrap().iter().map(|f|f.number).find(|n|*n!=1).unwrap()
|
||||||
|
}).collect();
|
||||||
|
assert_eq!(cases,kinds,"{mac:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[test] fn local_change_also_sends_windows_text_change_request(){
|
||||||
|
let mut viewer=SyncState::default();
|
||||||
|
let messages=viewer.local("windows 中文".into()).unwrap().unwrap();assert_eq!(messages.len(),2);
|
||||||
|
let rpc=decode_wire(&messages[1]).unwrap().into_iter().find_map(|f|match (f.number,f.value){(21,WireValue::Bytes(b))=>Some(b.to_vec()),_=>None}).unwrap();
|
||||||
|
let (_,text)=crate::clipboard_protocol::read_text_request(&rpc).unwrap().unwrap();assert_eq!(text,"windows 中文");
|
||||||
|
}
|
||||||
#[test] fn control_clipboard_reproduces_native_host_failures(){
|
#[test] fn control_clipboard_reproduces_native_host_failures(){
|
||||||
assert_eq!(native_host_exchange(0,false),(None,false)); // Complete transfer, no CONTROL desktop sink.
|
assert_eq!(native_host_exchange(0,false),(None,false)); // Complete transfer, no CONTROL desktop sink.
|
||||||
assert_eq!(native_host_exchange(0,true),(None,true)); // TEXT offer, empty CONTROL provider.
|
assert_eq!(native_host_exchange(0,true),(None,true)); // TEXT offer, empty CONTROL provider.
|
||||||
@@ -250,9 +365,12 @@ impl Drop for Bridge{fn drop(&mut self){self.stop();}}
|
|||||||
assert!(!Bridge::new(false).enabled);assert!(!Bridge::new(true).enabled);
|
assert!(!Bridge::new(false).enabled);assert!(!Bridge::new(true).enabled);
|
||||||
let mut shape:wire::Shape=serde_json::from_str(include_str!("../../tests/fixtures/viewer/shape.json")).unwrap();
|
let mut shape:wire::Shape=serde_json::from_str(include_str!("../../tests/fixtures/viewer/shape.json")).unwrap();
|
||||||
shape.feature_flag=vec![[8,99]];
|
shape.feature_flag=vec![[8,99]];
|
||||||
let options=shape.connect_options("synthetic-device").unwrap();
|
let options=shape.connect_options("synthetic-device",false).unwrap();
|
||||||
let WireValue::Bytes(flags)=decode_wire(&options).unwrap().into_iter().find(|f|f.number==11).unwrap().value else{panic!()};
|
let WireValue::Bytes(flags)=decode_wire(&options).unwrap().into_iter().find(|f|f.number==11).unwrap().value else{panic!()};
|
||||||
assert!(!decode_wire(flags).unwrap().iter().any(|f|f.number==8));
|
assert!(!decode_wire(flags).unwrap().iter().any(|f|f.number==8));
|
||||||
|
let options=shape.connect_options("synthetic-device",true).unwrap();
|
||||||
|
let WireValue::Bytes(flags)=decode_wire(&options).unwrap().into_iter().find(|f|f.number==11).unwrap().value else{panic!()};
|
||||||
|
assert!(decode_wire(flags).unwrap().iter().any(|f|f.number==8 && f.value==WireValue::Varint(3)));
|
||||||
}
|
}
|
||||||
#[tokio::test] async fn backend_uses_stdin_preserves_text_and_bounds_reads(){
|
#[tokio::test] async fn backend_uses_stdin_preserves_text_and_bounds_reads(){
|
||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::PermissionsExt;
|
||||||
@@ -260,14 +378,15 @@ impl Drop for Bridge{fn drop(&mut self){self.stop();}}
|
|||||||
let script=root.join("mock");
|
let script=root.join("mock");
|
||||||
std::fs::write(&script,b"#!/bin/sh\ncd -- \"${0%/*}\" || exit 1\ncase \"$1\" in read) cat data;; write) cat > data;; esac\n").unwrap();
|
std::fs::write(&script,b"#!/bin/sh\ncd -- \"${0%/*}\" || exit 1\ncase \"$1\" in read) cat data;; write) cat > data;; esac\n").unwrap();
|
||||||
std::fs::set_permissions(&script,std::fs::Permissions::from_mode(0o700)).unwrap();
|
std::fs::set_permissions(&script,std::fs::Permissions::from_mode(0o700)).unwrap();
|
||||||
let backend=Backend{read:script.clone(),write:script,read_args:vec!["read"],write_args:vec!["write"]};
|
let command=CommandBackend{read:script.clone(),write:script,read_args:vec!["read"],write_args:vec!["write"]};
|
||||||
|
let backend=Backend::Command(command.clone());
|
||||||
let mut state=SyncState::default();
|
let mut state=SyncState::default();
|
||||||
for value in ["中文🙂\n", "", "literal $(echo secret) `echo secret`"]{
|
for value in ["中文🙂\n", "", "literal $(echo secret) `echo secret`"]{
|
||||||
state.apply(&backend,value.into()).await.unwrap();
|
state.apply(&backend,value.into()).await.unwrap();
|
||||||
let read=backend.read().await.unwrap().unwrap();assert_eq!(read,value);
|
let read=backend.read().await.unwrap().unwrap();assert_eq!(read,value);
|
||||||
assert!(state.local(read).unwrap().is_none());
|
assert!(state.local(read).unwrap().is_none());
|
||||||
}
|
}
|
||||||
let broken=Backend{write:root.join("missing"),..backend.clone()};
|
let broken=Backend::Command(CommandBackend{write:root.join("missing"),..command});
|
||||||
let previous=state.last.clone();assert!(state.apply(&broken,"failed".into()).await.is_err());assert_eq!(state.last,previous);
|
let previous=state.last.clone();assert!(state.apply(&broken,"failed".into()).await.is_err());assert_eq!(state.last,previous);
|
||||||
std::fs::write(root.join("data"),vec![b'a';MAX_TEXT+1]).unwrap();assert!(backend.read().await.unwrap().is_none());
|
std::fs::write(root.join("data"),vec![b'a';MAX_TEXT+1]).unwrap();assert!(backend.read().await.unwrap().is_none());
|
||||||
std::fs::write(root.join("data"),[255]).unwrap();assert!(backend.read().await.unwrap().is_none());
|
std::fs::write(root.join("data"),[255]).unwrap();assert!(backend.read().await.unwrap().is_none());
|
||||||
|
|||||||
+14
-7
@@ -1,29 +1,36 @@
|
|||||||
//! Uses the same optional FFmpeg dependency as the host inproc-av feature.
|
//! Uses the same optional FFmpeg dependency as the host inproc-av feature.
|
||||||
use super::desktop::{Decoder,Frame};
|
use super::desktop::{Decoder,Frame};
|
||||||
use ffmpeg_next as av;
|
use ffmpeg_next as av;
|
||||||
pub struct Ffmpeg { decoder:av::decoder::Video }
|
use super::media::Codec;
|
||||||
|
pub struct Ffmpeg { decoder:av::decoder::Video, codec:Codec }
|
||||||
impl Ffmpeg {
|
impl Ffmpeg {
|
||||||
pub fn new()->Result<Self,String> {
|
pub fn new()->Result<Self,String> {
|
||||||
|
Self::with_codec(Codec::H264)
|
||||||
|
}
|
||||||
|
fn with_codec(kind:Codec)->Result<Self,String>{
|
||||||
av::init().map_err(|_|"FFmpeg initialization failed")?;
|
av::init().map_err(|_|"FFmpeg initialization failed")?;
|
||||||
// Library diagnostics can contain bitstream data; suppress them globally in this viewer process.
|
// Library diagnostics can contain bitstream data; suppress them globally in this viewer process.
|
||||||
av::log::set_level(av::log::Level::Quiet);
|
av::log::set_level(av::log::Level::Quiet);
|
||||||
let codec=av::decoder::find(av::codec::Id::H264).ok_or("H264 decoder unavailable")?;
|
let codec=av::decoder::find(match kind {Codec::H264=>av::codec::Id::H264,Codec::H265=>av::codec::Id::HEVC}).ok_or("Video decoder unavailable")?;
|
||||||
let decoder=av::codec::context::Context::new().decoder().open_as(codec).and_then(|d|d.video()).map_err(|_|"H264 decoder initialization failed")?;
|
let decoder=av::codec::context::Context::new().decoder().open_as(codec).and_then(|d|d.video()).map_err(|_|"Video decoder initialization failed")?;
|
||||||
Ok(Self{decoder})
|
Ok(Self{decoder,codec:kind})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Decoder for Ffmpeg {
|
impl Decoder for Ffmpeg {
|
||||||
|
fn set_codec(&mut self,codec:Codec)->Result<(),String>{
|
||||||
|
if self.codec!=codec {*self=Self::with_codec(codec)?;}Ok(())
|
||||||
|
}
|
||||||
fn reset(&mut self){self.decoder.flush();}
|
fn reset(&mut self){self.decoder.flush();}
|
||||||
fn decode(&mut self,au:&[u8])->Result<Vec<Frame>,String>{
|
fn decode(&mut self,au:&[u8])->Result<Vec<Frame>,String>{
|
||||||
self.decoder.send_packet(&av::Packet::copy(au)).map_err(|_|"H264 packet rejected")?;
|
self.decoder.send_packet(&av::Packet::copy(au)).map_err(|_|"Video packet rejected")?;
|
||||||
let mut frames=Vec::new();let mut decoded=av::frame::Video::empty();
|
let mut frames=Vec::new();let mut decoded=av::frame::Video::empty();
|
||||||
loop {
|
loop {
|
||||||
match self.decoder.receive_frame(&mut decoded) {
|
match self.decoder.receive_frame(&mut decoded) {
|
||||||
Ok(())=>{},
|
Ok(())=>{},
|
||||||
Err(av::Error::Other{errno}) if errno==libc::EAGAIN=>break,
|
Err(av::Error::Other{errno}) if errno==libc::EAGAIN=>break,
|
||||||
Err(_)=>return Err("H264 decode failed".into()),
|
Err(_)=>return Err("Video decode failed".into()),
|
||||||
}
|
}
|
||||||
if decoded.is_corrupt(){return Err("Corrupt H264 frame".into())}
|
if decoded.is_corrupt(){return Err("Corrupt video frame".into())}
|
||||||
let (w,h)=(decoded.width(),decoded.height());
|
let (w,h)=(decoded.width(),decoded.height());
|
||||||
if w==0 || h==0 || w>8192 || h>8192 {return Err("Decoded dimensions exceed limit".into())}
|
if w==0 || h==0 || w>8192 || h>8192 {return Err("Decoded dimensions exceed limit".into())}
|
||||||
let mut scaler=av::software::scaling::context::Context::get(decoded.format(),w,h,av::format::Pixel::RGB24,w,h,av::software::scaling::flag::Flags::BILINEAR).map_err(|_|"Frame conversion unavailable")?;
|
let mut scaler=av::software::scaling::context::Context::get(decoded.format(),w,h,av::format::Pixel::RGB24,w,h,av::software::scaling::flag::Flags::BILINEAR).map_err(|_|"Frame conversion unavailable")?;
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
//! H.264 decoding through an external `ffmpeg` process, like the host's encoder pipeline.
|
//! H.264/H.265 decoding through an external `ffmpeg` process, like the host's encoder pipeline.
|
||||||
//! Needs only the ffmpeg CLI (any recent version), not FFmpeg development libraries.
|
//! Needs only the ffmpeg CLI, not FFmpeg development libraries.
|
||||||
//! Output is scaled into a fixed letterboxed size so every frame has a known byte length.
|
//! Output is scaled into a fixed letterboxed size so every frame has a known byte length.
|
||||||
use super::desktop::{Decoder,Frame};
|
use super::desktop::{Decoder,Frame};
|
||||||
|
use super::media::Codec;
|
||||||
use std::io::{Read,Write};
|
use std::io::{Read,Write};
|
||||||
use std::process::{Child,ChildStdin,Command,Stdio};
|
use std::process::{Child,ChildStdin,Command,Stdio};
|
||||||
use std::sync::mpsc::{Receiver,TryRecvError};
|
use std::sync::mpsc::{Receiver,TryRecvError};
|
||||||
|
|
||||||
pub struct Cli { child:Child, input:ChildStdin, frames:Receiver<Vec<u8>>, width:u32, height:u32 }
|
pub struct Cli { child:Child, input:Option<ChildStdin>, codec:Codec, frames:Receiver<Vec<u8>>, width:u32, height:u32 }
|
||||||
|
|
||||||
impl Cli {
|
impl Cli {
|
||||||
/// `UU_VIEWER_SIZE=WxH` picks the decoded size (default 1920x1080); `UU_VIEWER_FFMPEG` the binary.
|
/// `UU_VIEWER_SIZE=WxH` picks the decoded size (default 1920x1080); `UU_VIEWER_FFMPEG` the binary.
|
||||||
@@ -15,15 +16,19 @@ impl Cli {
|
|||||||
.and_then(|v|{let (w,h)=v.split_once('x')?;Some((w.parse().ok()?,h.parse().ok()?))})
|
.and_then(|v|{let (w,h)=v.split_once('x')?;Some((w.parse().ok()?,h.parse().ok()?))})
|
||||||
.filter(|&(w,h):&(u32,u32)|(16..=8192).contains(&w)&&(16..=8192).contains(&h)&&w%2==0&&h%2==0)
|
.filter(|&(w,h):&(u32,u32)|(16..=8192).contains(&w)&&(16..=8192).contains(&h)&&w%2==0&&h%2==0)
|
||||||
.unwrap_or((1920,1080));
|
.unwrap_or((1920,1080));
|
||||||
|
Self::with_codec(Codec::H264,width,height)
|
||||||
|
}
|
||||||
|
fn with_codec(codec:Codec,width:u32,height:u32)->Result<Self,String> {
|
||||||
let ffmpeg=std::env::var("UU_VIEWER_FFMPEG").unwrap_or_else(|_|"ffmpeg".into());
|
let ffmpeg=std::env::var("UU_VIEWER_FFMPEG").unwrap_or_else(|_|"ffmpeg".into());
|
||||||
let filter=format!("scale={width}:{height}:force_original_aspect_ratio=decrease,pad={width}:{height}:(ow-iw)/2:(oh-ih)/2");
|
let filter=format!("scale={width}:{height}:force_original_aspect_ratio=decrease,pad={width}:{height}:(ow-iw)/2:(oh-ih)/2");
|
||||||
let mut child=Command::new(ffmpeg)
|
let passthrough=passthrough_args(&ffmpeg);
|
||||||
|
let mut child=Command::new(&ffmpeg)
|
||||||
.args(["-hide_banner","-loglevel","error","-nostdin","-threads","1","-probesize","32","-analyzeduration","0",
|
.args(["-hide_banner","-loglevel","error","-nostdin","-threads","1","-probesize","32","-analyzeduration","0",
|
||||||
"-fflags","nobuffer","-flags","low_delay","-f","h264","-i","pipe:0","-an","-vf",&filter,
|
"-fflags","nobuffer","-flags","low_delay","-f",codec.demuxer(),"-i","pipe:0","-an","-vf",&filter,
|
||||||
"-pix_fmt","rgb24",
|
"-pix_fmt","rgb24",
|
||||||
// Raw H.264 carries no timestamps; constant-rate output dropped almost every frame of
|
// Raw H.264/H.265 carries no timestamps; constant-rate output dropped almost every frame of
|
||||||
// the official Mac host's stream (270 → 2). Pass frames through unchanged.
|
// the official Mac host's stream (270 → 2). Pass frames through unchanged.
|
||||||
"-fps_mode","passthrough","-f","rawvideo","pipe:1"])
|
passthrough.0,passthrough.1,"-f","rawvideo","pipe:1"])
|
||||||
.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::null())
|
.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::null())
|
||||||
.spawn().map_err(|e|format!("Start ffmpeg decoder: {e}"))?;
|
.spawn().map_err(|e|format!("Start ffmpeg decoder: {e}"))?;
|
||||||
let input=child.stdin.take().ok_or("ffmpeg stdin missing")?;
|
let input=child.stdin.take().ok_or("ffmpeg stdin missing")?;
|
||||||
@@ -38,14 +43,35 @@ impl Cli {
|
|||||||
match tx.try_send(frame) { Ok(())|Err(std::sync::mpsc::TrySendError::Full(_))=>{}, Err(_)=>break }
|
match tx.try_send(frame) { Ok(())|Err(std::sync::mpsc::TrySendError::Full(_))=>{}, Err(_)=>break }
|
||||||
}
|
}
|
||||||
}).map_err(|e|format!("Decoder reader: {e}"))?;
|
}).map_err(|e|format!("Decoder reader: {e}"))?;
|
||||||
Ok(Self{child,input,frames,width,height})
|
Ok(Self{child,input:Some(input),codec,frames,width,height})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// FFmpeg ≥ 5.1 has `-fps_mode`; FFmpeg 8+ removed `-vsync`, and Ubuntu 22.04's 4.4 lacks `-fps_mode`.
|
||||||
|
/// Probe once per binary with a one-frame null run instead of parsing version strings.
|
||||||
|
fn passthrough_args(ffmpeg:&str)->(&'static str,&'static str) {
|
||||||
|
static CACHE:std::sync::Mutex<Vec<(String,bool)>>=std::sync::Mutex::new(Vec::new());
|
||||||
|
let mut cache=CACHE.lock().unwrap_or_else(|e|e.into_inner());
|
||||||
|
let fps_mode=match cache.iter().find(|(name,_)|name==ffmpeg) {
|
||||||
|
Some(&(_,known))=>known,
|
||||||
|
None=>{
|
||||||
|
let ok=Command::new(ffmpeg).args(["-hide_banner","-loglevel","quiet","-nostdin","-f","lavfi","-i","color=s=16x16:d=0.04",
|
||||||
|
"-fps_mode","passthrough","-f","null","-"]).stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null())
|
||||||
|
.status().is_ok_and(|s|s.success());
|
||||||
|
cache.push((ffmpeg.into(),ok));ok
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if fps_mode {("-fps_mode","passthrough")} else {("-vsync","0")}
|
||||||
|
}
|
||||||
|
|
||||||
impl Decoder for Cli {
|
impl Decoder for Cli {
|
||||||
|
fn set_codec(&mut self,codec:Codec)->Result<(),String>{
|
||||||
|
if self.codec!=codec {*self=Self::with_codec(codec,self.width,self.height)?;}Ok(())
|
||||||
|
}
|
||||||
fn reset(&mut self) {}
|
fn reset(&mut self) {}
|
||||||
fn decode(&mut self,au:&[u8])->Result<Vec<Frame>,String> {
|
fn decode(&mut self,au:&[u8])->Result<Vec<Frame>,String> {
|
||||||
self.input.write_all(au).and_then(|_|self.input.flush()).map_err(|_|"ffmpeg decoder exited")?;
|
let input=self.input.as_mut().ok_or("ffmpeg input closed")?;
|
||||||
|
input.write_all(au).and_then(|_|input.flush()).map_err(|_|"ffmpeg decoder exited")?;
|
||||||
let mut out=Vec::new();
|
let mut out=Vec::new();
|
||||||
loop {
|
loop {
|
||||||
match self.frames.try_recv() {
|
match self.frames.try_recv() {
|
||||||
@@ -59,3 +85,47 @@ impl Decoder for Cli {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for Cli { fn drop(&mut self) { let _=self.child.kill(); let _=self.child.wait(); } }
|
impl Drop for Cli { fn drop(&mut self) { let _=self.child.kill(); let _=self.child.wait(); } }
|
||||||
|
|
||||||
|
#[cfg(test)] mod tests {
|
||||||
|
use super::*;
|
||||||
|
// Six synthetic red IDRs per fixture; no captured desktop or external encoder dependency.
|
||||||
|
#[cfg(unix)] #[test] fn passthrough_follows_ffmpeg_option_support(){
|
||||||
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
let dir=std::env::temp_dir().join(format!("uu-ffprobe-{}",std::process::id()));std::fs::create_dir_all(&dir).unwrap();
|
||||||
|
for (name,script,expected) in [("new","exit 0",("-fps_mode","passthrough")),("old","case \"$*\" in *-fps_mode*) exit 1;; esac",("-vsync","0"))]{
|
||||||
|
let path=dir.join(name);std::fs::write(&path,format!("#!/bin/sh\n{script}\n")).unwrap();
|
||||||
|
std::fs::set_permissions(&path,std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||||
|
assert_eq!(super::passthrough_args(path.to_str().unwrap()),expected);
|
||||||
|
}
|
||||||
|
std::fs::remove_dir_all(dir).unwrap();
|
||||||
|
}
|
||||||
|
#[test] fn ffmpeg_decodes_h264_and_hevc_420_and_444_to_rgb24(){
|
||||||
|
match Command::new("ffmpeg").arg("-version").stdout(Stdio::null()).stderr(Stdio::null()).status(){
|
||||||
|
Err(e) if e.kind()==std::io::ErrorKind::NotFound=>{eprintln!("skip: ffmpeg is not on PATH");return},
|
||||||
|
result=>assert!(result.expect("ffmpeg availability").success()),
|
||||||
|
}
|
||||||
|
let mut cli=Cli::with_codec(Codec::H265,32,32).unwrap();
|
||||||
|
for (codec,data) in [
|
||||||
|
(Codec::H264,&include_bytes!("../../tests/fixtures/viewer/red-420.h264")[..]),
|
||||||
|
(Codec::H265,&include_bytes!("../../tests/fixtures/viewer/red-420.hevc")[..]),
|
||||||
|
(Codec::H264,&include_bytes!("../../tests/fixtures/viewer/red-444.h264")[..]),
|
||||||
|
(Codec::H265,&include_bytes!("../../tests/fixtures/viewer/red-444.hevc")[..]),
|
||||||
|
] {
|
||||||
|
cli.set_codec(codec).unwrap();
|
||||||
|
// Exercise the same streaming process and RGB conversion as SDL, then drain at EOF.
|
||||||
|
let mut frames=cli.decode(data).unwrap();drop(cli.input.take());
|
||||||
|
loop {match cli.frames.recv_timeout(std::time::Duration::from_secs(5)) {
|
||||||
|
Ok(rgb)=>frames.push(Frame{width:32,height:32,rgb}),
|
||||||
|
Err(std::sync::mpsc::RecvTimeoutError::Disconnected)=>break,
|
||||||
|
Err(e)=>panic!("{codec:?} decoder did not finish: {e}"),
|
||||||
|
}}
|
||||||
|
let status=cli.child.wait().unwrap();
|
||||||
|
assert!(status.success(),"{codec:?}: ffmpeg exited with {status}");
|
||||||
|
assert!(!frames.is_empty(),"{codec:?}: no RGB frames");
|
||||||
|
for frame in frames {
|
||||||
|
assert_eq!(frame.rgb.len(),32*32*3);
|
||||||
|
assert!(frame.rgb.chunks_exact(3).all(|p|p[0]>220 && p[1]<30 && p[2]<30),"{codec:?}: RGB conversion");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,19 +1,19 @@
|
|||||||
use super::media::Latest;
|
use super::media::Latest;
|
||||||
use std::sync::{Arc, atomic::{AtomicBool,AtomicU32,Ordering}};
|
use std::sync::{Arc, atomic::{AtomicBool,AtomicU32,Ordering}};
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Desktop { pub units:Latest<Vec<u8>>, pub pli:Arc<AtomicBool>, pub reset:Arc<AtomicBool>, pub ssrc:Arc<AtomicU32> }
|
pub struct Desktop { pub units:Latest<super::media::AccessUnit>, pub pli:Arc<AtomicBool>, pub reset:Arc<AtomicBool>, pub ssrc:Arc<AtomicU32> }
|
||||||
impl Default for Desktop {
|
impl Default for Desktop {
|
||||||
fn default()->Self {Self{units:Latest::new(3),pli:Arc::new(AtomicBool::new(false)),reset:Arc::new(AtomicBool::new(false)),ssrc:Arc::new(AtomicU32::new(0))}}
|
fn default()->Self {Self{units:Latest::new(3),pli:Arc::new(AtomicBool::new(false)),reset:Arc::new(AtomicBool::new(false)),ssrc:Arc::new(AtomicU32::new(0))}}
|
||||||
}
|
}
|
||||||
pub struct Frame { pub width:u32, pub height:u32, pub rgb:Vec<u8> }
|
pub struct Frame { pub width:u32, pub height:u32, pub rgb:Vec<u8> }
|
||||||
pub trait Decoder { fn decode(&mut self, au:&[u8])->Result<Vec<Frame>,String>; fn reset(&mut self); }
|
pub trait Decoder { fn set_codec(&mut self,codec:super::media::Codec)->Result<(),String>; fn decode(&mut self, au:&[u8])->Result<Vec<Frame>,String>; fn reset(&mut self); }
|
||||||
pub trait FrameSink { fn present(&mut self,frame:&Frame)->Result<(),String>; }
|
pub trait FrameSink { fn present(&mut self,frame:&Frame)->Result<(),String>; }
|
||||||
/// Counts only (access units, presented frames, decode errors) for the periodic stats line.
|
/// Counts only (access units, presented frames, decode errors) for the periodic stats line.
|
||||||
pub static STATS:[std::sync::atomic::AtomicU64;3]=[std::sync::atomic::AtomicU64::new(0),std::sync::atomic::AtomicU64::new(0),std::sync::atomic::AtomicU64::new(0)];
|
pub static STATS:[std::sync::atomic::AtomicU64;3]=[std::sync::atomic::AtomicU64::new(0),std::sync::atomic::AtomicU64::new(0),std::sync::atomic::AtomicU64::new(0)];
|
||||||
pub fn display(decoder:&mut dyn Decoder,sink:&mut dyn FrameSink,au:&[u8],pli:&AtomicBool)->Result<(),String> {
|
pub fn display(decoder:&mut dyn Decoder,sink:&mut dyn FrameSink,au:&[u8],codec:super::media::Codec,pli:&AtomicBool)->Result<(),String> {
|
||||||
STATS[0].fetch_add(1,Ordering::Relaxed);
|
STATS[0].fetch_add(1,Ordering::Relaxed);
|
||||||
// Local diagnostic: UU_VIEWER_DUMP_H264=path appends the first 300 access units (owner's own stream).
|
// Local diagnostic: UU_VIEWER_DUMP_H264=path appends the first 300 access units (owner's own stream).
|
||||||
if STATS[0].load(Ordering::Relaxed)<=300 { if let Some(path)=std::env::var_os("UU_VIEWER_DUMP_H264") {
|
if codec==super::media::Codec::H264 && STATS[0].load(Ordering::Relaxed)<=300 { if let Some(path)=std::env::var_os("UU_VIEWER_DUMP_H264") {
|
||||||
use std::io::Write; if let Ok(mut f)=std::fs::OpenOptions::new().create(true).append(true).open(path){let _=f.write_all(au);}
|
use std::io::Write; if let Ok(mut f)=std::fs::OpenOptions::new().create(true).append(true).open(path){let _=f.write_all(au);}
|
||||||
}}
|
}}
|
||||||
match decoder.decode(au) {
|
match decoder.decode(au) {
|
||||||
@@ -29,7 +29,7 @@ pub fn display(decoder:&mut dyn Decoder,sink:&mut dyn FrameSink,au:&[u8],pli:&At
|
|||||||
}
|
}
|
||||||
#[cfg(test)] mod tests {
|
#[cfg(test)] mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
struct Mock(bool);impl Decoder for Mock {fn decode(&mut self,_:&[u8])->Result<Vec<Frame>,String>{if self.0{Err("synthetic".into())}else{Ok(vec![Frame{width:1,height:1,rgb:vec![0;3]}])}} fn reset(&mut self){self.0=false;}}
|
struct Mock(bool);impl Decoder for Mock {fn set_codec(&mut self,_:crate::viewer::media::Codec)->Result<(),String>{Ok(())} fn decode(&mut self,_:&[u8])->Result<Vec<Frame>,String>{if self.0{Err("synthetic".into())}else{Ok(vec![Frame{width:1,height:1,rgb:vec![0;3]}])}} fn reset(&mut self){self.0=false;}}
|
||||||
struct Sink(usize);impl FrameSink for Sink{fn present(&mut self,f:&Frame)->Result<(),String>{assert_eq!(f.rgb.len(),3);self.0+=1;Ok(())}}
|
struct Sink(usize);impl FrameSink for Sink{fn present(&mut self,f:&Frame)->Result<(),String>{assert_eq!(f.rgb.len(),3);self.0+=1;Ok(())}}
|
||||||
#[test] fn decode_error_requests_pli_and_recovers(){let mut d=Mock(true);let mut s=Sink(0);let p=AtomicBool::new(false);display(&mut d,&mut s,&[],&p).unwrap();assert!(p.load(Ordering::Acquire));display(&mut d,&mut s,&[],&p).unwrap();assert_eq!(s.0,1);}
|
#[test] fn decode_error_requests_pli_and_recovers(){let mut d=Mock(true);let mut s=Sink(0);let p=AtomicBool::new(false);display(&mut d,&mut s,&[],crate::viewer::media::Codec::H264,&p).unwrap();assert!(p.load(Ordering::Acquire));display(&mut d,&mut s,&[],crate::viewer::media::Codec::H264,&p).unwrap();assert_eq!(s.0,1);}
|
||||||
}
|
}
|
||||||
+49
-2
@@ -16,6 +16,25 @@ pub fn windows_vk(scan: i32) -> Option<i32> {
|
|||||||
_=>return None,
|
_=>return None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
pub fn mac_keycode(scan:i32)->Option<i32>{
|
||||||
|
// Inverted SDL Darwin table (release-2.32.10/src/events/scancodes_darwin.h).
|
||||||
|
// Live Mac host evidence: Windows VK 49 becomes Space, and VK 8 becomes C.
|
||||||
|
Some(match scan{
|
||||||
|
4=>0,22=>1,7=>2,9=>3,11=>4,10=>5,29=>6,27=>7,6=>8,25=>9,100=>10,5=>11,
|
||||||
|
20=>12,26=>13,8=>14,21=>15,28=>16,23=>17,30=>18,31=>19,32=>20,33=>21,
|
||||||
|
35=>22,34=>23,46=>24,38=>25,36=>26,45=>27,37=>28,39=>29,48=>30,18=>31,
|
||||||
|
24=>32,47=>33,12=>34,19=>35,40=>36,15=>37,13=>38,52=>39,14=>40,51=>41,
|
||||||
|
49=>42,54=>43,56=>44,17=>45,16=>46,55=>47,43=>48,44=>49,53=>50,42=>51,
|
||||||
|
41=>53,231=>54,227=>55,225=>56,57=>57,226=>58,224=>59,229=>60,230=>61,228=>62,
|
||||||
|
108=>64,99=>65,85=>67,87=>69,83=>71,84=>75,88=>76,86=>78,109=>79,110=>80,
|
||||||
|
103=>81,98=>82,89=>83,90=>84,91=>85,92=>86,93=>87,94=>88,95=>89,96=>91,97=>92,
|
||||||
|
62=>96,63=>97,64=>98,60=>99,65=>100,66=>101,68=>103,104=>105,107=>106,105=>107,
|
||||||
|
70=>105,71=>107,72=>113,
|
||||||
|
67=>109,101=>110,69=>111,106=>113,73=>114,74=>115,75=>116,76=>117,61=>118,
|
||||||
|
77=>119,59=>120,78=>121,58=>122,80=>123,79=>124,81=>125,82=>126,
|
||||||
|
_=>return None,
|
||||||
|
})
|
||||||
|
}
|
||||||
#[derive(Clone,Copy,Debug,PartialEq)]
|
#[derive(Clone,Copy,Debug,PartialEq)]
|
||||||
pub struct Viewport { pub x:f64, pub y:f64, pub width:f64, pub height:f64 }
|
pub struct Viewport { pub x:f64, pub y:f64, pub width:f64, pub height:f64 }
|
||||||
impl Viewport {
|
impl Viewport {
|
||||||
@@ -30,16 +49,17 @@ impl Viewport {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Input { view_only:bool, keys:BTreeMap<i32,i32>, buttons:BTreeSet<i32> }
|
pub struct Input { view_only:bool, mac:bool, keys:BTreeMap<i32,i32>, buttons:BTreeSet<i32> }
|
||||||
pub fn view_only(argument:Option<&str>,environment:Option<&str>)->Result<bool,String> {
|
pub fn view_only(argument:Option<&str>,environment:Option<&str>)->Result<bool,String> {
|
||||||
match argument {None=>Ok(environment==Some("1")),Some("--view-only")=>Ok(true),_=>Err("Expected --view-only".into())}
|
match argument {None=>Ok(environment==Some("1")),Some("--view-only")=>Ok(true),_=>Err("Expected --view-only".into())}
|
||||||
}
|
}
|
||||||
impl Input {
|
impl Input {
|
||||||
pub fn new(view_only:bool)->Self {Self{view_only,..Default::default()}}
|
pub fn new(view_only:bool)->Self {Self{view_only,..Default::default()}}
|
||||||
|
pub fn set_mac(&mut self,mac:bool){self.mac=mac;}
|
||||||
pub fn enabled(&self)->bool {!self.view_only}
|
pub fn enabled(&self)->bool {!self.view_only}
|
||||||
pub fn key(&mut self,scan:i32,down:bool)->Option<Value> {
|
pub fn key(&mut self,scan:i32,down:bool)->Option<Value> {
|
||||||
if self.view_only {return None}
|
if self.view_only {return None}
|
||||||
let vk=windows_vk(scan)?;
|
let vk=if self.mac{mac_keycode(scan)}else{windows_vk(scan)}?;
|
||||||
let changed=if down {
|
let changed=if down {
|
||||||
if self.keys.contains_key(&scan) {return None}
|
if self.keys.contains_key(&scan) {return None}
|
||||||
let already=self.keys.values().any(|v|*v==vk);self.keys.insert(scan,vk);!already
|
let already=self.keys.values().any(|v|*v==vk);self.keys.insert(scan,vk);!already
|
||||||
@@ -65,6 +85,33 @@ impl Input {
|
|||||||
}
|
}
|
||||||
#[cfg(test)] mod tests {
|
#[cfg(test)] mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
#[test] fn mac_target_uses_native_codes_and_releases_same_keys(){
|
||||||
|
let mut input=Input::default();input.set_mac(true);
|
||||||
|
for (scan,code) in [(30,18),(39,29),(42,51),(44,49),(6,8),(4,0),(40,36),(43,48),
|
||||||
|
(80,123),(79,124),(81,125),(82,126),(224,59),(225,56),(226,58),(227,55)]{
|
||||||
|
assert_eq!(input.key(scan,true).unwrap()["key"],code);
|
||||||
|
assert_eq!(input.key(scan,false).unwrap()["key"],code);
|
||||||
|
}
|
||||||
|
input.set_mac(false);assert_eq!(input.key(30,true).unwrap()["key"],49);
|
||||||
|
}
|
||||||
|
#[test] fn mac_focus_release_keeps_zero_keycode_and_modifiers(){
|
||||||
|
let mut input=Input::default();input.set_mac(true);
|
||||||
|
assert!(input.key(4,true).is_some());assert!(input.key(4,true).is_none());
|
||||||
|
input.key(225,true);input.key(227,true);
|
||||||
|
let release=input.release_all();
|
||||||
|
assert_eq!(release,vec![json!({"action":"kbd_release","key":0}),
|
||||||
|
json!({"action":"kbd_release","key":55}),json!({"action":"kbd_release","key":56})]);
|
||||||
|
assert!(input.release_all().is_empty());assert!(input.key(4,false).is_none());
|
||||||
|
let mut watch=Input::new(true);watch.set_mac(true);assert!(watch.key(4,true).is_none());
|
||||||
|
}
|
||||||
|
#[test] fn mac_function_key_aliases_release_after_last_keyup(){
|
||||||
|
for (pc,function,code) in [(70,104,105),(71,105,107),(72,106,113)]{
|
||||||
|
let mut input=Input::default();input.set_mac(true);
|
||||||
|
assert_eq!(input.key(pc,true).unwrap()["key"],code);
|
||||||
|
assert!(input.key(function,true).is_none());assert!(input.key(pc,false).is_none());
|
||||||
|
assert_eq!(input.key(function,false).unwrap()["key"],code);
|
||||||
|
}
|
||||||
|
}
|
||||||
#[test] fn watch_switch_is_explicit_and_validated(){
|
#[test] fn watch_switch_is_explicit_and_validated(){
|
||||||
assert!(!view_only(None,None).unwrap());assert!(!view_only(None,Some("0")).unwrap());
|
assert!(!view_only(None,None).unwrap());assert!(!view_only(None,Some("0")).unwrap());
|
||||||
assert!(view_only(None,Some("1")).unwrap());assert!(view_only(Some("--view-only"),Some("0")).unwrap());
|
assert!(view_only(None,Some("1")).unwrap());assert!(view_only(Some("--view-only"),Some("0")).unwrap());
|
||||||
|
|||||||
+95
-5
@@ -1,19 +1,32 @@
|
|||||||
//! Bounded RFC 6184 packetization-mode 1 receiver. No media contents in logs.
|
//! Bounded RFC 6184 / RFC 7798 non-interleaved receivers (no DONL). No media contents in logs.
|
||||||
use std::{collections::VecDeque, sync::{Arc, Mutex}};
|
use std::{collections::VecDeque, sync::{Arc, Mutex}};
|
||||||
use std::time::{Duration,Instant};
|
use std::time::{Duration,Instant};
|
||||||
|
#[derive(Clone,Copy,Debug,Default,PartialEq,Eq)]
|
||||||
|
pub enum Codec { #[default] H264, H265 }
|
||||||
|
impl Codec {
|
||||||
|
pub fn from_mime(mime:&str)->Option<Self> {
|
||||||
|
if mime.eq_ignore_ascii_case("video/H264"){Some(Self::H264)}
|
||||||
|
else if mime.eq_ignore_ascii_case("video/H265"){Some(Self::H265)}else{None}
|
||||||
|
}
|
||||||
|
pub fn demuxer(self)->&'static str {match self {Self::H264=>"h264",Self::H265=>"hevc"}}
|
||||||
|
}
|
||||||
|
pub struct AccessUnit {pub codec:Codec,pub data:Vec<u8>}
|
||||||
const MAX_AU: usize = 8 * 1024 * 1024;
|
const MAX_AU: usize = 8 * 1024 * 1024;
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Packet { pub seq: u16, pub timestamp: u32, pub marker: bool, pub payload: Vec<u8> }
|
pub struct Packet { pub seq: u16, pub timestamp: u32, pub marker: bool, pub payload: Vec<u8> }
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Output { pub units: Vec<Vec<u8>>, pub pli: bool }
|
pub struct Output { pub units: Vec<Vec<u8>>, pub pli: bool }
|
||||||
pub struct Receiver {
|
pub struct Receiver {
|
||||||
|
codec: Codec,
|
||||||
next: Option<u16>, pending: Vec<Packet>, timestamp: Option<u32>,
|
next: Option<u16>, pending: Vec<Packet>, timestamp: Option<u32>,
|
||||||
au: Vec<u8>, fragment: Option<u8>, damaged: bool, gap_since:Option<Instant>, last_packet:Instant,
|
au: Vec<u8>, fragment: Option<u16>, damaged: bool, gap_since:Option<Instant>, last_packet:Instant,
|
||||||
}
|
}
|
||||||
impl Default for Receiver {
|
impl Default for Receiver {
|
||||||
fn default() -> Self { Self { next: None, pending: Vec::new(), timestamp: None, au: Vec::new(), fragment: None, damaged: false, gap_since:None,last_packet:Instant::now() } }
|
fn default() -> Self { Self { codec: Codec::H264, next: None, pending: Vec::new(), timestamp: None, au: Vec::new(), fragment: None, damaged: false, gap_since:None,last_packet:Instant::now() } }
|
||||||
}
|
}
|
||||||
impl Receiver {
|
impl Receiver {
|
||||||
|
pub fn new(codec:Codec)->Self {Self{codec,..Self::default()}}
|
||||||
|
pub fn codec(&self)->Codec {self.codec}
|
||||||
pub fn push(&mut self, p: Packet) -> Output {
|
pub fn push(&mut self, p: Packet) -> Output {
|
||||||
if p.payload.len()>65536 {return Output{units:Vec::new(),pli:true}}
|
if p.payload.len()>65536 {return Output{units:Vec::new(),pli:true}}
|
||||||
let next = *self.next.get_or_insert(p.seq);
|
let next = *self.next.get_or_insert(p.seq);
|
||||||
@@ -67,6 +80,7 @@ impl Receiver {
|
|||||||
self.au.extend_from_slice(bytes); Ok(())
|
self.au.extend_from_slice(bytes); Ok(())
|
||||||
}
|
}
|
||||||
fn nal(&mut self, p: &[u8]) -> Result<(), ()> {
|
fn nal(&mut self, p: &[u8]) -> Result<(), ()> {
|
||||||
|
if self.codec==Codec::H265 {return self.hevc(p)}
|
||||||
let h = *p.first().ok_or(())?;
|
let h = *p.first().ok_or(())?;
|
||||||
if h & 0x80 != 0 { return Err(()) }
|
if h & 0x80 != 0 { return Err(()) }
|
||||||
match h & 31 {
|
match h & 31 {
|
||||||
@@ -86,14 +100,47 @@ impl Receiver {
|
|||||||
let header = (h & 0xe0) | kind;
|
let header = (h & 0xe0) | kind;
|
||||||
if start {
|
if start {
|
||||||
if self.fragment.is_some() { return Err(()) }
|
if self.fragment.is_some() { return Err(()) }
|
||||||
self.append(&[0,0,0,1,header])?; self.fragment = Some(header);
|
self.append(&[0,0,0,1,header])?; self.fragment = Some(u16::from(header));
|
||||||
} else if self.fragment != Some(header) { return Err(()) }
|
} else if self.fragment != Some(u16::from(header)) { return Err(()) }
|
||||||
self.append(&p[2..])?;
|
self.append(&p[2..])?;
|
||||||
if end { self.fragment = None; } Ok(())
|
if end { self.fragment = None; } Ok(())
|
||||||
}
|
}
|
||||||
_ => Err(()),
|
_ => Err(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fn hevc_header(p:&[u8])->Result<u16,()> {
|
||||||
|
if p.len()<2 || p[0]&0x80!=0 || p[1]&7==0 {return Err(())}
|
||||||
|
Ok(u16::from_be_bytes([p[0],p[1]]))
|
||||||
|
}
|
||||||
|
fn hevc(&mut self,p:&[u8])->Result<(),()> {
|
||||||
|
Self::hevc_header(p)?;
|
||||||
|
match (p[0]>>1)&63 {
|
||||||
|
0..=47 if self.fragment.is_none()=>{self.append(&[0,0,0,1])?;self.append(p)},
|
||||||
|
48 if self.fragment.is_none()=>{
|
||||||
|
let mut rest=&p[2..];if rest.is_empty(){return Err(())}
|
||||||
|
while !rest.is_empty() {
|
||||||
|
if rest.len()<2 {return Err(())}
|
||||||
|
let n=u16::from_be_bytes([rest[0],rest[1]]) as usize;rest=&rest[2..];
|
||||||
|
if n<2 || rest.len()<n {return Err(())}
|
||||||
|
Self::hevc_header(&rest[..n])?;
|
||||||
|
if (rest[0]>>1)&63>=48 {return Err(())}
|
||||||
|
self.append(&[0,0,0,1])?;self.append(&rest[..n])?;rest=&rest[n..];
|
||||||
|
}Ok(())
|
||||||
|
},
|
||||||
|
49 if p.len()>3=>{
|
||||||
|
let f=p[2];let kind=f&63;let start=f&128!=0;let end=f&64!=0;
|
||||||
|
if kind>=48 || (start&&end){return Err(())}
|
||||||
|
// RFC 7798 4.4.3: preserve LayerId and TID, replacing only FU's NAL type.
|
||||||
|
let header=[(p[0]&0x81)|(kind<<1),p[1]];let key=u16::from_be_bytes(header);
|
||||||
|
if start {
|
||||||
|
if self.fragment.is_some(){return Err(())}
|
||||||
|
self.append(&[0,0,0,1])?;self.append(&header)?;self.fragment=Some(key);
|
||||||
|
}else if self.fragment!=Some(key){return Err(())}
|
||||||
|
self.append(&p[3..])?;if end{self.fragment=None;}Ok(())
|
||||||
|
},
|
||||||
|
_=>Err(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/// Drop the oldest item on overflow; never block the RTP reader on a decoder/UI.
|
/// Drop the oldest item on overflow; never block the RTP reader on a decoder/UI.
|
||||||
pub struct Latest<T>(Arc<Mutex<VecDeque<T>>>, usize);
|
pub struct Latest<T>(Arc<Mutex<VecDeque<T>>>, usize);
|
||||||
@@ -136,3 +183,46 @@ mod tests {
|
|||||||
let q=Latest::new(2);assert!(!q.push(1));q.push(2);assert!(q.push(3));assert_eq!(q.pop(),Some(2));assert_eq!(q.pop(),Some(3));
|
let q=Latest::new(2);assert!(!q.push(1));q.push(2);assert!(q.push(3));assert_eq!(q.pop(),Some(2));assert_eq!(q.pop(),Some(3));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)] mod hevc_tests {
|
||||||
|
use super::*;
|
||||||
|
fn packet(seq:u16,ts:u32,marker:bool,payload:Vec<u8>)->Packet {Packet{seq,timestamp:ts,marker,payload}}
|
||||||
|
fn sample()->Vec<u8>{
|
||||||
|
let mut au=vec![0,0,0,1,64,1,9,0,0,0,1,66,1,8,0,0,0,1,68,1,7,0,0,0,1,0x27,0x2b];
|
||||||
|
au.extend((0..2501).map(|i|(i%251+1)as u8));au
|
||||||
|
}
|
||||||
|
#[test] fn host_payloader_round_trip_single_fu_and_reorder(){
|
||||||
|
for mtu in [4,5,7,1190,9000] {
|
||||||
|
let au=sample();let payloads=crate::hevc::payload(mtu,&au).unwrap();let n=payloads.len();
|
||||||
|
let mut packets:Vec<_>=payloads.into_iter().enumerate().map(|(i,b)|packet(65534u16.wrapping_add(i as u16),10,i+1==n,b.to_vec())).collect();
|
||||||
|
// Start at the first sequence, then reorder adjacent packets across wraparound.
|
||||||
|
for i in (1..n.saturating_sub(1)).step_by(2){packets.swap(i,i+1);}
|
||||||
|
let mut r=Receiver::new(Codec::H265);let mut units=Vec::new();
|
||||||
|
for p in packets {let o=r.push(p);assert!(!o.pli);units.extend(o.units);}
|
||||||
|
assert_eq!(units,vec![au]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[test] fn aggregation_and_malformed_packets(){
|
||||||
|
let ap=vec![96,1,0,3,64,1,9,0,3,38,1,7];
|
||||||
|
let mut r=Receiver::new(Codec::H265);
|
||||||
|
assert_eq!(r.push(packet(0,0,true,ap)).units,vec![vec![0,0,0,1,64,1,9,0,0,0,1,38,1,7]]);
|
||||||
|
for payload in [vec![],vec![64],vec![128,1],vec![64,0],vec![96,1],vec![96,1,0,4,38,1,7],
|
||||||
|
vec![96,1,0,2,96,1],vec![98,1,0xc1,3],vec![98,1,0x41,3],vec![98,1,0xb0,3],vec![100,1,7]] {
|
||||||
|
let o=Receiver::new(Codec::H265).push(packet(1,1,true,payload));assert!(o.pli);assert!(o.units.is_empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[test] fn loss_duplicates_incomplete_tail_and_header_mismatch(){
|
||||||
|
let payloads=crate::hevc::payload(1190,&sample()).unwrap();
|
||||||
|
let mut r=Receiver::new(Codec::H265);
|
||||||
|
// Parameter sets followed by a fragmented IDR; lose its middle fragment.
|
||||||
|
for (i,p) in payloads.iter().enumerate().take(4){r.push(packet(i as u16,1,false,p.to_vec()));}
|
||||||
|
let tail=packet(5,1,true,payloads[5].to_vec());
|
||||||
|
assert!(r.push(tail.clone()).units.is_empty());assert!(r.push(tail).units.is_empty());
|
||||||
|
let o=r.expire_at(Instant::now()+Duration::from_millis(50));assert!(o.pli);assert!(o.units.is_empty());
|
||||||
|
assert_eq!(r.push(packet(6,2,true,vec![38,1,9])).units.len(),1);
|
||||||
|
r.push(packet(7,3,false,vec![98,1,0x93,7]));
|
||||||
|
assert!(r.expire_at(Instant::now()+Duration::from_millis(201)).pli);
|
||||||
|
let mut r=Receiver::new(Codec::H265);r.push(packet(0,0,false,vec![98,1,0x93,7]));
|
||||||
|
let o=r.push(packet(1,0,true,vec![98,2,0x53,8]));assert!(o.pli);assert!(o.units.is_empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -12,7 +12,7 @@ pub mod audio;
|
|||||||
pub mod input;
|
pub mod input;
|
||||||
pub mod desktop;
|
pub mod desktop;
|
||||||
#[cfg(feature="viewer-ffmpeg")] pub mod decoder;
|
#[cfg(feature="viewer-ffmpeg")] pub mod decoder;
|
||||||
#[cfg(feature="viewer-sdl")] pub mod decoder_cli;
|
#[cfg(any(test,feature="viewer-sdl"))] pub mod decoder_cli;
|
||||||
#[cfg(uu_gtk)] pub mod ui;
|
#[cfg(uu_gtk)] pub mod ui;
|
||||||
#[cfg(uu_gtk)] pub mod tools_ui;
|
#[cfg(uu_gtk)] pub mod tools_ui;
|
||||||
#[cfg(feature="viewer-sdl")] pub mod sdl;
|
#[cfg(feature="viewer-sdl")] pub mod sdl;
|
||||||
|
|||||||
+51
-16
@@ -1,6 +1,6 @@
|
|||||||
use super::wire::{self, Shape, CHANNELS};
|
use super::wire::{self, Shape, CHANNELS};
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
use std::sync::{Arc,atomic::{AtomicBool,AtomicU64,Ordering}};
|
use std::sync::{Arc,atomic::{AtomicBool,AtomicU32,AtomicU64,Ordering}};
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use webrtc::{api::{APIBuilder,media_engine::MediaEngine,setting_engine::SettingEngine}, peer_connection::{RTCPeerConnection,configuration::RTCConfiguration,sdp::session_description::RTCSessionDescription,policy::ice_transport_policy::RTCIceTransportPolicy}, ice_transport::{ice_server::RTCIceServer,ice_candidate::RTCIceCandidateInit}, data_channel::{RTCDataChannel,data_channel_init::RTCDataChannelInit}, rtp_transceiver::{rtp_codec::RTPCodecType,rtp_transceiver_direction::RTCRtpTransceiverDirection,RTCRtpTransceiverInit}};
|
use webrtc::{api::{APIBuilder,media_engine::MediaEngine,setting_engine::SettingEngine}, peer_connection::{RTCPeerConnection,configuration::RTCConfiguration,sdp::session_description::RTCSessionDescription,policy::ice_transport_policy::RTCIceTransportPolicy}, ice_transport::{ice_server::RTCIceServer,ice_candidate::RTCIceCandidateInit}, data_channel::{RTCDataChannel,data_channel_init::RTCDataChannelInit}, rtp_transceiver::{rtp_codec::RTPCodecType,rtp_transceiver_direction::RTCRtpTransceiverDirection,RTCRtpTransceiverInit}};
|
||||||
|
|
||||||
@@ -34,6 +34,8 @@ pub struct Observed {
|
|||||||
pub control_messages:AtomicU64,
|
pub control_messages:AtomicU64,
|
||||||
pub other_messages:AtomicU64,
|
pub other_messages:AtomicU64,
|
||||||
pub media_packets:AtomicU64,
|
pub media_packets:AtomicU64,
|
||||||
|
pub encoder_codecs:AtomicU32,
|
||||||
|
pub chroma:AtomicU32,
|
||||||
}
|
}
|
||||||
pub struct Peer {
|
pub struct Peer {
|
||||||
clipboard:super::clipboard::Bridge,
|
clipboard:super::clipboard::Bridge,
|
||||||
@@ -49,17 +51,21 @@ pub struct Peer {
|
|||||||
ice_id:String,
|
ice_id:String,
|
||||||
remote_client:String,
|
remote_client:String,
|
||||||
}
|
}
|
||||||
|
fn media_engine(shape:&Shape)->Result<MediaEngine,String>{
|
||||||
|
use webrtc::rtp_transceiver::rtp_codec::{RTCRtpCodecParameters,RTCRtpCodecCapability};
|
||||||
|
let mut engine=MediaEngine::default();
|
||||||
|
let mut codecs=vec![(RTPCodecType::Video,"video/H264",90000,0,"level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f",102),
|
||||||
|
(RTPCodecType::Audio,"audio/opus",48000,2,"minptime=10;useinbandfec=1",111)];
|
||||||
|
// RFC 7798 single-layer non-interleaved mode: no sprop-max-don-diff, hence no DONL.
|
||||||
|
if shape.decoders.iter().any(|d|d.codec==2){codecs.insert(0,(RTPCodecType::Video,"video/H265",90000,0,"",104));}
|
||||||
|
for (kind,mime,rate,channels,fmtp,payload) in codecs {
|
||||||
|
engine.register_codec(RTCRtpCodecParameters{capability:RTCRtpCodecCapability{mime_type:mime.into(),clock_rate:rate,channels,sdp_fmtp_line:fmtp.into(),..Default::default()},payload_type:payload,..Default::default()},kind).map_err(|_|"Codec initialization failed")?;
|
||||||
|
}Ok(engine)
|
||||||
|
}
|
||||||
impl Peer {
|
impl Peer {
|
||||||
pub async fn new(config:RTCConfiguration, shape:&Shape, app_id:&str, ice_id:&str, remote_client:&str, outbound:mpsc::Sender<Value>, settings:SettingEngine)->Result<Self,String> {
|
pub async fn new(config:RTCConfiguration, shape:&Shape, app_id:&str, ice_id:&str, remote_client:&str, outbound:mpsc::Sender<Value>, settings:SettingEngine, mut clipboard:super::clipboard::Bridge)->Result<Self,String> {
|
||||||
shape.validate_session()?;
|
shape.validate_session()?;
|
||||||
let mut engine=MediaEngine::default();
|
let mut engine=media_engine(shape)?;
|
||||||
use webrtc::rtp_transceiver::rtp_codec::{RTCRtpCodecParameters,RTCRtpCodecCapability};
|
|
||||||
for (kind,mime,rate,channels,fmtp,payload) in [
|
|
||||||
(RTPCodecType::Video,"video/H264",90000,0,"level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f",102),
|
|
||||||
(RTPCodecType::Audio,"audio/opus",48000,2,"minptime=10;useinbandfec=1",111),
|
|
||||||
] {
|
|
||||||
engine.register_codec(RTCRtpCodecParameters{capability:RTCRtpCodecCapability{mime_type:mime.into(),clock_rate:rate,channels,sdp_fmtp_line:fmtp.into(),..Default::default()},payload_type:payload,..Default::default()},kind).map_err(|_|"Codec initialization failed")?;
|
|
||||||
}
|
|
||||||
// UU RTP metadata the official Mac viewer offers (src/rtp_metadata.rs on the host side).
|
// UU RTP metadata the official Mac viewer offers (src/rtp_metadata.rs on the host side).
|
||||||
for uri in ["http://www.webrtc.org/experiments/rtp-hdrext/video-capture-index","http://www.webrtc.org/experiments/rtp-hdrext/video-frame-is-new-frame"] {
|
for uri in ["http://www.webrtc.org/experiments/rtp-hdrext/video-capture-index","http://www.webrtc.org/experiments/rtp-hdrext/video-frame-is-new-frame"] {
|
||||||
engine.register_header_extension(webrtc::rtp_transceiver::rtp_codec::RTCRtpHeaderExtensionCapability{uri:uri.into()},RTPCodecType::Video,None).map_err(|_|"RTP header extension registration failed")?;
|
engine.register_header_extension(webrtc::rtp_transceiver::rtp_codec::RTCRtpHeaderExtensionCapability{uri:uri.into()},RTPCodecType::Video,None).map_err(|_|"RTP header extension registration failed")?;
|
||||||
@@ -104,9 +110,9 @@ impl Peer {
|
|||||||
let video=track.kind()==RTPCodecType::Video;
|
let video=track.kind()==RTPCodecType::Video;
|
||||||
log::info!("Viewer track: kind={} mime={} payload_type={}",track.kind(),track.codec().capability.mime_type,track.payload_type());
|
log::info!("Viewer track: kind={} mime={} payload_type={}",track.kind(),track.codec().capability.mime_type,track.payload_type());
|
||||||
let opus=track.kind()==RTPCodecType::Audio && track.codec().capability.mime_type.eq_ignore_ascii_case("audio/opus");
|
let opus=track.kind()==RTPCodecType::Audio && track.codec().capability.mime_type.eq_ignore_ascii_case("audio/opus");
|
||||||
if video && !track.codec().capability.mime_type.eq_ignore_ascii_case("video/H264") {return}
|
if video && super::media::Codec::from_mime(&track.codec().capability.mime_type).is_none() {return}
|
||||||
if video {desktop.ssrc.store(track.ssrc(),Ordering::Release);}
|
if video {desktop.ssrc.store(track.ssrc(),Ordering::Release);}
|
||||||
let mut receiver=super::media::Receiver::default();
|
let mut receiver=super::media::Receiver::new(super::media::Codec::from_mime(&track.codec().capability.mime_type).unwrap_or_default());
|
||||||
let mut tick=tokio::time::interval(std::time::Duration::from_millis(40));
|
let mut tick=tokio::time::interval(std::time::Duration::from_millis(40));
|
||||||
let mut last_pli=std::time::Instant::now()-std::time::Duration::from_secs(1);
|
let mut last_pli=std::time::Instant::now()-std::time::Duration::from_secs(1);
|
||||||
loop {
|
loop {
|
||||||
@@ -115,6 +121,12 @@ impl Peer {
|
|||||||
Ok((p,_))=>{
|
Ok((p,_))=>{
|
||||||
counters.media_packets.fetch_add(1,Ordering::Relaxed);
|
counters.media_packets.fetch_add(1,Ordering::Relaxed);
|
||||||
if !video {if opus {audio.push(p.header.sequence_number,&p.payload);}continue}
|
if !video {if opus {audio.push(p.header.sequence_number,&p.payload);}continue}
|
||||||
|
// TrackRemote updates codec from the negotiated payload type on each read.
|
||||||
|
let Some(codec)=super::media::Codec::from_mime(&track.codec().capability.mime_type) else{continue};
|
||||||
|
if receiver.codec()!=codec {
|
||||||
|
receiver=super::media::Receiver::new(codec);
|
||||||
|
desktop.reset.store(true,Ordering::Release);desktop.pli.store(true,Ordering::Release);
|
||||||
|
}
|
||||||
receiver.push(super::media::Packet{seq:p.header.sequence_number,timestamp:p.header.timestamp,marker:p.header.marker,payload:p.payload.to_vec()})
|
receiver.push(super::media::Packet{seq:p.header.sequence_number,timestamp:p.header.timestamp,marker:p.header.marker,payload:p.payload.to_vec()})
|
||||||
},
|
},
|
||||||
Err(_)=>break,
|
Err(_)=>break,
|
||||||
@@ -122,7 +134,7 @@ impl Peer {
|
|||||||
_=tick.tick()=>{if !video {continue} receiver.expire()},
|
_=tick.tick()=>{if !video {continue} receiver.expire()},
|
||||||
};
|
};
|
||||||
if output.pli {desktop.reset.store(true,Ordering::Release);desktop.pli.store(true,Ordering::Release);}
|
if output.pli {desktop.reset.store(true,Ordering::Release);desktop.pli.store(true,Ordering::Release);}
|
||||||
for au in output.units {if desktop.units.push(au) {desktop.reset.store(true,Ordering::Release);desktop.pli.store(true,Ordering::Release);}}
|
for au in output.units {if desktop.units.push(super::media::AccessUnit{codec:receiver.codec(),data:au}) {desktop.reset.store(true,Ordering::Release);desktop.pli.store(true,Ordering::Release);}}
|
||||||
if desktop.pli.load(Ordering::Acquire) && last_pli.elapsed()>=std::time::Duration::from_millis(250) {
|
if desktop.pli.load(Ordering::Acquire) && last_pli.elapsed()>=std::time::Duration::from_millis(250) {
|
||||||
desktop.pli.swap(false,Ordering::AcqRel);
|
desktop.pli.swap(false,Ordering::AcqRel);
|
||||||
if let Some(pc)=weak_pc.upgrade() {
|
if let Some(pc)=weak_pc.upgrade() {
|
||||||
@@ -143,7 +155,6 @@ impl Peer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut channels=Vec::new();
|
let mut channels=Vec::new();
|
||||||
let mut clipboard=super::clipboard::Bridge::new(shape.capture_type==1);
|
|
||||||
let clipboard_enabled=clipboard.enabled;
|
let clipboard_enabled=clipboard.enabled;
|
||||||
// Diagnostic: hosts may open their own channels; log the label and message count only.
|
// Diagnostic: hosts may open their own channels; log the label and message count only.
|
||||||
pc.on_data_channel(Box::new(|channel|Box::pin(async move {
|
pc.on_data_channel(Box::new(|channel|Box::pin(async move {
|
||||||
@@ -160,9 +171,13 @@ impl Peer {
|
|||||||
for (index,channel) in channels.iter().enumerate() {
|
for (index,channel) in channels.iter().enumerate() {
|
||||||
if index==0 {
|
if index==0 {
|
||||||
let weak=Arc::downgrade(&channel);let state=observed.clone();
|
let weak=Arc::downgrade(&channel);let state=observed.clone();
|
||||||
|
let chroma444=shape.capture_type==1 && std::env::var("UU_VIEWER_CHROMA").is_ok_and(|v|v=="444");
|
||||||
channel.on_open(Box::new(move ||Box::pin(async move {
|
channel.on_open(Box::new(move ||Box::pin(async move {
|
||||||
state.control_open.store(true,Ordering::Release);
|
state.control_open.store(true,Ordering::Release);
|
||||||
if let Some(channel)=weak.upgrade(){let _=channel.send(&bytes::Bytes::from(wire::echo_request_with_clipboard(1,clipboard_enabled))).await;}
|
if let Some(channel)=weak.upgrade(){
|
||||||
|
let _=channel.send(&bytes::Bytes::from(wire::echo_request_with_clipboard(1,clipboard_enabled))).await;
|
||||||
|
if chroma444 {let _=channel.send(&bytes::Bytes::from(wire::request_chroma444())).await;}
|
||||||
|
}
|
||||||
})));
|
})));
|
||||||
}
|
}
|
||||||
let closed=tools.clone();let state=observed.clone();
|
let closed=tools.clone();let state=observed.clone();
|
||||||
@@ -174,8 +189,14 @@ impl Peer {
|
|||||||
let control_for_echo=if index==0 {Arc::downgrade(&channel)} else {std::sync::Weak::new()};
|
let control_for_echo=if index==0 {Arc::downgrade(&channel)} else {std::sync::Weak::new()};
|
||||||
channel.on_message(Box::new(move |message| {let state=state.clone();let inbox=inbox.clone();let control_for_echo=control_for_echo.clone();let clipboard_tx=clipboard_tx.clone();Box::pin(async move {
|
channel.on_message(Box::new(move |message| {let state=state.clone();let inbox=inbox.clone();let control_for_echo=control_for_echo.clone();let clipboard_tx=clipboard_tx.clone();Box::pin(async move {
|
||||||
if index==0 {
|
if index==0 {
|
||||||
|
let (codecs,chroma)=wire::media_control(&message.data);
|
||||||
|
if let Some(codecs)=codecs {state.encoder_codecs.store(codecs,Ordering::Release);}
|
||||||
|
if let Some(chroma)=chroma {
|
||||||
|
if state.chroma.swap(chroma,Ordering::AcqRel)!=chroma {log::info!("Viewer capture sync: chroma={chroma}");}
|
||||||
|
}
|
||||||
let count=state.control_messages.fetch_add(1,Ordering::Relaxed);
|
let count=state.control_messages.fetch_add(1,Ordering::Relaxed);
|
||||||
if wire::echo_matches(&message.data,1) {state.clipboard_supported.store(wire::echo_clipboard_supported(&message.data),Ordering::Release);state.echo_received.store(true,Ordering::Release);}
|
if wire::echo_matches(&message.data,1) {let supported=wire::echo_clipboard_supported(&message.data);
|
||||||
|
if state.clipboard_supported.swap(supported,Ordering::AcqRel)!=supported || !state.echo_received.load(Ordering::Acquire) {log::info!("Viewer echo: clipboard_supported={supported}");}state.echo_received.store(true,Ordering::Release);}
|
||||||
if let Some(request)=wire::echo_request_seq(&message.data) {
|
if let Some(request)=wire::echo_request_seq(&message.data) {
|
||||||
if let Some(control)=control_for_echo.upgrade() {let _=control.send(&bytes::Bytes::from(wire::echo_response_with_clipboard(1_000_000+request,request,clipboard_enabled))).await;}
|
if let Some(control)=control_for_echo.upgrade() {let _=control.send(&bytes::Bytes::from(wire::echo_response_with_clipboard(1_000_000+request,request,clipboard_enabled))).await;}
|
||||||
}
|
}
|
||||||
@@ -313,3 +334,17 @@ pub fn with_max_message_size(sdp:&str)->String{
|
|||||||
assert_eq!(with_max_message_size("m=application 9\r\na=sctp-port:5000\r\na=mid:0\r\n"),"m=application 9\r\na=sctp-port:5000\r\na=max-message-size:262144\r\na=mid:0\r\n");
|
assert_eq!(with_max_message_size("m=application 9\r\na=sctp-port:5000\r\na=mid:0\r\n"),"m=application 9\r\na=sctp-port:5000\r\na=max-message-size:262144\r\na=mid:0\r\n");
|
||||||
assert_eq!(with_max_message_size("a=max-message-size:1\r\n"),"a=max-message-size:1\r\n");
|
assert_eq!(with_max_message_size("a=max-message-size:1\r\n"),"a=max-message-size:1\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)] #[tokio::test] async fn offers_both_codecs_or_h264_only(){
|
||||||
|
for force in [false,true] {
|
||||||
|
let shape:Shape=serde_json::from_str(include_str!("../../tests/fixtures/viewer/session-shape.json")).unwrap();
|
||||||
|
let shape=shape.video_preferences(force);
|
||||||
|
let api=APIBuilder::new().with_media_engine(media_engine(&shape).unwrap()).build();
|
||||||
|
let pc=api.new_peer_connection(RTCConfiguration::default()).await.unwrap();
|
||||||
|
pc.add_transceiver_from_kind(RTPCodecType::Video,Some(RTCRtpTransceiverInit{direction:RTCRtpTransceiverDirection::Recvonly,send_encodings:vec![]})).await.unwrap();
|
||||||
|
let offer=pc.create_offer(None).await.unwrap();
|
||||||
|
assert!(offer.sdp.contains("a=rtpmap:102 H264/90000"));
|
||||||
|
assert_eq!(offer.sdp.contains("a=rtpmap:104 H265/90000"),!force);
|
||||||
|
assert!(!offer.sdp.contains("sprop-max-don-diff"));pc.close().await.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
+99
-5
@@ -1,17 +1,61 @@
|
|||||||
use super::{desktop::{Frame,FrameSink},input::{Input,Viewport},session::Session};
|
use super::{desktop::{Frame,FrameSink},input::{Input,Viewport},session::Session};
|
||||||
use sdl2::{event::{Event,WindowEvent},mouse::MouseButton,pixels::PixelFormatEnum,rect::Rect};
|
use sdl2::{event::{Event,WindowEvent},mouse::MouseButton,pixels::PixelFormatEnum,rect::Rect};
|
||||||
pub struct Window { canvas:sdl2::render::Canvas<sdl2::video::Window>, events:sdl2::EventPump, _sdl:sdl2::Sdl, frame:(u32,u32), focused:bool, files:bool }
|
pub struct Window { canvas:sdl2::render::Canvas<sdl2::video::Window>, events:sdl2::EventPump, _sdl:sdl2::Sdl, frame:(u32,u32), focused:bool, files:bool, mac:bool,
|
||||||
|
clipboard:sdl2::clipboard::ClipboardUtil, clipboard_dirty:bool, clipboard_tx:tokio::sync::mpsc::Sender<super::clipboard::ClipboardRequest>, clipboard_requests:tokio::sync::mpsc::Receiver<super::clipboard::ClipboardRequest> }
|
||||||
impl Window {
|
impl Window {
|
||||||
pub fn new()->Result<Self,String>{
|
pub fn new()->Result<Self,String>{
|
||||||
let sdl=sdl2::init()?;let video=sdl.video()?;
|
let sdl=sdl2::init()?;let video=sdl.video()?;
|
||||||
let window=video.window("Native viewer (experimental)",1280,720).position_centered().resizable().build().map_err(|_|"Window creation failed")?;
|
let window=video.window("Native viewer (experimental)",1280,720).position_centered().resizable().build().map_err(|_|"Window creation failed")?;
|
||||||
let canvas=window.into_canvas().build().map_err(|_|"Renderer creation failed")?;
|
let canvas=window.into_canvas().build().map_err(|_|"Renderer creation failed")?;
|
||||||
let events=sdl.event_pump()?;Ok(Self{canvas,events,_sdl:sdl,frame:(1920,1080),focused:true,files:false})
|
let events=sdl.event_pump()?;let clipboard=video.clipboard();
|
||||||
|
let(clipboard_tx,clipboard_requests)=tokio::sync::mpsc::channel(16);
|
||||||
|
Ok(Self{canvas,events,_sdl:sdl,frame:(1920,1080),focused:true,files:false,mac:false,clipboard,clipboard_dirty:true,clipboard_tx,clipboard_requests})
|
||||||
}
|
}
|
||||||
|
pub fn clipboard_bridge(&self)->super::clipboard::Bridge{super::clipboard::Bridge::sdl(self.clipboard_tx.clone())}
|
||||||
|
pub fn set_mac(&mut self,mac:bool){self.mac=mac;}
|
||||||
fn viewport(&self)->Viewport {Viewport::fit(self.canvas.window().size(),self.frame)}
|
fn viewport(&self)->Viewport {Viewport::fit(self.canvas.window().size(),self.frame)}
|
||||||
|
fn poll_clipboard(&mut self){
|
||||||
|
// Use the existing SDL window; wl-paste creates a transient toplevel on GNOME every poll.
|
||||||
|
while let Ok(request)=self.clipboard_requests.try_recv(){
|
||||||
|
use super::clipboard::ClipboardRequest;
|
||||||
|
match request {
|
||||||
|
ClipboardRequest::Read(reply)=>{
|
||||||
|
if self.clipboard_dirty {log::debug!("Viewer clipboard read: focused={} has_text={}",self.focused,self.clipboard.has_clipboard_text());}
|
||||||
|
let result=if self.focused && std::mem::take(&mut self.clipboard_dirty) && self.clipboard.has_clipboard_text(){read_clipboard()}else{Ok(None)};
|
||||||
|
let _=reply.send(result);
|
||||||
|
},
|
||||||
|
ClipboardRequest::Write(text,reply)=>{
|
||||||
|
let result=if self.focused {
|
||||||
|
self.clipboard_dirty=true;
|
||||||
|
self.clipboard.set_clipboard_text(&text).map(|()|true).map_err(|_|())
|
||||||
|
}else{Ok(false)};
|
||||||
|
let _=reply.send(result);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn poll(&mut self,input:&mut Input)->(bool,Vec<serde_json::Value>){
|
pub fn poll(&mut self,input:&mut Input)->(bool,Vec<serde_json::Value>){
|
||||||
let viewport=self.viewport();
|
let viewport=self.viewport();
|
||||||
translate_actions(self.events.poll_iter(),input,viewport,&mut self.focused,&mut self.files)
|
let dirty=&mut self.clipboard_dirty;
|
||||||
|
let events=self.events.poll_iter().inspect(|event|{
|
||||||
|
if matches!(event,Event::ClipboardUpdate{..}|Event::Window{win_event:WindowEvent::FocusGained,..}){*dirty=true;}
|
||||||
|
});
|
||||||
|
translate_actions(events,input,viewport,&mut self.focused,&mut self.files)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn clipboard_text(bytes:&[u8])->Option<String>{
|
||||||
|
if bytes.len()>super::clipboard::MAX_TEXT{return None}
|
||||||
|
std::str::from_utf8(bytes).ok().map(str::to_owned)
|
||||||
|
}
|
||||||
|
fn read_clipboard()->Result<Option<String>,()>{
|
||||||
|
// SDL2 0.37's String wrapper panics on invalid UTF-8; validate native bytes before copying.
|
||||||
|
// SDL still acquires the selection synchronously; the bridge timeout cannot interrupt it.
|
||||||
|
unsafe{
|
||||||
|
let data=sdl2::sys::SDL_GetClipboardText();
|
||||||
|
if data.is_null(){return Err(())}
|
||||||
|
let length=libc::strnlen(data,super::clipboard::MAX_TEXT+1);
|
||||||
|
let text=clipboard_text(std::slice::from_raw_parts(data.cast::<u8>(),length));
|
||||||
|
sdl2::sys::SDL_free(data.cast());Ok(text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn translate(events:impl IntoIterator<Item=Event>,input:&mut Input,viewport:Viewport,focused:&mut bool)->(bool,Vec<serde_json::Value>){
|
fn translate(events:impl IntoIterator<Item=Event>,input:&mut Input,viewport:Viewport,focused:&mut bool)->(bool,Vec<serde_json::Value>){
|
||||||
@@ -67,7 +111,7 @@ pub async fn run(session:&mut Session,window:&mut Window,decoder:&mut dyn super:
|
|||||||
let mut command_open=true;let mut requested=false;
|
let mut command_open=true;let mut requested=false;
|
||||||
let mut terminate=tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()).map_err(|_|"Cannot watch termination")?;
|
let mut terminate=tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()).map_err(|_|"Cannot watch termination")?;
|
||||||
let mut audio=super::audio::Playback::new(&window._sdl,session.peer.audio.clone());
|
let mut audio=super::audio::Playback::new(&window._sdl,session.peer.audio.clone());
|
||||||
let mut input=Input::new(view_only);let mut tick=tokio::time::interval(std::time::Duration::from_millis(8));
|
let mut input=Input::new(view_only);input.set_mac(window.mac);let mut tick=tokio::time::interval(std::time::Duration::from_millis(8));
|
||||||
let mut heartbeat=tokio::time::interval(std::time::Duration::from_secs(1));
|
let mut heartbeat=tokio::time::interval(std::time::Duration::from_secs(1));
|
||||||
let deadline=tokio::time::sleep(std::time::Duration::from_secs(30));tokio::pin!(deadline);
|
let deadline=tokio::time::sleep(std::time::Duration::from_secs(30));tokio::pin!(deadline);
|
||||||
let result=async {
|
let result=async {
|
||||||
@@ -86,6 +130,7 @@ pub async fn run(session:&mut Session,window:&mut Window,decoder:&mut dyn super:
|
|||||||
_=tick.tick()=>{
|
_=tick.tick()=>{
|
||||||
if let Some(playback)=audio.as_mut(){if playback.pump().is_err(){log::warn!("Viewer audio playback stopped");audio=None;}}
|
if let Some(playback)=audio.as_mut(){if playback.pump().is_err(){log::warn!("Viewer audio playback stopped");audio=None;}}
|
||||||
let (quit,messages)=window.poll(&mut input);
|
let (quit,messages)=window.poll(&mut input);
|
||||||
|
window.poll_clipboard();
|
||||||
requested|=std::mem::take(&mut window.files);
|
requested|=std::mem::take(&mut window.files);
|
||||||
#[cfg(uu_gtk)] {
|
#[cfg(uu_gtk)] {
|
||||||
if files.as_ref().is_some_and(|f|f.is_finished()){
|
if files.as_ref().is_some_and(|f|f.is_finished()){
|
||||||
@@ -106,7 +151,8 @@ pub async fn run(session:&mut Session,window:&mut Window,decoder:&mut dyn super:
|
|||||||
// At most one AU per iteration keeps local event polling responsive.
|
// At most one AU per iteration keeps local event polling responsive.
|
||||||
if let Some(au)=session.peer.desktop.units.pop(){
|
if let Some(au)=session.peer.desktop.units.pop(){
|
||||||
if session.peer.desktop.reset.swap(false,std::sync::atomic::Ordering::AcqRel){decoder.reset();}
|
if session.peer.desktop.reset.swap(false,std::sync::atomic::Ordering::AcqRel){decoder.reset();}
|
||||||
super::desktop::display(decoder,window,&au,&session.peer.desktop.pli)?;}
|
decoder.set_codec(au.codec)?;
|
||||||
|
super::desktop::display(decoder,window,&au.data,au.codec,&session.peer.desktop.pli)?;}
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
}.await;
|
}.await;
|
||||||
@@ -117,6 +163,54 @@ pub async fn run(session:&mut Session,window:&mut Window,decoder:&mut dyn super:
|
|||||||
|
|
||||||
#[cfg(test)] mod tests {
|
#[cfg(test)] mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
// macOS: SDL video must start on the main thread, which the test harness does not use; CI runs it on Linux.
|
||||||
|
#[cfg_attr(target_os="macos",ignore)]
|
||||||
|
#[tokio::test] async fn clipboard_requests_use_existing_window(){
|
||||||
|
if std::env::var_os("UU_VIEWER_CLIPBOARD_TEST_CHILD").is_none(){
|
||||||
|
let output=std::process::Command::new(std::env::current_exe().unwrap())
|
||||||
|
.args(["--exact","viewer::sdl::tests::clipboard_requests_use_existing_window","--test-threads=1"])
|
||||||
|
.env("SDL_VIDEODRIVER","dummy").env("UU_VIEWER_CLIPBOARD_TEST_CHILD","1").output().unwrap();
|
||||||
|
assert!(output.status.success(),"{}{}",String::from_utf8_lossy(&output.stdout),String::from_utf8_lossy(&output.stderr));return
|
||||||
|
}
|
||||||
|
let mut window=Window::new().unwrap();
|
||||||
|
let tx=window.clipboard_tx.clone();
|
||||||
|
use super::super::clipboard::ClipboardRequest;
|
||||||
|
for text in ["clipboard text\n",""]{
|
||||||
|
let(reply,rx)=tokio::sync::oneshot::channel();
|
||||||
|
tx.send(ClipboardRequest::Write(text.into(),reply)).await.unwrap();
|
||||||
|
window.poll_clipboard();assert_eq!(rx.await.unwrap(),Ok(true));
|
||||||
|
let(reply,rx)=tokio::sync::oneshot::channel();
|
||||||
|
tx.send(ClipboardRequest::Read(reply)).await.unwrap();
|
||||||
|
window.poll_clipboard();assert_eq!(rx.await.unwrap(),Ok((!text.is_empty()).then(||text.to_owned())));
|
||||||
|
let(reply,rx)=tokio::sync::oneshot::channel();
|
||||||
|
tx.send(ClipboardRequest::Read(reply)).await.unwrap();
|
||||||
|
window.poll_clipboard();assert_eq!(rx.await.unwrap(),Ok(None));
|
||||||
|
}
|
||||||
|
window.focused=false;window.clipboard_dirty=true;
|
||||||
|
let(reply,rx)=tokio::sync::oneshot::channel();
|
||||||
|
tx.send(ClipboardRequest::Read(reply)).await.unwrap();
|
||||||
|
window.poll_clipboard();assert_eq!(rx.await.unwrap(),Ok(None));assert!(window.clipboard_dirty);
|
||||||
|
let(reply,rx)=tokio::sync::oneshot::channel();
|
||||||
|
tx.send(ClipboardRequest::Write("background".into(),reply)).await.unwrap();
|
||||||
|
window.poll_clipboard();assert_eq!(rx.await.unwrap(),Ok(false));
|
||||||
|
window.focused=true;
|
||||||
|
let(reply,rx)=tokio::sync::oneshot::channel();
|
||||||
|
tx.send(ClipboardRequest::Read(reply)).await.unwrap();
|
||||||
|
window.poll_clipboard();assert_eq!(rx.await.unwrap(),Ok(None));
|
||||||
|
window.clipboard_dirty=false;
|
||||||
|
window._sdl.event().unwrap().push_event(Event::Window{timestamp:0,window_id:window.canvas.window().id(),win_event:WindowEvent::FocusGained}).unwrap();
|
||||||
|
window.poll(&mut Input::new(true));assert!(window.clipboard_dirty);
|
||||||
|
let bridge=window.clipboard_bridge();assert!(bridge.enabled);
|
||||||
|
drop(window);
|
||||||
|
assert!(tx.is_closed());drop(bridge);
|
||||||
|
let window=Window::new().unwrap();assert!(window.clipboard_bridge().enabled);
|
||||||
|
}
|
||||||
|
#[test] fn clipboard_text_rejects_invalid_or_large_data_and_keeps_empty_text(){
|
||||||
|
assert_eq!(clipboard_text(b""),Some(String::new()));
|
||||||
|
assert_eq!(clipboard_text(b"text\n"),Some("text\n".into()));
|
||||||
|
assert_eq!(clipboard_text(&[255]),None);
|
||||||
|
assert_eq!(clipboard_text(&vec![b'a';super::super::clipboard::MAX_TEXT+1]),None);
|
||||||
|
}
|
||||||
#[test] fn file_shortcut_is_local_in_control_and_watch_modes(){
|
#[test] fn file_shortcut_is_local_in_control_and_watch_modes(){
|
||||||
use sdl2::keyboard::{Scancode as S,Mod as M};
|
use sdl2::keyboard::{Scancode as S,Mod as M};
|
||||||
for watch in [false,true]{
|
for watch in [false,true]{
|
||||||
|
|||||||
@@ -4,16 +4,18 @@ use std::{sync::atomic::Ordering,time::Duration};
|
|||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use webrtc::api::setting_engine::SettingEngine;
|
use webrtc::api::setting_engine::SettingEngine;
|
||||||
|
|
||||||
pub struct Options {pub timeout:Duration,pub legacy_prefix:bool,pub ready_policy:ReadyPolicy}
|
pub struct Options {pub timeout:Duration,pub legacy_prefix:bool,pub ready_policy:ReadyPolicy,pub clipboard:super::clipboard::Bridge}
|
||||||
pub struct Session {pub peer:Peer,signal:Signal,candidates:mpsc::Receiver<Value>,policy:ReadyPolicy}
|
pub struct Session {pub peer:Peer,signal:Signal,candidates:mpsc::Receiver<Value>,policy:ReadyPolicy}
|
||||||
impl Session {
|
impl Session {
|
||||||
pub async fn open(room:&JoinData,shape:&Shape,device:&str,client:&str,options:Options,settings:SettingEngine)->Result<Self,String> {
|
pub async fn open(room:&JoinData,shape:&Shape,device:&str,client:&str,options:Options,settings:SettingEngine)->Result<Self,String> {
|
||||||
|
shape.validate_session()?;
|
||||||
|
let shape=shape.video_preferences(std::env::var("UU_VIEWER_CODEC").is_ok_and(|v|v.eq_ignore_ascii_case("h264")));
|
||||||
shape.validate_session()?;
|
shape.validate_session()?;
|
||||||
let app=wire::uuid();let ice=hex::encode(rand::random::<[u8;16]>());
|
let app=wire::uuid();let ice=hex::encode(rand::random::<[u8;16]>());
|
||||||
let timeout=if room.ws_connect_timeout_ms>0 {Duration::from_millis(room.ws_connect_timeout_ms as u64)}else{options.timeout};
|
let timeout=if room.ws_connect_timeout_ms>0 {Duration::from_millis(room.ws_connect_timeout_ms as u64)}else{options.timeout};
|
||||||
let mut signal=Signal::connect(&room.signaling_list,&room.token,timeout,options.legacy_prefix).await?;
|
let mut signal=Signal::connect(&room.signaling_list,&room.token,timeout,options.legacy_prefix).await?;
|
||||||
let result=async {
|
let result=async {
|
||||||
let result=signal.control(shape.control(client,&app,&ice)?,shape.connect_options(device)?,options.timeout).await?;
|
let result=signal.control(shape.control(client,&app,&ice)?,shape.connect_options(device,options.clipboard.enabled)?,options.timeout).await?;
|
||||||
// Key names per ICE entry and URL scheme only (no values) to see where TURN credentials live.
|
// Key names per ICE entry and URL scheme only (no values) to see where TURN credentials live.
|
||||||
if let Some(list)=result.get("iceServers").and_then(Value::as_array) {
|
if let Some(list)=result.get("iceServers").and_then(Value::as_array) {
|
||||||
let summary:Vec<String>=list.iter().map(|s|{
|
let summary:Vec<String>=list.iter().map(|s|{
|
||||||
@@ -31,7 +33,7 @@ impl Session {
|
|||||||
let remote=result.get("client_id").and_then(Value::as_str).filter(|s|!s.is_empty()).ok_or("Control acknowledgement missing peer route")?;
|
let remote=result.get("client_id").and_then(Value::as_str).filter(|s|!s.is_empty()).ok_or("Control acknowledgement missing peer route")?;
|
||||||
let config=rtc::configuration(&result)?;
|
let config=rtc::configuration(&result)?;
|
||||||
let(tx,rx)=mpsc::channel(256);
|
let(tx,rx)=mpsc::channel(256);
|
||||||
let peer=Peer::new(config,shape,&app,&ice,remote,tx,settings).await?;
|
let peer=Peer::new(config,&shape,&app,&ice,remote,tx,settings,options.clipboard).await?;
|
||||||
// The official host drops an offer whose ice_id is not yet in its connection table
|
// The official host drops an offer whose ice_id is not yet in its connection table
|
||||||
// (libstreamer 0xc3080); give it time to process be-controlled first.
|
// (libstreamer 0xc3080); give it time to process be-controlled first.
|
||||||
let delay=std::env::var("UU_VIEWER_OFFER_DELAY_MS").ok().and_then(|v|v.parse().ok()).unwrap_or(0u64).min(10_000);
|
let delay=std::env::var("UU_VIEWER_OFFER_DELAY_MS").ok().and_then(|v|v.parse().ok()).unwrap_or(0u64).min(10_000);
|
||||||
|
|||||||
+61
-9
@@ -130,7 +130,7 @@ async fn complete_session_uses_loopback_signaling_and_virtual_rtc_through_echo()
|
|||||||
Some("offer")=> {
|
Some("offer")=> {
|
||||||
let offered=body["sdp"].as_str().unwrap();
|
let offered=body["sdp"].as_str().unwrap();
|
||||||
assert!(offered.contains("H264/90000") && offered.contains("opus/48000"));
|
assert!(offered.contains("H264/90000") && offered.contains("opus/48000"));
|
||||||
assert!(!offered.contains("H265") && !offered.contains("VP8"));
|
assert!(offered.contains("H265/90000") && !offered.contains("VP8"));
|
||||||
assert_eq!(offered.matches("a=recvonly").count(),2);
|
assert_eq!(offered.matches("a=recvonly").count(),2);
|
||||||
server_host.set_remote_description(RTCSessionDescription::offer(offered.into()).unwrap()).await.unwrap();
|
server_host.set_remote_description(RTCSessionDescription::offer(offered.into()).unwrap()).await.unwrap();
|
||||||
let answer=server_host.create_answer(None).await.unwrap();let sdp=answer.sdp.clone();let mut gathered=server_host.gathering_complete_promise().await;server_host.set_local_description(answer).await.unwrap();
|
let answer=server_host.create_answer(None).await.unwrap();let sdp=answer.sdp.clone();let mut gathered=server_host.gathering_complete_promise().await;server_host.set_local_description(answer).await.unwrap();
|
||||||
@@ -148,7 +148,7 @@ async fn complete_session_uses_loopback_signaling_and_virtual_rtc_through_echo()
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
let room:JoinData=serde_json::from_value(json!({"token":"synthetic-room-ticket","signaling_list":[url],"report_token":"synthetic-report","report_url":""})).unwrap();
|
let room:JoinData=serde_json::from_value(json!({"token":"synthetic-room-ticket","signaling_list":[url],"report_token":"synthetic-report","report_url":""})).unwrap();
|
||||||
let options=Options{timeout:Duration::from_secs(3),legacy_prefix:false,ready_policy:ReadyPolicy::Explicit};
|
let options=Options{timeout:Duration::from_secs(3),legacy_prefix:false,ready_policy:ReadyPolicy::Explicit,clipboard:super::clipboard::Bridge::new(false)};
|
||||||
let mut session=Session::open(&room,&shape(),"synthetic-device","synthetic-client",options,viewer_settings).await.unwrap();
|
let mut session=Session::open(&room,&shape(),"synthetic-device","synthetic-client",options,viewer_settings).await.unwrap();
|
||||||
assert_eq!(session.peer.channels.len(),5);
|
assert_eq!(session.peer.channels.len(),5);
|
||||||
let(tool_io,mut tool_rx)=super::tools::attach(&session.peer).unwrap();
|
let(tool_io,mut tool_rx)=super::tools::attach(&session.peer).unwrap();
|
||||||
@@ -169,7 +169,7 @@ async fn complete_session_uses_loopback_signaling_and_virtual_rtc_through_echo()
|
|||||||
let heartbeat=super::input::Input::heartbeat();session.peer.send_input(&heartbeat).await.unwrap();assert_eq!(input_rx.recv().await.unwrap(),heartbeat);
|
let heartbeat=super::input::Input::heartbeat();session.peer.send_input(&heartbeat).await.unwrap();assert_eq!(input_rx.recv().await.unwrap(),heartbeat);
|
||||||
let packet=|seq,ts,marker,payload:&[u8]|webrtc::rtp::packet::Packet{header:webrtc::rtp::header::Header{version:2,sequence_number:seq,timestamp:ts,marker,..Default::default()},payload:bytes::Bytes::copy_from_slice(payload)};
|
let packet=|seq,ts,marker,payload:&[u8]|webrtc::rtp::packet::Packet{header:webrtc::rtp::header::Header{version:2,sequence_number:seq,timestamp:ts,marker,..Default::default()},payload:bytes::Bytes::copy_from_slice(payload)};
|
||||||
track.write_rtp(&packet(9,1,true,&[0x65,1])).await.unwrap();
|
track.write_rtp(&packet(9,1,true,&[0x65,1])).await.unwrap();
|
||||||
loop {if let Some(au)=session.peer.desktop.units.pop(){assert_eq!(au,vec![0,0,0,1,0x65,1]);break}tokio::time::sleep(Duration::from_millis(10)).await;}
|
loop {if let Some(au)=session.peer.desktop.units.pop(){assert_eq!(au.codec,super::media::Codec::H264);assert_eq!(au.data,vec![0,0,0,1,0x65,1]);break}tokio::time::sleep(Duration::from_millis(10)).await;}
|
||||||
track.write_rtp(&packet(10,2,false,&[0x7c,0x85,1])).await.unwrap();
|
track.write_rtp(&packet(10,2,false,&[0x7c,0x85,1])).await.unwrap();
|
||||||
track.write_rtp(&packet(12,2,true,&[0x7c,0x45,3])).await.unwrap();
|
track.write_rtp(&packet(12,2,true,&[0x7c,0x45,3])).await.unwrap();
|
||||||
loop {let (packets,_)=sender.read_rtcp().await.unwrap();if packets.iter().any(|p|p.as_any().is::<webrtc::rtcp::payload_feedbacks::picture_loss_indication::PictureLossIndication>()){break}}
|
loop {let (packets,_)=sender.read_rtcp().await.unwrap();if packets.iter().any(|p|p.as_any().is::<webrtc::rtcp::payload_feedbacks::picture_loss_indication::PictureLossIndication>()){break}}
|
||||||
@@ -188,7 +188,7 @@ async fn complete_session_uses_loopback_signaling_and_virtual_rtc_through_echo()
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn early_candidates_and_foreign_session_messages_are_scoped() {
|
async fn early_candidates_and_foreign_session_messages_are_scoped() {
|
||||||
let(settings,_,router)=virtual_network().await;let(tx,_rx)=mpsc::channel(256);
|
let(settings,_,router)=virtual_network().await;let(tx,_rx)=mpsc::channel(256);
|
||||||
let mut peer=Peer::new(RTCConfiguration::default(),&shape(),"synthetic-app","synthetic-ice","synthetic-host",tx,settings).await.unwrap();
|
let mut peer=Peer::new(RTCConfiguration::default(),&shape(),"synthetic-app","synthetic-ice","synthetic-host",tx,settings,super::clipboard::Bridge::new(false)).await.unwrap();
|
||||||
assert!(!peer.matches_route(&json!({"ice_id":"foreign"})));
|
assert!(!peer.matches_route(&json!({"ice_id":"foreign"})));
|
||||||
assert!(!peer.matches_route(&json!({"app_control_id":"foreign"})));
|
assert!(!peer.matches_route(&json!({"app_control_id":"foreign"})));
|
||||||
peer.receive(&json!({"type":"answer","ice_id":"foreign","sdp":"invalid"})).await.unwrap();
|
peer.receive(&json!({"type":"answer","ice_id":"foreign","sdp":"invalid"})).await.unwrap();
|
||||||
@@ -201,8 +201,8 @@ async fn early_candidates_and_foreign_session_messages_are_scoped() {
|
|||||||
async fn closing_one_peer_preserves_another_peer() {
|
async fn closing_one_peer_preserves_another_peer() {
|
||||||
let(a,b,router)=virtual_network().await;
|
let(a,b,router)=virtual_network().await;
|
||||||
let(tx1,_rx1)=mpsc::channel(256);let(tx2,_rx2)=mpsc::channel(256);
|
let(tx1,_rx1)=mpsc::channel(256);let(tx2,_rx2)=mpsc::channel(256);
|
||||||
let first=Peer::new(RTCConfiguration::default(),&shape(),"synthetic-first","synthetic-first-ice","synthetic-host",tx1,a).await.unwrap();
|
let first=Peer::new(RTCConfiguration::default(),&shape(),"synthetic-first","synthetic-first-ice","synthetic-host",tx1,a,super::clipboard::Bridge::new(false)).await.unwrap();
|
||||||
let second=Peer::new(RTCConfiguration::default(),&shape(),"synthetic-second","synthetic-second-ice","synthetic-host",tx2,b).await.unwrap();
|
let second=Peer::new(RTCConfiguration::default(),&shape(),"synthetic-second","synthetic-second-ice","synthetic-host",tx2,b,super::clipboard::Bridge::new(false)).await.unwrap();
|
||||||
first.close().await;
|
first.close().await;
|
||||||
assert_ne!(second.pc.connection_state(),webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Closed);
|
assert_ne!(second.pc.connection_state(),webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Closed);
|
||||||
assert_eq!(second.channels.len(),5);
|
assert_eq!(second.channels.len(),5);
|
||||||
@@ -220,7 +220,7 @@ async fn opus_and_video_tracks_are_received_independently_in_memory() {
|
|||||||
let video=Arc::new(TrackLocalStaticRTP::new(RTCRtpCodecCapability{mime_type:"video/H264".into(),clock_rate:90000,sdp_fmtp_line:"level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f".into(),..Default::default()},"video".into(),"test".into()));
|
let video=Arc::new(TrackLocalStaticRTP::new(RTCRtpCodecCapability{mime_type:"video/H264".into(),clock_rate:90000,sdp_fmtp_line:"level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f".into(),..Default::default()},"video".into(),"test".into()));
|
||||||
host.add_track(audio.clone() as Arc<dyn TrackLocal+Send+Sync>).await.unwrap();host.add_track(video.clone() as Arc<dyn TrackLocal+Send+Sync>).await.unwrap();
|
host.add_track(audio.clone() as Arc<dyn TrackLocal+Send+Sync>).await.unwrap();host.add_track(video.clone() as Arc<dyn TrackLocal+Send+Sync>).await.unwrap();
|
||||||
let(tx,_rx)=mpsc::channel(256);
|
let(tx,_rx)=mpsc::channel(256);
|
||||||
let peer=Peer::new(RTCConfiguration::default(),&shape(),"app","ice","host",tx,viewer_settings).await.unwrap();
|
let peer=Peer::new(RTCConfiguration::default(),&shape().video_preferences(false),"app","ice","host",tx,viewer_settings,super::clipboard::Bridge::new(false)).await.unwrap();
|
||||||
let mut gathered=peer.pc.gathering_complete_promise().await;peer.offer().await.unwrap();gathered.recv().await;
|
let mut gathered=peer.pc.gathering_complete_promise().await;peer.offer().await.unwrap();gathered.recv().await;
|
||||||
let offer=peer.pc.local_description().await.unwrap();assert!(offer.sdp.contains("m=audio") && offer.sdp.contains("opus/48000/2"));
|
let offer=peer.pc.local_description().await.unwrap();assert!(offer.sdp.contains("m=audio") && offer.sdp.contains("opus/48000/2"));
|
||||||
host.set_remote_description(offer).await.unwrap();let answer=host.create_answer(None).await.unwrap();
|
host.set_remote_description(offer).await.unwrap();let answer=host.create_answer(None).await.unwrap();
|
||||||
@@ -233,7 +233,7 @@ async fn opus_and_video_tracks_are_received_independently_in_memory() {
|
|||||||
audio.write_rtp(&packet(&[0xf8,0xff,0xfe])).await.unwrap();video.write_rtp(&packet(&[0x65,1])).await.unwrap();
|
audio.write_rtp(&packet(&[0xf8,0xff,0xfe])).await.unwrap();video.write_rtp(&packet(&[0x65,1])).await.unwrap();
|
||||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||||
if let Some(data)=peer.audio.pop(){assert_eq!(data,vec![0xf8,0xff,0xfe]);heard=true;}
|
if let Some(data)=peer.audio.pop(){assert_eq!(data,vec![0xf8,0xff,0xfe]);heard=true;}
|
||||||
if let Some(data)=peer.desktop.units.pop(){assert_eq!(data,vec![0,0,0,1,0x65,1]);seen=true;}
|
if let Some(data)=peer.desktop.units.pop(){assert_eq!(data.codec,super::media::Codec::H264);assert_eq!(data.data,vec![0,0,0,1,0x65,1]);seen=true;}
|
||||||
if heard && seen {break}
|
if heard && seen {break}
|
||||||
}
|
}
|
||||||
assert!(heard && seen);peer.close().await;host.close().await.unwrap();router.lock().await.stop().await.unwrap();
|
assert!(heard && seen);peer.close().await;host.close().await.unwrap();router.lock().await.stop().await.unwrap();
|
||||||
@@ -267,7 +267,7 @@ async fn desktop_file_window_reattaches_without_replacing_peer(){
|
|||||||
}));}
|
}));}
|
||||||
})}));
|
})}));
|
||||||
let(tx,_rx)=mpsc::channel(256);
|
let(tx,_rx)=mpsc::channel(256);
|
||||||
let peer=Peer::new(RTCConfiguration::default(),&shape(),"app","ice","host",tx,viewer_settings).await.unwrap();
|
let peer=Peer::new(RTCConfiguration::default(),&shape(),"app","ice","host",tx,viewer_settings,super::clipboard::Bridge::new(false)).await.unwrap();
|
||||||
let mut gathered=peer.pc.gathering_complete_promise().await;peer.offer().await.unwrap();gathered.recv().await;
|
let mut gathered=peer.pc.gathering_complete_promise().await;peer.offer().await.unwrap();gathered.recv().await;
|
||||||
host.set_remote_description(peer.pc.local_description().await.unwrap()).await.unwrap();
|
host.set_remote_description(peer.pc.local_description().await.unwrap()).await.unwrap();
|
||||||
let answer=host.create_answer(None).await.unwrap();let mut gathered=host.gathering_complete_promise().await;
|
let answer=host.create_answer(None).await.unwrap();let mut gathered=host.gathering_complete_promise().await;
|
||||||
@@ -287,3 +287,55 @@ async fn desktop_file_window_reattaches_without_replacing_peer(){
|
|||||||
peer.close().await;host.close().await.unwrap();router.lock().await.stop().await.unwrap();
|
peer.close().await;host.close().await.unwrap();router.lock().await.stop().await.unwrap();
|
||||||
}).await.expect("desktop file reuse timed out");
|
}).await.expect("desktop file reuse timed out");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn hevc_track_uses_negotiated_payload_type_in_memory(){
|
||||||
|
tokio::time::timeout(Duration::from_secs(10),async {
|
||||||
|
use super::media::Codec;
|
||||||
|
use webrtc::{track::track_local::{track_local_static_rtp::TrackLocalStaticRTP,TrackLocal,TrackLocalWriter},
|
||||||
|
rtp_transceiver::rtp_codec::{RTCRtpCodecCapability,RTCRtpCodecParameters,RTPCodecType}};
|
||||||
|
let(viewer_settings,host_settings,router)=virtual_network().await;
|
||||||
|
// The host uses a different local PT; the answer binds its track to the viewer offer.
|
||||||
|
let mut engine=MediaEngine::default();
|
||||||
|
let capability=RTCRtpCodecCapability{mime_type:"video/H265".into(),clock_rate:90000,..Default::default()};
|
||||||
|
engine.register_codec(RTCRtpCodecParameters{capability:capability.clone(),payload_type:126,..Default::default()},RTPCodecType::Video).unwrap();
|
||||||
|
let host=Arc::new(APIBuilder::new().with_media_engine(engine).with_setting_engine(host_settings).build().new_peer_connection(RTCConfiguration::default()).await.unwrap());
|
||||||
|
let video=Arc::new(TrackLocalStaticRTP::new(capability,"video".into(),"test".into()));
|
||||||
|
host.add_track(video.clone() as Arc<dyn TrackLocal+Send+Sync>).await.unwrap();
|
||||||
|
host.on_data_channel(Box::new(|channel|Box::pin(async move {
|
||||||
|
if channel.label()=="CONTROL_DATA_CHANNEL" {
|
||||||
|
let weak=Arc::downgrade(&channel);
|
||||||
|
channel.on_open(Box::new(move||Box::pin(async move {
|
||||||
|
if let Some(channel)=weak.upgrade(){
|
||||||
|
let mut caps=Vec::new();for codec in [1,2]{let mut cap=Vec::new();wire::int(3,codec,&mut cap);wire::bytes(1,&cap,&mut caps);}
|
||||||
|
channel.send(&bytes::Bytes::from(wire::envelope(2,18,&caps))).await.unwrap();
|
||||||
|
// Model a host that declines 4:4:4 and encodes 4:2:0.
|
||||||
|
channel.send(&bytes::Bytes::from(wire::envelope(3,25,&[24,1]))).await.unwrap();
|
||||||
|
}
|
||||||
|
})));
|
||||||
|
}
|
||||||
|
})));
|
||||||
|
let(tx,_rx)=mpsc::channel(256);
|
||||||
|
let peer=Peer::new(RTCConfiguration::default(),&shape().video_preferences(false),"app","ice","host",tx,viewer_settings,super::clipboard::Bridge::new(false)).await.unwrap();
|
||||||
|
let mut gathered=peer.pc.gathering_complete_promise().await;peer.offer().await.unwrap();gathered.recv().await;
|
||||||
|
host.set_remote_description(peer.pc.local_description().await.unwrap()).await.unwrap();
|
||||||
|
let answer=host.create_answer(None).await.unwrap();
|
||||||
|
assert!(answer.sdp.contains("H265/90000"));assert!(!answer.sdp.contains("H264/90000"));
|
||||||
|
let mut gathered=host.gathering_complete_promise().await;host.set_local_description(answer).await.unwrap();gathered.recv().await;
|
||||||
|
peer.pc.set_remote_description(host.local_description().await.unwrap()).await.unwrap();
|
||||||
|
while peer.pc.connection_state()!=webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Connected {tokio::time::sleep(Duration::from_millis(10)).await;}
|
||||||
|
let mut au=vec![0,0,0,1,64,1,9,0,0,0,1,38,1];au.extend(vec![7;200]);
|
||||||
|
let payloads=crate::hevc::payload(80,&au).unwrap();let mut seq=0;let mut seen=false;
|
||||||
|
for timestamp in 0..100 {
|
||||||
|
for (i,payload) in payloads.iter().enumerate(){
|
||||||
|
video.write_rtp(&webrtc::rtp::packet::Packet{header:webrtc::rtp::header::Header{version:2,sequence_number:seq,timestamp:timestamp*3000,marker:i+1==payloads.len(),..Default::default()},payload:payload.clone()}).await.unwrap();seq=seq.wrapping_add(1);
|
||||||
|
}
|
||||||
|
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||||
|
if let Some(unit)=peer.desktop.units.pop(){assert_eq!(unit.codec,Codec::H265);assert_eq!(unit.data,au);seen=true;}
|
||||||
|
if seen && peer.observed.chroma.load(Ordering::Acquire)==1 {break}
|
||||||
|
}
|
||||||
|
assert!(seen);assert_eq!(peer.observed.encoder_codecs.load(Ordering::Acquire),3);
|
||||||
|
assert_eq!(peer.observed.chroma.load(Ordering::Acquire),1);
|
||||||
|
peer.close().await;host.close().await.unwrap();router.lock().await.stop().await.unwrap();
|
||||||
|
}).await.expect("in-memory H265 timed out");
|
||||||
|
}
|
||||||
@@ -16,6 +16,19 @@ pub fn inflate(data:&[u8])->Result<Vec<u8>,String>{
|
|||||||
let mut out=Vec::new();flate2::read::ZlibDecoder::new(data).take(16*1024*1024+1).read_to_end(&mut out).map_err(|_|"Invalid directory compression")?;
|
let mut out=Vec::new();flate2::read::ZlibDecoder::new(data).take(16*1024*1024+1).read_to_end(&mut out).map_err(|_|"Invalid directory compression")?;
|
||||||
if out.len()>16*1024*1024{return Err("Directory expansion limit".into())}Ok(out)
|
if out.len()>16*1024*1024{return Err("Directory expansion limit".into())}Ok(out)
|
||||||
}
|
}
|
||||||
|
/// Splits a remote target into (directory, file name). Windows hosts list paths with `\\` (C:\\Users\\...),
|
||||||
|
/// Linux hosts with `/`; the directory keeps the host's own separator style. No directory means home.
|
||||||
|
/// Download block limit. Our host sends 32 KiB blocks; the official Windows host sends larger ones
|
||||||
|
/// (a 64 KiB file arrived as one block), bounded by the 256 KiB SCTP message size.
|
||||||
|
const MAX_BLOCK:usize=256*1024;
|
||||||
|
fn split_remote(remote:&str)->Option<(&str,&str)>{
|
||||||
|
let cut=remote.rfind(['/','\\']);
|
||||||
|
let (dir,name)=match cut{Some(i)=>(&remote[..i],&remote[i+1..]),None=>("",remote)};
|
||||||
|
if name.is_empty(){return None}
|
||||||
|
// "/x" keeps the root; "C:\\x" keeps "C:\\" (a bare drive letter is not a directory).
|
||||||
|
let dir=match cut{None=>"~/",Some(i) if dir.is_empty()||dir.ends_with(':')=>&remote[..i+1],_=>dir};
|
||||||
|
Some((dir,name))
|
||||||
|
}
|
||||||
fn safe(path:&str)->Result<PathBuf,String>{
|
fn safe(path:&str)->Result<PathBuf,String>{
|
||||||
let normalized=path.replace('\\',"/");let p=Path::new(&normalized);
|
let normalized=path.replace('\\',"/");let p=Path::new(&normalized);
|
||||||
if p.as_os_str().is_empty() || p.components().any(|c|!matches!(c,std::path::Component::Normal(_))) || normalized.contains(':') {return Err("Unsafe remote relative path".into())}Ok(p.to_owned())
|
if p.as_os_str().is_empty() || p.components().any(|c|!matches!(c,std::path::Component::Normal(_))) || normalized.contains(':') {return Err("Unsafe remote relative path".into())}Ok(p.to_owned())
|
||||||
@@ -58,11 +71,22 @@ impl Files {
|
|||||||
let mut rx=self.rx.lock().await;self.canceled.store(false,Ordering::Release);
|
let mut rx=self.rx.lock().await;self.canceled.store(false,Ordering::Release);
|
||||||
let mut file=tokio::fs::File::open(local).await.map_err(|_|"Cannot open upload file")?;
|
let mut file=tokio::fs::File::open(local).await.map_err(|_|"Cannot open upload file")?;
|
||||||
let meta=file.metadata().await.map_err(|_|"Cannot stat upload file")?;if !meta.is_file(){return Err("Upload requires a regular file".into())}let size=meta.len();
|
let meta=file.metadata().await.map_err(|_|"Cannot stat upload file")?;if !meta.is_file(){return Err("Upload requires a regular file".into())}let size=meta.len();
|
||||||
let remote=Path::new(remote);let name=remote.file_name().and_then(|s|s.to_str()).ok_or("Invalid remote file name")?;safe(name)?;
|
let (parent,name)=split_remote(remote).ok_or("Invalid remote file name")?;safe(name)?;
|
||||||
let parent=remote.parent().and_then(|p|p.to_str()).filter(|p|!p.is_empty()).unwrap_or("~/");
|
|
||||||
let task=self.next.fetch_add(1,Ordering::Relaxed);
|
let task=self.next.fetch_add(1,Ordering::Relaxed);
|
||||||
let mut info=nested(1,name.as_bytes());wire::int(2,size,&mut info);
|
// GvPb_FileInfo {1 rel_path, 2 size, 3 modified_time (unix s)}; receive_request {.., 7 task_unique_id}.
|
||||||
|
// The official Windows host rejects (code 2) a request without the modified time / unique id.
|
||||||
|
let modified=meta.modified().ok().and_then(|t|t.duration_since(std::time::UNIX_EPOCH).ok()).map_or(0,|d|d.as_secs());
|
||||||
|
let mut info=nested(1,name.as_bytes());wire::int(2,size,&mut info);wire::int(3,modified,&mut info);
|
||||||
|
let now=std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default();
|
||||||
|
let unique=format!("{:08x}-{:04x}-4{:03x}-8{:03x}-{:012x}",now.as_secs() as u32,task&0xffff,(now.subsec_nanos()>>20)&0xfff,std::process::id()&0xfff,now.as_nanos()&0xffff_ffff_ffff);
|
||||||
let mut body=id(task,0);wire::bytes(2,parent.as_bytes(),&mut body);wire::bytes(3,&info,&mut body);wire::int(4,1,&mut body);
|
let mut body=id(task,0);wire::bytes(2,parent.as_bytes(),&mut body);wire::bytes(3,&info,&mut body);wire::int(4,1,&mut body);
|
||||||
|
// Official viewers with the list-compress feature send FileList{1 files} zlib-compressed in list_data (6);
|
||||||
|
// the official Windows host only registers files from there. Keep the inline copy for older hosts.
|
||||||
|
let mut list=Vec::new();wire::bytes(1,&info,&mut list);
|
||||||
|
let mut z=flate2::write::ZlibEncoder::new(Vec::new(),flate2::Compression::default());
|
||||||
|
std::io::Write::write_all(&mut z,&list).map_err(|_|"Cannot compress file list")?;
|
||||||
|
wire::bytes(6,&z.finish().map_err(|_|"Cannot compress file list")?,&mut body);
|
||||||
|
wire::bytes(7,unique.as_bytes(),&mut body);
|
||||||
let result=async {
|
let result=async {
|
||||||
let r=self.request(&mut rx,1,&body,1).await?;check(uint(&r,3))?;
|
let r=self.request(&mut rx,1,&body,1).await?;check(uint(&r,3))?;
|
||||||
let mut ask=id(task,1);wire::int(3,size,&mut ask);let r=self.request(&mut rx,3,&ask,3).await?;check(uint(&r,3))?;
|
let mut ask=id(task,1);wire::int(3,size,&mut ask);let r=self.request(&mut rx,3,&ask,3).await?;check(uint(&r,3))?;
|
||||||
@@ -120,7 +144,7 @@ impl Files {
|
|||||||
let block=uint(body,2);let chunk=blob(body,3).unwrap_or_default();
|
let block=uint(body,2);let chunk=blob(body,3).unwrap_or_default();
|
||||||
if let Some(d)=open.get_mut(&index){
|
if let Some(d)=open.get_mut(&index){
|
||||||
let expected=files[index as usize-1].1;
|
let expected=files[index as usize-1].1;
|
||||||
if chunk.len()>32*1024 || d.next.is_some_and(|n|block<n) || d.blocks.contains_key(&block)||d.blocks.len()>=32 {code=3}else{
|
if chunk.len()>MAX_BLOCK || d.next.is_some_and(|n|block<n) || d.blocks.contains_key(&block)||d.blocks.len()>=32 {code=3}else{
|
||||||
d.blocks.insert(block,chunk.to_vec());
|
d.blocks.insert(block,chunk.to_vec());
|
||||||
if d.next.is_none() && (block==0 || d.blocks.len()>=16) {d.next=d.blocks.keys().next().copied();}
|
if d.next.is_none() && (block==0 || d.blocks.len()>=16) {d.next=d.blocks.keys().next().copied();}
|
||||||
while let Some(chunk)=d.next.and_then(|n|d.blocks.remove(&n)){
|
while let Some(chunk)=d.next.and_then(|n|d.blocks.remove(&n)){
|
||||||
@@ -176,3 +200,14 @@ impl Files {
|
|||||||
result
|
result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#[cfg(test)]
|
||||||
|
mod split_tests{
|
||||||
|
#[test] fn remote_targets_keep_the_host_separator(){
|
||||||
|
assert_eq!(super::split_remote(r"C:\Users\Public\Downloads\a.bin"),Some((r"C:\Users\Public\Downloads","a.bin")));
|
||||||
|
assert_eq!(super::split_remote(r"C:\a.bin"),Some((r"C:\","a.bin")));
|
||||||
|
assert_eq!(super::split_remote("/tmp/a.bin"),Some(("/tmp","a.bin")));
|
||||||
|
assert_eq!(super::split_remote("/a.bin"),Some(("/","a.bin")));
|
||||||
|
assert_eq!(super::split_remote("a.bin"),Some(("~/","a.bin")));
|
||||||
|
assert_eq!(super::split_remote("/tmp/"),None);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,13 +13,21 @@ impl Drop for Route {fn drop(&mut self){self.routes.lock().unwrap().remove(&self
|
|||||||
impl Drop for PortMap {fn drop(&mut self){self.worker.abort();for(_,r)in self.rules.lock().unwrap().drain(){let _=r.stop.send(true);}self.routes.lock().unwrap().clear();}}
|
impl Drop for PortMap {fn drop(&mut self){self.worker.abort();for(_,r)in self.rules.lock().unwrap().drain(){let _=r.stop.send(true);}self.routes.lock().unwrap().clear();}}
|
||||||
impl PortMap {
|
impl PortMap {
|
||||||
pub fn new(io:Transport,mut rx:mpsc::Receiver<Packet>)->Self{
|
pub fn new(io:Transport,mut rx:mpsc::Receiver<Packet>)->Self{
|
||||||
let session=rand::random::<u64>();let routes:Routes=Arc::new(Mutex::new(HashMap::new()));let route=routes.clone();
|
// The official Windows host answers every frame with session_id 1 whatever the viewer sent (observed),
|
||||||
|
// and this project's host echoes it; one port-mapping peer carries one session, so use 1.
|
||||||
|
let session=1u64;let routes:Routes=Arc::new(Mutex::new(HashMap::new()));let route=routes.clone();
|
||||||
let(life,alive)=watch::channel(());
|
let(life,alive)=watch::channel(());
|
||||||
let worker=tokio::spawn(async move{
|
let worker=tokio::spawn(async move{
|
||||||
let _life=life;
|
let _life=life;
|
||||||
while let Some((channel,b))=rx.recv().await {
|
while let Some((channel,b))=rx.recv().await {
|
||||||
if channel!=FILE{continue}let Some(f)=blob(&b,27)else{continue};if uint(f,1)!=session{continue}
|
if channel!=FILE{continue}let Some(f)=blob(&b,27)else{continue};
|
||||||
let key=(uint(f,2),uint(f,3));let sender=route.lock().unwrap().get(&key).cloned();
|
let key=(uint(f,2),uint(f,3));let sender=route.lock().unwrap().get(&key).cloned();
|
||||||
|
// Diagnostics without payload: wire types per field and whether session/route matched.
|
||||||
|
if uint(f,1)!=session || sender.is_none() {
|
||||||
|
let kinds=crate::protocol::decode_wire(f).map(|x|x.iter().map(|w|(w.number,match w.value{crate::protocol::WireValue::Varint(_)=>"varint",crate::protocol::WireValue::Bytes(_)=>"bytes",_=>"fixed"})).collect::<Vec<_>>()).unwrap_or_default();
|
||||||
|
log::warn!("Port mapping frame unmatched: fields={kinds:?} session_match={} route_match={} type={}",uint(f,1)==session,sender.is_some(),uint(f,4));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if let Some(tx)=sender {if tx.try_send((uint(f,4),blob(f,5).unwrap_or_default().to_vec())).is_err(){route.lock().unwrap().remove(&key);}}
|
if let Some(tx)=sender {if tx.try_send((uint(f,4),blob(f,5).unwrap_or_default().to_vec())).is_err(){route.lock().unwrap().remove(&key);}}
|
||||||
}
|
}
|
||||||
route.lock().unwrap().clear();
|
route.lock().unwrap().clear();
|
||||||
@@ -51,7 +59,8 @@ impl PortMap {
|
|||||||
let io=io.clone();let host=host.clone();let mut cancel=stop.clone();let mut transport_closed=disconnected.clone();
|
let io=io.clone();let host=host.clone();let mut cancel=stop.clone();let mut transport_closed=disconnected.clone();
|
||||||
streams.spawn(async move {
|
streams.spawn(async move {
|
||||||
let result=tokio::select!{r=forward(&io,session,id,stream,socket,rx,&host,target_port)=>r,_=cancel.changed()=>Ok(()),_=transport_closed.changed()=>Err("Forwarding transport closed".into())};
|
let result=tokio::select!{r=forward(&io,session,id,stream,socket,rx,&host,target_port)=>r,_=cancel.changed()=>Ok(()),_=transport_closed.changed()=>Err("Forwarding transport closed".into())};
|
||||||
if result.is_err(){guard.status.lock().unwrap().failures+=1;}
|
// Reason only (fixed strings), never forwarded bytes or the target address.
|
||||||
|
if let Err(reason)=&result{guard.status.lock().unwrap().failures+=1;log::warn!("Port mapping stream ended: {reason}");}
|
||||||
let _=io.send(FILE,frame(session,id,stream,1,&[])).await;drop(guard);
|
let _=io.send(FILE,frame(session,id,stream,1,&[])).await;drop(guard);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -71,7 +80,8 @@ async fn forward(io:&Transport,session:u64,rule:u64,stream:u64,mut socket:TcpStr
|
|||||||
let syn=serde_json::json!({"version":1,"target_host":host,"target_port":port}).to_string();
|
let syn=serde_json::json!({"version":1,"target_host":host,"target_port":port}).to_string();
|
||||||
io.send(FILE,frame(session,rule,stream,0,syn.as_bytes())).await?;
|
io.send(FILE,frame(session,rule,stream,0,syn.as_bytes())).await?;
|
||||||
let ack=tokio::time::timeout(Duration::from_secs(12),rx.recv()).await.map_err(|_|"Forwarding SYN timeout")?.ok_or("Forwarding channel closed")?;
|
let ack=tokio::time::timeout(Duration::from_secs(12),rx.recv()).await.map_err(|_|"Forwarding SYN timeout")?.ok_or("Forwarding channel closed")?;
|
||||||
if ack.0!=3 || serde_json::from_slice::<serde_json::Value>(&ack.1).ok().and_then(|v|v["ok"].as_bool())!=Some(true){return Err("Forwarding target rejected".into())}
|
if ack.0!=3 {return Err(format!("Forwarding SYN answered with frame type {}",ack.0))}
|
||||||
|
if serde_json::from_slice::<serde_json::Value>(&ack.1).ok().and_then(|v|v["ok"].as_bool())!=Some(true){return Err("Forwarding target rejected".into())}
|
||||||
let mut buf=vec![0;16*1024];
|
let mut buf=vec![0;16*1024];
|
||||||
loop {tokio::select!{
|
loop {tokio::select!{
|
||||||
r=socket.read(&mut buf)=>{let n=r.map_err(|_|"Forwarding read failed")?;if n==0{break}io.send(FILE,frame(session,rule,stream,2,&buf[..n])).await?;},
|
r=socket.read(&mut buf)=>{let n=r.map_err(|_|"Forwarding read failed")?;if n==0{break}io.send(FILE,frame(session,rule,stream,2,&buf[..n])).await?;},
|
||||||
|
|||||||
@@ -40,7 +40,9 @@ impl Terminal {
|
|||||||
let worker=tokio::spawn(async move {
|
let worker=tokio::spawn(async move {
|
||||||
let mut decoder=Decoder::default();
|
let mut decoder=Decoder::default();
|
||||||
while let Some((channel,data))=rx.recv().await {
|
while let Some((channel,data))=rx.recv().await {
|
||||||
if channel==CONTROL {
|
// Terminal RPC answers (check_env) come on CONTROL or, from the official Mac host, on BINARY
|
||||||
|
// next to TERM frames (as this project's host does).
|
||||||
|
if channel==CONTROL || (channel==BINARY && !data.starts_with(b"TERM")) {
|
||||||
if let Some(r)=blob(&data,22) {if let (Some(h),Some(body))=(blob(r,1),blob(r,24)) {
|
if let Some(r)=blob(&data,22) {if let (Some(h),Some(body))=(blob(r,1),blob(r,24)) {
|
||||||
if let Some(t)=p.lock().unwrap().remove(&(24,uint(h,1))) {let _=t.send(Ok(body.to_vec()));}
|
if let Some(t)=p.lock().unwrap().remove(&(24,uint(h,1))) {let _=t.send(Ok(body.to_vec()));}
|
||||||
}}continue
|
}}continue
|
||||||
@@ -57,7 +59,9 @@ impl Terminal {
|
|||||||
if let Some(event)=event {if tx.try_send(event).is_err(){overflow=true;break}}
|
if let Some(event)=event {if tx.try_send(event).is_err(){overflow=true;break}}
|
||||||
let key=if f.kind==OPEN_RESP {(OPEN_RESP,0)}else{(f.kind,f.session as u64)};
|
let key=if f.kind==OPEN_RESP {(OPEN_RESP,0)}else{(f.kind,f.session as u64)};
|
||||||
let payload=if f.kind==OPEN_RESP {
|
let payload=if f.kind==OPEN_RESP {
|
||||||
serde_json::from_slice::<Value>(&f.payload).ok().filter(Value::is_object).map(|mut v|{v["session_id"]=json!(f.session);v.to_string().into_bytes()}).unwrap_or(f.payload)
|
// Our host puts the new session in the frame header; the official Windows host answers with header 0,
|
||||||
|
// so only a nonzero header overrides the JSON body.
|
||||||
|
serde_json::from_slice::<Value>(&f.payload).ok().filter(Value::is_object).map(|mut v|{if f.session!=0{v["session_id"]=json!(f.session);}v.to_string().into_bytes()}).unwrap_or(f.payload)
|
||||||
}else{f.payload};
|
}else{f.payload};
|
||||||
if let Some(t)=p.lock().unwrap().remove(&key){let _=t.send(Ok(payload));}
|
if let Some(t)=p.lock().unwrap().remove(&key){let _=t.send(Ok(payload));}
|
||||||
}
|
}
|
||||||
@@ -79,8 +83,17 @@ impl Terminal {
|
|||||||
}
|
}
|
||||||
pub async fn check_env(&self)->Result<(),String>{
|
pub async fn check_env(&self)->Result<(),String>{
|
||||||
let id=self.rpc.fetch_add(1,Ordering::Relaxed);let mut r=nested(1,&ints(&[(1,id)]));wire::bytes(28,&[],&mut r);
|
let id=self.rpc.fetch_add(1,Ordering::Relaxed);let mut r=nested(1,&ints(&[(1,id)]));wire::bytes(28,&[],&mut r);
|
||||||
let b=self.request((24,id),CONTROL,wire::envelope(id,21,&r)).await?;
|
// The official Windows host's rpc_main.proto has no check_env (requests end at 25): no answer means
|
||||||
if uint(&b,1)!=0 {return Err("Terminal environment unavailable".into())}Ok(())
|
// "not implemented", not "unavailable"; go on and let OPEN report availability.
|
||||||
|
match tokio::time::timeout(std::time::Duration::from_secs(3),self.request((24,id),CONTROL,wire::envelope(id,21,&r))).await{
|
||||||
|
Err(_)=>Ok(()),
|
||||||
|
Ok(b)=>{let b=b?;let code=uint(&b,1);
|
||||||
|
if code!=0 {
|
||||||
|
let message=blob(&b,2).map(|m|String::from_utf8_lossy(m).chars().take(80).collect::<String>()).unwrap_or_default();
|
||||||
|
log::info!("Terminal check_env: code={code} message={message:?}");
|
||||||
|
return Err("Terminal environment unavailable".into())
|
||||||
|
}Ok(())}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub async fn list_sessions(&self)->Result<Vec<Value>,String>{
|
pub async fn list_sessions(&self)->Result<Vec<Value>,String>{
|
||||||
let id=self.next.fetch_add(1,Ordering::Relaxed);
|
let id=self.next.fetch_add(1,Ordering::Relaxed);
|
||||||
@@ -94,10 +107,16 @@ impl Terminal {
|
|||||||
self.open_session(id,cols,rows).await
|
self.open_session(id,cols,rows).await
|
||||||
}
|
}
|
||||||
async fn open_session(&self,id:u32,cols:u16,rows:u16)->Result<u32,String>{
|
async fn open_session(&self,id:u32,cols:u16,rows:u16)->Result<u32,String>{
|
||||||
// Host TerminalConn handles OPEN(nonzero) as reattachment.
|
// Host TerminalConn handles OPEN(nonzero) as reattachment. The official Windows host rejects an OPEN
|
||||||
let b=self.request((OPEN_RESP,0),BINARY,encode(OPEN,id,json!({"cols":cols.max(1),"rows":rows.max(1)}).to_string().as_bytes())).await?;
|
// without "option" ("terminal open: missing 'option' field", kError 1004); "none" is its plain mode.
|
||||||
|
let b=self.request((OPEN_RESP,0),BINARY,encode(OPEN,id,json!({"cols":cols.max(1),"rows":rows.max(1),"option":"none"}).to_string().as_bytes())).await?;
|
||||||
let v:Value=serde_json::from_slice(&b).map_err(|_|"Invalid terminal open response")?;
|
let v:Value=serde_json::from_slice(&b).map_err(|_|"Invalid terminal open response")?;
|
||||||
if v["code"].as_i64()!=Some(0){return Err("Terminal open rejected".into())}v["session_id"].as_u64().and_then(|n|u32::try_from(n).ok()).filter(|n|*n!=0).ok_or("Invalid terminal session id".into())
|
if v["code"].as_i64()!=Some(0){return Err("Terminal open rejected".into())}
|
||||||
|
let id=v["session_id"].as_u64().or_else(||v["session_id"].as_str().and_then(|s|s.parse().ok())).and_then(|n|u32::try_from(n).ok()).filter(|n|*n!=0);
|
||||||
|
// The official Windows host answers {"code":0,"message"} with header 0 and keeps one terminal per
|
||||||
|
// connection addressed as session 0 (it then prints "[terminal] resumed existing session").
|
||||||
|
if id.is_none(){log::info!("Terminal open response without a session id: keys={:?}; using session 0",v.as_object().map(|o|o.keys().collect::<Vec<_>>()));}
|
||||||
|
Ok(id.unwrap_or(0))
|
||||||
}
|
}
|
||||||
pub async fn write(&self,id:u32,data:&[u8])->Result<(),String>{for b in data.chunks(16*1024){self.io.send(BINARY,encode(STDIN,id,b)).await?}Ok(())}
|
pub async fn write(&self,id:u32,data:&[u8])->Result<(),String>{for b in data.chunks(16*1024){self.io.send(BINARY,encode(STDIN,id,b)).await?}Ok(())}
|
||||||
pub async fn resize(&self,id:u32,cols:u16,rows:u16)->Result<(),String>{self.io.send(BINARY,encode(RESIZE,id,json!({"cols":cols.max(1),"rows":rows.max(1)}).to_string().as_bytes())).await}
|
pub async fn resize(&self,id:u32,cols:u16,rows:u16)->Result<(),String>{self.io.send(BINARY,encode(RESIZE,id,json!({"cols":cols.max(1),"rows":rows.max(1)}).to_string().as_bytes())).await}
|
||||||
|
|||||||
@@ -195,6 +195,15 @@ pub async fn run(credentials: impl Fn() -> Result<crate::auth::Credentials, Stri
|
|||||||
Err(_) => toast("电源指令失败:无法启动会话进程"),
|
Err(_) => toast("电源指令失败:无法启动会话进程"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
["connect", id, "unbind"] => {
|
||||||
|
if let Some(s)=sessions.remove(*id){s.stop().await;}
|
||||||
|
let result=match credentials(){
|
||||||
|
Ok(creds)=>super::api::Api::new(crate::auth::UUAuthClient::new(creds),crate::auth::BASE_URL).unbind(id,false).await.map_err(|e|e.to_string()),
|
||||||
|
Err(e)=>Err(e),
|
||||||
|
};
|
||||||
|
match result {Ok(())=>toast("设备已从账号删除"),Err(e)=>toast(&format!("删除设备失败:{e}"))}
|
||||||
|
next_refresh=Instant::now();
|
||||||
|
}
|
||||||
["connect", _, kind] => log::info!("Viewer UI: {kind} is not supported yet"),
|
["connect", _, kind] => log::info!("Viewer UI: {kind} is not supported yet"),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|||||||
+76
-9
@@ -52,15 +52,30 @@ impl Shape {
|
|||||||
}
|
}
|
||||||
pub fn validate_session(&self)->Result<(),String> {
|
pub fn validate_session(&self)->Result<(),String> {
|
||||||
self.validate()?;
|
self.validate()?;
|
||||||
// H264/420 receive only. streamer_data video_codec_capability is sent by the official Mac
|
if self.codecs.iter().any(|c|![1,2].contains(&c.video_codec) || ![1,3].contains(&c.chroma_sampling))
|
||||||
// viewer (8 entries observed live), so H264/420 entries are allowed there too.
|
|| (self.capture_type==1 && self.decoders.is_empty()) {
|
||||||
if self.decoders.iter().any(|d|d.codec!=1 || d.chroma!=1) || self.codecs.iter().any(|c|c.video_codec!=1 || c.chroma_sampling!=1)
|
return Err("Sessions require supported decoder and codec capabilities".into());
|
||||||
|| (self.capture_type==1 && (self.chroma!=1 || self.decoders.is_empty())) {
|
|
||||||
return Err("Sessions require H264/420 decoder and codec capabilities".into());
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn connect_options(&self, device:&str) -> Result<Vec<u8>,String> {
|
/// Advertise software decoding of both 8-bit chroma formats. Keep caller size/fps limits.
|
||||||
|
/// The explicit chroma request is sent on CONTROL, so initial capture stays 4:2:0.
|
||||||
|
pub fn video_preferences(&self,h264_only:bool)->Self {
|
||||||
|
let mut shape=self.clone();
|
||||||
|
if shape.capture_type==1 {
|
||||||
|
shape.chroma=1;
|
||||||
|
shape.decoders.clear();
|
||||||
|
for codec in if h264_only {&[1][..]}else{&[2,1][..]} {
|
||||||
|
let base=self.decoders.iter().find(|d|d.codec==*codec).or_else(||self.decoders.first());
|
||||||
|
if let Some(base)=base {for chroma in [1,3] {
|
||||||
|
let mut d=base.clone();d.codec=*codec;d.chroma=chroma;shape.decoders.push(d);
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if h264_only {shape.decoders.retain(|d|d.codec==1);shape.codecs.retain(|c|c.video_codec==1);}
|
||||||
|
shape
|
||||||
|
}
|
||||||
|
pub fn connect_options(&self, device:&str, clipboard:bool) -> Result<Vec<u8>,String> {
|
||||||
self.validate()?;
|
self.validate()?;
|
||||||
let mut out=Vec::new(); int(1,self.capture_type.into(),&mut out);
|
let mut out=Vec::new(); int(1,self.capture_type.into(),&mut out);
|
||||||
if [1,8].contains(&self.capture_type) { int(2,u64::MAX,&mut out); }
|
if [1,8].contains(&self.capture_type) { int(2,u64::MAX,&mut out); }
|
||||||
@@ -85,7 +100,7 @@ impl Shape {
|
|||||||
int(8,self.platform.into(),&mut out); bytes(9,device.as_bytes(),&mut out); int(10,1,&mut out);
|
int(8,self.platform.into(),&mut out); bytes(9,device.as_bytes(),&mut out); int(10,1,&mut out);
|
||||||
if self.capture_type==1 {
|
if self.capture_type==1 {
|
||||||
let mut flags=Vec::new(); for [field,version] in &self.feature_flag { if *field!=8 {int((*field).into(),(*version).into(),&mut flags);} }
|
let mut flags=Vec::new(); for [field,version] in &self.feature_flag { if *field!=8 {int((*field).into(),(*version).into(),&mut flags);} }
|
||||||
if super::clipboard::available(){int(8,3,&mut flags);}
|
if clipboard{int(8,3,&mut flags);}
|
||||||
bytes(11,&flags,&mut out);
|
bytes(11,&flags,&mut out);
|
||||||
}
|
}
|
||||||
if self.capture_type!=5 { bytes(12,self.client_version.as_bytes(),&mut out); }
|
if self.capture_type!=5 { bytes(12,self.client_version.as_bytes(),&mut out); }
|
||||||
@@ -128,7 +143,8 @@ pub fn echo_request_seq(data:&[u8]) -> Option<u64> {
|
|||||||
let fields=decode_wire(data).ok()?;
|
let fields=decode_wire(data).ok()?;
|
||||||
let seq=fields.iter().find_map(|f|match (f.number,&f.value){(1,WireValue::Varint(v))=>Some(*v),_=>None})?;
|
let seq=fields.iter().find_map(|f|match (f.number,&f.value){(1,WireValue::Varint(v))=>Some(*v),_=>None})?;
|
||||||
fields.iter().find_map(|f|match (f.number,&f.value) {
|
fields.iter().find_map(|f|match (f.number,&f.value) {
|
||||||
(3,WireValue::Bytes(action))=>decode_wire(action).ok()?.iter().any(|f|f.number==1 && f.value==WireValue::Varint(0)).then_some(seq),
|
// proto3 omits the default action 0, so a SimpleAction without field 1 is an echo request too.
|
||||||
|
(3,WireValue::Bytes(action))=>decode_wire(action).ok()?.iter().find_map(|f|match (f.number,&f.value){(1,WireValue::Varint(v))=>Some(*v),_=>None}).unwrap_or(0).eq(&0).then_some(seq),
|
||||||
_=>None,
|
_=>None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -160,7 +176,7 @@ mod tests {
|
|||||||
let mut s=shape();
|
let mut s=shape();
|
||||||
for capture in [1,5,8,9] {
|
for capture in [1,5,8,9] {
|
||||||
s.capture_type=capture;
|
s.capture_type=capture;
|
||||||
let b=s.connect_options("synthetic-device").unwrap(); let f=decode_wire(&b).unwrap();
|
let b=s.connect_options("synthetic-device",false).unwrap(); let f=decode_wire(&b).unwrap();
|
||||||
let numbers:Vec<u32>=f.iter().map(|f|f.number).collect();
|
let numbers:Vec<u32>=f.iter().map(|f|f.number).collect();
|
||||||
let expected=match capture {1=>vec![1,2,3,4,4,4,4,6,6,7,8,9,10,11,12],5=>vec![1,3,4,4,4,4,6,6,7,8,9,10],8=>vec![1,2,8,9,10,12],_=>vec![1,8,9,10,12]};
|
let expected=match capture {1=>vec![1,2,3,4,4,4,4,6,6,7,8,9,10,11,12],5=>vec![1,3,4,4,4,4,6,6,7,8,9,10],8=>vec![1,2,8,9,10,12],_=>vec![1,8,9,10,12]};
|
||||||
assert_eq!(numbers,expected);
|
assert_eq!(numbers,expected);
|
||||||
@@ -181,3 +197,54 @@ mod tests {
|
|||||||
assert!(!echo_matches(&echo_request(7),7)); assert!(!echo_matches(&[255],7));
|
assert!(!echo_matches(&echo_request(7),7)); assert!(!echo_matches(&[255],7));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// CaptureSettingRequest field 7 = ChromaFormat 3, wrapped in RPC 21/case 2.
|
||||||
|
pub fn request_chroma444()->Vec<u8>{
|
||||||
|
let mut header=Vec::new();int(1,2_000_000,&mut header);
|
||||||
|
let mut setting=Vec::new();int(7,3,&mut setting);
|
||||||
|
let mut rpc=Vec::new();bytes(1,&header,&mut rpc);bytes(2,&setting,&mut rpc);
|
||||||
|
envelope(2_000_000,21,&rpc)
|
||||||
|
}
|
||||||
|
/// Message 18 is an encoder capability LIST, not the chosen codec; RTP's negotiated PT is authoritative.
|
||||||
|
/// Message 25 reports actual encoded chroma, which may fall back to 4:2:0.
|
||||||
|
pub fn media_control(data:&[u8])->(Option<u32>,Option<u32>){
|
||||||
|
let mut codecs=None;let mut chroma=None;
|
||||||
|
if let Ok(fields)=decode_wire(data){for field in fields {
|
||||||
|
if let WireValue::Bytes(body)=field.value {match field.number {
|
||||||
|
18=>{if let Ok(caps)=decode_wire(body){let mut mask=0;for cap in caps {if let (1,WireValue::Bytes(cap))=(cap.number,cap.value){
|
||||||
|
match super::tools::uint(cap,3){1=>mask|=1,2=>mask|=2,_=>{}}
|
||||||
|
}}codecs=Some(mask);}},
|
||||||
|
25=>{let value=super::tools::uint(body,3);if [1,3].contains(&value){chroma=Some(value as u32);}},
|
||||||
|
_=>{},
|
||||||
|
}}
|
||||||
|
}}(codecs,chroma)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)] mod video_tests {
|
||||||
|
use super::*;
|
||||||
|
#[test] fn decoder_caps_cover_both_codecs_and_chroma_and_force_h264(){
|
||||||
|
let original:Shape=serde_json::from_str(include_str!("../../tests/fixtures/viewer/session-shape.json")).unwrap();
|
||||||
|
for force in [false,true] {
|
||||||
|
let s=original.video_preferences(force);s.validate_session().unwrap();assert_eq!(s.chroma,1);
|
||||||
|
let options=s.connect_options("synthetic-device",false).unwrap();
|
||||||
|
let mut caps=Vec::new();
|
||||||
|
for f in decode_wire(&options).unwrap(){if let (4,WireValue::Bytes(cap))=(f.number,f.value){
|
||||||
|
assert_eq!(super::super::tools::uint(cap,1),30);
|
||||||
|
assert_eq!(super::super::tools::uint(cap,3),1280);assert_eq!(super::super::tools::uint(cap,4),720);
|
||||||
|
caps.push((super::super::tools::uint(cap,2),super::super::tools::uint(cap,5)));
|
||||||
|
}}
|
||||||
|
assert_eq!(caps,if force{vec![(1,1),(1,3)]}else{vec![(2,1),(2,3),(1,1),(1,3)]});
|
||||||
|
assert!(s.video_preferences(force).connect_options("synthetic-device",false).unwrap()==options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[test] fn chroma_request_and_actual_sync_are_independent_of_encoder_list(){
|
||||||
|
let request=request_chroma444();let rpc=super::super::tools::blob(&request,21).unwrap();
|
||||||
|
let setting=super::super::tools::blob(rpc,2).unwrap();assert_eq!(super::super::tools::uint(setting,7),3);
|
||||||
|
assert_eq!(decode_wire(setting).unwrap().len(),1);
|
||||||
|
let mut caps=Vec::new();for codec in [1,2] {let mut cap=Vec::new();int(3,codec,&mut cap);bytes(1,&cap,&mut caps);}
|
||||||
|
let mut msg=envelope(1,18,&caps);let mut sync=Vec::new();int(3,1,&mut sync);bytes(25,&sync,&mut msg);
|
||||||
|
assert_eq!(media_control(&msg),(Some(3),Some(1)));
|
||||||
|
assert_eq!(media_control(&envelope(2,25,&[24,3])),(None,Some(3)));
|
||||||
|
assert_eq!(media_control(&[255]),(None,None));
|
||||||
|
}
|
||||||
|
}
|
||||||
+21
-5
@@ -4,6 +4,7 @@
|
|||||||
#[path = "config.rs"] mod config;
|
#[path = "config.rs"] mod config;
|
||||||
#[path = "protocol.rs"] mod protocol;
|
#[path = "protocol.rs"] mod protocol;
|
||||||
mod viewer;
|
mod viewer;
|
||||||
|
#[cfg(test)] #[path="hevc.rs"] mod hevc;
|
||||||
use viewer::clipboard;
|
use viewer::clipboard;
|
||||||
#[path="clipboard_protocol.rs"] mod clipboard_protocol;
|
#[path="clipboard_protocol.rs"] mod clipboard_protocol;
|
||||||
#[path="clipboard_rich.rs"] mod clipboard_rich;
|
#[path="clipboard_rich.rs"] mod clipboard_rich;
|
||||||
@@ -12,7 +13,7 @@ use viewer::clipboard;
|
|||||||
#[cfg(test)] #[path="initialization.rs"] mod initialization;
|
#[cfg(test)] #[path="initialization.rs"] mod initialization;
|
||||||
use viewer::{api::Api,rtc::ReadyPolicy,session::{Options,Session},wire::Shape};
|
use viewer::{api::Api,rtc::ReadyPolicy,session::{Options,Session},wire::Shape};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
const HELP:&str="Experimental native viewer.\n uuremote-viewer ui (GTK window: sign-in, devices, sessions) Desktop UI requires viewer-sdl (decodes with the ffmpeg CLI, or FFmpeg 6.x libraries with viewer-ffmpeg).\n uuremote-viewer power DEVICE reboot|shutdown\n uuremote-viewer terminal DEVICE [SESSION]\n uuremote-viewer files DEVICE ls PATH | get REMOTE LOCAL | put LOCAL REMOTE\n uuremote-viewer portmap DEVICE LOCAL_PORT HOST PORT\n uuremote-viewer files-ui|portmap-ui|terminal-ui DEVICE NAME (GTK tool windows)\n uuremote-viewer login [--no-gui]\n uuremote-viewer devices METHOD ARRAY_POINTER ID_POINTER NAME_POINTER\n uuremote-viewer assist CONNECT_ID (验证码从 stdin 读取一行)\n uuremote-viewer connect DEVICE true|false SHAPE_JSON echo|manual raw|legacy [--view-only]\nAudio: UU_VIEWER_AUDIO=0 disables playback. UU_VIEWER_VIEW_ONLY=1 disables desktop input.\nDevice-list method/JSON pointers and compatibility shape require verified operator configuration.\nQR login reuses the sibling uuremote-server login command. Never run login in a captured log.\nEcho readiness is transport observation only; no sendVideoTrack RPC is emitted.";
|
const HELP:&str="Experimental native viewer.\n uuremote-viewer ui (GTK window: sign-in, devices, sessions) Desktop UI requires viewer-sdl (decodes with the ffmpeg CLI, or FFmpeg 6.x libraries with viewer-ffmpeg).\n uuremote-viewer power DEVICE reboot|shutdown\n uuremote-viewer unbind DEVICE [--shared] (从账号删除设备 / remove a device from the account)\n uuremote-viewer terminal DEVICE [SESSION]\n uuremote-viewer files DEVICE ls PATH | get REMOTE LOCAL | put LOCAL REMOTE\n uuremote-viewer portmap DEVICE LOCAL_PORT HOST PORT\n uuremote-viewer files-ui|portmap-ui|terminal-ui DEVICE NAME (GTK tool windows)\n uuremote-viewer login [--no-gui]\n uuremote-viewer devices METHOD ARRAY_POINTER ID_POINTER NAME_POINTER\n uuremote-viewer assist CONNECT_ID (验证码从 stdin 读取一行)\n uuremote-viewer connect DEVICE true|false SHAPE_JSON echo|manual raw|legacy [--view-only]\nVideo: UU_VIEWER_CODEC=h264 forces H.264; UU_VIEWER_CHROMA=444 requests true color (default 4:2:0).\nAudio: UU_VIEWER_AUDIO=0 disables playback. UU_VIEWER_VIEW_ONLY=1 disables desktop input.\nDevice-list method/JSON pointers and compatibility shape require verified operator configuration.\nQR login reuses the sibling uuremote-server login command. Never run login in a captured log.\nEcho readiness is transport observation only; no sendVideoTrack RPC is emitted.";
|
||||||
const DEFAULT_SHAPE:&str=r#"{"capture_type":1,"platform":3,"client_version":"4.42.0","width":1920,"height":1080,"fps":60,"chroma":1,
|
const DEFAULT_SHAPE:&str=r#"{"capture_type":1,"platform":3,"client_version":"4.42.0","width":1920,"height":1080,"fps":60,"chroma":1,
|
||||||
"decoders":[{"fps":60,"codec":1,"width":3840,"height":2160,"chroma":1}],"display_modes":[[1920,1080,60]],
|
"decoders":[{"fps":60,"codec":1,"width":3840,"height":2160,"chroma":1}],"display_modes":[[1920,1080,60]],
|
||||||
"displays":[{"id":1,"fps":60,"type":0,"hdr":0}],
|
"displays":[{"id":1,"fps":60,"type":0,"hdr":0}],
|
||||||
@@ -54,6 +55,10 @@ async fn run(args:&[String])->Result<(),String> {
|
|||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Some("unbind") if (args.len()==2 || (args.len()==3 && args[2]=="--shared")) && !args[1].is_empty() => {
|
||||||
|
api()?.unbind(&args[1],args.len()==3).await.map_err(|e|format!("删除设备失败:{e}"))?;
|
||||||
|
println!("已从账号删除设备");Ok(())
|
||||||
|
}
|
||||||
Some("power")=>power_command(args).await,
|
Some("power")=>power_command(args).await,
|
||||||
Some("terminal-local") if args.len()==2 || args.len()==3=>{
|
Some("terminal-local") if args.len()==2 || args.len()==3=>{
|
||||||
let session=if args.len()==3{Some(args[2].parse::<u32>().ok().filter(|n|*n!=0).ok_or("Invalid terminal session id")?)}else{None};
|
let session=if args.len()==3{Some(args[2].parse::<u32>().ok().filter(|n|*n!=0).ok_or("Invalid terminal session id")?)}else{None};
|
||||||
@@ -88,11 +93,22 @@ async fn desktop_command(args:&[String],code:Option<String>)->Result<(),String>
|
|||||||
}else{None};
|
}else{None};
|
||||||
let creds=credentials()?;
|
let creds=credentials()?;
|
||||||
let api=Api::new(auth::UUAuthClient::new(creds.clone()),auth::BASE_URL);
|
let api=Api::new(auth::UUAuthClient::new(creds.clone()),auth::BASE_URL);
|
||||||
|
let account_connection=code.is_none();
|
||||||
let room=match code {
|
let room=match code {
|
||||||
Some(code)=>api.join_share(&args[1],&code).await,
|
Some(code)=>api.join_share(&args[1],&code).await,
|
||||||
None=>api.join(&args[1],force,true).await,
|
None=>api.join(&args[1],force,true).await,
|
||||||
}.map_err(|e|e.to_string())?;
|
}.map_err(|e|e.to_string())?;
|
||||||
let options=Options{timeout:Duration::from_secs(12),legacy_prefix:legacy,ready_policy:policy};
|
let target_mac=if shape.capture_type==1 {api.target_is_mac(&room,account_connection.then_some(args[1].as_str())).await} else {Err(viewer::api::Error::UnknownKeyboardPlatform)};
|
||||||
|
#[cfg(feature="viewer-sdl")]
|
||||||
|
if let Some((window,_))=desktop.as_mut().filter(|_|!view_only){
|
||||||
|
window.set_mac(*target_mac.as_ref().map_err(|e|e.to_string())?);
|
||||||
|
}
|
||||||
|
#[cfg(feature="viewer-sdl")]
|
||||||
|
let mut clipboard=desktop.as_ref().map(|(window,_)|window.clipboard_bridge()).unwrap_or_else(||clipboard::Bridge::new(false));
|
||||||
|
#[cfg(not(feature="viewer-sdl"))]
|
||||||
|
let mut clipboard=clipboard::Bridge::new(shape.capture_type==1);
|
||||||
|
clipboard.set_target_mac(target_mac.ok());
|
||||||
|
let options=Options{timeout:Duration::from_secs(12),legacy_prefix:legacy,ready_policy:policy,clipboard};
|
||||||
let mut session=Session::open(&room,&shape,&creds.device_id,&creds.client_id,options,Default::default()).await?;
|
let mut session=Session::open(&room,&shape,&creds.device_id,&creds.client_id,options,Default::default()).await?;
|
||||||
#[cfg(feature="viewer-sdl")]
|
#[cfg(feature="viewer-sdl")]
|
||||||
let result=if let Some((window,decoder))=desktop.as_mut(){viewer::sdl::run(&mut session,window,decoder.as_mut(),view_only).await}else{session.run().await};
|
let result=if let Some((window,decoder))=desktop.as_mut(){viewer::sdl::run(&mut session,window,decoder.as_mut(),view_only).await}else{session.run().await};
|
||||||
@@ -112,7 +128,7 @@ mod cli_tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn invalid_commands_never_fall_through_to_login_or_host_service() {
|
async fn invalid_commands_never_fall_through_to_login_or_host_service() {
|
||||||
for args in [vec!["invalid"],vec!["power"],vec!["power","device","logout"],vec!["power","device","reboot","extra"],vec!["login","--invalid"],vec!["connect"],vec!["assist"],vec!["assist","id","forbidden-code-argument"],vec!["terminal"],vec!["terminal","device","extra"],vec!["terminal","device","0"],vec!["terminal","device","4294967296"],vec!["terminal-ui","device"],vec!["files","device","bad","path"],vec!["portmap","device","bad","host","80"],vec!["portmap","device","8000","host","0"],vec!["devices","DELETE","/data","/id","/name"]] {
|
for args in [vec!["invalid"],vec!["power"],vec!["power","device","logout"],vec!["power","device","reboot","extra"],vec!["login","--invalid"],vec!["connect"],vec!["assist"],vec!["assist","id","forbidden-code-argument"],vec!["terminal"],vec!["terminal","device","extra"],vec!["terminal","device","0"],vec!["terminal","device","4294967296"],vec!["terminal-ui","device"],vec!["files","device","bad","path"],vec!["portmap","device","bad","host","80"],vec!["portmap","device","8000","host","0"],vec!["devices","DELETE","/data","/id","/name"],vec!["unbind"],vec!["unbind",""],vec!["unbind","device","--other"],vec!["unbind","device","--shared","extra"]] {
|
||||||
assert!(run(&args.into_iter().map(str::to_owned).collect::<Vec<_>>()).await.is_err());
|
assert!(run(&args.into_iter().map(str::to_owned).collect::<Vec<_>>()).await.is_err());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -139,7 +155,7 @@ async fn tool_command(args:&[String])->Result<(),String>{
|
|||||||
let creds=credentials()?;let api=Api::new(auth::UUAuthClient::new(creds.clone()),auth::BASE_URL);
|
let creds=credentials()?;let api=Api::new(auth::UUAuthClient::new(creds.clone()),auth::BASE_URL);
|
||||||
let room=api.join(&args[1],false,true).await.map_err(|e|e.to_string())?;
|
let room=api.join(&args[1],false,true).await.map_err(|e|e.to_string())?;
|
||||||
// Match the existing UI connection framing (9bb42d5); the host tolerates this prefix.
|
// Match the existing UI connection framing (9bb42d5); the host tolerates this prefix.
|
||||||
let options=Options{timeout:Duration::from_secs(12),legacy_prefix:true,ready_policy:ReadyPolicy::EchoOnly};
|
let options=Options{timeout:Duration::from_secs(12),legacy_prefix:true,ready_policy:ReadyPolicy::EchoOnly,clipboard:clipboard::Bridge::new(false)};
|
||||||
let mut session=Session::open(&room,&shape,&creds.device_id,&creds.client_id,options,Default::default()).await?;
|
let mut session=Session::open(&room,&shape,&creds.device_id,&creds.client_id,options,Default::default()).await?;
|
||||||
let result=async{
|
let result=async{
|
||||||
let(io,rx)=viewer::tools::attach(&session.peer)?;
|
let(io,rx)=viewer::tools::attach(&session.peer)?;
|
||||||
@@ -168,7 +184,7 @@ async fn power_command(args:&[String])->Result<(),String>{
|
|||||||
shape.capture_type=5;shape.feature_flag.clear();
|
shape.capture_type=5;shape.feature_flag.clear();
|
||||||
let creds=credentials()?;let api=Api::new(auth::UUAuthClient::new(creds.clone()),auth::BASE_URL);
|
let creds=credentials()?;let api=Api::new(auth::UUAuthClient::new(creds.clone()),auth::BASE_URL);
|
||||||
let room=api.join(&args[1],false,true).await.map_err(|e|e.to_string())?;
|
let room=api.join(&args[1],false,true).await.map_err(|e|e.to_string())?;
|
||||||
let options=Options{timeout:Duration::from_secs(12),legacy_prefix:true,ready_policy:ReadyPolicy::EchoOnly};
|
let options=Options{timeout:Duration::from_secs(12),legacy_prefix:true,ready_policy:ReadyPolicy::EchoOnly,clipboard:clipboard::Bridge::new(false)};
|
||||||
let mut session=Session::open(&room,&shape,&creds.device_id,&creds.client_id,options,Default::default()).await?;
|
let mut session=Session::open(&room,&shape,&creds.device_id,&creds.client_id,options,Default::default()).await?;
|
||||||
let result=viewer::power::run(&mut session,action).await;
|
let result=viewer::power::run(&mut session,action).await;
|
||||||
session.close().await;
|
session.close().await;
|
||||||
|
|||||||
+27
-1
@@ -1041,6 +1041,21 @@ impl WebRtcSession {
|
|||||||
if let Ok(txt) = std::str::from_utf8(&msg.data) {
|
if let Ok(txt) = std::str::from_utf8(&msg.data) {
|
||||||
log::debug!("DataChannel [{}] received UTF-8 data: {} bytes", channel_name, txt.len());
|
log::debug!("DataChannel [{}] received UTF-8 data: {} bytes", channel_name, txt.len());
|
||||||
if let Ok(val) = serde_json::from_str::<serde_json::Value>(txt) {
|
if let Ok(val) = serde_json::from_str::<serde_json::Value>(txt) {
|
||||||
|
if !channel_name.contains("CONTROL") {
|
||||||
|
// Input JSON on another channel (mobile input-method text was missing from CONTROL, #13/#46):
|
||||||
|
// record action names only, once per name, and accept text_input wherever it arrives.
|
||||||
|
if let Some(action) = val.get("action").and_then(serde_json::Value::as_str) {
|
||||||
|
static SEEN: std::sync::Mutex<Vec<String>> = std::sync::Mutex::new(Vec::new());
|
||||||
|
let mut seen = SEEN.lock().unwrap();
|
||||||
|
if seen.len() < 32 && action.len() <= 40 && action.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_') && !seen.iter().any(|s| s == action) {
|
||||||
|
seen.push(action.to_owned());
|
||||||
|
log::info!("JSON action on {} channel: action={}", if channel_name.contains("STREAMER") {"STREAMER"} else if channel_name.contains("TEXT") {"TEXT"} else {"other"}, action);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Ok(crate::control_protocol::ControlMessage::Input(crate::control_protocol::ControlInput::TextInput {text})) = crate::control_protocol::decode_control(&msg.data) {
|
||||||
|
if let Some(session)=message_input_session.as_ref() { session.with_input(|| input_worker.text_input(&text)); }
|
||||||
|
}
|
||||||
|
}
|
||||||
if channel_name.contains("CONTROL") {
|
if channel_name.contains("CONTROL") {
|
||||||
match crate::control_protocol::decode_control(&msg.data) {
|
match crate::control_protocol::decode_control(&msg.data) {
|
||||||
Ok(crate::control_protocol::ControlMessage::Input(event)) => {
|
Ok(crate::control_protocol::ControlMessage::Input(event)) => {
|
||||||
@@ -1051,7 +1066,7 @@ impl WebRtcSession {
|
|||||||
static RECEIPTS: [std::sync::atomic::AtomicUsize; 3] = [const { std::sync::atomic::AtomicUsize::new(0) }; 3];
|
static RECEIPTS: [std::sync::atomic::AtomicUsize; 3] = [const { std::sync::atomic::AtomicUsize::new(0) }; 3];
|
||||||
let (slot, kind) = match &event {
|
let (slot, kind) = match &event {
|
||||||
ControlInput::MouseButton {..} | ControlInput::MouseClick {..} => (0, "button"),
|
ControlInput::MouseButton {..} | ControlInput::MouseClick {..} => (0, "button"),
|
||||||
ControlInput::Keyboard {..} | ControlInput::KeyboardClick {..} => (1, "keyboard"),
|
ControlInput::Keyboard {..} | ControlInput::KeyboardClick {..} | ControlInput::TextInput {..} => (1, "keyboard"),
|
||||||
_ => (2, "motion"),
|
_ => (2, "motion"),
|
||||||
};
|
};
|
||||||
if RECEIPTS[slot].fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 16 {
|
if RECEIPTS[slot].fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 16 {
|
||||||
@@ -1063,6 +1078,7 @@ impl WebRtcSession {
|
|||||||
ControlInput::MouseRelative {dx,dy} => input_worker.mouse_relative(*dx,*dy),
|
ControlInput::MouseRelative {dx,dy} => input_worker.mouse_relative(*dx,*dy),
|
||||||
ControlInput::MouseScroll {dx,dy} => input_worker.mouse_scroll(*dx,*dy),
|
ControlInput::MouseScroll {dx,dy} => input_worker.mouse_scroll(*dx,*dy),
|
||||||
ControlInput::Keyboard {key, pressed} => input_worker.windows_key_event(*key, *pressed),
|
ControlInput::Keyboard {key, pressed} => input_worker.windows_key_event(*key, *pressed),
|
||||||
|
ControlInput::TextInput {text} => input_worker.text_input(text),
|
||||||
ControlInput::KeyboardClick {key} => {input_worker.windows_key_event(*key, true);input_worker.windows_key_event(*key, false);},
|
ControlInput::KeyboardClick {key} => {input_worker.windows_key_event(*key, true);input_worker.windows_key_event(*key, false);},
|
||||||
ControlInput::MouseButton {button,pressed} => input_worker.wire_mouse_button(*button,*pressed),
|
ControlInput::MouseButton {button,pressed} => input_worker.wire_mouse_button(*button,*pressed),
|
||||||
ControlInput::MouseClick {button} => {input_worker.wire_mouse_button(*button,true);input_worker.wire_mouse_button(*button,false);},
|
ControlInput::MouseClick {button} => {input_worker.wire_mouse_button(*button,true);input_worker.wire_mouse_button(*button,false);},
|
||||||
@@ -1071,6 +1087,16 @@ impl WebRtcSession {
|
|||||||
});}
|
});}
|
||||||
|
|
||||||
},
|
},
|
||||||
|
Ok(crate::control_protocol::ControlMessage::Unsupported(v)) => {
|
||||||
|
// Action name and key names only, never values (an IME text event would carry typed text).
|
||||||
|
static UNSUPPORTED: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||||
|
if UNSUPPORTED.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 16 {
|
||||||
|
let action = v.get("action").and_then(serde_json::Value::as_str)
|
||||||
|
.filter(|a| a.len() <= 40 && a.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_')).unwrap_or("?");
|
||||||
|
let keys: Vec<&str> = v.as_object().map(|o| o.keys().map(String::as_str).take(12).collect()).unwrap_or_default();
|
||||||
|
log::info!("Unsupported CONTROL input: action={action} keys={keys:?}");
|
||||||
|
}
|
||||||
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
} else if channel_name.contains("STREAMER") {
|
} else if channel_name.contains("STREAMER") {
|
||||||
|
|||||||
Vendored
+15
@@ -0,0 +1,15 @@
|
|||||||
|
The `red-{420,444}.{h264,hevc}` fixtures contain six synthetic 32x32 red IDRs,
|
||||||
|
not desktop captures. Their decoded pixel formats are yuv420p and yuv444p.
|
||||||
|
They let the CLI decoder test run without requiring encoders at test time.
|
||||||
|
|
||||||
|
Generated with FFmpeg (substitute `420` / `444` for `CHROMA`):
|
||||||
|
|
||||||
|
```sh
|
||||||
|
ffmpeg -f lavfi -i color=c=red:s=32x32:r=10 -frames:v 6 -pix_fmt yuvCHROMAp \
|
||||||
|
-c:v libx264 -threads 1 -preset ultrafast -tune zerolatency -g 1 \
|
||||||
|
-f h264 red-CHROMA.h264
|
||||||
|
ffmpeg -f lavfi -i color=c=red:s=32x32:r=10 -frames:v 6 -pix_fmt yuvCHROMAp \
|
||||||
|
-c:v libx265 -threads 1 -preset ultrafast -tune zerolatency -g 1 \
|
||||||
|
-x265-params pools=none:frame-threads=1:log-level=none:info=0 \
|
||||||
|
-f hevc red-CHROMA.hevc
|
||||||
|
```
|
||||||
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
Reference in new issue
Block a user