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
@@ -19,6 +19,20 @@ jobs:
|
|||||||
- uses: Swatinem/rust-cache@v2
|
- uses: Swatinem/rust-cache@v2
|
||||||
- run: cargo test --locked
|
- run: cargo test --locked
|
||||||
|
|
||||||
|
test-viewer:
|
||||||
|
runs-on: ubuntu-22.04
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Install build dependencies (X11 + GTK + SDL2 + ffmpeg CLI for the viewer)
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install -y build-essential pkg-config \
|
||||||
|
libx11-dev libxext-dev libxtst-dev libxfixes-dev libgtk-3-dev libsdl2-dev ffmpeg
|
||||||
|
- uses: dtolnay/rust-toolchain@stable
|
||||||
|
- uses: Swatinem/rust-cache@v2
|
||||||
|
- run: cargo test --locked --features native-viewer,viewer-sdl --bin uuremote-viewer
|
||||||
|
- run: cargo build --locked --features native-viewer,viewer-sdl --bin uuremote-viewer
|
||||||
|
|
||||||
test-wayland:
|
test-wayland:
|
||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
steps:
|
steps:
|
||||||
@@ -28,10 +42,22 @@ jobs:
|
|||||||
sudo apt-get update
|
sudo apt-get update
|
||||||
sudo apt-get install -y build-essential pkg-config \
|
sudo apt-get install -y build-essential pkg-config \
|
||||||
libx11-dev libxext-dev libxtst-dev libxfixes-dev libgtk-3-dev \
|
libx11-dev libxext-dev libxtst-dev libxfixes-dev libgtk-3-dev \
|
||||||
libwayland-dev libxkbcommon-dev libglib2.0-dev libpipewire-0.3-dev
|
libwayland-dev libxkbcommon-dev libglib2.0-dev libpipewire-0.3-dev libei-dev libeis-dev
|
||||||
- uses: dtolnay/rust-toolchain@stable
|
- uses: dtolnay/rust-toolchain@stable
|
||||||
- uses: Swatinem/rust-cache@v2
|
- uses: Swatinem/rust-cache@v2
|
||||||
- run: cargo test --locked --features wayland,portal
|
- run: cargo test --locked --features wayland,portal
|
||||||
|
- name: Native portal and EIS regression tests (ASan/UBSan)
|
||||||
|
env:
|
||||||
|
ASAN_OPTIONS: detect_leaks=1
|
||||||
|
UBSAN_OPTIONS: halt_on_error=1
|
||||||
|
run: |
|
||||||
|
for source in tests/portal_engine.c tests/portal_input.c tests/portal_eis.c tests/desktop_power.c; do
|
||||||
|
target="/tmp/$(basename "$source" .c)"
|
||||||
|
cc -O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer \
|
||||||
|
-Wall -Wextra -Wno-unused-parameter "$source" -o "$target" \
|
||||||
|
$(pkg-config --cflags --libs gio-2.0 gio-unix-2.0 libpipewire-0.3 libei-1.0 libeis-1.0) -lpthread -lm
|
||||||
|
"$target"
|
||||||
|
done
|
||||||
|
|
||||||
test-inproc:
|
test-inproc:
|
||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
|
|||||||
@@ -17,15 +17,17 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
sudo apt-get update
|
sudo apt-get update
|
||||||
sudo apt-get install -y build-essential pkg-config \
|
sudo apt-get install -y build-essential pkg-config \
|
||||||
libx11-dev libxext-dev libxtst-dev libxfixes-dev libgtk-3-dev
|
libx11-dev libxext-dev libxtst-dev libxfixes-dev libgtk-3-dev libsdl2-dev
|
||||||
- uses: dtolnay/rust-toolchain@stable
|
- uses: dtolnay/rust-toolchain@stable
|
||||||
- run: cargo test --release --locked
|
- run: cargo test --release --locked
|
||||||
- run: cargo build --release --locked
|
- run: cargo build --release --locked
|
||||||
|
# Experimental viewer; built after the host so uuremote-server keeps the default features.
|
||||||
|
- run: cargo build --release --locked --features native-viewer,viewer-sdl --bin uuremote-viewer
|
||||||
- name: Package
|
- name: Package
|
||||||
run: |
|
run: |
|
||||||
name=uu-remote-linux-${GITHUB_REF_NAME}-x86_64-linux
|
name=uu-remote-linux-${GITHUB_REF_NAME}-x86_64-linux
|
||||||
mkdir -p "$name"
|
mkdir -p "$name"
|
||||||
cp target/release/uuremote-server LICENSE README.md README.en.md config.example.json "$name/"
|
cp target/release/uuremote-server target/release/uuremote-viewer LICENSE README.md README.en.md config.example.json "$name/"
|
||||||
cp -r contrib "$name/"
|
cp -r contrib "$name/"
|
||||||
tar czf "$name.tar.gz" "$name"
|
tar czf "$name.tar.gz" "$name"
|
||||||
sha256sum "$name.tar.gz" > "$name.tar.gz.sha256"
|
sha256sum "$name.tar.gz" > "$name.tar.gz.sha256"
|
||||||
|
|||||||
@@ -7,8 +7,9 @@ This file is written for large language model agents (Claude Code, Codex, Cursor
|
|||||||
## 1. What this project is
|
## 1. What this project is
|
||||||
|
|
||||||
- An **unofficial Linux host** for NetEase UU Remote (网易 UU 远程). The official Mac / Windows / mobile
|
- An **unofficial Linux host** for NetEase UU Remote (网易 UU 远程). The official Mac / Windows / mobile
|
||||||
clients connect to it and control the Linux desktop. There is no viewer/controller side here.
|
clients connect to it and control the Linux desktop. An experimental Linux viewer/controller, `uuremote-viewer`
|
||||||
- One Rust binary, `uuremote-server` (Cargo package `uu-remote-linux`), plus small C engines in `c_engine/`
|
(`src/viewer_main.rs`, `src/viewer/`, feature `native-viewer`), connects to other hosts with the same protocol.
|
||||||
|
- One host binary, `uuremote-server` (Cargo package `uu-remote-linux`), plus small C engines in `c_engine/`
|
||||||
compiled by `build.rs`. No Wine, no NetEase binaries.
|
compiled by `build.rs`. No Wine, no NetEase binaries.
|
||||||
- The host registers with the UU cloud as a **Windows PC** (`x-param-plat=1`) so the official clients accept it.
|
- The host registers with the UU cloud as a **Windows PC** (`x-param-plat=1`) so the official clients accept it.
|
||||||
- License GPL-3.0. Not affiliated with NetEase.
|
- License GPL-3.0. Not affiliated with NetEase.
|
||||||
@@ -40,7 +41,7 @@ sudo apt install build-essential pkg-config \
|
|||||||
# --features inproc-av: libavcodec-dev libavformat-dev libavdevice-dev libswscale-dev libclang-dev
|
# --features inproc-av: libavcodec-dev libavformat-dev libavdevice-dev libswscale-dev libclang-dev
|
||||||
# (FFmpeg 6.x; in-process capture/encoding, Ubuntu 24.04 ships it)
|
# (FFmpeg 6.x; in-process capture/encoding, Ubuntu 24.04 ships it)
|
||||||
# --features wayland: libwayland-dev libxkbcommon-dev wl-clipboard
|
# --features wayland: libwayland-dev libxkbcommon-dev wl-clipboard
|
||||||
# --features wayland,portal: + libglib2.0-dev libpipewire-0.3-dev
|
# --features wayland,portal: + libglib2.0-dev libpipewire-0.3-dev libei-dev (libei >= 1.1)
|
||||||
```
|
```
|
||||||
|
|
||||||
Rust stable via rustup. Commands:
|
Rust stable via rustup. Commands:
|
||||||
@@ -50,6 +51,7 @@ cargo test # unit tests (macOS builds use C st
|
|||||||
cargo build --release # X11 host
|
cargo build --release # X11 host
|
||||||
cargo build --release --features wayland # + Hyprland/wlroots
|
cargo build --release --features wayland # + Hyprland/wlroots
|
||||||
cargo build --release --features wayland,portal # + GNOME/KDE Wayland via xdg-desktop-portal
|
cargo build --release --features wayland,portal # + GNOME/KDE Wayland via xdg-desktop-portal
|
||||||
|
cargo build --release --features native-viewer,viewer-sdl --bin uuremote-viewer # viewer (needs libsdl2-dev)
|
||||||
```
|
```
|
||||||
|
|
||||||
- `build.rs` links X11/Xext/Xtst/Xfixes and `dl` (PAM is loaded at runtime with `dlopen`), and builds the GTK3
|
- `build.rs` links X11/Xext/Xtst/Xfixes and `dl` (PAM is loaded at runtime with `dlopen`), and builds the GTK3
|
||||||
@@ -106,12 +108,15 @@ viewer does not reconnect them automatically.
|
|||||||
| `~/.config/uuremote/host_settings.json` | persisted 安全 settings: auto_lock, keep_mute, mute_permanent |
|
| `~/.config/uuremote/host_settings.json` | persisted 安全 settings: auto_lock, keep_mute, mute_permanent |
|
||||||
| `UU_DESKTOP_BACKEND` | `auto` (default) / `x11` / `wayland` / `portal` / `kms` (experimental feature) / `none` (headless: no display; desktop offers ignored; tool peers get a nominal 1920x1080 screen + Allow permissions, which the port-mapping viewer requires) |
|
| `UU_DESKTOP_BACKEND` | `auto` (default) / `x11` / `wayland` / `portal` / `kms` (experimental feature) / `none` (headless: no display; desktop offers ignored; tool peers get a nominal 1920x1080 screen + Allow permissions, which the port-mapping viewer requires) |
|
||||||
| `UU_INPUT_BACKEND` | `auto` (uinput on Wayland/portal when `/dev/uinput` is accessible, udev `uaccess` rule in README) / `uinput` / `native`. X11 keeps XTest (held-key autorepeat handling) |
|
| `UU_INPUT_BACKEND` | `auto` (uinput on Wayland/portal when `/dev/uinput` is accessible, udev `uaccess` rule in README) / `uinput` / `native`. X11 keeps XTest (held-key autorepeat handling) |
|
||||||
|
| Mac viewer Cmd | With 「使用 macOS 快捷键」 off the Mac viewer sends Cmd=VK_LWIN 0x5B, Option=0xA4, Ctrl=0xA2; connect header `controller_platform`=4 is macOS. For Mac controllers Cmd is held back: Cmd+C/V/A/F/X/Z → Ctrl+key (terminal: Cmd+C/V → Ctrl+Shift+C/V), other combos/alone → Super |
|
||||||
| `UU_TERMINAL_SHORTCUTS=0` | disable Ctrl+V → Ctrl+Shift+V in focused X11 terminals (Mac Cmd and Ctrl both arrive as VK_LCONTROL) |
|
| `UU_TERMINAL_SHORTCUTS=0` | disable Ctrl+V → Ctrl+Shift+V in focused X11 terminals (Mac Cmd and Ctrl both arrive as VK_LCONTROL) |
|
||||||
| `contrib/virtual-desktop/` | Xvfb + session (`UU_VIRTUAL_SESSION`, `UU_VIRTUAL_BUS=user|private`) for monitorless hosts; serve it with `DISPLAY=:99` |
|
| `contrib/virtual-desktop/` | Xvfb + session (`UU_VIRTUAL_SESSION`, `UU_VIRTUAL_BUS=user|private`) for monitorless hosts; serve it with `DISPLAY=:99` |
|
||||||
| `UU_VIDEO_ENCODER` | `cpu`: libx264; `vaapi`: explicit Intel/AMD; `nvenc`: force; unset: NVENC→CPU only if `/proc/driver/nvidia/version` or `/dev/nvidia0` exists, else CPU (#10) |
|
| `UU_VIDEO_ENCODER` | `cpu`: libx264; `vaapi`: explicit Intel/AMD; `nvenc`: force; unset: NVENC→CPU only if `/proc/driver/nvidia/version` or `/dev/nvidia0` exists, else CPU (#10) |
|
||||||
|
| `UU_VIDEO_CODEC=h264` | force H.264 even when the viewer negotiated H.265 (default: H.265 when both sides support it) |
|
||||||
| `UU_FFMPEG_ROOT` | FFmpeg prefix (`bin/ffmpeg`, libs in `lib/x86_64-linux-gnu`) |
|
| `UU_FFMPEG_ROOT` | FFmpeg prefix (`bin/ffmpeg`, libs in `lib/x86_64-linux-gnu`) |
|
||||||
| `UU_AUDIO=0`, `UU_AUDIO_SOURCE` | disable sound / choose the Pulse source (default `@DEFAULT_MONITOR@`) |
|
| `UU_AUDIO=0`, `UU_AUDIO_SOURCE` | disable sound / choose the Pulse source (default `@DEFAULT_MONITOR@`) |
|
||||||
| `UU_LOGIN_WINDOW=0` | don't auto-open the login window |
|
| `UU_LOGIN_WINDOW=0` | don't auto-open the login window |
|
||||||
|
| `UU_REMOTE_ASSIST=0`, `UU_ASSIST_ALLOW=1`, `UU_ASSIST_UI=tty`, `UU_ASSIST_DEBUG=1` | disable assistance / allow at start / terminal UI for `assist` / include the server's short error message in assist errors |
|
||||||
|
|
||||||
Token has no expiry field; API code 1120 (HTTP 422) means invalid/expired → re-login. The config is re-read
|
Token has no expiry field; API code 1120 (HTTP 422) means invalid/expired → re-login. The config is re-read
|
||||||
every 60 s.
|
every 60 s.
|
||||||
@@ -124,7 +129,8 @@ every 60 s.
|
|||||||
| `src/signaling.rs` | Engine.IO v4 / Socket.IO client; `be-controlled` binary attachment = ConnectOptions (field 1 capture_type) |
|
| `src/signaling.rs` | Engine.IO v4 / Socket.IO client; `be-controlled` binary attachment = ConnectOptions (field 1 capture_type) |
|
||||||
| `src/auth.rs`, `src/config.rs`, `src/login.rs`, `src/login_ui.rs` | signed REST client, config, device init + QR login, GTK window |
|
| `src/auth.rs`, `src/config.rs`, `src/login.rs`, `src/login_ui.rs` | signed REST client, config, device init + QR login, GTK window |
|
||||||
| `src/webrtc.rs` | peer connection, data channels, CONTROL dispatch, initialization messages |
|
| `src/webrtc.rs` | peer connection, data channels, CONTROL dispatch, initialization messages |
|
||||||
| `src/video.rs`, `src/capture_setting.rs` | FFmpeg H.264 pipeline; viewer quality/fps/resolution settings |
|
| `src/video.rs`, `src/capture_setting.rs`, `src/video_track.rs`, `src/hevc.rs` | FFmpeg H.264/H.265 pipeline, one video track for both codecs (RFC 7798 HEVC payloader); viewer quality/fps/resolution/chroma settings |
|
||||||
|
| `src/locked_video.rs` | low-rate synthetic black video on the negotiated track while Portal is pending and locked; enables iPhone cold auto unlock (#29), requires FFmpeg libx264/libx265 for the selected codec |
|
||||||
| `src/audio.rs` | Pulse/PipeWire monitor → Opus audio track |
|
| `src/audio.rs` | Pulse/PipeWire monitor → Opus audio track |
|
||||||
| `src/input.rs`, `src/input_mapping.rs`, `src/uinput.rs` | input injection (X11 XTest / Wayland / portal / uinput), Windows VK → keysym → evdev |
|
| `src/input.rs`, `src/input_mapping.rs`, `src/uinput.rs` | input injection (X11 XTest / Wayland / portal / uinput), Windows VK → keysym → evdev |
|
||||||
| `src/cursor.rs` | pointer shape mirroring (X11) |
|
| `src/cursor.rs` | pointer shape mirroring (X11) |
|
||||||
@@ -136,12 +142,15 @@ every 60 s.
|
|||||||
| `src/initialization.rs`, `src/control_protocol.rs`, `src/protocol.rs` | protobuf encoders/decoders for CONTROL |
|
| `src/initialization.rs`, `src/control_protocol.rs`, `src/protocol.rs` | protobuf encoders/decoders for CONTROL |
|
||||||
| `src/session_ice.rs`, `src/network_diagnostics.rs` | cloud ICE servers, address-free path diagnostics |
|
| `src/session_ice.rs`, `src/network_diagnostics.rs` | cloud ICE servers, address-free path diagnostics |
|
||||||
| `src/desktop_backend.rs`, `src/wayland.rs`, `src/portal*.rs` | backend selection, Hyprland, GNOME/KDE portal |
|
| `src/desktop_backend.rs`, `src/wayland.rs`, `src/portal*.rs` | backend selection, Hyprland, GNOME/KDE portal |
|
||||||
|
| `c_engine/portal_eis.inc` | scaled GNOME input through the existing Portal session's EIS connection; all input must use EIS once connected |
|
||||||
| `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 |
|
||||||
| `c_engine/login_window.c` | GTK3 login window |
|
| `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 |
|
||||||
|
| `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),
|
||||||
`desktop-probe [new.png]`, `desktop-video-probe`, `wayland-input-probe`, `clipboard-acceptance`,
|
`desktop-probe [new.png]`, `desktop-video-probe` (`UU_PROBE_FPS`, `UU_PROBE_FRAMES`, `UU_PROBE_QUALITY` 1-5 = viewer bitrate preset, 5 auto; `UU_PROBE_INPUT` fixed video/image sequence, `UU_PROBE_LOOP=1` repeats), `wayland-input-probe`, `clipboard-acceptance`,
|
||||||
`portal-acceptance`, `help`. Unknown arguments exit with code 2. Builds up to v0.1.0 did **not** reject them and
|
`portal-acceptance`, `help`. Unknown arguments exit with code 2. Builds up to v0.1.0 did **not** reject them and
|
||||||
started a second full host server instead, so check that a subcommand exists in the binary you run.
|
started a second full host server instead, so check that a subcommand exists in the binary you run.
|
||||||
|
|
||||||
@@ -162,7 +171,17 @@ started a second full host server instead, so check that a subcommand exists in
|
|||||||
- CaptureSettingRequest: field 1 is an **fps option index** (1=30, 2=60, 3=90); the real frame rate is
|
- CaptureSettingRequest: field 1 is an **fps option index** (1=30, 2=60, 3=90); the real frame rate is
|
||||||
field 18 `fpsCount`. Field 2 quality: 1 Fast, 2 General (高清 2M), 3 HD (超清 8M), 4 Bluray (原画 20M),
|
field 18 `fpsCount`. Field 2 quality: 1 Fast, 2 General (高清 2M), 3 HD (超清 8M), 4 Bluray (原画 20M),
|
||||||
5 Auto, 6 Custom. Resolution: 16/17 pixel size or 5/6, 10 resolution_type (3 = follow remote).
|
5 Auto, 6 Custom. Resolution: 16/17 pixel size or 5/6, 10 resolution_type (3 = follow remote).
|
||||||
Reply `RpcResponse{2 CaptureSettingResponse{}}` (no errors = applied) and send Message 25 CaptureSettingSync.
|
Field 7 `chorma_format` (ChromaFormat 1 = 4:2:0, 3 = 4:4:4) is sent when 真彩 is toggled; ff_capture_setting versions
|
||||||
|
are cumulative (3 CHROMA_SAMPLING, 4 CUSTOM_BITRATE, 5 DPI_SCALE, 6 HDR). 4:4:4 = NVENC `high444p` / x264 `high444`
|
||||||
|
with `yuv444p`; VAAPI and inproc stay 4:2:0.
|
||||||
|
Reply `RpcResponse{2 CaptureSettingResponse{}}` (no errors = applied) and send Message 25 CaptureSettingSync
|
||||||
|
(field 3 = the chroma actually encoded).
|
||||||
|
- ConnectOptions field 4 `decoder_cap_list` = DecoderCap {1 fps, 2 codec_type (1 H264, 2 H265), 3 width, 4 height,
|
||||||
|
5 chroma_format}. The Mac viewer lists H.264 and H.265, each 4:2:0 and 4:4:4, up to 3840x2160@120.
|
||||||
|
- Codec choice is the host's (official negotiateCodec): the Mac viewer offers H265 before H264 in SDP and
|
||||||
|
CaptureSettingRequest field 13 only reports its current codec. The host sends CONTROL Message 18
|
||||||
|
CodecNegotiation {1 EncoderCap {1 capture_mode=1, 2 fps, 3 codec_type, 4 width, 5 height}} at CONTROL open and
|
||||||
|
selects H.265 when both m-line codecs are bound (`UU_VIDEO_CODEC=h264` forces H.264).
|
||||||
- SystemMetrics (Message 13): GeneralMetric `2 system_sound_support` is an enum, **2 = supported** (1 = not
|
- SystemMetrics (Message 13): GeneralMetric `2 system_sound_support` is an enum, **2 = supported** (1 = not
|
||||||
supported); SettingMetric 1 keep_mute, 2 mute_permanent, 3 auto_lock, 5 file_transfer_save_location,
|
supported); SettingMetric 1 keep_mute, 2 mute_permanent, 3 auto_lock, 5 file_transfer_save_location,
|
||||||
8/9/10 permission states (2 = allow).
|
8/9/10 permission states (2 = allow).
|
||||||
@@ -189,6 +208,23 @@ started a second full host server instead, so check that a subcommand exists in
|
|||||||
`/qrcode/login/status` (1 wait, 2 scanned, 4 confirmed); `/login/by_qrcode` → user token. QR expires after
|
`/qrcode/login/status` (1 wait, 2 scanned, 4 confirmed); `/login/by_qrcode` → user token. QR expires after
|
||||||
~5 min with HTTP 400 code 1190 (not fatal: generate a new one).
|
~5 min with HTTP 400 code 1190 (not fatal: generate a new one).
|
||||||
|
|
||||||
|
- Remote assistance (recipient side), live-verified with the official Mac viewer; protocol from the official
|
||||||
|
Windows lite host (GameViewerLite: GameViewerServer + GameViewerLiteInner). Guest (not signed in) and account
|
||||||
|
identities use parallel endpoints:
|
||||||
|
| step | guest | signed in |
|
||||||
|
|---|---|---|
|
||||||
|
| identity | register the Windows device, then `POST /api/v1/guest/create` (422/1001 if unregistered) | account token |
|
||||||
|
| room | `POST /api/v1/guest/room/create` **before** share info (else 400/2004) | the host's own room |
|
||||||
|
| ID + code owner | `POST /api/v1/guest/share/info` → `connect_id` | `GET /api/v1/device/share/info` (POST = 405) |
|
||||||
|
| withdraw | `POST /api/v1/guest/share/cancel` | `POST /api/v1/device/share/cancel` |
|
||||||
|
Cloud pushes arrive as `bmsg_push`: `get_control_mode {control_id}` → `POST …/room/share/upload_control_mode`
|
||||||
|
`{control_id, allow_control, control_mode: by_password}` (guest v1, account v2); the viewer only asks for the code
|
||||||
|
after this. `remote_control {control_id, salt}` → `upload/sign` (guest v1) / `upload_sign` (account v2)
|
||||||
|
`{control_id, can_remote_control, sign: sha256hex(salt+code), backup_sign}`. Temporary-only: sign=temp code,
|
||||||
|
empty backup; both: backup=custom; custom-only: sign=custom. The share ID stays the same across sign-in/out.
|
||||||
|
Same-account devices cannot assist each other (viewer: 无法对同账号的设备发起远程协助). On a signed-in host never gate
|
||||||
|
the owner's offers on the assist switch; the cloud already gates assist peers.
|
||||||
|
|
||||||
## 8. Known pitfalls
|
## 8. Known pitfalls
|
||||||
|
|
||||||
- webrtc-rs `Sample.duration` sets the gap to the **next** frame. FFmpeg 6.1 rejects `-r` together with
|
- webrtc-rs `Sample.duration` sets the gap to the **next** frame. FFmpeg 6.1 rejects `-r` together with
|
||||||
|
|||||||
Generated
+102
-1
@@ -382,6 +382,22 @@ version = "0.9.6"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8"
|
checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "core-foundation"
|
||||||
|
version = "0.10.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b2a6cd9ae233e7f62ba4e9353e81a88df7fc8a5987b8d445b4d90c879bd156f6"
|
||||||
|
dependencies = [
|
||||||
|
"core-foundation-sys",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "core-foundation-sys"
|
||||||
|
version = "0.8.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cpufeatures"
|
name = "cpufeatures"
|
||||||
version = "0.2.17"
|
version = "0.2.17"
|
||||||
@@ -969,6 +985,7 @@ dependencies = [
|
|||||||
"hyper",
|
"hyper",
|
||||||
"hyper-util",
|
"hyper-util",
|
||||||
"rustls 0.23.45",
|
"rustls 0.23.45",
|
||||||
|
"rustls-native-certs",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-rustls",
|
"tokio-rustls",
|
||||||
"tower-service",
|
"tower-service",
|
||||||
@@ -1417,6 +1434,12 @@ version = "0.3.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381"
|
checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "openssl-probe"
|
||||||
|
version = "0.2.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "p256"
|
name = "p256"
|
||||||
version = "0.13.2"
|
version = "0.13.2"
|
||||||
@@ -1797,6 +1820,7 @@ dependencies = [
|
|||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"quinn",
|
"quinn",
|
||||||
"rustls 0.23.45",
|
"rustls 0.23.45",
|
||||||
|
"rustls-native-certs",
|
||||||
"rustls-pki-types",
|
"rustls-pki-types",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
@@ -1946,6 +1970,18 @@ dependencies = [
|
|||||||
"zeroize",
|
"zeroize",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustls-native-certs"
|
||||||
|
version = "0.8.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dab5152771c58876a2146916e53e35057e1a4dfa2b9df0f0305b07f611fdea4d"
|
||||||
|
dependencies = [
|
||||||
|
"openssl-probe",
|
||||||
|
"rustls-pki-types",
|
||||||
|
"schannel",
|
||||||
|
"security-framework",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustls-pki-types"
|
name = "rustls-pki-types"
|
||||||
version = "1.15.1"
|
version = "1.15.1"
|
||||||
@@ -1989,6 +2025,15 @@ version = "1.0.23"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f"
|
checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "schannel"
|
||||||
|
version = "0.1.29"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "91c1b7e4904c873ef0710c1f407dde2e6287de2bebc1bbbf7d430bb7cbffd939"
|
||||||
|
dependencies = [
|
||||||
|
"windows-sys 0.61.2",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "scopeguard"
|
name = "scopeguard"
|
||||||
version = "1.2.0"
|
version = "1.2.0"
|
||||||
@@ -2005,6 +2050,30 @@ dependencies = [
|
|||||||
"untrusted 0.9.0",
|
"untrusted 0.9.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sdl2"
|
||||||
|
version = "0.37.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3b498da7d14d1ad6c839729bd4ad6fc11d90a57583605f3b4df2cd709a9cd380"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 1.3.2",
|
||||||
|
"lazy_static",
|
||||||
|
"libc",
|
||||||
|
"sdl2-sys",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sdl2-sys"
|
||||||
|
version = "0.37.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "951deab27af08ed9c6068b7b0d05a93c91f0a8eb16b6b816a5e73452a43521d3"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"pkg-config",
|
||||||
|
"version-compare",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sdp"
|
name = "sdp"
|
||||||
version = "0.6.2"
|
version = "0.6.2"
|
||||||
@@ -2031,6 +2100,29 @@ dependencies = [
|
|||||||
"zeroize",
|
"zeroize",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "security-framework"
|
||||||
|
version = "3.7.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.13.2",
|
||||||
|
"core-foundation",
|
||||||
|
"core-foundation-sys",
|
||||||
|
"libc",
|
||||||
|
"security-framework-sys",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "security-framework-sys"
|
||||||
|
version = "2.17.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6ce2691df843ecc5d231c0b14ece2acc3efb62c0a398c7e1d875f3983ce020e3"
|
||||||
|
dependencies = [
|
||||||
|
"core-foundation-sys",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "semver"
|
name = "semver"
|
||||||
version = "1.0.28"
|
version = "1.0.28"
|
||||||
@@ -2649,8 +2741,9 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "uu-remote-linux"
|
name = "uu-remote-linux"
|
||||||
version = "0.1.10"
|
version = "0.2.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"async-trait",
|
||||||
"base64 0.22.1",
|
"base64 0.22.1",
|
||||||
"bytes",
|
"bytes",
|
||||||
"cc",
|
"cc",
|
||||||
@@ -2664,7 +2757,9 @@ dependencies = [
|
|||||||
"log",
|
"log",
|
||||||
"nix 0.29.0",
|
"nix 0.29.0",
|
||||||
"qrcode",
|
"qrcode",
|
||||||
|
"rand 0.8.8",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
|
"sdl2",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"sha2",
|
"sha2",
|
||||||
@@ -2691,6 +2786,12 @@ version = "0.2.15"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
|
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "version-compare"
|
||||||
|
version = "0.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "version_check"
|
name = "version_check"
|
||||||
version = "0.9.5"
|
version = "0.9.5"
|
||||||
|
|||||||
+15
-2
@@ -1,7 +1,8 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "uu-remote-linux"
|
name = "uu-remote-linux"
|
||||||
version = "0.1.10"
|
version = "0.2.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
default-run = "uuremote-server"
|
||||||
authors = ["uu-remote-linux contributors"]
|
authors = ["uu-remote-linux contributors"]
|
||||||
description = "Unofficial Linux host for NetEase UU Remote: control a Linux desktop from the official clients"
|
description = "Unofficial Linux host for NetEase UU Remote: control a Linux desktop from the official clients"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
@@ -13,8 +14,16 @@ repository = "https://github.com/VallierDev/uu-remote-linux"
|
|||||||
name = "uuremote-server"
|
name = "uuremote-server"
|
||||||
path = "src/main.rs"
|
path = "src/main.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "uuremote-viewer"
|
||||||
|
path = "src/viewer_main.rs"
|
||||||
|
required-features = ["native-viewer"]
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
|
native-viewer = []
|
||||||
|
viewer-ffmpeg = ["native-viewer", "dep:ffmpeg-next"]
|
||||||
|
viewer-sdl = ["native-viewer", "dep:sdl2"]
|
||||||
wayland = []
|
wayland = []
|
||||||
# Experimental direct linear framebuffer capture; never enabled in release builds.
|
# Experimental direct linear framebuffer capture; never enabled in release builds.
|
||||||
kms = []
|
kms = []
|
||||||
@@ -29,13 +38,15 @@ tokio-tungstenite = { version = "0.23", features = ["rustls-tls-webpki-roots"] }
|
|||||||
futures-util = "0.3"
|
futures-util = "0.3"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
|
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls", "rustls-tls-native-roots"] }
|
||||||
hmac = "0.12"
|
hmac = "0.12"
|
||||||
sha2 = "0.10"
|
sha2 = "0.10"
|
||||||
hex = "0.4"
|
hex = "0.4"
|
||||||
base64 = "0.22"
|
base64 = "0.22"
|
||||||
flate2 = "1.0"
|
flate2 = "1.0"
|
||||||
bytes = "1"
|
bytes = "1"
|
||||||
|
async-trait = "0.1"
|
||||||
|
rand = "0.8"
|
||||||
webrtc = "0.10"
|
webrtc = "0.10"
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
nix = { version = "0.29", features = ["term", "process", "fs", "ioctl", "signal"] }
|
nix = { version = "0.29", features = ["term", "process", "fs", "ioctl", "signal"] }
|
||||||
@@ -45,6 +56,8 @@ env_logger = "0.11"
|
|||||||
ffmpeg-next = { version = "6.1", optional = true, default-features = false, features = ["codec", "format", "device", "software-scaling", "software-resampling"] }
|
ffmpeg-next = { version = "6.1", optional = true, default-features = false, features = ["codec", "format", "device", "software-scaling", "software-resampling"] }
|
||||||
x11rb = { version = "0.13", features = ["damage", "shm", "allow-unsafe-code"], optional = true }
|
x11rb = { version = "0.13", features = ["damage", "shm", "allow-unsafe-code"], optional = true }
|
||||||
|
|
||||||
|
sdl2 = { version = "0.37", optional = true, features = ["use-pkgconfig"] }
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
cc = "1.0"
|
cc = "1.0"
|
||||||
|
|
||||||
|
|||||||
+114
-13
@@ -12,7 +12,7 @@
|
|||||||
> 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/login-window.png" width="560" alt="QR login window on the Linux host"></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>The QR login window shown on first run (QR code blurred). 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
|
||||||
@@ -31,10 +31,11 @@
|
|||||||
|
|
||||||
| Feature | Notes |
|
| Feature | Notes |
|
||||||
|---|---|
|
|---|---|
|
||||||
| QR login | GTK login window (or a terminal QR code with `login --no-gui`); expired codes refresh automatically; the service opens the login window by itself when credentials are missing or expired |
|
| Main window and QR login | One window with "This device" (remote assistance) and "Account": a QR code before sign-in, the account and a sign-out button after; `login --no-gui` prints the QR in a terminal |
|
||||||
|
| Remote assistance (experimental) | Like the official client: share the **device ID + verification code**; the other person enters them under 开始协助 in UU Remote. Works before sign-in (guest identity) and after; see below |
|
||||||
| Remote desktop | H.264 video, NVENC hardware encoding on NVIDIA GPUs, libx264 otherwise; the viewer draws its local pointer, so there is no double cursor |
|
| Remote desktop | H.264 video, NVENC hardware encoding on NVIDIA GPUs, libx264 otherwise; the viewer draws its local pointer, so there is no double cursor |
|
||||||
| Mouse / keyboard | Absolute and relative motion, clicks, wheel, keys; no spurious key repeat on high-latency links; optional uinput virtual devices on Wayland (layout independent, see below) |
|
| Mouse / keyboard | Absolute and relative motion, clicks, wheel, keys; no spurious key repeat on high-latency links; optional uinput virtual devices on Wayland (layout independent, see below) |
|
||||||
| Terminal shortcuts | Mac viewers send both Cmd and Ctrl as Ctrl. In a focused terminal window Ctrl+V / Cmd+V become Ctrl+Shift+V (paste); Ctrl+C always interrupts, copy with Cmd+Shift+C (X11) |
|
| Mac shortcuts | **Recommended**: turn off 「使用 macOS 快捷键」 (use macOS shortcuts) in the Mac viewer's remote-window menu; then Cmd+C/V/A/F/X/Z work as on a Mac (Cmd+C/V become Ctrl+Shift+C/V in terminals) and Ctrl+C always interrupts. With the option on, the viewer sends Cmd as Ctrl: in a terminal Ctrl+V / Cmd+V paste, Ctrl+C interrupts, copy is Cmd+Shift+C (X11) |
|
||||||
| Pointer shape | Remote cursor shape is mirrored to the viewer (X11) |
|
| Pointer shape | Remote cursor shape is mirrored to the viewer (X11) |
|
||||||
| Clipboard | Two-way text sync |
|
| Clipboard | Two-way text sync |
|
||||||
| Quality / frame rate / resolution | Auto, Original 20M, Ultra 8M, HD 2M, custom; 30/60/90/144 fps; "other resolutions" are scaled by the encoder, the host monitor is left untouched |
|
| Quality / frame rate / resolution | Auto, Original 20M, Ultra 8M, HD 2M, custom; 30/60/90/144 fps; "other resolutions" are scaled by the encoder, the host monitor is left untouched |
|
||||||
@@ -44,7 +45,7 @@
|
|||||||
| Lock state and auto unlock | Lock state is reported to the viewer; with "auto unlock" enabled in the client, the password is verified with PAM and the session is unlocked |
|
| Lock state and auto unlock | Lock state is reported to the viewer; with "auto unlock" enabled in the client, the password is verified with PAM and the session is unlocked |
|
||||||
| Lock after remote session | Stored on the host, so it still applies after reconnecting |
|
| Lock after remote session | Stored on the host, so it still applies after reconnecting |
|
||||||
| Mute host while controlled | Host speakers are muted while the viewer still hears the sound; the previous mute state is restored afterwards |
|
| Mute host while controlled | Host speakers are muted while the viewer still hears the sound; the previous mute state is restored afterwards |
|
||||||
| System actions | Lock, show desktop, task manager (system monitor), reboot, shut down |
|
| System actions | Lock, show desktop, task manager (system monitor), reboot, shut down (via `systemctl`; needs polkit permission for the user, usually an active local graphical session; without it the action fails and is logged, no password dialog appears on the host) |
|
||||||
| Port mapping | The official "port mapping" tool: forwards a local port on the viewer to an address reachable from the Linux host (e.g. 127.0.0.1:22); `UU_PORT_MAPPING=0` disables it |
|
| Port mapping | The official "port mapping" tool: forwards a local port on the viewer to an address reachable from the Linux host (e.g. 127.0.0.1:22); `UU_PORT_MAPPING=0` disables it |
|
||||||
| Reconnects | Every tool window has its own connection; they do not disturb each other |
|
| Reconnects | Every tool window has its own connection; they do not disturb each other |
|
||||||
| Headless servers | Goes online without a monitor with terminal, file transfer and port mapping; an Xvfb virtual desktop gives a full remote desktop (see below) |
|
| Headless servers | Goes online without a monitor with terminal, file transfer and port mapping; an Xvfb virtual desktop gives a full remote desktop (see below) |
|
||||||
@@ -64,16 +65,27 @@ authorized session. Independent file/terminal tool peers do not reset input.
|
|||||||
| Xfce | X11 | ✅ Tested in a VM | default |
|
| Xfce | X11 | ✅ Tested in a VM | default |
|
||||||
| Cinnamon / MATE | X11 | ✅ Tested in a VM | default |
|
| Cinnamon / MATE | X11 | ✅ Tested in a VM | default |
|
||||||
| Hyprland | Wayland (native wlroots protocols) | 🧪 Experimental, tested in a VM (some features limited, see below) | `--features wayland` |
|
| Hyprland | Wayland (native wlroots protocols) | 🧪 Experimental, tested in a VM (some features limited, see below) | `--features wayland` |
|
||||||
| GNOME / KDE Plasma | Wayland (xdg-desktop-portal) | 🧪 Experimental, tested in a VM (some features limited, see below) | `--features wayland,portal` |
|
| GNOME / KDE Plasma | Wayland (xdg-desktop-portal) | 🧪 Experimental; validation scope and limits below | `--features wayland,portal` |
|
||||||
|
|
||||||
Apart from GNOME on X11, these desktops have been tested in virtual machines so far. If something goes wrong on
|
The GNOME Wayland Portal/EIS path has been checked for video and mouse/keyboard input on Ubuntu 26.04, GNOME 50, with one 4K screen at 200% scale; auto unlock after a cold locked connection is verified with the official Mac and iPhone 4.42.0 viewers, while other lock recovery and reconnection still need hardware acceptance testing. Apart from that path and GNOME on X11, desktop testing has primarily used virtual machines. If something goes wrong on
|
||||||
real hardware, please [open an issue](../../issues) with your distribution, desktop, GPU and the relevant lines from
|
real hardware, please [open an issue](../../issues) with your distribution, desktop, GPU and the relevant lines from
|
||||||
`journalctl --user -u uuremote-native.service`.
|
`journalctl --user -u uuremote-native.service`.
|
||||||
|
|
||||||
Wayland limits: single screen, fixed resolution. The native Hyprland backend now pipelines two shm screencopy buffers and passes the viewer's requested fps to both capture pacing and the encoder. Actual fps remains limited by the compositor, rendering and readback (the old serial path measured about 16–22 fps at 2560x1600; the pipeline is VM-measured only, not a hardware guarantee). The portal backend does not support screen wake or
|
Wayland limits: single screen, fixed resolution. The native Hyprland backend now pipelines two shm screencopy buffers and passes the viewer's requested fps to both capture pacing and the encoder. Actual fps remains limited by the compositor, rendering and readback (the old serial path measured about 16–22 fps at 2560x1600; the pipeline is VM-measured only, not a hardware guarantee). The portal backend does not support dynamic resolution; see below for GNOME wake and lock recovery; the clipboard syncs while the viewer's remote window has focus. On the first connection the desktop asks for "Remote Desktop / Screen Sharing"
|
||||||
dynamic resolution yet; the clipboard syncs while the viewer's remote window has focus. On the first connection the desktop asks for "Remote Desktop / Screen Sharing"
|
|
||||||
permission; the permission is remembered once granted.
|
permission; the permission is remembered once granted.
|
||||||
|
|
||||||
|
Scaled GNOME desktops using native Portal input use EIS on the same authorized session when `ConnectToEIS` and a screen `mapping_id` are available. Each axis is mapped using the matched region and actual frame dimensions, with no fixed resolution or scale factor. This avoids scaled-coordinate rejection by `NotifyPointerMotionAbsolute`. Portal builds require libei >= 1.1. Missing EIS support retains the legacy input path and its existing scaling limitations; other initialization failures are reported and trigger session recovery. After a successful EIS connection, all input uses EIS. Capture alone and uinput do not initialize EIS; missing optional logical dimensions do not block capture. Keyboard input uses the existing Windows VK to evdev mapping. Recovery keeps the encoder frame dimensions fixed but accepts logical-size changes after scaling. On GNOME, a display-change notification enables earlier recovery when previously streaming Portal capture remains paused without a valid format for a 200 ms grace after both conditions are present. Healthy renegotiation cancels the early check; repeated notifications do not postpone the same interruption. The five-second fallback remains without a related notification. Static video and valid-format pauses do not trigger recovery. Tested while connected on Ubuntu 26.04, GNOME Wayland, one 3840x2160 display: 200% -> 166.67% -> 200%, with continuously updating video and full-screen pointer control after recovery. Switching briefly freezes and goes black before recovering; five seconds is the fallback threshold, not total recovery time. See [#35](../../pull/35) for timing measurements and validation limits. Lock recovery still needs hardware acceptance testing; multi-monitor support, live resolution changes, and rotation/reflection transforms are outside this fix's scope. Capture settings remain at protocol version 4: advertised resolutions use pixel coordinates and DPR 1; Portal logical dimensions are used only for local input mapping.
|
||||||
|
|
||||||
|
GNOME Portal CONTROL no longer waits for a consent dialog or the first video frame. It announces the selected desktop before reporting secure-desktop state and receiving the official viewer's “auto unlock host” request. The official Android viewer requires this desktop selection before entering its auto-unlock flow, so the notification must precede capture readiness; actual screen dimensions, codec information and capture permissions still follow their real readiness state. Enable auto unlock in the viewer and use the current Linux user's login password. The host verifies it with PAM, unlocks the existing GNOME session, requests display wake, and restores capture, input and clipboard through the shared Portal session. Initial consent or revoked permissions still require local approval. Reconnect if physical or logical dimensions change.
|
||||||
|
|
||||||
|
The host also accepts a desktop connection while GNOME is locked with no Portal session yet (a cold connection): it reports lock state and accepts auto unlock, then restores picture, input and clipboard after unlock. While Portal is pending, the host sends generated black video once per second on the negotiated track by default, allowing the official iPhone viewer to enter its auto-unlock flow without extra configuration. Verified on Ubuntu GNOME Wayland with the official Mac viewer; iPhone 4.42.0 with H.265 completed a same-executable control experiment: black video ON succeeded, OFF failed, and ON succeeded again ([#29](../../issues/29)). **Limitation**: this is synthetic black video, not the GNOME lock screen, and it does not enable native mouse/keyboard input while locked. Enable 「自动解锁被控端」 in the UU client under 控制中心 → 安全 and enter the password. Without saved Portal permission, local consent is still required after unlock before the real desktop can appear.
|
||||||
|
|
||||||
|
Existing screen-off and lock settings remain unchanged. Only capture recovery in an unlocked session temporarily holds a GNOME idle inhibitor; the first frame, lock, disconnect or initialization failure releases it. An idle viewer still allows the normal screen-off and lock timeout, and manual lock remains effective. Desktop input is blocked and real video is hidden while locked or when lock state is unknown. Independent terminal, file-transfer and port-mapping peers do not wait for video initialization and survive desktop reconnection. Automated protocol and lifecycle regression tests cover this path; auto unlock and capture recovery after a cold locked connection are verified on GNOME hardware with the official Mac and iPhone 4.42.0 viewers, while screen-off and the other scenarios still need acceptance testing. Waking a fully suspended or hibernating machine is outside this CONTROL path.
|
||||||
|
|
||||||
|
Locked black video requires FFmpeg's libx264/libx265 encoder for the negotiated H.264/H.265 codec; hardware encoding support alone does not satisfy this dependency. Encoder failures are logged explicitly. Synthetic video does not change capture permissions or advertised monitor dimensions. It pauses on unlock or unknown lock state, ends when capture is ready/rejected or the connection closes, and then hands the same video track to real capture. See [#29](../../issues/29) and [#34](../../pull/34) for the controlled experiment and validation scope.
|
||||||
|
|
||||||
|
The native Wayland backend needs `wlr-output-power-management` from the compositor to wake a turned-off screen; without it only wake is unavailable — capture and encoding are unaffected.
|
||||||
|
|
||||||
Native Wayland experiments: `UU_WAYLAND_PIPELINE=0` disables pipelining for comparison.
|
Native Wayland experiments: `UU_WAYLAND_PIPELINE=0` disables pipelining for comparison.
|
||||||
`UU_WAYLAND_DMABUF=1` explicitly tries dma-buf → GBM CPU mapping (off by default;
|
`UU_WAYLAND_DMABUF=1` explicitly tries dma-buf → GBM CPU mapping (off by default;
|
||||||
requires runtime libgbm; `UU_WAYLAND_DMABUF_DEVICE` overrides `/dev/dri/renderD128`).
|
requires runtime libgbm; `UU_WAYLAND_DMABUF_DEVICE` overrides `/dev/dri/renderD128`).
|
||||||
@@ -87,15 +99,15 @@ mapping and hardware acceleration remain unverified. This is not zero-copy VAAPI
|
|||||||
| Feature | Status |
|
| Feature | Status |
|
||||||
|---|---|
|
|---|---|
|
||||||
| Switching between multiple monitors | In progress |
|
| Switching between multiple monitors | In progress |
|
||||||
| True color (4:4:4) | Not implemented; not declared in the handshake, so the client does not enable it |
|
| True color (4:4:4) | Supported (v0.1.15): the default FFmpeg pipeline with NVENC / libx264 encodes H.264 High 4:4:4; VAAPI and `inproc-av` stay 4:2:0 and report that to the client |
|
||||||
| HDR | Not implemented |
|
| HDR | Not implemented |
|
||||||
| Super screen / virtual display | Not implemented |
|
| Super screen / virtual display | Not implemented |
|
||||||
| Privacy screen on the host | Not implemented |
|
| Privacy screen on the host | Not implemented |
|
||||||
| Microphone (viewer audio played on the host) | Not implemented (needs a virtual sound card) |
|
| Microphone (viewer audio played on the host) | Not implemented (needs a virtual sound card) |
|
||||||
| Plugins such as the WoW queue notifier | Not supported; the client is told that no plugins exist |
|
| Plugins such as the WoW queue notifier | Not supported; the client is told that no plugins exist |
|
||||||
| Access before login (greeter / no user logged in) | The greeter cannot be controlled; without a logged-in user use headless mode or a virtual desktop (see below) |
|
| Linux login screen | The system login screen (before anyone logs in to the Linux desktop) cannot be controlled yet; use headless mode or a virtual desktop (see below). This is about the Linux login: **without a UU account** the host can now be used through remote assistance |
|
||||||
| Relay connections on restrictive networks | Uses the ICE servers provided by the UU cloud; complex network setups are not thoroughly tested |
|
| Relay connections on restrictive networks | Uses the ICE servers provided by the UU cloud; complex network setups are not thoroughly tested |
|
||||||
| Acting as a viewer (controlling other machines from Linux) | Out of scope |
|
| Acting as a viewer (controlling other machines from Linux) | 🧪 Experimental `uuremote-viewer`, see [below](#experimental-native-viewer-uuremote-viewer) |
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -108,7 +120,7 @@ sudo apt install build-essential pkg-config \
|
|||||||
# optional: Wayland backend
|
# optional: Wayland backend
|
||||||
sudo apt install libwayland-dev libxkbcommon-dev wl-clipboard
|
sudo apt install libwayland-dev libxkbcommon-dev wl-clipboard
|
||||||
# optional: GNOME/KDE portal backend
|
# optional: GNOME/KDE portal backend
|
||||||
sudo apt install libglib2.0-dev libpipewire-0.3-dev
|
sudo apt install libglib2.0-dev libpipewire-0.3-dev libei-dev
|
||||||
```
|
```
|
||||||
|
|
||||||
- A Rust toolchain is required (<https://rustup.rs>).
|
- A Rust toolchain is required (<https://rustup.rs>).
|
||||||
@@ -180,6 +192,37 @@ sudo udevadm control --reload && sudo udevadm trigger --name-match=uinput
|
|||||||
`uaccess` grants access only to the user sitting at the machine. `UU_INPUT_BACKEND=uinput|native` forces a backend.
|
`uaccess` grants access only to the user sitting at the machine. `UU_INPUT_BACKEND=uinput|native` forces a backend.
|
||||||
X11 keeps XTest by default because it handles held keys correctly on high-latency links.
|
X11 keeps XTest by default because it handles held keys correctly on high-latency links.
|
||||||
|
|
||||||
|
## Remote assistance and the main window (experimental)
|
||||||
|
|
||||||
|
As in the official client, this host shows a **device ID** and a **verification code**; whoever you give them to can
|
||||||
|
connect from 开始协助 (remote assistance) in UU Remote.
|
||||||
|
|
||||||
|
- **Not signed in**: the main window opens at start, offers remote assistance as a guest and shows the sign-in QR code.
|
||||||
|
Signing in switches to account mode automatically.
|
||||||
|
- **Signed in**: the host is in your device list and remote assistance keeps working (same device ID). A background
|
||||||
|
service keeps the window hidden; open it with `uuremote-server show` or the "UU Remote Linux Host" launcher.
|
||||||
|
Closing the window only hides it.
|
||||||
|
- **Tray icon**: a background host shows a tray icon with Open main window, the allow switch and Quit. GNOME needs
|
||||||
|
the AppIndicator extension (bundled on Ubuntu); Xfce / Cinnamon / KDE work as is. Quit stops `uuremote-native.service`;
|
||||||
|
the launcher starts it again.
|
||||||
|
- **Settings** (as in the official client): allow switch (off by default); verification by temporary code, custom code
|
||||||
|
(8–16 letters and digits) or either; temporary code rotation after each session / hourly / 12 h / 24 h / never;
|
||||||
|
copy buttons for the ID and the code. Stored in the 0600 config.
|
||||||
|
- The code is only shown in the local window, never logged. Without a desktop, `uuremote-server assist` shows it on the
|
||||||
|
terminal (`allow` / `deny` / `reset` / `mode …` / `refresh …` / `off`).
|
||||||
|
- **Security**: the ID and code give full control (desktop, files, terminal). Share them only with people you trust.
|
||||||
|
|
||||||
|
Optional launcher icon:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
install -Dm644 contrib/desktop/uu-remote-linux.svg ~/.local/share/icons/hicolor/scalable/apps/uu-remote-linux.svg
|
||||||
|
install -Dm644 contrib/desktop/uu-remote-linux.desktop ~/.local/share/applications/uu-remote-linux.desktop
|
||||||
|
```
|
||||||
|
|
||||||
|
Verified with the official Mac viewer: guest connections, wrong-code rejection, code rotation, switching assistance off,
|
||||||
|
signing in and out. **Remote assistance while signed in has not yet been tested from a different account** (devices of
|
||||||
|
the same account cannot assist each other, an official restriction).
|
||||||
|
|
||||||
## Servers without a monitor
|
## Servers without a monitor
|
||||||
|
|
||||||
**Headless mode** (terminal, file transfer and port mapping only): add `Environment=UU_DESKTOP_BACKEND=none` to the
|
**Headless mode** (terminal, file transfer and port mapping only): add `Environment=UU_DESKTOP_BACKEND=none` to the
|
||||||
@@ -220,18 +263,24 @@ Common environment variables:
|
|||||||
| `UU_VIDEO_ENCODER` | `cpu`: libx264; `vaapi`: opt-in Intel/AMD; `nvenc`: force NVENC; unset: NVENC only when an NVIDIA driver is loaded, else CPU |
|
| `UU_VIDEO_ENCODER` | `cpu`: libx264; `vaapi`: opt-in Intel/AMD; `nvenc`: force NVENC; unset: NVENC only when an NVIDIA driver is loaded, else CPU |
|
||||||
| `UU_FFMPEG_ROOT` | Use FFmpeg from this prefix |
|
| `UU_FFMPEG_ROOT` | Use FFmpeg from this prefix |
|
||||||
| `UU_AUDIO=0` | Disable sound; `UU_AUDIO_SOURCE` picks the PulseAudio source |
|
| `UU_AUDIO=0` | Disable sound; `UU_AUDIO_SOURCE` picks the PulseAudio source |
|
||||||
|
| `UU_REMOTE_ASSIST=0` | Disable remote assistance (no guest assistance before sign-in either) |
|
||||||
|
| `UU_ASSIST_ALLOW=1` | Allow remote assistance at start (overrides the saved switch) |
|
||||||
| `UU_LOGIN_WINDOW=0` | Do not open the login window automatically when credentials are missing |
|
| `UU_LOGIN_WINDOW=0` | Do not open the login window automatically when credentials are missing |
|
||||||
| `UU_PORT_MAPPING=0` | Disable port mapping (the official host's controlled-side switch) |
|
| `UU_PORT_MAPPING=0` | Disable port mapping (the official host's controlled-side switch) |
|
||||||
|
|
||||||
Host settings such as "lock after remote session" and "mute host" are stored in
|
Host settings such as "lock after remote session" and "mute host" are stored in
|
||||||
`~/.config/uuremote/host_settings.json`.
|
`~/.config/uuremote/host_settings.json`.
|
||||||
|
|
||||||
|
With external FFmpeg CPU encoding, the bitrate presets (Original / Ultra / HD / custom) use average bitrate (ABR) to avoid gray keyframes at 4K and low bitrates; Auto keeps constant rate factor (CRF).
|
||||||
|
|
||||||
## Let an AI agent install or develop it
|
## Let an AI agent install or develop it
|
||||||
|
|
||||||
If you use Claude Code, Codex, Cursor or a similar coding agent, have it read [AGENTS.md](AGENTS.md) first.
|
If you use Claude Code, Codex, Cursor or a similar coding agent, have it read [AGENTS.md](AGENTS.md) first.
|
||||||
It is written for language models: build and install steps, a code map, protocol notes and development rules,
|
It is written for language models: build and install steps, a code map, protocol notes and development rules,
|
||||||
enough to take over installation or development.
|
enough to take over installation or development.
|
||||||
|
|
||||||
|
For local encoding diagnostics, `desktop-video-probe [output.h264]` accepts `UU_PROBE_FPS`, `UU_PROBE_FRAMES` (default 5), and `UU_PROBE_QUALITY` (1–5); set `UU_PROBE_INPUT=clip.y4m` (also `.mp4` or `frames/%05d.png`) to bypass desktop capture and measure fixed input with the same production FFmpeg encoding options, printing dimensions and encoding statistics, and optionally `UU_PROBE_LOOP=1` to repeat until the requested frame count (unreadable or short input exits nonzero; fixed input always uses the external FFmpeg pipeline).
|
||||||
|
|
||||||
## How it works
|
## How it works
|
||||||
|
|
||||||
```mermaid
|
```mermaid
|
||||||
@@ -277,6 +326,16 @@ Released under the [GPL-3.0](LICENSE): you may use, modify and redistribute it,
|
|||||||
- [webrtc-rs](https://github.com/webrtc-rs/webrtc), FFmpeg, GTK and the other open-source projects this builds on.
|
- [webrtc-rs](https://github.com/webrtc-rs/webrtc), FFmpeg, GTK and the other open-source projects this builds on.
|
||||||
- Most of the code was written with the help of Claude and Codex.
|
- Most of the code was written with the help of Claude and Codex.
|
||||||
|
|
||||||
|
### H.265 / HEVC
|
||||||
|
|
||||||
|
When both sides support H.265 the host uses it (as the official host does: the host picks the codec from both
|
||||||
|
capability lists; the official Mac viewer lists H265 first in its SDP). The first desktop connection probes a
|
||||||
|
synthetic frame and announces encoder support with CodecNegotiation. `hevc_nvenc`, `libx265` and explicit
|
||||||
|
`UU_VIDEO_ENCODER=vaapi` (`hevc_vaapi`); hardware failure falls back to software HEVC, then to H.264.
|
||||||
|
`UU_VIDEO_CODEC=h264` forces H.264. 8-bit; true color 4:4:4 uses hevc_nvenc `rext` / libx265 `main444-8`.
|
||||||
|
Verified with the official Mac viewer on an RTX 3090 (hevc_nvenc, including 4:4:4); libx265, hevc_vaapi and
|
||||||
|
other viewers are not hardware-verified.
|
||||||
|
|
||||||
### VAAPI (opt-in / 显式启用)
|
### VAAPI (opt-in / 显式启用)
|
||||||
|
|
||||||
`UU_VIDEO_ENCODER=vaapi` selects Intel/AMD H.264 VAAPI; `UU_VAAPI_DEVICE` defaults to
|
`UU_VIDEO_ENCODER=vaapi` selects Intel/AMD H.264 VAAPI; `UU_VAAPI_DEVICE` defaults to
|
||||||
@@ -320,10 +379,14 @@ cargo build --release --features wayland
|
|||||||
Omarchy uses Hyprland: run in its user session, select `UU_DESKTOP_BACKEND=wayland`,
|
Omarchy uses Hyprland: run in its user session, select `UU_DESKTOP_BACKEND=wayland`,
|
||||||
and retain `WAYLAND_DISPLAY` / `XDG_RUNTIME_DIR`. This requires wlr-screencopy and the
|
and retain `WAYLAND_DISPLAY` / `XDG_RUNTIME_DIR`. This requires wlr-screencopy and the
|
||||||
virtual input protocols supported by Hyprland; other compositors may need `--features wayland,portal`.
|
virtual input protocols supported by Hyprland; other compositors may need `--features wayland,portal`.
|
||||||
For uinput, load `uinput` and apply the uaccess rule above; seat ACLs require an active login.
|
For uinput, load `uinput` (at boot: `echo uinput | sudo tee /etc/modules-load.d/uinput.conf`) and apply the uaccess rule above; seat ACLs require an active login.
|
||||||
For unattended accounts, explicitly restrict `/dev/uinput` access to a dedicated group.
|
For unattended accounts, explicitly restrict `/dev/uinput` access to a dedicated group.
|
||||||
This permission can inject into the whole seat, including lock/greeter screens.
|
This permission can inject into the whole seat, including lock/greeter screens.
|
||||||
`UU_INPUT_BACKEND=uinput` requests kernel devices; verify startup actually reports uinput.
|
`UU_INPUT_BACKEND=uinput` requests kernel devices; verify startup actually reports uinput.
|
||||||
|
In Hyprland / uwsm sessions `default.target` may start before the compositor exports `WAYLAND_DISPLAY`;
|
||||||
|
tie the unit to the graphical session instead (community-verified autostart on Omarchy):
|
||||||
|
`After=` and `PartOf=graphical-session.target`, `ConditionEnvironment=WAYLAND_DISPLAY`,
|
||||||
|
`Environment=UU_DESKTOP_BACKEND=wayland` (no `DISPLAY`), and `WantedBy=graphical-session.target`.
|
||||||
Optional VAAPI: `intel-media-driver` (modern Intel) or `libva-mesa-driver` (AMD), `libva-utils`
|
Optional VAAPI: `intel-media-driver` (modern Intel) or `libva-mesa-driver` (AMD), `libva-utils`
|
||||||
for `vainfo`. Optional KMS: `libdrm`. Virtual Xfce: `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
|
for `vainfo`. Optional KMS: `libdrm`. Virtual Xfce: `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
|
||||||
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`.
|
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`.
|
||||||
@@ -340,3 +403,41 @@ This capability is broad: it applies to the whole network-facing process and can
|
|||||||
The VM GDM greeter capture was solid black (failed visual acceptance); lock/greeter support and Intel/AMD hardware remain unverified. Use uinput for input; clipboard
|
The VM GDM greeter capture was solid black (failed visual acceptance); lock/greeter support and Intel/AMD hardware remain unverified. Use uinput for input; clipboard
|
||||||
is unavailable without a desktop session. No default release behavior changes.
|
is unavailable without a desktop session. No default release behavior changes.
|
||||||
No official "no desktop" error code was found for headless desktop offers, so the existing offer behavior is retained.
|
No official "no desktop" error code was found for headless desktop offers, so the existing offer behavior is retained.
|
||||||
|
|
||||||
|
## Experimental native viewer (uuremote-viewer)
|
||||||
|
|
||||||
|
`uuremote-viewer` is a Linux **viewer/controller**: it connects to and controls other devices on your UU account (official Windows / Mac
|
||||||
|
hosts or this project's Linux host), and can start remote assistance with someone's device ID and code. It is experimental and
|
||||||
|
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:
|
||||||
|
`libsdl2-2.0-0`) and the `ffmpeg` command; clipboard needs `wl-clipboard` (Wayland) or `xclip`/`xsel` (X11); the terminal needs a local
|
||||||
|
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`.
|
||||||
|
|
||||||
|
**Use**:
|
||||||
|
- `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
|
||||||
|
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.
|
||||||
|
- 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`,
|
||||||
|
`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,
|
||||||
|
`UU_VIEWER_CLIPBOARD=0` the clipboard, `UU_VIEWER_VIEW_ONLY=1` input; `UU_VIEWER_FFMPEG` selects the ffmpeg binary.
|
||||||
|
|
||||||
|
**Tested live** (author's setup, not full acceptance):
|
||||||
|
| Feature | Official Windows host | Official Mac host | This project's Linux host |
|
||||||
|
|---|---|---|---|
|
||||||
|
| Desktop video (H.264) | ✅ | ✅ | ✅ |
|
||||||
|
| Keyboard, mouse | ✅ | — | ✅ |
|
||||||
|
| File list / download / upload | ✅ list, download | — | ✅ |
|
||||||
|
| Port mapping, terminal | — | — | ✅ |
|
||||||
|
| Clipboard (text, both ways), sound | — | — | ✅ (VM) |
|
||||||
|
| Remote assistance (ID + code) | — | — | ✅ (VM, guest host) |
|
||||||
|
| Terminal session list (create, double-click reattach, end) | — | — | ✅ (VM) |
|
||||||
|
| Reboot, shutdown | — | — | ✅ (VM) |
|
||||||
|
| Wayland desktop window and clipboard (Hyprland) | — | — | ✅ (VM) |
|
||||||
|
|
||||||
|
"—" = 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
|
||||||
|
numbers, sizes and states only, never keystrokes, clipboard or file contents, codes or credentials.
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
> 这是个人兴趣项目,**与网易无关,也不是官方客户端**。如果网易认为本项目侵权或有不妥,
|
> 这是个人兴趣项目,**与网易无关,也不是官方客户端**。如果网易认为本项目侵权或有不妥,
|
||||||
> 请在推特私信我 **[@ai_fengshou](https://x.com/ai_fengshou)**,我会第一时间自行下架。
|
> 请在推特私信我 **[@ai_fengshou](https://x.com/ai_fengshou)**,我会第一时间自行下架。
|
||||||
|
|
||||||
<p align="center"><img src="docs/images/login-window.png" width="560" 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>首次运行时弹出的扫码登录窗口(二维码已打码)</sub></p>
|
||||||
|
|
||||||
<!-- 截图占位:在官方客户端里远控 Linux 桌面的效果图。请自行截图并去掉账号、设备名等信息后放到
|
<!-- 截图占位:在官方客户端里远控 Linux 桌面的效果图。请自行截图并去掉账号、设备名等信息后放到
|
||||||
@@ -27,10 +27,11 @@
|
|||||||
|
|
||||||
| 功能 | 说明 |
|
| 功能 | 说明 |
|
||||||
|---|---|
|
|---|---|
|
||||||
| 扫码登录 | GTK 登录窗口(或 `login --no-gui` 终端二维码);二维码过期自动刷新;登录凭证失效时服务会自动弹出登录窗口 |
|
| 主窗口与扫码登录 | 一个窗口包含「本设备」(远程协助)和「账号」两部分:未登录时显示扫码二维码,登录后显示账号并可退出登录;也可用 `login --no-gui` 在终端扫码 |
|
||||||
|
| 远程协助(实验性) | 像官方客户端一样,把**设备 ID + 验证码**发给别人,对方在 UU「开始协助」里输入即可连接;登录前(访客身份)和登录后都能用,见下文 |
|
||||||
| 远程桌面 | H.264 视频,NVIDIA 显卡用 NVENC 硬件编码,否则用 libx264;查看端绘制本地鼠标,不会出现双光标 |
|
| 远程桌面 | H.264 视频,NVIDIA 显卡用 NVENC 硬件编码,否则用 libx264;查看端绘制本地鼠标,不会出现双光标 |
|
||||||
| 鼠标 / 键盘 | 绝对/相对移动、点击、滚轮、按键;修复了网络延迟导致的按键连发;Wayland 下可用 uinput 虚拟键鼠(不依赖键盘布局,见下文) |
|
| 鼠标 / 键盘 | 绝对/相对移动、点击、滚轮、按键;修复了网络延迟导致的按键连发;Wayland 下可用 uinput 虚拟键鼠(不依赖键盘布局,见下文) |
|
||||||
| 终端快捷键 | Mac 控制端的 Cmd 和 Ctrl 到被控端都是 Ctrl。焦点在终端窗口时,Ctrl+V / Cmd+V 自动转成 Ctrl+Shift+V 粘贴;Ctrl+C 始终是中断,复制请用 Cmd+Shift+C(X11) |
|
| Mac 快捷键 | **推荐**:在 Mac 远控窗口菜单里关闭「使用 macOS 快捷键」,之后和在 Mac 上一样:Cmd+C/V/A/F/X/Z 是复制、粘贴、全选、查找、剪切、撤销(终端里 Cmd+C/V 自动变成 Ctrl+Shift+C/V),Ctrl+C 在终端里始终是中断。保持开启时,客户端会把 Cmd 和 Ctrl 都当成 Ctrl 发送:终端里 Ctrl+V / Cmd+V 自动转成粘贴,Ctrl+C 是中断,复制用 Cmd+Shift+C(X11) |
|
||||||
| 鼠标指针样式 | 远端指针形状实时同步(X11) |
|
| 鼠标指针样式 | 远端指针形状实时同步(X11) |
|
||||||
| 剪贴板 | 双向文本同步 |
|
| 剪贴板 | 双向文本同步 |
|
||||||
| 画质 / 帧率 / 分辨率 | 自动、原画 20M、超清 8M、高清 2M、自定义;30/60/90/144 帧;「其他分辨率」由编码器缩放,不改动主机显示器 |
|
| 画质 / 帧率 / 分辨率 | 自动、原画 20M、超清 8M、高清 2M、自定义;30/60/90/144 帧;「其他分辨率」由编码器缩放,不改动主机显示器 |
|
||||||
@@ -40,7 +41,7 @@
|
|||||||
| 锁屏与自动解锁 | 向控制端上报锁屏状态;开启「自动解锁被控端」后,通过 PAM 校验密码并解锁当前会话 |
|
| 锁屏与自动解锁 | 向控制端上报锁屏状态;开启「自动解锁被控端」后,通过 PAM 校验密码并解锁当前会话 |
|
||||||
| 远程结束后自动锁屏 | 设置保存在本机,重连后依然生效 |
|
| 远程结束后自动锁屏 | 设置保存在本机,重连后依然生效 |
|
||||||
| 被控端静音运行 | 主机扬声器静音,控制端照常能听到声音;结束后恢复原来的音量设置 |
|
| 被控端静音运行 | 主机扬声器静音,控制端照常能听到声音;结束后恢复原来的音量设置 |
|
||||||
| 系统操作 | 锁屏、显示桌面、任务管理器(系统监视器)、重启、关机 |
|
| 系统操作 | 锁屏、显示桌面、任务管理器(系统监视器)、重启、关机(通过 `systemctl`,需要当前用户有 polkit 权限:通常本机有活动的图形会话即可;没有权限时直接失败并记日志,不会在被控端弹出密码框) |
|
||||||
| 端口映射 | 官方「端口映射」工具:把控制端本地端口转发到 Linux 能访问到的地址(如 127.0.0.1:22);`UU_PORT_MAPPING=0` 可关闭 |
|
| 端口映射 | 官方「端口映射」工具:把控制端本地端口转发到 Linux 能访问到的地址(如 127.0.0.1:22);`UU_PORT_MAPPING=0` 可关闭 |
|
||||||
| 多次重连 | 每个工具窗口独立连接,互不影响 |
|
| 多次重连 | 每个工具窗口独立连接,互不影响 |
|
||||||
| 无头服务器 | 没有显示器也能上线,提供终端、文件传输和端口映射;也可以用 Xvfb 虚拟桌面提供完整远程桌面(见下文) |
|
| 无头服务器 | 没有显示器也能上线,提供终端、文件传输和端口映射;也可以用 Xvfb 虚拟桌面提供完整远程桌面(见下文) |
|
||||||
@@ -58,12 +59,24 @@ Portal 仅作用于本服务获授权的会话。文件传输、终端等独立
|
|||||||
| Xfce | X11 | ✅ 虚拟机测试通过 | 默认 |
|
| Xfce | X11 | ✅ 虚拟机测试通过 | 默认 |
|
||||||
| Cinnamon / MATE | X11 | ✅ 虚拟机测试通过 | 默认 |
|
| Cinnamon / MATE | X11 | ✅ 虚拟机测试通过 | 默认 |
|
||||||
| Hyprland | Wayland(wlroots 原生协议) | 🧪 实验性,虚拟机测试(部分功能受限,见下) | `--features wayland` |
|
| Hyprland | Wayland(wlroots 原生协议) | 🧪 实验性,虚拟机测试(部分功能受限,见下) | `--features wayland` |
|
||||||
| GNOME / KDE Plasma | Wayland(xdg-desktop-portal) | 🧪 实验性,虚拟机测试(部分功能受限,见下) | `--features wayland,portal` |
|
| GNOME / KDE Plasma | Wayland(xdg-desktop-portal) | 🧪 实验性,验证范围与限制见下 | `--features wayland,portal` |
|
||||||
|
|
||||||
除 GNOME(X11)外,其余桌面目前都是在虚拟机里测试的。如果你在真机上遇到问题,欢迎[提交 issue](../../issues),
|
GNOME Wayland 的 Portal/EIS 路径已在 Ubuntu 26.04、GNOME 50、单屏 4K/200% 缩放下验证画面和键鼠;锁屏冷连接自动解锁已用 Mac 和 iPhone 4.42.0 客户端实机验证,其余锁屏恢复及重连仍待验收。除 GNOME 的上述路径和 X11 外,其余桌面目前主要在虚拟机里测试。如果你在真机上遇到问题,欢迎[提交 issue](../../issues),
|
||||||
请附上发行版、桌面环境、显卡型号和 `journalctl --user -u uuremote-native.service` 里的相关日志。
|
请附上发行版、桌面环境、显卡型号和 `journalctl --user -u uuremote-native.service` 里的相关日志。
|
||||||
|
|
||||||
Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端默认使用双缓冲流水线 shm screencopy,控制端帧率设置会同步到采集节拍和编码器,但实际帧率仍受合成器、渲染和读回速度限制(旧串行路径在 2560x1600 曾测得约 16~22 帧;流水线只在虚拟机里测量过,不代表实机保证);portal 后端暂不支持唤醒屏幕和动态分辨率;剪贴板只在控制端的远控窗口处于前台时同步。
|
Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端默认使用双缓冲流水线 shm screencopy,控制端帧率设置会同步到采集节拍和编码器,但实际帧率仍受合成器、渲染和读回速度限制(旧串行路径在 2560x1600 曾测得约 16~22 帧;流水线只在虚拟机里测量过,不代表实机保证);portal 后端暂不支持动态分辨率;GNOME 的屏幕唤醒与锁屏恢复见下;剪贴板只在控制端的远控窗口处于前台时同步。
|
||||||
|
|
||||||
|
GNOME 缩放桌面在使用 Portal 原生键鼠时,若支持 `ConnectToEIS` 和屏幕 `mapping_id`,通过同一授权会话的 EIS 接口注入输入,按匹配的区域与实际帧尺寸分别映射横纵坐标,没有固定分辨率或缩放倍率,可避免 `NotifyPointerMotionAbsolute` 在缩放时拒绝坐标。Portal 构建需要 libei ≥ 1.1。缺少 EIS 支持时保留旧输入路径及其既有缩放限制;其他初始化错误会明确报告并触发会话恢复。EIS 连接成功后全部输入统一使用 EIS。只采集画面或使用 uinput 时不会初始化 EIS;缺少可选的逻辑尺寸也不阻断采集。键盘沿用项目现有的 Windows VK → evdev 映射。恢复时保持编码器的采集帧尺寸不变,允许缩放后的逻辑尺寸变化。GNOME 显示配置变化后,已推流的 Portal 采集若持续「暂停且无有效格式」,会在关联通知后的 200 毫秒宽限结束后提前进入现有恢复流程;正常重协商恢复会取消提前判定,连续通知不会推迟同次异常的判定。未收到相关通知时保留五秒兜底;静态画面或有效格式下的暂停不会触发恢复。Ubuntu 26.04、GNOME Wayland 单屏 3840×2160 已实测保持连接切换 200%→166.67%→200%,恢复后画面持续更新且鼠标可控制全屏;切换期间会短暂停顿、黑屏后恢复,五秒是兜底判定阈值,并非总恢复时间。提前恢复的测量结果和验证范围见 [#35](../../pull/35)。此路径仍需实机验证锁屏恢复;多屏、连接中改变分辨率及旋转/镜像变换不属于本次修复范围。 捕获设置协议仍为版本 4:对客户端上报的分辨率统一使用像素坐标、DPR 为 1;Portal 逻辑尺寸仅用于本地输入映射。
|
||||||
|
|
||||||
|
GNOME Portal 的控制通道不再等待授权窗口或第一帧画面:先通知客户端当前选择桌面,再上报锁屏状态、接收官方客户端的「自动解锁被控端」请求。官方 Android 客户端需要桌面目标通知才会进入自动解锁流程,因此该通知不能等待采集就绪;真实屏幕尺寸、编码信息及采集权限仍按实际就绪状态上报。请在客户端启用自动解锁,使用当前 Linux 用户的登录密码;主机通过 PAM 验证后解锁当前 GNOME 会话、请求点亮屏幕,并恢复同一 Portal 会话中的画面、键鼠和剪贴板。首次授权或权限被撤销时仍需本机确认。物理或逻辑分辨率变化后需要重连。
|
||||||
|
|
||||||
|
桌面锁屏且尚未建立 Portal 时(冷连接),主机也会建立控制通道:上报锁屏状态并接受自动解锁,解锁后再恢复画面、键鼠和剪贴板。Portal 等待就绪期间,主机默认通过已协商的视频轨每秒发送一帧生成的纯黑画面,让官方 iPhone 客户端进入自动解锁流程,无需额外配置。Ubuntu GNOME Wayland 上已用官方 Mac 客户端验证;iPhone 4.42.0 的 H.265 冷连接完成了同一实验二进制「开启黑帧成功 → 关闭失败 → 再开成功」对照([#29](../../issues/29))。**局限**:黑帧不是真实 GNOME 锁屏画面,也不支持在锁屏界面操作原生键鼠;需要在 UU 客户端 控制中心 → 安全 → 自动解锁被控端 中启用自动解锁并输入密码。若本机没有已保存的 Portal 授权,解锁后仍需本机确认授权才能显示真实桌面。
|
||||||
|
|
||||||
|
无需修改现有熄屏和锁屏设置。主机仅在会话已解锁的采集恢复期间临时申请 GNOME 的 idle 抑制,拿到首帧后立即释放;锁屏、断开或初始化失败也会释放。远控窗口空闲时仍按原策略熄屏和锁屏,手动锁屏也继续生效。锁屏及锁状态未知时停止桌面输入并隐藏真实画面;独立终端、文件传输和端口映射连接不依赖画面初始化,也不会因桌面重连被关闭。这一恢复路径已覆盖自动化协议与生命周期回归测试,锁屏冷连接后的自动解锁与采集恢复已用官方 Mac 和 iPhone 4.42.0 客户端在 GNOME 实机验证,熄屏等其余场景仍待验收;不支持从整机挂起或休眠状态通过现有控制通道唤醒。
|
||||||
|
|
||||||
|
锁屏黑帧使用 FFmpeg 的 libx264/libx265 编码器(按已协商的 H.264/H.265 选择),硬件编码器可用并不代表这些软件编码器已安装。编码失败会明确记录日志。黑帧不改变采集权限或上报的真实屏幕尺寸;解锁或锁状态未知时暂停,采集就绪、拒绝或连接关闭时结束,再由真实视频接续同一条视频轨。对照实验与验证范围见 [#29](../../issues/29)、[#34](../../pull/34)。
|
||||||
|
|
||||||
|
原生 Wayland 后端需要合成器提供 `wlr-output-power-management` 才能唤醒已关闭的屏幕;缺少该协议不影响采集和编码。
|
||||||
|
|
||||||
原生 Wayland 实验选项:`UU_WAYLAND_PIPELINE=0` 可关闭流水线做对照。`UU_WAYLAND_DMABUF=1` 显式尝试 dma-buf→GBM CPU 映射(默认关闭,需要运行时 libgbm;可用 `UU_WAYLAND_DMABUF_DEVICE` 指定 render node,默认 `/dev/dri/renderD128`)。仅支持显式线性修饰符、单平面 8-bit RGB,失败回退 SHM;virgl VM 中只验证了回退,成功导入/映射和硬件加速均未验证。这不是零拷贝 VAAPI 路径;选择 `UU_VIDEO_ENCODER=vaapi` 时仍由 FFmpeg 上传 CPU 帧。
|
原生 Wayland 实验选项:`UU_WAYLAND_PIPELINE=0` 可关闭流水线做对照。`UU_WAYLAND_DMABUF=1` 显式尝试 dma-buf→GBM CPU 映射(默认关闭,需要运行时 libgbm;可用 `UU_WAYLAND_DMABUF_DEVICE` 指定 render node,默认 `/dev/dri/renderD128`)。仅支持显式线性修饰符、单平面 8-bit RGB,失败回退 SHM;virgl VM 中只验证了回退,成功导入/映射和硬件加速均未验证。这不是零拷贝 VAAPI 路径;选择 `UU_VIDEO_ENCODER=vaapi` 时仍由 FFmpeg 上传 CPU 帧。
|
||||||
首次连接时,桌面会弹窗请求「远程桌面 / 屏幕共享」授权,同意后会记住授权。
|
首次连接时,桌面会弹窗请求「远程桌面 / 屏幕共享」授权,同意后会记住授权。
|
||||||
@@ -73,15 +86,15 @@ Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端
|
|||||||
| 功能 | 状态 |
|
| 功能 | 状态 |
|
||||||
|---|---|
|
|---|---|
|
||||||
| 多显示器切换 | 开发中 |
|
| 多显示器切换 | 开发中 |
|
||||||
| 真彩(4:4:4) | 未实现,握手时不声明,客户端不会启用 |
|
| 真彩(4:4:4) | 支持(v0.1.15):NVENC / libx264 的默认 FFmpeg 管线输出 H.264 High 4:4:4;VAAPI 和 `inproc-av` 管线仍为 4:2:0,并如实回报给客户端 |
|
||||||
| HDR | 未实现 |
|
| HDR | 未实现 |
|
||||||
| 超级屏 / 虚拟显示器 | 未实现 |
|
| 超级屏 / 虚拟显示器 | 未实现 |
|
||||||
| 被控端防窥模式 | 未实现 |
|
| 被控端防窥模式 | 未实现 |
|
||||||
| 麦克风(控制端声音传到被控端) | 未实现(需要虚拟声卡) |
|
| 麦克风(控制端声音传到被控端) | 未实现(需要虚拟声卡) |
|
||||||
| 魔兽世界排队提醒等插件 | 不支持,会明确告诉客户端「无插件」 |
|
| 魔兽世界排队提醒等插件 | 不支持,会明确告诉客户端「无插件」 |
|
||||||
| 登录前(登录界面 / 无人登录)访问 | 不支持控制登录界面;没有人登录时可以用无头模式或虚拟桌面(见下文) |
|
| Linux 系统登录界面 | 还不支持控制开机后的系统登录界面(还没有人登录 Linux 桌面时);这种情况可以用无头模式或虚拟桌面(见下文)。注意:这里说的是 Linux 系统登录,**不登录 UU 账号**现在可以通过「远程协助」使用 |
|
||||||
| 跨运营商 / 受限网络的中继连接 | 已接入官方下发的 ICE 服务器,但复杂网络环境未充分测试 |
|
| 跨运营商 / 受限网络的中继连接 | 已接入官方下发的 ICE 服务器,但复杂网络环境未充分测试 |
|
||||||
| 作为控制端(在 Linux 上控制别的电脑) | 不在本项目范围 |
|
| 作为控制端(在 Linux 上控制别的电脑) | 🧪 实验性 `uuremote-viewer`,见[下文](#实验性原生控制端uuremote-viewer) |
|
||||||
|
|
||||||
## 安装
|
## 安装
|
||||||
|
|
||||||
@@ -94,7 +107,7 @@ sudo apt install build-essential pkg-config \
|
|||||||
# 可选:Wayland 后端
|
# 可选:Wayland 后端
|
||||||
sudo apt install libwayland-dev libxkbcommon-dev wl-clipboard
|
sudo apt install libwayland-dev libxkbcommon-dev wl-clipboard
|
||||||
# 可选:GNOME/KDE portal 后端
|
# 可选:GNOME/KDE portal 后端
|
||||||
sudo apt install libglib2.0-dev libpipewire-0.3-dev
|
sudo apt install libglib2.0-dev libpipewire-0.3-dev libei-dev
|
||||||
```
|
```
|
||||||
|
|
||||||
- 需要 Rust 工具链(<https://rustup.rs>)。
|
- 需要 Rust 工具链(<https://rustup.rs>)。
|
||||||
@@ -164,6 +177,27 @@ sudo udevadm control --reload && sudo udevadm trigger --name-match=uinput
|
|||||||
`uaccess` 只把权限给当前坐在这台机器前的登录用户。`UU_INPUT_BACKEND=uinput|native` 可以强制指定后端。
|
`uaccess` 只把权限给当前坐在这台机器前的登录用户。`UU_INPUT_BACKEND=uinput|native` 可以强制指定后端。
|
||||||
X11 默认继续用 XTest,因为它能正确处理网络延迟下按住不放的按键。
|
X11 默认继续用 XTest,因为它能正确处理网络延迟下按住不放的按键。
|
||||||
|
|
||||||
|
## 远程协助与主窗口(实验性)
|
||||||
|
|
||||||
|
和官方客户端的「远程协助」一样:本机显示**设备 ID** 和**验证码**,把它们发给信任的人,对方在 UU 远程的「开始协助」里输入即可控制本机。
|
||||||
|
|
||||||
|
- **未登录**:启动后直接显示主窗口,用访客身份提供远程协助,同时显示扫码登录二维码。扫码登录后自动切换到账号模式。
|
||||||
|
- **已登录**:本机出现在你的设备列表里,可以直接连;远程协助也照常可用(同一设备 ID)。服务在后台运行时窗口默认隐藏,用 `uuremote-server show` 或应用菜单里的「UU 远程 Linux 主机」打开。关闭窗口只是隐藏,不会停止服务。
|
||||||
|
- **托盘图标**:后台运行时顶栏/任务栏有托盘图标,菜单里可以「打开主窗口」、切换「允许他人远程协助」、「退出」。GNOME 需要 AppIndicator 扩展(Ubuntu 自带),Xfce / Cinnamon / KDE 直接可用。从托盘退出会停止 `uuremote-native.service`,之后从应用菜单打开会重新启动服务。
|
||||||
|
- **设置**(与官方一致):「允许他人远程协助」开关(默认关闭);验证方式:仅临时验证码 / 仅自定义验证码(8–16 位,需同时包含字母和数字) / 两种都可以;临时验证码自动刷新:每次远控结束 / 每小时 / 每 12 小时 / 每 24 小时 / 不刷新;ID 和验证码旁各有「复制」按钮。设置保存在本机配置文件(0600)。
|
||||||
|
- **只在本机窗口显示**:验证码不会写进日志。没有图形界面时,单独运行 `uuremote-server assist` 会在终端显示(`allow` / `deny` / `reset` / `mode …` / `refresh …` / `off`)。
|
||||||
|
- **安全**:拿到 ID 和验证码的人可以完全控制这台电脑(桌面、文件、终端)。只发给信任的人,用完关掉开关或刷新验证码。
|
||||||
|
|
||||||
|
安装应用菜单图标(可选):
|
||||||
|
|
||||||
|
```sh
|
||||||
|
install -Dm644 contrib/desktop/uu-remote-linux.svg ~/.local/share/icons/hicolor/scalable/apps/uu-remote-linux.svg
|
||||||
|
install -Dm644 contrib/desktop/uu-remote-linux.desktop ~/.local/share/applications/uu-remote-linux.desktop
|
||||||
|
```
|
||||||
|
|
||||||
|
验证情况:访客身份下的连接、错误验证码拒绝、用完换码、关闭后不可连接、登录与退出登录的切换,已用官方 Mac 客户端实测通过。
|
||||||
|
**登录状态下的远程协助尚未用另一个账号实测**(同一账号的设备不能互相发起远程协助,这是官方限制)。
|
||||||
|
|
||||||
## 没有显示器的服务器
|
## 没有显示器的服务器
|
||||||
|
|
||||||
**无头模式**:只要终端、文件传输和端口映射,不需要图形界面。在服务里加 `Environment=UU_DESKTOP_BACKEND=none`,
|
**无头模式**:只要终端、文件传输和端口映射,不需要图形界面。在服务里加 `Environment=UU_DESKTOP_BACKEND=none`,
|
||||||
@@ -203,10 +237,16 @@ systemctl --user restart uuremote-native.service
|
|||||||
| `UU_FFMPEG_ROOT` | 使用指定目录下的 FFmpeg |
|
| `UU_FFMPEG_ROOT` | 使用指定目录下的 FFmpeg |
|
||||||
| `UU_AUDIO=0` | 关闭声音转发;`UU_AUDIO_SOURCE` 指定 PulseAudio 采集源 |
|
| `UU_AUDIO=0` | 关闭声音转发;`UU_AUDIO_SOURCE` 指定 PulseAudio 采集源 |
|
||||||
| `UU_LOGIN_WINDOW=0` | 凭证缺失时不自动弹出登录窗口 |
|
| `UU_LOGIN_WINDOW=0` | 凭证缺失时不自动弹出登录窗口 |
|
||||||
|
| `UU_REMOTE_ASSIST=0` | 关闭远程协助(未登录时也不再以访客身份提供协助) |
|
||||||
|
| `UU_ASSIST_ALLOW=1` | 启动时就允许他人远程协助(覆盖窗口里保存的开关状态) |
|
||||||
| `UU_PORT_MAPPING=0` | 关闭端口映射(对应官方被控端的开关) |
|
| `UU_PORT_MAPPING=0` | 关闭端口映射(对应官方被控端的开关) |
|
||||||
|
|
||||||
「远程结束后自动锁屏」「静音运行」等设置保存在 `~/.config/uuremote/host_settings.json`。
|
「远程结束后自动锁屏」「静音运行」等设置保存在 `~/.config/uuremote/host_settings.json`。
|
||||||
|
|
||||||
|
选择码率档(原画 / 超清 / 高清 / 自定义)时,外部 FFmpeg 的 CPU 编码以平均码率(ABR)为目标,避免 4K 低码率下关键帧发灰;「自动」档仍使用恒定质量(CRF)。
|
||||||
|
|
||||||
|
本地编码诊断 `desktop-video-probe [output.h264]` 可设置 `UU_PROBE_FPS`、`UU_PROBE_FRAMES`(默认 5)和 `UU_PROBE_QUALITY`(1–5);设置 `UU_PROBE_INPUT=clip.y4m`(也支持 `.mp4`、`frames/%05d.png`)可绕过桌面采集,以相同的生产 FFmpeg 编码参数测量固定输入并打印分辨率和编码统计,`UU_PROBE_LOOP=1` 循环到指定帧数,输入无法打开或帧数不足时以非零状态退出(固定输入始终使用外部 FFmpeg 管线)。
|
||||||
|
|
||||||
## 让 AI 帮你安装或开发
|
## 让 AI 帮你安装或开发
|
||||||
|
|
||||||
如果你用 Claude Code、Codex、Cursor 等 AI 编程工具,让它先阅读 [AGENTS.md](AGENTS.md)。
|
如果你用 Claude Code、Codex、Cursor 等 AI 编程工具,让它先阅读 [AGENTS.md](AGENTS.md)。
|
||||||
@@ -251,6 +291,14 @@ flowchart LR
|
|||||||
- [webrtc-rs](https://github.com/webrtc-rs/webrtc)、FFmpeg、GTK 等开源项目。
|
- [webrtc-rs](https://github.com/webrtc-rs/webrtc)、FFmpeg、GTK 等开源项目。
|
||||||
- 本项目的大部分代码在 Claude 和 Codex 的协助下完成。
|
- 本项目的大部分代码在 Claude 和 Codex 的协助下完成。
|
||||||
|
|
||||||
|
### H.265 / HEVC
|
||||||
|
|
||||||
|
控制端和主机都支持 H.265 时,主机自动使用 H.265(与官方主机一致,由主机按双方能力选择;官方 Mac 客户端在 SDP
|
||||||
|
中优先列出 H.265)。首次桌面连接会用合成帧探测实际编码能力,并通过 CodecNegotiation 告知客户端。支持 `hevc_nvenc`、
|
||||||
|
`libx265`、显式 `UU_VIDEO_ENCODER=vaapi` 的 `hevc_vaapi`;硬件失败先回落软件 HEVC,再回落 H.264。
|
||||||
|
`UU_VIDEO_CODEC=h264` 强制使用 H.264。8-bit;真彩 4:4:4 使用 hevc_nvenc `rext` / libx265 `main444-8`。
|
||||||
|
已用官方 Mac 客户端在 RTX 3090(hevc_nvenc,含 4:4:4)实机验收;libx265、hevc_vaapi 及其他客户端尚未实机验证。
|
||||||
|
|
||||||
### VAAPI 硬件编码(显式启用)
|
### VAAPI 硬件编码(显式启用)
|
||||||
|
|
||||||
设置 `UU_VIDEO_ENCODER=vaapi` 使用 Intel/AMD H.264 VAAPI;`UU_VAAPI_DEVICE` 默认
|
设置 `UU_VIDEO_ENCODER=vaapi` 使用 Intel/AMD H.264 VAAPI;`UU_VAAPI_DEVICE` 默认
|
||||||
@@ -287,10 +335,27 @@ cargo build --release --features wayland
|
|||||||
|
|
||||||
Omarchy 使用 Hyprland:在其用户会话中运行,设置 `UU_DESKTOP_BACKEND=wayland`,保留
|
Omarchy 使用 Hyprland:在其用户会话中运行,设置 `UU_DESKTOP_BACKEND=wayland`,保留
|
||||||
`WAYLAND_DISPLAY` 和 `XDG_RUNTIME_DIR`。其他合成器可能需要 `--features wayland,portal`。
|
`WAYLAND_DISPLAY` 和 `XDG_RUNTIME_DIR`。其他合成器可能需要 `--features wayland,portal`。
|
||||||
uinput 需加载内核模块并应用上文 uaccess 规则;无人登录账号需要专用组的受限授权。
|
uinput 需加载内核模块(开机自动加载:`echo uinput | sudo tee /etc/modules-load.d/uinput.conf`)并应用上文 uaccess 规则;无人登录账号需要专用组的受限授权。
|
||||||
该权限能向整个 seat(包括锁屏/登录界面)注入输入。设置 `UU_INPUT_BACKEND=uinput` 后,
|
该权限能向整个 seat(包括锁屏/登录界面)注入输入。设置 `UU_INPUT_BACKEND=uinput` 后,
|
||||||
仍须确认启动日志实际选择了 uinput。
|
仍须确认启动日志实际选择了 uinput。
|
||||||
|
|
||||||
|
Hyprland / uwsm 等会话中,`default.target` 可能早于合成器导出 `WAYLAND_DISPLAY`。systemd 单元可改为随图形会话启动
|
||||||
|
(社区在 Omarchy 上验证过开机自启):
|
||||||
|
|
||||||
|
```ini
|
||||||
|
[Unit]
|
||||||
|
After=graphical-session.target
|
||||||
|
PartOf=graphical-session.target
|
||||||
|
ConditionEnvironment=WAYLAND_DISPLAY
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Environment=UU_DESKTOP_BACKEND=wayland
|
||||||
|
# 其余同上文 X11 示例,去掉 DISPLAY
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=graphical-session.target
|
||||||
|
```
|
||||||
|
|
||||||
VAAPI 可选包:Intel 新显卡用 `intel-media-driver`,AMD 用 `libva-mesa-driver`,`libva-utils` 提供
|
VAAPI 可选包:Intel 新显卡用 `intel-media-driver`,AMD 用 `libva-mesa-driver`,`libva-utils` 提供
|
||||||
`vainfo`。KMS 依赖 `libdrm`。虚拟 Xfce 依赖 `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
|
`vainfo`。KMS 依赖 `libdrm`。虚拟 Xfce 依赖 `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
|
||||||
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`。
|
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`。
|
||||||
@@ -305,3 +370,38 @@ xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`。
|
|||||||
当前仅为线性帧缓冲原型:VM 的 GDM 截图仍是黑屏,**没有通过登录界面视觉验收**,不承诺锁屏可用。
|
当前仅为线性帧缓冲原型:VM 的 GDM 截图仍是黑屏,**没有通过登录界面视觉验收**,不承诺锁屏可用。
|
||||||
键鼠需 uinput;没有桌面会话时不提供剪贴板。
|
键鼠需 uinput;没有桌面会话时不提供剪贴板。
|
||||||
无头模式下桌面连接没有找到官方的「无桌面」错误码,因此保持原有 offer 行为。
|
无头模式下桌面连接没有找到官方的「无桌面」错误码,因此保持原有 offer 行为。
|
||||||
|
|
||||||
|
## 实验性原生控制端(uuremote-viewer)
|
||||||
|
|
||||||
|
`uuremote-viewer` 是 Linux 上的**控制端**:用你的 UU 账号连接并控制其他设备(官方 Windows / Mac 被控端,或本项目的 Linux 主机),
|
||||||
|
也可以凭对方的设备 ID 和验证码发起远程协助。它是实验性功能,与主机 `uuremote-server` 相互独立,默认主机构建和行为不变。
|
||||||
|
|
||||||
|
**获取**:Release 包从本版本起附带 `uuremote-viewer`(x86_64)。运行时需要 GTK3、SDL2(Ubuntu/Debian:`libsdl2-2.0-0`)和
|
||||||
|
`ffmpeg` 命令;剪贴板需要 `wl-clipboard`(Wayland)或 `xclip`/`xsel`(X11);终端功能需要本机终端程序(可用 `TERMINAL` 指定)。
|
||||||
|
从源码构建:`sudo apt install libsdl2-dev`,然后 `cargo build --release --locked --features native-viewer,viewer-sdl --bin uuremote-viewer`。
|
||||||
|
|
||||||
|
**使用**:
|
||||||
|
- `uuremote-viewer login`:扫码登录(复用同目录下 `uuremote-server` 的登录窗口;`--no-gui` 为终端二维码)。控制端与主机共用
|
||||||
|
`~/.config/uuremote/config.json`(可用 `UU_CONFIG` 另指定),因此**不能用同一身份连接本机上的主机**;控制其他设备不受影响。
|
||||||
|
- `uuremote-viewer ui`:启动窗口,列出账号下的设备,可进入桌面、只看模式、文件传输、终端、端口映射、重启/关机、远程协助。
|
||||||
|
- 命令行:`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`、
|
||||||
|
`assist CONNECT_ID`(验证码从 stdin 读取一行,不能作为参数传入)。`uuremote-viewer --help` 查看全部参数。
|
||||||
|
- 桌面窗口中按 Ctrl+Alt+F 打开文件窗口(复用同一连接)。`UU_VIEWER_AUDIO=0` 关闭声音,`UU_VIEWER_CLIPBOARD=0` 关闭剪贴板,
|
||||||
|
`UU_VIEWER_VIEW_ONLY=1` 只看不控,`UU_VIEWER_FFMPEG` 指定 ffmpeg 路径。
|
||||||
|
|
||||||
|
**已实测**(作者环境,非全面验收):
|
||||||
|
| 功能 | 官方 Windows 被控端 | 官方 Mac 被控端 | 本项目 Linux 主机 |
|
||||||
|
|---|---|---|---|
|
||||||
|
| 桌面画面(H.264) | ✅ | ✅ | ✅ |
|
||||||
|
| 键盘、鼠标 | ✅ | — | ✅ |
|
||||||
|
| 文件列表 / 下载 / 上传 | ✅ 列表、下载 | — | ✅ |
|
||||||
|
| 端口映射、终端 | — | — | ✅ |
|
||||||
|
| 剪贴板(文本,双向)、声音 | — | — | ✅(虚拟机) |
|
||||||
|
| 远程协助(ID + 验证码) | — | — | ✅(虚拟机,访客主机) |
|
||||||
|
| 终端会话列表(新建、双击接回、结束) | — | — | ✅(虚拟机) |
|
||||||
|
| 重启、关机 | — | — | ✅(虚拟机) |
|
||||||
|
| Wayland 下的桌面窗口与剪贴板(Hyprland) | — | — | ✅(虚拟机) |
|
||||||
|
|
||||||
|
「—」表示尚未测试。重启/关机只应在可以重启的机器上使用。**未实测**:GNOME/KDE Wayland 下的窗口与剪贴板、
|
||||||
|
H.265 与 4:4:4(目前只解码 H.264 4:2:0)、多显示器。日志只记录字段号、大小和状态,不记录键盘、剪贴板、文件内容、验证码或凭据。
|
||||||
@@ -2,7 +2,9 @@ fn main() {
|
|||||||
println!("cargo:rerun-if-changed=c_engine/portal_clipboard.inc");
|
println!("cargo:rerun-if-changed=c_engine/portal_clipboard.inc");
|
||||||
println!("cargo:rerun-if-changed=c_engine/x11_engine.c");
|
println!("cargo:rerun-if-changed=c_engine/x11_engine.c");
|
||||||
println!("cargo:rerun-if-changed=c_engine/clipboard.c");
|
println!("cargo:rerun-if-changed=c_engine/clipboard.c");
|
||||||
println!("cargo:rerun-if-changed=c_engine/login_window.c");
|
println!("cargo:rerun-if-changed=c_engine/assist_window.c");
|
||||||
|
println!("cargo:rerun-if-changed=c_engine/viewer_window.c");
|
||||||
|
println!("cargo:rerun-if-changed=c_engine/viewer_tools.c");
|
||||||
println!("cargo:rerun-if-changed=c_engine/pam_check.c");
|
println!("cargo:rerun-if-changed=c_engine/pam_check.c");
|
||||||
println!("cargo:rerun-if-env-changed=UU_NO_GTK");
|
println!("cargo:rerun-if-env-changed=UU_NO_GTK");
|
||||||
println!("cargo:rustc-check-cfg=cfg(uu_gtk)");
|
println!("cargo:rustc-check-cfg=cfg(uu_gtk)");
|
||||||
@@ -36,7 +38,7 @@ fn main() {
|
|||||||
match (std::env::var_os("UU_NO_GTK").is_none()).then(|| gtk("--cflags").zip(gtk("--libs"))).flatten() {
|
match (std::env::var_os("UU_NO_GTK").is_none()).then(|| gtk("--cflags").zip(gtk("--libs"))).flatten() {
|
||||||
Some((cflags, libs)) => {
|
Some((cflags, libs)) => {
|
||||||
let mut build = cc::Build::new();
|
let mut build = cc::Build::new();
|
||||||
build.file("c_engine/login_window.c").flag_if_supported("-Wno-deprecated-declarations");
|
build.file("c_engine/assist_window.c").file("c_engine/viewer_window.c").file("c_engine/viewer_tools.c").flag_if_supported("-Wno-deprecated-declarations");
|
||||||
for flag in cflags.split_whitespace() { build.flag(flag); }
|
for flag in cflags.split_whitespace() { build.flag(flag); }
|
||||||
build.compile("uu_login_window");
|
build.compile("uu_login_window");
|
||||||
for flag in libs.split_whitespace() {
|
for flag in libs.split_whitespace() {
|
||||||
@@ -51,14 +53,16 @@ fn main() {
|
|||||||
if std::env::var_os("CARGO_FEATURE_PORTAL").is_some() {
|
if std::env::var_os("CARGO_FEATURE_PORTAL").is_some() {
|
||||||
println!("cargo:rerun-if-changed=c_engine/portal_engine.c");
|
println!("cargo:rerun-if-changed=c_engine/portal_engine.c");
|
||||||
println!("cargo:rerun-if-changed=c_engine/portal_engine.h");
|
println!("cargo:rerun-if-changed=c_engine/portal_engine.h");
|
||||||
|
println!("cargo:rerun-if-changed=c_engine/portal_eis.inc");
|
||||||
|
println!("cargo:rerun-if-changed=c_engine/desktop_power.c");
|
||||||
let flags=|kind:&str| {
|
let flags=|kind:&str| {
|
||||||
let output=std::process::Command::new("pkg-config").args([kind,"gio-2.0","gio-unix-2.0","libpipewire-0.3"]).output()
|
let output=std::process::Command::new("pkg-config").args([kind,"gio-2.0","gio-unix-2.0","libpipewire-0.3","libei-1.0 >= 1.1"]).output()
|
||||||
.expect("Install pkg-config and GIO/PipeWire development libraries");
|
.expect("Install pkg-config and GIO/PipeWire/libei development libraries");
|
||||||
assert!(output.status.success(),"Portal requires GIO and PipeWire development libraries");
|
assert!(output.status.success(),"Portal requires GIO, PipeWire and libei >= 1.1 development libraries");
|
||||||
String::from_utf8(output.stdout).expect("pkg-config flags must be UTF-8")
|
String::from_utf8(output.stdout).expect("pkg-config flags must be UTF-8")
|
||||||
};
|
};
|
||||||
let mut build=cc::Build::new();
|
let mut build=cc::Build::new();
|
||||||
build.file("c_engine/portal_engine.c").flag_if_supported("-Wno-unused-parameter");
|
build.file("c_engine/portal_engine.c").file("c_engine/desktop_power.c").flag_if_supported("-Wno-unused-parameter");
|
||||||
for flag in flags("--cflags").split_whitespace() { build.flag(flag); }
|
for flag in flags("--cflags").split_whitespace() { build.flag(flag); }
|
||||||
build.compile("uu_portal_engine");
|
build.compile("uu_portal_engine");
|
||||||
for flag in flags("--libs").split_whitespace() {
|
for flag in flags("--libs").split_whitespace() {
|
||||||
|
|||||||
@@ -0,0 +1,644 @@
|
|||||||
|
// GTK3 window for guest remote assistance (`uuremote-server assist`). Layout follows the official
|
||||||
|
// "本设备" card (allow switch, device ID, temporary code with show/hide, copy & share) without any
|
||||||
|
// NetEase assets. GTK runs on its own thread; Rust pushes state through g_idle_add and polls the
|
||||||
|
// user's commands. The code is only drawn in this local window and copied on explicit request.
|
||||||
|
#include <gtk/gtk.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <stdatomic.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <dlfcn.h>
|
||||||
|
|
||||||
|
static const char *CSS =
|
||||||
|
"window { background-color: #f5f6f8; }\n"
|
||||||
|
"* { font-family: 'Noto Sans CJK SC', 'Noto Sans SC', sans-serif; }\n"
|
||||||
|
".card { background-color: #ffffff; border-radius: 10px; border: 1px solid #e5e7eb; }\n"
|
||||||
|
".heading { font-size: 20px; font-weight: 700; color: #1d1d1f; }\n"
|
||||||
|
".key { font-size: 14px; color: #4b5563; }\n"
|
||||||
|
".big { font-size: 32px; font-weight: 800; color: #111111; }\n"
|
||||||
|
".note { font-size: 13px; color: #9ca3af; }\n"
|
||||||
|
".hint { font-size: 13px; color: #6b7280; }\n"
|
||||||
|
".footer { font-size: 12px; color: #9ca3af; }\n"
|
||||||
|
"button.flat { padding: 4px 8px; }\n"
|
||||||
|
"button.share { background-image: none; background-color: #ffffff; border: 1px solid #d1d5db;"
|
||||||
|
" border-radius: 6px; padding: 6px 14px; font-size: 14px; }\n"
|
||||||
|
"separator { background-color: #e5e7eb; min-height: 1px; }\n"
|
||||||
|
".user { font-size: 22px; font-weight: 700; color: #111111; }\n"
|
||||||
|
".status { font-size: 13px; color: #2f6bff; }\n"
|
||||||
|
".status.ok { color: #16a34a; }\n"
|
||||||
|
".status.error { color: #dc2626; }\n";
|
||||||
|
|
||||||
|
enum { CMD_NONE = 0, CMD_ALLOW = 1, CMD_DENY = 2, CMD_RESET = 3, CMD_STOP = 4, CMD_VERIFY = 5, CMD_REFRESH = 6, CMD_CUSTOM = 7, CMD_LOGOUT = 8, CMD_QUIT = 9 };
|
||||||
|
enum { VERIFY_TEMPORARY = 0, VERIFY_CUSTOM = 1, VERIFY_BOTH = 2 };
|
||||||
|
|
||||||
|
typedef struct { char *id; char *code; int allowed, verify, refresh, has_custom; } State;
|
||||||
|
typedef struct { int cmd; char arg[40]; } Cmd;
|
||||||
|
|
||||||
|
// Small FIFO so quick successive UI actions (e.g. set code, then mode) are all delivered.
|
||||||
|
static Cmd queue[16];
|
||||||
|
static int q_head, q_len;
|
||||||
|
static pthread_mutex_t q_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
static atomic_int closed;
|
||||||
|
static pthread_t ui_thread;
|
||||||
|
static int ui_started, init_state; // 0 pending, 1 ok, -1 failed
|
||||||
|
static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
static pthread_cond_t init_cond = PTHREAD_COND_INITIALIZER;
|
||||||
|
static GtkWidget *window, *allow_switch, *id_label, *code_label, *eye_button, *share_button, *hint_label, *verify_combo, *note_label;
|
||||||
|
static GtkWidget *refresh_items[5], *footer_label;
|
||||||
|
// Account card: QR login before sign-in, user info (+ logout) after.
|
||||||
|
static GtkWidget *assist_card, *account_card, *account_stack, *qr_stack, *qr_image, *login_status, *user_label;
|
||||||
|
static int show_assist = 1, show_account;
|
||||||
|
// Tray (host service only): AppIndicator via dlopen (no build dependency), else GtkStatusIcon.
|
||||||
|
static GtkWidget *tray_menu, *tray_allow;
|
||||||
|
static int suppress_tray;
|
||||||
|
static int cur_verify, cur_refresh, cur_has_custom;
|
||||||
|
static char current_id[64], current_code[64];
|
||||||
|
static int code_visible = 1, suppress_switch, start_hidden, close_hides;
|
||||||
|
|
||||||
|
static GtkWidget *label(const char *text, const char *cls) {
|
||||||
|
GtkWidget *l = gtk_label_new(text);
|
||||||
|
gtk_style_context_add_class(gtk_widget_get_style_context(l), cls);
|
||||||
|
gtk_widget_set_halign(l, GTK_ALIGN_START);
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void post_arg(int cmd, const char *arg) {
|
||||||
|
pthread_mutex_lock(&q_lock);
|
||||||
|
if (q_len < 16) {
|
||||||
|
Cmd *c = &queue[(q_head + q_len) % 16];
|
||||||
|
c->cmd = cmd; g_strlcpy(c->arg, arg ? arg : "", sizeof c->arg); q_len++;
|
||||||
|
}
|
||||||
|
pthread_mutex_unlock(&q_lock);
|
||||||
|
}
|
||||||
|
static void post(int cmd) { post_arg(cmd, NULL); }
|
||||||
|
static void post_int(int cmd, int v) { char b[8]; g_snprintf(b, sizeof b, "%d", v); post_arg(cmd, b); }
|
||||||
|
|
||||||
|
// 8-16 ASCII letters/digits including both, as the official custom code dialog requires.
|
||||||
|
static int valid_custom(const char *c) {
|
||||||
|
size_t n = strlen(c); int alpha = 0, digit = 0;
|
||||||
|
if (n < 8 || n > 16) return 0;
|
||||||
|
for (size_t i = 0; i < n; i++) {
|
||||||
|
if (g_ascii_isalpha(c[i])) alpha = 1; else if (g_ascii_isdigit(c[i])) digit = 1; else return 0;
|
||||||
|
}
|
||||||
|
return alpha && digit;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_custom_changed(GtkEditable *e, gpointer ok) {
|
||||||
|
gtk_widget_set_sensitive(GTK_WIDGET(ok), valid_custom(gtk_entry_get_text(GTK_ENTRY(e))));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns 1 when a new custom code was entered and posted.
|
||||||
|
static int ask_custom_code(void) {
|
||||||
|
GtkWidget *d = gtk_dialog_new_with_buttons("设置自定义验证码", GTK_WINDOW(window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||||
|
"取消", GTK_RESPONSE_CANCEL, NULL);
|
||||||
|
GtkWidget *ok = gtk_dialog_add_button(GTK_DIALOG(d), "确定", GTK_RESPONSE_OK);
|
||||||
|
gtk_widget_set_sensitive(ok, FALSE);
|
||||||
|
GtkWidget *area = gtk_dialog_get_content_area(GTK_DIALOG(d));
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(area), 16);
|
||||||
|
gtk_box_set_spacing(GTK_BOX(area), 8);
|
||||||
|
GtkWidget *entry = gtk_entry_new();
|
||||||
|
gtk_entry_set_max_length(GTK_ENTRY(entry), 16);
|
||||||
|
gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
|
||||||
|
gtk_entry_set_placeholder_text(GTK_ENTRY(entry), "请输入自定义验证码");
|
||||||
|
g_signal_connect(entry, "changed", G_CALLBACK(on_custom_changed), ok);
|
||||||
|
gtk_box_pack_start(GTK_BOX(area), entry, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(area), label("8-16 位,需同时包含字母和数字;不会自动刷新,请妥善保管", "note"), FALSE, FALSE, 0);
|
||||||
|
gtk_widget_show_all(d);
|
||||||
|
int done = 0;
|
||||||
|
if (gtk_dialog_run(GTK_DIALOG(d)) == GTK_RESPONSE_OK) {
|
||||||
|
const char *code = gtk_entry_get_text(GTK_ENTRY(entry));
|
||||||
|
if (valid_custom(code)) { post_arg(CMD_CUSTOM, code); done = 1; }
|
||||||
|
}
|
||||||
|
gtk_widget_destroy(d);
|
||||||
|
return done;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int suppress_combo;
|
||||||
|
static void on_verify_changed(GtkComboBox *c, gpointer d) {
|
||||||
|
(void)d;
|
||||||
|
if (suppress_combo) return;
|
||||||
|
int v = gtk_combo_box_get_active(c);
|
||||||
|
if (v != VERIFY_TEMPORARY && !cur_has_custom && !ask_custom_code()) {
|
||||||
|
suppress_combo = 1; gtk_combo_box_set_active(c, cur_verify); suppress_combo = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
post_int(CMD_VERIFY, v);
|
||||||
|
}
|
||||||
|
static void on_set_custom(GtkButton *b, gpointer d) { (void)b; (void)d; ask_custom_code(); }
|
||||||
|
|
||||||
|
static int suppress_refresh;
|
||||||
|
static void on_refresh_item(GtkCheckMenuItem *item, gpointer data) {
|
||||||
|
if (suppress_refresh || !gtk_check_menu_item_get_active(item)) return;
|
||||||
|
post_int(CMD_REFRESH, GPOINTER_TO_INT(data));
|
||||||
|
}
|
||||||
|
static void on_refresh_now(GtkMenuItem *i, gpointer d) { (void)i; (void)d; post(CMD_RESET); }
|
||||||
|
|
||||||
|
static const char *REFRESH_NOTES[5] = { "每次远控结束刷新", "每小时自动刷新", "每 12 小时自动刷新", "每 24 小时自动刷新", "不自动刷新" };
|
||||||
|
|
||||||
|
static void render_code(void) {
|
||||||
|
if (!code_label) return;
|
||||||
|
gtk_label_set_text(GTK_LABEL(code_label), current_code[0] ? (code_visible ? current_code : "********") : "--------");
|
||||||
|
gtk_button_set_label(GTK_BUTTON(eye_button), code_visible ? "隐藏" : "显示");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void copy_text(const char *text, const char *hint) {
|
||||||
|
if (!text || !text[0]) return;
|
||||||
|
gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), text, -1);
|
||||||
|
gtk_label_set_text(GTK_LABEL(hint_label), hint);
|
||||||
|
}
|
||||||
|
// The ID is copied without the display spaces so it pastes straight into the viewer's ID field.
|
||||||
|
static void on_copy_id(GtkButton *b, gpointer d) {
|
||||||
|
(void)b; (void)d;
|
||||||
|
char digits[64]; size_t n = 0;
|
||||||
|
for (const char *p = current_id; *p && n + 1 < sizeof digits; p++) if (*p != ' ') digits[n++] = *p;
|
||||||
|
digits[n] = 0;
|
||||||
|
copy_text(digits, "已复制设备 ID");
|
||||||
|
}
|
||||||
|
static void on_copy_code(GtkButton *b, gpointer d) { (void)b; (void)d; copy_text(current_code, "已复制验证码,请只发给信任的人"); }
|
||||||
|
|
||||||
|
static void on_eye(GtkButton *b, gpointer d) { (void)b; (void)d; code_visible = !code_visible; render_code(); }
|
||||||
|
|
||||||
|
static void on_share(GtkButton *b, gpointer d) {
|
||||||
|
(void)b; (void)d;
|
||||||
|
if (!current_id[0] || !current_code[0]) return;
|
||||||
|
char *text = g_strdup_printf("UU 远程协助\n设备 ID:%s\n验证码:%s", current_id, current_code);
|
||||||
|
gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), text, -1);
|
||||||
|
g_free(text);
|
||||||
|
gtk_label_set_text(GTK_LABEL(hint_label), "已复制设备 ID 和验证码,请只发给信任的人");
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean on_switch(GtkSwitch *s, gboolean state, gpointer d) {
|
||||||
|
(void)s; (void)d;
|
||||||
|
if (!suppress_switch) post(state ? CMD_ALLOW : CMD_DENY);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Host service: the close button only hides the window (assistance keeps its own on/off switch).
|
||||||
|
static gboolean on_delete(GtkWidget *w, GdkEvent *e, gpointer d) {
|
||||||
|
(void)e; (void)d;
|
||||||
|
if (!close_hides) return FALSE;
|
||||||
|
gtk_widget_hide(w);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_destroy(GtkWidget *w, gpointer d) {
|
||||||
|
(void)w; (void)d;
|
||||||
|
window = code_label = NULL;
|
||||||
|
atomic_store(&closed, 1);
|
||||||
|
post(CMD_STOP);
|
||||||
|
gtk_main_quit();
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean draw_avatar(GtkWidget *w, cairo_t *cr, gpointer d) {
|
||||||
|
(void)w; (void)d;
|
||||||
|
double s = 64;
|
||||||
|
cairo_arc(cr, s / 2, s / 2, s / 2 - 1, 0, 2 * G_PI);
|
||||||
|
cairo_set_source_rgb(cr, 0.84, 0.90, 1.0); cairo_fill_preserve(cr);
|
||||||
|
cairo_set_source_rgb(cr, 0.36, 0.55, 1.0); cairo_set_line_width(cr, 2); cairo_stroke(cr);
|
||||||
|
cairo_save(cr);
|
||||||
|
cairo_arc(cr, s / 2, s / 2, s / 2 - 2, 0, 2 * G_PI); cairo_clip(cr);
|
||||||
|
cairo_set_source_rgb(cr, 0.27, 0.51, 1.0);
|
||||||
|
cairo_arc(cr, s / 2, s * 0.38, s * 0.16, 0, 2 * G_PI); cairo_fill(cr);
|
||||||
|
cairo_arc(cr, s / 2, s * 0.92, s * 0.32, G_PI, 2 * G_PI); cairo_fill(cr);
|
||||||
|
cairo_restore(cr);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
static void on_logout(GtkButton *b, gpointer d) {
|
||||||
|
(void)b; (void)d;
|
||||||
|
GtkWidget *dlg = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
|
||||||
|
"退出登录后,本机将从你的设备列表中下线,远程协助会改用访客身份。确定退出吗?");
|
||||||
|
if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) {
|
||||||
|
post(CMD_LOGOUT);
|
||||||
|
gtk_label_set_text(GTK_LABEL(user_label), "正在退出登录…");
|
||||||
|
}
|
||||||
|
gtk_widget_destroy(dlg);
|
||||||
|
}
|
||||||
|
|
||||||
|
static GtkWidget *build_account_card(void) {
|
||||||
|
GtkWidget *card = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
||||||
|
gtk_style_context_add_class(gtk_widget_get_style_context(card), "card");
|
||||||
|
GtkWidget *header = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(header), 20);
|
||||||
|
gtk_box_pack_start(GTK_BOX(header), label("账号", "heading"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(card), header, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(card), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE, FALSE, 0);
|
||||||
|
account_stack = gtk_stack_new();
|
||||||
|
gtk_stack_set_transition_type(GTK_STACK(account_stack), GTK_STACK_TRANSITION_TYPE_CROSSFADE);
|
||||||
|
gtk_stack_set_vhomogeneous(GTK_STACK(account_stack), FALSE); // user page is shorter than the QR page
|
||||||
|
|
||||||
|
// Not signed in: QR code + guidance.
|
||||||
|
GtkWidget *qr_page = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 24);
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(qr_page), 20);
|
||||||
|
qr_stack = gtk_stack_new();
|
||||||
|
gtk_widget_set_size_request(qr_stack, 180, 180);
|
||||||
|
GtkWidget *loading = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
|
||||||
|
gtk_widget_set_valign(loading, GTK_ALIGN_CENTER);
|
||||||
|
GtkWidget *spinner = gtk_spinner_new();
|
||||||
|
gtk_spinner_start(GTK_SPINNER(spinner));
|
||||||
|
gtk_box_pack_start(GTK_BOX(loading), spinner, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(loading), label("正在获取二维码…", "note"), FALSE, FALSE, 0);
|
||||||
|
gtk_stack_add_named(GTK_STACK(qr_stack), loading, "loading");
|
||||||
|
qr_image = gtk_image_new();
|
||||||
|
gtk_stack_add_named(GTK_STACK(qr_stack), qr_image, "qr");
|
||||||
|
gtk_box_pack_start(GTK_BOX(qr_page), qr_stack, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *qr_text = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
|
||||||
|
gtk_widget_set_valign(qr_text, GTK_ALIGN_CENTER);
|
||||||
|
gtk_box_pack_start(GTK_BOX(qr_text), label("扫码登录", "heading"), FALSE, FALSE, 0);
|
||||||
|
GtkWidget *cap = label("请使用网易 UU 远程 App 扫一扫登录。\n登录后本机会出现在你的设备列表中,可直接远程控制;\n远程协助在登录前后都可以使用。", "hint");
|
||||||
|
gtk_label_set_xalign(GTK_LABEL(cap), 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(qr_text), cap, FALSE, FALSE, 0);
|
||||||
|
login_status = label("", "status");
|
||||||
|
gtk_label_set_line_wrap(GTK_LABEL(login_status), TRUE);
|
||||||
|
gtk_box_pack_start(GTK_BOX(qr_text), login_status, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(qr_page), qr_text, TRUE, TRUE, 0);
|
||||||
|
gtk_stack_add_named(GTK_STACK(account_stack), qr_page, "qr");
|
||||||
|
|
||||||
|
// Signed in: avatar + account name + logout.
|
||||||
|
GtkWidget *user_page = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 16);
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(user_page), 20);
|
||||||
|
GtkWidget *avatar = gtk_drawing_area_new();
|
||||||
|
gtk_widget_set_size_request(avatar, 64, 64);
|
||||||
|
gtk_widget_set_valign(avatar, GTK_ALIGN_CENTER);
|
||||||
|
g_signal_connect(avatar, "draw", G_CALLBACK(draw_avatar), NULL);
|
||||||
|
gtk_box_pack_start(GTK_BOX(user_page), avatar, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *user_text = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4);
|
||||||
|
gtk_widget_set_valign(user_text, GTK_ALIGN_CENTER);
|
||||||
|
user_label = label("", "user");
|
||||||
|
gtk_box_pack_start(GTK_BOX(user_text), user_label, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(user_text), label("已登录 · 本机已出现在你的设备列表中", "hint"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(user_page), user_text, TRUE, TRUE, 0);
|
||||||
|
GtkWidget *logout = gtk_button_new_with_label("退出登录");
|
||||||
|
gtk_style_context_add_class(gtk_widget_get_style_context(logout), "share");
|
||||||
|
gtk_widget_set_valign(logout, GTK_ALIGN_CENTER);
|
||||||
|
g_signal_connect(logout, "clicked", G_CALLBACK(on_logout), NULL);
|
||||||
|
gtk_box_pack_end(GTK_BOX(user_page), logout, FALSE, FALSE, 0);
|
||||||
|
gtk_stack_add_named(GTK_STACK(account_stack), user_page, "user");
|
||||||
|
|
||||||
|
gtk_box_pack_start(GTK_BOX(card), account_stack, FALSE, FALSE, 0);
|
||||||
|
return card;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void present_window(void) { if (window) { gtk_widget_show(window); gtk_window_present(GTK_WINDOW(window)); } }
|
||||||
|
static void on_tray_open(GtkMenuItem *i, gpointer d) { (void)i; (void)d; present_window(); }
|
||||||
|
static void on_tray_allow(GtkCheckMenuItem *i, gpointer d) {
|
||||||
|
(void)d;
|
||||||
|
if (!suppress_tray) post(gtk_check_menu_item_get_active(i) ? CMD_ALLOW : CMD_DENY);
|
||||||
|
}
|
||||||
|
static void on_tray_quit(GtkMenuItem *i, gpointer d) {
|
||||||
|
(void)i; (void)d;
|
||||||
|
GtkWidget *dlg = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
|
||||||
|
"退出后本机将下线:设备列表和远程协助都无法连接,直到再次启动。确定退出吗?");
|
||||||
|
gtk_window_set_title(GTK_WINDOW(dlg), "UU 远程 Linux 主机");
|
||||||
|
if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) post(CMD_QUIT);
|
||||||
|
gtk_widget_destroy(dlg);
|
||||||
|
}
|
||||||
|
static void on_status_activate(GtkStatusIcon *i, gpointer d) { (void)i; (void)d; present_window(); }
|
||||||
|
static void on_status_menu(GtkStatusIcon *i, guint button, guint time, gpointer d) {
|
||||||
|
(void)i; (void)button; (void)time; (void)d;
|
||||||
|
gtk_menu_popup_at_pointer(GTK_MENU(tray_menu), NULL);
|
||||||
|
}
|
||||||
|
static void build_tray(void) {
|
||||||
|
tray_menu = gtk_menu_new();
|
||||||
|
GtkWidget *open = gtk_menu_item_new_with_label("打开主窗口");
|
||||||
|
g_signal_connect(open, "activate", G_CALLBACK(on_tray_open), NULL);
|
||||||
|
tray_allow = gtk_check_menu_item_new_with_label("允许他人远程协助");
|
||||||
|
g_signal_connect(tray_allow, "toggled", G_CALLBACK(on_tray_allow), NULL);
|
||||||
|
GtkWidget *quit = gtk_menu_item_new_with_label("退出");
|
||||||
|
g_signal_connect(quit, "activate", G_CALLBACK(on_tray_quit), NULL);
|
||||||
|
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), open);
|
||||||
|
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), tray_allow);
|
||||||
|
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), gtk_separator_menu_item_new());
|
||||||
|
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), quit);
|
||||||
|
gtk_widget_show_all(tray_menu);
|
||||||
|
const char *icon = gtk_icon_theme_has_icon(gtk_icon_theme_get_default(), "uu-remote-linux") ? "uu-remote-linux" : "computer";
|
||||||
|
|
||||||
|
// StatusNotifierItem (GNOME AppIndicator extension, KDE): libayatana-appindicator3 / libappindicator3.
|
||||||
|
const char *libs[] = { "libayatana-appindicator3.so.1", "libappindicator3.so.1" };
|
||||||
|
for (int i = 0; i < 2; i++) {
|
||||||
|
void *h = dlopen(libs[i], RTLD_LAZY | RTLD_LOCAL);
|
||||||
|
if (!h) continue;
|
||||||
|
void *(*ind_new)(const char *, const char *, int) = (void *(*)(const char *, const char *, int))dlsym(h, "app_indicator_new");
|
||||||
|
void (*set_status)(void *, int) = (void (*)(void *, int))dlsym(h, "app_indicator_set_status");
|
||||||
|
void (*set_menu)(void *, GtkMenu *) = (void (*)(void *, GtkMenu *))dlsym(h, "app_indicator_set_menu");
|
||||||
|
void (*set_title)(void *, const char *) = (void (*)(void *, const char *))dlsym(h, "app_indicator_set_title");
|
||||||
|
if (ind_new && set_status && set_menu) {
|
||||||
|
void *ind = ind_new("uu-remote-linux", icon, 0 /* APPLICATION_STATUS */);
|
||||||
|
if (set_title) set_title(ind, "UU 远程 Linux 主机");
|
||||||
|
set_menu(ind, GTK_MENU(tray_menu));
|
||||||
|
set_status(ind, 1 /* ACTIVE */);
|
||||||
|
return; // keep the library loaded for the indicator's lifetime
|
||||||
|
}
|
||||||
|
dlclose(h);
|
||||||
|
}
|
||||||
|
// XEmbed system tray (Xfce, Cinnamon, MATE...).
|
||||||
|
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
||||||
|
GtkStatusIcon *status = gtk_status_icon_new_from_icon_name(icon);
|
||||||
|
gtk_status_icon_set_tooltip_text(status, "UU 远程 Linux 主机");
|
||||||
|
gtk_status_icon_set_title(status, "UU 远程 Linux 主机");
|
||||||
|
g_signal_connect(status, "activate", G_CALLBACK(on_status_activate), NULL);
|
||||||
|
g_signal_connect(status, "popup-menu", G_CALLBACK(on_status_menu), NULL);
|
||||||
|
G_GNUC_END_IGNORE_DEPRECATIONS
|
||||||
|
}
|
||||||
|
|
||||||
|
static void build_window(void) {
|
||||||
|
GtkCssProvider *css = gtk_css_provider_new();
|
||||||
|
gtk_css_provider_load_from_data(css, CSS, -1, NULL);
|
||||||
|
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css),
|
||||||
|
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||||
|
g_object_unref(css);
|
||||||
|
|
||||||
|
gtk_window_set_default_icon_name("uu-remote-linux"); // contrib/desktop icon, when installed
|
||||||
|
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
||||||
|
gtk_window_set_title(GTK_WINDOW(window), "UU 远程 Linux 主机(非官方)");
|
||||||
|
gtk_window_set_default_size(GTK_WINDOW(window), 760, -1);
|
||||||
|
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
|
||||||
|
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
|
||||||
|
g_signal_connect(window, "delete-event", G_CALLBACK(on_delete), NULL);
|
||||||
|
g_signal_connect(window, "destroy", G_CALLBACK(on_destroy), NULL);
|
||||||
|
|
||||||
|
GtkWidget *root = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(root), 24);
|
||||||
|
gtk_container_add(GTK_CONTAINER(window), root);
|
||||||
|
|
||||||
|
GtkWidget *card = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
||||||
|
gtk_style_context_add_class(gtk_widget_get_style_context(card), "card");
|
||||||
|
gtk_box_pack_start(GTK_BOX(root), card, FALSE, FALSE, 0);
|
||||||
|
assist_card = card;
|
||||||
|
|
||||||
|
// Header: 本设备 ... 允许他人远程协助 [switch]
|
||||||
|
GtkWidget *header = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(header), 20);
|
||||||
|
gtk_box_pack_start(GTK_BOX(header), label("本设备", "heading"), FALSE, FALSE, 0);
|
||||||
|
allow_switch = gtk_switch_new();
|
||||||
|
gtk_widget_set_valign(allow_switch, GTK_ALIGN_CENTER);
|
||||||
|
g_signal_connect(allow_switch, "state-set", G_CALLBACK(on_switch), NULL);
|
||||||
|
gtk_box_pack_end(GTK_BOX(header), allow_switch, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_end(GTK_BOX(header), label("允许他人远程协助", "key"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(card), header, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(card), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE, FALSE, 0);
|
||||||
|
|
||||||
|
// Body: ID | code + controls
|
||||||
|
GtkWidget *body = gtk_grid_new();
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(body), 20);
|
||||||
|
gtk_grid_set_column_spacing(GTK_GRID(body), 48);
|
||||||
|
gtk_grid_set_row_spacing(GTK_GRID(body), 6);
|
||||||
|
gtk_grid_attach(GTK_GRID(body), label("本设备 ID", "key"), 0, 0, 1, 1);
|
||||||
|
GtkWidget *id_row = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
|
||||||
|
id_label = label("获取中…", "big");
|
||||||
|
gtk_label_set_selectable(GTK_LABEL(id_label), TRUE);
|
||||||
|
gtk_box_pack_start(GTK_BOX(id_row), id_label, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *copy_id = gtk_button_new_with_label("复制");
|
||||||
|
gtk_style_context_add_class(gtk_widget_get_style_context(copy_id), "flat");
|
||||||
|
gtk_widget_set_valign(copy_id, GTK_ALIGN_CENTER);
|
||||||
|
g_signal_connect(copy_id, "clicked", G_CALLBACK(on_copy_id), NULL);
|
||||||
|
gtk_box_pack_start(GTK_BOX(id_row), copy_id, FALSE, FALSE, 0);
|
||||||
|
gtk_grid_attach(GTK_GRID(body), id_row, 0, 1, 1, 1);
|
||||||
|
|
||||||
|
GtkWidget *verify_row = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
|
||||||
|
gtk_box_pack_start(GTK_BOX(verify_row), label("验证方式:", "key"), FALSE, FALSE, 0);
|
||||||
|
verify_combo = gtk_combo_box_text_new();
|
||||||
|
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(verify_combo), "仅使用临时验证码");
|
||||||
|
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(verify_combo), "仅使用自定义验证码");
|
||||||
|
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(verify_combo), "同时使用两种验证码");
|
||||||
|
gtk_combo_box_set_active(GTK_COMBO_BOX(verify_combo), 0);
|
||||||
|
g_signal_connect(verify_combo, "changed", G_CALLBACK(on_verify_changed), NULL);
|
||||||
|
gtk_box_pack_start(GTK_BOX(verify_row), verify_combo, FALSE, FALSE, 0);
|
||||||
|
gtk_grid_attach(GTK_GRID(body), verify_row, 1, 0, 1, 1);
|
||||||
|
GtkWidget *code_row = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
|
||||||
|
code_label = label("--------", "big");
|
||||||
|
gtk_box_pack_start(GTK_BOX(code_row), code_label, FALSE, FALSE, 0);
|
||||||
|
eye_button = gtk_button_new_with_label("隐藏");
|
||||||
|
gtk_style_context_add_class(gtk_widget_get_style_context(eye_button), "flat");
|
||||||
|
g_signal_connect(eye_button, "clicked", G_CALLBACK(on_eye), NULL);
|
||||||
|
// Gear menu: 立即刷新 / 自动刷新 ▸ (每次远控结束, 每小时, 每 12 小时, 每 24 小时, 关闭) / 设置自定义验证码
|
||||||
|
GtkWidget *menu = gtk_menu_new();
|
||||||
|
GtkWidget *now = gtk_menu_item_new_with_label("立即刷新");
|
||||||
|
g_signal_connect(now, "activate", G_CALLBACK(on_refresh_now), NULL);
|
||||||
|
gtk_menu_shell_append(GTK_MENU_SHELL(menu), now);
|
||||||
|
GtkWidget *auto_item = gtk_menu_item_new_with_label("自动刷新");
|
||||||
|
GtkWidget *sub = gtk_menu_new();
|
||||||
|
const char *names[5] = { "每次远控结束", "每小时", "每 12 小时", "每 24 小时", "关闭" };
|
||||||
|
GSList *group = NULL;
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
refresh_items[i] = gtk_radio_menu_item_new_with_label(group, names[i]);
|
||||||
|
group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(refresh_items[i]));
|
||||||
|
g_signal_connect(refresh_items[i], "toggled", G_CALLBACK(on_refresh_item), GINT_TO_POINTER(i));
|
||||||
|
gtk_menu_shell_append(GTK_MENU_SHELL(sub), refresh_items[i]);
|
||||||
|
}
|
||||||
|
gtk_menu_item_set_submenu(GTK_MENU_ITEM(auto_item), sub);
|
||||||
|
gtk_menu_shell_append(GTK_MENU_SHELL(menu), auto_item);
|
||||||
|
GtkWidget *custom_item = gtk_menu_item_new_with_label("设置自定义验证码…");
|
||||||
|
g_signal_connect(custom_item, "activate", G_CALLBACK(on_set_custom), NULL);
|
||||||
|
gtk_menu_shell_append(GTK_MENU_SHELL(menu), custom_item);
|
||||||
|
gtk_widget_show_all(menu);
|
||||||
|
GtkWidget *gear = gtk_menu_button_new();
|
||||||
|
gtk_button_set_image(GTK_BUTTON(gear), gtk_image_new_from_icon_name("emblem-system-symbolic", GTK_ICON_SIZE_BUTTON));
|
||||||
|
gtk_widget_set_tooltip_text(gear, "刷新设置");
|
||||||
|
gtk_menu_button_set_popup(GTK_MENU_BUTTON(gear), menu);
|
||||||
|
GtkWidget *copy_code = gtk_button_new_with_label("复制");
|
||||||
|
gtk_style_context_add_class(gtk_widget_get_style_context(copy_code), "flat");
|
||||||
|
gtk_widget_set_valign(copy_code, GTK_ALIGN_CENTER);
|
||||||
|
g_signal_connect(copy_code, "clicked", G_CALLBACK(on_copy_code), NULL);
|
||||||
|
gtk_widget_set_valign(eye_button, GTK_ALIGN_CENTER);
|
||||||
|
gtk_widget_set_valign(gear, GTK_ALIGN_CENTER);
|
||||||
|
gtk_box_pack_start(GTK_BOX(code_row), copy_code, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(code_row), eye_button, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(code_row), gear, FALSE, FALSE, 0);
|
||||||
|
gtk_grid_attach(GTK_GRID(body), code_row, 1, 1, 1, 1);
|
||||||
|
note_label = label(REFRESH_NOTES[0], "note");
|
||||||
|
gtk_grid_attach(GTK_GRID(body), note_label, 1, 2, 1, 1);
|
||||||
|
|
||||||
|
share_button = gtk_button_new_with_label("复制并分享");
|
||||||
|
gtk_style_context_add_class(gtk_widget_get_style_context(share_button), "share");
|
||||||
|
gtk_widget_set_valign(share_button, GTK_ALIGN_CENTER);
|
||||||
|
g_signal_connect(share_button, "clicked", G_CALLBACK(on_share), NULL);
|
||||||
|
gtk_grid_attach(GTK_GRID(body), share_button, 2, 1, 1, 1);
|
||||||
|
gtk_box_pack_start(GTK_BOX(card), body, FALSE, FALSE, 0);
|
||||||
|
|
||||||
|
hint_label = label("", "hint");
|
||||||
|
gtk_box_pack_start(GTK_BOX(root), hint_label, FALSE, FALSE, 0);
|
||||||
|
account_card = build_account_card();
|
||||||
|
gtk_widget_set_margin_top(account_card, 6);
|
||||||
|
gtk_box_pack_start(GTK_BOX(root), account_card, FALSE, FALSE, 0);
|
||||||
|
footer_label = label("开源 Linux 被控端 · 非网易官方客户端 · 关闭窗口即停止远程协助", "footer");
|
||||||
|
gtk_box_pack_end(GTK_BOX(root), footer_label, FALSE, FALSE, 0);
|
||||||
|
if (close_hides) gtk_label_set_text(GTK_LABEL(footer_label), "开源 Linux 被控端 · 非网易官方客户端 · 关闭窗口后远程协助仍在后台运行");
|
||||||
|
render_code();
|
||||||
|
if (close_hides) build_tray();
|
||||||
|
gtk_widget_show_all(gtk_bin_get_child(GTK_BIN(window)));
|
||||||
|
gtk_widget_set_visible(assist_card, show_assist);
|
||||||
|
gtk_widget_set_visible(hint_label, show_assist);
|
||||||
|
gtk_widget_set_visible(account_card, show_account);
|
||||||
|
if (!start_hidden) gtk_widget_show(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void *ui_main(void *arg) {
|
||||||
|
(void)arg;
|
||||||
|
int ok = gtk_init_check(NULL, NULL);
|
||||||
|
if (ok) build_window();
|
||||||
|
pthread_mutex_lock(&init_lock);
|
||||||
|
init_state = ok ? 1 : -1;
|
||||||
|
pthread_cond_signal(&init_cond);
|
||||||
|
pthread_mutex_unlock(&init_lock);
|
||||||
|
if (ok) gtk_main();
|
||||||
|
else atomic_store(&closed, 1);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns 0 when the window was created, -1 when no display is usable. hidden: create without
|
||||||
|
// showing (host service, opened later with uu_assist_ui_present); hide_on_close: closing hides.
|
||||||
|
static gboolean apply_sections(gpointer d) {
|
||||||
|
(void)d;
|
||||||
|
if (window) {
|
||||||
|
gtk_widget_set_visible(assist_card, show_assist); gtk_widget_set_visible(hint_label, show_assist);
|
||||||
|
gtk_widget_set_visible(account_card, show_account);
|
||||||
|
}
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
static int ui_start(int hidden, int hide_on_close);
|
||||||
|
int uu_assist_ui_start(int hidden, int hide_on_close) {
|
||||||
|
if (ui_started) { show_assist = 1; g_idle_add(apply_sections, NULL); return init_state == 1 ? 0 : -1; }
|
||||||
|
show_assist = 1;
|
||||||
|
return ui_start(hidden, hide_on_close);
|
||||||
|
}
|
||||||
|
// Reveals the account card on the shared window; standalone `login` opens it with the account card only.
|
||||||
|
int uu_login_ui_start(const char *name, const char *os, const char *cpu, const char *gpu) {
|
||||||
|
(void)name; (void)os; (void)cpu; (void)gpu;
|
||||||
|
show_account = 1;
|
||||||
|
if (ui_started) { g_idle_add(apply_sections, NULL); return init_state == 1 ? 0 : -1; }
|
||||||
|
show_assist = 0;
|
||||||
|
return ui_start(0, 0);
|
||||||
|
}
|
||||||
|
void uu_ui_show_account(void) { show_account = 1; if (ui_started && init_state == 1) g_idle_add(apply_sections, NULL); }
|
||||||
|
static int ui_start(int hidden, int hide_on_close) {
|
||||||
|
if (ui_started) return init_state == 1 ? 0 : -1;
|
||||||
|
start_hidden = hidden; close_hides = hide_on_close;
|
||||||
|
if (pthread_create(&ui_thread, NULL, ui_main, NULL) != 0) return -1;
|
||||||
|
ui_started = 1;
|
||||||
|
pthread_mutex_lock(&init_lock);
|
||||||
|
while (init_state == 0) pthread_cond_wait(&init_cond, &init_lock);
|
||||||
|
pthread_mutex_unlock(&init_lock);
|
||||||
|
if (init_state != 1) { pthread_join(ui_thread, NULL); return -1; }
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean apply_state(gpointer data) {
|
||||||
|
State *s = data;
|
||||||
|
if (window) {
|
||||||
|
g_strlcpy(current_id, s->id, sizeof current_id);
|
||||||
|
g_strlcpy(current_code, s->code, sizeof current_code);
|
||||||
|
gtk_label_set_text(GTK_LABEL(id_label), current_id[0] ? current_id : "获取中…");
|
||||||
|
render_code();
|
||||||
|
suppress_switch = 1;
|
||||||
|
gtk_switch_set_active(GTK_SWITCH(allow_switch), s->allowed);
|
||||||
|
suppress_switch = 0;
|
||||||
|
if (tray_allow) { suppress_tray = 1; gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(tray_allow), s->allowed); suppress_tray = 0; }
|
||||||
|
cur_verify = s->verify; cur_refresh = s->refresh; cur_has_custom = s->has_custom;
|
||||||
|
suppress_combo = 1; gtk_combo_box_set_active(GTK_COMBO_BOX(verify_combo), s->verify); suppress_combo = 0;
|
||||||
|
if (s->refresh >= 0 && s->refresh < 5) {
|
||||||
|
suppress_refresh = 1; gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(refresh_items[s->refresh]), TRUE); suppress_refresh = 0;
|
||||||
|
}
|
||||||
|
gtk_label_set_text(GTK_LABEL(note_label), s->verify == VERIFY_CUSTOM ? "自定义验证码不会自动刷新"
|
||||||
|
: s->verify == VERIFY_BOTH ? "也可使用自定义验证码连接" : REFRESH_NOTES[s->refresh >= 0 && s->refresh < 5 ? s->refresh : 0]);
|
||||||
|
gtk_widget_set_sensitive(share_button, current_id[0] && current_code[0]);
|
||||||
|
gtk_label_set_text(GTK_LABEL(hint_label), s->allowed
|
||||||
|
? "对方在 UU 远程「开始协助」中输入设备 ID 和验证码即可连接"
|
||||||
|
: "已关闭:他人暂时无法远程协助本机,打开右上角开关后才能连接");
|
||||||
|
}
|
||||||
|
g_free(s->id); g_free(s->code); g_free(s);
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// id is already formatted for display (e.g. "123 456 789").
|
||||||
|
void uu_assist_ui_set(const char *id, const char *code, int allowed, int verify, int refresh, int has_custom) {
|
||||||
|
if (!ui_started || init_state != 1) return;
|
||||||
|
State *s = g_new0(State, 1);
|
||||||
|
s->id = g_strdup(id ? id : ""); s->code = g_strdup(code ? code : ""); s->allowed = allowed;
|
||||||
|
s->verify = verify; s->refresh = refresh; s->has_custom = has_custom;
|
||||||
|
g_idle_add(apply_state, s);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pops the next user command (see CMD_*), copying its argument into arg.
|
||||||
|
int uu_assist_ui_take_command(char *arg, size_t len) {
|
||||||
|
int cmd = CMD_NONE;
|
||||||
|
if (arg && len) arg[0] = 0;
|
||||||
|
pthread_mutex_lock(&q_lock);
|
||||||
|
if (q_len) {
|
||||||
|
Cmd *c = &queue[q_head];
|
||||||
|
cmd = c->cmd; if (arg && len) g_strlcpy(arg, c->arg, len);
|
||||||
|
memset(c->arg, 0, sizeof c->arg); // drop the custom code copy promptly
|
||||||
|
q_head = (q_head + 1) % 16; q_len--;
|
||||||
|
}
|
||||||
|
pthread_mutex_unlock(&q_lock);
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
int uu_assist_ui_closed(void) { return atomic_load(&closed); }
|
||||||
|
|
||||||
|
static gboolean apply_present(gpointer data) {
|
||||||
|
(void)data;
|
||||||
|
if (window) { gtk_widget_show(window); gtk_window_present(GTK_WINDOW(window)); }
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
void uu_assist_ui_present(void) { if (ui_started && init_state == 1 && !atomic_load(&closed)) g_idle_add(apply_present, NULL); }
|
||||||
|
|
||||||
|
static gboolean apply_close(gpointer data) { (void)data; if (window) gtk_widget_destroy(window); return G_SOURCE_REMOVE; }
|
||||||
|
void uu_assist_ui_close(void) { if (ui_started && init_state == 1 && !atomic_load(&closed)) g_idle_add(apply_close, NULL); }
|
||||||
|
|
||||||
|
// ---- Account card API (QR login / signed-in user); same names as the former login window ----
|
||||||
|
typedef struct { unsigned char *rgba; int edge; } QrUpdate;
|
||||||
|
typedef struct { char *text; int kind; } StatusUpdate;
|
||||||
|
|
||||||
|
static gboolean apply_qr(gpointer data) {
|
||||||
|
QrUpdate *u = data;
|
||||||
|
if (window) {
|
||||||
|
GdkPixbuf *pb = gdk_pixbuf_new_from_data(u->rgba, GDK_COLORSPACE_RGB, TRUE, 8, u->edge, u->edge, u->edge * 4, NULL, NULL);
|
||||||
|
GdkPixbuf *scaled = gdk_pixbuf_scale_simple(pb, 180, 180, GDK_INTERP_NEAREST);
|
||||||
|
gtk_image_set_from_pixbuf(GTK_IMAGE(qr_image), scaled);
|
||||||
|
g_object_unref(scaled); g_object_unref(pb);
|
||||||
|
gtk_stack_set_visible_child_name(GTK_STACK(qr_stack), "qr");
|
||||||
|
gtk_stack_set_visible_child_name(GTK_STACK(account_stack), "qr");
|
||||||
|
}
|
||||||
|
g_free(u->rgba); g_free(u);
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
void uu_login_ui_set_qr(const unsigned char *rgba, int edge) {
|
||||||
|
if (!ui_started || init_state != 1 || atomic_load(&closed) || edge <= 0) return;
|
||||||
|
QrUpdate *u = g_new(QrUpdate, 1);
|
||||||
|
u->rgba = g_memdup2(rgba, (gsize)edge * edge * 4); u->edge = edge;
|
||||||
|
g_idle_add(apply_qr, u);
|
||||||
|
}
|
||||||
|
static gboolean apply_login_status(gpointer data) {
|
||||||
|
StatusUpdate *u = data;
|
||||||
|
if (window) {
|
||||||
|
GtkStyleContext *ctx = gtk_widget_get_style_context(login_status);
|
||||||
|
gtk_style_context_remove_class(ctx, "ok"); gtk_style_context_remove_class(ctx, "error");
|
||||||
|
if (u->kind == 1) gtk_style_context_add_class(ctx, "ok");
|
||||||
|
if (u->kind == 2) gtk_style_context_add_class(ctx, "error");
|
||||||
|
gtk_label_set_text(GTK_LABEL(login_status), u->text);
|
||||||
|
}
|
||||||
|
g_free(u->text); g_free(u);
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
void uu_login_ui_set_status(const char *text, int kind) {
|
||||||
|
if (!ui_started || init_state != 1 || atomic_load(&closed)) return;
|
||||||
|
StatusUpdate *u = g_new(StatusUpdate, 1);
|
||||||
|
u->text = g_strdup(text ? text : ""); u->kind = kind;
|
||||||
|
g_idle_add(apply_login_status, u);
|
||||||
|
}
|
||||||
|
static gboolean apply_user(gpointer data) {
|
||||||
|
if (window) {
|
||||||
|
gtk_label_set_text(GTK_LABEL(user_label), data);
|
||||||
|
gtk_stack_set_visible_child_name(GTK_STACK(account_stack), "user");
|
||||||
|
show_account = 1; apply_sections(NULL);
|
||||||
|
}
|
||||||
|
g_free(data);
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
// Signed in: show the user page with this (already masked) account name.
|
||||||
|
void uu_ui_set_user(const char *name) {
|
||||||
|
if (!ui_started || init_state != 1) return;
|
||||||
|
g_idle_add(apply_user, g_strdup(name ? name : ""));
|
||||||
|
}
|
||||||
|
void uu_login_ui_set_done(const char *text) { uu_ui_set_user(text); }
|
||||||
|
int uu_login_ui_closed(void) { return atomic_load(&closed); }
|
||||||
|
void uu_login_ui_close(void) { uu_assist_ui_close(); }
|
||||||
@@ -0,0 +1,154 @@
|
|||||||
|
#include <gio/gio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define CALL_TIMEOUT_MS 3000
|
||||||
|
#define MUTTER_DEST "org.gnome.Mutter.DisplayConfig"
|
||||||
|
#define MUTTER_PATH "/org/gnome/Mutter/DisplayConfig"
|
||||||
|
#define SESSION_DEST "org.gnome.SessionManager"
|
||||||
|
#define SESSION_PATH "/org/gnome/SessionManager"
|
||||||
|
#define SESSION_IFACE "org.gnome.SessionManager"
|
||||||
|
|
||||||
|
enum {
|
||||||
|
UU_POWER_OK = 0,
|
||||||
|
UU_POWER_SESSION_BUS = 1,
|
||||||
|
UU_POWER_CALL_FAILED = 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct uu_idle_inhibitor {
|
||||||
|
GDBusConnection *connection;
|
||||||
|
guint32 cookie;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct setup_timeout {
|
||||||
|
GMutex lock;
|
||||||
|
GCond cond;
|
||||||
|
gboolean finished;
|
||||||
|
GCancellable *cancellable;
|
||||||
|
};
|
||||||
|
|
||||||
|
static gpointer cancel_setup_on_timeout(gpointer data) {
|
||||||
|
struct setup_timeout *timeout = data;
|
||||||
|
gint64 deadline = g_get_monotonic_time() + CALL_TIMEOUT_MS * G_TIME_SPAN_MILLISECOND;
|
||||||
|
g_mutex_lock(&timeout->lock);
|
||||||
|
while (!timeout->finished &&
|
||||||
|
g_cond_wait_until(&timeout->cond, &timeout->lock, deadline)) {}
|
||||||
|
gboolean expired = !timeout->finished;
|
||||||
|
g_mutex_unlock(&timeout->lock);
|
||||||
|
if (expired) g_cancellable_cancel(timeout->cancellable);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void finish_setup_timeout(struct setup_timeout *timeout, GThread *thread) {
|
||||||
|
g_mutex_lock(&timeout->lock);
|
||||||
|
timeout->finished = TRUE;
|
||||||
|
g_cond_signal(&timeout->cond);
|
||||||
|
g_mutex_unlock(&timeout->lock);
|
||||||
|
g_thread_join(thread);
|
||||||
|
g_cond_clear(&timeout->cond);
|
||||||
|
g_mutex_clear(&timeout->lock);
|
||||||
|
g_object_unref(timeout->cancellable);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Use a dedicated connection so closing this inhibitor cannot affect another app's bus state. */
|
||||||
|
static GDBusConnection *private_session_bus(int *error_code) {
|
||||||
|
struct setup_timeout timeout = {0};
|
||||||
|
g_mutex_init(&timeout.lock);
|
||||||
|
g_cond_init(&timeout.cond);
|
||||||
|
timeout.cancellable = g_cancellable_new();
|
||||||
|
GThread *timer = g_thread_new("uu-dbus-connect-timeout", cancel_setup_on_timeout, &timeout);
|
||||||
|
|
||||||
|
GError *error = NULL;
|
||||||
|
gchar *address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SESSION, timeout.cancellable, &error);
|
||||||
|
if (!address) {
|
||||||
|
g_clear_error(&error);
|
||||||
|
finish_setup_timeout(&timeout, timer);
|
||||||
|
*error_code = UU_POWER_SESSION_BUS;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
GDBusConnection *connection = g_dbus_connection_new_for_address_sync(
|
||||||
|
address,
|
||||||
|
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
|
||||||
|
G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
|
||||||
|
NULL, timeout.cancellable, &error);
|
||||||
|
g_free(address);
|
||||||
|
finish_setup_timeout(&timeout, timer);
|
||||||
|
if (!connection) {
|
||||||
|
g_clear_error(&error);
|
||||||
|
*error_code = UU_POWER_SESSION_BUS;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
g_dbus_connection_set_exit_on_close(connection, FALSE);
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void close_private_bus(GDBusConnection *connection) {
|
||||||
|
if (!connection) return;
|
||||||
|
g_dbus_connection_close(connection, NULL, NULL, NULL);
|
||||||
|
g_object_unref(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
int uu_desktop_power_wake(void) {
|
||||||
|
int error_code = UU_POWER_OK;
|
||||||
|
GDBusConnection *connection = private_session_bus(&error_code);
|
||||||
|
if (!connection) return error_code;
|
||||||
|
|
||||||
|
GError *error = NULL;
|
||||||
|
GVariant *reply = g_dbus_connection_call_sync(
|
||||||
|
connection, MUTTER_DEST, MUTTER_PATH, "org.freedesktop.DBus.Properties", "Set",
|
||||||
|
g_variant_new("(ssv)", "org.gnome.Mutter.DisplayConfig", "PowerSaveMode",
|
||||||
|
g_variant_new_int32(0)),
|
||||||
|
G_VARIANT_TYPE("()"), G_DBUS_CALL_FLAGS_NONE, CALL_TIMEOUT_MS, NULL, &error);
|
||||||
|
if (!reply) error_code = UU_POWER_CALL_FAILED;
|
||||||
|
if (reply) g_variant_unref(reply);
|
||||||
|
g_clear_error(&error);
|
||||||
|
close_private_bus(connection);
|
||||||
|
return error_code;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct uu_idle_inhibitor *uu_desktop_idle_inhibitor_acquire(int *error_code) {
|
||||||
|
if (!error_code) return NULL;
|
||||||
|
*error_code = UU_POWER_OK;
|
||||||
|
GDBusConnection *connection = private_session_bus(error_code);
|
||||||
|
if (!connection) return NULL;
|
||||||
|
|
||||||
|
GError *error = NULL;
|
||||||
|
GVariant *reply = g_dbus_connection_call_sync(
|
||||||
|
connection, SESSION_DEST, SESSION_PATH, SESSION_IFACE, "Inhibit",
|
||||||
|
g_variant_new("(susu)", "uu-remote-linux", 0u,
|
||||||
|
"Remote desktop session is active", 8u),
|
||||||
|
G_VARIANT_TYPE("(u)"), G_DBUS_CALL_FLAGS_NONE, CALL_TIMEOUT_MS, NULL, &error);
|
||||||
|
if (!reply) {
|
||||||
|
*error_code = UU_POWER_CALL_FAILED;
|
||||||
|
g_clear_error(&error);
|
||||||
|
close_private_bus(connection);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct uu_idle_inhibitor *inhibitor = calloc(1, sizeof(*inhibitor));
|
||||||
|
if (!inhibitor) {
|
||||||
|
*error_code = UU_POWER_CALL_FAILED;
|
||||||
|
g_variant_unref(reply);
|
||||||
|
close_private_bus(connection);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
g_variant_get(reply, "(u)", &inhibitor->cookie);
|
||||||
|
g_variant_unref(reply);
|
||||||
|
inhibitor->connection = connection;
|
||||||
|
return inhibitor;
|
||||||
|
}
|
||||||
|
|
||||||
|
void uu_desktop_idle_inhibitor_release(struct uu_idle_inhibitor *inhibitor) {
|
||||||
|
if (!inhibitor) return;
|
||||||
|
GError *error = NULL;
|
||||||
|
GVariant *reply = g_dbus_connection_call_sync(
|
||||||
|
inhibitor->connection, SESSION_DEST, SESSION_PATH, SESSION_IFACE, "Uninhibit",
|
||||||
|
g_variant_new("(u)", inhibitor->cookie), G_VARIANT_TYPE("()"),
|
||||||
|
G_DBUS_CALL_FLAGS_NONE, CALL_TIMEOUT_MS, NULL, &error);
|
||||||
|
if (reply) g_variant_unref(reply);
|
||||||
|
g_clear_error(&error);
|
||||||
|
|
||||||
|
/* Closing the private bus also revokes the cookie if Uninhibit failed or the host exits. */
|
||||||
|
close_private_bus(inhibitor->connection);
|
||||||
|
free(inhibitor);
|
||||||
|
}
|
||||||
@@ -1,321 +0,0 @@
|
|||||||
// GTK3 QR login window for `uuremote-server login`. The window runs its own GTK main loop on a
|
|
||||||
// dedicated thread; the Rust login flow pushes QR images and status text through g_idle_add.
|
|
||||||
// Layout follows the official login window (QR on the left), but uses no NetEase assets: the
|
|
||||||
// mark is drawn with cairo and the right column shows this host's hardware summary instead of SMS.
|
|
||||||
#include <gtk/gtk.h>
|
|
||||||
#include <pthread.h>
|
|
||||||
#include <stdatomic.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
static const char *CSS =
|
|
||||||
"window { background-image: linear-gradient(165deg, #d6e8ff 0%, #eaf3ff 40%, #ffffff 100%); }\n"
|
|
||||||
"* { font-family: 'Noto Sans CJK SC', 'Noto Sans SC', sans-serif; }\n"
|
|
||||||
".title { font-size: 26px; font-weight: 800; color: #111111; }\n"
|
|
||||||
".subtitle { font-size: 13px; color: #6b7280; }\n"
|
|
||||||
".heading { font-size: 19px; font-weight: 700; color: #1d1d1f; }\n"
|
|
||||||
".card { background-color: #ffffff; border-radius: 10px; padding: 14px; }\n"
|
|
||||||
".caption { font-size: 14px; color: #555555; }\n"
|
|
||||||
".status { font-size: 13px; color: #2f6bff; }\n"
|
|
||||||
".status.ok { color: #16a34a; }\n"
|
|
||||||
".status.error { color: #dc2626; }\n"
|
|
||||||
".key { font-size: 13px; color: #8a8f98; }\n"
|
|
||||||
".value { font-size: 13px; color: #222222; }\n"
|
|
||||||
".note { font-size: 12px; color: #6b7280; }\n"
|
|
||||||
".done-mark { font-size: 72px; color: #16a34a; }\n"
|
|
||||||
".done-text { font-size: 17px; font-weight: 700; color: #1d1d1f; }\n"
|
|
||||||
".footer { font-size: 12px; color: #9ca3af; }\n"
|
|
||||||
"button.primary { background-image: none; background-color: #2f6bff; color: #ffffff; border: none;"
|
|
||||||
" border-radius: 6px; padding: 6px 28px; font-size: 15px; }\n"
|
|
||||||
"button.primary:hover { background-color: #1f5ae6; }\n"
|
|
||||||
"separator { background-color: #dfe5ee; min-width: 1px; }\n";
|
|
||||||
|
|
||||||
typedef struct { unsigned char *rgba; int edge; } QrUpdate;
|
|
||||||
typedef struct { char *text; int kind; } StatusUpdate;
|
|
||||||
|
|
||||||
static atomic_int closed;
|
|
||||||
static pthread_t ui_thread;
|
|
||||||
static int ui_started;
|
|
||||||
static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
||||||
static pthread_cond_t init_cond = PTHREAD_COND_INITIALIZER;
|
|
||||||
static int init_state; // 0 pending, 1 ok, -1 failed
|
|
||||||
static char *info[4];
|
|
||||||
static GtkWidget *window, *stack, *qr_image, *status_label, *done_label, *done_button;
|
|
||||||
|
|
||||||
static gboolean draw_mark(GtkWidget *w, cairo_t *cr, gpointer data) {
|
|
||||||
(void)w; (void)data;
|
|
||||||
cairo_pattern_t *g = cairo_pattern_create_linear(0, 0, 44, 44);
|
|
||||||
cairo_pattern_add_color_stop_rgb(g, 0, 0.13, 0.83, 0.93);
|
|
||||||
cairo_pattern_add_color_stop_rgb(g, 1, 0.18, 0.42, 1.0);
|
|
||||||
double r = 11, s = 44;
|
|
||||||
cairo_new_sub_path(cr);
|
|
||||||
cairo_arc(cr, s - r, r, r, -G_PI / 2, 0);
|
|
||||||
cairo_arc(cr, s - r, s - r, r, 0, G_PI / 2);
|
|
||||||
cairo_arc(cr, r, s - r, r, G_PI / 2, G_PI);
|
|
||||||
cairo_arc(cr, r, r, r, G_PI, 3 * G_PI / 2);
|
|
||||||
cairo_close_path(cr);
|
|
||||||
cairo_set_source(cr, g);
|
|
||||||
cairo_fill(cr);
|
|
||||||
cairo_pattern_destroy(g);
|
|
||||||
cairo_set_source_rgb(cr, 1, 1, 1);
|
|
||||||
cairo_select_font_face(cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
|
|
||||||
cairo_set_font_size(cr, 19);
|
|
||||||
cairo_text_extents_t e;
|
|
||||||
cairo_text_extents(cr, "UU", &e);
|
|
||||||
cairo_move_to(cr, (s - e.width) / 2 - e.x_bearing, (s - e.height) / 2 - e.y_bearing);
|
|
||||||
cairo_show_text(cr, "UU");
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
static GtkWidget *label(const char *text, const char *cls) {
|
|
||||||
GtkWidget *l = gtk_label_new(text);
|
|
||||||
gtk_style_context_add_class(gtk_widget_get_style_context(l), cls);
|
|
||||||
return l;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void info_row(GtkGrid *grid, int row, const char *key, const char *value) {
|
|
||||||
GtkWidget *k = label(key, "key"), *v = label(value && *value ? value : "-", "value");
|
|
||||||
gtk_widget_set_halign(k, GTK_ALIGN_START);
|
|
||||||
gtk_widget_set_valign(k, GTK_ALIGN_START);
|
|
||||||
gtk_widget_set_halign(v, GTK_ALIGN_START);
|
|
||||||
gtk_label_set_xalign(GTK_LABEL(v), 0);
|
|
||||||
gtk_label_set_line_wrap(GTK_LABEL(v), TRUE);
|
|
||||||
gtk_label_set_max_width_chars(GTK_LABEL(v), 26);
|
|
||||||
gtk_grid_attach(grid, k, 0, row, 1, 1);
|
|
||||||
gtk_grid_attach(grid, v, 1, row, 1, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void on_destroy(GtkWidget *w, gpointer data) {
|
|
||||||
(void)w; (void)data;
|
|
||||||
atomic_store(&closed, 1);
|
|
||||||
window = NULL;
|
|
||||||
gtk_main_quit();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void on_done_clicked(GtkButton *b, gpointer data) {
|
|
||||||
(void)b; (void)data;
|
|
||||||
if (window) gtk_widget_destroy(window);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void build_window(void) {
|
|
||||||
GtkCssProvider *css = gtk_css_provider_new();
|
|
||||||
gtk_css_provider_load_from_data(css, CSS, -1, NULL);
|
|
||||||
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css),
|
|
||||||
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
|
||||||
g_object_unref(css);
|
|
||||||
|
|
||||||
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
|
||||||
gtk_window_set_title(GTK_WINDOW(window), "UU 远程 Linux 主机 - 登录");
|
|
||||||
gtk_window_set_default_size(GTK_WINDOW(window), 720, 520);
|
|
||||||
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
|
|
||||||
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
|
|
||||||
g_signal_connect(window, "destroy", G_CALLBACK(on_destroy), NULL);
|
|
||||||
|
|
||||||
GtkWidget *root = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
|
||||||
gtk_container_set_border_width(GTK_CONTAINER(root), 24);
|
|
||||||
gtk_container_add(GTK_CONTAINER(window), root);
|
|
||||||
|
|
||||||
GtkWidget *header = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 12);
|
|
||||||
gtk_widget_set_halign(header, GTK_ALIGN_CENTER);
|
|
||||||
GtkWidget *mark = gtk_drawing_area_new();
|
|
||||||
gtk_widget_set_size_request(mark, 44, 44);
|
|
||||||
gtk_widget_set_valign(mark, GTK_ALIGN_CENTER);
|
|
||||||
g_signal_connect(mark, "draw", G_CALLBACK(draw_mark), NULL);
|
|
||||||
GtkWidget *titles = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
|
||||||
GtkWidget *title = label("UU 远程 · Linux 主机", "title");
|
|
||||||
gtk_widget_set_halign(title, GTK_ALIGN_START);
|
|
||||||
GtkWidget *subtitle = label("登录后,本机会出现在你的 UU 远程设备列表中", "subtitle");
|
|
||||||
gtk_widget_set_halign(subtitle, GTK_ALIGN_START);
|
|
||||||
gtk_box_pack_start(GTK_BOX(titles), title, FALSE, FALSE, 0);
|
|
||||||
gtk_box_pack_start(GTK_BOX(titles), subtitle, FALSE, FALSE, 0);
|
|
||||||
gtk_box_pack_start(GTK_BOX(header), mark, FALSE, FALSE, 0);
|
|
||||||
gtk_box_pack_start(GTK_BOX(header), titles, FALSE, FALSE, 0);
|
|
||||||
gtk_box_pack_start(GTK_BOX(root), header, FALSE, FALSE, 4);
|
|
||||||
|
|
||||||
GtkWidget *columns = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
|
|
||||||
gtk_box_set_homogeneous(GTK_BOX(columns), FALSE);
|
|
||||||
gtk_widget_set_margin_top(columns, 22);
|
|
||||||
gtk_box_pack_start(GTK_BOX(root), columns, TRUE, TRUE, 0);
|
|
||||||
|
|
||||||
// Left: QR login.
|
|
||||||
GtkWidget *left = gtk_box_new(GTK_ORIENTATION_VERTICAL, 12);
|
|
||||||
gtk_widget_set_size_request(left, 330, -1);
|
|
||||||
gtk_box_pack_start(GTK_BOX(left), label("扫码登录", "heading"), FALSE, FALSE, 0);
|
|
||||||
GtkWidget *card = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
|
||||||
gtk_style_context_add_class(gtk_widget_get_style_context(card), "card");
|
|
||||||
gtk_widget_set_halign(card, GTK_ALIGN_CENTER);
|
|
||||||
gtk_widget_set_size_request(card, 252, 252);
|
|
||||||
stack = gtk_stack_new();
|
|
||||||
gtk_stack_set_transition_type(GTK_STACK(stack), GTK_STACK_TRANSITION_TYPE_CROSSFADE);
|
|
||||||
GtkWidget *loading = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
|
|
||||||
gtk_widget_set_valign(loading, GTK_ALIGN_CENTER);
|
|
||||||
GtkWidget *spinner = gtk_spinner_new();
|
|
||||||
gtk_spinner_start(GTK_SPINNER(spinner));
|
|
||||||
gtk_widget_set_size_request(spinner, 32, 32);
|
|
||||||
gtk_box_pack_start(GTK_BOX(loading), spinner, FALSE, FALSE, 0);
|
|
||||||
gtk_box_pack_start(GTK_BOX(loading), label("正在获取二维码…", "caption"), FALSE, FALSE, 0);
|
|
||||||
gtk_stack_add_named(GTK_STACK(stack), loading, "loading");
|
|
||||||
qr_image = gtk_image_new();
|
|
||||||
gtk_stack_add_named(GTK_STACK(stack), qr_image, "qr");
|
|
||||||
GtkWidget *done = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4);
|
|
||||||
gtk_widget_set_valign(done, GTK_ALIGN_CENTER);
|
|
||||||
gtk_box_pack_start(GTK_BOX(done), label("✓", "done-mark"), FALSE, FALSE, 0);
|
|
||||||
done_label = label("登录成功", "done-text");
|
|
||||||
gtk_label_set_justify(GTK_LABEL(done_label), GTK_JUSTIFY_CENTER);
|
|
||||||
gtk_label_set_line_wrap(GTK_LABEL(done_label), TRUE);
|
|
||||||
gtk_box_pack_start(GTK_BOX(done), done_label, FALSE, FALSE, 0);
|
|
||||||
gtk_stack_add_named(GTK_STACK(stack), done, "done");
|
|
||||||
gtk_box_pack_start(GTK_BOX(card), stack, TRUE, TRUE, 0);
|
|
||||||
gtk_box_pack_start(GTK_BOX(left), card, FALSE, FALSE, 0);
|
|
||||||
GtkWidget *caption = label("请使用网易 UU 远程 App 扫一扫\n可快速登录", "caption");
|
|
||||||
gtk_label_set_justify(GTK_LABEL(caption), GTK_JUSTIFY_CENTER);
|
|
||||||
gtk_box_pack_start(GTK_BOX(left), caption, FALSE, FALSE, 0);
|
|
||||||
status_label = label("", "status");
|
|
||||||
gtk_label_set_line_wrap(GTK_LABEL(status_label), TRUE);
|
|
||||||
gtk_label_set_justify(GTK_LABEL(status_label), GTK_JUSTIFY_CENTER);
|
|
||||||
gtk_label_set_max_width_chars(GTK_LABEL(status_label), 34);
|
|
||||||
gtk_box_pack_start(GTK_BOX(left), status_label, FALSE, FALSE, 0);
|
|
||||||
gtk_box_pack_start(GTK_BOX(columns), left, TRUE, TRUE, 0);
|
|
||||||
|
|
||||||
GtkWidget *sep = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
|
|
||||||
gtk_widget_set_margin_top(sep, 6);
|
|
||||||
gtk_widget_set_margin_bottom(sep, 24);
|
|
||||||
gtk_box_pack_start(GTK_BOX(columns), sep, FALSE, FALSE, 0);
|
|
||||||
|
|
||||||
// Right: what will be registered.
|
|
||||||
GtkWidget *right = gtk_box_new(GTK_ORIENTATION_VERTICAL, 14);
|
|
||||||
gtk_widget_set_margin_start(right, 30);
|
|
||||||
gtk_widget_set_size_request(right, 300, -1);
|
|
||||||
GtkWidget *rh = label("本机信息", "heading");
|
|
||||||
gtk_box_pack_start(GTK_BOX(right), rh, FALSE, FALSE, 0);
|
|
||||||
GtkWidget *grid = gtk_grid_new();
|
|
||||||
gtk_grid_set_row_spacing(GTK_GRID(grid), 12);
|
|
||||||
gtk_grid_set_column_spacing(GTK_GRID(grid), 16);
|
|
||||||
gtk_widget_set_margin_top(grid, 10);
|
|
||||||
const char *keys[4] = {"设备名称", "操作系统", "处理器", "显卡"};
|
|
||||||
for (int i = 0; i < 4; i++) info_row(GTK_GRID(grid), i, keys[i], info[i]);
|
|
||||||
gtk_box_pack_start(GTK_BOX(right), grid, FALSE, FALSE, 0);
|
|
||||||
GtkWidget *note = label("登录后可在 Mac / Windows / 手机上的官方 UU 远程客户端中连接本机的桌面、文件传输和终端。"
|
|
||||||
"登录信息只保存在本机配置文件中。", "note");
|
|
||||||
gtk_label_set_line_wrap(GTK_LABEL(note), TRUE);
|
|
||||||
gtk_label_set_max_width_chars(GTK_LABEL(note), 30);
|
|
||||||
gtk_label_set_xalign(GTK_LABEL(note), 0);
|
|
||||||
gtk_widget_set_margin_top(note, 10);
|
|
||||||
gtk_box_pack_start(GTK_BOX(right), note, FALSE, FALSE, 0);
|
|
||||||
done_button = gtk_button_new_with_label("完成");
|
|
||||||
gtk_style_context_add_class(gtk_widget_get_style_context(done_button), "primary");
|
|
||||||
gtk_widget_set_halign(done_button, GTK_ALIGN_START);
|
|
||||||
gtk_widget_set_no_show_all(done_button, TRUE);
|
|
||||||
g_signal_connect(done_button, "clicked", G_CALLBACK(on_done_clicked), NULL);
|
|
||||||
gtk_box_pack_end(GTK_BOX(right), done_button, FALSE, FALSE, 20);
|
|
||||||
gtk_box_pack_start(GTK_BOX(columns), right, TRUE, TRUE, 0);
|
|
||||||
|
|
||||||
GtkWidget *footer = label("开源 Linux 主机端 · 非网易官方客户端", "footer");
|
|
||||||
gtk_box_pack_end(GTK_BOX(root), footer, FALSE, FALSE, 0);
|
|
||||||
gtk_widget_show_all(window);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void *ui_main(void *arg) {
|
|
||||||
(void)arg;
|
|
||||||
int ok = gtk_init_check(NULL, NULL);
|
|
||||||
if (ok) build_window();
|
|
||||||
pthread_mutex_lock(&init_lock);
|
|
||||||
init_state = ok ? 1 : -1;
|
|
||||||
pthread_cond_signal(&init_cond);
|
|
||||||
pthread_mutex_unlock(&init_lock);
|
|
||||||
if (ok) gtk_main();
|
|
||||||
else atomic_store(&closed, 1);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns 0 when the window is shown, -1 when no display is usable.
|
|
||||||
int uu_login_ui_start(const char *name, const char *os, const char *cpu, const char *gpu) {
|
|
||||||
if (ui_started) return init_state == 1 ? 0 : -1;
|
|
||||||
const char *src[4] = {name, os, cpu, gpu};
|
|
||||||
for (int i = 0; i < 4; i++) info[i] = g_strdup(src[i] ? src[i] : "");
|
|
||||||
if (pthread_create(&ui_thread, NULL, ui_main, NULL) != 0) return -1;
|
|
||||||
ui_started = 1;
|
|
||||||
pthread_mutex_lock(&init_lock);
|
|
||||||
while (init_state == 0) pthread_cond_wait(&init_cond, &init_lock);
|
|
||||||
pthread_mutex_unlock(&init_lock);
|
|
||||||
if (init_state != 1) { pthread_join(ui_thread, NULL); return -1; }
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static gboolean apply_qr(gpointer data) {
|
|
||||||
QrUpdate *u = data;
|
|
||||||
if (window) {
|
|
||||||
GdkPixbuf *pb = gdk_pixbuf_new_from_data(u->rgba, GDK_COLORSPACE_RGB, TRUE, 8, u->edge, u->edge, u->edge * 4,
|
|
||||||
(GdkPixbufDestroyNotify)g_free, NULL);
|
|
||||||
gtk_image_set_from_pixbuf(GTK_IMAGE(qr_image), pb);
|
|
||||||
g_object_unref(pb);
|
|
||||||
gtk_stack_set_visible_child_name(GTK_STACK(stack), "qr");
|
|
||||||
} else g_free(u->rgba);
|
|
||||||
g_free(u);
|
|
||||||
return G_SOURCE_REMOVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
void uu_login_ui_set_qr(const unsigned char *rgba, int edge) {
|
|
||||||
if (atomic_load(&closed) || edge <= 0) return;
|
|
||||||
QrUpdate *u = g_new(QrUpdate, 1);
|
|
||||||
u->rgba = g_memdup2(rgba, (gsize)edge * edge * 4);
|
|
||||||
u->edge = edge;
|
|
||||||
g_idle_add(apply_qr, u);
|
|
||||||
}
|
|
||||||
|
|
||||||
static gboolean apply_status(gpointer data) {
|
|
||||||
StatusUpdate *u = data;
|
|
||||||
if (window) {
|
|
||||||
GtkStyleContext *ctx = gtk_widget_get_style_context(status_label);
|
|
||||||
gtk_style_context_remove_class(ctx, "ok");
|
|
||||||
gtk_style_context_remove_class(ctx, "error");
|
|
||||||
if (u->kind == 1) gtk_style_context_add_class(ctx, "ok");
|
|
||||||
if (u->kind == 2) gtk_style_context_add_class(ctx, "error");
|
|
||||||
gtk_label_set_text(GTK_LABEL(status_label), u->text);
|
|
||||||
if (u->kind == 2 && !gtk_image_get_pixbuf(GTK_IMAGE(qr_image)))
|
|
||||||
gtk_widget_hide(gtk_stack_get_child_by_name(GTK_STACK(stack), "loading"));
|
|
||||||
}
|
|
||||||
g_free(u->text);
|
|
||||||
g_free(u);
|
|
||||||
return G_SOURCE_REMOVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
// kind: 0 info, 1 success, 2 error.
|
|
||||||
void uu_login_ui_set_status(const char *text, int kind) {
|
|
||||||
if (atomic_load(&closed)) return;
|
|
||||||
StatusUpdate *u = g_new(StatusUpdate, 1);
|
|
||||||
u->text = g_strdup(text ? text : "");
|
|
||||||
u->kind = kind;
|
|
||||||
g_idle_add(apply_status, u);
|
|
||||||
}
|
|
||||||
|
|
||||||
static gboolean apply_done(gpointer data) {
|
|
||||||
if (window) {
|
|
||||||
gtk_label_set_text(GTK_LABEL(done_label), data);
|
|
||||||
gtk_stack_set_visible_child_name(GTK_STACK(stack), "done");
|
|
||||||
gtk_widget_show(done_button);
|
|
||||||
gtk_widget_grab_focus(done_button);
|
|
||||||
}
|
|
||||||
g_free(data);
|
|
||||||
return G_SOURCE_REMOVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
void uu_login_ui_set_done(const char *text) {
|
|
||||||
if (atomic_load(&closed)) return;
|
|
||||||
g_idle_add(apply_done, g_strdup(text ? text : ""));
|
|
||||||
}
|
|
||||||
|
|
||||||
int uu_login_ui_closed(void) { return atomic_load(&closed); }
|
|
||||||
|
|
||||||
static gboolean apply_close(gpointer data) {
|
|
||||||
(void)data;
|
|
||||||
if (window) gtk_widget_destroy(window);
|
|
||||||
return G_SOURCE_REMOVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
void uu_login_ui_close(void) {
|
|
||||||
if (!ui_started) return;
|
|
||||||
if (!atomic_load(&closed)) g_idle_add(apply_close, NULL);
|
|
||||||
pthread_join(ui_thread, NULL);
|
|
||||||
ui_started = 0;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,167 @@
|
|||||||
|
/* All access is serialized by PortalSession's handle mutex, including close.
|
||||||
|
* ConnectToEIS disables every legacy Notify* input method on this session. */
|
||||||
|
enum { PORTAL_EIS_UNSUPPORTED = 13 };
|
||||||
|
static void portal_eis_log(struct ei *ei, enum ei_log_priority priority,
|
||||||
|
const char *message, struct ei_log_context *context) {
|
||||||
|
/* Library error text can include input values. Keep diagnostics numeric. */
|
||||||
|
static atomic_uint count;
|
||||||
|
if (atomic_fetch_add(&count, 1) < 12)
|
||||||
|
fprintf(stderr, "Portal EIS library diagnostic: priority=%d\n", priority);
|
||||||
|
}
|
||||||
|
static int portal_eis_dispatch(struct uu_portal *p) {
|
||||||
|
ei_dispatch(p->ei);
|
||||||
|
struct ei_event *event;
|
||||||
|
while ((event = ei_get_event(p->ei))) {
|
||||||
|
struct ei_device *device = ei_event_get_device(event);
|
||||||
|
switch (ei_event_get_type(event)) {
|
||||||
|
case EI_EVENT_SEAT_ADDED:
|
||||||
|
ei_seat_bind_capabilities(ei_event_get_seat(event),
|
||||||
|
EI_DEVICE_CAP_POINTER, EI_DEVICE_CAP_POINTER_ABSOLUTE,
|
||||||
|
EI_DEVICE_CAP_BUTTON, EI_DEVICE_CAP_SCROLL, EI_DEVICE_CAP_KEYBOARD, NULL);
|
||||||
|
break;
|
||||||
|
case EI_EVENT_DEVICE_RESUMED:
|
||||||
|
if (!g_list_find(p->ei_devices, device)) {
|
||||||
|
p->ei_devices = g_list_append(p->ei_devices, ei_device_ref(device));
|
||||||
|
ei_device_start_emulating(device, ++p->ei_sequence);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case EI_EVENT_DEVICE_PAUSED:
|
||||||
|
case EI_EVENT_DEVICE_REMOVED:
|
||||||
|
if (g_list_find(p->ei_devices, device)) {
|
||||||
|
p->ei_devices = g_list_remove(p->ei_devices, device);
|
||||||
|
ei_device_unref(device);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case EI_EVENT_DISCONNECT:
|
||||||
|
p->ei_failed = 1;
|
||||||
|
break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
ei_event_unref(event);
|
||||||
|
}
|
||||||
|
return p->ei_failed ? 9 : 0;
|
||||||
|
}
|
||||||
|
static struct ei_device *portal_eis_device(struct uu_portal *p, enum ei_device_capability capability) {
|
||||||
|
for (GList *item = p->ei_devices; item; item = item->next)
|
||||||
|
if (ei_device_has_capability(item->data, capability)) return item->data;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
static struct ei_region *portal_eis_region(struct uu_portal *p, struct ei_device *device) {
|
||||||
|
for (size_t i = 0; ; i++) {
|
||||||
|
struct ei_region *region = ei_device_get_region(device, i);
|
||||||
|
if (!region) return NULL;
|
||||||
|
const char *id = ei_region_get_mapping_id(region);
|
||||||
|
if (id && p->mapping_id && !strcmp(id, p->mapping_id) &&
|
||||||
|
ei_region_get_width(region) && ei_region_get_height(region)) return region;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static struct ei_device *portal_eis_absolute(struct uu_portal *p, struct ei_region **region) {
|
||||||
|
for (GList *item = p->ei_devices; item; item = item->next) {
|
||||||
|
struct ei_device *device = item->data;
|
||||||
|
if (ei_device_has_capability(device, EI_DEVICE_CAP_POINTER_ABSOLUTE) &&
|
||||||
|
(*region = portal_eis_region(p, device))) return device;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
static void portal_eis_close(struct uu_portal *p) {
|
||||||
|
for (GList *item = p->ei_devices; item; item = item->next) ei_device_unref(item->data);
|
||||||
|
g_list_free(p->ei_devices); p->ei_devices = NULL;
|
||||||
|
if (p->ei) p->ei = ei_unref(p->ei);
|
||||||
|
}
|
||||||
|
static int portal_eis_open(struct uu_portal *p) {
|
||||||
|
if (!p->mapping_id) return PORTAL_EIS_UNSUPPORTED;
|
||||||
|
GUnixFDList *fds = NULL;
|
||||||
|
GError *error = NULL;
|
||||||
|
GVariant *reply = g_dbus_connection_call_with_unix_fd_list_sync(p->bus, DEST, PATH, REMOTE,
|
||||||
|
"ConnectToEIS", g_variant_new("(o@a{sv})", p->session, empty_options()),
|
||||||
|
G_VARIANT_TYPE("(h)"), G_DBUS_CALL_FLAGS_NONE, 3000, NULL, &fds, NULL, &error);
|
||||||
|
if (!reply) {
|
||||||
|
int unsupported = g_error_matches(error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD) ||
|
||||||
|
g_error_matches(error, G_DBUS_ERROR, G_DBUS_ERROR_NOT_SUPPORTED);
|
||||||
|
g_clear_error(&error); g_clear_object(&fds);
|
||||||
|
return unsupported ? PORTAL_EIS_UNSUPPORTED : 9;
|
||||||
|
}
|
||||||
|
gint32 index; g_variant_get(reply, "(h)", &index);
|
||||||
|
int fd = fds && index >= 0 && index < g_unix_fd_list_get_length(fds) ?
|
||||||
|
g_unix_fd_list_get(fds, index, NULL) : -1;
|
||||||
|
g_variant_unref(reply); g_clear_object(&fds);
|
||||||
|
if (fd < 0) return 9;
|
||||||
|
p->ei = ei_new_sender(NULL);
|
||||||
|
if (!p->ei) { close(fd); return 8; }
|
||||||
|
ei_log_set_handler(p->ei, portal_eis_log);
|
||||||
|
ei_configure_name(p->ei, "uu-remote-linux");
|
||||||
|
if (ei_setup_backend_fd(p->ei, fd)) return 9;
|
||||||
|
gint64 deadline = g_get_monotonic_time() + 3000000;
|
||||||
|
while (!portal_eis_dispatch(p)) {
|
||||||
|
struct ei_region *region = NULL;
|
||||||
|
if (portal_eis_absolute(p, ®ion) &&
|
||||||
|
portal_eis_device(p, EI_DEVICE_CAP_POINTER) &&
|
||||||
|
portal_eis_device(p, EI_DEVICE_CAP_BUTTON) &&
|
||||||
|
portal_eis_device(p, EI_DEVICE_CAP_SCROLL) &&
|
||||||
|
portal_eis_device(p, EI_DEVICE_CAP_KEYBOARD)) {
|
||||||
|
fprintf(stderr, "Portal EIS input ready: region=%ux%u\n",
|
||||||
|
ei_region_get_width(region), ei_region_get_height(region));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
gint64 remaining = deadline - g_get_monotonic_time();
|
||||||
|
if (remaining <= 0) break;
|
||||||
|
struct pollfd pending = {.fd = ei_get_fd(p->ei), .events = POLLIN};
|
||||||
|
int result = poll(&pending, 1, (int)((remaining + 999) / 1000));
|
||||||
|
if (result < 0 && errno != EINTR) break;
|
||||||
|
}
|
||||||
|
fprintf(stderr, "Portal EIS input initialization failed\n");
|
||||||
|
return 9;
|
||||||
|
}
|
||||||
|
static int portal_eis_available(struct uu_portal *p) {
|
||||||
|
if (!uu_portal_alive(p)) return 9;
|
||||||
|
return portal_eis_dispatch(p);
|
||||||
|
}
|
||||||
|
static int portal_eis_frame(struct uu_portal *p, struct ei_device *device) {
|
||||||
|
ei_device_frame(device, ei_now(p->ei));
|
||||||
|
return portal_eis_dispatch(p);
|
||||||
|
}
|
||||||
|
static int portal_eis_move(struct uu_portal *p, double x, double y, int relative) {
|
||||||
|
if (portal_eis_available(p)) return 9;
|
||||||
|
struct ei_device *device;
|
||||||
|
if (relative) {
|
||||||
|
device = portal_eis_device(p, EI_DEVICE_CAP_POINTER);
|
||||||
|
if (!device) return 9;
|
||||||
|
ei_device_pointer_motion(device, x * p->logical_width / p->width,
|
||||||
|
y * p->logical_height / p->height);
|
||||||
|
} else {
|
||||||
|
if (x < 0 || y < 0 || x >= p->width || y >= p->height) return 11;
|
||||||
|
struct ei_region *region = NULL;
|
||||||
|
device = portal_eis_absolute(p, ®ion);
|
||||||
|
if (!device) return 9;
|
||||||
|
double target_x = ei_region_get_x(region) + x * ei_region_get_width(region) / p->width;
|
||||||
|
double target_y = ei_region_get_y(region) + y * ei_region_get_height(region) / p->height;
|
||||||
|
ei_device_pointer_motion_absolute(device, target_x, target_y);
|
||||||
|
}
|
||||||
|
return portal_eis_frame(p, device);
|
||||||
|
}
|
||||||
|
static int portal_eis_button(struct uu_portal *p, int button, int pressed) {
|
||||||
|
if (portal_eis_available(p)) return 9;
|
||||||
|
if (button >= 4 && button <= 7) {
|
||||||
|
if (!pressed) return 0;
|
||||||
|
struct ei_device *device = portal_eis_device(p, EI_DEVICE_CAP_SCROLL);
|
||||||
|
if (!device) return 9;
|
||||||
|
int step = button == 4 || button == 6 ? -120 : 120;
|
||||||
|
ei_device_scroll_discrete(device, button <= 5 ? 0 : step, button <= 5 ? step : 0);
|
||||||
|
return portal_eis_frame(p, device);
|
||||||
|
}
|
||||||
|
int code = button == 1 ? 0x110 : button == 2 ? 0x112 : button == 3 ? 0x111 :
|
||||||
|
button == 8 ? 0x113 : button == 9 ? 0x114 : 0;
|
||||||
|
if (!code) return 11;
|
||||||
|
struct ei_device *device = portal_eis_device(p, EI_DEVICE_CAP_BUTTON);
|
||||||
|
if (!device) return 9;
|
||||||
|
ei_device_button_button(device, code, !!pressed);
|
||||||
|
return portal_eis_frame(p, device);
|
||||||
|
}
|
||||||
|
static int portal_eis_key(struct uu_portal *p, int code, int pressed) {
|
||||||
|
if (code < 0 || code > 0x2ff) return 11;
|
||||||
|
if (portal_eis_available(p)) return 9;
|
||||||
|
struct ei_device *device = portal_eis_device(p, EI_DEVICE_CAP_KEYBOARD);
|
||||||
|
if (!device) return 9;
|
||||||
|
ei_device_keyboard_key(device, (uint32_t)code, !!pressed);
|
||||||
|
return portal_eis_frame(p, device);
|
||||||
|
}
|
||||||
+238
-39
@@ -2,6 +2,8 @@
|
|||||||
#include "portal_engine.h"
|
#include "portal_engine.h"
|
||||||
#include <gio/gio.h>
|
#include <gio/gio.h>
|
||||||
#include <gio/gunixfdlist.h>
|
#include <gio/gunixfdlist.h>
|
||||||
|
#include <libei.h>
|
||||||
|
#include <poll.h>
|
||||||
#include <pipewire/pipewire.h>
|
#include <pipewire/pipewire.h>
|
||||||
#include <spa/param/video/format-utils.h>
|
#include <spa/param/video/format-utils.h>
|
||||||
#include <spa/pod/builder.h>
|
#include <spa/pod/builder.h>
|
||||||
@@ -21,6 +23,10 @@
|
|||||||
#define SCREEN "org.freedesktop.portal.ScreenCast"
|
#define SCREEN "org.freedesktop.portal.ScreenCast"
|
||||||
#define REQUEST "org.freedesktop.portal.Request"
|
#define REQUEST "org.freedesktop.portal.Request"
|
||||||
#define SESSION "org.freedesktop.portal.Session"
|
#define SESSION "org.freedesktop.portal.Session"
|
||||||
|
#define PORTAL_RENEGOTIATION_TIMEOUT (5 * G_TIME_SPAN_SECOND)
|
||||||
|
#define PORTAL_DISPLAY_GRACE (200 * G_TIME_SPAN_MILLISECOND)
|
||||||
|
#define MUTTER_DISPLAY "org.gnome.Mutter.DisplayConfig"
|
||||||
|
#define MUTTER_DISPLAY_PATH "/org/gnome/Mutter/DisplayConfig"
|
||||||
|
|
||||||
struct uu_portal {
|
struct uu_portal {
|
||||||
GDBusConnection *bus;
|
GDBusConnection *bus;
|
||||||
@@ -30,7 +36,7 @@ struct uu_portal {
|
|||||||
int dbus_started;
|
int dbus_started;
|
||||||
char *session;
|
char *session;
|
||||||
char *restore_token;
|
char *restore_token;
|
||||||
guint closed_subscription, clipboard_owner_subscription, clipboard_transfer_subscription;
|
guint closed_subscription, clipboard_owner_subscription, clipboard_transfer_subscription, display_subscription;
|
||||||
int clipboard_enabled, clipboard_changed;
|
int clipboard_enabled, clipboard_changed;
|
||||||
char *clipboard_text, *clipboard_mime;
|
char *clipboard_text, *clipboard_mime;
|
||||||
size_t clipboard_length;
|
size_t clipboard_length;
|
||||||
@@ -43,14 +49,26 @@ struct uu_portal {
|
|||||||
pthread_mutex_t lock;
|
pthread_mutex_t lock;
|
||||||
pthread_cond_t changed;
|
pthread_cond_t changed;
|
||||||
atomic_int closed;
|
atomic_int closed;
|
||||||
|
uu_portal_cancelled_fn cancelled;
|
||||||
|
void *cancel_data;
|
||||||
int error;
|
int error;
|
||||||
|
enum pw_stream_state state;
|
||||||
|
int format_valid, stream_started;
|
||||||
|
gint64 interrupted_at, display_changed_at;
|
||||||
uint32_t node, width, height;
|
uint32_t node, width, height;
|
||||||
int logical_width, logical_height;
|
int logical_width, logical_height;
|
||||||
int absolute_stream_pixels;
|
int absolute_stream_pixels;
|
||||||
|
struct ei *ei;
|
||||||
|
GList *ei_devices;
|
||||||
|
char *mapping_id;
|
||||||
|
uint32_t ei_sequence;
|
||||||
|
atomic_int ei_failed;
|
||||||
|
int ei_unavailable;
|
||||||
enum spa_video_format format;
|
enum spa_video_format format;
|
||||||
uint8_t *frame;
|
uint8_t *frame;
|
||||||
size_t frame_length;
|
size_t frame_length;
|
||||||
uint64_t sequence;
|
uint64_t sequence;
|
||||||
|
uint64_t revision;
|
||||||
};
|
};
|
||||||
#include "portal_clipboard.inc"
|
#include "portal_clipboard.inc"
|
||||||
|
|
||||||
@@ -58,6 +76,11 @@ static pthread_once_t pw_once = PTHREAD_ONCE_INIT;
|
|||||||
static atomic_uint token_sequence = 1;
|
static atomic_uint token_sequence = 1;
|
||||||
static void initialize_pipewire(void) { pw_init(NULL, NULL); }
|
static void initialize_pipewire(void) { pw_init(NULL, NULL); }
|
||||||
static GVariant *empty_options(void) { return g_variant_new_array(G_VARIANT_TYPE("{sv}"), NULL, 0); }
|
static GVariant *empty_options(void) { return g_variant_new_array(G_VARIANT_TYPE("{sv}"), NULL, 0); }
|
||||||
|
static int open_cancelled(struct uu_portal *p) {
|
||||||
|
return p->cancelled && p->cancelled(p->cancel_data);
|
||||||
|
}
|
||||||
|
static int wait_for_first_frame(struct uu_portal *p);
|
||||||
|
#include "portal_eis.inc"
|
||||||
static void fail(struct uu_portal *p, int error) {
|
static void fail(struct uu_portal *p, int error) {
|
||||||
pthread_mutex_lock(&p->lock);
|
pthread_mutex_lock(&p->lock);
|
||||||
if (!p->error) p->error = error;
|
if (!p->error) p->error = error;
|
||||||
@@ -73,6 +96,16 @@ static void response_received(GDBusConnection *bus, const gchar *sender, const g
|
|||||||
g_variant_get(parameters, "(u@a{sv})", &r->code, &r->results);
|
g_variant_get(parameters, "(u@a{sv})", &r->code, &r->results);
|
||||||
r->done = 1;
|
r->done = 1;
|
||||||
}
|
}
|
||||||
|
/* Bounded consent wait; cancellation must break it within one poll interval. */
|
||||||
|
static int wait_for_response(struct uu_portal *p, struct response *response, unsigned consent_seconds) {
|
||||||
|
gint64 deadline = g_get_monotonic_time() + consent_seconds * G_TIME_SPAN_SECOND;
|
||||||
|
while (!response->done && !atomic_load(&p->closed) && !open_cancelled(p) &&
|
||||||
|
g_get_monotonic_time() < deadline) {
|
||||||
|
while (g_main_context_iteration(p->context, FALSE)) {}
|
||||||
|
if (!response->done) g_usleep(5000);
|
||||||
|
}
|
||||||
|
return response->done ? 0 : open_cancelled(p) ? 13 : atomic_load(&p->closed) ? 9 : 5;
|
||||||
|
}
|
||||||
static void close_request(struct uu_portal *p, const char *path) {
|
static void close_request(struct uu_portal *p, const char *path) {
|
||||||
if (!path) return;
|
if (!path) return;
|
||||||
GVariant *r = g_dbus_connection_call_sync(p->bus, DEST, path, REQUEST, "Close",
|
GVariant *r = g_dbus_connection_call_sync(p->bus, DEST, path, REQUEST, "Close",
|
||||||
@@ -82,6 +115,7 @@ static void close_request(struct uu_portal *p, const char *path) {
|
|||||||
/* Subscribe before invocation, using the documented handle_token path. */
|
/* Subscribe before invocation, using the documented handle_token path. */
|
||||||
static GVariant *request(struct uu_portal *p, const char *interface, const char *method,
|
static GVariant *request(struct uu_portal *p, const char *interface, const char *method,
|
||||||
GVariantBuilder *options, int mode, int *error) {
|
GVariantBuilder *options, int mode, int *error) {
|
||||||
|
if (open_cancelled(p)) { *error = 13; return NULL; }
|
||||||
char token[64];
|
char token[64];
|
||||||
snprintf(token, sizeof(token), "uu_%u_%u", (unsigned)getpid(), atomic_fetch_add(&token_sequence, 1));
|
snprintf(token, sizeof(token), "uu_%u_%u", (unsigned)getpid(), atomic_fetch_add(&token_sequence, 1));
|
||||||
g_variant_builder_add(options, "{sv}", "handle_token", g_variant_new_string(token));
|
g_variant_builder_add(options, "{sv}", "handle_token", g_variant_new_string(token));
|
||||||
@@ -98,7 +132,7 @@ static GVariant *request(struct uu_portal *p, const char *interface, const char
|
|||||||
GVariant *reply = g_dbus_connection_call_sync(p->bus, DEST, PATH, interface, method, args,
|
GVariant *reply = g_dbus_connection_call_sync(p->bus, DEST, PATH, interface, method, args,
|
||||||
G_VARIANT_TYPE("(o)"), G_DBUS_CALL_FLAGS_NONE, 10000, NULL, NULL);
|
G_VARIANT_TYPE("(o)"), G_DBUS_CALL_FLAGS_NONE, 10000, NULL, NULL);
|
||||||
char *actual = NULL;
|
char *actual = NULL;
|
||||||
if (!reply) { *error = 3; goto finish; }
|
if (!reply) { *error = open_cancelled(p) ? 13 : 3; goto finish; }
|
||||||
const char *handle;
|
const char *handle;
|
||||||
g_variant_get(reply, "(&o)", &handle);
|
g_variant_get(reply, "(&o)", &handle);
|
||||||
actual = g_strdup(handle);
|
actual = g_strdup(handle);
|
||||||
@@ -111,13 +145,8 @@ static GVariant *request(struct uu_portal *p, const char *interface, const char
|
|||||||
unsigned long parsed=strtoul(configured_timeout,&end,10);
|
unsigned long parsed=strtoul(configured_timeout,&end,10);
|
||||||
if(end && !*end && parsed>=30 && parsed<=900) consent_seconds=(unsigned)parsed;
|
if(end && !*end && parsed>=30 && parsed<=900) consent_seconds=(unsigned)parsed;
|
||||||
}
|
}
|
||||||
gint64 deadline = g_get_monotonic_time() + consent_seconds * G_TIME_SPAN_SECOND;
|
*error = wait_for_response(p, &response, consent_seconds);
|
||||||
while (!response.done && !atomic_load(&p->closed) && g_get_monotonic_time() < deadline) {
|
if (!*error && response.code != 0) *error = 4;
|
||||||
while (g_main_context_iteration(p->context, FALSE)) {}
|
|
||||||
if (!response.done) g_usleep(5000);
|
|
||||||
}
|
|
||||||
if (!response.done) *error = atomic_load(&p->closed) ? 9 : 5;
|
|
||||||
else if (response.code != 0) *error = 4;
|
|
||||||
finish:
|
finish:
|
||||||
if (*error) close_request(p, actual ? actual : expected);
|
if (*error) close_request(p, actual ? actual : expected);
|
||||||
g_dbus_connection_signal_unsubscribe(p->bus, subscription);
|
g_dbus_connection_signal_unsubscribe(p->bus, subscription);
|
||||||
@@ -145,13 +174,50 @@ static int has_interface(struct uu_portal *p, const char *interface) {
|
|||||||
if (!r) return 0;
|
if (!r) return 0;
|
||||||
g_variant_unref(r); return 1;
|
g_variant_unref(r); return 1;
|
||||||
}
|
}
|
||||||
|
/* Called with the frame mutex held. Count only the continuous invalid pause,
|
||||||
|
* not a healthy pause before format removal, and never arm during startup. */
|
||||||
|
static void update_interruption(struct uu_portal *p) {
|
||||||
|
if (p->stream_started && p->state == PW_STREAM_STATE_PAUSED && !p->format_valid) {
|
||||||
|
if (!p->interrupted_at) p->interrupted_at = g_get_monotonic_time();
|
||||||
|
} else {
|
||||||
|
if (p->interrupted_at) p->display_changed_at = 0;
|
||||||
|
p->interrupted_at = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static void monitors_changed(GDBusConnection *bus, const gchar *sender, const gchar *path,
|
||||||
|
const gchar *interface, const gchar *signal, GVariant *parameters, gpointer data) {
|
||||||
|
struct uu_portal *p = data;
|
||||||
|
pthread_mutex_lock(&p->lock);
|
||||||
|
/* #27: measured NULL format 10-36ms after the notification, with PAUSED
|
||||||
|
* 5-9ms before it. Preserve a correlated signal across repeated changes. */
|
||||||
|
if (p->stream_started && (!p->display_changed_at || !p->interrupted_at ||
|
||||||
|
p->display_changed_at < p->interrupted_at - PORTAL_DISPLAY_GRACE))
|
||||||
|
p->display_changed_at = g_get_monotonic_time();
|
||||||
|
pthread_mutex_unlock(&p->lock);
|
||||||
|
}
|
||||||
static void stream_state(void *data, enum pw_stream_state old, enum pw_stream_state state, const char *message) {
|
static void stream_state(void *data, enum pw_stream_state old, enum pw_stream_state state, const char *message) {
|
||||||
(void)old; (void)message;
|
(void)old; (void)message;
|
||||||
|
struct uu_portal *p = data;
|
||||||
|
pthread_mutex_lock(&p->lock);
|
||||||
|
p->state = state;
|
||||||
|
if (state == PW_STREAM_STATE_STREAMING) {
|
||||||
|
p->stream_started = 1;
|
||||||
|
p->display_changed_at = 0;
|
||||||
|
}
|
||||||
|
update_interruption(p);
|
||||||
|
pthread_mutex_unlock(&p->lock);
|
||||||
if (state == PW_STREAM_STATE_ERROR || state == PW_STREAM_STATE_UNCONNECTED) fail(data, 9);
|
if (state == PW_STREAM_STATE_ERROR || state == PW_STREAM_STATE_UNCONNECTED) fail(data, 9);
|
||||||
}
|
}
|
||||||
static void stream_format(void *data, uint32_t id, const struct spa_pod *param) {
|
static void stream_format(void *data, uint32_t id, const struct spa_pod *param) {
|
||||||
struct uu_portal *p = data;
|
struct uu_portal *p = data;
|
||||||
if (id != SPA_PARAM_Format || !param) return;
|
if (id != SPA_PARAM_Format) return;
|
||||||
|
if (!param) {
|
||||||
|
pthread_mutex_lock(&p->lock);
|
||||||
|
p->format_valid = 0;
|
||||||
|
update_interruption(p);
|
||||||
|
pthread_mutex_unlock(&p->lock);
|
||||||
|
return;
|
||||||
|
}
|
||||||
struct spa_video_info_raw info = {0};
|
struct spa_video_info_raw info = {0};
|
||||||
if (spa_format_video_raw_parse(param, &info) < 0 || !info.size.width || !info.size.height ||
|
if (spa_format_video_raw_parse(param, &info) < 0 || !info.size.width || !info.size.height ||
|
||||||
info.size.width > 8192 || info.size.height > 8192 ||
|
info.size.width > 8192 || info.size.height > 8192 ||
|
||||||
@@ -166,13 +232,16 @@ static void stream_format(void *data, uint32_t id, const struct spa_pod *param)
|
|||||||
if (!p->frame) { p->error = 8; pthread_mutex_unlock(&p->lock); return; }
|
if (!p->frame) { p->error = 8; pthread_mutex_unlock(&p->lock); return; }
|
||||||
p->width = info.size.width; p->height = info.size.height; p->format = info.format;
|
p->width = info.size.width; p->height = info.size.height; p->format = info.format;
|
||||||
p->frame_length = length;
|
p->frame_length = length;
|
||||||
|
p->format_valid = 1;
|
||||||
|
p->display_changed_at = 0;
|
||||||
|
update_interruption(p);
|
||||||
pthread_cond_broadcast(&p->changed);
|
pthread_cond_broadcast(&p->changed);
|
||||||
pthread_mutex_unlock(&p->lock);
|
pthread_mutex_unlock(&p->lock);
|
||||||
}
|
}
|
||||||
/* Called with the frame mutex held. Invalid transient chunks are discarded;
|
/* Called with the frame mutex held. Invalid transient chunks are discarded;
|
||||||
* allocation capacity is not evidence that valid pixels have been supplied. */
|
* allocation capacity is not evidence that valid pixels have been supplied. */
|
||||||
static void copy_video_buffer(struct uu_portal *p, struct spa_buffer *spa) {
|
static void copy_video_buffer(struct uu_portal *p, struct spa_buffer *spa) {
|
||||||
if (!p->error && p->frame && spa->n_datas >= 1) {
|
if (!p->error && p->format_valid && p->frame && spa->n_datas >= 1) {
|
||||||
struct spa_data *d = &spa->datas[0];
|
struct spa_data *d = &spa->datas[0];
|
||||||
struct spa_meta_header *header = spa_buffer_find_meta_data(spa, SPA_META_Header, sizeof(*header));
|
struct spa_meta_header *header = spa_buffer_find_meta_data(spa, SPA_META_Header, sizeof(*header));
|
||||||
if (!d->chunk || (d->chunk->flags & SPA_CHUNK_FLAG_CORRUPTED) ||
|
if (!d->chunk || (d->chunk->flags & SPA_CHUNK_FLAG_CORRUPTED) ||
|
||||||
@@ -182,6 +251,7 @@ static void copy_video_buffer(struct uu_portal *p, struct spa_buffer *spa) {
|
|||||||
memset(p->frame, 0, p->frame_length);
|
memset(p->frame, 0, p->frame_length);
|
||||||
for (size_t pixel = 0; pixel < p->frame_length / 4; pixel++) p->frame[pixel * 4 + 3] = 255;
|
for (size_t pixel = 0; pixel < p->frame_length / 4; pixel++) p->frame[pixel * 4 + 3] = 255;
|
||||||
p->sequence++;
|
p->sequence++;
|
||||||
|
p->revision++;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
struct spa_meta_region *crop = spa_buffer_find_meta_data(spa, SPA_META_VideoCrop, sizeof(*crop));
|
struct spa_meta_region *crop = spa_buffer_find_meta_data(spa, SPA_META_VideoCrop, sizeof(*crop));
|
||||||
@@ -203,6 +273,7 @@ static void copy_video_buffer(struct uu_portal *p, struct spa_buffer *spa) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
p->sequence++;
|
p->sequence++;
|
||||||
|
p->revision++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -224,10 +295,16 @@ static const struct pw_stream_events stream_events = {
|
|||||||
PW_VERSION_STREAM_EVENTS, .state_changed = stream_state, .param_changed = stream_format, .process = stream_process
|
PW_VERSION_STREAM_EVENTS, .state_changed = stream_state, .param_changed = stream_format, .process = stream_process
|
||||||
};
|
};
|
||||||
static int connect_pipewire(struct uu_portal *p) {
|
static int connect_pipewire(struct uu_portal *p) {
|
||||||
|
if (open_cancelled(p)) return 13;
|
||||||
GUnixFDList *fds = NULL;
|
GUnixFDList *fds = NULL;
|
||||||
GVariant *reply = g_dbus_connection_call_with_unix_fd_list_sync(p->bus, DEST, PATH, SCREEN,
|
GVariant *reply = g_dbus_connection_call_with_unix_fd_list_sync(p->bus, DEST, PATH, SCREEN,
|
||||||
"OpenPipeWireRemote", g_variant_new("(o@a{sv})", p->session, empty_options()),
|
"OpenPipeWireRemote", g_variant_new("(o@a{sv})", p->session, empty_options()),
|
||||||
G_VARIANT_TYPE("(h)"), G_DBUS_CALL_FLAGS_NONE, 10000, NULL, &fds, NULL, NULL);
|
G_VARIANT_TYPE("(h)"), G_DBUS_CALL_FLAGS_NONE, 10000, NULL, &fds, NULL, NULL);
|
||||||
|
if (open_cancelled(p)) {
|
||||||
|
if (reply) g_variant_unref(reply);
|
||||||
|
if (fds) g_object_unref(fds);
|
||||||
|
return 13;
|
||||||
|
}
|
||||||
if (!reply) return 7;
|
if (!reply) return 7;
|
||||||
gint32 index; g_variant_get(reply, "(h)", &index);
|
gint32 index; g_variant_get(reply, "(h)", &index);
|
||||||
int fd = g_unix_fd_list_get(fds, index, NULL);
|
int fd = g_unix_fd_list_get(fds, index, NULL);
|
||||||
@@ -259,19 +336,54 @@ static int connect_pipewire(struct uu_portal *p) {
|
|||||||
if (pw_stream_connect(p->stream, PW_DIRECTION_INPUT, p->node,
|
if (pw_stream_connect(p->stream, PW_DIRECTION_INPUT, p->node,
|
||||||
PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS, params, 2) < 0) return 7;
|
PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS, params, 2) < 0) return 7;
|
||||||
if (pw_thread_loop_start(p->pw_loop) < 0) return 7;
|
if (pw_thread_loop_start(p->pw_loop) < 0) return 7;
|
||||||
struct timespec until; clock_gettime(CLOCK_REALTIME, &until); until.tv_sec += 10;
|
return wait_for_first_frame(p);
|
||||||
|
}
|
||||||
|
static int wait_for_first_frame(struct uu_portal *p) {
|
||||||
|
struct timespec until;
|
||||||
|
gint64 deadline = g_get_monotonic_time() + 10 * G_TIME_SPAN_SECOND;
|
||||||
pthread_mutex_lock(&p->lock);
|
pthread_mutex_lock(&p->lock);
|
||||||
while (!p->sequence && !p->error) {
|
while (!p->sequence && !p->error && !open_cancelled(p)) {
|
||||||
if (pthread_cond_timedwait(&p->changed, &p->lock, &until)) break;
|
gint64 remaining = deadline - g_get_monotonic_time();
|
||||||
|
if (remaining <= 0) break;
|
||||||
|
clock_gettime(CLOCK_REALTIME, &until);
|
||||||
|
gint64 wait_us = MIN(100000, remaining);
|
||||||
|
until.tv_nsec += wait_us * 1000;
|
||||||
|
if (until.tv_nsec >= 1000000000) { until.tv_sec++; until.tv_nsec -= 1000000000; }
|
||||||
|
pthread_cond_timedwait(&p->changed, &p->lock, &until);
|
||||||
}
|
}
|
||||||
int result = p->error ? p->error : p->sequence ? 0 : 10;
|
int result = p->error ? p->error : open_cancelled(p) ? 13 : p->sequence ? 0 : 10;
|
||||||
pthread_mutex_unlock(&p->lock);
|
pthread_mutex_unlock(&p->lock);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)(const char *, void *), void *data, int *error, uint32_t *width, uint32_t *height) {
|
static int read_stream_result(struct uu_portal *p, GVariant *results) {
|
||||||
|
guint devices = 0;
|
||||||
|
GVariant *streams = g_variant_lookup_value(results, "streams", G_VARIANT_TYPE("a(ua{sv})"));
|
||||||
|
gboolean has_devices = g_variant_lookup(results, "devices", "u", &devices);
|
||||||
|
fprintf(stderr, "Portal authorization result: devices_present=%d device_bits=%u stream_count=%zu\n",
|
||||||
|
has_devices, devices, streams ? (size_t)g_variant_n_children(streams) : 0);
|
||||||
|
int error = 0;
|
||||||
|
if (!has_devices || (devices & 3) != 3 || !streams || g_variant_n_children(streams) != 1) {
|
||||||
|
error = 6;
|
||||||
|
} else {
|
||||||
|
GVariant *entry = g_variant_get_child_value(streams, 0), *properties;
|
||||||
|
g_variant_get(entry, "(u@a{sv})", &p->node, &properties);
|
||||||
|
/* Optional 'size' is compositor logical coordinates, not frame pixels. */
|
||||||
|
g_variant_lookup(properties, "size", "(ii)", &p->logical_width, &p->logical_height);
|
||||||
|
const char *mapping_id = NULL;
|
||||||
|
if (g_variant_lookup(properties, "mapping_id", "&s", &mapping_id)) p->mapping_id = g_strdup(mapping_id);
|
||||||
|
g_variant_unref(properties); g_variant_unref(entry);
|
||||||
|
}
|
||||||
|
if (streams) g_variant_unref(streams);
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
static struct uu_portal *open_persist(const char *restore, void (*save_token)(const char *, void *),
|
||||||
|
void *save_data, uu_portal_cancelled_fn cancelled, void *cancel_data,
|
||||||
|
int *error, uint32_t *width, uint32_t *height) {
|
||||||
*error = 0; *width = *height = 0;
|
*error = 0; *width = *height = 0;
|
||||||
struct uu_portal *p = calloc(1, sizeof(*p));
|
struct uu_portal *p = calloc(1, sizeof(*p));
|
||||||
if (!p) { *error = 8; return NULL; }
|
if (!p) { *error = 8; return NULL; }
|
||||||
|
p->cancelled = cancelled;
|
||||||
|
p->cancel_data = cancel_data;
|
||||||
/* GNOME forwards NotifyPointerMotionAbsolute into Mutter's stream-to-stage
|
/* GNOME forwards NotifyPointerMotionAbsolute into Mutter's stream-to-stage
|
||||||
* transform. It expects stream pixels, unlike the compositor-sized KDE path.
|
* transform. It expects stream pixels, unlike the compositor-sized KDE path.
|
||||||
* Do not apply the compositor scale twice on fractional-scale displays. */
|
* Do not apply the compositor scale twice on fractional-scale displays. */
|
||||||
@@ -283,7 +395,10 @@ struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)
|
|||||||
p->context = g_main_context_new(); g_main_context_push_thread_default(p->context);
|
p->context = g_main_context_new(); g_main_context_push_thread_default(p->context);
|
||||||
p->bus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
|
p->bus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
|
||||||
if (!p->bus) { *error = 1; goto done; }
|
if (!p->bus) { *error = 1; goto done; }
|
||||||
if (!has_interface(p, REMOTE) || !has_interface(p, SCREEN)) { *error = 2; goto done; }
|
if (open_cancelled(p)) { *error = 13; goto done; }
|
||||||
|
int interfaces_available = has_interface(p, REMOTE) && has_interface(p, SCREEN);
|
||||||
|
if (open_cancelled(p)) { *error = 13; goto done; }
|
||||||
|
if (!interfaces_available) { *error = 2; goto done; }
|
||||||
GVariantBuilder options; GVariant *results;
|
GVariantBuilder options; GVariant *results;
|
||||||
g_variant_builder_init(&options, G_VARIANT_TYPE_VARDICT);
|
g_variant_builder_init(&options, G_VARIANT_TYPE_VARDICT);
|
||||||
char token[64]; snprintf(token, sizeof(token), "uu_session_%u_%u", (unsigned)getpid(), atomic_fetch_add(&token_sequence, 1));
|
char token[64]; snprintf(token, sizeof(token), "uu_session_%u_%u", (unsigned)getpid(), atomic_fetch_add(&token_sequence, 1));
|
||||||
@@ -338,18 +453,7 @@ struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)
|
|||||||
g_variant_lookup(results,"clipboard_enabled","b",&clipboard_enabled);
|
g_variant_lookup(results,"clipboard_enabled","b",&clipboard_enabled);
|
||||||
p->clipboard_enabled=clipboard_enabled;
|
p->clipboard_enabled=clipboard_enabled;
|
||||||
fprintf(stderr,"Portal clipboard permission: granted=%d\n",clipboard_enabled);
|
fprintf(stderr,"Portal clipboard permission: granted=%d\n",clipboard_enabled);
|
||||||
guint devices = 0; GVariant *streams = g_variant_lookup_value(results, "streams", G_VARIANT_TYPE("a(ua{sv})"));
|
*error = read_stream_result(p, results);
|
||||||
gboolean has_devices=g_variant_lookup(results, "devices", "u", &devices);
|
|
||||||
fprintf(stderr,"Portal authorization result: devices_present=%d device_bits=%u stream_count=%zu\n",has_devices,devices,streams?(size_t)g_variant_n_children(streams):0);
|
|
||||||
if (!has_devices || (devices & 3) != 3 || !streams || g_variant_n_children(streams) != 1) {
|
|
||||||
*error = 6;
|
|
||||||
} else {
|
|
||||||
GVariant *entry = g_variant_get_child_value(streams, 0), *properties;
|
|
||||||
g_variant_get(entry, "(u@a{sv})", &p->node, &properties);
|
|
||||||
/* 'size' is compositor logical coordinates, not the pixel frame size. */
|
|
||||||
g_variant_lookup(properties, "size", "(ii)", &p->logical_width, &p->logical_height);
|
|
||||||
g_variant_unref(properties); g_variant_unref(entry);
|
|
||||||
}
|
|
||||||
if (!*error) {
|
if (!*error) {
|
||||||
const char *next = NULL;
|
const char *next = NULL;
|
||||||
if (g_variant_lookup(results, "restore_token", "&s", &next) && next && strlen(next) <= 4096)
|
if (g_variant_lookup(results, "restore_token", "&s", &next) && next && strlen(next) <= 4096)
|
||||||
@@ -357,25 +461,45 @@ struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)
|
|||||||
fprintf(stderr, "Portal persistence: requested=%d restore_attempted=%d token_returned=%d\n",
|
fprintf(stderr, "Portal persistence: requested=%d restore_attempted=%d token_returned=%d\n",
|
||||||
version >= 2, restore && *restore, p->restore_token && *p->restore_token);
|
version >= 2, restore && *restore, p->restore_token && *p->restore_token);
|
||||||
}
|
}
|
||||||
if (!*error && p->restore_token && save_token) save_token(p->restore_token, data);
|
if (!*error && p->restore_token && save_token) save_token(p->restore_token, save_data);
|
||||||
if (streams) g_variant_unref(streams);
|
|
||||||
g_variant_unref(results);
|
g_variant_unref(results);
|
||||||
if (*error) goto done;
|
if (*error) goto done;
|
||||||
p->main_loop = g_main_loop_new(p->context, FALSE);
|
p->main_loop = g_main_loop_new(p->context, FALSE);
|
||||||
|
if (p->absolute_stream_pixels)
|
||||||
|
p->display_subscription = g_dbus_connection_signal_subscribe(p->bus, MUTTER_DISPLAY, MUTTER_DISPLAY,
|
||||||
|
"MonitorsChanged", MUTTER_DISPLAY_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE, monitors_changed, p, NULL);
|
||||||
g_main_context_pop_thread_default(p->context);
|
g_main_context_pop_thread_default(p->context);
|
||||||
if (pthread_create(&p->dbus_thread, NULL, run_dbus, p)) { *error = 3; uu_portal_close(p); return NULL; }
|
if (pthread_create(&p->dbus_thread, NULL, run_dbus, p)) {
|
||||||
|
*error = 3; p->cancelled = NULL; p->cancel_data = NULL;
|
||||||
|
uu_portal_close(p); return NULL;
|
||||||
|
}
|
||||||
p->dbus_started = 1;
|
p->dbus_started = 1;
|
||||||
*error = connect_pipewire(p);
|
*error = connect_pipewire(p);
|
||||||
if (*error) { uu_portal_close(p); return NULL; }
|
if (*error) {
|
||||||
|
p->cancelled = NULL; p->cancel_data = NULL;
|
||||||
|
uu_portal_close(p); return NULL;
|
||||||
|
}
|
||||||
fprintf(stderr, "Portal pointer geometry: frame=%ux%u compositor=%dx%d absolute=%s\n",
|
fprintf(stderr, "Portal pointer geometry: frame=%ux%u compositor=%dx%d absolute=%s\n",
|
||||||
p->width, p->height, p->logical_width, p->logical_height,
|
p->width, p->height, p->logical_width, p->logical_height,
|
||||||
p->absolute_stream_pixels ? "stream-pixels" : "compositor-logical");
|
p->absolute_stream_pixels ? "stream-pixels-or-eis" : "compositor-logical");
|
||||||
*width = p->width; *height = p->height;
|
*width = p->width; *height = p->height;
|
||||||
|
p->cancelled = NULL; p->cancel_data = NULL;
|
||||||
return p;
|
return p;
|
||||||
done:
|
done:
|
||||||
g_main_context_pop_thread_default(p->context);
|
g_main_context_pop_thread_default(p->context);
|
||||||
|
p->cancelled = NULL; p->cancel_data = NULL;
|
||||||
uu_portal_close(p); return NULL;
|
uu_portal_close(p); return NULL;
|
||||||
}
|
}
|
||||||
|
struct uu_portal *uu_portal_open_persist_cancellable(const char *restore,
|
||||||
|
void (*save_token)(const char *, void *), void *save_data,
|
||||||
|
uu_portal_cancelled_fn cancelled, void *cancel_data,
|
||||||
|
int *error, uint32_t *width, uint32_t *height) {
|
||||||
|
return open_persist(restore, save_token, save_data, cancelled, cancel_data, error, width, height);
|
||||||
|
}
|
||||||
|
struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)(const char *, void *),
|
||||||
|
void *data, int *error, uint32_t *width, uint32_t *height) {
|
||||||
|
return open_persist(restore, save_token, data, NULL, NULL, error, width, height);
|
||||||
|
}
|
||||||
struct uu_portal *uu_portal_open_restore(const char *restore, int *error, uint32_t *width, uint32_t *height) {
|
struct uu_portal *uu_portal_open_restore(const char *restore, int *error, uint32_t *width, uint32_t *height) {
|
||||||
return uu_portal_open_persist(restore, NULL, NULL, error, width, height);
|
return uu_portal_open_persist(restore, NULL, NULL, error, width, height);
|
||||||
}
|
}
|
||||||
@@ -387,6 +511,7 @@ static gboolean quit_dbus_loop(gpointer data) { g_main_loop_quit(data); return G
|
|||||||
void uu_portal_close(struct uu_portal *p) {
|
void uu_portal_close(struct uu_portal *p) {
|
||||||
if (!p) return;
|
if (!p) return;
|
||||||
atomic_store(&p->closed, 1);
|
atomic_store(&p->closed, 1);
|
||||||
|
portal_eis_close(p);
|
||||||
if (p->bus && p->session) {
|
if (p->bus && p->session) {
|
||||||
GVariant *reply = g_dbus_connection_call_sync(p->bus, DEST, p->session, SESSION, "Close",
|
GVariant *reply = g_dbus_connection_call_sync(p->bus, DEST, p->session, SESSION, "Close",
|
||||||
NULL, NULL, G_DBUS_CALL_FLAGS_NONE, 3000, NULL, NULL);
|
NULL, NULL, G_DBUS_CALL_FLAGS_NONE, 3000, NULL, NULL);
|
||||||
@@ -395,6 +520,7 @@ void uu_portal_close(struct uu_portal *p) {
|
|||||||
if (p->clipboard_owner_subscription) g_dbus_connection_signal_unsubscribe(p->bus,p->clipboard_owner_subscription);
|
if (p->clipboard_owner_subscription) g_dbus_connection_signal_unsubscribe(p->bus,p->clipboard_owner_subscription);
|
||||||
if (p->clipboard_transfer_subscription) g_dbus_connection_signal_unsubscribe(p->bus,p->clipboard_transfer_subscription);
|
if (p->clipboard_transfer_subscription) g_dbus_connection_signal_unsubscribe(p->bus,p->clipboard_transfer_subscription);
|
||||||
if (p->closed_subscription) g_dbus_connection_signal_unsubscribe(p->bus, p->closed_subscription);
|
if (p->closed_subscription) g_dbus_connection_signal_unsubscribe(p->bus, p->closed_subscription);
|
||||||
|
if (p->display_subscription) g_dbus_connection_signal_unsubscribe(p->bus, p->display_subscription);
|
||||||
if (p->dbus_started) {
|
if (p->dbus_started) {
|
||||||
/* A queued source also handles close racing with thread startup; an
|
/* A queued source also handles close racing with thread startup; an
|
||||||
* immediate g_main_loop_quit before run would leave run blocking. */
|
* immediate g_main_loop_quit before run would leave run blocking. */
|
||||||
@@ -402,6 +528,8 @@ void uu_portal_close(struct uu_portal *p) {
|
|||||||
g_source_set_callback(quit, quit_dbus_loop, p->main_loop, NULL);
|
g_source_set_callback(quit, quit_dbus_loop, p->main_loop, NULL);
|
||||||
g_source_attach(quit, p->context); g_source_unref(quit);
|
g_source_attach(quit, p->context); g_source_unref(quit);
|
||||||
g_main_context_wakeup(p->context); pthread_join(p->dbus_thread, NULL);
|
g_main_context_wakeup(p->context); pthread_join(p->dbus_thread, NULL);
|
||||||
|
} else if (p->context) {
|
||||||
|
while (g_main_context_iteration(p->context, FALSE)) {}
|
||||||
}
|
}
|
||||||
if (p->pw_loop) pw_thread_loop_stop(p->pw_loop);
|
if (p->pw_loop) pw_thread_loop_stop(p->pw_loop);
|
||||||
if (p->stream) pw_stream_destroy(p->stream);
|
if (p->stream) pw_stream_destroy(p->stream);
|
||||||
@@ -411,6 +539,7 @@ void uu_portal_close(struct uu_portal *p) {
|
|||||||
if (p->main_loop) g_main_loop_unref(p->main_loop);
|
if (p->main_loop) g_main_loop_unref(p->main_loop);
|
||||||
if (p->context) g_main_context_unref(p->context);
|
if (p->context) g_main_context_unref(p->context);
|
||||||
if (p->bus) g_object_unref(p->bus);
|
if (p->bus) g_object_unref(p->bus);
|
||||||
|
g_free(p->mapping_id);
|
||||||
g_free(p->clipboard_text); g_free(p->clipboard_mime); g_free(p->restore_token); free(p->session); free(p->frame);
|
g_free(p->clipboard_text); g_free(p->clipboard_mime); g_free(p->restore_token); free(p->session); free(p->frame);
|
||||||
pthread_cond_destroy(&p->changed); pthread_mutex_destroy(&p->lock); free(p);
|
pthread_cond_destroy(&p->changed); pthread_mutex_destroy(&p->lock); free(p);
|
||||||
}
|
}
|
||||||
@@ -418,23 +547,80 @@ void uu_portal_reset_frame(struct uu_portal *p) {
|
|||||||
if (!p) return;
|
if (!p) return;
|
||||||
pthread_mutex_lock(&p->lock);
|
pthread_mutex_lock(&p->lock);
|
||||||
p->sequence = 0;
|
p->sequence = 0;
|
||||||
|
p->revision++;
|
||||||
if (p->frame) memset(p->frame, 0, p->frame_length);
|
if (p->frame) memset(p->frame, 0, p->frame_length);
|
||||||
pthread_mutex_unlock(&p->lock);
|
pthread_mutex_unlock(&p->lock);
|
||||||
}
|
}
|
||||||
int uu_portal_capture(struct uu_portal *p, uint8_t *frame, size_t length) {
|
int uu_portal_capture(struct uu_portal *p, uint8_t *frame, size_t length) {
|
||||||
|
return uu_portal_capture_since(p, frame, length, NULL);
|
||||||
|
}
|
||||||
|
/* A static desktop can stop producing frames indefinitely. Only time out the
|
||||||
|
* paused, format-less renegotiation observed after a live scaling change (#25).
|
||||||
|
* Called with the frame mutex held; recovery runs on the Rust capture worker. */
|
||||||
|
static int stream_stalled(struct uu_portal *p) {
|
||||||
|
if (p->state != PW_STREAM_STATE_PAUSED || p->format_valid || !p->interrupted_at) return 0;
|
||||||
|
gint64 now = g_get_monotonic_time();
|
||||||
|
return now - p->interrupted_at >= PORTAL_RENEGOTIATION_TIMEOUT ||
|
||||||
|
(p->display_changed_at && p->display_changed_at >= p->interrupted_at - PORTAL_DISPLAY_GRACE &&
|
||||||
|
now - MAX(p->interrupted_at, p->display_changed_at) >= PORTAL_DISPLAY_GRACE);
|
||||||
|
}
|
||||||
|
int uu_portal_capture_since(struct uu_portal *p, uint8_t *frame, size_t length, uint64_t *revision) {
|
||||||
if (!p || !frame) return 11;
|
if (!p || !frame) return 11;
|
||||||
pthread_mutex_lock(&p->lock);
|
pthread_mutex_lock(&p->lock);
|
||||||
int result = atomic_load(&p->closed) ? 9 : p->error ? p->error : !p->sequence ? 10 : length != p->frame_length ? 11 : 0;
|
int result = atomic_load(&p->closed) ? 9 : p->error ? p->error : stream_stalled(p) ? 9 :
|
||||||
if (!result) memcpy(frame, p->frame, length);
|
!p->sequence ? 10 : length != p->frame_length ? 11 : 0;
|
||||||
|
if (!result) {
|
||||||
|
if (revision && *revision == p->revision) result = 12;
|
||||||
|
else memcpy(frame, p->frame, length);
|
||||||
|
if (revision) *revision = p->revision;
|
||||||
|
}
|
||||||
pthread_mutex_unlock(&p->lock); return result;
|
pthread_mutex_unlock(&p->lock); return result;
|
||||||
}
|
}
|
||||||
int uu_portal_alive(struct uu_portal *p) {
|
int uu_portal_alive(struct uu_portal *p) {
|
||||||
if (!p || atomic_load(&p->closed)) return 0;
|
if (!p || atomic_load(&p->closed) || atomic_load(&p->ei_failed)) return 0;
|
||||||
pthread_mutex_lock(&p->lock);
|
pthread_mutex_lock(&p->lock);
|
||||||
int alive = !p->error;
|
int alive = !p->error;
|
||||||
pthread_mutex_unlock(&p->lock);
|
pthread_mutex_unlock(&p->lock);
|
||||||
return alive;
|
return alive;
|
||||||
}
|
}
|
||||||
|
/* Video renegotiation must not block input releases or clipboard traffic. */
|
||||||
|
int uu_portal_capture_alive(struct uu_portal *p) {
|
||||||
|
if (!uu_portal_alive(p)) return 0;
|
||||||
|
pthread_mutex_lock(&p->lock);
|
||||||
|
int alive = !stream_stalled(p);
|
||||||
|
pthread_mutex_unlock(&p->lock);
|
||||||
|
return alive;
|
||||||
|
}
|
||||||
|
void uu_portal_input_dispatch(struct uu_portal *p) {
|
||||||
|
if (p->ei) portal_eis_dispatch(p);
|
||||||
|
}
|
||||||
|
static int prepare_input(struct uu_portal *p) {
|
||||||
|
if (!uu_portal_alive(p)) return 9;
|
||||||
|
/* xdg-desktop-portal 1.21 checks Notify absolute positions against logical
|
||||||
|
* stream size, while GNOME 50 forwards them into Mutter's pixel transform.
|
||||||
|
* Initialize EIS only when native input is used, leaving capture/uinput
|
||||||
|
* independent of EIS support. ConnectToEIS disables all Notify* methods. */
|
||||||
|
if (!p->ei && !p->ei_unavailable && p->absolute_stream_pixels &&
|
||||||
|
p->logical_width > 0 && p->logical_height > 0 &&
|
||||||
|
(p->width != (uint32_t)p->logical_width || p->height != (uint32_t)p->logical_height)) {
|
||||||
|
int result = portal_eis_open(p);
|
||||||
|
if (result == PORTAL_EIS_UNSUPPORTED) {
|
||||||
|
p->ei_unavailable = 1;
|
||||||
|
fprintf(stderr, "Portal EIS unavailable; retaining legacy input\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (result) {
|
||||||
|
/* A timeout may have committed ConnectToEIS at the portal. Any
|
||||||
|
* failure except explicit lack of support needs a fresh session. */
|
||||||
|
atomic_store(&p->ei_failed, 1);
|
||||||
|
static atomic_uint failures;
|
||||||
|
if (atomic_fetch_add(&failures, 1) < 12)
|
||||||
|
fprintf(stderr, "Portal EIS input initialization failed: code=%d\n", result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
static int notify(struct uu_portal *p, const char *method, GVariant *parameters) {
|
static int notify(struct uu_portal *p, const char *method, GVariant *parameters) {
|
||||||
if (!uu_portal_alive(p)) { g_variant_ref_sink(parameters); g_variant_unref(parameters); return 9; }
|
if (!uu_portal_alive(p)) { g_variant_ref_sink(parameters); g_variant_unref(parameters); return 9; }
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
@@ -451,18 +637,29 @@ static int notify(struct uu_portal *p, const char *method, GVariant *parameters)
|
|||||||
}
|
}
|
||||||
g_variant_unref(reply); return 0;
|
g_variant_unref(reply); return 0;
|
||||||
}
|
}
|
||||||
|
void uu_portal_logical_size(struct uu_portal *p, uint32_t *width, uint32_t *height) {
|
||||||
|
/* Stream size is optional; unknown logical geometry keeps the existing
|
||||||
|
* pixel-sized inventory without preventing capture or uinput. */
|
||||||
|
int known = p->logical_width > 0 && p->logical_height > 0;
|
||||||
|
*width = known ? (uint32_t)p->logical_width : p->width;
|
||||||
|
*height = known ? (uint32_t)p->logical_height : p->height;
|
||||||
|
}
|
||||||
int uu_portal_move(struct uu_portal *p, double x, double y, int relative) {
|
int uu_portal_move(struct uu_portal *p, double x, double y, int relative) {
|
||||||
if (!p || !isfinite(x) || !isfinite(y)) return 11;
|
if (!p || !isfinite(x) || !isfinite(y)) return 11;
|
||||||
if (p->logical_width <= 0 || p->logical_height <= 0 || !p->width || !p->height) return 11;
|
if (p->logical_width <= 0 || p->logical_height <= 0 || !p->width || !p->height) return 11;
|
||||||
|
if (!relative && (x < 0 || y < 0 || x >= p->width || y >= p->height)) return 11;
|
||||||
|
int result = prepare_input(p); if (result) return result;
|
||||||
|
if (p->ei) return portal_eis_move(p, x, y, relative);
|
||||||
if (relative) return notify(p, "NotifyPointerMotion", g_variant_new("(o@a{sv}dd)", p->session, empty_options(),
|
if (relative) return notify(p, "NotifyPointerMotion", g_variant_new("(o@a{sv}dd)", p->session, empty_options(),
|
||||||
x * p->logical_width / p->width, y * p->logical_height / p->height));
|
x * p->logical_width / p->width, y * p->logical_height / p->height));
|
||||||
if (x < 0 || y < 0 || x >= p->width || y >= p->height) return 11;
|
|
||||||
return notify(p, "NotifyPointerMotionAbsolute", g_variant_new("(o@a{sv}udd)", p->session, empty_options(), p->node,
|
return notify(p, "NotifyPointerMotionAbsolute", g_variant_new("(o@a{sv}udd)", p->session, empty_options(), p->node,
|
||||||
p->absolute_stream_pixels ? x : x * p->logical_width / p->width,
|
p->absolute_stream_pixels ? x : x * p->logical_width / p->width,
|
||||||
p->absolute_stream_pixels ? y : y * p->logical_height / p->height));
|
p->absolute_stream_pixels ? y : y * p->logical_height / p->height));
|
||||||
}
|
}
|
||||||
int uu_portal_button(struct uu_portal *p, int button, int pressed) {
|
int uu_portal_button(struct uu_portal *p, int button, int pressed) {
|
||||||
if (!p) return 11;
|
if (!p) return 11;
|
||||||
|
int result = prepare_input(p); if (result) return result;
|
||||||
|
if (p->ei) return portal_eis_button(p, button, pressed);
|
||||||
if (button >= 4 && button <= 7) {
|
if (button >= 4 && button <= 7) {
|
||||||
if (!pressed) return 0;
|
if (!pressed) return 0;
|
||||||
return notify(p, "NotifyPointerAxisDiscrete", g_variant_new("(o@a{sv}ui)", p->session, empty_options(),
|
return notify(p, "NotifyPointerAxisDiscrete", g_variant_new("(o@a{sv}ui)", p->session, empty_options(),
|
||||||
@@ -472,7 +669,9 @@ int uu_portal_button(struct uu_portal *p, int button, int pressed) {
|
|||||||
if (!code) return 11;
|
if (!code) return 11;
|
||||||
return notify(p, "NotifyPointerButton", g_variant_new("(o@a{sv}iu)", p->session, empty_options(), code, (guint)!!pressed));
|
return notify(p, "NotifyPointerButton", g_variant_new("(o@a{sv}iu)", p->session, empty_options(), code, (guint)!!pressed));
|
||||||
}
|
}
|
||||||
int uu_portal_key(struct uu_portal *p, uint32_t symbol, int pressed) {
|
int uu_portal_key(struct uu_portal *p, uint32_t symbol, int keycode, int pressed) {
|
||||||
if (!p || !symbol || symbol > INT32_MAX) return 11;
|
if (!p || !symbol || symbol > INT32_MAX) return 11;
|
||||||
|
int result = prepare_input(p); if (result) return result;
|
||||||
|
if (p->ei) return portal_eis_key(p, keycode, pressed);
|
||||||
return notify(p, "NotifyKeyboardKeysym", g_variant_new("(o@a{sv}iu)", p->session, empty_options(), (gint)symbol, (guint)!!pressed));
|
return notify(p, "NotifyKeyboardKeysym", g_variant_new("(o@a{sv}iu)", p->session, empty_options(), (gint)symbol, (guint)!!pressed));
|
||||||
}
|
}
|
||||||
@@ -3,10 +3,13 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
struct uu_portal;
|
struct uu_portal;
|
||||||
|
typedef int (*uu_portal_cancelled_fn)(void *data);
|
||||||
/* One object owns one consent/session and one PipeWire stream. */
|
/* One object owns one consent/session and one PipeWire stream. */
|
||||||
struct uu_portal *uu_portal_open(int *error, uint32_t *width, uint32_t *height);
|
struct uu_portal *uu_portal_open(int *error, uint32_t *width, uint32_t *height);
|
||||||
/* Saves the rotated token immediately after Start, before video initialization. */
|
/* Saves the rotated token immediately after Start, before video initialization. */
|
||||||
struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)(const char *, void *), void *data, int *error, uint32_t *width, uint32_t *height);
|
struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)(const char *, void *), void *data, int *error, uint32_t *width, uint32_t *height);
|
||||||
|
/* The cancellation callback and data are used only until this call returns. */
|
||||||
|
struct uu_portal *uu_portal_open_persist_cancellable(const char *restore, void (*save_token)(const char *, void *), void *save_data, uu_portal_cancelled_fn cancelled, void *cancel_data, int *error, uint32_t *width, uint32_t *height);
|
||||||
struct uu_portal *uu_portal_open_restore(const char *restore, int *error, uint32_t *width, uint32_t *height);
|
struct uu_portal *uu_portal_open_restore(const char *restore, int *error, uint32_t *width, uint32_t *height);
|
||||||
const char *uu_portal_restore_token(struct uu_portal *portal);
|
const char *uu_portal_restore_token(struct uu_portal *portal);
|
||||||
void uu_portal_close(struct uu_portal *portal);
|
void uu_portal_close(struct uu_portal *portal);
|
||||||
@@ -14,9 +17,13 @@ int uu_portal_clipboard_ready(struct uu_portal *portal);
|
|||||||
int uu_portal_clipboard_set(struct uu_portal *portal, const uint8_t *text, size_t length);
|
int uu_portal_clipboard_set(struct uu_portal *portal, const uint8_t *text, size_t length);
|
||||||
int uu_portal_clipboard_poll(struct uu_portal *portal, uint8_t *text, size_t capacity);
|
int uu_portal_clipboard_poll(struct uu_portal *portal, uint8_t *text, size_t capacity);
|
||||||
int uu_portal_alive(struct uu_portal *portal);
|
int uu_portal_alive(struct uu_portal *portal);
|
||||||
|
int uu_portal_capture_alive(struct uu_portal *portal);
|
||||||
|
void uu_portal_input_dispatch(struct uu_portal *portal);
|
||||||
|
void uu_portal_logical_size(struct uu_portal *portal, uint32_t *width, uint32_t *height);
|
||||||
void uu_portal_reset_frame(struct uu_portal *portal);
|
void uu_portal_reset_frame(struct uu_portal *portal);
|
||||||
int uu_portal_capture(struct uu_portal *portal, uint8_t *bgra, size_t length);
|
int uu_portal_capture(struct uu_portal *portal, uint8_t *bgra, size_t length);
|
||||||
|
int uu_portal_capture_since(struct uu_portal *portal, uint8_t *bgra, size_t length, uint64_t *revision);
|
||||||
int uu_portal_move(struct uu_portal *portal, double x, double y, int relative);
|
int uu_portal_move(struct uu_portal *portal, double x, double y, int relative);
|
||||||
int uu_portal_button(struct uu_portal *portal, int button, int pressed);
|
int uu_portal_button(struct uu_portal *portal, int button, int pressed);
|
||||||
int uu_portal_key(struct uu_portal *portal, uint32_t keysym, int pressed);
|
int uu_portal_key(struct uu_portal *portal, uint32_t keysym, int keycode, int pressed);
|
||||||
#endif
|
#endif
|
||||||
@@ -0,0 +1,494 @@
|
|||||||
|
// GTK3 tool windows for the native viewer: file transfer (`files-ui`) and port mapping (`portmap-ui`).
|
||||||
|
// One tool window per process; desktop files reuse the desktop peer. GTK runs on its own thread; Rust pushes state with g_idle_add and polls commands. Paths and
|
||||||
|
// names stay inside this window; nothing here logs.
|
||||||
|
#include <gtk/gtk.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <stdatomic.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define QSIZE 64
|
||||||
|
static char queue[QSIZE][4096];
|
||||||
|
static int q_head, q_len;
|
||||||
|
static pthread_mutex_t q_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
static atomic_int closed;
|
||||||
|
static int init_state;
|
||||||
|
static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
static pthread_cond_t init_cond = PTHREAD_COND_INITIALIZER;
|
||||||
|
static GtkWidget *window, *toast;
|
||||||
|
static char remote_name[256];
|
||||||
|
|
||||||
|
static void push(const char *cmd) {
|
||||||
|
pthread_mutex_lock(&q_lock);
|
||||||
|
if (q_len < QSIZE) { snprintf(queue[(q_head + q_len) % QSIZE], sizeof queue[0], "%s", cmd); q_len++; }
|
||||||
|
pthread_mutex_unlock(&q_lock);
|
||||||
|
}
|
||||||
|
int uu_tools_ui_take(char *out, size_t len) {
|
||||||
|
pthread_mutex_lock(&q_lock);
|
||||||
|
int got = q_len > 0;
|
||||||
|
if (got) { snprintf(out, len, "%s", queue[q_head]); q_head = (q_head + 1) % QSIZE; q_len--; }
|
||||||
|
pthread_mutex_unlock(&q_lock);
|
||||||
|
return got;
|
||||||
|
}
|
||||||
|
int uu_tools_ui_closed(void) { return atomic_load(&closed); }
|
||||||
|
static void add_class(GtkWidget *w, const char *cls) { gtk_style_context_add_class(gtk_widget_get_style_context(w), cls); }
|
||||||
|
static GtkWidget *hbox(int s) { return gtk_box_new(GTK_ORIENTATION_HORIZONTAL, s); }
|
||||||
|
static GtkWidget *vbox(int s) { return gtk_box_new(GTK_ORIENTATION_VERTICAL, s); }
|
||||||
|
static GtkWidget *label(const char *text, const char *cls) { GtkWidget *l = gtk_label_new(text); if (cls) add_class(l, cls); gtk_label_set_xalign(GTK_LABEL(l), 0); return l; }
|
||||||
|
static GtkWidget *button(const char *text, const char *cls) { GtkWidget *b = gtk_button_new_with_label(text); if (cls) add_class(b, cls); return b; }
|
||||||
|
static gboolean hide_toast(gpointer d) { (void)d; gtk_widget_hide(toast); return G_SOURCE_REMOVE; }
|
||||||
|
static void show_toast(const char *text) { gtk_label_set_text(GTK_LABEL(toast), text); gtk_widget_show(toast); g_timeout_add(3500, hide_toast, NULL); }
|
||||||
|
static gboolean toast_idle(gpointer d) { show_toast(d); g_free(d); return G_SOURCE_REMOVE; }
|
||||||
|
void uu_tools_ui_toast(const char *text) { g_idle_add(toast_idle, g_strdup(text)); }
|
||||||
|
static gboolean on_delete(GtkWidget *w, GdkEvent *e, gpointer d) { (void)e; (void)d; gtk_widget_hide(w); atomic_store(&closed, 1); return TRUE; }
|
||||||
|
|
||||||
|
static const char *css =
|
||||||
|
"window { background: #f5f6f8; }\n"
|
||||||
|
".title { font-size: 22px; font-weight: bold; color: #111827; }\n"
|
||||||
|
".tag { background: #e5e7eb; color: #4b5563; border-radius: 6px; padding: 2px 8px; }\n"
|
||||||
|
".tag.remote { background: #d1fae5; color: #047857; }\n"
|
||||||
|
".card { background: white; border: 1px solid #e5e7eb; border-radius: 10px; padding: 8px; }\n"
|
||||||
|
".section { font-size: 17px; font-weight: bold; }\n"
|
||||||
|
".toast { background: rgba(17,24,39,0.88); color: white; border-radius: 8px; padding: 10px 18px; }\n"
|
||||||
|
".primary { background-image: none; background-color: #10b981; color: white; border: none; }\n"
|
||||||
|
".empty { color: #6b7280; font-size: 15px; }\n"
|
||||||
|
".online { color: #10b981; }\n";
|
||||||
|
|
||||||
|
static GtkWidget *frame(const char *title, GtkWidget **content) {
|
||||||
|
GtkCssProvider *p = gtk_css_provider_new(); gtk_css_provider_load_from_data(p, css, -1, NULL);
|
||||||
|
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(p), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||||
|
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
||||||
|
gtk_window_set_title(GTK_WINDOW(window), title);
|
||||||
|
g_signal_connect(window, "delete-event", G_CALLBACK(on_delete), NULL);
|
||||||
|
GtkWidget *overlay = gtk_overlay_new(); *content = vbox(14);
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(*content), 22);
|
||||||
|
gtk_container_add(GTK_CONTAINER(overlay), *content);
|
||||||
|
toast = label("", "toast"); gtk_widget_set_halign(toast, GTK_ALIGN_CENTER); gtk_widget_set_valign(toast, GTK_ALIGN_END);
|
||||||
|
gtk_widget_set_margin_bottom(toast, 30); gtk_widget_set_no_show_all(toast, TRUE);
|
||||||
|
gtk_overlay_add_overlay(GTK_OVERLAY(overlay), toast);
|
||||||
|
gtk_container_add(GTK_CONTAINER(window), overlay);
|
||||||
|
return overlay;
|
||||||
|
}
|
||||||
|
static GtkWidget *add_column(GtkWidget *view, const char *title, int col, int expand) {
|
||||||
|
GtkCellRenderer *r = gtk_cell_renderer_text_new();
|
||||||
|
if (expand) g_object_set(r, "ellipsize", PANGO_ELLIPSIZE_MIDDLE, NULL);
|
||||||
|
GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes(title, r, "text", col, NULL);
|
||||||
|
gtk_tree_view_column_set_resizable(c, TRUE); gtk_tree_view_column_set_expand(c, expand);
|
||||||
|
if (expand) gtk_tree_view_column_set_min_width(c, 160);
|
||||||
|
gtk_tree_view_append_column(GTK_TREE_VIEW(view), c);
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
static GtkWidget *scrolled(GtkWidget *child, int height) {
|
||||||
|
GtkWidget *s = gtk_scrolled_window_new(NULL, NULL);
|
||||||
|
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(s), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
|
||||||
|
gtk_widget_set_size_request(s, -1, height); gtk_container_add(GTK_CONTAINER(s), child);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
static void format_size(guint64 size, int kind, char *out, size_t len) {
|
||||||
|
if (kind != 4) { snprintf(out, len, "--"); return; }
|
||||||
|
if (size < 1024) snprintf(out, len, "%llu B", (unsigned long long)size);
|
||||||
|
else if (size < 1024 * 1024) snprintf(out, len, "%.2f KB", size / 1024.0);
|
||||||
|
else if (size < 1024ull * 1024 * 1024) snprintf(out, len, "%.2f MB", size / 1048576.0);
|
||||||
|
else snprintf(out, len, "%.2f GB", size / 1073741824.0);
|
||||||
|
}
|
||||||
|
static void format_time(guint64 secs, char *out, size_t len) {
|
||||||
|
if (!secs) { snprintf(out, len, "--"); return; }
|
||||||
|
time_t t = (time_t)secs; struct tm tm; localtime_r(&t, &tm);
|
||||||
|
strftime(out, len, "%Y年%m月%d日", &tm);
|
||||||
|
}
|
||||||
|
static const char *kind_name(int kind) { return kind == 4 ? "文件" : kind == 3 ? "磁盘" : "文件夹"; }
|
||||||
|
|
||||||
|
/* ---------------- file transfer ---------------- */
|
||||||
|
enum { C_NAME, C_TIME, C_TYPE, C_SIZE, C_PATH, C_KIND, C_N };
|
||||||
|
enum { T_ID, T_NAME, T_STATUS, T_SIZE, T_FROM, T_TO, T_DONE, T_N };
|
||||||
|
static GtkListStore *local_store, *remote_store, *task_store;
|
||||||
|
static GtkWidget *local_view, *remote_view, *local_path, *remote_path;
|
||||||
|
static char local_dir[2048] = "", remote_dir[2048] = ":/";
|
||||||
|
|
||||||
|
static void load_local(const char *path) {
|
||||||
|
char *dir = g_strdup(path); /* path may alias local_dir */
|
||||||
|
GDir *d = g_dir_open(dir, 0, NULL);
|
||||||
|
if (!d) { show_toast("无法打开本机目录"); g_free(dir); return; }
|
||||||
|
snprintf(local_dir, sizeof local_dir, "%s", dir); g_free(dir);
|
||||||
|
gtk_entry_set_text(GTK_ENTRY(local_path), local_dir);
|
||||||
|
gtk_list_store_clear(local_store);
|
||||||
|
const char *name;
|
||||||
|
GPtrArray *names = g_ptr_array_new_with_free_func(g_free);
|
||||||
|
while ((name = g_dir_read_name(d))) if (name[0] != '.') g_ptr_array_add(names, g_strdup(name));
|
||||||
|
g_dir_close(d);
|
||||||
|
for (int pass = 0; pass < 2; pass++) for (guint i = 0; i < names->len; i++) {
|
||||||
|
char *full = g_build_filename(local_dir, names->pdata[i], NULL); struct stat st;
|
||||||
|
if (stat(full, &st) == 0 && (S_ISDIR(st.st_mode) ? pass == 0 : pass == 1)) {
|
||||||
|
int kind = S_ISDIR(st.st_mode) ? 0 : 4; char size[32], when[64];
|
||||||
|
format_size(st.st_size, kind, size, sizeof size); format_time(st.st_mtime, when, sizeof when);
|
||||||
|
GtkTreeIter it; gtk_list_store_append(local_store, &it);
|
||||||
|
gtk_list_store_set(local_store, &it, C_NAME, names->pdata[i], C_TIME, when, C_TYPE, kind_name(kind), C_SIZE, size, C_PATH, full, C_KIND, kind, -1);
|
||||||
|
}
|
||||||
|
g_free(full);
|
||||||
|
}
|
||||||
|
g_ptr_array_free(names, TRUE);
|
||||||
|
}
|
||||||
|
static void request_remote(const char *path) { char cmd[2100]; snprintf(cmd, sizeof cmd, "ls\t%s", path); push(cmd); }
|
||||||
|
static void on_local_up(GtkButton *b, gpointer d) { (void)b; (void)d; char *p = g_path_get_dirname(local_dir); load_local(p); g_free(p); }
|
||||||
|
static void on_local_refresh(GtkButton *b, gpointer d) { (void)b; (void)d; load_local(local_dir); }
|
||||||
|
static void on_local_path(GtkEntry *e, gpointer d) { (void)d; load_local(gtk_entry_get_text(e)); }
|
||||||
|
static void on_remote_up(GtkButton *b, gpointer d) {
|
||||||
|
(void)b; (void)d;
|
||||||
|
if (!strcmp(remote_dir, ":/") || !strcmp(remote_dir, "/")) { request_remote(":/"); return; }
|
||||||
|
char *p = g_path_get_dirname(remote_dir); request_remote(p); g_free(p);
|
||||||
|
}
|
||||||
|
static void on_remote_refresh(GtkButton *b, gpointer d) { (void)b; (void)d; request_remote(remote_dir); }
|
||||||
|
static void on_remote_path(GtkEntry *e, gpointer d) { (void)d; request_remote(gtk_entry_get_text(e)); }
|
||||||
|
static void on_row_activated(GtkTreeView *view, GtkTreePath *path, GtkTreeViewColumn *col, gpointer remote) {
|
||||||
|
(void)col; GtkTreeModel *m = gtk_tree_view_get_model(view); GtkTreeIter it; char *full; int kind;
|
||||||
|
if (!gtk_tree_model_get_iter(m, &it, path)) return;
|
||||||
|
gtk_tree_model_get(m, &it, C_PATH, &full, C_KIND, &kind, -1);
|
||||||
|
if (kind != 4) { if (remote) request_remote(full); else load_local(full); }
|
||||||
|
g_free(full);
|
||||||
|
}
|
||||||
|
static char *ask_name(const char *title) {
|
||||||
|
GtkWidget *dlg = gtk_dialog_new_with_buttons(title, GTK_WINDOW(window), GTK_DIALOG_MODAL, "取消", GTK_RESPONSE_CANCEL, "确定", GTK_RESPONSE_OK, NULL);
|
||||||
|
GtkWidget *entry = gtk_entry_new(); gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
|
||||||
|
gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_OK);
|
||||||
|
GtkWidget *area = gtk_dialog_get_content_area(GTK_DIALOG(dlg)); gtk_container_set_border_width(GTK_CONTAINER(area), 16);
|
||||||
|
gtk_box_pack_start(GTK_BOX(area), entry, FALSE, FALSE, 0); gtk_widget_show_all(dlg);
|
||||||
|
char *name = NULL;
|
||||||
|
if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) {
|
||||||
|
const char *t = gtk_entry_get_text(GTK_ENTRY(entry));
|
||||||
|
if (*t && !strchr(t, '/') && strcmp(t, ".") && strcmp(t, "..")) name = g_strdup(t); else if (*t) show_toast("名称无效");
|
||||||
|
}
|
||||||
|
gtk_widget_destroy(dlg);
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
static void on_local_mkdir(GtkButton *b, gpointer d) {
|
||||||
|
(void)b; (void)d; char *name = ask_name("新建文件夹"); if (!name) return;
|
||||||
|
char *full = g_build_filename(local_dir, name, NULL);
|
||||||
|
if (mkdir(full, 0755) != 0) show_toast("无法创建文件夹"); else load_local(local_dir);
|
||||||
|
g_free(full); g_free(name);
|
||||||
|
}
|
||||||
|
static void on_remote_mkdir(GtkButton *b, gpointer d) {
|
||||||
|
(void)b; (void)d;
|
||||||
|
if (!strcmp(remote_dir, ":/")) { show_toast("请先进入一个文件夹"); return; }
|
||||||
|
char *name = ask_name("新建文件夹"); if (!name) return;
|
||||||
|
char *full = g_build_filename(remote_dir, name, NULL); char cmd[2200];
|
||||||
|
snprintf(cmd, sizeof cmd, "mkdir\t%s", full); push(cmd); g_free(full); g_free(name);
|
||||||
|
}
|
||||||
|
/* Each selected row becomes one queued transfer. Remote folders download recursively; local folders
|
||||||
|
* are not supported by the upload RPC sequence implemented here. */
|
||||||
|
static void on_send(GtkButton *b, gpointer upload) {
|
||||||
|
(void)b; GtkTreeView *view = GTK_TREE_VIEW(upload ? local_view : remote_view);
|
||||||
|
GtkTreeModel *m; GList *rows = gtk_tree_selection_get_selected_rows(gtk_tree_view_get_selection(view), &m);
|
||||||
|
if (!rows) { show_toast("请先选择要发送的文件"); return; }
|
||||||
|
if (upload && !strcmp(remote_dir, ":/")) { show_toast("请先在远端进入目标文件夹"); g_list_free_full(rows, (GDestroyNotify)gtk_tree_path_free); return; }
|
||||||
|
int skipped = 0;
|
||||||
|
for (GList *r = rows; r; r = r->next) {
|
||||||
|
GtkTreeIter it; char *full, *name; int kind;
|
||||||
|
if (!gtk_tree_model_get_iter(m, &it, r->data)) continue;
|
||||||
|
gtk_tree_model_get(m, &it, C_PATH, &full, C_NAME, &name, C_KIND, &kind, -1);
|
||||||
|
char cmd[4096];
|
||||||
|
if (upload && kind != 4) skipped++;
|
||||||
|
else if (!upload && kind == 3) skipped++;
|
||||||
|
else {
|
||||||
|
if (upload) snprintf(cmd, sizeof cmd, "put\t%s\t%s", full, remote_dir);
|
||||||
|
else snprintf(cmd, sizeof cmd, "get\t%s\t%s", full, local_dir);
|
||||||
|
push(cmd);
|
||||||
|
}
|
||||||
|
g_free(full); g_free(name);
|
||||||
|
}
|
||||||
|
g_list_free_full(rows, (GDestroyNotify)gtk_tree_path_free);
|
||||||
|
if (skipped) show_toast(upload ? "暂不支持上传文件夹,已跳过" : "不能下载整个磁盘,已跳过");
|
||||||
|
}
|
||||||
|
static void on_cancel_all(GtkButton *b, gpointer d) { (void)b; (void)d; push("cancel"); }
|
||||||
|
static void on_clear_done(GtkButton *b, gpointer d) {
|
||||||
|
(void)b; (void)d; GtkTreeIter it; gboolean ok = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(task_store), &it);
|
||||||
|
while (ok) { int done; gtk_tree_model_get(GTK_TREE_MODEL(task_store), &it, T_DONE, &done, -1);
|
||||||
|
ok = done ? gtk_list_store_remove(task_store, &it) : gtk_tree_model_iter_next(GTK_TREE_MODEL(task_store), &it); }
|
||||||
|
}
|
||||||
|
static GtkWidget *pane(int remote) {
|
||||||
|
GtkWidget *card = vbox(6); add_class(card, "card");
|
||||||
|
GtkWidget *bar = hbox(6), *up = button("↑", NULL), *refresh = button("刷新", NULL), *mk = button("新建文件夹", NULL), *path = gtk_entry_new();
|
||||||
|
gtk_widget_set_tooltip_text(up, "上一级"); gtk_widget_set_tooltip_text(refresh, "刷新");
|
||||||
|
gtk_box_pack_start(GTK_BOX(bar), up, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(bar), refresh, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(bar), mk, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(bar), path, TRUE, TRUE, 0);
|
||||||
|
GtkListStore *store = gtk_list_store_new(C_N, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
|
||||||
|
GtkWidget *view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
|
||||||
|
add_column(view, "名称", C_NAME, 1); add_column(view, "修改日期", C_TIME, 0); add_column(view, "类型", C_TYPE, 0); add_column(view, "大小", C_SIZE, 0);
|
||||||
|
gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)), GTK_SELECTION_MULTIPLE);
|
||||||
|
g_signal_connect(view, "row-activated", G_CALLBACK(on_row_activated), GINT_TO_POINTER(remote));
|
||||||
|
g_signal_connect(up, "clicked", G_CALLBACK(remote ? on_remote_up : on_local_up), NULL);
|
||||||
|
g_signal_connect(refresh, "clicked", G_CALLBACK(remote ? on_remote_refresh : on_local_refresh), NULL);
|
||||||
|
g_signal_connect(mk, "clicked", G_CALLBACK(remote ? on_remote_mkdir : on_local_mkdir), NULL);
|
||||||
|
g_signal_connect(path, "activate", G_CALLBACK(remote ? on_remote_path : on_local_path), NULL);
|
||||||
|
gtk_box_pack_start(GTK_BOX(card), bar, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(card), scrolled(view, 300), TRUE, TRUE, 0);
|
||||||
|
if (remote) { remote_store = store; remote_view = view; remote_path = path; }
|
||||||
|
else { local_store = store; local_view = view; local_path = path; }
|
||||||
|
return card;
|
||||||
|
}
|
||||||
|
static void build_files(void) {
|
||||||
|
GtkWidget *content; frame("文件传输", &content);
|
||||||
|
gtk_window_set_default_size(GTK_WINDOW(window), 1100, 760);
|
||||||
|
GtkWidget *head = gtk_grid_new(); gtk_grid_set_column_homogeneous(GTK_GRID(head), TRUE); gtk_grid_set_column_spacing(GTK_GRID(head), 24);
|
||||||
|
GtkWidget *left = hbox(10), *right = hbox(10);
|
||||||
|
char host[256]; if (gethostname(host, sizeof host) != 0) snprintf(host, sizeof host, "本机"); host[sizeof host - 1] = 0;
|
||||||
|
GtkWidget *tag_l = label("本机", "tag"), *tag_r = label("远端", "tag"); add_class(tag_r, "remote");
|
||||||
|
GtkWidget *send_r = button("发送 →", NULL), *send_l = button("← 发送", NULL);
|
||||||
|
g_signal_connect(send_r, "clicked", G_CALLBACK(on_send), GINT_TO_POINTER(1));
|
||||||
|
g_signal_connect(send_l, "clicked", G_CALLBACK(on_send), NULL);
|
||||||
|
gtk_box_pack_start(GTK_BOX(left), label(host, "title"), FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(left), tag_l, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_end(GTK_BOX(left), send_r, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(right), send_l, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_end(GTK_BOX(right), label(remote_name, "title"), FALSE, FALSE, 0); gtk_box_pack_end(GTK_BOX(right), tag_r, FALSE, FALSE, 0);
|
||||||
|
gtk_grid_attach(GTK_GRID(head), left, 0, 0, 1, 1); gtk_grid_attach(GTK_GRID(head), right, 1, 0, 1, 1);
|
||||||
|
gtk_grid_attach(GTK_GRID(head), pane(0), 0, 1, 1, 1); gtk_grid_attach(GTK_GRID(head), pane(1), 1, 1, 1, 1);
|
||||||
|
gtk_grid_set_row_spacing(GTK_GRID(head), 14);
|
||||||
|
gtk_box_pack_start(GTK_BOX(content), head, TRUE, TRUE, 0);
|
||||||
|
GtkWidget *tasks = vbox(8); add_class(tasks, "card");
|
||||||
|
GtkWidget *tbar = hbox(8), *cancel = button("全部取消", NULL), *clear = button("清除完结任务", NULL);
|
||||||
|
g_signal_connect(cancel, "clicked", G_CALLBACK(on_cancel_all), NULL); g_signal_connect(clear, "clicked", G_CALLBACK(on_clear_done), NULL);
|
||||||
|
gtk_box_pack_start(GTK_BOX(tbar), label("传输列表", "section"), FALSE, FALSE, 4);
|
||||||
|
gtk_box_pack_end(GTK_BOX(tbar), clear, FALSE, FALSE, 0); gtk_box_pack_end(GTK_BOX(tbar), cancel, FALSE, FALSE, 0);
|
||||||
|
task_store = gtk_list_store_new(T_N, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
|
||||||
|
GtkWidget *tview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(task_store));
|
||||||
|
add_column(tview, "名称", T_NAME, 1); add_column(tview, "状态", T_STATUS, 0); add_column(tview, "大小", T_SIZE, 0);
|
||||||
|
add_column(tview, "发送路径", T_FROM, 1); add_column(tview, "接收路径", T_TO, 1);
|
||||||
|
gtk_box_pack_start(GTK_BOX(tasks), tbar, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(tasks), scrolled(tview, 180), TRUE, TRUE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(content), tasks, TRUE, TRUE, 0);
|
||||||
|
const char *home = g_get_home_dir(); load_local(home ? home : "/");
|
||||||
|
gtk_entry_set_text(GTK_ENTRY(remote_path), "此电脑");
|
||||||
|
}
|
||||||
|
|
||||||
|
struct listing { char *path; char *rows; };
|
||||||
|
static gboolean remote_idle(gpointer d) {
|
||||||
|
struct listing *l = d;
|
||||||
|
snprintf(remote_dir, sizeof remote_dir, "%s", l->path);
|
||||||
|
gtk_entry_set_text(GTK_ENTRY(remote_path), strcmp(remote_dir, ":/") ? remote_dir : "此电脑");
|
||||||
|
gtk_list_store_clear(remote_store);
|
||||||
|
char **lines = g_strsplit(l->rows, "\n", -1);
|
||||||
|
for (char **line = lines; *line; line++) {
|
||||||
|
char **f = g_strsplit(*line, "\t", 5);
|
||||||
|
if (g_strv_length(f) == 5) {
|
||||||
|
int kind = atoi(f[0]); char size[32], when[64];
|
||||||
|
format_size(g_ascii_strtoull(f[2], NULL, 10), kind, size, sizeof size); format_time(g_ascii_strtoull(f[3], NULL, 10), when, sizeof when);
|
||||||
|
GtkTreeIter it; gtk_list_store_append(remote_store, &it);
|
||||||
|
gtk_list_store_set(remote_store, &it, C_NAME, f[1], C_TIME, when, C_TYPE, kind_name(kind), C_SIZE, size, C_PATH, f[4], C_KIND, kind, -1);
|
||||||
|
}
|
||||||
|
g_strfreev(f);
|
||||||
|
}
|
||||||
|
g_strfreev(lines); g_free(l->path); g_free(l->rows); g_free(l);
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
/* rows: "kind\tname\tsize\tmodified\tpath\n" */
|
||||||
|
void uu_files_ui_set_remote(const char *path, const char *rows) {
|
||||||
|
struct listing *l = g_new0(struct listing, 1); l->path = g_strdup(path); l->rows = g_strdup(rows); g_idle_add(remote_idle, l);
|
||||||
|
}
|
||||||
|
struct task { int id, done; char *name, *status, *size, *from, *to; };
|
||||||
|
static gboolean task_idle(gpointer d) {
|
||||||
|
struct task *t = d; GtkTreeIter it; gboolean ok = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(task_store), &it), found = FALSE;
|
||||||
|
while (ok) { int id; gtk_tree_model_get(GTK_TREE_MODEL(task_store), &it, T_ID, &id, -1); if (id == t->id) { found = TRUE; break; } ok = gtk_tree_model_iter_next(GTK_TREE_MODEL(task_store), &it); }
|
||||||
|
if (!found) { gtk_list_store_append(task_store, &it); gtk_list_store_set(task_store, &it, T_ID, t->id, T_NAME, t->name, -1); }
|
||||||
|
gtk_list_store_set(task_store, &it, T_STATUS, t->status, T_SIZE, t->size, T_FROM, t->from, T_TO, t->to, T_DONE, t->done, -1);
|
||||||
|
g_free(t->name); g_free(t->status); g_free(t->size); g_free(t->from); g_free(t->to); g_free(t);
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
/* total == 0 means unknown size; done: 0 running/queued, 1 finished (success), 2 finished (failed/canceled). */
|
||||||
|
void uu_files_ui_task(int id, const char *name, const char *status, unsigned long long transferred, unsigned long long total, const char *from, const char *to, int done) {
|
||||||
|
struct task *t = g_new0(struct task, 1); char a[32], b[32];
|
||||||
|
t->id = id; t->done = done;
|
||||||
|
format_size(transferred, 4, a, sizeof a); format_size(total, 4, b, sizeof b);
|
||||||
|
t->name = g_strdup(name); t->status = g_strdup(status); t->from = g_strdup(from); t->to = g_strdup(to);
|
||||||
|
t->size = total ? g_strdup_printf("%s / %s", a, b) : g_strdup(transferred ? a : "--");
|
||||||
|
g_idle_add(task_idle, t);
|
||||||
|
}
|
||||||
|
static gboolean refresh_local_idle(gpointer d) { (void)d; load_local(local_dir); return G_SOURCE_REMOVE; }
|
||||||
|
void uu_files_ui_refresh_local(void) { g_idle_add(refresh_local_idle, NULL); }
|
||||||
|
|
||||||
|
/* ---------------- port mapping ---------------- */
|
||||||
|
enum { P_ID, P_NAME, P_LOCAL, P_TARGET, P_STATUS, P_N };
|
||||||
|
static GtkListStore *rule_store;
|
||||||
|
static GtkWidget *rule_stack;
|
||||||
|
static void on_add_rule(GtkButton *b, gpointer d) {
|
||||||
|
(void)b; (void)d;
|
||||||
|
GtkWidget *dlg = gtk_dialog_new_with_buttons("添加映射规则", GTK_WINDOW(window), GTK_DIALOG_MODAL, "取消", GTK_RESPONSE_CANCEL, "保存后立即启用", GTK_RESPONSE_OK, NULL);
|
||||||
|
GtkWidget *grid = gtk_grid_new(); gtk_grid_set_row_spacing(GTK_GRID(grid), 12); gtk_grid_set_column_spacing(GTK_GRID(grid), 24);
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(grid), 20);
|
||||||
|
const char *labels[] = {"名称", "本地端口", "目标地址", "目标端口"}, *hints[] = {"名称", "请输入端口", "", "请输入端口"};
|
||||||
|
GtkWidget *entries[4];
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
entries[i] = gtk_entry_new(); gtk_entry_set_placeholder_text(GTK_ENTRY(entries[i]), hints[i]); gtk_widget_set_hexpand(entries[i], TRUE);
|
||||||
|
gtk_grid_attach(GTK_GRID(grid), label(labels[i], NULL), 0, i, 1, 1); gtk_grid_attach(GTK_GRID(grid), entries[i], 1, i, 1, 1);
|
||||||
|
}
|
||||||
|
gtk_entry_set_text(GTK_ENTRY(entries[2]), "127.0.0.1");
|
||||||
|
gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dlg))), grid, TRUE, TRUE, 0);
|
||||||
|
gtk_window_set_default_size(GTK_WINDOW(dlg), 480, -1); gtk_widget_show_all(dlg);
|
||||||
|
while (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) {
|
||||||
|
const char *name = gtk_entry_get_text(GTK_ENTRY(entries[0])), *host = gtk_entry_get_text(GTK_ENTRY(entries[2]));
|
||||||
|
char *end; long local = strtol(gtk_entry_get_text(GTK_ENTRY(entries[1])), &end, 10); int bad_local = *end || local < 0 || local > 65535;
|
||||||
|
long port = strtol(gtk_entry_get_text(GTK_ENTRY(entries[3])), &end, 10); int bad_port = *end || port < 1 || port > 65535;
|
||||||
|
if (!*name || strchr(name, '\t') || !*host || strchr(host, '\t') || bad_local || bad_port) { show_toast("请填写名称、有效端口和目标地址"); continue; }
|
||||||
|
char cmd[1024]; snprintf(cmd, sizeof cmd, "add\t%s\t%ld\t%s\t%ld", name, local, host, port); push(cmd);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
gtk_widget_destroy(dlg);
|
||||||
|
}
|
||||||
|
static void on_remove_rule(GtkButton *b, gpointer view) {
|
||||||
|
(void)b; GtkTreeModel *m; GtkTreeIter it;
|
||||||
|
if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(view), &m, &it)) { show_toast("请先选择一条规则"); return; }
|
||||||
|
int id; gtk_tree_model_get(m, &it, P_ID, &id, -1); char cmd[64]; snprintf(cmd, sizeof cmd, "remove\t%d", id); push(cmd);
|
||||||
|
}
|
||||||
|
static void build_portmap(void) {
|
||||||
|
GtkWidget *content; frame("端口映射", &content);
|
||||||
|
gtk_window_set_default_size(GTK_WINDOW(window), 820, 560);
|
||||||
|
GtkWidget *head = hbox(12), *add = button("添加规则", "primary"), *del = button("删除", NULL);
|
||||||
|
GtkWidget *dot = label("● 已连接", "online");
|
||||||
|
gtk_box_pack_start(GTK_BOX(head), label(remote_name, "title"), FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(head), dot, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_end(GTK_BOX(head), add, FALSE, FALSE, 0); gtk_box_pack_end(GTK_BOX(head), del, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(content), head, FALSE, FALSE, 0);
|
||||||
|
rule_store = gtk_list_store_new(P_N, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
|
||||||
|
GtkWidget *view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(rule_store));
|
||||||
|
add_column(view, "名称", P_NAME, 1); add_column(view, "本地地址", P_LOCAL, 0); add_column(view, "目标地址", P_TARGET, 1); add_column(view, "状态", P_STATUS, 0);
|
||||||
|
rule_stack = gtk_stack_new();
|
||||||
|
GtkWidget *empty = label("当前没有端口映射规则,点击「添加规则」把远端端口映射到本机", "empty"); gtk_widget_set_halign(empty, GTK_ALIGN_CENTER);
|
||||||
|
gtk_stack_add_named(GTK_STACK(rule_stack), empty, "empty");
|
||||||
|
gtk_stack_add_named(GTK_STACK(rule_stack), scrolled(view, 360), "list");
|
||||||
|
GtkWidget *card = vbox(0); add_class(card, "card"); gtk_box_pack_start(GTK_BOX(card), rule_stack, TRUE, TRUE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(content), card, TRUE, TRUE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(content), label("本地端口只监听 127.0.0.1;本地端口填 0 时自动分配。", "empty"), FALSE, FALSE, 0);
|
||||||
|
g_signal_connect(add, "clicked", G_CALLBACK(on_add_rule), NULL);
|
||||||
|
g_signal_connect(del, "clicked", G_CALLBACK(on_remove_rule), view);
|
||||||
|
}
|
||||||
|
static gboolean rules_idle(gpointer d) {
|
||||||
|
char *rows = d; gtk_list_store_clear(rule_store); int n = 0;
|
||||||
|
char **lines = g_strsplit(rows, "\n", -1);
|
||||||
|
for (char **line = lines; *line; line++) {
|
||||||
|
char **f = g_strsplit(*line, "\t", 5);
|
||||||
|
if (g_strv_length(f) == 5) { GtkTreeIter it; gtk_list_store_append(rule_store, &it);
|
||||||
|
gtk_list_store_set(rule_store, &it, P_ID, atoi(f[0]), P_NAME, f[1], P_LOCAL, f[2], P_TARGET, f[3], P_STATUS, f[4], -1); n++; }
|
||||||
|
g_strfreev(f);
|
||||||
|
}
|
||||||
|
g_strfreev(lines); g_free(rows);
|
||||||
|
gtk_stack_set_visible_child_name(GTK_STACK(rule_stack), n ? "list" : "empty");
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
/* rows: "id\tname\tlocal\ttarget\tstatus\n" */
|
||||||
|
void uu_portmap_ui_set_rules(const char *rows) { g_idle_add(rules_idle, g_strdup(rows)); }
|
||||||
|
|
||||||
|
/* ---------------- terminal sessions ---------------- */
|
||||||
|
enum { S_ID, S_SHELL, S_TIME, S_STATE, S_N };
|
||||||
|
static GtkListStore *session_store;
|
||||||
|
static GtkWidget *session_view, *session_stack, *session_dot, *session_create, *session_kill;
|
||||||
|
static guint selected_session(void) {
|
||||||
|
GtkTreeModel *m; GtkTreeIter it; guint id = 0;
|
||||||
|
if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(session_view)), &m, &it)) gtk_tree_model_get(m, &it, S_ID, &id, -1);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
static void on_create_session(GtkButton *b, gpointer d) { (void)b; (void)d; push("create"); }
|
||||||
|
static void on_open_session(GtkTreeView *v, GtkTreePath *path, GtkTreeViewColumn *col, gpointer d) {
|
||||||
|
(void)col; (void)d; GtkTreeIter it; guint id;
|
||||||
|
if (!gtk_widget_get_sensitive(GTK_WIDGET(v)) || !gtk_tree_model_get_iter(GTK_TREE_MODEL(session_store), &it, path)) return;
|
||||||
|
gtk_tree_model_get(GTK_TREE_MODEL(session_store), &it, S_ID, &id, -1);
|
||||||
|
char cmd[64]; snprintf(cmd, sizeof cmd, "open\t%u", id); push(cmd);
|
||||||
|
}
|
||||||
|
static void on_kill_session(GtkButton *b, gpointer d) {
|
||||||
|
(void)b; (void)d; guint id = selected_session();
|
||||||
|
if (!id) { show_toast("请先选择一个会话"); return; }
|
||||||
|
GtkWidget *dlg = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||||
|
GTK_MESSAGE_WARNING, GTK_BUTTONS_NONE, "确定结束会话 %u?", id);
|
||||||
|
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dlg), "会话内正在运行的程序将被终止。");
|
||||||
|
gtk_dialog_add_buttons(GTK_DIALOG(dlg), "取消", GTK_RESPONSE_CANCEL, "结束会话", GTK_RESPONSE_OK, NULL);
|
||||||
|
gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_CANCEL);
|
||||||
|
if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) { char cmd[64]; snprintf(cmd, sizeof cmd, "kill\t%u", id); push(cmd); }
|
||||||
|
gtk_widget_destroy(dlg);
|
||||||
|
}
|
||||||
|
static void build_terminal(void) {
|
||||||
|
GtkWidget *content; frame("终端远控", &content); gtk_window_set_default_size(GTK_WINDOW(window), 820, 560);
|
||||||
|
GtkWidget *head = hbox(12); session_dot = label("● 正在读取会话", "online");
|
||||||
|
session_create = button("创建会话", "primary"); session_kill = button("结束会话", NULL);
|
||||||
|
gtk_box_pack_start(GTK_BOX(head), label(remote_name, "title"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(head), session_dot, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_end(GTK_BOX(head), session_create, FALSE, FALSE, 0); gtk_box_pack_end(GTK_BOX(head), session_kill, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(content), head, FALSE, FALSE, 0);
|
||||||
|
session_store = gtk_list_store_new(S_N, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
|
||||||
|
session_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(session_store));
|
||||||
|
add_column(session_view, "会话 ID", S_ID, 0); add_column(session_view, "Shell", S_SHELL, 1);
|
||||||
|
add_column(session_view, "创建时间", S_TIME, 0); add_column(session_view, "状态", S_STATE, 0);
|
||||||
|
session_stack = gtk_stack_new();
|
||||||
|
GtkWidget *empty = label("当前无正在运行的终端会话", "empty"); gtk_widget_set_halign(empty, GTK_ALIGN_CENTER);
|
||||||
|
gtk_stack_add_named(GTK_STACK(session_stack), empty, "empty");
|
||||||
|
gtk_stack_add_named(GTK_STACK(session_stack), label("正在读取会话…", "empty"), "loading");
|
||||||
|
gtk_stack_add_named(GTK_STACK(session_stack), label("连接已断开,请关闭窗口后重试", "empty"), "offline");
|
||||||
|
gtk_stack_add_named(GTK_STACK(session_stack), scrolled(session_view, 360), "list");
|
||||||
|
gtk_stack_set_visible_child_name(GTK_STACK(session_stack), "loading");
|
||||||
|
gtk_box_pack_start(GTK_BOX(content), session_stack, TRUE, TRUE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(content), label("双击接回会话;创建会话将在本机终端程序中打开。", "empty"), FALSE, FALSE, 0);
|
||||||
|
gtk_widget_set_sensitive(session_create, FALSE); gtk_widget_set_sensitive(session_kill, FALSE);
|
||||||
|
g_signal_connect(session_create, "clicked", G_CALLBACK(on_create_session), NULL);
|
||||||
|
g_signal_connect(session_kill, "clicked", G_CALLBACK(on_kill_session), NULL);
|
||||||
|
g_signal_connect(session_view, "row-activated", G_CALLBACK(on_open_session), NULL);
|
||||||
|
}
|
||||||
|
struct sessions { char *rows; int connected; };
|
||||||
|
static gboolean sessions_idle(gpointer d) {
|
||||||
|
struct sessions *s = d;
|
||||||
|
if (atomic_load(&closed)) { g_free(s->rows); g_free(s); return G_SOURCE_REMOVE; }
|
||||||
|
guint selected = selected_session(); gtk_list_store_clear(session_store); int n = 0;
|
||||||
|
char **lines = g_strsplit(s->rows, "\n", -1);
|
||||||
|
for (char **line = lines; *line; line++) {
|
||||||
|
char **f = g_strsplit(*line, "\t", 4);
|
||||||
|
if (g_strv_length(f) == 4) {
|
||||||
|
guint id = (guint)g_ascii_strtoull(f[0], NULL, 10); GtkTreeIter it; char date[64] = "--";
|
||||||
|
gint64 ms = g_ascii_strtoll(f[2], NULL, 10);
|
||||||
|
GDateTime *time = ms > 0 ? g_date_time_new_from_unix_local(ms / 1000) : NULL;
|
||||||
|
if (time) { char *text = g_date_time_format(time, "%Y-%m-%d %H:%M:%S"); g_strlcpy(date, text, sizeof date); g_free(text); g_date_time_unref(time); }
|
||||||
|
gtk_list_store_append(session_store, &it);
|
||||||
|
gtk_list_store_set(session_store, &it, S_ID, id, S_SHELL, f[1], S_TIME, date, S_STATE, f[3], -1); n++;
|
||||||
|
if (id == selected) gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(session_view)), &it);
|
||||||
|
}
|
||||||
|
g_strfreev(f);
|
||||||
|
}
|
||||||
|
g_strfreev(lines);
|
||||||
|
gtk_label_set_text(GTK_LABEL(session_dot), s->connected ? "● 已连接" : "● 已断开");
|
||||||
|
gtk_widget_set_sensitive(session_create, s->connected); gtk_widget_set_sensitive(session_kill, s->connected);
|
||||||
|
gtk_widget_set_sensitive(session_view, s->connected);
|
||||||
|
gtk_stack_set_visible_child_name(GTK_STACK(session_stack), s->connected ? (n ? "list" : "empty") : "offline");
|
||||||
|
g_free(s->rows); g_free(s); return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
/* rows: "id\tshell\tcreated_at_ms\tstate\n"; copied before returning to Rust. */
|
||||||
|
void uu_terminal_ui_set(const char *rows, int connected) {
|
||||||
|
struct sessions *s = g_new0(struct sessions, 1); s->rows = g_strdup(rows); s->connected = connected; g_idle_add(sessions_idle, s);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------- startup ---------------- */
|
||||||
|
static char tool_kind[16];
|
||||||
|
static gboolean show_window(gpointer d) {
|
||||||
|
(void)d;
|
||||||
|
if (!window) {
|
||||||
|
if (!strcmp(tool_kind, "files")) build_files(); else if (!strcmp(tool_kind, "terminal")) build_terminal(); else build_portmap();
|
||||||
|
}
|
||||||
|
atomic_store(&closed, 0); gtk_widget_show_all(window); gtk_window_present(GTK_WINDOW(window));
|
||||||
|
pthread_mutex_lock(&init_lock); init_state = 1; pthread_cond_signal(&init_cond); pthread_mutex_unlock(&init_lock);
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
static void *ui_thread(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; }
|
||||||
|
show_window(NULL); gtk_main(); return NULL;
|
||||||
|
}
|
||||||
|
/* Hidden windows retain their widgets so queued idle callbacks stay valid. */
|
||||||
|
int uu_tools_ui_start(const char *kind, const char *name) {
|
||||||
|
static int started;
|
||||||
|
pthread_mutex_lock(&init_lock); if (init_state < 0) { pthread_mutex_unlock(&init_lock); return -1; } init_state = 0;
|
||||||
|
snprintf(tool_kind, sizeof tool_kind, "%s", kind); snprintf(remote_name, sizeof remote_name, "%s", name);
|
||||||
|
pthread_mutex_lock(&q_lock); q_head = q_len = 0; pthread_mutex_unlock(&q_lock);
|
||||||
|
if (!started) {
|
||||||
|
pthread_t t; if (pthread_create(&t, NULL, ui_thread, NULL) != 0) { pthread_mutex_unlock(&init_lock); return -1; }
|
||||||
|
pthread_detach(t); started = 1;
|
||||||
|
} else g_idle_add(show_window, NULL);
|
||||||
|
while (!init_state) pthread_cond_wait(&init_cond, &init_lock);
|
||||||
|
int s = init_state; pthread_mutex_unlock(&init_lock); return s == 1 ? 0 : -1;
|
||||||
|
}
|
||||||
|
static gboolean hide_window(gpointer d) { (void)d; if (window) gtk_widget_hide(window); atomic_store(&closed, 1); return G_SOURCE_REMOVE; }
|
||||||
|
void uu_tools_ui_hide(void) { g_idle_add(hide_window, NULL); }
|
||||||
|
void uu_tools_ui_present(void) { pthread_mutex_lock(&init_lock); if (init_state == 1) g_idle_add(show_window, NULL); pthread_mutex_unlock(&init_lock); }
|
||||||
@@ -0,0 +1,715 @@
|
|||||||
|
// GTK3 launcher window for the experimental native viewer (`uuremote-viewer ui`). Layout follows the
|
||||||
|
// official client's screens (see analysis notes: sidebar, device page, all devices, assistance,
|
||||||
|
// favorites, settings, QR sign-in, sign-out confirmation) without any NetEase assets. GTK runs on
|
||||||
|
// its own thread; Rust pushes state through g_idle_add and polls user commands. Account data is
|
||||||
|
// shown only in this local window and never logged.
|
||||||
|
#include <gtk/gtk.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <stdatomic.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
static const char *CSS =
|
||||||
|
"window { background-color: #ffffff; }\n"
|
||||||
|
"* { font-family: 'Noto Sans CJK SC', 'Noto Sans SC', sans-serif; }\n"
|
||||||
|
".sidebar { background-color: #e9e9ec; }\n"
|
||||||
|
".section { font-size: 16px; font-weight: 600; color: #1d1d1f; }\n"
|
||||||
|
".sidebar list, .sidebar row { background-color: transparent; }\n"
|
||||||
|
".sidebar row { border-radius: 8px; margin: 1px 10px; padding: 8px 10px; }\n"
|
||||||
|
".sidebar row:selected { background-color: #d4d4d9; color: #1d1d1f; }\n"
|
||||||
|
".devname { font-size: 15px; color: #1d1d1f; }\n"
|
||||||
|
".offline { color: #9ca3af; }\n"
|
||||||
|
".dot { color: #22c55e; font-size: 10px; }\n"
|
||||||
|
".dot.off { color: transparent; }\n"
|
||||||
|
".icon { background-color: #2f7bff; color: #ffffff; border-radius: 6px; font-size: 11px; font-weight: 700; padding: 2px 5px; }\n"
|
||||||
|
".icon.off { background-color: #c4c7cf; }\n"
|
||||||
|
".tag { background-color: #e8f0ff; color: #2f6bff; border-radius: 4px; font-size: 12px; padding: 1px 6px; }\n"
|
||||||
|
".title { font-size: 20px; font-weight: 700; color: #1d1d1f; }\n"
|
||||||
|
".header { border-bottom: 1px solid #e5e7eb; padding: 12px 22px; background-color: #f7f7f8; }\n"
|
||||||
|
".badge { background-color: #1d1d1f; color: #ffffff; border-radius: 12px; padding: 3px 12px; font-size: 13px; }\n"
|
||||||
|
".badge.off { background-color: #9ca3af; }\n"
|
||||||
|
".hbtn { background-image: none; background-color: transparent; border: none; font-size: 17px; color: #4b5563; padding: 4px 10px; }\n"
|
||||||
|
".preview { background-image: linear-gradient(135deg, #a8c0ff 0%, #e2c2f2 50%, #f6c8a8 100%); border-radius: 12px 12px 0 0; }\n"
|
||||||
|
".enter { background-image: none; background-color: rgba(0,0,0,0.18); color: #ffffff; border: none; font-size: 19px; padding: 8px 20px; border-radius: 8px; }\n"
|
||||||
|
".toolbar { border: 1px solid #e5e7eb; border-top: none; border-radius: 0 0 12px 12px; padding: 4px; }\n"
|
||||||
|
".tool { background-image: none; background-color: transparent; border: none; font-size: 15px; color: #374151; padding: 10px; }\n"
|
||||||
|
".dashed { border: 1px dashed #d1d5db; border-radius: 12px; padding: 36px; }\n"
|
||||||
|
".round { background-image: none; background-color: #f3f4f6; border: none; border-radius: 40px; font-size: 26px; min-width: 64px; min-height: 64px; }\n"
|
||||||
|
".card { background-color: #fafafa; border: 1px solid #e5e7eb; border-radius: 10px; padding: 14px 18px; }\n"
|
||||||
|
".cardbtn { background-image: none; background-color: #fafafa; border: 1px solid #e5e7eb; border-radius: 10px; padding: 12px 16px; }\n"
|
||||||
|
".banner { background-color: #f7f8fa; border: 1px solid #e5e7eb; border-radius: 10px; padding: 14px 18px; }\n"
|
||||||
|
".group { font-size: 16px; font-weight: 600; color: #1d1d1f; }\n"
|
||||||
|
".h2 { font-size: 19px; font-weight: 700; color: #1d1d1f; }\n"
|
||||||
|
".big { font-size: 30px; font-weight: 800; color: #111111; }\n"
|
||||||
|
".hint { font-size: 14px; color: #6b7280; }\n"
|
||||||
|
".primary { background-image: none; background-color: #3b82f6; color: #ffffff; border: none; border-radius: 8px; font-size: 16px; padding: 8px 26px; }\n"
|
||||||
|
".secondary { background-image: none; background-color: #ffffff; border: 1px solid #d1d5db; border-radius: 8px; padding: 6px 16px; }\n"
|
||||||
|
".qrbox { background-color: #ffffff; border-radius: 12px; border: 1px solid #e5e7eb; padding: 14px; }\n"
|
||||||
|
".status { font-size: 13px; color: #2f6bff; }\n"
|
||||||
|
".toast { background-color: #ffffff; border: 1px solid #e5e7eb; border-radius: 10px; padding: 10px 18px; font-size: 15px; }\n";
|
||||||
|
|
||||||
|
typedef struct { char id[64]; char name[128]; int platform, online, current; } Device;
|
||||||
|
static Device devices[64];
|
||||||
|
static int device_count;
|
||||||
|
static char queue[16][200];
|
||||||
|
static int q_head, q_len;
|
||||||
|
static pthread_mutex_t q_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
static pthread_mutex_t d_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
static atomic_int closed;
|
||||||
|
static pthread_t ui_thread;
|
||||||
|
static int ui_started, init_state;
|
||||||
|
static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
static pthread_cond_t init_cond = PTHREAD_COND_INITIALIZER;
|
||||||
|
|
||||||
|
static GtkWidget *assist_list;
|
||||||
|
static GtkWidget *window, *stack, *title_label, *badge, *more_button, *device_list, *my_section, *all_box,
|
||||||
|
*qr_image, *status_label, *toast, *banner, *fav_stack, *settings_window;
|
||||||
|
static int logged_in, selected = -1, banner_closed;
|
||||||
|
static char qr_path[512], status_text[256], user_text[128];
|
||||||
|
|
||||||
|
static int push(const char *cmd) {
|
||||||
|
pthread_mutex_lock(&q_lock);
|
||||||
|
int accepted = q_len < 16;
|
||||||
|
if (accepted) { snprintf(queue[(q_head + q_len) % 16], sizeof queue[0], "%s", cmd); q_len++; }
|
||||||
|
pthread_mutex_unlock(&q_lock);
|
||||||
|
return accepted;
|
||||||
|
}
|
||||||
|
static void add_class(GtkWidget *w, const char *cls) { gtk_style_context_add_class(gtk_widget_get_style_context(w), cls); }
|
||||||
|
static GtkWidget *label(const char *text, const char *cls) {
|
||||||
|
GtkWidget *l = gtk_label_new(text);
|
||||||
|
if (cls) add_class(l, cls);
|
||||||
|
gtk_label_set_xalign(GTK_LABEL(l), 0);
|
||||||
|
gtk_label_set_line_wrap(GTK_LABEL(l), TRUE);
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
static GtkWidget *button(const char *text, const char *cls) { GtkWidget *b = gtk_button_new_with_label(text); if (cls) add_class(b, cls); return b; }
|
||||||
|
static GtkWidget *hbox(int spacing) { return gtk_box_new(GTK_ORIENTATION_HORIZONTAL, spacing); }
|
||||||
|
static GtkWidget *vbox(int spacing) { return gtk_box_new(GTK_ORIENTATION_VERTICAL, spacing); }
|
||||||
|
static GtkWidget *platform_icon(int platform, int online) {
|
||||||
|
GtkWidget *i = gtk_label_new(platform == 4 ? "Mac" : platform == 1 ? "Win" : "PC");
|
||||||
|
add_class(i, "icon"); if (!online) add_class(i, "off");
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
static gboolean hide_toast(gpointer d) { (void)d; gtk_widget_hide(toast); return G_SOURCE_REMOVE; }
|
||||||
|
static void show_toast(const char *text) { gtk_label_set_text(GTK_LABEL(toast), text); gtk_widget_show(toast); g_timeout_add(3500, hide_toast, NULL); }
|
||||||
|
static void unsupported(GtkButton *b, gpointer what) { (void)b; char t[128]; snprintf(t, sizeof t, "%s:暂未支持", (const char *)what); show_toast(t); }
|
||||||
|
|
||||||
|
static void connect_kind(const char *kind) {
|
||||||
|
if (selected < 0) return;
|
||||||
|
char cmd[200];
|
||||||
|
pthread_mutex_lock(&d_lock);
|
||||||
|
snprintf(cmd, sizeof cmd, "connect\t%s\t%s", devices[selected].id, kind);
|
||||||
|
pthread_mutex_unlock(&d_lock);
|
||||||
|
push(cmd);
|
||||||
|
if (!strcmp(kind, "desktop") || !strcmp(kind, "watch")) show_toast("正在连接…");
|
||||||
|
}
|
||||||
|
static void on_tool(GtkButton *b, gpointer kind);
|
||||||
|
static void more_item(GtkButton *b, gpointer kind) {
|
||||||
|
GtkWidget *dialog = gtk_widget_get_toplevel(GTK_WIDGET(b));
|
||||||
|
const char *k = kind;
|
||||||
|
if (!strcmp(k, "reboot") || !strcmp(k, "shutdown")) {
|
||||||
|
GtkWidget *d = gtk_message_dialog_new(GTK_WINDOW(dialog), GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_NONE,
|
||||||
|
!strcmp(k, "reboot") ? "确定要重启被控电脑吗?" : "确定要关闭被控电脑吗?");
|
||||||
|
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) { connect_kind(k); show_toast("正在发送电源指令…"); }
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
gtk_widget_destroy(dialog);
|
||||||
|
on_tool(NULL, kind);
|
||||||
|
}
|
||||||
|
static void open_more(void) {
|
||||||
|
GtkWidget *dialog = gtk_dialog_new_with_buttons("更多工具", GTK_WINDOW(window), GTK_DIALOG_MODAL, NULL, NULL);
|
||||||
|
gtk_window_set_default_size(GTK_WINDOW(dialog), 520, -1);
|
||||||
|
GtkWidget *area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)), *box = vbox(10);
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(box), 22);
|
||||||
|
gtk_box_pack_start(GTK_BOX(box), label("更多工具", "h2"), FALSE, FALSE, 6);
|
||||||
|
const char *names[] = {"🗂 文件传输", "▶ 观看模式", "⌨ 终端", "⇄ 端口映射", "⏻ 远程开机", "↻ 重启", "⏻ 关机"};
|
||||||
|
const char *kinds[] = {"file", "watch", "terminal", "portmap", "wol", "reboot", "shutdown"};
|
||||||
|
for (int i = 0; i < 7; i++) {
|
||||||
|
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_end(GTK_BOX(line), label("›", "hint"), FALSE, FALSE, 0);
|
||||||
|
gtk_container_add(GTK_CONTAINER(b), line);
|
||||||
|
if (i == 4) gtk_widget_set_sensitive(b, FALSE); // 远程开机 is greyed for devices without Wake-on-LAN
|
||||||
|
else g_signal_connect(b, "clicked", G_CALLBACK(more_item), (gpointer)kinds[i]);
|
||||||
|
gtk_box_pack_start(GTK_BOX(box), b, FALSE, FALSE, 0);
|
||||||
|
}
|
||||||
|
GtkWidget *foot = hbox(10), *order = button("调整顺序", "secondary"), *close = button("关闭", "secondary");
|
||||||
|
g_signal_connect(order, "clicked", G_CALLBACK(unsupported), "调整顺序");
|
||||||
|
g_signal_connect_swapped(close, "clicked", G_CALLBACK(gtk_widget_destroy), dialog);
|
||||||
|
gtk_box_pack_start(GTK_BOX(foot), order, FALSE, FALSE, 0); gtk_box_pack_end(GTK_BOX(foot), close, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(box), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE, FALSE, 8);
|
||||||
|
gtk_box_pack_start(GTK_BOX(box), foot, FALSE, FALSE, 0);
|
||||||
|
gtk_container_add(GTK_CONTAINER(area), box);
|
||||||
|
gtk_widget_show_all(dialog);
|
||||||
|
}
|
||||||
|
static void on_tool(GtkButton *b, gpointer kind) {
|
||||||
|
(void)b;
|
||||||
|
if (!strcmp(kind, "more")) open_more();
|
||||||
|
else if (!strcmp(kind, "wol") || !strcmp(kind, "reboot") || !strcmp(kind, "shutdown"))
|
||||||
|
unsupported(NULL, (gpointer)(!strcmp(kind, "wol") ? "远程开机" : !strcmp(kind, "reboot") ? "重启" : "关机"));
|
||||||
|
else connect_kind(kind);
|
||||||
|
}
|
||||||
|
static void on_login(GtkButton *b, gpointer d) { (void)b; (void)d; push("login"); gtk_stack_set_visible_child_name(GTK_STACK(stack), "login"); gtk_label_set_text(GTK_LABEL(title_label), "登录"); }
|
||||||
|
|
||||||
|
static void set_header(const char *title, int device_page) {
|
||||||
|
gtk_label_set_text(GTK_LABEL(title_label), title);
|
||||||
|
gtk_widget_set_visible(badge, device_page); gtk_widget_set_visible(more_button, device_page);
|
||||||
|
}
|
||||||
|
static void show_page(const char *name, const char *title) {
|
||||||
|
int need_login = strcmp(name, "assist") && strcmp(name, "login");
|
||||||
|
if (!logged_in && need_login) { set_header("全部设备", 0); gtk_stack_set_visible_child_name(GTK_STACK(stack), "welcome"); return; }
|
||||||
|
set_header(title, 0);
|
||||||
|
gtk_stack_set_visible_child_name(GTK_STACK(stack), name);
|
||||||
|
}
|
||||||
|
static void show_device(int index) {
|
||||||
|
pthread_mutex_lock(&d_lock);
|
||||||
|
if (index < 0 || index >= device_count) { pthread_mutex_unlock(&d_lock); return; }
|
||||||
|
selected = index;
|
||||||
|
char name[128]; snprintf(name, sizeof name, "%s", devices[index].name);
|
||||||
|
int online = devices[index].online;
|
||||||
|
pthread_mutex_unlock(&d_lock);
|
||||||
|
set_header(name, 1);
|
||||||
|
gtk_label_set_markup(GTK_LABEL(badge), online ? "<span foreground='#22c55e'>●</span> 在线" : "离线");
|
||||||
|
GtkStyleContext *ctx = gtk_widget_get_style_context(badge);
|
||||||
|
if (online) gtk_style_context_remove_class(ctx, "off"); else gtk_style_context_add_class(ctx, "off");
|
||||||
|
gtk_stack_set_visible_child_name(GTK_STACK(stack), "device");
|
||||||
|
}
|
||||||
|
static void on_row(GtkListBox *box, GtkListBoxRow *row, gpointer d) {
|
||||||
|
(void)d;
|
||||||
|
if (!row) return;
|
||||||
|
// The two sidebar lists act as one selection.
|
||||||
|
gtk_list_box_unselect_all(GTK_LIST_BOX(box == GTK_LIST_BOX(device_list) ? assist_list : device_list));
|
||||||
|
const char *kind = g_object_get_data(G_OBJECT(row), "kind");
|
||||||
|
if (!kind) { show_device(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(row), "index"))); return; }
|
||||||
|
if (!strcmp(kind, "all")) show_page("all", "全部设备");
|
||||||
|
else if (!strcmp(kind, "assist")) show_page("assist", "远程协助");
|
||||||
|
else if (!strcmp(kind, "favorite")) show_page("favorite", "收藏设备");
|
||||||
|
}
|
||||||
|
static void on_card(GtkButton *b, gpointer index) { (void)b; show_device(GPOINTER_TO_INT(index)); }
|
||||||
|
|
||||||
|
static GtkWidget *nav_row(const char *icon, const char *text, const char *kind) {
|
||||||
|
GtkWidget *row = gtk_list_box_row_new(), *line = hbox(10);
|
||||||
|
gtk_box_pack_start(GTK_BOX(line), label(icon, "devname"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(line), label(text, "devname"), TRUE, TRUE, 0);
|
||||||
|
gtk_container_add(GTK_CONTAINER(row), line);
|
||||||
|
g_object_set_data_full(G_OBJECT(row), "kind", g_strdup(kind), g_free);
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
static void clear(GtkWidget *container) {
|
||||||
|
GList *children = gtk_container_get_children(GTK_CONTAINER(container));
|
||||||
|
for (GList *c = children; c; c = c->next) gtk_widget_destroy(c->data);
|
||||||
|
g_list_free(children);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- device info dialog (设备信息) ----
|
||||||
|
static void on_info(GtkButton *b, gpointer index) {
|
||||||
|
(void)b;
|
||||||
|
int i = GPOINTER_TO_INT(index);
|
||||||
|
char name[128] = "";
|
||||||
|
pthread_mutex_lock(&d_lock); if (i >= 0 && i < device_count) snprintf(name, sizeof name, "%s", devices[i].name); pthread_mutex_unlock(&d_lock);
|
||||||
|
GtkWidget *dialog = gtk_dialog_new_with_buttons("设备信息", GTK_WINDOW(window), GTK_DIALOG_MODAL, "完成", GTK_RESPONSE_OK, NULL);
|
||||||
|
GtkWidget *area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)), *box = vbox(12);
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(box), 20);
|
||||||
|
GtkWidget *c1 = vbox(8); add_class(c1, "card");
|
||||||
|
GtkWidget *r = hbox(8); gtk_box_pack_start(GTK_BOX(r), label("名称", "devname"), TRUE, TRUE, 0); gtk_box_pack_end(GTK_BOX(r), label(name, "hint"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(c1), r, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *rename = button("重命名", "secondary"); gtk_widget_set_halign(rename, GTK_ALIGN_END);
|
||||||
|
g_signal_connect(rename, "clicked", G_CALLBACK(unsupported), "重命名");
|
||||||
|
gtk_box_pack_start(GTK_BOX(c1), rename, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(box), c1, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *c2 = vbox(10); add_class(c2, "card");
|
||||||
|
const char *rows[] = {"处理器", "网卡", "总内存", "系统版本", "显卡"};
|
||||||
|
for (int k = 0; k < 5; k++) {
|
||||||
|
GtkWidget *line = hbox(8);
|
||||||
|
gtk_box_pack_start(GTK_BOX(line), label(rows[k], "devname"), TRUE, TRUE, 0);
|
||||||
|
gtk_box_pack_end(GTK_BOX(line), label("—", "hint"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(c2), line, FALSE, FALSE, 0);
|
||||||
|
}
|
||||||
|
gtk_box_pack_start(GTK_BOX(box), c2, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(box), label("硬件信息暂未获取。", "hint"), FALSE, FALSE, 0);
|
||||||
|
gtk_container_add(GTK_CONTAINER(area), box);
|
||||||
|
gtk_widget_show_all(dialog); gtk_dialog_run(GTK_DIALOG(dialog)); gtk_widget_destroy(dialog);
|
||||||
|
}
|
||||||
|
static void on_more(GtkButton *b, gpointer d) { (void)b; (void)d; on_info(NULL, GINT_TO_POINTER(selected)); }
|
||||||
|
|
||||||
|
static gboolean apply_devices(gpointer d) {
|
||||||
|
(void)d;
|
||||||
|
clear(device_list); clear(all_box);
|
||||||
|
pthread_mutex_lock(&d_lock);
|
||||||
|
if (!banner_closed) gtk_widget_show_all(banner);
|
||||||
|
char group[64]; snprintf(group, sizeof group, "⌄ 电脑 %d", device_count);
|
||||||
|
gtk_box_pack_start(GTK_BOX(all_box), label(group, "group"), FALSE, FALSE, 8);
|
||||||
|
for (int i = 0; i < device_count; i++) {
|
||||||
|
GtkWidget *row = gtk_list_box_row_new(), *line = hbox(8);
|
||||||
|
GtkWidget *dot = gtk_label_new("●"); add_class(dot, "dot"); if (!devices[i].online) add_class(dot, "off");
|
||||||
|
GtkWidget *name = label(devices[i].name, "devname"); if (!devices[i].online) add_class(name, "offline");
|
||||||
|
gtk_label_set_line_wrap(GTK_LABEL(name), FALSE); gtk_label_set_ellipsize(GTK_LABEL(name), PANGO_ELLIPSIZE_END);
|
||||||
|
gtk_box_pack_start(GTK_BOX(line), dot, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(line), platform_icon(devices[i].platform, devices[i].online), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(line), name, TRUE, TRUE, 0);
|
||||||
|
gtk_container_add(GTK_CONTAINER(row), line);
|
||||||
|
g_object_set_data(G_OBJECT(row), "index", GINT_TO_POINTER(i));
|
||||||
|
if (!devices[i].current) gtk_list_box_insert(GTK_LIST_BOX(device_list), row, -1); else gtk_widget_destroy(row);
|
||||||
|
GtkWidget *card = hbox(12); add_class(card, "card");
|
||||||
|
gtk_box_pack_start(GTK_BOX(card), platform_icon(devices[i].platform, 1), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(card), label(devices[i].name, "devname"), FALSE, FALSE, 0);
|
||||||
|
if (devices[i].current) gtk_box_pack_start(GTK_BOX(card), label("本机", "tag"), FALSE, FALSE, 0);
|
||||||
|
GtkWidget *open = button("›", "hbtn"), *info = button("ⓘ", "hbtn");
|
||||||
|
g_signal_connect(info, "clicked", G_CALLBACK(on_info), GINT_TO_POINTER(i));
|
||||||
|
if (!devices[i].current) { g_signal_connect(open, "clicked", G_CALLBACK(on_card), GINT_TO_POINTER(i)); gtk_box_pack_end(GTK_BOX(card), open, FALSE, FALSE, 0); }
|
||||||
|
else gtk_widget_destroy(open);
|
||||||
|
gtk_box_pack_end(GTK_BOX(card), info, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(all_box), card, FALSE, FALSE, 4);
|
||||||
|
}
|
||||||
|
pthread_mutex_unlock(&d_lock);
|
||||||
|
gtk_list_box_insert(GTK_LIST_BOX(device_list), nav_row("◼", "全部设备", "all"), -1);
|
||||||
|
gtk_widget_show_all(device_list); gtk_widget_show_all(all_box);
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
static gboolean apply_login(gpointer d) {
|
||||||
|
(void)d;
|
||||||
|
gtk_label_set_text(GTK_LABEL(status_label), status_text);
|
||||||
|
if (qr_path[0]) {
|
||||||
|
GdkPixbuf *pix = gdk_pixbuf_new_from_file_at_size(qr_path, 240, 240, NULL);
|
||||||
|
if (pix) { gtk_image_set_from_pixbuf(GTK_IMAGE(qr_image), pix); g_object_unref(pix); }
|
||||||
|
} else gtk_image_clear(GTK_IMAGE(qr_image));
|
||||||
|
gtk_widget_set_visible(my_section, logged_in);
|
||||||
|
const char *page = gtk_stack_get_visible_child_name(GTK_STACK(stack));
|
||||||
|
if (logged_in && (!strcmp(page, "login") || !strcmp(page, "welcome"))) show_page("all", "全部设备");
|
||||||
|
if (!logged_in && strcmp(page, "login") && strcmp(page, "assist")) show_page("welcome", "全部设备");
|
||||||
|
if (!logged_in && qr_path[0]) { gtk_stack_set_visible_child_name(GTK_STACK(stack), "login"); set_header("登录", 0); }
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- pages ----
|
||||||
|
static GtkWidget *build_welcome(void) {
|
||||||
|
GtkWidget *page = vbox(22); gtk_widget_set_valign(page, GTK_ALIGN_CENTER); gtk_widget_set_halign(page, GTK_ALIGN_CENTER);
|
||||||
|
GtkWidget *art = gtk_event_box_new(); add_class(art, "preview"); gtk_widget_set_size_request(art, 520, 300);
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), art, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *t = label("随时访问个人设备,可用于办公、游戏、文件传输。", "h2"); gtk_widget_set_halign(t, GTK_ALIGN_CENTER);
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), t, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *b = button("立即登录", "primary"); gtk_widget_set_halign(b, GTK_ALIGN_CENTER);
|
||||||
|
g_signal_connect(b, "clicked", G_CALLBACK(on_login), NULL);
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), b, FALSE, FALSE, 0);
|
||||||
|
return page;
|
||||||
|
}
|
||||||
|
static GtkWidget *build_login(void) {
|
||||||
|
GtkWidget *page = vbox(16); gtk_widget_set_valign(page, GTK_ALIGN_CENTER); gtk_widget_set_halign(page, GTK_ALIGN_CENTER);
|
||||||
|
GtkWidget *brand = label("UU 远程 Linux 控制端(非官方)", "h2"); gtk_widget_set_halign(brand, GTK_ALIGN_CENTER);
|
||||||
|
GtkWidget *t = label("扫码登录", "group"); gtk_widget_set_halign(t, GTK_ALIGN_CENTER);
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), brand, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), t, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *box = vbox(0); add_class(box, "qrbox"); gtk_widget_set_halign(box, GTK_ALIGN_CENTER);
|
||||||
|
qr_image = gtk_image_new(); gtk_widget_set_size_request(qr_image, 240, 240);
|
||||||
|
gtk_container_add(GTK_CONTAINER(box), qr_image);
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), box, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *h = label("请使用网易 UU 远程 App 扫一扫\n可快速登录", "hint"); gtk_label_set_justify(GTK_LABEL(h), GTK_JUSTIFY_CENTER); gtk_widget_set_halign(h, GTK_ALIGN_CENTER);
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), h, FALSE, FALSE, 0);
|
||||||
|
status_label = label("", "status"); gtk_widget_set_halign(status_label, GTK_ALIGN_CENTER);
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), status_label, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *again = button("刷新二维码", "secondary"); gtk_widget_set_halign(again, GTK_ALIGN_CENTER);
|
||||||
|
g_signal_connect(again, "clicked", G_CALLBACK(on_login), NULL);
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), again, FALSE, FALSE, 0);
|
||||||
|
return page;
|
||||||
|
}
|
||||||
|
static GtkWidget *build_device(void) {
|
||||||
|
GtkWidget *page = vbox(0); gtk_container_set_border_width(GTK_CONTAINER(page), 26);
|
||||||
|
GtkWidget *preview = gtk_overlay_new(), *bg = gtk_event_box_new(); add_class(bg, "preview"); gtk_widget_set_size_request(bg, -1, 320);
|
||||||
|
gtk_container_add(GTK_CONTAINER(preview), bg);
|
||||||
|
GtkWidget *enter = button("进入桌面 →", "enter");
|
||||||
|
gtk_widget_set_halign(enter, GTK_ALIGN_CENTER); gtk_widget_set_valign(enter, GTK_ALIGN_END); gtk_widget_set_margin_bottom(enter, 26);
|
||||||
|
g_signal_connect(enter, "clicked", G_CALLBACK(on_tool), "desktop");
|
||||||
|
gtk_overlay_add_overlay(GTK_OVERLAY(preview), enter);
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), preview, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *tools = hbox(0); add_class(tools, "toolbar");
|
||||||
|
const char *names[] = {"🗂 文件传输", "▶ 观看模式", "⌨ 终端", "⇄ 端口映射", "▦"}, *kinds[] = {"file", "watch", "terminal", "portmap", "more"};
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
GtkWidget *b = button(names[i], "tool");
|
||||||
|
g_signal_connect(b, "clicked", G_CALLBACK(on_tool), (gpointer)kinds[i]);
|
||||||
|
gtk_box_pack_start(GTK_BOX(tools), b, i < 4, TRUE, 0);
|
||||||
|
if (i < 4) gtk_box_pack_start(GTK_BOX(tools), gtk_separator_new(GTK_ORIENTATION_VERTICAL), FALSE, FALSE, 0);
|
||||||
|
}
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), tools, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), label("快速启动", "h2"), FALSE, FALSE, 22);
|
||||||
|
GtkWidget *quick = vbox(10); add_class(quick, "dashed");
|
||||||
|
GtkWidget *add = button("+", "round"); gtk_widget_set_halign(add, GTK_ALIGN_CENTER);
|
||||||
|
g_signal_connect(add, "clicked", G_CALLBACK(unsupported), "快速启动");
|
||||||
|
GtkWidget *al = label("添加", "hint"); gtk_widget_set_halign(al, GTK_ALIGN_CENTER);
|
||||||
|
gtk_box_pack_start(GTK_BOX(quick), add, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(quick), al, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), quick, FALSE, FALSE, 0);
|
||||||
|
return page;
|
||||||
|
}
|
||||||
|
static void close_banner(GtkButton *b, gpointer d) { (void)b; (void)d; banner_closed = 1; gtk_widget_hide(banner); }
|
||||||
|
static GtkWidget *build_all(void) {
|
||||||
|
GtkWidget *page = vbox(8); gtk_container_set_border_width(GTK_CONTAINER(page), 26);
|
||||||
|
banner = hbox(10); add_class(banner, "banner");
|
||||||
|
GtkWidget *text = vbox(4);
|
||||||
|
gtk_box_pack_start(GTK_BOX(text), label("ⓘ 添加你的其他设备", "group"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(text), label("在其他电脑上安装 UU 远程官方客户端或本项目主机端,登录同一账号后即可在这里远程访问。", "hint"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(banner), text, TRUE, TRUE, 0);
|
||||||
|
GtkWidget *x = button("×", "hbtn"); gtk_widget_set_valign(x, GTK_ALIGN_START);
|
||||||
|
g_signal_connect(x, "clicked", G_CALLBACK(close_banner), NULL);
|
||||||
|
gtk_box_pack_end(GTK_BOX(banner), x, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), banner, FALSE, FALSE, 0);
|
||||||
|
all_box = vbox(4);
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), all_box, FALSE, FALSE, 8);
|
||||||
|
GtkWidget *scroll = gtk_scrolled_window_new(NULL, NULL); gtk_container_add(GTK_CONTAINER(scroll), page);
|
||||||
|
return scroll;
|
||||||
|
}
|
||||||
|
static void on_assist_connect(GtkButton *b, gpointer entry) {
|
||||||
|
(void)b;
|
||||||
|
const char *id = gtk_entry_get_text(GTK_ENTRY(entry));
|
||||||
|
if (!id[0]) { show_toast("请输入伙伴的设备 ID"); return; }
|
||||||
|
GtkWidget *code_entry = g_object_get_data(G_OBJECT(entry), "assist-code");
|
||||||
|
const char *code = gtk_entry_get_text(GTK_ENTRY(code_entry));
|
||||||
|
if (!code[0]) { show_toast("请输入伙伴的设备验证码"); return; }
|
||||||
|
if (strpbrk(id, "\t\r\n") || strpbrk(code, "\t\r\n")) { show_toast("设备 ID 和验证码不能包含换行或制表符"); return; }
|
||||||
|
char cmd[200];
|
||||||
|
int n = snprintf(cmd, sizeof cmd, "assist\t%s\t%s", id, code);
|
||||||
|
if (n < 0 || (size_t)n >= sizeof cmd) { show_toast("设备 ID 或验证码过长"); return; }
|
||||||
|
int accepted = push(cmd);
|
||||||
|
memset(cmd, 0, sizeof cmd);
|
||||||
|
if (!accepted) { show_toast("连接请求过多,请稍后重试"); return; }
|
||||||
|
gtk_entry_set_text(GTK_ENTRY(code_entry), "");
|
||||||
|
show_toast("正在连接…");
|
||||||
|
}
|
||||||
|
static GtkWidget *build_assist(void) {
|
||||||
|
GtkWidget *page = vbox(20); gtk_container_set_border_width(GTK_CONTAINER(page), 26);
|
||||||
|
GtkWidget *mine = vbox(14); add_class(mine, "card");
|
||||||
|
GtkWidget *top = hbox(10); gtk_box_pack_start(GTK_BOX(top), label("本设备", "h2"), TRUE, TRUE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(top), label("允许他人远程协助", "devname"), FALSE, FALSE, 0);
|
||||||
|
GtkWidget *sw = gtk_switch_new(); gtk_widget_set_sensitive(sw, FALSE); gtk_box_pack_start(GTK_BOX(top), sw, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(mine), top, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(mine), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE, FALSE, 0);
|
||||||
|
GtkWidget *grid = hbox(40), *c1 = vbox(6), *c2 = vbox(6);
|
||||||
|
gtk_box_pack_start(GTK_BOX(c1), label("本设备 ID", "devname"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(c1), label("— — —", "big"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(c2), label("验证方式:仅使用临时验证码", "devname"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(c2), label("—", "big"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(c2), label("每次远控结束刷新", "hint"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(grid), c1, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(grid), c2, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(mine), grid, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(mine), label("本机被协助由 uuremote-server 主机端提供(uuremote-server assist)。", "hint"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), mine, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *peer = vbox(12); add_class(peer, "card");
|
||||||
|
gtk_box_pack_start(GTK_BOX(peer), label("远控伙伴设备", "h2"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(peer), label("通过其他设备的【设备 ID】及【设备验证码】启动远程控制", "hint"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(peer), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(peer), label("伙伴的设备 ID", "devname"), FALSE, FALSE, 0);
|
||||||
|
GtkWidget *row = hbox(20), *entry = gtk_entry_new(); gtk_entry_set_placeholder_text(GTK_ENTRY(entry), "请输入设备 ID"); gtk_widget_set_size_request(entry, 320, -1);
|
||||||
|
GtkWidget *code = gtk_entry_new(); gtk_entry_set_placeholder_text(GTK_ENTRY(code), "请输入设备验证码");
|
||||||
|
gtk_entry_set_visibility(GTK_ENTRY(code), FALSE);
|
||||||
|
gtk_entry_set_input_purpose(GTK_ENTRY(code), GTK_INPUT_PURPOSE_PASSWORD);
|
||||||
|
gtk_entry_set_max_length(GTK_ENTRY(entry), 64); gtk_entry_set_max_length(GTK_ENTRY(code), 64);
|
||||||
|
g_object_set_data(G_OBJECT(entry), "assist-code", code);
|
||||||
|
g_signal_connect(code, "activate", G_CALLBACK(on_assist_connect), entry);
|
||||||
|
GtkWidget *go = button("连接", "primary"); g_signal_connect(go, "clicked", G_CALLBACK(on_assist_connect), entry);
|
||||||
|
gtk_box_pack_start(GTK_BOX(row), entry, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(row), code, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(row), go, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(peer), row, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), peer, FALSE, FALSE, 0);
|
||||||
|
return page;
|
||||||
|
}
|
||||||
|
static void on_add_favorite(GtkButton *b, gpointer d) {
|
||||||
|
(void)b; (void)d;
|
||||||
|
GtkWidget *dialog = gtk_dialog_new_with_buttons("添加收藏设备", GTK_WINDOW(window), GTK_DIALOG_MODAL, "取消", GTK_RESPONSE_CANCEL, "添加", GTK_RESPONSE_OK, NULL);
|
||||||
|
GtkWidget *area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)), *card = vbox(12); add_class(card, "card");
|
||||||
|
gtk_container_set_border_width(GTK_CONTAINER(area), 16);
|
||||||
|
const char *names[] = {"伙伴的设备 ID", "设备验证码(可选)", "备注名称(可选)"}, *hints[] = {"请输入设备 ID", "请输入设备验证码", "请输入名称"};
|
||||||
|
GtkWidget *first = NULL;
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
GtkWidget *line = hbox(20), *e = gtk_entry_new(); gtk_entry_set_placeholder_text(GTK_ENTRY(e), hints[i]); gtk_widget_set_size_request(e, 280, -1);
|
||||||
|
if (!first) first = e;
|
||||||
|
gtk_box_pack_start(GTK_BOX(line), label(names[i], "devname"), TRUE, TRUE, 0); gtk_box_pack_end(GTK_BOX(line), e, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(card), line, FALSE, FALSE, 0);
|
||||||
|
}
|
||||||
|
gtk_container_add(GTK_CONTAINER(area), card);
|
||||||
|
gtk_widget_show_all(dialog);
|
||||||
|
int response = gtk_dialog_run(GTK_DIALOG(dialog));
|
||||||
|
int has_id = first && gtk_entry_get_text(GTK_ENTRY(first))[0];
|
||||||
|
gtk_widget_destroy(dialog);
|
||||||
|
if (response == GTK_RESPONSE_OK) show_toast(has_id ? "收藏设备:暂未支持" : "请输入伙伴的设备 ID");
|
||||||
|
}
|
||||||
|
static GtkWidget *build_favorite(void) {
|
||||||
|
GtkWidget *page = vbox(10); gtk_container_set_border_width(GTK_CONTAINER(page), 26);
|
||||||
|
GtkWidget *add = button("添加收藏设备", "secondary"); gtk_widget_set_halign(add, GTK_ALIGN_END);
|
||||||
|
g_signal_connect(add, "clicked", G_CALLBACK(on_add_favorite), NULL);
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), add, FALSE, FALSE, 0);
|
||||||
|
fav_stack = gtk_stack_new();
|
||||||
|
GtkWidget *empty = vbox(10); gtk_widget_set_valign(empty, GTK_ALIGN_CENTER);
|
||||||
|
GtkWidget *box = label("📦", "big"); gtk_widget_set_halign(box, GTK_ALIGN_CENTER);
|
||||||
|
GtkWidget *t = label("暂无收藏", "devname"); gtk_widget_set_halign(t, GTK_ALIGN_CENTER);
|
||||||
|
gtk_box_pack_start(GTK_BOX(empty), box, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(empty), t, FALSE, FALSE, 0);
|
||||||
|
gtk_stack_add_named(GTK_STACK(fav_stack), empty, "empty");
|
||||||
|
gtk_box_pack_start(GTK_BOX(page), fav_stack, TRUE, TRUE, 0);
|
||||||
|
return page;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- settings window (常规 / 安全 / 键盘 / 网络) ----
|
||||||
|
static GtkWidget *setting_row(const char *title, const char *detail, GtkWidget *control) {
|
||||||
|
GtkWidget *row = hbox(12), *text = vbox(2);
|
||||||
|
gtk_box_pack_start(GTK_BOX(text), label(title, "devname"), FALSE, FALSE, 0);
|
||||||
|
if (detail) gtk_box_pack_start(GTK_BOX(text), label(detail, "hint"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(row), text, TRUE, TRUE, 0);
|
||||||
|
if (control) { gtk_widget_set_valign(control, GTK_ALIGN_CENTER); gtk_box_pack_end(GTK_BOX(row), control, FALSE, FALSE, 0); }
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
static GtkWidget *combo(const char *const *items, int n) {
|
||||||
|
GtkWidget *c = gtk_combo_box_text_new();
|
||||||
|
for (int i = 0; i < n; i++) gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(c), items[i]);
|
||||||
|
gtk_combo_box_set_active(GTK_COMBO_BOX(c), 0);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
static GtkWidget *settings_card(GtkWidget **rows, int n) {
|
||||||
|
GtkWidget *card = vbox(10); add_class(card, "card");
|
||||||
|
for (int i = 0; i < n; i++) { if (i) gtk_box_pack_start(GTK_BOX(card), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(card), rows[i], FALSE, FALSE, 0); }
|
||||||
|
return card;
|
||||||
|
}
|
||||||
|
static GtkWidget *settings_page(void) { GtkWidget *p = vbox(14); gtk_container_set_border_width(GTK_CONTAINER(p), 22); return p; }
|
||||||
|
static GtkWidget *edit_button(const char *what) { GtkWidget *b = button("编辑", "secondary"); g_signal_connect(b, "clicked", G_CALLBACK(unsupported), (gpointer)what); return b; }
|
||||||
|
static gboolean settings_hidden(GtkWidget *w, GdkEvent *e, gpointer d) { (void)e; (void)d; gtk_widget_hide(w); return TRUE; }
|
||||||
|
static gboolean settings_key(GtkWidget *w, GdkEventKey *e, gpointer d) { (void)d; if (e->keyval == GDK_KEY_Escape) { gtk_widget_hide(w); return TRUE; } return FALSE; }
|
||||||
|
// Compositors such as Hyprland draw no title bar, so windows carry their own (with a close button).
|
||||||
|
static void own_titlebar(GtkWidget *w, const char *title) {
|
||||||
|
GtkWidget *bar = gtk_header_bar_new();
|
||||||
|
gtk_header_bar_set_title(GTK_HEADER_BAR(bar), title);
|
||||||
|
gtk_header_bar_set_show_close_button(GTK_HEADER_BAR(bar), TRUE);
|
||||||
|
gtk_window_set_titlebar(GTK_WINDOW(w), bar);
|
||||||
|
}
|
||||||
|
static void open_settings(void) {
|
||||||
|
if (settings_window) { gtk_window_present(GTK_WINDOW(settings_window)); return; }
|
||||||
|
settings_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
||||||
|
gtk_window_set_title(GTK_WINDOW(settings_window), "设置");
|
||||||
|
gtk_window_set_transient_for(GTK_WINDOW(settings_window), GTK_WINDOW(window));
|
||||||
|
gtk_window_set_default_size(GTK_WINDOW(settings_window), 680, 600);
|
||||||
|
g_signal_connect(settings_window, "delete-event", G_CALLBACK(settings_hidden), NULL);
|
||||||
|
g_signal_connect(settings_window, "key-press-event", G_CALLBACK(settings_key), NULL);
|
||||||
|
own_titlebar(settings_window, "设置");
|
||||||
|
GtkWidget *root = vbox(0), *tabs = gtk_stack_new(), *switcher = gtk_stack_switcher_new();
|
||||||
|
gtk_stack_switcher_set_stack(GTK_STACK_SWITCHER(switcher), GTK_STACK(tabs));
|
||||||
|
gtk_widget_set_halign(switcher, GTK_ALIGN_CENTER); gtk_widget_set_margin_top(switcher, 10); gtk_widget_set_margin_bottom(switcher, 6);
|
||||||
|
gtk_box_pack_start(GTK_BOX(root), switcher, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(root), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE, FALSE, 0);
|
||||||
|
// 常规
|
||||||
|
GtkWidget *general = settings_page();
|
||||||
|
const char *wake[] = {"永不", "接通电源时", "始终"}, *close_opts[] = {"最小化", "退出程序"};
|
||||||
|
GtkWidget *g1[] = { setting_row("开机自启动", NULL, gtk_switch_new()),
|
||||||
|
setting_row("保持唤醒状态", "防止电脑进入睡眠状态,导致无法远程访问。", combo(wake, 3)),
|
||||||
|
setting_row("关闭窗口时", NULL, combo(close_opts, 2)) };
|
||||||
|
gtk_box_pack_start(GTK_BOX(general), settings_card(g1, 3), FALSE, FALSE, 0);
|
||||||
|
GtkWidget *path = hbox(8); GtkWidget *change = button("修改路径", "secondary"), *open = button("打开文件夹", "secondary");
|
||||||
|
g_signal_connect(change, "clicked", G_CALLBACK(unsupported), "修改路径"); g_signal_connect(open, "clicked", G_CALLBACK(unsupported), "打开文件夹");
|
||||||
|
gtk_box_pack_end(GTK_BOX(path), open, FALSE, FALSE, 0); gtk_box_pack_end(GTK_BOX(path), change, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *g2[] = { setting_row("传输文件存储路径", "~/Downloads", path) };
|
||||||
|
gtk_box_pack_start(GTK_BOX(general), settings_card(g2, 1), FALSE, FALSE, 0);
|
||||||
|
GtkWidget *real = button("在手机 App 中查看", "secondary"); gtk_widget_set_sensitive(real, FALSE);
|
||||||
|
GtkWidget *g3[] = { setting_row("实名认证", NULL, real) };
|
||||||
|
gtk_box_pack_start(GTK_BOX(general), settings_card(g3, 1), FALSE, FALSE, 0);
|
||||||
|
gtk_stack_add_titled(GTK_STACK(tabs), general, "general", "⚙ 常规");
|
||||||
|
// 安全
|
||||||
|
GtkWidget *security = settings_page();
|
||||||
|
const char *verify[] = {"仅使用临时验证码", "仅使用自定义验证码", "临时与自定义验证码"};
|
||||||
|
GtkWidget *s1[] = { setting_row("允许同账号控制本设备", NULL, gtk_switch_new()), setting_row("允许他人远程协助", NULL, gtk_switch_new()) };
|
||||||
|
GtkWidget *s2[] = { setting_row("远程协助连接本设备的验证方式", NULL, combo(verify, 3)) };
|
||||||
|
GtkWidget *s3[] = { setting_row("客户端锁定", "防止客户端设置被修改,保障数据与使用安全。", gtk_switch_new()) };
|
||||||
|
gtk_box_pack_start(GTK_BOX(security), settings_card(s1, 2), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(security), settings_card(s2, 1), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(security), settings_card(s3, 1), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(security), label("本机被控相关设置由 uuremote-server 主机端提供。", "hint"), FALSE, FALSE, 0);
|
||||||
|
gtk_stack_add_titled(GTK_STACK(tabs), security, "security", "🔒 安全");
|
||||||
|
// 键盘
|
||||||
|
GtkWidget *keys = settings_page();
|
||||||
|
gtk_box_pack_start(GTK_BOX(keys), label("仅控制端响应的快捷键", "h2"), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(keys), label("远控时按下以下快捷键,仅在控制端本地响应,不会发送到被控端。", "hint"), FALSE, FALSE, 0);
|
||||||
|
GtkWidget *addk = button("添加控制端快捷键", "secondary"); g_signal_connect(addk, "clicked", G_CALLBACK(unsupported), "添加快捷键");
|
||||||
|
GtkWidget *k1[] = { setting_row("当前暂无设置", NULL, NULL), setting_row("", NULL, addk) };
|
||||||
|
gtk_box_pack_start(GTK_BOX(keys), settings_card(k1, 2), FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(keys), label("功能快捷键", "h2"), FALSE, FALSE, 6);
|
||||||
|
gtk_box_pack_start(GTK_BOX(keys), label("按下以下快捷键时,仅在本地触发对应功能,不会发送到被控端。", "hint"), FALSE, FALSE, 0);
|
||||||
|
const char *fns[] = {"使用被控端鼠标/解锁鼠标", "被控时解除防窥模式", "切换上一个屏幕", "切换下一个屏幕", "显示/隐藏悬浮球", "静音/取消静音", "老板键(隐藏/显示所有客户端窗口)", "发送 Ctrl+Alt+Delete 组合键"};
|
||||||
|
GtkWidget *k2[8];
|
||||||
|
for (int i = 0; i < 8; i++) k2[i] = setting_row(fns[i], NULL, edit_button(fns[i]));
|
||||||
|
gtk_box_pack_start(GTK_BOX(keys), settings_card(k2, 8), FALSE, FALSE, 0);
|
||||||
|
GtkWidget *kscroll = gtk_scrolled_window_new(NULL, NULL); gtk_container_add(GTK_CONTAINER(kscroll), keys);
|
||||||
|
gtk_stack_add_titled(GTK_STACK(tabs), kscroll, "keys", "⌨ 键盘");
|
||||||
|
// 网络
|
||||||
|
GtkWidget *network = settings_page();
|
||||||
|
const char *proxy[] = {"使用系统环境代理", "不使用代理"};
|
||||||
|
GtkWidget *n1[] = { setting_row("代理设置", NULL, combo(proxy, 2)) };
|
||||||
|
gtk_box_pack_start(GTK_BOX(network), settings_card(n1, 1), FALSE, FALSE, 0);
|
||||||
|
gtk_stack_add_titled(GTK_STACK(tabs), network, "network", "🌐 网络");
|
||||||
|
gtk_box_pack_start(GTK_BOX(root), tabs, TRUE, TRUE, 0);
|
||||||
|
gtk_container_add(GTK_CONTAINER(settings_window), root);
|
||||||
|
gtk_widget_show_all(settings_window);
|
||||||
|
}
|
||||||
|
static void confirm_logout(void) {
|
||||||
|
GtkWidget *d = gtk_message_dialog_new(GTK_WINDOW(window), 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);
|
||||||
|
if (gtk_dialog_run(GTK_DIALOG(d)) == GTK_RESPONSE_OK) push("logout");
|
||||||
|
gtk_widget_destroy(d);
|
||||||
|
}
|
||||||
|
static void on_menu(GtkMenuItem *item, gpointer what) {
|
||||||
|
(void)item;
|
||||||
|
if (!strcmp(what, "settings")) open_settings();
|
||||||
|
else if (!strcmp(what, "logout")) { if (logged_in) confirm_logout(); }
|
||||||
|
else if (!strcmp(what, "refresh")) push("refresh");
|
||||||
|
else if (!strcmp(what, "quit")) { atomic_store(&closed, 1); push("quit"); gtk_main_quit(); }
|
||||||
|
}
|
||||||
|
static void popup(GtkButton *b, gpointer kind) {
|
||||||
|
GtkWidget *menu = gtk_menu_new();
|
||||||
|
if (!strcmp(kind, "account")) {
|
||||||
|
GtkWidget *who = gtk_menu_item_new_with_label(logged_in ? (user_text[0] ? user_text : "已登录") : "未登录"); gtk_widget_set_sensitive(who, FALSE);
|
||||||
|
gtk_menu_shell_append(GTK_MENU_SHELL(menu), who);
|
||||||
|
GtkWidget *out = gtk_menu_item_new_with_label("退出登录"); g_signal_connect(out, "activate", G_CALLBACK(on_menu), "logout");
|
||||||
|
gtk_widget_set_sensitive(out, logged_in); gtk_menu_shell_append(GTK_MENU_SHELL(menu), out);
|
||||||
|
} else if (!strcmp(kind, "notice")) {
|
||||||
|
GtkWidget *none = gtk_menu_item_new_with_label("暂无通知"); gtk_widget_set_sensitive(none, FALSE);
|
||||||
|
gtk_menu_shell_append(GTK_MENU_SHELL(menu), none);
|
||||||
|
} else {
|
||||||
|
const char *labels[] = {"刷新设备列表", "设置", "退出程序"}, *whats[] = {"refresh", "settings", "quit"};
|
||||||
|
for (int i = 0; i < 3; i++) { GtkWidget *it = gtk_menu_item_new_with_label(labels[i]); g_signal_connect(it, "activate", G_CALLBACK(on_menu), (gpointer)whats[i]); gtk_menu_shell_append(GTK_MENU_SHELL(menu), it); }
|
||||||
|
}
|
||||||
|
gtk_widget_show_all(menu);
|
||||||
|
gtk_menu_popup_at_widget(GTK_MENU(menu), GTK_WIDGET(b), GDK_GRAVITY_SOUTH_EAST, GDK_GRAVITY_NORTH_EAST, NULL);
|
||||||
|
}
|
||||||
|
static gboolean on_delete(GtkWidget *w, GdkEvent *e, gpointer d) { (void)w; (void)e; (void)d; atomic_store(&closed, 1); push("quit"); gtk_main_quit(); return FALSE; }
|
||||||
|
|
||||||
|
static void *run(void *arg) {
|
||||||
|
(void)arg;
|
||||||
|
int ok = gtk_init_check(NULL, NULL);
|
||||||
|
if (ok) {
|
||||||
|
GtkCssProvider *css = gtk_css_provider_new();
|
||||||
|
gtk_css_provider_load_from_data(css, CSS, -1, NULL);
|
||||||
|
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||||
|
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
||||||
|
gtk_window_set_title(GTK_WINDOW(window), "UU 远程 Linux 控制端(非官方)");
|
||||||
|
gtk_window_set_default_size(GTK_WINDOW(window), 1180, 780);
|
||||||
|
g_signal_connect(window, "delete-event", G_CALLBACK(on_delete), NULL);
|
||||||
|
own_titlebar(window, "UU 远程 Linux 控制端(非官方)");
|
||||||
|
GtkWidget *root = hbox(0);
|
||||||
|
// Sidebar
|
||||||
|
GtkWidget *side = vbox(0); add_class(side, "sidebar"); gtk_widget_set_size_request(side, 270, -1);
|
||||||
|
my_section = vbox(0);
|
||||||
|
GtkWidget *mh = hbox(8); gtk_widget_set_margin_start(mh, 18); gtk_widget_set_margin_top(mh, 22); gtk_widget_set_margin_bottom(mh, 6);
|
||||||
|
gtk_box_pack_start(GTK_BOX(mh), label("⧉ 我的设备", "section"), TRUE, TRUE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(my_section), mh, FALSE, FALSE, 0);
|
||||||
|
device_list = gtk_list_box_new(); g_signal_connect(device_list, "row-activated", G_CALLBACK(on_row), NULL);
|
||||||
|
gtk_box_pack_start(GTK_BOX(my_section), device_list, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *scroll = gtk_scrolled_window_new(NULL, NULL); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
|
||||||
|
GtkWidget *side_inner = vbox(0); gtk_box_pack_start(GTK_BOX(side_inner), my_section, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *welcome_nav = gtk_list_box_new(); g_signal_connect(welcome_nav, "row-activated", G_CALLBACK(on_row), NULL);
|
||||||
|
GtkWidget *ah = label("⧉ 远程协助", "section"); gtk_widget_set_margin_start(ah, 18); gtk_widget_set_margin_top(ah, 16); gtk_widget_set_margin_bottom(ah, 6);
|
||||||
|
gtk_box_pack_start(GTK_BOX(side_inner), ah, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *assist = assist_list = gtk_list_box_new(); g_signal_connect(assist, "row-activated", G_CALLBACK(on_row), NULL);
|
||||||
|
gtk_list_box_insert(GTK_LIST_BOX(assist), nav_row("◧", "开始协助", "assist"), -1);
|
||||||
|
gtk_list_box_insert(GTK_LIST_BOX(assist), nav_row("★", "收藏设备", "favorite"), -1);
|
||||||
|
gtk_box_pack_start(GTK_BOX(side_inner), assist, FALSE, FALSE, 0);
|
||||||
|
gtk_list_box_unselect_all(GTK_LIST_BOX(assist));
|
||||||
|
(void)welcome_nav;
|
||||||
|
gtk_container_add(GTK_CONTAINER(scroll), side_inner);
|
||||||
|
gtk_box_pack_start(GTK_BOX(side), scroll, TRUE, TRUE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(root), side, FALSE, FALSE, 0);
|
||||||
|
// Main area
|
||||||
|
GtkWidget *main = vbox(0), *header = hbox(10); add_class(header, "header");
|
||||||
|
badge = gtk_label_new(""); add_class(badge, "badge");
|
||||||
|
title_label = label("全部设备", "title");
|
||||||
|
more_button = button("⋮", "hbtn"); g_signal_connect(more_button, "clicked", G_CALLBACK(on_more), NULL);
|
||||||
|
gtk_box_pack_start(GTK_BOX(header), badge, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(header), title_label, FALSE, FALSE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(header), more_button, FALSE, FALSE, 0);
|
||||||
|
const char *icons[] = {"☰", "◉", "🔔"}, *kinds[] = {"menu", "account", "notice"};
|
||||||
|
for (int i = 0; i < 3; i++) { GtkWidget *b = button(icons[i], "hbtn"); g_signal_connect(b, "clicked", G_CALLBACK(popup), (gpointer)kinds[i]); gtk_box_pack_end(GTK_BOX(header), b, FALSE, FALSE, 0); }
|
||||||
|
gtk_box_pack_start(GTK_BOX(main), header, FALSE, FALSE, 0);
|
||||||
|
GtkWidget *overlay = gtk_overlay_new();
|
||||||
|
stack = gtk_stack_new();
|
||||||
|
gtk_stack_add_named(GTK_STACK(stack), build_welcome(), "welcome");
|
||||||
|
gtk_stack_add_named(GTK_STACK(stack), build_login(), "login");
|
||||||
|
gtk_stack_add_named(GTK_STACK(stack), build_device(), "device");
|
||||||
|
gtk_stack_add_named(GTK_STACK(stack), build_all(), "all");
|
||||||
|
gtk_stack_add_named(GTK_STACK(stack), build_assist(), "assist");
|
||||||
|
gtk_stack_add_named(GTK_STACK(stack), build_favorite(), "favorite");
|
||||||
|
gtk_container_add(GTK_CONTAINER(overlay), stack);
|
||||||
|
toast = gtk_label_new(""); add_class(toast, "toast"); gtk_widget_set_halign(toast, GTK_ALIGN_CENTER); gtk_widget_set_valign(toast, GTK_ALIGN_START); gtk_widget_set_margin_top(toast, 30);
|
||||||
|
gtk_overlay_add_overlay(GTK_OVERLAY(overlay), toast);
|
||||||
|
gtk_box_pack_start(GTK_BOX(main), overlay, TRUE, TRUE, 0);
|
||||||
|
gtk_box_pack_start(GTK_BOX(root), main, TRUE, TRUE, 0);
|
||||||
|
gtk_container_add(GTK_CONTAINER(window), root);
|
||||||
|
gtk_widget_show_all(window);
|
||||||
|
gtk_widget_hide(badge); gtk_widget_hide(more_button); gtk_widget_hide(toast); gtk_widget_hide(my_section);
|
||||||
|
gtk_stack_set_visible_child_name(GTK_STACK(stack), "welcome");
|
||||||
|
}
|
||||||
|
pthread_mutex_lock(&init_lock); init_state = ok ? 1 : -1; pthread_cond_signal(&init_cond); pthread_mutex_unlock(&init_lock);
|
||||||
|
if (ok) gtk_main();
|
||||||
|
atomic_store(&closed, 1);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int uu_viewer_ui_start(void) {
|
||||||
|
if (ui_started) return init_state == 1 ? 0 : -1;
|
||||||
|
ui_started = 1;
|
||||||
|
if (pthread_create(&ui_thread, NULL, run, NULL)) return -1;
|
||||||
|
pthread_mutex_lock(&init_lock); while (!init_state) pthread_cond_wait(&init_cond, &init_lock); pthread_mutex_unlock(&init_lock);
|
||||||
|
return init_state == 1 ? 0 : -1;
|
||||||
|
}
|
||||||
|
/// Lines of "id\tname\tplatform\tonline\tcurrent".
|
||||||
|
void uu_viewer_ui_set_devices(const char *tsv) {
|
||||||
|
pthread_mutex_lock(&d_lock);
|
||||||
|
device_count = 0;
|
||||||
|
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)) {
|
||||||
|
char *f[5] = {0}, *s2 = NULL; int n = 0;
|
||||||
|
for (char *p = strtok_r(line, "\t", &s2); p && n < 5; p = strtok_r(NULL, "\t", &s2)) f[n++] = p;
|
||||||
|
if (n < 5) continue;
|
||||||
|
Device *d = &devices[device_count++];
|
||||||
|
snprintf(d->id, sizeof d->id, "%s", f[0]); snprintf(d->name, sizeof d->name, "%s", f[1]);
|
||||||
|
d->platform = atoi(f[2]); d->online = atoi(f[3]); d->current = atoi(f[4]);
|
||||||
|
}
|
||||||
|
free(copy);
|
||||||
|
selected = -1;
|
||||||
|
pthread_mutex_unlock(&d_lock);
|
||||||
|
if (init_state == 1) g_idle_add(apply_devices, NULL);
|
||||||
|
}
|
||||||
|
void uu_viewer_ui_set_login(int signed_in, const char *qr_png, const char *status, const char *user) {
|
||||||
|
logged_in = signed_in;
|
||||||
|
snprintf(qr_path, sizeof qr_path, "%s", qr_png ? qr_png : "");
|
||||||
|
snprintf(status_text, sizeof status_text, "%s", status ? status : "");
|
||||||
|
snprintf(user_text, sizeof user_text, "%s", user ? user : "");
|
||||||
|
if (init_state == 1) g_idle_add(apply_login, NULL);
|
||||||
|
}
|
||||||
|
int uu_viewer_ui_take_command(char *out, size_t len) {
|
||||||
|
pthread_mutex_lock(&q_lock);
|
||||||
|
int has = q_len > 0;
|
||||||
|
if (has) { snprintf(out, len, "%s", queue[q_head]); memset(queue[q_head], 0, sizeof queue[q_head]); q_head = (q_head + 1) % 16; q_len--; }
|
||||||
|
pthread_mutex_unlock(&q_lock);
|
||||||
|
return has;
|
||||||
|
}
|
||||||
|
int uu_viewer_ui_closed(void) { return atomic_load(&closed); }
|
||||||
|
static char toast_text[256];
|
||||||
|
static gboolean apply_toast(gpointer d) { (void)d; show_toast(toast_text); return G_SOURCE_REMOVE; }
|
||||||
|
void uu_viewer_ui_toast(const char *text) {
|
||||||
|
snprintf(toast_text, sizeof toast_text, "%s", text ? text : "");
|
||||||
|
if (init_state == 1) g_idle_add(apply_toast, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int replace_result;
|
||||||
|
static gboolean confirm_replace(gpointer d) {
|
||||||
|
(void)d;
|
||||||
|
GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||||
|
GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL, "打开后将断开当前与该设备的连接,继续吗?");
|
||||||
|
int accepted = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK;
|
||||||
|
gtk_widget_destroy(dialog);
|
||||||
|
pthread_mutex_lock(&init_lock); replace_result = accepted; pthread_cond_signal(&init_cond); pthread_mutex_unlock(&init_lock);
|
||||||
|
return G_SOURCE_REMOVE;
|
||||||
|
}
|
||||||
|
int uu_viewer_ui_confirm_replace(void) {
|
||||||
|
pthread_mutex_lock(&init_lock); replace_result = -1; g_idle_add(confirm_replace, NULL);
|
||||||
|
while (replace_result < 0 && !atomic_load(&closed)) {
|
||||||
|
struct timespec until; clock_gettime(CLOCK_REALTIME, &until); until.tv_sec++;
|
||||||
|
pthread_cond_timedwait(&init_cond, &init_lock, &until);
|
||||||
|
}
|
||||||
|
int result = replace_result; pthread_mutex_unlock(&init_lock); return result;
|
||||||
|
}
|
||||||
+94
-32
@@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
static struct wl_display *display;
|
static struct wl_display *display;
|
||||||
|
static struct wl_registry *registry;
|
||||||
static struct wl_shm *shm;
|
static struct wl_shm *shm;
|
||||||
static struct wl_seat *seat;
|
static struct wl_seat *seat;
|
||||||
static struct zwlr_screencopy_manager_v1 *copy_manager;
|
static struct zwlr_screencopy_manager_v1 *copy_manager;
|
||||||
@@ -65,18 +66,22 @@ static int initialized;
|
|||||||
/* Independent slots let the compositor fill the next frame during delivery. */
|
/* Independent slots let the compositor fill the next frame during delivery. */
|
||||||
struct frame {
|
struct frame {
|
||||||
struct zwlr_screencopy_frame_v1 *handle;
|
struct zwlr_screencopy_frame_v1 *handle;
|
||||||
struct wl_buffer *buffer;
|
struct wl_buffer *buffer,*shm_buffer;
|
||||||
void *pixels;
|
struct wl_shm_pool *shm_pool;
|
||||||
size_t length;
|
void *pixels,*shm_pixels;
|
||||||
|
size_t shm_length;
|
||||||
|
int shm_fd,shm_busy;
|
||||||
|
uint32_t shm_width,shm_height,shm_stride,shm_format;
|
||||||
uint32_t width,height,stride,format,flags;
|
uint32_t width,height,stride,format,flags;
|
||||||
int done,failed,submitted;
|
int done,failed,submitted,released;
|
||||||
struct zwp_linux_buffer_params_v1 *params;
|
struct zwp_linux_buffer_params_v1 *params;
|
||||||
struct gbm_bo *bo;
|
struct gbm_bo *bo;
|
||||||
int bo_fd,dmabuf_used;
|
int bo_fd,dmabuf_used;
|
||||||
void *map_data;
|
void *map_data;
|
||||||
uint32_t dma_format,dma_width,dma_height;
|
uint32_t dma_format,dma_width,dma_height;
|
||||||
|
uint32_t bo_format,bo_width,bo_height;
|
||||||
};
|
};
|
||||||
static struct frame frames[2];
|
static struct frame frames[2]={{.shm_fd=-1,.bo_fd=-1},{.shm_fd=-1,.bo_fd=-1}};
|
||||||
static unsigned pending;
|
static unsigned pending;
|
||||||
|
|
||||||
static uint32_t timestamp(void) {
|
static uint32_t timestamp(void) {
|
||||||
@@ -177,15 +182,37 @@ static void dma_init(void) {
|
|||||||
gbm.fd=open(device && *device ? device:"/dev/dri/renderD128",O_RDWR|O_CLOEXEC);
|
gbm.fd=open(device && *device ? device:"/dev/dri/renderD128",O_RDWR|O_CLOEXEC);
|
||||||
if(gbm.fd<0 || !(gbm.device=gbm.create_device(gbm.fd))) dma_disable("render-device");
|
if(gbm.fd<0 || !(gbm.device=gbm.create_device(gbm.fd))) dma_disable("render-device");
|
||||||
}
|
}
|
||||||
|
static void shm_clear(struct frame *frame) {
|
||||||
|
if(frame->shm_buffer) wl_buffer_destroy(frame->shm_buffer);
|
||||||
|
if(frame->shm_pool) wl_shm_pool_destroy(frame->shm_pool);
|
||||||
|
if(frame->shm_pixels) munmap(frame->shm_pixels,frame->shm_length);
|
||||||
|
if(frame->shm_fd>=0) close(frame->shm_fd);
|
||||||
|
frame->shm_buffer=NULL; frame->shm_pool=NULL; frame->shm_pixels=NULL;
|
||||||
|
frame->shm_length=0; frame->shm_fd=-1; frame->shm_busy=0;
|
||||||
|
frame->shm_width=frame->shm_height=frame->shm_stride=frame->shm_format=0;
|
||||||
|
}
|
||||||
|
static void dma_clear(struct frame *frame) {
|
||||||
|
if(frame->buffer) wl_buffer_destroy(frame->buffer);
|
||||||
|
if(frame->dmabuf_used && frame->pixels) {
|
||||||
|
gbm.bo_unmap(frame->bo,frame->map_data); frame->pixels=NULL; frame->map_data=NULL;
|
||||||
|
}
|
||||||
|
if(frame->bo) { if(frame->bo_fd>=0)close(frame->bo_fd); gbm.bo_destroy(frame->bo); }
|
||||||
|
frame->buffer=NULL; frame->bo=NULL; frame->bo_fd=-1; frame->dmabuf_used=frame->released=0;
|
||||||
|
frame->bo_format=frame->bo_width=frame->bo_height=0;
|
||||||
|
}
|
||||||
static void frame_clear(struct frame *frame) {
|
static void frame_clear(struct frame *frame) {
|
||||||
if(frame->handle) zwlr_screencopy_frame_v1_destroy(frame->handle);
|
if(frame->handle) zwlr_screencopy_frame_v1_destroy(frame->handle);
|
||||||
if(frame->params) zwp_linux_buffer_params_v1_destroy(frame->params);
|
if(frame->params) zwp_linux_buffer_params_v1_destroy(frame->params);
|
||||||
if(frame->buffer) wl_buffer_destroy(frame->buffer);
|
|
||||||
if(frame->dmabuf_used && frame->pixels) gbm.bo_unmap(frame->bo,frame->map_data);
|
if(frame->dmabuf_used && frame->pixels) gbm.bo_unmap(frame->bo,frame->map_data);
|
||||||
else if(frame->pixels) munmap(frame->pixels,frame->length);
|
if(frame->shm_busy) shm_clear(frame);
|
||||||
if(frame->bo) { if(frame->bo_fd>=0)close(frame->bo_fd); gbm.bo_destroy(frame->bo); }
|
frame->handle=NULL; frame->params=NULL;
|
||||||
memset(frame,0,sizeof(*frame));
|
frame->pixels=NULL; frame->map_data=NULL; frame->dmabuf_used=0;
|
||||||
|
frame->width=frame->height=frame->stride=frame->format=frame->flags=0;
|
||||||
|
frame->done=frame->failed=frame->submitted=0;
|
||||||
|
frame->dma_format=frame->dma_width=frame->dma_height=0;
|
||||||
}
|
}
|
||||||
|
static void buffer_release(void*d,struct wl_buffer*b) { ((struct frame*)d)->released=1; }
|
||||||
|
static const struct wl_buffer_listener buffer_listener={buffer_release};
|
||||||
static void frame_fail(struct frame *frame) { frame->failed=frame->done=frame->submitted=1; }
|
static void frame_fail(struct frame *frame) { frame->failed=frame->done=frame->submitted=1; }
|
||||||
static void copy_buffer(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h,uint32_t s) {
|
static void copy_buffer(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h,uint32_t s) {
|
||||||
struct frame *frame=d;
|
struct frame *frame=d;
|
||||||
@@ -194,22 +221,32 @@ static void copy_buffer(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,ui
|
|||||||
(fmt!=WL_SHM_FORMAT_XRGB8888 && fmt!=WL_SHM_FORMAT_ARGB8888 &&
|
(fmt!=WL_SHM_FORMAT_XRGB8888 && fmt!=WL_SHM_FORMAT_ARGB8888 &&
|
||||||
fmt!=WL_SHM_FORMAT_XBGR8888 && fmt!=WL_SHM_FORMAT_ABGR8888)) { frame_fail(frame); return; }
|
fmt!=WL_SHM_FORMAT_XBGR8888 && fmt!=WL_SHM_FORMAT_ABGR8888)) { frame_fail(frame); return; }
|
||||||
size_t length=(size_t)s*h;
|
size_t length=(size_t)s*h;
|
||||||
int fd=memfd_create("uuremote-wayland-frame",MFD_CLOEXEC);
|
if (frame->shm_busy || !frame->shm_buffer || !frame->shm_pool || !frame->shm_pixels || frame->shm_fd<0 ||
|
||||||
if (fd<0 || ftruncate(fd,(off_t)length)!=0) { if(fd>=0)close(fd); frame_fail(frame); return; }
|
frame->shm_length!=length || frame->shm_width!=w || frame->shm_height!=h ||
|
||||||
frame->pixels=mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
|
frame->shm_stride!=s || frame->shm_format!=fmt) {
|
||||||
if (frame->pixels==MAP_FAILED) { frame->pixels=NULL; close(fd); frame_fail(frame); return; }
|
shm_clear(frame);
|
||||||
frame->length=length;
|
int fd=memfd_create("uuremote-wayland-frame",MFD_CLOEXEC);
|
||||||
struct wl_shm_pool *pool=wl_shm_create_pool(shm,fd,(int32_t)length);
|
if (fd<0 || ftruncate(fd,(off_t)length)!=0) { if(fd>=0)close(fd); frame_fail(frame); return; }
|
||||||
frame->buffer=wl_shm_pool_create_buffer(pool,0,w,h,s,fmt);
|
void *pixels=mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
|
||||||
wl_shm_pool_destroy(pool); close(fd);
|
if (pixels==MAP_FAILED) { close(fd); frame_fail(frame); return; }
|
||||||
|
struct wl_shm_pool *pool=wl_shm_create_pool(shm,fd,(int32_t)length);
|
||||||
|
if (!pool) { munmap(pixels,length); close(fd); frame_fail(frame); return; }
|
||||||
|
struct wl_buffer *buffer=wl_shm_pool_create_buffer(pool,0,w,h,s,fmt);
|
||||||
|
if (!buffer) { wl_shm_pool_destroy(pool); munmap(pixels,length); close(fd); frame_fail(frame); return; }
|
||||||
|
frame->shm_fd=fd; frame->shm_pixels=pixels; frame->shm_length=length;
|
||||||
|
frame->shm_pool=pool; frame->shm_buffer=buffer;
|
||||||
|
frame->shm_width=w; frame->shm_height=h; frame->shm_stride=s; frame->shm_format=fmt;
|
||||||
|
}
|
||||||
|
frame->pixels=frame->shm_pixels;
|
||||||
/* copy is valid immediately for SHM, including frame versions <3. */
|
/* copy is valid immediately for SHM, including frame versions <3. */
|
||||||
if (zwlr_screencopy_frame_v1_get_version(f)<3) {
|
if (zwlr_screencopy_frame_v1_get_version(f)<3) {
|
||||||
zwlr_screencopy_frame_v1_copy(f,frame->buffer); frame->submitted=1;
|
zwlr_screencopy_frame_v1_copy(f,frame->shm_buffer); frame->shm_busy=frame->submitted=1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static void copy_flags(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t value) { ((struct frame*)d)->flags=value; }
|
static void copy_flags(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t value) { ((struct frame*)d)->flags=value; }
|
||||||
static void copy_ready(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t hi,uint32_t lo,uint32_t ns) {
|
static void copy_ready(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t hi,uint32_t lo,uint32_t ns) {
|
||||||
struct frame *frame=d;
|
struct frame *frame=d;
|
||||||
|
frame->shm_busy=0;
|
||||||
if(frame->dmabuf_used) {
|
if(frame->dmabuf_used) {
|
||||||
/* GBM READ mapping synchronizes readback, possibly via a staging buffer. */
|
/* GBM READ mapping synchronizes readback, possibly via a staging buffer. */
|
||||||
uint32_t stride=0;
|
uint32_t stride=0;
|
||||||
@@ -223,7 +260,7 @@ static void copy_ready(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t hi,uint
|
|||||||
}
|
}
|
||||||
frame->done=1;
|
frame->done=1;
|
||||||
}
|
}
|
||||||
static void copy_failed(void*d,struct zwlr_screencopy_frame_v1*f) { frame_fail(d); }
|
static void copy_failed(void*d,struct zwlr_screencopy_frame_v1*f) { ((struct frame*)d)->shm_busy=0; frame_fail(d); }
|
||||||
static void copy_damage(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t x,uint32_t y,uint32_t w,uint32_t h) {}
|
static void copy_damage(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t x,uint32_t y,uint32_t w,uint32_t h) {}
|
||||||
static void copy_dmabuf(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h) {
|
static void copy_dmabuf(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h) {
|
||||||
struct frame *frame=d;
|
struct frame *frame=d;
|
||||||
@@ -232,30 +269,40 @@ static void copy_dmabuf(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,ui
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
static void submit_shm(struct frame *frame) {
|
static void submit_shm(struct frame *frame) {
|
||||||
if(frame->buffer && !frame->failed) {
|
if(frame->shm_buffer && !frame->shm_busy && !frame->failed) {
|
||||||
zwlr_screencopy_frame_v1_copy(frame->handle,frame->buffer); frame->submitted=1;
|
zwlr_screencopy_frame_v1_copy(frame->handle,frame->shm_buffer); frame->shm_busy=frame->submitted=1;
|
||||||
} else frame_fail(frame);
|
} else frame_fail(frame);
|
||||||
}
|
}
|
||||||
static void dma_created(void*d,struct zwp_linux_buffer_params_v1*params,struct wl_buffer*buffer) {
|
static void dma_created(void*d,struct zwp_linux_buffer_params_v1*params,struct wl_buffer*buffer) {
|
||||||
struct frame *frame=d;
|
struct frame *frame=d;
|
||||||
zwp_linux_buffer_params_v1_destroy(params); frame->params=NULL;
|
zwp_linux_buffer_params_v1_destroy(params); frame->params=NULL;
|
||||||
wl_buffer_destroy(frame->buffer);
|
frame->pixels=NULL;
|
||||||
munmap(frame->pixels,frame->length); frame->pixels=NULL; frame->length=0;
|
|
||||||
frame->buffer=buffer; frame->dmabuf_used=1;
|
frame->buffer=buffer; frame->dmabuf_used=1;
|
||||||
frame->width=frame->dma_width; frame->height=frame->dma_height;
|
frame->width=frame->dma_width; frame->height=frame->dma_height;
|
||||||
frame->format=frame->dma_format==0x34325258 ? WL_SHM_FORMAT_XRGB8888:
|
frame->format=frame->dma_format==0x34325258 ? WL_SHM_FORMAT_XRGB8888:
|
||||||
frame->dma_format==0x34325241 ? WL_SHM_FORMAT_ARGB8888:frame->dma_format;
|
frame->dma_format==0x34325241 ? WL_SHM_FORMAT_ARGB8888:frame->dma_format;
|
||||||
|
wl_buffer_add_listener(buffer,&buffer_listener,frame);
|
||||||
|
frame->released=0;
|
||||||
zwlr_screencopy_frame_v1_copy(frame->handle,buffer); frame->submitted=1;
|
zwlr_screencopy_frame_v1_copy(frame->handle,buffer); frame->submitted=1;
|
||||||
}
|
}
|
||||||
static void dma_failed(void*d,struct zwp_linux_buffer_params_v1*params) {
|
static void dma_failed(void*d,struct zwp_linux_buffer_params_v1*params) {
|
||||||
struct frame *frame=d;
|
struct frame *frame=d;
|
||||||
zwp_linux_buffer_params_v1_destroy(params); frame->params=NULL;
|
zwp_linux_buffer_params_v1_destroy(params); frame->params=NULL;
|
||||||
dma_disable("import"); submit_shm(frame);
|
dma_disable("import"); dma_clear(frame); submit_shm(frame);
|
||||||
}
|
}
|
||||||
static const struct zwp_linux_buffer_params_v1_listener dma_params_listener={dma_created,dma_failed};
|
static const struct zwp_linux_buffer_params_v1_listener dma_params_listener={dma_created,dma_failed};
|
||||||
static int dma_start(struct frame *frame) {
|
static int dma_start(struct frame *frame) {
|
||||||
if(!dmabuf_requested || dmabuf_disabled || !gbm.device || frame->failed || !frame->buffer ||
|
if(!dmabuf_requested || dmabuf_disabled || !gbm.device || frame->failed || !frame->shm_buffer ||
|
||||||
!(dmabuf_linear_formats&dma_format_bit(frame->dma_format))) return 0;
|
!(dmabuf_linear_formats&dma_format_bit(frame->dma_format))) return 0;
|
||||||
|
if(frame->buffer && frame->bo && frame->released && frame->bo_width==frame->dma_width &&
|
||||||
|
frame->bo_height==frame->dma_height && frame->bo_format==frame->dma_format) {
|
||||||
|
frame->pixels=NULL; frame->dmabuf_used=1; frame->released=0;
|
||||||
|
frame->width=frame->dma_width; frame->height=frame->dma_height;
|
||||||
|
frame->format=frame->dma_format==0x34325258 ? WL_SHM_FORMAT_XRGB8888:
|
||||||
|
frame->dma_format==0x34325241 ? WL_SHM_FORMAT_ARGB8888:frame->dma_format;
|
||||||
|
zwlr_screencopy_frame_v1_copy(frame->handle,frame->buffer); frame->submitted=1; return 1;
|
||||||
|
}
|
||||||
|
dma_clear(frame);
|
||||||
/* Single-plane linear RGB only; unsupported imports are asynchronous failures,
|
/* Single-plane linear RGB only; unsupported imports are asynchronous failures,
|
||||||
never create_immed protocol errors. No privilege or device probing changes. */
|
never create_immed protocol errors. No privilege or device probing changes. */
|
||||||
frame->bo=gbm.bo_create(gbm.device,frame->dma_width,frame->dma_height,frame->dma_format,(1u<<2)|(1u<<4));
|
frame->bo=gbm.bo_create(gbm.device,frame->dma_width,frame->dma_height,frame->dma_format,(1u<<2)|(1u<<4));
|
||||||
@@ -264,7 +311,8 @@ static int dma_start(struct frame *frame) {
|
|||||||
uint32_t stride=gbm.bo_get_stride(frame->bo);
|
uint32_t stride=gbm.bo_get_stride(frame->bo);
|
||||||
if(gbm.bo_get_plane_count(frame->bo)!=1 || gbm.bo_get_modifier(frame->bo)!=0 ||
|
if(gbm.bo_get_plane_count(frame->bo)!=1 || gbm.bo_get_modifier(frame->bo)!=0 ||
|
||||||
stride<frame->dma_width*4 || stride>131072 || (uint64_t)stride*frame->dma_height>256*1024*1024 ||
|
stride<frame->dma_width*4 || stride>131072 || (uint64_t)stride*frame->dma_height>256*1024*1024 ||
|
||||||
(frame->bo_fd=gbm.bo_get_fd(frame->bo))<0) {dma_disable("layout-or-export"); return 0;}
|
(frame->bo_fd=gbm.bo_get_fd(frame->bo))<0) {dma_disable("layout-or-export"); dma_clear(frame); return 0;}
|
||||||
|
frame->bo_format=frame->dma_format; frame->bo_width=frame->dma_width; frame->bo_height=frame->dma_height;
|
||||||
frame->params=zwp_linux_dmabuf_v1_create_params(dmabuf);
|
frame->params=zwp_linux_dmabuf_v1_create_params(dmabuf);
|
||||||
zwp_linux_buffer_params_v1_add_listener(frame->params,&dma_params_listener,frame);
|
zwp_linux_buffer_params_v1_add_listener(frame->params,&dma_params_listener,frame);
|
||||||
zwp_linux_buffer_params_v1_add(frame->params,frame->bo_fd,0,gbm.bo_get_offset(frame->bo,0),stride,0,0);
|
zwp_linux_buffer_params_v1_add(frame->params,frame->bo_fd,0,gbm.bo_get_offset(frame->bo,0),stride,0,0);
|
||||||
@@ -285,14 +333,25 @@ static void reset(void) {
|
|||||||
if (key_state) xkb_state_unref(key_state);
|
if (key_state) xkb_state_unref(key_state);
|
||||||
if (keymap) xkb_keymap_unref(keymap);
|
if (keymap) xkb_keymap_unref(keymap);
|
||||||
if (xkb) xkb_context_unref(xkb);
|
if (xkb) xkb_context_unref(xkb);
|
||||||
for(unsigned i=0;i<2;i++) frame_clear(&frames[i]);
|
for(unsigned i=0;i<2;i++) { frame_clear(&frames[i]); dma_clear(&frames[i]); shm_clear(&frames[i]); }
|
||||||
if(gbm.device) gbm.device_destroy(gbm.device);
|
if(gbm.device) gbm.device_destroy(gbm.device);
|
||||||
if(gbm.fd>=0) close(gbm.fd);
|
if(gbm.fd>=0) close(gbm.fd);
|
||||||
if(gbm.library) dlclose(gbm.library);
|
if(gbm.library) dlclose(gbm.library);
|
||||||
memset(&gbm,0,sizeof(gbm)); gbm.fd=-1;
|
memset(&gbm,0,sizeof(gbm)); gbm.fd=-1;
|
||||||
/* Disconnect drops every proxy; failure never leaves an initialized backend. */
|
if(keyboard) zwp_virtual_keyboard_v1_destroy(keyboard);
|
||||||
|
if(pointer) zwlr_virtual_pointer_v1_destroy(pointer);
|
||||||
|
if(power) zwlr_output_power_v1_destroy(power);
|
||||||
|
if(keyboard_manager) zwp_virtual_keyboard_manager_v1_destroy(keyboard_manager);
|
||||||
|
if(pointer_manager) zwlr_virtual_pointer_manager_v1_destroy(pointer_manager);
|
||||||
|
if(power_manager) zwlr_output_power_manager_v1_destroy(power_manager);
|
||||||
|
if(copy_manager) zwlr_screencopy_manager_v1_destroy(copy_manager);
|
||||||
|
if(dmabuf) zwp_linux_dmabuf_v1_destroy(dmabuf);
|
||||||
|
for(unsigned i=0;i<output_count;i++) if(outputs[i].handle) wl_output_destroy(outputs[i].handle);
|
||||||
|
if(seat) wl_seat_destroy(seat);
|
||||||
|
if(shm) wl_shm_destroy(shm);
|
||||||
|
if(registry) wl_registry_destroy(registry);
|
||||||
if (display) wl_display_disconnect(display);
|
if (display) wl_display_disconnect(display);
|
||||||
display=NULL; shm=NULL; seat=NULL; copy_manager=NULL; pointer_manager=NULL; keyboard_manager=NULL;
|
display=NULL; registry=NULL; shm=NULL; seat=NULL; copy_manager=NULL; pointer_manager=NULL; keyboard_manager=NULL;
|
||||||
pointer=NULL; keyboard=NULL; xkb=NULL; keymap=NULL; key_state=NULL;
|
pointer=NULL; keyboard=NULL; xkb=NULL; keymap=NULL; key_state=NULL;
|
||||||
power_manager=NULL; power=NULL; power_failed=0;
|
power_manager=NULL; power=NULL; power_failed=0;
|
||||||
dmabuf=NULL; dmabuf_requested=dmabuf_disabled=dmabuf_reported=0; dmabuf_linear_formats=0;
|
dmabuf=NULL; dmabuf_requested=dmabuf_disabled=dmabuf_reported=0; dmabuf_linear_formats=0;
|
||||||
@@ -305,7 +364,7 @@ int uu_wl_init(int32_t *w,int32_t *h) {
|
|||||||
int result=-1;
|
int result=-1;
|
||||||
const char *dma=getenv("UU_WAYLAND_DMABUF"); dmabuf_requested=dma && !strcmp(dma,"1");
|
const char *dma=getenv("UU_WAYLAND_DMABUF"); dmabuf_requested=dma && !strcmp(dma,"1");
|
||||||
display=wl_display_connect(NULL); if (!display) goto end;
|
display=wl_display_connect(NULL); if (!display) goto end;
|
||||||
struct wl_registry *registry=wl_display_get_registry(display);
|
registry=wl_display_get_registry(display);
|
||||||
wl_registry_add_listener(registry,®istry_listener,NULL);
|
wl_registry_add_listener(registry,®istry_listener,NULL);
|
||||||
if(sync_display()!=0 || sync_display()!=0) goto end;
|
if(sync_display()!=0 || sync_display()!=0) goto end;
|
||||||
dma_init();
|
dma_init();
|
||||||
@@ -347,6 +406,8 @@ end:
|
|||||||
}
|
}
|
||||||
int uu_wl_wake(void) {
|
int uu_wl_wake(void) {
|
||||||
pthread_mutex_lock(&lock); int result=-1;
|
pthread_mutex_lock(&lock); int result=-1;
|
||||||
|
/* Output power management is optional; missing it must not block screencopy. */
|
||||||
|
if(initialized && !power) result=0;
|
||||||
if(initialized && power && !power_failed) {
|
if(initialized && power && !power_failed) {
|
||||||
zwlr_output_power_v1_set_mode(power,ZWLR_OUTPUT_POWER_V1_MODE_ON);
|
zwlr_output_power_v1_set_mode(power,ZWLR_OUTPUT_POWER_V1_MODE_ON);
|
||||||
if(sync_display()==0 && !power_failed) result=0;
|
if(sync_display()==0 && !power_failed) result=0;
|
||||||
@@ -364,7 +425,8 @@ retry:;
|
|||||||
if(frame->failed && frame->dmabuf_used && !retried) {
|
if(frame->failed && frame->dmabuf_used && !retried) {
|
||||||
/* Mapping/copy failure retries once on SHM; connection errors do not. */
|
/* Mapping/copy failure retries once on SHM; connection errors do not. */
|
||||||
dma_disable("copy"); retried=1;
|
dma_disable("copy"); retried=1;
|
||||||
for(unsigned i=0;i<2;i++) frame_clear(&frames[i]);
|
/* dma-buf stays disabled, so its cached buffers are never reused. */
|
||||||
|
for(unsigned i=0;i<2;i++) { frame_clear(&frames[i]); dma_clear(&frames[i]); }
|
||||||
pending=0; goto retry;
|
pending=0; goto retry;
|
||||||
}
|
}
|
||||||
if(frame->failed || !frame->pixels || frame->width!=width || frame->height!=height) goto end;
|
if(frame->failed || !frame->pixels || frame->width!=width || frame->height!=height) goto end;
|
||||||
@@ -389,7 +451,7 @@ retry:;
|
|||||||
}
|
}
|
||||||
frame_clear(frame); pending=1-pending;
|
frame_clear(frame); pending=1-pending;
|
||||||
end:
|
end:
|
||||||
if(result!=0) for(unsigned i=0;i<2;i++) frame_clear(&frames[i]);
|
if(result!=0) for(unsigned i=0;i<2;i++) { frame_clear(&frames[i]); dma_clear(&frames[i]); }
|
||||||
pthread_mutex_unlock(&lock); return result;
|
pthread_mutex_unlock(&lock); return result;
|
||||||
}
|
}
|
||||||
int uu_wl_move(int32_t x,int32_t y,int relative) {
|
int uu_wl_move(int32_t x,int32_t y,int relative) {
|
||||||
|
|||||||
+70
-45
@@ -16,6 +16,7 @@
|
|||||||
#include <X11/extensions/Xfixes.h>
|
#include <X11/extensions/Xfixes.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <poll.h>
|
#include <poll.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
static Display *g_display = NULL;
|
static Display *g_display = NULL;
|
||||||
static Window g_root = 0;
|
static Window g_root = 0;
|
||||||
@@ -27,6 +28,8 @@ static int g_depth = 0;
|
|||||||
static XShmSegmentInfo g_shminfo;
|
static XShmSegmentInfo g_shminfo;
|
||||||
static XImage *g_shm_image = NULL;
|
static XImage *g_shm_image = NULL;
|
||||||
static int g_use_shm = 0;
|
static int g_use_shm = 0;
|
||||||
|
/* Guards the capture geometry and SHM image against a resize between connections. */
|
||||||
|
static pthread_mutex_t g_capture_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
static unsigned char g_injected_down[32]; // keycodes currently held by the remote viewer
|
static unsigned char g_injected_down[32]; // keycodes currently held by the remote viewer
|
||||||
static int g_injected_count = 0;
|
static int g_injected_count = 0;
|
||||||
static int g_restore_repeat = 0;
|
static int g_restore_repeat = 0;
|
||||||
@@ -52,6 +55,34 @@ static void track_injected_key(int keycode, int is_press) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void shm_open_image(void) {
|
||||||
|
g_use_shm = 0;
|
||||||
|
if (!XShmQueryExtension(g_display)) return;
|
||||||
|
g_shm_image = XShmCreateImage(g_display, DefaultVisual(g_display, g_screen),
|
||||||
|
g_depth, ZPixmap, NULL, &g_shminfo, g_width, g_height);
|
||||||
|
if (!g_shm_image) return;
|
||||||
|
g_shminfo.shmid = shmget(IPC_PRIVATE, g_shm_image->bytes_per_line * g_shm_image->height, IPC_CREAT | 0777);
|
||||||
|
if (g_shminfo.shmid == -1) return;
|
||||||
|
g_shminfo.shmaddr = g_shm_image->data = (char *)shmat(g_shminfo.shmid, 0, 0);
|
||||||
|
g_shminfo.readOnly = False;
|
||||||
|
if (g_shminfo.shmaddr != (char *)-1 && XShmAttach(g_display, &g_shminfo)) {
|
||||||
|
XSync(g_display, False);
|
||||||
|
g_use_shm = 1;
|
||||||
|
}
|
||||||
|
shmctl(g_shminfo.shmid, IPC_RMID, 0); // 标记删除,进程退出时自动回收;失败时也不遗留段
|
||||||
|
}
|
||||||
|
static void shm_close_image(void) {
|
||||||
|
if (g_use_shm) XShmDetach(g_display, &g_shminfo);
|
||||||
|
if (g_shm_image) {
|
||||||
|
g_shm_image->data = NULL; /* shm memory is released by shmdt, not XDestroyImage */
|
||||||
|
XDestroyImage(g_shm_image);
|
||||||
|
g_shm_image = NULL;
|
||||||
|
}
|
||||||
|
if (g_shminfo.shmaddr && g_shminfo.shmaddr != (char *)-1) shmdt(g_shminfo.shmaddr);
|
||||||
|
memset(&g_shminfo, 0, sizeof(g_shminfo));
|
||||||
|
g_use_shm = 0;
|
||||||
|
}
|
||||||
|
|
||||||
int x11_init(const char *display_name) {
|
int x11_init(const char *display_name) {
|
||||||
if (g_display) return 0;
|
if (g_display) return 0;
|
||||||
|
|
||||||
@@ -77,59 +108,60 @@ int x11_init(const char *display_name) {
|
|||||||
g_height = DisplayHeight(g_display, g_screen);
|
g_height = DisplayHeight(g_display, g_screen);
|
||||||
g_depth = DefaultDepth(g_display, g_screen);
|
g_depth = DefaultDepth(g_display, g_screen);
|
||||||
|
|
||||||
// 尝试初始化 XShm
|
shm_open_image();
|
||||||
if (XShmQueryExtension(g_display)) {
|
|
||||||
g_shm_image = XShmCreateImage(g_display, DefaultVisual(g_display, g_screen),
|
|
||||||
g_depth, ZPixmap, NULL, &g_shminfo, g_width, g_height);
|
|
||||||
if (g_shm_image) {
|
|
||||||
g_shminfo.shmid = shmget(IPC_PRIVATE, g_shm_image->bytes_per_line * g_shm_image->height, IPC_CREAT | 0777);
|
|
||||||
if (g_shminfo.shmid != -1) {
|
|
||||||
g_shminfo.shmaddr = g_shm_image->data = (char *)shmat(g_shminfo.shmid, 0, 0);
|
|
||||||
g_shminfo.readOnly = False;
|
|
||||||
if (g_shminfo.shmaddr != (char *)-1) {
|
|
||||||
if (XShmAttach(g_display, &g_shminfo)) {
|
|
||||||
XSync(g_display, False);
|
|
||||||
shmctl(g_shminfo.shmid, IPC_RMID, 0); // 标记删除,进程退出时自动回收
|
|
||||||
g_use_shm = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int x11_get_dimensions(int *out_w, int *out_h) {
|
int x11_get_dimensions(int *out_w, int *out_h) {
|
||||||
if (!g_display) return -1;
|
if (!g_display) return -1;
|
||||||
|
pthread_mutex_lock(&g_capture_lock);
|
||||||
*out_w = g_width;
|
*out_w = g_width;
|
||||||
*out_h = g_height;
|
*out_h = g_height;
|
||||||
|
pthread_mutex_unlock(&g_capture_lock);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* The startup size goes stale when RandR/xrdp resizes the root window. Query the
|
||||||
|
* server and rebuild the SHM image on change. Returns 1 if changed, 0 if not, <0 on error. */
|
||||||
|
int x11_refresh_geometry(void) {
|
||||||
|
if (!g_display) return -1;
|
||||||
|
Window root; int x, y; unsigned int w, h, border, depth;
|
||||||
|
if (!XGetGeometry(g_display, g_root, &root, &x, &y, &w, &h, &border, &depth) || !w || !h) return -2;
|
||||||
|
pthread_mutex_lock(&g_capture_lock);
|
||||||
|
int changed = (int)w != g_width || (int)h != g_height;
|
||||||
|
if (changed) {
|
||||||
|
shm_close_image();
|
||||||
|
g_width = (int)w; g_height = (int)h;
|
||||||
|
shm_open_image();
|
||||||
|
}
|
||||||
|
pthread_mutex_unlock(&g_capture_lock);
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 截取一帧画面(RGBA/BGRA 格式)
|
* 截取一帧画面(RGBA/BGRA 格式)
|
||||||
* @param out_buffer 输出缓冲区,大小必须至少为 width * height * 4
|
* @param out_buffer 输出缓冲区,大小必须至少为 width * height * 4
|
||||||
*/
|
*/
|
||||||
|
/* *out_w/*out_h carry the caller's buffer geometry in; a mismatch (desktop resized
|
||||||
|
* since that buffer was sized) returns -4 instead of overflowing it. */
|
||||||
int x11_capture_frame(unsigned char *out_buffer, int *out_w, int *out_h) {
|
int x11_capture_frame(unsigned char *out_buffer, int *out_w, int *out_h) {
|
||||||
if (!g_display) return -1;
|
if (!g_display) return -1;
|
||||||
|
pthread_mutex_lock(&g_capture_lock);
|
||||||
|
int result = 0;
|
||||||
|
if (*out_w != g_width || *out_h != g_height) result = -4;
|
||||||
|
else if (g_use_shm && g_shm_image) {
|
||||||
|
if (!XShmGetImage(g_display, g_root, g_shm_image, 0, 0, AllPlanes)) result = -2;
|
||||||
|
else memcpy(out_buffer, g_shm_image->data, (size_t)g_width * g_height * 4);
|
||||||
|
} else {
|
||||||
|
// Fallback: XGetImage
|
||||||
|
XImage *img = XGetImage(g_display, g_root, 0, 0, g_width, g_height, AllPlanes, ZPixmap);
|
||||||
|
if (!img) result = -3;
|
||||||
|
else { memcpy(out_buffer, img->data, (size_t)g_width * g_height * 4); XDestroyImage(img); }
|
||||||
|
}
|
||||||
*out_w = g_width;
|
*out_w = g_width;
|
||||||
*out_h = g_height;
|
*out_h = g_height;
|
||||||
|
pthread_mutex_unlock(&g_capture_lock);
|
||||||
if (g_use_shm && g_shm_image) {
|
return result;
|
||||||
if (!XShmGetImage(g_display, g_root, g_shm_image, 0, 0, AllPlanes)) {
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
memcpy(out_buffer, g_shm_image->data, g_width * g_height * 4);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback: XGetImage
|
|
||||||
XImage *img = XGetImage(g_display, g_root, 0, 0, g_width, g_height, AllPlanes, ZPixmap);
|
|
||||||
if (!img) return -3;
|
|
||||||
memcpy(out_buffer, img->data, g_width * g_height * 4);
|
|
||||||
XDestroyImage(img);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============ 原生输入注入 ============
|
// ============ 原生输入注入 ============
|
||||||
@@ -261,16 +293,9 @@ int x11_terminal_shortcut_state(void) {
|
|||||||
|
|
||||||
void x11_close() {
|
void x11_close() {
|
||||||
if (!g_display) return;
|
if (!g_display) return;
|
||||||
if (g_use_shm) {
|
pthread_mutex_lock(&g_capture_lock);
|
||||||
XShmDetach(g_display, &g_shminfo);
|
shm_close_image();
|
||||||
if (g_shm_image) {
|
pthread_mutex_unlock(&g_capture_lock);
|
||||||
XDestroyImage(g_shm_image);
|
|
||||||
g_shm_image = NULL;
|
|
||||||
}
|
|
||||||
if (g_shminfo.shmaddr && g_shminfo.shmaddr != (char *)-1) {
|
|
||||||
shmdt(g_shminfo.shmaddr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
XCloseDisplay(g_display);
|
XCloseDisplay(g_display);
|
||||||
g_display = NULL;
|
g_display = NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
[Desktop Entry]
|
||||||
|
Type=Application
|
||||||
|
Name=UU 远程 Linux 主机
|
||||||
|
Name[en]=UU Remote Linux Host
|
||||||
|
GenericName=Remote assistance host
|
||||||
|
Comment=非官方 UU 远程被控端:远程协助与扫码登录
|
||||||
|
Comment[en]=Unofficial NetEase UU Remote host: remote assistance and QR sign-in
|
||||||
|
Exec=uuremote-server show
|
||||||
|
Icon=uu-remote-linux
|
||||||
|
Terminal=false
|
||||||
|
Categories=Network;RemoteAccess;
|
||||||
|
Keywords=remote;assist;UU;远程;协助;
|
||||||
|
StartupNotify=false
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" width="128" height="128" viewBox="0 0 128 128">
|
||||||
|
<!-- uu-remote-linux app icon (original artwork, GPL-3.0 with the project): a desktop being reached remotely. -->
|
||||||
|
<defs>
|
||||||
|
<linearGradient id="bg" x1="0" y1="0" x2="1" y2="1">
|
||||||
|
<stop offset="0" stop-color="#22d3ee"/>
|
||||||
|
<stop offset="1" stop-color="#2563eb"/>
|
||||||
|
</linearGradient>
|
||||||
|
</defs>
|
||||||
|
<rect x="4" y="4" width="120" height="120" rx="28" fill="url(#bg)"/>
|
||||||
|
<rect x="26" y="32" width="76" height="52" rx="7" fill="none" stroke="#ffffff" stroke-width="7"/>
|
||||||
|
<path d="M52 98h24M64 84v14" stroke="#ffffff" stroke-width="7" stroke-linecap="round"/>
|
||||||
|
<path d="M46 70l22-22M54 48h14v14" fill="none" stroke="#ffffff" stroke-width="7" stroke-linecap="round" stroke-linejoin="round"/>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 794 B |
Binary file not shown.
|
Before Width: | Height: | Size: 84 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 51 KiB |
@@ -0,0 +1,27 @@
|
|||||||
|
远程协助(实验性)、统一主窗口和应用图标。/ Remote assistance (experimental), a unified main window and an app icon.
|
||||||
|
|
||||||
|
<p align="center"><img src="https://raw.githubusercontent.com/VallierDev/uu-remote-linux/v0.1.11/docs/images/main-window.png" width="560" alt="main window"></p>
|
||||||
|
|
||||||
|
### 新增 / Added
|
||||||
|
- **远程协助(实验性)**:和官方客户端一样,把「设备 ID + 验证码」发给别人,对方在 UU 远程的「开始协助」里输入即可连接本机。
|
||||||
|
- **未登录也能用**(访客身份);**登录后同样可用**,设备 ID 不变。
|
||||||
|
- 「允许他人远程协助」开关(默认关闭);验证方式:仅临时验证码 / 仅自定义验证码(8–16 位字母+数字)/ 两种都可以;
|
||||||
|
临时验证码刷新:每次远控结束 / 每小时 / 每 12 小时 / 每 24 小时 / 不刷新;ID 和验证码各有「复制」按钮,另有「复制并分享」。
|
||||||
|
- 协议参照官方 Windows 精简版被控端实现。✅ 用官方 Mac 客户端实测:访客连接、错误验证码被拒绝、用完自动换码、关闭后不可连接、
|
||||||
|
登录与退出登录的切换。⚠️ **登录状态下用另一个账号发起远程协助尚未实测**(同账号设备之间不能互相协助,这是官方限制)。
|
||||||
|
- 安全:拿到 ID 和验证码的人可以完全控制这台电脑,只发给信任的人;验证码只显示在本机窗口,不写入日志。
|
||||||
|
Remote assistance like the official client (device ID + code), before and after sign-in. Verified with the official
|
||||||
|
Mac viewer except signed-in assistance from another account.
|
||||||
|
- **统一主窗口**:一个窗口包含「本设备」(远程协助)和「账号」:未登录时显示扫码二维码,登录后显示账号(手机号打码)和「退出登录」。
|
||||||
|
扫码登录或退出登录后,主机会自动切换模式。已登录的后台服务默认隐藏窗口,用 `uuremote-server show` 或应用菜单打开;关闭窗口只是隐藏。
|
||||||
|
One window with remote assistance and the account (QR before sign-in, account + sign out after).
|
||||||
|
- **应用图标和启动项**(#13):`contrib/desktop/` 里有图标和 `.desktop` 文件,安装后应用菜单里会出现「UU 远程 Linux 主机」。
|
||||||
|
App icon and desktop launcher.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- 服务模式下扫码登录时,二维码(登录凭证)不再打印到标准输出(会进入系统日志)。/ The QR is no longer printed to a service's stdout.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.11-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
Mac 快捷键和在 Mac 上一样用。/ Mac shortcuts work as on a Mac.
|
||||||
|
|
||||||
|
### 新增 / Added
|
||||||
|
- **Mac 快捷键**:在 Mac 远控窗口菜单里**关闭「使用 macOS 快捷键」**后:
|
||||||
|
- Cmd+C / V / A / F / X / Z 是复制、粘贴、全选、查找、剪切、撤销(Cmd+Shift+Z 等组合同样可用);
|
||||||
|
- 在终端里 Cmd+C / Cmd+V 自动变成终端的复制、粘贴(Ctrl+Shift+C / V),**Ctrl+C 始终是中断**;
|
||||||
|
- 其他 Cmd 组合键和单独按 Cmd 仍然是 Super(Windows 键)。
|
||||||
|
只对 Mac 控制端生效(按连接请求里的控制端平台判断),Windows 和手机控制端不受影响。✅ 已用官方 Mac 客户端在 X11 实机验收。
|
||||||
|
保持该选项开启时行为和以前一样(客户端会把 Cmd 当成 Ctrl 发送)。
|
||||||
|
With 「使用 macOS 快捷键」 off, Cmd shortcuts behave as on a Mac (terminal-aware copy/paste), only for macOS viewers.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.12-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
托盘图标。/ Tray icon.
|
||||||
|
|
||||||
|
### 新增 / Added
|
||||||
|
- **托盘图标**:后台运行时,顶栏/任务栏会显示托盘图标,菜单里有「打开主窗口」、「允许他人远程协助」(与主窗口开关同步)和「退出」。
|
||||||
|
- GNOME 通过 AppIndicator 显示(Ubuntu 自带扩展;运行时加载 libayatana-appindicator,无需额外编译依赖);Xfce / Cinnamon / KDE 等使用系统托盘。
|
||||||
|
- 「退出」会停止 `uuremote-native.service`(不会被自动重启);之后从应用菜单「UU 远程 Linux 主机」打开,会重新启动服务并显示主窗口。
|
||||||
|
✅ Ubuntu 24.04 GNOME(X11)实机验收:图标、菜单、开关同步、退出、从应用菜单重新启动。⚠️ Xfce 等系统托盘路径尚未实机验收。
|
||||||
|
Tray icon with open / allow / quit; Quit stops the user service and the launcher restarts it. Verified on Ubuntu GNOME.
|
||||||
|
|
||||||
|
### 改进 / Improved
|
||||||
|
- `uuremote-server show`:主机没在运行时,优先启动已安装的 `uuremote-native.service`,而不是另起一个不受管理的进程。
|
||||||
|
- 修复启动时的一个时序问题:先注册窗口唤起信号再写入 PID 文件,避免过早的 `show` 意外结束主机进程。
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.13-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
修复手机切回终端时黑屏。/ Fixes a black terminal after returning to it on mobile.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- **终端历史输出较多时,重连后黑屏**(#14,感谢 @cnetst):手机客户端切到后台再回到同一个终端时,如果之前积累的输出较多,
|
||||||
|
会整屏黑屏,连新建终端也没有输出,只能完全退出 App 重连。原因是重连时把最多 256 KB 的历史输出作为一条消息发送,
|
||||||
|
超过了 WebRTC 数据通道单条消息 64 KB 的上限,发送失败后整个终端连接卡住。现在历史输出按 16 KB 分段发送,顺序不变。
|
||||||
|
✅ 已用官方手机客户端在 3090 实机验收。
|
||||||
|
Terminal scrollback is replayed in 16 KiB chunks instead of one message over the 64 KiB SCTP limit. Verified on mobile.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.14-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
支持真彩(4:4:4)。/ True color (4:4:4) support.
|
||||||
|
|
||||||
|
### 新功能 / Added
|
||||||
|
- **真彩(4:4:4)**:在客户端画面菜单打开「真彩」后,主机改用 H.264 High 4:4:4 编码,彩色细字(红字、链接、
|
||||||
|
代码高亮)边缘不再发虚;关闭后切回 4:2:0。适用于默认的 FFmpeg 管线(NVENC 或 libx264);VAAPI 和
|
||||||
|
`--features inproc-av` 管线仍为 4:2:0,并如实告知客户端。
|
||||||
|
✅ 已用官方 Mac 客户端在 RTX 3090(NVENC)上实机验证:开关来回切换正常、画面正常解码。
|
||||||
|
When 真彩 is enabled the host encodes H.264 High 4:4:4 (default FFmpeg pipeline with NVENC or libx264); VAAPI and
|
||||||
|
inproc-av stay 4:2:0 and report it. Verified with the official Mac viewer on NVENC (toggling, decoding).
|
||||||
|
- 日志记录客户端声明的解码能力(编码类型、色度、最大分辨率、帧率,仅数字),便于排查画质问题。
|
||||||
|
The log now shows the viewer's declared decoder capabilities (numbers only).
|
||||||
|
|
||||||
|
### 说明 / Notes
|
||||||
|
- 4:4:4 码率会略高于 4:2:0;在缩放到较低分辨率时差别不明显,建议配合「跟随远端」分辨率使用。
|
||||||
|
4:4:4 costs some bitrate; the difference is hardly visible when the stream is scaled down.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.15-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
支持 H.265(HEVC)。/ H.265 (HEVC) support.
|
||||||
|
|
||||||
|
### 新功能 / Added
|
||||||
|
- **H.265**:控制端和主机都支持时自动使用 H.265,同等画质下码率更低。与官方主机一致,由主机按双方能力选择编码
|
||||||
|
(官方 Mac 客户端没有手动切换编码的菜单)。连接时主机会告知客户端自己能编哪些格式(CodecNegotiation)。
|
||||||
|
支持 `hevc_nvenc`、`libx265` 和显式 VAAPI 的 `hevc_vaapi`;硬件失败先回落软件 HEVC,再回落 H.264。
|
||||||
|
需要时用 `UU_VIDEO_CODEC=h264` 强制使用 H.264。
|
||||||
|
- 真彩(4:4:4)同样适用于 H.265(hevc_nvenc `rext` / libx265 `main444-8`)。
|
||||||
|
✅ 已用官方 Mac 客户端在 RTX 3090(hevc_nvenc,含 4:4:4)实机验收:画面正常、重连正常、无丢帧。
|
||||||
|
⚠️ libx265、hevc_vaapi、Windows/手机客户端尚未实机验证;如有问题请设置 `UU_VIDEO_CODEC=h264` 并提 issue。
|
||||||
|
The host now uses H.265 when both sides support it (the host picks the codec, like the official host), announces
|
||||||
|
its encoders with CodecNegotiation, and falls back to software HEVC, then H.264. `UU_VIDEO_CODEC=h264` forces H.264.
|
||||||
|
Verified with the official Mac viewer on NVENC including 4:4:4; other encoders and viewers are unverified.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- 日志新增客户端 offer 中的编码列表与最终协商结果(仅编码名),便于排查画面问题。
|
||||||
|
The log shows offered and negotiated video codecs (names only).
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.16-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
修复终端接回错位、低码率 CPU 编码灰屏、niri 无法建流。/ Terminal reattach, low-bitrate CPU encoding and niri fixes.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- **终端接回后全屏程序画面错位**(#17):接回已有会话时,主机先把终端行数临时减一、稍后恢复,触发 tmux、vim、
|
||||||
|
Claude Code 等全屏程序整屏重画。
|
||||||
|
⚠️ 已通过单元测试;尚未用 Android 客户端实机验证,如仍错位请在 #17 反馈。
|
||||||
|
Reattaching a terminal session now nudges the PTY size so full-screen programs repaint. Unit-tested; not yet
|
||||||
|
verified with the Android viewer.
|
||||||
|
- **低码率档周期性灰屏**(#15,感谢 @mengdehong):外部 FFmpeg 的 CPU 编码(libx264,以及同样处理的 libx265)在
|
||||||
|
码率档(原画 / 超清 / 高清 / 自定义)改用平均码率(ABR),避免 4K 低码率下关键帧耗尽缓冲而发灰;「自动」档仍用 CRF。
|
||||||
|
libx264 修复由贡献者在 Android 客户端实测;libx265 只验证了参数可用。
|
||||||
|
CPU encoding uses ABR for bitrate presets (CRF for Auto), fixing gray keyframes at 4K and low bitrates.
|
||||||
|
- **niri 等合成器无法建流**(#15):原生 Wayland 后端在合成器没有 `wlr-output-power-management` 时跳过唤醒屏幕,
|
||||||
|
不再中断采集。Capture continues when the compositor lacks output power management.
|
||||||
|
|
||||||
|
### 文档 / Docs
|
||||||
|
- Wayland 会话的 systemd 单元写法、uinput 模块开机加载(#16,感谢 @meotao 的实机测量与建议);未编入 `wayland`
|
||||||
|
特性时的报错改为明确提示重新编译。
|
||||||
|
Wayland systemd unit, uinput autoload, and a clearer error when the binary lacks the `wayland` feature.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.17-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
修复分辨率变化后无法出画面;Wayland 采集提速;终端接回修复。/ Resolution change fix, faster Wayland capture, terminal fixes.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- **服务运行中桌面分辨率变化后,新连接一直停在「正在传输画面」**(#18,感谢 @liangjiahui01):每次建立桌面连接前重新读取
|
||||||
|
X11 桌面尺寸,变化时重建截屏;截屏尺寸不符时报明确错误,不再误报「HEVC unavailable」。
|
||||||
|
已在隔离 Xvfb 上用 `xrandr` 改尺寸验证;xrdp 场景未实测。连接期间改分辨率暂不支持(需重连)。
|
||||||
|
X11 now re-reads the desktop size before each connection, so a RandR/xrdp resize no longer stalls new connections.
|
||||||
|
- **终端接回**(#17 后续):修复延迟恢复尺寸可能覆盖新尺寸的竞态,以及接回时同一段输出既在回放里又被实时重发的问题。
|
||||||
|
Fixed a size-restore race and duplicated output on terminal reattach.
|
||||||
|
|
||||||
|
### 改进 / Improved
|
||||||
|
- **原生 Wayland 采集**(#16,感谢 @meotao 的实机测量):采集进程直接写入 FFmpeg,不再经主进程中转 4K 帧;采集与写管道
|
||||||
|
双线程交替,管道缓冲扩大;SHM 截屏缓冲复用,不再每帧分配。
|
||||||
|
⚠️ 只做了编译和单元测试,没有在真机上测到提速效果;请求高帧率时的实际帧率欢迎在 #16 反馈。
|
||||||
|
Native Wayland: capture pipes straight into FFmpeg, overlaps capture with writes and reuses SHM buffers. Not yet
|
||||||
|
measured on real hardware.
|
||||||
|
- 视频统计日志新增本窗口的 `window_s`、`window_sent`、`window_skipped`(原 `sent`/`skipped` 仍为累计值)。
|
||||||
|
The latest-frame stats line adds per-window counts.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.18-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
Wayland 后端需自行用 `--features wayland` 编译。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
信令断线检测:主机休眠恢复或网络悄悄断开后自动重新上线。/ Detect dead signaling connections and reconnect.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- **主机显示在线但控制端看不到、只能手动重启服务**(#19,感谢 @Keinns):之前只有收到关闭帧或读错误才判定信令断开;
|
||||||
|
系统休眠恢复、NAT 超时等造成的半开连接会让主机一直以为自己在线。现在按 Engine.IO 协议,超过服务器下发的
|
||||||
|
`pingInterval + pingTimeout`(目前约 33 秒)没收到心跳就判定断线,走已有的自动重连。日志:`Signaling heartbeat lost`。
|
||||||
|
已用模拟的「不发心跳」信令服务器单元测试验证;真实休眠恢复场景未实测。
|
||||||
|
Signaling now treats a connection without pings for pingInterval + pingTimeout as dead and reconnects.
|
||||||
|
Unit-tested against a silent mock server; real suspend/resume not yet measured.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.19-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
登录诊断与系统证书支持。/ Login diagnostics and system certificate support.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- **企业代理 / 自定义 CA 环境下登录失败**(#21,感谢 @liuji10000qian-svg):除内置根证书外,现在也信任系统证书库,
|
||||||
|
并遵循 `SSL_CERT_FILE` / `SSL_CERT_DIR`。会做 TLS 拦截的代理(使用自签 CA)此前会导致设备注册请求失败。
|
||||||
|
Trust the system certificate store and honor `SSL_CERT_FILE` / `SSL_CERT_DIR` in addition to the bundled roots.
|
||||||
|
- **网络请求失败时只显示「error sending request」**:错误信息现在包含底层原因链(证书、代理、DNS、超时等),
|
||||||
|
并去掉 URL 中的用户名密码(如代理凭据)和查询参数。
|
||||||
|
Transport errors now include their cause chain, with URL credentials and query strings removed.
|
||||||
|
|
||||||
|
⚠️ 自定义 CA / 代理环境未实测,请在 #21 反馈新版本的错误信息。
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.20-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
视频管线减少重复分配和过量编码。/ Fewer allocations and less wasted encoding in the video pipeline.
|
||||||
|
|
||||||
|
### 改进 / Improved(#20,感谢 @mengdehong)
|
||||||
|
- 采集到发送之间最多 4 帧在途:网络发不出去时采集端自动放慢,不再白白编码无法送出的帧。
|
||||||
|
贡献者实验(发送受阻场景):编码 CPU 耗时降低约 88%。
|
||||||
|
- 原生 Wayland 可选的 dma-buf 路径、Portal / KMS 路径复用采集缓冲;Portal 同一帧不重复复制。
|
||||||
|
- X11 进程内编码(`inproc-av`,需自行编译)缓存静态画面的采集与转色结果:静态画面每帧 CPU 降低约 40%。
|
||||||
|
- 原生 Wayland 关闭时完整释放协议对象。
|
||||||
|
|
||||||
|
⚠️ 以上性能数据来自贡献者在隔离 headless Sway / Xvfb 中的合成画面实验,未在维护者的真机上复测。
|
||||||
|
预编译的 X11 包默认走外部 FFmpeg 管线,受影响的主要是 Wayland / Portal / KMS / `inproc-av` 自编译用户。
|
||||||
|
Performance figures come from the contributor's headless Sway/Xvfb experiments; not re-measured on maintainer hardware.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.21-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
v0.1.21 视频管线改动的两处修正。/ Two follow-up fixes to the v0.1.21 video pipeline changes.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- 原生 Wayland 的 dma-buf 映射失败、回退到 SHM 时,已缓存的 dma-buf 缓冲没有释放(最多两个槽位,到会话结束才回收)。
|
||||||
|
Release cached dma-buf buffers when capture falls back to SHM.
|
||||||
|
- 原生 Wayland 在发送受阻(背压)解除后的第一帧可能是受阻前预取的旧画面;现在等待超过两个帧间隔时会重新采集。
|
||||||
|
After a backpressure stall the first native Wayland frame is captured fresh instead of reusing the stale prefetch.
|
||||||
|
|
||||||
|
两处均由代码审查发现,只做了编译和单元测试,未在真机复现。
|
||||||
|
Both found in review; compile- and unit-tested only.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.22-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
测量工具:`desktop-video-probe` 可选码率档。/ Probe bitrate presets.
|
||||||
|
|
||||||
|
### 新增 / Added
|
||||||
|
- `desktop-video-probe` 支持 `UU_PROBE_QUALITY=1..5`,对应控制端画质档(1 极速 1M、2 流畅 2M、3 高清 8M、4 蓝光 20M、5 自动)。
|
||||||
|
码率档走平均码率(ABR)路径,便于不用控制端复测 #15 的灰屏修复(#9 的建议)。
|
||||||
|
`desktop-video-probe` accepts `UU_PROBE_QUALITY=1..5` (viewer bitrate presets; 5 = Auto/CRF).
|
||||||
|
|
||||||
|
### 其他 / Other
|
||||||
|
- 修复一个在多网卡机器上偶发超时的编码协商单元测试。
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.23-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
支持 Ubuntu 22.04 自带的 FFmpeg 4.4。/ Works with Ubuntu 22.04's FFmpeg 4.4.
|
||||||
|
|
||||||
|
### 改进 / Improved
|
||||||
|
- **自动适配 FFmpeg 版本**(#22,感谢 @Guancheng-Li):启动编码前检测所用 FFmpeg 支持的参数。FFmpeg 5 及以上继续使用
|
||||||
|
`-fps_mode vfr -enc_time_base 1/<fps>`;FFmpeg 4.x(如 Ubuntu 22.04)改用 `-vsync 0`。检测失败时保持新版参数。
|
||||||
|
FFmpeg 4.x 下采集卡顿后的补帧会全部编码(新版会丢弃),属于已知降级。贡献者已在 FFmpeg 4.4.2 上验证。
|
||||||
|
The host probes the FFmpeg it uses and falls back to `-vsync 0` only on FFmpeg 4.x; probe failures keep the modern arguments.
|
||||||
|
|
||||||
|
### 其他 / Other
|
||||||
|
- 新增几条只记结构的诊断日志(收到的 SOAC offer 结构、SDP 段落属性名、ice_id 是否一致),用于排查与官方控制端的互通,
|
||||||
|
不记录 SDP 内容或任何 ID。
|
||||||
|
Structure-only diagnostics for incoming offers (no SDP contents or ids).
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.24-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
GNOME Wayland 缩放下的鼠标修复。/ Pointer fix for scaled GNOME Wayland desktops.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- **GNOME Wayland 开启桌面缩放后,远程鼠标只能控制左上部分**(#23、#24,感谢 @wanglz111):Portal 后端在同一授权会话里
|
||||||
|
改用 EIS 输入,按 `mapping_id` 匹配屏幕区域,并按实际帧尺寸映射坐标;缺少映射信息或 Portal 不支持 EIS 时保留原路径。
|
||||||
|
日志不记录输入内容。X11、原生 wlroots 和 uinput 路径不变。
|
||||||
|
⚠️ 贡献者在此前的迭代上实测过 Ubuntu 26.04 / GNOME 50、单屏 4K、200% 缩放;最终提交、其他缩放比例、锁屏恢复、
|
||||||
|
断线重连和多屏尚未实机验证。
|
||||||
|
Portal input uses EIS regions mapped to the captured frame; the final commit and other scales are not yet hardware-verified.
|
||||||
|
|
||||||
|
### 构建 / Build
|
||||||
|
- `--features wayland,portal` 现在需要 `libei >= 1.1` 开发包(Debian/Ubuntu:`libei-dev`)。预编译的 X11 包不受影响。
|
||||||
|
Portal builds now require libei >= 1.1.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.25-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
大目录文件传输修复、上报实际刷新率、NVENC 省掉 CPU 颜色转换。/ Large-folder file transfer fix, real refresh rate, NVENC without CPU color conversion.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- **文件传输打开文件很多的目录时一直转圈**:目录回复同时带了未压缩和压缩两份列表,几千个文件时超过数据通道单条消息上限,
|
||||||
|
回复被丢弃。现在压缩列表始终发送,未压缩副本只在放得下时附带;同时把 SCTP 发送上限从 64 KB 提到 256 KB
|
||||||
|
(与官方控制端在 SDP 中声明的一致)。已在 5600 个文件的目录上实测。
|
||||||
|
Large directory listings no longer exceed the data-channel message limit (verified with 5600 entries).
|
||||||
|
|
||||||
|
### 改进 / Improved
|
||||||
|
- **上报显示器实际刷新率**(#16):`display_info`、能力通知和编解码协商原先固定报 60,现在取当前显示模式
|
||||||
|
(Hyprland 用 `hyprctl`,X11 用 `xrandr`;取不到时仍为 60,`UU_DISPLAY_FPS` 可手动指定)。
|
||||||
|
The host reports the monitor's refresh rate instead of a fixed 60 (`UU_DISPLAY_FPS` overrides).
|
||||||
|
- **NVENC 直接接收 BGRA/BGR0**(#16):4:2:0 且不缩放时,颜色转换交给 GPU,省掉 FFmpeg 子进程里的 CPU 转换;
|
||||||
|
色彩标记与原来一致(BT.601 有限范围)。`UU_NVENC_RGB=0` 可恢复原路径。
|
||||||
|
⚠️ 已在 X11 + RTX 3090 上实测画面与颜色;Hyprland 4K 高帧率下的 CPU/帧率收益尚未实测。
|
||||||
|
NVENC converts RGB on the GPU; verified on X11, the Hyprland 4K gain is not yet measured.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.26-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
GNOME Wayland 连接中切换缩放后的停帧修复。/ Recover from live scaling changes on GNOME Wayland.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- **GNOME Wayland 连接中切换显示缩放后画面持续冻结**(#25、#26,感谢 @wanglz111):已推流的 Portal 流若「暂停且没有有效格式」
|
||||||
|
持续 5 秒,复用现有会话恢复;恢复时允许逻辑尺寸变化,仍校验采集帧尺寸。启动阶段和静态画面不会触发这个超时,
|
||||||
|
等待授权与 D-Bus 的过程不再占用输入锁。
|
||||||
|
⚠️ 贡献者实测:Ubuntu 26.04 + GNOME Wayland、单屏 3840×2160,连接中 200%→166.67%→200% 恢复后画面和全屏鼠标正常;
|
||||||
|
**切换时会短暂停顿、黑屏后恢复,并非无缝;5 秒是判定阈值,不是总恢复时间**。物理分辨率变化、多屏、锁屏恢复未实机验证。
|
||||||
|
Portal recovers after a live scale change (brief freeze/black, not seamless); physical resolution changes remain unsupported.
|
||||||
|
- Portal 失效检查出错时只记日志,不再让主机主循环退出。/ A failed Portal staleness check no longer ends the host loop.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.27-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
GNOME/KDE Wayland 的 Portal 后端需自行用 `--features wayland,portal` 构建(需要 libei-dev ≥ 1.1)。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
编码探针支持固定输入,测试更稳定。/ Fixed-input encoder probe and deterministic WebRTC tests.
|
||||||
|
|
||||||
|
### 新增 / Added
|
||||||
|
- **`desktop-video-probe` 支持固定输入**(#9,感谢 @hiclaw 的建议):设置 `UU_PROBE_INPUT=<视频或图片序列>`(如 `clip.y4m`、
|
||||||
|
`clip.mp4`、`frames/%05d.png`)时不采集桌面,而是用与生产会话相同的 FFmpeg 编码参数(编码器、码率档、GOP、色度、缩放)
|
||||||
|
编码该输入,消除桌面内容对测量的影响;`UU_PROBE_LOOP=1` 循环输入直到 `UU_PROBE_FRAMES`。输入打不开或帧数不足时非零退出。
|
||||||
|
正常会话路径不变。已用 Y4M、MP4、PNG 序列验证,重复运行输出逐字节一致。⚠️ NVENC/VAAPI 下的固定输入未实机验证。
|
||||||
|
`desktop-video-probe` can encode a fixed input (`UU_PROBE_INPUT`) with the production encoder arguments.
|
||||||
|
|
||||||
|
### 内部 / Internal
|
||||||
|
- 本地 WebRTC 单测改用虚拟网络,修复全量并行测试时偶发的 30 秒超时。/ Local WebRTC tests run on a virtual network.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.28-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
GNOME Wayland 锁屏时也能连接并自动解锁。/ Connect and auto unlock while GNOME Wayland is locked.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- **GNOME Wayland(Portal)锁屏冷连接**(#28,感谢 @wanglz111,#30):服务在已解锁时启动、随后锁屏,再从客户端连接时,
|
||||||
|
以前因为 Portal 无法创建而整个连接被丢弃,无法远程自动解锁。现在先建立控制通道、上报锁屏状态并处理「自动解锁被控端」,
|
||||||
|
Portal 就绪后再恢复画面、输入和剪贴板;锁屏期间不发画面、不注入桌面输入,并唤醒显示器(不会解锁)。
|
||||||
|
官方 **Mac 客户端**已实机验证(锁屏 → 连接 → 自动解锁 → 画面和输入恢复)。
|
||||||
|
- **iPhone 锁屏冷连接自动解锁**(#29,感谢 @wanglz111,#34):iPhone 4.42.0 要先收到视频帧才会发起自动解锁。Portal 等待就绪且
|
||||||
|
确认锁屏时,主机每秒在已协商的视频轨上发一帧生成的黑画面(需 FFmpeg 带 libx264/libx265),解锁后由真实画面接续。
|
||||||
|
贡献者用同一二进制做了 开→关→开 对照,iPhone 4.42.0 + H.265 实机通过;⚠️ H.264 手机实机、其他客户端版本、慢速中继未验收。
|
||||||
|
黑帧不是真实锁屏画面,锁屏界面不能操作键鼠;未开启「自动解锁被控端」时无法远程解锁(控制中心 → 安全 → 自动解锁被控端)。
|
||||||
|
Locked cold connections on GNOME Wayland now establish CONTROL first and support auto unlock (Mac verified); black video
|
||||||
|
while locked lets iPhone 4.42.0 start auto unlock (H.265 verified by the contributor).
|
||||||
|
- 断开事件只关闭对应 `ice_id` 的连接,不再误关同时打开的文件传输、终端、端口映射连接。
|
||||||
|
Releases close only the matching peer; tool peers stay open.
|
||||||
|
- X11 主机回归:控制通道处理改动后,已在 X11 + NVENC 主机上实测连接、Echo、锁屏状态上报和画面;画质切换由自动化测试覆盖。
|
||||||
|
X11 host re-checked live (connection, Echo, lock state, video).
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.29-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
修复未登录(访客)远程协助时 X11 没有画面。/ Fix missing video during guest remote assistance on X11.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- **X11 访客远程协助无画面**:未登录账号、以访客身份开启远程协助时,设备初始化过程会临时打开再关闭一次屏幕采集,
|
||||||
|
把正在使用的 X11 连接一起关掉,日志出现 `Live desktop stream stopped: X11 display wake failed`,控制端连上后一直黑屏。
|
||||||
|
现在 X11 连接按引用计数管理,只有最后一个采集对象释放时才关闭;X11 分辨率变化后重建采集也不再有同样的隐患。
|
||||||
|
已在虚拟机(Xvfb 虚拟桌面)上实测:访客协助加入、画面、鼠标移动/点击、键盘均正常。⚠️ 未用官方客户端在真机上复测。
|
||||||
|
Guest assistance on X11 no longer loses the display connection; video, mouse and keyboard verified in a VM.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.30-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
GNOME 修改缩放后画面恢复更快。/ Faster capture recovery after GNOME display changes.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- **GNOME Wayland 改缩放后黑屏/停顿时间缩短**(#27,感谢 @wanglz111,#35):以前切换缩放(如 200% ↔ 166.67%)后,Portal 采集要等
|
||||||
|
5 秒兜底才恢复。现在监听 Mutter 的显示配置变化通知,采集持续「暂停且无有效格式」超过 200 毫秒宽限即提前进入原有恢复流程;
|
||||||
|
正常重协商会取消提前判定,连续切换只恢复一次,5 秒兜底保留。贡献者在 Ubuntu 26.04、GNOME 50、单屏 3840×2160 上实测:
|
||||||
|
通知到会话恢复从约 6.2 秒降到约 1.4 秒。⚠️ 200 毫秒是在该机器上测得的初始值;官方客户端侧的画面恢复耗时、多屏、
|
||||||
|
物理分辨率变化与旋转未验收。X11 与其他桌面不受影响。
|
||||||
|
On GNOME, Mutter `MonitorsChanged` lets a stalled Portal capture recover after a 200 ms grace instead of the 5 s fallback
|
||||||
|
(host-side recovery about 6.2 s → 1.4 s on the contributor's 4K display).
|
||||||
|
|
||||||
|
### 内部 / Internal
|
||||||
|
- CONTROL 生命周期单测改用虚拟网络,不再因本机网卡环境偶发超时。/ CONTROL lifecycle tests run on the virtual network.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.1.31-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
新增实验性 Linux 控制端 `uuremote-viewer`。/ Experimental Linux viewer `uuremote-viewer`.
|
||||||
|
|
||||||
|
### 新增 / Added
|
||||||
|
- **🧪 原生控制端 `uuremote-viewer`**(#36):在 Linux 上用 UU 账号连接并控制其他设备——官方 Windows / Mac 被控端或本项目的 Linux 主机,
|
||||||
|
也可凭对方的设备 ID 和验证码发起远程协助。包含启动窗口(设备列表)、桌面窗口(H.264 画面、键鼠、声音、文本剪贴板、只看模式)、
|
||||||
|
文件传输、终端与终端会话列表、端口映射(只监听 127.0.0.1)、重启/关机。与主机 `uuremote-server` 相互独立,**主机行为不变**。
|
||||||
|
- 使用:`uuremote-viewer login` 扫码登录,`uuremote-viewer ui` 打开启动窗口;命令行用法见 `uuremote-viewer --help` 和 README。
|
||||||
|
- 运行依赖:GTK3、SDL2(Ubuntu/Debian `libsdl2-2.0-0`)、`ffmpeg` 命令;剪贴板需 `xclip`/`xsel` 或 `wl-clipboard`。
|
||||||
|
- 与主机共用 `~/.config/uuremote/config.json`(`UU_CONFIG` 可另指定),因此不能用同一身份连接本机上的主机。
|
||||||
|
- **已实测**:官方 Windows 被控端(画面、键鼠、文件列表与下载)、官方 Mac 被控端(画面)、本项目主机(画面、键鼠、文件、
|
||||||
|
端口映射、终端;剪贴板、声音和远程协助在虚拟机中)。
|
||||||
|
- ⚠️ **未实测**:重启/关机、终端会话列表的新建与接回、Wayland 下的窗口与剪贴板、多显示器;只解码 H.264 4:2:0。
|
||||||
|
这是实验性功能,欢迎反馈问题。
|
||||||
|
An experimental viewer/controller for UU Remote hosts; the host build and behaviour are unchanged. See the README for usage,
|
||||||
|
dependencies and the tested scope (reboot/shutdown, terminal session list and Wayland are not tested live).
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.2.0-x86_64-linux.tar.gz`:主机 `uuremote-server`(X11 版)与控制端 `uuremote-viewer`(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
控制端补测:重启/关机、终端会话列表、Wayland;主机远程关机不再等待授权弹窗。/ Viewer power, terminal list and Wayland tested; host power actions never wait for a polkit prompt.
|
||||||
|
|
||||||
|
### 修复 / Fixed
|
||||||
|
- **远程重启/关机不再卡在授权弹窗**:被控端用户没有 polkit 权限时(例如本机没有活动的图形会话),`systemctl reboot/poweroff`
|
||||||
|
会一直等待,并可能在被控端屏幕上弹出密码框。现在加了 `--no-ask-password`,没有权限时直接失败并在日志记录 `ok=false`。
|
||||||
|
正常情况下(本机有活动的图形会话)行为不变。
|
||||||
|
Reboot/shutdown no longer wait for a polkit authentication dialog on the host; without permission they fail and are logged.
|
||||||
|
|
||||||
|
### 控制端实测补充 / Viewer testing
|
||||||
|
在虚拟机中用第二个主机身份实测通过(`uuremote-viewer` 连接本项目主机):
|
||||||
|
- 远程**重启**、**关机**,以及关机后重新开机上线。
|
||||||
|
- 终端会话列表:新建、双击接回(重放历史输出、原 shell 环境保留)、确认后结束会话。
|
||||||
|
- Wayland(Hyprland)下的桌面窗口与剪贴板(`wl-copy`/`wl-paste`,双向)。
|
||||||
|
⚠️ 仍未实测:GNOME/KDE Wayland 下的窗口与剪贴板、H.265 与 4:4:4(只解码 H.264 4:2:0)、多显示器。
|
||||||
|
Tested in a VM: reboot, shutdown and power-on, terminal session list, Wayland (Hyprland) window and clipboard.
|
||||||
|
|
||||||
|
### 下载 / Download
|
||||||
|
`uu-remote-linux-v0.2.1-x86_64-linux.tar.gz`:主机 `uuremote-server`(X11 版)与控制端 `uuremote-viewer`(x86_64,glibc ≥ 2.35;控制端需 SDL2、GTK3、ffmpeg),附带 `contrib/`。
|
||||||
|
|
||||||
|
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||||
+536
@@ -0,0 +1,536 @@
|
|||||||
|
//! Opt-in guest assistance. Protocol sources: AGENTS.md "Remote assistance".
|
||||||
|
//! Secret values never implement Display/Debug or enter diagnostics.
|
||||||
|
use crate::auth::{Credentials, RoomData, UUAuthClient};
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
use std::io::{Read, Write};
|
||||||
|
use std::os::unix::{fs::OpenOptionsExt, io::AsRawFd};
|
||||||
|
|
||||||
|
const CREATE: &str = "/api/v1/guest/create";
|
||||||
|
const INFO: &str = "/api/v1/guest/share/info";
|
||||||
|
const CANCEL: &str = "/api/v1/guest/share/cancel";
|
||||||
|
const ROOM: &str = "/api/v1/guest/room/create";
|
||||||
|
const SIGN: &str = "/api/v1/guest/room/share/upload/sign";
|
||||||
|
const CONTROL_MODE: &str = "/api/v1/guest/room/share/upload_control_mode";
|
||||||
|
// Logged-in host: the same flow on the account identity (official Windows host NrdDevice/NrdBusinessEntry).
|
||||||
|
const ACCOUNT_INFO: &str = "/api/v1/device/share/info";
|
||||||
|
const ACCOUNT_CANCEL: &str = "/api/v1/device/share/cancel";
|
||||||
|
const ACCOUNT_SIGN: &str = "/api/v2/room/share/upload_sign";
|
||||||
|
const ACCOUNT_CONTROL_MODE: &str = "/api/v2/room/share/upload_control_mode";
|
||||||
|
const ALPHABET: &[u8] = b"ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
||||||
|
|
||||||
|
/// Guest mode uses its own device/config identity and never consumes an account token.
|
||||||
|
pub fn configure() -> Result<(), String> {
|
||||||
|
if ["UU_USER_ID", "UU_TOKEN", "UU_CSRF"].iter().any(|k| std::env::var_os(k).is_some_and(|v| !v.is_empty())) {
|
||||||
|
return Err("assist mode cannot use account credential environment variables".into());
|
||||||
|
}
|
||||||
|
if std::env::var_os("UU_CONFIG").is_none() {
|
||||||
|
if crate::config::read_fresh()?.as_ref().is_some_and(|v| ["token", "user_id"].iter()
|
||||||
|
.any(|k| v.get(k).and_then(Value::as_str).is_some_and(|s| !s.is_empty()))) {
|
||||||
|
return Err("account identity already configured; guest assist needs an explicit separate UU_CONFIG".into());
|
||||||
|
}
|
||||||
|
let path = crate::config::path().with_file_name("assist").join("config.json");
|
||||||
|
std::env::set_var("UU_CONFIG", path);
|
||||||
|
}
|
||||||
|
if crate::config::read_fresh()?.as_ref().is_some_and(|v| ["token", "user_id", "csrf"].iter()
|
||||||
|
.any(|k| v.get(k).and_then(Value::as_str).is_some_and(|s| !s.is_empty()))) {
|
||||||
|
return Err("assist requires a separate config without account credentials".into());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Official RecipientConfigModel.verificationType: temporary only, custom only, or both.
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||||
|
pub enum Verify { Temporary = 0, Custom = 1, Both = 2 }
|
||||||
|
impl Verify {
|
||||||
|
fn parse(v: &str) -> Option<Self> { match v { "temporary" => Some(Self::Temporary), "custom" => Some(Self::Custom), "both" => Some(Self::Both), _ => None } }
|
||||||
|
fn name(self) -> &'static str { match self { Self::Temporary => "temporary", Self::Custom => "custom", Self::Both => "both" } }
|
||||||
|
fn from_index(i: i64) -> Option<Self> { match i { 0 => Some(Self::Temporary), 1 => Some(Self::Custom), 2 => Some(Self::Both), _ => None } }
|
||||||
|
}
|
||||||
|
/// Official RecipientCodeRefreshType: after each session (closeConnect), hourly, 12 h, 24 h, never.
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||||
|
pub enum Refresh { Session = 0, Hour = 1, Hour12 = 2, Hour24 = 3, Never = 4 }
|
||||||
|
impl Refresh {
|
||||||
|
fn parse(v: &str) -> Option<Self> { match v { "session" => Some(Self::Session), "1h" => Some(Self::Hour), "12h" => Some(Self::Hour12), "24h" => Some(Self::Hour24), "never" => Some(Self::Never), _ => None } }
|
||||||
|
fn name(self) -> &'static str { match self { Self::Session => "session", Self::Hour => "1h", Self::Hour12 => "12h", Self::Hour24 => "24h", Self::Never => "never" } }
|
||||||
|
fn from_index(i: i64) -> Option<Self> { [Self::Session, Self::Hour, Self::Hour12, Self::Hour24, Self::Never].get(usize::try_from(i).ok()?).copied() }
|
||||||
|
fn period(self) -> Option<std::time::Duration> {
|
||||||
|
match self { Self::Hour => Some(3600), Self::Hour12 => Some(12 * 3600), Self::Hour24 => Some(24 * 3600), _ => None }.map(std::time::Duration::from_secs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Mainland mobile numbers are shown like the official client: 186******08.
|
||||||
|
pub fn display_account(name: &str) -> String {
|
||||||
|
if name.len() == 11 && name.bytes().all(|b| b.is_ascii_digit()) { format!("{}******{}", &name[..3], &name[9..]) } else { name.to_owned() }
|
||||||
|
}
|
||||||
|
/// 8-16 ASCII letters/digits including both (official RecipientCustomCodeView rules).
|
||||||
|
fn valid_custom(code: &str) -> bool {
|
||||||
|
(8..=16).contains(&code.len()) && code.bytes().all(|b| b.is_ascii_alphanumeric())
|
||||||
|
&& code.bytes().any(|b| b.is_ascii_alphabetic()) && code.bytes().any(|b| b.is_ascii_digit())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Command { Allow, Deny, Reset, Stop, Verify(Verify), Refresh(Refresh), Custom(String), Logout, Quit }
|
||||||
|
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
extern "C" {
|
||||||
|
fn uu_assist_ui_start(hidden: libc::c_int, hide_on_close: libc::c_int) -> libc::c_int;
|
||||||
|
fn uu_assist_ui_present();
|
||||||
|
fn uu_ui_set_user(name: *const libc::c_char);
|
||||||
|
fn uu_assist_ui_set(id: *const libc::c_char, code: *const libc::c_char, allowed: libc::c_int, verify: libc::c_int, refresh: libc::c_int, has_custom: libc::c_int);
|
||||||
|
fn uu_assist_ui_take_command(arg: *mut libc::c_char, len: libc::size_t) -> libc::c_int;
|
||||||
|
fn uu_assist_ui_close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// "243829726" -> "243 829 726", as the official client shows device IDs.
|
||||||
|
fn display_id(id: &str) -> String {
|
||||||
|
if id.is_empty() || !id.bytes().all(|b| b.is_ascii_digit()) { return id.to_owned(); }
|
||||||
|
id.as_bytes().chunks(3).map(|c| String::from_utf8_lossy(c).into_owned()).collect::<Vec<_>>().join(" ")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Where the ID/code are shown and commands come from: a local GTK window (c_engine/assist_window.c)
|
||||||
|
/// when a display is available, otherwise /dev/tty, which bypasses stdout/stderr, journals,
|
||||||
|
/// redirected output and the logger. Without either, refuse startup rather than disclose the code.
|
||||||
|
pub enum LocalUi { Tty(tokio::io::unix::AsyncFd<std::fs::File>), #[cfg(uu_gtk)] Window }
|
||||||
|
impl LocalUi {
|
||||||
|
/// Standalone `assist`: visible window (closing it stops assistance), else /dev/tty.
|
||||||
|
pub fn open() -> Result<Self, String> { Self::open_visible(false) }
|
||||||
|
/// `hide_on_close`: the not-logged-in host service (window stays reachable with `uuremote-server show`).
|
||||||
|
pub fn open_visible(hide_on_close: bool) -> Result<Self, String> {
|
||||||
|
#[cfg(not(uu_gtk))] let _ = hide_on_close;
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
if std::env::var("UU_ASSIST_UI").ok().as_deref() != Some("tty") && Self::display() && unsafe { uu_assist_ui_start(0, i32::from(hide_on_close)) } == 0 {
|
||||||
|
return Ok(Self::Window);
|
||||||
|
}
|
||||||
|
let tty = std::fs::OpenOptions::new().read(true).write(true)
|
||||||
|
.custom_flags(libc::O_NONBLOCK | libc::O_NOCTTY).open("/dev/tty")
|
||||||
|
.map_err(|_| "assist requires a desktop window or a local controlling terminal; no code was printed")?;
|
||||||
|
if unsafe { libc::isatty(tty.as_raw_fd()) } != 1 { return Err("assist output is not a terminal".into()); }
|
||||||
|
Ok(Self::Tty(tokio::io::unix::AsyncFd::new(tty).map_err(|_| "assist terminal registration failed")?))
|
||||||
|
}
|
||||||
|
fn display() -> bool { std::env::var_os("DISPLAY").is_some() || std::env::var_os("WAYLAND_DISPLAY").is_some() }
|
||||||
|
/// Host service: a hidden window opened on demand (`uuremote-server show`); closing only hides it.
|
||||||
|
/// No terminal fallback: a background service has nowhere safe to print the code.
|
||||||
|
pub fn open_service() -> Result<Self, String> {
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
if Self::display() && unsafe { uu_assist_ui_start(1, 1) } == 0 { return Ok(Self::Window); }
|
||||||
|
Err("remote assistance needs a desktop window for the host service".into())
|
||||||
|
}
|
||||||
|
/// Account card: signed-in user (name already masked for display).
|
||||||
|
pub fn set_user(&self, name: &str) {
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
if matches!(self, Self::Window) { let name = std::ffi::CString::new(name).unwrap_or_default(); unsafe { uu_ui_set_user(name.as_ptr()) } }
|
||||||
|
#[cfg(not(uu_gtk))] let _ = name;
|
||||||
|
}
|
||||||
|
pub fn present(&self) {
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
if matches!(self, Self::Window) { unsafe { uu_assist_ui_present() } }
|
||||||
|
}
|
||||||
|
fn show(&self, id: &str, code: &str, allowed: bool, verify: Verify, refresh: Refresh, has_custom: bool) -> Result<(), String> {
|
||||||
|
#[cfg(not(uu_gtk))] let _ = has_custom;
|
||||||
|
match self {
|
||||||
|
Self::Tty(tty) => {
|
||||||
|
// Bound/control-character-validated server identifier; no terminal escape injection.
|
||||||
|
let mut tty = tty.get_ref();
|
||||||
|
write!(tty, "\n远程协助 / Remote assistance\n设备 ID: {}\n临时验证码: {code}\n允许他人远程协助: {allowed}\n验证方式: {} 自动刷新: {}\n仅向可信的人提供验证码。allow=允许,deny=不允许,reset=换码,mode temporary|custom|both,refresh session|1h|12h|24h|never,off=关闭并撤销\n", display_id(id), verify.name(), refresh.name())
|
||||||
|
.map_err(|_| "assist terminal display failed".into())
|
||||||
|
}
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
Self::Window => {
|
||||||
|
let (id, code) = (std::ffi::CString::new(display_id(id)).unwrap_or_default(), std::ffi::CString::new(code).unwrap_or_default());
|
||||||
|
unsafe { uu_assist_ui_set(id.as_ptr(), code.as_ptr(), i32::from(allowed), verify as i32, refresh as i32, i32::from(has_custom)) };
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub async fn command(&self) -> Command {
|
||||||
|
match self {
|
||||||
|
Self::Tty(tty) => Self::tty_command(tty).await,
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
Self::Window => loop {
|
||||||
|
let mut arg = [0 as libc::c_char; 40];
|
||||||
|
let cmd = unsafe { uu_assist_ui_take_command(arg.as_mut_ptr(), arg.len()) };
|
||||||
|
let text = unsafe { std::ffi::CStr::from_ptr(arg.as_ptr()) }.to_string_lossy().into_owned();
|
||||||
|
let index = text.parse::<i64>().unwrap_or(-1);
|
||||||
|
match cmd {
|
||||||
|
1 => return Command::Allow, 2 => return Command::Deny, 3 => return Command::Reset, 4 => return Command::Stop,
|
||||||
|
5 => if let Some(v) = Verify::from_index(index) { return Command::Verify(v) },
|
||||||
|
6 => if let Some(r) = Refresh::from_index(index) { return Command::Refresh(r) },
|
||||||
|
7 => return Command::Custom(text),
|
||||||
|
8 => return Command::Logout,
|
||||||
|
9 => return Command::Quit,
|
||||||
|
_ => tokio::time::sleep(std::time::Duration::from_millis(150)).await,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async fn tty_command(tty: &tokio::io::unix::AsyncFd<std::fs::File>) -> Command {
|
||||||
|
let mut line = Vec::new();
|
||||||
|
loop {
|
||||||
|
let mut ready = match tty.readable().await { Ok(v) => v, Err(_) => return Command::Stop };
|
||||||
|
let result = ready.try_io(|fd| { let mut byte = [0u8]; let mut tty = fd.get_ref(); tty.read(&mut byte).map(|n| (n, byte[0])) });
|
||||||
|
match result {
|
||||||
|
Ok(Ok((0, _))) | Ok(Err(_)) => return Command::Stop,
|
||||||
|
Ok(Ok((_, byte))) if byte == b'\n' => {
|
||||||
|
let text = String::from_utf8_lossy(&line).trim().to_owned();
|
||||||
|
let cmd = match text.split_once(' ') {
|
||||||
|
Some(("mode", v)) => Verify::parse(v).map(Command::Verify),
|
||||||
|
Some(("refresh", v)) => Refresh::parse(v).map(Command::Refresh),
|
||||||
|
_ => match text.as_str() { "allow" => Some(Command::Allow), "deny" => Some(Command::Deny), "reset" => Some(Command::Reset), "off" | "q" => Some(Command::Stop), _ => None },
|
||||||
|
};
|
||||||
|
line.clear(); if let Some(cmd) = cmd { return cmd; }
|
||||||
|
}
|
||||||
|
Ok(Ok((_, byte))) => { if line.len() < 64 { line.push(byte); } },
|
||||||
|
Err(_) => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Drop for LocalUi {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
if matches!(self, Self::Window) { unsafe { uu_assist_ui_close() } }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn response(status: u16, value: &Value) -> Result<&Value, String> {
|
||||||
|
let code = value.get("code").and_then(Value::as_i64).unwrap_or(-1);
|
||||||
|
if status == 200 && code == 0 { return Ok(value.get("data").unwrap_or(&Value::Null)); }
|
||||||
|
// No server message, validation payload, headers, URLs, token or code in errors. UU_ASSIST_DEBUG=1 adds
|
||||||
|
// the server's short human-readable message (bounded) for diagnosing rejected guest calls.
|
||||||
|
let message = (std::env::var("UU_ASSIST_DEBUG").ok().as_deref() == Some("1"))
|
||||||
|
.then(|| value.get("message").or_else(|| value.get("msg")).and_then(Value::as_str)
|
||||||
|
.map(|m| m.chars().filter(|c| !c.is_control()).take(120).collect::<String>()))
|
||||||
|
.flatten().map(|m| format!(" message={m:?}")).unwrap_or_default();
|
||||||
|
Err(format!("assist API rejected request: http={status} code={code}{message}"))
|
||||||
|
}
|
||||||
|
fn text(value: &Value, key: &str) -> Result<String, String> {
|
||||||
|
value.get(key).and_then(Value::as_str).filter(|s| !s.is_empty() && s.len() <= 16384)
|
||||||
|
.map(str::to_owned).ok_or_else(|| "assist response missing required string".into())
|
||||||
|
}
|
||||||
|
fn share_id(value: &Value) -> Result<String, String> {
|
||||||
|
let id = text(value, "connect_id")?;
|
||||||
|
if id.len() > 128 || !id.bytes().all(|c| c.is_ascii_alphanumeric() || c == b'-') {
|
||||||
|
return Err("assist response invalid device identifier".into());
|
||||||
|
}
|
||||||
|
Ok(id)
|
||||||
|
}
|
||||||
|
fn generate_code() -> Result<String, String> {
|
||||||
|
let mut random = std::fs::File::open("/dev/urandom").map_err(|_| "assist random source unavailable")?;
|
||||||
|
let mut code = String::new();
|
||||||
|
// Rejection sampling avoids modulo bias (alphabet has 32 entries).
|
||||||
|
let limit = 256 / ALPHABET.len() * ALPHABET.len();
|
||||||
|
while code.len() < 8 {
|
||||||
|
let mut byte = [0u8]; random.read_exact(&mut byte).map_err(|_| "assist random source failed")?;
|
||||||
|
if (byte[0] as usize) < limit { code.push(ALPHABET[byte[0] as usize % ALPHABET.len()] as char); }
|
||||||
|
}
|
||||||
|
Ok(code)
|
||||||
|
}
|
||||||
|
fn digest(salt: &str, code: &str) -> String {
|
||||||
|
let mut hash = Sha256::new(); hash.update(salt.as_bytes()); hash.update(code.as_bytes()); hex::encode(hash.finalize())
|
||||||
|
}
|
||||||
|
/// Official: temporary-only signs the temporary code with an empty backup; both adds the custom code
|
||||||
|
/// as `backup_sign`; custom-only puts the custom code in the primary `sign`.
|
||||||
|
fn sign_body(control_id: &str, salt: &str, temporary: &str, custom: Option<&str>, verify: Verify, allow: bool) -> Value {
|
||||||
|
let (primary, backup) = match (verify, custom) {
|
||||||
|
(Verify::Custom, Some(c)) => (c, None),
|
||||||
|
(Verify::Both, Some(c)) => (temporary, Some(c)),
|
||||||
|
_ => (temporary, None),
|
||||||
|
};
|
||||||
|
json!({"can_remote_control": allow, "control_id": control_id,
|
||||||
|
"sign": digest(salt, primary), "backup_sign": backup.map(|c| digest(salt, c)).unwrap_or_default()})
|
||||||
|
}
|
||||||
|
fn challenge(value: &Value) -> Option<(&str, &str)> {
|
||||||
|
let id = value.get("control_id")?.as_str()?; let salt = value.get("salt")?.as_str()?;
|
||||||
|
(!id.is_empty() && id.len() <= 256 && !salt.is_empty() && salt.len() <= 4096).then_some((id, salt))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct RetryBackoff(u64);
|
||||||
|
impl RetryBackoff {
|
||||||
|
fn next(&mut self) -> std::time::Duration { self.0 = (self.0 + 15).min(99); std::time::Duration::from_secs(self.0) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Assist {
|
||||||
|
_lock: std::fs::File,
|
||||||
|
pub renewed: bool,
|
||||||
|
device: Credentials,
|
||||||
|
pub client: std::sync::Arc<UUAuthClient>,
|
||||||
|
ui: std::sync::Arc<LocalUi>,
|
||||||
|
code: String,
|
||||||
|
custom: Option<String>,
|
||||||
|
id: String,
|
||||||
|
pub allowed: bool,
|
||||||
|
verify: Verify,
|
||||||
|
refresh: Refresh,
|
||||||
|
code_at: std::time::Instant,
|
||||||
|
/// Logged-in host: account endpoints and the host's own room (no guest identity).
|
||||||
|
account: bool,
|
||||||
|
}
|
||||||
|
impl Assist {
|
||||||
|
pub async fn start(shutdown: &mut tokio::sync::watch::Receiver<bool>, service: bool) -> Result<Self, String> {
|
||||||
|
let ui = std::sync::Arc::new(LocalUi::open_visible(service)?);
|
||||||
|
let lock = Self::lock()?;
|
||||||
|
// guest/create rejects an unregistered device (HTTP 422 code 1001, verified live): register the
|
||||||
|
// Windows device identity first, exactly as account login does (no account involved).
|
||||||
|
let (config, device) = crate::login::prepare_device().await.map_err(|_| "assist device registration failed")?;
|
||||||
|
let (custom, verify, refresh, allowed) = Self::settings(&config)?;
|
||||||
|
let mut retry = RetryBackoff::default();
|
||||||
|
let client = loop {
|
||||||
|
if *shutdown.borrow() { return Err("assist stopped".into()); }
|
||||||
|
let result = tokio::select! {
|
||||||
|
result = Self::authenticate(&device) => result,
|
||||||
|
_ = shutdown.changed() => return Err("assist stopped".into()),
|
||||||
|
_ = ui.command() => return Err("assist stopped before guest identity was available".into()),
|
||||||
|
};
|
||||||
|
match result { Ok(client) => break client, Err(error) => {
|
||||||
|
let delay = retry.next();
|
||||||
|
// `error` carries only HTTP status / API code numbers or a fixed transport label.
|
||||||
|
log::warn!("Guest identity request failed: {error}; retry_s={}", delay.as_secs());
|
||||||
|
tokio::select! {
|
||||||
|
_ = tokio::time::sleep(delay) => {},
|
||||||
|
_ = shutdown.changed() => return Err("assist stopped".into()),
|
||||||
|
_ = ui.command() => return Err("assist stopped before guest identity was available".into()),
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
};
|
||||||
|
let this = Self { _lock: lock, renewed: false, device, client: std::sync::Arc::new(client), ui, code: generate_code()?, custom, id: String::new(),
|
||||||
|
allowed, verify, refresh, code_at: std::time::Instant::now(), account: false };
|
||||||
|
// guest/share/info answers 400/2004 until a guest room exists (verified live), so the ID/code
|
||||||
|
// are published right after the first room(), which serve() requests before signaling.
|
||||||
|
Ok(this)
|
||||||
|
}
|
||||||
|
fn settings(config: &Value) -> Result<(Option<String>, Verify, Refresh, bool), String> {
|
||||||
|
let custom = config.get("assist_custom_code").and_then(Value::as_str).map(str::to_owned);
|
||||||
|
if custom.as_deref().is_some_and(|c| !valid_custom(c)) {
|
||||||
|
return Err("assist_custom_code requires 8-16 ASCII letters/digits, including both".into());
|
||||||
|
}
|
||||||
|
let setting = |key: &str| config.get(key).and_then(Value::as_str).map(str::to_owned);
|
||||||
|
let verify = setting("assist_verify").as_deref().and_then(Verify::parse).filter(|v| *v == Verify::Temporary || custom.is_some()).unwrap_or(Verify::Temporary);
|
||||||
|
let refresh = setting("assist_refresh").as_deref().and_then(Refresh::parse).unwrap_or(Refresh::Session);
|
||||||
|
// Official default is "not allowed"; the switch state persists like isAllowRecipient.
|
||||||
|
let allowed = std::env::var("UU_ASSIST_ALLOW").ok().as_deref() == Some("1")
|
||||||
|
|| config.get("assist_allow").and_then(Value::as_bool) == Some(true);
|
||||||
|
Ok((custom, verify, refresh, allowed))
|
||||||
|
}
|
||||||
|
fn lock() -> Result<std::fs::File, String> {
|
||||||
|
let lock_path = crate::config::path().with_file_name("assist.lock");
|
||||||
|
if let Some(dir) = lock_path.parent() { std::fs::create_dir_all(dir).map_err(|_| "assist config directory unavailable")?; }
|
||||||
|
let lock = std::fs::OpenOptions::new().create(true).write(true).mode(0o600).custom_flags(libc::O_NOFOLLOW).open(lock_path)
|
||||||
|
.map_err(|_| "assist lock unavailable")?;
|
||||||
|
if unsafe { libc::flock(lock.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } != 0 { return Err("another assist host uses this config".into()); }
|
||||||
|
Ok(lock)
|
||||||
|
}
|
||||||
|
/// Remote assistance on a logged-in host, shown in the service's hidden window.
|
||||||
|
pub fn start_account(client: std::sync::Arc<UUAuthClient>, device: Credentials) -> Result<Self, String> {
|
||||||
|
let lock = Self::lock()?;
|
||||||
|
let ui = std::sync::Arc::new(LocalUi::open_service()?);
|
||||||
|
let config = crate::config::read_fresh()?.unwrap_or_else(|| json!({}));
|
||||||
|
let (custom, verify, refresh, allowed) = Self::settings(&config)?;
|
||||||
|
let this = Self { _lock: lock, renewed: false, device, client, ui, code: generate_code()?, custom, id: String::new(),
|
||||||
|
allowed, verify, refresh, code_at: std::time::Instant::now(), account: true };
|
||||||
|
this.show()?;
|
||||||
|
Ok(this)
|
||||||
|
}
|
||||||
|
pub fn is_account(&self) -> bool { self.account }
|
||||||
|
/// Logged-in host: account name for the account card (saved at login, else /api/v1/user/info).
|
||||||
|
pub async fn load_account_name(&self) {
|
||||||
|
let saved = crate::config::read_fresh().ok().flatten()
|
||||||
|
.and_then(|c| c.get("account_name").and_then(Value::as_str).map(str::to_owned)).filter(|n| !n.is_empty());
|
||||||
|
let name = match saved {
|
||||||
|
Some(name) => name,
|
||||||
|
None => match self.client.assist_get("/api/v1/user/info").await {
|
||||||
|
Ok((200, v)) => v.get("data").and_then(|d| ["nickname", "mobile", "phone"].iter()
|
||||||
|
.find_map(|k| d.get(*k).and_then(Value::as_str).filter(|s| !s.is_empty()).map(str::to_owned)))
|
||||||
|
.unwrap_or_else(|| self.device.user_id.clone()),
|
||||||
|
_ => self.device.user_id.clone(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
self.ui.set_user(&display_account(&name));
|
||||||
|
}
|
||||||
|
/// Sign out: official /api/v1/user/logout (best effort), withdraw the share, drop local credentials.
|
||||||
|
pub async fn logout(&mut self) {
|
||||||
|
let _ = self.client.assist_post("/api/v1/user/logout", None).await;
|
||||||
|
let _ = self.cancel().await;
|
||||||
|
self.save_many(&[("token", Value::Null), ("csrf", Value::Null), ("user_id", Value::Null), ("account_name", Value::Null)]);
|
||||||
|
log::info!("Signed out; restarting without an account");
|
||||||
|
}
|
||||||
|
pub fn ui(&self) -> std::sync::Arc<LocalUi> { self.ui.clone() }
|
||||||
|
fn path(&self, guest: &'static str) -> &'static str {
|
||||||
|
if !self.account { return guest; }
|
||||||
|
match guest { INFO => ACCOUNT_INFO, CANCEL => ACCOUNT_CANCEL, SIGN => ACCOUNT_SIGN, CONTROL_MODE => ACCOUNT_CONTROL_MODE, other => other }
|
||||||
|
}
|
||||||
|
/// Logged-in host: publish the share ID/code once its own room exists (same ordering as guests).
|
||||||
|
pub async fn ensure_published(&mut self) -> Result<(), String> {
|
||||||
|
if self.id.is_empty() { self.publish().await } else { Ok(()) }
|
||||||
|
}
|
||||||
|
async fn authenticate(device: &Credentials) -> Result<UUAuthClient, String> {
|
||||||
|
let client = UUAuthClient::new(device.clone());
|
||||||
|
let (status, value) = client.assist_post(CREATE, None).await?;
|
||||||
|
let data = response(status, &value)?;
|
||||||
|
Ok(UUAuthClient::new_guest(device.clone(), text(data, "guest_id")?, text(data, "token")?))
|
||||||
|
}
|
||||||
|
async fn post(&mut self, path: &'static str, body: Option<&Value>) -> Result<Value, String> {
|
||||||
|
let path = self.path(path);
|
||||||
|
let (mut status, mut value) = if path == ACCOUNT_INFO { self.client.assist_get(path).await? } else { self.client.assist_post(path, body).await? };
|
||||||
|
if self.account { return response(status, &value).map(Clone::clone); }
|
||||||
|
// Official wrapper refreshes on 1120 and retries once, rather than on a guessed TTL.
|
||||||
|
if value.get("code").and_then(Value::as_i64) == Some(crate::auth::TOKEN_INVALID) {
|
||||||
|
self.client = std::sync::Arc::new(Self::authenticate(&self.device).await?);
|
||||||
|
self.renewed = true;
|
||||||
|
(status, value) = self.client.assist_post(path, body).await?;
|
||||||
|
}
|
||||||
|
response(status, &value).map(Clone::clone)
|
||||||
|
}
|
||||||
|
async fn publish(&mut self) -> Result<(), String> {
|
||||||
|
self.id = share_id(&self.post(INFO, None).await?)?;
|
||||||
|
self.show()
|
||||||
|
}
|
||||||
|
pub async fn room(&mut self) -> Result<RoomData, String> {
|
||||||
|
let value = self.post(ROOM, None).await?;
|
||||||
|
if self.renewed || self.id.is_empty() {
|
||||||
|
if let Err(error) = self.publish().await { let _ = self.cancel().await; return Err(error); }
|
||||||
|
self.renewed = false;
|
||||||
|
}
|
||||||
|
serde_json::from_value(value).map_err(|_| "assist room response malformed".into())
|
||||||
|
}
|
||||||
|
pub async fn challenge(&mut self, value: &Value) -> Result<(), String> {
|
||||||
|
let (control, salt) = challenge(value).ok_or("assist challenge malformed")?;
|
||||||
|
self.post(SIGN, Some(&sign_body(control, salt, &self.code, self.custom.as_deref(), self.verify, self.allowed))).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
/// Answer the cloud's `get_control_mode` push (data {control_id}). Official Windows host
|
||||||
|
/// (GameViewerServer RemoteAssistCode::respRemoteGetControlMode -> NrdBusinessEntry::uploadControlMode)
|
||||||
|
/// posts {control_id, allow_control, control_mode: by_password|by_confirmation|password_confirmation}.
|
||||||
|
/// Only the temporary/custom code mode is implemented, so this host reports `by_password`.
|
||||||
|
pub async fn control_mode(&mut self, value: &Value) -> Result<(), String> {
|
||||||
|
let control = value.get("control_id").and_then(Value::as_str).filter(|s| !s.is_empty() && s.len() <= 256)
|
||||||
|
.ok_or("assist control mode request malformed")?;
|
||||||
|
self.post(CONTROL_MODE, Some(&json!({"control_id": control, "allow_control": self.allowed, "control_mode": "by_password"}))).await.map(|_| ())
|
||||||
|
}
|
||||||
|
/// The code the controller types: the custom code in custom-only mode, else the temporary one.
|
||||||
|
fn show(&self) -> Result<(), String> {
|
||||||
|
let code = match (self.verify, &self.custom) { (Verify::Custom, Some(c)) => c.as_str(), _ => self.code.as_str() };
|
||||||
|
self.ui.show(&self.id, code, self.allowed, self.verify, self.refresh, self.custom.is_some())
|
||||||
|
}
|
||||||
|
/// Saves one assist setting into the owner's 0600 config (custom code included, never logged).
|
||||||
|
fn save_many(&self, entries: &[(&str, Value)]) {
|
||||||
|
let mut cfg = std::fs::read(crate::config::path()).ok().and_then(|b| serde_json::from_slice::<Value>(&b).ok())
|
||||||
|
.filter(Value::is_object).unwrap_or_else(|| json!({}));
|
||||||
|
if let Some(map) = cfg.as_object_mut() { for (k, v) in entries { if v.is_null() { map.remove(*k); } else { map.insert((*k).to_owned(), v.clone()); } } }
|
||||||
|
if crate::login::write_config(&cfg).is_err() { log::warn!("Account settings could not be saved"); }
|
||||||
|
}
|
||||||
|
fn save(&self, key: &str, value: Value) {
|
||||||
|
// Re-read the file: config::file() is a startup snapshot that predates device registration,
|
||||||
|
// so writing it back would drop device_id (and earlier saves) and change the device ID.
|
||||||
|
let mut cfg = std::fs::read(crate::config::path()).ok().and_then(|b| serde_json::from_slice::<Value>(&b).ok())
|
||||||
|
.filter(Value::is_object).unwrap_or_else(|| json!({}));
|
||||||
|
cfg[key] = value;
|
||||||
|
if crate::login::write_config(&cfg).is_err() { log::warn!("Assist setting could not be saved: key={key}"); }
|
||||||
|
}
|
||||||
|
pub fn reset(&mut self) -> Result<(), String> { self.code = generate_code()?; self.code_at = std::time::Instant::now(); self.show() }
|
||||||
|
pub fn allow(&mut self) -> Result<(), String> { self.allowed = true; self.save("assist_allow", json!(true)); self.show() }
|
||||||
|
pub fn deny(&mut self) -> Result<(), String> { self.allowed = false; self.save("assist_allow", json!(false)); self.show() }
|
||||||
|
pub fn set_verify(&mut self, verify: Verify) -> Result<(), String> {
|
||||||
|
if verify != Verify::Temporary && self.custom.is_none() { return self.show(); } // UI asks for a code first
|
||||||
|
self.verify = verify; self.save("assist_verify", json!(verify.name())); self.show()
|
||||||
|
}
|
||||||
|
pub fn set_refresh(&mut self, refresh: Refresh) -> Result<(), String> {
|
||||||
|
self.refresh = refresh; self.code_at = std::time::Instant::now(); self.save("assist_refresh", json!(refresh.name())); self.show()
|
||||||
|
}
|
||||||
|
pub fn set_custom(&mut self, code: String) -> Result<(), String> {
|
||||||
|
if !valid_custom(&code) { return self.show(); }
|
||||||
|
self.custom = Some(code.clone()); self.save("assist_custom_code", json!(code));
|
||||||
|
if self.verify == Verify::Temporary { self.verify = Verify::Custom; self.save("assist_verify", json!(self.verify.name())); }
|
||||||
|
self.show()
|
||||||
|
}
|
||||||
|
/// After the last desktop session: rotate only in "每次远控结束" mode.
|
||||||
|
pub fn session_ended(&mut self) -> Result<(), String> { if self.refresh == Refresh::Session { self.reset() } else { Ok(()) } }
|
||||||
|
/// Periodic rotation for the hourly/12 h/24 h settings.
|
||||||
|
pub fn tick(&mut self) -> Result<(), String> {
|
||||||
|
match self.refresh.period() { Some(p) if self.code_at.elapsed() >= p => self.reset(), _ => Ok(()) }
|
||||||
|
}
|
||||||
|
pub fn commands(&self, shutdown: tokio::sync::watch::Sender<bool>) -> (tokio::sync::mpsc::Receiver<Command>, tokio::task::JoinHandle<()>) {
|
||||||
|
let (tx, rx) = tokio::sync::mpsc::channel(4);
|
||||||
|
let ui = self.ui.clone();
|
||||||
|
let task = tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
let command = tokio::select! { command = ui.command() => command, _ = tx.closed() => return };
|
||||||
|
if matches!(command, Command::Stop) { let _ = shutdown.send(true); return; }
|
||||||
|
match tx.try_send(command) {
|
||||||
|
Ok(()) | Err(tokio::sync::mpsc::error::TrySendError::Full(_)) => {},
|
||||||
|
Err(tokio::sync::mpsc::error::TrySendError::Closed(_)) => return,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
(rx, task)
|
||||||
|
}
|
||||||
|
pub async fn cancel(&mut self) -> Result<(), String> { self.post(CANCEL, None).await.map(|_| ()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[tokio::test]
|
||||||
|
async fn local_terminal_display_and_explicit_commands() {
|
||||||
|
// Private PTY pair only, never the user's actual terminal; all values synthetic.
|
||||||
|
let pair = nix::pty::openpty(None, None).unwrap();
|
||||||
|
let slave = std::fs::File::from(pair.slave);
|
||||||
|
unsafe { libc::fcntl(slave.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK); }
|
||||||
|
let ui = LocalUi::Tty(tokio::io::unix::AsyncFd::new(slave).unwrap());
|
||||||
|
let mut master = std::fs::File::from(pair.master);
|
||||||
|
ui.show("fixture-123", "FIXTURE8", false, Verify::Temporary, Refresh::Session, false).unwrap();
|
||||||
|
// A PTY may deliver the banner in several chunks: read until complete (bounded).
|
||||||
|
let mut text = String::new();
|
||||||
|
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(3);
|
||||||
|
let complete = |t: &str| t.contains("fixture-123") && t.contains("FIXTURE8") && t.contains("false") && t.contains("off=");
|
||||||
|
while !complete(&text) && std::time::Instant::now() < deadline {
|
||||||
|
let mut bytes = [0u8; 1024];
|
||||||
|
match master.read(&mut bytes) { Ok(n) if n > 0 => text.push_str(&String::from_utf8_lossy(&bytes[..n])), _ => break }
|
||||||
|
}
|
||||||
|
assert!(complete(&text));
|
||||||
|
for (input, expected) in [(b"unknown\nallow\n".as_slice(), 0), (b"reset\n".as_slice(), 1), (b"off\n".as_slice(), 2)] {
|
||||||
|
master.write_all(input).unwrap();
|
||||||
|
let command = tokio::time::timeout(std::time::Duration::from_secs(2), ui.command()).await.unwrap();
|
||||||
|
assert!(matches!((command, expected), (Command::Allow, 0) | (Command::Reset, 1) | (Command::Stop, 2)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn official_challenge_sign_and_shape() {
|
||||||
|
// Synthetic fixture, independently calculated SHA256("saltABC234XY").
|
||||||
|
let body = sign_body("fixture-control", "salt", "ABC234XY", None, Verify::Temporary, false);
|
||||||
|
assert_eq!(body.as_object().unwrap().len(), 4);
|
||||||
|
assert_eq!(body["can_remote_control"], false);
|
||||||
|
assert_eq!(body["control_id"], "fixture-control");
|
||||||
|
assert_eq!(body["sign"], "dc747ee8adb0e591f5ff4ab6cabcaf3a0ec312851580f225d4ca140766229c94");
|
||||||
|
assert_eq!(body["backup_sign"], "");
|
||||||
|
let both = sign_body("fixture", "salt", "ABC234XY", Some("test1234"), Verify::Both, true);
|
||||||
|
assert_ne!(both["sign"], both["backup_sign"]);
|
||||||
|
assert_eq!(both["can_remote_control"], true);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn rejects_missing_or_unbounded_challenges_and_terminal_injection() {
|
||||||
|
for value in [json!({}), json!({"control_id":"x", "salt":""}), json!({"control_id":12,"salt":"s"}), json!({"control_id":"x","salt":"s".repeat(4097)})] {
|
||||||
|
assert!(challenge(&value).is_none());
|
||||||
|
}
|
||||||
|
assert!(challenge(&json!({"control_id":"fixture", "salt":"fixture"})).is_some());
|
||||||
|
assert!(share_id(&json!({"connect_id":"fixture-123"})).is_ok());
|
||||||
|
for id in ["", "\u{1b}[2J", "a\nb", "x\u{7}"] { assert!(share_id(&json!({"connect_id":id})).is_err()); }
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn responses_do_not_disclose_untrusted_server_errors() {
|
||||||
|
let error = response(422, &json!({"code":1120,"msg":"synthetic-secret","data":{"token":"synthetic-secret"}})).unwrap_err();
|
||||||
|
assert!(!error.contains("synthetic-secret"));
|
||||||
|
assert!(response(200, &json!({"code":0})).unwrap().is_null());
|
||||||
|
assert!(text(&Value::Null, "token").is_err());
|
||||||
|
assert_eq!(text(&json!({"guest_id":"fixture-guest"}), "guest_id").unwrap(), "fixture-guest");
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn official_guest_failure_retry_schedule() {
|
||||||
|
let mut retry = RetryBackoff::default();
|
||||||
|
let actual: Vec<_> = (0..9).map(|_| retry.next().as_secs()).collect();
|
||||||
|
assert_eq!(actual, [15,30,45,60,75,90,99,99,99]);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn secure_temporary_code_format() {
|
||||||
|
for _ in 0..64 { let code = generate_code().unwrap(); assert_eq!(code.len(),8); assert!(code.bytes().all(|b| ALPHABET.contains(&b))); }
|
||||||
|
}
|
||||||
|
}
|
||||||
+122
-6
@@ -73,7 +73,7 @@ pub struct ApiResponse<T> {
|
|||||||
pub data: Option<T>,
|
pub data: Option<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Clone, Deserialize)]
|
||||||
pub struct RoomData {
|
pub struct RoomData {
|
||||||
pub signaling_server: String,
|
pub signaling_server: String,
|
||||||
pub token: String,
|
pub token: String,
|
||||||
@@ -81,9 +81,44 @@ pub struct RoomData {
|
|||||||
pub ws_connect_timeout_ms: Option<u64>,
|
pub ws_connect_timeout_ms: Option<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Debug for RoomData {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str("RoomData { .. }") }
|
||||||
|
}
|
||||||
|
|
||||||
pub struct UUAuthClient {
|
pub struct UUAuthClient {
|
||||||
pub creds: Credentials,
|
pub creds: Credentials,
|
||||||
http: reqwest::Client,
|
http: reqwest::Client,
|
||||||
|
guest_id: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Transport failures name only the outer request; the cause (certificate, proxy, DNS,
|
||||||
|
/// timeout) lives in the source chain. URL user-info (proxy credentials) and query
|
||||||
|
/// strings are removed before the chain is shown.
|
||||||
|
pub fn transport_error(error: &(dyn std::error::Error + 'static)) -> String {
|
||||||
|
let mut parts = vec![error.to_string()];
|
||||||
|
let mut source = error.source();
|
||||||
|
while let Some(cause) = source {
|
||||||
|
let text = cause.to_string();
|
||||||
|
if parts.last() != Some(&text) { parts.push(text); }
|
||||||
|
source = cause.source();
|
||||||
|
if parts.len() >= 8 { break; }
|
||||||
|
}
|
||||||
|
redact_urls(&parts.join(": "))
|
||||||
|
}
|
||||||
|
fn redact_urls(text: &str) -> String {
|
||||||
|
let mut out = String::with_capacity(text.len());
|
||||||
|
let mut rest = text;
|
||||||
|
while let Some(i) = rest.find("://") {
|
||||||
|
let (head, tail) = rest.split_at(i + 3);
|
||||||
|
out.push_str(head);
|
||||||
|
let end = tail.find(|c: char| c.is_whitespace() || c == ')' || c == '"' || c == '\'').unwrap_or(tail.len());
|
||||||
|
let url = &tail[..end];
|
||||||
|
let url = url.rsplit_once('@').map_or(url, |(_, host)| host);
|
||||||
|
out.push_str(url.split(['?', '#']).next().unwrap_or(""));
|
||||||
|
rest = &tail[end..];
|
||||||
|
}
|
||||||
|
out.push_str(rest);
|
||||||
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UUAuthClient {
|
impl UUAuthClient {
|
||||||
@@ -92,7 +127,16 @@ impl UUAuthClient {
|
|||||||
.timeout(std::time::Duration::from_secs(12))
|
.timeout(std::time::Duration::from_secs(12))
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
Self { creds, http }
|
Self { creds, http, guest_id: None }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Explicit guest identity: never inherits account Authorization/cookies.
|
||||||
|
/// Official ApiDefaultHeaders 0x1003853ac/0x1003853f8 (see assist evidence).
|
||||||
|
pub fn new_guest(mut device: Credentials, guest_id: String, token: String) -> Self {
|
||||||
|
device.user_id.clear(); device.csrf.clear(); device.token = token;
|
||||||
|
let mut client = Self::new(device);
|
||||||
|
client.guest_id = Some(guest_id);
|
||||||
|
client
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 计算网易 NRD HMAC-SHA256 签名
|
/// 计算网易 NRD HMAC-SHA256 签名
|
||||||
@@ -147,6 +191,7 @@ impl UUAuthClient {
|
|||||||
("x-param-system-id", &self.creds.system_id), ("x-param-user-id", &self.creds.user_id)] {
|
("x-param-system-id", &self.creds.system_id), ("x-param-user-id", &self.creds.user_id)] {
|
||||||
if !value.is_empty() { param_map.insert(key.into(), value.clone()); }
|
if !value.is_empty() { param_map.insert(key.into(), value.clone()); }
|
||||||
}
|
}
|
||||||
|
if let Some(id) = &self.guest_id { param_map.insert("x-param-guest-id".into(), id.clone()); }
|
||||||
param_map.insert("x-param-ts".into(), ts);
|
param_map.insert("x-param-ts".into(), ts);
|
||||||
|
|
||||||
let sign = self.sign_request(method, path, ¶m_map, body_str);
|
let sign = self.sign_request(method, path, ¶m_map, body_str);
|
||||||
@@ -160,10 +205,12 @@ impl UUAuthClient {
|
|||||||
);
|
);
|
||||||
if !self.creds.token.is_empty() {
|
if !self.creds.token.is_empty() {
|
||||||
if let Ok(v) = HeaderValue::from_str(&format!("Bearer {}", self.creds.token)) { headers.insert("Authorization", v); }
|
if let Ok(v) = HeaderValue::from_str(&format!("Bearer {}", self.creds.token)) { headers.insert("Authorization", v); }
|
||||||
if let Ok(v) = HeaderValue::from_str(&format!("nrd_access_token={}; nrd_csrf_access_token={}", self.creds.token, self.creds.csrf)) {
|
if self.guest_id.is_none() {
|
||||||
headers.insert("Cookie", v);
|
if let Ok(v) = HeaderValue::from_str(&format!("nrd_access_token={}; nrd_csrf_access_token={}", self.creds.token, self.creds.csrf)) {
|
||||||
|
headers.insert("Cookie", v);
|
||||||
|
}
|
||||||
|
if let Ok(v) = HeaderValue::from_str(&self.creds.csrf) { headers.insert("X-CSRF-TOKEN", v); }
|
||||||
}
|
}
|
||||||
if let Ok(v) = HeaderValue::from_str(&self.creds.csrf) { headers.insert("X-CSRF-TOKEN", v); }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (k, v) in param_map {
|
for (k, v) in param_map {
|
||||||
@@ -183,12 +230,40 @@ impl UUAuthClient {
|
|||||||
let body_str = serde_json::to_string(body).map_err(|e| e.to_string())?;
|
let body_str = serde_json::to_string(body).map_err(|e| e.to_string())?;
|
||||||
let resp = self.http.post(format!("{}{}", BASE_URL, path))
|
let resp = self.http.post(format!("{}{}", BASE_URL, path))
|
||||||
.headers(self.build_headers("POST", path, &body_str)).body(body_str)
|
.headers(self.build_headers("POST", path, &body_str)).body(body_str)
|
||||||
.send().await.map_err(|e| format!("HTTP post {path} failed: {e}"))?;
|
.send().await.map_err(|e| format!("HTTP post {path} failed: {}", transport_error(&e)))?;
|
||||||
let status = resp.status().as_u16();
|
let status = resp.status().as_u16();
|
||||||
let value = resp.json().await.unwrap_or(serde_json::Value::Null);
|
let value = resp.json().await.unwrap_or(serde_json::Value::Null);
|
||||||
Ok((status, value))
|
Ok((status, value))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Guest APIs use requestPlain for empty requests; sign the actual empty body.
|
||||||
|
/// Errors intentionally exclude response text and URL (both can contain secrets).
|
||||||
|
pub async fn assist_post(&self, path: &str, body: Option<&serde_json::Value>) -> Result<(u16, serde_json::Value), String> {
|
||||||
|
self.assist_send(self.assist_request(path, body)?).await
|
||||||
|
}
|
||||||
|
/// Account share info is a GET (POST answers 405, verified live).
|
||||||
|
pub async fn assist_get(&self, path: &str) -> Result<(u16, serde_json::Value), String> {
|
||||||
|
let request = self.http.get(format!("{BASE_URL}{path}")).headers(self.build_headers("GET", path, "")).build()
|
||||||
|
.map_err(|_| "assist request construction failed")?;
|
||||||
|
self.assist_send(request).await
|
||||||
|
}
|
||||||
|
async fn assist_send(&self, request: reqwest::Request) -> Result<(u16, serde_json::Value), String> {
|
||||||
|
let response = self.http.execute(request)
|
||||||
|
.await.map_err(|e| format!("assist request transport failed: {}", transport_error(&e)))?;
|
||||||
|
let status = response.status().as_u16();
|
||||||
|
let bytes = response.bytes().await.map_err(|_| "assist response read failed")?;
|
||||||
|
if bytes.len() > 65536 { return Err("assist response too large".into()); }
|
||||||
|
let value = serde_json::from_slice(&bytes).map_err(|_| "assist response malformed")?;
|
||||||
|
Ok((status, value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn assist_request(&self, path: &str, body: Option<&serde_json::Value>) -> Result<reqwest::Request, String> {
|
||||||
|
let body = body.map(serde_json::to_string).transpose().map_err(|_| "assist request encoding failed")?.unwrap_or_default();
|
||||||
|
self.http.post(format!("{BASE_URL}{path}"))
|
||||||
|
.headers(self.build_headers("POST", path, &body)).body(body).build()
|
||||||
|
.map_err(|_| "assist request construction failed".into())
|
||||||
|
}
|
||||||
|
|
||||||
/// 向网易服务器请求创建房间,获取最新的信令服务器与鉴权 Token
|
/// 向网易服务器请求创建房间,获取最新的信令服务器与鉴权 Token
|
||||||
pub async fn create_room(&self) -> Result<RoomData, RoomError> {
|
pub async fn create_room(&self) -> Result<RoomData, RoomError> {
|
||||||
let (status, body) = self.post_json("/api/v1/room/create", &serde_json::json!({})).await.map_err(RoomError::Other)?;
|
let (status, body) = self.post_json("/api/v1/room/create", &serde_json::json!({})).await.map_err(RoomError::Other)?;
|
||||||
@@ -231,3 +306,44 @@ mod room_lifetime_tests {
|
|||||||
assert_eq!(room_token_lifetime("h.invalid.s",1000),None);
|
assert_eq!(room_token_lifetime("h.invalid.s",1000),None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod guest_tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn official_request_plain_body_and_sanitized_room_debug() {
|
||||||
|
let client = UUAuthClient::new_guest(Credentials { sign_key: "fixture-key".into(), ..Default::default() },
|
||||||
|
"fixture-guest".into(), "fixture-token".into());
|
||||||
|
let request = client.assist_request("/api/v1/guest/create", None).unwrap();
|
||||||
|
assert_eq!(request.method(), reqwest::Method::POST);
|
||||||
|
assert_eq!(request.body().unwrap().as_bytes(), Some(&b""[..]));
|
||||||
|
let other = client.assist_request("/api/v1/guest/create", Some(&serde_json::json!({}))).unwrap();
|
||||||
|
assert_eq!(other.body().unwrap().as_bytes(), Some(&b"{}"[..]));
|
||||||
|
assert_ne!(request.headers()["x-param-sign"], other.headers()["x-param-sign"]);
|
||||||
|
let room: RoomData = serde_json::from_value(serde_json::json!({"signaling_server":"wss://fixture.invalid", "token":"synthetic-room-secret"})).unwrap();
|
||||||
|
assert!(!format!("{room:?}").contains("synthetic-room-secret"));
|
||||||
|
assert!(serde_json::from_value::<RoomData>(serde_json::json!({"token":42})).is_err());
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn guest_signs_guest_id_and_never_inherits_account_identity() {
|
||||||
|
let account = Credentials { user_id: "fixture-account".into(), token: "fixture-account-token".into(),
|
||||||
|
csrf: "fixture-account-csrf".into(), client_id: "fixture-client".into(),
|
||||||
|
device_id: "fixture-device".into(), sign_key: "fixture-sign-key".into(), ..Default::default() };
|
||||||
|
let client = UUAuthClient::new_guest(account, "fixture-guest".into(), "fixture-guest-token".into());
|
||||||
|
let headers = client.build_headers("POST", "/api/v1/guest/room/create", "");
|
||||||
|
assert_eq!(headers["authorization"], "Bearer fixture-guest-token");
|
||||||
|
assert_eq!(headers["x-param-guest-id"], "fixture-guest");
|
||||||
|
for key in ["x-param-user-id", "cookie", "x-csrf-token"] { assert!(!headers.contains_key(key)); }
|
||||||
|
let params: BTreeMap<String,String> = headers.iter().filter_map(|(key, value)|
|
||||||
|
key.as_str().starts_with("x-param-").then(|| (key.to_string(), value.to_str().unwrap().to_owned()))).collect();
|
||||||
|
assert_eq!(headers["x-param-sign"], client.sign_request("POST", "/api/v1/guest/room/create", ¶ms, ""));
|
||||||
|
let mut without_guest = params; without_guest.remove("x-param-guest-id");
|
||||||
|
assert_ne!(headers["x-param-sign"], client.sign_request("POST", "/api/v1/guest/room/create", &without_guest, ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn transport_error_redacts_proxy_credentials_and_queries() {
|
||||||
|
let text = super::redact_urls("proxy http://user:[email protected]:3128 failed for (https://api.example/p?token=abc)");
|
||||||
|
assert_eq!(text, "proxy http://proxy.example:3128 failed for (https://api.example/p)");
|
||||||
|
}
|
||||||
|
}
|
||||||
+18
-6
@@ -27,17 +27,20 @@ pub fn h264_device_capability(
|
|||||||
fps: i32,
|
fps: i32,
|
||||||
codec_impl: i32,
|
codec_impl: i32,
|
||||||
) -> Value {
|
) -> Value {
|
||||||
|
device_capability(client_id, ice_id, display_id, fps, codec_impl, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn device_capability(client_id: &str, ice_id: &str, display_id: i32, fps: i32, codec_impl: i32, hevc: bool) -> Value {
|
||||||
|
let mut codecs = vec![json!({"video_codec": VIDEO_CODEC_H264, "chroma_sampling": 1,
|
||||||
|
"codec_impl": codec_impl, "bit_depth": 8})];
|
||||||
|
// Official VideoCodecCapability::SerializeJson uses the same fields for CodecType H265=2.
|
||||||
|
if hevc { codecs.push(json!({"video_codec": 2, "chroma_sampling": 1, "codec_impl": codec_impl, "bit_depth": 8})); }
|
||||||
json!({
|
json!({
|
||||||
"client_id": client_id,
|
"client_id": client_id,
|
||||||
"type": "device_capability",
|
"type": "device_capability",
|
||||||
"data": {
|
"data": {
|
||||||
"display_info": [{"id": display_id, "fps": fps, "type": 0, "hdr": 0}],
|
"display_info": [{"id": display_id, "fps": fps, "type": 0, "hdr": 0}],
|
||||||
"video_codec_capability": [{
|
"video_codec_capability": codecs,
|
||||||
"video_codec": VIDEO_CODEC_H264,
|
|
||||||
"chroma_sampling": 1,
|
|
||||||
"codec_impl": codec_impl,
|
|
||||||
"bit_depth": 8
|
|
||||||
}],
|
|
||||||
"ice_id": ice_id
|
"ice_id": ice_id
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -47,6 +50,15 @@ pub fn h264_device_capability(
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn hevc_capability_uses_official_codec_enum_and_420_only() {
|
||||||
|
let payload = device_capability("client-example", "ice-example", 0, 60, 5, true);
|
||||||
|
assert_eq!(payload["data"]["video_codec_capability"], json!([
|
||||||
|
{"video_codec":1,"chroma_sampling":1,"codec_impl":5,"bit_depth":8},
|
||||||
|
{"video_codec":2,"chroma_sampling":1,"codec_impl":5,"bit_depth":8}]));
|
||||||
|
assert_eq!(device_capability("c", "i", 0, 60, 5, false)["data"]["video_codec_capability"].as_array().unwrap().len(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn ice_finished_keeps_string_identifiers_and_exact_fields() {
|
fn ice_finished_keeps_string_identifiers_and_exact_fields() {
|
||||||
let payload = ice_finished("ice-example", "control-example");
|
let payload = ice_finished("ice-example", "control-example");
|
||||||
|
|||||||
+36
-10
@@ -6,6 +6,7 @@ use std::sync::Mutex;
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
fn x11_init(display_name: *const libc::c_char) -> libc::c_int;
|
fn x11_init(display_name: *const libc::c_char) -> libc::c_int;
|
||||||
fn x11_get_dimensions(out_w: *mut libc::c_int, out_h: *mut libc::c_int) -> libc::c_int;
|
fn x11_get_dimensions(out_w: *mut libc::c_int, out_h: *mut libc::c_int) -> libc::c_int;
|
||||||
|
fn x11_refresh_geometry() -> libc::c_int;
|
||||||
fn x11_capture_frame(
|
fn x11_capture_frame(
|
||||||
out_buffer: *mut libc::c_uchar,
|
out_buffer: *mut libc::c_uchar,
|
||||||
out_w: *mut libc::c_int,
|
out_w: *mut libc::c_int,
|
||||||
@@ -37,10 +38,22 @@ unsafe fn x11_capture_frame(
|
|||||||
}
|
}
|
||||||
#[cfg(not(target_os = "linux"))]
|
#[cfg(not(target_os = "linux"))]
|
||||||
unsafe fn x11_close() {}
|
unsafe fn x11_close() {}
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
|
unsafe fn x11_refresh_geometry() -> libc::c_int { 0 }
|
||||||
|
|
||||||
#[cfg(not(target_os = "linux"))]
|
#[cfg(not(target_os = "linux"))]
|
||||||
unsafe fn x11_display_name() -> *const libc::c_char { b":0\0".as_ptr().cast() }
|
unsafe fn x11_display_name() -> *const libc::c_char { b":0\0".as_ptr().cast() }
|
||||||
|
|
||||||
|
/// The X11 root can be resized while the host runs (RandR, xrdp reconnects); the
|
||||||
|
/// geometry read at startup then makes x11grab reject every new connection.
|
||||||
|
pub fn x11_resized() -> bool {
|
||||||
|
match unsafe { x11_refresh_geometry() } {
|
||||||
|
1 => true,
|
||||||
|
0 => false,
|
||||||
|
code => { log::warn!("X11 geometry query failed with code {code}"); false }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct ScreenCapture {
|
pub struct ScreenCapture {
|
||||||
pub backend: crate::desktop_backend::DesktopBackend,
|
pub backend: crate::desktop_backend::DesktopBackend,
|
||||||
pub init_error: Option<String>,
|
pub init_error: Option<String>,
|
||||||
@@ -53,24 +66,27 @@ pub struct ScreenCapture {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ScreenCapture {
|
impl ScreenCapture {
|
||||||
|
pub fn open_portal(cancelled: &std::sync::atomic::AtomicBool) -> Result<Self, crate::portal::PortalError> {
|
||||||
|
let session = std::sync::Arc::new(crate::portal::PortalSession::open_cancellable(cancelled)?);
|
||||||
|
Ok(Self { backend: crate::desktop_backend::DesktopBackend::Portal, init_error: None,
|
||||||
|
display: "wayland:portal".into(), width: session.width, height: session.height,
|
||||||
|
portal: Some(session), kms: None, buffer: Mutex::new(Vec::new()) })
|
||||||
|
}
|
||||||
pub fn new() -> Result<Self, String> {
|
pub fn new() -> Result<Self, String> {
|
||||||
let backend=crate::desktop_backend::detect()?;
|
let backend=crate::desktop_backend::detect()?;
|
||||||
if backend == crate::desktop_backend::DesktopBackend::Kms {
|
if backend == crate::desktop_backend::DesktopBackend::Kms {
|
||||||
let kms=crate::kms::Capture::open()?;
|
let kms=crate::kms::Capture::open()?;
|
||||||
let (width,height)=(kms.width,kms.height);
|
let (width,height)=(kms.width,kms.height);
|
||||||
return Ok(Self { backend,init_error:None,portal:None,display:"kms:linear".into(),width,height,
|
return Ok(Self { backend,init_error:None,portal:None,display:"kms:linear".into(),width,height,
|
||||||
buffer:Mutex::new(vec![0;width as usize*height as usize*4]),kms:Some(kms) });
|
buffer:Mutex::new(Vec::new()),kms:Some(kms) });
|
||||||
}
|
}
|
||||||
if backend == crate::desktop_backend::DesktopBackend::Portal {
|
if backend == crate::desktop_backend::DesktopBackend::Portal {
|
||||||
let session=std::sync::Arc::new(crate::portal::PortalSession::open()?);
|
return Self::open_portal(&std::sync::atomic::AtomicBool::new(false)).map_err(|error| error.to_string());
|
||||||
let (width,height)=(session.width,session.height);
|
|
||||||
return Ok(Self { backend,init_error:None,portal:Some(session),kms:None,display:"wayland:portal".into(),width,height,
|
|
||||||
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
|
|
||||||
}
|
}
|
||||||
if backend == crate::desktop_backend::DesktopBackend::Wayland {
|
if backend == crate::desktop_backend::DesktopBackend::Wayland {
|
||||||
let (width,height)=crate::wayland::init()?;
|
let (width,height)=crate::wayland::init()?;
|
||||||
return Ok(Self { backend, init_error:None, portal:None, kms:None, display:"wayland:native".into(), width,height,
|
return Ok(Self { backend, init_error:None, portal:None, kms:None, display:"wayland:native".into(), width,height,
|
||||||
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
|
buffer:Mutex::new(Vec::new()) });
|
||||||
}
|
}
|
||||||
let ret = unsafe { x11_init(std::ptr::null()) };
|
let ret = unsafe { x11_init(std::ptr::null()) };
|
||||||
if ret != 0 {
|
if ret != 0 {
|
||||||
@@ -85,14 +101,14 @@ impl ScreenCapture {
|
|||||||
return Err(format!("Invalid screen dimensions: {}x{}", w, h));
|
return Err(format!("Invalid screen dimensions: {}x{}", w, h));
|
||||||
}
|
}
|
||||||
|
|
||||||
let buf_size = (w * h * 4) as usize;
|
let buffer = Mutex::new(Vec::new());
|
||||||
let buffer = Mutex::new(vec![0u8; buf_size]);
|
|
||||||
|
|
||||||
log::info!("ScreenCapture initialized: {}x{}", w, h);
|
log::info!("ScreenCapture initialized: {}x{}", w, h);
|
||||||
let display_ptr = unsafe { x11_display_name() };
|
let display_ptr = unsafe { x11_display_name() };
|
||||||
if display_ptr.is_null() { return Err("Capture display name unavailable".into()); }
|
if display_ptr.is_null() { return Err("Capture display name unavailable".into()); }
|
||||||
let display = unsafe { std::ffi::CStr::from_ptr(display_ptr) }.to_string_lossy().into_owned();
|
let display = unsafe { std::ffi::CStr::from_ptr(display_ptr) }.to_string_lossy().into_owned();
|
||||||
log::info!("Selected capture display: {}", display);
|
log::info!("Selected capture display: {}", display);
|
||||||
|
X11_USERS.fetch_add(1, std::sync::atomic::Ordering::AcqRel);
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
backend,
|
backend,
|
||||||
init_error:None,
|
init_error:None,
|
||||||
@@ -115,6 +131,7 @@ impl ScreenCapture {
|
|||||||
pub fn capture_frame(&self) -> Result<Vec<u8>, String> {
|
pub fn capture_frame(&self) -> Result<Vec<u8>, String> {
|
||||||
if let Some(error)=&self.init_error { return Err(error.clone()); }
|
if let Some(error)=&self.init_error { return Err(error.clone()); }
|
||||||
let mut buf = self.buffer.lock().unwrap();
|
let mut buf = self.buffer.lock().unwrap();
|
||||||
|
buf.resize(self.width as usize * self.height as usize * 4, 0);
|
||||||
if let Some(kms)=&self.kms { kms.capture(&mut buf)?; return Ok(buf.clone()); }
|
if let Some(kms)=&self.kms { kms.capture(&mut buf)?; return Ok(buf.clone()); }
|
||||||
if let Some(session)=&self.portal { session.capture(&mut buf)?; return Ok(buf.clone()); }
|
if let Some(session)=&self.portal { session.capture(&mut buf)?; return Ok(buf.clone()); }
|
||||||
if self.backend == crate::desktop_backend::DesktopBackend::Wayland {
|
if self.backend == crate::desktop_backend::DesktopBackend::Wayland {
|
||||||
@@ -126,6 +143,7 @@ impl ScreenCapture {
|
|||||||
|
|
||||||
let ret = unsafe { x11_capture_frame(buf.as_mut_ptr(), &mut w, &mut h) };
|
let ret = unsafe { x11_capture_frame(buf.as_mut_ptr(), &mut w, &mut h) };
|
||||||
if ret != 0 {
|
if ret != 0 {
|
||||||
|
if ret == -4 { return Err(format!("desktop resized to {w}x{h}; reconnect to capture the new size")); }
|
||||||
return Err(format!("x11_capture_frame failed with code {}", ret));
|
return Err(format!("x11_capture_frame failed with code {}", ret));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,16 +151,24 @@ impl ScreenCapture {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The X11 engine keeps one process-wide display connection shared by every ScreenCapture.
|
||||||
|
/// Only the last one may close it: a short-lived capture (device init's screen size during guest
|
||||||
|
/// assistance, or the replaced capture after a resize) used to close it under the live stream.
|
||||||
|
static X11_USERS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||||
|
|
||||||
impl Drop for ScreenCapture {
|
impl Drop for ScreenCapture {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if self.init_error.is_none() && self.backend==crate::desktop_backend::DesktopBackend::X11 { unsafe { x11_close() }; }
|
if self.init_error.is_none() && self.backend==crate::desktop_backend::DesktopBackend::X11
|
||||||
|
&& X11_USERS.fetch_sub(1, std::sync::atomic::Ordering::AcqRel) == 1 { unsafe { x11_close() }; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Keep the display visible only while a peer is streaming; never unlock it.
|
/// Keep the display visible only while a peer is streaming; never unlock it.
|
||||||
pub fn keep_display_awake() -> Result<(), String> {
|
pub fn keep_display_awake() -> Result<(), String> {
|
||||||
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Kms { return Ok(()); }
|
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Kms { return Ok(()); }
|
||||||
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Portal { return Ok(()); }
|
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Portal {
|
||||||
|
return crate::desktop_power::wake();
|
||||||
|
}
|
||||||
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Wayland {
|
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Wayland {
|
||||||
return crate::wayland::wake();
|
return crate::wayland::wake();
|
||||||
}
|
}
|
||||||
|
|||||||
+243
-8
@@ -19,24 +19,40 @@ pub const QUALITY_AUTO: u64 = 5;
|
|||||||
pub const QUALITY_CUSTOM: u64 = 6;
|
pub const QUALITY_CUSTOM: u64 = 6;
|
||||||
|
|
||||||
/// FeatureFlag.ff_capture_setting we declare: fps, preset quality and custom bitrate.
|
/// FeatureFlag.ff_capture_setting we declare: fps, preset quality and custom bitrate.
|
||||||
/// (Chroma 4:4:4, DPI scaling and HDR are higher versions and are not implemented.)
|
/// Versions are cumulative (official FF_CaptureSetting enum: 3 CHROMA_SAMPLING, 4 CUSTOM_BITRATE,
|
||||||
|
/// 5 DPI_SCALE, 6 HDR), so 4 also tells the viewer we accept a chroma format (真彩).
|
||||||
pub const FEATURE_VERSION: u64 = 4;
|
pub const FEATURE_VERSION: u64 = 4;
|
||||||
|
|
||||||
|
/// Official GvPb_ChromaFormat values (CaptureSettingRequest field 7, CaptureSettingSync field 3).
|
||||||
|
pub const CHROMA_420: u64 = 1;
|
||||||
|
pub const CHROMA_444: u64 = 3;
|
||||||
|
|
||||||
const MIN_FPS: u32 = 5;
|
const MIN_FPS: u32 = 5;
|
||||||
const MAX_FPS: u32 = 144;
|
const MAX_FPS: u32 = 144;
|
||||||
|
|
||||||
|
/// Official GvPb_CodecType, CaptureSettingRequest.codec_type (field 13).
|
||||||
|
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum VideoCodec { #[default] H264, H265 }
|
||||||
|
impl VideoCodec {
|
||||||
|
pub fn wire(self) -> u64 { match self { Self::H264 => 1, Self::H265 => 2 } }
|
||||||
|
pub fn mime(self) -> &'static str { match self { Self::H264 => "video/H264", Self::H265 => "video/H265" } }
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub struct VideoSettings {
|
pub struct VideoSettings {
|
||||||
pub fps: u32,
|
pub fps: u32,
|
||||||
|
pub codec: VideoCodec,
|
||||||
pub quality: u64,
|
pub quality: u64,
|
||||||
/// Viewer max_custom_bitrate (only used for QUALITY_CUSTOM): Mbit/s, or kbit/s above 1000.
|
/// Viewer max_custom_bitrate (only used for QUALITY_CUSTOM): Mbit/s, or kbit/s above 1000.
|
||||||
pub custom_mbps: u32,
|
pub custom_mbps: u32,
|
||||||
/// Scaled stream size (其他分辨率); None streams the native desktop size.
|
/// Scaled stream size (其他分辨率); None streams the native desktop size.
|
||||||
pub output: Option<(u32, u32)>,
|
pub output: Option<(u32, u32)>,
|
||||||
|
/// CHROMA_420 or CHROMA_444 (真彩); 4:4:4 only when the encoder pipeline supports it.
|
||||||
|
pub chroma: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for VideoSettings {
|
impl Default for VideoSettings {
|
||||||
fn default() -> Self { Self { fps: 60, quality: QUALITY_AUTO, custom_mbps: 0, output: None } }
|
fn default() -> Self { Self { codec: VideoCodec::H264, fps: 60, quality: QUALITY_AUTO, custom_mbps: 0, output: None, chroma: CHROMA_420 } }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Selectable smaller stream sizes with the desktop's aspect ratio (even dimensions).
|
/// Selectable smaller stream sizes with the desktop's aspect ratio (even dimensions).
|
||||||
@@ -95,6 +111,21 @@ fn fps_from_option(value: u64) -> Option<u64> {
|
|||||||
/// Applies a capture-setting RPC to `settings` and returns the RpcResponse payload (Message 22),
|
/// Applies a capture-setting RPC to `settings` and returns the RpcResponse payload (Message 22),
|
||||||
/// or None when the RPC is not a capture/fps/quality setting.
|
/// or None when the RPC is not a capture/fps/quality setting.
|
||||||
pub fn handle_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native: (u32, u32)) -> Option<Vec<u8>> {
|
pub fn handle_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native: (u32, u32)) -> Option<Vec<u8>> {
|
||||||
|
handle_rpc_with_codecs(rpc, settings, native, true, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Initial codec once the m-line is negotiated: H.265 when both sides support it (the Mac viewer
|
||||||
|
/// lists H265 first in its offer), unless UU_VIDEO_CODEC=h264.
|
||||||
|
pub fn preferred_codec(h264: bool, h265: bool) -> VideoCodec {
|
||||||
|
let forced_h264 = std::env::var("UU_VIDEO_CODEC").is_ok_and(|v| v.eq_ignore_ascii_case("h264"));
|
||||||
|
if h265 && !(forced_h264 && h264) { VideoCodec::H265 } else { VideoCodec::H264 }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn handle_rpc_with_codecs(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native: (u32, u32), h264: bool, h265: bool) -> Option<Vec<u8>> {
|
||||||
|
apply_rpc(rpc, settings, native, h264, h265, crate::video::full_chroma_available())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native: (u32, u32), h264: bool, h265: bool, full_chroma: bool) -> Option<Vec<u8>> {
|
||||||
let header = message(rpc, 1).unwrap_or(&[]);
|
let header = message(rpc, 1).unwrap_or(&[]);
|
||||||
let mut next = *settings.borrow();
|
let mut next = *settings.borrow();
|
||||||
if let Some(request) = message(rpc, 2) {
|
if let Some(request) = message(rpc, 2) {
|
||||||
@@ -102,11 +133,22 @@ pub fn handle_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, na
|
|||||||
// Numbers only: fps, quality, bitrate, resolution and flags carry no user content.
|
// Numbers only: fps, quality, bitrate, resolution and flags carry no user content.
|
||||||
log::info!("Capture setting request: {:?}", fields.iter().filter_map(|f| match f.value {
|
log::info!("Capture setting request: {:?}", fields.iter().filter_map(|f| match f.value {
|
||||||
WireValue::Varint(v) => Some((f.number, v)), _ => None }).collect::<Vec<_>>());
|
WireValue::Varint(v) => Some((f.number, v)), _ => None }).collect::<Vec<_>>());
|
||||||
|
// codec_type 13 (1 H264, 2 H265): like the official host (negotiateCodec), the host picks the
|
||||||
|
// codec from both capability lists; the Mac viewer sends its current codec (1 while receiving
|
||||||
|
// H.264), so only an explicit H.265 request switches, never one absent from the m-line.
|
||||||
|
if number(&fields, 13) == Some(2) && h265 { next.codec = VideoCodec::H265; }
|
||||||
// Field 1 is the viewer's fps option index (1 = 30, 2 = 60, ...); 18 fpsCount is the frame rate.
|
// Field 1 is the viewer's fps option index (1 = 30, 2 = 60, ...); 18 fpsCount is the frame rate.
|
||||||
if let Some(fps) = number(&fields, 18).filter(|v| *v > 0).or_else(|| number(&fields, 1).and_then(fps_from_option)) { next.fps = fps as u32; }
|
if let Some(fps) = number(&fields, 18).filter(|v| *v > 0).or_else(|| number(&fields, 1).and_then(fps_from_option)) { next.fps = fps as u32; }
|
||||||
if let Some(quality) = number(&fields, 2).filter(|v| *v > 0) { next.quality = quality; }
|
if let Some(quality) = number(&fields, 2).filter(|v| *v > 0) { next.quality = quality; }
|
||||||
if let Some(bitrate) = number(&fields, 8).filter(|v| *v > 0) { next.custom_mbps = bitrate as u32; }
|
if let Some(bitrate) = number(&fields, 8).filter(|v| *v > 0) { next.custom_mbps = bitrate as u32; }
|
||||||
// ChooseResolutionType 3 = follow remote (native); otherwise pixel size, else logical size.
|
// Field 7 chorma_format (sic): 3 = 4:4:4 when 真彩 is on, 1 = 4:2:0. The Sync reports what is encoded.
|
||||||
|
match number(&fields, 7) {
|
||||||
|
Some(CHROMA_444) if full_chroma => next.chroma = CHROMA_444,
|
||||||
|
Some(CHROMA_444) => { log::info!("Chroma 4:4:4 requested; this encoder pipeline only supports 4:2:0"); next.chroma = CHROMA_420; }
|
||||||
|
Some(CHROMA_420) => next.chroma = CHROMA_420,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
// ChooseResolutionType 3 = follow remote (native); v4 uses pixels in both size pairs.
|
||||||
if number(&fields, 10) == Some(3) { next.output = None; }
|
if number(&fields, 10) == Some(3) { next.output = None; }
|
||||||
else if let Some((w, h)) = number(&fields, 16).zip(number(&fields, 17)).or_else(|| number(&fields, 5).zip(number(&fields, 6))) {
|
else if let Some((w, h)) = number(&fields, 16).zip(number(&fields, 17)).or_else(|| number(&fields, 5).zip(number(&fields, 6))) {
|
||||||
next.output = clamp_output(native, w, h);
|
next.output = clamp_output(native, w, h);
|
||||||
@@ -129,7 +171,7 @@ pub fn handle_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, na
|
|||||||
}
|
}
|
||||||
next.fps = next.fps.clamp(MIN_FPS, MAX_FPS);
|
next.fps = next.fps.clamp(MIN_FPS, MAX_FPS);
|
||||||
settings.send_if_modified(|current| if *current != next {
|
settings.send_if_modified(|current| if *current != next {
|
||||||
log::info!("Desktop video settings: fps={} quality={} max_kbps={:?} output={:?}", next.fps, next.quality, next.max_kbps(), next.output);
|
log::info!("Desktop video settings: fps={} quality={} max_kbps={:?} output={:?} chroma={}", next.fps, next.quality, next.max_kbps(), next.output, next.chroma);
|
||||||
*current = next; true
|
*current = next; true
|
||||||
} else { false });
|
} else { false });
|
||||||
let mut out = Vec::new();
|
let mut out = Vec::new();
|
||||||
@@ -138,13 +180,15 @@ pub fn handle_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, na
|
|||||||
Some(out)
|
Some(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Message 7 ScreenSources for the single desktop: native size, scaled choices, current size.
|
/// Message 7 ScreenSources in pixels for capture-setting v4: native, choices, current size.
|
||||||
pub fn screen_sources(seq: u64, timestamp: u64, native: (u32, u32), output: Option<(u32, u32)>, name: &str) -> Result<Vec<u8>, String> {
|
pub fn screen_sources(seq: u64, timestamp: u64, native: (u32, u32), output: Option<(u32, u32)>, name: &str) -> Result<Vec<u8>, String> {
|
||||||
use crate::initialization::{Screen, WinRect};
|
use crate::initialization::{Screen, WinRect};
|
||||||
let (w, h) = (native.0 as i32, native.1 as i32);
|
let (w, h) = (native.0 as i32, native.1 as i32);
|
||||||
let scaled: Vec<(i32, i32)> = scaled_sizes(native).into_iter().map(|(w, h)| (w as i32, h as i32)).collect();
|
let scaled: Vec<(i32, i32)> = scaled_sizes(native).into_iter().map(|(w, h)| (w as i32, h as i32)).collect();
|
||||||
|
// DPI_SCALE starts at version 5; the observed v4 request uses fields 5/6 as pixels.
|
||||||
|
// Keep every advertised rectangle in that space; Portal/EIS maps input independently.
|
||||||
let resolution = WinRect { left: 0, top: 0, width: w, height: h, pixel_width: w, pixel_height: h };
|
let resolution = WinRect { left: 0, top: 0, width: w, height: h, pixel_width: w, pixel_height: h };
|
||||||
let screens = [Screen { id: 0, fps: crate::video::FPS as i32, resolution, primary: true, dpr: 1.0,
|
let screens = [Screen { id: 0, fps: crate::video::display_fps() as i32, resolution, primary: true, dpr: 1.0,
|
||||||
display_name: name, video_track_index: 0, screen_type: 0, builtin_screen_type: 0,
|
display_name: name, video_track_index: 0, screen_type: 0, builtin_screen_type: 0,
|
||||||
scaled: &scaled, current: output.map(|(w, h)| (w as i32, h as i32)) }];
|
scaled: &scaled, current: output.map(|(w, h)| (w as i32, h as i32)) }];
|
||||||
crate::initialization::encode_screen_sources(seq, timestamp, &screens, 0)
|
crate::initialization::encode_screen_sources(seq, timestamp, &screens, 0)
|
||||||
@@ -156,7 +200,7 @@ pub fn sync_payload(settings: &VideoSettings, native: (u32, u32)) -> Vec<u8> {
|
|||||||
let mut out = Vec::new();
|
let mut out = Vec::new();
|
||||||
put_varint(1, u64::from(settings.fps), &mut out);
|
put_varint(1, u64::from(settings.fps), &mut out);
|
||||||
put_varint(2, settings.quality, &mut out);
|
put_varint(2, settings.quality, &mut out);
|
||||||
put_varint(3, 1, &mut out); // ChromaFormat_420
|
put_varint(3, settings.chroma, &mut out);
|
||||||
if settings.custom_mbps > 0 { put_varint(4, u64::from(settings.custom_mbps), &mut out); }
|
if settings.custom_mbps > 0 { put_varint(4, u64::from(settings.custom_mbps), &mut out); }
|
||||||
put_varint(5, u64::from(size.0), &mut out);
|
put_varint(5, u64::from(size.0), &mut out);
|
||||||
put_varint(6, u64::from(size.1), &mut out);
|
put_varint(6, u64::from(size.1), &mut out);
|
||||||
@@ -165,6 +209,23 @@ pub fn sync_payload(settings: &VideoSettings, native: (u32, u32)) -> Vec<u8> {
|
|||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Message 18 CodecNegotiation {1 encoder_cap_list: EncoderCap {1 capture_mode (1 DESKTOP), 2 fps,
|
||||||
|
/// 3 codec_type, 4 width, 5 height}} (official Windows host descriptor). The viewer's own list is
|
||||||
|
/// ConnectOptions field 4 decoder_cap_list; it only selects a codec the host announces here.
|
||||||
|
pub fn codec_negotiation(size: (u32, u32), fps: u32, hevc: bool) -> Vec<u8> {
|
||||||
|
let mut out = Vec::new();
|
||||||
|
for codec in [Some(VideoCodec::H264), hevc.then_some(VideoCodec::H265)].into_iter().flatten() {
|
||||||
|
let mut cap = Vec::new();
|
||||||
|
put_varint(1, 1, &mut cap);
|
||||||
|
put_varint(2, u64::from(fps), &mut cap);
|
||||||
|
put_varint(3, codec.wire(), &mut cap);
|
||||||
|
put_varint(4, u64::from(size.0), &mut cap);
|
||||||
|
put_varint(5, u64::from(size.1), &mut cap);
|
||||||
|
put_bytes(1, &cap, &mut out);
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -187,7 +248,7 @@ mod tests {
|
|||||||
put_varint(18, 30, &mut body);
|
put_varint(18, 30, &mut body);
|
||||||
let request = rpc(77, 2, &body);
|
let request = rpc(77, 2, &body);
|
||||||
let response = handle_rpc(&decode_wire(&request).unwrap(), &tx, (1920, 1080)).unwrap();
|
let response = handle_rpc(&decode_wire(&request).unwrap(), &tx, (1920, 1080)).unwrap();
|
||||||
assert_eq!(*rx.borrow(), VideoSettings { fps: 30, quality: QUALITY_GENERAL, custom_mbps: 0, output: None });
|
assert_eq!(*rx.borrow(), VideoSettings { codec: VideoCodec::H264, fps: 30, quality: QUALITY_GENERAL, custom_mbps: 0, output: None, chroma: CHROMA_420 });
|
||||||
assert_eq!(rx.borrow().max_kbps(), Some(2_000));
|
assert_eq!(rx.borrow().max_kbps(), Some(2_000));
|
||||||
let fields = decode_wire(&response).unwrap();
|
let fields = decode_wire(&response).unwrap();
|
||||||
assert_eq!(message(&fields, 1), Some(&[8u8, 77][..]));
|
assert_eq!(message(&fields, 1), Some(&[8u8, 77][..]));
|
||||||
@@ -208,6 +269,29 @@ mod tests {
|
|||||||
assert!(handle_rpc(&decode_wire(&rpc(3, 9, &[])).unwrap(), &tx, (1920, 1080)).is_none());
|
assert!(handle_rpc(&decode_wire(&rpc(3, 9, &[])).unwrap(), &tx, (1920, 1080)).is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn true_color_request_selects_444_only_when_the_encoder_supports_it() {
|
||||||
|
let (tx, rx) = watch::channel(VideoSettings::default());
|
||||||
|
// Observed Mac viewer request with 真彩 on: field 7 = 3.
|
||||||
|
let request = rpc(5, 2, &[8, 2, 16, 4, 56, 3, 144, 1, 60]);
|
||||||
|
apply_rpc(&decode_wire(&request).unwrap(), &tx, (1920, 1080), true, false, false).unwrap();
|
||||||
|
assert_eq!(rx.borrow().chroma, CHROMA_420);
|
||||||
|
apply_rpc(&decode_wire(&request).unwrap(), &tx, (1920, 1080), true, false, true).unwrap();
|
||||||
|
assert_eq!(rx.borrow().chroma, CHROMA_444);
|
||||||
|
assert_eq!(number(&decode_wire(&sync_payload(&rx.borrow(), (1920, 1080))).unwrap(), 3), Some(CHROMA_444));
|
||||||
|
apply_rpc(&decode_wire(&rpc(6, 2, &[56, 1])).unwrap(), &tx, (1920, 1080), true, false, true).unwrap();
|
||||||
|
assert_eq!(rx.borrow().chroma, CHROMA_420);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn codec_negotiation_lists_h264_then_optional_h265() {
|
||||||
|
assert_eq!(codec_negotiation((1920, 1080), 60, false), [10, 12, 8, 1, 16, 60, 24, 1, 32, 128, 15, 40, 184, 8]);
|
||||||
|
let both = codec_negotiation((1920, 1080), 60, true);
|
||||||
|
let caps: Vec<_> = decode_wire(&both).unwrap().iter().filter_map(|f| match f.value { WireValue::Bytes(b) => Some(b), _ => None })
|
||||||
|
.map(|b| number(&decode_wire(b).unwrap(), 3)).collect();
|
||||||
|
assert_eq!(caps, [Some(1), Some(2)]);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn resolution_requests_scale_below_native_only() {
|
fn resolution_requests_scale_below_native_only() {
|
||||||
let (tx, rx) = watch::channel(VideoSettings::default());
|
let (tx, rx) = watch::channel(VideoSettings::default());
|
||||||
@@ -218,6 +302,133 @@ mod tests {
|
|||||||
assert_eq!(scaled_sizes((1920, 1080)), vec![(1600, 900), (1280, 720), (960, 540), (640, 360)]);
|
assert_eq!(scaled_sizes((1920, 1080)), vec![(1600, 900), (1280, 720), (960, 540), (640, 360)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn version_four_native_inventory_roundtrips_without_downscaling() {
|
||||||
|
assert_eq!(FEATURE_VERSION, 4);
|
||||||
|
let native = (3840, 2160);
|
||||||
|
let inventory = screen_sources(1, 2, native, None, "test").unwrap();
|
||||||
|
let envelope = decode_wire(&inventory).unwrap();
|
||||||
|
let sources = decode_wire(message(&envelope, 7).unwrap()).unwrap();
|
||||||
|
let display = decode_wire(message(&sources, 1).unwrap()).unwrap();
|
||||||
|
let rect = decode_wire(message(&display, 4).unwrap()).unwrap();
|
||||||
|
let mut body = Vec::new();
|
||||||
|
put_varint(5, number(&rect, 3).unwrap(), &mut body);
|
||||||
|
put_varint(6, number(&rect, 4).unwrap(), &mut body);
|
||||||
|
put_varint(2, QUALITY_BLURAY, &mut body);
|
||||||
|
let (tx, rx) = watch::channel(VideoSettings::default());
|
||||||
|
handle_rpc(&decode_wire(&rpc(77, 2, &body)).unwrap(), &tx, native).unwrap();
|
||||||
|
assert_eq!(rx.borrow().output, None, "native selection must preserve the full frame");
|
||||||
|
let sync = sync_payload(&rx.borrow(), native);
|
||||||
|
let fields = decode_wire(&sync).unwrap();
|
||||||
|
assert_eq!(number(&fields, 5), Some(3840));
|
||||||
|
assert_eq!(number(&fields, 6), Some(2160));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn advertised_display(native: (u32, u32), output: Option<(u32, u32)>) -> Vec<u8> {
|
||||||
|
let inventory = screen_sources(1, 2, native, output, "test").unwrap();
|
||||||
|
let envelope = decode_wire(&inventory).unwrap();
|
||||||
|
let sources = decode_wire(message(&envelope, 7).unwrap()).unwrap();
|
||||||
|
message(&sources, 1).unwrap().to_vec()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn version_four_resolution_choices_and_settings_roundtrip_in_pixels() {
|
||||||
|
assert_eq!(FEATURE_VERSION, 4);
|
||||||
|
for native in [(1920, 1080), (2560, 1440), (3840, 2160), (5120, 2880),
|
||||||
|
(7680, 4320), (3440, 1440), (2160, 3840), (2257, 1505)] {
|
||||||
|
let display = advertised_display(native, None);
|
||||||
|
let fields = decode_wire(&display).unwrap();
|
||||||
|
assert!(fields.iter().any(|f| f.number == 8 && f.value == WireValue::Fixed64(1.0f64.to_le_bytes())));
|
||||||
|
assert_eq!(message(&fields, 6), message(&fields, 4));
|
||||||
|
let choices: Vec<_> = fields.iter().filter_map(|f| match f.value {
|
||||||
|
WireValue::Bytes(rect) if f.number == 3 => Some(rect), _ => None,
|
||||||
|
}).collect();
|
||||||
|
assert_eq!(choices.len(), 1 + scaled_sizes(native).len());
|
||||||
|
assert_eq!(choices[0], message(&fields, 6).unwrap());
|
||||||
|
for rect in choices {
|
||||||
|
let rect = decode_wire(rect).unwrap();
|
||||||
|
let (w, h) = (number(&rect, 3).unwrap(), number(&rect, 4).unwrap());
|
||||||
|
assert_eq!(number(&rect, 5), Some(w));
|
||||||
|
assert_eq!(number(&rect, 6), Some(h));
|
||||||
|
let selected = (w as u32, h as u32);
|
||||||
|
let expected = (selected != native).then_some(selected);
|
||||||
|
// Exercise both the observed legacy capture request and the resolution-only RPC.
|
||||||
|
for case in [2, 6] {
|
||||||
|
let (tx, rx) = watch::channel(VideoSettings::default());
|
||||||
|
let mut body = Vec::new();
|
||||||
|
let (wf, hf) = if case == 2 { (5, 6) } else { (1, 2) };
|
||||||
|
put_varint(wf, w, &mut body); put_varint(hf, h, &mut body);
|
||||||
|
if case == 2 {
|
||||||
|
put_varint(2, QUALITY_GENERAL, &mut body);
|
||||||
|
put_varint(18, 30, &mut body);
|
||||||
|
}
|
||||||
|
let response = handle_rpc(&decode_wire(&rpc(77, case, &body)).unwrap(), &tx, native).unwrap();
|
||||||
|
let response = decode_wire(&response).unwrap();
|
||||||
|
assert_eq!(message(&response, 1), Some(&[8u8, 77][..]));
|
||||||
|
assert_eq!(message(&response, 2), Some(&[][..]));
|
||||||
|
assert_eq!(rx.borrow().output, expected);
|
||||||
|
if case == 2 {
|
||||||
|
assert_eq!(rx.borrow().quality, QUALITY_GENERAL);
|
||||||
|
assert_eq!(rx.borrow().fps, 30);
|
||||||
|
}
|
||||||
|
let sync = sync_payload(&rx.borrow(), native);
|
||||||
|
let sync = decode_wire(&sync).unwrap();
|
||||||
|
for (wf, hf) in [(5, 6), (9, 10)] {
|
||||||
|
assert_eq!(number(&sync, wf), Some(w));
|
||||||
|
assert_eq!(number(&sync, hf), Some(h));
|
||||||
|
}
|
||||||
|
let updated = advertised_display(native, rx.borrow().output);
|
||||||
|
let updated = decode_wire(&updated).unwrap();
|
||||||
|
assert_eq!(message(&updated, 6), message(&fields, 6));
|
||||||
|
assert_eq!(message(&updated, 3), message(&fields, 3));
|
||||||
|
let current = decode_wire(message(&updated, 4).unwrap()).unwrap();
|
||||||
|
for (wf, hf) in [(3, 4), (5, 6)] {
|
||||||
|
assert_eq!(number(¤t, wf), Some(w));
|
||||||
|
assert_eq!(number(¤t, hf), Some(h));
|
||||||
|
}
|
||||||
|
// Echo synchronized dimensions with a quality/fps change, then restore native.
|
||||||
|
let mut body = Vec::new();
|
||||||
|
put_varint(5, number(&sync, 5).unwrap(), &mut body);
|
||||||
|
put_varint(6, number(&sync, 6).unwrap(), &mut body);
|
||||||
|
put_varint(2, QUALITY_BLURAY, &mut body);
|
||||||
|
put_varint(18, 60, &mut body);
|
||||||
|
handle_rpc(&decode_wire(&rpc(78, 2, &body)).unwrap(), &tx, native).unwrap();
|
||||||
|
assert_eq!(rx.borrow().output, expected);
|
||||||
|
assert_eq!(rx.borrow().quality, QUALITY_BLURAY);
|
||||||
|
assert_eq!(rx.borrow().fps, 60);
|
||||||
|
let mut native_request = Vec::new();
|
||||||
|
put_varint(5, u64::from(native.0), &mut native_request);
|
||||||
|
put_varint(6, u64::from(native.1), &mut native_request);
|
||||||
|
handle_rpc(&decode_wire(&rpc(79, 2, &native_request)).unwrap(), &tx, native).unwrap();
|
||||||
|
assert_eq!(rx.borrow().output, None);
|
||||||
|
handle_rpc(&decode_wire(&rpc(80, 2, &body)).unwrap(), &tx, native).unwrap();
|
||||||
|
assert_eq!(rx.borrow().output, expected);
|
||||||
|
handle_rpc(&decode_wire(&rpc(81, 2, &[80, 3])).unwrap(), &tx, native).unwrap();
|
||||||
|
assert_eq!(rx.borrow().output, None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn explicit_pixel_size_takes_precedence_and_follow_remote_restores_native() {
|
||||||
|
let native = (3840, 2160);
|
||||||
|
let (tx, rx) = watch::channel(VideoSettings { output: Some((1280, 720)), ..Default::default() });
|
||||||
|
let mut body = Vec::new();
|
||||||
|
for (field, value) in [(5, 1920), (6, 1080), (16, 2560), (17, 1440)] {
|
||||||
|
put_varint(field, value, &mut body);
|
||||||
|
}
|
||||||
|
handle_rpc(&decode_wire(&rpc(1, 2, &body)).unwrap(), &tx, native).unwrap();
|
||||||
|
assert_eq!(rx.borrow().output, Some((2560, 1440)));
|
||||||
|
put_varint(10, 3, &mut body);
|
||||||
|
handle_rpc(&decode_wire(&rpc(2, 2, &body)).unwrap(), &tx, native).unwrap();
|
||||||
|
assert_eq!(rx.borrow().output, None);
|
||||||
|
let sync = sync_payload(&rx.borrow(), native);
|
||||||
|
let fields = decode_wire(&sync).unwrap();
|
||||||
|
assert_eq!(number(&fields, 5), Some(3840));
|
||||||
|
assert_eq!(number(&fields, 6), Some(2160));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn auto_quality_keeps_constant_quality_encoding() {
|
fn auto_quality_keeps_constant_quality_encoding() {
|
||||||
assert_eq!(VideoSettings::default().max_kbps(), None);
|
assert_eq!(VideoSettings::default().max_kbps(), None);
|
||||||
@@ -235,3 +446,27 @@ mod tests {
|
|||||||
assert_eq!(number(&fields, 10), Some(1080));
|
assert_eq!(number(&fields, 10), Some(1080));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod codec_tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn codec_is_opt_in_and_requires_negotiated_support() {
|
||||||
|
let (tx, rx) = watch::channel(VideoSettings::default());
|
||||||
|
let rpc = |value| decode_wire(if value == 2 { &[18, 2, 104, 2] } else { &[18, 2, 104, 1] }).unwrap();
|
||||||
|
assert_eq!(rx.borrow().codec, VideoCodec::H264);
|
||||||
|
handle_rpc_with_codecs(&rpc(2), &tx, (1920,1080), true, false).unwrap();
|
||||||
|
assert_eq!(rx.borrow().codec, VideoCodec::H264);
|
||||||
|
handle_rpc_with_codecs(&rpc(2), &tx, (1920,1080), true, true).unwrap();
|
||||||
|
assert_eq!(rx.borrow().codec, VideoCodec::H265);
|
||||||
|
handle_rpc_with_codecs(&rpc(1), &tx, (1920,1080), false, true).unwrap();
|
||||||
|
assert_eq!(rx.borrow().codec, VideoCodec::H265); // unnegotiated H264 is never sent
|
||||||
|
// An unrelated FPS RPC must preserve the explicit codec selection.
|
||||||
|
handle_rpc_with_codecs(&decode_wire(&[26,2,8,1]).unwrap(), &tx, (1920,1080), true, true).unwrap();
|
||||||
|
assert_eq!(rx.borrow().codec, VideoCodec::H265);
|
||||||
|
// The viewer reports its current codec (1) in every request; that must not undo H.265.
|
||||||
|
handle_rpc_with_codecs(&rpc(1), &tx, (1920,1080), true, true).unwrap();
|
||||||
|
assert_eq!(rx.borrow().codec, VideoCodec::H265);
|
||||||
|
assert_eq!(preferred_codec(true, false), VideoCodec::H264);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,217 @@
|
|||||||
|
//! Real local SCTP peers; no cloud, desktop consent, audio or power operations.
|
||||||
|
use super::*;
|
||||||
|
use crate::protocol::{decode_wire, WireValue};
|
||||||
|
use std::{collections::HashMap, time::Duration};
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
|
type Messages = mpsc::UnboundedReceiver<(String, Vec<u8>)>;
|
||||||
|
struct Viewer {
|
||||||
|
pc: Arc<RTCPeerConnection>,
|
||||||
|
channels: HashMap<String, Arc<RTCDataChannel>>,
|
||||||
|
messages: Messages,
|
||||||
|
router: Arc<Mutex<webrtc::util::vnet::router::Router>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn connect(input: Arc<InputInjector>, desktop: bool,
|
||||||
|
portal: Option<Arc<crate::desktop_session::PortalDesktop>>, labels: &[&str]) -> (WebRtcSession, Viewer) {
|
||||||
|
let (host_setting, viewer_setting, router) = crate::test_network::virtual_network().await;
|
||||||
|
let screen = Arc::new(ScreenCapture::unavailable("synthetic locked desktop".into()));
|
||||||
|
let host = WebRtcSession::new(screen, input, crate::signaling::SignalingClient::disconnected(),
|
||||||
|
desktop, crate::session_ice::SessionIce::virtual_net(host_setting), portal).await.unwrap();
|
||||||
|
let pc = Arc::new(APIBuilder::new().with_setting_engine(viewer_setting).build().new_peer_connection(RTCConfiguration::default()).await.unwrap());
|
||||||
|
let (tx, messages) = mpsc::unbounded_channel();
|
||||||
|
let mut channels = HashMap::new();
|
||||||
|
for label in labels {
|
||||||
|
let channel = pc.create_data_channel(label, None).await.unwrap();
|
||||||
|
let (tx, name) = (tx.clone(), label.to_string());
|
||||||
|
channel.on_message(Box::new(move |message| {
|
||||||
|
let _ = tx.send((name.clone(), message.data.to_vec()));
|
||||||
|
Box::pin(async {})
|
||||||
|
}));
|
||||||
|
channels.insert(label.to_string(), channel);
|
||||||
|
}
|
||||||
|
let mut gathered = pc.gathering_complete_promise().await;
|
||||||
|
pc.set_local_description(pc.create_offer(None).await.unwrap()).await.unwrap();
|
||||||
|
gathered.recv().await;
|
||||||
|
host.pc.set_remote_description(pc.local_description().await.unwrap()).await.unwrap();
|
||||||
|
let mut gathered = host.pc.gathering_complete_promise().await;
|
||||||
|
host.pc.set_local_description(host.pc.create_answer(None).await.unwrap()).await.unwrap();
|
||||||
|
gathered.recv().await;
|
||||||
|
pc.set_remote_description(host.pc.local_description().await.unwrap()).await.unwrap();
|
||||||
|
tokio::time::timeout(Duration::from_secs(8), async {
|
||||||
|
while channels.values().any(|channel| channel.ready_state() != webrtc::data_channel::data_channel_state::RTCDataChannelState::Open) {
|
||||||
|
tokio::time::sleep(Duration::from_millis(20)).await;
|
||||||
|
}
|
||||||
|
}).await.expect("local SCTP channels did not open");
|
||||||
|
(host, Viewer { pc, channels, messages, router })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn body(message: &[u8], number: u32) -> Option<&[u8]> {
|
||||||
|
decode_wire(message).ok()?.into_iter().find_map(|field| match (field.number, field.value) {
|
||||||
|
(n, WireValue::Bytes(bytes)) if n == number => Some(bytes), _ => None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
fn bytes(number: u32, value: &[u8]) -> Vec<u8> {
|
||||||
|
fn varint(mut value: usize, out: &mut Vec<u8>) {
|
||||||
|
while value >= 128 { out.push((value as u8 & 127) | 128); value >>= 7; }
|
||||||
|
out.push(value as u8);
|
||||||
|
}
|
||||||
|
let mut out = Vec::new();
|
||||||
|
varint(((number << 3) | 2) as usize, &mut out);
|
||||||
|
varint(value.len(), &mut out);
|
||||||
|
out.extend_from_slice(value);
|
||||||
|
out
|
||||||
|
}
|
||||||
|
async fn receive(messages: &mut Messages, label: &str, field: u32) -> Vec<u8> {
|
||||||
|
tokio::time::timeout(Duration::from_secs(5), async {
|
||||||
|
loop {
|
||||||
|
let (channel, message) = messages.recv().await.expect("peer stopped responding");
|
||||||
|
if channel == label && body(&message, field).is_some() { return message; }
|
||||||
|
}
|
||||||
|
}).await.expect("expected protocol response timed out")
|
||||||
|
}
|
||||||
|
async fn send(viewer: &Viewer, label: &str, message: Vec<u8>) {
|
||||||
|
viewer.channels[label].send(&message.into()).await.unwrap();
|
||||||
|
}
|
||||||
|
fn mapping(kind: u8, payload: &[u8]) -> Vec<u8> {
|
||||||
|
let mut frame = vec![8, 1, 16, 2, 24, 3, 32, kind];
|
||||||
|
frame.extend(bytes(5, payload));
|
||||||
|
crate::initialization::envelope(1, 0, 27, &frame)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn closing_unnegotiated_peers_releases_callback_state() {
|
||||||
|
for desktop in [false, true] {
|
||||||
|
let input = Arc::new(InputInjector::without_desktop());
|
||||||
|
let capture = Arc::new(ScreenCapture::unavailable("synthetic desktop".into()));
|
||||||
|
let peer = WebRtcSession::new(capture, input,
|
||||||
|
crate::signaling::SignalingClient::disconnected(), desktop,
|
||||||
|
crate::session_ice::SessionIce::local(), None).await.unwrap();
|
||||||
|
let weak = Arc::downgrade(&peer.pc);
|
||||||
|
peer.pc.close().await.unwrap();
|
||||||
|
drop(peer);
|
||||||
|
tokio::time::timeout(Duration::from_secs(4), async {
|
||||||
|
while weak.upgrade().is_some() {
|
||||||
|
tokio::time::sleep(Duration::from_millis(20)).await;
|
||||||
|
}
|
||||||
|
}).await.expect("closed peer retained by callbacks");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn locked_control_unlock_requests_and_tools_work_without_a_video_frame() {
|
||||||
|
tokio::time::timeout(Duration::from_secs(35), async {
|
||||||
|
let input = Arc::new(InputInjector::without_desktop());
|
||||||
|
let (lock, locked) = tokio::sync::watch::channel(Some(true));
|
||||||
|
let portal = Arc::new(crate::desktop_session::PortalDesktop::with_lock_state(
|
||||||
|
Arc::new(ScreenCapture::unavailable("synthetic locked desktop".into())), locked));
|
||||||
|
let (desktop, mut viewer) = connect(input.clone(), true, Some(portal.clone()), &["CONTROL_DATA_CHANNEL"]).await;
|
||||||
|
// Official Android 4.43.0's unlock presenter requires CaptureChange
|
||||||
|
// (desktop/window) before it will use the secure state to send action15.
|
||||||
|
let selected = receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 8).await;
|
||||||
|
let selected = decode_wire(body(&selected, 8).unwrap()).unwrap();
|
||||||
|
let capture_type = selected.iter().find_map(|f| match (f.number, &f.value) {
|
||||||
|
(1, WireValue::Varint(value)) => Some(*value), _ => None,
|
||||||
|
}).unwrap_or(0);
|
||||||
|
assert_eq!(capture_type, 0);
|
||||||
|
let state = receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 15).await;
|
||||||
|
let secure = body(body(&state, 15).unwrap(), 1).unwrap();
|
||||||
|
assert!(decode_wire(secure).unwrap().iter().any(|f| f.number == 1 && f.value == WireValue::Varint(1)));
|
||||||
|
assert!(portal.snapshot().screen.init_error.is_some());
|
||||||
|
// A capture request waiting for unlock must not occupy CONTROL dispatch.
|
||||||
|
let mut capture = bytes(1, &[8, 31]);
|
||||||
|
capture.extend(bytes(2, &[16, 2, 144, 1, 60]));
|
||||||
|
send(&viewer, "CONTROL_DATA_CHANNEL", crate::initialization::envelope(10, 0, 21, &capture)).await;
|
||||||
|
send(&viewer, "CONTROL_DATA_CHANNEL", crate::initialization::envelope(91, 0, 3, &[8, 0])).await;
|
||||||
|
let echo = receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 3).await;
|
||||||
|
let echo: serde_json::Value = serde_json::from_slice(body(body(&echo, 3).unwrap(), 2).unwrap()).unwrap();
|
||||||
|
assert_eq!(echo["seq"], 91);
|
||||||
|
send(&viewer, "CONTROL_DATA_CHANNEL",
|
||||||
|
crate::initialization::envelope(92, 0, 16, &[8, 1])).await;
|
||||||
|
let state = receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 15).await;
|
||||||
|
assert!(body(body(&state, 15).unwrap(), 1).is_some());
|
||||||
|
// Missing password exercises the real AUTO_UNLOCK_SYSTEM failure route
|
||||||
|
// without invoking PAM or unlocking the local user's desktop.
|
||||||
|
send(&viewer, "CONTROL_DATA_CHANNEL", crate::initialization::envelope(1, 0, 3, &[8, 15])).await;
|
||||||
|
receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 12).await;
|
||||||
|
|
||||||
|
let (terminal, mut terminal_viewer) = connect(input.clone(), false, None,
|
||||||
|
&["CONTROL_DATA_CHANNEL", "BINARY_DATA_CHANNEL"]).await;
|
||||||
|
send(&terminal_viewer, "BINARY_DATA_CHANNEL",
|
||||||
|
crate::terminal::encode(crate::terminal::LIST_REQ, 71, &[])).await;
|
||||||
|
let frame = tokio::time::timeout(Duration::from_secs(5), async {
|
||||||
|
loop {
|
||||||
|
let (channel, message) = terminal_viewer.messages.recv().await.unwrap();
|
||||||
|
if channel == "BINARY_DATA_CHANNEL" { break crate::terminal::decode(&message).unwrap(); }
|
||||||
|
}
|
||||||
|
}).await.unwrap();
|
||||||
|
assert_eq!((frame.kind, frame.session), (crate::terminal::LIST_RESP, 71));
|
||||||
|
|
||||||
|
let (files, mut file_viewer) = connect(input.clone(), false, None,
|
||||||
|
&["CONTROL_DATA_CHANNEL", "FILE_DATA_CHANNEL"]).await;
|
||||||
|
let directory = std::env::temp_dir().join(format!("uu-control-empty-{}-{}", std::process::id(), rand::random::<u64>()));
|
||||||
|
std::fs::create_dir(&directory).unwrap();
|
||||||
|
let mut read = bytes(1, &[8, 7]);
|
||||||
|
read.extend(bytes(2, directory.to_str().unwrap().as_bytes()));
|
||||||
|
let mut request = bytes(1, &[8, 99]);
|
||||||
|
request.extend(bytes(8, &bytes(10, &read)));
|
||||||
|
let query = crate::initialization::envelope(5, 0, 21, &request);
|
||||||
|
send(&file_viewer, "CONTROL_DATA_CHANNEL", query.clone()).await;
|
||||||
|
let response = receive(&mut file_viewer.messages, "FILE_DATA_CHANNEL", 22).await;
|
||||||
|
let listing = body(body(body(&response, 22).unwrap(), 4).unwrap(), 7).unwrap();
|
||||||
|
assert!(decode_wire(listing).unwrap().iter().any(|f| f.number == 4 && f.value == WireValue::Varint(1)));
|
||||||
|
|
||||||
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||||
|
let port = listener.local_addr().unwrap().port();
|
||||||
|
let echo = tokio::spawn(async move {
|
||||||
|
let (socket, _) = listener.accept().await.unwrap();
|
||||||
|
let (mut read, mut write) = socket.into_split();
|
||||||
|
tokio::io::copy(&mut read, &mut write).await.unwrap();
|
||||||
|
});
|
||||||
|
let (ports, mut port_viewer) = connect(input, false, None,
|
||||||
|
&["CONTROL_DATA_CHANNEL", "FILE_DATA_CHANNEL"]).await;
|
||||||
|
let target = serde_json::json!({"version":1,"target_host":"127.0.0.1","target_port":port}).to_string();
|
||||||
|
send(&port_viewer, "FILE_DATA_CHANNEL", mapping(0, target.as_bytes())).await;
|
||||||
|
let ack = receive(&mut port_viewer.messages, "FILE_DATA_CHANNEL", 27).await;
|
||||||
|
let ack: serde_json::Value = serde_json::from_slice(body(body(&ack, 27).unwrap(), 5).unwrap()).unwrap();
|
||||||
|
assert_eq!(ack["ok"], true);
|
||||||
|
|
||||||
|
let mut sessions = HashMap::from([
|
||||||
|
("desktop".into(), desktop), ("terminal".into(), terminal), ("files".into(), files),
|
||||||
|
("ports".into(), ports),
|
||||||
|
]);
|
||||||
|
assert!(crate::superseded_peer_ids(&sessions, "another-tool", false).is_empty());
|
||||||
|
assert_eq!(crate::superseded_peer_ids(&sessions, "next-desktop", true), ["desktop"]);
|
||||||
|
for id in crate::superseded_peer_ids(&sessions, "next-desktop", true) {
|
||||||
|
sessions.remove(&id).unwrap().pc.close().await.unwrap();
|
||||||
|
}
|
||||||
|
assert_eq!(sessions["terminal"].pc.connection_state(), RTCPeerConnectionState::Connected);
|
||||||
|
assert_eq!(sessions["files"].pc.connection_state(), RTCPeerConnectionState::Connected);
|
||||||
|
// A desktop close must not cancel the independent file-transfer responder.
|
||||||
|
send(&file_viewer, "CONTROL_DATA_CHANNEL", query).await;
|
||||||
|
receive(&mut file_viewer.messages, "FILE_DATA_CHANNEL", 22).await;
|
||||||
|
send(&port_viewer, "FILE_DATA_CHANNEL", mapping(2, b"local echo")).await;
|
||||||
|
let echoed = receive(&mut port_viewer.messages, "FILE_DATA_CHANNEL", 27).await;
|
||||||
|
assert_eq!(body(body(&echoed, 27).unwrap(), 5), Some(b"local echo".as_slice()));
|
||||||
|
send(&terminal_viewer, "BINARY_DATA_CHANNEL",
|
||||||
|
crate::terminal::encode(crate::terminal::LIST_REQ, 72, &[])).await;
|
||||||
|
let frame = tokio::time::timeout(Duration::from_secs(5), async {
|
||||||
|
loop {
|
||||||
|
let (channel, message) = terminal_viewer.messages.recv().await.unwrap();
|
||||||
|
if channel == "BINARY_DATA_CHANNEL" { break crate::terminal::decode(&message).unwrap(); }
|
||||||
|
}
|
||||||
|
}).await.unwrap();
|
||||||
|
assert_eq!((frame.kind, frame.session), (crate::terminal::LIST_RESP, 72));
|
||||||
|
std::fs::remove_dir(directory).unwrap();
|
||||||
|
for (_, peer) in sessions { peer.pc.close().await.unwrap(); }
|
||||||
|
viewer.pc.close().await.unwrap();
|
||||||
|
terminal_viewer.pc.close().await.unwrap();
|
||||||
|
file_viewer.pc.close().await.unwrap();
|
||||||
|
port_viewer.pc.close().await.unwrap();
|
||||||
|
for router in [&viewer.router, &terminal_viewer.router, &file_viewer.router, &port_viewer.router] {
|
||||||
|
router.lock().await.stop().await.unwrap();
|
||||||
|
}
|
||||||
|
echo.abort();
|
||||||
|
drop(lock);
|
||||||
|
}).await.expect("locked bootstrap/tool regression exceeded deadline");
|
||||||
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
//! Small GNOME-only display wake and idle-inhibition bridge.
|
||||||
|
|
||||||
|
#[cfg(all(target_os = "linux", feature = "portal"))]
|
||||||
|
mod native {
|
||||||
|
use std::{ffi::c_void, ptr::NonNull};
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
fn uu_desktop_power_wake() -> i32;
|
||||||
|
fn uu_desktop_idle_inhibitor_acquire(error: *mut i32) -> *mut c_void;
|
||||||
|
fn uu_desktop_idle_inhibitor_release(inhibitor: *mut c_void);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Handle(NonNull<c_void>);
|
||||||
|
|
||||||
|
// GDBusConnection is thread-safe; the C handle is only used by its single Drop call.
|
||||||
|
unsafe impl Send for Handle {}
|
||||||
|
|
||||||
|
impl Handle {
|
||||||
|
pub fn acquire() -> Result<Self, String> {
|
||||||
|
let mut error = 0;
|
||||||
|
let pointer = unsafe { uu_desktop_idle_inhibitor_acquire(&mut error) };
|
||||||
|
NonNull::new(pointer).map(Self).ok_or_else(|| super::error("idle inhibition", error))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn wake() -> Result<(), String> {
|
||||||
|
super::check("display wake", unsafe { uu_desktop_power_wake() })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Handle {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
unsafe { uu_desktop_idle_inhibitor_release(self.0.as_ptr()) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set GNOME's display power-save mode to on. This does not unlock the session or change
|
||||||
|
/// lock-screen policy. Session-bus setup and each method call are bounded to three seconds.
|
||||||
|
pub fn wake() -> Result<(), String> {
|
||||||
|
if !gnome_portal_active() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
#[cfg(all(target_os = "linux", feature = "portal"))]
|
||||||
|
{
|
||||||
|
native::Handle::wake()
|
||||||
|
}
|
||||||
|
#[cfg(not(all(target_os = "linux", feature = "portal")))]
|
||||||
|
{
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Holds a GNOME SessionManager idle-only inhibitor until dropped.
|
||||||
|
///
|
||||||
|
/// Acquire and drop perform blocking D-Bus work. Session-bus setup and each method call are
|
||||||
|
/// bounded to three seconds. Callers on an async runtime should move both operations to a
|
||||||
|
/// blocking worker.
|
||||||
|
pub struct IdleInhibitor {
|
||||||
|
#[cfg(all(target_os = "linux", feature = "portal"))]
|
||||||
|
_handle: Option<native::Handle>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IdleInhibitor {
|
||||||
|
pub fn acquire() -> Result<Self, String> {
|
||||||
|
if !gnome_portal_active() {
|
||||||
|
return Ok(Self {
|
||||||
|
#[cfg(all(target_os = "linux", feature = "portal"))]
|
||||||
|
_handle: None,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
#[cfg(all(target_os = "linux", feature = "portal"))]
|
||||||
|
{
|
||||||
|
native::Handle::acquire().map(|handle| Self { _handle: Some(handle) })
|
||||||
|
}
|
||||||
|
#[cfg(not(all(target_os = "linux", feature = "portal")))]
|
||||||
|
{
|
||||||
|
Ok(Self {})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gnome_portal_active() -> bool {
|
||||||
|
let gnome = std::env::var("XDG_CURRENT_DESKTOP").unwrap_or_default().split(':')
|
||||||
|
.any(|desktop| desktop.eq_ignore_ascii_case("gnome"));
|
||||||
|
gnome && crate::desktop_backend::detect().is_ok_and(|backend| backend == crate::desktop_backend::DesktopBackend::Portal)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(all(target_os = "linux", feature = "portal"))]
|
||||||
|
fn check(operation: &str, code: i32) -> Result<(), String> {
|
||||||
|
if code == 0 { Ok(()) } else { Err(error(operation, code)) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(all(target_os = "linux", feature = "portal"))]
|
||||||
|
fn error(operation: &str, code: i32) -> String {
|
||||||
|
let reason = match code {
|
||||||
|
1 => "session D-Bus unavailable",
|
||||||
|
2 => "GNOME D-Bus operation failed",
|
||||||
|
_ => "GNOME D-Bus operation failed",
|
||||||
|
};
|
||||||
|
format!("{operation}: {reason} (code {code})")
|
||||||
|
}
|
||||||
@@ -0,0 +1,567 @@
|
|||||||
|
//! Portal availability is independent of the authenticated CONTROL transport.
|
||||||
|
use std::future::Future;
|
||||||
|
use std::sync::{Arc, atomic::{AtomicBool, Ordering}};
|
||||||
|
use std::time::Instant;
|
||||||
|
use tokio::sync::{watch, Mutex};
|
||||||
|
use crate::{capture::ScreenCapture, input::DesktopInputSession};
|
||||||
|
use crate::portal::PortalError;
|
||||||
|
use webrtc::{data_channel::{RTCDataChannel, data_channel_state::RTCDataChannelState},
|
||||||
|
peer_connection::{RTCPeerConnection, peer_connection_state::RTCPeerConnectionState}};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct State {
|
||||||
|
pub screen: Arc<ScreenCapture>,
|
||||||
|
pub waiting: bool,
|
||||||
|
}
|
||||||
|
impl State {
|
||||||
|
pub fn permission(&self, desktop: bool) -> crate::system_metrics::Permission {
|
||||||
|
use crate::system_metrics::Permission;
|
||||||
|
if !desktop { Permission::Allow }
|
||||||
|
else if self.waiting { Permission::Unknown }
|
||||||
|
else if self.screen.init_error.is_none() { Permission::Allow }
|
||||||
|
else { Permission::Reject }
|
||||||
|
}
|
||||||
|
/// Capture exists and is not waiting for a replacement of a closed Portal session.
|
||||||
|
pub fn ready(&self) -> bool { self.screen.init_error.is_none() && !self.waiting }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct PortalDesktop {
|
||||||
|
state: watch::Sender<State>,
|
||||||
|
opening: Arc<Mutex<()>>,
|
||||||
|
locked: watch::Receiver<Option<bool>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct CancelOnDrop(Arc<AtomicBool>);
|
||||||
|
impl Drop for CancelOnDrop {
|
||||||
|
fn drop(&mut self) { self.0.store(true, Ordering::Release); }
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Inhibitor {
|
||||||
|
Real(crate::desktop_power::IdleInhibitor),
|
||||||
|
#[cfg(test)]
|
||||||
|
Fake(DropSignal),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
struct DropSignal(Arc<std::sync::atomic::AtomicUsize>);
|
||||||
|
#[cfg(test)]
|
||||||
|
impl Drop for DropSignal {
|
||||||
|
fn drop(&mut self) { self.0.fetch_add(1, Ordering::Relaxed); }
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AttemptPrep {
|
||||||
|
wake: Result<(), String>,
|
||||||
|
inhibitor: IdleGuard,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct IdleGuard(Option<Inhibitor>);
|
||||||
|
impl Drop for IdleGuard {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
match self.0.take() {
|
||||||
|
Some(Inhibitor::Real(guard)) => { tokio::task::spawn_blocking(move || drop(guard)); }
|
||||||
|
#[cfg(test)]
|
||||||
|
Some(Inhibitor::Fake(signal)) => drop(signal),
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum AttemptOutcome {
|
||||||
|
Ready(Arc<ScreenCapture>),
|
||||||
|
Retry(String),
|
||||||
|
Terminal(String),
|
||||||
|
Cancelled,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn classify(result: Result<Arc<ScreenCapture>, PortalError>) -> AttemptOutcome {
|
||||||
|
match result {
|
||||||
|
Ok(screen) => AttemptOutcome::Ready(screen),
|
||||||
|
Err(error) if error.code == 13 => AttemptOutcome::Cancelled,
|
||||||
|
// 4/5/14 are fatal session/geometry states; 6/8 are unsupported
|
||||||
|
// consent/format shapes which must not loop prompts forever.
|
||||||
|
Err(error) if matches!(error.code, 4 | 5 | 6 | 8 | 14) => AttemptOutcome::Terminal(error.to_string()),
|
||||||
|
Err(error) => AttemptOutcome::Retry(error.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn should_prepare_power(healthy: bool, force_wake: bool) -> bool {
|
||||||
|
!healthy || force_wake
|
||||||
|
}
|
||||||
|
|
||||||
|
fn prepare_attempt<W, I>(
|
||||||
|
cancelled: &AtomicBool,
|
||||||
|
owner: &DesktopInputSession,
|
||||||
|
locked: &watch::Receiver<Option<bool>>,
|
||||||
|
wake: W,
|
||||||
|
inhibit: I,
|
||||||
|
) -> Option<AttemptPrep>
|
||||||
|
where
|
||||||
|
W: FnOnce() -> Result<(), String>,
|
||||||
|
I: FnOnce() -> IdleGuard,
|
||||||
|
{
|
||||||
|
let current = || !cancelled.load(Ordering::Acquire)
|
||||||
|
&& owner.is_current()
|
||||||
|
&& *locked.borrow() == Some(false);
|
||||||
|
if !current() { return None; }
|
||||||
|
let wake = wake();
|
||||||
|
if !current() { return None; }
|
||||||
|
let inhibitor = inhibit();
|
||||||
|
if !current() { return None; }
|
||||||
|
Some(AttemptPrep { wake, inhibitor })
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PortalDesktop {
|
||||||
|
pub fn new(screen: Arc<ScreenCapture>) -> Self {
|
||||||
|
Self { state: watch::channel(State { waiting: screen.init_error.is_some(), screen }).0,
|
||||||
|
opening: Arc::new(Mutex::new(())), locked: crate::session_lock::watch_locked() }
|
||||||
|
}
|
||||||
|
pub fn begin(&self) {
|
||||||
|
// GNOME closes the Portal session on lock; a cached screen with a dead session
|
||||||
|
// is not ready (no Allow, no stale video path) until maintain() rebuilds it.
|
||||||
|
self.state.send_modify(|state| state.waiting = state.screen.init_error.is_some()
|
||||||
|
|| state.screen.portal.as_ref().is_some_and(|portal| !portal.is_alive()));
|
||||||
|
}
|
||||||
|
pub fn snapshot(&self) -> State { self.state.borrow().clone() }
|
||||||
|
pub fn subscribe(&self) -> watch::Receiver<State> { self.state.subscribe() }
|
||||||
|
pub fn lock_state(&self) -> watch::Receiver<Option<bool>> { self.locked.clone() }
|
||||||
|
#[cfg(test)]
|
||||||
|
pub fn with_lock_state(screen: Arc<ScreenCapture>, locked: watch::Receiver<Option<bool>>) -> Self {
|
||||||
|
Self { state: watch::channel(State { waiting: screen.init_error.is_some(), screen }).0,
|
||||||
|
opening: Arc::new(Mutex::new(())), locked }
|
||||||
|
}
|
||||||
|
#[cfg(test)]
|
||||||
|
pub fn publish_test_screen(&self, screen: Arc<ScreenCapture>) {
|
||||||
|
self.state.send_replace(State { screen, waiting: false });
|
||||||
|
}
|
||||||
|
|
||||||
|
fn publish(&self, owner: &DesktopInputSession, screen: Arc<ScreenCapture>) {
|
||||||
|
owner.with_current(|| {
|
||||||
|
if screen.init_error.is_some() { owner.release_input(); }
|
||||||
|
owner.bind_portal(screen.portal.clone());
|
||||||
|
self.state.send_replace(State { screen, waiting: false });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pause(&self, owner: &DesktopInputSession) {
|
||||||
|
let screen = self.snapshot().screen;
|
||||||
|
owner.with_current(|| {
|
||||||
|
owner.release_input();
|
||||||
|
if let Some(portal) = &screen.portal { portal.pause(); }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run(self: Arc<Self>, pc: Arc<RTCPeerConnection>, channel: Arc<RTCDataChannel>,
|
||||||
|
owner: Arc<DesktopInputSession>) {
|
||||||
|
self.run_until(owner, closed(&pc, &channel)).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run_until<F>(self: Arc<Self>, owner: Arc<DesktopInputSession>, wait_closed: F)
|
||||||
|
where F: Future<Output = ()> {
|
||||||
|
tokio::select! {
|
||||||
|
result = self.maintain(owner.clone()) => {
|
||||||
|
if let Err(error) = result {
|
||||||
|
log::warn!("Portal desktop unavailable: {error}");
|
||||||
|
self.publish(&owner, Arc::new(ScreenCapture::unavailable(error)));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ = wait_closed => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn maintain(&self, owner: Arc<DesktopInputSession>) -> Result<(), String> {
|
||||||
|
self.maintain_with(owner,
|
||||||
|
crate::desktop_power::wake,
|
||||||
|
|| match crate::desktop_power::IdleInhibitor::acquire() {
|
||||||
|
Ok(guard) => IdleGuard(Some(Inhibitor::Real(guard))),
|
||||||
|
Err(error) => { log::warn!("Desktop idle inhibition unavailable: {error}"); IdleGuard::default() }
|
||||||
|
},
|
||||||
|
|screen, cancel| {
|
||||||
|
if let Some(portal) = &screen.portal {
|
||||||
|
portal.recover(&cancel)?;
|
||||||
|
Ok(screen)
|
||||||
|
} else {
|
||||||
|
ScreenCapture::open_portal(&cancel).map(Arc::new)
|
||||||
|
}
|
||||||
|
}).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn maintain_with<W, I, O>(&self, owner: Arc<DesktopInputSession>, wake: W, inhibit: I, open: O) -> Result<(), String>
|
||||||
|
where
|
||||||
|
W: Fn() -> Result<(), String> + Send + Sync + 'static,
|
||||||
|
I: Fn() -> IdleGuard + Send + Sync + 'static,
|
||||||
|
O: Fn(Arc<ScreenCapture>, Arc<AtomicBool>) -> Result<Arc<ScreenCapture>, PortalError> + Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
let wake = Arc::new(wake);
|
||||||
|
let inhibit = Arc::new(inhibit);
|
||||||
|
let open = Arc::new(open);
|
||||||
|
let mut locked = self.locked.clone();
|
||||||
|
let mut previous = None;
|
||||||
|
let mut retry_at = Instant::now();
|
||||||
|
loop {
|
||||||
|
if !owner.is_current() { return Ok(()); }
|
||||||
|
let state = *locked.borrow_and_update();
|
||||||
|
if state != Some(false) {
|
||||||
|
if previous == Some(false) { self.pause(&owner); }
|
||||||
|
} else {
|
||||||
|
// A fresh unlocked transition gets an immediate wake/recovery attempt.
|
||||||
|
let force_wake = previous != Some(false);
|
||||||
|
if force_wake { retry_at = Instant::now(); }
|
||||||
|
let mut attempt_locked = locked.clone();
|
||||||
|
let outcome = tokio::select! {
|
||||||
|
biased;
|
||||||
|
_ = wait_for_lock(&mut locked) => AttemptOutcome::Cancelled,
|
||||||
|
outcome = self.portal_attempt(&owner, &mut attempt_locked, retry_at, force_wake, wake.clone(), inhibit.clone(), open.clone()) => outcome,
|
||||||
|
};
|
||||||
|
match outcome {
|
||||||
|
AttemptOutcome::Ready(screen) => self.publish(&owner, screen),
|
||||||
|
AttemptOutcome::Terminal(error) => return Err(error),
|
||||||
|
AttemptOutcome::Retry(error) => {
|
||||||
|
log::warn!("Portal desktop recovery deferred: {error}");
|
||||||
|
retry_at = Instant::now() + std::time::Duration::from_secs(5);
|
||||||
|
}
|
||||||
|
AttemptOutcome::Cancelled => {}
|
||||||
|
}
|
||||||
|
if *locked.borrow_and_update() != Some(false) { self.pause(&owner); }
|
||||||
|
}
|
||||||
|
previous = *locked.borrow_and_update();
|
||||||
|
tokio::select! {
|
||||||
|
_ = locked.changed() => {},
|
||||||
|
_ = tokio::time::sleep(std::time::Duration::from_millis(200)) => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn portal_attempt<W, I, O>(&self, owner: &Arc<DesktopInputSession>,
|
||||||
|
locked: &mut watch::Receiver<Option<bool>>, retry_at: Instant, force_wake: bool,
|
||||||
|
wake: Arc<W>, inhibit: Arc<I>, open: Arc<O>) -> AttemptOutcome
|
||||||
|
where
|
||||||
|
W: Fn() -> Result<(), String> + Send + Sync + 'static,
|
||||||
|
I: Fn() -> IdleGuard + Send + Sync + 'static,
|
||||||
|
O: Fn(Arc<ScreenCapture>, Arc<AtomicBool>) -> Result<Arc<ScreenCapture>, PortalError> + Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
if !owner.is_current() || *locked.borrow() != Some(false) || Instant::now() < retry_at {
|
||||||
|
return AttemptOutcome::Cancelled;
|
||||||
|
}
|
||||||
|
let gate = self.opening.clone().lock_owned().await;
|
||||||
|
// The state can change while this attempt waits for an earlier worker
|
||||||
|
// to finish rotating the Portal permission token.
|
||||||
|
if !owner.is_current() || *locked.borrow() != Some(false) {
|
||||||
|
return AttemptOutcome::Cancelled;
|
||||||
|
}
|
||||||
|
let screen = self.snapshot().screen;
|
||||||
|
let healthy = screen.portal.as_ref().is_some_and(|portal| portal.is_alive());
|
||||||
|
if !should_prepare_power(healthy, force_wake) {
|
||||||
|
return AttemptOutcome::Cancelled;
|
||||||
|
}
|
||||||
|
let cancel = Arc::new(AtomicBool::new(false));
|
||||||
|
let _cancel = CancelOnDrop(cancel.clone());
|
||||||
|
let prep_cancel = cancel.clone();
|
||||||
|
let prep_owner = owner.clone();
|
||||||
|
let prep_locked = locked.clone();
|
||||||
|
let prep = match tokio::task::spawn_blocking(move || prepare_attempt(
|
||||||
|
&prep_cancel,
|
||||||
|
&prep_owner,
|
||||||
|
&prep_locked,
|
||||||
|
|| (*wake)(),
|
||||||
|
|| (*inhibit)(),
|
||||||
|
)).await {
|
||||||
|
Ok(Some(prep)) => prep,
|
||||||
|
Ok(None) => return AttemptOutcome::Cancelled,
|
||||||
|
Err(_) => return AttemptOutcome::Terminal("Desktop power worker failed".into()),
|
||||||
|
};
|
||||||
|
if let Err(error) = prep.wake { log::warn!("Portal screen wake failed: {error}"); }
|
||||||
|
// Held by the cancellable async helper, not the blocking worker, so a
|
||||||
|
// lock transition releases it while worker cleanup may still be running.
|
||||||
|
let _inhibitor = prep.inhibitor;
|
||||||
|
if !owner.is_current() || *locked.borrow() != Some(false) {
|
||||||
|
return AttemptOutcome::Cancelled;
|
||||||
|
}
|
||||||
|
// A new controller can arrive while the cached Portal handle is alive
|
||||||
|
// but the display has gone to sleep without a lock transition.
|
||||||
|
if healthy { return AttemptOutcome::Ready(screen); }
|
||||||
|
let worker_cancel = cancel.clone();
|
||||||
|
let result = tokio::task::spawn_blocking(move || {
|
||||||
|
// The worker owns the gate until its real cleanup completes; a
|
||||||
|
// cancelled helper only signals it and cannot start an overlap.
|
||||||
|
let _gate = gate;
|
||||||
|
if worker_cancel.load(Ordering::Acquire) {
|
||||||
|
return Err(PortalError { code: 13 });
|
||||||
|
}
|
||||||
|
open(screen, worker_cancel)
|
||||||
|
}).await;
|
||||||
|
if !owner.is_current() || *locked.borrow() != Some(false) {
|
||||||
|
return AttemptOutcome::Cancelled;
|
||||||
|
}
|
||||||
|
match result {
|
||||||
|
Ok(result) => classify(result),
|
||||||
|
Err(_) => AttemptOutcome::Terminal("Portal initialization worker failed".into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn wait_for_lock(locked: &mut watch::Receiver<Option<bool>>) {
|
||||||
|
while locked.changed().await.is_ok() {
|
||||||
|
if *locked.borrow_and_update() != Some(false) { return; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn closed(pc: &RTCPeerConnection, channel: &RTCDataChannel) {
|
||||||
|
while !matches!(pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed)
|
||||||
|
&& channel.ready_state() == RTCDataChannelState::Open {
|
||||||
|
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn wait_ready(state: &mut watch::Receiver<State>, channel: &RTCDataChannel) -> Option<Arc<ScreenCapture>> {
|
||||||
|
loop {
|
||||||
|
let current = state.borrow_and_update().clone();
|
||||||
|
if current.ready() { return Some(current.screen); }
|
||||||
|
if !current.waiting || channel.ready_state() != RTCDataChannelState::Open { return None; }
|
||||||
|
tokio::select! {
|
||||||
|
changed = state.changed() => if changed.is_err() { return None; },
|
||||||
|
_ = tokio::time::sleep(std::time::Duration::from_millis(100)) => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::sync::atomic::AtomicUsize;
|
||||||
|
fn host() -> PortalDesktop {
|
||||||
|
let (_, locked) = watch::channel(Some(true));
|
||||||
|
PortalDesktop::with_lock_state(Arc::new(ScreenCapture::unavailable("pending".into())), locked)
|
||||||
|
}
|
||||||
|
fn ready_screen() -> Arc<ScreenCapture> {
|
||||||
|
let mut screen = ScreenCapture::unavailable("synthetic".into());
|
||||||
|
screen.backend = crate::desktop_backend::DesktopBackend::Portal;
|
||||||
|
screen.init_error = None;
|
||||||
|
screen.width = 1920;
|
||||||
|
screen.height = 1080;
|
||||||
|
Arc::new(screen)
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn cached_screen_waiting_for_a_closed_portal_is_not_ready_or_allowed() {
|
||||||
|
use crate::system_metrics::Permission;
|
||||||
|
let live = State { screen: ready_screen(), waiting: false };
|
||||||
|
assert!(live.ready() && live.permission(true) == Permission::Allow);
|
||||||
|
let stale = State { screen: ready_screen(), waiting: true };
|
||||||
|
assert!(!stale.ready());
|
||||||
|
assert!(stale.permission(true) == Permission::Unknown);
|
||||||
|
assert!(stale.permission(false) == Permission::Allow);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn recovered_screen_is_retained_across_transient_disconnect() {
|
||||||
|
let host = host();
|
||||||
|
let mut updates = host.subscribe();
|
||||||
|
let input = Arc::new(crate::input::InputInjector::without_desktop());
|
||||||
|
let owner = input.desktop_session();
|
||||||
|
owner.connected();owner.disconnected();
|
||||||
|
let screen = ready_screen();
|
||||||
|
host.publish(&owner, screen.clone());
|
||||||
|
assert!(updates.has_changed().unwrap());
|
||||||
|
assert!(!updates.borrow_and_update().waiting);
|
||||||
|
assert!(Arc::ptr_eq(&host.snapshot().screen, &screen));
|
||||||
|
assert!(!host.snapshot().waiting);
|
||||||
|
assert_eq!(host.snapshot().permission(true), crate::system_metrics::Permission::Allow);
|
||||||
|
owner.with_input(|| panic!("publication must not reactivate input"));
|
||||||
|
owner.connected();
|
||||||
|
assert!(Arc::ptr_eq(&host.snapshot().screen, &screen));
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn terminal_failure_publishes_rejection_while_disconnected() {
|
||||||
|
let host = host();
|
||||||
|
let mut updates = host.subscribe();
|
||||||
|
let input = Arc::new(crate::input::InputInjector::without_desktop());
|
||||||
|
let owner = input.desktop_session();
|
||||||
|
owner.connected();owner.disconnected();
|
||||||
|
host.publish(&owner, Arc::new(ScreenCapture::unavailable("consent rejected".into())));
|
||||||
|
assert!(updates.has_changed().unwrap());
|
||||||
|
assert!(!updates.borrow_and_update().waiting);
|
||||||
|
assert!(!host.snapshot().waiting);
|
||||||
|
assert_eq!(host.snapshot().permission(true), crate::system_metrics::Permission::Reject);
|
||||||
|
owner.connected();
|
||||||
|
assert!(!host.snapshot().waiting);
|
||||||
|
assert_eq!(host.snapshot().screen.init_error.as_deref(), Some("consent rejected"));
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn replaced_peer_cannot_publish_success_or_failure() {
|
||||||
|
let host = host();
|
||||||
|
let input = Arc::new(crate::input::InputInjector::without_desktop());
|
||||||
|
let old = input.desktop_session();
|
||||||
|
let current = input.desktop_session();
|
||||||
|
let screen = ready_screen();
|
||||||
|
host.publish(¤t, screen.clone());
|
||||||
|
host.publish(&old, Arc::new(ScreenCapture::unavailable("late failure".into())));
|
||||||
|
host.publish(&old, ready_screen());
|
||||||
|
assert!(Arc::ptr_eq(&host.snapshot().screen, &screen));
|
||||||
|
assert!(!host.snapshot().waiting);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn unavailable_desktop_does_not_reject_independent_tools() {
|
||||||
|
let host = PortalDesktop::new(Arc::new(ScreenCapture::unavailable("not authorized".into())));
|
||||||
|
assert_eq!(host.snapshot().permission(true), crate::system_metrics::Permission::Unknown);
|
||||||
|
assert_eq!(host.snapshot().permission(false), crate::system_metrics::Permission::Allow);
|
||||||
|
host.state.send_modify(|state| state.waiting = false);
|
||||||
|
assert_eq!(host.snapshot().permission(true), crate::system_metrics::Permission::Reject);
|
||||||
|
assert_eq!(host.snapshot().permission(false), crate::system_metrics::Permission::Allow);
|
||||||
|
host.begin();
|
||||||
|
assert!(host.snapshot().waiting);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn unsupported_portal_shapes_are_terminal_while_transient_codes_retry() {
|
||||||
|
for code in [4, 5, 6, 8, 14] {
|
||||||
|
assert!(matches!(classify(Err(PortalError { code })), AttemptOutcome::Terminal(_)), "code {code}");
|
||||||
|
}
|
||||||
|
for code in [1, 3, 7, 9, 10] {
|
||||||
|
assert!(matches!(classify(Err(PortalError { code })), AttemptOutcome::Retry(_)), "code {code}");
|
||||||
|
}
|
||||||
|
assert!(matches!(classify(Err(PortalError { code: 13 })), AttemptOutcome::Cancelled));
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn fresh_unlock_wakes_even_when_cached_portal_is_healthy() {
|
||||||
|
assert!(should_prepare_power(true, true));
|
||||||
|
assert!(should_prepare_power(false, false));
|
||||||
|
assert!(!should_prepare_power(true, false));
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn queued_power_prep_checks_cancel_lock_and_current_owner_before_wake() {
|
||||||
|
let input = Arc::new(crate::input::InputInjector::without_desktop());
|
||||||
|
let owner = input.desktop_session();
|
||||||
|
let (lock, locked) = watch::channel(Some(false));
|
||||||
|
let wakes = AtomicUsize::new(0);
|
||||||
|
let inhibitors = AtomicUsize::new(0);
|
||||||
|
let cancelled = AtomicBool::new(true);
|
||||||
|
assert!(prepare_attempt(&cancelled, &owner, &locked,
|
||||||
|
|| { wakes.fetch_add(1, Ordering::Relaxed); Ok(()) },
|
||||||
|
|| { inhibitors.fetch_add(1, Ordering::Relaxed); IdleGuard::default() }).is_none());
|
||||||
|
assert_eq!(wakes.load(Ordering::Relaxed), 0);
|
||||||
|
cancelled.store(false, Ordering::Release);
|
||||||
|
lock.send(Some(true)).unwrap();
|
||||||
|
assert!(prepare_attempt(&cancelled, &owner, &locked,
|
||||||
|
|| { wakes.fetch_add(1, Ordering::Relaxed); Ok(()) },
|
||||||
|
|| { inhibitors.fetch_add(1, Ordering::Relaxed); IdleGuard::default() }).is_none());
|
||||||
|
assert_eq!(wakes.load(Ordering::Relaxed), 0);
|
||||||
|
lock.send(Some(false)).unwrap();
|
||||||
|
let locked_during_wake = lock.clone();
|
||||||
|
assert!(prepare_attempt(&cancelled, &owner, &locked,
|
||||||
|
|| {
|
||||||
|
wakes.fetch_add(1, Ordering::Relaxed);
|
||||||
|
locked_during_wake.send(Some(true)).unwrap();
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
|| { inhibitors.fetch_add(1, Ordering::Relaxed); IdleGuard::default() }).is_none());
|
||||||
|
assert_eq!(wakes.load(Ordering::Relaxed), 1);
|
||||||
|
assert_eq!(inhibitors.load(Ordering::Relaxed), 0);
|
||||||
|
lock.send(Some(false)).unwrap();
|
||||||
|
let replacement = input.desktop_session();
|
||||||
|
assert!(prepare_attempt(&cancelled, &owner, &locked,
|
||||||
|
|| { wakes.fetch_add(1, Ordering::Relaxed); Ok(()) },
|
||||||
|
|| { inhibitors.fetch_add(1, Ordering::Relaxed); IdleGuard::default() }).is_none());
|
||||||
|
assert_eq!(wakes.load(Ordering::Relaxed), 1);
|
||||||
|
assert!(replacement.is_current());
|
||||||
|
assert_eq!(inhibitors.load(Ordering::Relaxed), 0);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn cancelling_a_peer_signals_its_blocking_permission_worker() {
|
||||||
|
let flag = Arc::new(AtomicBool::new(false));
|
||||||
|
let guard = CancelOnDrop(flag.clone());
|
||||||
|
assert!(!flag.load(Ordering::Acquire));
|
||||||
|
drop(guard);
|
||||||
|
assert!(flag.load(Ordering::Acquire));
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn lock_transition_cancels_blocked_attempt_and_releases_inhibitor() {
|
||||||
|
let (lock, locked) = watch::channel(Some(false));
|
||||||
|
let host = Arc::new(PortalDesktop::with_lock_state(
|
||||||
|
Arc::new(ScreenCapture::unavailable("pending".into())), locked));
|
||||||
|
let input = Arc::new(crate::input::InputInjector::without_desktop());
|
||||||
|
let owner = input.desktop_session();
|
||||||
|
let releases = Arc::new(AtomicUsize::new(0));
|
||||||
|
let wakes = Arc::new(AtomicUsize::new(0));
|
||||||
|
let (started, mut started_rx) = tokio::sync::mpsc::unbounded_channel();
|
||||||
|
let task = tokio::spawn({
|
||||||
|
let host = host.clone();
|
||||||
|
let owner = owner.clone();
|
||||||
|
let releases = releases.clone();
|
||||||
|
let wakes = wakes.clone();
|
||||||
|
async move {
|
||||||
|
host.maintain_with(owner,
|
||||||
|
move || {
|
||||||
|
wakes.fetch_add(1, Ordering::Relaxed);
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
move || IdleGuard(Some(Inhibitor::Fake(DropSignal(releases.clone())))),
|
||||||
|
move |_screen, cancel| {
|
||||||
|
let _ = started.send(());
|
||||||
|
while !cancel.load(Ordering::Acquire) {
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(1));
|
||||||
|
}
|
||||||
|
Err(PortalError { code: 13 })
|
||||||
|
}).await
|
||||||
|
}
|
||||||
|
});
|
||||||
|
started_rx.recv().await.unwrap();
|
||||||
|
lock.send(Some(true)).unwrap();
|
||||||
|
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(2);
|
||||||
|
while releases.load(Ordering::Relaxed) == 0 {
|
||||||
|
assert!(tokio::time::Instant::now() < deadline, "cancelled attempt did not release inhibitor");
|
||||||
|
tokio::task::yield_now().await;
|
||||||
|
}
|
||||||
|
assert_eq!(wakes.load(Ordering::Relaxed), 1);
|
||||||
|
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(2);
|
||||||
|
while host.opening.try_lock().is_err() {
|
||||||
|
assert!(tokio::time::Instant::now() < deadline, "cancelled worker did not finish");
|
||||||
|
tokio::task::yield_now().await;
|
||||||
|
}
|
||||||
|
task.abort();
|
||||||
|
let _ = task.await;
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn inhibitor_release_does_not_wait_for_blocked_worker_cleanup() {
|
||||||
|
let (lock, locked) = watch::channel(Some(false));
|
||||||
|
let host = Arc::new(PortalDesktop::with_lock_state(
|
||||||
|
Arc::new(ScreenCapture::unavailable("pending".into())), locked));
|
||||||
|
let input = Arc::new(crate::input::InputInjector::without_desktop());
|
||||||
|
let owner = input.desktop_session();
|
||||||
|
let releases = Arc::new(AtomicUsize::new(0));
|
||||||
|
let worker_release = Arc::new(AtomicBool::new(false));
|
||||||
|
let (started, mut started_rx) = tokio::sync::mpsc::unbounded_channel();
|
||||||
|
let task = tokio::spawn({
|
||||||
|
let host = host.clone();
|
||||||
|
let owner = owner.clone();
|
||||||
|
let releases = releases.clone();
|
||||||
|
let worker_release = worker_release.clone();
|
||||||
|
async move {
|
||||||
|
host.maintain_with(owner,
|
||||||
|
|| Ok(()),
|
||||||
|
move || IdleGuard(Some(Inhibitor::Fake(DropSignal(releases.clone())))),
|
||||||
|
move |screen, _cancel| {
|
||||||
|
let _ = started.send(());
|
||||||
|
while !worker_release.load(Ordering::Acquire) {
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(1));
|
||||||
|
}
|
||||||
|
Ok(screen)
|
||||||
|
}).await
|
||||||
|
}
|
||||||
|
});
|
||||||
|
started_rx.recv().await.unwrap();
|
||||||
|
lock.send(Some(true)).unwrap();
|
||||||
|
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(2);
|
||||||
|
while releases.load(Ordering::Relaxed) == 0 {
|
||||||
|
assert!(tokio::time::Instant::now() < deadline, "cancelled attempt did not release inhibitor");
|
||||||
|
tokio::task::yield_now().await;
|
||||||
|
}
|
||||||
|
assert!(host.opening.try_lock().is_err(), "worker cleanup must still own the gate");
|
||||||
|
worker_release.store(true, Ordering::Release);
|
||||||
|
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(2);
|
||||||
|
loop {
|
||||||
|
if host.opening.try_lock().is_ok() { break; }
|
||||||
|
assert!(tokio::time::Instant::now() < deadline, "worker cleanup did not release the gate");
|
||||||
|
tokio::task::yield_now().await;
|
||||||
|
}
|
||||||
|
task.abort();
|
||||||
|
let _ = task.await;
|
||||||
|
}
|
||||||
|
}
|
||||||
+6
-2
@@ -392,8 +392,12 @@ impl FileSession {
|
|||||||
let mut listing = Enc::default();
|
let mut listing = Enc::default();
|
||||||
for e in &entries { listing = listing.bytes(1, &e.0); }
|
for e in &entries { listing = listing.bytes(1, &e.0); }
|
||||||
let compressed = zlib(&listing.0);
|
let compressed = zlib(&listing.0);
|
||||||
log::info!("File transfer read_dir: request_id={} root={} entries={} dir_data_bytes={}", rid, path.is_empty(), entries.len(), compressed.len());
|
// Both copies in one message: a 5600-entry folder is ~73 KB compressed but several hundred KB
|
||||||
for e in entries { body = body.msg(3, e); }
|
// inline, beyond the 256 KB SCTP message limit, and the reply was dropped. Keep the inline copy
|
||||||
|
// only when the message still fits; current viewers read dir_data.
|
||||||
|
let inline = listing.0.len() + compressed.len() < 200_000;
|
||||||
|
log::info!("File transfer read_dir: request_id={} root={} entries={} dir_data_bytes={} inline={}", rid, path.is_empty(), entries.len(), compressed.len(), inline);
|
||||||
|
if inline { for e in entries { body = body.msg(3, e); } }
|
||||||
body = body.uint(4, FT_SUCCEEDED).bytes(5, &compressed);
|
body = body.uint(4, FT_SUCCEEDED).bytes(5, &compressed);
|
||||||
}
|
}
|
||||||
Err(code) => { log::info!("File transfer read_dir failed: request_id={} error={}", rid, code); body = body.uint(4, code); }
|
Err(code) => { log::info!("File transfer read_dir failed: request_id={} error={}", rid, code); body = body.uint(4, code); }
|
||||||
|
|||||||
+72
@@ -0,0 +1,72 @@
|
|||||||
|
//! RFC 7798 §4.4.1 / §4.4.3, single-layer non-interleaved HEVC (no DONL).
|
||||||
|
//! Parameter sets travel in-band before each IDR; no aggregation is required.
|
||||||
|
use bytes::Bytes;
|
||||||
|
|
||||||
|
pub fn payload(mtu: usize, annex_b: &[u8]) -> Result<Vec<Bytes>, String> {
|
||||||
|
if mtu < 4 { return Err("HEVC payload MTU too small".into()); }
|
||||||
|
let mut starts = Vec::new();
|
||||||
|
let mut i = 0;
|
||||||
|
while i + 3 <= annex_b.len() {
|
||||||
|
let prefix = if annex_b[i..].starts_with(&[0, 0, 0, 1]) { 4 }
|
||||||
|
else if annex_b[i..].starts_with(&[0, 0, 1]) { 3 } else { i += 1; continue; };
|
||||||
|
starts.push((i, i + prefix)); i += prefix;
|
||||||
|
}
|
||||||
|
if starts.is_empty() { return Err("HEVC access unit has no Annex-B start code".into()); }
|
||||||
|
let mut out = Vec::new();
|
||||||
|
for (index, &(_, start)) in starts.iter().enumerate() {
|
||||||
|
let mut end = starts.get(index + 1).map_or(annex_b.len(), |s| s.0);
|
||||||
|
while end > start && annex_b[end - 1] == 0 { end -= 1; } // trailing_zero_8bits
|
||||||
|
let nal = &annex_b[start..end];
|
||||||
|
if nal.len() < 2 || nal[0] & 0x80 != 0 || nal[1] & 7 == 0 {
|
||||||
|
return Err("Invalid HEVC NAL header".into());
|
||||||
|
}
|
||||||
|
let kind = (nal[0] >> 1) & 63;
|
||||||
|
if kind >= 48 { return Err("HEVC Annex-B contains RTP packet type".into()); }
|
||||||
|
if nal.len() <= mtu { out.push(Bytes::copy_from_slice(nal)); continue; }
|
||||||
|
let chunks = nal[2..].chunks(mtu - 3);
|
||||||
|
let last = chunks.len() - 1;
|
||||||
|
for (n, chunk) in chunks.enumerate() {
|
||||||
|
let mut fu = Vec::with_capacity(3 + chunk.len());
|
||||||
|
// Keep F, LayerId and TID, replacing only the NAL type with FU=49.
|
||||||
|
fu.extend_from_slice(&[(nal[0] & 0x81) | (49 << 1), nal[1],
|
||||||
|
kind | if n == 0 { 0x80 } else { 0 } | if n == last { 0x40 } else { 0 }]);
|
||||||
|
fu.extend_from_slice(chunk); out.push(fu.into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn parameter_sets_and_small_nals_remain_in_order_without_start_codes() {
|
||||||
|
assert_eq!(payload(1200, &[0,0,0,1,64,1,9,0,0,1,66,1,8,0,0,1,38,1,7]).unwrap(),
|
||||||
|
vec![Bytes::from_static(&[64,1,9]), Bytes::from_static(&[66,1,8]), Bytes::from_static(&[38,1,7])]);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn fu_round_trip_preserves_header_payload_and_mtu_boundaries() {
|
||||||
|
for mtu in [4, 5, 7, 1190] {
|
||||||
|
let header = [0x27, 0x2b]; // IDR type19, nonzero layer and temporal id
|
||||||
|
let data: Vec<u8> = (0..2501).map(|i| (i % 251 + 1) as u8).collect();
|
||||||
|
let mut unit = vec![0,0,0,1]; unit.extend(header); unit.extend(&data);
|
||||||
|
let packets = payload(mtu, &unit).unwrap();
|
||||||
|
assert!(packets.len() > 1);
|
||||||
|
let mut rebuilt = vec![(packets[0][0] & 0x81) | ((packets[0][2] & 63) << 1), packets[0][1]];
|
||||||
|
for (i, p) in packets.iter().enumerate() {
|
||||||
|
assert!(p.len() <= mtu); assert_eq!((p[0] >> 1) & 63, 49);
|
||||||
|
assert_eq!(p[2] & 0x80 != 0, i == 0);
|
||||||
|
assert_eq!(p[2] & 0x40 != 0, i + 1 == packets.len());
|
||||||
|
rebuilt.extend_from_slice(&p[3..]);
|
||||||
|
}
|
||||||
|
assert_eq!(&rebuilt[..2], &header); assert_eq!(&rebuilt[2..], &data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn rejects_invalid_headers_and_too_small_mtu() {
|
||||||
|
for bytes in [&[0,0,1,64][..], &[0,0,1,64,0][..], &[0,0,1,128,1][..], &[64,1][..]] {
|
||||||
|
assert!(payload(1200, bytes).is_err());
|
||||||
|
}
|
||||||
|
assert!(payload(3, &[0,0,1,64,1]).is_err());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -62,7 +62,7 @@ pub struct Screen<'a> {
|
|||||||
pub fps: i32,
|
pub fps: i32,
|
||||||
pub resolution: WinRect,
|
pub resolution: WinRect,
|
||||||
pub primary: bool,
|
pub primary: bool,
|
||||||
/// Measured pixel/logical scale; never zero or guessed from a display name.
|
/// Pixel/logical scale of the advertised coordinate space (1.0 for capture-setting v4).
|
||||||
pub dpr: f64,
|
pub dpr: f64,
|
||||||
pub display_name: &'a str,
|
pub display_name: &'a str,
|
||||||
pub video_track_index: i32,
|
pub video_track_index: i32,
|
||||||
@@ -128,7 +128,8 @@ pub fn encode_screen_sources(seq: u64, timestamp: u64, screens: &[Screen<'_>], c
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Message.which field 8 CaptureChange. Enum: desktop=0, window=1,
|
/// Message.which field 8 CaptureChange. Enum: desktop=0, window=1,
|
||||||
/// MuMu=2, hook=3, none=99. Caller confirms the active capture before emitting.
|
/// MuMu=2, hook=3, none=99. Declares the selected target, not frame readiness:
|
||||||
|
/// Android's unlock presenter requires desktop/window here even while locked.
|
||||||
pub fn encode_capture_change(seq: u64, timestamp: u64, capture_type: i32, type_value: i32, description: &str) -> Result<Vec<u8>, String> {
|
pub fn encode_capture_change(seq: u64, timestamp: u64, capture_type: i32, type_value: i32, description: &str) -> Result<Vec<u8>, String> {
|
||||||
if !matches!(capture_type, 0 | 1 | 2 | 3 | 99) {
|
if !matches!(capture_type, 0 | 1 | 2 | 3 | 99) {
|
||||||
return Err("Unknown CaptureChange type".into());
|
return Err("Unknown CaptureChange type".into());
|
||||||
|
|||||||
+77
-26
@@ -1,4 +1,4 @@
|
|||||||
//! In-process X11 capture and H.264 encoding through libavdevice/libavcodec.
|
//! In-process X11 capture and H.264/HEVC encoding through libavdevice/libavcodec.
|
||||||
//! It replaces the `ffmpeg` child process with the same codec options, so the viewer
|
//! It replaces the `ffmpeg` child process with the same codec options, so the viewer
|
||||||
//! still receives the same Annex-B access units (AUD, SPS/PPS, IDR, P).
|
//! still receives the same Annex-B access units (AUD, SPS/PPS, IDR, P).
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
@@ -11,6 +11,8 @@ use ff::ChannelLayout;
|
|||||||
use ff::util::picture;
|
use ff::util::picture;
|
||||||
use ff::{codec, encoder, frame, software::scaling, Dictionary, Packet, Rational};
|
use ff::{codec, encoder, frame, software::scaling, Dictionary, Packet, Rational};
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
use tokio::sync::{Semaphore, OwnedSemaphorePermit};
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
use crate::damage_capture::DamageCapture;
|
use crate::damage_capture::DamageCapture;
|
||||||
|
|
||||||
@@ -25,6 +27,7 @@ pub struct Config {
|
|||||||
pub max_bps: i64,
|
pub max_bps: i64,
|
||||||
pub buffer_bits: i64,
|
pub buffer_bits: i64,
|
||||||
pub encoder: crate::video::Encoder,
|
pub encoder: crate::video::Encoder,
|
||||||
|
pub codec: crate::capture_setting::VideoCodec,
|
||||||
/// Capture on XDamage changes (XShm) instead of x11grab; `UU_CAPTURE=grab` turns it off.
|
/// Capture on XDamage changes (XShm) instead of x11grab; `UU_CAPTURE=grab` turns it off.
|
||||||
pub damage: bool,
|
pub damage: bool,
|
||||||
}
|
}
|
||||||
@@ -57,13 +60,38 @@ fn ensure_init() -> Result<(), String> {
|
|||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sends one access unit per encoder packet. Returns `Ok(false)` when the receiver is gone.
|
pub struct EncodedUnit { pub data: Vec<u8>, pub permit: OwnedSemaphorePermit, pub completed_at: Instant }
|
||||||
fn drain(encoder: &mut encoder::Video, packet: &mut Packet, units: &mpsc::Sender<Vec<u8>>) -> Result<bool, String> {
|
|
||||||
|
fn acquire(budget: &Arc<Semaphore>, units: &mpsc::Sender<EncodedUnit>) -> Option<OwnedSemaphorePermit> {
|
||||||
|
tokio::runtime::Handle::current().block_on(async {
|
||||||
|
tokio::select! {
|
||||||
|
permit = budget.clone().acquire_owned() => permit.ok(),
|
||||||
|
_ = units.closed() => None,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod budget_tests {
|
||||||
|
use super::*;
|
||||||
|
#[tokio::test]
|
||||||
|
async fn closing_encoder_output_wakes_a_blocked_capture() {
|
||||||
|
let budget=Arc::new(Semaphore::new(0));
|
||||||
|
let (tx,rx)=mpsc::channel(1);
|
||||||
|
let worker=tokio::task::spawn_blocking(move || acquire(&budget,&tx));
|
||||||
|
tokio::task::yield_now().await;
|
||||||
|
drop(rx);
|
||||||
|
assert!(tokio::time::timeout(Duration::from_secs(1),worker).await.unwrap().unwrap().is_none());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn drain(encoder: &mut encoder::Video, packet: &mut Packet, units: &mpsc::Sender<EncodedUnit>, permits: &mut VecDeque<OwnedSemaphorePermit>) -> Result<bool, String> {
|
||||||
loop {
|
loop {
|
||||||
match encoder.receive_packet(packet) {
|
match encoder.receive_packet(packet) {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
let data = packet.data().unwrap_or_default().to_vec();
|
let data = packet.data().unwrap_or_default().to_vec();
|
||||||
if units.blocking_send(data).is_err() { return Ok(false); }
|
let permit = permits.pop_front().ok_or("Encoder returned an unbudgeted access unit")?;
|
||||||
|
if units.blocking_send(EncodedUnit {data, permit, completed_at: Instant::now()}).is_err() { return Ok(false); }
|
||||||
}
|
}
|
||||||
Err(ff::Error::Other { errno }) if errno == libc::EAGAIN => return Ok(true),
|
Err(ff::Error::Other { errno }) if errno == libc::EAGAIN => return Ok(true),
|
||||||
Err(error) => return Err(text(error)),
|
Err(error) => return Err(text(error)),
|
||||||
@@ -71,11 +99,11 @@ fn drain(encoder: &mut encoder::Video, packet: &mut Packet, units: &mpsc::Sender
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Opens the H.264 encoder for `config`. Returns the encoder, its input pixel format and output size.
|
/// Opens the selected video encoder for `config`. Returns the encoder, its input pixel format and output size.
|
||||||
fn open_encoder(config: &Config) -> Result<(encoder::Video, Pixel, u32, u32), String> {
|
fn open_encoder(config: &Config) -> Result<(encoder::Video, Pixel, u32, u32), String> {
|
||||||
let (width, height) = config.output.unwrap_or(config.input);
|
let (width, height) = config.output.unwrap_or(config.input);
|
||||||
let codec = encoder::find_by_name(config.encoder.name())
|
let codec = encoder::find_by_name(config.encoder.name_for(config.codec))
|
||||||
.ok_or("H.264 encoder is not available in this FFmpeg build")?;
|
.ok_or_else(|| format!("{} is not available in this FFmpeg build", config.encoder.name_for(config.codec)))?;
|
||||||
// Allocate for this codec so FFmpeg fills its defaults; libx264 rejects a context allocated without one.
|
// Allocate for this codec so FFmpeg fills its defaults; libx264 rejects a context allocated without one.
|
||||||
let mut context = unsafe { codec::context::Context::wrap(ff::sys::avcodec_alloc_context3(codec.as_ptr()), None) }.encoder().video().map_err(text)?;
|
let mut context = unsafe { codec::context::Context::wrap(ff::sys::avcodec_alloc_context3(codec.as_ptr()), None) }.encoder().video().map_err(text)?;
|
||||||
context.set_width(width);
|
context.set_width(width);
|
||||||
@@ -99,15 +127,22 @@ fn open_encoder(config: &Config) -> Result<(encoder::Video, Pixel, u32, u32), St
|
|||||||
let cq = config.cq.to_string();
|
let cq = config.cq.to_string();
|
||||||
let gop = config.gop.to_string();
|
let gop = config.gop.to_string();
|
||||||
let mut options = Dictionary::new();
|
let mut options = Dictionary::new();
|
||||||
|
let hevc = config.codec == crate::capture_setting::VideoCodec::H265;
|
||||||
if config.encoder == crate::video::Encoder::Vaapi {
|
if config.encoder == crate::video::Encoder::Vaapi {
|
||||||
for (key,value) in [("profile", "constrained_baseline"), ("rc_mode", "VBR"), ("async_depth", "1"), ("aud", "1")] { options.set(key,value); }
|
for (key,value) in [("profile", if hevc { "main" } else { "constrained_baseline" }), ("rc_mode", "VBR"), ("async_depth", "1"), ("aud", "1")] { options.set(key,value); }
|
||||||
options.set("qmin", &cq);
|
options.set("qmin", &cq);
|
||||||
} else if config.encoder == crate::video::Encoder::Nvenc {
|
} else if config.encoder == crate::video::Encoder::Nvenc {
|
||||||
for (key, value) in [("preset", "p4"), ("tune", "ull"), ("profile", "baseline"), ("rc", "vbr"),
|
for (key, value) in [("preset", "p4"), ("tune", "ull"), ("profile", if hevc { "main" } else { "baseline" }), ("rc", "vbr"),
|
||||||
("rc-lookahead", "0"), ("zerolatency", "1"), ("delay", "0"), ("aud", "1"), ("forced-idr", "1")] {
|
("rc-lookahead", "0"), ("zerolatency", "1"), ("delay", "0"), ("aud", "1"), ("forced-idr", "1")] {
|
||||||
options.set(key, value);
|
options.set(key, value);
|
||||||
}
|
}
|
||||||
options.set("cq", &cq);
|
options.set("cq", &cq);
|
||||||
|
} else if hevc {
|
||||||
|
for (key, value) in [("preset", "superfast"), ("tune", "zerolatency"), ("profile", "main"), ("threads", "4"), ("forced-idr", "1")] {
|
||||||
|
options.set(key, value);
|
||||||
|
}
|
||||||
|
options.set("crf", &cq);
|
||||||
|
options.set("x265-params", &crate::video::x265_params(&gop));
|
||||||
} else {
|
} else {
|
||||||
let x264 = format!("aud=1:repeat-headers=1:scenecut=0:keyint={gop}:min-keyint={gop}");
|
let x264 = format!("aud=1:repeat-headers=1:scenecut=0:keyint={gop}:min-keyint={gop}");
|
||||||
for (key, value) in [("preset", "superfast"), ("tune", "zerolatency"), ("profile", "baseline"), ("threads", "4")] {
|
for (key, value) in [("preset", "superfast"), ("tune", "zerolatency"), ("profile", "baseline"), ("threads", "4")] {
|
||||||
@@ -160,11 +195,11 @@ fn send_video_frame(encoder: &mut encoder::Video, frame: &frame::Video, kind: cr
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Runs until `stop` is set or the receiver is dropped. A set `idr` flag forces the next frame to be an IDR.
|
/// Runs until `stop` is set or the receiver is dropped. A set `idr` flag forces the next frame to be an IDR.
|
||||||
pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>) -> Result<(), String> {
|
pub fn run(config: Config, units: mpsc::Sender<EncodedUnit>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>, budget: Arc<Semaphore>) -> Result<(), String> {
|
||||||
ensure_init()?;
|
ensure_init()?;
|
||||||
if config.damage {
|
if config.damage {
|
||||||
match DamageCapture::open(&config.display) {
|
match DamageCapture::open(&config.display) {
|
||||||
Ok(capture) => return run_damage(config, capture, units, stop, idr),
|
Ok(capture) => return run_damage(config, capture, units, stop, idr, budget),
|
||||||
Err(error) => log::warn!("Damage-driven capture unavailable, using x11grab: {error}"),
|
Err(error) => log::warn!("Damage-driven capture unavailable, using x11grab: {error}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -187,14 +222,18 @@ pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>,
|
|||||||
|
|
||||||
let mut scaler: Option<(scaling::Context, (Pixel, u32, u32))> = None;
|
let mut scaler: Option<(scaling::Context, (Pixel, u32, u32))> = None;
|
||||||
let mut decoded = frame::Video::empty();
|
let mut decoded = frame::Video::empty();
|
||||||
let mut converted = frame::Video::new(pixel, width, height);
|
let mut converted = frame::Video::empty();
|
||||||
let mut packet = Packet::empty();
|
let mut packet = Packet::empty();
|
||||||
let started = Instant::now();
|
let started = Instant::now();
|
||||||
let frame_interval = Duration::from_nanos(1_000_000_000 / u64::from(config.fps));
|
let frame_interval = Duration::from_nanos(1_000_000_000 / u64::from(config.fps));
|
||||||
let mut last_at: Option<Duration> = None;
|
let mut last_at: Option<Duration> = None;
|
||||||
let mut last_slot: i64 = -1;
|
let mut last_slot: i64 = -1;
|
||||||
for (stream, input_packet) in input.packets() {
|
let mut permits = VecDeque::with_capacity(4);
|
||||||
|
let mut packets = input.packets();
|
||||||
|
loop {
|
||||||
if stop.load(Ordering::Relaxed) { return Ok(()); }
|
if stop.load(Ordering::Relaxed) { return Ok(()); }
|
||||||
|
let Some(permit) = acquire(&budget, &units) else { return Ok(()); };
|
||||||
|
let Some((stream, input_packet)) = packets.next() else { return Ok(()); };
|
||||||
if stream.index() != index { continue; }
|
if stream.index() != index { continue; }
|
||||||
decoder.send_packet(&input_packet).map_err(text)?;
|
decoder.send_packet(&input_packet).map_err(text)?;
|
||||||
match decoder.receive_frame(&mut decoded) {
|
match decoder.receive_frame(&mut decoded) {
|
||||||
@@ -228,20 +267,19 @@ pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>,
|
|||||||
frame.set_pts(Some(slot));
|
frame.set_pts(Some(slot));
|
||||||
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
|
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
|
||||||
send_video_frame(&mut encoder, frame, config.encoder)?;
|
send_video_frame(&mut encoder, frame, config.encoder)?;
|
||||||
if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); }
|
permits.push_back(permit);
|
||||||
|
if !drain(&mut encoder, &mut packet, &units, &mut permits)? { return Ok(()); }
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Damage-driven loop. Damage only marks the picture dirty; a dirty picture is captured and encoded on
|
/// Damage-driven loop. Damage only marks the picture dirty; a dirty picture is captured and encoded on
|
||||||
/// the next fixed frame tick, so the stream keeps a steady cadence while the screen moves. The viewer's
|
/// the next fixed frame tick, so the stream keeps a steady cadence while the screen moves. The viewer's
|
||||||
/// jitter buffer grows with irregular arrivals, and irregular sends were what raised its latency.
|
/// jitter buffer grows with irregular arrivals, and irregular sends were what raised its latency.
|
||||||
/// An idle screen sends a keepalive about once a second so the viewer still sees traffic.
|
fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<EncodedUnit>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>, budget: Arc<Semaphore>) -> Result<(), String> {
|
||||||
fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>) -> Result<(), String> {
|
|
||||||
let (mut encoder, pixel, width, height) = open_encoder(&config)?;
|
let (mut encoder, pixel, width, height) = open_encoder(&config)?;
|
||||||
let (cap_width, cap_height) = capture.size();
|
let (cap_width, cap_height) = capture.size();
|
||||||
let mut captured = frame::Video::new(Pixel::BGRZ, cap_width, cap_height);
|
let mut captured = frame::Video::new(Pixel::BGRZ, cap_width, cap_height);
|
||||||
let mut converted = frame::Video::new(pixel, width, height);
|
let mut converted = frame::Video::empty();
|
||||||
let mut scaler = None;
|
let mut scaler = None;
|
||||||
let mut packet = Packet::empty();
|
let mut packet = Packet::empty();
|
||||||
let started = Instant::now();
|
let started = Instant::now();
|
||||||
@@ -253,6 +291,9 @@ fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Ve
|
|||||||
let mut next_tick = Instant::now();
|
let mut next_tick = Instant::now();
|
||||||
let mut last_send = Instant::now();
|
let mut last_send = Instant::now();
|
||||||
let mut last_slot: i64 = -1;
|
let mut last_slot: i64 = -1;
|
||||||
|
let mut permits = VecDeque::with_capacity(4);
|
||||||
|
let locked = crate::session_lock::watch_locked();
|
||||||
|
let mut last_locked = *locked.borrow();
|
||||||
loop {
|
loop {
|
||||||
if stop.load(Ordering::Relaxed) { return Ok(()); }
|
if stop.load(Ordering::Relaxed) { return Ok(()); }
|
||||||
let deadline = if dirty { next_tick } else { last_send + keepalive };
|
let deadline = if dirty { next_tick } else { last_send + keepalive };
|
||||||
@@ -263,13 +304,20 @@ fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Ve
|
|||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
if dirty && now < next_tick { continue; }
|
if dirty && now < next_tick { continue; }
|
||||||
if !dirty && now < last_send + keepalive { continue; }
|
if !dirty && now < last_send + keepalive { continue; }
|
||||||
|
let Some(permit) = acquire(&budget, &units) else { return Ok(()); };
|
||||||
|
if stop.load(Ordering::Relaxed) { return Ok(()); }
|
||||||
|
dirty |= capture.wait(Duration::ZERO)?;
|
||||||
|
let state = *locked.borrow();
|
||||||
|
if state != last_locked { dirty = true; last_locked = state; }
|
||||||
|
|
||||||
let source = capture.capture()?;
|
if dirty {
|
||||||
let bytes_per_line = cap_width as usize * 4;
|
let source = capture.capture()?;
|
||||||
let stride = captured.stride(0);
|
let bytes_per_line = cap_width as usize * 4;
|
||||||
let row = bytes_per_line.min(stride);
|
let stride = captured.stride(0);
|
||||||
for (y, src) in source.chunks_exact(bytes_per_line).enumerate() {
|
let row = bytes_per_line.min(stride);
|
||||||
captured.data_mut(0)[y * stride..y * stride + row].copy_from_slice(&src[..row]);
|
for (y, src) in source.chunks_exact(bytes_per_line).enumerate() {
|
||||||
|
captured.data_mut(0)[y * stride..y * stride + row].copy_from_slice(&src[..row]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let slot = ((started.elapsed().as_micros() * u128::from(config.fps) + 500_000) / 1_000_000) as i64;
|
let slot = ((started.elapsed().as_micros() * u128::from(config.fps) + 500_000) / 1_000_000) as i64;
|
||||||
@@ -282,13 +330,16 @@ fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Ve
|
|||||||
if scaler.is_none() {
|
if scaler.is_none() {
|
||||||
scaler = Some(scaling::Context::get(Pixel::BGRZ, cap_width, cap_height, pixel, width, height, scaling::Flags::BILINEAR).map_err(text)?);
|
scaler = Some(scaling::Context::get(Pixel::BGRZ, cap_width, cap_height, pixel, width, height, scaling::Flags::BILINEAR).map_err(text)?);
|
||||||
}
|
}
|
||||||
if let Some(context) = scaler.as_mut() { context.run(&captured, &mut converted).map_err(text)?; }
|
if dirty {
|
||||||
|
if let Some(context) = scaler.as_mut() { context.run(&captured, &mut converted).map_err(text)?; }
|
||||||
|
}
|
||||||
&mut converted
|
&mut converted
|
||||||
};
|
};
|
||||||
frame.set_pts(Some(slot));
|
frame.set_pts(Some(slot));
|
||||||
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
|
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
|
||||||
send_video_frame(&mut encoder, frame, config.encoder)?;
|
send_video_frame(&mut encoder, frame, config.encoder)?;
|
||||||
if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); }
|
permits.push_back(permit);
|
||||||
|
if !drain(&mut encoder, &mut packet, &units, &mut permits)? { return Ok(()); }
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
if dirty {
|
if dirty {
|
||||||
// Keep the tick phase; after a long idle, restart the phase from now instead of bursting to catch up.
|
// Keep the tick phase; after a long idle, restart the phase from now instead of bursting to catch up.
|
||||||
|
|||||||
+128
-2
@@ -43,8 +43,12 @@ struct HeldInput { buttons: std::collections::BTreeSet<i32>, keys: std::collecti
|
|||||||
/// Held modifier VKs (bit 0 Ctrl, 1 Shift, 2 Alt, 3 Win) and keys that got a synthetic Shift.
|
/// Held modifier VKs (bit 0 Ctrl, 1 Shift, 2 Alt, 3 Win) and keys that got a synthetic Shift.
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct Shortcuts { modifiers: u8, shifted: std::collections::BTreeSet<i32> }
|
struct Shortcuts { modifiers: u8, shifted: std::collections::BTreeSet<i32> }
|
||||||
|
/// Mac viewer with 「使用 macOS 快捷键」 off sends Cmd as VK_LWIN (verified: Cmd 0x5B, Option 0xA4,
|
||||||
|
/// Ctrl 0xA2). Cmd is held back until we know whether it starts a Mac shortcut.
|
||||||
|
#[derive(Default)]
|
||||||
|
struct CmdState { held: bool, pending: bool, passthrough: bool, combos: std::collections::BTreeMap<i32, bool> }
|
||||||
|
|
||||||
pub struct InputInjector { serial:std::sync::Mutex<()>, owner:std::sync::Mutex<DesktopOwner>, shortcuts: std::sync::Mutex<Shortcuts>, uinput: Option<crate::uinput::Devices>, screen: std::sync::Mutex<(u32,u32)>, portal_failures: std::sync::atomic::AtomicUsize, portal_outcomes: [std::sync::atomic::AtomicUsize; 3], portal:std::sync::Mutex<Option<std::sync::Arc<crate::portal::PortalSession>>>, backend: crate::desktop_backend::DesktopBackend, pending: std::sync::Mutex<PendingPointer>, held: std::sync::Mutex<HeldInput> }
|
pub struct InputInjector { mac_controller: std::sync::atomic::AtomicBool, cmd: std::sync::Mutex<CmdState>, serial:std::sync::Mutex<()>, owner:std::sync::Mutex<DesktopOwner>, shortcuts: std::sync::Mutex<Shortcuts>, uinput: Option<crate::uinput::Devices>, screen: std::sync::Mutex<(u32,u32)>, portal_failures: std::sync::atomic::AtomicUsize, portal_outcomes: [std::sync::atomic::AtomicUsize; 3], portal:std::sync::Mutex<Option<std::sync::Arc<crate::portal::PortalSession>>>, backend: crate::desktop_backend::DesktopBackend, pending: std::sync::Mutex<PendingPointer>, held: std::sync::Mutex<HeldInput> }
|
||||||
|
|
||||||
impl InputInjector {
|
impl InputInjector {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
@@ -55,7 +59,7 @@ impl InputInjector {
|
|||||||
Err(e) => { log::warn!("uinput unavailable ({}); using the desktop input path. See README for the udev rule.", e.kind()); None }
|
Err(e) => { log::warn!("uinput unavailable ({}); using the desktop input path. See README for the udev rule.", e.kind()); None }
|
||||||
}
|
}
|
||||||
} else { None };
|
} else { None };
|
||||||
Self { serial:std::sync::Mutex::new(()), owner:std::sync::Mutex::new(DesktopOwner::default()), shortcuts: std::sync::Mutex::new(Shortcuts::default()), uinput, screen: std::sync::Mutex::new((0,0)), portal_failures: std::sync::atomic::AtomicUsize::new(0), portal_outcomes: std::array::from_fn(|_| std::sync::atomic::AtomicUsize::new(0)), portal:std::sync::Mutex::new(None), backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11), pending: std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()) }
|
Self { mac_controller: std::sync::atomic::AtomicBool::new(false), cmd: std::sync::Mutex::new(CmdState::default()), serial:std::sync::Mutex::new(()), owner:std::sync::Mutex::new(DesktopOwner::default()), shortcuts: std::sync::Mutex::new(Shortcuts::default()), uinput, screen: std::sync::Mutex::new((0,0)), portal_failures: std::sync::atomic::AtomicUsize::new(0), portal_outcomes: std::array::from_fn(|_| std::sync::atomic::AtomicUsize::new(0)), portal:std::sync::Mutex::new(None), backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11), pending: std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bind_portal(&self,session:Option<std::sync::Arc<crate::portal::PortalSession>>) {
|
pub fn bind_portal(&self,session:Option<std::sync::Arc<crate::portal::PortalSession>>) {
|
||||||
@@ -167,8 +171,62 @@ impl InputInjector {
|
|||||||
shift
|
shift
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The current desktop controller is the macOS viewer (connect header controller_platform).
|
||||||
|
pub fn set_mac_controller(&self, mac: bool) {
|
||||||
|
self.mac_controller.store(mac, std::sync::atomic::Ordering::Relaxed);
|
||||||
|
*self.cmd.lock().unwrap() = CmdState::default();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn terminal_focused(&self) -> bool {
|
||||||
|
self.backend == crate::desktop_backend::DesktopBackend::X11 && std::env::var_os("DISPLAY").is_some()
|
||||||
|
&& !std::env::var("UU_TERMINAL_SHORTCUTS").is_ok_and(|v| v == "0")
|
||||||
|
&& unsafe { x11_terminal_shortcut_state() } & 1 != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Mac habits for a Mac viewer with 「使用 macOS 快捷键」 off: Cmd+C/V/A/F/X/Z (the keys that option
|
||||||
|
/// lists) become Ctrl+key, and in a focused terminal Cmd+C/V become Ctrl+Shift+C/V (copy/paste)
|
||||||
|
/// while Ctrl+C still interrupts. Other Cmd combos, and Cmd alone, stay Super. Returns true when handled.
|
||||||
|
fn mac_cmd(&self, key: i32, is_press: bool) -> bool {
|
||||||
|
const SUPER_L: u64 = 0xffeb; const CONTROL_L: u64 = 0xffe3; const SHIFT_L: u64 = 0xffe1;
|
||||||
|
let mut st = self.cmd.lock().unwrap();
|
||||||
|
if matches!(key, 0x5b | 0x5c) {
|
||||||
|
if is_press { st.held = true; st.pending = true; st.passthrough = false; }
|
||||||
|
else {
|
||||||
|
if st.passthrough { self.inject_key(SUPER_L, false); }
|
||||||
|
else if st.pending { self.inject_key(SUPER_L, true); self.inject_key(SUPER_L, false); }
|
||||||
|
st.held = false; st.pending = false; st.passthrough = false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if !is_press {
|
||||||
|
let Some(shifted) = st.combos.remove(&key) else { return false };
|
||||||
|
self.key_symbol_event(key, false);
|
||||||
|
if shifted { self.inject_key(SHIFT_L, false); }
|
||||||
|
self.inject_key(CONTROL_L, false);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if !st.held { return false; }
|
||||||
|
// Shift/Option/Ctrl pressed with Cmd (e.g. Cmd+Shift+Z) must not decide the combo yet.
|
||||||
|
if matches!(key, 0x10..=0x12 | 0xa0..=0xa5) { return false; }
|
||||||
|
if st.combos.contains_key(&key) { self.key_symbol_event(key, true); return true; } // autorepeat
|
||||||
|
if matches!(key, 0x41 | 0x43 | 0x46 | 0x56 | 0x58 | 0x5a) && !st.passthrough {
|
||||||
|
st.pending = false;
|
||||||
|
let shifted = matches!(key, 0x43 | 0x56) && self.terminal_focused();
|
||||||
|
self.inject_key(CONTROL_L, true);
|
||||||
|
if shifted { self.inject_key(SHIFT_L, true); }
|
||||||
|
self.key_symbol_event(key, true);
|
||||||
|
st.combos.insert(key, shifted);
|
||||||
|
static NOTED: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||||
|
if NOTED.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 4 { log::info!("Mac shortcut: Cmd combo sent as Ctrl{}", if shifted { "+Shift (terminal)" } else { "" }); }
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if st.pending { self.inject_key(SUPER_L, true); st.pending = false; st.passthrough = true; }
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
pub fn windows_key_event(&self, key: i32, is_press: bool) {
|
pub fn windows_key_event(&self, key: i32, is_press: bool) {
|
||||||
let _serial=self.serial.lock().unwrap();
|
let _serial=self.serial.lock().unwrap();
|
||||||
|
if self.mac_controller.load(std::sync::atomic::Ordering::Relaxed) && self.mac_cmd(key, is_press) { return; }
|
||||||
if self.terminal_shift(key, is_press) {
|
if self.terminal_shift(key, is_press) {
|
||||||
const SHIFT_L: u64 = 0xffe1;
|
const SHIFT_L: u64 = 0xffe1;
|
||||||
if is_press {
|
if is_press {
|
||||||
@@ -207,6 +265,8 @@ impl InputInjector {
|
|||||||
unsafe {x11_release_all();}
|
unsafe {x11_release_all();}
|
||||||
} else if self.backend==crate::desktop_backend::DesktopBackend::Wayland && self.portal.lock().unwrap().is_none() {
|
} else if self.backend==crate::desktop_backend::DesktopBackend::Wayland && self.portal.lock().unwrap().is_none() {
|
||||||
crate::wayland::release_all();
|
crate::wayland::release_all();
|
||||||
|
} else if self.backend==crate::desktop_backend::DesktopBackend::Portal && self.portal.lock().unwrap().is_none() {
|
||||||
|
// Nothing is bound yet (e.g. the first locked connection): no input can be held.
|
||||||
} else {
|
} else {
|
||||||
// Portal/Wayland cannot query global state. Sweep every key/button
|
// Portal/Wayland cannot query global state. Sweep every key/button
|
||||||
// that this host can emit through the currently bound session.
|
// that this host can emit through the currently bound session.
|
||||||
@@ -221,6 +281,7 @@ impl InputInjector {
|
|||||||
}
|
}
|
||||||
*held=HeldInput::default();
|
*held=HeldInput::default();
|
||||||
*self.shortcuts.lock().unwrap()=Shortcuts::default();
|
*self.shortcuts.lock().unwrap()=Shortcuts::default();
|
||||||
|
*self.cmd.lock().unwrap()=CmdState::default();
|
||||||
}
|
}
|
||||||
// Per-injector budgets cover missing bindings, locked sessions, stale handles
|
// Per-injector budgets cover missing bindings, locked sessions, stale handles
|
||||||
// and D-Bus errors. Never include keys, coordinates, text or portal tokens.
|
// and D-Bus errors. Never include keys, coordinates, text or portal tokens.
|
||||||
@@ -386,6 +447,15 @@ impl InputInjector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl DesktopInputSession {
|
impl DesktopInputSession {
|
||||||
|
pub fn is_current(&self) -> bool {
|
||||||
|
let owner = self.input.owner.lock().unwrap();
|
||||||
|
owner.owns(self.id)
|
||||||
|
}
|
||||||
|
/// Call only inside with_current or with_input, so a superseded peer cannot change bindings.
|
||||||
|
pub fn bind_portal(&self, portal: Option<std::sync::Arc<crate::portal::PortalSession>>) {
|
||||||
|
self.input.bind_portal(portal);
|
||||||
|
}
|
||||||
|
pub fn release_input(&self) { self.input.release_held(); }
|
||||||
pub fn connected(&self) {
|
pub fn connected(&self) {
|
||||||
let mut owner=self.input.owner.lock().unwrap();
|
let mut owner=self.input.owner.lock().unwrap();
|
||||||
if owner.owns(self.id) && !owner.active {self.input.release_held();owner.active=true;}
|
if owner.owns(self.id) && !owner.active {self.input.release_held();owner.active=true;}
|
||||||
@@ -394,6 +464,11 @@ impl DesktopInputSession {
|
|||||||
let mut owner=self.input.owner.lock().unwrap();
|
let mut owner=self.input.owner.lock().unwrap();
|
||||||
if owner.owns(self.id) && owner.active {owner.active=false;self.input.release_held();}
|
if owner.owns(self.id) && owner.active {owner.active=false;self.input.release_held();}
|
||||||
}
|
}
|
||||||
|
/// Lifecycle updates survive a transient disconnect, but serialize with replacement and shutdown.
|
||||||
|
pub fn with_current(&self, action:impl FnOnce()) {
|
||||||
|
let owner=self.input.owner.lock().unwrap();
|
||||||
|
if owner.owns(self.id) {action();}
|
||||||
|
}
|
||||||
pub fn with_input(&self, action:impl FnOnce()) {
|
pub fn with_input(&self, action:impl FnOnce()) {
|
||||||
let owner=self.input.owner.lock().unwrap();
|
let owner=self.input.owner.lock().unwrap();
|
||||||
if owner.owns(self.id) && owner.active {action();}
|
if owner.owns(self.id) && owner.active {action();}
|
||||||
@@ -401,11 +476,62 @@ impl DesktopInputSession {
|
|||||||
}
|
}
|
||||||
impl Drop for DesktopInputSession {fn drop(&mut self) {self.disconnected();}}
|
impl Drop for DesktopInputSession {fn drop(&mut self) {self.disconnected();}}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
impl InputInjector {
|
||||||
|
pub fn without_desktop() -> Self {
|
||||||
|
Self { mac_controller:std::sync::atomic::AtomicBool::new(false),
|
||||||
|
cmd:std::sync::Mutex::new(CmdState::default()), serial:std::sync::Mutex::new(()),
|
||||||
|
owner:std::sync::Mutex::new(DesktopOwner::default()), shortcuts:std::sync::Mutex::new(Shortcuts::default()),
|
||||||
|
uinput:None, screen:std::sync::Mutex::new((0,0)), portal_failures:std::sync::atomic::AtomicUsize::new(0),
|
||||||
|
portal_outcomes:std::array::from_fn(|_|std::sync::atomic::AtomicUsize::new(0)),
|
||||||
|
portal:std::sync::Mutex::new(None), backend:crate::desktop_backend::DesktopBackend::Portal,
|
||||||
|
pending:std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod lifecycle_tests {
|
mod lifecycle_tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::sync::{Arc,atomic::{AtomicUsize,Ordering}};
|
use std::sync::{Arc,atomic::{AtomicUsize,Ordering}};
|
||||||
#[test]
|
#[test]
|
||||||
|
fn current_owner_can_publish_while_disconnected_but_cannot_inject() {
|
||||||
|
let input=Arc::new(InputInjector::without_desktop());
|
||||||
|
let session=input.desktop_session();
|
||||||
|
session.connected();session.disconnected();
|
||||||
|
let publications=AtomicUsize::new(0);
|
||||||
|
session.with_current(||{publications.fetch_add(1,Ordering::Relaxed);});
|
||||||
|
session.with_input(||panic!("disconnected owner must not inject"));
|
||||||
|
assert_eq!(publications.load(Ordering::Relaxed),1);
|
||||||
|
session.connected();
|
||||||
|
session.with_input(||{publications.fetch_add(1,Ordering::Relaxed);});
|
||||||
|
let next=input.desktop_session();
|
||||||
|
session.with_current(||panic!("replaced owner must not publish"));
|
||||||
|
next.with_current(||{publications.fetch_add(1,Ordering::Relaxed);});
|
||||||
|
input.shutdown();
|
||||||
|
next.with_current(||panic!("shutdown must reject lifecycle updates"));
|
||||||
|
assert_eq!(publications.load(Ordering::Relaxed),3);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn current_owner_gate_holds_ownership_through_publication() {
|
||||||
|
let input=Arc::new(InputInjector::without_desktop());
|
||||||
|
let session=input.desktop_session();
|
||||||
|
let (started,ready)=std::sync::mpsc::channel();
|
||||||
|
let (finish,resume)=std::sync::mpsc::channel();
|
||||||
|
let worker=session.clone();
|
||||||
|
let publication=std::thread::spawn(move ||worker.with_current(|| {
|
||||||
|
started.send(()).unwrap();resume.recv().unwrap();
|
||||||
|
}));
|
||||||
|
ready.recv_timeout(std::time::Duration::from_secs(2)).unwrap();
|
||||||
|
assert!(input.owner.try_lock().is_err());
|
||||||
|
let replacing=input.clone();
|
||||||
|
let replacement=std::thread::spawn(move ||replacing.desktop_session());
|
||||||
|
finish.send(()).unwrap();
|
||||||
|
publication.join().unwrap();
|
||||||
|
let next=replacement.join().unwrap();
|
||||||
|
assert!(next.is_current());
|
||||||
|
session.with_current(||panic!("late publication after replacement"));
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
fn cleanup_discards_motion_shortcuts_and_untracked_input_ledger() {
|
fn cleanup_discards_motion_shortcuts_and_untracked_input_ledger() {
|
||||||
let input=Arc::new(InputInjector::new());
|
let input=Arc::new(InputInjector::new());
|
||||||
input.held.lock().unwrap().keys.insert(0xffe1);
|
input.held.lock().unwrap().keys.insert(0xffe1);
|
||||||
|
|||||||
@@ -0,0 +1,101 @@
|
|||||||
|
//! Black video keeps locked Portal initialization reachable by the iPhone viewer.
|
||||||
|
use std::time::Duration;
|
||||||
|
use bytes::Bytes;
|
||||||
|
use webrtc::{media::Sample, peer_connection::{RTCPeerConnection, peer_connection_state::RTCPeerConnectionState}};
|
||||||
|
use crate::{capture_setting::VideoCodec, desktop_session::PortalDesktop, video_track::VideoTrack};
|
||||||
|
|
||||||
|
async fn black_frame(codec: VideoCodec) -> Result<Bytes, String> {
|
||||||
|
let mut command = crate::video::ffmpeg_command();
|
||||||
|
command.args(["-hide_banner", "-loglevel", "error", "-nostdin",
|
||||||
|
"-f", "lavfi", "-i", "color=c=black:s=1280x720:r=1", "-frames:v", "1",
|
||||||
|
"-an", "-pix_fmt", "yuv420p", "-threads", "1", "-preset", "ultrafast", "-tune", "zerolatency"]);
|
||||||
|
match codec {
|
||||||
|
VideoCodec::H264 => { command.args(["-c:v", "libx264", "-profile:v", "baseline",
|
||||||
|
"-x264-params", "keyint=1:repeat-headers=1:aud=1", "-f", "h264"]); },
|
||||||
|
VideoCodec::H265 => { command.args(["-c:v", "libx265",
|
||||||
|
"-x265-params", "keyint=1:repeat-headers=1:aud=1:pools=1:frame-threads=1:log-level=error", "-f", "hevc"]); },
|
||||||
|
}
|
||||||
|
command.arg("pipe:1").kill_on_drop(true);
|
||||||
|
let output = tokio::time::timeout(Duration::from_secs(5), command.output()).await
|
||||||
|
.map_err(|_| "Locked desktop video encoder timed out".to_string())?
|
||||||
|
.map_err(|error| format!("Locked desktop video encoder: {error}"))?;
|
||||||
|
if !output.status.success() || output.stdout.is_empty() {
|
||||||
|
return Err(format!("Locked desktop video encoder failed: status={}", output.status));
|
||||||
|
}
|
||||||
|
Ok(output.stdout.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run(pc: &RTCPeerConnection, track: &VideoTrack, desktop: &PortalDesktop) -> Result<(), String> {
|
||||||
|
run_with_encoder(pc, track, desktop, black_frame).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run_with_encoder<F, Fut>(pc: &RTCPeerConnection, track: &VideoTrack, desktop: &PortalDesktop, mut encode: F) -> Result<(), String>
|
||||||
|
where F: FnMut(VideoCodec) -> Fut, Fut: std::future::Future<Output = Result<Bytes, String>> {
|
||||||
|
// #29: iPhone 4.42.0 cold ON/OFF/ON tests only sent action15 after black video.
|
||||||
|
// Keep CONTROL permissions truthful and use the existing video track until real capture is ready.
|
||||||
|
let mut updates = desktop.subscribe();
|
||||||
|
let mut locked = desktop.lock_state();
|
||||||
|
let mut tick = tokio::time::interval(Duration::from_secs(1));
|
||||||
|
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||||
|
let mut frame = None;
|
||||||
|
let mut sent = 0u64;
|
||||||
|
let mut attempts = 0u64;
|
||||||
|
loop {
|
||||||
|
let state = updates.borrow_and_update().clone();
|
||||||
|
let _ = locked.borrow_and_update();
|
||||||
|
if state.ready() || !state.waiting || crate::video::transport_closed(pc) {
|
||||||
|
if attempts > 0 { log::info!("Locked desktop video ended: sent={sent} attempts={attempts}"); }
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
tokio::select! {
|
||||||
|
biased;
|
||||||
|
changed = updates.changed() => if changed.is_err() { return Ok(()); },
|
||||||
|
changed = locked.changed() => if changed.is_err() { return Ok(()); },
|
||||||
|
_ = tick.tick() => {
|
||||||
|
if !updates.borrow().waiting || *locked.borrow() != Some(true)
|
||||||
|
|| pc.connection_state() != RTCPeerConnectionState::Connected { continue; }
|
||||||
|
if frame.is_none() {
|
||||||
|
let codec = crate::capture_setting::preferred_codec(
|
||||||
|
track.supports(VideoCodec::H264), track.supports(VideoCodec::H265));
|
||||||
|
let encoded = tokio::select! {
|
||||||
|
biased;
|
||||||
|
_ = updates.changed() => continue,
|
||||||
|
_ = locked.changed() => continue,
|
||||||
|
result = encode(codec) => result?,
|
||||||
|
};
|
||||||
|
frame = Some((codec, encoded));
|
||||||
|
}
|
||||||
|
if crate::video::transport_closed(pc) { return Ok(()); }
|
||||||
|
// Encoding can finish in the same poll that publishes unlock/readiness.
|
||||||
|
if !updates.borrow().waiting || *locked.borrow() != Some(true)
|
||||||
|
|| pc.connection_state() != RTCPeerConnectionState::Connected { continue; }
|
||||||
|
let (codec, data) = frame.as_ref().unwrap();
|
||||||
|
let sample = Sample { data: data.clone(), duration: Duration::from_secs(1), ..Default::default() };
|
||||||
|
let extensions = [crate::rtp_metadata::capture_index(0), crate::rtp_metadata::new_frame()];
|
||||||
|
let revision = track.revision();
|
||||||
|
// No monitor inventory or permission claims: 1280x720 is only this synthetic bitstream.
|
||||||
|
tokio::select! {
|
||||||
|
biased;
|
||||||
|
_ = updates.changed() => continue,
|
||||||
|
_ = locked.changed() => continue,
|
||||||
|
result = tokio::time::timeout(Duration::from_secs(2),
|
||||||
|
track.write_sample_with_extensions(*codec, &sample, &extensions)) => {
|
||||||
|
if crate::video::transport_closed(pc) { return Ok(()); }
|
||||||
|
result.map_err(|_| "Locked desktop video send timed out".to_string())?
|
||||||
|
.map_err(|error| format!("Locked desktop video send: {error}"))?;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
attempts += 1;
|
||||||
|
if track.revision() > revision { sent += 1; }
|
||||||
|
tick.reset();
|
||||||
|
if attempts == 1 || attempts % 10 == 0 {
|
||||||
|
log::info!("Locked desktop video: sent={sent} codec={codec:?} synthetic_size=1280x720 bytes={} attempts={attempts}", data.len());
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[path = "locked_video_tests.rs"]
|
||||||
|
mod tests;
|
||||||
@@ -0,0 +1,350 @@
|
|||||||
|
use super::{black_frame, run, run_with_encoder};
|
||||||
|
use crate::{
|
||||||
|
capture::ScreenCapture,
|
||||||
|
capture_setting::VideoCodec,
|
||||||
|
desktop_backend::DesktopBackend,
|
||||||
|
desktop_session::PortalDesktop,
|
||||||
|
video_track::VideoTrack,
|
||||||
|
};
|
||||||
|
use std::{process::Stdio, sync::Arc, time::Duration};
|
||||||
|
use tokio::{io::AsyncWriteExt, sync::{mpsc, watch}};
|
||||||
|
use webrtc::{
|
||||||
|
api::{media_engine::MediaEngine, APIBuilder},
|
||||||
|
media::Sample,
|
||||||
|
peer_connection::{
|
||||||
|
configuration::RTCConfiguration,
|
||||||
|
peer_connection_state::RTCPeerConnectionState,
|
||||||
|
RTCPeerConnection,
|
||||||
|
},
|
||||||
|
rtp::packet::Packet,
|
||||||
|
rtp_transceiver::{
|
||||||
|
rtp_codec::{RTCRtpCodecCapability, RTCRtpCodecParameters, RTPCodecType},
|
||||||
|
},
|
||||||
|
util::vnet::router::Router,
|
||||||
|
};
|
||||||
|
|
||||||
|
async fn decode_gray(data: &[u8], demuxer: &str) -> (Vec<u8>, String) {
|
||||||
|
let mut command = crate::video::ffmpeg_command();
|
||||||
|
command.args([
|
||||||
|
"-hide_banner", "-loglevel", "info", "-nostdin", "-f", demuxer, "-i", "pipe:0",
|
||||||
|
"-frames:v", "1", "-vf", "scale=in_range=tv:out_range=pc,format=gray,showinfo",
|
||||||
|
"-pix_fmt", "gray", "-f", "rawvideo", "pipe:1",
|
||||||
|
]);
|
||||||
|
command.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::piped()).kill_on_drop(true);
|
||||||
|
let mut child = command.spawn().expect("start FFmpeg decoder");
|
||||||
|
let mut stdin = child.stdin.take().expect("FFmpeg decoder stdin");
|
||||||
|
let output = tokio::time::timeout(Duration::from_secs(10), async move {
|
||||||
|
stdin.write_all(data).await.expect("write encoded frame to FFmpeg");
|
||||||
|
drop(stdin);
|
||||||
|
child.wait_with_output().await.expect("wait for FFmpeg decoder")
|
||||||
|
}).await.expect("FFmpeg decoder timed out");
|
||||||
|
assert!(output.status.success(), "FFmpeg decoder failed: status={}", output.status);
|
||||||
|
(output.stdout, String::from_utf8_lossy(&output.stderr).into_owned())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn black_frame_decodes_as_1280x720_black(codec: VideoCodec, demuxer: &str) {
|
||||||
|
let encoded = black_frame(codec).await.expect("encode black frame");
|
||||||
|
assert!(!encoded.is_empty());
|
||||||
|
let (gray, diagnostics) = decode_gray(&encoded, demuxer).await;
|
||||||
|
assert!(diagnostics.contains("s:1280x720"), "decoded dimensions were not 1280x720");
|
||||||
|
assert_eq!(gray.len(), 1280 * 720, "expected one 1280x720 gray frame");
|
||||||
|
assert!(gray.iter().all(|&pixel| pixel <= 1), "decoded frame was not black");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "requires FFmpeg libx264/libx265"]
|
||||||
|
async fn h264_black_frame_decodes_to_1280x720_black() {
|
||||||
|
black_frame_decodes_as_1280x720_black(VideoCodec::H264, "h264").await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "requires FFmpeg libx264/libx265"]
|
||||||
|
async fn h265_black_frame_decodes_to_1280x720_black() {
|
||||||
|
black_frame_decodes_as_1280x720_black(VideoCodec::H265, "hevc").await;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ready_screen() -> Arc<ScreenCapture> {
|
||||||
|
let mut screen = ScreenCapture::unavailable("synthetic ready desktop".into());
|
||||||
|
screen.backend = DesktopBackend::Portal;
|
||||||
|
screen.init_error = None;
|
||||||
|
screen.display = "wayland:test".into();
|
||||||
|
screen.width = 1280;
|
||||||
|
screen.height = 720;
|
||||||
|
Arc::new(screen)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn receive_frame(packets: &mut mpsc::UnboundedReceiver<Packet>, within: Duration) -> Vec<Packet> {
|
||||||
|
tokio::time::timeout(within, async {
|
||||||
|
let mut frame = Vec::new();
|
||||||
|
loop {
|
||||||
|
let packet = packets.recv().await.expect("RTP packet receiver closed");
|
||||||
|
let marker = packet.header.marker;
|
||||||
|
frame.push(packet);
|
||||||
|
if marker {
|
||||||
|
return frame;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).await.expect("timed out waiting for an RTP frame")
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn assert_no_rtp(packets: &mut mpsc::UnboundedReceiver<Packet>, within: Duration) {
|
||||||
|
assert!(tokio::time::timeout(within, packets.recv()).await.is_err(),
|
||||||
|
"locked video task sent RTP while transmission was disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn assert_single_rtp_frame(frame: &[Packet]) {
|
||||||
|
let first = frame.first().expect("RTP frame has packets");
|
||||||
|
let last = frame.last().unwrap();
|
||||||
|
assert!(last.header.marker, "RTP frame ends with a marker packet");
|
||||||
|
for pair in frame.windows(2) {
|
||||||
|
assert_eq!(pair[0].header.ssrc, pair[1].header.ssrc);
|
||||||
|
assert_eq!(pair[0].header.timestamp, pair[1].header.timestamp);
|
||||||
|
assert_eq!(pair[1].header.sequence_number, pair[0].header.sequence_number.wrapping_add(1));
|
||||||
|
}
|
||||||
|
assert!(frame.iter().all(|packet| packet.header.ssrc == first.header.ssrc));
|
||||||
|
}
|
||||||
|
|
||||||
|
struct VirtualPeers {
|
||||||
|
host: Arc<RTCPeerConnection>,
|
||||||
|
remote: Arc<RTCPeerConnection>,
|
||||||
|
_control: Arc<webrtc::data_channel::RTCDataChannel>,
|
||||||
|
router: Arc<tokio::sync::Mutex<Router>>,
|
||||||
|
track: Arc<VideoTrack>,
|
||||||
|
packets: mpsc::UnboundedReceiver<Packet>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VirtualPeers {
|
||||||
|
async fn new(codec: VideoCodec) -> Self {
|
||||||
|
let (remote_setting, host_setting, router) = crate::test_network::virtual_network().await;
|
||||||
|
|
||||||
|
let mut remote_media = MediaEngine::default();
|
||||||
|
match codec {
|
||||||
|
VideoCodec::H264 => remote_media.register_codec(RTCRtpCodecParameters {
|
||||||
|
capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: VideoCodec::H264.mime().into(),
|
||||||
|
clock_rate: 90_000,
|
||||||
|
sdp_fmtp_line: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f".into(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
payload_type: 102,
|
||||||
|
..Default::default()
|
||||||
|
}, RTPCodecType::Video).unwrap(),
|
||||||
|
VideoCodec::H265 => crate::video_track::register_hevc(&mut remote_media).unwrap(),
|
||||||
|
}
|
||||||
|
let remote = Arc::new(APIBuilder::new().with_setting_engine(remote_setting)
|
||||||
|
.with_media_engine(remote_media).build()
|
||||||
|
.new_peer_connection(RTCConfiguration::default()).await.unwrap());
|
||||||
|
remote.add_transceiver_from_kind(RTPCodecType::Video, None).await.unwrap();
|
||||||
|
let control = remote.create_data_channel("CONTROL_DATA_CHANNEL", None).await.unwrap();
|
||||||
|
|
||||||
|
let mut host_media = MediaEngine::default();
|
||||||
|
host_media.register_default_codecs().unwrap();
|
||||||
|
crate::video_track::register_hevc(&mut host_media).unwrap();
|
||||||
|
let host = Arc::new(APIBuilder::new().with_setting_engine(host_setting)
|
||||||
|
.with_media_engine(host_media).build()
|
||||||
|
.new_peer_connection(RTCConfiguration::default()).await.unwrap());
|
||||||
|
let track = Arc::new(VideoTrack::new());
|
||||||
|
host.add_track(track.clone()).await.unwrap();
|
||||||
|
|
||||||
|
let (packet_tx, packets) = mpsc::unbounded_channel();
|
||||||
|
remote.on_track(Box::new(move |remote_track, _, _| {
|
||||||
|
let packet_tx = packet_tx.clone();
|
||||||
|
Box::pin(async move {
|
||||||
|
tokio::spawn(async move {
|
||||||
|
while let Ok((packet, _)) = remote_track.read_rtp().await {
|
||||||
|
if packet_tx.send(packet).is_err() { break; }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})
|
||||||
|
}));
|
||||||
|
|
||||||
|
Self { host, remote, _control: control, router, track, packets }
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn connect(&self, codec: VideoCodec) {
|
||||||
|
let offer = self.remote.create_offer(None).await.unwrap();
|
||||||
|
let mut gathered = self.remote.gathering_complete_promise().await;
|
||||||
|
self.remote.set_local_description(offer).await.unwrap();
|
||||||
|
gathered.recv().await;
|
||||||
|
self.host.set_remote_description(self.remote.local_description().await.unwrap()).await.unwrap();
|
||||||
|
let answer = self.host.create_answer(None).await.unwrap();
|
||||||
|
let mut gathered = self.host.gathering_complete_promise().await;
|
||||||
|
self.host.set_local_description(answer).await.unwrap();
|
||||||
|
gathered.recv().await;
|
||||||
|
self.remote.set_remote_description(self.host.local_description().await.unwrap()).await.unwrap();
|
||||||
|
tokio::time::timeout(Duration::from_secs(15), async {
|
||||||
|
while self.host.connection_state() != RTCPeerConnectionState::Connected
|
||||||
|
|| !self.track.supports(codec)
|
||||||
|
{
|
||||||
|
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||||
|
}
|
||||||
|
}).await.expect("virtual WebRTC peers did not connect with the requested codec");
|
||||||
|
assert!(!self.track.supports(match codec {
|
||||||
|
VideoCodec::H264 => VideoCodec::H265,
|
||||||
|
VideoCodec::H265 => VideoCodec::H264,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn close(self) {
|
||||||
|
self.host.close().await.unwrap();
|
||||||
|
self.remote.close().await.unwrap();
|
||||||
|
self.router.lock().await.stop().await.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn exercise_locked_video(codec: VideoCodec) {
|
||||||
|
tokio::time::timeout(Duration::from_secs(35), async {
|
||||||
|
let mut peers = VirtualPeers::new(codec).await;
|
||||||
|
let (lock_tx, lock_rx) = watch::channel(Some(true));
|
||||||
|
let desktop = Arc::new(PortalDesktop::with_lock_state(
|
||||||
|
Arc::new(ScreenCapture::unavailable("synthetic pending desktop".into())),
|
||||||
|
lock_rx,
|
||||||
|
));
|
||||||
|
assert!(desktop.snapshot().waiting);
|
||||||
|
assert!(!desktop.snapshot().ready());
|
||||||
|
|
||||||
|
let run_host = peers.host.clone();
|
||||||
|
let run_track = peers.track.clone();
|
||||||
|
let run_desktop = desktop.clone();
|
||||||
|
let task = tokio::spawn(async move { run(&run_host, &run_track, &run_desktop).await });
|
||||||
|
|
||||||
|
assert_ne!(peers.host.connection_state(), RTCPeerConnectionState::Connected);
|
||||||
|
assert_no_rtp(&mut peers.packets, Duration::from_millis(150)).await;
|
||||||
|
|
||||||
|
peers.connect(codec).await;
|
||||||
|
|
||||||
|
let locked_frame = receive_frame(&mut peers.packets, Duration::from_secs(5)).await;
|
||||||
|
assert_single_rtp_frame(&locked_frame);
|
||||||
|
let last_locked_packet = locked_frame.last().unwrap().clone();
|
||||||
|
|
||||||
|
lock_tx.send_replace(None);
|
||||||
|
assert_no_rtp(&mut peers.packets, Duration::from_millis(1200)).await;
|
||||||
|
lock_tx.send_replace(Some(false));
|
||||||
|
assert_no_rtp(&mut peers.packets, Duration::from_millis(1200)).await;
|
||||||
|
|
||||||
|
desktop.publish_test_screen(ready_screen());
|
||||||
|
tokio::time::timeout(Duration::from_secs(2), task).await
|
||||||
|
.expect("locked video task did not stop when the desktop became ready")
|
||||||
|
.expect("locked video task panicked")
|
||||||
|
.expect("locked video task returned an error");
|
||||||
|
|
||||||
|
let live_data = black_frame(codec).await.expect("encode live test sample");
|
||||||
|
peers.track.write_sample_with_extensions(codec, &Sample {
|
||||||
|
data: live_data,
|
||||||
|
duration: Duration::from_millis(20),
|
||||||
|
..Default::default()
|
||||||
|
}, &[]).await.unwrap();
|
||||||
|
let live_frame = receive_frame(&mut peers.packets, Duration::from_secs(5)).await;
|
||||||
|
assert_single_rtp_frame(&live_frame);
|
||||||
|
let first_live_packet = &live_frame[0];
|
||||||
|
assert_eq!(first_live_packet.header.ssrc, last_locked_packet.header.ssrc);
|
||||||
|
assert_eq!(first_live_packet.header.sequence_number,
|
||||||
|
last_locked_packet.header.sequence_number.wrapping_add(1));
|
||||||
|
assert_eq!(first_live_packet.header.timestamp,
|
||||||
|
last_locked_packet.header.timestamp.wrapping_add(90_000));
|
||||||
|
|
||||||
|
peers.close().await;
|
||||||
|
}).await.expect("locked video WebRTC test exceeded its deadline");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "requires FFmpeg libx264/libx265"]
|
||||||
|
async fn locked_video_gates_on_pending_locked_connected_and_preserves_video_timeline() {
|
||||||
|
exercise_locked_video(VideoCodec::H264).await;
|
||||||
|
exercise_locked_video(VideoCodec::H265).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn locked_video_rechecks_state_when_encoding_finishes() {
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
enum Transition { Unlocked, Unknown, Ready }
|
||||||
|
tokio::time::timeout(Duration::from_secs(20), async {
|
||||||
|
let mut peers = VirtualPeers::new(VideoCodec::H264).await;
|
||||||
|
peers.connect(VideoCodec::H264).await;
|
||||||
|
for transition in [Transition::Unlocked, Transition::Unknown, Transition::Ready] {
|
||||||
|
let (lock_tx, lock_rx) = watch::channel(Some(true));
|
||||||
|
let desktop = Arc::new(PortalDesktop::with_lock_state(
|
||||||
|
Arc::new(ScreenCapture::unavailable("synthetic pending desktop".into())),
|
||||||
|
lock_rx,
|
||||||
|
));
|
||||||
|
let run_host = peers.host.clone();
|
||||||
|
let run_track = peers.track.clone();
|
||||||
|
let run_desktop = desktop.clone();
|
||||||
|
let (encoded_tx, mut encoded_rx) = mpsc::unbounded_channel();
|
||||||
|
let revision = peers.track.revision();
|
||||||
|
let task = tokio::spawn(async move {
|
||||||
|
run_with_encoder(&run_host, &run_track, &run_desktop, |_| {
|
||||||
|
let desktop = run_desktop.clone();
|
||||||
|
let lock_tx = lock_tx.clone();
|
||||||
|
let encoded_tx = encoded_tx.clone();
|
||||||
|
async move {
|
||||||
|
// Change state during the encoder's final poll, before it returns Ready.
|
||||||
|
match transition {
|
||||||
|
Transition::Unlocked => { lock_tx.send_replace(Some(false)); },
|
||||||
|
Transition::Unknown => { lock_tx.send_replace(None); },
|
||||||
|
Transition::Ready => desktop.publish_test_screen(ready_screen()),
|
||||||
|
}
|
||||||
|
encoded_tx.send(()).unwrap();
|
||||||
|
Ok(bytes::Bytes::from_static(&[0, 0, 1, 65, 1, 2]))
|
||||||
|
}
|
||||||
|
}).await
|
||||||
|
});
|
||||||
|
tokio::time::timeout(Duration::from_secs(2), encoded_rx.recv()).await
|
||||||
|
.expect("test encoder was not polled").expect("test encoder channel closed");
|
||||||
|
assert_no_rtp(&mut peers.packets, Duration::from_millis(250)).await;
|
||||||
|
assert_eq!(peers.track.revision(), revision, "stale state allowed a synthetic frame");
|
||||||
|
desktop.publish_test_screen(ready_screen());
|
||||||
|
tokio::time::timeout(Duration::from_secs(2), task).await
|
||||||
|
.expect("locked video task did not stop after readiness")
|
||||||
|
.expect("locked video task panicked")
|
||||||
|
.expect("locked video task returned an error");
|
||||||
|
}
|
||||||
|
peers.close().await;
|
||||||
|
}).await.expect("locked video concurrent-transition test exceeded its deadline");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn locked_video_stops_on_capture_rejection_and_terminal_peer_close() {
|
||||||
|
tokio::time::timeout(Duration::from_secs(25), async {
|
||||||
|
let mut peers = VirtualPeers::new(VideoCodec::H264).await;
|
||||||
|
peers.connect(VideoCodec::H264).await;
|
||||||
|
let (_lock_tx, lock_rx) = watch::channel(Some(false));
|
||||||
|
|
||||||
|
let rejected_desktop = Arc::new(PortalDesktop::with_lock_state(
|
||||||
|
Arc::new(ScreenCapture::unavailable("synthetic pending desktop".into())),
|
||||||
|
lock_rx.clone(),
|
||||||
|
));
|
||||||
|
let run_host = peers.host.clone();
|
||||||
|
let run_track = peers.track.clone();
|
||||||
|
let run_desktop = rejected_desktop.clone();
|
||||||
|
let rejection_task = tokio::spawn(async move { run(&run_host, &run_track, &run_desktop).await });
|
||||||
|
rejected_desktop.publish_test_screen(
|
||||||
|
Arc::new(ScreenCapture::unavailable("synthetic capture rejection".into())),
|
||||||
|
);
|
||||||
|
assert_eq!(rejected_desktop.snapshot().permission(true),
|
||||||
|
crate::system_metrics::Permission::Reject);
|
||||||
|
tokio::time::timeout(Duration::from_secs(2), rejection_task).await
|
||||||
|
.expect("locked video task did not stop after capture rejection")
|
||||||
|
.expect("locked video task panicked")
|
||||||
|
.expect("locked video task returned an error");
|
||||||
|
|
||||||
|
let pending_desktop = Arc::new(PortalDesktop::with_lock_state(
|
||||||
|
Arc::new(ScreenCapture::unavailable("synthetic pending desktop".into())),
|
||||||
|
lock_rx,
|
||||||
|
));
|
||||||
|
let run_host = peers.host.clone();
|
||||||
|
let run_track = peers.track.clone();
|
||||||
|
let run_desktop = pending_desktop;
|
||||||
|
let close_task = tokio::spawn(async move { run(&run_host, &run_track, &run_desktop).await });
|
||||||
|
peers.host.close().await.unwrap();
|
||||||
|
tokio::time::timeout(Duration::from_secs(2), close_task).await
|
||||||
|
.expect("locked video task did not stop after terminal peer close")
|
||||||
|
.expect("locked video task panicked")
|
||||||
|
.expect("locked video task returned an error");
|
||||||
|
|
||||||
|
peers.packets.close();
|
||||||
|
assert!(peers.packets.try_recv().is_err(), "locked video task sent RTP while unlocked");
|
||||||
|
peers.remote.close().await.unwrap();
|
||||||
|
peers.router.lock().await.stop().await.unwrap();
|
||||||
|
}).await.expect("locked video lifecycle test exceeded its deadline");
|
||||||
|
}
|
||||||
+26
-7
@@ -143,7 +143,7 @@ fn qr_rgba(content: &str, scale: usize, quiet: usize) -> Result<(Vec<u8>, usize)
|
|||||||
Ok((rgba, edge))
|
Ok((rgba, edge))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_config(cfg: &Value) -> Result<std::path::PathBuf, String> {
|
pub(crate) fn write_config(cfg: &Value) -> Result<std::path::PathBuf, String> {
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
|
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
|
||||||
let path = crate::config::path();
|
let path = crate::config::path();
|
||||||
@@ -227,8 +227,8 @@ pub async fn run(gui: bool) -> Result<(), String> {
|
|||||||
result.map(|_| ())
|
result.map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the account nickname (or user id) on success.
|
/// Local installation identifiers only; guest creation does not require device/init.
|
||||||
async fn flow(ui: &LoginUi) -> Result<String, String> {
|
pub fn prepare_identity() -> Result<(Value, Credentials), String> {
|
||||||
let mut cfg = crate::config::file()?.cloned().unwrap_or_else(|| json!({}));
|
let mut cfg = crate::config::file()?.cloned().unwrap_or_else(|| json!({}));
|
||||||
let get = |cfg: &Value, k: &str| std::env::var(format!("UU_{}", k.to_uppercase())).ok().filter(|v| !v.is_empty())
|
let get = |cfg: &Value, k: &str| std::env::var(format!("UU_{}", k.to_uppercase())).ok().filter(|v| !v.is_empty())
|
||||||
.or_else(|| cfg.get(k).and_then(|v| v.as_str()).map(str::to_owned)).unwrap_or_default();
|
.or_else(|| cfg.get(k).and_then(|v| v.as_str()).map(str::to_owned)).unwrap_or_default();
|
||||||
@@ -237,8 +237,16 @@ async fn flow(ui: &LoginUi) -> Result<String, String> {
|
|||||||
for (key, make) in [("client_id", random_uuid as fn() -> String), ("system_id", || random_uuid().to_uppercase()), ("machine_guid", machine_guid)] {
|
for (key, make) in [("client_id", random_uuid as fn() -> String), ("system_id", || random_uuid().to_uppercase()), ("machine_guid", machine_guid)] {
|
||||||
if get(&cfg, key).is_empty() { cfg[key] = json!(make()); }
|
if get(&cfg, key).is_empty() { cfg[key] = json!(make()); }
|
||||||
}
|
}
|
||||||
let mut creds = Credentials { client_id: get(&cfg, "client_id"), system_id: get(&cfg, "system_id"), sign_key, ..Default::default() };
|
let creds = Credentials { client_id: get(&cfg, "client_id"), system_id: get(&cfg, "system_id"), device_id: get(&cfg, "device_id"), sign_key, ..Default::default() };
|
||||||
|
|
||||||
|
write_config(&cfg)?;
|
||||||
|
Ok((cfg, creds))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn prepare_device() -> Result<(Value, Credentials), String> {
|
||||||
|
let (mut cfg, mut creds) = prepare_identity()?;
|
||||||
|
let get = |cfg: &Value, k: &str| std::env::var(format!("UU_{}", k.to_uppercase())).ok().filter(|v| !v.is_empty())
|
||||||
|
.or_else(|| cfg.get(k).and_then(|v| v.as_str()).map(str::to_owned)).unwrap_or_default();
|
||||||
if get(&cfg, "device_id").is_empty() {
|
if get(&cfg, "device_id").is_empty() {
|
||||||
let body = json!({
|
let body = json!({
|
||||||
"name": hostname(), "client_id": creds.client_id, "system_id": creds.system_id, "machine_guid": get(&cfg, "machine_guid"),
|
"name": hostname(), "client_id": creds.client_id, "system_id": creds.system_id, "machine_guid": get(&cfg, "machine_guid"),
|
||||||
@@ -248,11 +256,17 @@ async fn flow(ui: &LoginUi) -> Result<String, String> {
|
|||||||
let (status, resp) = UUAuthClient::new(creds.clone()).post_json("/api/v1/device/windows/init", &body).await?;
|
let (status, resp) = UUAuthClient::new(creds.clone()).post_json("/api/v1/device/windows/init", &body).await?;
|
||||||
let d = data("device init", status, &resp)?;
|
let d = data("device init", status, &resp)?;
|
||||||
let id = d.get("device_id").and_then(|v| v.as_str()).ok_or("device init returned no device_id")?;
|
let id = d.get("device_id").and_then(|v| v.as_str()).ok_or("device init returned no device_id")?;
|
||||||
println!("Registered device: name={} id={}", d.get("alias").and_then(|v| v.as_str()).unwrap_or(""), id);
|
log::info!("Registered local device identity");
|
||||||
cfg["device_id"] = json!(id);
|
cfg["device_id"] = json!(id);
|
||||||
write_config(&cfg)?; // keep the device even if the QR step is abandoned
|
write_config(&cfg)?; // keep the device even if the QR step is abandoned
|
||||||
}
|
}
|
||||||
creds.device_id = get(&cfg, "device_id");
|
creds.device_id = get(&cfg, "device_id");
|
||||||
|
Ok((cfg, creds))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the account nickname (or user id) on success.
|
||||||
|
pub(crate) async fn flow(ui: &LoginUi) -> Result<String, String> {
|
||||||
|
let (mut cfg, creds) = prepare_device().await?;
|
||||||
let client = UUAuthClient::new(creds.clone());
|
let client = UUAuthClient::new(creds.clone());
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@@ -274,7 +288,7 @@ async fn flow(ui: &LoginUi) -> Result<String, String> {
|
|||||||
let mut file=std::fs::OpenOptions::new().write(true).create(true).truncate(true).mode(0o600).open(path).map_err(|e|e.to_string())?;
|
let mut file=std::fs::OpenOptions::new().write(true).create(true).truncate(true).mode(0o600).open(path).map_err(|e|e.to_string())?;
|
||||||
file.write_all(&crate::cursor::encode_png(edge as u32,edge as u32,&rgba)).map_err(|e|e.to_string())?;
|
file.write_all(&crate::cursor::encode_png(edge as u32,edge as u32,&rgba)).map_err(|e|e.to_string())?;
|
||||||
}
|
}
|
||||||
println!("\nScan with the UU Remote mobile app (网易UU远程 App 扫码登录):\n{}", render_qr(&url)?);
|
if !ui.quiet() { println!("\nScan with the UU Remote mobile app (网易UU远程 App 扫码登录):\n{}", render_qr(&url)?); }
|
||||||
// Status is a long poll: send the state already shown (server enum 1..4; observed:
|
// Status is a long poll: send the state already shown (server enum 1..4; observed:
|
||||||
// 1 waiting, 2 scanned, 4 after the phone confirmed); the server answers when it changes.
|
// 1 waiting, 2 scanned, 4 after the phone confirmed); the server answers when it changes.
|
||||||
let mut login_status = 1i64;
|
let mut login_status = 1i64;
|
||||||
@@ -315,8 +329,13 @@ async fn flow(ui: &LoginUi) -> Result<String, String> {
|
|||||||
cfg["token"] = json!(token);
|
cfg["token"] = json!(token);
|
||||||
cfg["user_id"] = json!(user);
|
cfg["user_id"] = json!(user);
|
||||||
cfg["csrf"] = json!(csrf_from_token(token).ok_or("token carries no csrf claim")?);
|
cfg["csrf"] = json!(csrf_from_token(token).ok_or("token carries no csrf claim")?);
|
||||||
let path = write_config(&cfg)?;
|
|
||||||
let nickname = d.get("nickname").and_then(|v| v.as_str()).unwrap_or("");
|
let nickname = d.get("nickname").and_then(|v| v.as_str()).unwrap_or("");
|
||||||
|
if !nickname.is_empty() { cfg["account_name"] = json!(nickname); }
|
||||||
|
// Keep settings saved meanwhile (e.g. from the assist card): merge into the fresh file.
|
||||||
|
if let Ok(Some(Value::Object(fresh))) = crate::config::read_fresh() {
|
||||||
|
for (k, v) in fresh { if cfg.get(&k).is_none() { cfg[k] = v; } }
|
||||||
|
}
|
||||||
|
let path = write_config(&cfg)?;
|
||||||
println!("Logged in as {} ({}); saved to {}", nickname, user, path.display());
|
println!("Logged in as {} ({}); saved to {}", nickname, user, path.display());
|
||||||
return Ok(if nickname.is_empty() { user.to_string() } else { nickname.to_string() });
|
return Ok(if nickname.is_empty() { user.to_string() } else { nickname.to_string() });
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-4
@@ -15,13 +15,13 @@ extern "C" {
|
|||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub enum Tone { Info = 0, Ok = 1, Error = 2 }
|
pub enum Tone { Info = 0, Ok = 1, Error = 2 }
|
||||||
|
|
||||||
pub struct LoginUi { shown: bool }
|
pub struct LoginUi { shown: bool, attached: bool }
|
||||||
|
|
||||||
#[cfg(uu_gtk)]
|
#[cfg(uu_gtk)]
|
||||||
fn cstr(s: &str) -> std::ffi::CString { std::ffi::CString::new(s.replace('\0', "")).unwrap_or_default() }
|
fn cstr(s: &str) -> std::ffi::CString { std::ffi::CString::new(s.replace('\0', "")).unwrap_or_default() }
|
||||||
|
|
||||||
impl LoginUi {
|
impl LoginUi {
|
||||||
pub fn none() -> Self { LoginUi { shown: false } }
|
pub fn none() -> Self { LoginUi { shown: false, attached: false } }
|
||||||
|
|
||||||
/// Opens the window when built with GTK and a display is present; falls back silently otherwise.
|
/// Opens the window when built with GTK and a display is present; falls back silently otherwise.
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
@@ -30,13 +30,23 @@ impl LoginUi {
|
|||||||
if std::env::var_os("DISPLAY").is_some() || std::env::var_os("WAYLAND_DISPLAY").is_some() {
|
if std::env::var_os("DISPLAY").is_some() || std::env::var_os("WAYLAND_DISPLAY").is_some() {
|
||||||
let (n, o, c, g) = (cstr(name), cstr(os), cstr(cpu), cstr(gpu));
|
let (n, o, c, g) = (cstr(name), cstr(os), cstr(cpu), cstr(gpu));
|
||||||
if unsafe { uu_login_ui_start(n.as_ptr(), o.as_ptr(), c.as_ptr(), g.as_ptr()) } == 0 {
|
if unsafe { uu_login_ui_start(n.as_ptr(), o.as_ptr(), c.as_ptr(), g.as_ptr()) } == 0 {
|
||||||
return LoginUi { shown: true };
|
return LoginUi { shown: true, attached: false };
|
||||||
}
|
}
|
||||||
eprintln!("Login window unavailable; using the terminal QR code");
|
eprintln!("Login window unavailable; using the terminal QR code");
|
||||||
}
|
}
|
||||||
LoginUi::none()
|
LoginUi::none()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Account card of the host's own main window (already open): the QR stays in the window only
|
||||||
|
/// (a service's stdout is the journal and the QR URL is a login ticket), and dropping this
|
||||||
|
/// handle never closes the shared window.
|
||||||
|
pub fn attached() -> Self {
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
{ unsafe { uu_login_ui_start(std::ptr::null(), std::ptr::null(), std::ptr::null(), std::ptr::null()) }; LoginUi { shown: true, attached: true } }
|
||||||
|
#[cfg(not(uu_gtk))]
|
||||||
|
LoginUi { shown: false, attached: true }
|
||||||
|
}
|
||||||
|
pub fn quiet(&self) -> bool { self.attached }
|
||||||
pub fn shown(&self) -> bool { self.shown }
|
pub fn shown(&self) -> bool { self.shown }
|
||||||
|
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
@@ -73,6 +83,6 @@ impl LoginUi {
|
|||||||
impl Drop for LoginUi {
|
impl Drop for LoginUi {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
#[cfg(uu_gtk)]
|
#[cfg(uu_gtk)]
|
||||||
if self.shown { unsafe { uu_login_ui_close() } }
|
if self.shown && !self.attached { unsafe { uu_login_ui_close() } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+378
-78
@@ -2,11 +2,14 @@
|
|||||||
//! Zero-Wine, Zero-Python, Single-Binary, 5 Native Tools Integrated
|
//! Zero-Wine, Zero-Python, Single-Binary, 5 Native Tools Integrated
|
||||||
|
|
||||||
mod desktop_backend;
|
mod desktop_backend;
|
||||||
|
mod desktop_session;
|
||||||
|
mod desktop_power;
|
||||||
mod portal;
|
mod portal;
|
||||||
mod network_diagnostics;
|
mod network_diagnostics;
|
||||||
mod session_ice;
|
mod session_ice;
|
||||||
mod wayland;
|
mod wayland;
|
||||||
mod auth;
|
mod auth;
|
||||||
|
mod assist;
|
||||||
mod config;
|
mod config;
|
||||||
mod login;
|
mod login;
|
||||||
mod login_ui;
|
mod login_ui;
|
||||||
@@ -23,6 +26,11 @@ mod signaling;
|
|||||||
mod terminal;
|
mod terminal;
|
||||||
mod webrtc;
|
mod webrtc;
|
||||||
mod video;
|
mod video;
|
||||||
|
mod video_track;
|
||||||
|
mod locked_video;
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test_network;
|
||||||
|
mod hevc;
|
||||||
#[cfg(feature = "inproc-av")]
|
#[cfg(feature = "inproc-av")]
|
||||||
mod damage_capture;
|
mod damage_capture;
|
||||||
#[cfg(feature = "inproc-av")]
|
#[cfg(feature = "inproc-av")]
|
||||||
@@ -53,18 +61,149 @@ use std::sync::Arc;
|
|||||||
use tokio::sync::{mpsc, Mutex};
|
use tokio::sync::{mpsc, Mutex};
|
||||||
use webrtc::WebRtcSession;
|
use webrtc::WebRtcSession;
|
||||||
|
|
||||||
|
/// Set before a normal return when the host must come back in the other mode (signed in / out).
|
||||||
|
static RESTART: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
|
||||||
|
|
||||||
|
fn superseded_peer_ids(sessions: &HashMap<String, WebRtcSession>, ice: &str, desktop: bool) -> Vec<String> {
|
||||||
|
sessions.iter().filter(|(id, peer)| id.as_str() == ice || (desktop && peer.desktop))
|
||||||
|
.map(|(id, _)| id.clone()).collect()
|
||||||
|
}
|
||||||
|
fn released_peer_ids<T>(sessions: &HashMap<String, T>, ice: Option<&serde_json::Value>) -> Vec<String> {
|
||||||
|
let ice = ice.map(|id| id.as_str().map(str::to_owned).unwrap_or_else(|| id.to_string()));
|
||||||
|
sessions.keys().filter(|id| ice.as_deref().map_or(true, |ice| id.as_str() == ice)).cloned().collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod peer_release_tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn explicit_releases_leave_independent_and_replacement_peers_alive() {
|
||||||
|
let mut peers = HashMap::from([
|
||||||
|
("old-desktop".to_owned(), ()), ("files".to_owned(), ()),
|
||||||
|
("terminal".to_owned(), ()), ("port-mapping".to_owned(), ()),
|
||||||
|
]);
|
||||||
|
let targets = released_peer_ids(&peers, Some(&serde_json::json!("old-desktop")));
|
||||||
|
assert_eq!(targets, ["old-desktop"]);
|
||||||
|
for id in targets { peers.remove(&id); }
|
||||||
|
peers.insert("new-desktop".to_owned(), ());
|
||||||
|
assert!(released_peer_ids(&peers, Some(&serde_json::json!("old-desktop"))).is_empty());
|
||||||
|
assert!(released_peer_ids(&peers, Some(&serde_json::json!("unknown"))).is_empty());
|
||||||
|
for id in ["files", "terminal", "port-mapping", "new-desktop"] {
|
||||||
|
assert_eq!(released_peer_ids(&peers, Some(&serde_json::json!(id))), [id]);
|
||||||
|
}
|
||||||
|
assert_eq!(peers.len(), 4);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn release_without_an_ice_id_retains_room_wide_cleanup() {
|
||||||
|
let peers = HashMap::from([("desktop".to_owned(), ()), ("files".to_owned(), ())]);
|
||||||
|
let mut targets = released_peer_ids(&peers, None);
|
||||||
|
targets.sort();
|
||||||
|
assert_eq!(targets, ["desktop", "files"]);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn explicit_non_string_ice_ids_never_become_room_wide_releases() {
|
||||||
|
let peers = HashMap::from([("123".to_owned(), ()), ("files".to_owned(), ())]);
|
||||||
|
for id in [serde_json::json!(999), serde_json::Value::Null, serde_json::json!(false), serde_json::json!({})] {
|
||||||
|
assert!(released_peer_ids(&peers, Some(&id)).is_empty());
|
||||||
|
}
|
||||||
|
assert_eq!(released_peer_ids(&peers, Some(&serde_json::json!(123))), ["123"]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Re-executes this host; the new instance opens its window so the user sees the result.
|
||||||
|
fn restart_host() -> ! {
|
||||||
|
use std::os::unix::process::CommandExt;
|
||||||
|
let error = std::process::Command::new("/proc/self/exe").args(std::env::args_os().skip(1)).env("UU_SHOW_WINDOW", "1").exec();
|
||||||
|
eprintln!("restart failed: {error}");
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
/// systemd user unit running this process (from /proc/self/cgroup), e.g. "uuremote-native.service".
|
||||||
|
fn systemd_unit() -> Option<String> {
|
||||||
|
std::env::var_os("INVOCATION_ID")?;
|
||||||
|
let cgroup = std::fs::read_to_string("/proc/self/cgroup").ok()?;
|
||||||
|
cgroup.lines().find_map(|l| l.rsplit('/').next().filter(|u| u.ends_with(".service")).map(str::to_owned))
|
||||||
|
}
|
||||||
|
fn display_present() -> bool { std::env::var_os("DISPLAY").is_some() || std::env::var_os("WAYLAND_DISPLAY").is_some() }
|
||||||
|
/// SIGUSR1 (`uuremote-server show`) opens the host window.
|
||||||
|
fn present_on_usr1(ui: std::sync::Arc<assist::LocalUi>) {
|
||||||
|
// Install the handler before publishing the pid: SIGUSR1's default action terminates the process.
|
||||||
|
let Ok(mut usr1) = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::user_defined1()) else { return };
|
||||||
|
write_host_pid();
|
||||||
|
tokio::spawn(async move { while usr1.recv().await.is_some() { ui.present(); } });
|
||||||
|
}
|
||||||
|
|
||||||
|
fn host_pid_path() -> std::path::PathBuf { config::path().with_file_name("host.pid") }
|
||||||
|
/// Lets `uuremote-server show` find this host service to open its window (SIGUSR1).
|
||||||
|
fn write_host_pid() {
|
||||||
|
use std::os::unix::fs::OpenOptionsExt;
|
||||||
|
use std::io::Write;
|
||||||
|
if let Ok(mut f) = std::fs::OpenOptions::new().write(true).create(true).truncate(true).mode(0o600).open(host_pid_path()) {
|
||||||
|
let _ = write!(f, "{}", std::process::id());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// `uuremote-server show` (desktop launcher): open the running host's window, or start the host in the
|
||||||
|
/// foreground with its window when none runs (e.g. no systemd unit installed yet).
|
||||||
|
fn show_running_host() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let running = std::fs::read_to_string(host_pid_path()).ok().and_then(|s| s.trim().parse::<i32>().ok()).filter(|p| *p > 1)
|
||||||
|
.filter(|pid| {
|
||||||
|
let exe = std::fs::read_link(format!("/proc/{pid}/exe")).ok();
|
||||||
|
let own = std::env::current_exe().ok();
|
||||||
|
exe.is_some() && exe.as_ref().and_then(|p| p.file_name()) == own.as_ref().and_then(|p| p.file_name())
|
||||||
|
});
|
||||||
|
if let Some(pid) = running {
|
||||||
|
if unsafe { libc::kill(pid, libc::SIGUSR1) } == 0 { return Ok(()); }
|
||||||
|
}
|
||||||
|
// Stopped from the tray but installed as a user service: start the service (not a second,
|
||||||
|
// unmanaged host), wait for it to publish its pid, then ask it to show the window.
|
||||||
|
let unit = std::env::var("UU_SERVICE_UNIT").unwrap_or_else(|_| "uuremote-native.service".into());
|
||||||
|
let installed = std::process::Command::new("systemctl").args(["--user", "cat", &unit])
|
||||||
|
.stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null()).status().is_ok_and(|s| s.success());
|
||||||
|
if installed && std::process::Command::new("systemctl").args(["--user", "start", &unit]).status().is_ok_and(|s| s.success()) {
|
||||||
|
let before = std::fs::metadata(host_pid_path()).and_then(|m| m.modified()).ok();
|
||||||
|
for _ in 0..60 {
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(500));
|
||||||
|
let fresh = std::fs::metadata(host_pid_path()).and_then(|m| m.modified()).ok().is_some_and(|t| Some(t) != before);
|
||||||
|
let pid = std::fs::read_to_string(host_pid_path()).ok().and_then(|s| s.trim().parse::<i32>().ok());
|
||||||
|
if let (true, Some(pid)) = (fresh, pid) {
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(500)); // signal handler installed right after the pid
|
||||||
|
if unsafe { libc::kill(pid, libc::SIGUSR1) } == 0 { return Ok(()); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Err("the UU remote host service started but did not open its window".into());
|
||||||
|
}
|
||||||
|
use std::os::unix::process::CommandExt;
|
||||||
|
Err(Box::new(std::process::Command::new("/proc/self/exe").env("UU_SHOW_WINDOW", "1").exec()))
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
|
// Not signed in, with a desktop: one window offers remote assistance (guest identity) and the QR
|
||||||
|
// login together; signing in restarts the host in account mode (assistance continues there).
|
||||||
|
let unified = std::env::args().nth(1).is_none() && display_present()
|
||||||
|
&& std::env::var("UU_REMOTE_ASSIST").ok().as_deref() != Some("0") && std::env::var("UU_LOGIN_WINDOW").ok().as_deref() != Some("0")
|
||||||
|
&& config::read_fresh().ok().flatten().and_then(|c| c.get("token").and_then(|t| t.as_str()).map(|t| !t.is_empty())) != Some(true)
|
||||||
|
&& ["UU_USER_ID", "UU_TOKEN", "UU_CSRF"].iter().all(|k| std::env::var_os(k).map_or(true, |v| v.is_empty()));
|
||||||
|
let assist_enabled = unified || std::env::args().nth(1).as_deref() == Some("assist")
|
||||||
|
|| (std::env::args().nth(1).is_none() && std::env::var("UU_REMOTE_ASSIST").ok().as_deref() == Some("1"));
|
||||||
|
if assist_enabled {
|
||||||
|
// HTTP/WS dependency trace logging can expose Authorization or room headers.
|
||||||
|
env_logger::Builder::new().filter_level(log::LevelFilter::Off)
|
||||||
|
.filter_module("uuremote_server", log::LevelFilter::Info).init();
|
||||||
|
} else { env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init(); }
|
||||||
// An unknown argument must never fall through to starting a second full host server.
|
// An unknown argument must never fall through to starting a second full host server.
|
||||||
const SUBCOMMANDS: [&str; 11] = ["desktop-input-probe", "virtual-audio-probe", "login", "unlock-check", "desktop-probe", "desktop-video-probe", "desktop-audio-probe", "wayland-capture",
|
const SUBCOMMANDS: [&str; 13] = ["show", "assist", "desktop-input-probe", "virtual-audio-probe", "login", "unlock-check", "desktop-probe", "desktop-video-probe", "desktop-audio-probe", "wayland-capture",
|
||||||
"wayland-input-probe", "clipboard-acceptance", "portal-acceptance"];
|
"wayland-input-probe", "clipboard-acceptance", "portal-acceptance"];
|
||||||
if let Some(arg) = std::env::args().nth(1).filter(|a| !SUBCOMMANDS.contains(&a.as_str())) {
|
if let Some(arg) = std::env::args().nth(1).filter(|a| !SUBCOMMANDS.contains(&a.as_str())) {
|
||||||
let usage = format!("usage: uuremote-server [{}]\n (no argument: run the host)", SUBCOMMANDS.join(" | "));
|
let usage = format!("usage: uuremote-server [{}]\n (no argument: run the host)", SUBCOMMANDS.join(" | "));
|
||||||
if matches!(arg.as_str(), "help" | "-h" | "--help") { println!("{usage}"); return Ok(()); }
|
if matches!(arg.as_str(), "help" | "-h" | "--help") { println!("{usage}"); return Ok(()); }
|
||||||
eprintln!("unknown argument: {arg}\n{usage}");
|
eprintln!("unknown argument\n{usage}");
|
||||||
std::process::exit(2);
|
std::process::exit(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if std::env::args().nth(1).as_deref() == Some("show") { return show_running_host(); }
|
||||||
|
if assist_enabled && !unified {
|
||||||
|
if std::env::args().len() > 2 { return Err("assist accepts no additional arguments; configure it with a private UU_CONFIG".into()); }
|
||||||
|
assist::configure()?;
|
||||||
|
}
|
||||||
if std::env::args().nth(1).as_deref() == Some("clipboard-acceptance") {
|
if std::env::args().nth(1).as_deref() == Some("clipboard-acceptance") {
|
||||||
return tokio::task::spawn_blocking(clipboard_probe::run).await?.map_err(Into::into);
|
return tokio::task::spawn_blocking(clipboard_probe::run).await?.map_err(Into::into);
|
||||||
}
|
}
|
||||||
@@ -144,6 +283,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let mut frame = vec![0; screen.width as usize * screen.height as usize * 4];
|
let mut frame = vec![0; screen.width as usize * screen.height as usize * 4];
|
||||||
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
|
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
|
||||||
loop {
|
loop {
|
||||||
|
if *locked.borrow() == Some(false) {
|
||||||
|
session.recover(&std::sync::atomic::AtomicBool::new(false)).map_err(|error| error.to_string())?;
|
||||||
|
}
|
||||||
session.capture(&mut frame)?;
|
session.capture(&mut frame)?;
|
||||||
if *locked.borrow() == Some(false) && session.is_alive() { break; }
|
if *locked.borrow() == Some(false) && session.is_alive() { break; }
|
||||||
if std::time::Instant::now() >= deadline { return Err("Test VM portal did not recover".into()); }
|
if std::time::Instant::now() >= deadline { return Err("Test VM portal did not recover".into()); }
|
||||||
@@ -230,6 +372,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
if std::env::args().nth(1).as_deref() == Some("desktop-video-probe") {
|
if std::env::args().nth(1).as_deref() == Some("desktop-video-probe") {
|
||||||
|
if let Some(input)=std::env::var_os("UU_PROBE_INPUT") {
|
||||||
|
return video::probe_input(input,std::env::args().nth(2)).await.map_err(|e|e.into());
|
||||||
|
}
|
||||||
let screen=ScreenCapture::new()?;
|
let screen=ScreenCapture::new()?;
|
||||||
capture::keep_display_awake()?;
|
capture::keep_display_awake()?;
|
||||||
return video::probe((screen.width,screen.height),screen.display.clone(),screen.portal.clone(),std::env::args().nth(2)).await.map_err(|e|e.into());
|
return video::probe((screen.width,screen.height),screen.display.clone(),screen.portal.clone(),std::env::args().nth(2)).await.map_err(|e|e.into());
|
||||||
@@ -261,11 +406,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let _audio_cleanup=virtual_audio::Cleanup;
|
let _audio_cleanup=virtual_audio::Cleanup;
|
||||||
log::info!("Starting uu-remote-linux host v{}...", env!("CARGO_PKG_VERSION"));
|
log::info!("Starting uu-remote-linux host v{}...", env!("CARGO_PKG_VERSION"));
|
||||||
|
|
||||||
let mut capture = Arc::new((if desktop_backend::headless() {
|
let capture = Arc::new((if desktop_backend::headless() {
|
||||||
Err("headless mode (UU_DESKTOP_BACKEND=none)".to_owned())
|
Err("headless mode (UU_DESKTOP_BACKEND=none)".to_owned())
|
||||||
} else if desktop_backend::detect()?==desktop_backend::DesktopBackend::Portal
|
} else if desktop_backend::detect()?==desktop_backend::DesktopBackend::Portal {
|
||||||
&& std::env::var("UU_PORTAL_PREAUTHORIZE").ok().as_deref()!=Some("1") {
|
Err("Portal capture waits for a desktop CONTROL connection".to_owned())
|
||||||
Err("Portal screen access requires a desktop connection and local consent".to_owned())
|
|
||||||
} else { ScreenCapture::new() }).unwrap_or_else(|error| {
|
} else { ScreenCapture::new() }).unwrap_or_else(|error| {
|
||||||
log::warn!("Desktop unavailable; terminal and file tools remain available: {error}");
|
log::warn!("Desktop unavailable; terminal and file tools remain available: {error}");
|
||||||
ScreenCapture::unavailable(error)
|
ScreenCapture::unavailable(error)
|
||||||
@@ -274,34 +418,124 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
input.bind_portal(capture.portal.clone());
|
input.bind_portal(capture.portal.clone());
|
||||||
input.release_held(); // recover stale input before accepting any peer
|
input.release_held(); // recover stale input before accepting any peer
|
||||||
let _pointer_worker = input.start_pointer_worker();
|
let _pointer_worker = input.start_pointer_worker();
|
||||||
#[cfg(target_os="linux")]
|
let (shutdown_tx, mut shutdown) = tokio::sync::watch::channel(false);
|
||||||
{
|
let shutdown_input = input.clone();
|
||||||
let shutdown_input=input.clone();
|
let signal_tx = shutdown_tx.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
#[cfg(target_os="linux")]
|
||||||
|
{
|
||||||
|
let mut term = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()).expect("SIGTERM handler");
|
||||||
|
tokio::select! { _ = term.recv() => {}, _ = tokio::signal::ctrl_c() => {} }
|
||||||
|
}
|
||||||
|
#[cfg(not(target_os="linux"))]
|
||||||
|
let _ = tokio::signal::ctrl_c().await;
|
||||||
|
if !assist_enabled { shutdown_input.shutdown(); virtual_audio::disable(); terminal::shutdown_all(); std::process::exit(0); }
|
||||||
|
let _ = signal_tx.send(true);
|
||||||
|
});
|
||||||
|
let mut assist = if assist_enabled { Some(assist::Assist::start(&mut shutdown, unified).await?) } else { None };
|
||||||
|
if let Some(session) = assist.as_ref().filter(|_| unified) {
|
||||||
|
present_on_usr1(session.ui());
|
||||||
|
let login_done = shutdown_tx.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
if let Ok(mut term)=tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
|
let ui = login_ui::LoginUi::attached();
|
||||||
tokio::select! { _=term.recv()=>{}, _=tokio::signal::ctrl_c()=>{} }
|
loop {
|
||||||
shutdown_input.shutdown(); virtual_audio::disable(); terminal::shutdown_all(); std::process::exit(0);
|
match login::flow(&ui).await {
|
||||||
|
Ok(_) => { RESTART.store(true, std::sync::atomic::Ordering::SeqCst); let _ = login_done.send(true); return; }
|
||||||
|
Err(error) => { log::warn!("QR login failed: {error}; retrying"); tokio::time::sleep(std::time::Duration::from_secs(5)).await; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
let creds = match config::file().and_then(Credentials::load) {
|
let webrtc_sessions: Arc<Mutex<HashMap<String, WebRtcSession>>> = Arc::new(Mutex::new(HashMap::new()));
|
||||||
Ok(creds) => creds,
|
let (mut commands, command_task) = match assist.as_ref() {
|
||||||
Err(e) => {
|
Some(s) => { let (rx, task) = s.commands(shutdown_tx); (rx, Some(task)) },
|
||||||
log::warn!("Not logged in: {e} ({} or UU_* environment variables)", config::path().display());
|
None => (tokio::sync::mpsc::channel(1).1, None),
|
||||||
login::wait_for_credentials(None).await
|
};
|
||||||
|
let mut immediate_shutdown = shutdown.clone();
|
||||||
|
let result = tokio::select! {
|
||||||
|
result = serve(capture, input.clone(), &mut shutdown, &mut assist, &mut commands, webrtc_sessions.clone()) => result,
|
||||||
|
_ = immediate_shutdown.changed() => Ok(()),
|
||||||
|
};
|
||||||
|
if let Some(task) = command_task { task.abort(); }
|
||||||
|
// One exit path for commands, signals, transport failures and UI failures.
|
||||||
|
if let Some(session) = assist.as_mut() { session.allowed = false; }
|
||||||
|
for (_, old) in webrtc_sessions.lock().await.drain() { let _ = old.pc.close().await; }
|
||||||
|
virtual_audio::disable(); input.shutdown(); terminal::shutdown_all();
|
||||||
|
// Switching identity (signed in / out) restarts at once and the new identity republishes the same
|
||||||
|
// device share: logout already withdrew it, and after a QR login the cloud moved it to the account
|
||||||
|
// (a guest cancel then fails). Only a real stop withdraws here.
|
||||||
|
let switching = RESTART.load(std::sync::atomic::Ordering::SeqCst);
|
||||||
|
let revoke = match assist.as_mut() { Some(s) if !switching => s.cancel().await, _ => Ok(()) };
|
||||||
|
if revoke.is_err() { log::error!("Cloud assistance revocation failed; host is closed locally"); }
|
||||||
|
result?;
|
||||||
|
if RESTART.load(std::sync::atomic::Ordering::SeqCst) { restart_host(); }
|
||||||
|
revoke.map_err(|_| "assist disabled locally, but cloud cancel failed; cloud revocation is unverified")?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutdown: &mut tokio::sync::watch::Receiver<bool>,
|
||||||
|
assist: &mut Option<assist::Assist>, commands: &mut mpsc::Receiver<assist::Command>, webrtc_sessions: Arc<Mutex<HashMap<String, WebRtcSession>>>) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let portal_desktop = (capture.backend == desktop_backend::DesktopBackend::Portal && !desktop_backend::headless())
|
||||||
|
.then(|| Arc::new(desktop_session::PortalDesktop::new(capture.clone())));
|
||||||
|
let mut auth = if let Some(session) = assist.as_ref() { session.client.clone() } else {
|
||||||
|
let creds = match config::file().and_then(Credentials::load) {
|
||||||
|
Ok(creds) => creds,
|
||||||
|
Err(e) => {
|
||||||
|
log::warn!("Not logged in: {e} ({} or UU_* environment variables)", config::path().display());
|
||||||
|
login::wait_for_credentials(None).await
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let client = Arc::new(UUAuthClient::new(creds.clone()));
|
||||||
|
// Logged-in host: remote assistance (ID + code) alongside the device list, like the official
|
||||||
|
// client. Shown in a hidden window opened with `uuremote-server show`; UU_REMOTE_ASSIST=0 disables.
|
||||||
|
if std::env::var("UU_REMOTE_ASSIST").ok().as_deref() != Some("0") {
|
||||||
|
match assist::Assist::start_account(client.clone(), creds) {
|
||||||
|
Ok(session) => {
|
||||||
|
let (rx, _task) = session.commands(tokio::sync::watch::channel(false).0);
|
||||||
|
*commands = rx;
|
||||||
|
session.load_account_name().await;
|
||||||
|
present_on_usr1(session.ui());
|
||||||
|
if std::env::var("UU_SHOW_WINDOW").ok().as_deref() == Some("1") { session.ui().present(); }
|
||||||
|
std::env::remove_var("UU_SHOW_WINDOW");
|
||||||
|
log::info!("Remote assistance ready on the logged-in host (window: uuremote-server show)");
|
||||||
|
*assist = Some(session);
|
||||||
|
}
|
||||||
|
Err(error) => log::info!("Remote assistance unavailable on this host: {error}"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
client
|
||||||
};
|
};
|
||||||
let mut auth = Arc::new(UUAuthClient::new(creds));
|
|
||||||
|
|
||||||
// One peer per viewer connection (desktop, file transfer, terminal...), keyed by ice_id.
|
// One peer per viewer connection (desktop, file transfer, terminal...), keyed by ice_id.
|
||||||
let webrtc_sessions: Arc<Mutex<HashMap<String, WebRtcSession>>> = Arc::new(Mutex::new(HashMap::new()));
|
|
||||||
let mut next_capture_type: Option<(u64,crate::session_ice::SessionIce)> = None;
|
let mut next_capture_type: Option<(u64,crate::session_ice::SessionIce)> = None;
|
||||||
let mut capture_types: HashMap<String, (u64,crate::session_ice::SessionIce)> = HashMap::new();
|
let mut capture_types: HashMap<String, (u64,crate::session_ice::SessionIce)> = HashMap::new();
|
||||||
|
let mut mac_by_ice: HashMap<String, bool> = HashMap::new();
|
||||||
|
let mut next_mac = false;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
if *shutdown.borrow() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
log::info!("Requesting fresh room credentials from NetEase cloud...");
|
log::info!("Requesting fresh room credentials from NetEase cloud...");
|
||||||
let room_data = match auth.create_room().await {
|
let room_result = tokio::select! {
|
||||||
Ok(data) => data,
|
result = async {
|
||||||
|
match assist.as_mut() {
|
||||||
|
Some(session) if !session.is_account() => {
|
||||||
|
let result = session.room().await.map_err(auth::RoomError::Other);
|
||||||
|
auth = session.client.clone(); result
|
||||||
|
}
|
||||||
|
_ => auth.create_room().await,
|
||||||
|
}
|
||||||
|
} => result,
|
||||||
|
_ = shutdown.changed() => return Ok(()),
|
||||||
|
};
|
||||||
|
let room_data = match room_result {
|
||||||
|
Ok(data) => {
|
||||||
|
if let Some(session) = assist.as_mut().filter(|s| s.is_account()) {
|
||||||
|
if let Err(error) = session.ensure_published().await { log::warn!("Remote assistance share info failed: {error}"); }
|
||||||
|
}
|
||||||
|
data
|
||||||
|
}
|
||||||
Err(auth::RoomError::LoginExpired) => {
|
Err(auth::RoomError::LoginExpired) => {
|
||||||
// Like the official client: an expired token needs a new QR login, not retries.
|
// Like the official client: an expired token needs a new QR login, not retries.
|
||||||
log::error!("UU login is invalid or expired; a new QR login is required");
|
log::error!("UU login is invalid or expired; a new QR login is required");
|
||||||
@@ -316,20 +550,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
log::info!(
|
log::info!("Acquired room signaling credentials");
|
||||||
"Acquired room signaling target: {}",
|
|
||||||
room_data.signaling_server
|
|
||||||
);
|
|
||||||
|
|
||||||
let (tx_event, mut rx_event) = mpsc::channel::<SignalingEvent>(64);
|
let (tx_event, mut rx_event) = mpsc::channel::<SignalingEvent>(64);
|
||||||
let tx_timer = tx_event.clone();
|
let tx_timer = tx_event.clone();
|
||||||
let signaling_client = match SignalingClient::connect(
|
let connected = tokio::select! {
|
||||||
&room_data.signaling_server,
|
result = SignalingClient::connect(&room_data.signaling_server, &room_data.token, tx_event) => result,
|
||||||
&room_data.token,
|
_ = shutdown.changed() => return Ok(()),
|
||||||
tx_event,
|
};
|
||||||
)
|
let signaling_client = match connected {
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(cli) => {
|
Ok(cli) => {
|
||||||
let renewal_peers=webrtc_sessions.clone();
|
let renewal_peers=webrtc_sessions.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
@@ -351,27 +580,88 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
Arc::new(cli)
|
Arc::new(cli)
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Failed to connect signaling: {}. Retrying in 5s...", e);
|
let _ = e;
|
||||||
|
log::error!("Failed to connect signaling; retrying in 5s (details omitted)");
|
||||||
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
|
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 事件处理主循环
|
// Guest display/control commands never use stdout, stderr or logs.
|
||||||
while let Some(evt) = rx_event.recv().await {
|
let mut assist_tick = tokio::time::interval(std::time::Duration::from_secs(1));
|
||||||
|
loop {
|
||||||
|
let event = tokio::select! {
|
||||||
|
biased;
|
||||||
|
_ = shutdown.changed() => Some(SignalingEvent::Stop),
|
||||||
|
command = commands.recv(), if assist.is_some() => Some(SignalingEvent::AssistCommand(command.unwrap_or(assist::Command::Stop))),
|
||||||
|
_ = assist_tick.tick(), if assist.is_some() => Some(SignalingEvent::AssistTick),
|
||||||
|
event = rx_event.recv() => event,
|
||||||
|
};
|
||||||
|
let Some(evt) = event else { break; };
|
||||||
match evt {
|
match evt {
|
||||||
|
SignalingEvent::Stop | SignalingEvent::AssistCommand(assist::Command::Stop) => {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
SignalingEvent::AssistCommand(assist::Command::Allow) => { if let Some(s) = assist.as_mut() { s.allow()?; } }
|
||||||
|
SignalingEvent::AssistCommand(assist::Command::Deny) => { if let Some(s) = assist.as_mut() { s.deny()?; } }
|
||||||
|
SignalingEvent::AssistCommand(assist::Command::Verify(v)) => { if let Some(s) = assist.as_mut() { s.set_verify(v)?; } }
|
||||||
|
SignalingEvent::AssistCommand(assist::Command::Refresh(r)) => { if let Some(s) = assist.as_mut() { s.set_refresh(r)?; } }
|
||||||
|
SignalingEvent::AssistCommand(assist::Command::Custom(c)) => { if let Some(s) = assist.as_mut() { s.set_custom(c)?; } }
|
||||||
|
SignalingEvent::AssistCommand(assist::Command::Quit) => {
|
||||||
|
// Tray 退出: under systemd (Restart=always) stop the unit itself, else just exit.
|
||||||
|
log::info!("Quit requested from the tray");
|
||||||
|
if let Some(unit) = systemd_unit() {
|
||||||
|
let _ = std::process::Command::new("systemctl").args(["--user", "stop", "--no-block", &unit]).status();
|
||||||
|
}
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
SignalingEvent::AssistCommand(assist::Command::Logout) => {
|
||||||
|
if let Some(s) = assist.as_mut().filter(|s| s.is_account()) {
|
||||||
|
s.logout().await;
|
||||||
|
RESTART.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SignalingEvent::AssistCommand(assist::Command::Reset) => { if let Some(s) = assist.as_mut() { s.reset()?; } }
|
||||||
|
SignalingEvent::AssistTick => {
|
||||||
|
let mut sessions = webrtc_sessions.lock().await;
|
||||||
|
let ended: Vec<_> = sessions.iter().filter(|(_, s)| matches!(s.pc.connection_state(),
|
||||||
|
::webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Closed |
|
||||||
|
::webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Failed)).map(|(id, _)| id.clone()).collect();
|
||||||
|
let mut desktop_ended = false;
|
||||||
|
for id in ended { if let Some(s) = sessions.remove(&id) { desktop_ended |= s.desktop; let _ = s.pc.close().await; } }
|
||||||
|
if desktop_ended && !sessions.values().any(|s| s.desktop) { if let Some(s) = assist.as_mut() { s.session_ended()?; } }
|
||||||
|
if let Some(s) = assist.as_mut() { s.tick()?; }
|
||||||
|
}
|
||||||
SignalingEvent::Connected => {
|
SignalingEvent::Connected => {
|
||||||
log::info!("Signaling handshake established.");
|
log::info!("Signaling handshake established.");
|
||||||
}
|
}
|
||||||
SignalingEvent::RoomInfoAck(val) => {
|
SignalingEvent::RoomInfoAck(_) => { log::info!("RoomInfo acknowledged by NetEase server"); }
|
||||||
log::info!(
|
|
||||||
"RoomInfo acknowledged by NetEase server: room_id={}",
|
|
||||||
val.get("room_id")
|
|
||||||
.and_then(|v| v.as_str())
|
|
||||||
.unwrap_or("unknown")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
SignalingEvent::DeviceInfoChanged(val) => {
|
SignalingEvent::DeviceInfoChanged(val) => {
|
||||||
|
if assist.is_some() {
|
||||||
|
// Shape only (type value + key names), never field values: guest challenges carry salts/ids.
|
||||||
|
let keys = |v: Option<&serde_json::Value>| v.and_then(|v| v.as_object()).map(|o| o.keys().cloned().collect::<Vec<_>>()).unwrap_or_default();
|
||||||
|
log::debug!("Assist bmsg_push shape: type={:?} keys={:?} data_keys={:?}",
|
||||||
|
val.get("type").and_then(serde_json::Value::as_str), keys(Some(&val)), keys(val.get("data")));
|
||||||
|
}
|
||||||
|
if val.get("type").and_then(serde_json::Value::as_str) == Some("get_control_mode") {
|
||||||
|
if let Some(s) = assist.as_mut() {
|
||||||
|
match s.control_mode(val.get("data").unwrap_or(&serde_json::Value::Null)).await {
|
||||||
|
Ok(()) => log::info!("Assist control mode reported"),
|
||||||
|
Err(error) => log::warn!("Assist control mode report failed: {error}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if val.get("type").and_then(serde_json::Value::as_str) == Some("remote_control") {
|
||||||
|
if let Some(s) = assist.as_mut() {
|
||||||
|
if let Err(error) = s.challenge(val.get("data").unwrap_or(&serde_json::Value::Null)).await {
|
||||||
|
log::warn!("Assist challenge failed: {error}");
|
||||||
|
}
|
||||||
|
if s.renewed { break; }
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
log::info!(
|
log::info!(
|
||||||
"Device status synchronized: status={:?}, availability={:?}",
|
"Device status synchronized: status={:?}, availability={:?}",
|
||||||
val.get("data")
|
val.get("data")
|
||||||
@@ -382,7 +672,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
.and_then(|s| s.as_str())
|
.and_then(|s| s.as_str())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
SignalingEvent::ConnectRequest { capture_type, ice_id, transport } => {
|
SignalingEvent::ConnectRequest { capture_type, ice_id, transport, controller_mac } => {
|
||||||
|
if mac_by_ice.len()>=32 { mac_by_ice.clear(); }
|
||||||
|
match ice_id.clone() { Some(id) => { mac_by_ice.insert(id, controller_mac); } None => next_mac = controller_mac }
|
||||||
// Pending cloud credentials live only within this room; cap
|
// Pending cloud credentials live only within this room; cap
|
||||||
// requests that never receive an offer rather than retain them.
|
// requests that never receive an offer rather than retain them.
|
||||||
if capture_types.len()>=32 { capture_types.clear(); }
|
if capture_types.len()>=32 { capture_types.clear(); }
|
||||||
@@ -394,41 +686,40 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SignalingEvent::Soac(soac) => {
|
SignalingEvent::Soac(soac) => {
|
||||||
log::info!(
|
log::info!("SOAC signaling received: offer={} candidate={}", soac.msg_type == "offer", soac.msg_type == "candidate");
|
||||||
"SOAC signaling received: type={}, ice_id={:?}",
|
// Guest host: every peer is an assist peer. A logged-in host must keep serving its owner;
|
||||||
soac.msg_type,
|
// assist peers there are already gated by the cloud (allow_control / code check).
|
||||||
soac.ice_id
|
if assist.as_ref().is_some_and(|s| !s.is_account() && !s.allowed) { continue; }
|
||||||
);
|
|
||||||
let ice = soac.ice_id.as_ref().map(|v| v.as_str().map(str::to_owned).unwrap_or_else(|| v.to_string())).unwrap_or_default();
|
let ice = soac.ice_id.as_ref().map(|v| v.as_str().map(str::to_owned).unwrap_or_else(|| v.to_string())).unwrap_or_default();
|
||||||
let mut sessions = webrtc_sessions.lock().await;
|
let mut sessions = webrtc_sessions.lock().await;
|
||||||
// Drop peers that already ended.
|
// Drop peers that already ended.
|
||||||
let ended: Vec<String> = sessions.iter().filter(|(_, s)| matches!(s.pc.connection_state(),
|
let ended: Vec<String> = sessions.iter().filter(|(_, s)| matches!(s.pc.connection_state(),
|
||||||
::webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Closed |
|
::webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Closed |
|
||||||
::webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Failed)).map(|(k, _)| k.clone()).collect();
|
::webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Failed)).map(|(k, _)| k.clone()).collect();
|
||||||
for k in ended { if let Some(old) = sessions.remove(&k) { let _ = old.pc.close().await; } }
|
let mut desktop_ended = false;
|
||||||
|
for k in ended { if let Some(old) = sessions.remove(&k) { desktop_ended |= old.desktop; let _ = old.pc.close().await; } }
|
||||||
|
if desktop_ended && !sessions.values().any(|s| s.desktop) { if let Some(s) = assist.as_mut() { s.session_ended()?; } }
|
||||||
|
if let Some(s) = assist.as_mut() { s.tick()?; }
|
||||||
|
|
||||||
if soac.msg_type == "offer" {
|
if soac.msg_type == "offer" {
|
||||||
let (capture_type,transport) = capture_types.remove(&ice).or_else(|| next_capture_type.take()).unwrap_or((1,crate::session_ice::SessionIce::default()));
|
let (capture_type,transport) = capture_types.remove(&ice).or_else(|| next_capture_type.take()).unwrap_or((1,crate::session_ice::SessionIce::default()));
|
||||||
// CaptureType: 0 unknown, 1 desktop, 2 window; 5 file transfer, 8 terminal, 9 port mapping.
|
// CaptureType: 0 unknown, 1 desktop, 2 window; 5 file transfer, 8 terminal, 9 port mapping.
|
||||||
let desktop = capture_type <= 2;
|
let desktop = capture_type <= 2;
|
||||||
|
let controller_mac = mac_by_ice.remove(&ice).unwrap_or(next_mac);
|
||||||
|
if desktop { input.set_mac_controller(controller_mac); }
|
||||||
log::info!("Offer for connection: capture_type={} desktop={} active_peers={}", capture_type, desktop, sessions.len());
|
log::info!("Offer for connection: capture_type={} desktop={} active_peers={}", capture_type, desktop, sessions.len());
|
||||||
if let Some(old) = sessions.remove(&ice) { let _ = old.pc.close().await; }
|
// Replace only this ICE id or a previous desktop, never tool peers.
|
||||||
if desktop {
|
for id in superseded_peer_ids(&sessions, &ice, desktop) {
|
||||||
// One desktop stream at a time: a new desktop offer replaces stale ones.
|
if let Some(old) = sessions.remove(&id) { let _ = old.pc.close().await; }
|
||||||
let stale: Vec<String> = sessions.iter().filter(|(_, s)| s.desktop).map(|(k, _)| k.clone()).collect();
|
|
||||||
for k in stale { if let Some(old) = sessions.remove(&k) { let _ = old.pc.close().await; } }
|
|
||||||
}
|
}
|
||||||
let stale_portal=capture.portal.as_ref().is_some_and(|session|!session.is_alive())
|
|
||||||
&& crate::session_lock::locked_now() == Some(false);
|
|
||||||
if desktop && desktop_backend::headless() {
|
if desktop && desktop_backend::headless() {
|
||||||
log::info!("Desktop connection ignored: headless mode (terminal, file transfer and port mapping remain available)");
|
log::info!("Desktop connection ignored: headless mode (terminal, file transfer and port mapping remain available)");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let session_capture=if desktop && (capture.init_error.is_some() || stale_portal) {
|
let resized=desktop && capture.init_error.is_none() && capture.backend==desktop_backend::DesktopBackend::X11
|
||||||
if stale_portal {
|
&& tokio::task::spawn_blocking(crate::capture::x11_resized).await.unwrap_or(false);
|
||||||
input.release_held();input.bind_portal(None);
|
if resized { log::info!("Desktop geometry changed since startup; reinitializing capture"); }
|
||||||
capture=Arc::new(ScreenCapture::unavailable("Portal authorization expired".into()));
|
let session_capture=if desktop && portal_desktop.is_none() && (capture.init_error.is_some() || resized) {
|
||||||
}
|
|
||||||
match tokio::task::spawn_blocking(ScreenCapture::new).await {
|
match tokio::task::spawn_blocking(ScreenCapture::new).await {
|
||||||
Ok(Ok(screen))=>{ capture=Arc::new(screen);capture.clone() },
|
Ok(Ok(screen))=>{ capture=Arc::new(screen);capture.clone() },
|
||||||
Ok(Err(error))=>{ log::warn!("Desktop connection unavailable: {error}"); continue; },
|
Ok(Err(error))=>{ log::warn!("Desktop connection unavailable: {error}"); continue; },
|
||||||
@@ -437,9 +728,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
} else { capture.clone() };
|
} else { capture.clone() };
|
||||||
// Independent file/terminal peers must not detach the
|
// Independent file/terminal peers must not detach the
|
||||||
// current desktop's authorized Portal input session.
|
// current desktop's authorized Portal input session.
|
||||||
if desktop { input.bind_portal(session_capture.portal.clone()); }
|
if desktop && portal_desktop.is_none() { input.bind_portal(session_capture.portal.clone()); }
|
||||||
match WebRtcSession::new(session_capture, input.clone(), signaling_client.clone(), desktop, transport).await {
|
if desktop { if let Some(portal) = &portal_desktop { portal.begin(); } }
|
||||||
Ok(sess) => match sess.handle_offer(&soac).await {
|
match WebRtcSession::new(session_capture, input.clone(), signaling_client.clone(), desktop, transport,
|
||||||
|
if desktop { portal_desktop.clone() } else { None }).await {
|
||||||
|
Ok(sess) => {
|
||||||
|
// Register before awaiting SDP: shutdown can always close
|
||||||
|
// a peer whose answer may already be queued to the viewer.
|
||||||
|
sessions.insert(ice.clone(), sess);
|
||||||
|
let offer = sessions.get(&ice).expect("registered peer").handle_offer(&soac).await;
|
||||||
|
match offer {
|
||||||
Ok(answer_sdp) => {
|
Ok(answer_sdp) => {
|
||||||
log::info!("Local SDP Answer generated, emitting soac response...");
|
log::info!("Local SDP Answer generated, emitting soac response...");
|
||||||
let resp = SoacMessage {
|
let resp = SoacMessage {
|
||||||
@@ -454,35 +752,37 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
app_control_id: soac.app_control_id.clone(),
|
app_control_id: soac.app_control_id.clone(),
|
||||||
};
|
};
|
||||||
if let Err(e) = signaling_client.emit_soac(resp).await { log::error!("Answer signaling send failed: {}", e); }
|
if let Err(e) = signaling_client.emit_soac(resp).await { log::error!("Answer signaling send failed: {}", e); }
|
||||||
sessions.insert(ice, sess);
|
|
||||||
}
|
}
|
||||||
Err(e) => { log::error!("Failed to handle offer: {}", e); let _ = sess.pc.close().await; }
|
Err(_) => {
|
||||||
},
|
log::error!("Failed to handle offer (details omitted)");
|
||||||
Err(e) => log::error!("Create peer failed: {}", e),
|
if let Some(session) = sessions.remove(&ice) { let _ = session.pc.close().await; }
|
||||||
|
}
|
||||||
|
}},
|
||||||
|
Err(_) => log::error!("Create peer failed (details omitted)"),
|
||||||
}
|
}
|
||||||
} else if soac.msg_type == "candidate" {
|
} else if soac.msg_type == "candidate" {
|
||||||
if let Some(sess) = sessions.get(&ice) {
|
if let Some(sess) = sessions.get(&ice) {
|
||||||
if let Err(e) = sess.add_candidate(&soac).await { log::error!("Remote ICE candidate failed: {}", e); }
|
if let Err(e) = sess.add_candidate(&soac).await { let _ = e; log::error!("Remote ICE candidate failed (details omitted)"); }
|
||||||
} else {
|
} else {
|
||||||
log::warn!("Remote ICE candidate for unknown connection");
|
log::warn!("Remote ICE candidate for unknown connection");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SignalingEvent::Released(val) | SignalingEvent::Left(val) => {
|
SignalingEvent::Released(val) | SignalingEvent::Left(val) => {
|
||||||
let ice = ["ice_id", "iceId"].iter().find_map(|k| val.get(*k).or_else(|| val.get("data").and_then(|d| d.get(*k))))
|
let ice = ["ice_id", "iceId"].iter().find_map(|k| val.get(*k).or_else(|| val.get("data").and_then(|d| d.get(*k))));
|
||||||
.and_then(|v| v.as_str()).map(str::to_owned);
|
|
||||||
let keys: Vec<String> = val.as_object().map(|o| o.keys().take(16).cloned().collect()).unwrap_or_default();
|
let keys: Vec<String> = val.as_object().map(|o| o.keys().take(16).cloned().collect()).unwrap_or_default();
|
||||||
let mut sessions = webrtc_sessions.lock().await;
|
let mut sessions = webrtc_sessions.lock().await;
|
||||||
let targets: Vec<String> = match &ice {
|
let targets = released_peer_ids(&sessions, ice);
|
||||||
Some(id) if sessions.contains_key(id) => vec![id.clone()],
|
log::info!("Remote controller disconnected: keys={:?} ice_matched={} closing={}", keys, ice.is_some() && !targets.is_empty(), targets.len());
|
||||||
_ => sessions.keys().cloned().collect(),
|
let mut desktop_ended = false;
|
||||||
};
|
|
||||||
log::info!("Remote controller disconnected: keys={:?} ice_matched={} closing={}", keys, ice.as_ref().is_some_and(|i| sessions.contains_key(i)), targets.len());
|
|
||||||
for k in targets {
|
for k in targets {
|
||||||
if let Some(old) = sessions.remove(&k) {
|
if let Some(old) = sessions.remove(&k) {
|
||||||
|
desktop_ended |= old.desktop;
|
||||||
if let Err(e) = old.pc.close().await { log::warn!("Close released peer failed: {}", e); }
|
if let Err(e) = old.pc.close().await { log::warn!("Close released peer failed: {}", e); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if desktop_ended && !sessions.values().any(|s| s.desktop) { if let Some(s) = assist.as_mut() { s.session_ended()?; } }
|
||||||
|
if let Some(s) = assist.as_mut() { s.tick()?; }
|
||||||
}
|
}
|
||||||
SignalingEvent::Disconnected => {
|
SignalingEvent::Disconnected => {
|
||||||
capture_types.clear();
|
capture_types.clear();
|
||||||
|
|||||||
@@ -154,22 +154,21 @@ mod tests {
|
|||||||
assert_eq!(rates.sample(now + std::time::Duration::from_secs(3), 0, 0), None);
|
assert_eq!(rates.sample(now + std::time::Duration::from_secs(3), 0, 0), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Exercises actual ICE nomination and SCTP delivery between local peers, without
|
/// Exercises actual ICE nomination and SCTP delivery between virtual peers, without
|
||||||
/// login, public STUN, external endpoints or production configuration.
|
/// login, public STUN, external endpoints or production configuration.
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
#[ignore = "opens local UDP sockets; run explicitly for network acceptance"]
|
|
||||||
async fn local_selected_pair_and_payload() {
|
async fn local_selected_pair_and_payload() {
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use webrtc::api::{APIBuilder, setting_engine::SettingEngine};
|
use webrtc::api::APIBuilder;
|
||||||
use webrtc::peer_connection::configuration::RTCConfiguration;
|
use webrtc::peer_connection::configuration::RTCConfiguration;
|
||||||
let mut settings = SettingEngine::default();
|
let (left_setting, right_setting, router) = crate::test_network::virtual_network().await;
|
||||||
// This crate excludes loopback addresses during ICE gathering. Both
|
|
||||||
// peers still run in this process and use only gathered local addresses.
|
|
||||||
settings.set_ice_multicast_dns_mode(webrtc::ice::mdns::MulticastDnsMode::Disabled);
|
|
||||||
let mut media = webrtc::api::media_engine::MediaEngine::default();
|
let mut media = webrtc::api::media_engine::MediaEngine::default();
|
||||||
media.register_default_codecs().unwrap();
|
media.register_default_codecs().unwrap();
|
||||||
let api = APIBuilder::new().with_media_engine(media).with_setting_engine(settings).build();
|
let api = APIBuilder::new().with_media_engine(media).with_setting_engine(left_setting).build();
|
||||||
let left = Arc::new(api.new_peer_connection(RTCConfiguration::default()).await.unwrap());
|
let left = Arc::new(api.new_peer_connection(RTCConfiguration::default()).await.unwrap());
|
||||||
|
let mut media = webrtc::api::media_engine::MediaEngine::default();
|
||||||
|
media.register_default_codecs().unwrap();
|
||||||
|
let api = APIBuilder::new().with_media_engine(media).with_setting_engine(right_setting).build();
|
||||||
let right = Arc::new(api.new_peer_connection(RTCConfiguration::default()).await.unwrap());
|
let right = Arc::new(api.new_peer_connection(RTCConfiguration::default()).await.unwrap());
|
||||||
for (label, peer) in [("left", &left), ("right", &right)] {
|
for (label, peer) in [("left", &left), ("right", &right)] {
|
||||||
peer.on_peer_connection_state_change(Box::new(move |state| {
|
peer.on_peer_connection_state_change(Box::new(move |state| {
|
||||||
@@ -222,6 +221,7 @@ mod tests {
|
|||||||
let outcome = tokio::time::timeout(std::time::Duration::from_secs(15), acceptance).await;
|
let outcome = tokio::time::timeout(std::time::Duration::from_secs(15), acceptance).await;
|
||||||
left.close().await.unwrap();
|
left.close().await.unwrap();
|
||||||
right.close().await.unwrap();
|
right.close().await.unwrap();
|
||||||
|
router.lock().await.stop().await.unwrap();
|
||||||
outcome.expect("isolated local ICE/SCTP did not connect within 15 seconds");
|
outcome.expect("isolated local ICE/SCTP did not connect within 15 seconds");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+518
-43
@@ -1,37 +1,121 @@
|
|||||||
//! GNOME/KDE portal session. Capture and input must share this Arc; do not
|
//! GNOME/KDE portal session. Capture and input must share this Arc; do not
|
||||||
//! recreate it in an encoder subprocess (that would request consent again).
|
//! recreate it in an encoder subprocess (that would request consent again).
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
|
pub struct PortalError { pub code: i32 }
|
||||||
|
|
||||||
|
impl std::fmt::Display for PortalError {
|
||||||
|
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(formatter, "{}", error(self.code))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for PortalError {}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub struct FrameVersion(usize, u64);
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
#[path = "portal_permissions.rs"]
|
#[path = "portal_permissions.rs"]
|
||||||
mod permissions;
|
mod permissions;
|
||||||
|
|
||||||
#[cfg(all(target_os = "linux", feature = "portal"))]
|
#[cfg(all(target_os = "linux", feature = "portal"))]
|
||||||
mod native {
|
mod native {
|
||||||
use std::{ffi::c_void, ptr::NonNull, sync::Mutex};
|
use std::{ffi::c_void, ptr::NonNull, sync::{Mutex, MutexGuard, TryLockError}};
|
||||||
extern "C" {
|
extern "C" {
|
||||||
fn uu_portal_open_persist(restore: *const libc::c_char, save: extern "C" fn(*const libc::c_char, *mut c_void), data: *mut c_void, error: *mut i32, width: *mut u32, height: *mut u32) -> *mut c_void;
|
fn uu_portal_open_persist(restore: *const libc::c_char, save: extern "C" fn(*const libc::c_char, *mut c_void), data: *mut c_void, error: *mut i32, width: *mut u32, height: *mut u32) -> *mut c_void;
|
||||||
|
fn uu_portal_open_persist_cancellable(restore: *const libc::c_char, save: extern "C" fn(*const libc::c_char, *mut c_void), save_data: *mut c_void, cancelled: extern "C" fn(*mut c_void) -> i32, cancel_data: *mut c_void, error: *mut i32, width: *mut u32, height: *mut u32) -> *mut c_void;
|
||||||
fn uu_portal_clipboard_supported(portal: *mut c_void) -> i32;
|
fn uu_portal_clipboard_supported(portal: *mut c_void) -> i32;
|
||||||
fn uu_portal_clipboard_ready(portal: *mut c_void) -> i32;
|
fn uu_portal_clipboard_ready(portal: *mut c_void) -> i32;
|
||||||
fn uu_portal_clipboard_set(portal: *mut c_void, text: *const u8, length: usize) -> i32;
|
fn uu_portal_clipboard_set(portal: *mut c_void, text: *const u8, length: usize) -> i32;
|
||||||
fn uu_portal_clipboard_poll(portal: *mut c_void, text: *mut u8, capacity: usize) -> i32;
|
fn uu_portal_clipboard_poll(portal: *mut c_void, text: *mut u8, capacity: usize) -> i32;
|
||||||
fn uu_portal_close(portal: *mut c_void);
|
fn uu_portal_close(portal: *mut c_void);
|
||||||
fn uu_portal_alive(portal: *mut c_void) -> i32;
|
fn uu_portal_alive(portal: *mut c_void) -> i32;
|
||||||
|
fn uu_portal_capture_alive(portal: *mut c_void) -> i32;
|
||||||
|
fn uu_portal_input_dispatch(portal: *mut c_void);
|
||||||
fn uu_portal_reset_frame(portal: *mut c_void);
|
fn uu_portal_reset_frame(portal: *mut c_void);
|
||||||
fn uu_portal_capture(portal: *mut c_void, bgra: *mut u8, length: usize) -> i32;
|
fn uu_portal_capture_since(portal: *mut c_void, bgra: *mut u8, length: usize, revision: *mut u64) -> i32;
|
||||||
fn uu_portal_move(portal: *mut c_void, x: f64, y: f64, relative: i32) -> i32;
|
fn uu_portal_move(portal: *mut c_void, x: f64, y: f64, relative: i32) -> i32;
|
||||||
fn uu_portal_button(portal: *mut c_void, button: i32, pressed: i32) -> i32;
|
fn uu_portal_button(portal: *mut c_void, button: i32, pressed: i32) -> i32;
|
||||||
fn uu_portal_key(portal: *mut c_void, keysym: u32, pressed: i32) -> i32;
|
fn uu_portal_key(portal: *mut c_void, keysym: u32, keycode: i32, pressed: i32) -> i32;
|
||||||
}
|
}
|
||||||
struct Handle(NonNull<c_void>);
|
struct Handle(NonNull<c_void>);
|
||||||
// The engine owns callback threads; public calls are serialized by Mutex.
|
// The engine owns callback threads; public calls are serialized by Mutex.
|
||||||
unsafe impl Send for Handle {}
|
unsafe impl Send for Handle {}
|
||||||
impl Drop for Handle { fn drop(&mut self) { unsafe { uu_portal_close(self.0.as_ptr()) } } }
|
impl Drop for Handle { fn drop(&mut self) { unsafe { uu_portal_close(self.0.as_ptr()) } } }
|
||||||
pub struct Session { handle: Mutex<Handle>, locked: tokio::sync::watch::Receiver<Option<bool>>, was_locked: std::sync::atomic::AtomicBool, retry_at: Mutex<Option<std::time::Instant>>, pub width: u32, pub height: u32 }
|
fn blank_changed(frame: &mut [u8], version: &mut Option<super::FrameVersion>) -> bool {
|
||||||
|
if *version == Some(super::FrameVersion(0, 0)) { return false; }
|
||||||
|
super::blank(frame); *version = Some(super::FrameVersion(0, 0)); true
|
||||||
|
}
|
||||||
|
// Portal preparation and native teardown can wait for consent or D-Bus.
|
||||||
|
// Only the swap may hold the mutex shared with input and clipboard.
|
||||||
|
fn replace_handle<T>(current: &Mutex<T>, locked: MutexGuard<'_, T>, prepare: impl FnOnce() -> Result<Option<T>, String>) -> Result<bool, String> {
|
||||||
|
drop(locked);
|
||||||
|
let Some(replacement) = prepare()? else { return Ok(false); };
|
||||||
|
let old = {
|
||||||
|
let mut handle = current.lock().map_err(|_| "Portal session lock poisoned")?;
|
||||||
|
std::mem::replace(&mut *handle, replacement)
|
||||||
|
};
|
||||||
|
drop(old);
|
||||||
|
Ok(true)
|
||||||
|
}
|
||||||
|
#[derive(Default)]
|
||||||
|
struct Recovery { retry_at: Option<std::time::Instant>, failed_until: Option<std::time::Instant>, denied: bool }
|
||||||
|
impl Recovery {
|
||||||
|
// Shared by the encoder's stream-loss recovery and the desktop supervisor: a failed open
|
||||||
|
// backs both off, and a terminal consent/format failure stops the encoder from prompting again.
|
||||||
|
fn failed(&mut self, now: std::time::Instant, code: i32) {
|
||||||
|
let until = now + std::time::Duration::from_secs(5);
|
||||||
|
self.failed_until = Some(until);
|
||||||
|
self.retry_at = Some(until);
|
||||||
|
if matches!(code, 4 | 5 | 6 | 8 | 14) { self.denied = true; }
|
||||||
|
}
|
||||||
|
fn succeeded(&mut self) { *self = Self::default(); }
|
||||||
|
fn ready(&mut self, now: std::time::Instant, unlocked: impl FnOnce() -> bool) -> bool {
|
||||||
|
if self.denied { return false; }
|
||||||
|
let deadline = self.retry_at.get_or_insert(now + std::time::Duration::from_secs(1));
|
||||||
|
if now < *deadline { return false; }
|
||||||
|
*deadline = now + std::time::Duration::from_secs(1);
|
||||||
|
unlocked()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn clear_pause_for_unlocked_replacement(was_locked: &std::sync::atomic::AtomicBool, state: Option<bool>) {
|
||||||
|
if state == Some(false) {
|
||||||
|
was_locked.store(false, std::sync::atomic::Ordering::Release);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub struct Session { handle: Mutex<Handle>, locked: tokio::sync::watch::Receiver<Option<bool>>, was_locked: std::sync::atomic::AtomicBool, recovery: Mutex<Recovery>, pub width: u32, pub height: u32 }
|
||||||
impl Session {
|
impl Session {
|
||||||
|
fn take_lock_observation(&self) -> Result<bool, String> {
|
||||||
|
// A recovery in progress must see the lock even if another capture
|
||||||
|
// observes unlock and would otherwise reset only the old handle.
|
||||||
|
let _recovery = match self.recovery.try_lock() {
|
||||||
|
Ok(recovery) => recovery,
|
||||||
|
Err(TryLockError::WouldBlock) => return Ok(false),
|
||||||
|
Err(TryLockError::Poisoned(_)) => return Err("Portal recovery lock poisoned".into()),
|
||||||
|
};
|
||||||
|
Ok(self.was_locked.swap(false, std::sync::atomic::Ordering::AcqRel))
|
||||||
|
}
|
||||||
|
fn check_geometry(&self, replacement: &Self) -> Result<(), super::PortalError> {
|
||||||
|
// Encoder buffers use frame pixels. Logical geometry and EIS regions
|
||||||
|
// belong to the replacement handle and may change with scaling (#25).
|
||||||
|
if (self.width, self.height) != (replacement.width, replacement.height) {
|
||||||
|
Err(super::PortalError { code: 14 })
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn is_alive(&self) -> bool {
|
pub fn is_alive(&self) -> bool {
|
||||||
self.handle.lock().is_ok_and(|handle| unsafe { uu_portal_alive(handle.0.as_ptr()) != 0 })
|
self.handle.lock().is_ok_and(|handle| unsafe { uu_portal_alive(handle.0.as_ptr()) != 0 })
|
||||||
}
|
}
|
||||||
pub fn open() -> Result<Self, String> {
|
fn open_inner(cancelled: Option<&std::sync::atomic::AtomicBool>) -> Result<Self, super::PortalError> {
|
||||||
|
extern "C" fn is_cancelled(data: *mut c_void) -> i32 {
|
||||||
|
let cancelled = unsafe { &*(data as *const std::sync::atomic::AtomicBool) };
|
||||||
|
i32::from(cancelled.load(std::sync::atomic::Ordering::Acquire))
|
||||||
|
}
|
||||||
|
if cancelled.is_some_and(|value| value.load(std::sync::atomic::Ordering::Acquire)) {
|
||||||
|
return Err(super::PortalError { code: 13 });
|
||||||
|
}
|
||||||
let (mut error, mut width, mut height) = (0, 0, 0);
|
let (mut error, mut width, mut height) = (0, 0, 0);
|
||||||
let directory = super::permissions::directory();
|
let directory = super::permissions::directory();
|
||||||
let key = super::permissions::desktop_key();
|
let key = super::permissions::desktop_key();
|
||||||
@@ -48,57 +132,160 @@ mod native {
|
|||||||
});
|
});
|
||||||
if result.is_err() { log::warn!("Portal permission cache callback failed"); }
|
if result.is_err() { log::warn!("Portal permission cache callback failed"); }
|
||||||
}
|
}
|
||||||
let pointer = unsafe { uu_portal_open_persist(token.as_ref().map_or(std::ptr::null(), |v| v.as_ptr()), save_token, &mut cache as *mut _ as *mut c_void, &mut error, &mut width, &mut height) };
|
let restore = token.as_ref().map_or(std::ptr::null(), |value| value.as_ptr());
|
||||||
let handle = NonNull::new(pointer).ok_or_else(|| super::error(error))?;
|
let pointer = unsafe {
|
||||||
|
if let Some(cancelled) = cancelled {
|
||||||
|
uu_portal_open_persist_cancellable(
|
||||||
|
restore, save_token, &mut cache as *mut _ as *mut c_void,
|
||||||
|
is_cancelled, cancelled as *const _ as *mut c_void,
|
||||||
|
&mut error, &mut width, &mut height,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
uu_portal_open_persist(restore, save_token, &mut cache as *mut _ as *mut c_void, &mut error, &mut width, &mut height)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let handle = NonNull::new(pointer).ok_or(super::PortalError { code: error })?;
|
||||||
// A freshly opened session holds no lock-screen frame: only an observed lock (or a
|
// A freshly opened session holds no lock-screen frame: only an observed lock (or a
|
||||||
// closed stream) must discard the cached frame. Starting "locked" cleared the first
|
// closed stream) must discard the cached frame. Starting "locked" cleared the first
|
||||||
// valid frame, and a static desktop sends no new one, so the viewer stayed black (#2).
|
// valid frame, and a static desktop sends no new one, so the viewer stayed black (#2).
|
||||||
Ok(Self { handle: Mutex::new(Handle(handle)), locked: crate::session_lock::watch_locked(), was_locked: std::sync::atomic::AtomicBool::new(false), retry_at: Mutex::new(None), width, height })
|
Ok(Self { handle: Mutex::new(Handle(handle)), locked: crate::session_lock::watch_locked(), was_locked: std::sync::atomic::AtomicBool::new(false), recovery: Mutex::new(Recovery::default()), width, height })
|
||||||
|
}
|
||||||
|
pub fn open() -> Result<Self, String> {
|
||||||
|
Self::open_inner(None).map_err(|error| error.to_string())
|
||||||
|
}
|
||||||
|
pub fn open_cancellable(cancelled: &std::sync::atomic::AtomicBool) -> Result<Self, super::PortalError> {
|
||||||
|
Self::open_inner(Some(cancelled))
|
||||||
|
}
|
||||||
|
pub fn recover(&self, cancelled: &std::sync::atomic::AtomicBool) -> Result<bool, super::PortalError> {
|
||||||
|
use std::sync::atomic::Ordering;
|
||||||
|
if cancelled.load(Ordering::Acquire) { return Err(super::PortalError { code: 13 }); }
|
||||||
|
// Serialize with the encoder's stream-loss recovery: two openers would ask for consent twice.
|
||||||
|
let mut recovery = loop {
|
||||||
|
match self.recovery.try_lock() {
|
||||||
|
Ok(recovery) => break recovery,
|
||||||
|
Err(TryLockError::WouldBlock) => {
|
||||||
|
if cancelled.load(Ordering::Acquire) { return Err(super::PortalError { code: 13 }); }
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(20));
|
||||||
|
}
|
||||||
|
Err(TryLockError::Poisoned(_)) => return Err(super::PortalError { code: 3 }),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if recovery.failed_until.is_some_and(|until| std::time::Instant::now() < until) {
|
||||||
|
return Err(super::PortalError { code: 3 });
|
||||||
|
}
|
||||||
|
let alive = {
|
||||||
|
let handle = self.handle.lock().map_err(|_| super::PortalError { code: 3 })?;
|
||||||
|
unsafe { uu_portal_alive(handle.0.as_ptr()) != 0 && uu_portal_capture_alive(handle.0.as_ptr()) != 0 }
|
||||||
|
};
|
||||||
|
if alive { return Ok(false); }
|
||||||
|
|
||||||
|
let result = super::open_then_install(cancelled, || Self::open_cancellable(cancelled), |replacement| {
|
||||||
|
self.check_geometry(&replacement)?;
|
||||||
|
let replacement_handle = replacement.handle.into_inner().map_err(|_| super::PortalError { code: 3 })?;
|
||||||
|
let mut handle = self.handle.lock().map_err(|_| super::PortalError { code: 3 })?;
|
||||||
|
if cancelled.load(Ordering::Acquire) { return Err(super::PortalError { code: 13 }); }
|
||||||
|
*handle = replacement_handle;
|
||||||
|
clear_pause_for_unlocked_replacement(&self.was_locked, *self.locked.borrow());
|
||||||
|
log::info!("Portal capture session restored after closure");
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
match result {
|
||||||
|
Ok(()) => { recovery.succeeded(); Ok(true) }
|
||||||
|
Err(error) => {
|
||||||
|
if error.code != 13 { recovery.failed(std::time::Instant::now(), error.code); }
|
||||||
|
Err(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn pause(&self) {
|
||||||
|
if let Ok(handle) = self.handle.lock() {
|
||||||
|
unsafe { uu_portal_reset_frame(handle.0.as_ptr()) };
|
||||||
|
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
|
||||||
|
} else {
|
||||||
|
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn capture(&self, frame: &mut [u8]) -> Result<(), String> {
|
pub fn capture(&self, frame: &mut [u8]) -> Result<(), String> {
|
||||||
let mut handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?;
|
self.capture_changed(frame, &mut None).map(|_| ())
|
||||||
|
}
|
||||||
|
pub fn capture_changed(&self, frame: &mut [u8], version: &mut Option<super::FrameVersion>) -> Result<bool, String> {
|
||||||
|
if frame.len() != self.width as usize * self.height as usize * 4 { return Err(super::error(11)); }
|
||||||
|
let handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?;
|
||||||
|
// Drain EIS even on a static desktop so pause/disconnect and sync
|
||||||
|
// events are processed without waiting for remote input.
|
||||||
|
unsafe { uu_portal_input_dispatch(handle.0.as_ptr()) };
|
||||||
let state = *self.locked.borrow();
|
let state = *self.locked.borrow();
|
||||||
if state != Some(false) {
|
if state != Some(false) {
|
||||||
// Unknown state (watcher not reported yet): show nothing but keep the cached frame.
|
// Unknown state (watcher not reported yet): show nothing but keep the cached frame.
|
||||||
if state == Some(true) {
|
if state == Some(true) {
|
||||||
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
|
|
||||||
unsafe { uu_portal_reset_frame(handle.0.as_ptr()) };
|
unsafe { uu_portal_reset_frame(handle.0.as_ptr()) };
|
||||||
}
|
|
||||||
super::blank(frame); return Ok(());
|
|
||||||
}
|
|
||||||
if self.was_locked.swap(false, std::sync::atomic::Ordering::AcqRel) {
|
|
||||||
// First unlocked capture after a real lock: drop frames captured while locked.
|
|
||||||
unsafe { uu_portal_reset_frame(handle.0.as_ptr()) };
|
|
||||||
super::blank(frame); return Ok(());
|
|
||||||
}
|
|
||||||
if unsafe { uu_portal_alive(handle.0.as_ptr()) } == 0 {
|
|
||||||
// GNOME can close the stream before the periodic lock watcher updates.
|
|
||||||
if crate::session_lock::locked_now() != Some(false) {
|
|
||||||
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
|
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
|
||||||
super::blank(frame); return Ok(());
|
|
||||||
}
|
}
|
||||||
// logind's LockedHint can lag compositor stream closure. Give it a
|
return Ok(blank_changed(frame, version));
|
||||||
// full watcher interval before attempting another permission session.
|
}
|
||||||
let mut retry = self.retry_at.lock().map_err(|_| "Portal recovery lock poisoned")?;
|
if self.was_locked.load(std::sync::atomic::Ordering::Acquire) {
|
||||||
let deadline = retry.get_or_insert_with(|| std::time::Instant::now() + std::time::Duration::from_secs(1));
|
// First unlocked capture after a real lock: drop frames captured while locked.
|
||||||
if std::time::Instant::now() < *deadline { super::blank(frame); return Ok(()); }
|
if self.take_lock_observation()? { unsafe { uu_portal_reset_frame(handle.0.as_ptr()) }; }
|
||||||
let replacement = match Self::open() {
|
return Ok(blank_changed(frame, version));
|
||||||
Ok(value) => value,
|
}
|
||||||
Err(error) => {
|
if unsafe { uu_portal_capture_alive(handle.0.as_ptr()) } == 0 {
|
||||||
log::warn!("Portal capture recovery deferred: {error}");
|
// Serialize recovery without making another capture worker hold
|
||||||
*retry = Some(std::time::Instant::now() + std::time::Duration::from_secs(5));
|
// the input mutex while waiting for this worker's Portal calls.
|
||||||
super::blank(frame); return Ok(());
|
let mut recovery = match self.recovery.try_lock() {
|
||||||
|
Ok(retry) => retry,
|
||||||
|
Err(TryLockError::WouldBlock) => return Ok(blank_changed(frame, version)),
|
||||||
|
Err(TryLockError::Poisoned(_)) => return Err("Portal recovery lock poisoned".into()),
|
||||||
|
};
|
||||||
|
if replace_handle(&self.handle, handle, || {
|
||||||
|
{
|
||||||
|
let handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?;
|
||||||
|
if unsafe { uu_portal_capture_alive(handle.0.as_ptr()) } != 0 {
|
||||||
|
recovery.retry_at = None;
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// logind's LockedHint can lag compositor stream closure.
|
||||||
|
if !recovery.ready(std::time::Instant::now(), || {
|
||||||
|
if crate::session_lock::locked_now() == Some(false) { return true; }
|
||||||
|
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
|
||||||
|
false
|
||||||
|
}) { return Ok(None); }
|
||||||
|
let replacement = match Self::open_inner(None) {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(error) => {
|
||||||
|
log::warn!("Portal capture recovery deferred: {error}");
|
||||||
|
recovery.failed(std::time::Instant::now(), error.code);
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
recovery.succeeded();
|
||||||
|
self.check_geometry(&replacement).map_err(|error| error.to_string())?;
|
||||||
|
// Consent can outlive the unlocked state checked above.
|
||||||
|
if *self.locked.borrow() != Some(false) || self.was_locked.load(std::sync::atomic::Ordering::Acquire) {
|
||||||
|
return Ok(None);
|
||||||
}
|
}
|
||||||
};
|
replacement.handle.into_inner().map(Some).map_err(|_| "Portal session lock poisoned".into())
|
||||||
*retry = None;
|
})? {
|
||||||
if replacement.width != self.width || replacement.height != self.height {
|
*version = None;
|
||||||
return Err("Portal dimensions changed; reconnect required".into());
|
log::info!("Portal capture session restored after stream loss");
|
||||||
|
}
|
||||||
|
// Recheck lock state on the next capture before showing the new session.
|
||||||
|
return Ok(blank_changed(frame, version));
|
||||||
|
} else {
|
||||||
|
match self.recovery.try_lock() {
|
||||||
|
Ok(mut recovery) => recovery.retry_at = None,
|
||||||
|
Err(TryLockError::WouldBlock) => (),
|
||||||
|
Err(TryLockError::Poisoned(_)) => return Err("Portal recovery lock poisoned".into()),
|
||||||
}
|
}
|
||||||
*handle = replacement.handle.into_inner().map_err(|_| "Portal session lock poisoned")?;
|
}
|
||||||
log::info!("Portal capture session restored after closure");
|
let identity = handle.0.as_ptr() as usize;
|
||||||
|
let mut revision = version.filter(|v| v.0 == identity).map_or(0, |v| v.1);
|
||||||
|
let code = unsafe { uu_portal_capture_since(handle.0.as_ptr(), frame.as_mut_ptr(), frame.len(), &mut revision) };
|
||||||
|
match code {
|
||||||
|
0 | 12 => { *version = Some(super::FrameVersion(identity, revision)); Ok(code == 0) }
|
||||||
|
9 | 10 => Ok(blank_changed(frame, version)),
|
||||||
|
_ => Err(super::error(code)),
|
||||||
}
|
}
|
||||||
let code = unsafe { uu_portal_capture(handle.0.as_ptr(), frame.as_mut_ptr(), frame.len()) };
|
|
||||||
if code == 10 || code == 9 { super::blank(frame); Ok(()) } else { super::check(code) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clipboard_supported(&self) -> bool {
|
pub fn clipboard_supported(&self) -> bool {
|
||||||
@@ -134,20 +321,270 @@ mod native {
|
|||||||
if pressed && *self.locked.borrow() != Some(false) { return Err("Portal input blocked while locked".into()); }
|
if pressed && *self.locked.borrow() != Some(false) { return Err("Portal input blocked while locked".into()); }
|
||||||
let symbol = u32::try_from(symbol).map_err(|_| "Invalid Portal keysym")?;
|
let symbol = u32::try_from(symbol).map_err(|_| "Invalid Portal keysym")?;
|
||||||
let handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?;
|
let handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?;
|
||||||
super::check(unsafe { uu_portal_key(handle.0.as_ptr(), symbol, i32::from(pressed)) })
|
let keycode = crate::uinput::keysym_to_evdev(u64::from(symbol)).map_or(-1, i32::from);
|
||||||
|
super::check(unsafe { uu_portal_key(handle.0.as_ptr(), symbol, keycode, i32::from(pressed)) })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::{mem::ManuallyDrop, sync::Arc};
|
||||||
|
|
||||||
|
struct Fixture(ManuallyDrop<Session>);
|
||||||
|
impl Fixture {
|
||||||
|
fn new(physical: (u32, u32)) -> Self {
|
||||||
|
let (_, locked) = tokio::sync::watch::channel(None);
|
||||||
|
Self(ManuallyDrop::new(Session {
|
||||||
|
handle: Mutex::new(Handle(NonNull::dangling())),
|
||||||
|
locked,
|
||||||
|
was_locked: std::sync::atomic::AtomicBool::new(false),
|
||||||
|
recovery: Mutex::new(Recovery::default()),
|
||||||
|
width: physical.0,
|
||||||
|
height: physical.1,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn session(&self) -> &Session { &self.0 }
|
||||||
|
}
|
||||||
|
impl Drop for Fixture {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let Session { handle, locked, was_locked, recovery, .. } =
|
||||||
|
unsafe { ManuallyDrop::take(&mut self.0) };
|
||||||
|
let handle = handle.into_inner().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||||
|
std::mem::forget(handle);
|
||||||
|
drop((locked, was_locked, recovery));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn recovery_requires_only_matching_frame_dimensions() {
|
||||||
|
let original = Fixture::new((3840, 2160));
|
||||||
|
let same = Fixture::new((3840, 2160));
|
||||||
|
let width_changed = Fixture::new((2560, 2160));
|
||||||
|
let height_changed = Fixture::new((3840, 1440));
|
||||||
|
|
||||||
|
assert!(original.session().check_geometry(same.session()).is_ok());
|
||||||
|
assert!(original.session().check_geometry(width_changed.session()).is_err());
|
||||||
|
assert!(original.session().check_geometry(height_changed.session()).is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TestHandle {
|
||||||
|
generation: u8,
|
||||||
|
key_held: bool,
|
||||||
|
button_held: bool,
|
||||||
|
retirement: Option<(std::sync::mpsc::Sender<()>, std::sync::mpsc::Receiver<()>)>,
|
||||||
|
}
|
||||||
|
impl Drop for TestHandle {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
if let Some((entered, resume)) = self.retirement.take() {
|
||||||
|
let _ = entered.send(());
|
||||||
|
let _ = resume.recv_timeout(std::time::Duration::from_secs(10));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn release_input(current: Arc<Mutex<TestHandle>>) -> (std::thread::JoinHandle<()>, std::sync::mpsc::Receiver<(u8, bool, bool)>) {
|
||||||
|
let (released, received) = std::sync::mpsc::channel();
|
||||||
|
let worker = std::thread::spawn(move || {
|
||||||
|
let mut handle = current.lock().unwrap();
|
||||||
|
handle.key_held = false;
|
||||||
|
handle.button_held = false;
|
||||||
|
released.send((handle.generation, handle.key_held, handle.button_held)).unwrap();
|
||||||
|
});
|
||||||
|
(worker, received)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn recovery_does_not_block_input_releases_during_open_or_close() {
|
||||||
|
use std::{sync::mpsc::channel, time::Duration};
|
||||||
|
let timeout = Duration::from_secs(3);
|
||||||
|
let (opening, opened) = channel();
|
||||||
|
let (resume_open, wait_open) = channel();
|
||||||
|
let (closing, closed) = channel();
|
||||||
|
let (resume_close, wait_close) = channel();
|
||||||
|
let current = Arc::new(Mutex::new(TestHandle {
|
||||||
|
generation: 1, key_held: true, button_held: true, retirement: Some((closing, wait_close)),
|
||||||
|
}));
|
||||||
|
let retry = Arc::new(Mutex::new(None::<std::time::Instant>));
|
||||||
|
let recovery = {
|
||||||
|
let current = current.clone();
|
||||||
|
let retry = retry.clone();
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
let handle = current.lock().unwrap();
|
||||||
|
let _owner = retry.try_lock().unwrap();
|
||||||
|
replace_handle(¤t, handle, || {
|
||||||
|
opening.send(()).unwrap();
|
||||||
|
wait_open.recv_timeout(Duration::from_secs(10)).unwrap();
|
||||||
|
Ok(Some(TestHandle {
|
||||||
|
generation: 2, key_held: true, button_held: true, retirement: None,
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
};
|
||||||
|
let opening_result = opened.recv_timeout(timeout);
|
||||||
|
let (first_worker, first_release) = release_input(current.clone());
|
||||||
|
let first_result = first_release.recv_timeout(timeout);
|
||||||
|
// A concurrent capture must not wait on recovery while retaining input's mutex.
|
||||||
|
let (contended, contention) = channel();
|
||||||
|
let contender = {
|
||||||
|
let current = current.clone();
|
||||||
|
let retry = retry.clone();
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
let _handle = current.lock().unwrap();
|
||||||
|
contended.send(matches!(retry.try_lock(), Err(TryLockError::WouldBlock))).unwrap();
|
||||||
|
})
|
||||||
|
};
|
||||||
|
let contention_result = contention.recv_timeout(timeout);
|
||||||
|
let _ = resume_open.send(());
|
||||||
|
let closing_result = closed.recv_timeout(timeout);
|
||||||
|
let (second_worker, second_release) = release_input(current.clone());
|
||||||
|
let second_result = second_release.recv_timeout(timeout);
|
||||||
|
// Always release blocked work before asserting, including on regression.
|
||||||
|
let _ = resume_close.send(());
|
||||||
|
let recovered = recovery.join().unwrap();
|
||||||
|
first_worker.join().unwrap();
|
||||||
|
second_worker.join().unwrap();
|
||||||
|
contender.join().unwrap();
|
||||||
|
assert!(opening_result.is_ok() && closing_result.is_ok());
|
||||||
|
assert_eq!(first_result.unwrap(), (1, false, false));
|
||||||
|
assert_eq!(second_result.unwrap(), (2, false, false));
|
||||||
|
assert!(contention_result.unwrap());
|
||||||
|
assert!(recovered.unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn failed_or_deferred_recovery_keeps_the_current_handle_unlocked() {
|
||||||
|
let current = Mutex::new(1);
|
||||||
|
for failure in [false, true] {
|
||||||
|
let handle = current.lock().unwrap();
|
||||||
|
let result = replace_handle(¤t, handle, || {
|
||||||
|
assert_eq!(*current.try_lock().unwrap(), 1);
|
||||||
|
if failure { Err("open failed".into()) } else { Ok(None) }
|
||||||
|
});
|
||||||
|
if failure { assert_eq!(result.unwrap_err(), "open failed"); }
|
||||||
|
else { assert!(!result.unwrap()); }
|
||||||
|
assert_eq!(*current.try_lock().unwrap(), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn terminal_failure_stops_encoder_recovery_until_the_supervisor_succeeds() {
|
||||||
|
let now = std::time::Instant::now();
|
||||||
|
let later = now + std::time::Duration::from_secs(60);
|
||||||
|
let mut recovery = Recovery::default();
|
||||||
|
recovery.failed(now, 3);
|
||||||
|
assert!(!recovery.denied && recovery.failed_until.is_some());
|
||||||
|
recovery.failed(now, 4);
|
||||||
|
assert!(recovery.denied);
|
||||||
|
assert!(!recovery.ready(later, || panic!("denied consent must not be probed again")));
|
||||||
|
recovery.succeeded();
|
||||||
|
assert!(!recovery.denied && recovery.failed_until.is_none() && recovery.retry_at.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn recovery_backoff_skips_lock_probes_until_the_deadline() {
|
||||||
|
use std::{cell::Cell, time::{Duration, Instant}};
|
||||||
|
let now = Instant::now();
|
||||||
|
let probes = Cell::new(0);
|
||||||
|
let unlocked = || { probes.set(probes.get() + 1); true };
|
||||||
|
let mut recovery = Recovery::default();
|
||||||
|
// Initial one-second grace period, polled at up to 144 fps.
|
||||||
|
for millis in 0..1000 {
|
||||||
|
assert!(!recovery.ready(now + Duration::from_millis(millis), unlocked));
|
||||||
|
}
|
||||||
|
assert_eq!(probes.get(), 0);
|
||||||
|
assert!(recovery.ready(now + Duration::from_secs(1), unlocked));
|
||||||
|
assert_eq!(probes.get(), 1);
|
||||||
|
// A failed open schedules the existing five-second retry delay.
|
||||||
|
recovery.retry_at = Some(now + Duration::from_secs(6));
|
||||||
|
for millis in 1000..6000 {
|
||||||
|
assert!(!recovery.ready(now + Duration::from_millis(millis), unlocked));
|
||||||
|
}
|
||||||
|
assert_eq!(probes.get(), 1);
|
||||||
|
// A locked/unknown probe denies opening and is itself throttled.
|
||||||
|
assert!(!recovery.ready(now + Duration::from_secs(6), || { probes.set(2); false }));
|
||||||
|
assert!(!recovery.ready(now + Duration::from_millis(6999), || panic!("early lock probe")));
|
||||||
|
assert!(recovery.ready(now + Duration::from_secs(7), unlocked));
|
||||||
|
assert_eq!(probes.get(), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn lock_observation_survives_unlock_while_recovery_is_opening() {
|
||||||
|
use std::{sync::{atomic::Ordering, mpsc::channel}, time::Duration};
|
||||||
|
let mut fixture = Fixture::new((2, 2));
|
||||||
|
let (locked, watch) = tokio::sync::watch::channel(Some(false));
|
||||||
|
fixture.0.locked = watch;
|
||||||
|
let session = fixture.session();
|
||||||
|
std::thread::scope(|threads| {
|
||||||
|
let (opening, opened) = channel();
|
||||||
|
let (resume, wait) = channel();
|
||||||
|
let recovery = threads.spawn(move || {
|
||||||
|
let _owner = session.recovery.lock().unwrap();
|
||||||
|
opening.send(()).unwrap();
|
||||||
|
wait.recv_timeout(Duration::from_secs(10)).unwrap();
|
||||||
|
});
|
||||||
|
let ready = opened.recv_timeout(Duration::from_secs(3));
|
||||||
|
locked.send_replace(Some(true));
|
||||||
|
session.was_locked.store(true, Ordering::Release);
|
||||||
|
locked.send_replace(Some(false));
|
||||||
|
let (reset, result) = channel();
|
||||||
|
let capture = threads.spawn(move || {
|
||||||
|
let _handle = session.handle.lock().unwrap();
|
||||||
|
assert_eq!(*session.locked.borrow(), Some(false));
|
||||||
|
reset.send(session.take_lock_observation()).unwrap();
|
||||||
|
});
|
||||||
|
let consumed = result.recv_timeout(Duration::from_secs(3));
|
||||||
|
let still_pending = session.was_locked.load(Ordering::Acquire);
|
||||||
|
let _ = resume.send(());
|
||||||
|
recovery.join().unwrap();
|
||||||
|
capture.join().unwrap();
|
||||||
|
assert!(ready.is_ok());
|
||||||
|
assert!(!consumed.unwrap().unwrap());
|
||||||
|
assert!(still_pending);
|
||||||
|
});
|
||||||
|
assert!(session.take_lock_observation().unwrap());
|
||||||
|
assert!(!session.was_locked.load(std::sync::atomic::Ordering::Acquire));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn cancelled_recovery_does_not_touch_the_handle() {
|
||||||
|
let session = Fixture::new((1920, 1080));
|
||||||
|
let cancelled = std::sync::atomic::AtomicBool::new(true);
|
||||||
|
assert_eq!(session.session().recover(&cancelled).unwrap_err().code, 13);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unlocked_replacement_keeps_its_first_static_frame() {
|
||||||
|
let was_locked = std::sync::atomic::AtomicBool::new(true);
|
||||||
|
clear_pause_for_unlocked_replacement(&was_locked, Some(false));
|
||||||
|
assert!(!was_locked.load(std::sync::atomic::Ordering::Acquire));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn blank(frame: &mut [u8]) { for pixel in frame.chunks_exact_mut(4) { pixel.copy_from_slice(&[0, 0, 0, 255]); } }
|
fn blank(frame: &mut [u8]) { for pixel in frame.chunks_exact_mut(4) { pixel.copy_from_slice(&[0, 0, 0, 255]); } }
|
||||||
|
|
||||||
|
fn open_then_install<T>(
|
||||||
|
cancelled: &std::sync::atomic::AtomicBool,
|
||||||
|
open: impl FnOnce() -> Result<T, PortalError>,
|
||||||
|
install: impl FnOnce(T) -> Result<(), PortalError>,
|
||||||
|
) -> Result<(), PortalError> {
|
||||||
|
use std::sync::atomic::Ordering;
|
||||||
|
if cancelled.load(Ordering::Acquire) { return Err(PortalError { code: 13 }); }
|
||||||
|
let replacement = open()?;
|
||||||
|
if cancelled.load(Ordering::Acquire) { return Err(PortalError { code: 13 }); }
|
||||||
|
install(replacement)
|
||||||
|
}
|
||||||
|
|
||||||
fn error(code: i32) -> String {
|
fn error(code: i32) -> String {
|
||||||
let reason = match code {
|
let reason = match code {
|
||||||
1 => "session bus unavailable", 2 => "RemoteDesktop/ScreenCast portal unavailable",
|
1 => "session bus unavailable", 2 => "RemoteDesktop/ScreenCast portal unavailable",
|
||||||
3 => "request failed", 4 => "permission cancelled or denied", 5 => "permission request timed out",
|
3 => "request failed", 4 => "permission cancelled or denied", 5 => "permission request timed out",
|
||||||
6 => "single monitor and keyboard/pointer permission required", 7 => "PipeWire connection failed",
|
6 => "single monitor and keyboard/pointer permission required", 7 => "PipeWire connection failed",
|
||||||
8 => "unsupported frame format or size", 9 => "session closed or stream lost",
|
8 => "unsupported frame format or size", 9 => "session closed or stream lost",
|
||||||
10 => "frame unavailable", 11 => "invalid input or frame buffer", _ => "operation failed",
|
10 => "frame unavailable", 11 => "invalid input or frame buffer",
|
||||||
|
13 => "operation cancelled", 14 => "dimensions changed; reconnect required",
|
||||||
|
_ => "operation failed",
|
||||||
};
|
};
|
||||||
format!("Portal {reason} (code {code})")
|
format!("Portal {reason} (code {code})")
|
||||||
}
|
}
|
||||||
@@ -165,7 +602,17 @@ impl PortalSession {
|
|||||||
pub fn clipboard_poll(&self) -> Option<String> { None }
|
pub fn clipboard_poll(&self) -> Option<String> { None }
|
||||||
pub fn is_alive(&self) -> bool { false }
|
pub fn is_alive(&self) -> bool { false }
|
||||||
pub fn open() -> Result<Self, String> { Err("Portal backend requires Linux and --features portal".into()) }
|
pub fn open() -> Result<Self, String> { Err("Portal backend requires Linux and --features portal".into()) }
|
||||||
|
pub fn open_cancellable(cancelled: &std::sync::atomic::AtomicBool) -> Result<Self, PortalError> {
|
||||||
|
if cancelled.load(std::sync::atomic::Ordering::Acquire) { Err(PortalError { code: 13 }) }
|
||||||
|
else { Err(PortalError { code: 2 }) }
|
||||||
|
}
|
||||||
|
pub fn recover(&self, cancelled: &std::sync::atomic::AtomicBool) -> Result<bool, PortalError> {
|
||||||
|
if cancelled.load(std::sync::atomic::Ordering::Acquire) { Err(PortalError { code: 13 }) }
|
||||||
|
else { Err(PortalError { code: 2 }) }
|
||||||
|
}
|
||||||
|
pub fn pause(&self) {}
|
||||||
pub fn capture(&self, _: &mut [u8]) -> Result<(), String> { Err(error(2)) }
|
pub fn capture(&self, _: &mut [u8]) -> Result<(), String> { Err(error(2)) }
|
||||||
|
pub fn capture_changed(&self, _: &mut [u8], _: &mut Option<FrameVersion>) -> Result<bool, String> { Err(error(2)) }
|
||||||
pub fn move_pointer(&self, _: f64, _: f64, _: bool) -> Result<(), String> { Err(error(2)) }
|
pub fn move_pointer(&self, _: f64, _: f64, _: bool) -> Result<(), String> { Err(error(2)) }
|
||||||
pub fn button(&self, _: i32, _: bool) -> Result<(), String> { Err(error(2)) }
|
pub fn button(&self, _: i32, _: bool) -> Result<(), String> { Err(error(2)) }
|
||||||
pub fn key(&self, _: u64, _: bool) -> Result<(), String> { Err(error(2)) }
|
pub fn key(&self, _: u64, _: bool) -> Result<(), String> { Err(error(2)) }
|
||||||
@@ -195,4 +642,32 @@ mod tests {
|
|||||||
assert!(super::check(4).unwrap_err().contains("cancelled or denied"));
|
assert!(super::check(4).unwrap_err().contains("cancelled or denied"));
|
||||||
assert!(super::check(9).unwrap_err().contains("closed or stream lost"));
|
assert!(super::check(9).unwrap_err().contains("closed or stream lost"));
|
||||||
}
|
}
|
||||||
|
#[test]
|
||||||
|
fn portal_error_display_is_fixed_and_sanitized() {
|
||||||
|
assert_eq!(super::PortalError { code: 13 }.to_string(), "Portal operation cancelled (code 13)");
|
||||||
|
assert_eq!(super::PortalError { code: 14 }.to_string(), "Portal dimensions changed; reconnect required (code 14)");
|
||||||
|
assert_eq!(super::PortalError { code: 123 }.to_string(), "Portal operation failed (code 123)");
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn opening_with_pre_cancelled_flag_returns_cancellation_code() {
|
||||||
|
let cancelled = std::sync::atomic::AtomicBool::new(true);
|
||||||
|
let code = match super::PortalSession::open_cancellable(&cancelled) {
|
||||||
|
Err(error) => error.code,
|
||||||
|
Ok(_) => panic!("pre-cancelled portal open unexpectedly succeeded"),
|
||||||
|
};
|
||||||
|
assert_eq!(code, 13);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn replacement_opens_before_installing_under_the_handle_lock() {
|
||||||
|
let handle = std::sync::Mutex::new(7);
|
||||||
|
let cancelled = std::sync::atomic::AtomicBool::new(false);
|
||||||
|
super::open_then_install(&cancelled, || {
|
||||||
|
assert_eq!(*handle.try_lock().unwrap(), 7);
|
||||||
|
Ok(11)
|
||||||
|
}, |replacement| {
|
||||||
|
*handle.lock().unwrap() = replacement;
|
||||||
|
Ok(())
|
||||||
|
}).unwrap();
|
||||||
|
assert_eq!(*handle.lock().unwrap(), 11);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -75,6 +75,66 @@ pub fn decode_wire(input: &[u8]) -> Result<Vec<WireField<'_>>, WireError> {
|
|||||||
Ok(fields)
|
Ok(fields)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Structure-only rendering for diagnostics: field numbers, wire types, lengths and small
|
||||||
|
/// varints (enums, sizes, fps). String and byte contents are never rendered.
|
||||||
|
pub fn wire_shape(data: &[u8], depth: u8) -> String {
|
||||||
|
let Ok(fields) = decode_wire(data) else { return format!("bytes({})", data.len()) };
|
||||||
|
let parts: Vec<String> = fields.iter().map(|f| match f.value {
|
||||||
|
WireValue::Varint(u64::MAX) => format!("{}=-1", f.number),
|
||||||
|
WireValue::Varint(v) if v <= 0xFFFF => format!("{}={v}", f.number),
|
||||||
|
WireValue::Varint(_) => format!("{}=big", f.number),
|
||||||
|
WireValue::Fixed32(_) => format!("{}:f32", f.number),
|
||||||
|
WireValue::Fixed64(_) => format!("{}:f64", f.number),
|
||||||
|
WireValue::Bytes(b) => format!("{}:{}", f.number, bytes_shape(b, depth)),
|
||||||
|
}).collect();
|
||||||
|
format!("{{{}}}", parts.join(","))
|
||||||
|
}
|
||||||
|
fn bytes_shape(b: &[u8], depth: u8) -> String {
|
||||||
|
let printable = !b.is_empty() && std::str::from_utf8(b).is_ok_and(|t| t.chars().all(|c| !c.is_control()));
|
||||||
|
if printable { return format!("str({})", b.len()); }
|
||||||
|
if depth > 0 && !b.is_empty() && decode_wire(b).is_ok_and(|f| !f.is_empty() && f.iter().all(|x| (1..=128).contains(&x.number))) {
|
||||||
|
return wire_shape(b, depth - 1);
|
||||||
|
}
|
||||||
|
format!("bytes({})", b.len())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// JSON counterpart: keys and value types; strings as lengths (or nested JSON/base64-protobuf
|
||||||
|
/// shape), numbers only when small. Values that could identify a user are never rendered.
|
||||||
|
pub fn json_shape(value: &serde_json::Value, depth: u8) -> String {
|
||||||
|
use base64::Engine;
|
||||||
|
use serde_json::Value;
|
||||||
|
match value {
|
||||||
|
Value::Null => "null".into(),
|
||||||
|
Value::Bool(b) => b.to_string(),
|
||||||
|
Value::Number(n) => match n.as_i64() { Some(v) if v.abs() <= 0xFFFF => v.to_string(), _ => "num".into() },
|
||||||
|
Value::String(t) => {
|
||||||
|
if depth > 0 {
|
||||||
|
if let Ok(inner @ (Value::Object(_) | Value::Array(_))) = serde_json::from_str::<Value>(t) {
|
||||||
|
return format!("json{}", json_shape(&inner, depth - 1));
|
||||||
|
}
|
||||||
|
if t.len() >= 8 && t.len() % 4 == 0 {
|
||||||
|
if let Ok(raw) = base64::engine::general_purpose::STANDARD.decode(t) {
|
||||||
|
if decode_wire(&raw).is_ok_and(|f| !f.is_empty()) { return format!("b64{}", wire_shape(&raw, depth - 1)); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
format!("str({})", t.len())
|
||||||
|
}
|
||||||
|
Value::Array(items) => match items.first() {
|
||||||
|
Some(first) if depth > 0 => format!("[{}x{}]", items.len(), json_shape(first, depth - 1)),
|
||||||
|
_ => format!("[{}]", items.len()),
|
||||||
|
},
|
||||||
|
Value::Object(map) if depth > 0 => {
|
||||||
|
let parts: Vec<String> = map.iter().take(48).map(|(k, v)| {
|
||||||
|
let key: String = k.chars().filter(|c| c.is_ascii_alphanumeric() || *c == '_').take(40).collect();
|
||||||
|
format!("{key}:{}", json_shape(v, depth - 1))
|
||||||
|
}).collect();
|
||||||
|
format!("{{{}}}", parts.join(","))
|
||||||
|
}
|
||||||
|
Value::Object(map) => format!("{{{} keys}}", map.len()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -105,4 +165,20 @@ mod tests {
|
|||||||
assert_eq!(decode_wire(&[11]), Err(WireError::UnsupportedWireType(3)));
|
assert_eq!(decode_wire(&[11]), Err(WireError::UnsupportedWireType(3)));
|
||||||
assert_eq!(decode_wire(&[255, 255, 255, 255, 31]), Err(WireError::InvalidField));
|
assert_eq!(decode_wire(&[255, 255, 255, 255, 31]), Err(WireError::InvalidField));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn shapes_never_render_contents() {
|
||||||
|
// {1:1, 2:-1, 4:{1:60,2:2}, 9:"device-secret", 12:big}
|
||||||
|
let mut data = vec![0x08, 0x01, 0x10];
|
||||||
|
data.extend([0xff; 9]); data.push(0x01);
|
||||||
|
data.extend([0x22, 0x04, 0x08, 0x3c, 0x10, 0x02]);
|
||||||
|
data.extend([0x4a, 0x0d]); data.extend(b"device-secret");
|
||||||
|
data.extend([0x60, 0x80, 0x80, 0x80, 0x80, 0x10]);
|
||||||
|
let shape = super::wire_shape(&data, 3);
|
||||||
|
assert_eq!(shape, "{1=1,2=-1,4:{1=60,2=2},9:str(13),12=big}");
|
||||||
|
let header = serde_json::json!({"streamer_data": "{\"token\":\"secret-value\",\"n\":3}", "city": "Somewhere", "id": 123456789});
|
||||||
|
let shape = super::json_shape(&header, 4);
|
||||||
|
assert!(!shape.contains("secret") && !shape.contains("Somewhere") && !shape.contains("123456789"), "{shape}");
|
||||||
|
assert!(shape.contains("streamer_data:json{n:3,token:str(12)}"), "{shape}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -7,9 +7,22 @@ use webrtc::peer_connection::{configuration::RTCConfiguration,policy::ice_transp
|
|||||||
pub struct SessionIce {
|
pub struct SessionIce {
|
||||||
servers: Vec<RTCIceServer>,
|
servers: Vec<RTCIceServer>,
|
||||||
relay_only: bool,
|
relay_only: bool,
|
||||||
|
#[cfg(test)]
|
||||||
|
pub(crate) setting_engine: Option<webrtc::api::setting_engine::SettingEngine>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SessionIce {
|
impl SessionIce {
|
||||||
|
#[cfg(test)]
|
||||||
|
pub fn local() -> Self {
|
||||||
|
// A supplied empty URL list disables the public STUN defaults in local peer tests.
|
||||||
|
Self { servers: vec![RTCIceServer::default()], ..Default::default() }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub fn virtual_net(setting_engine: webrtc::api::setting_engine::SettingEngine) -> Self {
|
||||||
|
Self { setting_engine: Some(setting_engine), ..Self::local() }
|
||||||
|
}
|
||||||
|
|
||||||
pub fn from_header(header:&Value)->Self {
|
pub fn from_header(header:&Value)->Self {
|
||||||
let data=header.get("data").unwrap_or(header);
|
let data=header.get("data").unwrap_or(header);
|
||||||
let field=|name:&str|header.get(name).or_else(||data.get(name));
|
let field=|name:&str|header.get(name).or_else(||data.get(name));
|
||||||
|
|||||||
+13
-8
@@ -119,7 +119,9 @@ fn unlock_with(password: &str) -> UnlockResult {
|
|||||||
if Command::new("loginctl").args(["unlock-session", &session]).status().map(|s| !s.success()).unwrap_or(true) {
|
if Command::new("loginctl").args(["unlock-session", &session]).status().map(|s| !s.success()).unwrap_or(true) {
|
||||||
return UnlockResult::NoPermission;
|
return UnlockResult::NoPermission;
|
||||||
}
|
}
|
||||||
let _ = crate::capture::keep_display_awake(); // a blanked monitor would keep the stream black
|
if let Err(error) = crate::capture::keep_display_awake() {
|
||||||
|
log::warn!("Display wake after auto unlock failed: {error}");
|
||||||
|
}
|
||||||
// Post-unlock confirmation like the official host: the lock screen must actually go away.
|
// Post-unlock confirmation like the official host: the lock screen must actually go away.
|
||||||
for _ in 0..100 { // GNOME takes ~3 s to drop the shield after logind's Unlock
|
for _ in 0..100 { // GNOME takes ~3 s to drop the shield after logind's Unlock
|
||||||
if session_locked(&session) == Some(false) { return UnlockResult::Success; }
|
if session_locked(&session) == Some(false) { return UnlockResult::Success; }
|
||||||
@@ -134,23 +136,26 @@ type Sequence = std::sync::Arc<std::sync::atomic::AtomicU64>;
|
|||||||
fn now() -> u64 { std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs() }
|
fn now() -> u64 { std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs() }
|
||||||
|
|
||||||
/// Message 15 SystemStateChange { 1: SecureDesktopState } on CONTROL.
|
/// Message 15 SystemStateChange { 1: SecureDesktopState } on CONTROL.
|
||||||
pub async fn send_state(channel: &Channel, sequence: &Sequence, locked: bool) {
|
pub async fn send_state(channel: &Channel, sequence: &Sequence, locked: bool) -> bool {
|
||||||
let seq = sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
let seq = sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||||
let message = crate::initialization::encode_secure_desktop(seq, now(), locked, false, supported());
|
let message = crate::initialization::encode_secure_desktop(seq, now(), locked, false, supported());
|
||||||
if let Err(e) = channel.send(&bytes::Bytes::from(message)).await { log::warn!("Secure desktop state send failed: {e}"); }
|
match channel.send(&bytes::Bytes::from(message)).await {
|
||||||
|
Ok(_) => {
|
||||||
|
log::info!("Secure desktop state sent: locked={} auto_unlock={}", locked, supported());
|
||||||
|
true
|
||||||
|
}
|
||||||
|
Err(e) => { log::warn!("Secure desktop state send failed: {e}"); false }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reports the lock state to one desktop viewer: now, then on every change, until it closes.
|
/// Reports the lock state to one desktop viewer: now, then on every change, until it closes.
|
||||||
pub async fn report_states(channel: Channel, sequence: Sequence) {
|
pub async fn report_states(channel: Channel, sequence: Sequence, mut state: watch::Receiver<Option<bool>>) {
|
||||||
use webrtc::data_channel::data_channel_state::RTCDataChannelState;
|
use webrtc::data_channel::data_channel_state::RTCDataChannelState;
|
||||||
let mut state = watch_locked();
|
|
||||||
let mut sent = None;
|
let mut sent = None;
|
||||||
loop {
|
loop {
|
||||||
let locked = *state.borrow_and_update();
|
let locked = *state.borrow_and_update();
|
||||||
if let Some(locked) = locked.filter(|l| sent != Some(*l)) {
|
if let Some(locked) = locked.filter(|l| sent != Some(*l)) {
|
||||||
send_state(&channel, &sequence, locked).await;
|
if send_state(&channel, &sequence, locked).await { sent = Some(locked); }
|
||||||
log::info!("Secure desktop state sent: locked={} auto_unlock={}", locked, supported());
|
|
||||||
sent = Some(locked);
|
|
||||||
}
|
}
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
changed = state.changed() => if changed.is_err() { return; },
|
changed = state.changed() => if changed.is_err() { return; },
|
||||||
|
|||||||
+92
-5
@@ -32,13 +32,16 @@ pub struct SoacMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub enum SignalingEvent {
|
pub enum SignalingEvent {
|
||||||
|
Stop,
|
||||||
|
AssistCommand(crate::assist::Command),
|
||||||
|
AssistTick,
|
||||||
Connected,
|
Connected,
|
||||||
RoomInfoAck(Value),
|
RoomInfoAck(Value),
|
||||||
DeviceInfoChanged(Value),
|
DeviceInfoChanged(Value),
|
||||||
BeControlled(Value),
|
BeControlled(Value),
|
||||||
/// be-controlled with its binary app_data (ConnectOptions): capture type and the
|
/// be-controlled with its binary app_data (ConnectOptions): capture type and the
|
||||||
/// ice_id it announces, when present.
|
/// ice_id it announces, when present.
|
||||||
ConnectRequest { capture_type: u64, ice_id: Option<String>, transport:crate::session_ice::SessionIce },
|
ConnectRequest { capture_type: u64, ice_id: Option<String>, transport:crate::session_ice::SessionIce, controller_mac: bool },
|
||||||
Soac(SoacMessage),
|
Soac(SoacMessage),
|
||||||
Released(Value),
|
Released(Value),
|
||||||
Left(Value),
|
Left(Value),
|
||||||
@@ -51,6 +54,13 @@ pub struct SignalingClient {
|
|||||||
_reconnect_key: Arc<std::sync::Mutex<Option<String>>>,
|
_reconnect_key: Arc<std::sync::Mutex<Option<String>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
impl SignalingClient {
|
||||||
|
pub fn disconnected() -> Arc<Self> {
|
||||||
|
Arc::new(Self { tx_outgoing: mpsc::channel(1).0, _reconnect_key: Arc::new(std::sync::Mutex::new(None)) })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn refreshed_key(ack:&Value)->Option<String> {
|
fn refreshed_key(ack:&Value)->Option<String> {
|
||||||
let values=ack.as_array()?;
|
let values=ack.as_array()?;
|
||||||
if values.first()?.as_str()? != "success" { return None; }
|
if values.first()?.as_str()? != "success" { return None; }
|
||||||
@@ -92,6 +102,30 @@ mod refresh_tests {
|
|||||||
// Retain it until after the server sees closure, just like PC callbacks.
|
// Retain it until after the server sees closure, just like PC callbacks.
|
||||||
drop(retained);
|
drop(retained);
|
||||||
}
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn silent_half_open_socket_is_reported_disconnected() {
|
||||||
|
use futures_util::{SinkExt,StreamExt};
|
||||||
|
use tokio_tungstenite::tungstenite::Message;
|
||||||
|
let listener=tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||||
|
let address=listener.local_addr().unwrap();
|
||||||
|
let (hold_tx,hold_rx)=tokio::sync::oneshot::channel::<()>();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let (tcp,_)=listener.accept().await.unwrap();
|
||||||
|
let mut socket=tokio_tungstenite::accept_async(tcp).await.unwrap();
|
||||||
|
// Shortest accepted window (clamped to 5 s); the server then never pings.
|
||||||
|
socket.send(Message::Text(r#"0{"pingInterval":1000,"pingTimeout":1000}"#.into())).await.unwrap();
|
||||||
|
let _=socket.next().await;
|
||||||
|
socket.send(Message::Text("40{}".into())).await.unwrap();
|
||||||
|
let _=hold_rx.await; // keep the TCP connection open but silent
|
||||||
|
});
|
||||||
|
let (sender,mut events)=tokio::sync::mpsc::channel(8);
|
||||||
|
let _client=super::SignalingClient::connect(&format!("ws://{address}"),"synthetic-only",sender).await.unwrap();
|
||||||
|
let disconnected=tokio::time::timeout(std::time::Duration::from_secs(8),async {
|
||||||
|
loop { if matches!(events.recv().await,Some(super::SignalingEvent::Disconnected)|None) { return; } }
|
||||||
|
}).await;
|
||||||
|
assert!(disconnected.is_ok(),"a socket without pings must be reported dead");
|
||||||
|
drop(hold_tx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SignalingClient {
|
impl SignalingClient {
|
||||||
@@ -119,7 +153,7 @@ impl SignalingClient {
|
|||||||
.await
|
.await
|
||||||
.map_err(|e| format!("WS connect failed: {}", e))?;
|
.map_err(|e| format!("WS connect failed: {}", e))?;
|
||||||
|
|
||||||
log::info!("Connected to Engine.IO / Socket.IO at {}", ws_url);
|
log::info!("Connected to Engine.IO / Socket.IO");
|
||||||
let (mut write, mut read) = ws_stream.split();
|
let (mut write, mut read) = ws_stream.split();
|
||||||
let (tx_outgoing, mut rx_outgoing) = mpsc::channel::<String>(32);
|
let (tx_outgoing, mut rx_outgoing) = mpsc::channel::<String>(32);
|
||||||
|
|
||||||
@@ -127,7 +161,8 @@ impl SignalingClient {
|
|||||||
let writer=tokio::spawn(async move {
|
let writer=tokio::spawn(async move {
|
||||||
while let Some(msg) = rx_outgoing.recv().await {
|
while let Some(msg) = rx_outgoing.recv().await {
|
||||||
if let Err(e) = write.send(Message::Text(msg)).await {
|
if let Err(e) = write.send(Message::Text(msg)).await {
|
||||||
log::warn!("WS send error: {}", e);
|
let _ = e;
|
||||||
|
log::warn!("WS send failed (details omitted)");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -152,9 +187,22 @@ impl SignalingClient {
|
|||||||
let mut accepted_refresh=0;
|
let mut accepted_refresh=0;
|
||||||
// Socket.IO binary event awaiting attachments: (event name, header JSON).
|
// Socket.IO binary event awaiting attachments: (event name, header JSON).
|
||||||
let mut pending_binary: Option<(String, Value)> = None;
|
let mut pending_binary: Option<(String, Value)> = None;
|
||||||
|
// Relation check only: the be-controlled ice_id versus the next offer's data.ice_id.
|
||||||
|
let mut last_connect_ice: Option<String> = None;
|
||||||
|
let mut last_connect_client: Option<String> = None;
|
||||||
|
// Engine.IO 4: a client treats the connection as dead when no ping arrives within
|
||||||
|
// pingInterval + pingTimeout. A half-open TCP socket (suspend, NAT drop) otherwise
|
||||||
|
// blocks the read forever and the host silently stays offline.
|
||||||
|
let mut ping_window=std::time::Duration::from_secs(25+20);
|
||||||
|
let mut ping_deadline=tokio::time::Instant::now()+ping_window;
|
||||||
loop {
|
loop {
|
||||||
let msg_res=tokio::select! {
|
let msg_res=tokio::select! {
|
||||||
_=event_sender.closed()=>break,
|
_=event_sender.closed()=>break,
|
||||||
|
_=tokio::time::sleep_until(ping_deadline) => {
|
||||||
|
log::warn!("Signaling heartbeat lost: generation={} age_s={} pings={} last_ping_gap_s={}",generation,started.elapsed().as_secs(),pings,last_ping.elapsed().as_secs());
|
||||||
|
let _ = event_sender.send(SignalingEvent::Disconnected).await;
|
||||||
|
break;
|
||||||
|
},
|
||||||
// Official ControlledSession RefreshSignalTokenLoop: first
|
// Official ControlledSession RefreshSignalTokenLoop: first
|
||||||
// 30s, success every ~300s, failure retry after 30s. Native
|
// 30s, success every ~300s, failure retry after 30s. Native
|
||||||
// libstreamer 0x885774 / 0x885e58; emit has NO payload args.
|
// libstreamer 0x885774 / 0x885e58; emit has NO payload args.
|
||||||
@@ -181,12 +229,17 @@ impl SignalingClient {
|
|||||||
if txt.starts_with('0') {
|
if txt.starts_with('0') {
|
||||||
if let Ok(open)=serde_json::from_str::<Value>(&txt[1..]) {
|
if let Ok(open)=serde_json::from_str::<Value>(&txt[1..]) {
|
||||||
log::info!("Signaling heartbeat config: generation={} ping_interval_ms={:?} ping_timeout_ms={:?}",generation,open.get("pingInterval").and_then(Value::as_u64),open.get("pingTimeout").and_then(Value::as_u64));
|
log::info!("Signaling heartbeat config: generation={} ping_interval_ms={:?} ping_timeout_ms={:?}",generation,open.get("pingInterval").and_then(Value::as_u64),open.get("pingTimeout").and_then(Value::as_u64));
|
||||||
|
if let (Some(interval),Some(timeout))=(open.get("pingInterval").and_then(Value::as_u64),open.get("pingTimeout").and_then(Value::as_u64)) {
|
||||||
|
ping_window=std::time::Duration::from_millis(interval.saturating_add(timeout).clamp(5_000,300_000));
|
||||||
|
ping_deadline=tokio::time::Instant::now()+ping_window;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Open packet -> 发送 Socket.IO connect
|
// Open packet -> 发送 Socket.IO connect
|
||||||
let _ = tx_out_clone.send("40".into()).await;
|
let _ = tx_out_clone.send("40".into()).await;
|
||||||
} else if txt == "2" {
|
} else if txt == "2" {
|
||||||
// Ping -> 回复 Pong
|
// Ping -> 回复 Pong
|
||||||
let gap=last_ping.elapsed().as_secs();last_ping=std::time::Instant::now();pings+=1;
|
let gap=last_ping.elapsed().as_secs();last_ping=std::time::Instant::now();pings+=1;
|
||||||
|
ping_deadline=tokio::time::Instant::now()+ping_window;
|
||||||
if tx_out_clone.send("3".into()).await.is_ok() { pong_queued+=1; }
|
if tx_out_clone.send("3".into()).await.is_ok() { pong_queued+=1; }
|
||||||
if pings==1 || pings%12==0 {
|
if pings==1 || pings%12==0 {
|
||||||
log::info!("Signaling heartbeat progress: generation={} age_s={} pings={} pong_queued={} gap_s={}",generation,started.elapsed().as_secs(),pings,pong_queued,gap);
|
log::info!("Signaling heartbeat progress: generation={} age_s={} pings={} pong_queued={} gap_s={}",generation,started.elapsed().as_secs(),pings,pong_queued,gap);
|
||||||
@@ -251,6 +304,15 @@ impl SignalingClient {
|
|||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
"soac" => {
|
"soac" => {
|
||||||
|
// Offers only: structure (keys, types, lengths), never SDP text or ids.
|
||||||
|
let kind = data.get("data").unwrap_or(data).get("type").and_then(Value::as_str);
|
||||||
|
if kind == Some("offer") {
|
||||||
|
log::info!("SOAC offer shape: {}", crate::protocol::json_shape(data, 3));
|
||||||
|
let offer_ice = data.get("data").unwrap_or(data).get("ice_id").and_then(Value::as_str);
|
||||||
|
log::info!("SOAC offer ice_id equals be-controlled ice_id: {:?}", last_connect_ice.as_deref().zip(offer_ice).map(|(a, b)| a == b));
|
||||||
|
let offer_client = data.get("client_id").and_then(Value::as_str);
|
||||||
|
log::info!("SOAC offer client_id equals be-controlled client_id: {:?}", last_connect_client.as_deref().zip(offer_client).map(|(a, b)| a == b));
|
||||||
|
}
|
||||||
let (target_val, client_id_opt) = if let Some(inner) = data.get("data") {
|
let (target_val, client_id_opt) = if let Some(inner) = data.get("data") {
|
||||||
(inner.clone(), data.get("client_id").and_then(|v| v.as_str()).map(|s| s.to_string()))
|
(inner.clone(), data.get("client_id").and_then(|v| v.as_str()).map(|s| s.to_string()))
|
||||||
} else {
|
} else {
|
||||||
@@ -267,7 +329,8 @@ impl SignalingClient {
|
|||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Failed to deserialize SoacMessage: error={}, raw={}", e, data);
|
let _ = e;
|
||||||
|
log::error!("Failed to deserialize SoacMessage (payload omitted)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -312,7 +375,31 @@ impl SignalingClient {
|
|||||||
let ice_id = ["ice_id", "iceId"].iter().find_map(|k| header.get(*k).or_else(|| header.get("data").and_then(|d| d.get(*k))))
|
let ice_id = ["ice_id", "iceId"].iter().find_map(|k| header.get(*k).or_else(|| header.get("data").and_then(|d| d.get(*k))))
|
||||||
.and_then(|v| v.as_str()).map(str::to_owned);
|
.and_then(|v| v.as_str()).map(str::to_owned);
|
||||||
log::info!("be-controlled connect request: capture_type={} header_keys={:?} ice_id_present={}", capture_type, keys, ice_id.is_some());
|
log::info!("be-controlled connect request: capture_type={} header_keys={:?} ice_id_present={}", capture_type, keys, ice_id.is_some());
|
||||||
let _ = event_sender.send(SignalingEvent::ConnectRequest { capture_type, ice_id, transport:crate::session_ice::SessionIce::from_header(&header) }).await;
|
last_connect_ice = ice_id.clone();
|
||||||
|
last_connect_client = header.get("client_id").and_then(Value::as_str).map(str::to_owned);
|
||||||
|
// Structure only (numbers, types, lengths): documents what the viewer and cloud send.
|
||||||
|
let header_shape: String = crate::protocol::json_shape(&header, 7).chars().take(8000).collect();
|
||||||
|
log::info!("Connect request shape: app_data={} header={}", crate::protocol::wire_shape(data, 3), header_shape);
|
||||||
|
// Relation only, never the ids themselves.
|
||||||
|
let control_id = header.get("streamer_data").and_then(Value::as_str)
|
||||||
|
.and_then(|t| serde_json::from_str::<Value>(t).ok())
|
||||||
|
.and_then(|v| v.get("control_id").and_then(Value::as_str).map(str::to_owned));
|
||||||
|
let app_control_id = header.get("app_control_id").and_then(Value::as_str);
|
||||||
|
log::info!("Connect request ids: streamer_control_id_equals_app_control_id={:?}",
|
||||||
|
control_id.as_deref().zip(app_control_id).map(|(a, b)| a == b));
|
||||||
|
// ConnectOptions 4 decoder_cap_list: DecoderCap {1 fps, 2 codec_type (1 H264, 2 H265),
|
||||||
|
// 3 width, 4 height, 5 chroma_format (1 4:2:0, 3 4:4:4)}. Numbers only.
|
||||||
|
let caps: Vec<Vec<(u32, u64)>> = fields.iter().flatten().filter(|f| f.number == 4).filter_map(|f| match f.value {
|
||||||
|
crate::protocol::WireValue::Bytes(b) => crate::protocol::decode_wire(b).ok().map(|cap| cap.iter().filter_map(|x| match x.value {
|
||||||
|
crate::protocol::WireValue::Varint(v) => Some((x.number, v)), _ => None }).collect()),
|
||||||
|
_ => None }).collect();
|
||||||
|
if !caps.is_empty() { log::info!("Viewer decoder caps (fps,codec,w,h,chroma fields): {:?}", caps); }
|
||||||
|
// Viewer platform (website enum: 1 windows, 2 android, 3 ios, 4 macos, 5 tv).
|
||||||
|
let platform = header.get("controller_platform").or_else(|| header.get("data").and_then(|d| d.get("controller_platform")));
|
||||||
|
let controller_mac = platform.is_some_and(|v| v.as_i64() == Some(4)
|
||||||
|
|| v.as_str().is_some_and(|s| s == "4" || s.to_ascii_lowercase().contains("mac") || s.to_ascii_lowercase().contains("darwin")));
|
||||||
|
log::info!("Controller platform: {} mac={}", platform.map(|v| v.to_string()).unwrap_or_default().chars().take(24).collect::<String>(), controller_mac);
|
||||||
|
let _ = event_sender.send(SignalingEvent::ConnectRequest { capture_type, ice_id, transport:crate::session_ice::SessionIce::from_header(&header), controller_mac }).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,6 +61,8 @@ fn save(settings: &Settings) {
|
|||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct PeerActions { started: AtomicBool }
|
pub struct PeerActions { started: AtomicBool }
|
||||||
|
|
||||||
|
/// Power actions pass --no-ask-password: without polkit permission (e.g. no active local session) systemctl
|
||||||
|
/// would otherwise wait for an authentication dialog on the controlled screen; fail and log instead.
|
||||||
fn quiet(program: &str, args: &[&str]) -> bool {
|
fn quiet(program: &str, args: &[&str]) -> bool {
|
||||||
Command::new(program).args(args).stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null())
|
Command::new(program).args(args).stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null())
|
||||||
.status().map(|s| s.success()).unwrap_or(false)
|
.status().map(|s| s.success()).unwrap_or(false)
|
||||||
@@ -135,8 +137,8 @@ impl PeerActions {
|
|||||||
let ok = detached("gnome-system-monitor", &[]) || detached("plasma-systemmonitor", &[]) || detached("xfce4-taskmanager", &[]);
|
let ok = detached("gnome-system-monitor", &[]) || detached("plasma-systemmonitor", &[]) || detached("xfce4-taskmanager", &[]);
|
||||||
log::info!("System action show_taskmgr: ok={}", ok); return false
|
log::info!("System action show_taskmgr: ok={}", ok); return false
|
||||||
}
|
}
|
||||||
REBOOT_SYSTEM => { log::warn!("System action reboot_system requested by viewer"); log::info!("reboot ok={}", quiet("systemctl", &["reboot"])); return false }
|
REBOOT_SYSTEM => { log::warn!("System action reboot_system requested by viewer"); log::info!("reboot ok={}", quiet("systemctl", &["--no-ask-password", "reboot"])); return false }
|
||||||
SHUTDOWN_SYSTEM => { log::warn!("System action shutdown_system requested by viewer"); log::info!("poweroff ok={}", quiet("systemctl", &["poweroff"])); return false }
|
SHUTDOWN_SYSTEM => { log::warn!("System action shutdown_system requested by viewer"); log::info!("poweroff ok={}", quiet("systemctl", &["--no-ask-password", "poweroff"])); return false }
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
let mut state = state().lock().unwrap();
|
let mut state = state().lock().unwrap();
|
||||||
|
|||||||
+102
-12
@@ -34,6 +34,7 @@ const MAGIC: &[u8; 4] = b"TERM";
|
|||||||
const HEADER: usize = 16;
|
const HEADER: usize = 16;
|
||||||
const MAX_PAYLOAD: usize = 0x10_0000;
|
const MAX_PAYLOAD: usize = 0x10_0000;
|
||||||
const SCROLLBACK: usize = 256 * 1024;
|
const SCROLLBACK: usize = 256 * 1024;
|
||||||
|
const OUTPUT_CHUNK: usize = 16 * 1024;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct Frame { pub kind: u8, pub flags: [u8; 2], pub session: u32, pub payload: Vec<u8> }
|
pub struct Frame { pub kind: u8, pub flags: [u8; 2], pub session: u32, pub payload: Vec<u8> }
|
||||||
@@ -70,9 +71,24 @@ struct Session {
|
|||||||
pid: i32,
|
pid: i32,
|
||||||
sink: Mutex<Option<UnboundedSender<Vec<u8>>>>,
|
sink: Mutex<Option<UnboundedSender<Vec<u8>>>>,
|
||||||
scrollback: Mutex<VecDeque<u8>>,
|
scrollback: Mutex<VecDeque<u8>>,
|
||||||
|
size: Mutex<(u16, u16)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Session {
|
impl Session {
|
||||||
|
/// Lock order sink → scrollback matches OPEN, so a reattach snapshot sees each
|
||||||
|
/// chunk either in the replay or live, never both.
|
||||||
|
fn record_output(&self, data: &[u8]) {
|
||||||
|
let mut sink = self.sink.lock().unwrap();
|
||||||
|
{
|
||||||
|
let mut sb = self.scrollback.lock().unwrap();
|
||||||
|
sb.extend(data);
|
||||||
|
let excess = sb.len().saturating_sub(SCROLLBACK);
|
||||||
|
sb.drain(..excess);
|
||||||
|
}
|
||||||
|
if let Some(tx) = sink.as_ref() {
|
||||||
|
if tx.send(encode(STDOUT, self.id, data)).is_err() { *sink = None; }
|
||||||
|
}
|
||||||
|
}
|
||||||
fn emit(&self, kind: u8, payload: &[u8]) -> bool {
|
fn emit(&self, kind: u8, payload: &[u8]) -> bool {
|
||||||
let mut sink = self.sink.lock().unwrap();
|
let mut sink = self.sink.lock().unwrap();
|
||||||
if let Some(tx) = sink.as_ref() {
|
if let Some(tx) = sink.as_ref() {
|
||||||
@@ -81,10 +97,31 @@ impl Session {
|
|||||||
}
|
}
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
// The size lock is held across the ioctl so a delayed restore cannot reapply a stale size.
|
||||||
fn resize(&self, cols: u16, rows: u16) {
|
fn resize(&self, cols: u16, rows: u16) {
|
||||||
|
let mut size = self.size.lock().unwrap();
|
||||||
|
*size = (cols, rows);
|
||||||
|
self.set_winsize(cols, rows);
|
||||||
|
}
|
||||||
|
fn set_winsize(&self, cols: u16, rows: u16) {
|
||||||
let win = libc::winsize { ws_row: rows, ws_col: cols, ws_xpixel: 0, ws_ypixel: 0 };
|
let win = libc::winsize { ws_row: rows, ws_col: cols, ws_xpixel: 0, ws_ypixel: 0 };
|
||||||
unsafe { libc::ioctl(self.master.as_raw_fd(), libc::TIOCSWINSZ as _, &win); }
|
unsafe { libc::ioctl(self.master.as_raw_fd(), libc::TIOCSWINSZ as _, &win); }
|
||||||
}
|
}
|
||||||
|
/// Full-screen programs repaint only on SIGWINCH, which an unchanged size never raises:
|
||||||
|
/// shrink by one row, then restore the latest requested size after a pause.
|
||||||
|
fn force_redraw(self: &Arc<Self>) {
|
||||||
|
{
|
||||||
|
let size = self.size.lock().unwrap();
|
||||||
|
if size.1 <= 1 { return; }
|
||||||
|
self.set_winsize(size.0, size.1 - 1);
|
||||||
|
}
|
||||||
|
let s = self.clone();
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(150));
|
||||||
|
let size = s.size.lock().unwrap();
|
||||||
|
s.set_winsize(size.0, size.1);
|
||||||
|
});
|
||||||
|
}
|
||||||
fn write(&self, data: &[u8]) {
|
fn write(&self, data: &[u8]) {
|
||||||
let mut off = 0;
|
let mut off = 0;
|
||||||
while off < data.len() {
|
while off < data.len() {
|
||||||
@@ -136,23 +173,15 @@ fn spawn(requested_id: u32, cols: u16, rows: u16, shell: Option<&str>) -> Result
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
let session = Arc::new(Session { id, shell: shell.clone(), created_ms: now_ms(), pid: child.id() as i32,
|
let session = Arc::new(Session { id, shell: shell.clone(), created_ms: now_ms(), pid: child.id() as i32,
|
||||||
master: pty.master, sink: Mutex::new(None), scrollback: Mutex::new(VecDeque::new()) });
|
master: pty.master, sink: Mutex::new(None), scrollback: Mutex::new(VecDeque::new()), size: Mutex::new((cols, rows)) });
|
||||||
let mut reader = std::fs::File::from(session.master.try_clone().map_err(|e| format!("pty dup failed: {e}"))?);
|
let mut reader = std::fs::File::from(session.master.try_clone().map_err(|e| format!("pty dup failed: {e}"))?);
|
||||||
let worker = session.clone();
|
let worker = session.clone();
|
||||||
std::thread::Builder::new().name(format!("uu-term-{id}")).spawn(move || {
|
std::thread::Builder::new().name(format!("uu-term-{id}")).spawn(move || {
|
||||||
let mut buf = vec![0u8; 16 * 1024];
|
let mut buf = vec![0u8; OUTPUT_CHUNK];
|
||||||
loop {
|
loop {
|
||||||
match reader.read(&mut buf) {
|
match reader.read(&mut buf) {
|
||||||
Ok(0) | Err(_) => break,
|
Ok(0) | Err(_) => break,
|
||||||
Ok(n) => {
|
Ok(n) => worker.record_output(&buf[..n]),
|
||||||
{
|
|
||||||
let mut sb = worker.scrollback.lock().unwrap();
|
|
||||||
sb.extend(&buf[..n]);
|
|
||||||
let excess = sb.len().saturating_sub(SCROLLBACK);
|
|
||||||
sb.drain(..excess);
|
|
||||||
}
|
|
||||||
worker.emit(STDOUT, &buf[..n]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let code = child.wait().ok().and_then(|s| s.code()).unwrap_or(-1);
|
let code = child.wait().ok().and_then(|s| s.code()).unwrap_or(-1);
|
||||||
@@ -211,6 +240,7 @@ impl TerminalConn {
|
|||||||
OPEN => {
|
OPEN => {
|
||||||
let v = json(&frame.payload);
|
let v = json(&frame.payload);
|
||||||
let (cols, rows) = dims(&v);
|
let (cols, rows) = dims(&v);
|
||||||
|
let reattach = existing.is_some();
|
||||||
let session = match existing {
|
let session = match existing {
|
||||||
Some(s) => { s.resize(cols, rows); Ok(s) }
|
Some(s) => { s.resize(cols, rows); Ok(s) }
|
||||||
None => spawn(frame.session, cols, rows, v.get("shell").and_then(|s| s.as_str())),
|
None => spawn(frame.session, cols, rows, v.get("shell").and_then(|s| s.as_str())),
|
||||||
@@ -222,7 +252,11 @@ impl TerminalConn {
|
|||||||
self.send(OPEN_RESP, s.id, &reply(0, "ok"));
|
self.send(OPEN_RESP, s.id, &reply(0, "ok"));
|
||||||
*sink = Some(self.out.clone());
|
*sink = Some(self.out.clone());
|
||||||
let replay: Vec<u8> = s.scrollback.lock().unwrap().iter().copied().collect();
|
let replay: Vec<u8> = s.scrollback.lock().unwrap().iter().copied().collect();
|
||||||
if !replay.is_empty() { self.send(STDOUT, s.id, &replay); }
|
// A full scrollback exceeds SCTP's 64 KiB outbound message limit.
|
||||||
|
for chunk in replay.chunks(OUTPUT_CHUNK) { self.send(STDOUT, s.id, chunk); }
|
||||||
|
drop(sink);
|
||||||
|
// The replay tail starts mid-redraw for full-screen programs.
|
||||||
|
if reattach { s.force_redraw(); }
|
||||||
}
|
}
|
||||||
Err(e) => { log::warn!("Terminal open failed: {}", e); self.send(OPEN_RESP, frame.session, &reply(1, &e)); }
|
Err(e) => { log::warn!("Terminal open failed: {}", e); self.send(OPEN_RESP, frame.session, &reply(1, &e)); }
|
||||||
}
|
}
|
||||||
@@ -289,6 +323,62 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
|
fn force_redraw_shrinks_then_restores_latest_size() {
|
||||||
|
let pty = nix::pty::openpty(None, None).unwrap();
|
||||||
|
let session = Arc::new(Session { id: u32::MAX - 2, shell: "/bin/sh".into(), created_ms: now_ms(),
|
||||||
|
master: pty.master, pid: 0, sink: Mutex::new(None),
|
||||||
|
scrollback: Mutex::new(VecDeque::new()), size: Mutex::new((80, 24)) });
|
||||||
|
let rows = |s: &Session| {
|
||||||
|
let mut win = libc::winsize { ws_row: 0, ws_col: 0, ws_xpixel: 0, ws_ypixel: 0 };
|
||||||
|
unsafe { libc::ioctl(s.master.as_raw_fd(), libc::TIOCGWINSZ as _, &mut win); }
|
||||||
|
(win.ws_col, win.ws_row)
|
||||||
|
};
|
||||||
|
session.resize(100, 30);
|
||||||
|
session.force_redraw();
|
||||||
|
assert_eq!(rows(&session), (100, 29));
|
||||||
|
session.resize(120, 40);
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(400));
|
||||||
|
assert_eq!(rows(&session), (120, 40), "restore must use the latest requested size");
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn reconnect_replays_large_history_in_bounded_frames() {
|
||||||
|
let pty = nix::pty::openpty(None, None).unwrap();
|
||||||
|
let id = u32::MAX - 1;
|
||||||
|
let history: Vec<u8> = (0..SCROLLBACK).map(|n| (n % 251) as u8).collect();
|
||||||
|
let session = Arc::new(Session { id, shell: "/bin/sh".into(), created_ms: now_ms(),
|
||||||
|
master: pty.master, pid: 0, sink: Mutex::new(None),
|
||||||
|
scrollback: Mutex::new(history.iter().copied().collect()), size: Mutex::new((80, 24)) });
|
||||||
|
registry().lock().unwrap().insert(id, session.clone());
|
||||||
|
struct Cleanup(u32);
|
||||||
|
impl Drop for Cleanup { fn drop(&mut self) { registry().lock().unwrap().remove(&self.0); } }
|
||||||
|
let _cleanup = Cleanup(id);
|
||||||
|
|
||||||
|
for _ in 0..2 {
|
||||||
|
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
|
||||||
|
let conn = TerminalConn::new(tx);
|
||||||
|
conn.handle(&encode(OPEN, id, br#"{"cols":80,"rows":24}"#));
|
||||||
|
let opened = decode(&rx.try_recv().unwrap()).unwrap();
|
||||||
|
assert_eq!(opened.kind, OPEN_RESP);
|
||||||
|
assert_eq!(opened.session, id);
|
||||||
|
let mut replay = Vec::new();
|
||||||
|
while replay.len() < history.len() {
|
||||||
|
let packet = rx.try_recv().unwrap();
|
||||||
|
// SCTP's default outbound limit includes the TERM header.
|
||||||
|
assert!(packet.len() <= 65536, "terminal replay exceeds the SCTP message limit");
|
||||||
|
let frame = decode(&packet).unwrap();
|
||||||
|
assert_eq!(frame.kind, STDOUT);
|
||||||
|
assert_eq!(frame.session, id);
|
||||||
|
replay.extend(frame.payload);
|
||||||
|
}
|
||||||
|
assert!(replay == history, "terminal replay must preserve every byte in order");
|
||||||
|
assert!(session.emit(STDOUT, b"live output"));
|
||||||
|
assert_eq!(decode(&rx.try_recv().unwrap()).unwrap().payload, b"live output");
|
||||||
|
conn.handle(&encode(LIST_REQ, 7, &[]));
|
||||||
|
assert_eq!(decode(&rx.try_recv().unwrap()).unwrap().kind, LIST_RESP);
|
||||||
|
conn.handle(&encode(DETACH, id, &[]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
fn open_spawns_shell_and_streams_output() {
|
fn open_spawns_shell_and_streams_output() {
|
||||||
for requested_id in [0, u32::MAX] {
|
for requested_id in [0, u32::MAX] {
|
||||||
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
|
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
use webrtc::{api::setting_engine::SettingEngine, ice::{mdns::MulticastDnsMode, network_type::NetworkType},
|
||||||
|
util::vnet::{net::{Net, NetConfig}, router::{Router, RouterConfig}}};
|
||||||
|
|
||||||
|
pub(crate) async fn virtual_network() -> (SettingEngine, SettingEngine, Arc<Mutex<Router>>) {
|
||||||
|
// Each pair owns its router and address space; no host interfaces or sockets are used.
|
||||||
|
let router = Arc::new(Mutex::new(Router::new(RouterConfig {
|
||||||
|
cidr: "192.0.2.0/24".into(), ..Default::default()
|
||||||
|
}).unwrap()));
|
||||||
|
let mut settings = Vec::new();
|
||||||
|
for ip in ["192.0.2.1", "192.0.2.2"] {
|
||||||
|
let net = Arc::new(Net::new(Some(NetConfig { static_ips: vec![ip.into()], ..Default::default() })));
|
||||||
|
let nic = net.get_nic().unwrap();
|
||||||
|
router.lock().await.add_net(nic.clone()).await.unwrap();
|
||||||
|
nic.lock().await.set_router(router.clone()).await.unwrap();
|
||||||
|
let mut setting = SettingEngine::default();
|
||||||
|
setting.set_vnet(Some(net));
|
||||||
|
setting.set_network_types(vec![NetworkType::Udp4]);
|
||||||
|
setting.set_ice_multicast_dns_mode(MulticastDnsMode::Disabled);
|
||||||
|
settings.push(setting);
|
||||||
|
}
|
||||||
|
router.lock().await.start().await.unwrap();
|
||||||
|
let right = settings.pop().unwrap();
|
||||||
|
(settings.pop().unwrap(), right, router)
|
||||||
|
}
|
||||||
+737
-104
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,174 @@
|
|||||||
|
//! One negotiated video m-line/SSRC, with codec-specific payload types.
|
||||||
|
//! A single RTP sequence/timestamp survives format changes (never two independent SSRC timelines).
|
||||||
|
use std::{any::Any, sync::atomic::{AtomicBool, Ordering}};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
use webrtc::{error::{Error, Result}, media::Sample, rtp::{packet::Packet, header::Header, packetizer::Payloader}};
|
||||||
|
use webrtc::rtp_transceiver::rtp_codec::{RTCRtpCodecCapability, RTCRtpCodecParameters, RTPCodecType};
|
||||||
|
use webrtc::track::track_local::{TrackLocal, TrackLocalContext, track_local_static_rtp::TrackLocalStaticRTP};
|
||||||
|
use crate::capture_setting::VideoCodec;
|
||||||
|
|
||||||
|
/// Official SDK GetSupportedFormats constructs H265 with an empty parameter map.
|
||||||
|
pub fn register_hevc(media: &mut webrtc::api::media_engine::MediaEngine) -> Result<()> {
|
||||||
|
media.register_codec(RTCRtpCodecParameters { capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: VideoCodec::H265.mime().into(), clock_rate: 90_000,
|
||||||
|
rtcp_feedback: vec![webrtc::rtp_transceiver::RTCPFeedback { typ: "nack".into(), parameter: String::new() },
|
||||||
|
webrtc::rtp_transceiver::RTCPFeedback { typ: "nack".into(), parameter: "pli".into() }],
|
||||||
|
..Default::default() }, payload_type: 126, ..Default::default() }, RTPCodecType::Video)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Timeline { sequence: u16, timestamp: u32, h264: webrtc::rtp::codecs::h264::H264Payloader }
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct VideoTrack {
|
||||||
|
h264: TrackLocalStaticRTP, h265: TrackLocalStaticRTP,
|
||||||
|
has_h264: AtomicBool, has_h265: AtomicBool, timeline: Mutex<Timeline>,
|
||||||
|
progress: tokio::sync::watch::Sender<(u64, VideoCodec)>,
|
||||||
|
}
|
||||||
|
impl VideoTrack {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let track = |codec: VideoCodec| TrackLocalStaticRTP::new(
|
||||||
|
RTCRtpCodecCapability { mime_type: codec.mime().into(), clock_rate: 90_000, ..Default::default() },
|
||||||
|
"video_0".into(), "video_0".into());
|
||||||
|
let (progress, _) = tokio::sync::watch::channel((0, VideoCodec::H264));
|
||||||
|
Self { progress, h264: track(VideoCodec::H264), h265: track(VideoCodec::H265), has_h264: AtomicBool::new(false),
|
||||||
|
has_h265: AtomicBool::new(false), timeline: Mutex::new(Timeline { sequence: rand::random(), timestamp: rand::random(), h264: Default::default() }) }
|
||||||
|
}
|
||||||
|
pub fn supports(&self, codec: VideoCodec) -> bool {
|
||||||
|
match codec { VideoCodec::H264 => &self.has_h264, VideoCodec::H265 => &self.has_h265 }.load(Ordering::Acquire)
|
||||||
|
}
|
||||||
|
pub fn revision(&self) -> u64 { self.progress.borrow().0 }
|
||||||
|
/// Format-change RPC success follows a transmitted frame, including a negotiated fallback.
|
||||||
|
pub async fn wait_for_applied(&self, since: u64, settings: &tokio::sync::watch::Sender<crate::capture_setting::VideoSettings>) -> bool {
|
||||||
|
let mut progress = self.progress.subscribe();
|
||||||
|
tokio::time::timeout(std::time::Duration::from_secs(10), async {
|
||||||
|
loop {
|
||||||
|
let (revision, codec) = *progress.borrow_and_update();
|
||||||
|
if revision > since && codec == settings.borrow().codec { return true; }
|
||||||
|
if progress.changed().await.is_err() { return false; }
|
||||||
|
}
|
||||||
|
}).await.unwrap_or(false)
|
||||||
|
}
|
||||||
|
pub async fn write_sample_with_extensions(&self, codec: VideoCodec, sample: &Sample, extensions: &[webrtc::rtp::extension::HeaderExtension]) -> Result<()> {
|
||||||
|
if !self.supports(codec) { return Err(Error::ErrUnsupportedCodec); }
|
||||||
|
let track = match codec { VideoCodec::H264 => &self.h264, VideoCodec::H265 => &self.h265 };
|
||||||
|
if track.all_binding_paused().await { return Ok(()); }
|
||||||
|
let mut state = self.timeline.lock().await;
|
||||||
|
// Leave room for the RTP header and negotiated UU extensions below the 1200 byte MTU.
|
||||||
|
let payloads = match codec {
|
||||||
|
VideoCodec::H264 => state.h264.payload(1160, &sample.data).map_err(Error::Rtp)?,
|
||||||
|
VideoCodec::H265 => crate::hevc::payload(1160, &sample.data).map_err(Error::new)?,
|
||||||
|
};
|
||||||
|
let count = payloads.len();
|
||||||
|
let mut complete = count > 0;
|
||||||
|
let timestamp = state.timestamp;
|
||||||
|
state.timestamp = state.timestamp.wrapping_add((sample.duration.as_secs_f64() * 90_000.0) as u32);
|
||||||
|
for (index, payload) in payloads.into_iter().enumerate() {
|
||||||
|
let packet = Packet { header: Header { version: 2, sequence_number: state.sequence, timestamp,
|
||||||
|
marker: index + 1 == count, ..Default::default() }, payload };
|
||||||
|
state.sequence = state.sequence.wrapping_add(1);
|
||||||
|
complete &= track.write_rtp_with_extensions(&packet, extensions).await? > 0;
|
||||||
|
}
|
||||||
|
if complete { self.progress.send_modify(|value| { value.0 += 1; value.1 = codec; }); }
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[async_trait]
|
||||||
|
impl TrackLocal for VideoTrack {
|
||||||
|
async fn bind(&self, context: &TrackLocalContext) -> Result<RTCRtpCodecParameters> {
|
||||||
|
let h264 = self.h264.bind(context).await.ok();
|
||||||
|
let h265 = self.h265.bind(context).await.ok();
|
||||||
|
self.has_h264.store(h264.is_some(), Ordering::Release);
|
||||||
|
self.has_h265.store(h265.is_some(), Ordering::Release);
|
||||||
|
log::info!("Video track negotiated: h264={} h265={}", h264.is_some(), h265.is_some());
|
||||||
|
h264.or(h265).ok_or(Error::ErrUnsupportedCodec)
|
||||||
|
}
|
||||||
|
async fn unbind(&self, context: &TrackLocalContext) -> Result<()> {
|
||||||
|
if self.has_h264.swap(false, Ordering::AcqRel) { self.h264.unbind(context).await?; }
|
||||||
|
if self.has_h265.swap(false, Ordering::AcqRel) { self.h265.unbind(context).await?; }
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
fn id(&self) -> &str { "video_0" }
|
||||||
|
fn stream_id(&self) -> &str { "video_0" }
|
||||||
|
fn kind(&self) -> RTPCodecType { RTPCodecType::Video }
|
||||||
|
fn as_any(&self) -> &dyn Any { self }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use webrtc::{api::{APIBuilder, media_engine::MediaEngine}, peer_connection::configuration::RTCConfiguration};
|
||||||
|
async fn negotiate(h264: bool, hevc: bool) {
|
||||||
|
let mut remote_media = MediaEngine::default();
|
||||||
|
if h264 { remote_media.register_codec(RTCRtpCodecParameters { capability: RTCRtpCodecCapability {
|
||||||
|
mime_type: VideoCodec::H264.mime().into(), clock_rate: 90_000,
|
||||||
|
sdp_fmtp_line: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f".into(), ..Default::default()
|
||||||
|
}, payload_type: 102, ..Default::default() }, RTPCodecType::Video).unwrap(); }
|
||||||
|
if hevc { register_hevc(&mut remote_media).unwrap(); }
|
||||||
|
let (remote_setting, host_setting, router) = crate::test_network::virtual_network().await;
|
||||||
|
let remote = APIBuilder::new().with_setting_engine(remote_setting).with_media_engine(remote_media).build().new_peer_connection(RTCConfiguration::default()).await.unwrap();
|
||||||
|
remote.add_transceiver_from_kind(RTPCodecType::Video, None).await.unwrap();
|
||||||
|
let mut media = MediaEngine::default(); media.register_default_codecs().unwrap(); register_hevc(&mut media).unwrap();
|
||||||
|
let host = APIBuilder::new().with_setting_engine(host_setting).with_media_engine(media).build().new_peer_connection(RTCConfiguration::default()).await.unwrap();
|
||||||
|
let (packets_tx, mut packets_rx) = tokio::sync::mpsc::unbounded_channel();
|
||||||
|
remote.on_track(Box::new(move |remote_track, _, _| {
|
||||||
|
let packets_tx = packets_tx.clone();
|
||||||
|
Box::pin(async move {
|
||||||
|
tokio::spawn(async move {
|
||||||
|
while let Ok((packet, _)) = remote_track.read_rtp().await {
|
||||||
|
if packets_tx.send(packet).is_err() { break; }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})
|
||||||
|
}));
|
||||||
|
let track = std::sync::Arc::new(VideoTrack::new());
|
||||||
|
host.add_track(track.clone()).await.unwrap();
|
||||||
|
let offer = remote.create_offer(None).await.unwrap();
|
||||||
|
let mut gathered = remote.gathering_complete_promise().await;
|
||||||
|
remote.set_local_description(offer).await.unwrap();
|
||||||
|
gathered.recv().await;
|
||||||
|
host.set_remote_description(remote.local_description().await.unwrap()).await.unwrap();
|
||||||
|
let answer = host.create_answer(None).await.unwrap();
|
||||||
|
assert_eq!(answer.sdp.contains("H265/90000"), hevc);
|
||||||
|
assert_eq!(answer.sdp.contains("H264/90000"), h264);
|
||||||
|
let mut gathered = host.gathering_complete_promise().await;
|
||||||
|
host.set_local_description(answer).await.unwrap();
|
||||||
|
gathered.recv().await;
|
||||||
|
remote.set_remote_description(host.local_description().await.unwrap()).await.unwrap();
|
||||||
|
tokio::time::timeout(std::time::Duration::from_secs(10), async {
|
||||||
|
while !track.supports(VideoCodec::H264) && !track.supports(VideoCodec::H265) { tokio::time::sleep(std::time::Duration::from_millis(10)).await; }
|
||||||
|
}).await.unwrap();
|
||||||
|
assert_eq!(track.supports(VideoCodec::H264), h264);
|
||||||
|
assert_eq!(track.supports(VideoCodec::H265), hevc);
|
||||||
|
tokio::time::timeout(std::time::Duration::from_secs(30), async {
|
||||||
|
while host.connection_state() != webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Connected {
|
||||||
|
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
|
||||||
|
}
|
||||||
|
}).await.unwrap();
|
||||||
|
let codecs = if !h264 { vec![VideoCodec::H265] } else if hevc { vec![VideoCodec::H264, VideoCodec::H265, VideoCodec::H264] } else { vec![VideoCodec::H264] };
|
||||||
|
let mut previous: Option<Packet> = None;
|
||||||
|
for codec in codecs {
|
||||||
|
let data = match codec { VideoCodec::H264 => bytes::Bytes::from_static(&[0,0,1,65,1,2]),
|
||||||
|
VideoCodec::H265 => bytes::Bytes::from_static(&[0,0,1,38,1,2]) };
|
||||||
|
let (settings, _) = tokio::sync::watch::channel(crate::capture_setting::VideoSettings { codec, ..Default::default() });
|
||||||
|
let applied = track.wait_for_applied(track.revision(), &settings);
|
||||||
|
tokio::pin!(applied);
|
||||||
|
assert!(tokio::time::timeout(std::time::Duration::from_millis(1), &mut applied).await.is_err());
|
||||||
|
track.write_sample_with_extensions(codec, &Sample { data, duration: std::time::Duration::from_millis(20), ..Default::default() }, &[]).await.unwrap();
|
||||||
|
assert!(applied.await);
|
||||||
|
let packet = tokio::time::timeout(std::time::Duration::from_secs(3), packets_rx.recv()).await.unwrap().unwrap();
|
||||||
|
assert_eq!(packet.header.payload_type, if codec == VideoCodec::H264 { 102 } else { 126 });
|
||||||
|
assert!(packet.header.marker);
|
||||||
|
if let Some(previous) = previous {
|
||||||
|
assert_eq!(packet.header.ssrc, previous.header.ssrc);
|
||||||
|
assert_eq!(packet.header.sequence_number, previous.header.sequence_number.wrapping_add(1));
|
||||||
|
assert_eq!(packet.header.timestamp, previous.header.timestamp.wrapping_add(1800));
|
||||||
|
}
|
||||||
|
previous = Some(packet);
|
||||||
|
}
|
||||||
|
host.close().await.unwrap(); remote.close().await.unwrap();
|
||||||
|
router.lock().await.stop().await.unwrap();
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn negotiated_codecs_only_and_legacy_h264_offer() { negotiate(true, false).await; negotiate(true, true).await; negotiate(false, true).await; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,334 @@
|
|||||||
|
use crate::auth::UUAuthClient;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
// No Debug: room/report tokens and signaling URLs are credentials.
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct JoinData {
|
||||||
|
#[serde(default)] pub token: String,
|
||||||
|
#[serde(default)] pub signaling_list: Vec<String>,
|
||||||
|
#[serde(default)] pub ws_connect_timeout_ms: i32,
|
||||||
|
pub report_token: String,
|
||||||
|
pub report_url: String,
|
||||||
|
#[serde(default)] pub report_server_address: String,
|
||||||
|
#[serde(default)] pub streamer_retry_delta_ms: i32,
|
||||||
|
#[serde(default)] pub international_connect: bool,
|
||||||
|
}
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub enum Error { AuthenticationExpired, Api(i32, String), InvalidResponse, Transport, MissingRoom, InvalidDevice, InvalidAssist }
|
||||||
|
impl std::fmt::Display for Error {
|
||||||
|
// The server's short message is user-facing (e.g. 2002 「被控端正在被远控中,无法发起连接」).
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Error::Api(code, msg) if !msg.is_empty() => write!(f, "{msg} (code {code})"),
|
||||||
|
Error::AuthenticationExpired => write!(f, "登录已过期,请重新登录"),
|
||||||
|
Error::Transport => write!(f, "网络连接失败"),
|
||||||
|
Error::InvalidAssist => write!(f, "请输入对方的设备 ID 和有效验证码"),
|
||||||
|
Error::InvalidResponse => write!(f, "服务器响应格式无效"),
|
||||||
|
Error::MissingRoom => write!(f, "服务器未返回可用的房间或信令信息"),
|
||||||
|
Error::Api(code, _) => write!(f, "连接请求失败(错误码 {code})"),
|
||||||
|
_ => write!(f, "viewer API: {self:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mirrors Windows 0x140b77cd0 / 0x140b7a6d0, not host RoomData.
|
||||||
|
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 msg = value.get("msg").and_then(Value::as_str).ok_or(Error::InvalidResponse)?.chars().take(120).collect::<String>();
|
||||||
|
if code == 1120 { return Err(Error::AuthenticationExpired); }
|
||||||
|
let code = if code == 0 && status != 200 { i32::from(status) } else { code };
|
||||||
|
// Error responses carry an empty data object (observed: 400 / 2002 with data {}); report the code.
|
||||||
|
if code != 0 { return Err(Error::Api(code, msg)); }
|
||||||
|
let data = match value.get("data") {
|
||||||
|
Some(Value::Object(o)) => Some(serde_json::from_value::<JoinData>(Value::Object(o.clone())).map_err(|_| Error::InvalidResponse)?),
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
data.ok_or(Error::MissingRoom)
|
||||||
|
}
|
||||||
|
fn parse_share(status: u16, mut body: Value, secrets: &[&str]) -> Result<JoinData, Error> {
|
||||||
|
if let Some(Value::String(msg)) = body.get_mut("msg") {
|
||||||
|
// A server error must not echo credentials to stderr or the launcher.
|
||||||
|
for secret in secrets {
|
||||||
|
if !secret.is_empty() { *msg = msg.replace(secret, "[已隐藏]"); }
|
||||||
|
}
|
||||||
|
*msg = msg.chars().filter(|c| !c.is_control()).collect();
|
||||||
|
}
|
||||||
|
let room = parse_join(status, body)?;
|
||||||
|
if room.token.is_empty() || room.signaling_list.is_empty() { return Err(Error::MissingRoom); }
|
||||||
|
Ok(room)
|
||||||
|
}
|
||||||
|
pub fn retry_delay(error: &Error, retries: usize, enabled: bool) -> Option<Duration> {
|
||||||
|
match error {
|
||||||
|
Error::Api(code, _) if enabled && retries < 5 && ![-1, 2002, 2006, 2007, 4042].contains(code) => Some(Duration::from_millis(3000)),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Api {
|
||||||
|
signer: UUAuthClient,
|
||||||
|
http: reqwest::Client,
|
||||||
|
base: String,
|
||||||
|
}
|
||||||
|
impl Api {
|
||||||
|
pub fn new(signer: UUAuthClient, base: &str) -> Self {
|
||||||
|
Self { signer, base: base.trim_end_matches('/').into(), http: reqwest::Client::builder().timeout(Duration::from_secs(12)).build().unwrap() }
|
||||||
|
}
|
||||||
|
pub async fn join_share(&self, connect_id: &str, connect_code: &str) -> Result<JoinData, Error> {
|
||||||
|
if connect_id.trim().is_empty() || connect_code.trim().is_empty() || connect_id.chars().chain(connect_code.chars()).any(char::is_control) { return Err(Error::InvalidAssist); }
|
||||||
|
// Win LiteInner 0x140b6db90 sends plaintext connect_code; Mac uses the v1 path.
|
||||||
|
let text = json!({"connect_id":connect_id,"connect_code":connect_code}).to_string();
|
||||||
|
for version in [2, 1] {
|
||||||
|
let path = format!("/api/v{version}/room/join/share/by_code");
|
||||||
|
let response = self.http.post(format!("{}{path}", self.base))
|
||||||
|
.headers(self.signer.build_headers("POST", &path, &text)).body(text.clone())
|
||||||
|
.send().await.map_err(|_| Error::Transport)?;
|
||||||
|
let status = response.status().as_u16();
|
||||||
|
log::info!("Assist join response: status={status}");
|
||||||
|
// A missing endpoint may return HTML; inspect HTTP status before decoding JSON.
|
||||||
|
if version == 2 && matches!(status, 404 | 405) { continue; }
|
||||||
|
let body = response.json().await.map_err(|_| Error::InvalidResponse)?;
|
||||||
|
return parse_share(status, body, &[connect_code, &self.signer.creds.token, &self.signer.creds.csrf]);
|
||||||
|
}
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
async fn request(&self, method: reqwest::Method, path: &str, body: Option<Value>) -> Result<(u16, Value), Error> {
|
||||||
|
let text = body.map(|v| v.to_string()).unwrap_or_default();
|
||||||
|
let response = self.http.request(method.clone(), format!("{}{path}", self.base))
|
||||||
|
.headers(self.signer.build_headers(method.as_str(), path, &text)).body(text)
|
||||||
|
.send().await.map_err(|_| Error::Transport)?;
|
||||||
|
let status = response.status().as_u16();
|
||||||
|
Ok((status, response.json().await.map_err(|_| Error::InvalidResponse)?))
|
||||||
|
}
|
||||||
|
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.
|
||||||
|
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;
|
||||||
|
loop {
|
||||||
|
let result = match self.request(reqwest::Method::POST, &path, Some(json!({"force_join":force_join}))).await {
|
||||||
|
Ok((status, body)) => {
|
||||||
|
// Structure only (codes and key names), never the room token or URLs.
|
||||||
|
let shape = crate::protocol::json_shape(&body, 3);
|
||||||
|
let server_msg = body.get("msg").and_then(Value::as_str).map(|m| m.chars().take(120).collect::<String>());
|
||||||
|
let parsed = parse_join(status, body);
|
||||||
|
if parsed.is_err() {
|
||||||
|
// UU_VIEWER_DEBUG=1 adds the server's short error message (like UU_ASSIST_DEBUG on the host).
|
||||||
|
let msg = std::env::var("UU_VIEWER_DEBUG").is_ok_and(|v| v == "1").then_some(server_msg).flatten();
|
||||||
|
log::warn!("Join response: status={status} shape={shape} msg={msg:?}");
|
||||||
|
}
|
||||||
|
parsed
|
||||||
|
}
|
||||||
|
Err(e) => Err(e),
|
||||||
|
};
|
||||||
|
match result {
|
||||||
|
Ok(data) => {
|
||||||
|
if data.token.is_empty() || data.signaling_list.is_empty() { return Err(Error::MissingRoom); }
|
||||||
|
return Ok(data);
|
||||||
|
}
|
||||||
|
Err(error) => match retry_delay(&error, retries, retry) {
|
||||||
|
Some(delay) => { tokio::time::sleep(delay).await; retries += 1; }
|
||||||
|
None => return Err(error),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// 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> {
|
||||||
|
let path = "/api/v1/device/list";
|
||||||
|
let response = self.http.request(method.clone(), format!("{}{path}", self.base))
|
||||||
|
.headers(self.signer.build_headers(method.as_str(), path, "")).send().await.map_err(|_| Error::Transport)?;
|
||||||
|
let status = response.status().as_u16();
|
||||||
|
let bytes = response.bytes().await.map_err(|_| Error::InvalidResponse)?;
|
||||||
|
Ok(match serde_json::from_slice::<Value>(&bytes) {
|
||||||
|
Ok(v) => format!("status={status} shape={}", crate::protocol::json_shape(&v, 7)),
|
||||||
|
Err(_) => format!("status={status} non_json_bytes={}", bytes.len()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/// GET /api/v1/device/list → data {current_device, my_binded_devices[], others_shared_devices[]} (observed live).
|
||||||
|
pub async fn device_list(&self) -> Result<Value, Error> {
|
||||||
|
let (status, body) = self.request(reqwest::Method::GET, "/api/v1/device/list", None).await?;
|
||||||
|
if body.get("code").and_then(Value::as_i64) == Some(1120) { return Err(Error::AuthenticationExpired); }
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
/// 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> {
|
||||||
|
let (status, body) = self.request(method, "/api/v1/device/list", None).await?;
|
||||||
|
if body.get("code").and_then(Value::as_i64) == Some(1120) { return Err(Error::AuthenticationExpired); }
|
||||||
|
if status != 200 || body.get("code").and_then(Value::as_i64) != Some(0) { return Err(Error::InvalidResponse); }
|
||||||
|
body.pointer(array_pointer).and_then(Value::as_array).cloned().ok_or(Error::InvalidResponse)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn assistance_response_validation_and_safe_user_errors() {
|
||||||
|
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"]["share_id"]=json!("synthetic-share");
|
||||||
|
assert!(!parse_share(200,fixture.clone(),&[]).unwrap().token.is_empty());
|
||||||
|
for key in ["token","signaling_list"] {
|
||||||
|
let mut v=fixture.clone();v["data"].as_object_mut().unwrap().remove(key);
|
||||||
|
assert_eq!(parse_share(200,v,&[]).err().unwrap(),Error::MissingRoom);
|
||||||
|
}
|
||||||
|
for msg in ["验证码错误","对方未开启协助","无法对同账号的设备发起远程协助"] {
|
||||||
|
assert_eq!(parse_share(422,json!({"code":9876,"msg":msg}),&[]).err().unwrap().to_string(),format!("{msg} (code 9876)"));
|
||||||
|
}
|
||||||
|
assert_eq!(parse_share(422,json!({"code":9876,"msg":"fixture-code\nfixture-token"}),&["fixture-code","fixture-token",""]).err().unwrap(),Error::Api(9876,"[已隐藏][已隐藏]".into()));
|
||||||
|
assert_eq!(parse_share(422,json!({"code":1120,"msg":"expired"}),&[]).err().unwrap(),Error::AuthenticationExpired);
|
||||||
|
assert_eq!(parse_share(200,json!({"code":0,"msg":"","data":{}}),&[]).err().unwrap(),Error::InvalidResponse);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn join_schema_defaults_required_types_and_error_policy() {
|
||||||
|
let fixture: Value = serde_json::from_str(include_str!("../../tests/fixtures/viewer/join.json")).unwrap();
|
||||||
|
let data = parse_join(200, fixture.clone()).unwrap();
|
||||||
|
assert_eq!(data.signaling_list.len(), 1);
|
||||||
|
for key in ["report_token", "report_url"] {
|
||||||
|
let mut v = fixture.clone(); v["data"].as_object_mut().unwrap().remove(key);
|
||||||
|
assert!(matches!(parse_join(200,v), Err(Error::InvalidResponse)));
|
||||||
|
let mut v = fixture.clone(); v["data"][key] = Value::Null;
|
||||||
|
assert!(matches!(parse_join(200,v), Err(Error::InvalidResponse)));
|
||||||
|
}
|
||||||
|
let v = json!({"code":0,"msg":"","data":{"report_token":"","report_url":""}});
|
||||||
|
let d = parse_join(200,v).unwrap();
|
||||||
|
assert!(d.token.is_empty() && d.signaling_list.is_empty() && !d.international_connect);
|
||||||
|
assert_eq!(d.ws_connect_timeout_ms,0); assert_eq!(d.streamer_retry_delta_ms,0);
|
||||||
|
assert!(matches!(parse_join(503,fixture),Err(Error::Api(503, _))));
|
||||||
|
for data in [Value::Null,json!([]),json!("ignored")] {
|
||||||
|
assert!(matches!(parse_join(422,json!({"code":1120,"msg":"","data":data})),Err(Error::AuthenticationExpired)));
|
||||||
|
}
|
||||||
|
for code in [-1,2002,2006,2007,4042] { assert_eq!(retry_delay(&Error::Api(code,String::new()),0,true),None); }
|
||||||
|
for retries in 0..5 { assert_eq!(retry_delay(&Error::Api(5000,String::new()),retries,true),Some(Duration::from_millis(3000))); }
|
||||||
|
assert_eq!(retry_delay(&Error::Api(5000,String::new()),5,true),None);
|
||||||
|
assert_eq!(retry_delay(&Error::Api(5000,String::new()),0,false),None);
|
||||||
|
for error in [Error::Transport,Error::InvalidResponse,Error::AuthenticationExpired] { assert_eq!(retry_delay(&error,0,true),None); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod http_tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::auth::Credentials;
|
||||||
|
use tokio::{io::{AsyncReadExt,AsyncWriteExt},net::TcpListener};
|
||||||
|
pub async fn server(replies:Vec<(u16,Value)>)->(String,tokio::task::JoinHandle<Vec<String>>) {
|
||||||
|
server_raw(replies.into_iter().map(|(status,body)|(status,body.to_string())).collect()).await
|
||||||
|
}
|
||||||
|
async fn server_raw(replies:Vec<(u16,String)>)->(String,tokio::task::JoinHandle<Vec<String>>) {
|
||||||
|
let listener=TcpListener::bind((std::net::Ipv4Addr::LOCALHOST,0)).await.unwrap();
|
||||||
|
let url=format!("http://{}",listener.local_addr().unwrap());
|
||||||
|
let job=tokio::spawn(async move {
|
||||||
|
let mut requests=Vec::new();
|
||||||
|
for (status,body) in replies {
|
||||||
|
let(mut stream,_)=listener.accept().await.unwrap();let mut request=Vec::new();let mut buf=[0;1024];
|
||||||
|
loop {
|
||||||
|
let n=stream.read(&mut buf).await.unwrap();assert_ne!(n,0);request.extend_from_slice(&buf[..n]);
|
||||||
|
if let Some(end)=request.windows(4).position(|w|w==b"\r\n\r\n") {
|
||||||
|
let headers=String::from_utf8_lossy(&request[..end]);
|
||||||
|
let length=headers.lines().find_map(|l|l.to_lowercase().strip_prefix("content-length:").map(str::trim).and_then(|s|s.parse::<usize>().ok())).unwrap_or(0);
|
||||||
|
if request.len()>=end+4+length {break}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
requests.push(String::from_utf8(request).unwrap());
|
||||||
|
let reply=format!("HTTP/1.1 {status} Mock\r\nContent-Type: application/json\r\nConnection: close\r\nContent-Length: {}\r\n\r\n{body}",body.len());
|
||||||
|
stream.write_all(reply.as_bytes()).await.unwrap();
|
||||||
|
}
|
||||||
|
requests
|
||||||
|
});
|
||||||
|
(url,job)
|
||||||
|
}
|
||||||
|
fn signer()->UUAuthClient {UUAuthClient::new(Credentials{token:"synthetic-auth".into(),sign_key:"synthetic-key".into(),..Default::default()})}
|
||||||
|
#[tokio::test]
|
||||||
|
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(url,job)=server(vec![(200,json!({"code":0,"data":{"synthetic_devices":[{"id":"synthetic-device","name":"fixture"}]}})),(200,fixture)]).await;
|
||||||
|
let api=Api::new(signer(),&url);
|
||||||
|
let devices=api.devices(reqwest::Method::GET,"/data/synthetic_devices").await.unwrap();assert_eq!(devices.len(),1);
|
||||||
|
assert!(api.join("",true,true).await.is_err());
|
||||||
|
api.join("synthetic-device",true,true).await.unwrap();
|
||||||
|
let requests=job.await.unwrap();assert!(requests[0].starts_with("GET /api/v1/device/list "));
|
||||||
|
assert!(requests[1].starts_with("POST /api/v1/room/join/by_device/synthetic-device "));
|
||||||
|
assert!(requests[1].to_lowercase().contains("x-param-sign:"));
|
||||||
|
assert_eq!(requests[1].split("\r\n\r\n").nth(1).unwrap(),r#"{"force_join":true}"#);
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn assistance_uses_signed_json_and_only_http_endpoint_fallback() {
|
||||||
|
let mut fixture:Value=serde_json::from_str(include_str!("../../tests/fixtures/viewer/join.json")).unwrap();
|
||||||
|
// Extra share identity fields do not change the existing room parser.
|
||||||
|
fixture["data"]["share_id"]=json!("synthetic-share");
|
||||||
|
fixture["data"]["publisher_device_id"]=json!("synthetic-publisher");
|
||||||
|
fixture["data"]["publisher_platform"]=json!(1);
|
||||||
|
for status in [200,404,405] {
|
||||||
|
let replies=if status==200 {vec![(200,fixture.to_string())]} else {vec![(status,"<html>endpoint unavailable</html>".into()),(200,fixture.to_string())]};
|
||||||
|
let(url,job)=server_raw(replies).await;
|
||||||
|
let room=Api::new(signer(),&url).join_share("synthetic-id","synthetic-code").await.unwrap();
|
||||||
|
assert!(!room.token.is_empty());assert_eq!(room.signaling_list.len(),1);
|
||||||
|
let requests=job.await.unwrap();assert_eq!(requests.len(),if status==200{1}else{2});
|
||||||
|
for (i,request) in requests.iter().enumerate() {
|
||||||
|
let version=if i==0{2}else{1};
|
||||||
|
assert!(request.starts_with(&format!("POST /api/v{version}/room/join/share/by_code ")));
|
||||||
|
let(headers,body)=request.split_once("\r\n\r\n").unwrap();
|
||||||
|
assert!(!headers.contains("synthetic-code"));
|
||||||
|
assert!(headers.to_lowercase().contains("x-param-sign:"));
|
||||||
|
assert!(headers.to_lowercase().contains("content-type: application/json"));
|
||||||
|
assert_eq!(serde_json::from_str::<Value>(body).unwrap(),json!({"connect_id":"synthetic-id","connect_code":"synthetic-code"}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn assistance_reports_actual_errors_without_retrying_or_echoing_secrets() {
|
||||||
|
for msg in ["验证码错误","对方未开启协助","无法对同账号的设备发起远程协助"] {
|
||||||
|
let(url,job)=server(vec![(422,json!({"code":9876,"msg":msg}))]).await;
|
||||||
|
let error=Api::new(signer(),&url).join_share("synthetic-id","synthetic-code").await.err().unwrap();
|
||||||
|
assert_eq!(error.to_string(),format!("{msg} (code 9876)"));assert_eq!(job.await.unwrap().len(),1);
|
||||||
|
}
|
||||||
|
for (status,body,expected) in [
|
||||||
|
(200,json!({"code":405,"msg":"业务拒绝"}),Error::Api(405,"业务拒绝".into())),
|
||||||
|
(500,json!({"code":5000,"msg":"服务忙"}),Error::Api(5000,"服务忙".into())),
|
||||||
|
(422,json!({"code":1120,"msg":""}),Error::AuthenticationExpired),
|
||||||
|
(200,json!({"code":0,"msg":"","data":{"report_url":"","report_token":""}}),Error::MissingRoom),
|
||||||
|
(200,json!({"code":0,"msg":"","data":{}}),Error::InvalidResponse),
|
||||||
|
(422,json!({"code":9876,"msg":"synthetic-code synthetic-auth"}),Error::Api(9876,"[已隐藏] [已隐藏]".into())),
|
||||||
|
] {
|
||||||
|
let(url,job)=server(vec![(status,body)]).await;
|
||||||
|
assert_eq!(Api::new(signer(),&url).join_share("synthetic-id","synthetic-code").await.err().unwrap(),expected);
|
||||||
|
assert_eq!(job.await.unwrap().len(),1);
|
||||||
|
}
|
||||||
|
let api=Api::new(signer(),"http://127.0.0.1:1");
|
||||||
|
for (id,code) in [("","fixture"),("id",""),("id","a\nb")] {assert_eq!(api.join_share(id,code).await.err().unwrap(),Error::InvalidAssist);}
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn retry_stops_after_five_delays_and_terminal_errors_stop_immediately() {
|
||||||
|
let(url,job)=server(vec![(503,json!({"code":5000,"msg":"fixture"}));6]).await;
|
||||||
|
let start=tokio::time::Instant::now();
|
||||||
|
assert!(matches!(Api::new(signer(),&url).join("synthetic-device",false,true).await,Err(Error::Api(5000, _))));
|
||||||
|
assert!(start.elapsed()>=Duration::from_secs(15));assert_eq!(job.await.unwrap().len(),6);
|
||||||
|
for code in [-1,1120,2002,2006,2007,4042] {
|
||||||
|
let(url,job)=server(vec![(422,json!({"code":code,"msg":"fixture"}))]).await;
|
||||||
|
let error=Api::new(signer(),&url).join("synthetic-device",false,true).await.err().unwrap();
|
||||||
|
if code==1120 {assert_eq!(error,Error::AuthenticationExpired)} else {assert!(matches!(error,Error::Api(c,_) if c==code))}
|
||||||
|
assert_eq!(job.await.unwrap().len(),1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod strict_tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn rejects_wrong_json_types_and_missing_envelope_members() {
|
||||||
|
let fixture:Value=serde_json::from_str(include_str!("../../tests/fixtures/viewer/join.json")).unwrap();
|
||||||
|
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)));
|
||||||
|
}
|
||||||
|
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)));
|
||||||
|
}
|
||||||
|
assert!(matches!(parse_join(200,json!({"code":0,"msg":"","data":{}})),Err(Error::InvalidResponse)));
|
||||||
|
assert!(matches!(parse_join(200,json!({"code":2147483648u64,"msg":""})),Err(Error::InvalidResponse)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
//! Assistance secrets travel only through memory and the child's stdin pipe.
|
||||||
|
use std::{io::BufRead,path::Path,process::Stdio};
|
||||||
|
use tokio::{io::AsyncWriteExt,process::{Child,Command}};
|
||||||
|
|
||||||
|
pub fn read_code(input: impl BufRead) -> Result<String,String> {
|
||||||
|
let mut code=String::new();
|
||||||
|
input.take(1025).read_line(&mut code).map_err(|_|"无法从 stdin 读取验证码")?;
|
||||||
|
if code.len()>1024 {return Err("验证码过长".into())}
|
||||||
|
if code.ends_with('\n') {code.pop();if code.ends_with('\r'){code.pop();}}
|
||||||
|
if code.trim().is_empty() || code.chars().any(char::is_control) {return Err("请输入有效验证码".into())}
|
||||||
|
Ok(code)
|
||||||
|
}
|
||||||
|
pub fn command(exe:&Path,id:&str)->Command {
|
||||||
|
let mut command=Command::new(exe);
|
||||||
|
command.args(["assist",id]).stdin(Stdio::piped()).stdout(Stdio::null()).stderr(Stdio::piped()).kill_on_drop(true);
|
||||||
|
command
|
||||||
|
}
|
||||||
|
pub async fn send_code(child:&mut Child,code:&str)->Result<(),String> {
|
||||||
|
let result=async {
|
||||||
|
let mut stdin=child.stdin.take().ok_or("无法打开会话输入管道")?;
|
||||||
|
stdin.write_all(code.as_bytes()).await.map_err(|_|"无法向会话传递验证码")?;
|
||||||
|
stdin.write_all(b"\n").await.map_err(|_|"无法向会话传递验证码")?;
|
||||||
|
stdin.shutdown().await.map_err(|_|"无法关闭会话输入管道")
|
||||||
|
}.await;
|
||||||
|
if result.is_err() {let _=child.kill().await;}
|
||||||
|
result.map_err(str::to_owned)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)] mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test] fn stdin_reads_one_line_without_changing_code() {
|
||||||
|
assert_eq!(read_code(&b"a b\r\nignored\n"[..]).unwrap(),"a b");
|
||||||
|
assert_eq!(read_code(&b"fixture"[..]).unwrap(),"fixture");
|
||||||
|
for input in ["","\n"," \r\n","a\tb\n","a\0b\n",&"x".repeat(1025)] {assert!(read_code(input.as_bytes()).is_err());}
|
||||||
|
}
|
||||||
|
#[tokio::test] async fn launcher_passes_only_id_in_arguments_and_closes_stdin() {
|
||||||
|
let command=command(Path::new("/bin/sh"),"synthetic-id");
|
||||||
|
assert_eq!(command.as_std().get_args().collect::<Vec<_>>(),["assist","synthetic-id"]);
|
||||||
|
// Local process mock: echo the pipe after EOF, without files or credentials.
|
||||||
|
let mut child=Command::new("/bin/sh").args(["-c","cat"]).stdin(Stdio::piped()).stdout(Stdio::piped()).spawn().unwrap();
|
||||||
|
send_code(&mut child,"synthetic-code").await.unwrap();
|
||||||
|
let output=child.wait_with_output().await.unwrap();
|
||||||
|
assert!(output.status.success());assert_eq!(output.stdout,b"synthetic-code\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,170 @@
|
|||||||
|
use super::media::Latest;
|
||||||
|
|
||||||
|
const PACKETS:usize=12;
|
||||||
|
const MAX_PACKET:usize=8192;
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Packet { seq:u16, data:Vec<u8> }
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Audio { packets:Latest<Packet>, enabled:bool }
|
||||||
|
impl Default for Audio {
|
||||||
|
fn default()->Self {Self{packets:Latest::new(PACKETS),enabled:std::env::var("UU_VIEWER_AUDIO").map_or(true,|v|v!="0")}}
|
||||||
|
}
|
||||||
|
impl Audio {
|
||||||
|
#[cfg(test)] pub fn pop(&self)->Option<Vec<u8>> {self.packets.pop().map(|p|p.data)}
|
||||||
|
pub fn push(&self,seq:u16,data:&[u8]) {
|
||||||
|
if self.enabled && data.len()<=MAX_PACKET && samples(data).is_some() {self.packets.push(Packet{seq,data:data.to_vec()});}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn samples(data:&[u8])->Option<u64> {
|
||||||
|
let toc=*data.first()?;
|
||||||
|
let frame=if toc&0x80!=0 {120u64<<(toc>>3&3)} else if toc&0x60==0x60 {if toc&8!=0 {960}else{480}} else {match toc>>3&3 {3=>2880,n=>480u64<<n}};
|
||||||
|
let count=match toc&3 {0=>1,1|2=>2,_=>u64::from(*data.get(1)?&63)};
|
||||||
|
let total=frame*count;(count>0 && total<=5760).then_some(total)
|
||||||
|
}
|
||||||
|
|
||||||
|
// One complete packet per page; zero pre-skip because RTP has no encoder pre-skip header.
|
||||||
|
#[derive(Default)]
|
||||||
|
struct Ogg { sequence:u32, granule:u64, last:Option<u16> }
|
||||||
|
impl Ogg {
|
||||||
|
fn page(&mut self,payload:&[u8],flags:u8)->Vec<u8> {
|
||||||
|
let mut page=b"OggS\0".to_vec();page.push(flags);page.extend(self.granule.to_le_bytes());
|
||||||
|
page.extend(1u32.to_le_bytes());page.extend(self.sequence.to_le_bytes());self.sequence=self.sequence.wrapping_add(1);
|
||||||
|
page.extend([0;4]);page.push((payload.len()/255+1) as u8);
|
||||||
|
page.extend(std::iter::repeat(255).take(payload.len()/255));page.push((payload.len()%255) as u8);page.extend(payload);
|
||||||
|
let mut crc=0u32;
|
||||||
|
for byte in &page {crc^=u32::from(*byte)<<24;for _ in 0..8 {crc=if crc&0x80000000!=0 {(crc<<1)^0x04c11db7}else{crc<<1};}}
|
||||||
|
page[22..26].copy_from_slice(&crc.to_le_bytes());page
|
||||||
|
}
|
||||||
|
fn headers(&mut self)->Vec<u8> {
|
||||||
|
let mut head=b"OpusHead\x01\x02\0\0".to_vec();head.extend(48000u32.to_le_bytes());head.extend([0,0,0]);
|
||||||
|
let mut out=self.page(&head,2);let mut tags=b"OpusTags".to_vec();tags.extend([0;8]);out.extend(self.page(&tags,0));out
|
||||||
|
}
|
||||||
|
fn packet(&mut self,packet:Packet)->Option<Vec<u8>> {
|
||||||
|
if self.last.is_some_and(|last|{let delta=packet.seq.wrapping_sub(last);delta==0 || delta>=0x8000}) {return None}
|
||||||
|
self.granule+=samples(&packet.data)?;self.last=Some(packet.seq);Some(self.page(&packet.data,0))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature="viewer-sdl")]
|
||||||
|
mod playback {
|
||||||
|
use super::*;
|
||||||
|
use tokio::{io::{AsyncReadExt,AsyncWriteExt},process::{Child,Command},task::JoinHandle};
|
||||||
|
use std::{process::Stdio,time::Duration};
|
||||||
|
const PCM_SAMPLES:usize=1920;
|
||||||
|
const PCM_BYTES:usize=PCM_SAMPLES*2;
|
||||||
|
const SDL_BYTES:u32=48000*2*2/10;
|
||||||
|
struct Decoder { child:Option<Child>, tasks:Vec<JoinHandle<()>>, pcm:Latest<Vec<i16>> }
|
||||||
|
impl Decoder {
|
||||||
|
fn new(audio:Audio)->Result<Self,String> {
|
||||||
|
let mut child=Command::new(std::env::var_os("UU_VIEWER_FFMPEG").unwrap_or_else(||"ffmpeg".into()))
|
||||||
|
.args(["-hide_banner","-loglevel","error","-nostdin","-threads","1","-probesize","32","-analyzeduration","0",
|
||||||
|
"-f","ogg","-i","pipe:0","-vn","-ac","2","-ar","48000","-c:a","pcm_s16le","-f","s16le","-flush_packets","1","pipe:1"])
|
||||||
|
.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::null()).kill_on_drop(true).spawn().map_err(|_|"Audio decoder start failed")?;
|
||||||
|
let mut input=child.stdin.take().ok_or("Audio decoder stdin missing")?;
|
||||||
|
let mut output=child.stdout.take().ok_or("Audio decoder stdout missing")?;
|
||||||
|
let pcm=Latest::new(PACKETS);let decoded=pcm.clone();
|
||||||
|
let writer=tokio::spawn(async move {
|
||||||
|
let mut ogg=Ogg::default();
|
||||||
|
let result:std::io::Result<()>=async {
|
||||||
|
input.write_all(&ogg.headers()).await?;
|
||||||
|
let mut tick=tokio::time::interval(Duration::from_millis(4));
|
||||||
|
loop {
|
||||||
|
tick.tick().await;
|
||||||
|
while let Some(packet)=audio.packets.pop() {if let Some(page)=ogg.packet(packet) {
|
||||||
|
tokio::time::timeout(Duration::from_secs(1),input.write_all(&page)).await??;
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
}.await;
|
||||||
|
if result.is_err() {log::warn!("Viewer audio decoder input stopped");}
|
||||||
|
});
|
||||||
|
let reader=tokio::spawn(async move {
|
||||||
|
let mut bytes=[0u8;PCM_BYTES];
|
||||||
|
while output.read_exact(&mut bytes).await.is_ok() {
|
||||||
|
decoded.push(bytes.chunks_exact(2).map(|b|i16::from_le_bytes([b[0],b[1]])).collect());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Ok(Self{child:Some(child),tasks:vec![writer,reader],pcm})
|
||||||
|
}
|
||||||
|
fn alive(&mut self)->bool {self.child.as_mut().is_some_and(|child|matches!(child.try_wait(),Ok(None))) && self.tasks.iter().all(|t|!t.is_finished())}
|
||||||
|
}
|
||||||
|
impl Drop for Decoder {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
for task in &self.tasks {task.abort();}
|
||||||
|
if let Some(mut child)=self.child.take() {let _=child.start_kill();tokio::spawn(async move {let _=child.wait().await;});}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub struct Playback { queue:sdl2::audio::AudioQueue<i16>, decoder:Decoder }
|
||||||
|
impl Playback {
|
||||||
|
pub fn new(sdl:&sdl2::Sdl,audio:Audio)->Option<Self> {
|
||||||
|
if !audio.enabled {return None}
|
||||||
|
let result=(|| {
|
||||||
|
let subsystem=sdl.audio()?;
|
||||||
|
let queue=subsystem.open_queue::<i16,_>(None,&sdl2::audio::AudioSpecDesired{freq:Some(48000),channels:Some(2),samples:Some(960)})?;
|
||||||
|
let decoder=Decoder::new(audio)?;queue.resume();Ok::<_,String>(Self{queue,decoder})
|
||||||
|
})();
|
||||||
|
match result {Ok(p)=>Some(p),Err(_)=>{log::warn!("Viewer audio playback unavailable");None}}
|
||||||
|
}
|
||||||
|
pub fn pump(&mut self)->Result<(),String> {
|
||||||
|
if !self.decoder.alive() {return Err("Audio decoder stopped".into())}
|
||||||
|
while let Some(samples)=self.decoder.pcm.pop() {
|
||||||
|
if self.queue.size()+samples.len() as u32*2>SDL_BYTES {self.queue.clear();}
|
||||||
|
self.queue.queue_audio(&samples).map_err(|_|"Audio queue failed")?;
|
||||||
|
}Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[cfg(test)] mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[tokio::test] async fn sdl_dummy_queue_is_bounded_and_audio_can_be_disabled() {
|
||||||
|
if std::env::var_os("UU_VIEWER_AUDIO_TEST_CHILD").is_none() {
|
||||||
|
let output=std::process::Command::new(std::env::current_exe().unwrap()).args(["--exact","viewer::audio::playback::tests::sdl_dummy_queue_is_bounded_and_audio_can_be_disabled","--test-threads=1"])
|
||||||
|
.env("SDL_AUDIODRIVER","dummy").env("UU_VIEWER_AUDIO_TEST_CHILD","1").output().unwrap();
|
||||||
|
assert!(output.status.success(),"{}",String::from_utf8_lossy(&output.stdout));return
|
||||||
|
}
|
||||||
|
let sdl=sdl2::init().unwrap();
|
||||||
|
assert!(Playback::new(&sdl,Audio{packets:Latest::new(PACKETS),enabled:false}).is_none());
|
||||||
|
let mut playback=Playback::new(&sdl,Audio{packets:Latest::new(PACKETS),enabled:true}).unwrap();playback.queue.pause();
|
||||||
|
for value in 0..100 {playback.decoder.pcm.push(vec![value;PCM_SAMPLES]);}
|
||||||
|
playback.pump().unwrap();assert!(playback.queue.size()>0 && playback.queue.size()<=SDL_BYTES);
|
||||||
|
assert!(playback.decoder.pcm.pop().is_none());
|
||||||
|
}
|
||||||
|
#[tokio::test] async fn ffmpeg_streams_pcm_before_eof_and_is_reaped() {
|
||||||
|
let audio=Audio{packets:Latest::new(PACKETS),enabled:true};let mut decoder=Decoder::new(audio.clone()).unwrap();
|
||||||
|
let id=decoder.child.as_ref().unwrap().id().unwrap();
|
||||||
|
tokio::time::timeout(Duration::from_secs(5),async {
|
||||||
|
for seq in 0..250 {audio.push(seq,&[0xf8,0xff,0xfe]);tokio::time::sleep(Duration::from_millis(8)).await;
|
||||||
|
if let Some(pcm)=decoder.pcm.pop() {assert_eq!(pcm.len(),PCM_SAMPLES);assert!(decoder.alive());return}
|
||||||
|
}panic!("No PCM before EOF");
|
||||||
|
}).await.unwrap();
|
||||||
|
drop(decoder);
|
||||||
|
tokio::time::timeout(Duration::from_secs(2),async {while unsafe{libc::kill(id as i32,0)}==0 {tokio::time::sleep(Duration::from_millis(10)).await;}}).await.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[cfg(feature="viewer-sdl")] pub use playback::Playback;
|
||||||
|
|
||||||
|
#[cfg(test)] mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test] fn duration_bounds_and_latest_packets() {
|
||||||
|
assert_eq!(samples(&[0xf8,0xff,0xfe]),Some(960));assert_eq!(samples(&[0x98]),Some(960));
|
||||||
|
assert_eq!(samples(&[0x18]),Some(2880));assert_eq!(samples(&[0x83,48]),Some(5760));
|
||||||
|
assert_eq!(samples(&[0x83,49]),None);assert_eq!(samples(&[3,0]),None);assert_eq!(samples(&[]),None);
|
||||||
|
let audio=Audio{packets:Latest::new(PACKETS),enabled:true};
|
||||||
|
for seq in 0..1000 {audio.push(seq,&[0xf8,0xff,0xfe]);}
|
||||||
|
audio.push(1000,&vec![0;MAX_PACKET+1]);audio.push(1000,&[]);
|
||||||
|
for seq in 988..1000 {assert_eq!(audio.packets.pop().unwrap().seq,seq);}assert!(audio.packets.pop().is_none());
|
||||||
|
let disabled=Audio{enabled:false,..audio};disabled.push(0,&[0xf8,0xff,0xfe]);assert!(disabled.packets.pop().is_none());
|
||||||
|
}
|
||||||
|
#[test] fn ogg_headers_crc_lacing_and_sequence_wrap() {
|
||||||
|
let mut ogg=Ogg::default();let headers=ogg.headers();assert_eq!(&headers[..4],b"OggS");assert_eq!(&headers[28..36],b"OpusHead");
|
||||||
|
let page=ogg.page(&vec![0;255],0);assert_eq!(&page[27..29],&[255,0]);
|
||||||
|
let mut stream=headers;stream.extend(&page);
|
||||||
|
let (mut reader,head)=webrtc::media::io::ogg_reader::OggReader::new(std::io::Cursor::new(stream.clone()),true).unwrap();
|
||||||
|
assert_eq!(head.channels,2);assert_eq!(head.sample_rate,48000);
|
||||||
|
assert!(reader.parse_next_page().unwrap().0.starts_with(b"OpusTags"));
|
||||||
|
assert_eq!(reader.parse_next_page().unwrap().0.len(),255);
|
||||||
|
stream[28]^=1;assert!(webrtc::media::io::ogg_reader::OggReader::new(std::io::Cursor::new(stream),true).is_err());
|
||||||
|
let packet=|seq|Packet{seq,data:vec![0xf8,0xff,0xfe]};
|
||||||
|
assert!(ogg.packet(packet(65535)).is_some());assert!(ogg.packet(packet(0)).is_some());
|
||||||
|
assert!(ogg.packet(packet(0)).is_none());assert!(ogg.packet(packet(65534)).is_none());assert_eq!(ogg.granule,1920);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,277 @@
|
|||||||
|
//! Session-scoped text clipboard. Payloads never enter logs or process arguments.
|
||||||
|
use super::{rtc::Observed,wire};
|
||||||
|
use crate::{clipboard_protocol as text,clipboard_rich as rich,protocol::{decode_wire,WireValue}};
|
||||||
|
use std::{path::PathBuf,process::Stdio,sync::{Arc,atomic::{AtomicBool,Ordering}},time::Duration};
|
||||||
|
use tokio::{io::{AsyncReadExt,AsyncWriteExt},sync::mpsc};
|
||||||
|
use webrtc::data_channel::RTCDataChannel;
|
||||||
|
|
||||||
|
// Same limit as src/clipboard.rs; shared host codecs enforce it on both wire formats.
|
||||||
|
pub const MAX_TEXT:usize=30*1024;
|
||||||
|
pub const CHANNEL:usize=1; // TEXT_DATA_CHANNEL; native host clipboard state is scoped to this channel.
|
||||||
|
const POLL:Duration=Duration::from_millis(500);
|
||||||
|
static WARNED:AtomicBool=AtomicBool::new(false);
|
||||||
|
fn warn(){if !WARNED.swap(true,Ordering::Relaxed){log::warn!("Viewer text clipboard unavailable; synchronization disabled");}}
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct Backend {read:PathBuf,read_args:Vec<&'static str>,write:PathBuf,write_args:Vec<&'static str>}
|
||||||
|
fn executable(name:&str)->Option<PathBuf>{
|
||||||
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
std::env::var_os("PATH").and_then(|p|std::env::split_paths(&p).map(|d|d.join(name))
|
||||||
|
.find(|p|std::fs::metadata(p).is_ok_and(|m|m.is_file() && m.permissions().mode()&0o111!=0)))
|
||||||
|
}
|
||||||
|
impl Backend {
|
||||||
|
fn detect()->Option<Self>{
|
||||||
|
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.
|
||||||
|
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);
|
||||||
|
if selected.is_none(){warn();}selected
|
||||||
|
}
|
||||||
|
fn select(wayland:bool,find:impl Fn(&str)->Option<PathBuf>)->Option<Self>{
|
||||||
|
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"]})
|
||||||
|
}else{None};
|
||||||
|
selected.or_else(||find("xclip").map(|p|Self{read:p.clone(),write:p,read_args:vec!["-selection","clipboard","-out"],write_args:vec!["-selection","clipboard","-in"]}))
|
||||||
|
.or_else(||find("xsel").map(|p|Self{read:p.clone(),write:p,read_args:vec!["--clipboard","--output"],write_args:vec!["--clipboard","--input"]}))
|
||||||
|
}
|
||||||
|
async fn read(&self)->Result<Option<String>,()>{
|
||||||
|
let mut child=tokio::process::Command::new(&self.read).args(&self.read_args).stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::null()).kill_on_drop(true).spawn().map_err(|_|())?;
|
||||||
|
let operation=async{
|
||||||
|
let mut data=Vec::new();child.stdout.take().ok_or(())?.take((MAX_TEXT+1) as u64).read_to_end(&mut data).await.map_err(|_|())?;
|
||||||
|
if data.len()>MAX_TEXT {let _=child.kill().await;return Ok(None)}
|
||||||
|
// No text selection (including an image-only selection) is not a fatal backend failure.
|
||||||
|
if !child.wait().await.map_err(|_|())?.success(){return Ok(None)}
|
||||||
|
Ok(String::from_utf8(data).ok())
|
||||||
|
};
|
||||||
|
tokio::time::timeout(Duration::from_secs(2),operation).await.map_err(|_|())?
|
||||||
|
}
|
||||||
|
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 operation=async{
|
||||||
|
let mut stdin=child.stdin.take().ok_or(())?;stdin.write_all(value.as_bytes()).await.map_err(|_|())?;drop(stdin);
|
||||||
|
if child.wait().await.map_err(|_|())?.success(){Ok(())}else{Err(())}
|
||||||
|
};
|
||||||
|
tokio::time::timeout(Duration::from_secs(2),operation).await.map_err(|_|())?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Default)]
|
||||||
|
struct SyncState {last:Option<String>,assembly:text::Assembly,rich:rich::State,seq:u64}
|
||||||
|
impl SyncState {
|
||||||
|
async fn apply(&mut self,backend:&Backend,value:String)->Result<(),()>{
|
||||||
|
backend.write(&value).await?;
|
||||||
|
log::info!("Viewer remote clipboard applied: bytes={}",value.len());
|
||||||
|
self.last=Some(value);Ok(())
|
||||||
|
}
|
||||||
|
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>{
|
||||||
|
if self.last.as_ref()==Some(&value){return Ok(None)}
|
||||||
|
let id=self.next();let payload=self.rich.offer(id,value.clone())?;
|
||||||
|
self.last=Some(value);Ok(Some(wire::envelope(self.next(),21,&payload)))
|
||||||
|
}
|
||||||
|
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")}
|
||||||
|
let fields=decode_wire(data).map_err(|_|"invalid clipboard envelope")?;
|
||||||
|
let mut replies=Vec::new();let mut value=None;
|
||||||
|
for field in fields {
|
||||||
|
let WireValue::Bytes(payload)=field.value else{continue};
|
||||||
|
match field.number {
|
||||||
|
17=>if let Some(text)=self.assembly.accept(payload)?{value=Some(text)},
|
||||||
|
21=>{
|
||||||
|
let seq=&mut self.seq;
|
||||||
|
let result=self.rich.request(payload,&mut ||{*seq=seq.wrapping_add(1).max(100);*seq})?;
|
||||||
|
if result.text.is_some(){value=result.text;}
|
||||||
|
for (kind,body) in result.messages {replies.push(wire::envelope(self.next(),kind,&body));}
|
||||||
|
if let Some((header,text))=text::read_text_request(payload)?{
|
||||||
|
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)},
|
||||||
|
_=>{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok((replies,value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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(())
|
||||||
|
}
|
||||||
|
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<()>>}
|
||||||
|
impl Bridge {
|
||||||
|
pub fn new(desktop:bool)->Self{
|
||||||
|
let backend=if desktop{Backend::detect()}else{None};let enabled=backend.is_some();
|
||||||
|
Self{backend,enabled,tx:None,task:None}
|
||||||
|
}
|
||||||
|
pub fn start(&mut self,channel:&Arc<RTCDataChannel>,control:&Arc<RTCDataChannel>,observed:Arc<Observed>){
|
||||||
|
let Some(backend)=self.backend.take() else{return};
|
||||||
|
let (tx,mut rx)=mpsc::channel::<Vec<u8>>(16);self.tx=Some(tx);
|
||||||
|
let channel=Arc::downgrade(channel);let control=Arc::downgrade(control);
|
||||||
|
self.task=Some(tokio::spawn(async move{
|
||||||
|
let mut state=SyncState::default();let mut tick=tokio::time::interval(POLL);
|
||||||
|
let mut retries=0;let mut last_probe=std::time::Instant::now();
|
||||||
|
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||||
|
loop {
|
||||||
|
let Some(channel)=channel.upgrade() else{break};
|
||||||
|
tokio::select!{
|
||||||
|
data=rx.recv()=>{
|
||||||
|
let Some(data)=data else{break};
|
||||||
|
if !observed.clipboard_supported.load(Ordering::Acquire){continue}
|
||||||
|
match state.receive(&data){
|
||||||
|
Ok((replies,value))=>{
|
||||||
|
if let Some(value)=value {
|
||||||
|
// Update only after the write succeeds; the next poll cannot echo it.
|
||||||
|
if state.apply(&backend,value).await.is_err(){warn();break}
|
||||||
|
}
|
||||||
|
let mut failed=false;for reply in replies{if send(&channel,reply).await.is_err(){failed=true;break}}
|
||||||
|
if failed{break}
|
||||||
|
},
|
||||||
|
Err(_)=>log::warn!("Viewer clipboard message rejected"),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_=tick.tick()=>{
|
||||||
|
if observed.echo_received.load(Ordering::Acquire) && !observed.control_open.load(Ordering::Acquire){break}
|
||||||
|
if !observed.echo_received.load(Ordering::Acquire){continue}
|
||||||
|
if !observed.clipboard_supported.load(Ordering::Acquire){
|
||||||
|
// Host TEXT-channel initialization may finish after the initial CONTROL echo.
|
||||||
|
if retries<5 && last_probe.elapsed()>=Duration::from_secs(1){
|
||||||
|
retries+=1;last_probe=std::time::Instant::now();
|
||||||
|
let Some(control)=control.upgrade() else{break};
|
||||||
|
if send(&control,wire::echo_request_with_clipboard(1,true)).await.is_err(){break}
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if channel.ready_state()!=webrtc::data_channel::data_channel_state::RTCDataChannelState::Open{continue}
|
||||||
|
match backend.read().await {
|
||||||
|
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(None)=>{},Err(_)=>{},
|
||||||
|
}},
|
||||||
|
Ok(None)=>{},Err(())=>{warn();break},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
pub fn sender(&self)->Option<mpsc::Sender<Vec<u8>>>{self.tx.clone()}
|
||||||
|
pub fn stop(&self){if let Some(task)=&self.task{task.abort();}}
|
||||||
|
}
|
||||||
|
impl Drop for Bridge{fn drop(&mut self){self.stop();}}
|
||||||
|
|
||||||
|
#[cfg(test)] mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[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 all=["wl-copy","wl-paste","xclip","xsel"];
|
||||||
|
assert_eq!(select(true,&all).unwrap().write,PathBuf::from("wl-copy"));
|
||||||
|
assert_eq!(select(false,&all).unwrap().write,PathBuf::from("xclip"));
|
||||||
|
assert_eq!(select(true,&["wl-copy","xclip","xsel"]).unwrap().write,PathBuf::from("xclip"));
|
||||||
|
assert_eq!(select(true,&["xsel"]).unwrap().write,PathBuf::from("xsel"));
|
||||||
|
assert!(select(true,&[]).is_none());
|
||||||
|
}
|
||||||
|
#[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!(POLL>=Duration::from_millis(500));
|
||||||
|
let mut state=SyncState::default();
|
||||||
|
assert!(state.local("中文🙂\n".into()).unwrap().is_some());
|
||||||
|
assert!(state.local("中文🙂\n".into()).unwrap().is_none());
|
||||||
|
state.last=Some("remote".into());
|
||||||
|
assert!(state.local("remote".into()).unwrap().is_none());
|
||||||
|
assert!(state.local(String::new()).unwrap().is_some());
|
||||||
|
assert!(state.local(String::new()).unwrap().is_none());
|
||||||
|
assert!(state.local("a".repeat(MAX_TEXT+1)).is_err());
|
||||||
|
}
|
||||||
|
#[test] fn message17_fragments_and_mobile_rpc_keep_unicode_and_request_id(){
|
||||||
|
let mut state=SyncState::default();let value="中文🙂\n".repeat(2200);
|
||||||
|
let parts=text::chunks(&value).unwrap();
|
||||||
|
for (i,part) in parts.iter().enumerate(){
|
||||||
|
let (replies,received)=state.receive(&wire::envelope(1,17,&text::encode(3,part,i+1<parts.len()))).unwrap();
|
||||||
|
assert!(replies.is_empty());
|
||||||
|
assert_eq!(received,if i+1==parts.len(){Some(value.clone())}else{None});
|
||||||
|
}
|
||||||
|
let (replies,received)=state.receive(&wire::envelope(2,21,&text::text_request(73,"mobile"))).unwrap();
|
||||||
|
assert_eq!(received.as_deref(),Some("mobile"));assert_eq!(replies.len(),1);
|
||||||
|
let fields=decode_wire(&replies[0]).unwrap();
|
||||||
|
assert!(fields.iter().any(|f|f.number==22));
|
||||||
|
let WireValue::Bytes(response)=fields.iter().find(|f|f.number==22).unwrap().value else{panic!()};
|
||||||
|
let WireValue::Bytes(header)=decode_wire(response).unwrap().into_iter().find(|f|f.number==1).unwrap().value else{panic!()};
|
||||||
|
assert_eq!(decode_wire(header).unwrap()[0].value,WireValue::Varint(73));
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
// 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){
|
||||||
|
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 offer=if reverse{wire::envelope(1,21,&host[1].offer(1,value.clone()).unwrap())}else{viewer.local(value.clone()).unwrap().unwrap()};
|
||||||
|
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;
|
||||||
|
while let Some((to_viewer,channel,packet))=queue.pop_front(){
|
||||||
|
steps+=1;assert!(steps<20);
|
||||||
|
if to_viewer {
|
||||||
|
// Capture DataConfirm status without reading or logging text.
|
||||||
|
for field in decode_wire(&packet).unwrap(){
|
||||||
|
if let (22,WireValue::Bytes(body))=(field.number,field.value){
|
||||||
|
if let Some(clip)=super::super::tools::blob(body,5){
|
||||||
|
if let Some(confirm)=super::super::tools::blob(clip,2){unavailable|=super::super::tools::uint(confirm,1)==2;}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let (replies,text)=viewer.receive(&packet).unwrap();if text.is_some(){received=text;}
|
||||||
|
queue.extend(replies.into_iter().map(|p|(false,outbound,p)));
|
||||||
|
}else{
|
||||||
|
for field in decode_wire(&packet).unwrap(){let WireValue::Bytes(body)=field.value else{continue};
|
||||||
|
if field.number==21 {let result=host[channel].request(body,&mut ||{id+=1;id}).unwrap();
|
||||||
|
if channel==1 && result.text.is_some(){received=result.text;}
|
||||||
|
queue.extend(result.messages.into_iter().map(|(kind,body)|(true,channel,wire::envelope(1,kind,&body))));
|
||||||
|
}else if field.number==22 {let text=host[channel].response(body).unwrap();if channel==1 && text.is_some(){received=text;}}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if reverse && received.is_some(){viewer.last=Some(value.clone());assert!(viewer.local(value).unwrap().is_none());}
|
||||||
|
(received,unavailable)
|
||||||
|
}
|
||||||
|
#[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,true),(None,true)); // TEXT offer, empty CONTROL provider.
|
||||||
|
}
|
||||||
|
#[test] fn text_clipboard_negotiates_both_directions_with_native_host(){
|
||||||
|
assert_eq!(wire::CHANNELS[CHANNEL],"TEXT_DATA_CHANNEL");
|
||||||
|
for reverse in [false,true]{
|
||||||
|
let (received,unavailable)=native_host_exchange(CHANNEL,reverse);
|
||||||
|
assert_eq!(received.as_deref(),Some("clipboard-中文🙂\n"));assert!(!unavailable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[test] fn clipboard_capability_is_version3_and_can_be_disabled(){
|
||||||
|
assert!(wire::echo_clipboard_supported(&wire::echo_request_with_clipboard(1,true)));
|
||||||
|
assert!(!wire::echo_clipboard_supported(&wire::echo_request_with_clipboard(1,false)));
|
||||||
|
assert!(wire::echo_matches(&wire::echo_response_with_clipboard(2,1,true),1));
|
||||||
|
assert!(!wire::echo_matches(&wire::echo_response_with_clipboard(2,7,true),1));
|
||||||
|
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();
|
||||||
|
shape.feature_flag=vec![[8,99]];
|
||||||
|
let options=shape.connect_options("synthetic-device").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));
|
||||||
|
}
|
||||||
|
#[tokio::test] async fn backend_uses_stdin_preserves_text_and_bounds_reads(){
|
||||||
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
let root=std::env::temp_dir().join(format!("uu-clipboard-mock-{}",wire::uuid()));std::fs::create_dir(&root).unwrap();
|
||||||
|
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::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 mut state=SyncState::default();
|
||||||
|
for value in ["中文🙂\n", "", "literal $(echo secret) `echo secret`"]{
|
||||||
|
state.apply(&backend,value.into()).await.unwrap();
|
||||||
|
let read=backend.read().await.unwrap().unwrap();assert_eq!(read,value);
|
||||||
|
assert!(state.local(read).unwrap().is_none());
|
||||||
|
}
|
||||||
|
let broken=Backend{write:root.join("missing"),..backend.clone()};
|
||||||
|
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"),[255]).unwrap();assert!(backend.read().await.unwrap().is_none());
|
||||||
|
assert!(backend.write(&"a".repeat(MAX_TEXT+1)).await.is_err());
|
||||||
|
std::fs::remove_dir_all(root).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
//! Uses the same optional FFmpeg dependency as the host inproc-av feature.
|
||||||
|
use super::desktop::{Decoder,Frame};
|
||||||
|
use ffmpeg_next as av;
|
||||||
|
pub struct Ffmpeg { decoder:av::decoder::Video }
|
||||||
|
impl Ffmpeg {
|
||||||
|
pub fn new()->Result<Self,String> {
|
||||||
|
av::init().map_err(|_|"FFmpeg initialization failed")?;
|
||||||
|
// Library diagnostics can contain bitstream data; suppress them globally in this viewer process.
|
||||||
|
av::log::set_level(av::log::Level::Quiet);
|
||||||
|
let codec=av::decoder::find(av::codec::Id::H264).ok_or("H264 decoder unavailable")?;
|
||||||
|
let decoder=av::codec::context::Context::new().decoder().open_as(codec).and_then(|d|d.video()).map_err(|_|"H264 decoder initialization failed")?;
|
||||||
|
Ok(Self{decoder})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Decoder for Ffmpeg {
|
||||||
|
fn reset(&mut self){self.decoder.flush();}
|
||||||
|
fn decode(&mut self,au:&[u8])->Result<Vec<Frame>,String>{
|
||||||
|
self.decoder.send_packet(&av::Packet::copy(au)).map_err(|_|"H264 packet rejected")?;
|
||||||
|
let mut frames=Vec::new();let mut decoded=av::frame::Video::empty();
|
||||||
|
loop {
|
||||||
|
match self.decoder.receive_frame(&mut decoded) {
|
||||||
|
Ok(())=>{},
|
||||||
|
Err(av::Error::Other{errno}) if errno==libc::EAGAIN=>break,
|
||||||
|
Err(_)=>return Err("H264 decode failed".into()),
|
||||||
|
}
|
||||||
|
if decoded.is_corrupt(){return Err("Corrupt H264 frame".into())}
|
||||||
|
let (w,h)=(decoded.width(),decoded.height());
|
||||||
|
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 rgb=av::frame::Video::empty();scaler.run(&decoded,&mut rgb).map_err(|_|"Frame conversion failed")?;
|
||||||
|
let mut data=Vec::with_capacity(w as usize*h as usize*3);
|
||||||
|
for row in 0..h as usize {let start=row*rgb.stride(0);data.extend_from_slice(&rgb.data(0)[start..start+w as usize*3]);}
|
||||||
|
frames.push(Frame{width:w,height:h,rgb:data});
|
||||||
|
} Ok(frames)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
//! H.264 decoding through an external `ffmpeg` process, like the host's encoder pipeline.
|
||||||
|
//! Needs only the ffmpeg CLI (any recent version), not FFmpeg development libraries.
|
||||||
|
//! Output is scaled into a fixed letterboxed size so every frame has a known byte length.
|
||||||
|
use super::desktop::{Decoder,Frame};
|
||||||
|
use std::io::{Read,Write};
|
||||||
|
use std::process::{Child,ChildStdin,Command,Stdio};
|
||||||
|
use std::sync::mpsc::{Receiver,TryRecvError};
|
||||||
|
|
||||||
|
pub struct Cli { child:Child, input:ChildStdin, frames:Receiver<Vec<u8>>, width:u32, height:u32 }
|
||||||
|
|
||||||
|
impl Cli {
|
||||||
|
/// `UU_VIEWER_SIZE=WxH` picks the decoded size (default 1920x1080); `UU_VIEWER_FFMPEG` the binary.
|
||||||
|
pub fn new()->Result<Self,String> {
|
||||||
|
let (width,height)=std::env::var("UU_VIEWER_SIZE").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)
|
||||||
|
.unwrap_or((1920,1080));
|
||||||
|
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 mut child=Command::new(ffmpeg)
|
||||||
|
.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,
|
||||||
|
"-pix_fmt","rgb24",
|
||||||
|
// Raw H.264 carries no timestamps; constant-rate output dropped almost every frame of
|
||||||
|
// the official Mac host's stream (270 → 2). Pass frames through unchanged.
|
||||||
|
"-fps_mode","passthrough","-f","rawvideo","pipe:1"])
|
||||||
|
.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::null())
|
||||||
|
.spawn().map_err(|e|format!("Start ffmpeg decoder: {e}"))?;
|
||||||
|
let input=child.stdin.take().ok_or("ffmpeg stdin missing")?;
|
||||||
|
let mut output=child.stdout.take().ok_or("ffmpeg stdout missing")?;
|
||||||
|
let size=width as usize*height as usize*3;
|
||||||
|
// Bounded: a slow window drops decoded frames instead of growing memory.
|
||||||
|
let (tx,frames)=std::sync::mpsc::sync_channel(2);
|
||||||
|
std::thread::Builder::new().name("uu-viewer-decode".into()).spawn(move||{
|
||||||
|
loop {
|
||||||
|
let mut frame=vec![0;size];
|
||||||
|
if output.read_exact(&mut frame).is_err() {break;}
|
||||||
|
match tx.try_send(frame) { Ok(())|Err(std::sync::mpsc::TrySendError::Full(_))=>{}, Err(_)=>break }
|
||||||
|
}
|
||||||
|
}).map_err(|e|format!("Decoder reader: {e}"))?;
|
||||||
|
Ok(Self{child,input,frames,width,height})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decoder for Cli {
|
||||||
|
fn reset(&mut self) {}
|
||||||
|
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 mut out=Vec::new();
|
||||||
|
loop {
|
||||||
|
match self.frames.try_recv() {
|
||||||
|
Ok(rgb)=>out.push(Frame{width:self.width,height:self.height,rgb}),
|
||||||
|
Err(TryRecvError::Empty)=>break,
|
||||||
|
Err(TryRecvError::Disconnected)=>return Err("ffmpeg decoder exited".into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Cli { fn drop(&mut self) { let _=self.child.kill(); let _=self.child.wait(); } }
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
use super::media::Latest;
|
||||||
|
use std::sync::{Arc, atomic::{AtomicBool,AtomicU32,Ordering}};
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Desktop { pub units:Latest<Vec<u8>>, pub pli:Arc<AtomicBool>, pub reset:Arc<AtomicBool>, pub ssrc:Arc<AtomicU32> }
|
||||||
|
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))}}
|
||||||
|
}
|
||||||
|
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 FrameSink { fn present(&mut self,frame:&Frame)->Result<(),String>; }
|
||||||
|
/// 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 fn display(decoder:&mut dyn Decoder,sink:&mut dyn FrameSink,au:&[u8],pli:&AtomicBool)->Result<(),String> {
|
||||||
|
STATS[0].fetch_add(1,Ordering::Relaxed);
|
||||||
|
// 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") {
|
||||||
|
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) {
|
||||||
|
Ok(frames)=>{for frame in frames {
|
||||||
|
sink.present(&frame)?;
|
||||||
|
// Local acceptance aid: UU_VIEWER_DUMP_FRAME=path writes the 5th frame as PPM (owner's own screen).
|
||||||
|
if STATS[1].fetch_add(1,Ordering::Relaxed)==std::env::var("UU_VIEWER_DUMP_FRAME_INDEX").ok().and_then(|v|v.parse().ok()).unwrap_or(4u64) { if let Some(path)=std::env::var_os("UU_VIEWER_DUMP_FRAME") {
|
||||||
|
let mut ppm=format!("P6\n{} {}\n255\n",frame.width,frame.height).into_bytes();ppm.extend_from_slice(&frame.rgb);let _=std::fs::write(path,ppm);
|
||||||
|
}}
|
||||||
|
}Ok(())},
|
||||||
|
Err(_)=>{STATS[2].fetch_add(1,Ordering::Relaxed);decoder.reset();pli.store(true,Ordering::Release);Ok(())},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[cfg(test)] mod tests {
|
||||||
|
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 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);}
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
//! SDL scancodes are USB HID usages; translate physical keys, not typed text.
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
use std::collections::{BTreeMap,BTreeSet};
|
||||||
|
pub fn windows_vk(scan: i32) -> Option<i32> {
|
||||||
|
Some(match scan {
|
||||||
|
4..=29 => 0x41 + scan - 4, 30..=38 => 0x31 + scan - 30, 39 => 0x30,
|
||||||
|
40=>0x0d,41=>0x1b,42=>8,43=>9,44=>0x20,
|
||||||
|
45=>0xbd,46=>0xbb,47=>0xdb,48=>0xdd,49=>0xdc,51=>0xba,52=>0xde,53=>0xc0,54=>0xbc,55=>0xbe,56=>0xbf,
|
||||||
|
57=>0x14,58..=69=>0x70+scan-58,70=>0x2c,71=>0x91,72=>0x13,
|
||||||
|
73=>0x2d,74=>0x24,75=>0x21,76=>0x2e,77=>0x23,78=>0x22,
|
||||||
|
79=>0x27,80=>0x25,81=>0x28,82=>0x26,83=>0x90,
|
||||||
|
84=>0x6f,85=>0x6a,86=>0x6d,87=>0x6b,88=>0x0d,
|
||||||
|
89..=97=>0x61+scan-89,98=>0x60,99=>0x6e,101=>0x5d,
|
||||||
|
104..=115=>0x7c+scan-104,
|
||||||
|
224=>0xa2,225=>0xa0,226=>0xa4,227=>0x5b,228=>0xa3,229=>0xa1,230=>0xa5,231=>0x5c,
|
||||||
|
_=>return None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
#[derive(Clone,Copy,Debug,PartialEq)]
|
||||||
|
pub struct Viewport { pub x:f64, pub y:f64, pub width:f64, pub height:f64 }
|
||||||
|
impl Viewport {
|
||||||
|
pub fn fit(window:(u32,u32), frame:(u32,u32))->Self {
|
||||||
|
let scale=(window.0 as f64 / frame.0.max(1) as f64).min(window.1 as f64 / frame.1.max(1) as f64);
|
||||||
|
let (w,h)=(frame.0 as f64*scale,frame.1 as f64*scale);
|
||||||
|
Self{x:(window.0 as f64-w)/2.,y:(window.1 as f64-h)/2.,width:w,height:h}
|
||||||
|
}
|
||||||
|
pub fn motion(self,x:f64,y:f64)->Option<Value> {
|
||||||
|
if self.width<=0. || self.height<=0. || !x.is_finite() || !y.is_finite() {return None}
|
||||||
|
Some(json!({"action":"mouse_move_absolute","abs_x":((x-self.x)/self.width).clamp(0.,1.),"abs_y":((y-self.y)/self.height).clamp(0.,1.)}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct Input { view_only:bool, keys:BTreeMap<i32,i32>, buttons:BTreeSet<i32> }
|
||||||
|
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())}
|
||||||
|
}
|
||||||
|
impl Input {
|
||||||
|
pub fn new(view_only:bool)->Self {Self{view_only,..Default::default()}}
|
||||||
|
pub fn enabled(&self)->bool {!self.view_only}
|
||||||
|
pub fn key(&mut self,scan:i32,down:bool)->Option<Value> {
|
||||||
|
if self.view_only {return None}
|
||||||
|
let vk=windows_vk(scan)?;
|
||||||
|
let changed=if down {
|
||||||
|
if self.keys.contains_key(&scan) {return None}
|
||||||
|
let already=self.keys.values().any(|v|*v==vk);self.keys.insert(scan,vk);!already
|
||||||
|
}else{
|
||||||
|
if self.keys.remove(&scan).is_none(){return None}
|
||||||
|
!self.keys.values().any(|v|*v==vk)
|
||||||
|
};
|
||||||
|
changed.then(||json!({"action":if down{"kbd_press"}else{"kbd_release"},"key":vk}))
|
||||||
|
}
|
||||||
|
pub fn button(&mut self,button:i32,down:bool)->Option<Value> {
|
||||||
|
if self.view_only {return None}
|
||||||
|
if !matches!(button,1|2|4) {return None}
|
||||||
|
let changed=if down{self.buttons.insert(button)}else{self.buttons.remove(&button)};
|
||||||
|
changed.then(||json!({"action":if down{"mouse_press"}else{"mouse_release"},"button":button}))
|
||||||
|
}
|
||||||
|
pub fn release_all(&mut self)->Vec<Value> {
|
||||||
|
let mut out=Vec::new();
|
||||||
|
for key in std::mem::take(&mut self.keys).into_values().collect::<BTreeSet<_>>() {out.push(json!({"action":"kbd_release","key":key}));}
|
||||||
|
for button in std::mem::take(&mut self.buttons) {out.push(json!({"action":"mouse_release","button":button}));}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
pub fn heartbeat()->Value {json!({"heartbeat":"kbd_heartbeat"})}
|
||||||
|
}
|
||||||
|
#[cfg(test)] mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[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,Some("1")).unwrap());assert!(view_only(Some("--view-only"),Some("0")).unwrap());
|
||||||
|
assert!(view_only(Some("invalid"),None).is_err());
|
||||||
|
}
|
||||||
|
#[test] fn mappings_encoding_and_focus() {
|
||||||
|
for (scan,vk) in [(4,65),(29,90),(39,48),(58,112),(80,37),(224,162),(230,165),(52,222)] {assert_eq!(windows_vk(scan),Some(vk));}
|
||||||
|
assert_eq!(windows_vk(100),None);
|
||||||
|
let mut i=Input::default();assert_eq!(i.key(4,true).unwrap(),json!({"action":"kbd_press","key":65}));assert!(i.key(4,true).is_none());
|
||||||
|
i.key(225,true);i.button(2,true);let release=i.release_all();assert_eq!(release.len(),3);
|
||||||
|
assert!(release.iter().all(|v|v["action"].as_str().unwrap().ends_with("release")));
|
||||||
|
assert!(i.release_all().is_empty());
|
||||||
|
assert_eq!(Input::heartbeat(),json!({"heartbeat":"kbd_heartbeat"}));
|
||||||
|
}
|
||||||
|
#[test] fn aliased_physical_keys_release_only_after_last_keyup() {
|
||||||
|
let mut i=Input::default();assert!(i.key(40,true).is_some());assert!(i.key(88,true).is_none());
|
||||||
|
assert!(i.key(40,false).is_none());assert_eq!(i.key(88,false).unwrap(),json!({"action":"kbd_release","key":13}));
|
||||||
|
assert!(i.button(3,true).is_none());assert_eq!(i.button(4,true).unwrap()["button"],4);
|
||||||
|
}
|
||||||
|
#[test] fn letterbox() {
|
||||||
|
let v=Viewport::fit((1000,1000),(1920,1080));
|
||||||
|
assert_eq!(v.motion(500.,500.).unwrap()["abs_y"],0.5);
|
||||||
|
assert_eq!(v.motion(-10.,0.).unwrap()["abs_y"],0.0);
|
||||||
|
assert_eq!(v.motion(1010.,1000.).unwrap()["abs_x"],1.0);
|
||||||
|
assert!(v.motion(f64::NAN,0.).is_none());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
//! Bounded RFC 6184 packetization-mode 1 receiver. No media contents in logs.
|
||||||
|
use std::{collections::VecDeque, sync::{Arc, Mutex}};
|
||||||
|
use std::time::{Duration,Instant};
|
||||||
|
const MAX_AU: usize = 8 * 1024 * 1024;
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Packet { pub seq: u16, pub timestamp: u32, pub marker: bool, pub payload: Vec<u8> }
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct Output { pub units: Vec<Vec<u8>>, pub pli: bool }
|
||||||
|
pub struct Receiver {
|
||||||
|
next: Option<u16>, pending: Vec<Packet>, timestamp: Option<u32>,
|
||||||
|
au: Vec<u8>, fragment: Option<u8>, damaged: bool, gap_since:Option<Instant>, last_packet:Instant,
|
||||||
|
}
|
||||||
|
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() } }
|
||||||
|
}
|
||||||
|
impl Receiver {
|
||||||
|
pub fn push(&mut self, p: Packet) -> Output {
|
||||||
|
if p.payload.len()>65536 {return Output{units:Vec::new(),pli:true}}
|
||||||
|
let next = *self.next.get_or_insert(p.seq);
|
||||||
|
if p.seq.wrapping_sub(next) >= 32768 || self.pending.iter().any(|q| q.seq == p.seq) { return Output::default() }
|
||||||
|
self.last_packet=Instant::now();
|
||||||
|
self.pending.push(p);
|
||||||
|
let mut out = Output::default();
|
||||||
|
self.drain(&mut out);
|
||||||
|
if self.pending.len() >= 32 { self.skip_gap(&mut out); }
|
||||||
|
if self.pending.is_empty(){self.gap_since=None;}else{self.gap_since.get_or_insert(Instant::now());}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
// A timer bounds latency even if the missing packet is the last packet sent.
|
||||||
|
pub fn expire(&mut self) -> Output {
|
||||||
|
self.expire_at(Instant::now())
|
||||||
|
}
|
||||||
|
fn expire_at(&mut self,now:Instant)->Output {
|
||||||
|
let mut out=Output::default();
|
||||||
|
if self.gap_since.is_some_and(|since|now.duration_since(since)>=Duration::from_millis(40)) && !self.pending.is_empty() {
|
||||||
|
self.skip_gap(&mut out);self.gap_since=if self.pending.is_empty(){None}else{Some(now)};
|
||||||
|
}
|
||||||
|
if !self.au.is_empty() && now.duration_since(self.last_packet)>=Duration::from_millis(200) {
|
||||||
|
self.au.clear();self.fragment=None;self.damaged=true;out.pli=true;
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
fn skip_gap(&mut self, out: &mut Output) {
|
||||||
|
let next = self.next.unwrap();
|
||||||
|
self.next = self.pending.iter().min_by_key(|p| p.seq.wrapping_sub(next)).map(|p| p.seq);
|
||||||
|
self.au.clear(); self.fragment = None; self.damaged = true; out.pli = true;
|
||||||
|
self.drain(out);
|
||||||
|
}
|
||||||
|
fn drain(&mut self, out: &mut Output) {
|
||||||
|
while let Some(i) = self.pending.iter().position(|p| Some(p.seq) == self.next) {
|
||||||
|
let p = self.pending.remove(i); self.next = Some(p.seq.wrapping_add(1));
|
||||||
|
if self.timestamp != Some(p.timestamp) {
|
||||||
|
// A timestamp transition without marker is an incomplete AU.
|
||||||
|
if !self.au.is_empty() || self.fragment.is_some() { out.pli = true; }
|
||||||
|
self.au.clear(); self.fragment = None; self.damaged = false; self.timestamp = Some(p.timestamp);
|
||||||
|
}
|
||||||
|
if !self.damaged && self.nal(&p.payload).is_err() { self.damaged = true; self.au.clear(); self.fragment = None; out.pli = true; }
|
||||||
|
if p.marker {
|
||||||
|
if self.fragment.is_some() { out.pli = true; self.damaged = true; }
|
||||||
|
if !self.damaged && !self.au.is_empty() { out.units.push(std::mem::take(&mut self.au)); }
|
||||||
|
self.au.clear(); self.fragment = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn append(&mut self, bytes: &[u8]) -> Result<(), ()> {
|
||||||
|
if self.au.len() + bytes.len() > MAX_AU { return Err(()) }
|
||||||
|
self.au.extend_from_slice(bytes); Ok(())
|
||||||
|
}
|
||||||
|
fn nal(&mut self, p: &[u8]) -> Result<(), ()> {
|
||||||
|
let h = *p.first().ok_or(())?;
|
||||||
|
if h & 0x80 != 0 { return Err(()) }
|
||||||
|
match h & 31 {
|
||||||
|
1..=23 if self.fragment.is_none() => { self.append(&[0,0,0,1])?; self.append(p) }
|
||||||
|
24 if self.fragment.is_none() => {
|
||||||
|
let mut rest = &p[1..]; 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 == 0 || rest.len() < n || rest[0] & 0x80 != 0 || !(1..=23).contains(&(rest[0] & 31)) { return Err(()) }
|
||||||
|
self.append(&[0,0,0,1])?; self.append(&rest[..n])?; rest = &rest[n..];
|
||||||
|
} Ok(())
|
||||||
|
}
|
||||||
|
28 if p.len() > 2 => {
|
||||||
|
let f = p[1]; let kind = f & 31; let start = f & 128 != 0; let end = f & 64 != 0;
|
||||||
|
if f & 32 != 0 || kind == 0 || kind > 23 || (start && end) { return Err(()) }
|
||||||
|
let header = (h & 0xe0) | kind;
|
||||||
|
if start {
|
||||||
|
if self.fragment.is_some() { return Err(()) }
|
||||||
|
self.append(&[0,0,0,1,header])?; self.fragment = Some(header);
|
||||||
|
} else if self.fragment != Some(header) { return Err(()) }
|
||||||
|
self.append(&p[2..])?;
|
||||||
|
if end { self.fragment = None; } Ok(())
|
||||||
|
}
|
||||||
|
_ => Err(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Drop the oldest item on overflow; never block the RTP reader on a decoder/UI.
|
||||||
|
pub struct Latest<T>(Arc<Mutex<VecDeque<T>>>, usize);
|
||||||
|
impl<T> Clone for Latest<T> { fn clone(&self) -> Self { Self(self.0.clone(), self.1) } }
|
||||||
|
impl<T> Latest<T> {
|
||||||
|
pub fn new(capacity: usize) -> Self { assert!(capacity > 0); Self(Arc::new(Mutex::new(VecDeque::new())), capacity) }
|
||||||
|
pub fn push(&self, value: T) -> bool { let mut q = self.0.lock().unwrap(); let dropped = q.len() == self.1; if dropped { q.pop_front(); } q.push_back(value); dropped }
|
||||||
|
pub fn pop(&self) -> Option<T> { self.0.lock().unwrap().pop_front() }
|
||||||
|
}
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
fn p(seq:u16, ts:u32, marker:bool, payload:&[u8])->Packet { Packet{seq,timestamp:ts,marker,payload:payload.into()} }
|
||||||
|
#[test] fn single_stap_and_boundary() {
|
||||||
|
let mut r=Receiver::default();
|
||||||
|
assert!(r.push(p(1,1,false,&[0x67,1])).units.is_empty());
|
||||||
|
assert_eq!(r.push(p(2,1,true,&[24,0,2,0x68,2,0,2,0x65,3])).units[0],vec![0,0,0,1,0x67,1,0,0,0,1,0x68,2,0,0,0,1,0x65,3]);
|
||||||
|
r.push(p(3,2,false,&[0x61,1]));
|
||||||
|
let o=r.push(p(4,3,true,&[0x65,4])); assert!(o.pli); assert_eq!(o.units.len(),1);
|
||||||
|
}
|
||||||
|
#[test] fn fu_reorder_wrap_duplicate_and_loss() {
|
||||||
|
let mut r=Receiver::default(); r.push(p(65535,1,false,&[0x7c,0x85,1]));
|
||||||
|
assert!(r.push(p(1,1,true,&[0x7c,0x45,3])).units.is_empty());
|
||||||
|
let o=r.push(p(0,1,false,&[0x7c,5,2])); assert_eq!(o.units[0],vec![0,0,0,1,0x65,1,2,3]);
|
||||||
|
assert!(r.push(p(0,1,false,&[0x61])).units.is_empty());
|
||||||
|
r.push(p(2,2,false,&[0x7c,0x85,1]));r.push(p(4,2,true,&[0x7c,0x45,3]));
|
||||||
|
let o=r.expire_at(Instant::now()+Duration::from_millis(50));assert!(o.pli);assert!(o.units.is_empty());
|
||||||
|
assert_eq!(r.push(p(5,3,true,&[0x65])).units.len(),1);
|
||||||
|
}
|
||||||
|
#[test] fn missing_tail_and_reorder_window_are_bounded() {
|
||||||
|
let mut r=Receiver::default();r.push(p(0,1,false,&[0x7c,0x85,1]));
|
||||||
|
assert!(!r.expire().pli);assert!(r.expire_at(Instant::now()+Duration::from_millis(201)).pli);
|
||||||
|
let mut pli=false;for seq in 2..34{pli|=r.push(p(seq,seq as u32,true,&[0x65])).pli;}
|
||||||
|
assert!(pli);assert!(r.pending.len()<32);
|
||||||
|
}
|
||||||
|
#[test] fn malformed_and_bounded() {
|
||||||
|
for bytes in [vec![],vec![24,0,4,0x65],vec![0x7c,0xc5,1],vec![0xff]] {
|
||||||
|
assert!(Receiver::default().push(p(1,1,true,&bytes)).pli);
|
||||||
|
}
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
//! Opt-in controller session; independent of the host room and peer lifecycle.
|
||||||
|
pub mod api;
|
||||||
|
pub mod assist;
|
||||||
|
pub mod wire;
|
||||||
|
pub mod signal;
|
||||||
|
pub mod rtc;
|
||||||
|
pub mod session;
|
||||||
|
#[cfg(test)] mod tests;
|
||||||
|
|
||||||
|
pub mod media;
|
||||||
|
pub mod audio;
|
||||||
|
pub mod input;
|
||||||
|
pub mod desktop;
|
||||||
|
#[cfg(feature="viewer-ffmpeg")] pub mod decoder;
|
||||||
|
#[cfg(feature="viewer-sdl")] pub mod decoder_cli;
|
||||||
|
#[cfg(uu_gtk)] pub mod ui;
|
||||||
|
#[cfg(uu_gtk)] pub mod tools_ui;
|
||||||
|
#[cfg(feature="viewer-sdl")] pub mod sdl;
|
||||||
|
|
||||||
|
pub mod tools;
|
||||||
|
|
||||||
|
pub mod clipboard;
|
||||||
|
pub mod power;
|
||||||
|
|
||||||
|
fn device_label(name:Option<&str>)->&str{
|
||||||
|
name.filter(|name|!name.is_empty()).unwrap_or("远端设备")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod device_label_tests {
|
||||||
|
#[test]
|
||||||
|
fn device_name_or_fallback(){
|
||||||
|
assert_eq!(super::device_label(Some("测试电脑")),"测试电脑");
|
||||||
|
assert_eq!(super::device_label(None),"远端设备");
|
||||||
|
assert_eq!(super::device_label(Some("")),"远端设备");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
//! SimpleAction numbers are from src/system_actions.rs (REBOOT_SYSTEM / SHUTDOWN_SYSTEM).
|
||||||
|
use super::{session::Session,wire};
|
||||||
|
use std::{sync::atomic::Ordering,time::Duration};
|
||||||
|
#[derive(Clone,Copy,Debug,PartialEq)]
|
||||||
|
pub enum Action {Reboot,Shutdown}
|
||||||
|
impl Action {
|
||||||
|
pub fn parse(value:&str)->Result<Self,String>{match value{"reboot"=>Ok(Self::Reboot),"shutdown"=>Ok(Self::Shutdown),_=>Err("Power action must be reboot or shutdown".into())}}
|
||||||
|
pub fn message(self,seq:u64)->Vec<u8>{
|
||||||
|
let mut action=Vec::new();wire::int(1,match self{Self::Reboot=>9,Self::Shutdown=>12},&mut action);
|
||||||
|
wire::envelope(seq,3,&action)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub async fn run(session:&mut Session,action:Action)->Result<(),String>{
|
||||||
|
let channel=session.peer.channels[0].clone();
|
||||||
|
let ready=async{
|
||||||
|
loop {
|
||||||
|
if session.peer.observed.control_open.load(Ordering::Acquire) && session.peer.observed.echo_received.load(Ordering::Acquire){return Ok::<_,String>(())}
|
||||||
|
tokio::select!{r=session.step()=>r?,_=tokio::time::sleep(Duration::from_millis(20))=>{}}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let send=async move {
|
||||||
|
tokio::time::timeout(Duration::from_secs(5),channel.send(&bytes::Bytes::from(action.message(2))))
|
||||||
|
.await.map_err(|_|"Power command send timed out")?.map_err(|_|"Power command send failed")?;
|
||||||
|
// Drain SCTP locally before closing; this is not a remote execution acknowledgement.
|
||||||
|
let drain=async{while channel.buffered_amount().await>0 {tokio::time::sleep(Duration::from_millis(20)).await;}};
|
||||||
|
tokio::time::timeout(Duration::from_secs(2),drain).await.map_err(|_|"Power command drain timed out")?;
|
||||||
|
tokio::time::sleep(Duration::from_millis(200)).await;Ok(())
|
||||||
|
};
|
||||||
|
after_echo(ready,send,Duration::from_secs(30)).await
|
||||||
|
}
|
||||||
|
async fn after_echo(ready:impl std::future::Future<Output=Result<(),String>>,send:impl std::future::Future<Output=Result<(),String>>,timeout:Duration)->Result<(),String>{
|
||||||
|
tokio::time::timeout(timeout,ready).await.map_err(|_|"CONTROL echo timed out")??;
|
||||||
|
send.await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)] mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::protocol::{decode_wire,WireValue};
|
||||||
|
#[test] fn only_host_evidenced_actions_are_encoded(){
|
||||||
|
for (name,number) in [("reboot",9),("shutdown",12)]{
|
||||||
|
let bytes=Action::parse(name).unwrap().message(2);let fields=decode_wire(&bytes).unwrap();
|
||||||
|
let WireValue::Bytes(body)=fields.iter().find(|f|f.number==3).unwrap().value else{panic!()};
|
||||||
|
assert_eq!(decode_wire(body).unwrap()[0].value,WireValue::Varint(number));
|
||||||
|
let host=include_str!("../system_actions.rs");
|
||||||
|
assert!(host.contains(&format!("pub const {}_SYSTEM: u64 = {number};",name.to_uppercase())));
|
||||||
|
}
|
||||||
|
for value in ["logout","wol","9",""]{assert!(Action::parse(value).is_err());}
|
||||||
|
}
|
||||||
|
#[tokio::test] async fn waits_for_echo_and_sends_once_without_retrying_failure(){
|
||||||
|
use std::sync::{Arc,atomic::AtomicUsize};
|
||||||
|
for fail in [false,true]{
|
||||||
|
let calls=Arc::new(AtomicUsize::new(0));let recorded=calls.clone();
|
||||||
|
let(tx,rx)=tokio::sync::oneshot::channel();
|
||||||
|
let operation=tokio::spawn(after_echo(async{rx.await.map_err(|_|"closed".into())},async move{
|
||||||
|
recorded.fetch_add(1,Ordering::Relaxed);if fail{Err("send failed".into())}else{Ok(())}
|
||||||
|
},Duration::from_secs(1)));
|
||||||
|
tokio::task::yield_now().await;assert_eq!(calls.load(Ordering::Relaxed),0);
|
||||||
|
tx.send(()).unwrap();assert_eq!(operation.await.unwrap().is_err(),fail);
|
||||||
|
assert_eq!(calls.load(Ordering::Relaxed),1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[tokio::test] async fn failed_or_missing_echo_never_sends(){
|
||||||
|
for failed in [false,true]{
|
||||||
|
let called=std::sync::atomic::AtomicBool::new(false);
|
||||||
|
let result=after_echo(async{if failed{return Err("connection closed".into())}std::future::pending().await},async{called.store(true,Ordering::Relaxed);Ok(())},Duration::from_millis(5)).await;
|
||||||
|
assert!(result.is_err());assert!(!called.load(Ordering::Relaxed));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,315 @@
|
|||||||
|
use super::wire::{self, Shape, CHANNELS};
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
use std::sync::{Arc,atomic::{AtomicBool,AtomicU64,Ordering}};
|
||||||
|
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}};
|
||||||
|
|
||||||
|
pub fn configuration(result:&Value)->Result<RTCConfiguration,String> {
|
||||||
|
let list=result.get("iceServers").and_then(Value::as_array).ok_or("Control acknowledgement missing iceServers")?;
|
||||||
|
let mut servers=Vec::new();
|
||||||
|
for server in list {
|
||||||
|
let urls=match server.get("urls") {
|
||||||
|
Some(Value::String(s))=>vec![s.clone()],
|
||||||
|
Some(Value::Array(a))=>a.iter().map(|v|v.as_str().map(str::to_owned).ok_or("Invalid ICE URL")).collect::<Result<Vec<_>,_>>()?,
|
||||||
|
_=>return Err("Invalid ICE URLs".into()),
|
||||||
|
};
|
||||||
|
if urls.is_empty() || urls.iter().any(|u| !["stun:","stuns:","turn:","turns:"].iter().any(|p|u.starts_with(p))) {return Err("Invalid ICE URL scheme".into())}
|
||||||
|
let string=|key|match server.get(key) {Some(Value::String(s))=>Ok(s.clone()),None=>Ok(String::new()),_=>Err("Invalid ICE authentication")};
|
||||||
|
// webrtc-rs rejects TURN unless the credential type is Password (as in the host's session_ice.rs).
|
||||||
|
servers.push(RTCIceServer { urls,username:string("username")?,credential:string("credential")?,
|
||||||
|
credential_type:webrtc::ice_transport::ice_credential_type::RTCIceCredentialType::Password });
|
||||||
|
}
|
||||||
|
let relay=match result.get("force_relay") {None=>false,Some(Value::Bool(b))=>*b,_=>return Err("Invalid relay policy".into())};
|
||||||
|
if relay && !servers.iter().any(|s|s.urls.iter().any(|u|u.starts_with("turn:")||u.starts_with("turns:"))) {return Err("Relay-only session without TURN".into())}
|
||||||
|
// An empty cloud list means host candidates only. Never invent public STUN.
|
||||||
|
Ok(RTCConfiguration{ice_servers:servers,ice_transport_policy:if relay{RTCIceTransportPolicy::Relay}else{RTCIceTransportPolicy::All},..Default::default()})
|
||||||
|
}
|
||||||
|
#[derive(Clone,Copy)]
|
||||||
|
pub enum ReadyPolicy { EchoOnly, Explicit }
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct Observed {
|
||||||
|
pub control_open:AtomicBool,
|
||||||
|
pub echo_received:AtomicBool,
|
||||||
|
pub clipboard_supported:AtomicBool,
|
||||||
|
pub control_messages:AtomicU64,
|
||||||
|
pub other_messages:AtomicU64,
|
||||||
|
pub media_packets:AtomicU64,
|
||||||
|
}
|
||||||
|
pub struct Peer {
|
||||||
|
clipboard:super::clipboard::Bridge,
|
||||||
|
pub tools:super::tools::Inbox,
|
||||||
|
pub pc:Arc<RTCPeerConnection>,
|
||||||
|
pub channels:Vec<Arc<RTCDataChannel>>,
|
||||||
|
pub observed:Arc<Observed>,
|
||||||
|
pub desktop:super::desktop::Desktop,
|
||||||
|
pub audio:super::audio::Audio,
|
||||||
|
answered:bool,
|
||||||
|
early:Vec<RTCIceCandidateInit>,
|
||||||
|
app_id:String,
|
||||||
|
ice_id:String,
|
||||||
|
remote_client:String,
|
||||||
|
}
|
||||||
|
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> {
|
||||||
|
shape.validate_session()?;
|
||||||
|
let mut engine=MediaEngine::default();
|
||||||
|
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).
|
||||||
|
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")?;
|
||||||
|
}
|
||||||
|
let registry=webrtc::api::interceptor_registry::register_default_interceptors(webrtc::interceptor::registry::Registry::new(),&mut engine).map_err(|_|"RTP interceptor initialization failed")?;
|
||||||
|
let api=APIBuilder::new().with_media_engine(engine).with_setting_engine(settings).with_interceptor_registry(registry).build();
|
||||||
|
let pc=Arc::new(api.new_peer_connection(config).await.map_err(|e|format!("Peer creation failed: {}",crate::auth::transport_error(&e)))?);
|
||||||
|
let tools:super::tools::Inbox=Arc::new(std::sync::Mutex::new(None));
|
||||||
|
let inbox=tools.clone();
|
||||||
|
pc.on_peer_connection_state_change(Box::new(move |state|{
|
||||||
|
use webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState;
|
||||||
|
log::info!("Viewer peer connection state: {state}");
|
||||||
|
if matches!(state,RTCPeerConnectionState::Failed|RTCPeerConnectionState::Closed){inbox.lock().unwrap().take();}
|
||||||
|
Box::pin(async{})
|
||||||
|
}));
|
||||||
|
let observed=Arc::new(Observed::default());
|
||||||
|
let (app,ice,client)=(app_id.to_owned(),ice_id.to_owned(),remote_client.to_owned());
|
||||||
|
let weak_pc=Arc::downgrade(&pc);
|
||||||
|
pc.on_ice_candidate(Box::new(move |candidate| {
|
||||||
|
let weak_pc=weak_pc.clone();let tx=outbound.clone();let(app,ice,client)=(app.clone(),ice.clone(),client.clone());
|
||||||
|
Box::pin(async move {if let Some(candidate)=candidate {if let Ok(mut value)=candidate.to_json() {
|
||||||
|
// Candidate callbacks can omit MID. Bind to the first actual bundled m-line.
|
||||||
|
if let Some(pc)=weak_pc.upgrade() {
|
||||||
|
if let Some(local)=pc.local_description().await {
|
||||||
|
if let Some(mid)=local.sdp.lines().find_map(|line|line.strip_prefix("a=mid:")) {
|
||||||
|
value.sdp_mid=Some(mid.trim().into());value.sdp_mline_index=Some(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let _=tx.send(json!({"client_id":client,"data":{"type":"candidate","candidate":value,"ice_id":ice,"app_control_id":app}})).await;
|
||||||
|
}}})
|
||||||
|
}));
|
||||||
|
let counters=observed.clone();
|
||||||
|
let desktop=super::desktop::Desktop::default();
|
||||||
|
let audio=super::audio::Audio::default();let sound=audio.clone();
|
||||||
|
let receiver=desktop.clone();let weak_pc=Arc::downgrade(&pc);
|
||||||
|
pc.on_track(Box::new(move |track,_,_| {
|
||||||
|
let counters=counters.clone();let desktop=receiver.clone();let audio=sound.clone();let weak_pc=weak_pc.clone();
|
||||||
|
// The handler must return: webrtc-rs awaits it before surfacing the next track,
|
||||||
|
// so an inline read loop on the audio track starved the video track.
|
||||||
|
Box::pin(async move { tokio::spawn(async move {
|
||||||
|
let video=track.kind()==RTPCodecType::Video;
|
||||||
|
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");
|
||||||
|
if video && !track.codec().capability.mime_type.eq_ignore_ascii_case("video/H264") {return}
|
||||||
|
if video {desktop.ssrc.store(track.ssrc(),Ordering::Release);}
|
||||||
|
let mut receiver=super::media::Receiver::default();
|
||||||
|
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);
|
||||||
|
loop {
|
||||||
|
let output=tokio::select! {
|
||||||
|
packet=track.read_rtp()=>match packet {
|
||||||
|
Ok((p,_))=>{
|
||||||
|
counters.media_packets.fetch_add(1,Ordering::Relaxed);
|
||||||
|
if !video {if opus {audio.push(p.header.sequence_number,&p.payload);}continue}
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
_=tick.tick()=>{if !video {continue} receiver.expire()},
|
||||||
|
};
|
||||||
|
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);}}
|
||||||
|
if desktop.pli.load(Ordering::Acquire) && last_pli.elapsed()>=std::time::Duration::from_millis(250) {
|
||||||
|
desktop.pli.swap(false,Ordering::AcqRel);
|
||||||
|
if let Some(pc)=weak_pc.upgrade() {
|
||||||
|
use webrtc::rtcp::payload_feedbacks::picture_loss_indication::PictureLossIndication;
|
||||||
|
let _=pc.write_rtcp(&[Box::new(PictureLossIndication{sender_ssrc:0,media_ssrc:track.ssrc()})]).await;
|
||||||
|
} else {break}
|
||||||
|
last_pli=std::time::Instant::now();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}); })
|
||||||
|
}));
|
||||||
|
let result=async {
|
||||||
|
if shape.capture_type==1 {
|
||||||
|
// The official Mac viewer offers five recvonly video sections (observed live).
|
||||||
|
let videos=std::env::var("UU_VIEWER_VIDEO_SECTIONS").ok().and_then(|v|v.parse::<usize>().ok()).filter(|n|(1..=8).contains(n)).unwrap_or(1);
|
||||||
|
for kind in std::iter::repeat(RTPCodecType::Video).take(videos).chain([RTPCodecType::Audio]) {
|
||||||
|
pc.add_transceiver_from_kind(kind,Some(RTCRtpTransceiverInit{direction:RTCRtpTransceiverDirection::Recvonly,send_encodings:vec![]})).await.map_err(|_|"Media transceiver failed")?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut channels=Vec::new();
|
||||||
|
let mut clipboard=super::clipboard::Bridge::new(shape.capture_type==1);
|
||||||
|
let clipboard_enabled=clipboard.enabled;
|
||||||
|
// 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 {
|
||||||
|
let label=channel.label().to_owned();log::info!("Viewer remote data channel opened: label={label}");
|
||||||
|
let count=Arc::new(std::sync::atomic::AtomicU64::new(0));
|
||||||
|
channel.on_message(Box::new(move |m|{let n=count.fetch_add(1,Ordering::Relaxed);if n<8 {log::info!("Viewer remote channel message: label={label} bytes={} fields={:?}",m.data.len(),crate::protocol::decode_wire(&m.data).map(|f|f.iter().map(|x|x.number).collect::<Vec<_>>()).unwrap_or_default());}Box::pin(async{})}));
|
||||||
|
})));
|
||||||
|
for label in CHANNELS {
|
||||||
|
channels.push(pc.create_data_channel(label,Some(RTCDataChannelInit{ordered:Some(true),..Default::default()})).await.map_err(|_|"Data channel creation failed")?);
|
||||||
|
}
|
||||||
|
// Native host TEXT offers are RPC 21/case 9; CONTROL replies hit a separate empty State.
|
||||||
|
// Keep clipboard offers/replies on TEXT, where the host desktop bridge lives.
|
||||||
|
clipboard.start(&channels[super::clipboard::CHANNEL],&channels[0],observed.clone());
|
||||||
|
for (index,channel) in channels.iter().enumerate() {
|
||||||
|
if index==0 {
|
||||||
|
let weak=Arc::downgrade(&channel);let state=observed.clone();
|
||||||
|
channel.on_open(Box::new(move ||Box::pin(async move {
|
||||||
|
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;}
|
||||||
|
})));
|
||||||
|
}
|
||||||
|
let closed=tools.clone();let state=observed.clone();
|
||||||
|
channel.on_close(Box::new(move ||{let closed=closed.clone();let state=state.clone();Box::pin(async move {
|
||||||
|
if index==0 {state.control_open.store(false,Ordering::Release);}
|
||||||
|
if [0,3,4].contains(&index){closed.lock().unwrap().take();}
|
||||||
|
})}));
|
||||||
|
let state=observed.clone();let inbox=tools.clone();let clipboard_tx=clipboard.sender();
|
||||||
|
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 {
|
||||||
|
if index==0 {
|
||||||
|
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 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;}
|
||||||
|
}
|
||||||
|
// Observe only protobuf field numbers; never print channel payloads.
|
||||||
|
if count<32 {if let Ok(fields)=crate::protocol::decode_wire(&message.data) {let action=fields.iter().find_map(|f|match(f.number,&f.value){(3,crate::protocol::WireValue::Bytes(b))=>Some(super::tools::uint(b,1)),_=>None});
|
||||||
|
log::info!("Viewer CONTROL fields={:?} simple_action={:?}",fields.iter().take(32).map(|f|f.number).collect::<Vec<_>>(),action);}}
|
||||||
|
}
|
||||||
|
if [0,1].contains(&index) && message.data.len()<=super::clipboard::MAX_TEXT*2+1024 {
|
||||||
|
if let Some(tx)=clipboard_tx {
|
||||||
|
if crate::protocol::decode_wire(&message.data).is_ok_and(|f|f.iter().any(|f|[17,21,22].contains(&f.number))){let _=tx.try_send(message.data.to_vec());}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Tools need FILE/BINARY traffic and CONTROL RPC replies (Message 22) only. Official
|
||||||
|
// hosts stream many other CONTROL messages; a full queue drops that message instead of
|
||||||
|
// detaching the tool, which previously lost every later reply.
|
||||||
|
if index!=0 {let n=state.other_messages.fetch_add(1,Ordering::Relaxed);if n<8 {
|
||||||
|
// Shape only: protobuf field numbers or JSON keys, never values.
|
||||||
|
let shape=match serde_json::from_slice::<serde_json::Value>(&message.data){Ok(serde_json::Value::Object(o))=>format!("json{:?}",o.keys().collect::<Vec<_>>()),
|
||||||
|
_=>format!("pb{:?}",crate::protocol::decode_wire(&message.data).map(|f|f.iter().map(|x|x.number).collect::<Vec<_>>()).unwrap_or_default())};
|
||||||
|
let inner=crate::protocol::decode_wire(&message.data).ok().and_then(|f|f.into_iter().find_map(|x|match x.value{crate::protocol::WireValue::Bytes(b) if x.number>2=>Some((x.number,crate::protocol::decode_wire(b).map(|g|g.iter().map(|y|y.number).collect::<Vec<_>>()).unwrap_or_default())),_=>None}));
|
||||||
|
log::info!("Viewer channel message: index={index} bytes={} shape={shape} inner={inner:?}",message.data.len());}}
|
||||||
|
// The official Windows host answers file RPCs (Message 21/22) on TEXT_DATA_CHANNEL (observed
|
||||||
|
// on SHARE: a 320-byte Message 22 on index 1); our host uses FILE. Treat both as the file channel.
|
||||||
|
let has=|n:&[u32]|crate::protocol::decode_wire(&message.data).is_ok_and(|f|f.iter().any(|x|n.contains(&x.number)));
|
||||||
|
let route=if index==1 && has(&[21,22,27]) {3} else {index};
|
||||||
|
// CONTROL carries host-initiated file RPCs (Message 21 with ftp case 8) during downloads too.
|
||||||
|
let route=if index==0 && crate::protocol::decode_wire(&message.data).ok().and_then(|f|f.into_iter().find_map(|x|match x.value{crate::protocol::WireValue::Bytes(b) if x.number==21=>Some(super::tools::blob(b,8).is_some()),_=>None}))==Some(true) {3} else {route};
|
||||||
|
let wanted=index==3 || index==4 || route==3 || (index==0 && has(&[22]));
|
||||||
|
let sender=inbox.lock().unwrap().clone();
|
||||||
|
if let (true,Some(sender))=(wanted,sender) {
|
||||||
|
if let Err(tokio::sync::mpsc::error::TrySendError::Closed(_))=sender.try_send((route,message.data.to_vec())) {
|
||||||
|
let mut slot=inbox.lock().unwrap();if slot.as_ref().is_some_and(|s|s.same_channel(&sender)){slot.take();}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})}));
|
||||||
|
}
|
||||||
|
Ok::<_,String>((channels,tools,clipboard))
|
||||||
|
}.await;
|
||||||
|
match result {
|
||||||
|
Ok((channels,tools,clipboard))=>Ok(Self{clipboard,tools,pc,channels,observed,desktop,audio,answered:false,early:Vec::new(),app_id:app_id.into(),ice_id:ice_id.into(),remote_client:remote_client.into()}),
|
||||||
|
Err(e)=>{let _=pc.close().await;Err(e)},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub async fn offer(&self)->Result<Value,String> {
|
||||||
|
let offer=self.pc.create_offer(None).await.map_err(|_|"Create offer failed")?;
|
||||||
|
let mut sdp=offer.sdp.clone();self.pc.set_local_description(offer).await.map_err(|_|"Set local offer failed")?;
|
||||||
|
// The official Mac viewer's offer carries a bare session-level a=x-uuremote-quic. Only the sent
|
||||||
|
// copy is changed: webrtc-rs rejects a modified local description.
|
||||||
|
if !sdp.contains("a=x-uuremote-quic") { if let Some(i)=sdp.find("a=group:BUNDLE") { sdp.insert_str(i,"a=x-uuremote-quic\r\n"); } }
|
||||||
|
// webrtc-rs omits a=max-message-size, so the host assumes 64 KB and drops larger replies (a 5600-entry
|
||||||
|
// directory listing compresses to ~73 KB). Advertise libwebrtc's 256 KB like the host does; the
|
||||||
|
// vendored data channel already accepts messages of that size.
|
||||||
|
sdp=with_max_message_size(&sdp);
|
||||||
|
// Local diagnostic: UU_VIEWER_DUMP_OFFER=path writes the sent offer SDP (stays on the operator's machine).
|
||||||
|
if let Some(path)=std::env::var_os("UU_VIEWER_DUMP_OFFER") {let _=std::fs::write(path,&sdp);}
|
||||||
|
// ice_network_type=3 as sent by the official Mac viewer (observed on our host's SOAC shape log).
|
||||||
|
Ok(json!({"client_id":self.remote_client,"data":{"type":"offer","sdp":sdp,"ice_id":self.ice_id,"app_control_id":self.app_id,"ice_network_type":3}}))
|
||||||
|
}
|
||||||
|
pub async fn receive(&mut self,event:&Value)->Result<(),String> {
|
||||||
|
let body=event.get("data").unwrap_or(event);
|
||||||
|
if !self.matches_route(event) {return Ok(())}
|
||||||
|
match body.get("type").and_then(Value::as_str) {
|
||||||
|
Some("answer")=> {
|
||||||
|
if self.answered {return Ok(())}
|
||||||
|
let sdp=answer_sdp(body)?;
|
||||||
|
let answer=RTCSessionDescription::answer(sdp).map_err(|_|"Invalid answer")?;
|
||||||
|
self.pc.set_remote_description(answer).await.map_err(|_|"Set remote answer failed")?;
|
||||||
|
self.answered=true;
|
||||||
|
for c in self.early.drain(..) {self.pc.add_ice_candidate(c).await.map_err(|_|"Queued ICE candidate failed")?;}
|
||||||
|
}
|
||||||
|
Some("candidate")=> {
|
||||||
|
let value=body.get("candidate").ok_or("Missing candidate")?;
|
||||||
|
let candidate=if let Some(s)=value.as_str() {
|
||||||
|
RTCIceCandidateInit {candidate:s.into(),sdp_mid:body.get("sdp_mid").and_then(Value::as_str).map(str::to_owned),sdp_mline_index:body.get("sdp_mline_index").and_then(Value::as_u64).and_then(|v|u16::try_from(v).ok()),..Default::default()}
|
||||||
|
} else {serde_json::from_value(value.clone()).map_err(|_|"Invalid candidate")?};
|
||||||
|
if self.answered {self.pc.add_ice_candidate(candidate).await.map_err(|_|"ICE candidate failed")?;}
|
||||||
|
else {if self.early.len()>=256{return Err("Too many early ICE candidates".into())}self.early.push(candidate);}
|
||||||
|
}
|
||||||
|
_=>{},
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub fn matches_route(&self,event:&Value)->bool {
|
||||||
|
let body=event.get("data").unwrap_or(event);
|
||||||
|
!body.get("ice_id").is_some_and(|v|v.as_str()!=Some(self.ice_id.as_str()))
|
||||||
|
&& !body.get("app_control_id").is_some_and(|v|v.as_str()!=Some(self.app_id.as_str()))
|
||||||
|
&& !event.get("client_id").is_some_and(|v|v.as_str()!=Some(self.remote_client.as_str()))
|
||||||
|
}
|
||||||
|
pub fn ready(&self,policy:ReadyPolicy,explicit:bool)->bool {
|
||||||
|
self.observed.echo_received.load(Ordering::Acquire) && match policy {ReadyPolicy::EchoOnly=>true,ReadyPolicy::Explicit=>explicit}
|
||||||
|
}
|
||||||
|
pub async fn send_input(&self,value:&Value)->Result<(),String> {
|
||||||
|
let channel=&self.channels[0];
|
||||||
|
if channel.ready_state()!=webrtc::data_channel::data_channel_state::RTCDataChannelState::Open {return Err("CONTROL is not open".into())}
|
||||||
|
channel.send(&bytes::Bytes::from(value.to_string())).await.map_err(|_|"CONTROL input send failed")?;Ok(())
|
||||||
|
}
|
||||||
|
pub async fn close(&self) {self.clipboard.stop();self.tools.lock().unwrap().take();let _=self.pc.close().await;}
|
||||||
|
}
|
||||||
|
fn answer_sdp(body:&Value)->Result<String,String> {
|
||||||
|
if let Some(s)=body.get("sdp").and_then(Value::as_str) {if s.len()<=1024*1024{return Ok(s.into())}}
|
||||||
|
if let Some(s)=body.get("gzip_sdp").and_then(Value::as_str) {
|
||||||
|
use base64::Engine;use std::io::Read;
|
||||||
|
if s.len()>2*1024*1024 {return Err("Compressed answer too large".into())}
|
||||||
|
let data=base64::engine::general_purpose::STANDARD.decode(s).map_err(|_|"Invalid compressed answer")?;
|
||||||
|
let mut output=String::new();flate2::read::GzDecoder::new(data.as_slice()).take(1024*1024+1).read_to_string(&mut output).map_err(|_|"Invalid compressed answer")?;
|
||||||
|
if output.len()<=1024*1024 {return Ok(output)}
|
||||||
|
}
|
||||||
|
Err("Answer missing or too large".into())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn compressed_answers_are_supported_and_bounded() {
|
||||||
|
use base64::Engine;use std::io::Write;
|
||||||
|
let mut encoder=flate2::write::GzEncoder::new(Vec::new(),flate2::Compression::default());
|
||||||
|
encoder.write_all(b"synthetic-sdp").unwrap();
|
||||||
|
let encoded=base64::engine::general_purpose::STANDARD.encode(encoder.finish().unwrap());
|
||||||
|
assert_eq!(answer_sdp(&json!({"gzip_sdp":encoded})).unwrap(),"synthetic-sdp");
|
||||||
|
assert!(answer_sdp(&json!({"gzip_sdp":"invalid!"})).is_err());
|
||||||
|
assert!(answer_sdp(&json!({"sdp":"x".repeat(1024*1024+1)})).is_err());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Adds `a=max-message-size:262144` after `a=sctp-port` when missing (sent copy only).
|
||||||
|
pub fn with_max_message_size(sdp:&str)->String{
|
||||||
|
if sdp.contains("a=max-message-size:"){return sdp.to_owned()}
|
||||||
|
let eol=if sdp.contains("\r\n"){"\r\n"}else{"\n"};
|
||||||
|
let mut out=String::with_capacity(sdp.len()+32);
|
||||||
|
for line in sdp.split_inclusive('\n'){out.push_str(line);if line.trim_end().starts_with("a=sctp-port:"){out.push_str("a=max-message-size:262144");out.push_str(eol);}}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
#[cfg(test)] #[test] fn offer_advertises_max_message_size(){
|
||||||
|
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");
|
||||||
|
}
|
||||||
@@ -0,0 +1,156 @@
|
|||||||
|
use super::{desktop::{Frame,FrameSink},input::{Input,Viewport},session::Session};
|
||||||
|
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 }
|
||||||
|
impl Window {
|
||||||
|
pub fn new()->Result<Self,String>{
|
||||||
|
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 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})
|
||||||
|
}
|
||||||
|
fn viewport(&self)->Viewport {Viewport::fit(self.canvas.window().size(),self.frame)}
|
||||||
|
pub fn poll(&mut self,input:&mut Input)->(bool,Vec<serde_json::Value>){
|
||||||
|
let viewport=self.viewport();
|
||||||
|
translate_actions(self.events.poll_iter(),input,viewport,&mut self.focused,&mut self.files)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn translate(events:impl IntoIterator<Item=Event>,input:&mut Input,viewport:Viewport,focused:&mut bool)->(bool,Vec<serde_json::Value>){
|
||||||
|
translate_actions(events,input,viewport,focused,&mut false)
|
||||||
|
}
|
||||||
|
fn translate_actions(events:impl IntoIterator<Item=Event>,input:&mut Input,viewport:Viewport,focused:&mut bool,files:&mut bool)->(bool,Vec<serde_json::Value>){
|
||||||
|
let mut messages=Vec::new();let mut quit=false;
|
||||||
|
for event in events{
|
||||||
|
let message=match event {
|
||||||
|
Event::Quit{..}|Event::Window{win_event:WindowEvent::Close,..}=>{quit=true;messages.extend(input.release_all());None},
|
||||||
|
Event::Window{win_event:WindowEvent::FocusLost,..}=>{*focused=false;messages.extend(input.release_all());None},
|
||||||
|
Event::Window{win_event:WindowEvent::FocusGained,..}=>{*focused=true;None},
|
||||||
|
Event::KeyDown{scancode:Some(sdl2::keyboard::Scancode::F),keymod,repeat,..}
|
||||||
|
if *focused && keymod.intersects(sdl2::keyboard::Mod::LCTRLMOD|sdl2::keyboard::Mod::RCTRLMOD)
|
||||||
|
&& keymod.intersects(sdl2::keyboard::Mod::LALTMOD|sdl2::keyboard::Mod::RALTMOD)=>{
|
||||||
|
if !repeat{*files=true;messages.extend(input.release_all());}None
|
||||||
|
},
|
||||||
|
_ if !*focused || !input.enabled()=>None,
|
||||||
|
Event::KeyDown{scancode:Some(s),repeat:false,..}=>input.key(s as i32,true),
|
||||||
|
Event::KeyUp{scancode:Some(s),..}=>input.key(s as i32,false),
|
||||||
|
Event::MouseMotion{x,y,..}=>viewport.motion(x as f64,y as f64),
|
||||||
|
Event::MouseButtonDown{mouse_btn,..}=>button(mouse_btn).and_then(|b|input.button(b,true)),
|
||||||
|
Event::MouseButtonUp{mouse_btn,..}=>button(mouse_btn).and_then(|b|input.button(b,false)),
|
||||||
|
_=>None,
|
||||||
|
};if let Some(m)=message{messages.push(m);}
|
||||||
|
} (quit,messages)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn button(b:MouseButton)->Option<i32>{match b{MouseButton::Left=>Some(1),MouseButton::Right=>Some(2),MouseButton::Middle=>Some(4),_=>None}}
|
||||||
|
impl FrameSink for Window {
|
||||||
|
fn present(&mut self,frame:&Frame)->Result<(),String>{
|
||||||
|
self.frame=(frame.width,frame.height);let v=self.viewport();let creator=self.canvas.texture_creator();
|
||||||
|
let mut texture=creator.create_texture_streaming(PixelFormatEnum::RGB24,frame.width,frame.height).map_err(|_|"Texture creation failed")?;
|
||||||
|
texture.update(None,&frame.rgb,frame.width as usize*3).map_err(|_|"Texture update failed")?;
|
||||||
|
self.canvas.set_draw_color(sdl2::pixels::Color::BLACK);self.canvas.clear();
|
||||||
|
self.canvas.copy(&texture,None,Some(Rect::new(v.x as i32,v.y as i32,v.width as u32,v.height as u32)))?;self.canvas.present();Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub async fn run(session:&mut Session,window:&mut Window,decoder:&mut dyn super::desktop::Decoder,view_only:bool)->Result<(),String>{
|
||||||
|
#[cfg(uu_gtk)]
|
||||||
|
let mut files:Option<tokio::task::JoinHandle<Result<(),String>>>=None;
|
||||||
|
let(tx,mut commands)=tokio::sync::mpsc::channel(8);
|
||||||
|
if std::env::var("UU_VIEWER_COMMANDS").as_deref()==Ok("1") {
|
||||||
|
std::thread::spawn(move||{
|
||||||
|
use std::io::Read;
|
||||||
|
let mut input=std::io::stdin().lock();let mut line=Vec::new();let mut byte=[0];
|
||||||
|
while input.read(&mut byte).ok()==Some(1){
|
||||||
|
if byte[0]==b'\n'{if line==b"files" && tx.blocking_send(()).is_err(){break}line.clear();}
|
||||||
|
else if line.len()<32{line.push(byte[0]);}else{break}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}else{drop(tx);}
|
||||||
|
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 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 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 result=async {
|
||||||
|
loop {tokio::select!{
|
||||||
|
command=commands.recv(),if command_open=>{if command.is_some(){requested=true}else{command_open=false}},
|
||||||
|
_=terminate.recv()=>return Ok(()),
|
||||||
|
_=tokio::signal::ctrl_c()=>return Ok(()),
|
||||||
|
result=session.step()=>result?,
|
||||||
|
_=&mut deadline,if !session.peer.observed.echo_received.load(std::sync::atomic::Ordering::Acquire)=>return Err("CONTROL echo timed out".into()),
|
||||||
|
_=heartbeat.tick()=>{
|
||||||
|
if input.enabled() && session.peer.ready(super::rtc::ReadyPolicy::EchoOnly,false){session.peer.send_input(&Input::heartbeat()).await?;}
|
||||||
|
let o=&session.peer.observed;let s=&super::desktop::STATS;let r=std::sync::atomic::Ordering::Relaxed;
|
||||||
|
log::info!("Viewer desktop stats: control_open={} echo={} media_packets={} access_units={} presented={} decode_errors={}",
|
||||||
|
o.control_open.load(r),o.echo_received.load(r),o.media_packets.load(r),s[0].load(r),s[1].load(r),s[2].load(r));
|
||||||
|
},
|
||||||
|
_=tick.tick()=>{
|
||||||
|
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);
|
||||||
|
requested|=std::mem::take(&mut window.files);
|
||||||
|
#[cfg(uu_gtk)] {
|
||||||
|
if files.as_ref().is_some_and(|f|f.is_finished()){
|
||||||
|
if let Some(f)=files.take(){let _=f.await;}session.peer.tools.lock().unwrap().take();
|
||||||
|
}
|
||||||
|
if requested && !(files.is_some() && super::tools_ui::closed()) && session.peer.channels[super::tools::FILE].ready_state()==webrtc::data_channel::data_channel_state::RTCDataChannelState::Open {
|
||||||
|
if files.is_some(){super::tools_ui::present();}
|
||||||
|
else {let(io,rx)=super::tools::attach(&session.peer)?;
|
||||||
|
let name=super::device_label(std::env::var("UU_VIEWER_DEVICE_NAME").ok().as_deref()).to_owned();
|
||||||
|
files=Some(tokio::spawn(async move{super::tools_ui::files(io,rx,&name).await}));}
|
||||||
|
requested=false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[cfg(not(uu_gtk))] {if requested{log::warn!("File window requires GTK3");requested=false;}}
|
||||||
|
if session.peer.ready(super::rtc::ReadyPolicy::EchoOnly,false){for m in messages{session.peer.send_input(&m).await?;}}
|
||||||
|
else {input.release_all();}
|
||||||
|
if quit{return Ok(())}
|
||||||
|
// At most one AU per iteration keeps local event polling responsive.
|
||||||
|
if let Some(au)=session.peer.desktop.units.pop(){
|
||||||
|
if session.peer.desktop.reset.swap(false,std::sync::atomic::Ordering::AcqRel){decoder.reset();}
|
||||||
|
super::desktop::display(decoder,window,&au,&session.peer.desktop.pli)?;}
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
}.await;
|
||||||
|
#[cfg(uu_gtk)] if let Some(f)=files {f.abort();let _=f.await;super::tools_ui::hide();session.peer.tools.lock().unwrap().take();}
|
||||||
|
for m in input.release_all(){let _=session.peer.send_input(&m).await;}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)] mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test] fn file_shortcut_is_local_in_control_and_watch_modes(){
|
||||||
|
use sdl2::keyboard::{Scancode as S,Mod as M};
|
||||||
|
for watch in [false,true]{
|
||||||
|
let mut input=Input::new(watch);let mut focused=true;let mut files=false;
|
||||||
|
input.key(S::LCtrl as i32,true);input.key(S::LAlt as i32,true);
|
||||||
|
let key=|repeat|Event::KeyDown{timestamp:0,window_id:1,keycode:None,scancode:Some(S::F),keymod:M::LCTRLMOD|M::LALTMOD,repeat};
|
||||||
|
let (_,out)=translate_actions([key(false),key(true),Event::KeyUp{timestamp:0,window_id:1,keycode:None,scancode:Some(S::F),keymod:M::NOMOD,repeat:false}],
|
||||||
|
&mut input,Viewport::fit((800,600),(1920,1080)),&mut focused,&mut files);
|
||||||
|
assert!(files);assert!(out.iter().all(|v|v["action"]=="kbd_release"));
|
||||||
|
assert!(input.release_all().is_empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[test] fn watch_events_never_emit_input_and_still_close(){
|
||||||
|
let mut input=Input::new(true);let mut focused=true;
|
||||||
|
assert!(input.key(4,true).is_none());assert!(input.button(1,true).is_none());assert!(!input.enabled());
|
||||||
|
let events=[
|
||||||
|
Event::KeyDown{timestamp:0,window_id:1,keycode:None,scancode:Some(sdl2::keyboard::Scancode::A),keymod:sdl2::keyboard::Mod::NOMOD,repeat:false},
|
||||||
|
Event::MouseMotion{timestamp:0,window_id:1,which:0,mousestate:sdl2::mouse::MouseState::from_sdl_state(0),x:12,y:34,xrel:1,yrel:1},
|
||||||
|
Event::MouseButtonDown{timestamp:0,window_id:1,which:0,mouse_btn:MouseButton::Left,clicks:1,x:12,y:34},
|
||||||
|
Event::MouseButtonUp{timestamp:0,window_id:1,which:0,mouse_btn:MouseButton::Left,clicks:1,x:12,y:34},
|
||||||
|
Event::KeyUp{timestamp:0,window_id:1,keycode:None,scancode:Some(sdl2::keyboard::Scancode::A),keymod:sdl2::keyboard::Mod::NOMOD,repeat:false},
|
||||||
|
Event::Window{timestamp:0,window_id:1,win_event:WindowEvent::FocusLost},
|
||||||
|
Event::Window{timestamp:0,window_id:1,win_event:WindowEvent::FocusGained},Event::Quit{timestamp:0}];
|
||||||
|
let (quit,out)=translate(events,&mut input,Viewport::fit((800,600),(1920,1080)),&mut focused);
|
||||||
|
assert!(quit);assert!(out.is_empty());assert!(input.release_all().is_empty());
|
||||||
|
}
|
||||||
|
#[test] fn focus_event_releases_and_suppresses_input_until_focus_returns(){
|
||||||
|
let mut input=Input::default();input.key(4,true);input.button(1,true);let mut focused=true;
|
||||||
|
let focus=|win_event|Event::Window{timestamp:0,window_id:1,win_event};
|
||||||
|
let key=Event::KeyDown{timestamp:0,window_id:1,keycode:None,scancode:Some(sdl2::keyboard::Scancode::A),keymod:sdl2::keyboard::Mod::NOMOD,repeat:false};
|
||||||
|
let v=Viewport::fit((800,600),(1920,1080));
|
||||||
|
let (quit,out)=translate([focus(WindowEvent::FocusLost),key.clone()],&mut input,v,&mut focused);
|
||||||
|
assert!(!quit);assert_eq!(out.len(),2);assert!(input.release_all().is_empty());assert!(!focused);
|
||||||
|
let (_,out)=translate([focus(WindowEvent::FocusGained),key,Event::Quit{timestamp:0}],&mut input,v,&mut focused);
|
||||||
|
assert_eq!(out.len(),2);assert_eq!(out[0]["action"],"kbd_press");assert_eq!(out[1]["action"],"kbd_release");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
use super::{api::JoinData,wire::{self,Shape},signal::Signal,rtc::{self,Peer,ReadyPolicy}};
|
||||||
|
use serde_json::Value;
|
||||||
|
use std::{sync::atomic::Ordering,time::Duration};
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
use webrtc::api::setting_engine::SettingEngine;
|
||||||
|
|
||||||
|
pub struct Options {pub timeout:Duration,pub legacy_prefix:bool,pub ready_policy:ReadyPolicy}
|
||||||
|
pub struct Session {pub peer:Peer,signal:Signal,candidates:mpsc::Receiver<Value>,policy:ReadyPolicy}
|
||||||
|
impl Session {
|
||||||
|
pub async fn open(room:&JoinData,shape:&Shape,device:&str,client:&str,options:Options,settings:SettingEngine)->Result<Self,String> {
|
||||||
|
shape.validate_session()?;
|
||||||
|
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 mut signal=Signal::connect(&room.signaling_list,&room.token,timeout,options.legacy_prefix).await?;
|
||||||
|
let result=async {
|
||||||
|
let result=signal.control(shape.control(client,&app,&ice)?,shape.connect_options(device)?,options.timeout).await?;
|
||||||
|
// 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) {
|
||||||
|
let summary:Vec<String>=list.iter().map(|s|{
|
||||||
|
let scheme=s.get("urls").and_then(|u|u.as_str().map(str::to_owned).or_else(||u.get(0).and_then(Value::as_str).map(str::to_owned))).unwrap_or_default();
|
||||||
|
let keys:Vec<&str>=s.as_object().map(|o|o.keys().map(String::as_str).collect()).unwrap_or_default();
|
||||||
|
format!("{}:{:?}",scheme.split(':').next().unwrap_or(""),keys)}).collect();
|
||||||
|
log::info!("Control ack ICE entries: {:?}",summary);
|
||||||
|
}
|
||||||
|
// Structure and relations only; never the ids or ICE credentials.
|
||||||
|
log::info!("Control ack shape={} app_control_id_matches={:?} ice_id_matches={:?}",crate::protocol::json_shape(&result,5),
|
||||||
|
result.get("app_control_id").map(|v|v.as_str()==Some(app.as_str())),result.get("ice_id").map(|v|v.as_str()==Some(ice.as_str())));
|
||||||
|
if result.get("app_control_id").is_some_and(|v|v.as_str()!=Some(app.as_str())) {return Err("Control acknowledgement belongs to another session".into())}
|
||||||
|
// Observed live: the cloud assigns the session ice_id in this ack rather than echoing ours.
|
||||||
|
let ice=result.get("ice_id").and_then(Value::as_str).filter(|s|!s.is_empty()&&s.len()<=128).map(str::to_owned).unwrap_or(ice);
|
||||||
|
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(tx,rx)=mpsc::channel(256);
|
||||||
|
let peer=Peer::new(config,shape,&app,&ice,remote,tx,settings).await?;
|
||||||
|
// 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.
|
||||||
|
let delay=std::env::var("UU_VIEWER_OFFER_DELAY_MS").ok().and_then(|v|v.parse().ok()).unwrap_or(0u64).min(10_000);
|
||||||
|
tokio::time::sleep(Duration::from_millis(delay)).await;
|
||||||
|
match peer.offer().await {
|
||||||
|
Ok(offer)=>match signal.soac(offer).await {Ok(())=>Ok((peer,rx)),Err(e)=>{peer.close().await;Err(e)}},
|
||||||
|
Err(e)=>{peer.close().await;Err(e)},
|
||||||
|
}
|
||||||
|
}.await;
|
||||||
|
match result {
|
||||||
|
Ok((peer,candidates))=>Ok(Self{peer,signal,candidates,policy:options.ready_policy}),
|
||||||
|
Err(e)=>{signal.close().await;Err(e)},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub async fn step(&mut self)->Result<(),String> {
|
||||||
|
tokio::select! {
|
||||||
|
candidate=self.candidates.recv()=>if let Some(candidate)=candidate {self.signal.soac(candidate).await?},
|
||||||
|
event=self.signal.event()=> {
|
||||||
|
let event=event?;
|
||||||
|
// Event name and SOAC type only.
|
||||||
|
log::info!("Viewer signaling event: name={} soac_type={}",event.get(0).and_then(Value::as_str).unwrap_or("-"),
|
||||||
|
event.get(1).and_then(|b|b.get("type").or_else(||b.get("data").and_then(|d|d.get("type")))).and_then(Value::as_str).unwrap_or("-"));
|
||||||
|
match event.get(0).and_then(Value::as_str) {
|
||||||
|
Some("soac")=>if let Some(body)=event.get(1) {self.peer.receive(body).await?},
|
||||||
|
Some("released"|"left"|"leave")=> {
|
||||||
|
// An event for another peer must not tear down this session.
|
||||||
|
if self.peer.matches_route(event.get(1).unwrap_or(&Value::Null)) {return Err("Remote session released".into())}
|
||||||
|
}
|
||||||
|
_=>{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub async fn run(&mut self)->Result<(),String> {
|
||||||
|
let mut interval=tokio::time::interval(Duration::from_secs(1));
|
||||||
|
let mut reported=(false,false,false);
|
||||||
|
let deadline=tokio::time::sleep(Duration::from_secs(30));tokio::pin!(deadline);
|
||||||
|
loop {
|
||||||
|
tokio::select! {
|
||||||
|
_=tokio::signal::ctrl_c()=>return Ok(()),
|
||||||
|
result=self.step()=>result?,
|
||||||
|
_=&mut deadline,if !self.peer.observed.echo_received.load(Ordering::Acquire)=>return Err("CONTROL echo timed out".into()),
|
||||||
|
_=interval.tick()=> {
|
||||||
|
if self.peer.observed.control_open.load(Ordering::Acquire) {self.peer.send_input(&super::input::Input::heartbeat()).await?;}
|
||||||
|
let state=(self.peer.observed.control_open.load(Ordering::Acquire),self.peer.observed.echo_received.load(Ordering::Acquire),self.peer.ready(self.policy,false));
|
||||||
|
if state!=reported {log::info!("Viewer session: control_open={} echo_received={} configured_ready={} control_messages={} media_packets={}",state.0,state.1,state.2,self.peer.observed.control_messages.load(Ordering::Relaxed),self.peer.observed.media_packets.load(Ordering::Relaxed));reported=state;}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub async fn close(&mut self) {self.peer.close().await;self.signal.close().await;}
|
||||||
|
}
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
//! Framing/ack evidence: internal viewer-evidence-20261016.md section 3.
|
||||||
|
//! Raw Engine.IO4 attachments and legacy SDK 0x04 framing are explicit alternatives.
|
||||||
|
use base64::Engine as _;
|
||||||
|
use futures_util::{SinkExt, StreamExt};
|
||||||
|
use serde_json::Value;
|
||||||
|
use tokio::net::TcpStream;
|
||||||
|
use tokio_tungstenite::{connect_async, MaybeTlsStream, WebSocketStream};
|
||||||
|
use tokio_tungstenite::tungstenite::{client::IntoClientRequest, Message};
|
||||||
|
use std::{collections::VecDeque,time::Duration};
|
||||||
|
|
||||||
|
pub struct Signal {
|
||||||
|
ws: WebSocketStream<MaybeTlsStream<TcpStream>>,
|
||||||
|
next_ack:u64,
|
||||||
|
pending:VecDeque<Value>,
|
||||||
|
legacy_prefix:bool,
|
||||||
|
}
|
||||||
|
impl Signal {
|
||||||
|
pub async fn connect(urls:&[String], token:&str, timeout:Duration, legacy_prefix:bool)->Result<Self,String> {
|
||||||
|
for url in urls {
|
||||||
|
if let Ok(Ok(signal))=tokio::time::timeout(timeout,Self::one(url,token,legacy_prefix)).await {return Ok(signal)}
|
||||||
|
}
|
||||||
|
Err("Signaling connection failed (details omitted)".into())
|
||||||
|
}
|
||||||
|
async fn one(url:&str,token:&str,legacy_prefix:bool)->Result<Self,String> {
|
||||||
|
let mut request=format!("{}/socket.io/?transport=websocket&EIO=4",url.trim_end_matches('/')).into_client_request().map_err(|_|"Invalid signaling endpoint")?;
|
||||||
|
request.headers_mut().insert("X-NRD-AUTH",token.parse().map_err(|_|"Invalid room authentication header")?);
|
||||||
|
let (ws,_)=connect_async(request).await.map_err(|_|"Signaling transport failed")?;
|
||||||
|
let mut s=Self{ws,next_ack:10,pending:VecDeque::new(),legacy_prefix};
|
||||||
|
let mut opened=false;
|
||||||
|
loop {
|
||||||
|
match s.ws.next().await {
|
||||||
|
Some(Ok(Message::Text(t))) if t.starts_with('0') && !opened => {
|
||||||
|
serde_json::from_str::<Value>(&t[1..]).map_err(|_|"Invalid Engine.IO open")?;
|
||||||
|
opened=true;s.send(Message::Text("40".into())).await?;
|
||||||
|
}
|
||||||
|
Some(Ok(Message::Text(t))) if t.starts_with("40") && opened =>return Ok(s),
|
||||||
|
Some(Ok(Message::Text(t))) if t.starts_with('2')=>s.send(Message::Text(format!("3{}",&t[1..]))).await?,
|
||||||
|
Some(Ok(Message::Ping(b)))=>s.send(Message::Pong(b)).await?,
|
||||||
|
_=>return Err("Signaling handshake failed".into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async fn send(&mut self,message:Message)->Result<(),String> {self.ws.send(message).await.map_err(|_|"Signaling send failed".into())}
|
||||||
|
pub async fn control(&mut self, header:Value, app_data:Vec<u8>, timeout:Duration)->Result<Value,String> {
|
||||||
|
self.next_ack+=1;let id=self.next_ack;
|
||||||
|
self.send(Message::Text(format!("451-{id}{}",serde_json::json!(["control",header])))).await?;
|
||||||
|
let mut attachment=Vec::new();if self.legacy_prefix {attachment.push(4)} attachment.extend(app_data);
|
||||||
|
self.send(Message::Binary(attachment)).await?;
|
||||||
|
tokio::time::timeout(timeout,async {
|
||||||
|
loop {
|
||||||
|
match self.packet().await? {
|
||||||
|
Packet::Ack(ack,value) if ack==id=> {
|
||||||
|
let a=value.as_array().ok_or("Invalid control acknowledgement")?;
|
||||||
|
if a.len()!=2 || a[0].as_str()!=Some("success") || !a[1].is_object() {return Err("Control refused or malformed acknowledgement".into())}
|
||||||
|
return Ok(a[1].clone());
|
||||||
|
}
|
||||||
|
Packet::Event(value)=> {
|
||||||
|
if self.pending.len()>=64 {return Err("Too many pre-control signaling events".into())}
|
||||||
|
self.pending.push_back(value);
|
||||||
|
}
|
||||||
|
_=>{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).await.map_err(|_|"Control acknowledgement timed out")?
|
||||||
|
}
|
||||||
|
async fn packet(&mut self)->Result<Packet,String> {
|
||||||
|
loop {
|
||||||
|
match self.ws.next().await {
|
||||||
|
Some(Ok(Message::Text(t))) if t.starts_with('2')=>self.send(Message::Text(format!("3{}",&t[1..]))).await?,
|
||||||
|
Some(Ok(Message::Ping(b)))=>self.send(Message::Pong(b)).await?,
|
||||||
|
Some(Ok(Message::Text(t))) if t.starts_with("43")=> {
|
||||||
|
let begin=t.find('[').ok_or("Malformed signaling acknowledgement")?;
|
||||||
|
let id=t[2..begin].parse().map_err(|_|"Malformed signaling acknowledgement id")?;
|
||||||
|
let value=serde_json::from_str(&t[begin..]).map_err(|_|"Malformed signaling acknowledgement body")?;
|
||||||
|
return Ok(Packet::Ack(id,value))
|
||||||
|
}
|
||||||
|
Some(Ok(Message::Text(t))) if t.starts_with("42")=> {
|
||||||
|
let begin=t.find('[').ok_or("Malformed signaling event")?;
|
||||||
|
let value=serde_json::from_str(&t[begin..]).map_err(|_|"Malformed signaling event body")?;
|
||||||
|
return Ok(Packet::Event(value))
|
||||||
|
}
|
||||||
|
// Socket.IO binary event (45N-[...] / 46N-[...] ack) followed by N attachment frames.
|
||||||
|
// The official host sends its answer this way (binary gzip_sdp); attachments are
|
||||||
|
// substituted as base64 strings so the existing gzip_sdp decoder applies.
|
||||||
|
Some(Ok(Message::Text(t))) if t.starts_with("45") || t.starts_with("46")=> {
|
||||||
|
let dash=t.find('-').ok_or("Malformed binary signaling packet")?;
|
||||||
|
let count:usize=t[2..dash].parse().map_err(|_|"Malformed binary attachment count")?;
|
||||||
|
if count>8 {return Err("Too many signaling attachments".into())}
|
||||||
|
let begin=t.find('[').ok_or("Malformed binary signaling packet")?;
|
||||||
|
let mut value:serde_json::Value=serde_json::from_str(&t[begin..]).map_err(|_|"Malformed binary signaling body")?;
|
||||||
|
let mut attachments=Vec::with_capacity(count);
|
||||||
|
while attachments.len()<count {
|
||||||
|
match self.ws.next().await {
|
||||||
|
Some(Ok(Message::Binary(mut b)))=>{
|
||||||
|
if b.len()>2 && b[0]==4 && b[1]==0x1f {b.remove(0);}
|
||||||
|
if b.len()>4*1024*1024 {return Err("Signaling attachment too large".into())}
|
||||||
|
attachments.push(base64::engine::general_purpose::STANDARD.encode(&b));
|
||||||
|
}
|
||||||
|
Some(Ok(Message::Text(t))) if t.starts_with('2')=>self.send(Message::Text(format!("3{}",&t[1..]))).await?,
|
||||||
|
Some(Ok(_))=>{},
|
||||||
|
_=>return Err("Signaling disconnected".into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn fill(v:&mut serde_json::Value,a:&[String]) {
|
||||||
|
match v {
|
||||||
|
serde_json::Value::Object(o) if o.get("_placeholder")==Some(&serde_json::Value::Bool(true))=>{
|
||||||
|
if let Some(s)=o.get("num").and_then(|n|n.as_u64()).and_then(|n|a.get(n as usize)) {*v=serde_json::Value::String(s.clone());}
|
||||||
|
}
|
||||||
|
serde_json::Value::Object(o)=>for x in o.values_mut(){fill(x,a)},
|
||||||
|
serde_json::Value::Array(l)=>for x in l{fill(x,a)},
|
||||||
|
_=>{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fill(&mut value,&attachments);
|
||||||
|
log::info!("Viewer binary signaling event: name={} attachments={}",value.get(0).and_then(|v|v.as_str()).unwrap_or("-"),count);
|
||||||
|
if t.starts_with("46") {
|
||||||
|
let id=t[dash+1..begin].parse().map_err(|_|"Malformed binary acknowledgement id")?;
|
||||||
|
return Ok(Packet::Ack(id,value))
|
||||||
|
}
|
||||||
|
return Ok(Packet::Event(value))
|
||||||
|
}
|
||||||
|
Some(Ok(Message::Text(t))) if t=="41" || t=="1"=>return Err("Signaling room closed".into()),
|
||||||
|
Some(Ok(Message::Close(_)))|None|Some(Err(_))=>return Err("Signaling disconnected".into()),
|
||||||
|
// Type, short prefix and length only, to see what else the room delivers.
|
||||||
|
Some(Ok(Message::Text(t)))=>log::info!("Viewer signaling frame ignored: text prefix={} bytes={}",t.chars().take(3).collect::<String>(),t.len()),
|
||||||
|
Some(Ok(Message::Binary(b)))=>log::info!("Viewer signaling frame ignored: binary bytes={}",b.len()),
|
||||||
|
_=>{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub async fn event(&mut self)->Result<Value,String> {
|
||||||
|
if let Some(v)=self.pending.pop_front(){return Ok(v)}
|
||||||
|
loop {if let Packet::Event(v)=self.packet().await?{return Ok(v)}}
|
||||||
|
}
|
||||||
|
pub async fn soac(&mut self, value:Value)->Result<(),String> {self.send(Message::Text(format!("42{}",serde_json::json!(["soac",value])))).await}
|
||||||
|
pub async fn close(&mut self) {let _=self.ws.close(None).await;}
|
||||||
|
}
|
||||||
|
enum Packet { Ack(u64,Value), Event(Value) }
|
||||||
@@ -0,0 +1,289 @@
|
|||||||
|
use super::{api::JoinData,session::{Session,Options},signal::Signal,rtc::{self,Peer,ReadyPolicy},wire::{self,Shape}};
|
||||||
|
use serde_json::{json,Value};
|
||||||
|
use std::{sync::{Arc,atomic::{AtomicUsize,Ordering}},time::Duration};
|
||||||
|
use tokio::{net::TcpListener,sync::{Mutex,mpsc}};
|
||||||
|
use futures_util::{SinkExt,StreamExt};
|
||||||
|
use tokio_tungstenite::{accept_async,tungstenite::Message};
|
||||||
|
use webrtc::{api::{APIBuilder,media_engine::MediaEngine,setting_engine::SettingEngine},peer_connection::{configuration::RTCConfiguration,sdp::session_description::RTCSessionDescription},util::vnet::{net::{Net,NetConfig},router::{Router,RouterConfig}}};
|
||||||
|
|
||||||
|
async fn virtual_network()->(SettingEngine,SettingEngine,Arc<Mutex<Router>>) {
|
||||||
|
// RFC 5737 documentation addresses live only inside webrtc's in-memory network.
|
||||||
|
let router=Arc::new(Mutex::new(Router::new(RouterConfig{cidr:"192.0.2.0/24".into(),..Default::default()}).unwrap()));
|
||||||
|
let mut settings=Vec::new();
|
||||||
|
for address in ["192.0.2.10","192.0.2.11"] {
|
||||||
|
let net=Arc::new(Net::new(Some(NetConfig{static_ips:vec![address.into()],..Default::default()})));
|
||||||
|
let nic=net.get_nic().unwrap();router.lock().await.add_net(nic.clone()).await.unwrap();
|
||||||
|
nic.lock().await.set_router(router.clone()).await.unwrap();
|
||||||
|
let mut setting=SettingEngine::default();setting.set_vnet(Some(net));
|
||||||
|
setting.set_network_types(vec![webrtc::ice::network_type::NetworkType::Udp4]);
|
||||||
|
settings.push(setting);
|
||||||
|
}
|
||||||
|
router.lock().await.start().await.unwrap();
|
||||||
|
let b=settings.pop().unwrap();(settings.pop().unwrap(),b,router)
|
||||||
|
}
|
||||||
|
fn shape()->Shape {serde_json::from_str(include_str!("../../tests/fixtures/viewer/session-shape.json")).unwrap()}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ice_configuration_preserves_credentials_without_public_fallback() {
|
||||||
|
let result=json!({"iceServers":[{"urls":"turn:relay.example.invalid:3478","username":"synthetic-user","credential":"synthetic-pass"}],"force_relay":true});
|
||||||
|
let config=rtc::configuration(&result).unwrap();assert_eq!(config.ice_servers.len(),1);
|
||||||
|
assert_eq!(config.ice_servers[0].credential,"synthetic-pass");
|
||||||
|
assert_eq!(config.ice_transport_policy,webrtc::peer_connection::policy::ice_transport_policy::RTCIceTransportPolicy::Relay);
|
||||||
|
assert!(rtc::configuration(&json!({"iceServers":[]})).unwrap().ice_servers.is_empty());
|
||||||
|
for result in [json!({}),json!({"iceServers":[],"force_relay":true}),json!({"iceServers":[{"urls":"https://invalid.example"}]}),json!({"iceServers":[{"urls":[7]}]})] {assert!(rtc::configuration(&result).is_err());}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn websocket_control_ack_framing_heartbeat_failure_and_timeout() {
|
||||||
|
for legacy in [false,true] {
|
||||||
|
let listener=TcpListener::bind((std::net::Ipv4Addr::LOCALHOST,0)).await.unwrap();
|
||||||
|
let url=format!("ws://{}",listener.local_addr().unwrap());
|
||||||
|
let server=tokio::spawn(async move {
|
||||||
|
let(stream,_)=listener.accept().await.unwrap();let mut ws=accept_async(stream).await.unwrap();
|
||||||
|
ws.send(Message::Text("0{}".into())).await.unwrap();assert_eq!(ws.next().await.unwrap().unwrap(),Message::Text("40".into()));
|
||||||
|
ws.send(Message::Text("40{}".into())).await.unwrap();
|
||||||
|
let Message::Text(t)=ws.next().await.unwrap().unwrap() else{panic!()};assert!(t.starts_with("451-11["));
|
||||||
|
let Message::Binary(b)=ws.next().await.unwrap().unwrap() else{panic!()};assert_eq!(b,if legacy{vec![4,8,1]}else{vec![8,1]});
|
||||||
|
ws.send(Message::Text("2fixture".into())).await.unwrap();assert_eq!(ws.next().await.unwrap().unwrap(),Message::Text("3fixture".into()));
|
||||||
|
ws.send(Message::Text("43999[\"success\",{}]".into())).await.unwrap();
|
||||||
|
ws.send(Message::Text("4311[\"fail\",{}]".into())).await.unwrap();
|
||||||
|
});
|
||||||
|
let mut signal=Signal::connect(&["invalid endpoint".into(),url],"synthetic-ticket",Duration::from_millis(100),legacy).await.unwrap();
|
||||||
|
assert!(signal.control(json!({}),vec![8,1],Duration::from_secs(2)).await.unwrap_err().contains("refused"));signal.close().await;server.await.unwrap();
|
||||||
|
}
|
||||||
|
let listener=TcpListener::bind((std::net::Ipv4Addr::LOCALHOST,0)).await.unwrap();let url=format!("ws://{}",listener.local_addr().unwrap());
|
||||||
|
let job=tokio::spawn(async move {
|
||||||
|
let(s,_)=listener.accept().await.unwrap();let mut ws=accept_async(s).await.unwrap();ws.send(Message::Text("0{}".into())).await.unwrap();let _=ws.next().await;
|
||||||
|
ws.send(Message::Text("40{}".into())).await.unwrap();while ws.next().await.is_some(){}
|
||||||
|
});
|
||||||
|
let mut signal=Signal::connect(&[url],"synthetic-ticket",Duration::from_secs(2),false).await.unwrap();
|
||||||
|
assert!(signal.control(json!({}),vec![],Duration::from_millis(20)).await.unwrap_err().contains("timed out"));signal.close().await;job.await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn complete_session_uses_loopback_signaling_and_virtual_rtc_through_echo() {
|
||||||
|
tokio::time::timeout(Duration::from_secs(20),async {
|
||||||
|
let(viewer_settings,host_settings,router)=virtual_network().await;
|
||||||
|
let mut engine=MediaEngine::default();engine.register_default_codecs().unwrap();
|
||||||
|
let host=Arc::new(APIBuilder::new().with_media_engine(engine).with_setting_engine(host_settings).build().new_peer_connection(RTCConfiguration::default()).await.unwrap());
|
||||||
|
use webrtc::track::track_local::{track_local_static_rtp::TrackLocalStaticRTP,TrackLocal,TrackLocalWriter};
|
||||||
|
let track=Arc::new(TrackLocalStaticRTP::new(webrtc::rtp_transceiver::rtp_codec::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()},"synthetic-video".into(),"synthetic-stream".into()));
|
||||||
|
let sender=host.add_track(track.clone() as Arc<dyn TrackLocal+Send+Sync>).await.unwrap();
|
||||||
|
let (input_tx,mut input_rx)=mpsc::channel(8);
|
||||||
|
let opened=Arc::new(AtomicUsize::new(0));let count=opened.clone();
|
||||||
|
host.on_data_channel(Box::new(move |channel| {
|
||||||
|
let count=count.clone();let input_tx=input_tx.clone();Box::pin(async move {
|
||||||
|
assert!(wire::CHANNELS.contains(&channel.label()));assert!(channel.ordered());assert!(!channel.negotiated());
|
||||||
|
let c=count.clone();channel.on_open(Box::new(move ||Box::pin(async move{c.fetch_add(1,Ordering::Relaxed);} )));
|
||||||
|
if channel.label()==wire::CHANNELS[0] {
|
||||||
|
let weak=Arc::downgrade(&channel);
|
||||||
|
channel.on_message(Box::new(move |message| {let weak=weak.clone();let input_tx=input_tx.clone();Box::pin(async move {
|
||||||
|
if message.data.first()==Some(&b'{') {input_tx.send(serde_json::from_slice::<Value>(&message.data).unwrap()).await.unwrap();return}
|
||||||
|
for (action,number) in [(super::power::Action::Reboot,9),(super::power::Action::Shutdown,12)] {
|
||||||
|
if message.data.as_ref()==action.message(2).as_slice(){input_tx.send(json!({"power":number})).await.unwrap();return}
|
||||||
|
}
|
||||||
|
assert_eq!(&message.data[..],&wire::echo_request(1));
|
||||||
|
let mut action=Vec::new();wire::int(1,1,&mut action);wire::bytes(2,br#"{"seq":1}"#,&mut action);
|
||||||
|
if let Some(channel)=weak.upgrade(){channel.send(&bytes::Bytes::from(wire::envelope(100,3,&action))).await.unwrap();}
|
||||||
|
})}));
|
||||||
|
} else if [wire::CHANNELS[3],wire::CHANNELS[4]].contains(&channel.label()) {
|
||||||
|
let weak=Arc::downgrade(&channel);
|
||||||
|
channel.on_message(Box::new(move |message|{let weak=weak.clone();Box::pin(async move{
|
||||||
|
if let Some(dc)=weak.upgrade(){dc.send(&message.data).await.unwrap();}
|
||||||
|
})}));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}));
|
||||||
|
let(tx,mut candidates)=mpsc::channel(256);
|
||||||
|
host.on_ice_candidate(Box::new(move |candidate| {let tx=tx.clone();Box::pin(async move {
|
||||||
|
if let Some(candidate)=candidate {let mut c=candidate.to_json().unwrap();c.sdp_mid=Some("0".into());c.sdp_mline_index=Some(0);tx.send(c).await.unwrap();}
|
||||||
|
})}));
|
||||||
|
let listener=TcpListener::bind((std::net::Ipv4Addr::LOCALHOST,0)).await.unwrap();let url=format!("ws://{}",listener.local_addr().unwrap());
|
||||||
|
let server_host=host.clone();let(done_tx,mut done_rx)=mpsc::channel::<()>(1);
|
||||||
|
let server=tokio::spawn(async move {
|
||||||
|
let(stream,_)=listener.accept().await.unwrap();
|
||||||
|
let mut ws=tokio_tungstenite::accept_hdr_async(stream,|request:&tokio_tungstenite::tungstenite::handshake::server::Request,response| {
|
||||||
|
assert_eq!(request.headers()["X-NRD-AUTH"],"synthetic-room-ticket");assert_eq!(request.uri().path(),"/socket.io/");Ok(response)
|
||||||
|
}).await.unwrap();
|
||||||
|
ws.send(Message::Text("0{\"sid\":\"fixture\"}".into())).await.unwrap();assert_eq!(ws.next().await.unwrap().unwrap(),Message::Text("40".into()));ws.send(Message::Text("40{}".into())).await.unwrap();
|
||||||
|
let Message::Text(packet)=ws.next().await.unwrap().unwrap() else{panic!()};
|
||||||
|
let begin=packet.find('[').unwrap();let ack=packet[4..begin].parse::<u64>().unwrap();let args:Value=serde_json::from_str(&packet[begin..]).unwrap();assert_eq!(args[0],"control");
|
||||||
|
let app=args[1]["app_control_id"].clone();
|
||||||
|
// As observed live: the viewer sends no ice_id and the cloud assigns one in the ack.
|
||||||
|
assert!(args[1].get("ice_id").is_none()&&args[1].get("client_id").is_none()&&args[1].get("controller_platform").is_none());
|
||||||
|
let ice=json!("synthetic-cloud-ice");
|
||||||
|
let streamer:Value=serde_json::from_str(args[1]["streamer_data"].as_str().unwrap()).unwrap();assert_eq!(streamer["control_id"],app);
|
||||||
|
let Message::Binary(b)=ws.next().await.unwrap().unwrap() else{panic!()};assert!(crate::protocol::decode_wire(&b).is_ok());
|
||||||
|
// A queued foreign candidate must not be installed into this PC.
|
||||||
|
ws.send(Message::Text(format!("42{}",json!(["soac",{"data":{"type":"candidate","ice_id":"synthetic-other-session","candidate":"invalid"}}])))).await.unwrap();
|
||||||
|
ws.send(Message::Text(format!("43{ack}{}",json!(["success",{"iceServers":[],"client_id":"synthetic-host","app_control_id":app,"ice_id":ice}])))).await.unwrap();
|
||||||
|
loop {
|
||||||
|
tokio::select! {
|
||||||
|
_=done_rx.recv()=>break,
|
||||||
|
candidate=candidates.recv()=>if let Some(candidate)=candidate {
|
||||||
|
ws.send(Message::Text(format!("42{}",json!(["soac",{"client_id":"synthetic-host","data":{"type":"candidate","candidate":candidate,"app_control_id":app,"ice_id":ice}}])))).await.unwrap();
|
||||||
|
},
|
||||||
|
message=ws.next()=> {
|
||||||
|
let Some(Ok(Message::Text(packet)))=message else{break};
|
||||||
|
let value:Value=serde_json::from_str(&packet[2..]).unwrap();let body=&value[1]["data"];
|
||||||
|
match body["type"].as_str() {
|
||||||
|
Some("offer")=> {
|
||||||
|
let offered=body["sdp"].as_str().unwrap();
|
||||||
|
assert!(offered.contains("H264/90000") && offered.contains("opus/48000"));
|
||||||
|
assert!(!offered.contains("H265") && !offered.contains("VP8"));
|
||||||
|
assert_eq!(offered.matches("a=recvonly").count(),2);
|
||||||
|
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();
|
||||||
|
gathered.recv().await;
|
||||||
|
// Force one real remote candidate to arrive before the answer.
|
||||||
|
let candidate=candidates.recv().await.unwrap();
|
||||||
|
ws.send(Message::Text(format!("42{}",json!(["soac",{"client_id":"synthetic-host","data":{"type":"candidate","candidate":candidate,"app_control_id":app,"ice_id":ice}}])))).await.unwrap();
|
||||||
|
ws.send(Message::Text(format!("42{}",json!(["soac",{"client_id":"synthetic-host","data":{"type":"answer","sdp":sdp,"ice_id":ice,"app_control_id":app}}])))).await.unwrap();
|
||||||
|
}
|
||||||
|
Some("candidate")=>server_host.add_ice_candidate(serde_json::from_value(body["candidate"].clone()).unwrap()).await.unwrap(),
|
||||||
|
_=>panic!("unexpected synthetic event"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
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 mut session=Session::open(&room,&shape(),"synthetic-device","synthetic-client",options,viewer_settings).await.unwrap();
|
||||||
|
assert_eq!(session.peer.channels.len(),5);
|
||||||
|
let(tool_io,mut tool_rx)=super::tools::attach(&session.peer).unwrap();
|
||||||
|
assert!(super::tools::attach(&session.peer).is_err());
|
||||||
|
loop {
|
||||||
|
tokio::select! {_=tokio::time::sleep(Duration::from_millis(20))=>{},result=session.step()=>result.unwrap()}
|
||||||
|
if session.peer.ready(ReadyPolicy::EchoOnly,false) && opened.load(Ordering::Relaxed)==5 {break}
|
||||||
|
}
|
||||||
|
assert!(!session.peer.ready(ReadyPolicy::Explicit,false));assert!(session.peer.ready(ReadyPolicy::Explicit,true));
|
||||||
|
assert!(session.peer.observed.control_open.load(Ordering::Acquire));
|
||||||
|
for channel in [super::tools::FILE,super::tools::BINARY] {
|
||||||
|
let data=vec![channel as u8;32*1024];tool_io.send(channel,data.clone()).await.unwrap();
|
||||||
|
loop{let(c,b)=tool_rx.recv().await.unwrap();if c==channel{assert_eq!(b,data);break}}
|
||||||
|
}
|
||||||
|
let mut input=super::input::Input::default();
|
||||||
|
let press=input.key(4,true).unwrap();session.peer.send_input(&press).await.unwrap();assert_eq!(input_rx.recv().await.unwrap(),press);
|
||||||
|
for release in input.release_all(){session.peer.send_input(&release).await.unwrap();assert_eq!(input_rx.recv().await.unwrap(),release);}
|
||||||
|
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)};
|
||||||
|
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;}
|
||||||
|
track.write_rtp(&packet(10,2,false,&[0x7c,0x85,1])).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}}
|
||||||
|
assert!(session.peer.desktop.units.pop().is_none());
|
||||||
|
for (action,number) in [(super::power::Action::Reboot,9),(super::power::Action::Shutdown,12)]{
|
||||||
|
super::power::run(&mut session,action).await.unwrap();
|
||||||
|
assert_eq!(input_rx.recv().await.unwrap(),json!({"power":number}));
|
||||||
|
assert!(input_rx.try_recv().is_err());
|
||||||
|
}
|
||||||
|
done_tx.send(()).await.unwrap();server.await.unwrap();session.close().await;
|
||||||
|
assert!(tool_rx.recv().await.is_none());
|
||||||
|
host.close().await.unwrap();router.lock().await.stop().await.unwrap();
|
||||||
|
}).await.expect("offline RTC/session timed out");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn early_candidates_and_foreign_session_messages_are_scoped() {
|
||||||
|
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();
|
||||||
|
assert!(!peer.matches_route(&json!({"ice_id":"foreign"})));
|
||||||
|
assert!(!peer.matches_route(&json!({"app_control_id":"foreign"})));
|
||||||
|
peer.receive(&json!({"type":"answer","ice_id":"foreign","sdp":"invalid"})).await.unwrap();
|
||||||
|
for _ in 0..256 {peer.receive(&json!({"type":"candidate","candidate":{"candidate":""}})).await.unwrap();}
|
||||||
|
assert!(peer.receive(&json!({"type":"candidate","candidate":{"candidate":""}})).await.is_err());
|
||||||
|
peer.close().await;router.lock().await.stop().await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn closing_one_peer_preserves_another_peer() {
|
||||||
|
let(a,b,router)=virtual_network().await;
|
||||||
|
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 second=Peer::new(RTCConfiguration::default(),&shape(),"synthetic-second","synthetic-second-ice","synthetic-host",tx2,b).await.unwrap();
|
||||||
|
first.close().await;
|
||||||
|
assert_ne!(second.pc.connection_state(),webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Closed);
|
||||||
|
assert_eq!(second.channels.len(),5);
|
||||||
|
second.close().await;router.lock().await.stop().await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn opus_and_video_tracks_are_received_independently_in_memory() {
|
||||||
|
tokio::time::timeout(Duration::from_secs(10),async {
|
||||||
|
let(viewer_settings,host_settings,router)=virtual_network().await;
|
||||||
|
let mut engine=MediaEngine::default();engine.register_default_codecs().unwrap();
|
||||||
|
let host=Arc::new(APIBuilder::new().with_media_engine(engine).with_setting_engine(host_settings).build().new_peer_connection(RTCConfiguration::default()).await.unwrap());
|
||||||
|
use webrtc::{track::track_local::{track_local_static_rtp::TrackLocalStaticRTP,TrackLocal,TrackLocalWriter},rtp_transceiver::rtp_codec::RTCRtpCodecCapability};
|
||||||
|
let audio=Arc::new(TrackLocalStaticRTP::new(RTCRtpCodecCapability{mime_type:"audio/opus".into(),clock_rate:48000,channels:2,..Default::default()},"audio".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();
|
||||||
|
let(tx,_rx)=mpsc::channel(256);
|
||||||
|
let peer=Peer::new(RTCConfiguration::default(),&shape(),"app","ice","host",tx,viewer_settings).await.unwrap();
|
||||||
|
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"));
|
||||||
|
host.set_remote_description(offer).await.unwrap();let answer=host.create_answer(None).await.unwrap();
|
||||||
|
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 heard=false;let mut seen=false;
|
||||||
|
for seq in 0..200 {
|
||||||
|
let packet=|payload:&[u8]|webrtc::rtp::packet::Packet{header:webrtc::rtp::header::Header{version:2,sequence_number:seq,timestamp:u32::from(seq)*960,marker:true,..Default::default()},payload:bytes::Bytes::copy_from_slice(payload)};
|
||||||
|
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;
|
||||||
|
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 heard && seen {break}
|
||||||
|
}
|
||||||
|
assert!(heard && seen);peer.close().await;host.close().await.unwrap();router.lock().await.stop().await.unwrap();
|
||||||
|
}).await.expect("in-memory media timed out");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn desktop_file_window_reattaches_without_replacing_peer(){
|
||||||
|
tokio::time::timeout(Duration::from_secs(10),async{
|
||||||
|
use super::tools::{self,files::Files};
|
||||||
|
let(viewer_settings,host_settings,router)=virtual_network().await;
|
||||||
|
let mut engine=MediaEngine::default();engine.register_default_codecs().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 file:Arc<std::sync::Mutex<Option<Arc<webrtc::data_channel::RTCDataChannel>>>>=Default::default();
|
||||||
|
let(tx,mut received)=mpsc::channel(32);
|
||||||
|
let opened=Arc::new(AtomicUsize::new(0));let count=opened.clone();
|
||||||
|
host.on_data_channel(Box::new(move|channel|{let(tx,file,count)=(tx.clone(),file.clone(),count.clone());Box::pin(async move{
|
||||||
|
count.fetch_add(1,Ordering::Relaxed);
|
||||||
|
if channel.label()==wire::CHANNELS[tools::FILE]{*file.lock().unwrap()=Some(channel.clone());}
|
||||||
|
if channel.label()==wire::CHANNELS[tools::CONTROL]{channel.on_message(Box::new(move|m|{
|
||||||
|
let(tx,file)=(tx.clone(),file.clone());Box::pin(async move{
|
||||||
|
if let Some(r)=tools::blob(&m.data,21){
|
||||||
|
if tools::blob(r,8).and_then(|b|tools::blob(b,10)).is_some(){
|
||||||
|
let id=tools::uint(tools::blob(r,1).unwrap(),1);
|
||||||
|
let reply=tools::rpc(id,true,7,&tools::ints(&[(4,1)]));
|
||||||
|
let file=file.lock().unwrap().clone().unwrap();file.send(&bytes::Bytes::from(reply)).await.unwrap();return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let _=tx.send(m.data.to_vec()).await;
|
||||||
|
})
|
||||||
|
}));}
|
||||||
|
})}));
|
||||||
|
let(tx,_rx)=mpsc::channel(256);
|
||||||
|
let peer=Peer::new(RTCConfiguration::default(),&shape(),"app","ice","host",tx,viewer_settings).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();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 opened.load(Ordering::Relaxed)!=5 || peer.channels.iter().any(|c|c.ready_state()!=webrtc::data_channel::data_channel_state::RTCDataChannelState::Open){tokio::time::sleep(Duration::from_millis(10)).await;}
|
||||||
|
let pc=peer.pc.clone();
|
||||||
|
for _ in 0..2{
|
||||||
|
let(io,rx)=tools::attach(&peer).unwrap();assert!(tools::attach(&peer).is_err());
|
||||||
|
let(f,_)=Files::new(io,rx);assert!(f.list_dir(":/").await.unwrap().is_empty());drop(f);
|
||||||
|
peer.tools.lock().unwrap().take();
|
||||||
|
assert!(Arc::ptr_eq(&pc,&peer.pc));
|
||||||
|
let heartbeat=super::input::Input::heartbeat();peer.send_input(&heartbeat).await.unwrap();
|
||||||
|
loop{if serde_json::from_slice::<Value>(&received.recv().await.unwrap()).ok()==Some(heartbeat.clone()){break}}
|
||||||
|
}
|
||||||
|
assert_eq!(opened.load(Ordering::Relaxed),5);
|
||||||
|
peer.close().await;host.close().await.unwrap();router.lock().await.stop().await.unwrap();
|
||||||
|
}).await.expect("desktop file reuse timed out");
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
//! Command-line adapters; terminal output is intentional user output, never a log.
|
||||||
|
use super::*;
|
||||||
|
use std::os::fd::AsRawFd;
|
||||||
|
use tokio::io::AsyncWriteExt;
|
||||||
|
struct Tty {fd:i32,saved:libc::termios}
|
||||||
|
impl Tty {
|
||||||
|
fn raw()->Result<Self,String>{
|
||||||
|
let fd=std::io::stdin().as_raw_fd();let mut saved=unsafe{std::mem::zeroed()};
|
||||||
|
if unsafe{libc::tcgetattr(fd,&mut saved)}!=0{return Err("Terminal command requires a local TTY".into())}
|
||||||
|
let mut raw=saved;unsafe{libc::cfmakeraw(&mut raw)};
|
||||||
|
if unsafe{libc::tcsetattr(fd,libc::TCSANOW,&raw)}!=0{return Err("Cannot set terminal raw mode".into())}Ok(Self{fd,saved})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Drop for Tty {fn drop(&mut self){unsafe{libc::tcsetattr(self.fd,libc::TCSANOW,&self.saved);}}}
|
||||||
|
pub fn size()->(u16,u16){let mut w:libc::winsize=unsafe{std::mem::zeroed()};if unsafe{libc::ioctl(0,libc::TIOCGWINSZ as _,&mut w)}==0{(w.ws_col.max(1),w.ws_row.max(1))}else{(80,24)}}
|
||||||
|
pub async fn terminal(io:Transport,rx:mpsc::Receiver<Packet>,session:Option<u32>)->Result<(),String>{
|
||||||
|
let(t,output)=terminal::Terminal::new(io,rx);t.check_env().await?;
|
||||||
|
let(c,r)=size();let id=match session{Some(id)=>t.attach(id,c,r).await?,None=>t.open(c,r).await?};
|
||||||
|
let result=terminal_loop(&t,output,id).await;let _=t.close(id).await;result
|
||||||
|
}
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait TerminalInput:Sync {
|
||||||
|
async fn write(&self,id:u32,data:&[u8])->Result<(),String>;
|
||||||
|
async fn resize(&self,id:u32,cols:u16,rows:u16)->Result<(),String>;
|
||||||
|
}
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl TerminalInput for terminal::Terminal {
|
||||||
|
async fn write(&self,id:u32,data:&[u8])->Result<(),String>{self.write(id,data).await}
|
||||||
|
async fn resize(&self,id:u32,cols:u16,rows:u16)->Result<(),String>{self.resize(id,cols,rows).await}
|
||||||
|
}
|
||||||
|
pub async fn terminal_loop(t:&impl TerminalInput,mut output:mpsc::Receiver<terminal::Event>,id:u32)->Result<(),String>{
|
||||||
|
async{
|
||||||
|
let _tty=Tty::raw()?;
|
||||||
|
use std::os::unix::fs::OpenOptionsExt;
|
||||||
|
let input=std::fs::OpenOptions::new().read(true).custom_flags(libc::O_NONBLOCK).open("/dev/tty").map_err(|_|"Cannot open local TTY")?;
|
||||||
|
let input=tokio::io::unix::AsyncFd::new(input).map_err(|_|"Cannot watch local TTY")?;let mut stdout=tokio::io::stdout();let mut buf=vec![0;16*1024];
|
||||||
|
let mut resize=tokio::signal::unix::signal(tokio::signal::unix::SignalKind::window_change()).map_err(|_|"Cannot watch terminal size")?;
|
||||||
|
let mut terminate=tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()).map_err(|_|"Cannot watch termination")?;
|
||||||
|
loop{tokio::select!{
|
||||||
|
_=terminate.recv()=>break,
|
||||||
|
n=async {
|
||||||
|
loop {let mut ready=input.readable().await?;
|
||||||
|
match ready.try_io(|fd|std::io::Read::read(&mut fd.get_ref(),&mut buf)){Ok(n)=>break n,Err(_)=>continue}
|
||||||
|
}
|
||||||
|
}=>{let n=n.map_err(|_|"TTY read failed")?;if n==0{break}t.write(id,&buf[..n]).await?;},
|
||||||
|
_=resize.recv()=>{let(c,r)=size();t.resize(id,c,r).await?;},
|
||||||
|
e=output.recv()=>match e{
|
||||||
|
Some(terminal::Event::Output{session,data})if session==id=>{stdout.write_all(&data).await.map_err(|_|"TTY write failed")?;stdout.flush().await.map_err(|_|"TTY flush failed")?;},
|
||||||
|
Some(terminal::Event::Exit{session,code})if session==id=>{if code!=0{return Err(format!("Remote shell exited with code {code}"))}break},
|
||||||
|
Some(terminal::Event::Error{..})|None=>return Err("Terminal session failed".into()),_=>{},
|
||||||
|
}
|
||||||
|
}}Ok(())
|
||||||
|
}.await
|
||||||
|
}
|
||||||
|
pub async fn files(io:Transport,rx:mpsc::Receiver<Packet>,args:&[String])->Result<(),String>{
|
||||||
|
let(f,mut progress)=files::Files::new(io,rx);
|
||||||
|
let reporter=tokio::spawn(async move{while let Some(p)=progress.recv().await{eprintln!("{} / {} bytes",p.transferred,p.total);}});
|
||||||
|
let operation=async {match args.first().map(String::as_str){
|
||||||
|
Some("ls")if args.len()==2=>match f.list_dir(&args[1]).await{Ok(entries)=>{for e in entries{println!("{}",serde_json::json!({"kind":e.kind,"name":e.name,"size":e.size,"path":e.path}));}Ok(())},Err(e)=>Err(e)},
|
||||||
|
Some("get")if args.len()==3=>f.download(&args[1],&args[2]).await,
|
||||||
|
Some("put")if args.len()==3=>f.upload(&args[1],&args[2]).await,
|
||||||
|
_=>Err("files DEVICE ls PATH | get REMOTE LOCAL | put LOCAL REMOTE".into()),
|
||||||
|
}};
|
||||||
|
tokio::pin!(operation);
|
||||||
|
let result=tokio::select!{r=&mut operation=>r,_=tokio::signal::ctrl_c()=>{f.cancel();operation.await}};
|
||||||
|
reporter.abort();result
|
||||||
|
}
|
||||||
|
pub async fn portmap(io:Transport,rx:mpsc::Receiver<Packet>,local:u16,host:&str,port:u16)->Result<(),String>{
|
||||||
|
let p=portmap::PortMap::new(io,rx);let id=p.add_rule("CLI",local,host,port).await?;
|
||||||
|
eprintln!("Listening on 127.0.0.1:{}",p.status()[0].local_port);
|
||||||
|
let result=tokio::select!{r=tokio::signal::ctrl_c()=>r.map_err(|_|"Cannot watch interrupt".to_owned()),_=p.closed()=>Err("Forwarding transport closed".into())};
|
||||||
|
p.remove_rule(id).await?;result
|
||||||
|
}
|
||||||
@@ -0,0 +1,178 @@
|
|||||||
|
//! FTP RPC cases and 1-based file indexes follow src/file_trans.rs.
|
||||||
|
use super::*;
|
||||||
|
use std::{collections::{BTreeMap,HashMap},io::Read,path::{Path,PathBuf},sync::atomic::{AtomicBool,AtomicU64,Ordering}};
|
||||||
|
use tokio::{io::{AsyncReadExt,AsyncWriteExt},sync::{Mutex as AsyncMutex,Notify}};
|
||||||
|
#[derive(Debug,Clone)]
|
||||||
|
pub struct Entry {pub kind:u64,pub name:String,pub size:u64,pub modified:u64,pub path:String}
|
||||||
|
#[derive(Clone,Copy,Debug)]
|
||||||
|
pub struct Progress {pub task:u64,pub transferred:u64,pub total:u64}
|
||||||
|
// Keep request/task ids unique when a desktop file window is reopened.
|
||||||
|
static NEXT:AtomicU64=AtomicU64::new(100);
|
||||||
|
pub struct Files {io:Transport,rx:AsyncMutex<mpsc::Receiver<Packet>>,next:&'static AtomicU64,canceled:AtomicBool,poisoned:AtomicBool,wake:Notify,progress:mpsc::Sender<Progress>}
|
||||||
|
fn check(code:u64)->Result<(),String>{if code==1{Ok(())}else{Err(format!("File transfer error code {code}"))}}
|
||||||
|
fn id(task:u64,index:u64)->Vec<u8>{nested(1,&ints(&[(1,task),(2,index)]))}
|
||||||
|
fn all(data:&[u8],n:u32)->Result<Vec<&[u8]>,String>{Ok(decode_wire(data).map_err(|_|"Invalid file protobuf")?.into_iter().filter_map(|f|match(f.number,f.value){(k,WireValue::Bytes(b))if k==n=>Some(b),_=>None}).collect())}
|
||||||
|
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")?;
|
||||||
|
if out.len()>16*1024*1024{return Err("Directory expansion limit".into())}Ok(out)
|
||||||
|
}
|
||||||
|
fn safe(path:&str)->Result<PathBuf,String>{
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
struct Stage(PathBuf);
|
||||||
|
impl Drop for Stage {fn drop(&mut self){let _=std::fs::remove_dir_all(&self.0);}}
|
||||||
|
/// Download block ids start at 1 on the official Windows host and at 0 (sent out of order within a
|
||||||
|
/// 16-block window) on ours. `next` stays unset until block 0 arrives, 16 blocks are buffered, or the
|
||||||
|
/// file completes; the lowest buffered id is then the base.
|
||||||
|
struct Download {file:tokio::fs::File,written:u64,next:Option<u64>,blocks:BTreeMap<u64,Vec<u8>>}
|
||||||
|
impl Files {
|
||||||
|
pub fn new(io:Transport,rx:mpsc::Receiver<Packet>)->(Self,mpsc::Receiver<Progress>){let(tx,progress)=mpsc::channel(64);(Self{io,rx:AsyncMutex::new(rx),next:&NEXT,canceled:AtomicBool::new(false),poisoned:AtomicBool::new(false),wake:Notify::new(),progress:tx},progress)}
|
||||||
|
/// Cancels the active operation; subsequent operations can reuse the session.
|
||||||
|
pub fn cancel(&self){self.canceled.store(true,Ordering::Release);self.wake.notify_waiters();}
|
||||||
|
async fn recv(&self,rx:&mut mpsc::Receiver<Packet>)->Result<Vec<u8>,String>{loop{
|
||||||
|
let notified=self.wake.notified();tokio::pin!(notified);notified.as_mut().enable();
|
||||||
|
if self.canceled.load(Ordering::Acquire){return Err("File transfer canceled".into())}
|
||||||
|
let(c,b)=tokio::select!{r=receive(rx)=>r?,_=notified=>return Err("File transfer canceled".into())};if c==FILE{return Ok(b)}
|
||||||
|
}}
|
||||||
|
async fn request(&self,rx:&mut mpsc::Receiver<Packet>,case:u32,body:&[u8],expected:u32)->Result<Vec<u8>,String>{
|
||||||
|
if self.poisoned.load(Ordering::Acquire){return Err("File session requires reconnect after failed cleanup".into())}
|
||||||
|
let rid=self.next.fetch_add(1,Ordering::Relaxed);self.io.send(CONTROL,rpc(rid,false,case,body)).await?;
|
||||||
|
loop{let b=self.recv(rx).await?;
|
||||||
|
// Ids and field numbers only, to match official-host replies.
|
||||||
|
if let Some(r)=blob(&b,22){log::info!("File RPC reply: want_id={rid} got_id={} case_fields={:?}",blob(r,1).map(|h|uint(h,1)).unwrap_or(0),blob(r,4).and_then(|f|crate::protocol::decode_wire(f).ok()).map(|f|f.iter().map(|x|x.number).collect::<Vec<_>>()).unwrap_or_default());}
|
||||||
|
else {log::info!("File channel message: fields={:?}",crate::protocol::decode_wire(&b).map(|f|f.iter().map(|x|x.number).collect::<Vec<_>>()).unwrap_or_default());}
|
||||||
|
if let Some(r)=blob(&b,22){if blob(r,1).is_some_and(|h|uint(h,1)==rid){return blob(r,4).and_then(|f|blob(f,expected)).map(Vec::from).ok_or("Unexpected file response case".into())}}}
|
||||||
|
}
|
||||||
|
pub async fn list_dir(&self,path:&str)->Result<Vec<Entry>,String>{
|
||||||
|
let mut rx=self.rx.lock().await;self.canceled.store(false,Ordering::Release);
|
||||||
|
let mut body=id(self.next.fetch_add(1,Ordering::Relaxed),0);wire::bytes(2,path.as_bytes(),&mut body);
|
||||||
|
let b=self.request(&mut rx,10,&body,7).await?;check(uint(&b,4))?;
|
||||||
|
let expanded=blob(&b,5).map(inflate).transpose()?;
|
||||||
|
let rows=if let Some(ref d)=expanded {all(d,1)?}else{all(&b,3)?};
|
||||||
|
rows.into_iter().map(|r|Ok(Entry{kind:uint(r,1),name:text(r,2)?,size:uint(r,3),modified:uint(r,4),path:text(r,5)?})).collect()
|
||||||
|
}
|
||||||
|
pub async fn mkdir(&self,path:&str)->Result<(),String>{let mut rx=self.rx.lock().await;self.canceled.store(false,Ordering::Release);let b=self.request(&mut rx,6,&nested(1,path.as_bytes()),5).await?;check(uint(&b,1))}
|
||||||
|
fn report(&self,task:u64,transferred:u64,total:u64){let _=self.progress.try_send(Progress{task,transferred,total});}
|
||||||
|
pub async fn upload(&self,local:impl AsRef<Path>,remote:&str)->Result<(),String>{
|
||||||
|
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 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=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 mut info=nested(1,name.as_bytes());wire::int(2,size,&mut info);
|
||||||
|
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 result=async {
|
||||||
|
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))?;
|
||||||
|
if uint(&r,2)!=0 || uint(&r,4)!=0{return Err("Unexpected upload skip/resume".into())}
|
||||||
|
let mut done=0;let mut block=0;let mut buf=vec![0;32*1024];
|
||||||
|
loop{let n=file.read(&mut buf).await.map_err(|_|"Upload read failed")?;if n==0{break}
|
||||||
|
let mut b=id(task,1);wire::int(2,block,&mut b);wire::bytes(3,&buf[..n],&mut b);
|
||||||
|
let r=self.request(&mut rx,4,&b,4).await?;check(uint(&r,3))?;
|
||||||
|
if uint(&r,2)!=block || uint(&r,4)!=n as u64{return Err("Invalid upload block acknowledgement".into())}
|
||||||
|
done+=n as u64;block+=1;self.report(task,done,size);
|
||||||
|
}
|
||||||
|
if done!=size{return Err("Upload size changed".into())}
|
||||||
|
let mut b=id(task,1);wire::int(2,1,&mut b);let r=self.request(&mut rx,9,&b,6).await?;check(uint(&r,2))?;self.report(task,done,size);Ok(())
|
||||||
|
}.await;
|
||||||
|
if result.is_err(){let mut b=id(task,1);wire::int(2,7,&mut b);let _=self.io.send(CONTROL,rpc(self.next.fetch_add(1,Ordering::Relaxed),false,9,&b)).await;}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
pub async fn download(&self,remote:&str,local:impl AsRef<Path>)->Result<(),String>{
|
||||||
|
let local=local.as_ref();let mut rx=self.rx.lock().await;self.canceled.store(false,Ordering::Release);
|
||||||
|
let task=self.next.fetch_add(1,Ordering::Relaxed);let mut b=id(task,0);wire::bytes(2,remote.as_bytes(),&mut b);
|
||||||
|
let mut accepted=false;let mut ended=false;
|
||||||
|
let result=async {
|
||||||
|
let r=self.request(&mut rx,2,&b,2).await?;check(uint(&r,2))?;accepted=true;
|
||||||
|
let expanded=blob(&r,5).map(inflate).transpose()?;
|
||||||
|
let list=if let Some(ref e)=expanded{all(e,1)?}else{all(&r,3)?};
|
||||||
|
let files:Vec<(PathBuf,u64)>=list.iter().map(|f|Ok((safe(&text(f,1)?)?,uint(f,2)))).collect::<Result<_,String>>()?;
|
||||||
|
let folder=!text(&r,4)?.is_empty();if !folder && files.len()!=1{return Err("Invalid single file list".into())}
|
||||||
|
let stage=Stage(local.with_file_name(format!(".uu-download-{}",wire::uuid())));
|
||||||
|
tokio::fs::create_dir(&stage.0).await.map_err(|_|"Cannot create download staging directory")?;
|
||||||
|
let mut open:HashMap<u64,Download>=HashMap::new();let mut completed=std::collections::HashSet::new();let total=files.iter().try_fold(0u64,|sum,x|sum.checked_add(x.1)).ok_or("Download size overflow")?;let mut done=0;
|
||||||
|
loop {
|
||||||
|
let data=self.recv(&mut rx).await?;
|
||||||
|
let Some(rpc_body)=blob(&data,21).filter(|r|blob(r,8).is_some()) else{
|
||||||
|
// Shape only (field numbers, size): what the host sent that is not a file RPC request.
|
||||||
|
log::info!("Download skipped message: bytes={} fields={:?} inner={:?}",data.len(),decode_wire(&data).map(|f|f.iter().map(|x|x.number).collect::<Vec<_>>()).unwrap_or_default(),
|
||||||
|
decode_wire(&data).ok().and_then(|f|f.into_iter().find_map(|x|match x.value{WireValue::Bytes(b) if x.number>2=>decode_wire(b).ok().map(|g|(x.number,g.iter().map(|y|y.number).collect::<Vec<_>>())),_=>None})));
|
||||||
|
continue};
|
||||||
|
let ftp=blob(rpc_body,8).unwrap();
|
||||||
|
let rid=blob(rpc_body,1).map(|h|uint(h,1)).unwrap_or(0);
|
||||||
|
let Some(f)=decode_wire(ftp).ok().and_then(|v|v.into_iter().find(|f|matches!(f.value,WireValue::Bytes(_))))else{return Err("Invalid download request".into())};
|
||||||
|
let WireValue::Bytes(body)=f.value else{unreachable!()};let tid=blob(body,1).unwrap_or_default();if uint(tid,1)!=task{return Err("Unexpected download task".into())}let index=uint(tid,2);
|
||||||
|
let mut reply=id(task,index);let mut code=1;
|
||||||
|
let response=match f.number {
|
||||||
|
3=>{
|
||||||
|
if let Some((path,size))=index.checked_sub(1).and_then(|i|files.get(i as usize)) {
|
||||||
|
if uint(body,3)!=*size || open.contains_key(&index)||completed.contains(&index){code=3}else{
|
||||||
|
let path=stage.0.join(path);if let Some(p)=path.parent(){tokio::fs::create_dir_all(p).await.map_err(|_|"Cannot create download directory")?;}
|
||||||
|
match tokio::fs::OpenOptions::new().write(true).create_new(true).open(&path).await {
|
||||||
|
Ok(file)=>{open.insert(index,Download{file,written:0,next:None,blocks:BTreeMap::new()});},Err(_)=>code=6,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else{code=3}wire::int(3,code,&mut reply);3
|
||||||
|
},
|
||||||
|
4=>{
|
||||||
|
let block=uint(body,2);let chunk=blob(body,3).unwrap_or_default();
|
||||||
|
if let Some(d)=open.get_mut(&index){
|
||||||
|
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{
|
||||||
|
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();}
|
||||||
|
while let Some(chunk)=d.next.and_then(|n|d.blocks.remove(&n)){
|
||||||
|
if d.written+chunk.len() as u64>expected || d.file.write_all(&chunk).await.is_err(){code=6;break}
|
||||||
|
d.written+=chunk.len() as u64;done+=chunk.len() as u64;d.next=d.next.map(|n|n+1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else{code=3}
|
||||||
|
wire::int(2,block,&mut reply);wire::int(3,code,&mut reply);wire::int(4,chunk.len() as u64,&mut reply);self.report(task,done,total);4
|
||||||
|
},
|
||||||
|
9=>{
|
||||||
|
if index==u64::MAX {
|
||||||
|
ended=true;check(uint(body,2))?;
|
||||||
|
if completed.len()!=files.len() || !open.is_empty(){return Err("Incomplete download task".into())}
|
||||||
|
if folder {tokio::fs::rename(&stage.0,local).await.map_err(|_|"Cannot finalize download directory")?;}else{tokio::fs::rename(stage.0.join(&files[0].0),local).await.map_err(|_|"Cannot finalize download file")?;}
|
||||||
|
self.report(task,done,total);return Ok(())
|
||||||
|
}
|
||||||
|
if let Some(mut d)=open.remove(&index){
|
||||||
|
if d.next.is_none(){d.next=d.blocks.keys().next().copied();}
|
||||||
|
while let Some(chunk)=d.next.and_then(|n|d.blocks.remove(&n)){
|
||||||
|
if d.written+chunk.len() as u64>files[index as usize-1].1 || d.file.write_all(&chunk).await.is_err(){break}
|
||||||
|
d.written+=chunk.len() as u64;done+=chunk.len() as u64;d.next=d.next.map(|n|n+1);
|
||||||
|
}
|
||||||
|
if uint(body,2)!=1 || d.written!=files[index as usize-1].1 || !d.blocks.is_empty() || d.file.flush().await.is_err(){code=6}else{completed.insert(index);}
|
||||||
|
}else{code=3}wire::int(2,code,&mut reply);6
|
||||||
|
},_=>return Err("Unexpected download request case".into()),
|
||||||
|
};
|
||||||
|
// Numbers only: request case, file index, expected/declared sizes, our result code.
|
||||||
|
if code!=1 || f.number!=4 {log::info!("Download step: case={} index={} files={} declared={} expected={} status={} code={code}",f.number,index,files.len(),uint(body,3),index.checked_sub(1).and_then(|i|files.get(i as usize)).map_or(0,|x|x.1),uint(body,2));}
|
||||||
|
self.io.send(CONTROL,rpc(rid,true,response,&reply)).await?;
|
||||||
|
if code!=1{return Err(format!("Download rejected with code {code}"))}
|
||||||
|
}
|
||||||
|
}.await;
|
||||||
|
if result.is_err() && !ended && (accepted || self.canceled.load(Ordering::Acquire)) {
|
||||||
|
// Reject outstanding host requests so cancellation releases its transfer window.
|
||||||
|
let cleanup=async {
|
||||||
|
loop {
|
||||||
|
let Some((channel,data))=rx.recv().await else{return};if channel!=FILE{continue}
|
||||||
|
let Some(r)=blob(&data,21)else{continue};let Some(ftp)=blob(r,8)else{continue};
|
||||||
|
let rid=blob(r,1).map(|h|uint(h,1)).unwrap_or(0);
|
||||||
|
for (request,response,error_field) in [(3,3,3),(4,4,3),(9,6,2)] {
|
||||||
|
let Some(body)=blob(ftp,request)else{continue};let tid=blob(body,1).unwrap_or_default();
|
||||||
|
if uint(tid,1)!=task{continue}
|
||||||
|
if request==9 && uint(tid,2)==u64::MAX{return}
|
||||||
|
let mut reply=nested(1,tid);wire::int(error_field,7,&mut reply);
|
||||||
|
if request==4{wire::int(2,uint(body,2),&mut reply);}
|
||||||
|
let _=self.io.send(CONTROL,rpc(rid,true,response,&reply)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if tokio::time::timeout(Duration::from_secs(3),cleanup).await.is_err(){self.poisoned.store(true,Ordering::Release);}
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
//! Tool transports use only this peer's ordered channels; payloads are never logged.
|
||||||
|
pub mod terminal;
|
||||||
|
pub mod files;
|
||||||
|
pub mod portmap;
|
||||||
|
pub mod cli;
|
||||||
|
pub mod terminal_ipc;
|
||||||
|
use std::{sync::{Arc,Mutex},time::Duration};
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
use crate::protocol::{decode_wire,WireValue};
|
||||||
|
use super::wire;
|
||||||
|
pub const CONTROL:usize=0;
|
||||||
|
pub const FILE:usize=3;
|
||||||
|
pub const BINARY:usize=4;
|
||||||
|
pub type Packet=(usize,Vec<u8>);
|
||||||
|
pub type Inbox=Arc<Mutex<Option<mpsc::Sender<Packet>>>>;
|
||||||
|
pub const TIMEOUT:Duration=Duration::from_secs(30);
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait Send:std::marker::Send+Sync {async fn send(&self,channel:usize,data:Vec<u8>)->Result<(),String>;}
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Transport(pub Arc<dyn Send>);
|
||||||
|
impl Transport {
|
||||||
|
pub async fn send(&self,channel:usize,data:Vec<u8>)->Result<(),String> {
|
||||||
|
tokio::time::timeout(TIMEOUT,self.0.send(channel,data)).await.map_err(|_|"Tool send timeout")?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
struct Rtc(Vec<Arc<webrtc::data_channel::RTCDataChannel>>);
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl Send for Rtc {
|
||||||
|
async fn send(&self,channel:usize,data:Vec<u8>)->Result<(),String> {
|
||||||
|
let dc=self.0.get(channel).ok_or("Unknown tool channel")?;
|
||||||
|
while dc.buffered_amount().await>1024*1024 {tokio::time::sleep(Duration::from_millis(5)).await;}
|
||||||
|
dc.send(&bytes::Bytes::from(data)).await.map_err(|_|"Tool data channel send failed")?;Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn attach(peer:&super::rtc::Peer)->Result<(Transport,mpsc::Receiver<Packet>),String> {
|
||||||
|
let(tx,rx)=mpsc::channel(256);
|
||||||
|
let mut slot=peer.tools.lock().unwrap();
|
||||||
|
if slot.is_some(){return Err("Tool transport already attached".into())}
|
||||||
|
*slot=Some(tx);Ok((Transport(Arc::new(Rtc(peer.channels.clone()))),rx))
|
||||||
|
}
|
||||||
|
pub fn uint(data:&[u8],n:u32)->u64 {decode_wire(data).ok().and_then(|f|f.into_iter().find_map(|f|match(f.number,f.value){(k,WireValue::Varint(v))if k==n=>Some(v),_=>None})).unwrap_or(0)}
|
||||||
|
pub fn blob(data:&[u8],n:u32)->Option<&[u8]> {decode_wire(data).ok()?.into_iter().find_map(|f|match(f.number,f.value){(k,WireValue::Bytes(v))if k==n=>Some(v),_=>None})}
|
||||||
|
pub fn text(data:&[u8],n:u32)->Result<String,String> {String::from_utf8(blob(data,n).unwrap_or_default().to_vec()).map_err(|_|"Invalid UTF-8 field".into())}
|
||||||
|
pub fn ints(fields:&[(u32,u64)])->Vec<u8>{let mut b=Vec::new();for &(n,v) in fields{wire::int(n,v,&mut b)}b}
|
||||||
|
pub fn nested(n:u32,b:&[u8])->Vec<u8>{let mut out=Vec::new();wire::bytes(n,b,&mut out);out}
|
||||||
|
pub fn rpc(id:u64,response:bool,case:u32,body:&[u8])->Vec<u8>{
|
||||||
|
let mut b=nested(1,&ints(&[(1,id)]));wire::bytes(if response{4}else{8},&nested(case,body),&mut b);
|
||||||
|
wire::envelope(id,if response{22}else{21},&b)
|
||||||
|
}
|
||||||
|
pub async fn receive(rx:&mut mpsc::Receiver<Packet>)->Result<Packet,String>{tokio::time::timeout(TIMEOUT,rx.recv()).await.map_err(|_|"Tool response timeout")?.ok_or("Tool transport closed".into())}
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests;
|
||||||
|
|
||||||
|
pub struct Abort<T>(pub tokio::task::JoinHandle<T>);
|
||||||
|
impl<T> Drop for Abort<T>{fn drop(&mut self){self.0.abort();}}
|
||||||
|
|
||||||
|
pub struct Cancel(pub tokio::task::AbortHandle);
|
||||||
|
impl Drop for Cancel{fn drop(&mut self){self.0.abort();}}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
//! Message27/SYN JSON follows src/port_mapping.rs; TCP half-close is unsupported.
|
||||||
|
use super::*;
|
||||||
|
use std::{collections::HashMap,sync::atomic::{AtomicU64,Ordering}};
|
||||||
|
use tokio::{net::{TcpListener,TcpStream},io::{AsyncReadExt,AsyncWriteExt},sync::watch};
|
||||||
|
#[derive(Clone,Debug)]
|
||||||
|
pub struct Status {pub id:u64,pub name:String,pub local_port:u16,pub streams:usize,pub failures:u64}
|
||||||
|
struct Rule {status:Arc<Mutex<Status>>,stop:watch::Sender<bool>,task:tokio::task::JoinHandle<()>}
|
||||||
|
type Routes=Arc<Mutex<HashMap<(u64,u64),mpsc::Sender<(u64,Vec<u8>)>>>>;
|
||||||
|
pub struct PortMap {io:Transport,session:u64,next:Arc<AtomicU64>,routes:Routes,rules:Mutex<HashMap<u64,Rule>>,alive:watch::Receiver<()>,worker:tokio::task::JoinHandle<()>}
|
||||||
|
pub fn frame(session:u64,rule:u64,stream:u64,kind:u64,payload:&[u8])->Vec<u8>{let mut b=ints(&[(1,session),(2,rule),(3,stream),(4,kind)]);wire::bytes(5,payload,&mut b);wire::envelope(0,27,&b)}
|
||||||
|
struct Route {routes:Routes,key:(u64,u64),status:Arc<Mutex<Status>>}
|
||||||
|
impl Drop for Route {fn drop(&mut self){self.routes.lock().unwrap().remove(&self.key);self.status.lock().unwrap().streams-=1;}}
|
||||||
|
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 {
|
||||||
|
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();
|
||||||
|
let(life,alive)=watch::channel(());
|
||||||
|
let worker=tokio::spawn(async move{
|
||||||
|
let _life=life;
|
||||||
|
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}
|
||||||
|
let key=(uint(f,2),uint(f,3));let sender=route.lock().unwrap().get(&key).cloned();
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
Self{io,session,next:Arc::new(AtomicU64::new(1)),routes,rules:Mutex::new(HashMap::new()),alive,worker}
|
||||||
|
}
|
||||||
|
pub async fn closed(&self){let mut alive=self.alive.clone();let _=alive.changed().await;}
|
||||||
|
pub fn status(&self)->Vec<Status>{self.rules.lock().unwrap().values().map(|r|r.status.lock().unwrap().clone()).collect()}
|
||||||
|
pub async fn add_rule(&self,name:impl Into<String>,local_port:u16,target_host:impl Into<String>,target_port:u16)->Result<u64,String>{
|
||||||
|
if self.worker.is_finished(){return Err("Forwarding transport closed".into())}
|
||||||
|
let host=target_host.into();if host.trim().is_empty() || target_port==0 {return Err("Invalid forwarding target".into())}
|
||||||
|
let listener=TcpListener::bind((std::net::Ipv4Addr::LOCALHOST,local_port)).await.map_err(|_|"Cannot bind local forwarding port")?;
|
||||||
|
let port=listener.local_addr().map_err(|_|"Cannot inspect local forwarding port")?.port();
|
||||||
|
let id=self.next.fetch_add(1,Ordering::Relaxed);let status=Arc::new(Mutex::new(Status{id,name:name.into(),local_port:port,streams:0,failures:0}));
|
||||||
|
let(stopper,mut stop)=watch::channel(false);let st=status.clone();let io=self.io.clone();let routes=self.routes.clone();let next=self.next.clone();let session=self.session;
|
||||||
|
let mut disconnected=self.alive.clone();
|
||||||
|
let task=tokio::spawn(async move {
|
||||||
|
let mut streams=tokio::task::JoinSet::new();
|
||||||
|
loop {tokio::select!{
|
||||||
|
_=stop.changed()=>break,
|
||||||
|
_=disconnected.changed()=>break,
|
||||||
|
_=streams.join_next(),if !streams.is_empty()=>{},
|
||||||
|
accepted=listener.accept()=>{
|
||||||
|
let Ok((socket,_))=accepted else{break};
|
||||||
|
if st.lock().unwrap().streams>=128 {continue}
|
||||||
|
let stream=next.fetch_add(1,Ordering::Relaxed);let(tx,rx)=mpsc::channel(64);
|
||||||
|
routes.lock().unwrap().insert((id,stream),tx);st.lock().unwrap().streams+=1;
|
||||||
|
let guard=Route{routes:routes.clone(),key:(id,stream),status:st.clone()};
|
||||||
|
let io=io.clone();let host=host.clone();let mut cancel=stop.clone();let mut transport_closed=disconnected.clone();
|
||||||
|
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())};
|
||||||
|
if result.is_err(){guard.status.lock().unwrap().failures+=1;}
|
||||||
|
let _=io.send(FILE,frame(session,id,stream,1,&[])).await;drop(guard);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
drop(listener);
|
||||||
|
while streams.join_next().await.is_some(){}
|
||||||
|
});
|
||||||
|
self.rules.lock().unwrap().insert(id,Rule{status,stop:stopper,task});Ok(id)
|
||||||
|
}
|
||||||
|
pub async fn remove_rule(&self,id:u64)->Result<(),String>{
|
||||||
|
let rule=self.rules.lock().unwrap().remove(&id).ok_or("Unknown forwarding rule")?;
|
||||||
|
let _=rule.stop.send(true);let mut task=rule.task;
|
||||||
|
if tokio::time::timeout(TIMEOUT+Duration::from_secs(1),&mut task).await.is_err(){task.abort();return Err("Forwarding shutdown timed out".into())}Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async fn forward(io:&Transport,session:u64,rule:u64,stream:u64,mut socket:TcpStream,mut rx:mpsc::Receiver<(u64,Vec<u8>)>,host:&str,port:u16)->Result<(),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?;
|
||||||
|
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())}
|
||||||
|
let mut buf=vec![0;16*1024];
|
||||||
|
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?;},
|
||||||
|
f=rx.recv()=>match f {Some((2,b))=>if socket.write_all(&b).await.is_err(){break}/* local client hung up: a normal close */,Some((1,_))|None=>break,Some((4,_))=>{},_=>return Err("Unexpected forwarding frame".into())},
|
||||||
|
}}let _=socket.shutdown().await;Ok(())
|
||||||
|
}
|
||||||
@@ -0,0 +1,148 @@
|
|||||||
|
//! TERM types and JSON shapes follow src/terminal.rs (including bare LIST_RESP arrays).
|
||||||
|
use super::*;
|
||||||
|
use std::{collections::HashMap,sync::atomic::{AtomicU32,AtomicU64,Ordering}};
|
||||||
|
use tokio::sync::oneshot;
|
||||||
|
use serde_json::{json,Value};
|
||||||
|
pub const OPEN:u8=1;pub const STDIN:u8=2;pub const RESIZE:u8=3;pub const CLOSE:u8=4;
|
||||||
|
pub const STDOUT:u8=5;pub const EXIT:u8=6;pub const ERROR:u8=8;
|
||||||
|
pub const LIST_REQ:u8=9;pub const LIST_RESP:u8=10;pub const KILL_REQ:u8=11;pub const KILL_RESP:u8=12;pub const OPEN_RESP:u8=13;
|
||||||
|
pub struct Frame {pub kind:u8,pub session:u32,pub payload:Vec<u8>}
|
||||||
|
pub fn encode(kind:u8,session:u32,payload:&[u8])->Vec<u8>{
|
||||||
|
let mut b=b"TERM\x01".to_vec();b.extend([kind,0,0]);b.extend(session.to_le_bytes());b.extend((payload.len() as u32).to_le_bytes());b.extend(payload);b
|
||||||
|
}
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct Decoder {buffer:Vec<u8>}
|
||||||
|
impl Decoder {
|
||||||
|
pub fn push(&mut self,data:&[u8])->Result<Vec<Frame>,String>{
|
||||||
|
if self.buffer.len()+data.len()>2*1024*1024 {return Err("Terminal buffer limit".into())}
|
||||||
|
self.buffer.extend(data);let mut out=Vec::new();let mut at=0;
|
||||||
|
while self.buffer.len()-at>=16 {
|
||||||
|
let b=&self.buffer[at..];
|
||||||
|
if &b[..5]!=b"TERM\x01" || !(1..=16).contains(&b[5]) {return Err("Invalid TERM header".into())}
|
||||||
|
let len=u32::from_le_bytes(b[12..16].try_into().unwrap()) as usize;
|
||||||
|
if len>1024*1024{return Err("TERM payload limit".into())}
|
||||||
|
if b.len()<16+len {break}
|
||||||
|
out.push(Frame{kind:b[5],session:u32::from_le_bytes(b[8..12].try_into().unwrap()),payload:b[16..16+len].to_vec()});at+=16+len;
|
||||||
|
}
|
||||||
|
self.buffer.drain(..at);Ok(out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub enum Event {Output{session:u32,data:Vec<u8>},Exit{session:u32,code:i64},Error{session:u32}}
|
||||||
|
type Pending=Arc<Mutex<HashMap<(u8,u64),oneshot::Sender<Result<Vec<u8>,String>>>>>;
|
||||||
|
struct Request {pending:Pending,key:(u8,u64)}
|
||||||
|
impl Drop for Request {fn drop(&mut self){self.pending.lock().unwrap().remove(&self.key);}}
|
||||||
|
pub struct Terminal {io:Transport,pending:Pending,next:AtomicU32,rpc:AtomicU64,worker:tokio::task::JoinHandle<()>}
|
||||||
|
impl Drop for Terminal {fn drop(&mut self){self.worker.abort();}}
|
||||||
|
impl Terminal {
|
||||||
|
pub fn new(io:Transport,mut rx:mpsc::Receiver<Packet>)->(Self,mpsc::Receiver<Event>){
|
||||||
|
let pending:Pending=Arc::new(Mutex::new(HashMap::new()));let p=pending.clone();let(tx,events)=mpsc::channel(256);
|
||||||
|
let worker=tokio::spawn(async move {
|
||||||
|
let mut decoder=Decoder::default();
|
||||||
|
while let Some((channel,data))=rx.recv().await {
|
||||||
|
if channel==CONTROL {
|
||||||
|
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()));}
|
||||||
|
}}continue
|
||||||
|
}
|
||||||
|
if channel!=BINARY {continue}
|
||||||
|
let Ok(frames)=decoder.push(&data) else {break};
|
||||||
|
let mut overflow=false;
|
||||||
|
for f in frames {
|
||||||
|
let event=match f.kind {
|
||||||
|
STDOUT=>Some(Event::Output{session:f.session,data:f.payload.clone()}),
|
||||||
|
EXIT=>Some(Event::Exit{session:f.session,code:serde_json::from_slice::<Value>(&f.payload).ok().and_then(|v|v["exit_code"].as_i64()).unwrap_or(-1)}),
|
||||||
|
ERROR=>Some(Event::Error{session:f.session}),_=>None,
|
||||||
|
};
|
||||||
|
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 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)
|
||||||
|
}else{f.payload};
|
||||||
|
if let Some(t)=p.lock().unwrap().remove(&key){let _=t.send(Ok(payload));}
|
||||||
|
}
|
||||||
|
if overflow {break}
|
||||||
|
}
|
||||||
|
for (_,t) in p.lock().unwrap().drain(){let _=t.send(Err("Terminal channel closed or output overflow".into()));}
|
||||||
|
});
|
||||||
|
(Self{io,pending,next:AtomicU32::new(1),rpc:AtomicU64::new(10),worker},events)
|
||||||
|
}
|
||||||
|
async fn request(&self,key:(u8,u64),channel:usize,data:Vec<u8>)->Result<Vec<u8>,String>{
|
||||||
|
if self.worker.is_finished(){return Err("Terminal channel closed".into())}
|
||||||
|
let(tx,rx)=oneshot::channel();{
|
||||||
|
let mut pending=self.pending.lock().unwrap();
|
||||||
|
if pending.contains_key(&key){return Err("Terminal request already pending".into())}pending.insert(key,tx);
|
||||||
|
}
|
||||||
|
let _request=Request{pending:self.pending.clone(),key};
|
||||||
|
let result=async {self.io.send(channel,data).await?;tokio::time::timeout(TIMEOUT,rx).await.map_err(|_|"Terminal request timeout")?.map_err(|_|"Terminal closed")?}.await;
|
||||||
|
result
|
||||||
|
}
|
||||||
|
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 b=self.request((24,id),CONTROL,wire::envelope(id,21,&r)).await?;
|
||||||
|
if uint(&b,1)!=0 {return Err("Terminal environment unavailable".into())}Ok(())
|
||||||
|
}
|
||||||
|
pub async fn list_sessions(&self)->Result<Vec<Value>,String>{
|
||||||
|
let id=self.next.fetch_add(1,Ordering::Relaxed);
|
||||||
|
serde_json::from_slice(&self.request((LIST_RESP,id as u64),BINARY,encode(LIST_REQ,id,&[])).await?).map_err(|_|"Invalid terminal session list".into())
|
||||||
|
}
|
||||||
|
pub async fn open(&self,cols:u16,rows:u16)->Result<u32,String>{
|
||||||
|
self.open_session(0,cols,rows).await
|
||||||
|
}
|
||||||
|
pub async fn attach(&self,id:u32,cols:u16,rows:u16)->Result<u32,String>{
|
||||||
|
if id==0 || !self.list_sessions().await?.iter().any(|s|s["session_id"].as_u64()==Some(id as u64)){return Err("Terminal session no longer exists".into())}
|
||||||
|
self.open_session(id,cols,rows).await
|
||||||
|
}
|
||||||
|
async fn open_session(&self,id:u32,cols:u16,rows:u16)->Result<u32,String>{
|
||||||
|
// Host TerminalConn handles OPEN(nonzero) as reattachment.
|
||||||
|
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?;
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
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 close(&self,id:u32)->Result<(),String>{self.io.send(BINARY,encode(CLOSE,id,&[])).await}
|
||||||
|
pub async fn kill(&self,id:u32)->Result<(),String>{
|
||||||
|
let b=self.request((KILL_RESP,id as u64),BINARY,encode(KILL_REQ,id,&[])).await?;
|
||||||
|
if serde_json::from_slice::<Value>(&b).ok().and_then(|v|v["code"].as_u64())!=Some(0){return Err("Terminal kill rejected".into())}Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The remote shell needs a real terminal emulator: `$TERMINAL`, else the first installed common one.
|
||||||
|
/// On failure the window stays open so the error is readable.
|
||||||
|
pub fn terminal_command(exe: &std::path::Path, id: &str, session: Option<u32>) -> Option<std::process::Command> {
|
||||||
|
terminal_launcher(exe,"terminal",id,session)
|
||||||
|
}
|
||||||
|
pub fn local_terminal_command(exe:&std::path::Path,path:&std::path::Path,session:Option<u32>)->Option<std::process::Command>{
|
||||||
|
terminal_launcher(exe,"terminal-local",path.to_str()?,session)
|
||||||
|
}
|
||||||
|
fn terminal_launcher(exe:&std::path::Path,mode:&str,id:&str,session:Option<u32>)->Option<std::process::Command>{
|
||||||
|
let script = r#""$0" "$1" "$2" ${3:+"$3"} || { printf '\n终端连接失败,按回车关闭'; read _; }"#;
|
||||||
|
let inner = ["sh", "-c", script, &exe.to_string_lossy(), mode, id].map(str::to_owned);
|
||||||
|
let mut inner = inner.to_vec();
|
||||||
|
if let Some(id) = session { inner.push(id.to_string()); }
|
||||||
|
let found = |name: &str| std::env::var_os("PATH").is_some_and(|p| std::env::split_paths(&p).any(|d| d.join(name).is_file()));
|
||||||
|
let custom = std::env::var("TERMINAL").ok().filter(|t| !t.is_empty() && found(t));
|
||||||
|
let candidates = ["xdg-terminal-exec", "x-terminal-emulator", "kitty", "foot", "alacritty", "wezterm", "ghostty", "gnome-terminal", "konsole", "xfce4-terminal", "xterm"];
|
||||||
|
let program = custom.or_else(|| candidates.iter().find(|c| found(c)).map(|c| c.to_string()))?;
|
||||||
|
let prefix: &[&str] = match program.as_str() {
|
||||||
|
"xdg-terminal-exec" | "kitty" | "foot" => &[],
|
||||||
|
"wezterm" => &["start", "--"],
|
||||||
|
"gnome-terminal" => &["--"],
|
||||||
|
"xfce4-terminal" => &["-x"],
|
||||||
|
_ => &["-e"],
|
||||||
|
};
|
||||||
|
let mut command = std::process::Command::new(program);
|
||||||
|
command.args(prefix).args(inner).stdin(std::process::Stdio::null()).stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null());
|
||||||
|
Some(command)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Only known metadata is allowed across the GTK boundary; never serialize the full host object.
|
||||||
|
pub fn session_rows(sessions:&[Value])->String{
|
||||||
|
let mut rows:Vec<_>=sessions.iter().filter_map(|s|{
|
||||||
|
let id=u32::try_from(s["session_id"].as_u64()?).ok().filter(|n|*n!=0)?;
|
||||||
|
let shell=s["shell"].as_str().unwrap_or("--").chars().take(256).collect::<String>().replace(['\t','\n','\r','\0']," ");
|
||||||
|
let state=match s["state"].as_str(){Some("running")=>"运行中",Some("detached")=>"已分离",_=>"--"};
|
||||||
|
Some((id,format!("{id}\t{shell}\t{}\t{state}\n",s["created_at_ms"].as_u64().unwrap_or(0))))
|
||||||
|
}).collect();rows.sort_by_key(|r|r.0);rows.into_iter().map(|r|r.1).collect()
|
||||||
|
}
|
||||||
@@ -0,0 +1,152 @@
|
|||||||
|
//! Local TTY clients share the list process's Terminal; no cloud credentials cross IPC.
|
||||||
|
use super::{terminal::{Terminal,Event},cli};
|
||||||
|
use std::{path::{Path,PathBuf},sync::Arc};
|
||||||
|
use tokio::{io::{AsyncRead,AsyncReadExt,AsyncWrite,AsyncWriteExt},net::{UnixListener,UnixStream},sync::{broadcast,mpsc,Mutex},task::JoinSet};
|
||||||
|
use serde::{Serialize,Deserialize};
|
||||||
|
const MAX:usize=4*1024*1024+1024;
|
||||||
|
#[derive(Serialize,Deserialize)]
|
||||||
|
enum Message {Open{session:Option<u32>,cols:u16,rows:u16},Ready(u32),Input(Vec<u8>),Resize(u16,u16),Output(Vec<u8>),Exit(i64),Close,Error}
|
||||||
|
async fn read(r:&mut (impl AsyncRead+Unpin))->Result<Message,String>{
|
||||||
|
let n=r.read_u32().await.map_err(|_|"Local terminal closed")? as usize;
|
||||||
|
if n>MAX{return Err("Local terminal frame limit".into())}
|
||||||
|
let mut b=vec![0;n];r.read_exact(&mut b).await.map_err(|_|"Local terminal closed")?;
|
||||||
|
serde_json::from_slice(&b).map_err(|_|"Invalid local terminal frame".into())
|
||||||
|
}
|
||||||
|
async fn write(w:&mut (impl AsyncWrite+Unpin),m:&Message)->Result<(),String>{
|
||||||
|
let b=serde_json::to_vec(m).map_err(|_|"Invalid local terminal frame")?;
|
||||||
|
if b.len()>MAX{return Err("Local terminal frame limit".into())}
|
||||||
|
w.write_u32(b.len() as u32).await.map_err(|_|"Local terminal closed")?;
|
||||||
|
w.write_all(&b).await.map_err(|_|"Local terminal closed".into())
|
||||||
|
}
|
||||||
|
pub struct Server {dir:PathBuf,pub path:PathBuf,listener:UnixListener}
|
||||||
|
impl Drop for Server {fn drop(&mut self){let _=std::fs::remove_file(&self.path);let _=std::fs::remove_dir(&self.dir);}}
|
||||||
|
impl Server {
|
||||||
|
pub fn new()->Result<Self,String>{
|
||||||
|
use std::os::unix::fs::DirBuilderExt;
|
||||||
|
let dir=std::env::temp_dir().join(format!("uu-tty-{}",super::super::wire::uuid()));
|
||||||
|
std::fs::DirBuilder::new().mode(0o700).create(&dir).map_err(|_|"Cannot create private terminal directory")?;
|
||||||
|
let path=dir.join("tty");
|
||||||
|
match UnixListener::bind(&path){Ok(listener)=>Ok(Self{dir,path,listener}),Err(_)=>{let _=std::fs::remove_dir(dir);Err("Cannot bind local terminal socket".into())}}
|
||||||
|
}
|
||||||
|
pub async fn run(self,t:Arc<Terminal>,events:broadcast::Sender<Event>){
|
||||||
|
let opening=Arc::new(Mutex::new(()));let active=Arc::new(std::sync::Mutex::new(std::collections::HashSet::new()));let mut clients=JoinSet::new();
|
||||||
|
loop{tokio::select!{
|
||||||
|
accepted=self.listener.accept(),if clients.len()<16=>match accepted{
|
||||||
|
Ok((socket,_))=>{let(t,opening,active,output)=(t.clone(),opening.clone(),active.clone(),events.subscribe());clients.spawn(async move{let _=serve(socket,t,opening,active,output).await;});},
|
||||||
|
Err(_)=>break,
|
||||||
|
},
|
||||||
|
_=clients.join_next(),if !clients.is_empty()=>{},
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
type Sessions=Arc<std::sync::Mutex<std::collections::HashSet<u32>>>;
|
||||||
|
struct Active {id:u32,sessions:Sessions}
|
||||||
|
impl Drop for Active {fn drop(&mut self){self.sessions.lock().unwrap().remove(&self.id);}}
|
||||||
|
async fn serve<S:AsyncRead+AsyncWrite+Unpin+Send+'static>(mut socket:S,t:Arc<Terminal>,opening:Arc<Mutex<()>>,active:Sessions,mut output:broadcast::Receiver<Event>)->Result<(),String>{
|
||||||
|
let first=tokio::time::timeout(super::TIMEOUT,read(&mut socket)).await.map_err(|_|"Local terminal open timeout")??;
|
||||||
|
let Message::Open{session,cols,rows}=first else{return Err("Expected local terminal open".into())};
|
||||||
|
let id={let _guard=opening.lock().await;
|
||||||
|
if session.is_some_and(|id|active.lock().unwrap().contains(&id)){return Err("Terminal session already attached locally".into())}
|
||||||
|
let id=match session{Some(id)=>t.attach(id,cols,rows).await?,None=>t.open(cols,rows).await?};
|
||||||
|
active.lock().unwrap().insert(id);id
|
||||||
|
};
|
||||||
|
let _active=Active{id,sessions:active};
|
||||||
|
let result=async{
|
||||||
|
write(&mut socket,&Message::Ready(id)).await?;
|
||||||
|
let(mut r,mut w)=tokio::io::split(socket);let(tx,mut commands)=mpsc::channel(32);
|
||||||
|
let _reader=super::Abort(tokio::spawn(async move{loop{
|
||||||
|
let message=read(&mut r).await;let failed=message.is_err();
|
||||||
|
if tx.send(message).await.is_err() || failed{break}
|
||||||
|
}}));
|
||||||
|
loop{tokio::select!{
|
||||||
|
m=commands.recv()=>match m.ok_or("Local terminal closed")??{
|
||||||
|
Message::Input(b)=>t.write(id,&b).await?,Message::Resize(c,r)=>t.resize(id,c,r).await?,Message::Close=>break,
|
||||||
|
_=>return Err("Invalid local terminal command".into()),
|
||||||
|
},
|
||||||
|
e=output.recv()=>match e{
|
||||||
|
Ok(Event::Output{session,data})if session==id=>write(&mut w,&Message::Output(data)).await?,
|
||||||
|
Ok(Event::Exit{session,code})if session==id=>{write(&mut w,&Message::Exit(code)).await?;break},
|
||||||
|
Ok(Event::Error{session})if session==id=>{write(&mut w,&Message::Error).await?;break},
|
||||||
|
Err(_)=>return Err("Local terminal output closed or overflowed".into()),_=>{},
|
||||||
|
},
|
||||||
|
}}Ok(())
|
||||||
|
}.await;
|
||||||
|
let _=t.close(id).await;result
|
||||||
|
}
|
||||||
|
struct Input(Mutex<tokio::net::unix::OwnedWriteHalf>);
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl cli::TerminalInput for Input {
|
||||||
|
async fn write(&self,_:u32,b:&[u8])->Result<(),String>{write(&mut *self.0.lock().await,&Message::Input(b.to_vec())).await}
|
||||||
|
async fn resize(&self,_:u32,c:u16,r:u16)->Result<(),String>{write(&mut *self.0.lock().await,&Message::Resize(c,r)).await}
|
||||||
|
}
|
||||||
|
pub async fn client(path:&Path,session:Option<u32>)->Result<(),String>{
|
||||||
|
let mut socket=UnixStream::connect(path).await.map_err(|_|"Terminal list is no longer available")?;
|
||||||
|
let(c,r)=cli::size();write(&mut socket,&Message::Open{session,cols:c,rows:r}).await?;
|
||||||
|
let Message::Ready(id)=read(&mut socket).await? else{return Err("Local terminal open failed".into())};
|
||||||
|
let(mut r,w)=socket.into_split();let input=Input(Mutex::new(w));let(tx,rx)=mpsc::channel(256);
|
||||||
|
let worker=tokio::spawn(async move{loop{
|
||||||
|
let event=match read(&mut r).await{Ok(Message::Output(data))=>Event::Output{session:id,data},Ok(Message::Exit(code))=>Event::Exit{session:id,code},_=>break};
|
||||||
|
if tx.send(event).await.is_err(){break}
|
||||||
|
}});
|
||||||
|
let result=cli::terminal_loop(&input,rx,id).await;
|
||||||
|
worker.abort();let _=worker.await;let _=write(&mut *input.0.lock().await,&Message::Close).await;result
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)] mod tests {
|
||||||
|
use super::*;
|
||||||
|
struct Mock(mpsc::Sender<super::super::Packet>);
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl super::super::Send for Mock {
|
||||||
|
async fn send(&self,c:usize,b:Vec<u8>)->Result<(),String>{self.0.send((c,b)).await.map_err(|_|"Mock closed".into())}
|
||||||
|
}
|
||||||
|
#[tokio::test] async fn two_local_ttys_share_one_terminal_and_close_independently(){
|
||||||
|
tokio::time::timeout(std::time::Duration::from_secs(3),async{
|
||||||
|
use super::super::{terminal as term,BINARY};
|
||||||
|
let(tx,mut requests)=mpsc::channel(256);let(input,rx)=mpsc::channel(256);
|
||||||
|
let(t,mut events)=Terminal::new(super::super::Transport(Arc::new(Mock(tx))),rx);let t=Arc::new(t);
|
||||||
|
let(output,_)=broadcast::channel(256);let sender=output.clone();
|
||||||
|
let _relay=super::super::Abort(tokio::spawn(async move{while let Some(e)=events.recv().await{let _=sender.send(e);}}));
|
||||||
|
let(closed,mut closes)=mpsc::channel(8);
|
||||||
|
let _host=super::super::Abort(tokio::spawn(async move{
|
||||||
|
let mut next=40;
|
||||||
|
while let Some((channel,b))=requests.recv().await{
|
||||||
|
assert_eq!(channel,BINARY);let f=term::Decoder::default().push(&b).unwrap().remove(0);
|
||||||
|
let reply=match f.kind{
|
||||||
|
term::OPEN=>{next+=1;term::encode(term::OPEN_RESP,next,br#"{"code":0}"#)},
|
||||||
|
term::STDIN=>term::encode(term::STDOUT,f.session,&f.payload),
|
||||||
|
term::CLOSE=>{closed.send(f.session).await.unwrap();continue},_=>panic!("Unexpected remote request"),
|
||||||
|
};input.send((BINARY,reply)).await.unwrap();
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
let opening=Arc::new(Mutex::new(()));let active:Sessions=Default::default();let mut clients=Vec::new();let mut tasks=Vec::new();
|
||||||
|
for _ in 0..2{
|
||||||
|
let(mut client,server)=tokio::io::duplex(4096);
|
||||||
|
let(t,opening,active,events)=(t.clone(),opening.clone(),active.clone(),output.subscribe());
|
||||||
|
tasks.push(tokio::spawn(serve(server,t,opening,active,events)));
|
||||||
|
write(&mut client,&Message::Open{session:None,cols:80,rows:24}).await.unwrap();
|
||||||
|
let Message::Ready(id)=read(&mut client).await.unwrap()else{panic!()};clients.push((client,id));
|
||||||
|
}
|
||||||
|
let(mut first,id)=clients.remove(0);write(&mut first,&Message::Close).await.unwrap();
|
||||||
|
assert_eq!(closes.recv().await.unwrap(),id);tasks.remove(0).await.unwrap().unwrap();
|
||||||
|
let(mut second,id)=clients.remove(0);
|
||||||
|
// Split an incoming frame while remote output is ready: the reader must retain framing.
|
||||||
|
let b=serde_json::to_vec(&Message::Input(vec![0,255,27])).unwrap();second.write_u32(b.len() as u32).await.unwrap();second.write_all(&b[..2]).await.unwrap();
|
||||||
|
assert!(output.send(Event::Output{session:id,data:b"synthetic".to_vec()}).is_ok());
|
||||||
|
assert!(matches!(read(&mut second).await,Ok(Message::Output(b))if b==b"synthetic"));
|
||||||
|
second.write_all(&b[2..]).await.unwrap();
|
||||||
|
assert!(matches!(read(&mut second).await,Ok(Message::Output(b))if b==[0,255,27]));
|
||||||
|
drop(second);assert_eq!(closes.recv().await.unwrap(),id);tasks.remove(0).await.unwrap().unwrap_err();
|
||||||
|
assert!(active.lock().unwrap().is_empty());
|
||||||
|
}).await.unwrap();
|
||||||
|
}
|
||||||
|
#[tokio::test] async fn framing_rejects_oversize_before_payload(){
|
||||||
|
let(mut a,mut b)=tokio::io::duplex(64);a.write_u32(MAX as u32+1).await.unwrap();
|
||||||
|
assert!(read(&mut b).await.err().unwrap().contains("limit"));
|
||||||
|
}
|
||||||
|
#[tokio::test] async fn framing_roundtrips_binary_and_detects_truncation(){
|
||||||
|
let(mut a,mut b)=tokio::io::duplex(256);
|
||||||
|
write(&mut a,&Message::Input(vec![0,255,27])).await.unwrap();
|
||||||
|
assert!(matches!(read(&mut b).await,Ok(Message::Input(v))if v==[0,255,27]));
|
||||||
|
a.write_u32(30).await.unwrap();drop(a);assert!(read(&mut b).await.is_err());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,194 @@
|
|||||||
|
use super::*;
|
||||||
|
use tokio::io::{AsyncReadExt,AsyncWriteExt};
|
||||||
|
struct Mock(mpsc::Sender<Packet>);
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl Send for Mock {async fn send(&self,c:usize,b:Vec<u8>)->Result<(),String>{self.0.send((c,b)).await.map_err(|_|"Mock closed".into())}}
|
||||||
|
fn pair()->(Transport,mpsc::Receiver<Packet>,mpsc::Sender<Packet>,mpsc::Receiver<Packet>){let(tx,out)=mpsc::channel(256);let(input,rx)=mpsc::channel(256);(Transport(Arc::new(Mock(tx))),rx,input,out)}
|
||||||
|
struct Temp(std::path::PathBuf);
|
||||||
|
impl Temp {fn new()->Self{let p=std::env::temp_dir().join(format!("uu-tools-test-{}",wire::uuid()));std::fs::create_dir(&p).unwrap();Self(p)}}
|
||||||
|
impl Drop for Temp {fn drop(&mut self){let _=std::fs::remove_dir_all(&self.0);}}
|
||||||
|
fn host_files()->(files::Files,mpsc::Receiver<files::Progress>,tokio::task::JoinHandle<()>,tokio::task::JoinHandle<()>){
|
||||||
|
let(io,rx,input,mut out)=pair();let(tx,mut host)=mpsc::unbounded_channel();
|
||||||
|
let session=crate::file_trans::FileSession::new(tx,Arc::new(std::sync::atomic::AtomicU64::new(1)));
|
||||||
|
let server=tokio::spawn(async move{while let Some((c,b))=out.recv().await{assert_eq!(c,CONTROL);assert!(session.handle(&decode_wire(&b).unwrap()));}});
|
||||||
|
let pump=tokio::spawn(async move{while let Some(b)=host.recv().await{if input.send((FILE,b)).await.is_err(){break}}});
|
||||||
|
let(f,p)=files::Files::new(io,rx);(f,p,server,pump)
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn terminal_frames_match_host_and_incremental_decoder(){
|
||||||
|
let a=terminal::encode(terminal::OPEN,7,br#"{"cols":80,"rows":24}"#);
|
||||||
|
assert_eq!(a,crate::terminal::encode(crate::terminal::OPEN,7,br#"{"cols":80,"rows":24}"#));
|
||||||
|
let mut d=terminal::Decoder::default();let b=[a.clone(),terminal::encode(terminal::STDOUT,7,b"synthetic")].concat();let mut frames=Vec::new();
|
||||||
|
for part in b.chunks(3){frames.extend(d.push(part).unwrap());}assert_eq!(frames.len(),2);assert_eq!(frames[1].payload,b"synthetic");
|
||||||
|
assert!(terminal::Decoder::default().push(b"TERM\x02\x01\0\0\0\0\0\0\0\0\0\0").is_err());
|
||||||
|
let mut bad=a;bad[12..16].copy_from_slice(&u32::MAX.to_le_bytes());assert!(terminal::Decoder::default().push(&bad).is_err());
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn terminal_rpc_list_open_io_and_errors(){
|
||||||
|
let(io,rx,input,mut out)=pair();let(t,mut events)=terminal::Terminal::new(io,rx);
|
||||||
|
let server=tokio::spawn(async move{
|
||||||
|
let(tx,mut replies)=mpsc::unbounded_channel();let host=crate::terminal::TerminalConn::new(tx);
|
||||||
|
while let Some((c,b))=out.recv().await{
|
||||||
|
if c==CONTROL{let r=crate::terminal::rpc_response(&decode_wire(blob(&b,21).unwrap()).unwrap()).unwrap();input.send((CONTROL,wire::envelope(1,22,&r))).await.unwrap();continue}
|
||||||
|
assert_eq!(c,BINARY);let f=crate::terminal::decode(&b).unwrap();
|
||||||
|
match f.kind{
|
||||||
|
terminal::LIST_REQ=>{host.handle(&b);input.send((BINARY,replies.recv().await.unwrap())).await.unwrap();},
|
||||||
|
terminal::OPEN=>{let v:serde_json::Value=serde_json::from_slice(&f.payload).unwrap();assert_eq!(v["cols"],91);input.send((BINARY,terminal::encode(terminal::OPEN_RESP,37,br#"{"code":0}"#))).await.unwrap();},
|
||||||
|
terminal::STDIN=>input.send((BINARY,terminal::encode(terminal::STDOUT,f.session,&f.payload))).await.unwrap(),
|
||||||
|
terminal::RESIZE=>assert_eq!(serde_json::from_slice::<serde_json::Value>(&f.payload).unwrap()["rows"],40),
|
||||||
|
terminal::KILL_REQ=>input.send((BINARY,terminal::encode(terminal::KILL_RESP,f.session,br#"{"code":1}"#))).await.unwrap(),
|
||||||
|
terminal::CLOSE=>break,_=>panic!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
t.check_env().await.unwrap();let _sessions=t.list_sessions().await.unwrap();let id=t.open(91,30).await.unwrap();
|
||||||
|
let data=vec![42;50_000];t.write(id,&data).await.unwrap();let mut received=Vec::new();
|
||||||
|
while received.len()<data.len(){match tokio::time::timeout(TIMEOUT,events.recv()).await.unwrap().unwrap(){terminal::Event::Output{data,..}=>{assert!(data.len()<=16*1024);received.extend(data)},_=>panic!()}}
|
||||||
|
assert_eq!(received,data);t.resize(id,100,40).await.unwrap();assert!(t.kill(id).await.is_err());t.close(id).await.unwrap();server.await.unwrap();
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn file_host_roundtrip_multiblock_empty_mkdir_and_errors(){
|
||||||
|
let tmp=Temp::new();let(f,mut progress,server,pump)=host_files();
|
||||||
|
let remote=tmp.0.join("remote");f.mkdir(remote.to_str().unwrap()).await.unwrap();
|
||||||
|
let source=tmp.0.join("source");let data:Vec<u8>=(0..130_111).map(|i|(i%251)as u8).collect();std::fs::write(&source,&data).unwrap();
|
||||||
|
let dest=remote.join("uploaded");f.upload(&source,dest.to_str().unwrap()).await.unwrap();assert_eq!(std::fs::read(&dest).unwrap(),data);
|
||||||
|
let entries=f.list_dir(remote.to_str().unwrap()).await.unwrap();assert_eq!(entries.len(),1);assert_eq!(entries[0].size,data.len()as u64);
|
||||||
|
let downloaded=tmp.0.join("downloaded");f.download(dest.to_str().unwrap(),&downloaded).await.unwrap();assert_eq!(std::fs::read(downloaded).unwrap(),data);
|
||||||
|
let empty=tmp.0.join("empty");std::fs::write(&empty,[]).unwrap();f.upload(&empty,remote.join("empty").to_str().unwrap()).await.unwrap();
|
||||||
|
let copy=tmp.0.join("empty-copy");f.download(empty.to_str().unwrap(),©).await.unwrap();assert_eq!(std::fs::metadata(copy).unwrap().len(),0);
|
||||||
|
let folder_copy=tmp.0.join("folder-copy");f.download(remote.to_str().unwrap(),&folder_copy).await.unwrap();assert_eq!(std::fs::read(folder_copy.join("uploaded")).unwrap(),data);
|
||||||
|
assert!(f.list_dir(tmp.0.join("missing").to_str().unwrap()).await.unwrap_err().contains("9"));
|
||||||
|
assert!(f.download(tmp.0.join("missing").to_str().unwrap(),tmp.0.join("absent")).await.unwrap_err().contains("9"));
|
||||||
|
assert!(progress.try_recv().is_ok());server.abort();pump.abort();
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn directory_expansion_and_invalid_compression(){
|
||||||
|
use std::io::Write;let mut z=flate2::write::ZlibEncoder::new(Vec::new(),flate2::Compression::fast());z.write_all(&nested(1,b"synthetic")).unwrap();let data=z.finish().unwrap();assert_eq!(files::inflate(&data).unwrap(),nested(1,b"synthetic"));assert!(files::inflate(b"invalid").is_err());
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn files_cancel_wakes_pending_request(){
|
||||||
|
let(io,rx,_input,mut out)=pair();let(f,_)=files::Files::new(io,rx);
|
||||||
|
let op=f.list_dir("synthetic");tokio::pin!(op);
|
||||||
|
tokio::select!{r=&mut op=>panic!("unexpected {r:?}"),_=out.recv()=>{}};
|
||||||
|
f.cancel();assert!(tokio::time::timeout(Duration::from_secs(1),op).await.unwrap().unwrap_err().contains("canceled"));
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn forwarding_mock_loopback_echo_fragmentation_remove_and_reject(){
|
||||||
|
let target=tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();let port=target.local_addr().unwrap().port();
|
||||||
|
let echo=tokio::spawn(async move{let(mut s,_)=target.accept().await.unwrap();let mut b=vec![0;4096];loop{let n=s.read(&mut b).await.unwrap();if n==0{break}s.write_all(&b[..n]).await.unwrap();}});
|
||||||
|
let(io,rx,input,mut out)=pair();let p=portmap::PortMap::new(io,rx);
|
||||||
|
let host=tokio::spawn(async move{
|
||||||
|
let mut tcp:Option<tokio::net::TcpStream>=None;
|
||||||
|
while let Some((c,b))=out.recv().await{
|
||||||
|
assert_eq!(c,FILE);let f=blob(&b,27).unwrap();let(s,r,t)=(uint(f,1),uint(f,2),uint(f,3));
|
||||||
|
match uint(f,4){
|
||||||
|
0=>{let v:serde_json::Value=serde_json::from_slice(blob(f,5).unwrap()).unwrap();assert_eq!(v["version"],1);let target=v["target_port"].as_u64().unwrap()as u16;
|
||||||
|
let connected=if target==port {tokio::net::TcpStream::connect(("127.0.0.1",target)).await.ok()}else{None};let ok=connected.is_some();tcp=connected;
|
||||||
|
input.send((FILE,portmap::frame(s,r,t,3,if ok{br#"{"version":1,"ok":true}"#}else{br#"{"version":1,"ok":false}"#}))).await.unwrap();},
|
||||||
|
2=>{let payload=blob(f,5).unwrap();assert!(payload.len()<=16*1024);let socket=tcp.as_mut().unwrap();socket.write_all(payload).await.unwrap();let mut b=vec![0;payload.len()];socket.read_exact(&mut b).await.unwrap();input.send((FILE,portmap::frame(s,r,t,2,&b))).await.unwrap();},
|
||||||
|
1=>{tcp.take();},_=>panic!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let id=p.add_rule("echo",0,"127.0.0.1",port).await.unwrap();let local=p.status()[0].local_port;
|
||||||
|
let mut socket=tokio::net::TcpStream::connect(("127.0.0.1",local)).await.unwrap();let bytes=vec![91;100_000];socket.write_all(&bytes).await.unwrap();let mut received=vec![0;bytes.len()];tokio::time::timeout(TIMEOUT,socket.read_exact(&mut received)).await.unwrap().unwrap();assert_eq!(received,bytes);
|
||||||
|
p.remove_rule(id).await.unwrap();assert!(p.status().is_empty());assert!(tokio::net::TcpStream::connect(("127.0.0.1",local)).await.is_err());
|
||||||
|
let id=p.add_rule("reject",0,"127.0.0.1",1).await.unwrap();let mut socket=tokio::net::TcpStream::connect(("127.0.0.1",p.status()[0].local_port)).await.unwrap();assert_eq!(tokio::time::timeout(TIMEOUT,socket.read(&mut [0])).await.unwrap().unwrap(),0);
|
||||||
|
assert_eq!(p.status()[0].failures,1);p.remove_rule(id).await.unwrap();host.abort();echo.await.unwrap();
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn file_download_reorders_blocks_and_rejects_traversal(){
|
||||||
|
for (unsafe_name,one_based) in [(false,true),(false,false),(true,false)]{
|
||||||
|
let tmp=Temp::new();let(io,rx,input,mut out)=pair();let(f,_)=files::Files::new(io,rx);
|
||||||
|
let host=tokio::spawn(async move{
|
||||||
|
let(_,b)=out.recv().await.unwrap();let r=blob(&b,21).unwrap();let rid=uint(blob(r,1).unwrap(),1);let req=blob(blob(r,8).unwrap(),2).unwrap();let task=uint(blob(req,1).unwrap(),1);
|
||||||
|
let mut info=nested(1,if unsafe_name{b"../escape"}else{b"file"});wire::int(2,6,&mut info);
|
||||||
|
let mut response=nested(1,&ints(&[(1,task)]));wire::int(2,1,&mut response);wire::bytes(3,&info,&mut response);
|
||||||
|
input.send((FILE,rpc(rid,true,2,&response))).await.unwrap();
|
||||||
|
if unsafe_name{
|
||||||
|
// Let the client reject an ask, then close the host task normally.
|
||||||
|
let mut b=nested(1,&ints(&[(1,task),(2,1)]));wire::int(3,6,&mut b);input.send((FILE,rpc(900,false,3,&b))).await.unwrap();
|
||||||
|
let(_,reply)=out.recv().await.unwrap();assert_eq!(uint(blob(blob(blob(&reply,22).unwrap(),4).unwrap(),3).unwrap(),3),7);
|
||||||
|
}else{
|
||||||
|
let mut b=nested(1,&ints(&[(1,task),(2,1)]));wire::int(3,6,&mut b);input.send((FILE,rpc(900,false,3,&b))).await.unwrap();out.recv().await.unwrap();
|
||||||
|
// Official Windows host numbering starts at 1 (base fixed at completion); our host sends 0-based out of order.
|
||||||
|
let blocks:&[(u64,&[u8;2])]=if one_based {&[(1,b"ab"),(3,b"ef"),(2,b"cd")]} else {&[(2,b"ef"),(0,b"ab"),(1,b"cd")]};
|
||||||
|
for &(block,data) in blocks {let mut b=nested(1,&ints(&[(1,task),(2,1)]));wire::int(2,block,&mut b);wire::bytes(3,data,&mut b);input.send((FILE,rpc(901+block,false,4,&b))).await.unwrap();out.recv().await.unwrap();}
|
||||||
|
let mut b=nested(1,&ints(&[(1,task),(2,1)]));wire::int(2,1,&mut b);input.send((FILE,rpc(903,false,9,&b))).await.unwrap();out.recv().await.unwrap();
|
||||||
|
}
|
||||||
|
let mut b=nested(1,&ints(&[(1,task),(2,u64::MAX)]));wire::int(2,1,&mut b);input.send((FILE,rpc(904,false,9,&b))).await.unwrap();
|
||||||
|
});
|
||||||
|
let target=tmp.0.join("received");let result=f.download("synthetic",&target).await;
|
||||||
|
if unsafe_name{assert!(result.unwrap_err().contains("Unsafe"));assert!(!tmp.0.join("escape").exists());assert!(!target.exists());}else{result.unwrap();assert_eq!(std::fs::read(target).unwrap(),b"abcdef");}
|
||||||
|
host.await.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn cancel_download_rejects_host_and_removes_staging(){
|
||||||
|
let tmp=Temp::new();let(io,rx,input,mut out)=pair();let(f,_)=files::Files::new(io,rx);let(asked,ready)=tokio::sync::oneshot::channel();
|
||||||
|
let host=tokio::spawn(async move{
|
||||||
|
let(_,b)=out.recv().await.unwrap();let r=blob(&b,21).unwrap();let rid=uint(blob(r,1).unwrap(),1);let req=blob(blob(r,8).unwrap(),2).unwrap();let task=uint(blob(req,1).unwrap(),1);
|
||||||
|
let mut info=nested(1,b"file");wire::int(2,10,&mut info);let mut b=nested(1,&ints(&[(1,task)]));wire::int(2,1,&mut b);wire::bytes(3,&info,&mut b);
|
||||||
|
input.send((FILE,rpc(rid,true,2,&b))).await.unwrap();let _=asked.send(());
|
||||||
|
let mut b=nested(1,&ints(&[(1,task),(2,1)]));wire::int(3,10,&mut b);input.send((FILE,rpc(900,false,3,&b))).await.unwrap();
|
||||||
|
let(_,reply)=out.recv().await.unwrap();let code=uint(blob(blob(blob(&reply,22).unwrap(),4).unwrap(),3).unwrap(),3);
|
||||||
|
if code==1{
|
||||||
|
let mut b=nested(1,&ints(&[(1,task),(2,1)]));wire::int(2,0,&mut b);wire::bytes(3,b"synthetic!",&mut b);input.send((FILE,rpc(901,false,4,&b))).await.unwrap();let(_,r)=out.recv().await.unwrap();assert_eq!(uint(blob(blob(blob(&r,22).unwrap(),4).unwrap(),4).unwrap(),3),7);
|
||||||
|
}else{assert_eq!(code,7);}
|
||||||
|
let mut b=nested(1,&ints(&[(1,task),(2,u64::MAX)]));wire::int(2,7,&mut b);input.send((FILE,rpc(902,false,9,&b))).await.unwrap();
|
||||||
|
});
|
||||||
|
let target=tmp.0.join("received");let op=f.download("synthetic",&target);tokio::pin!(op);
|
||||||
|
tokio::select!{r=&mut op=>panic!("unexpected {r:?}"),_=ready=>{}}f.cancel();
|
||||||
|
assert!(tokio::time::timeout(Duration::from_secs(4),op).await.unwrap().unwrap_err().contains("canceled"));host.await.unwrap();assert!(!target.exists());assert_eq!(std::fs::read_dir(&tmp.0).unwrap().count(),0);
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn forwarding_transport_close_stops_listener(){
|
||||||
|
let(io,rx,input,_out)=pair();let p=portmap::PortMap::new(io,rx);let id=p.add_rule("closed",0,"127.0.0.1",1).await.unwrap();let port=p.status()[0].local_port;
|
||||||
|
drop(input);tokio::time::timeout(Duration::from_secs(1),p.closed()).await.unwrap();
|
||||||
|
p.remove_rule(id).await.unwrap();assert!(tokio::net::TcpStream::connect(("127.0.0.1",port)).await.is_err());
|
||||||
|
assert!(p.add_rule("unavailable",0,"127.0.0.1",1).await.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn terminal_metadata_is_allowlisted_and_sorted(){
|
||||||
|
let rows=terminal::session_rows(&[
|
||||||
|
serde_json::json!({"session_id":9,"shell":"/bin/sh\tX\nY\0","state":"detached","created_at_ms":123,"output":"PRIVATE","name":"PRIVATE"}),
|
||||||
|
serde_json::json!({"session_id":2,"shell":"/bin/bash","state":"running"}),
|
||||||
|
serde_json::json!({"session_id":0}),serde_json::json!({"session_id":4294967296u64}),
|
||||||
|
]);
|
||||||
|
assert_eq!(rows,"2\t/bin/bash\t0\t运行中\n9\t/bin/sh X Y \t123\t已分离\n");
|
||||||
|
assert!(!rows.contains("PRIVATE"));assert!(terminal::session_rows(&[]).is_empty());
|
||||||
|
}
|
||||||
|
#[tokio::test]
|
||||||
|
async fn terminal_attach_uses_existing_id_and_rejects_absent_session(){
|
||||||
|
let(io,rx,input,mut out)=pair();let(t,_events)=terminal::Terminal::new(io,rx);
|
||||||
|
let host=tokio::spawn(async move{
|
||||||
|
for id in [37,99]{
|
||||||
|
let(c,b)=out.recv().await.unwrap();assert_eq!(c,BINARY);
|
||||||
|
let f=crate::terminal::decode(&b).unwrap();assert_eq!(f.kind,terminal::LIST_REQ);
|
||||||
|
input.send((BINARY,terminal::encode(terminal::LIST_RESP,f.session,br#"[{"session_id":37}]"#))).await.unwrap();
|
||||||
|
if id==37{
|
||||||
|
let(_,b)=out.recv().await.unwrap();let f=crate::terminal::decode(&b).unwrap();
|
||||||
|
assert_eq!(f.kind,terminal::OPEN);assert_eq!(f.session,37);
|
||||||
|
let v:serde_json::Value=serde_json::from_slice(&f.payload).unwrap();assert_eq!(v["cols"],90);assert_eq!(v["rows"],30);
|
||||||
|
input.send((BINARY,terminal::encode(terminal::OPEN_RESP,37,br#"{"code":0}"#))).await.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert!(out.try_recv().is_err());
|
||||||
|
});
|
||||||
|
assert_eq!(t.attach(37,90,30).await.unwrap(),37);
|
||||||
|
assert!(t.attach(99,90,30).await.is_err());assert!(t.attach(0,90,30).await.is_err());host.await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn terminal_kill_acknowledgement_and_empty_refresh(){
|
||||||
|
let(io,rx,input,mut out)=pair();let(t,_events)=terminal::Terminal::new(io,rx);
|
||||||
|
let host=tokio::spawn(async move{
|
||||||
|
let(c,b)=out.recv().await.unwrap();assert_eq!(c,BINARY);
|
||||||
|
let f=crate::terminal::decode(&b).unwrap();assert_eq!(f.kind,terminal::KILL_REQ);assert_eq!(f.session,42);
|
||||||
|
input.send((BINARY,terminal::encode(terminal::KILL_RESP,42,br#"{"code":0}"#))).await.unwrap();
|
||||||
|
let(_,b)=out.recv().await.unwrap();let f=crate::terminal::decode(&b).unwrap();assert_eq!(f.kind,terminal::LIST_REQ);
|
||||||
|
input.send((BINARY,terminal::encode(terminal::LIST_RESP,f.session,b"[]"))).await.unwrap();
|
||||||
|
});
|
||||||
|
t.kill(42).await.unwrap();assert!(t.list_sessions().await.unwrap().is_empty());host.await.unwrap();
|
||||||
|
}
|
||||||
@@ -0,0 +1,240 @@
|
|||||||
|
//! GTK windows for file transfer, port mapping and terminal sessions. Paths, names and
|
||||||
|
//! ports are shown only in the local window; logs keep to the existing id/field diagnostics.
|
||||||
|
use super::tools::{files::{Entry,Files},portmap::PortMap,Packet,Transport};
|
||||||
|
use std::ffi::{CStr,CString};
|
||||||
|
use std::os::raw::{c_char,c_int,c_ulonglong};
|
||||||
|
use std::sync::{Arc,Mutex,atomic::{AtomicU64,AtomicBool,Ordering}};
|
||||||
|
use std::time::{Duration,Instant};
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
fn uu_tools_ui_start(kind:*const c_char,name:*const c_char)->c_int;
|
||||||
|
fn uu_tools_ui_take(out:*mut c_char,len:usize)->c_int;
|
||||||
|
fn uu_tools_ui_closed()->c_int;
|
||||||
|
fn uu_tools_ui_hide();
|
||||||
|
fn uu_tools_ui_present();
|
||||||
|
fn uu_tools_ui_toast(text:*const c_char);
|
||||||
|
fn uu_files_ui_set_remote(path:*const c_char,rows:*const c_char);
|
||||||
|
fn uu_files_ui_task(id:c_int,name:*const c_char,status:*const c_char,transferred:c_ulonglong,total:c_ulonglong,from:*const c_char,to:*const c_char,done:c_int);
|
||||||
|
fn uu_files_ui_refresh_local();
|
||||||
|
fn uu_terminal_ui_set(rows:*const c_char,connected:c_int);
|
||||||
|
fn uu_portmap_ui_set_rules(rows:*const c_char);
|
||||||
|
}
|
||||||
|
fn c(text:&str)->CString{CString::new(text.replace('\0',"")).unwrap_or_default()}
|
||||||
|
fn toast(text:&str){let t=c(text);unsafe{uu_tools_ui_toast(t.as_ptr())}}
|
||||||
|
fn take()->Option<String>{
|
||||||
|
let mut buf=vec![0 as c_char;4096];
|
||||||
|
(unsafe{uu_tools_ui_take(buf.as_mut_ptr(),buf.len())}!=0).then(||unsafe{CStr::from_ptr(buf.as_ptr())}.to_string_lossy().into_owned())
|
||||||
|
}
|
||||||
|
pub fn hide(){unsafe{uu_tools_ui_hide()}}
|
||||||
|
pub fn present(){unsafe{uu_tools_ui_present()}}
|
||||||
|
pub fn closed()->bool{(unsafe{uu_tools_ui_closed()})!=0}
|
||||||
|
fn start(kind:&str,name:&str)->Result<(),String>{
|
||||||
|
let(k,n)=(c(kind),c(name));
|
||||||
|
if unsafe{uu_tools_ui_start(k.as_ptr(),n.as_ptr())}!=0{return Err("Cannot open the window (GTK3 and a graphical session are required)".into())}Ok(())
|
||||||
|
}
|
||||||
|
/// Last path component for either separator; hosts may be Windows (`C:\x`) or Unix.
|
||||||
|
pub fn base_name(path:&str)->&str{path.trim_end_matches(['/','\\']).rsplit(['/','\\']).next().filter(|s|!s.is_empty()).unwrap_or(path)}
|
||||||
|
pub fn remote_join(dir:&str,name:&str)->String{
|
||||||
|
let sep=if dir.contains('\\') && !dir.contains('/'){'\\'}else{'/'};
|
||||||
|
if dir.ends_with(['/','\\']){format!("{dir}{name}")}else{format!("{dir}{sep}{name}")}
|
||||||
|
}
|
||||||
|
/// First free `name`, `name (1)`, ... in `dir` so a download never replaces a local file.
|
||||||
|
pub fn free_local(dir:&std::path::Path,name:&str)->std::path::PathBuf{
|
||||||
|
let(stem,ext)=match name.rfind('.'){Some(i) if i>0=>(&name[..i],&name[i..]),_=>(name,"")};
|
||||||
|
(0..).map(|n|dir.join(if n==0{name.to_owned()}else{format!("{stem} ({n}){ext}")})).find(|p|!p.exists()).unwrap()
|
||||||
|
}
|
||||||
|
pub fn entry_rows(entries:&[Entry])->String{
|
||||||
|
let clean=|s:&str|s.replace(['\t','\n','\r']," ");
|
||||||
|
entries.iter().map(|e|format!("{}\t{}\t{}\t{}\t{}\n",e.kind,clean(&e.name),e.size,e.modified,clean(&e.path))).collect()
|
||||||
|
}
|
||||||
|
enum Job {List(String),Mkdir(String,String),Transfer{id:i32,upload:bool,from:String,dir:String,generation:u64}}
|
||||||
|
struct Active {id:i32,name:String,from:String,to:String,last:Instant}
|
||||||
|
fn task(id:i32,name:&str,status:&str,done:u64,total:u64,from:&str,to:&str,state:i32){
|
||||||
|
let(n,s,f,t)=(c(name),c(status),c(from),c(to));
|
||||||
|
unsafe{uu_files_ui_task(id,n.as_ptr(),s.as_ptr(),done,total,f.as_ptr(),t.as_ptr(),state)}
|
||||||
|
}
|
||||||
|
fn reason(error:&str)->String{if error.contains("canceled"){"已取消".into()}else{format!("失败:{error}")}}
|
||||||
|
|
||||||
|
pub async fn files(io:Transport,rx:mpsc::Receiver<Packet>,name:&str)->Result<(),String>{
|
||||||
|
start("files",name)?;
|
||||||
|
let(f,mut progress)=Files::new(io,rx);let f=Arc::new(f);
|
||||||
|
let generation=Arc::new(AtomicU64::new(0));
|
||||||
|
let active:Arc<Mutex<Option<Active>>>=Arc::new(Mutex::new(None));
|
||||||
|
let shown=active.clone();
|
||||||
|
let reporter=tokio::spawn(async move{
|
||||||
|
while let Some(p)=progress.recv().await{
|
||||||
|
let mut guard=shown.lock().unwrap();let Some(a)=guard.as_mut() else{continue};
|
||||||
|
if a.last.elapsed()<Duration::from_millis(200) && p.transferred<p.total{continue}a.last=Instant::now();
|
||||||
|
let percent=if p.total>0{p.transferred*100/p.total}else{0};
|
||||||
|
task(a.id,&a.name,&format!("传输中 {percent}%"),p.transferred,p.total,&a.from,&a.to,0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let _reporter=super::tools::Cancel(reporter.abort_handle());
|
||||||
|
let(jobs,mut queue)=mpsc::unbounded_channel::<Job>();
|
||||||
|
let closing=Arc::new(AtomicBool::new(false));let worker_closing=closing.clone();
|
||||||
|
let(worker_f,worker_gen,worker_active)=(f.clone(),generation.clone(),active.clone());
|
||||||
|
let mut worker=tokio::spawn(async move{
|
||||||
|
let mut remote=":/".to_owned();
|
||||||
|
while let Some(job)=queue.recv().await{if worker_closing.load(Ordering::Acquire){break}match job{
|
||||||
|
Job::List(path)=>match worker_f.list_dir(&path).await{
|
||||||
|
Ok(entries)=>{remote=path.clone();let(p,r)=(c(&path),c(&entry_rows(&entries)));unsafe{uu_files_ui_set_remote(p.as_ptr(),r.as_ptr())}}
|
||||||
|
Err(e)=>toast(&format!("无法读取远端目录:{e}")),
|
||||||
|
},
|
||||||
|
Job::Mkdir(path,parent)=>match worker_f.mkdir(&path).await{
|
||||||
|
Ok(())=>if parent==remote{if let Ok(entries)=worker_f.list_dir(&remote).await{let(p,r)=(c(&remote),c(&entry_rows(&entries)));unsafe{uu_files_ui_set_remote(p.as_ptr(),r.as_ptr())}}},
|
||||||
|
Err(e)=>toast(&format!("无法创建远端文件夹:{e}")),
|
||||||
|
},
|
||||||
|
Job::Transfer{id,upload,from,dir,generation}=>{
|
||||||
|
let name=base_name(&from).to_owned();
|
||||||
|
let to=if upload{remote_join(&dir,&name)}else{free_local(std::path::Path::new(&dir),&name).to_string_lossy().into_owned()};
|
||||||
|
if generation!=worker_gen.load(Ordering::Acquire){task(id,&name,"已取消",0,0,&from,&to,2);continue}
|
||||||
|
*worker_active.lock().unwrap()=Some(Active{id,name:name.clone(),from:from.clone(),to:to.clone(),last:Instant::now()});
|
||||||
|
task(id,&name,"传输中",0,0,&from,&to,0);
|
||||||
|
let result=if upload{worker_f.upload(&from,&to).await}else{worker_f.download(&from,&to).await};
|
||||||
|
*worker_active.lock().unwrap()=None;
|
||||||
|
match result{
|
||||||
|
Ok(())=>{
|
||||||
|
let size=std::fs::metadata(if upload{&from}else{&to}).map(|m|if m.is_file(){m.len()}else{0}).unwrap_or(0);
|
||||||
|
task(id,&name,"已完成",size,size,&from,&to,1);
|
||||||
|
if upload{if dir==remote{if let Ok(entries)=worker_f.list_dir(&remote).await{let(p,r)=(c(&remote),c(&entry_rows(&entries)));unsafe{uu_files_ui_set_remote(p.as_ptr(),r.as_ptr())}}}}
|
||||||
|
else{unsafe{uu_files_ui_refresh_local()}}
|
||||||
|
}
|
||||||
|
Err(e)=>task(id,&name,&reason(&e),0,0,&from,&to,2),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
});
|
||||||
|
let _worker=super::tools::Cancel(worker.abort_handle());
|
||||||
|
let _=jobs.send(Job::List(":/".into()));
|
||||||
|
static NEXT_TASK:std::sync::atomic::AtomicI32=std::sync::atomic::AtomicI32::new(1);
|
||||||
|
let result=loop{
|
||||||
|
if closed(){break Ok(())}
|
||||||
|
if worker.is_finished(){break Err("File transfer worker stopped".into())}
|
||||||
|
while let Some(cmd)=take(){
|
||||||
|
let parts:Vec<&str>=cmd.split('\t').collect();
|
||||||
|
match parts.as_slice(){
|
||||||
|
["ls",path]=>{let _=jobs.send(Job::List((*path).into()));}
|
||||||
|
["mkdir",path]=>{let parent=path.rfind(['/','\\']).map(|i|path[..i.max(1)].to_owned()).unwrap_or_default();let _=jobs.send(Job::Mkdir((*path).into(),parent));}
|
||||||
|
[kind@("put"|"get"),from,dir]=>{
|
||||||
|
let(id,upload)=(NEXT_TASK.fetch_add(1,Ordering::Relaxed),*kind=="put");
|
||||||
|
let name=base_name(from);task(id,name,"等待中",0,0,from,dir,0);
|
||||||
|
let _=jobs.send(Job::Transfer{id,upload,from:(*from).into(),dir:(*dir).into(),generation:generation.load(Ordering::Acquire)});
|
||||||
|
}
|
||||||
|
["cancel"]=>{generation.fetch_add(1,Ordering::AcqRel);f.cancel();}
|
||||||
|
_=>{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||||
|
};
|
||||||
|
closing.store(true,Ordering::Release);drop(jobs);f.cancel();
|
||||||
|
// Allow the existing transfer cancellation handshake to release the host's window.
|
||||||
|
if tokio::time::timeout(Duration::from_secs(5),&mut worker).await.is_err(){worker.abort();let _=worker.await;}
|
||||||
|
reporter.abort();let _=reporter.await;result
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn portmap(io:Transport,rx:mpsc::Receiver<Packet>,name:&str)->Result<(),String>{
|
||||||
|
start("portmap",name)?;
|
||||||
|
let p=PortMap::new(io,rx);
|
||||||
|
let mut targets:std::collections::HashMap<u64,String>=Default::default();
|
||||||
|
let mut last=String::from("-");
|
||||||
|
let closed_transport=p.closed();tokio::pin!(closed_transport);
|
||||||
|
let result=loop{
|
||||||
|
if closed(){break Ok(())}
|
||||||
|
while let Some(cmd)=take(){
|
||||||
|
let parts:Vec<&str>=cmd.split('\t').collect();
|
||||||
|
match parts.as_slice(){
|
||||||
|
["add",rule,local,host,port]=>match(local.parse::<u16>(),port.parse::<u16>()){
|
||||||
|
(Ok(local),Ok(port))=>match p.add_rule(*rule,local,*host,port).await{
|
||||||
|
Ok(id)=>{targets.insert(id,format!("{host}:{port}"));}
|
||||||
|
Err(e)=>toast(&format!("添加失败:{}",if e.contains("bind"){"本地端口已被占用".to_owned()}else{e})),
|
||||||
|
},
|
||||||
|
_=>toast("端口无效"),
|
||||||
|
},
|
||||||
|
["remove",id]=>if let Ok(id)=id.parse::<u64>(){
|
||||||
|
if let Err(e)=p.remove_rule(id).await{toast(&format!("删除失败:{e}"))}targets.remove(&id);
|
||||||
|
},
|
||||||
|
_=>{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut status=p.status();status.sort_by_key(|s|s.id);
|
||||||
|
let rows:String=status.iter().map(|s|format!("{}\t{}\t127.0.0.1:{}\t{}\t{}\n",s.id,s.name.replace(['\t','\n']," "),s.local_port,
|
||||||
|
targets.get(&s.id).map(String::as_str).unwrap_or("-"),
|
||||||
|
if s.failures>0{format!("已启用 · {} 个连接 · {} 次失败",s.streams,s.failures)}else{format!("已启用 · {} 个连接",s.streams)})).collect();
|
||||||
|
if rows!=last{let r=c(&rows);unsafe{uu_portmap_ui_set_rules(r.as_ptr())};last=rows;}
|
||||||
|
tokio::select!{_=&mut closed_transport=>{toast("连接已断开");tokio::time::sleep(Duration::from_secs(3)).await;break Err("Forwarding transport closed".into())},_=tokio::time::sleep(Duration::from_millis(200))=>{}}
|
||||||
|
};
|
||||||
|
let ids:Vec<u64>=p.status().iter().map(|s|s.id).collect();for id in ids{let _=p.remove_rule(id).await;}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
// The worker serializes LIST/KILL requests; GTK commands remain responsive during network waits.
|
||||||
|
pub async fn terminal(io:Transport,rx:mpsc::Receiver<Packet>,_device:&str,name:&str)->Result<(),String>{
|
||||||
|
use super::tools::terminal::{Terminal,session_rows,local_terminal_command};
|
||||||
|
let exe=std::env::current_exe().map_err(|_|"Cannot locate executable")?;
|
||||||
|
start("terminal",name)?;
|
||||||
|
let(t,mut events)=Terminal::new(io,rx);let t=Arc::new(t);
|
||||||
|
let server=super::tools::terminal_ipc::Server::new()?;let path=server.path.clone();
|
||||||
|
let(output,_)=tokio::sync::broadcast::channel(256);
|
||||||
|
let ipc=super::tools::Abort(tokio::spawn(server.run(t.clone(),output.clone())));
|
||||||
|
let relay=super::tools::Abort(tokio::spawn(async move{while let Some(e)=events.recv().await{let _=output.send(e);}}));
|
||||||
|
let(jobs,mut queue)=mpsc::unbounded_channel::<u32>();
|
||||||
|
let worker=tokio::spawn(async move{
|
||||||
|
let operation=async{
|
||||||
|
t.check_env().await?;
|
||||||
|
let mut refresh=tokio::time::interval(Duration::from_secs(2));
|
||||||
|
refresh.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||||
|
loop{tokio::select!{
|
||||||
|
_=refresh.tick()=>{
|
||||||
|
let rows=c(&session_rows(&t.list_sessions().await?));
|
||||||
|
unsafe{uu_terminal_ui_set(rows.as_ptr(),1)}
|
||||||
|
},
|
||||||
|
id=queue.recv()=>match id{
|
||||||
|
Some(id)=>{if t.kill(id).await.is_err(){toast("结束会话失败,会话可能已退出")}
|
||||||
|
let rows=c(&session_rows(&t.list_sessions().await?));unsafe{uu_terminal_ui_set(rows.as_ptr(),1)}},
|
||||||
|
None=>return Ok::<(),String>(()),
|
||||||
|
},
|
||||||
|
_=tokio::time::sleep(Duration::from_millis(100)),if relay.0.is_finished()=>return Err("Terminal channel closed".into()),
|
||||||
|
}}
|
||||||
|
}.await;
|
||||||
|
if operation.is_err(){unsafe{uu_terminal_ui_set(c("").as_ptr(),0)};toast("终端连接已断开,请关闭窗口后重试")}
|
||||||
|
});
|
||||||
|
let _worker=super::tools::Cancel(worker.abort_handle());
|
||||||
|
let mut children:Vec<std::process::Child>=Vec::new();
|
||||||
|
while !closed(){
|
||||||
|
if worker.is_finished(){ipc.0.abort();}
|
||||||
|
children.retain_mut(|child|matches!(child.try_wait(),Ok(None)));
|
||||||
|
while let Some(cmd)=take(){
|
||||||
|
if worker.is_finished(){toast("连接已断开");continue}
|
||||||
|
let parts:Vec<_>=cmd.split('\t').collect();
|
||||||
|
let session=match parts.as_slice(){
|
||||||
|
["create"]=>None,
|
||||||
|
["open",id]=>match id.parse::<u32>().ok().filter(|n|*n!=0){Some(id)=>Some(id),None=>continue},
|
||||||
|
["kill",id]=>{if let Ok(id)=id.parse::<u32>(){let _=jobs.send(id);}continue},
|
||||||
|
_=>continue,
|
||||||
|
};
|
||||||
|
match local_terminal_command(&exe,&path,session){
|
||||||
|
Some(mut command)=>match command.spawn(){Ok(child)=>children.push(child),Err(_)=>toast("无法启动本机终端程序")},
|
||||||
|
None=>toast("没有找到本机终端程序(可设置 TERMINAL 环境变量)"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||||
|
}
|
||||||
|
worker.abort();let _=worker.await;
|
||||||
|
drop(ipc);
|
||||||
|
// Closing the list drops all local IPC clients; reap emulator launchers off-thread.
|
||||||
|
std::thread::spawn(move||{for mut child in children{let _=child.wait();}});
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)] mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test] fn path_helpers_handle_both_separators() {
|
||||||
|
assert_eq!(base_name("/home/u/a.txt"),"a.txt");assert_eq!(base_name("C:\\Users\\x\\"),"x");assert_eq!(base_name("/"),"/");
|
||||||
|
assert_eq!(remote_join("/home/u","a"),"/home/u/a");assert_eq!(remote_join("/","a"),"/a");assert_eq!(remote_join("C:\\Users","a"),"C:\\Users\\a");
|
||||||
|
let dir=std::env::temp_dir().join(format!("uu-free-{}",super::super::wire::uuid()));std::fs::create_dir(&dir).unwrap();
|
||||||
|
std::fs::write(dir.join("a.txt"),b"").unwrap();
|
||||||
|
assert_eq!(free_local(&dir,"a.txt"),dir.join("a (1).txt"));assert_eq!(free_local(&dir,"b"),dir.join("b"));
|
||||||
|
std::fs::remove_file(dir.join("a.txt")).unwrap();std::fs::remove_dir(&dir).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,217 @@
|
|||||||
|
//! Launcher window (`uuremote-viewer ui`): QR sign-in, device list, and one child process per
|
||||||
|
//! remote session so each SDL window is isolated. Account data never reaches the logs.
|
||||||
|
use serde_json::Value;
|
||||||
|
use std::ffi::{CStr, CString};
|
||||||
|
use std::os::raw::{c_char, c_int};
|
||||||
|
use std::time::{Duration, Instant, SystemTime};
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
fn uu_viewer_ui_start() -> c_int;
|
||||||
|
fn uu_viewer_ui_set_devices(tsv: *const c_char);
|
||||||
|
fn uu_viewer_ui_set_login(signed_in: c_int, qr_png: *const c_char, status: *const c_char, user: *const c_char);
|
||||||
|
fn uu_viewer_ui_take_command(out: *mut c_char, len: usize) -> c_int;
|
||||||
|
fn uu_viewer_ui_closed() -> c_int;
|
||||||
|
fn uu_viewer_ui_confirm_replace() -> c_int;
|
||||||
|
fn uu_viewer_ui_toast(text: *const c_char);
|
||||||
|
}
|
||||||
|
fn toast(text: &str) { let t = c(text); unsafe { uu_viewer_ui_toast(t.as_ptr()) } }
|
||||||
|
|
||||||
|
/// Runs one session child; on failure shows its last error line (log lines start with '[').
|
||||||
|
fn watch_session(mut child: tokio::process::Child, mut cancel: tokio::sync::oneshot::Receiver<()>, success: Option<&'static str>) -> tokio::task::JoinHandle<()> {
|
||||||
|
use tokio::io::AsyncBufReadExt;
|
||||||
|
let stderr = child.stderr.take();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let mut last = String::new();
|
||||||
|
let mut lines = stderr.map(|e| tokio::io::BufReader::new(e).lines());
|
||||||
|
let status = loop {
|
||||||
|
tokio::select! {
|
||||||
|
_ = &mut cancel => {
|
||||||
|
if let Some(id)=child.id(){unsafe{libc::kill(id as i32,libc::SIGTERM);}}
|
||||||
|
if tokio::time::timeout(Duration::from_secs(6),child.wait()).await.is_err(){let _=child.kill().await;}return;
|
||||||
|
}
|
||||||
|
status = child.wait() => break status,
|
||||||
|
line = async { match lines.as_mut() { Some(l) => l.next_line().await, None => std::future::pending().await } } => match line {
|
||||||
|
Ok(Some(l)) => { if !l.starts_with('[') && !l.trim().is_empty() { last = l.chars().take(160).collect(); } }
|
||||||
|
_ => lines = None,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// wait() may win before the final stderr line is read.
|
||||||
|
let _ = tokio::time::timeout(Duration::from_millis(200), async {
|
||||||
|
if let Some(lines) = lines.as_mut() {
|
||||||
|
while let Ok(Some(l)) = lines.next_line().await {
|
||||||
|
if !l.starts_with('[') && !l.trim().is_empty() { last = l.chars().take(160).collect(); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).await;
|
||||||
|
if !status.is_ok_and(|s| s.success()) {
|
||||||
|
let reason = match last.as_str() {
|
||||||
|
"" => "连接已断开".to_owned(),
|
||||||
|
"CONTROL echo timed out" => "连接超时:被控端没有响应(官方主机互通尚未完成)".to_owned(),
|
||||||
|
"Remote session released" => "被控端结束了本次连接".to_owned(),
|
||||||
|
other => other.to_owned(),
|
||||||
|
};
|
||||||
|
toast(&format!("连接失败:{reason}"));
|
||||||
|
} else if let Some(message) = success { toast(message); }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Running { cancel:tokio::sync::oneshot::Sender<()>, task:tokio::task::JoinHandle<()>, input:Option<tokio::process::ChildStdin> }
|
||||||
|
impl Running { async fn stop(self){let _=self.cancel.send(());let _=self.task.await;} }
|
||||||
|
async fn replace(sessions:&mut std::collections::HashMap<String,Running>,id:&str)->bool{
|
||||||
|
if sessions.get(id).is_some_and(|s|!s.task.is_finished()) && unsafe{uu_viewer_ui_confirm_replace()}!=1{return false}
|
||||||
|
if let Some(old)=sessions.remove(id){old.stop().await;}true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn c(text: &str) -> CString { CString::new(text.replace('\0', "")).unwrap_or_default() }
|
||||||
|
fn set_login(signed_in: bool, qr: &str, status: &str, user: &str) {
|
||||||
|
let (q, s, u) = (c(qr), c(status), c(user));
|
||||||
|
unsafe { uu_viewer_ui_set_login(signed_in as c_int, q.as_ptr(), s.as_ptr(), u.as_ptr()) }
|
||||||
|
}
|
||||||
|
fn take() -> Option<String> {
|
||||||
|
let mut buf = [0 as c_char; 200];
|
||||||
|
(unsafe { uu_viewer_ui_take_command(buf.as_mut_ptr(), buf.len()) } != 0)
|
||||||
|
.then(|| unsafe { CStr::from_ptr(buf.as_ptr()) }.to_string_lossy().into_owned())
|
||||||
|
}
|
||||||
|
/// "id\tname\tplatform\tonline\tcurrent" lines; tabs and newlines in names are flattened.
|
||||||
|
pub fn device_rows(devices: &[Value], current: Option<&str>) -> String {
|
||||||
|
let clean = |s: &str| s.replace(['\t', '\n', '\r'], " ");
|
||||||
|
devices.iter().filter_map(|d| {
|
||||||
|
let id = d.get("device_id")?.as_str()?;
|
||||||
|
let name = d.get("alias").and_then(Value::as_str).unwrap_or(id);
|
||||||
|
let platform = d.get("platform").and_then(Value::as_i64).unwrap_or(0);
|
||||||
|
let online = d.get("status").and_then(Value::as_str) == Some("CONNECTED");
|
||||||
|
Some(format!("{}\t{}\t{}\t{}\t{}\n", clean(id), clean(name), platform, online as u8, (current == Some(id)) as u8))
|
||||||
|
}).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run(credentials: impl Fn() -> Result<crate::auth::Credentials, String>) -> Result<(), String> {
|
||||||
|
if unsafe { uu_viewer_ui_start() } != 0 { return Err("Cannot open the window (GTK3 and a graphical session are required)".into()); }
|
||||||
|
let exe = std::env::current_exe().map_err(|_| "Cannot locate executable")?;
|
||||||
|
let qr = std::env::temp_dir().join(format!("uu-viewer-qr-{}.png", std::process::id()));
|
||||||
|
let mut login: Option<tokio::process::Child> = None;
|
||||||
|
let mut qr_seen: Option<SystemTime> = None;
|
||||||
|
let mut next_refresh = Instant::now();
|
||||||
|
let mut sessions:std::collections::HashMap<String,Running>=Default::default();
|
||||||
|
let mut names: std::collections::HashMap<String, String> = Default::default();
|
||||||
|
let refresh = |creds: &crate::auth::Credentials| {
|
||||||
|
let api = super::api::Api::new(crate::auth::UUAuthClient::new(creds.clone()), crate::auth::BASE_URL);
|
||||||
|
async move { api.device_list().await }
|
||||||
|
};
|
||||||
|
loop {
|
||||||
|
if unsafe { uu_viewer_ui_closed() } != 0 { break; }
|
||||||
|
sessions.retain(|_,s| !s.task.is_finished());
|
||||||
|
if let Some(child) = login.as_mut() {
|
||||||
|
if let Ok(meta) = std::fs::metadata(&qr) {
|
||||||
|
if meta.modified().ok() != qr_seen { qr_seen = meta.modified().ok(); set_login(false, &qr.to_string_lossy(), "请用手机扫码并确认", ""); }
|
||||||
|
}
|
||||||
|
if let Ok(Some(_)) = child.try_wait() { login = None; let _ = std::fs::remove_file(&qr); next_refresh = Instant::now(); }
|
||||||
|
} else if Instant::now() >= next_refresh {
|
||||||
|
next_refresh = Instant::now() + Duration::from_secs(30);
|
||||||
|
match credentials() {
|
||||||
|
Ok(creds) => {
|
||||||
|
set_login(true, "", "", "已登录");
|
||||||
|
match refresh(&creds).await {
|
||||||
|
Ok(data) => {
|
||||||
|
let list = data.get("my_binded_devices").and_then(Value::as_array).cloned().unwrap_or_default();
|
||||||
|
let current = data.pointer("/current_device/device_id").and_then(Value::as_str);
|
||||||
|
names = list.iter().filter_map(|d| Some((d.get("device_id")?.as_str()?.to_owned(),
|
||||||
|
d.get("alias").and_then(Value::as_str).unwrap_or("远端设备").to_owned()))).collect();
|
||||||
|
let rows = c(&device_rows(&list, current)); unsafe { uu_viewer_ui_set_devices(rows.as_ptr()) }
|
||||||
|
}
|
||||||
|
Err(super::api::Error::AuthenticationExpired) => set_login(false, "", "登录已过期,请重新登录", ""),
|
||||||
|
Err(_) => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => set_login(false, "", "尚未登录", ""),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while let Some(cmd) = take() {
|
||||||
|
let parts: Vec<&str> = cmd.split('\t').collect();
|
||||||
|
match parts.as_slice() {
|
||||||
|
["quit"] => break,
|
||||||
|
["refresh"] => next_refresh = Instant::now(),
|
||||||
|
["logout"] => {
|
||||||
|
// Sign out locally: drop the account token, keep the device identity.
|
||||||
|
let path = crate::config::path();
|
||||||
|
if let Ok(mut v) = std::fs::read_to_string(&path).ok().and_then(|t| serde_json::from_str::<Value>(&t).ok()).ok_or(()) {
|
||||||
|
if let Some(o) = v.as_object_mut() { o.remove("token"); o.remove("csrf"); }
|
||||||
|
let _ = std::fs::write(&path, v.to_string());
|
||||||
|
}
|
||||||
|
for (_,s) in sessions.drain() { s.stop().await; }
|
||||||
|
unsafe { uu_viewer_ui_set_devices(c("").as_ptr()) }
|
||||||
|
set_login(false, "", "已退出登录", "");
|
||||||
|
next_refresh = Instant::now() + Duration::from_secs(3600);
|
||||||
|
}
|
||||||
|
["login"] if login.is_none() => {
|
||||||
|
let server = exe.with_file_name("uuremote-server");
|
||||||
|
login = tokio::process::Command::new(server).args(["login", "--no-gui"])
|
||||||
|
.env("UU_LOGIN_QR_PNG", &qr).stdin(std::process::Stdio::null())
|
||||||
|
.stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null())
|
||||||
|
.kill_on_drop(true).spawn().ok();
|
||||||
|
if login.is_none() { set_login(false, "", "找不到配套的 uuremote-server", ""); }
|
||||||
|
}
|
||||||
|
["connect", id, kind] if matches!(*kind, "desktop" | "watch") => {
|
||||||
|
if !replace(&mut sessions,id).await{continue}
|
||||||
|
// One process per session; "-" selects the built-in desktop connection shape.
|
||||||
|
let mut command=tokio::process::Command::new(&exe);
|
||||||
|
command.args(["connect", id, "false", "-", "echo", "legacy"]);
|
||||||
|
command.env("UU_VIEWER_DEVICE_NAME", super::device_label(names.get(*id).map(String::as_str)));
|
||||||
|
if *kind=="watch" {command.arg("--view-only");}
|
||||||
|
match command.env("UU_VIEWER_COMMANDS","1").stdin(std::process::Stdio::piped())
|
||||||
|
.stderr(std::process::Stdio::piped()).kill_on_drop(true).spawn() {
|
||||||
|
Ok(mut child) => { let input=child.stdin.take();let (cancel,rx)=tokio::sync::oneshot::channel();let task=watch_session(child,rx,None);sessions.insert((*id).into(),Running{cancel,task,input}); }
|
||||||
|
Err(_) => toast("连接失败:无法启动会话进程"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
["assist", id, code] => {
|
||||||
|
match super::assist::command(&exe, id).spawn() {
|
||||||
|
Ok(mut child) => match super::assist::send_code(&mut child, code).await {
|
||||||
|
Ok(()) => { let (cancel,rx)=tokio::sync::oneshot::channel();let task=watch_session(child,rx,None);sessions.insert(format!("assist:{id}"),Running{cancel,task,input:None}); }
|
||||||
|
Err(reason) => toast(&format!("连接失败:{reason}")),
|
||||||
|
},
|
||||||
|
Err(_) => toast("连接失败:无法启动协助会话进程"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
["connect", id, kind @ ("file" | "portmap" | "terminal")] => {
|
||||||
|
if *kind=="file" {if let Some(s)=sessions.get_mut(*id){if let Some(input)=s.input.as_mut(){
|
||||||
|
use tokio::io::AsyncWriteExt;
|
||||||
|
if input.write_all(b"files\n").await.is_err(){toast("桌面会话命令发送失败,请重试")}continue
|
||||||
|
}}}
|
||||||
|
if !replace(&mut sessions,id).await{continue}
|
||||||
|
let name = names.get(*id).cloned().unwrap_or_else(|| "远端设备".into());
|
||||||
|
let tool = if *kind == "file" { "files-ui" } else if *kind == "terminal" { "terminal-ui" } else { "portmap-ui" };
|
||||||
|
match tokio::process::Command::new(&exe).args([tool, id, &name])
|
||||||
|
.stderr(std::process::Stdio::piped()).kill_on_drop(true).spawn() {
|
||||||
|
Ok(child) => { let (cancel,rx)=tokio::sync::oneshot::channel();let task=watch_session(child,rx,None);sessions.insert((*id).into(),Running{cancel,task,input:None}); }
|
||||||
|
Err(_) => toast("连接失败:无法启动工具进程"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
["connect", id, kind @ ("reboot" | "shutdown")] => {
|
||||||
|
if !replace(&mut sessions,id).await{continue}
|
||||||
|
match tokio::process::Command::new(&exe).args(["power", id, kind])
|
||||||
|
.stdout(std::process::Stdio::null()).stderr(std::process::Stdio::piped()).kill_on_drop(true).spawn() {
|
||||||
|
Ok(child) => { let (cancel,rx)=tokio::sync::oneshot::channel();let task=watch_session(child,rx,Some("电源指令已发送,未确认远端执行结果"));sessions.insert((*id).into(),Running{cancel,task,input:None}); }
|
||||||
|
Err(_) => toast("电源指令失败:无法启动会话进程"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
["connect", _, kind] => log::info!("Viewer UI: {kind} is not supported yet"),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tokio::time::sleep(Duration::from_millis(200)).await;
|
||||||
|
}
|
||||||
|
for (_,s) in sessions {s.stop().await;}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)] mod tests {
|
||||||
|
#[test] fn device_rows_flatten_names_and_mark_online() {
|
||||||
|
let rows = super::device_rows(&[
|
||||||
|
serde_json::json!({"device_id":"d1","alias":"a\tb","platform":4,"status":"CONNECTED"}),
|
||||||
|
serde_json::json!({"device_id":"d2","platform":1,"status":"DISCONNECTED"}),
|
||||||
|
serde_json::json!({"alias":"no id"}),
|
||||||
|
], Some("d2"));
|
||||||
|
assert_eq!(rows, "d1\ta b\t4\t1\t0\nd2\td2\t1\t0\t1\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,183 @@
|
|||||||
|
//! Shapes from viewer-connect-shape-20261016.md; values describe caller capabilities.
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
use crate::protocol::{decode_wire, WireValue};
|
||||||
|
|
||||||
|
pub const CHANNELS: [&str; 5] = ["CONTROL_DATA_CHANNEL", "TEXT_DATA_CHANNEL", "STREAMER_DATA_CHANNEL", "FILE_DATA_CHANNEL", "BINARY_DATA_CHANNEL"];
|
||||||
|
pub fn uuid() -> String {
|
||||||
|
let mut b: [u8;16] = rand::random(); b[6] = (b[6] & 15) | 64; b[8] = (b[8] & 63) | 128;
|
||||||
|
let s = hex::encode(b);
|
||||||
|
format!("{}-{}-{}-{}-{}", &s[..8], &s[8..12], &s[12..16], &s[16..20], &s[20..])
|
||||||
|
}
|
||||||
|
pub fn varint(mut n: u64, out: &mut Vec<u8>) { while n >= 128 { out.push(n as u8 | 128); n >>= 7; } out.push(n as u8); }
|
||||||
|
pub fn int(field: u32, value: u64, out: &mut Vec<u8>) { varint(u64::from(field) << 3,out); varint(value,out); }
|
||||||
|
pub fn bytes(field: u32, value: &[u8], out: &mut Vec<u8>) { varint((u64::from(field)<<3)|2,out); varint(value.len() as u64,out); out.extend(value); }
|
||||||
|
pub fn envelope(seq:u64, kind:u32, payload:&[u8]) -> Vec<u8> {
|
||||||
|
let mut out=Vec::new(); int(1,seq,&mut out); bytes(kind,payload,&mut out); out
|
||||||
|
}
|
||||||
|
#[derive(Clone, Deserialize)]
|
||||||
|
pub struct Decoder { pub fps:u32, pub codec:u32, pub width:u32, pub height:u32, pub chroma:u32 }
|
||||||
|
#[derive(Clone, Deserialize, Serialize)]
|
||||||
|
pub struct Display { pub id:u32, pub fps:u32, pub r#type:u32, pub hdr:u32 }
|
||||||
|
#[derive(Clone, Deserialize, Serialize)]
|
||||||
|
pub struct Codec { pub video_codec:u32, pub chroma_sampling:u32, pub codec_impl:u32, pub bit_depth:u32, pub width:u32, pub height:u32 }
|
||||||
|
#[derive(Clone, Deserialize)]
|
||||||
|
pub struct Shape {
|
||||||
|
pub capture_type:u32,
|
||||||
|
pub platform:u32,
|
||||||
|
pub client_version:String,
|
||||||
|
pub width:u32,
|
||||||
|
pub height:u32,
|
||||||
|
pub fps:u32,
|
||||||
|
pub chroma:u32,
|
||||||
|
pub decoders:Vec<Decoder>,
|
||||||
|
pub display_modes:Vec<[u32;3]>,
|
||||||
|
pub displays:Vec<Display>,
|
||||||
|
pub codecs:Vec<Codec>,
|
||||||
|
/// FeatureFlag {field: version} pairs (ConnectOptions 11). Empty by default; official hosts may
|
||||||
|
/// require it (live: official Mac/Windows hosts did not answer an empty flag set).
|
||||||
|
#[serde(default)]
|
||||||
|
pub feature_flag:Vec<[u32;2]>,
|
||||||
|
}
|
||||||
|
impl Shape {
|
||||||
|
pub fn validate(&self) -> Result<(),String> {
|
||||||
|
if ![1,5,8,9].contains(&self.capture_type) || ![3,4].contains(&self.platform)
|
||||||
|
|| self.width==0 || self.height==0 || self.fps==0 || ![1,3].contains(&self.chroma) {
|
||||||
|
return Err("Invalid explicit viewer shape".into());
|
||||||
|
}
|
||||||
|
if self.decoders.iter().any(|d| ![1,2].contains(&d.codec) || ![1,3].contains(&d.chroma) || d.width==0 || d.height==0 || d.fps==0) {
|
||||||
|
return Err("Invalid decoder capability".into());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub fn validate_session(&self)->Result<(),String> {
|
||||||
|
self.validate()?;
|
||||||
|
// H264/420 receive only. streamer_data video_codec_capability is sent by the official Mac
|
||||||
|
// viewer (8 entries observed live), so H264/420 entries are allowed there too.
|
||||||
|
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)
|
||||||
|
|| (self.capture_type==1 && (self.chroma!=1 || self.decoders.is_empty())) {
|
||||||
|
return Err("Sessions require H264/420 decoder and codec capabilities".into());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub fn connect_options(&self, device:&str) -> Result<Vec<u8>,String> {
|
||||||
|
self.validate()?;
|
||||||
|
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,5].contains(&self.capture_type) {
|
||||||
|
let mut params=Vec::new(); int(1,2,&mut params); int(2,4,&mut params); int(4,2,&mut params);
|
||||||
|
let mut size=Vec::new(); int(1,self.width.into(),&mut size); int(2,self.height.into(),&mut size);
|
||||||
|
bytes(5,&size,&mut params); bytes(6,&[],&mut params);
|
||||||
|
if self.capture_type==1 { int(7,self.chroma.into(),&mut params); int(9,1,&mut params); int(11,self.fps.into(),&mut params); }
|
||||||
|
bytes(3,¶ms,&mut out);
|
||||||
|
for d in &self.decoders {
|
||||||
|
let mut cap=Vec::new();
|
||||||
|
for (f,v) in [(1,d.fps),(2,d.codec),(3,d.width),(4,d.height)] { int(f,v.into(),&mut cap); }
|
||||||
|
if self.capture_type==1 { int(5,d.chroma.into(),&mut cap); }
|
||||||
|
bytes(4,&cap,&mut out);
|
||||||
|
}
|
||||||
|
for mode in &self.display_modes {
|
||||||
|
let mut value=Vec::new(); for (f,v) in mode.iter().enumerate() { int(f as u32+1,(*v).into(),&mut value); }
|
||||||
|
bytes(6,&value,&mut out);
|
||||||
|
}
|
||||||
|
bytes(7,&size,&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 {
|
||||||
|
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);}
|
||||||
|
bytes(11,&flags,&mut out);
|
||||||
|
}
|
||||||
|
if self.capture_type!=5 { bytes(12,self.client_version.as_bytes(),&mut out); }
|
||||||
|
Ok(out)
|
||||||
|
}
|
||||||
|
pub fn control(&self, client:&str, id:&str, ice:&str) -> Result<Value,String> {
|
||||||
|
self.validate()?;
|
||||||
|
let streamer=json!({"control_id":id,"device_capability":{"display_info":self.displays,"ice_id":"","video_codec_capability":self.codecs}});
|
||||||
|
// Official Control::SerializeSioMsg (libstreamer 0x107bf8) sends only these three keys. A
|
||||||
|
// viewer-chosen ice_id reaches the host via be-controlled while the ack assigns another, and the
|
||||||
|
// official host drops the offer ("drop the soac event ... not in current ice connections").
|
||||||
|
let _=(client,ice);
|
||||||
|
Ok(json!({"app_control_id":id,"app_data":{"_placeholder":true,"num":0},"streamer_data":streamer.to_string()}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Existing viewer FeatureFlag pairs; clipboard uses the host text protocol version (3).
|
||||||
|
/// Session encoders omit clipboard when its local backend is disabled or unavailable.
|
||||||
|
pub const VIEWER_FEATURES:[[u32;2];8]=[[1,6],[2,2],[3,1],[4,2],[6,2],[7,2],[8,3],[9,1]];
|
||||||
|
pub fn feature_flag(flags:&[[u32;2]]) -> Vec<u8> { let mut out=Vec::new(); for [f,v] in flags { int(*f,(*v).into(),&mut out); } out }
|
||||||
|
pub fn echo_request(seq:u64) -> Vec<u8> {echo_request_with_clipboard(seq,false)}
|
||||||
|
fn session_features(clipboard:bool)->Vec<u8>{
|
||||||
|
let mut flags:Vec<_>=VIEWER_FEATURES.iter().copied().filter(|p|p[0]!=8).collect();
|
||||||
|
if clipboard{flags.push([8,3]);}feature_flag(&flags)
|
||||||
|
}
|
||||||
|
pub fn echo_request_with_clipboard(seq:u64,clipboard:bool)->Vec<u8>{
|
||||||
|
let mut action=Vec::new();int(1,0,&mut action);bytes(4,&session_features(clipboard),&mut action);envelope(seq,3,&action)
|
||||||
|
}
|
||||||
|
pub fn echo_clipboard_supported(data:&[u8])->bool{
|
||||||
|
let Ok(fields)=decode_wire(data) else{return false};
|
||||||
|
fields.iter().any(|f|match (f.number,&f.value){
|
||||||
|
(3,WireValue::Bytes(action))=>decode_wire(action).is_ok_and(|fields|fields.iter().any(|f|match (f.number,&f.value){
|
||||||
|
(4,WireValue::Bytes(flags))=>decode_wire(flags).is_ok_and(|flags|flags.iter().any(|f|matches!((f.number,&f.value),(8,WireValue::Varint(3..))))),_=>false,
|
||||||
|
})),_=>false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/// Official hosts also send an echo request (SimpleAction action 0) and repeat it until answered.
|
||||||
|
/// Returns the request's Message seq when `data` is such a request.
|
||||||
|
pub fn echo_request_seq(data:&[u8]) -> Option<u64> {
|
||||||
|
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})?;
|
||||||
|
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),
|
||||||
|
_=>None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/// Answer like the host side does: action 1, args {"seq": request seq}, our FeatureFlag.
|
||||||
|
pub fn echo_response(seq:u64,request_seq:u64)->Vec<u8>{echo_response_with_clipboard(seq,request_seq,false)}
|
||||||
|
pub fn echo_response_with_clipboard(seq:u64, request_seq:u64,clipboard:bool) -> Vec<u8> {
|
||||||
|
let mut action=Vec::new(); int(1,1,&mut action);
|
||||||
|
bytes(2,format!("{{\"seq\":{request_seq}}}").as_bytes(),&mut action);
|
||||||
|
bytes(4,&session_features(clipboard),&mut action); envelope(seq,3,&action)
|
||||||
|
}
|
||||||
|
pub fn echo_matches(data:&[u8], seq:u64) -> bool {
|
||||||
|
let Ok(fields)=decode_wire(data) else {return false};
|
||||||
|
fields.iter().any(|f| match (f.number,&f.value) {
|
||||||
|
(3,WireValue::Bytes(action))=> {
|
||||||
|
let Ok(fields)=decode_wire(action) else {return false};
|
||||||
|
let response=fields.iter().any(|f|f.number==1 && f.value==WireValue::Varint(1));
|
||||||
|
response && fields.iter().any(|f|match (f.number,&f.value) {
|
||||||
|
(2,WireValue::Bytes(args))=>serde_json::from_slice::<Value>(args).ok().and_then(|v|v.get("seq").and_then(Value::as_u64))==Some(seq), _=>false,
|
||||||
|
})
|
||||||
|
}, _=>false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
pub fn shape()->Shape { serde_json::from_str(include_str!("../../tests/fixtures/viewer/shape.json")).unwrap() }
|
||||||
|
#[test]
|
||||||
|
fn session_shape_uses_observed_field_presence_and_nested_values() {
|
||||||
|
let mut s=shape();
|
||||||
|
for capture in [1,5,8,9] {
|
||||||
|
s.capture_type=capture;
|
||||||
|
let b=s.connect_options("synthetic-device").unwrap(); let f=decode_wire(&b).unwrap();
|
||||||
|
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]};
|
||||||
|
assert_eq!(numbers,expected);
|
||||||
|
for f in f.iter().filter(|f|f.number==4) {
|
||||||
|
let WireValue::Bytes(b)=f.value else {panic!()}; let cap=decode_wire(b).unwrap();
|
||||||
|
assert_eq!(cap.len(),if capture==1 {5}else{4});
|
||||||
|
}
|
||||||
|
if [1,8].contains(&capture) { assert!(f.iter().any(|f| f.number==2 && f.value==WireValue::Varint(u64::MAX))); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn streamer_identity_and_echo_correlation_are_separate_from_socket_ack() {
|
||||||
|
let s=shape();let id=uuid(); let v=s.control("synthetic-client",&id,"synthetic-ice").unwrap();
|
||||||
|
let streamer:Value=serde_json::from_str(v["streamer_data"].as_str().unwrap()).unwrap();
|
||||||
|
assert_eq!(streamer["control_id"],v["app_control_id"]); assert_eq!(id.len(),36); assert_ne!(id,uuid()); assert_eq!(&id[14..15],"4");
|
||||||
|
let mut action=Vec::new();int(1,1,&mut action);bytes(2,br#"{"seq":7}"#,&mut action);
|
||||||
|
let reply=envelope(99,3,&action); assert!(echo_matches(&reply,7)); assert!(!echo_matches(&reply,99));
|
||||||
|
assert!(!echo_matches(&echo_request(7),7)); assert!(!echo_matches(&[255],7));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loaded 100 of 112 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user