From 0afc602e66c3f7a25963e550ca47d7fb6ff67c59 Mon Sep 17 00:00:00 2001 From: VallierDev <24893073+VallierDev@users.noreply.github.com> Date: Tue, 20 Oct 2026 16:29:57 +0800 Subject: [PATCH] Login polling backoff and a hint for instantly expiring QR codes (#68); bitrate unit safeguard; doctor wording - Never spin on immediate unchanged status answers or transport errors (2 s backoff); repeated QR codes that expire within seconds back off exponentially and point to re-registering the device identity, which recovered a flagged device in #68. - Custom bitrate field 8 is bit/s (#67); values that can only be Mbit/s or kbit/s are scaled instead of capping the encoder at 1 kbit/s. - doctor: without DISPLAY the X11 engine still tries :0/:1, so the #66 warning says that. Co-Authored-By: Claude Opus 5.5 --- src/capture_setting.rs | 17 ++++++++++++++++- src/doctor.rs | 2 +- src/login.rs | 23 ++++++++++++++++++++--- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/capture_setting.rs b/src/capture_setting.rs index 5d124a4..eed91fa 100644 --- a/src/capture_setting.rs +++ b/src/capture_setting.rs @@ -112,6 +112,14 @@ fn fps_from_option(value: u64) -> Option { /// Applies a capture-setting RPC to `settings` and returns the RpcResponse payload (Message 22), /// or None when the RPC is not a capture/fps/quality setting. +/// max_custom_bitrate (field 8) in bit/s: the mobile viewer sends 1_000_000 for 1 Mbps (#67). Values +/// that can only be a different unit stay usable instead of collapsing to a 1 kbit/s cap: below 1000 +/// they are Mbit/s, below 200_000 kbit/s (no viewer offers custom rates under 200 kbit/s). +fn custom_bitrate_bps(value: u64) -> u32 { + let bps = if value < 1_000 { value * 1_000_000 } else if value < 200_000 { value * 1_000 } else { value }; + bps.min(u64::from(u32::MAX)) as u32 +} + pub fn handle_rpc(rpc: &[WireField], settings: &watch::Sender, native: (u32, u32)) -> Option> { handle_rpc_with_codecs(rpc, settings, native, true, false) } @@ -148,7 +156,7 @@ fn apply_rpc(rpc: &[WireField], settings: &watch::Sender, native: // Field 1 is the viewer's fps option index (1 = 30, 2 = 60, ...); 18 fpsCount is the frame rate. if let Some(fps) = number(&fields, 18).filter(|v| *v > 0).or_else(|| number(&fields, 1).and_then(fps_from_option)) { next.fps = fps as u32; } if let Some(quality) = number(&fields, 2).filter(|v| *v > 0) { next.quality = quality; } - if let Some(bitrate) = number(&fields, 8).filter(|v| *v > 0) { next.custom_bps = bitrate.min(u64::from(u32::MAX)) as u32; } + if let Some(bitrate) = number(&fields, 8).filter(|v| *v > 0) { next.custom_bps = custom_bitrate_bps(bitrate); } // Field 7 chorma_format (sic): 3 = 4:4:4 when 真彩 is on, 1 = 4:2:0. The Sync reports what is encoded. match number(&fields, 7) { Some(CHROMA_444) if full_chroma => next.chroma = CHROMA_444, @@ -487,6 +495,13 @@ mod tests { } } + #[test] + fn small_custom_bitrates_are_read_as_larger_units() { + assert_eq!(custom_bitrate_bps(20), 20_000_000); + assert_eq!(custom_bitrate_bps(8_000), 8_000_000); + assert_eq!(custom_bitrate_bps(1_000_000), 1_000_000); + } + #[test] fn oversized_custom_bitrate_does_not_wrap_before_encoder_limit() { let (tx, rx) = watch::channel(VideoSettings::default()); diff --git a/src/doctor.rs b/src/doctor.rs index e6360cd..6fade31 100644 --- a/src/doctor.rs +++ b/src/doctor.rs @@ -77,7 +77,7 @@ pub async fn run() -> Result<(), String> { // A session without any display cannot capture a screen; say so instead of naming a backend. if let Ok(selected) = &selected { if missing_display(*selected, backend::headless(), std::env::var_os("DISPLAY").as_deref(), std::env::var_os("WAYLAND_DISPLAY").as_deref()) { - println!(" ! no display in this session (DISPLAY and WAYLAND_DISPLAY are unset): the desktop backend cannot capture a screen"); + println!(" ! DISPLAY and WAYLAND_DISPLAY are unset: the X11 engine will only try :0 and :1; without an X server there, no screen can be captured"); println!(" terminal, file transfer and port mapping still work; UU_DESKTOP_BACKEND=none marks the host as headless"); } } diff --git a/src/login.rs b/src/login.rs index 94f25d2..176f2e9 100644 --- a/src/login.rs +++ b/src/login.rs @@ -268,6 +268,7 @@ pub async fn prepare_device() -> Result<(Value, Credentials), String> { pub(crate) async fn flow(ui: &LoginUi) -> Result { let (mut cfg, creds) = prepare_device().await?; let client = UUAuthClient::new(creds.clone()); + let mut instant_expiries = 0u32; loop { let (status, resp) = client.post_json("/api/v1/qrcode/gen/login", &json!({})).await?; @@ -293,11 +294,14 @@ pub(crate) async fn flow(ui: &LoginUi) -> Result { // 1 waiting, 2 scanned, 4 after the phone confirmed); the server answers when it changes. let mut login_status = 1i64; let mut expired = false; + let generated = std::time::Instant::now(); for _ in 0..150 { let query = json!({"token": ticket, "qrcode_jump_url": url, "login_status": login_status}); + let asked = std::time::Instant::now(); let (status, resp) = match client.post_json("/api/v1/qrcode/login/status", &query).await { Ok(r) => r, - Err(_) => continue, // long-poll timeout: ask again + // Long-poll timeout: ask again, but never spin on transport errors (#68). + Err(_) => { if asked.elapsed() < Duration::from_secs(1) { tokio::time::sleep(Duration::from_secs(2)).await; } continue; } }; // Code 1190 (HTTP 400, "二维码已失效"): the QR expired; generate a new one. if resp.get("code").and_then(|c| c.as_i64()) == Some(QR_EXPIRED) { @@ -305,7 +309,12 @@ pub(crate) async fn flow(ui: &LoginUi) -> Result { expired = true; break; } let st = data("qrcode status", status, &resp)?.get("status").and_then(|v| v.as_i64()).unwrap_or(login_status); - if st == login_status { continue; } + if st == login_status { + // The server normally holds this request; an immediate unchanged answer must not + // turn into a tight loop of dozens of requests per second (#68). + if asked.elapsed() < Duration::from_secs(1) { tokio::time::sleep(Duration::from_secs(2)).await; } + continue; + } login_status = st; match st { 2 => { @@ -343,7 +352,15 @@ pub(crate) async fn flow(ui: &LoginUi) -> Result { } } println!("{}", if expired { "QR code expired; generating a new one" } else { "QR code timed out; generating a new one" }); - tokio::time::sleep(Duration::from_secs(1)).await; // never spin if the server keeps refusing + // A QR rejected within seconds of being generated, again and again, was seen when the cloud + // flagged this device identity (#68): back off and say how to recover. + if expired && generated.elapsed() < Duration::from_secs(10) { instant_expiries += 1; } else { instant_expiries = 0; } + if instant_expiries == 3 { + eprintln!("The QR code keeps expiring immediately. If this persists, the cloud may reject this device identity: \ + back up and remove ~/.config/uuremote/config.json, then run `uuremote-server login` again to register a new device."); + ui.status("二维码反复立即失效:可备份并删除 ~/.config/uuremote/config.json 后重新登录(会注册新设备)", Tone::Error); + } + tokio::time::sleep(Duration::from_secs(1u64 << instant_expiries.min(5))).await; // never spin if the server keeps refusing } }