mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-16 08:50:40 +01:00
Compare commits
No files matched your search
@@ -133,7 +133,7 @@ viewer does not reconnect them automatically.
|
||||
| `UU_LOGIN_WINDOW=0` | don't auto-open the login window |
|
||||
| `UU_ENGINE_DIR` | directory holding `libuu-wayland.so` / `libuu-portal.so` (`--features engine-plugins`, used by release packages: engines are dlopen'ed only on Wayland sessions; also searched next to the binary, `../lib/uu-remote-linux`, `~/.local/lib/uu-remote-linux`, `/usr/lib/uu-remote-linux`) |
|
||||
| `UU_WOL_HELPER=1` | experimental: report LAN info (`api-ipv4…/api/v1/device/wake_on_lan/info` {mac, ip, subnet_mask}) after sign-in and answer `wake_on_lan` pushes (`target_device` {mac, ip, subnet_mask}) with a magic packet, like the official Windows host (field names from its log strings; not verified end to end). `wake_on_lan` push key names are always logged |
|
||||
| `UU_USE_PROXY=1` | honor HTTP/SOCKS proxy env vars; cloud API clients bypass env proxies by default |
|
||||
| `UU_USE_PROXY=1` | honor HTTP/HTTPS proxy env vars for REST and WebSocket signaling (not ICE/media); clients bypass env proxies by default |
|
||||
| `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. `config.json` is parsed once
|
||||
|
||||
Generated
+3
-1
@@ -2741,7 +2741,7 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "uu-remote-linux"
|
||||
version = "0.2.29"
|
||||
version = "0.2.36"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base64 0.22.1",
|
||||
@@ -2758,12 +2758,14 @@ dependencies = [
|
||||
"nix 0.29.0",
|
||||
"qrcode",
|
||||
"rand 0.8.8",
|
||||
"rcgen",
|
||||
"reqwest",
|
||||
"sdl2",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2",
|
||||
"tokio",
|
||||
"tokio-rustls",
|
||||
"tokio-tungstenite",
|
||||
"webrtc",
|
||||
"x11rb",
|
||||
|
||||
+6
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "uu-remote-linux"
|
||||
version = "0.2.29"
|
||||
version = "0.2.36"
|
||||
edition = "2021"
|
||||
default-run = "uuremote-server"
|
||||
authors = ["uu-remote-linux contributors"]
|
||||
@@ -60,6 +60,11 @@ x11rb = { version = "0.13", features = ["damage", "shm", "allow-unsafe-code"], o
|
||||
|
||||
sdl2 = { version = "0.37", optional = true, features = ["use-pkgconfig"] }
|
||||
|
||||
[dev-dependencies]
|
||||
# Synthetic loopback WSS certificates only; both crates already occur in Cargo.lock.
|
||||
rcgen = "0.11.3"
|
||||
tokio-rustls = { version = "0.26.6", default-features = false, features = ["ring", "tls12"] }
|
||||
|
||||
[build-dependencies]
|
||||
cc = "1.0"
|
||||
|
||||
|
||||
+14
-2
@@ -45,7 +45,7 @@
|
||||
| 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) |
|
||||
| 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) |
|
||||
| Clipboard | Two-way text sync |
|
||||
| Clipboard | Two-way text sync; Wayland mobile IME text inputs use clipboard paste fallback (see below) |
|
||||
| Quality / frame rate / resolution | Auto, Original 20M, Ultra 8M, HD 2M, custom; 30/60/90/144 fps; on X11 a resolution choice changes the desktop resolution itself like the official Windows host (xrandr, adding the mode when missing; restored for "original" or on disconnect), `UU_DESKTOP_RESIZE=0` scales in the encoder instead; Wayland scales in the encoder |
|
||||
| Sound | System audio captured from PulseAudio / PipeWire and sent as Opus |
|
||||
| File transfer | Upload, download (including folders), create folder, rename, delete |
|
||||
@@ -84,6 +84,8 @@ plugins, permissions and encoders only, no account data, tokens or addresses) an
|
||||
Wayland limits: captures one screen by default; Hyprland can enable monitor switching as described below. Resolution is fixed. 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"
|
||||
permission; the permission is remembered once granted.
|
||||
|
||||
**Mobile IME typing and clipboard on Wayland**: Mobile IME text (`text_input`) on Wayland is typed through spare keycodes on wlroots compositors. On Portal (GNOME / KDE) or compositors where keycodes are exhausted, it automatically falls back to typing via "system clipboard write + `Shift+Insert` (or `UU_WAYLAND_PASTE_KEY=ctrl_v`)". **Note**: This fallback replaces the host's existing clipboard contents and may echo back to the viewer's clipboard via two-way sync; set `UU_WAYLAND_TEXT=0` to completely disable Wayland mobile text input and clipboard paste fallback.
|
||||
|
||||
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.
|
||||
@@ -210,6 +212,10 @@ Run inside your graphical session:
|
||||
Scan the code with the **NetEase UU Remote mobile app** and confirm. Credentials are stored only on this
|
||||
machine, in `~/.config/uuremote/config.json` (mode 0600).
|
||||
|
||||
A failed status query or the 60-second request timeout does not prove that the QR expired or that the phone confirmed login. The official-client refresh policy is unchanged: after a query error, click Refresh QR (Enter in a terminal; refresh after 60 seconds with neither a window nor a terminal). QR request diagnostics contain only the request phase, error category, elapsed time, HTTP/API status and last known scan state—not tickets, URLs or response content. A `connect` category alone does not identify a DNS, TLS or proxy cause.
|
||||
|
||||
The same file holds this machine's device identity (`device_id`, `client_id`, `system_id`), which reinstalls and new QR logins keep. **Do not copy `config.json` between machines or clone a second host from a snapshot/disk that includes the home directory**: two host instances with one identity, on the same or different machines, kick each other out of the room. The log then shows `Cloud room leave without peer identifier` every few seconds with reconnects, and viewers disconnect right after connecting. On a cloned or copied machine, stop the service, remove those three keys plus `token`, `csrf` and `user_id`, and run `uuremote-server login` to register a new device. A leftover instance on the same machine (for example an old `~/.local/bin/uuremote-server` started by hand) has the same effect; `uuremote-server doctor` warns when more than one host instance is running.
|
||||
|
||||
### 4. Run as a user service
|
||||
|
||||
`~/.config/systemd/user/uuremote-native.service`:
|
||||
@@ -348,6 +354,7 @@ Common environment variables:
|
||||
| `UU_TERMINAL_SHORTCUTS=0` | Disable the Ctrl+V paste rewrite in terminal windows |
|
||||
| `UU_VIDEO_ENCODER` | `cpu`: libx264; `vaapi`: opt-in Intel/AMD; `nvenc`: force NVENC; unset or `auto`: a background probe (one synthetic IDR per candidate, 3 s each). With an NVIDIA driver the order is NVENC then CPU; without one the only candidate is CPU and the probe is skipped. A session that starts before the probe finishes uses the previous rule (NVENC if NVIDIA is present, else CPU). On a self-built `--features inproc-av` binary, a process-only failure of NVENC when the in-process engine would try it first on X11 is logged `native_unprobed` and keeps the previous NVENC choice (Wayland, portal, and KMS fall back to CPU). The skipped CPU-only probe is logged `not probed (only candidate)`. A successful probe and that CPU-only skip stay cached while NVIDIA presence matches the report. A later desktop connection whose presence differs starts one background re-probe and still uses the cached encoder until the new result is stored. A hardware-candidate failure (NVENC failed, CPU chosen) is kept for 10 minutes; the next desktop connection after that starts the same re-probe and still uses the cached encoder until the new result is stored. Explicit `cpu` / `vaapi` / `nvenc` does not start the probe. Another failure restarts the 10 minutes. `native_unprobed` is not a temporary failure. A non-`auto` `video_encoder` in `host_settings.json` wins. VAAPI stays out of auto until #33 |
|
||||
| `UU_VAAPI_BUFFER_MS` | VAAPI VBV buffer in ms (10-1000, VAAPI only); raise it when keyframes are squeezed into a blur at high resolution |
|
||||
| `UU_NVENC_BUFFER_MS` | NVENC VBV buffer in ms (10-1000, NVENC only, including the in-process engine); the default is 100-500 kbit, set `1000` when high-resolution keyframes blur and flicker once per GOP (#103) |
|
||||
| `UU_FFMPEG_ROOT` | Use FFmpeg from this prefix |
|
||||
| `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) |
|
||||
@@ -355,7 +362,12 @@ Common environment variables:
|
||||
| `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_WOL_HELPER=1` | Experimental: act as a remote-boot helper like the official Windows host — report this host's LAN info after sign-in and send a magic packet to the LAN when the cloud asks to wake a PC (not verified end to end) |
|
||||
| `UU_USE_PROXY=1` | Honor HTTP/SOCKS proxy environment variables; by default proxies are ignored and NetEase servers are reached directly |
|
||||
| `UU_USE_PROXY=1` | Share environment HTTP/HTTPS proxy policy between REST and host/viewer WebSocket signaling; proxies are ignored by default |
|
||||
|
||||
|
||||
`UU_USE_PROXY=1` uses only proxy settings already available to the current process. `HTTP_PROXY` / `HTTPS_PROXY` / `ALL_PROXY` (and lowercase forms) and `NO_PROXY` follow reqwest's standard selection, with uppercase taking precedence. Signaling rejects invalid/unsupported values in the selected variables (HTTP/HTTPS only, not SOCKS), proxy URLs with paths/queries/fragments, and CGI environments that would ignore proxies; this validation is intentionally stricter than silently ignoring bad values. No matching proxy or a `NO_PROXY` match still means direct access; a selected proxy failing does not trigger direct fallback. Certificate verification remains enabled and redirects are not followed, so room authentication is not forwarded to a redirect target.
|
||||
|
||||
**This covers HTTP/WebSocket compatibility only: it does not proxy ICE/WebRTC media, add TURN TCP/TLS, or create network access.** Login, signaling and media each need an allowed network path. Connector errors expose only fixed categories/HTTP status; do not enable dependency TRACE logs that may record URLs or authentication information.
|
||||
|
||||
Host settings such as "lock after remote session" and "mute host" are stored in
|
||||
`~/.config/uuremote/host_settings.json`. The same file stores `video_encoder`
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
| 鼠标 / 键盘 | 绝对/相对移动、点击、滚轮、按键;修复了网络延迟导致的按键连发;Wayland 下可用 uinput 虚拟键鼠(不依赖键盘布局,见下文) |
|
||||
| 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) |
|
||||
| 剪贴板 | 双向文本同步 |
|
||||
| 剪贴板 | 双向文本同步;Wayland 手机输入法输入通过剪贴板兜底粘贴,见下文 |
|
||||
| 画质 / 帧率 / 分辨率 | 自动、原画 20M、超清 8M、高清 2M、自定义;30/60/90/144 帧;X11 下切换分辨率会像官方 Windows 被控端一样真正修改桌面分辨率(xrandr,缺少该模式时自动添加;选「原始」或断开后恢复),`UU_DESKTOP_RESIZE=0` 改为仅由编码器缩放;Wayland 仍由编码器缩放 |
|
||||
| 声音 | 采集系统声音(PulseAudio / PipeWire),Opus 编码传到控制端 |
|
||||
| 文件传输 | 上传、下载(含文件夹)、新建文件夹、重命名、删除 |
|
||||
@@ -75,6 +75,8 @@ GNOME Wayland 的 Portal/EIS 路径已在 Ubuntu 26.04、GNOME 50、单屏 4K/20
|
||||
|
||||
Wayland 下的限制:默认采集单屏,Hyprland 可按上文启用切屏;分辨率固定;Hyprland 原生后端默认使用双缓冲流水线 shm screencopy,控制端帧率设置会同步到采集节拍和编码器,但实际帧率仍受合成器、渲染和读回速度限制(旧串行路径在 2560x1600 曾测得约 16~22 帧;流水线只在虚拟机里测量过,不代表实机保证);portal 后端暂不支持动态分辨率;GNOME 的屏幕唤醒与锁屏恢复见下;剪贴板只在控制端的远控窗口处于前台时同步。
|
||||
|
||||
**Wayland 手机输入法打字与剪贴板**:Wayland 下手机端输入法提交的文字(`text_input`)优先通过虚拟键盘空闲键码上屏;在 Portal(GNOME / KDE)或键码耗尽的环境下,自动通过「写系统剪贴板 + 触发 `Shift+Insert`(或 `UU_WAYLAND_PASTE_KEY=ctrl_v`)」兜底粘贴。**注意**:此兜底过程会覆盖被控端原有的剪贴板内容,且会被双向同步回传到控制端剪贴板;设置 `UU_WAYLAND_TEXT=0` 可完全关闭 Wayland 文字输入及该粘贴兜底。
|
||||
|
||||
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 会话中的画面、键鼠和剪贴板。首次授权或权限被撤销时仍需本机确认。物理或逻辑分辨率变化后需要重连。
|
||||
@@ -193,6 +195,10 @@ cargo build --release --features wayland,portal # + GNOME/KDE Wayland
|
||||
用手机上的 **网易 UU 远程 App** 扫码并确认即可。登录信息只保存在本机的
|
||||
`~/.config/uuremote/config.json`(权限 0600)里。
|
||||
|
||||
登录状态查询失败或达到 60 秒请求超时不代表二维码已过期,也不代表手机已经确认。程序保留官方客户端的刷新策略:查询出错后点击「刷新二维码」(终端按回车;无窗口且无终端时 60 秒后刷新)。二维码请求诊断只包含请求阶段、错误类别、耗时、HTTP/API 状态及最后已知扫码状态,不输出票据、URL 或响应内容;`connect` 类别本身不能确定是 DNS、TLS 还是代理问题。
|
||||
|
||||
这个文件里还有本机的设备身份(`device_id`、`client_id`、`system_id`),重装软件和重新扫码登录都会沿用它。**不要在机器之间拷贝 `config.json`,也不要从带着家目录的快照/数据盘克隆出第二台主机**:同一身份的两个主机实例(不论在同一台还是不同机器上)会互相把对方踢出房间,日志表现为每隔几秒出现 `Cloud room leave without peer identifier` 并反复重连,控制端一连就断。克隆或拷贝出来的机器先停掉服务,删掉配置里这三个键和 `token`、`csrf`、`user_id`,再运行 `uuremote-server login` 注册为新设备。同一台机器上残留的旧实例(例如以前手动启动的 `~/.local/bin/uuremote-server`)也会这样,`uuremote-server doctor` 发现多个主机实例时会提示。
|
||||
|
||||
### 4. 作为用户服务常驻
|
||||
|
||||
`~/.config/systemd/user/uuremote-native.service`:
|
||||
@@ -316,6 +322,7 @@ systemctl --user restart uuremote-native.service
|
||||
| `UU_TERMINAL_SHORTCUTS=0` | 关闭终端窗口里的 Ctrl+V 粘贴转换 |
|
||||
| `UU_VIDEO_ENCODER` | `cpu` 强制 CPU;`vaapi` 显式启用 Intel/AMD 硬编;`nvenc` 强制 NVENC;未设置或 `auto` 时后台探测(每个候选编码一帧,限时 3 秒)。有 NVIDIA 驱动时顺序是 NVENC → CPU;没有 NVIDIA 时只有 CPU,不探测。探测未完成就建流则沿用旧规则(有 NVIDIA 用 NVENC,否则 CPU)。自行编译的 `--features inproc-av` 上,仅 X11 会先走进程内引擎:外部 FFmpeg 探测失败时记为 `native_unprobed`,沿用旧规则(NVENC);Wayland、portal、KMS 回退 CPU。只有 CPU 时跳过探测,记为 `not probed (only candidate)`。探测成功和这种只含 CPU 的跳过会缓存到 NVIDIA 是否存在发生变化为止(`/proc/driver/nvidia/version` 或 `/dev/nvidia0`)。之后的桌面连接在后台再探测一次(不挡住这一路,仍用缓存的编码器)。NVENC 探测失败、选用 CPU 时只缓存 10 分钟;到期后下一次桌面连接同样在后台再探测一次,再次失败则重新计 10 分钟。显式 `cpu` / `vaapi` / `nvenc` 不启动探测。`native_unprobed` 不算失败缓存。优先级低于 `host_settings.json` 里非 `auto` 的 `video_encoder`。VAAPI 要等 #33 解决后才会进入 auto |
|
||||
| `UU_VAAPI_BUFFER_MS` | VAAPI 的 VBV 缓冲(10–1000 ms,仅 `UU_VIDEO_ENCODER=vaapi` 时生效);高分辨率下关键帧被压糊时用它换稳定画质 |
|
||||
| `UU_NVENC_BUFFER_MS` | NVENC 的 VBV 缓冲(10–1000 ms,仅 NVENC 生效,含进程内引擎);默认 100–500 kbit,1440p 等高分辨率下关键帧被压糊、按 GOP 周期闪烁时设 `1000`(#103) |
|
||||
| `UU_FFMPEG_ROOT` | 使用指定目录下的 FFmpeg |
|
||||
| `UU_AUDIO=0` | 关闭声音转发;`UU_AUDIO_SOURCE` 指定 PulseAudio 采集源 |
|
||||
| `UU_LOGIN_WINDOW=0` | 凭证缺失时不自动弹出登录窗口 |
|
||||
@@ -323,7 +330,12 @@ systemctl --user restart uuremote-native.service
|
||||
| `UU_ASSIST_ALLOW=1` | 启动时就允许他人远程协助(覆盖窗口里保存的开关状态) |
|
||||
| `UU_PORT_MAPPING=0` | 关闭端口映射(对应官方被控端的开关) |
|
||||
| `UU_WOL_HELPER=1` | 实验性:让这台主机像官方 Windows 被控端一样充当「远程开机」辅助设备——登录后向网易上报本机局域网信息,收到云端唤醒请求时向同一局域网发送唤醒包(未经端到端验证) |
|
||||
| `UU_USE_PROXY=1` | 使用环境变量里的 HTTP/SOCKS 代理;默认忽略代理、直连网易服务器 |
|
||||
| `UU_USE_PROXY=1` | REST API 与 host/viewer WebSocket 信令共用环境 HTTP/HTTPS 代理策略;默认忽略代理、直连 |
|
||||
|
||||
|
||||
`UU_USE_PROXY=1` 仅使用当前进程已有的代理配置;`HTTP_PROXY` / `HTTPS_PROXY` / `ALL_PROXY`(及小写形式)和 `NO_PROXY` 按 reqwest 的标准规则选择,大写优先。信令拒绝选定变量中的无效/不支持的代理 URL(仅 HTTP/HTTPS;不支持 SOCKS)、带路径/查询/片段的代理 URL,以及会忽略代理的 CGI 环境;此校验比 reqwest 默默忽略坏值更严格。未配置匹配代理或命中 `NO_PROXY` 时仍正常直连,选中代理后连接失败不会改为直连。信令保留 TLS 证书校验,不跟随重定向,也不转发认证头到重定向目标。
|
||||
|
||||
**这只解决 HTTP 与 WebSocket 的代理兼容性,不代理 ICE/WebRTC 媒体,也不提供 TURN TCP/TLS 或任何新的网络出口。** 登录、信令和媒体必须分别有允许使用的网络路径。连接器只返回固定错误类别/HTTP 状态;不要为诊断开启会记录 URL/认证信息的依赖库 TRACE 日志。
|
||||
|
||||
「远程结束后自动锁屏」「静音运行」等设置保存在 `~/.config/uuremote/host_settings.json`。同一文件里的 `video_encoder`(`auto`/`cpu`/`vaapi`/`nvenc`)和 `video_codec`(`auto`/`h264`/`h265`)用 `uuremote-server set-encoder`、`uuremote-server set-codec` 写入(非法参数退出码 2)。非 `auto` 的值优先于 `UU_VIDEO_ENCODER` / `UU_VIDEO_CODEC`;`auto` 或留空则落到环境变量,再落到 auto(有 NVIDIA 时 NVENC 优先,失败再 CPU;没有 NVIDIA 时就是 CPU,不探测;codec 仍是硬件优先 H.265、CPU 用 H.264)。探测在启动时后台进行,不挡住第一路出图。成功结果缓存到进程退出,但 NVIDIA 是否存在(`/proc/driver/nvidia/version` 或 `/dev/nvidia0`)与报告不一致时,下一次桌面连接会在后台再探测一次(`warm_auto_probe`),这一路仍用缓存的编码器,直到新结果写入。NVENC 失败只缓存 10 分钟,到期后的桌面连接同样在后台再探测一次,这一路仍用当前结果。`set-encoder` 和 `set-codec` 写完后若 `host.pid` 指向正在运行的本程序,都会发 `SIGUSR2`:编码器变化时进行中的桌面流立刻换编码器(不断开,沿用本会话已协商的 codec;新编码器起不来时按进程内 → FFmpeg 进程 → CPU 回退)。手改文件后可 `kill -USR2 $(cat ~/.config/uuremote/host.pid)`。`SIGUSR2` 不重新探测。codec 偏好仍在下次建立桌面连接时生效(运行中的 host 只记日志,不重开编码)。显式 `vaapi` 请求真彩时仍用 VAAPI、编码 4:2:0,并在 CaptureSettingSync 的 field 3 回报 4:2:0。所选编码器在本机不可用时,建流回退 CPU 并记 warning。VAAPI 进入 auto 要等 #33。
|
||||
|
||||
|
||||
@@ -732,7 +732,9 @@ int vaapi_encode_frame(
|
||||
rc.window_size = 1000;
|
||||
rc.initial_qp = 28;
|
||||
rc.min_qp = 16;
|
||||
rc.max_qp = 44;
|
||||
// The driver's VBR raises QP only up to max_qp; 44 blocked the capped presets from reaching
|
||||
// the QP 51 hardware floor on hard content (2560x1600 testsrc2: 4.16 -> 2.60 Mbps, #33)
|
||||
rc.max_qp = 51;
|
||||
rc.rc_flags.bits.disable_frame_skip = 1; // a remote desktop must not drop frames silently
|
||||
b.misc[b.misc_n++] = vaapi_create_misc_buffer(engine, VAEncMiscParameterTypeRateControl, &rc, sizeof(rc));
|
||||
|
||||
|
||||
+17
-2
@@ -56,6 +56,15 @@ static void track_injected_key(int keycode, int is_press) {
|
||||
}
|
||||
}
|
||||
|
||||
/* XShmAttach fails asynchronously (BadAccess when the X server cannot reach our segment, e.g. a
|
||||
* server in another container); Xlib's default handler would exit the process before the XGetImage fallback. */
|
||||
static volatile int g_shm_attach_failed = 0;
|
||||
static int shm_attach_error(Display *display, XErrorEvent *event) {
|
||||
(void)display; (void)event;
|
||||
g_shm_attach_failed = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void shm_open_image(void) {
|
||||
g_use_shm = 0;
|
||||
if (!XShmQueryExtension(g_display)) return;
|
||||
@@ -66,9 +75,15 @@ static void shm_open_image(void) {
|
||||
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)) {
|
||||
if (g_shminfo.shmaddr != (char *)-1) {
|
||||
XSync(g_display, False);
|
||||
g_use_shm = 1;
|
||||
g_shm_attach_failed = 0;
|
||||
int (*previous)(Display *, XErrorEvent *) = XSetErrorHandler(shm_attach_error);
|
||||
Status attached = XShmAttach(g_display, &g_shminfo);
|
||||
XSync(g_display, False);
|
||||
XSetErrorHandler(previous);
|
||||
if (attached && !g_shm_attach_failed) g_use_shm = 1;
|
||||
else fprintf(stderr, "X11 shared-memory capture unavailable; using XGetImage\n");
|
||||
}
|
||||
shmctl(g_shminfo.shmid, IPC_RMID, 0); // 标记删除,进程退出时自动回收;失败时也不遗留段
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
先开端口映射再打开桌面时不再黑屏;VAAPI 低码率档能压到更低码率。/ Opening the desktop after a port mapping no longer shows a black screen; VAAPI low-bitrate presets go lower.
|
||||
|
||||
### 修复 / Fixed
|
||||
- **端口映射后打开桌面黑屏(#99)**:已有端口映射连接时,Mac 控制端打开桌面不会新建连接,而是在原连接上发送 `capture_desktop`。
|
||||
宿主现在会把这条连接转为桌面:画面、光标、剪贴板和会话计数都会启动,剪贴板能力也会重新通告。之后再打开新的桌面时,旧连接只停掉桌面,
|
||||
端口映射照常可用。若此前 X11 分辨率有变化,会按新尺寸重建采集。限制:Wayland Portal 后端暂不支持这种方式;这条连接上没有声音。
|
||||
- **VAAPI 低档码率(#33,#102,感谢 @ferstar)**:in-process VAAPI 编码的最大 QP 从 44 放宽到 51,复杂画面下低档可降到硬件能达到的最低码率;中高档不受影响。
|
||||
- A viewer that opens the desktop on an existing port-mapping connection (`capture_desktop`) now gets video, cursor, clipboard and session handling on that connection;
|
||||
a later desktop stops it without closing the port mappings. In-process VAAPI `max_qp` raised from 44 to 51.
|
||||
|
||||
### 验证范围 / Validation
|
||||
#99:根因由报告者日志证实;修复只有单元测试与 CI,官方 Mac 控制端下尚未实机验证,欢迎反馈。#102:贡献者在 AMD 核显上实测(2560×1600 复杂画面最低档 4.16→2.60 Mbps,
|
||||
中高档输出一致),我们这边仅 CI。
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux_0.2.30_<amd64|arm64>.deb`,或 `uu-remote-linux-v0.2.30-<x86_64|aarch64>-linux.tar.gz`(解压后运行 `./install.sh`),或 `docker pull ghcr.io/vallierdev/uu-remote-linux:v0.2.30`(x86_64)。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,14 @@
|
||||
NVENC 新增可选 VBV 缓冲开关,缓解高分辨率下关键帧按 GOP 周期闪烁。/ Opt-in NVENC VBV buffer for high-resolution keyframe pulsing.
|
||||
|
||||
### 新增 / Added
|
||||
- **`UU_NVENC_BUFFER_MS`(#103,感谢 @zhl-HFUT 的分析)**:NVENC 的 VBV 缓冲(10–1000 ms,缓冲 = 码率上限 × 毫秒 / 1000),FFmpeg 进程管线和进程内引擎都生效。
|
||||
默认缓冲(100–500 kbit)在 1440p 等高分辨率下会把关键帧压糊,画面按 GOP 周期“糊一下再变清”;遇到时可设 `UU_NVENC_BUFFER_MS=1000`。默认行为不变。
|
||||
- New `UU_NVENC_BUFFER_MS` (10–1000 ms of the bitrate cap) for NVENC, applied to both the FFmpeg pipeline and the in-process engine. Default unchanged.
|
||||
|
||||
### 验证范围 / Validation
|
||||
仅单元测试与 CI,尚未在 NVENC 实机上验证,欢迎反馈。
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux_0.2.31_<amd64|arm64>.deb`,或 `uu-remote-linux-v0.2.31-<x86_64|aarch64>-linux.tar.gz`(解压后运行 `./install.sh`),或 `docker pull ghcr.io/vallierdev/uu-remote-linux:v0.2.31`(x86_64)。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,17 @@
|
||||
修复端口映射在 ICE 瞬断恢复后被续房拆掉、信令卡死后长时间假在线,以及查看器 CLI 的指针偏移与滚轮。/ Fixes port-mapping teardown after ICE recovery, stalled signaling, and viewer CLI pointer/wheel input.
|
||||
|
||||
### 修复 / Fixed
|
||||
- **ICE 瞬断恢复后连接被拆(#109,感谢 @sinoyster 的细致分析)**:WebRTC 的 ICE 状态回调读到的是占位 DTLS 对象,ICE `Disconnected → Connected` 恢复后 PeerConnection 停在 `New`,15 分钟续房把仍在使用的端口映射当成空闲,拆房并断开所有映射 TCP 流。现在恢复后会回到 `Connected`。
|
||||
After a brief ICE drop the peer state settled on `New` instead of `Connected`, so the idle room renewal tore down live port-mapping streams.
|
||||
- **信令投递卡住时心跳检测失效(#104,感谢 @RiverOnVenus)**:事件队列阻塞时 Engine.IO 超时不再执行,主机可能带着半关闭的连接“在线”数小时。现在有独立看门狗、发送超时和关闭超时。
|
||||
Heartbeat detection keeps running when event delivery stalls; the host reconnects instead of staying half-online.
|
||||
- **查看器 CLI**:16:10 等非 16:9 主机的指针按内容区域映射,不再内偏(#107,感谢 @liuyanghejerry);鼠标滚轮/触控板转发为滚动(#108,感谢 @liuyanghejerry)。
|
||||
Viewer CLI: pointer mapped to the content rectangle of the padded canvas; mouse wheel forwarded.
|
||||
|
||||
### 验证范围 / Validation
|
||||
单元测试与 CI;#109 的 ICE 恢复场景尚未实机复现,欢迎反馈。
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux_0.2.32_<amd64|arm64>.deb`,或 `uu-remote-linux-v0.2.32-<x86_64|aarch64>-linux.tar.gz`(解压后运行 `./install.sh`),或 `docker pull ghcr.io/vallierdev/uu-remote-linux:v0.2.32`(x86_64)。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,13 @@
|
||||
修复 Wayland 锁屏或熄屏后,由工具连接(端口映射)升级的桌面视频结束后无法再次打开。/ Fixes a promoted tool connection whose desktop video could not be reopened after the screen locked or blanked.
|
||||
|
||||
### 修复 / Fixed
|
||||
- **锁屏/熄屏后桌面无法重新打开(#114,感谢 @sinoyster 的完整报告)**:通过 capture_desktop 升级为桌面的工具连接上,若 Wayland 采集因锁屏或 DPMS 关闭而结束,再次 capture_desktop 不会重建视频,查看器只剩黑屏。现在会重新请求桌面并在原连接上重启视频,保留原输入租约。
|
||||
On a tool connection promoted with capture_desktop, a desktop stream that ended (e.g. Wayland capture failing on lock/DPMS) is now restarted on the next capture_desktop, keeping the original input lease.
|
||||
|
||||
### 验证范围 / Validation
|
||||
单元测试与 CI;#114 的锁屏场景尚未实机复现,欢迎反馈。
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux_0.2.33_<amd64|arm64>.deb`,或 `uu-remote-linux-v0.2.33-<x86_64|aarch64>-linux.tar.gz`(解压后运行 `./install.sh`),或 `docker pull ghcr.io/vallierdev/uu-remote-linux:v0.2.33`(x86_64)。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,15 @@
|
||||
修复网络波动后信令重连卡住,以及 Linux 控制端在平铺窗口管理器下画面空白。/ Fixes signaling reconnect stalling after a network hiccup, and the Linux viewer showing a blank window under tiling compositors.
|
||||
|
||||
### 修复 / Fixed
|
||||
- **网络异常后停在 Acquired room signaling credentials(#115,感谢 @Changxi-M)**:信令 WebSocket 握手没有超时,网络半断时会无限等待,重连不再重试。现在握手限时 20 秒,超时后按原有退避重试。
|
||||
The signaling WebSocket handshake is now bounded to 20 s, so a stalled handshake falls back to the normal reconnect retry instead of hanging forever.
|
||||
- **控制端窗口平铺后画面空白(#118,感谢 @shao-zhijie)**:Linux 控制端只在收到新解码帧时绘制,合成器调整窗口尺寸后静止画面不会重绘;在尚未收到首帧时,Wayland 也不会映射没有提交缓冲的窗口。现在窗口尺寸变化/重新显示时用最后一帧重绘,无帧时先画黑底。
|
||||
The Linux viewer now repaints the last frame after a compositor resize/expose, and paints a black frame before the first decoded frame so Wayland maps the window.
|
||||
|
||||
### 验证范围 / Validation
|
||||
单元测试与 CI;#115、#118 的场景尚未实机复现,欢迎反馈。
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux_0.2.34_<amd64|arm64>.deb`,或 `uu-remote-linux-v0.2.34-<x86_64|aarch64>-linux.tar.gz`(解压后运行 `./install.sh`),或 `docker pull ghcr.io/vallierdev/uu-remote-linux:v0.2.34`(x86_64)。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,21 @@
|
||||
信令可走 HTTP 代理、二维码登录错误分类、重复主机实例诊断,以及 Wayland 手机端文字输入兜底。/ Signaling through an opt-in HTTP proxy, clearer QR login errors, duplicate-host diagnostics, and a Wayland fallback for mobile text input.
|
||||
|
||||
### 新增 / Added
|
||||
- **信令遵循 `UU_USE_PROXY=1`(#123)**:主机和 Linux 控制端的 WebSocket 信令改用与 REST 接口相同的 HTTP 客户端,开启 `UU_USE_PROXY=1` 后也走环境里的 HTTP/HTTPS 代理(不支持 SOCKS;选中的代理失败不会改为直连;不跟随重定向)。不代理 ICE/WebRTC 媒体。默认(不开代理)行为不变。
|
||||
Host and viewer signaling now use the same HTTP client as the REST API, so `UU_USE_PROXY=1` applies to signaling too (HTTP/HTTPS proxies only, no direct fallback, no redirects). Media is not proxied; the default is unchanged.
|
||||
- **Wayland/Portal 手机端文字输入兜底(#119,感谢 @shofukantkts)**:没有空闲键码的合成器和 Portal 后端上,手机端输入的文字经剪贴板 + Shift+Insert 粘贴;`UU_WAYLAND_TEXT=0` 可关闭。
|
||||
Mobile text input on Portal and compositors without spare keycodes is pasted via the clipboard and Shift+Insert; `UU_WAYLAND_TEXT=0` disables it.
|
||||
|
||||
### 修复 / Fixed
|
||||
- **二维码登录错误分类(#121)**:状态查询失败不再一律提示「网络超时」,区分超时、连接失败、响应异常与二维码过期;诊断只含阶段、类别、耗时和状态码,不输出票据、URL 或服务器内容。
|
||||
QR polling failures are no longer all reported as a network timeout; timeouts, connection failures, bad responses and expiry are distinguished, with diagnostics that never include tickets, URLs or server content.
|
||||
- **重复主机实例(#122)**:同一身份的两个主机实例(拷贝了 `config.json`,或残留的旧手动实例)会互相把对方踢出房间,表现为控制端一连就断。`doctor` 现在能数到重新执行后进程名为 `exe` 的实例并给出提示;README 说明不要在机器之间拷贝 `config.json`;日志会记录无 peer id 的 leave 事件结构(仅键名/类型/长度)。
|
||||
Two host instances sharing one identity kick each other out of the room. `doctor` now counts re-executed instances and warns about extras, the README explains not to copy `config.json` between machines, and leave events without a peer id log their shape (keys/types/lengths only).
|
||||
|
||||
### 验证范围 / Validation
|
||||
单元测试与 CI。#123 在真实 Linux X11 主机上实测不开代理时信令与 iPhone 桌面连接正常;经真实代理的信令尚未实测。#122 的修复在报告环境实机验证。
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux_0.2.35_<amd64|arm64>.deb`,或 `uu-remote-linux-v0.2.35-<x86_64|aarch64>-linux.tar.gz`(解压后运行 `./install.sh`),或 `docker pull ghcr.io/vallierdev/uu-remote-linux:v0.2.35`(x86_64)。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,15 @@
|
||||
修复 X11 共享内存被拒时主机直接退出,以及 Linux 控制端加入已有会话时可能永久黑屏。/ Fixes the host exiting when X11 refuses shared memory, and a possible black screen when the Linux viewer joins a host that already streams to another session.
|
||||
|
||||
### 修复 / Fixed
|
||||
- **X11 MIT-SHM 被拒时直接退出**:X 服务器无法访问本程序的共享内存段时(例如 X 服务器在另一个容器里),`XShmAttach` 返回 BadAccess,Xlib 默认错误处理直接结束了进程,原有的 `XGetImage` 退路没有机会执行。现在捕获该错误并改用 `XGetImage` 抓屏。
|
||||
When `XShmAttach` is refused (BadAccess, e.g. an X server in another container), the host now falls back to `XGetImage` instead of being terminated by Xlib's default error handler.
|
||||
- **控制端加入已有会话时黑屏(#124,感谢 @shao-zhijie)**:Linux 控制端过去只在丢包或解码出错时请求关键帧;加入一条已在推流的干净 H.264 流时永远等不到 IDR。现在首帧显示前每 30 个访问单元请求一次关键帧(PLI),300 个仍无画面时日志给出提示。
|
||||
The Linux viewer now requests key frames (PLI) every 30 access units until the first frame is shown, and logs a warning after 300.
|
||||
|
||||
### 验证范围 / Validation
|
||||
单元测试与 CI。共享内存退路尚待在报告环境实机确认;#124 中官方 Mac 主机是否响应 PLI 尚未实测。
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux_0.2.36_<amd64|arm64>.deb`,或 `uu-remote-linux-v0.2.36-<x86_64|aarch64>-linux.tar.gz`(解压后运行 `./install.sh`),或 `docker pull ghcr.io/vallierdev/uu-remote-linux:v0.2.36`(x86_64)。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
+109
-9
@@ -91,6 +91,48 @@ pub struct UUAuthClient {
|
||||
guest_id: Option<String>,
|
||||
}
|
||||
|
||||
/// QR failures are safe to display: never retain a URL, source error or response body.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum QrRequestErrorKind { Timeout, Connect, Decode, Body, Request }
|
||||
impl QrRequestErrorKind {
|
||||
fn from_reqwest(error: &reqwest::Error) -> Self {
|
||||
if error.is_timeout() { Self::Timeout }
|
||||
else if error.is_connect() { Self::Connect }
|
||||
else if error.is_decode() { Self::Decode }
|
||||
else if error.is_body() { Self::Body }
|
||||
else { Self::Request }
|
||||
}
|
||||
fn label(self) -> &'static str {
|
||||
match self { Self::Timeout => "timeout", Self::Connect => "connect", Self::Decode => "decode",
|
||||
Self::Body => "body", Self::Request => "request" }
|
||||
}
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub struct QrRequestError {
|
||||
pub kind: QrRequestErrorKind,
|
||||
phase: &'static str,
|
||||
elapsed_ms: u128,
|
||||
http_status: Option<u16>,
|
||||
}
|
||||
impl std::fmt::Display for QrRequestError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "phase={} kind={} elapsed_ms={} http_status={:?}",
|
||||
self.phase, self.kind.label(), self.elapsed_ms, self.http_status)
|
||||
}
|
||||
}
|
||||
impl QrRequestError {
|
||||
pub fn response_received(&self) -> bool { self.http_status.is_some() }
|
||||
fn new(kind: QrRequestErrorKind, phase: &'static str, started: std::time::Instant, http_status: Option<u16>) -> Self {
|
||||
Self { kind, phase, elapsed_ms: started.elapsed().as_millis(), http_status }
|
||||
}
|
||||
}
|
||||
async fn send_qr_request(request: reqwest::RequestBuilder, started: std::time::Instant) -> Result<(u16, serde_json::Value), QrRequestError> {
|
||||
let resp = request.send().await.map_err(|e| QrRequestError::new(QrRequestErrorKind::from_reqwest(&e), "send", started, None))?;
|
||||
let status = resp.status().as_u16();
|
||||
let value = resp.json().await.map_err(|e| QrRequestError::new(QrRequestErrorKind::from_reqwest(&e), "response", started, Some(status)))?;
|
||||
Ok((status, value))
|
||||
}
|
||||
|
||||
/// 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.
|
||||
@@ -121,17 +163,20 @@ fn redact_urls(text: &str) -> String {
|
||||
out
|
||||
}
|
||||
|
||||
/// Cloud API clients ignore ambient HTTP/SOCKS proxy settings by default: a local
|
||||
/// Cloud API and signaling clients ignore ambient HTTP/HTTPS proxy settings by default: a local
|
||||
/// proxy that intercepts TLS breaks login and signaling. `UU_USE_PROXY=1` opts back
|
||||
/// into reqwest's standard proxy environment handling.
|
||||
pub fn http_client_builder() -> reqwest::ClientBuilder {
|
||||
let builder = reqwest::Client::builder();
|
||||
if std::env::var("UU_USE_PROXY").ok().as_deref().is_some_and(proxy_requested) {
|
||||
if proxy_enabled() {
|
||||
builder
|
||||
} else {
|
||||
builder.no_proxy()
|
||||
}
|
||||
}
|
||||
pub(crate) fn proxy_enabled() -> bool {
|
||||
std::env::var("UU_USE_PROXY").ok().as_deref().is_some_and(proxy_requested)
|
||||
}
|
||||
fn proxy_requested(value: &str) -> bool {
|
||||
matches!(value.trim().to_ascii_lowercase().as_str(), "1" | "true" | "yes" | "on")
|
||||
}
|
||||
@@ -253,13 +298,13 @@ impl UUAuthClient {
|
||||
|
||||
/// Signed POST with an optional JSON body (None = empty body, like Moya `requestPlain`) and an
|
||||
/// explicit timeout. The official clients long-poll the QR status with a 60 s request timeout.
|
||||
pub async fn post_with_timeout(&self, path: &str, body: Option<&serde_json::Value>, timeout: std::time::Duration) -> Result<(u16, serde_json::Value), String> {
|
||||
let body_str = body.map(serde_json::to_string).transpose().map_err(|e| e.to_string())?.unwrap_or_default();
|
||||
let resp = self.http.post(format!("{}{}", BASE_URL, path)).timeout(timeout)
|
||||
.headers(self.build_headers("POST", path, &body_str)).body(body_str)
|
||||
.send().await.map_err(|e| format!("HTTP post {path} failed: {}", transport_error(&e)))?;
|
||||
let status = resp.status().as_u16();
|
||||
Ok((status, resp.json().await.unwrap_or(serde_json::Value::Null)))
|
||||
pub async fn post_with_timeout(&self, path: &str, body: Option<&serde_json::Value>, timeout: std::time::Duration) -> Result<(u16, serde_json::Value), QrRequestError> {
|
||||
let started = std::time::Instant::now();
|
||||
let body_str = body.map(serde_json::to_string).transpose()
|
||||
.map_err(|_| QrRequestError::new(QrRequestErrorKind::Request, "encode", started, None))?.unwrap_or_default();
|
||||
let request = self.http.post(format!("{}{}", BASE_URL, path)).timeout(timeout)
|
||||
.headers(self.build_headers("POST", path, &body_str)).body(body_str);
|
||||
send_qr_request(request, started).await
|
||||
}
|
||||
|
||||
/// Signed JSON POST to another API host (the signature covers the path only).
|
||||
@@ -389,3 +434,58 @@ mod guest_tests {
|
||||
for value in ["", "0", "false", "no", "off", "2"] { assert!(!super::proxy_requested(value)); }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod qr_request_tests {
|
||||
use super::*;
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
|
||||
// Synthetic, loopback-only responses: no account, device, proxy or live login API.
|
||||
async fn response(reply: &'static [u8], stall: bool) -> QrRequestError {
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
let server = tokio::spawn(async move {
|
||||
let (mut socket, _) = listener.accept().await.unwrap();
|
||||
let mut request = [0u8; 4096];
|
||||
let _ = socket.read(&mut request).await.unwrap();
|
||||
socket.write_all(reply).await.unwrap();
|
||||
if stall { std::future::pending::<()>().await; }
|
||||
});
|
||||
let request = reqwest::Client::builder().no_proxy().build().unwrap()
|
||||
.post(format!("http://fixture-user:fixture-password@{addr}/fixture-path?token=fixture-ticket"))
|
||||
.timeout(Duration::from_secs(2));
|
||||
let error = send_qr_request(request, Instant::now()).await.unwrap_err();
|
||||
server.abort();
|
||||
let output = format!("{error} {error:?}");
|
||||
for secret in ["fixture-user", "fixture-password", "fixture-path", "fixture-ticket", "fixture-body", "127.0.0.1", "http://"] {
|
||||
assert!(!output.contains(secret));
|
||||
}
|
||||
error
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn qr_request_distinguishes_send_timeout_body_timeout_and_bad_json_without_secrets() {
|
||||
let error = response(b"", true).await;
|
||||
assert_eq!((error.kind, error.phase, error.http_status), (QrRequestErrorKind::Timeout, "send", None));
|
||||
assert!(!error.response_received());
|
||||
let error = response(b"HTTP/1.1 200 OK\r\nContent-Length: 100\r\n\r\n", true).await;
|
||||
assert_eq!((error.kind, error.phase, error.http_status), (QrRequestErrorKind::Timeout, "response", Some(200)));
|
||||
assert!(error.response_received());
|
||||
let error = response(b"HTTP/1.1 502 Bad Gateway\r\nContent-Length: 12\r\n\r\nfixture-body", false).await;
|
||||
assert_eq!((error.kind, error.phase, error.http_status), (QrRequestErrorKind::Decode, "response", Some(502)));
|
||||
assert!(error.response_received());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn qr_connection_failure_is_not_a_timeout() {
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
drop(listener);
|
||||
let request = reqwest::Client::builder().no_proxy().build().unwrap()
|
||||
.post(format!("http://{addr}/fixture-ticket")).timeout(Duration::from_secs(2));
|
||||
let error = send_qr_request(request, Instant::now()).await.unwrap_err();
|
||||
assert_eq!((error.kind, error.phase, error.http_status), (QrRequestErrorKind::Connect, "send", None));
|
||||
assert!(!format!("{error} {error:?}").contains("fixture-ticket"));
|
||||
}
|
||||
}
|
||||
@@ -215,3 +215,56 @@ async fn locked_control_unlock_requests_and_tools_work_without_a_video_frame() {
|
||||
drop(lock);
|
||||
}).await.expect("locked bootstrap/tool regression exceeded deadline");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn capture_desktop_on_a_port_mapping_peer_promotes_it_once() {
|
||||
tokio::time::timeout(Duration::from_secs(25), async {
|
||||
let input = Arc::new(InputInjector::without_desktop());
|
||||
let (host, mut viewer) = connect(input.clone(), false, None, &["CONTROL_DATA_CHANNEL", "FILE_DATA_CHANNEL"]).await;
|
||||
let (requests, mut requested) = mpsc::unbounded_channel();
|
||||
host.on_desktop_request(move || { let _ = requests.send(()); });
|
||||
receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 13).await;
|
||||
assert!(!host.role.is_desktop() && host.role.input().is_none());
|
||||
// The Mac viewer opens the desktop on its existing port-mapping peer (#99).
|
||||
send(&viewer, "CONTROL_DATA_CHANNEL", crate::initialization::envelope(5, 0, 3, &[8, 4])).await;
|
||||
tokio::time::timeout(Duration::from_secs(5), requested.recv()).await.unwrap().unwrap();
|
||||
host.promote(None);
|
||||
assert!(host.role.is_desktop());
|
||||
assert!(host.role.input().is_some_and(|session| session.is_current()));
|
||||
// CONTROL repeats the desktop initialization for the same viewer.
|
||||
receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 13).await;
|
||||
// The port-mapping session on this peer still answers.
|
||||
send(&viewer, "FILE_DATA_CHANNEL", mapping(2, b"still mapped")).await;
|
||||
receive(&mut viewer.messages, "FILE_DATA_CHANNEL", 27).await;
|
||||
// A repeated capture_desktop does not ask again, and a later promote is a no-op.
|
||||
send(&viewer, "CONTROL_DATA_CHANNEL", crate::initialization::envelope(6, 0, 3, &[8, 4])).await;
|
||||
send(&viewer, "CONTROL_DATA_CHANNEL", crate::initialization::envelope(7, 0, 3, &[8, 0])).await;
|
||||
receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 3).await;
|
||||
assert!(requested.try_recv().is_err());
|
||||
let lease = host.role.input().unwrap();
|
||||
host.promote(None);
|
||||
assert!(lease.is_current());
|
||||
// Its video ended on its own (capture failed while locked, #114): capture_desktop restarts it
|
||||
// on the same input lease.
|
||||
let ended = tokio::spawn(async {});
|
||||
*host.role.stream_task.lock().unwrap() = Some(ended.abort_handle());
|
||||
ended.await.unwrap();
|
||||
send(&viewer, "CONTROL_DATA_CHANNEL", crate::initialization::envelope(9, 0, 3, &[8, 4])).await;
|
||||
tokio::time::timeout(Duration::from_secs(5), requested.recv()).await.unwrap().unwrap();
|
||||
host.promote(None);
|
||||
assert!(host.role.is_desktop() && !host.role.stream_ended() && lease.is_current());
|
||||
// A newer desktop demotes it; the port mapping stays, and capture_desktop promotes it again.
|
||||
host.demote();
|
||||
assert!(!host.role.is_desktop() && host.role.input().is_none());
|
||||
send(&viewer, "FILE_DATA_CHANNEL", mapping(2, b"after demotion")).await;
|
||||
receive(&mut viewer.messages, "FILE_DATA_CHANNEL", 27).await;
|
||||
send(&viewer, "CONTROL_DATA_CHANNEL", crate::initialization::envelope(8, 0, 3, &[8, 4])).await;
|
||||
tokio::time::timeout(Duration::from_secs(5), requested.recv()).await.unwrap().unwrap();
|
||||
host.promote(None);
|
||||
assert!(host.role.is_desktop() && host.role.input().is_some_and(|session| session.is_current()));
|
||||
receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 13).await;
|
||||
host.pc.close().await.unwrap();
|
||||
viewer.pc.close().await.unwrap();
|
||||
viewer.router.lock().await.stop().await.unwrap();
|
||||
}).await.expect("capture_desktop promotion exceeded deadline");
|
||||
}
|
||||
+50
-2
@@ -147,11 +147,39 @@ pub async fn run() -> Result<(), String> {
|
||||
Err(_) => line("config mode", "missing (run `uuremote-server login`)"),
|
||||
}
|
||||
line("service", command_output("systemctl", &["--user", "is-active", "uuremote-native.service"]).await.unwrap_or_else(|| "systemctl unavailable".into()));
|
||||
let running = command_output("pgrep", &["-c", "-x", "uuremote-server"]).await.filter(|n| !n.is_empty()).unwrap_or_else(|| "unknown".into());
|
||||
line("uuremote-server procs", format!("{running} (including this one; more than 2 means duplicates)"));
|
||||
let running = host_processes();
|
||||
line("uuremote-server procs", format!("{} (including this one; more than 2 means duplicates)", running.map_or_else(|| "unknown".into(), |n| n.to_string())));
|
||||
if running.is_some_and(|n| n > 2) {
|
||||
println!(" ! another host instance is running (check `pgrep -af uuremote`); stop the extra one, e.g. an old copy started by hand");
|
||||
println!(" instances sharing one identity, on this or another machine, kick each other out of the room: never copy config.json between machines");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Hosts re-exec through /proc/self/exe (comm "exe"), so match the executable's file name, not the process name.
|
||||
/// Helpers a host spawns (Wayland capture, login window) are its children and are not separate instances.
|
||||
fn host_processes() -> Option<usize> {
|
||||
let hosts: Vec<(u32, u32)> = std::fs::read_dir("/proc").ok()?.flatten()
|
||||
.filter_map(|entry| Some((entry.file_name().to_str()?.parse::<u32>().ok()?, entry.path())))
|
||||
.filter(|(_, dir)| std::fs::read_link(dir.join("exe")).is_ok_and(|exe| is_host_executable(&exe)))
|
||||
.filter_map(|(pid, dir)| Some((pid, parent_pid(&std::fs::read_to_string(dir.join("stat")).ok()?)?)))
|
||||
.collect();
|
||||
Some(top_level(&hosts))
|
||||
}
|
||||
|
||||
/// Field 4 of /proc/<pid>/stat; the command name in field 2 may contain spaces and parentheses.
|
||||
fn parent_pid(stat: &str) -> Option<u32> {
|
||||
stat.rsplit_once(')')?.1.split_whitespace().nth(1)?.parse().ok()
|
||||
}
|
||||
|
||||
fn top_level(hosts: &[(u32, u32)]) -> usize {
|
||||
hosts.iter().filter(|(_, parent)| !hosts.iter().any(|(pid, _)| pid == parent)).count()
|
||||
}
|
||||
|
||||
fn is_host_executable(exe: &std::path::Path) -> bool {
|
||||
exe.file_name().and_then(|name| name.to_str()).is_some_and(|name| name.trim_end_matches(" (deleted)") == "uuremote-server")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
@@ -174,4 +202,24 @@ mod tests {
|
||||
assert!(!super::missing_display(DesktopBackend::Wayland, false, None, None));
|
||||
assert!(!super::missing_display(DesktopBackend::X11, true, None, None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn host_processes_match_the_executable_even_after_reexec() {
|
||||
use std::path::Path;
|
||||
for exe in ["/usr/bin/uuremote-server", "/home/u/.local/bin/uuremote-server", "/usr/bin/uuremote-server (deleted)"] {
|
||||
assert!(super::is_host_executable(Path::new(exe)));
|
||||
}
|
||||
for exe in ["/usr/bin/uuremote-viewer", "/usr/bin/bash", "/usr/bin/uuremote-server-old"] {
|
||||
assert!(!super::is_host_executable(Path::new(exe)));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn host_helpers_are_not_counted_as_instances() {
|
||||
assert_eq!(super::parent_pid("8943 (exe) S 1699 8943 1699 0 -1"), Some(1699));
|
||||
assert_eq!(super::parent_pid("12 (a) b (c) R 7 12"), Some(7));
|
||||
// service 10 with a login window 11, a stale copy 20, doctor 30
|
||||
assert_eq!(super::top_level(&[(10, 1), (11, 10), (20, 5), (30, 9)]), 3);
|
||||
assert_eq!(super::top_level(&[(10, 1), (11, 10), (30, 9)]), 2);
|
||||
}
|
||||
}
|
||||
+134
-4
@@ -372,8 +372,93 @@ impl InputInjector {
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::key(symbol,pressed) }
|
||||
else { unsafe { x11_key_symbol(symbol as libc::c_ulong,i32::from(pressed)) } }
|
||||
}
|
||||
/// Set clipboard content for text injection on Wayland / Portal.
|
||||
/// Tries Portal RemoteDesktop.SetSelection first, then wl-copy as fallback.
|
||||
fn set_clipboard_text(&self, text: &str) -> bool {
|
||||
if text.is_empty() || text.len() > crate::clipboard::MAX_TEXT {
|
||||
return false;
|
||||
}
|
||||
// In unit tests, never invoke external clipboard binaries (wl-copy)
|
||||
// or alter the system clipboard to prevent side effects on developer desktops.
|
||||
#[cfg(test)]
|
||||
{
|
||||
let _ = text;
|
||||
false
|
||||
}
|
||||
#[cfg(not(test))]
|
||||
{
|
||||
// 1. Try Portal clipboard if session is bound and active
|
||||
if let Some(portal) = self.portal.lock().unwrap().as_ref() {
|
||||
if portal.clipboard_set(text).is_ok() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// 2. Try wl-copy in Wayland session
|
||||
if std::env::var_os("WAYLAND_DISPLAY").is_some() {
|
||||
if let Ok(mut child) = std::process::Command::new("wl-copy")
|
||||
.args(["--type", "text/plain;charset=utf-8"])
|
||||
.stdin(std::process::Stdio::piped())
|
||||
.stdout(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::null())
|
||||
.spawn()
|
||||
{
|
||||
if let Some(mut stdin) = child.stdin.take() {
|
||||
use std::io::Write;
|
||||
let _ = stdin.write_all(text.as_bytes());
|
||||
}
|
||||
if let Ok(status) = child.wait() {
|
||||
if status.success() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Injects text by copying it to the clipboard and simulating a paste shortcut.
|
||||
/// Uses Shift+Insert by default (broadest Linux compatibility across GUI apps and terminals),
|
||||
/// or Ctrl+V if configured via UU_WAYLAND_PASTE_KEY=ctrl_v.
|
||||
fn paste_text_via_clipboard(&self, text: &str) -> bool {
|
||||
if !self.set_clipboard_text(text) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Give the clipboard daemon / compositor a brief moment to register the selection.
|
||||
std::thread::sleep(std::time::Duration::from_millis(25));
|
||||
|
||||
let use_ctrl_v = std::env::var("UU_WAYLAND_PASTE_KEY")
|
||||
.is_ok_and(|v| v.eq_ignore_ascii_case("ctrl_v") || v.eq_ignore_ascii_case("ctrl+v"));
|
||||
|
||||
if use_ctrl_v {
|
||||
// Control_L (0xffe3) + 'v' (0x76)
|
||||
self.inject_key(0xffe3, true);
|
||||
std::thread::sleep(std::time::Duration::from_millis(15));
|
||||
self.inject_key(0x76, true);
|
||||
std::thread::sleep(std::time::Duration::from_millis(20));
|
||||
self.inject_key(0x76, false);
|
||||
std::thread::sleep(std::time::Duration::from_millis(15));
|
||||
self.inject_key(0xffe3, false);
|
||||
} else {
|
||||
// Shift_L (0xffe1) + Insert (0xff63)
|
||||
self.inject_key(0xffe1, true);
|
||||
std::thread::sleep(std::time::Duration::from_millis(15));
|
||||
self.inject_key(0xff63, true);
|
||||
std::thread::sleep(std::time::Duration::from_millis(20));
|
||||
self.inject_key(0xff63, false);
|
||||
std::thread::sleep(std::time::Duration::from_millis(15));
|
||||
self.inject_key(0xffe1, false);
|
||||
}
|
||||
|
||||
// Brief delay allowing the target application to consume the paste event
|
||||
std::thread::sleep(std::time::Duration::from_millis(25));
|
||||
true
|
||||
}
|
||||
|
||||
/// Mobile input-method text (`text_input`). X11 types it directly; Wayland re-maps a spare
|
||||
/// keycode in the virtual keyboard's keymap. Other backends log once.
|
||||
/// keycode in the virtual keyboard's keymap. Portal and other Wayland environments inject
|
||||
/// via clipboard paste (Shift+Insert). Set UU_WAYLAND_TEXT=0 to disable Wayland text injection.
|
||||
pub fn text_input(&self, text: &str) {
|
||||
let _serial=self.serial.lock().unwrap();
|
||||
let Ok(c_text)=std::ffi::CString::new(text.replace('\0',"")) else { return };
|
||||
@@ -381,17 +466,31 @@ impl InputInjector {
|
||||
let typed=unsafe { x11_type_utf8(c_text.as_ptr()) };
|
||||
log::info!("Desktop text input: chars={} result={}", text.chars().count(), typed);
|
||||
} else if self.backend==crate::desktop_backend::DesktopBackend::Wayland {
|
||||
// Escape hatch: keep the old behaviour without rebuilding.
|
||||
// Escape hatch: disable Wayland text typing/pasting entirely without rebuilding.
|
||||
if std::env::var("UU_WAYLAND_TEXT").is_ok_and(|v|v=="0") { return; }
|
||||
let typed=crate::wayland::type_utf8(text);
|
||||
if typed>=0 {
|
||||
log::info!("Desktop text input: chars={} result={}", text.chars().count(), typed);
|
||||
return;
|
||||
}
|
||||
// A keymap whose keycodes are all mapped cannot carry text; report it once.
|
||||
// Fallback to clipboard paste if keymap has no spare keycodes or native typing failed
|
||||
if self.paste_text_via_clipboard(text) {
|
||||
log::info!("Desktop text input (Wayland clipboard paste fallback): chars={} result=ok", text.chars().count());
|
||||
return;
|
||||
}
|
||||
static WARNED:std::sync::atomic::AtomicBool=std::sync::atomic::AtomicBool::new(false);
|
||||
if !WARNED.swap(true,std::sync::atomic::Ordering::Relaxed) {
|
||||
log::warn!("Mobile text input unavailable on the Wayland backend (no spare keycode in this keymap)");
|
||||
log::warn!("Mobile text input unavailable on the Wayland backend (native and clipboard fallback failed)");
|
||||
}
|
||||
} else if self.backend==crate::desktop_backend::DesktopBackend::Portal {
|
||||
if std::env::var("UU_WAYLAND_TEXT").is_ok_and(|v|v=="0") { return; }
|
||||
if self.paste_text_via_clipboard(text) {
|
||||
log::info!("Desktop text input (Portal clipboard paste): chars={} result=ok", text.chars().count());
|
||||
return;
|
||||
}
|
||||
static WARNED:std::sync::atomic::AtomicBool=std::sync::atomic::AtomicBool::new(false);
|
||||
if !WARNED.swap(true,std::sync::atomic::Ordering::Relaxed) {
|
||||
log::warn!("Portal clipboard text paste failed: ensure clipboard permission or wl-copy is available");
|
||||
}
|
||||
} else {
|
||||
static WARNED:std::sync::atomic::AtomicBool=std::sync::atomic::AtomicBool::new(false);
|
||||
@@ -728,3 +827,34 @@ mod x11_monitor_pointer_tests {
|
||||
crate::wayland::select(0).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod clipboard_paste_text_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn rejects_empty_and_oversized_text() {
|
||||
let input = InputInjector::new();
|
||||
assert!(!input.set_clipboard_text(""));
|
||||
let oversized = "a".repeat(crate::clipboard::MAX_TEXT + 1);
|
||||
assert!(!input.set_clipboard_text(&oversized));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_input_handles_portal_backend_safely_without_side_effects() {
|
||||
let mut input = InputInjector::new();
|
||||
input.backend = crate::desktop_backend::DesktopBackend::Portal;
|
||||
// In unit tests, set_clipboard_text avoids external commands so this
|
||||
// will not mutate the host's actual clipboard.
|
||||
input.text_input("测试输入法文字");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_input_respects_disable_env() {
|
||||
let mut input = InputInjector::new();
|
||||
input.backend = crate::desktop_backend::DesktopBackend::Portal;
|
||||
std::env::set_var("UU_WAYLAND_TEXT", "0");
|
||||
input.text_input("测试输入法文字");
|
||||
std::env::remove_var("UU_WAYLAND_TEXT");
|
||||
}
|
||||
}
|
||||
+108
-19
@@ -8,7 +8,7 @@
|
||||
//! (analysis/tools-fix-20261013/viewer-fieldmd.json). Results are written to the private
|
||||
//! config file (chmod 600). Tokens and tickets are never printed or logged.
|
||||
|
||||
use crate::auth::{Credentials, UUAuthClient};
|
||||
use crate::auth::{Credentials, QrRequestErrorKind, UUAuthClient};
|
||||
use crate::login_ui::{LoginUi, Tone};
|
||||
use serde_json::{json, Value};
|
||||
use std::time::Duration;
|
||||
@@ -127,6 +127,35 @@ fn render_qr(content: &str) -> Result<String, String> {
|
||||
/// API code for an expired login QR code.
|
||||
const QR_EXPIRED: i64 = 1190;
|
||||
|
||||
/// QR API errors must not echo server messages/validation fields: they may contain credentials.
|
||||
fn qr_data<'a>(stage: &str, status: u16, body: &'a Value) -> Result<&'a Value, String> {
|
||||
let code = body.get("code").and_then(Value::as_i64);
|
||||
if status == 200 && code == Some(0) { return Ok(body.get("data").unwrap_or(&Value::Null)); }
|
||||
Err(format!("{stage}: http_status={status} api_code={code:?}"))
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
enum QrPollAction { Wait(i64), Scanned, Confirmed, Refresh(Duration), Failed(&'static str) }
|
||||
|
||||
/// Preserve the official refresh policy; a failed query says nothing about QR expiry.
|
||||
fn qr_poll_action(last_status: i64, http_status: u16, body: &Value) -> QrPollAction {
|
||||
if body.get("code").and_then(Value::as_i64) == Some(QR_EXPIRED) {
|
||||
return QrPollAction::Failed("二维码已失效");
|
||||
}
|
||||
let Ok(data) = qr_data("qrcode status", http_status, body) else {
|
||||
return QrPollAction::Failed("二维码状态查询失败");
|
||||
};
|
||||
match data.get("status").and_then(Value::as_i64) {
|
||||
Some(1) => QrPollAction::Wait(1),
|
||||
Some(2) if last_status != 2 => QrPollAction::Scanned,
|
||||
Some(2) => QrPollAction::Wait(2),
|
||||
Some(0) => QrPollAction::Refresh(Duration::from_millis(350)),
|
||||
Some(3) => QrPollAction::Refresh(Duration::ZERO),
|
||||
Some(4) => QrPollAction::Confirmed,
|
||||
_ => QrPollAction::Failed("二维码状态响应异常"),
|
||||
}
|
||||
}
|
||||
|
||||
/// QR modules as RGBA pixels: `scale` pixels per module, `quiet` light modules around the code.
|
||||
fn qr_rgba(content: &str, scale: usize, quiet: usize) -> Result<(Vec<u8>, usize), String> {
|
||||
let code = qrcode::QrCode::new(content.as_bytes()).map_err(|e| format!("QR encode failed: {e}"))?;
|
||||
@@ -276,8 +305,16 @@ pub(crate) async fn flow(ui: &LoginUi) -> Result<String, String> {
|
||||
// every QR within seconds (#68).
|
||||
let timeout = Duration::from_secs(60);
|
||||
loop {
|
||||
let (status, resp) = client.post_with_timeout("/api/v1/qrcode/gen/login", None, timeout).await?;
|
||||
let d = match data("qrcode gen", status, &resp) {
|
||||
let (status, resp) = match client.post_with_timeout("/api/v1/qrcode/gen/login", None, timeout).await {
|
||||
Ok(r) => r,
|
||||
Err(e) if e.response_received() => {
|
||||
eprintln!("QR generation response failed: {e}");
|
||||
wait_for_refresh(ui, "二维码加载失败").await;
|
||||
continue;
|
||||
}
|
||||
Err(e) => return Err(format!("QR generation failed: {e}")),
|
||||
};
|
||||
let d = match qr_data("qrcode gen", status, &resp) {
|
||||
Ok(d) => d.clone(),
|
||||
Err(e) => { println!("QR code could not be loaded ({e})"); wait_for_refresh(ui, "二维码加载失败").await; continue; }
|
||||
};
|
||||
@@ -303,24 +340,42 @@ pub(crate) async fn flow(ui: &LoginUi) -> Result<String, String> {
|
||||
let query = json!({"token": ticket, "qrcode_jump_url": url, "login_status": login_status});
|
||||
let (status, resp) = match client.post_with_timeout("/api/v1/qrcode/login/status", Some(&query), timeout).await {
|
||||
Ok(r) => r,
|
||||
Err(_) => break "网络超时,二维码已停止刷新",
|
||||
Err(e) => {
|
||||
eprintln!("QR status query failed: {e}; last_status={login_status}");
|
||||
break match e.kind {
|
||||
QrRequestErrorKind::Timeout => "二维码状态查询超时(不代表二维码已失效)",
|
||||
_ => "二维码状态查询失败(不代表二维码已失效)",
|
||||
};
|
||||
}
|
||||
};
|
||||
let code = resp.get("code").and_then(|c| c.as_i64());
|
||||
if code == Some(QR_EXPIRED) { break "二维码已失效"; }
|
||||
let Ok(d) = data("qrcode status", status, &resp) else { break "二维码状态查询失败"; };
|
||||
let st = d.get("status").and_then(|v| v.as_i64()).unwrap_or(login_status);
|
||||
match st {
|
||||
1 => {}
|
||||
2 => if login_status != 2 {
|
||||
match qr_poll_action(login_status, status, &resp) {
|
||||
QrPollAction::Wait(st) => {
|
||||
if st == 1 && login_status != 1 { ui.status("等待扫码…", Tone::Info); }
|
||||
login_status = st;
|
||||
}
|
||||
QrPollAction::Scanned => {
|
||||
login_status = 2;
|
||||
println!("已扫码,请在手机上确认 (scanned, confirm on the phone)");
|
||||
ui.status("已扫码,请在手机上确认登录", Tone::Ok);
|
||||
},
|
||||
3 => { println!("QR code expired; generating a new one"); break ""; }
|
||||
0 => { tokio::time::sleep(Duration::from_millis(350)).await; break ""; }
|
||||
4 => {
|
||||
}
|
||||
QrPollAction::Refresh(delay) => {
|
||||
if delay.is_zero() { println!("QR code expired; generating a new one"); }
|
||||
else { tokio::time::sleep(delay).await; }
|
||||
break "";
|
||||
}
|
||||
QrPollAction::Confirmed => {
|
||||
println!("已确认,正在完成登录 (confirmed, completing login)");
|
||||
ui.status("已确认,正在完成登录…", Tone::Info);
|
||||
let body = json!({"token": ticket, "qrcode_jump_url": url});
|
||||
let (status, resp) = client.post_with_timeout("/api/v1/login/by_qrcode", Some(&body), timeout).await?;
|
||||
let d = match data("qrcode login", status, &resp) {
|
||||
let (status, resp) = match client.post_with_timeout("/api/v1/login/by_qrcode", Some(&body), timeout).await {
|
||||
Ok(r) => r,
|
||||
Err(e) if e.response_received() => {
|
||||
eprintln!("QR login exchange response failed: {e}");
|
||||
break "登录失败";
|
||||
}
|
||||
Err(e) => return Err(format!("QR login exchange failed: {e}")),
|
||||
};
|
||||
let d = match qr_data("qrcode login", status, &resp) {
|
||||
Ok(d) => d,
|
||||
Err(e) => { println!("QR login not granted ({e})"); break "登录失败"; }
|
||||
};
|
||||
@@ -339,9 +394,12 @@ pub(crate) async fn flow(ui: &LoginUi) -> Result<String, String> {
|
||||
println!("Logged in as {} ({}); saved to {}", nickname, user, path.display());
|
||||
return Ok(if nickname.is_empty() { user.to_string() } else { nickname.to_string() });
|
||||
}
|
||||
other => { println!("QR status: {other}"); break "二维码已失效"; }
|
||||
QrPollAction::Failed(reason) => {
|
||||
eprintln!("QR status query stopped: http_status={status} api_code={:?} status={:?} last_status={login_status}",
|
||||
resp.get("code").and_then(Value::as_i64), resp.get("data").and_then(|d| d.get("status")).and_then(Value::as_i64));
|
||||
break reason;
|
||||
}
|
||||
}
|
||||
login_status = st;
|
||||
};
|
||||
if !failure.is_empty() { wait_for_refresh(ui, failure).await; }
|
||||
}
|
||||
@@ -376,6 +434,37 @@ async fn wait_for_refresh(ui: &LoginUi, reason: &str) {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn qr_poll_keeps_waiting_scanned_and_confirmed_distinct() {
|
||||
let reply = |status| json!({"code": 0, "data": {"status": status}});
|
||||
assert_eq!(qr_poll_action(1, 200, &reply(1)), QrPollAction::Wait(1));
|
||||
assert_eq!(qr_poll_action(1, 200, &reply(2)), QrPollAction::Scanned);
|
||||
assert_eq!(qr_poll_action(2, 200, &reply(2)), QrPollAction::Wait(2));
|
||||
assert_eq!(qr_poll_action(2, 200, &reply(4)), QrPollAction::Confirmed);
|
||||
assert_eq!(qr_poll_action(1, 200, &reply(0)), QrPollAction::Refresh(Duration::from_millis(350)));
|
||||
assert_eq!(qr_poll_action(2, 200, &reply(3)), QrPollAction::Refresh(Duration::ZERO));
|
||||
assert_eq!(qr_poll_action(2, 400, &json!({"code": 1190})), QrPollAction::Failed("二维码已失效"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qr_poll_failures_do_not_imply_expiry_or_confirmation() {
|
||||
for last in [1, 2] {
|
||||
for body in [json!(null), json!({"code": 1}), json!({"code": 0, "data": {"status": 4}})] {
|
||||
assert_eq!(qr_poll_action(last, 503, &body), QrPollAction::Failed("二维码状态查询失败"));
|
||||
}
|
||||
assert_eq!(qr_poll_action(last, 200, &json!({"code": 1})), QrPollAction::Failed("二维码状态查询失败"));
|
||||
for data in [json!(null), json!({}), json!({"status": "4"}), json!({"status": 99}), json!({"status": -1})] {
|
||||
assert_eq!(qr_poll_action(last, 200, &json!({"code": 0, "data": data})), QrPollAction::Failed("二维码状态响应异常"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qr_api_errors_do_not_echo_server_content() {
|
||||
let body = json!({"code": 1001, "msg": "fixture-token", "data": [{"loc": "fixture-ticket"}]});
|
||||
assert_eq!(qr_data("qrcode gen", 400, &body).unwrap_err(), "qrcode gen: http_status=400 api_code=Some(1001)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn status_ticket_prefers_status_query_ticket() {
|
||||
assert_eq!(qr_ticket(&json!({"token": "old", "status_query_ticket": "new"})), Some("new"));
|
||||
|
||||
+67
-11
@@ -1,6 +1,7 @@
|
||||
//! UU Remote Pure Native Linux Server (Rust Edition)
|
||||
//! Zero-Wine, Zero-Python, Single-Binary, 5 Native Tools Integrated
|
||||
|
||||
mod ws_transport;
|
||||
mod engine_plugin;
|
||||
mod doctor;
|
||||
mod wol;
|
||||
@@ -73,6 +74,14 @@ fn superseded_peer_ids(sessions: &HashMap<String, WebRtcSession>, ice: &str, des
|
||||
sessions.iter().filter(|(id, peer)| id.as_str() == ice || (desktop && peer.desktop))
|
||||
.map(|(id, _)| id.clone()).collect()
|
||||
}
|
||||
|
||||
/// A peer whose transport is already broken can leave `PeerConnection::close`
|
||||
/// waiting indefinitely; signaling must keep flowing without it.
|
||||
async fn close_peer(pc: &::webrtc::peer_connection::RTCPeerConnection) {
|
||||
if tokio::time::timeout(std::time::Duration::from_secs(5), pc.close()).await.is_err() {
|
||||
log::warn!("Peer connection close timed out; continuing");
|
||||
}
|
||||
}
|
||||
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()
|
||||
@@ -685,7 +694,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
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; }
|
||||
for (_, old) in webrtc_sessions.lock().await.drain() { close_peer(&old.pc).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
|
||||
@@ -737,6 +746,8 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
|
||||
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;
|
||||
// Tool peers ask here to become the desktop (capture_desktop on an existing connection, #99).
|
||||
let (desktop_requests, mut desktop_requested) = mpsc::unbounded_channel::<String>();
|
||||
|
||||
loop {
|
||||
if *shutdown.borrow() {
|
||||
@@ -821,6 +832,7 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
|
||||
_ = 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),
|
||||
Some(ice) = desktop_requested.recv() => Some(SignalingEvent::DesktopRequested(ice)),
|
||||
event = rx_event.recv() => event,
|
||||
};
|
||||
let Some(evt) = event else { break; };
|
||||
@@ -855,8 +867,8 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
|
||||
::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()?; } }
|
||||
for id in ended { if let Some(s) = sessions.remove(&id) { desktop_ended |= s.role.is_desktop(); close_peer(&s.pc).await; } }
|
||||
if desktop_ended && !sessions.values().any(|s| s.role.is_desktop()) { if let Some(s) = assist.as_mut() { s.session_ended()?; } }
|
||||
if let Some(s) = assist.as_mut() { s.tick()?; }
|
||||
}
|
||||
SignalingEvent::Connected => {
|
||||
@@ -943,8 +955,8 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
|
||||
::webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Closed |
|
||||
::webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Failed)).map(|(k, _)| k.clone()).collect();
|
||||
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()?; } }
|
||||
for k in ended { if let Some(old) = sessions.remove(&k) { desktop_ended |= old.role.is_desktop(); close_peer(&old.pc).await; } }
|
||||
if desktop_ended && !sessions.values().any(|s| s.role.is_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" {
|
||||
@@ -958,8 +970,10 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
|
||||
if desktop { video::warm_auto_probe(); video::check_ffmpeg_usable(); }
|
||||
// Replace only this ICE id or a previous desktop, never tool peers.
|
||||
for id in superseded_peer_ids(&sessions, &ice, desktop) {
|
||||
if let Some(old) = sessions.remove(&id) { let _ = old.pc.close().await; }
|
||||
if let Some(old) = sessions.remove(&id) { close_peer(&old.pc).await; }
|
||||
}
|
||||
// A tool peer promoted by capture_desktop stops its desktop but keeps its tools.
|
||||
if desktop { for old in sessions.values() { old.demote(); } }
|
||||
if desktop && desktop_backend::headless() {
|
||||
log::info!("Desktop connection ignored: headless mode (terminal, file transfer and port mapping remain available)");
|
||||
continue;
|
||||
@@ -981,7 +995,12 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
|
||||
if desktop { if let Some(portal) = &portal_desktop { portal.begin(); } }
|
||||
match WebRtcSession::new(session_capture, input.clone(), signaling_client.clone(), desktop, transport,
|
||||
if desktop { portal_desktop.clone() } else { None }).await {
|
||||
Ok(sess) => {
|
||||
Ok(mut sess) => {
|
||||
sess.controller_mac = controller_mac;
|
||||
if !desktop {
|
||||
let (requests, peer) = (desktop_requests.clone(), ice.clone());
|
||||
sess.on_desktop_request(move || { let _ = requests.send(peer.clone()); });
|
||||
}
|
||||
// Register before awaiting SDP: shutdown can always close
|
||||
// a peer whose answer may already be queued to the viewer.
|
||||
sessions.insert(ice.clone(), sess);
|
||||
@@ -1004,7 +1023,7 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
|
||||
}
|
||||
Err(_) => {
|
||||
log::error!("Failed to handle offer (details omitted)");
|
||||
if let Some(session) = sessions.remove(&ice) { let _ = session.pc.close().await; }
|
||||
if let Some(session) = sessions.remove(&ice) { close_peer(&session.pc).await; }
|
||||
}
|
||||
}},
|
||||
Err(_) => log::error!("Create peer failed (details omitted)"),
|
||||
@@ -1026,13 +1045,50 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
|
||||
let mut desktop_ended = false;
|
||||
for k in targets {
|
||||
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); }
|
||||
desktop_ended |= old.role.is_desktop();
|
||||
close_peer(&old.pc).await;
|
||||
}
|
||||
}
|
||||
if desktop_ended && !sessions.values().any(|s| s.desktop) { if let Some(s) = assist.as_mut() { s.session_ended()?; } }
|
||||
if desktop_ended && !sessions.values().any(|s| s.role.is_desktop()) { if let Some(s) = assist.as_mut() { s.session_ended()?; } }
|
||||
if let Some(s) = assist.as_mut() { s.tick()?; }
|
||||
}
|
||||
SignalingEvent::DesktopRequested(ice) => {
|
||||
let mut sessions = webrtc_sessions.lock().await;
|
||||
let Some(peer) = sessions.get(&ice).filter(|peer| !peer.role.is_desktop()) else { continue; };
|
||||
if desktop_backend::headless() {
|
||||
log::info!("Desktop request on a tool connection ignored: headless mode");
|
||||
continue;
|
||||
}
|
||||
if portal_desktop.is_some() {
|
||||
log::info!("Desktop request on a tool connection not supported with the Portal backend; open the desktop before port mapping");
|
||||
continue;
|
||||
}
|
||||
let controller_mac = peer.controller_mac;
|
||||
let current = peer.desktop_capture();
|
||||
// Same recovery as a desktop offer: the display may have come up or been resized after this peer.
|
||||
let resized = current.init_error.is_none() && current.backend == desktop_backend::DesktopBackend::X11
|
||||
&& tokio::task::spawn_blocking(crate::capture::x11_size).await.ok().flatten().is_some_and(|size| size != (current.width, current.height));
|
||||
if resized { log::info!("Desktop geometry changed since this connection opened; reinitializing capture"); }
|
||||
let screen = if current.init_error.is_some() || resized {
|
||||
match tokio::task::spawn_blocking(ScreenCapture::new).await {
|
||||
Ok(Ok(screen)) => { capture = Arc::new(screen); Some(capture.clone()) },
|
||||
Ok(Err(error)) => { log::info!("Desktop request on a tool connection ignored: {error}"); continue; },
|
||||
Err(_) => { log::warn!("Desktop initialization worker failed"); continue; },
|
||||
}
|
||||
} else { None };
|
||||
video::warm_auto_probe(); video::check_ffmpeg_usable();
|
||||
input.set_mac_controller(controller_mac);
|
||||
input.bind_portal(screen.as_ref().map_or_else(|| sessions[&ice].capture.portal.clone(), |screen| screen.portal.clone()));
|
||||
// One desktop at a time, as for a new desktop offer. Peers offered as desktops
|
||||
// close; a promoted tool peer only stops its desktop and keeps its tools.
|
||||
let superseded: Vec<String> = sessions.iter().filter(|(id, old)| id.as_str() != ice && old.role.is_desktop())
|
||||
.map(|(id, _)| id.clone()).collect();
|
||||
for id in superseded {
|
||||
if sessions[&id].desktop { if let Some(old) = sessions.remove(&id) { close_peer(&old.pc).await; } }
|
||||
else { sessions[&id].demote(); }
|
||||
}
|
||||
if let Some(peer) = sessions.get(&ice) { peer.promote(screen); }
|
||||
}
|
||||
SignalingEvent::Disconnected => {
|
||||
capture_types.clear();
|
||||
next_capture_type=None;
|
||||
|
||||
+193
-41
@@ -3,12 +3,22 @@
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::sync::mpsc;
|
||||
use tokio_tungstenite::connect_async;
|
||||
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
|
||||
/// A peer that stops reading must not park the heartbeat loop on a full queue.
|
||||
/// Kept below the Engine.IO window so the protocol deadline still fires.
|
||||
const SEND_STALL_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
// DNS/TCP/TLS/WebSocket upgrade must finish in bounded time: after a network
|
||||
// change the handshake can stall forever and the reconnect loop never retries.
|
||||
const CONNECT_TIMEOUT: Duration = Duration::from_secs(20);
|
||||
/// How often the watchdog checks the last inbound frame.
|
||||
const WATCHDOG_TICK: Duration = Duration::from_secs(1);
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SoacMessage {
|
||||
#[serde(rename = "type")]
|
||||
@@ -46,6 +56,8 @@ pub enum SignalingEvent {
|
||||
Released(Value),
|
||||
Left(Value),
|
||||
Disconnected,
|
||||
/// capture_desktop on this tool peer (ice_id); raised by the peer, not by signaling.
|
||||
DesktopRequested(String),
|
||||
}
|
||||
|
||||
pub struct SignalingClient {
|
||||
@@ -68,6 +80,11 @@ fn refreshed_key(ack:&Value)->Option<String> {
|
||||
(!key.is_empty() && key.len()<=16384).then(||key.to_owned())
|
||||
}
|
||||
|
||||
/// Deliver an event without letting a stalled consumer hide a dead socket.
|
||||
async fn send_event(sender: &mpsc::Sender<SignalingEvent>, event: SignalingEvent) -> bool {
|
||||
matches!(tokio::time::timeout(SEND_STALL_TIMEOUT, sender.send(event)).await, Ok(Ok(())))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod refresh_tests {
|
||||
use super::refreshed_key;
|
||||
@@ -126,6 +143,95 @@ mod refresh_tests {
|
||||
assert!(disconnected.is_ok(),"a socket without pings must be reported dead");
|
||||
drop(hold_tx);
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn blocked_event_consumer_does_not_hide_a_dead_socket() {
|
||||
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 (closed_tx,closed_rx)=tokio::sync::oneshot::channel::<()>();
|
||||
tokio::spawn(async move {
|
||||
let (tcp,_)=listener.accept().await.unwrap();
|
||||
let socket=tokio_tungstenite::accept_async(tcp).await.unwrap();
|
||||
let (mut sink,mut stream)=socket.split();
|
||||
// Shortest accepted window (clamped to 5 s); never ping, only events.
|
||||
sink.send(Message::Text(r#"0{"pingInterval":1000,"pingTimeout":1000}"#.into())).await.unwrap();
|
||||
let _=stream.next().await;
|
||||
sink.send(Message::Text("40{}".into())).await.unwrap();
|
||||
loop {
|
||||
tokio::select! {
|
||||
r=stream.next()=>match r {
|
||||
None|Some(Err(_))|Some(Ok(Message::Close(_)))=>{let _=closed_tx.send(());return;}
|
||||
_=>{}
|
||||
},
|
||||
_=tokio::time::sleep(std::time::Duration::from_millis(50))=>{
|
||||
if sink.send(Message::Text(r#"42["bmsg_push",{}]"#.into())).await.is_err() {
|
||||
let _=closed_tx.send(());return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
// Capacity 1 and no reader: the first event fills the queue, so the
|
||||
// receive loop would park inside `send().await` without the watchdog.
|
||||
let (sender,_events)=tokio::sync::mpsc::channel(1);
|
||||
let _client=super::SignalingClient::connect(&format!("ws://{address}"),"synthetic-only",sender).await.unwrap();
|
||||
let closed=tokio::time::timeout(std::time::Duration::from_secs(15),closed_rx).await;
|
||||
assert!(closed.is_ok(),"a blocked consumer must not keep a dead signaling socket open");
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn stalled_handshake_times_out() {
|
||||
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
|
||||
// Accepts TCP but never answers the upgrade, like a stalled path after a network change.
|
||||
let listener=tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let address=listener.local_addr().unwrap();
|
||||
tokio::spawn(async move {
|
||||
let (_tcp,_)=listener.accept().await.unwrap();
|
||||
tokio::time::sleep(std::time::Duration::from_secs(30)).await;
|
||||
});
|
||||
let req=format!("ws://{address}/socket.io/").into_client_request().unwrap();
|
||||
let result=tokio::time::timeout(std::time::Duration::from_secs(5),super::open_socket(req,std::time::Duration::from_secs(1))).await;
|
||||
assert!(matches!(result,Ok(Err(_))),"a stalled handshake must fail within the connect timeout");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn stalled_event_delivery_still_reports_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();
|
||||
tokio::spawn(async move {
|
||||
let (tcp,_)=listener.accept().await.unwrap();
|
||||
let socket=tokio_tungstenite::accept_async(tcp).await.unwrap();
|
||||
let (mut sink,mut stream)=socket.split();
|
||||
// A long window: the send stall must fire before the heartbeat deadline.
|
||||
sink.send(Message::Text(r#"0{"pingInterval":30000,"pingTimeout":30000}"#.into())).await.unwrap();
|
||||
let _=stream.next().await;
|
||||
sink.send(Message::Text("40{}".into())).await.unwrap();
|
||||
loop {
|
||||
tokio::select! {
|
||||
r=stream.next()=>{ if matches!(r,None|Some(Err(_))|Some(Ok(Message::Close(_)))){return} }
|
||||
_=tokio::time::sleep(std::time::Duration::from_millis(50))=>{
|
||||
if sink.send(Message::Text(r#"42["bmsg_push",{}]"#.into())).await.is_err(){return}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
let (sender,mut events)=tokio::sync::mpsc::channel(1);
|
||||
let _client=super::SignalingClient::connect(&format!("ws://{address}"),"synthetic-only",sender).await.unwrap();
|
||||
// Let SEND_STALL_TIMEOUT expire while nothing drains the queue.
|
||||
tokio::time::sleep(std::time::Duration::from_secs(12)).await;
|
||||
assert!(matches!(events.recv().await,Some(super::SignalingEvent::Connected)));
|
||||
let disconnected=tokio::time::timeout(std::time::Duration::from_secs(5),events.recv()).await;
|
||||
assert!(matches!(disconnected,Ok(Some(super::SignalingEvent::Disconnected))),"a stalled delivery must still report Disconnected");
|
||||
}
|
||||
}
|
||||
|
||||
async fn open_socket(
|
||||
req: tokio_tungstenite::tungstenite::handshake::client::Request,
|
||||
limit: Duration,
|
||||
) -> Result<crate::ws_transport::Socket, String> {
|
||||
crate::ws_transport::connect(req, limit).await
|
||||
}
|
||||
|
||||
impl SignalingClient {
|
||||
@@ -149,24 +255,30 @@ impl SignalingClient {
|
||||
room_token.parse().map_err(|_| "Invalid token header")?,
|
||||
);
|
||||
|
||||
let (ws_stream, _) = connect_async(req)
|
||||
.await
|
||||
.map_err(|e| format!("WS connect failed: {}", e))?;
|
||||
let ws_stream = open_socket(req, CONNECT_TIMEOUT).await?;
|
||||
|
||||
log::info!("Connected to Engine.IO / Socket.IO");
|
||||
let (mut write, mut read) = ws_stream.split();
|
||||
let (tx_outgoing, mut rx_outgoing) = mpsc::channel::<String>(32);
|
||||
let started = Instant::now();
|
||||
let last_rx = Arc::new(AtomicU64::new(0));
|
||||
let ping_window_ms = Arc::new(AtomicU64::new(45_000));
|
||||
let receiver_done = Arc::new(AtomicBool::new(false));
|
||||
|
||||
// 发送协程
|
||||
let writer=tokio::spawn(async move {
|
||||
while let Some(msg) = rx_outgoing.recv().await {
|
||||
if let Err(e) = write.send(Message::Text(msg)).await {
|
||||
let _ = e;
|
||||
log::warn!("WS send failed (details omitted)");
|
||||
break;
|
||||
match tokio::time::timeout(SEND_STALL_TIMEOUT, write.send(Message::Text(msg))).await {
|
||||
Ok(Ok(())) => {}
|
||||
Ok(Err(_)) | Err(_) => {
|
||||
// A peer that stopped reading must not park every producer.
|
||||
log::warn!("WS send failed or stalled (details omitted)");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
let writer_abort = writer.abort_handle();
|
||||
|
||||
let tx_out_clone = tx_outgoing.clone();
|
||||
static GENERATION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
|
||||
@@ -175,9 +287,12 @@ impl SignalingClient {
|
||||
let refreshed=reconnect_key.clone();
|
||||
|
||||
// 接收与状态机协程
|
||||
tokio::spawn(async move {
|
||||
let watchdog_events = event_sender.clone();
|
||||
let watchdog_last_rx = last_rx.clone();
|
||||
let watchdog_ping_window_ms = ping_window_ms.clone();
|
||||
let watchdog_done = receiver_done.clone();
|
||||
let receiver = tokio::spawn(async move {
|
||||
let mut req_counter = 10;
|
||||
let started=std::time::Instant::now();
|
||||
let mut last_ping=started;
|
||||
let mut pings=0u64;
|
||||
let mut pong_queued=0u64;
|
||||
@@ -200,7 +315,7 @@ impl SignalingClient {
|
||||
_=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;
|
||||
let _ = send_event(&event_sender, SignalingEvent::Disconnected).await;
|
||||
break;
|
||||
},
|
||||
// Official ControlledSession RefreshSignalTokenLoop: first
|
||||
@@ -211,8 +326,8 @@ impl SignalingClient {
|
||||
refresh_acks.push_back(req_counter);
|
||||
if refresh_acks.len()>16 { refresh_acks.pop_front(); }
|
||||
let packet=format!("42{}[\"refresh_reconnect_key\"]",req_counter);
|
||||
if tx_out_clone.send(packet).await.is_err() {
|
||||
let _=event_sender.send(SignalingEvent::Disconnected).await;break;
|
||||
if tx_out_clone.try_send(packet).is_err() {
|
||||
let _=send_event(&event_sender, SignalingEvent::Disconnected).await;break;
|
||||
}
|
||||
next_refresh=tokio::time::Instant::now()+std::time::Duration::from_secs(30);
|
||||
log::info!("Signaling reconnect-key refresh requested: generation={} age_s={}",generation,started.elapsed().as_secs());
|
||||
@@ -220,9 +335,14 @@ impl SignalingClient {
|
||||
},
|
||||
msg=read.next()=>match msg {
|
||||
Some(msg)=>msg,
|
||||
None=>{let _=event_sender.send(SignalingEvent::Disconnected).await;break;}
|
||||
None=>{let _=send_event(&event_sender, SignalingEvent::Disconnected).await;break;}
|
||||
},
|
||||
};
|
||||
// Any inbound frame proves the socket is alive. A reader parked on
|
||||
// event delivery stops updating this, which the watchdog catches.
|
||||
if msg_res.is_ok() {
|
||||
last_rx.store(started.elapsed().as_millis() as u64, Ordering::Relaxed);
|
||||
}
|
||||
match msg_res {
|
||||
Ok(Message::Text(txt)) => {
|
||||
// Engine.IO 报文处理
|
||||
@@ -231,16 +351,20 @@ impl SignalingClient {
|
||||
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_window_ms.store(ping_window.as_millis() as u64, Ordering::Relaxed);
|
||||
ping_deadline=tokio::time::Instant::now()+ping_window;
|
||||
}
|
||||
}
|
||||
// Open packet -> 发送 Socket.IO connect
|
||||
let _ = tx_out_clone.send("40".into()).await;
|
||||
if tx_out_clone.try_send("40".into()).is_err() {
|
||||
let _=send_event(&event_sender, SignalingEvent::Disconnected).await;break;
|
||||
}
|
||||
} else if txt == "2" {
|
||||
// Ping -> 回复 Pong
|
||||
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.try_send("3".into()).is_ok() { pong_queued+=1; }
|
||||
else { let _=send_event(&event_sender, SignalingEvent::Disconnected).await;break; }
|
||||
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);
|
||||
}
|
||||
@@ -250,8 +374,10 @@ impl SignalingClient {
|
||||
// Socket.IO connected -> 发送 room_info 请求
|
||||
req_counter += 1;
|
||||
let room_req = format!("42{}[\"room_info\"]", req_counter);
|
||||
let _ = tx_out_clone.send(room_req).await;
|
||||
let _ = event_sender.send(SignalingEvent::Connected).await;
|
||||
if tx_out_clone.try_send(room_req).is_err() {
|
||||
let _=send_event(&event_sender, SignalingEvent::Disconnected).await;break;
|
||||
}
|
||||
if !send_event(&event_sender, SignalingEvent::Connected).await { break; }
|
||||
} else if txt.starts_with("43") {
|
||||
// Socket.IO ack 回调
|
||||
let begin=txt.find('[').unwrap_or(2);
|
||||
@@ -270,7 +396,7 @@ impl SignalingClient {
|
||||
next_refresh=tokio::time::Instant::now()+std::time::Duration::from_secs(300);
|
||||
}
|
||||
log::info!("Signaling reconnect-key refresh acknowledged: generation={} age_s={} success={}",generation,started.elapsed().as_secs(),success);
|
||||
} else { let _ = event_sender.send(SignalingEvent::RoomInfoAck(v)).await; }
|
||||
} else if !send_event(&event_sender, SignalingEvent::RoomInfoAck(v)).await { break; }
|
||||
}
|
||||
} else if txt.starts_with("45") || txt.starts_with("46") {
|
||||
// Socket.IO binary event/ack: header text, attachments follow as binary frames.
|
||||
@@ -294,14 +420,10 @@ impl SignalingClient {
|
||||
|
||||
match event_name {
|
||||
"bmsg_push" => {
|
||||
let _ = event_sender
|
||||
.send(SignalingEvent::DeviceInfoChanged(data.clone()))
|
||||
.await;
|
||||
if !send_event(&event_sender, SignalingEvent::DeviceInfoChanged(data.clone())).await { break; }
|
||||
}
|
||||
"be-controlled" => {
|
||||
let _ = event_sender
|
||||
.send(SignalingEvent::BeControlled(data.clone()))
|
||||
.await;
|
||||
if !send_event(&event_sender, SignalingEvent::BeControlled(data.clone())).await { break; }
|
||||
}
|
||||
"soac" => {
|
||||
// Offers only: structure (keys, types, lengths), never SDP text or ids.
|
||||
@@ -324,9 +446,7 @@ impl SignalingClient {
|
||||
if soac.client_id.is_none() {
|
||||
soac.client_id = client_id_opt;
|
||||
}
|
||||
let _ = event_sender
|
||||
.send(SignalingEvent::Soac(soac))
|
||||
.await;
|
||||
if !send_event(&event_sender, SignalingEvent::Soac(soac)).await { break; }
|
||||
}
|
||||
Err(e) => {
|
||||
let _ = e;
|
||||
@@ -336,20 +456,17 @@ impl SignalingClient {
|
||||
}
|
||||
"released" => {
|
||||
log::info!("Cloud peer event: generation={} event=released age_s={} pings={} last_ping_gap_s={} ice_id_present={}",generation,started.elapsed().as_secs(),pings,last_ping.elapsed().as_secs(),data.get("ice_id").is_some());
|
||||
let _ = event_sender
|
||||
.send(SignalingEvent::Released(data.clone()))
|
||||
.await;
|
||||
if !send_event(&event_sender, SignalingEvent::Released(data.clone())).await { break; }
|
||||
}
|
||||
"left" | "leave" => {
|
||||
log::info!("Cloud peer event: generation={} event={} age_s={} pings={} last_ping_gap_s={} ice_id_present={}",generation,event_name,started.elapsed().as_secs(),pings,last_ping.elapsed().as_secs(),data.get("ice_id").is_some());
|
||||
let ice_id = data.get("ice_id").and_then(|v| v.as_str()).unwrap_or("");
|
||||
if ice_id.is_empty() {
|
||||
log::info!("Cloud room leave shape: {}", crate::protocol::json_shape(data, 3));
|
||||
log::warn!("Cloud room leave without peer identifier; renewing signaling (expiry cause unknown)");
|
||||
let _ = event_sender.send(SignalingEvent::Disconnected).await;
|
||||
if !send_event(&event_sender, SignalingEvent::Disconnected).await { break; }
|
||||
} else {
|
||||
let _ = event_sender
|
||||
.send(SignalingEvent::Left(data.clone()))
|
||||
.await;
|
||||
if !send_event(&event_sender, SignalingEvent::Left(data.clone())).await { break; }
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
@@ -399,28 +516,60 @@ impl SignalingClient {
|
||||
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;
|
||||
if !send_event(&event_sender, SignalingEvent::ConnectRequest { capture_type, ice_id, transport:crate::session_ice::SessionIce::from_header(&header), controller_mac }).await { break; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Message::Close(frame)) => {
|
||||
log::warn!("Signaling socket closed: generation={} age_s={} close_code={:?} pings={} last_ping_gap_s={}",generation,started.elapsed().as_secs(),frame.map(|f|u16::from(f.code)),pings,last_ping.elapsed().as_secs());
|
||||
let _ = event_sender.send(SignalingEvent::Disconnected).await;
|
||||
let _ = send_event(&event_sender, SignalingEvent::Disconnected).await;
|
||||
break;
|
||||
}
|
||||
Err(_) => {
|
||||
log::warn!("Signaling socket receive failed: 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;
|
||||
let _ = send_event(&event_sender, SignalingEvent::Disconnected).await;
|
||||
break;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
// A stalled event queue or a writer that already stopped can end this
|
||||
// loop without a delivered Disconnected. Hand one off from a detached
|
||||
// task so the main loop always learns the room is dead; duplicates are
|
||||
// harmless because it breaks on the first one.
|
||||
let events = event_sender.clone();
|
||||
tokio::spawn(async move { let _ = events.send(SignalingEvent::Disconnected).await; });
|
||||
receiver_done.store(true, Ordering::Relaxed);
|
||||
log::info!("Signaling receiver stopped: generation={} age_s={}",generation,started.elapsed().as_secs());
|
||||
// Live peer callbacks can retain the old SignalingClient sender.
|
||||
// Explicitly drop its write half too when this room stops reading.
|
||||
writer.abort();
|
||||
});
|
||||
let receiver_abort = receiver.abort_handle();
|
||||
|
||||
// Independent of the receive loop: a consumer that stops draining events
|
||||
// parks the loop inside an `.await`, where the ping deadline can no longer
|
||||
// be observed. The watchdog closes the socket anyway.
|
||||
tokio::spawn(async move {
|
||||
let mut tick = tokio::time::interval(WATCHDOG_TICK);
|
||||
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
|
||||
loop {
|
||||
tick.tick().await;
|
||||
if watchdog_done.load(Ordering::Relaxed) || watchdog_events.is_closed() { return; }
|
||||
let window_ms = watchdog_ping_window_ms.load(Ordering::Relaxed);
|
||||
let idle_ms = (started.elapsed().as_millis() as u64)
|
||||
.saturating_sub(watchdog_last_rx.load(Ordering::Relaxed));
|
||||
if idle_ms <= window_ms { continue; }
|
||||
log::warn!("Signaling heartbeat lost (watchdog): idle_s={} window_ms={}", idle_ms/1000, window_ms);
|
||||
// Deliver from a detached task: the receiver may be the one parked
|
||||
// on a full queue, and the main loop still needs this event.
|
||||
let events = watchdog_events.clone();
|
||||
tokio::spawn(async move { let _ = events.send(SignalingEvent::Disconnected).await; });
|
||||
receiver_abort.abort();
|
||||
writer_abort.abort();
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
Ok(Self { tx_outgoing, _reconnect_key:reconnect_key })
|
||||
}
|
||||
@@ -428,7 +577,10 @@ impl SignalingClient {
|
||||
pub async fn emit_event(&self, event: &str, payload: serde_json::Value) -> Result<(), String> {
|
||||
let packet = format!("42{}", serde_json::json!([event, payload]));
|
||||
log::info!("Emitting signaling event: {}", event);
|
||||
self.tx_outgoing.send(packet).await.map_err(|e| e.to_string())
|
||||
tokio::time::timeout(SEND_STALL_TIMEOUT, self.tx_outgoing.send(packet))
|
||||
.await
|
||||
.map_err(|_| "signaling send stalled".to_string())?
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// 发送 SOAC 消息(Answer 或 Candidate)
|
||||
@@ -443,9 +595,9 @@ impl SignalingClient {
|
||||
};
|
||||
let msg = format!("42[\"soac\",{}]", payload);
|
||||
log::info!("Emitting soac packet: type={}", soac.msg_type);
|
||||
self.tx_outgoing
|
||||
.send(msg)
|
||||
tokio::time::timeout(SEND_STALL_TIMEOUT, self.tx_outgoing.send(msg))
|
||||
.await
|
||||
.map_err(|_| "Send soac timed out".to_string())?
|
||||
.map_err(|e| format!("Send soac error: {}", e))
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,8 @@ pub const FF_SIMPLE_ACTION: u8 = 2;
|
||||
pub const FF_SYSTEM_METRICS: u8 = 2;
|
||||
|
||||
pub const SHOW_DESKTOP: u64 = 2;
|
||||
/// Opens the desktop on an existing connection (the viewer's port mapping reuses its peer, #99).
|
||||
pub const CAPTURE_DESKTOP: u64 = 4;
|
||||
pub const REBOOT_SYSTEM: u64 = 9;
|
||||
pub const LOCK_SYSTEM: u64 = 10;
|
||||
pub const SHUTDOWN_SYSTEM: u64 = 12;
|
||||
|
||||
+18
-17
@@ -780,19 +780,20 @@ async fn read_inprocess(
|
||||
/// whole IDR frame, otherwise large desktops get near-grey keyframes that sharpen over the GOP (#8).
|
||||
/// Half a second of bitrate still starved repeated 0.5 s-GOP keyframes at 3296x2036/2M; one second did not.
|
||||
fn buffer_kbits(max_kbps: u32, encoder: Encoder) -> u32 {
|
||||
if encoder == Encoder::Cpu { max_kbps.clamp(500, 8000) }
|
||||
else if encoder == Encoder::Vaapi {
|
||||
let requested = std::env::var("UU_VAAPI_BUFFER_MS").ok();
|
||||
vaapi_buffer_kbits(max_kbps, requested.as_deref()).unwrap_or((max_kbps / 8).clamp(100, 500))
|
||||
}
|
||||
else { (max_kbps / 8).clamp(100, 500) }
|
||||
let env = match encoder {
|
||||
Encoder::Cpu => return max_kbps.clamp(500, 8000),
|
||||
Encoder::Vaapi => "UU_VAAPI_BUFFER_MS",
|
||||
Encoder::Nvenc => "UU_NVENC_BUFFER_MS",
|
||||
};
|
||||
let requested = std::env::var(env).ok();
|
||||
buffer_ms_kbits(max_kbps, requested.as_deref()).unwrap_or((max_kbps / 8).clamp(100, 500))
|
||||
}
|
||||
|
||||
/// Opt-in VAAPI VBV buffer: `码率上限 × 毫秒 / 1000` for `UU_VAAPI_BUFFER_MS` in 10-1000 ms.
|
||||
/// The 100-500 kbit default is sized for NVENC; VAAPI quantizes a whole desktop keyframe when
|
||||
/// the buffer is that small, which reads as a blur that sharpens over each GOP (#42).
|
||||
/// Opt-in hardware VBV buffer: `码率上限 × 毫秒 / 1000` for `UU_VAAPI_BUFFER_MS` /
|
||||
/// `UU_NVENC_BUFFER_MS` in 10-1000 ms. With the 100-500 kbit default the encoder quantizes a
|
||||
/// whole desktop keyframe, which reads as a blur that sharpens over each GOP (#42 VAAPI, #103 NVENC).
|
||||
/// Unset, out of range or unparsable values keep the default buffer.
|
||||
fn vaapi_buffer_kbits(max_kbps: u32, value: Option<&str>) -> Option<u32> {
|
||||
fn buffer_ms_kbits(max_kbps: u32, value: Option<&str>) -> Option<u32> {
|
||||
let ms = value?.trim().parse::<u32>().ok().filter(|ms| (10..=1000).contains(ms))?;
|
||||
Some((max_kbps.saturating_mul(ms) / 1000).max(1))
|
||||
}
|
||||
@@ -1570,17 +1571,17 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vaapi_buffer_env_is_opt_in_and_bounded() {
|
||||
assert_eq!(vaapi_buffer_kbits(30_000, None), None);
|
||||
assert_eq!(vaapi_buffer_kbits(30_000, Some("250")), Some(7500));
|
||||
assert_eq!(vaapi_buffer_kbits(30_000, Some(" 10 ")), Some(300));
|
||||
assert_eq!(vaapi_buffer_kbits(30_000, Some("1000")), Some(30_000));
|
||||
fn hw_buffer_env_is_opt_in_and_bounded() {
|
||||
assert_eq!(buffer_ms_kbits(30_000, None), None);
|
||||
assert_eq!(buffer_ms_kbits(30_000, Some("250")), Some(7500));
|
||||
assert_eq!(buffer_ms_kbits(30_000, Some(" 10 ")), Some(300));
|
||||
assert_eq!(buffer_ms_kbits(30_000, Some("1000")), Some(30_000));
|
||||
// Out of the documented range, empty or unparsable keeps the default buffer.
|
||||
for value in ["", "9", "1001", "0", "abc", "-250", "250ms"] {
|
||||
assert_eq!(vaapi_buffer_kbits(30_000, Some(value)), None, "{value}");
|
||||
assert_eq!(buffer_ms_kbits(30_000, Some(value)), None, "{value}");
|
||||
}
|
||||
// A tiny cap must not produce a zero-sized buffer.
|
||||
assert_eq!(vaapi_buffer_kbits(1, Some("10")), Some(1));
|
||||
assert_eq!(buffer_ms_kbits(1, Some("10")), Some(1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -37,7 +37,7 @@ impl Decoder for Ffmpeg {
|
||||
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});
|
||||
frames.push(Frame{width:w,height:h,rgb:data,content:None});
|
||||
} Ok(frames)
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,16 @@
|
||||
//! H.264/H.265 decoding through an external `ffmpeg` process, like the host's encoder pipeline.
|
||||
//! Needs only the ffmpeg CLI, not FFmpeg development libraries.
|
||||
//! Output is scaled into a fixed letterboxed size so every frame has a known byte length.
|
||||
//! Output is scaled into a fixed letterboxed canvas so every frame has a known byte length; the
|
||||
//! source size from ffmpeg's stream line tells the window which sub-rectangle is picture content.
|
||||
use super::desktop::{Decoder,Frame};
|
||||
use super::media::Codec;
|
||||
use std::io::{Read,Write};
|
||||
use std::io::{BufRead,Read,Write};
|
||||
use std::process::{Child,ChildStdin,Command,Stdio};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicU64,Ordering};
|
||||
use std::sync::mpsc::{Receiver,TryRecvError};
|
||||
|
||||
pub struct Cli { child:Child, input:Option<ChildStdin>, codec:Codec, frames:Receiver<Vec<u8>>, width:u32, height:u32 }
|
||||
pub struct Cli { child:Child, input:Option<ChildStdin>, codec:Codec, frames:Receiver<Vec<u8>>, width:u32, height:u32, source:Arc<AtomicU64> }
|
||||
|
||||
impl Cli {
|
||||
/// `UU_VIEWER_SIZE=WxH` picks the decoded size (default 1920x1080); `UU_VIEWER_FFMPEG` the binary.
|
||||
@@ -23,16 +26,27 @@ impl Cli {
|
||||
let filter=format!("scale={width}:{height}:force_original_aspect_ratio=decrease,pad={width}:{height}:(ow-iw)/2:(oh-ih)/2");
|
||||
let passthrough=passthrough_args(&ffmpeg);
|
||||
let mut child=Command::new(&ffmpeg)
|
||||
.args(["-hide_banner","-loglevel","error","-nostdin","-threads","1","-probesize","32","-analyzeduration","0",
|
||||
.args(["-hide_banner","-loglevel","info","-nostats","-nostdin","-threads","1","-probesize","32","-analyzeduration","0",
|
||||
"-fflags","nobuffer","-flags","low_delay","-f",codec.demuxer(),"-i","pipe:0","-an","-vf",&filter,
|
||||
"-pix_fmt","rgb24",
|
||||
// Raw H.264/H.265 carries no timestamps; constant-rate output dropped almost every frame of
|
||||
// the official Mac host's stream (270 → 2). Pass frames through unchanged.
|
||||
passthrough.0,passthrough.1,"-f","rawvideo","pipe:1"])
|
||||
.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::null())
|
||||
.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::piped())
|
||||
.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")?;
|
||||
// Source picture size from the one-time stream line; 0 until parsed. Only the numbers are kept.
|
||||
let source=Arc::new(AtomicU64::new(0));
|
||||
if let Some(stderr)=child.stderr.take() {
|
||||
let source=source.clone();
|
||||
let _=std::thread::Builder::new().name("uu-viewer-ffmpeg-info".into()).spawn(move||{
|
||||
for line in std::io::BufReader::new(stderr).lines().map_while(Result::ok) {
|
||||
if source.load(Ordering::Relaxed)!=0 {continue}
|
||||
if let Some((w,h))=parse_video_size(&line) {source.store((u64::from(w)<<32)|u64::from(h),Ordering::Relaxed);}
|
||||
}
|
||||
});
|
||||
}
|
||||
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);
|
||||
@@ -43,10 +57,23 @@ impl Cli {
|
||||
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:Some(input),codec,frames,width,height})
|
||||
Ok(Self{child,input:Some(input),codec,frames,width,height,source})
|
||||
}
|
||||
}
|
||||
|
||||
/// `30000/1001`-style or display strings are irrelevant here: the first WxH on a `Video:` line is the
|
||||
/// coded picture size, which is the aspect the scale+pad filter letterboxes into the canvas.
|
||||
fn parse_video_size(line:&str)->Option<(u32,u32)> {
|
||||
let rest=line.split_once("Video:")?.1;
|
||||
rest.split(|c:char|c==','||c==' '||c=='('||c==')'||c=='['||c==']').find_map(|token|{
|
||||
let token=token.trim_matches(|c:char|!c.is_ascii_digit()&&c!='x');
|
||||
let (w,h)=token.split_once('x')?;
|
||||
if w.is_empty()||h.is_empty()||!w.bytes().all(|b|b.is_ascii_digit())||!h.bytes().all(|b|b.is_ascii_digit()) {return None}
|
||||
let (w,h)=(w.parse::<u32>().ok()?,h.parse::<u32>().ok()?);
|
||||
((16..=16384).contains(&w)&&(16..=16384).contains(&h)).then_some((w,h))
|
||||
})
|
||||
}
|
||||
|
||||
/// FFmpeg ≥ 5.1 has `-fps_mode`; FFmpeg 8+ removed `-vsync`, and Ubuntu 22.04's 4.4 lacks `-fps_mode`.
|
||||
/// Probe once per binary with a one-frame null run instead of parsing version strings.
|
||||
fn passthrough_args(ffmpeg:&str)->(&'static str,&'static str) {
|
||||
@@ -75,7 +102,11 @@ impl Decoder for Cli {
|
||||
let mut out=Vec::new();
|
||||
loop {
|
||||
match self.frames.try_recv() {
|
||||
Ok(rgb)=>out.push(Frame{width:self.width,height:self.height,rgb}),
|
||||
Ok(rgb)=>{
|
||||
let source=self.source.load(Ordering::Relaxed);
|
||||
let content=(source!=0).then(||((source>>32) as u32,source as u32));
|
||||
out.push(Frame{width:self.width,height:self.height,rgb,content})
|
||||
},
|
||||
Err(TryRecvError::Empty)=>break,
|
||||
Err(TryRecvError::Disconnected)=>return Err("ffmpeg decoder exited".into()),
|
||||
}
|
||||
@@ -115,7 +146,7 @@ impl Drop for Cli { fn drop(&mut self) { let _=self.child.kill(); let _=self.chi
|
||||
// Exercise the same streaming process and RGB conversion as SDL, then drain at EOF.
|
||||
let mut frames=cli.decode(data).unwrap();drop(cli.input.take());
|
||||
loop {match cli.frames.recv_timeout(std::time::Duration::from_secs(5)) {
|
||||
Ok(rgb)=>frames.push(Frame{width:32,height:32,rgb}),
|
||||
Ok(rgb)=>frames.push(Frame{width:32,height:32,rgb,content:None}),
|
||||
Err(std::sync::mpsc::RecvTimeoutError::Disconnected)=>break,
|
||||
Err(e)=>panic!("{codec:?} decoder did not finish: {e}"),
|
||||
}}
|
||||
@@ -128,4 +159,11 @@ impl Drop for Cli { fn drop(&mut self) { let _=self.child.kill(); let _=self.chi
|
||||
}
|
||||
}
|
||||
}
|
||||
#[test] fn source_size_is_read_from_the_ffmpeg_stream_line(){
|
||||
assert_eq!(super::parse_video_size(" Stream #0:0: Video: h264 (High), yuv420p(progressive), 3024x1964, 25 fps"),Some((3024,1964)));
|
||||
assert_eq!(super::parse_video_size("Stream #0:0: Video: hevc, yuv420p, 1920x1080 [SAR 1:1 DAR 16:9]"),Some((1920,1080)));
|
||||
assert_eq!(super::parse_video_size("Stream #0:0: Audio: opus, 48000 Hz, stereo"),None);
|
||||
assert_eq!(super::parse_video_size("frame= 1 fps=0.0 q=0.0"),None);
|
||||
assert_eq!(super::parse_video_size("Stream #0:0: Video: h264, 1920xsynth"),None);
|
||||
}
|
||||
}
|
||||
+14
-3
@@ -5,13 +5,20 @@ pub struct Desktop { pub units:Latest<super::media::AccessUnit>, pub pli:Arc<Ato
|
||||
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> }
|
||||
/// `content` is the source picture size inside the (possibly padded) RGB canvas; None = full frame.
|
||||
pub struct Frame { pub width:u32, pub height:u32, pub rgb:Vec<u8>, pub content:Option<(u32,u32)> }
|
||||
pub trait Decoder { fn set_codec(&mut self,codec:super::media::Codec)->Result<(),String>; fn decode(&mut self, au:&[u8])->Result<Vec<Frame>,String>; fn reset(&mut self); }
|
||||
pub trait FrameSink { fn present(&mut self,frame:&Frame)->Result<(),String>; }
|
||||
/// 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)];
|
||||
fn wants_key_frame(units:u64,presented:u64)->bool {presented==0 && units%30==0}
|
||||
pub fn display(decoder:&mut dyn Decoder,sink:&mut dyn FrameSink,au:&[u8],codec:super::media::Codec,pli:&AtomicBool)->Result<(),String> {
|
||||
STATS[0].fetch_add(1,Ordering::Relaxed);
|
||||
let units=STATS[0].fetch_add(1,Ordering::Relaxed)+1;
|
||||
// A later session can join a clean stream with no IDR in flight (#124): keep asking until a frame shows.
|
||||
if wants_key_frame(units,STATS[1].load(Ordering::Relaxed)) {
|
||||
pli.store(true,Ordering::Release);
|
||||
if units==300 { log::warn!("Viewer desktop: no decodable frame after {units} access units; requesting key frames (another session may hold the host)"); }
|
||||
}
|
||||
// Local diagnostic: UU_VIEWER_DUMP_H264=path appends the first 300 access units (owner's own stream).
|
||||
if codec==super::media::Codec::H264 && STATS[0].load(Ordering::Relaxed)<=300 { if let Some(path)=std::env::var_os("UU_VIEWER_DUMP_H264") {
|
||||
use std::io::Write; if let Ok(mut f)=std::fs::OpenOptions::new().create(true).append(true).open(path){let _=f.write_all(au);}
|
||||
@@ -29,7 +36,11 @@ pub fn display(decoder:&mut dyn Decoder,sink:&mut dyn FrameSink,au:&[u8],codec:s
|
||||
}
|
||||
#[cfg(test)] mod tests {
|
||||
use super::*;
|
||||
struct Mock(bool);impl Decoder for Mock {fn set_codec(&mut self,_:crate::viewer::media::Codec)->Result<(),String>{Ok(())} fn decode(&mut self,_:&[u8])->Result<Vec<Frame>,String>{if self.0{Err("synthetic".into())}else{Ok(vec![Frame{width:1,height:1,rgb:vec![0;3]}])}} fn reset(&mut self){self.0=false;}}
|
||||
struct Mock(bool);impl Decoder for Mock {fn set_codec(&mut self,_:crate::viewer::media::Codec)->Result<(),String>{Ok(())} fn decode(&mut self,_:&[u8])->Result<Vec<Frame>,String>{if self.0{Err("synthetic".into())}else{Ok(vec![Frame{width:1,height:1,rgb:vec![0;3],content:None}])}} 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 requests_key_frames_until_the_first_frame(){
|
||||
assert_eq!((1..=90).filter(|&n|wants_key_frame(n,0)).count(),3);
|
||||
assert!(!(1..=90).any(|n|wants_key_frame(n,1)));
|
||||
}
|
||||
#[test] fn decode_error_requests_pli_and_recovers(){let mut d=Mock(true);let mut s=Sink(0);let p=AtomicBool::new(false);display(&mut d,&mut s,&[],crate::viewer::media::Codec::H264,&p).unwrap();assert!(p.load(Ordering::Acquire));display(&mut d,&mut s,&[],crate::viewer::media::Codec::H264,&p).unwrap();assert_eq!(s.0,1);}
|
||||
}
|
||||
@@ -35,6 +35,15 @@ pub fn mac_keycode(scan:i32)->Option<i32>{
|
||||
_=>return None,
|
||||
})
|
||||
}
|
||||
/// The content rectangle inside a decoder canvas padded to a fixed size (ffmpeg's scale+pad).
|
||||
/// Mirrors `force_original_aspect_ratio=decrease,pad=…:(ow-iw)/2:(oh-ih)/2` so pointer input
|
||||
/// ignores the padding instead of compressing the valid area.
|
||||
pub fn content_rect(canvas:(u32,u32),content:(u32,u32))->Option<(f64,f64,f64,f64)> {
|
||||
if canvas.0==0||canvas.1==0||content.0==0||content.1==0 {return None}
|
||||
let scale=(canvas.0 as f64/content.0 as f64).min(canvas.1 as f64/content.1 as f64);
|
||||
let(w,h)=(content.0 as f64*scale,content.1 as f64*scale);
|
||||
Some(((canvas.0 as f64-w)/2.,(canvas.1 as f64-h)/2.,w,h))
|
||||
}
|
||||
#[derive(Clone,Copy,Debug,PartialEq)]
|
||||
pub struct Viewport { pub x:f64, pub y:f64, pub width:f64, pub height:f64 }
|
||||
impl Viewport {
|
||||
@@ -75,6 +84,14 @@ impl Input {
|
||||
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}))
|
||||
}
|
||||
/// SDL wheel units are notches; the wire uses the Mac convention (one notch = 120).
|
||||
/// Positive y is wheel-up, matching the host's `mouse_scroll` (button 4).
|
||||
pub fn scroll(&mut self,x:f64,y:f64)->Option<Value> {
|
||||
if self.view_only {return None}
|
||||
let(dx,dy)=((x*120.).round() as i32,(y*120.).round() as i32);
|
||||
if dx==0&&dy==0 {return None}
|
||||
Some(json!({"action":"mouse_scroll","delta_x":dx,"delta_y":dy}))
|
||||
}
|
||||
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}));}
|
||||
@@ -117,6 +134,15 @@ impl Input {
|
||||
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 wheel_notches_use_mac_wire_units(){
|
||||
let mut i=Input::default();
|
||||
assert_eq!(i.scroll(0.,1.).unwrap(),json!({"action":"mouse_scroll","delta_x":0,"delta_y":120}));
|
||||
assert_eq!(i.scroll(0.,-1.).unwrap()["delta_y"],-120);
|
||||
assert_eq!(i.scroll(1.,-2.).unwrap(),json!({"action":"mouse_scroll","delta_x":120,"delta_y":-240}));
|
||||
assert_eq!(i.scroll(0.,0.1).unwrap()["delta_y"],12); // precise trackpad deltas
|
||||
assert!(i.scroll(0.,0.).is_none());
|
||||
assert!(Input::new(true).scroll(0.,1.).is_none());
|
||||
}
|
||||
#[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);
|
||||
@@ -138,4 +164,21 @@ assert!(i.release_all().is_empty());
|
||||
assert_eq!(v.motion(1010.,1000.).unwrap()["abs_x"],1.0);
|
||||
assert!(v.motion(f64::NAN,0.).is_none());
|
||||
}
|
||||
#[test] fn padded_canvas_maps_only_its_content_rectangle(){
|
||||
// 16:10 picture inside the default 16:9 canvas: 1728x1080 centred, 96 px pillarbox each side.
|
||||
assert_eq!(content_rect((1920,1080),(1280,800)),Some((96.,0.,1728.,1080.)));
|
||||
assert_eq!(content_rect((1920,1080),(1920,1080)),Some((0.,0.,1920.,1080.)));
|
||||
// 4:3 picture: 1440x1080 centred, 240 px each side.
|
||||
assert_eq!(content_rect((1920,1080),(640,480)),Some((240.,0.,1440.,1080.)));
|
||||
assert_eq!(content_rect((0,1080),(640,480)),None);
|
||||
// The content rectangle's edges are the screen edges, not the canvas edges.
|
||||
let (x,y,w,h)=content_rect((1920,1080),(1280,800)).unwrap();
|
||||
let v=Viewport::fit((1920,1080),(1280,800));
|
||||
assert_eq!((x,y,w,h),(v.x,v.y,v.width,v.height));
|
||||
let abs=|px:f64|v.motion(px,540.).unwrap();
|
||||
assert_eq!(abs(96.)["abs_x"],0.0);
|
||||
assert_eq!(abs(960.)["abs_x"],0.5);
|
||||
assert_eq!(abs(1824.)["abs_x"],1.0);
|
||||
assert_eq!(abs(0.)["abs_x"],0.0); // padding is clamped to the screen edge
|
||||
}
|
||||
}
|
||||
+60
-9
@@ -8,7 +8,10 @@ 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 mut canvas=window.into_canvas().build().map_err(|_|"Renderer creation failed")?;
|
||||
// Wayland maps a toplevel only after its first buffer; without this the window stays
|
||||
// invisible until the first decoded frame, which a stalled stream never delivers.
|
||||
canvas.set_draw_color(sdl2::pixels::Color::BLACK);canvas.clear();canvas.present();
|
||||
let events=sdl.event_pump()?;let clipboard=video.clipboard();
|
||||
let(clipboard_tx,clipboard_requests)=tokio::sync::mpsc::channel(16);
|
||||
Ok(Self{canvas,events,_sdl:sdl,frame:(1920,1080),focused:true,files:false,mac:false,clipboard,clipboard_dirty:true,clipboard_tx,clipboard_requests,last_frame:None})
|
||||
@@ -38,11 +41,32 @@ impl Window {
|
||||
}
|
||||
pub fn poll(&mut self,input:&mut Input)->(bool,Vec<serde_json::Value>){
|
||||
let viewport=self.viewport();
|
||||
let dirty=&mut self.clipboard_dirty;
|
||||
let dirty=&mut self.clipboard_dirty;let mut redraw=false;
|
||||
let events=self.events.poll_iter().inspect(|event|{
|
||||
if matches!(event,Event::ClipboardUpdate{..}|Event::Window{win_event:WindowEvent::FocusGained,..}){*dirty=true;}
|
||||
redraw|=needs_redraw(event);
|
||||
});
|
||||
translate_actions(events,input,viewport,&mut self.focused,&mut self.files)
|
||||
let result=translate_actions(events,input,viewport,&mut self.focused,&mut self.files);
|
||||
// A compositor resize (tiling, maximize) leaves the surface without a buffer of the new size
|
||||
// until the next frame; a static remote desktop sends none, so repaint the last frame now.
|
||||
if redraw {if let Some(frame)=self.last_frame.take(){
|
||||
log::debug!("Viewer window resized: {:?}",self.canvas.window().size());
|
||||
if let Err(e)=self.draw(&frame){log::warn!("Viewer redraw failed: {e}");}
|
||||
self.last_frame=Some(frame);
|
||||
}else{self.canvas.set_draw_color(sdl2::pixels::Color::BLACK);self.canvas.clear();self.canvas.present();}}
|
||||
result
|
||||
}
|
||||
fn draw(&mut self,frame:&Frame)->Result<(),String>{
|
||||
// Pad bars belong to the decoded canvas, not the remote picture: viewport and input use the
|
||||
// content aspect, and only the content sub-rectangle of the texture is drawn.
|
||||
let content=frame.content.unwrap_or((frame.width,frame.height));
|
||||
self.frame=content;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();
|
||||
let src=super::input::content_rect((frame.width,frame.height),content)
|
||||
.map(|(x,y,w,h)|Rect::new(x.round() as i32,y.round() as i32,w.round().max(1.) as u32,h.round().max(1.) as u32));
|
||||
self.canvas.copy(&texture,src,Some(Rect::new(v.x.round() as i32,v.y.round() as i32,v.width.round().max(1.) as u32,v.height.round().max(1.) as u32)))?;self.canvas.present();Ok(())
|
||||
}
|
||||
}
|
||||
fn clipboard_text(bytes:&[u8])->Option<String>{
|
||||
@@ -81,20 +105,24 @@ fn translate_actions(events:impl IntoIterator<Item=Event>,input:&mut Input,viewp
|
||||
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)),
|
||||
Event::MouseWheel{x,y,precise_x,precise_y,direction,..}=>{
|
||||
let(dx,dy)=if precise_x!=0.||precise_y!=0.{(f64::from(precise_x),f64::from(precise_y))}else{(f64::from(x),f64::from(y))};
|
||||
let(dx,dy)=match direction{sdl2::mouse::MouseWheelDirection::Flipped=>(-dx,-dy),_=>(dx,dy)};
|
||||
input.scroll(dx,dy)
|
||||
},
|
||||
_=>None,
|
||||
};if let Some(m)=message{messages.push(m);}
|
||||
} (quit,messages)
|
||||
}
|
||||
|
||||
fn needs_redraw(event:&Event)->bool{
|
||||
matches!(event,Event::Window{win_event:WindowEvent::SizeChanged(..)|WindowEvent::Resized(..)|WindowEvent::Exposed|WindowEvent::Maximized|WindowEvent::Restored,..})
|
||||
}
|
||||
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.last_frame=Some(Frame{width:frame.width,height:frame.height,rgb:frame.rgb.clone()});
|
||||
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(())
|
||||
self.last_frame=Some(Frame{width:frame.width,height:frame.height,rgb:frame.rgb.clone(),content:frame.content});
|
||||
self.draw(frame)
|
||||
}
|
||||
}
|
||||
pub async fn run(session:&mut Session,window:&mut Window,decoder:&mut dyn super::desktop::Decoder,view_only:bool,device:&str)->Result<(),String>{
|
||||
@@ -432,6 +460,29 @@ async fn pump_type_job(job: &mut TypeJob, session: &mut Session, window: &mut Wi
|
||||
assert!(input.release_all().is_empty());
|
||||
}
|
||||
}
|
||||
#[test] fn compositor_resize_and_expose_repaint_the_last_frame(){
|
||||
let w=|win_event|Event::Window{timestamp:0,window_id:1,win_event};
|
||||
for e in [WindowEvent::SizeChanged(1280,800),WindowEvent::Resized(1280,800),WindowEvent::Exposed,WindowEvent::Maximized,WindowEvent::Restored]{assert!(needs_redraw(&w(e)));}
|
||||
for e in [WindowEvent::FocusGained,WindowEvent::Moved(0,0)]{assert!(!needs_redraw(&w(e)));}
|
||||
}
|
||||
#[test] fn wheel_events_become_scroll_messages(){
|
||||
use sdl2::mouse::MouseWheelDirection;
|
||||
let mut input=Input::default();let mut focused=true;
|
||||
let v=Viewport::fit((800,600),(1920,1080));
|
||||
let wheel=|direction,x,y|Event::MouseWheel{timestamp:0,window_id:1,which:0,mouse_x:0,mouse_y:0,x,y,precise_x:0.,precise_y:0.,direction};
|
||||
let(_,out)=translate([wheel(MouseWheelDirection::Normal,0,1),wheel(MouseWheelDirection::Flipped,0,1),wheel(MouseWheelDirection::Normal,0,0)],&mut input,v,&mut focused);
|
||||
assert_eq!(out.len(),2);
|
||||
assert_eq!(out[0]["action"],"mouse_scroll");
|
||||
assert_eq!(out[0]["delta_y"],120);
|
||||
assert_eq!(out[1]["delta_y"],-120);
|
||||
// Trackpads report fractional precise deltas; a quarter notch is 30 wire units.
|
||||
let precise=Event::MouseWheel{timestamp:0,window_id:1,which:0,mouse_x:0,mouse_y:0,x:0,y:0,precise_x:0.,precise_y:-0.25,direction:MouseWheelDirection::Normal};
|
||||
let(_,out)=translate([precise],&mut input,v,&mut focused);
|
||||
assert_eq!(out[0]["delta_y"],-30);
|
||||
let mut watch=Input::new(true);let mut watching=true;
|
||||
let(_,out)=translate([wheel(MouseWheelDirection::Normal,0,1)],&mut watch,v,&mut watching);
|
||||
assert!(out.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());
|
||||
|
||||
@@ -3,13 +3,11 @@
|
||||
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>>,
|
||||
ws: crate::ws_transport::Socket,
|
||||
next_ack:u64,
|
||||
pending:VecDeque<Value>,
|
||||
legacy_prefix:bool,
|
||||
@@ -17,14 +15,14 @@ pub struct Signal {
|
||||
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)}
|
||||
if let Ok(Ok(signal))=tokio::time::timeout(timeout,Self::one(url,token,legacy_prefix,timeout)).await {return Ok(signal)}
|
||||
}
|
||||
Err("Signaling connection failed (details omitted)".into())
|
||||
}
|
||||
async fn one(url:&str,token:&str,legacy_prefix:bool)->Result<Self,String> {
|
||||
async fn one(url:&str,token:&str,legacy_prefix:bool,timeout:Duration)->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 ws=crate::ws_transport::connect(request,timeout).await?;
|
||||
let mut s=Self{ws,next_ack:10,pending:VecDeque::new(),legacy_prefix};
|
||||
let mut opened=false;
|
||||
loop {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#[path = "auth.rs"] mod auth;
|
||||
#[path = "config.rs"] mod config;
|
||||
#[path = "protocol.rs"] mod protocol;
|
||||
mod ws_transport;
|
||||
mod viewer;
|
||||
#[cfg(test)] #[path="hevc.rs"] mod hevc;
|
||||
use viewer::clipboard;
|
||||
|
||||
+387
-181
@@ -42,12 +42,173 @@ pub struct SessionContext {
|
||||
pub bundle_media: Option<(String, u16)>,
|
||||
}
|
||||
|
||||
/// Desktop role of one peer. A tool peer (file transfer, terminal, port mapping) gains it
|
||||
/// when the viewer opens the desktop on that same connection with SimpleAction 4
|
||||
/// capture_desktop instead of a new desktop offer (#99).
|
||||
pub struct DesktopRole {
|
||||
active: tokio::sync::watch::Sender<bool>,
|
||||
input: std::sync::Mutex<Option<Arc<crate::input::DesktopInputSession>>>,
|
||||
stream: std::sync::Mutex<Option<DesktopStream>>,
|
||||
/// The running stream of a promoted peer; aborted on demotion so a quick re-promotion
|
||||
/// never leaves two encoders on one track.
|
||||
stream_task: std::sync::Mutex<Option<tokio::task::AbortHandle>>,
|
||||
/// Capture reinitialized at promotion; the peer's own capture was unavailable when offered.
|
||||
screen: std::sync::Mutex<Option<Arc<ScreenCapture>>>,
|
||||
/// One session tracker per peer follows every promotion and demotion.
|
||||
tracking: std::sync::atomic::AtomicBool,
|
||||
request: std::sync::OnceLock<Box<dyn Fn() + Send + Sync>>,
|
||||
}
|
||||
impl DesktopRole {
|
||||
fn new(desktop: bool) -> Self {
|
||||
Self { active: tokio::sync::watch::channel(desktop).0, input: Default::default(),
|
||||
stream: Default::default(), stream_task: Default::default(), screen: Default::default(), tracking: Default::default(), request: Default::default() }
|
||||
}
|
||||
pub fn is_desktop(&self) -> bool { *self.active.borrow() }
|
||||
/// A promoted peer whose video ended on its own (capture failed while locked or off, #114);
|
||||
/// capture_desktop on it starts the video again.
|
||||
fn stream_ended(&self) -> bool { self.stream_task.lock().unwrap().as_ref().is_some_and(|task| task.is_finished()) }
|
||||
fn input(&self) -> Option<Arc<crate::input::DesktopInputSession>> { self.input.lock().unwrap().clone() }
|
||||
fn screen_or(&self, screen: &Arc<ScreenCapture>) -> Arc<ScreenCapture> {
|
||||
self.screen.lock().unwrap().clone().unwrap_or_else(|| screen.clone())
|
||||
}
|
||||
/// Waits until this peer is a desktop; false once the channel closes first.
|
||||
async fn wait_desktop(&self, channel: &RTCDataChannel) -> bool {
|
||||
let mut active = self.active.subscribe();
|
||||
loop {
|
||||
if *active.borrow_and_update() { return true; }
|
||||
tokio::select! {
|
||||
changed = active.changed() => if changed.is_err() { return false; },
|
||||
_ = tokio::time::sleep(std::time::Duration::from_millis(200)) => {
|
||||
if matches!(channel.ready_state(), webrtc::data_channel::data_channel_state::RTCDataChannelState::Closing
|
||||
| webrtc::data_channel::data_channel_state::RTCDataChannelState::Closed) { return false; }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Live desktop video for one peer; a tool peer keeps it unstarted until it becomes a desktop.
|
||||
#[derive(Clone)]
|
||||
struct DesktopStream {
|
||||
pc: std::sync::Weak<RTCPeerConnection>,
|
||||
track: Arc<crate::video_track::VideoTrack>,
|
||||
portal_desktop: Option<Arc<crate::desktop_session::PortalDesktop>>,
|
||||
capture_size: (u32, u32),
|
||||
capture_display: String,
|
||||
portal: Option<Arc<crate::portal::PortalSession>>,
|
||||
settings_rx: tokio::sync::watch::Receiver<crate::capture_setting::VideoSettings>,
|
||||
settings: Arc<tokio::sync::watch::Sender<crate::capture_setting::VideoSettings>>,
|
||||
idr_request: Arc<std::sync::atomic::AtomicBool>,
|
||||
selection: crate::video_select::Selection,
|
||||
chroma_sync: Option<tokio::sync::mpsc::UnboundedSender<Vec<u8>>>,
|
||||
reload_at_session: u64,
|
||||
}
|
||||
|
||||
pub struct WebRtcSession {
|
||||
pub pc: Arc<RTCPeerConnection>,
|
||||
pub capture: Arc<ScreenCapture>,
|
||||
pub input: Arc<InputInjector>,
|
||||
/// The role this peer was offered with; `role` also covers a later capture_desktop.
|
||||
pub desktop: bool,
|
||||
pub role: Arc<DesktopRole>,
|
||||
pub controller_mac: bool,
|
||||
pub context: Arc<Mutex<SessionContext>>,
|
||||
actions: Arc<crate::system_actions::PeerActions>,
|
||||
}
|
||||
|
||||
/// Counts a desktop session once it is connected (a failed offer is not a session), while the
|
||||
/// peer is the desktop. Started once per peer, so a quick demote and re-promote cannot end the
|
||||
/// count of a session that is still streaming.
|
||||
fn track_session(end_pc: Arc<RTCPeerConnection>, actions: Arc<crate::system_actions::PeerActions>, role: &DesktopRole) {
|
||||
if role.tracking.swap(true, std::sync::atomic::Ordering::AcqRel) { return; }
|
||||
let active = role.active.subscribe();
|
||||
tokio::spawn(async move {
|
||||
use webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState as State;
|
||||
loop {
|
||||
while end_pc.connection_state() != State::Connected || !*active.borrow() {
|
||||
if matches!(end_pc.connection_state(), State::Closed | State::Failed) { return; }
|
||||
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
|
||||
}
|
||||
let starting = actions.clone();
|
||||
let _ = tokio::task::spawn_blocking(move || starting.session_started()).await;
|
||||
while !matches!(end_pc.connection_state(), State::Closed | State::Failed) && *active.borrow() {
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
}
|
||||
let closed = matches!(end_pc.connection_state(), State::Closed | State::Failed);
|
||||
let ending = actions.clone();
|
||||
let _ = tokio::task::spawn_blocking(move || ending.session_ended()).await;
|
||||
if closed { return; }
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
type EchoReplay = (Arc<std::sync::atomic::AtomicU64>, Arc<std::sync::atomic::AtomicBool>, Arc<std::sync::atomic::AtomicBool>);
|
||||
|
||||
/// A promoted tool peer starts its TEXT clipboard after the viewer's echo was answered without
|
||||
/// ff_clipboard; declare it again on CONTROL once the bridge is ready, as the Portal path does.
|
||||
fn replay_clipboard_echo(channel: Arc<RTCDataChannel>, sequence: Arc<std::sync::atomic::AtomicU64>,
|
||||
(echo_request, echo_seen, clipboard_ready): EchoReplay, role: Arc<DesktopRole>) {
|
||||
tokio::spawn(async move {
|
||||
for _ in 0..100 {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
if !role.is_desktop() || channel.ready_state() != webrtc::data_channel::data_channel_state::RTCDataChannelState::Open { return; }
|
||||
if !clipboard_ready.load(std::sync::atomic::Ordering::Acquire) { continue; }
|
||||
// echo_seen is published after the request seq, so reading it first gives a current seq.
|
||||
if echo_seen.load(std::sync::atomic::Ordering::Acquire) {
|
||||
let request_seq = echo_request.load(std::sync::atomic::Ordering::Acquire);
|
||||
let timestamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
|
||||
let response = crate::control_protocol::encode_echo_response_with_clipboard(
|
||||
sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed), timestamp, request_seq, true);
|
||||
let _ = channel.send(&bytes::Bytes::from(response)).await;
|
||||
log::info!("CONTROL clipboard capability declared after capture_desktop");
|
||||
}
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
impl DesktopStream {
|
||||
/// Encode current frames only while the peer is connected and still the desktop.
|
||||
fn spawn(self, screen: Option<Arc<ScreenCapture>>, mut active: tokio::sync::watch::Receiver<bool>) -> Option<tokio::task::AbortHandle> {
|
||||
let streaming_pc = self.pc.upgrade()?;
|
||||
let DesktopStream { track: video_track, portal_desktop, capture_size, capture_display, portal,
|
||||
settings_rx, settings, idr_request, selection, chroma_sync, reload_at_session, .. } = self;
|
||||
let (capture_size, capture_display, portal) = screen.map_or((capture_size, capture_display, portal),
|
||||
|screen| ((screen.width, screen.height), screen.display.clone(), screen.portal.clone()));
|
||||
Some(tokio::spawn(async move { tokio::select! {
|
||||
_ = active.wait_for(|active| !*active) => log::info!("Live desktop stream stopped: a newer desktop took over"),
|
||||
_ = async move {
|
||||
let screen = if let Some(desktop) = portal_desktop {
|
||||
if let Err(error) = crate::locked_video::run(&streaming_pc, &video_track, &desktop, selection).await {
|
||||
log::warn!("Locked desktop video unavailable: {error}");
|
||||
}
|
||||
let mut updates = desktop.subscribe();
|
||||
loop {
|
||||
let state = updates.borrow_and_update().clone();
|
||||
if state.ready() { break Some(state.screen); }
|
||||
if !state.waiting { return; }
|
||||
if crate::video::transport_closed(&streaming_pc) { return; }
|
||||
tokio::select! {
|
||||
changed = updates.changed() => if changed.is_err() { return; },
|
||||
_ = tokio::time::sleep(std::time::Duration::from_millis(100)) => {},
|
||||
}
|
||||
}
|
||||
} else { None };
|
||||
let (capture_size, capture_display, portal) = screen.as_ref().map_or(
|
||||
(capture_size, capture_display, portal),
|
||||
|screen| ((screen.width, screen.height), screen.display.clone(), screen.portal.clone()));
|
||||
let state_pc = streaming_pc.clone();
|
||||
if let Err(e) = crate::video::stream_desktop_at(streaming_pc, video_track, capture_size, capture_display, portal, settings_rx, settings, idr_request, selection, chroma_sync, reload_at_session).await {
|
||||
// A send racing the viewer's close fails with a misleading track error.
|
||||
if matches!(state_pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed) {
|
||||
log::info!("Live desktop stream ended: peer connection closed ({})", e);
|
||||
} else {
|
||||
log::error!("Live desktop stream stopped: {}", e);
|
||||
}
|
||||
}
|
||||
} => {},
|
||||
} }).abort_handle())
|
||||
}
|
||||
}
|
||||
|
||||
impl WebRtcSession {
|
||||
@@ -106,7 +267,8 @@ impl WebRtcSession {
|
||||
.map_err(|e| format!("Create PeerConnection failed: {}", e))?,
|
||||
);
|
||||
|
||||
let input_session=desktop.then(||input.desktop_session());
|
||||
let role = Arc::new(DesktopRole::new(desktop));
|
||||
if desktop { *role.input.lock().unwrap() = Some(input.desktop_session()); }
|
||||
let context = Arc::new(Mutex::new(SessionContext::default()));
|
||||
crate::network_diagnostics::start(Arc::downgrade(&pc));
|
||||
|
||||
@@ -164,13 +326,13 @@ impl WebRtcSession {
|
||||
}));
|
||||
|
||||
let connected_context = context.clone();
|
||||
let connection_input=input_session.clone();
|
||||
let connection_role=role.clone();
|
||||
let connected_signal = signaling_client.clone();
|
||||
let notified = Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||
let connected_selection = video_selection;
|
||||
pc.on_peer_connection_state_change(Box::new(move |state| {
|
||||
let ctx = connected_context.clone();
|
||||
let input=connection_input.clone();
|
||||
let input=connection_role.input();
|
||||
let sig = connected_signal.clone();
|
||||
let notified = notified.clone();
|
||||
let video_selection = connected_selection;
|
||||
@@ -282,72 +444,24 @@ impl WebRtcSession {
|
||||
let video_settings = Arc::new(video_settings);
|
||||
// Per-connection mute / lock-after-session settings (安全 menu).
|
||||
let peer_actions = Arc::new(crate::system_actions::PeerActions::default());
|
||||
let session_actions = peer_actions.clone();
|
||||
let desktop_sound = desktop && crate::audio::enabled();
|
||||
if desktop && !cfg!(test) {
|
||||
let (end_pc, actions) = (pc.clone(), peer_actions.clone());
|
||||
tokio::spawn(async move {
|
||||
{
|
||||
use webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState as State;
|
||||
// Count the session once it is actually connected (a failed offer is not a session).
|
||||
while end_pc.connection_state() != State::Connected {
|
||||
if matches!(end_pc.connection_state(), State::Closed | State::Failed) { return; }
|
||||
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
|
||||
}
|
||||
let starting = actions.clone();
|
||||
let _ = tokio::task::spawn_blocking(move || starting.session_started()).await;
|
||||
}
|
||||
use webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState as State;
|
||||
while !matches!(end_pc.connection_state(), State::Closed | State::Failed) {
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
}
|
||||
drop(end_pc);
|
||||
let _ = tokio::task::spawn_blocking(move || actions.session_ended()).await;
|
||||
});
|
||||
}
|
||||
let portal=capture.portal.clone();
|
||||
let stream_desktop = portal_desktop.clone();
|
||||
let stream_selection = video_selection;
|
||||
if desktop && !cfg!(test) { track_session(pc.clone(), peer_actions.clone(), &role); }
|
||||
// Live encoder switch (SIGUSR2) sends CaptureSettingSync when chroma is downgraded.
|
||||
let (chroma_sync_tx, chroma_sync_rx) = tokio::sync::mpsc::unbounded_channel::<Vec<u8>>();
|
||||
let chroma_sync = (desktop && !cfg!(test)).then_some(chroma_sync_tx);
|
||||
let chroma_sync_rx = Arc::new(std::sync::Mutex::new((!cfg!(test)).then_some(chroma_sync_rx)));
|
||||
// File/terminal/port-mapping connections carry no desktop video.
|
||||
// File/terminal/port-mapping connections carry no desktop video until capture_desktop.
|
||||
let control_video_track = video_track.clone();
|
||||
let stream_settings = video_settings.clone();
|
||||
if desktop && !cfg!(test) {
|
||||
let state_pc = streaming_pc.clone();
|
||||
tokio::spawn(async move {
|
||||
let screen = if let Some(desktop) = stream_desktop {
|
||||
if let Err(error) = crate::locked_video::run(&streaming_pc, &video_track, &desktop, stream_selection).await {
|
||||
log::warn!("Locked desktop video unavailable: {error}");
|
||||
}
|
||||
let mut updates = desktop.subscribe();
|
||||
loop {
|
||||
let state = updates.borrow_and_update().clone();
|
||||
if state.ready() { break Some(state.screen); }
|
||||
if !state.waiting { return; }
|
||||
if crate::video::transport_closed(&streaming_pc) { return; }
|
||||
tokio::select! {
|
||||
changed = updates.changed() => if changed.is_err() { return; },
|
||||
_ = tokio::time::sleep(std::time::Duration::from_millis(100)) => {},
|
||||
}
|
||||
}
|
||||
} else { None };
|
||||
let (capture_size, capture_display, portal) = screen.as_ref().map_or(
|
||||
(capture_size, capture_display, portal),
|
||||
|screen| ((screen.width, screen.height), screen.display.clone(), screen.portal.clone()));
|
||||
if let Err(e) = crate::video::stream_desktop_at(streaming_pc, video_track, capture_size, capture_display, portal, video_settings_rx, stream_settings, idr_request, stream_selection, chroma_sync.clone(), reload_at_session).await {
|
||||
// A send racing the viewer's close fails with a misleading track error.
|
||||
if matches!(state_pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed) {
|
||||
log::info!("Live desktop stream ended: peer connection closed ({})", e);
|
||||
} else {
|
||||
log::error!("Live desktop stream stopped: {}", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
drop((streaming_pc, video_track, capture_size, capture_display, video_settings_rx));
|
||||
}
|
||||
let stream = DesktopStream {
|
||||
pc: Arc::downgrade(&streaming_pc), track: video_track, portal_desktop: portal_desktop.clone(),
|
||||
capture_size, capture_display, portal: capture.portal.clone(), settings_rx: video_settings_rx,
|
||||
settings: video_settings.clone(), idr_request, selection: video_selection,
|
||||
chroma_sync: (!cfg!(test)).then_some(chroma_sync_tx), reload_at_session,
|
||||
};
|
||||
drop(streaming_pc);
|
||||
if cfg!(test) { drop(stream); }
|
||||
else if desktop { let _ = stream.spawn(None, role.active.subscribe()); }
|
||||
else { *role.stream.lock().unwrap() = Some(stream); }
|
||||
let input_clone = input.clone();
|
||||
let control_capture = capture.clone();
|
||||
let desktop_pc = Arc::downgrade(&pc);
|
||||
@@ -405,6 +519,7 @@ impl WebRtcSession {
|
||||
|
||||
// Settings arrive on CONTROL or TEXT; serialize both queues for this peer.
|
||||
let settings_serial=Arc::new(Mutex::new(()));
|
||||
let data_role = role.clone();
|
||||
// 监听 DataChannel
|
||||
pc.on_data_channel(Box::new(move |d: Arc<RTCDataChannel>| {
|
||||
let label = d.label().to_string();
|
||||
@@ -412,7 +527,7 @@ impl WebRtcSession {
|
||||
|
||||
let peer_settings_serial=settings_serial.clone();
|
||||
let input_inst = input_clone.clone();
|
||||
let channel_input_session=input_session.clone();
|
||||
let channel_role=data_role.clone();
|
||||
let screen = control_capture.clone();
|
||||
let portal_desktop = portal_desktop.clone();
|
||||
let desktop_pc = desktop_pc.clone();
|
||||
@@ -442,9 +557,9 @@ impl WebRtcSession {
|
||||
let rich_state=Arc::new(std::sync::Mutex::new(crate::clipboard_rich::State::default()));
|
||||
let clipboard_enabled = shared_clipboard_ready.clone();
|
||||
if label=="CONTROL_DATA_CHANNEL" {
|
||||
let closing=channel_input_session.clone();
|
||||
let closing=channel_role.clone();
|
||||
d.on_close(Box::new(move || {
|
||||
let closing=closing.clone();
|
||||
let closing=closing.input();
|
||||
Box::pin(async move {if let Some(session)=closing {session.disconnected();}})
|
||||
}));
|
||||
}
|
||||
@@ -500,7 +615,7 @@ impl WebRtcSession {
|
||||
let manager = portal_desktop.clone();
|
||||
let channel = Arc::downgrade(&d);
|
||||
let initial_screen = screen.clone();
|
||||
let switch_owner=channel_input_session.clone();
|
||||
let capture_role=channel_role.clone();
|
||||
let settings_serial=peer_settings_serial.clone();
|
||||
let capture_selection = peer_selection;
|
||||
tokio::spawn(async move {
|
||||
@@ -518,6 +633,7 @@ impl WebRtcSession {
|
||||
}
|
||||
};
|
||||
let _serial=settings_serial.lock().await;
|
||||
let (desktop,switch_owner)=(capture_role.is_desktop(),capture_role.input());
|
||||
if desktop && switch_owner.as_ref().is_none_or(|owner|!owner.is_current()) {continue;}
|
||||
let reject=|reason:&str| {
|
||||
if let Some(response)=crate::capture_setting::rejected_request(&request,reason) {
|
||||
@@ -563,10 +679,10 @@ impl WebRtcSession {
|
||||
if channel.upgrade().is_none_or(|channel| channel.ready_state() != webrtc::data_channel::data_channel_state::RTCDataChannelState::Open) { return; }
|
||||
}
|
||||
} else {
|
||||
Some(initial_screen.clone())
|
||||
Some(capture_role.screen_or(&initial_screen))
|
||||
}
|
||||
} else {
|
||||
Some(initial_screen.clone())
|
||||
Some(capture_role.screen_or(&initial_screen))
|
||||
};
|
||||
let Some(active_screen) = active_screen else { continue; };
|
||||
let input_size = if active_screen.init_error.is_none() {
|
||||
@@ -675,7 +791,8 @@ impl WebRtcSession {
|
||||
let open_video = channel_video_settings.clone();
|
||||
let open_track = peer_video_track.clone();
|
||||
let open_desktop = portal_desktop.clone();
|
||||
let open_owner = channel_input_session.clone();
|
||||
let open_owner = channel_role.input();
|
||||
let open_role = channel_role.clone();
|
||||
let open_screen = screen.clone();
|
||||
let open_selection = peer_selection;
|
||||
d.on_open(Box::new(move || {
|
||||
@@ -691,82 +808,91 @@ impl WebRtcSession {
|
||||
let rich_outgoing=rich_outgoing.clone();
|
||||
let portal_desktop = open_desktop.clone();
|
||||
let owner = open_owner.clone();
|
||||
let role = open_role.clone();
|
||||
let pc = desktop_pc.upgrade();
|
||||
Box::pin(async move {
|
||||
// Permission dialogs and first-frame waits must not occupy the
|
||||
// channel-open callback or delay CONTROL message dispatch.
|
||||
tokio::spawn(async move {
|
||||
if channel.label() == "TEXT_DATA_CHANNEL" {
|
||||
if !desktop { return; }
|
||||
let screen = if let Some(desktop) = &portal_desktop {
|
||||
let mut updates = desktop.subscribe();
|
||||
let Some(_) = crate::desktop_session::wait_ready(&mut updates, &channel).await else { return; };
|
||||
let mut locked = desktop.lock_state();
|
||||
loop {
|
||||
let state = desktop.snapshot();
|
||||
if !state.waiting && state.screen.init_error.is_some() { return; }
|
||||
if let Some(session) = state.screen.portal.as_ref() {
|
||||
if session.is_alive() && *locked.borrow() == Some(false) {
|
||||
break state.screen;
|
||||
}
|
||||
}
|
||||
tokio::select! {
|
||||
_ = tokio::time::sleep(std::time::Duration::from_millis(100)) => {},
|
||||
changed = locked.changed() => if changed.is_err() { return; },
|
||||
_ = async {
|
||||
loop {
|
||||
if matches!(channel.ready_state(), webrtc::data_channel::data_channel_state::RTCDataChannelState::Closing
|
||||
| webrtc::data_channel::data_channel_state::RTCDataChannelState::Closed) { return; }
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
}
|
||||
} => return,
|
||||
}
|
||||
}
|
||||
} else { screen };
|
||||
let bridge = if let Some(session)=screen.portal.clone() { crate::clipboard::start_portal(session) } else { crate::clipboard::start(screen.display.clone()) };
|
||||
*clipboard_sink.lock().unwrap() = Some(bridge.incoming);
|
||||
let clipboard_channel = channel.clone();
|
||||
let clipboard_sequence = sequence.clone();
|
||||
let clipboard_cleanup = clipboard_sink.clone();
|
||||
let ready = bridge.ready.await.unwrap_or(false);
|
||||
clipboard_ready.store(ready,std::sync::atomic::Ordering::Relaxed);
|
||||
tokio::spawn(async move {
|
||||
if !ready {
|
||||
*clipboard_cleanup.lock().unwrap() = None;
|
||||
log::warn!("Native text clipboard unavailable"); return;
|
||||
}
|
||||
log::info!("Native text clipboard bridge ready");
|
||||
let mut updates = bridge.outgoing;
|
||||
let mut health = tokio::time::interval(std::time::Duration::from_millis(200));
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = health.tick() => {
|
||||
if clipboard_channel.ready_state() != webrtc::data_channel::data_channel_state::RTCDataChannelState::Open { break; }
|
||||
},
|
||||
changed = updates.changed() => {
|
||||
if changed.is_err() { break; }
|
||||
let text = updates.borrow_and_update().clone();
|
||||
let Some(text) = text else { continue; };
|
||||
{
|
||||
let request_id=clipboard_sequence.fetch_add(1,std::sync::atomic::Ordering::Relaxed);
|
||||
let payload = match rich_outgoing.lock().unwrap().offer(request_id,text.clone()) {
|
||||
Ok(payload)=>payload,Err(reason)=>{log::warn!("Clipboard offer rejected: {}",reason);continue;}
|
||||
};
|
||||
let timestamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
|
||||
let message = crate::initialization::envelope(clipboard_sequence.fetch_add(1,std::sync::atomic::Ordering::Relaxed),timestamp,21,&payload);
|
||||
if !matches!(tokio::time::timeout(std::time::Duration::from_secs(2),clipboard_channel.send(&bytes::Bytes::from(message))).await,Ok(Ok(_))) {
|
||||
log::warn!("Native clipboard RPC send failed"); break;
|
||||
}
|
||||
}
|
||||
log::info!("Native clipboard text format offered: bytes={}",text.len());
|
||||
}
|
||||
}
|
||||
}
|
||||
*clipboard_cleanup.lock().unwrap() = None;
|
||||
clipboard_ready.store(false,std::sync::atomic::Ordering::Relaxed);
|
||||
});
|
||||
return;
|
||||
loop {
|
||||
if !role.is_desktop() && !role.wait_desktop(&channel).await { return; }
|
||||
let screen = if let Some(desktop) = &portal_desktop {
|
||||
let mut updates = desktop.subscribe();
|
||||
let Some(_) = crate::desktop_session::wait_ready(&mut updates, &channel).await else { return; };
|
||||
let mut locked = desktop.lock_state();
|
||||
loop {
|
||||
let state = desktop.snapshot();
|
||||
if !state.waiting && state.screen.init_error.is_some() { return; }
|
||||
if let Some(session) = state.screen.portal.as_ref() {
|
||||
if session.is_alive() && *locked.borrow() == Some(false) {
|
||||
break state.screen;
|
||||
}
|
||||
}
|
||||
tokio::select! {
|
||||
_ = tokio::time::sleep(std::time::Duration::from_millis(100)) => {},
|
||||
changed = locked.changed() => if changed.is_err() { return; },
|
||||
_ = async {
|
||||
loop {
|
||||
if matches!(channel.ready_state(), webrtc::data_channel::data_channel_state::RTCDataChannelState::Closing
|
||||
| webrtc::data_channel::data_channel_state::RTCDataChannelState::Closed) { return; }
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
}
|
||||
} => return,
|
||||
}
|
||||
}
|
||||
} else { role.screen_or(&screen) };
|
||||
let bridge = if let Some(session)=screen.portal.clone() { crate::clipboard::start_portal(session) } else { crate::clipboard::start(screen.display.clone()) };
|
||||
*clipboard_sink.lock().unwrap() = Some(bridge.incoming);
|
||||
let clipboard_channel = channel.clone();
|
||||
let clipboard_sequence = sequence.clone();
|
||||
let clipboard_cleanup = clipboard_sink.clone();
|
||||
let rich_outgoing = rich_outgoing.clone();
|
||||
let ready = bridge.ready.await.unwrap_or(false);
|
||||
clipboard_ready.store(ready,std::sync::atomic::Ordering::Relaxed);
|
||||
{
|
||||
if !ready {
|
||||
*clipboard_cleanup.lock().unwrap() = None;
|
||||
log::warn!("Native text clipboard unavailable"); return;
|
||||
}
|
||||
log::info!("Native text clipboard bridge ready");
|
||||
let mut updates = bridge.outgoing;
|
||||
let mut health = tokio::time::interval(std::time::Duration::from_millis(200));
|
||||
let mut active = role.active.subscribe();
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = health.tick() => {
|
||||
if clipboard_channel.ready_state() != webrtc::data_channel::data_channel_state::RTCDataChannelState::Open { break; }
|
||||
},
|
||||
_ = async { let _ = active.wait_for(|active| !*active).await; } => break,
|
||||
changed = updates.changed() => {
|
||||
if changed.is_err() { break; }
|
||||
let text = updates.borrow_and_update().clone();
|
||||
let Some(text) = text else { continue; };
|
||||
{
|
||||
let request_id=clipboard_sequence.fetch_add(1,std::sync::atomic::Ordering::Relaxed);
|
||||
let payload = match rich_outgoing.lock().unwrap().offer(request_id,text.clone()) {
|
||||
Ok(payload)=>payload,Err(reason)=>{log::warn!("Clipboard offer rejected: {}",reason);continue;}
|
||||
};
|
||||
let timestamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
|
||||
let message = crate::initialization::envelope(clipboard_sequence.fetch_add(1,std::sync::atomic::Ordering::Relaxed),timestamp,21,&payload);
|
||||
if !matches!(tokio::time::timeout(std::time::Duration::from_secs(2),clipboard_channel.send(&bytes::Bytes::from(message))).await,Ok(Ok(_))) {
|
||||
log::warn!("Native clipboard RPC send failed"); break;
|
||||
}
|
||||
}
|
||||
log::info!("Native clipboard text format offered: bytes={}",text.len());
|
||||
}
|
||||
}
|
||||
}
|
||||
*clipboard_cleanup.lock().unwrap() = None;
|
||||
clipboard_ready.store(false,std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
if channel.ready_state() != webrtc::data_channel::data_channel_state::RTCDataChannelState::Open { return; }
|
||||
// A newer desktop took over this promoted tool peer; capture_desktop restarts the clipboard.
|
||||
log::info!("Native text clipboard stopped: a newer desktop took over");
|
||||
}
|
||||
}
|
||||
let replay = (last_echo_request.clone(), echo_seen.clone(), clipboard_ready.clone());
|
||||
let early_desktop_selection = desktop && portal_desktop.is_some();
|
||||
if early_desktop_selection {
|
||||
// Android 4.43.0's unlock presenter requires CaptureChange
|
||||
@@ -853,58 +979,75 @@ impl WebRtcSession {
|
||||
}
|
||||
} else { snapshot.screen }
|
||||
} else { screen };
|
||||
let timestamp = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
|
||||
let next_seq = || sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
// Default file-transfer folder: the desktop user's home directory.
|
||||
let home = std::env::var("HOME").unwrap_or_else(|_| "/".into());
|
||||
// Tool peers are permitted without a desktop; the port-mapping viewer aborts on Reject.
|
||||
let desktop_permission=if screen.init_error.is_none() || !desktop {
|
||||
crate::system_metrics::Permission::Allow
|
||||
} else { crate::system_metrics::Permission::Reject };
|
||||
// Without a desktop (headless/portal before consent) the port-mapping tool still waits
|
||||
// for ScreenSources/CaptureChange and aborts otherwise: tool peers get a nominal screen.
|
||||
let screen_size=crate::wayland::input_size(if screen.init_error.is_none() { (screen.width, screen.height) } else { (1920, 1080) });
|
||||
let messages = [
|
||||
(8, crate::initialization::encode_capture_change(next_seq(), timestamp, 0, crate::wayland::selected() as i32, "Linux desktop")),
|
||||
(13, Ok(crate::initialization::envelope(next_seq(), timestamp, 13, &init_actions.metrics_payload(desktop_sound, desktop_permission)))),
|
||||
(26, Ok(crate::initialization::envelope(next_seq(), timestamp, 26,
|
||||
&crate::system_metrics::remote_download_path(&home)))),
|
||||
(18, Ok(crate::initialization::envelope(next_seq(), timestamp, 18,
|
||||
&crate::capture_setting::codec_negotiation(screen_size, crate::video::display_fps(), init_track.supports(crate::capture_setting::VideoCodec::H265))))),
|
||||
(7, crate::capture_setting::screen_sources(next_seq(), timestamp, screen_size, init_video.borrow().output, "Linux desktop")),
|
||||
];
|
||||
if desktop && screen.init_error.is_none() {
|
||||
let codec = crate::capture_setting::preferred_codec_for(init_track.supports(crate::capture_setting::VideoCodec::H264),
|
||||
init_track.supports(crate::capture_setting::VideoCodec::H265), open_selection);
|
||||
init_video.send_if_modified(|current| if current.codec != codec {
|
||||
log::info!("Desktop codec selected: {:?}", codec); current.codec = codec; true } else { false });
|
||||
}
|
||||
for (field, message) in messages {
|
||||
if early_desktop_selection && field == 8 { continue; }
|
||||
if (!desktop || screen.init_error.is_some()) && field == 18 { continue; }
|
||||
if desktop && screen.init_error.is_some() && matches!(field,7|8) { continue; }
|
||||
let message = match message {
|
||||
Ok(message) => message,
|
||||
Err(e) => { log::error!("CONTROL initialization encode failed: {}", e); return; }
|
||||
};
|
||||
if let Err(e) = channel.send(&bytes::Bytes::from(message)).await {
|
||||
log::error!("CONTROL initialization field {} failed: {}", field, e);
|
||||
return;
|
||||
let (mut desktop, mut promoted, mut lock_reported) = (desktop, false, desktop);
|
||||
let mut screen = screen;
|
||||
loop {
|
||||
loop {
|
||||
let timestamp = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
|
||||
// Tool peers are permitted without a desktop; the port-mapping viewer aborts on Reject.
|
||||
let desktop_permission=if screen.init_error.is_none() || !desktop {
|
||||
crate::system_metrics::Permission::Allow
|
||||
} else { crate::system_metrics::Permission::Reject };
|
||||
// Without a desktop (headless/portal before consent) the port-mapping tool still waits
|
||||
// for ScreenSources/CaptureChange and aborts otherwise: tool peers get a nominal screen.
|
||||
let screen_size=crate::wayland::input_size(if screen.init_error.is_none() { (screen.width, screen.height) } else { (1920, 1080) });
|
||||
let messages = [
|
||||
(8, crate::initialization::encode_capture_change(next_seq(), timestamp, 0, crate::wayland::selected() as i32, "Linux desktop")),
|
||||
(13, Ok(crate::initialization::envelope(next_seq(), timestamp, 13, &init_actions.metrics_payload(desktop_sound, desktop_permission)))),
|
||||
(26, Ok(crate::initialization::envelope(next_seq(), timestamp, 26,
|
||||
&crate::system_metrics::remote_download_path(&home)))),
|
||||
(18, Ok(crate::initialization::envelope(next_seq(), timestamp, 18,
|
||||
&crate::capture_setting::codec_negotiation(screen_size, crate::video::display_fps(), init_track.supports(crate::capture_setting::VideoCodec::H265))))),
|
||||
(7, crate::capture_setting::screen_sources(next_seq(), timestamp, screen_size, init_video.borrow().output, "Linux desktop")),
|
||||
];
|
||||
if desktop && screen.init_error.is_none() {
|
||||
let codec = crate::capture_setting::preferred_codec_for(init_track.supports(crate::capture_setting::VideoCodec::H264),
|
||||
init_track.supports(crate::capture_setting::VideoCodec::H265), open_selection);
|
||||
init_video.send_if_modified(|current| if current.codec != codec {
|
||||
log::info!("Desktop codec selected: {:?}", codec); current.codec = codec; true } else { false });
|
||||
}
|
||||
log::info!("CONTROL initialization field {} sent", field);
|
||||
for (field, message) in messages {
|
||||
if early_desktop_selection && field == 8 { continue; }
|
||||
if promoted && field == 26 { continue; }
|
||||
if (!desktop || screen.init_error.is_some()) && field == 18 { continue; }
|
||||
if desktop && screen.init_error.is_some() && matches!(field,7|8) { continue; }
|
||||
let message = match message {
|
||||
Ok(message) => message,
|
||||
Err(e) => { log::error!("CONTROL initialization encode failed: {}", e); return; }
|
||||
};
|
||||
if let Err(e) = channel.send(&bytes::Bytes::from(message)).await {
|
||||
log::error!("CONTROL initialization field {} failed: {}", field, e);
|
||||
return;
|
||||
}
|
||||
log::info!("CONTROL initialization field {} sent", field);
|
||||
}
|
||||
if desktop { break; }
|
||||
// The viewer opened the desktop on this tool connection (#99): repeat the
|
||||
// desktop initialization once the host has started its video and input.
|
||||
if !role.wait_desktop(&channel).await { return; }
|
||||
(desktop, promoted) = (true, true);
|
||||
screen = role.screen_or(&screen);
|
||||
replay_clipboard_echo(channel.clone(), sequence.clone(), replay.clone(), role.clone());
|
||||
}
|
||||
if !lock_reported {
|
||||
lock_reported = true;
|
||||
tokio::spawn(crate::session_lock::report_states(channel.clone(), sequence.clone(), crate::session_lock::watch_locked()));
|
||||
}
|
||||
// Mirror the Linux pointer shape; the video itself carries no pointer.
|
||||
if !desktop { return; }
|
||||
let mut shapes = crate::cursor::monitor(&screen.display);
|
||||
let mut health = tokio::time::interval(std::time::Duration::from_millis(200));
|
||||
let mut sent = 0usize;
|
||||
loop {
|
||||
let mut active = role.active.subscribe();
|
||||
'cursor: loop {
|
||||
let current = shapes.borrow_and_update().clone();
|
||||
if let Some(cursor) = current {
|
||||
let timestamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
|
||||
let message = crate::cursor::encode_cursor_shape(next_seq(), timestamp, &cursor);
|
||||
if channel.send(&bytes::Bytes::from(message)).await.is_err() { break; }
|
||||
if channel.send(&bytes::Bytes::from(message)).await.is_err() { return; }
|
||||
sent += 1;
|
||||
if sent <= 3 || sent % 100 == 0 {
|
||||
log::info!("Cursor shape sent: count={} size={}x{} hotspot={},{} png_bytes={}", sent, cursor.width, cursor.height, cursor.hot_x, cursor.hot_y, cursor.png.len());
|
||||
@@ -913,12 +1056,18 @@ impl WebRtcSession {
|
||||
loop {
|
||||
tokio::select! {
|
||||
changed = shapes.changed() => { if changed.is_err() { return; } break; },
|
||||
_ = active.wait_for(|active| !*active) => break 'cursor,
|
||||
_ = health.tick() => {
|
||||
if channel.ready_state() != webrtc::data_channel::data_channel_state::RTCDataChannelState::Open { return; }
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
// A newer desktop took over this promoted tool peer; wait for capture_desktop again.
|
||||
if !role.wait_desktop(&channel).await { return; }
|
||||
screen = role.screen_or(&screen);
|
||||
replay_clipboard_echo(channel.clone(), sequence.clone(), replay.clone(), role.clone());
|
||||
}
|
||||
});
|
||||
})
|
||||
}));
|
||||
@@ -927,14 +1076,15 @@ impl WebRtcSession {
|
||||
|
||||
d.on_message(Box::new(move |msg: DataChannelMessage| {
|
||||
// Keep the whole state: a cached screen waiting for a closed Portal is not ready for input.
|
||||
let (screen, desktop_ready) = portal_desktop.as_ref().map_or_else(|| (screen.clone(), true), |desktop| {
|
||||
let (screen, desktop_ready) = portal_desktop.as_ref().map_or_else(|| (channel_role.screen_or(&screen), true), |desktop| {
|
||||
let state = desktop.snapshot();
|
||||
let ready = state.ready();
|
||||
(state.screen, ready)
|
||||
});
|
||||
let channel_name = lbl.clone();
|
||||
let input_worker = inp.clone();
|
||||
let message_input_session=channel_input_session.clone();
|
||||
let message_input_session=channel_role.input();
|
||||
let message_role=channel_role.clone();
|
||||
let terminal_worker = terminal_conn.clone();
|
||||
let rpc_out = outbound.clone();
|
||||
let reply_chan = d_reply.clone();
|
||||
@@ -1168,6 +1318,19 @@ impl WebRtcSession {
|
||||
if let Err(e) = chan.send(&bytes::Bytes::from(message)).await { log::warn!("Settings report failed: {}", e); }
|
||||
}
|
||||
});
|
||||
} else if action == sa::CAPTURE_DESKTOP && channel_name == "CONTROL_DATA_CHANNEL" {
|
||||
if message_role.is_desktop() && !message_role.stream_ended() {
|
||||
log::info!("SimpleAction 4 (capture_desktop) received on a desktop connection");
|
||||
} else if let Some(request) = message_role.request.get() {
|
||||
if message_role.is_desktop() {
|
||||
log::info!("SimpleAction 4 (capture_desktop) received on a tool connection whose desktop video ended; restarting it");
|
||||
} else {
|
||||
log::info!("SimpleAction 4 (capture_desktop) received on a tool connection; starting the desktop");
|
||||
}
|
||||
request();
|
||||
} else {
|
||||
log::info!("SimpleAction 4 (capture_desktop) not handled: tool connection without a desktop route");
|
||||
}
|
||||
} else if action > 1 && action != 15 {
|
||||
let args = action_fields.iter().find_map(|f| match (&f.value, f.number) { (WireValue::Bytes(v), 2) => Some(*v), _ => None }).unwrap_or(&[]);
|
||||
log::info!("SimpleAction {} ({}) not handled: args={}", action, crate::control_protocol::simple_action_name(action).unwrap_or("?"), sa::args_shape(args));
|
||||
@@ -1290,10 +1453,53 @@ impl WebRtcSession {
|
||||
capture,
|
||||
input,
|
||||
desktop,
|
||||
role,
|
||||
controller_mac: false,
|
||||
context,
|
||||
actions: session_actions,
|
||||
})
|
||||
}
|
||||
|
||||
/// Route for capture_desktop on this tool peer; the caller decides whether it may become the desktop.
|
||||
pub fn on_desktop_request(&self, request: impl Fn() + Send + Sync + 'static) {
|
||||
let _ = self.role.request.set(Box::new(request));
|
||||
}
|
||||
|
||||
/// Makes this tool peer the desktop: it takes the input lease, starts the video it negotiated
|
||||
/// (H.264; audio was never negotiated) and repeats the desktop CONTROL initialization.
|
||||
/// `screen` replaces a capture that was unavailable when the peer was offered.
|
||||
/// A promoted peer whose video ended keeps its input lease and only restarts the video.
|
||||
pub fn promote(&self, screen: Option<Arc<ScreenCapture>>) {
|
||||
let restart = self.role.is_desktop();
|
||||
if restart && !self.role.stream_ended() { return; }
|
||||
if let Some(screen) = screen { *self.role.screen.lock().unwrap() = Some(screen); }
|
||||
if !restart {
|
||||
let session = self.input.desktop_session();
|
||||
if self.pc.connection_state() == RTCPeerConnectionState::Connected { session.connected(); }
|
||||
*self.role.input.lock().unwrap() = Some(session);
|
||||
self.role.active.send_replace(true);
|
||||
}
|
||||
let stream = self.role.stream.lock().unwrap().clone();
|
||||
let task = stream.and_then(|stream| stream.spawn(self.role.screen.lock().unwrap().clone(), self.role.active.subscribe()));
|
||||
if let Some(old) = std::mem::replace(&mut *self.role.stream_task.lock().unwrap(), task) { old.abort(); }
|
||||
if restart { log::info!("Desktop video restarted on a tool connection after its stream ended"); return; }
|
||||
if !cfg!(test) { track_session(self.pc.clone(), self.actions.clone(), &self.role); }
|
||||
log::info!("Desktop started on an existing tool connection");
|
||||
}
|
||||
|
||||
/// The capture this peer's desktop uses: its own, or the one reinitialized at promotion.
|
||||
pub fn desktop_capture(&self) -> Arc<ScreenCapture> { self.role.screen_or(&self.capture) }
|
||||
|
||||
/// A newer desktop took over a promoted tool peer: stop its video, input and session count,
|
||||
/// keep the connection's tools. capture_desktop on it promotes it again.
|
||||
pub fn demote(&self) {
|
||||
if self.desktop || !self.role.is_desktop() { return; }
|
||||
self.role.active.send_replace(false);
|
||||
if let Some(task) = self.role.stream_task.lock().unwrap().take() { task.abort(); }
|
||||
if let Some(session) = self.role.input.lock().unwrap().take() { session.disconnected(); }
|
||||
log::info!("Desktop on a tool connection stopped: a newer desktop took over");
|
||||
}
|
||||
|
||||
/// 应用远端 Offer,生成并返回 Answer
|
||||
pub async fn handle_offer(&self, soac: &SoacMessage) -> Result<String, String> {
|
||||
// 同步当前的客户端与房间会话上下文
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
//! Socket.IO's WebSocket transport uses the same opt-in proxy policy as the REST client.
|
||||
//! No proxy addresses, room headers, response text or underlying errors are displayed.
|
||||
use std::time::Duration;
|
||||
use reqwest::header::{HeaderMap, CONNECTION, UPGRADE, SEC_WEBSOCKET_ACCEPT, SEC_WEBSOCKET_KEY,
|
||||
SEC_WEBSOCKET_EXTENSIONS, SEC_WEBSOCKET_PROTOCOL};
|
||||
use tokio_tungstenite::{WebSocketStream, tungstenite::{handshake::{client::Request, derive_accept_key}, protocol::Role}};
|
||||
|
||||
pub type Socket = WebSocketStream<reqwest::Upgraded>;
|
||||
|
||||
/// Validate configuration rather than let reqwest silently ignore an unsupported proxy URL.
|
||||
/// Selection/NO_PROXY matching still belongs to reqwest, not a second proxy implementation.
|
||||
fn validate_proxy_url(value: &str) -> Result<(), &'static str> {
|
||||
if value.is_empty() { return Ok(()); }
|
||||
// Match reqwest's system-proxy parser before URL normalization (which would accept
|
||||
// whitespace/Unicode that http::Uri rejects and the connector would silently ignore).
|
||||
let uri: tokio_tungstenite::tungstenite::http::Uri = value.parse().map_err(|_| "Invalid signaling proxy configuration")?;
|
||||
if uri.authority().is_none() { return Err("Invalid signaling proxy configuration"); }
|
||||
let url = if value.contains("://") { reqwest::Url::parse(value) }
|
||||
else { reqwest::Url::parse(&format!("http://{value}")) }
|
||||
.map_err(|_| "Invalid signaling proxy configuration")?;
|
||||
if !matches!(url.scheme(), "http" | "https") || url.host_str().is_none()
|
||||
|| url.port_or_known_default().is_none() || url.path() != "/" || url.query().is_some() || url.fragment().is_some() {
|
||||
return Err("Unsupported signaling proxy configuration (HTTP/HTTPS only)");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn client() -> Result<reqwest::Client, String> {
|
||||
if crate::auth::proxy_enabled() {
|
||||
let mut configured = false;
|
||||
for names in [["HTTP_PROXY", "http_proxy"], ["HTTPS_PROXY", "https_proxy"], ["ALL_PROXY", "all_proxy"]] {
|
||||
if let Some(value) = names.iter().find_map(std::env::var_os) {
|
||||
let value = value.to_str().ok_or("Invalid signaling proxy configuration")?;
|
||||
validate_proxy_url(value)?;
|
||||
configured |= !value.is_empty();
|
||||
}
|
||||
}
|
||||
// reqwest disables environment proxies in CGI contexts; don't silently ignore an opt-in.
|
||||
if configured && std::env::var_os("REQUEST_METHOD").is_some() {
|
||||
return Err("Signaling environment proxies are disabled in CGI contexts".into());
|
||||
}
|
||||
}
|
||||
crate::auth::http_client_builder().http1_only().redirect(reqwest::redirect::Policy::none())
|
||||
.retry(reqwest::retry::never()).build().map_err(|_| "Signaling HTTP client setup failed".into())
|
||||
}
|
||||
|
||||
pub async fn connect(request: Request, timeout: Duration) -> Result<Socket, String> {
|
||||
let client = client()?;
|
||||
match tokio::time::timeout(timeout, connect_with_client(&client, request)).await {
|
||||
Ok(result) => result,
|
||||
Err(_) => Err("WS connect failed: timeout".into()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn connect_with_client(client: &reqwest::Client, request: Request) -> Result<Socket, String> {
|
||||
let (mut parts, _) = request.into_parts();
|
||||
if parts.method != reqwest::Method::GET || parts.headers.contains_key(SEC_WEBSOCKET_EXTENSIONS)
|
||||
|| parts.headers.contains_key(SEC_WEBSOCKET_PROTOCOL) || parts.headers.contains_key("proxy-authorization") {
|
||||
return Err("Unsupported signaling WebSocket request".into());
|
||||
}
|
||||
let mut url = reqwest::Url::parse(&parts.uri.to_string()).map_err(|_| "Invalid signaling endpoint")?;
|
||||
if !url.username().is_empty() || url.password().is_some() || url.fragment().is_some() {
|
||||
return Err("Invalid signaling endpoint".into());
|
||||
}
|
||||
let scheme = match url.scheme() { "ws" => "http", "wss" => "https", _ => return Err("Unsupported signaling endpoint scheme".into()) };
|
||||
url.set_scheme(scheme).map_err(|_| "Invalid signaling endpoint")?;
|
||||
let key = parts.headers.get(SEC_WEBSOCKET_KEY).ok_or("Missing WebSocket handshake key")?;
|
||||
let accept = derive_accept_key(key.as_bytes());
|
||||
for name in ["x-nrd-auth", "authorization", "cookie"] {
|
||||
if let Some(value) = parts.headers.get_mut(name) { value.set_sensitive(true); }
|
||||
}
|
||||
// reqwest owns DNS/TCP/proxy CONNECT/TLS. No custom relay, direct fallback or redirect.
|
||||
let response = client.get(url).version(reqwest::Version::HTTP_11).headers(parts.headers).send().await
|
||||
.map_err(|e| if e.is_timeout() { "WS connect failed: timeout" } else if e.is_connect() { "WS connect failed: connection" } else { "WS connect failed: request" })?;
|
||||
if response.status() != reqwest::StatusCode::SWITCHING_PROTOCOLS {
|
||||
return Err(format!("WS upgrade refused: http_status={}", response.status().as_u16()));
|
||||
}
|
||||
verify_upgrade(response.version(), response.headers(), &accept)?;
|
||||
let stream = response.upgrade().await.map_err(|_| "WS upgrade failed")?;
|
||||
Ok(WebSocketStream::from_raw_socket(stream, Role::Client, None).await)
|
||||
}
|
||||
|
||||
/// RFC 6455 §4.1: validate before handing upgraded bytes to the frame codec. Socket.IO
|
||||
/// requests no extensions/subprotocols. Unlike a generic HTTP client we never follow redirects.
|
||||
fn verify_upgrade(version: reqwest::Version, headers: &HeaderMap, accept: &str) -> Result<(), &'static str> {
|
||||
let single = |name: reqwest::header::HeaderName| {
|
||||
let mut values = headers.get_all(name).iter();
|
||||
let value = values.next()?.to_str().ok()?;
|
||||
values.next().is_none().then_some(value)
|
||||
};
|
||||
let connection_upgrade = headers.get_all(CONNECTION).iter().all(|value| value.to_str().is_ok())
|
||||
&& headers.get_all(CONNECTION).iter().any(|value|
|
||||
value.to_str().is_ok_and(|value| value.split(',').any(|token| token.trim().eq_ignore_ascii_case("upgrade"))));
|
||||
if version != reqwest::Version::HTTP_11 || !connection_upgrade
|
||||
|| !single(UPGRADE).is_some_and(|v| v.eq_ignore_ascii_case("websocket"))
|
||||
|| single(SEC_WEBSOCKET_ACCEPT) != Some(accept)
|
||||
|| headers.contains_key(SEC_WEBSOCKET_EXTENSIONS) || headers.contains_key(SEC_WEBSOCKET_PROTOCOL) {
|
||||
return Err("Invalid signaling WebSocket upgrade response");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "ws_transport_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,245 @@
|
||||
use super::*;
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use tokio::{io::{AsyncRead, AsyncReadExt, AsyncWriteExt}, net::{TcpListener, TcpStream}};
|
||||
use tokio_tungstenite::tungstenite::{client::IntoClientRequest, Message};
|
||||
|
||||
fn request(url: &str) -> Request {
|
||||
let mut request = url.into_client_request().unwrap();
|
||||
request.headers_mut().insert("x-nrd-auth", "fixture-room-secret".parse().unwrap());
|
||||
request
|
||||
}
|
||||
|
||||
fn test_client() -> reqwest::ClientBuilder {
|
||||
reqwest::Client::builder().no_proxy().http1_only()
|
||||
.redirect(reqwest::redirect::Policy::none()).retry(reqwest::retry::never())
|
||||
}
|
||||
|
||||
async fn read_headers<S: AsyncRead + Unpin>(socket: &mut S) -> String {
|
||||
let mut bytes = Vec::new();
|
||||
while !bytes.ends_with(b"\r\n\r\n") {
|
||||
let mut byte = [0];
|
||||
socket.read_exact(&mut byte).await.unwrap();
|
||||
bytes.push(byte[0]);
|
||||
assert!(bytes.len() < 16384);
|
||||
}
|
||||
String::from_utf8(bytes).unwrap()
|
||||
}
|
||||
|
||||
fn header<'a>(request: &'a str, name: &str) -> Option<&'a str> {
|
||||
request.lines().filter_map(|line| line.split_once(':'))
|
||||
.find(|(key, _)| key.eq_ignore_ascii_case(name)).map(|(_, value)| value.trim())
|
||||
}
|
||||
|
||||
async fn accept_upgrade(listener: TcpListener, proxy: bool) -> TcpStream {
|
||||
let (mut socket, _) = listener.accept().await.unwrap();
|
||||
let received = read_headers(&mut socket).await;
|
||||
assert!(received.starts_with(if proxy { "GET http://" } else { "GET /" }));
|
||||
assert!(header(&received, "x-nrd-auth") == Some("fixture-room-secret"));
|
||||
if !proxy { assert!(header(&received, "proxy-authorization").is_none()); }
|
||||
let accept = derive_accept_key(header(&received, "sec-websocket-key").unwrap().as_bytes());
|
||||
let mut response = format!("HTTP/1.1 101 Switching Protocols\r\nConnection: keep-alive, Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Accept: {accept}\r\n\r\n").into_bytes();
|
||||
// The first Engine.IO frame shares a single write with the HTTP response.
|
||||
response.extend_from_slice(&[0x81, 3, b'0', b'{', b'}']);
|
||||
socket.write_all(&response).await.unwrap();
|
||||
socket
|
||||
}
|
||||
|
||||
async fn no_connection(listener: &TcpListener) {
|
||||
assert!(tokio::time::timeout(Duration::from_millis(150), listener.accept()).await.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strict_upgrade_validation_accepts_tokens_and_rejects_unsolicited_or_ambiguous_headers() {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(CONNECTION, "keep-alive, Upgrade".parse().unwrap());
|
||||
headers.insert(UPGRADE, "WebSocket".parse().unwrap());
|
||||
headers.insert(SEC_WEBSOCKET_ACCEPT, "fixture-accept".parse().unwrap());
|
||||
assert!(verify_upgrade(reqwest::Version::HTTP_11, &headers, "fixture-accept").is_ok());
|
||||
headers.append(CONNECTION, "keep-alive".parse().unwrap());
|
||||
assert!(verify_upgrade(reqwest::Version::HTTP_11, &headers, "fixture-accept").is_ok());
|
||||
assert!(verify_upgrade(reqwest::Version::HTTP_2, &headers, "fixture-accept").is_err());
|
||||
assert!(verify_upgrade(reqwest::Version::HTTP_11, &headers, "wrong").is_err());
|
||||
for name in [CONNECTION, UPGRADE, SEC_WEBSOCKET_ACCEPT] {
|
||||
let mut missing = headers.clone(); missing.remove(name);
|
||||
assert!(verify_upgrade(reqwest::Version::HTTP_11, &missing, "fixture-accept").is_err());
|
||||
}
|
||||
for name in [UPGRADE, SEC_WEBSOCKET_ACCEPT, SEC_WEBSOCKET_EXTENSIONS, SEC_WEBSOCKET_PROTOCOL] {
|
||||
let mut invalid = headers.clone(); invalid.append(name, "fixture-server-secret".parse().unwrap());
|
||||
let error = verify_upgrade(reqwest::Version::HTTP_11, &invalid, "fixture-accept").unwrap_err();
|
||||
assert!(!error.contains("fixture"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn proxy_configuration_errors_never_echo_proxy_values() {
|
||||
for proxy in ["", "127.0.0.1:3128", "http://127.0.0.1:3128", "https://fixture-user:[email protected]:3128"] {
|
||||
assert!(validate_proxy_url(proxy).is_ok());
|
||||
}
|
||||
for proxy in ["socks5://fixture-user:[email protected]:3128", "http://127.0.0.1:3128/?fixture-token", "http://127.0.0.1:3128/#fixture-token", "http://127.0.0.1:3128/fixture-secret", "http://[fixture-invalid", " http://127.0.0.1:3128", "http://127.0.0.1:3128 ", "http://例子.invalid:3128"] {
|
||||
let error = validate_proxy_url(proxy).unwrap_err();
|
||||
assert!(!error.contains("fixture"));
|
||||
assert!(!error.contains("127.0.0.1"));
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn upgrade_keeps_prefetched_frame_and_does_not_limit_socket_lifetime() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let url = format!("ws://{}/socket.io/?fixture-query-secret", listener.local_addr().unwrap());
|
||||
let server = tokio::spawn(async move {
|
||||
let socket = accept_upgrade(listener, false).await;
|
||||
let mut socket = WebSocketStream::from_raw_socket(socket, Role::Server, None).await;
|
||||
tokio::time::sleep(Duration::from_millis(1200)).await;
|
||||
assert!(matches!(socket.next().await, Some(Ok(Message::Text(value))) if value == "fixture-client-frame"));
|
||||
socket.send(Message::Text("fixture-late-frame".into())).await.unwrap();
|
||||
});
|
||||
let http = test_client().build().unwrap();
|
||||
let mut socket = tokio::time::timeout(Duration::from_secs(1), connect_with_client(&http, request(&url))).await.unwrap().unwrap();
|
||||
assert!(matches!(socket.next().await, Some(Ok(Message::Text(value))) if value == "0{}"));
|
||||
// A one-second establishment deadline must not remain attached to the upgraded stream.
|
||||
tokio::time::sleep(Duration::from_millis(1300)).await;
|
||||
socket.send(Message::Text("fixture-client-frame".into())).await.unwrap();
|
||||
let message = tokio::time::timeout(Duration::from_secs(3), socket.next()).await.unwrap();
|
||||
assert!(matches!(message, Some(Ok(Message::Text(value))) if value == "fixture-late-frame"));
|
||||
server.await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn proxy_refusal_never_falls_back_to_origin_or_echoes_server_content() {
|
||||
let origin = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let proxy = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let proxy_url = format!("http://{}", proxy.local_addr().unwrap());
|
||||
let server = tokio::spawn(async move {
|
||||
let (mut socket, _) = proxy.accept().await.unwrap();
|
||||
let received = read_headers(&mut socket).await;
|
||||
assert!(received.starts_with("GET http://"));
|
||||
assert!(header(&received, "proxy-authorization").is_some());
|
||||
socket.write_all(b"HTTP/1.1 407 Proxy Authentication Required\r\nContent-Length: 19\r\n\r\nfixture-body-secret").await.unwrap();
|
||||
});
|
||||
let proxy = reqwest::Proxy::all(proxy_url).unwrap().basic_auth("fixture-user", "fixture-proxy-secret");
|
||||
let http = test_client().proxy(proxy).build().unwrap();
|
||||
let result = tokio::time::timeout(Duration::from_secs(5),
|
||||
connect_with_client(&http, request(&format!("ws://{}/fixture-path-secret", origin.local_addr().unwrap()))))
|
||||
.await.expect("proxy refusal must not wait on a fallback origin");
|
||||
let error = result.err().expect("proxy refusal must fail");
|
||||
assert!(error.contains("407")); assert!(!error.contains("fixture"));
|
||||
no_connection(&origin).await;
|
||||
server.await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn redirects_are_not_followed_with_room_credentials() {
|
||||
let origin = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let destination = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let url = format!("ws://{}/socket.io/", origin.local_addr().unwrap());
|
||||
let location = format!("http://{}/fixture-redirect-secret", destination.local_addr().unwrap());
|
||||
let server = tokio::spawn(async move {
|
||||
let (mut socket, _) = origin.accept().await.unwrap();
|
||||
let _ = read_headers(&mut socket).await;
|
||||
socket.write_all(format!("HTTP/1.1 302 Found\r\nLocation: {location}\r\nContent-Length: 0\r\n\r\n").as_bytes()).await.unwrap();
|
||||
});
|
||||
let result = tokio::time::timeout(Duration::from_secs(5),
|
||||
connect_with_client(&test_client().build().unwrap(), request(&url)))
|
||||
.await.expect("redirect must not wait on its destination");
|
||||
let error = result.err().expect("redirect must fail");
|
||||
assert!(error.contains("302")); assert!(!error.contains("fixture"));
|
||||
no_connection(&destination).await;
|
||||
server.await.unwrap();
|
||||
}
|
||||
|
||||
// No global environment mutation: the subprocess has only synthetic loopback configuration.
|
||||
#[test]
|
||||
fn environment_policy_probe() {
|
||||
let Ok(url) = std::env::var("UU_WS_TEST_ENDPOINT") else { return; };
|
||||
assert!(url.starts_with("ws://127.0.0.1:"));
|
||||
let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
|
||||
runtime.block_on(async {
|
||||
let mut socket = connect(request(&url), Duration::from_secs(5)).await.expect("fixture connection failed");
|
||||
let message = tokio::time::timeout(Duration::from_secs(3), socket.next()).await.unwrap();
|
||||
assert!(matches!(message, Some(Ok(Message::Text(value))) if value == "0{}"));
|
||||
});
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn environment_proxy_opt_in_and_no_proxy_match_rest_policy() {
|
||||
for (opt_in, bypass, via_proxy) in [(false, false, false), (true, false, true), (true, true, false)] {
|
||||
let origin = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let proxy = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let url = format!("ws://{}/socket.io/", origin.local_addr().unwrap());
|
||||
let proxy_url = format!("http://fixture-user:fixture-proxy-secret@{}", proxy.local_addr().unwrap());
|
||||
let (selected, unused) = if via_proxy { (proxy, origin) } else { (origin, proxy) };
|
||||
let server = tokio::spawn(async move { let _socket = accept_upgrade(selected, via_proxy).await; });
|
||||
let mut child = tokio::process::Command::new(std::env::current_exe().unwrap());
|
||||
child.args(["--exact", "ws_transport::tests::environment_policy_probe", "--nocapture"])
|
||||
.env_clear().env("UU_WS_TEST_ENDPOINT", url).env("HTTP_PROXY", proxy_url);
|
||||
if opt_in { child.env("UU_USE_PROXY", "1"); }
|
||||
if bypass { child.env("NO_PROXY", "127.0.0.1"); }
|
||||
let output = tokio::time::timeout(Duration::from_secs(10), child.output()).await.unwrap().unwrap();
|
||||
assert!(output.status.success(), "isolated proxy-policy subprocess failed");
|
||||
assert!(!output.stdout.windows(b"fixture-room-secret".len()).any(|s| s == b"fixture-room-secret"));
|
||||
assert!(!output.stderr.windows(b"fixture-proxy-secret".len()).any(|s| s == b"fixture-proxy-secret"));
|
||||
no_connection(&unused).await;
|
||||
server.await.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn wss_connect_keeps_origin_auth_inside_tls_and_requires_trusted_certificate() {
|
||||
use std::sync::Arc;
|
||||
use tokio_rustls::{rustls, TlsAcceptor};
|
||||
use rustls::pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer};
|
||||
// A fresh test certificate/key, kept only in memory. The mock does not relay anywhere:
|
||||
// it accepts CONNECT and then speaks TLS + WebSocket on that same loopback socket.
|
||||
let cert = rcgen::generate_simple_self_signed(vec!["localhost".into()]).unwrap();
|
||||
let cert_der = cert.serialize_der().unwrap();
|
||||
let root = reqwest::Certificate::from_der(&cert_der).unwrap();
|
||||
let config = rustls::ServerConfig::builder_with_provider(Arc::new(rustls::crypto::ring::default_provider()))
|
||||
.with_safe_default_protocol_versions().unwrap().with_no_client_auth()
|
||||
.with_single_cert(vec![CertificateDer::from(cert_der)],
|
||||
PrivateKeyDer::Pkcs8(PrivatePkcs8KeyDer::from(cert.serialize_private_key_der()))).unwrap();
|
||||
let acceptor = TlsAcceptor::from(Arc::new(config));
|
||||
for trusted in [false, true] {
|
||||
let origin = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let proxy = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let port = origin.local_addr().unwrap().port();
|
||||
let proxy_url = format!("http://{}", proxy.local_addr().unwrap());
|
||||
let acceptor = acceptor.clone();
|
||||
let server = tokio::spawn(async move {
|
||||
let (mut socket, _) = proxy.accept().await.unwrap();
|
||||
let received = read_headers(&mut socket).await;
|
||||
assert!(received.starts_with(&format!("CONNECT localhost:{port} HTTP/1.1\r\n")));
|
||||
assert!(header(&received, "x-nrd-auth").is_none());
|
||||
assert!(header(&received, "proxy-authorization").is_some());
|
||||
assert!(!received.contains("fixture-room-secret"));
|
||||
socket.write_all(b"HTTP/1.1 200 Connection Established\r\n\r\n").await.unwrap();
|
||||
let tls = acceptor.accept(socket).await;
|
||||
if !trusted { assert!(tls.is_err()); return; }
|
||||
let mut tls = tls.expect("fixture TLS handshake failed");
|
||||
let received = read_headers(&mut tls).await;
|
||||
assert!(received.starts_with("GET /socket.io/"));
|
||||
assert!(header(&received, "x-nrd-auth") == Some("fixture-room-secret"));
|
||||
assert!(header(&received, "proxy-authorization").is_none());
|
||||
assert!(!received.contains("fixture-proxy-secret"));
|
||||
let accept = derive_accept_key(header(&received, "sec-websocket-key").unwrap().as_bytes());
|
||||
let mut response = format!("HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Accept: {accept}\r\n\r\n").into_bytes();
|
||||
response.extend_from_slice(&[0x81, 3, b'0', b'{', b'}']);
|
||||
tls.write_all(&response).await.unwrap();
|
||||
});
|
||||
let proxy = reqwest::Proxy::all(proxy_url).unwrap().basic_auth("fixture-user", "fixture-proxy-secret");
|
||||
let mut builder = test_client().proxy(proxy);
|
||||
if trusted { builder = builder.add_root_certificate(root.clone()); }
|
||||
let client = builder.build().unwrap();
|
||||
let result = tokio::time::timeout(Duration::from_secs(5),
|
||||
connect_with_client(&client, request(&format!("wss://localhost:{port}/socket.io/?fixture-query-secret")))).await.unwrap();
|
||||
if trusted {
|
||||
let mut socket = result.expect("trusted fixture WebSocket failed");
|
||||
let message = tokio::time::timeout(Duration::from_secs(3), socket.next()).await.unwrap();
|
||||
assert!(matches!(message, Some(Ok(Message::Text(value))) if value == "0{}"));
|
||||
} else {
|
||||
let error = result.err().expect("untrusted fixture certificate must fail");
|
||||
assert!(!error.contains("fixture")); assert!(!error.contains("localhost"));
|
||||
}
|
||||
no_connection(&origin).await;
|
||||
tokio::time::timeout(Duration::from_secs(5), server).await.unwrap().unwrap();
|
||||
}
|
||||
}
|
||||
@@ -121,13 +121,17 @@ impl PeerConnectionInternal {
|
||||
ice_gather_policy: configuration.ice_transport_policy,
|
||||
})?);
|
||||
|
||||
// Create the ice transport
|
||||
pc.ice_transport = pc.create_ice_transport(api).await;
|
||||
// Create the ice transport. Its state callback must read the real DTLS
|
||||
// transport, which only exists after the ICE transport, so it is wired
|
||||
// in through a weak slot (the placeholder would always report New).
|
||||
let dtls_slot = Arc::new(std::sync::OnceLock::new());
|
||||
pc.ice_transport = pc.create_ice_transport(api, Arc::clone(&dtls_slot)).await;
|
||||
|
||||
// Create the DTLS transport
|
||||
let certificates = configuration.certificates.drain(..).collect();
|
||||
pc.dtls_transport =
|
||||
Arc::new(api.new_dtls_transport(Arc::clone(&pc.ice_transport), certificates)?);
|
||||
let _ = dtls_slot.set(Arc::downgrade(&pc.dtls_transport));
|
||||
|
||||
// Create the SCTP transport
|
||||
pc.sctp_transport = Arc::new(api.new_sctp_transport(Arc::clone(&pc.dtls_transport))?);
|
||||
@@ -1106,13 +1110,16 @@ impl PeerConnectionInternal {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn create_ice_transport(&self, api: &API) -> Arc<RTCIceTransport> {
|
||||
pub(super) async fn create_ice_transport(
|
||||
&self,
|
||||
api: &API,
|
||||
dtls_slot: Arc<std::sync::OnceLock<Weak<RTCDtlsTransport>>>,
|
||||
) -> Arc<RTCIceTransport> {
|
||||
let ice_transport = Arc::new(api.new_ice_transport(Arc::clone(&self.ice_gatherer)));
|
||||
|
||||
let ice_connection_state = Arc::clone(&self.ice_connection_state);
|
||||
let peer_connection_state = Arc::clone(&self.peer_connection_state);
|
||||
let is_closed = Arc::clone(&self.is_closed);
|
||||
let dtls_transport = Arc::clone(&self.dtls_transport);
|
||||
let on_ice_connection_state_change_handler =
|
||||
Arc::clone(&self.on_ice_connection_state_change_handler);
|
||||
let on_peer_connection_state_change_handler =
|
||||
@@ -1139,7 +1146,11 @@ impl PeerConnectionInternal {
|
||||
let on_peer_connection_state_change_handler2 =
|
||||
Arc::clone(&on_peer_connection_state_change_handler);
|
||||
let is_closed2 = Arc::clone(&is_closed);
|
||||
let dtls_transport_state = dtls_transport.state();
|
||||
let dtls_transport_state = dtls_slot
|
||||
.get()
|
||||
.and_then(Weak::upgrade)
|
||||
.map(|dtls| dtls.state())
|
||||
.unwrap_or(RTCDtlsTransportState::New);
|
||||
let peer_connection_state2 = Arc::clone(&peer_connection_state);
|
||||
Box::pin(async move {
|
||||
RTCPeerConnection::do_ice_connection_state_change(
|
||||
|
||||
Reference in new issue
Block a user