mirror of
https://github.com/rustdesk/rustdesk.git
synced 2026-12-04 15:46:07 +00:00
Fix/privacy mode display crash recovery (#17333)
* fix(windows): recover privacy mode displays after a forced exit Signed-off-by: fufesou <linlong1266@gmail.com> * fix(windows): retain pending privacy display recovery across restarts Signed-off-by: fufesou <linlong1266@gmail.com> * fix(windows): preserve pending privacy recovery during cleanup and activation Signed-off-by: fufesou <linlong1266@gmail.com> * fix(windows): clear obsolete privacy display recovery records Recheck active Amyuni displays during the existing monitor timeout before retiring a missing target. Keep the record for ambiguous selections and unplug errors so a stale record does not block the next privacy session after the monitor is gone. Read the saved Windows display name through its first terminator. Cover stale and temporary absences separately from a failed driver request. Signed-off-by: fufesou <linlong1266@gmail.com> * fix(windows): retire missing privacy displays with other monitors active Check for the saved Amyuni display before treating multiple active displays as ambiguous. Two unrelated displays can then remain connected while an obsolete recovery record expires. Cover that case in the existing recovery selection test. Signed-off-by: fufesou <linlong1266@gmail.com> * fix(windows): recheck pending display recovery before activation A Mode 2 request can skip monitor preparation when the owner or selected implementation changes. Check the saved recovery record under the activation lock before enumerating displays so the new session cannot replace the pending record. Exercise activation without preparation in the isolated Windows recovery test. Signed-off-by: fufesou <linlong1266@gmail.com> * fix(windows): attempt privacy display recovery once at startup Select a sole matching Amyuni display from the first scan and verify removal after the driver request. Consume the record after checking the registry even on unplug failure, so preparation, activation and idle shutdown can use their normal paths. Signed-off-by: fufesou <linlong1266@gmail.com> * fix(windows): limit privacy display recovery to server startup Read the saved Amyuni display only in the startup unplug branch. Keep the existing registry parser and unconditional record reset, including when display recovery fails. Cover the display-only record and legacy parsing without a callback or child process. Signed-off-by: fufesou <linlong1266@gmail.com> --------- Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
1 parent
805b5a5a23
commit
f705ebeb8d
2 files changed
+107
-6
No files matched your search
@@ -32,6 +32,7 @@ use winapi::{
|
||||
pub(super) const PRIVACY_MODE_IMPL: &str = super::PRIVACY_MODE_IMPL_WIN_VIRTUAL_DISPLAY;
|
||||
|
||||
const CONFIG_KEY_REG_RECOVERY: &str = base::config::keys::OPTION_REG_RECOVERY;
|
||||
const ADDED_DISPLAY: &str = "added_display";
|
||||
const STATE_CHECK_LOG_INTERVAL: Duration = Duration::from_secs(60);
|
||||
|
||||
fn check_active_displays(displays: impl Iterator<Item = DISPLAY_DEVICEW>) -> ResultType<()> {
|
||||
@@ -137,6 +138,11 @@ struct Display {
|
||||
primary: bool,
|
||||
}
|
||||
|
||||
fn decode_display_name(name: &[WCHAR]) -> Result<String, std::string::FromUtf16Error> {
|
||||
let end = name.iter().position(|&c| c == 0).unwrap_or(name.len());
|
||||
String::from_utf16(&name[..end])
|
||||
}
|
||||
|
||||
pub struct PrivacyModeImpl {
|
||||
impl_key: String,
|
||||
conn_id: i32,
|
||||
@@ -192,8 +198,7 @@ impl PrivacyModeImpl {
|
||||
|
||||
let mut i: DWORD = 0;
|
||||
loop {
|
||||
#[allow(invalid_value)]
|
||||
let mut dd: DISPLAY_DEVICEW = unsafe { std::mem::MaybeUninit::uninit().assume_init() };
|
||||
let mut dd: DISPLAY_DEVICEW = unsafe { std::mem::zeroed() };
|
||||
dd.cb = std::mem::size_of::<DISPLAY_DEVICEW>() as _;
|
||||
let ok = unsafe { EnumDisplayDevicesW(std::ptr::null(), i, &mut dd as _, 0) };
|
||||
if ok == FALSE {
|
||||
@@ -274,7 +279,7 @@ impl PrivacyModeImpl {
|
||||
fn set_primary_display(&mut self) -> ResultType<String> {
|
||||
// Multiple virtual displays with different origins are tested.
|
||||
let display = &self.virtual_displays[0];
|
||||
let display_name = std::string::String::from_utf16(&display.name)?;
|
||||
let display_name = decode_display_name(&display.name)?;
|
||||
|
||||
#[allow(invalid_value)]
|
||||
let mut new_primary_dm: DEVMODEW = unsafe { std::mem::MaybeUninit::uninit().assume_init() };
|
||||
@@ -601,9 +606,15 @@ impl PrivacyMode for PrivacyModeImpl {
|
||||
));
|
||||
let reg_connectivity_2 = reg_display_settings::read_reg_connectivity()?;
|
||||
|
||||
if let Some(reg_recovery) =
|
||||
reg_display_settings::diff_recent_connectivity(reg_connectivity_1, reg_connectivity_2)
|
||||
{
|
||||
let registry =
|
||||
reg_display_settings::diff_recent_connectivity(reg_connectivity_1, reg_connectivity_2);
|
||||
// Amyuni reports index zero for every monitor. Only a sole display can be identified here.
|
||||
let added_display = (virtual_display_manager::is_amyuni_idd()
|
||||
&& !guard.virtual_displays_added.is_empty()
|
||||
&& guard.virtual_displays.len() == 1)
|
||||
.then_some(primary_display_name.as_str());
|
||||
let reg_recovery = display_recovery_value(registry, added_display)?;
|
||||
if !reg_recovery.is_null() {
|
||||
Config::set_option(
|
||||
CONFIG_KEY_REG_RECOVERY.to_owned(),
|
||||
serde_json::to_string(®_recovery)?,
|
||||
@@ -670,6 +681,30 @@ impl Drop for PrivacyModeImpl {
|
||||
}
|
||||
}
|
||||
|
||||
fn display_recovery_value(
|
||||
registry: Option<reg_display_settings::RegRecovery>,
|
||||
added_display: Option<&str>,
|
||||
) -> serde_json::Result<serde_json::Value> {
|
||||
let mut record = serde_json::to_value(registry)?;
|
||||
if let Some(name) = added_display {
|
||||
record[ADDED_DISPLAY] = name.into();
|
||||
}
|
||||
Ok(record)
|
||||
}
|
||||
|
||||
fn recover_added_display(value: &str) -> ResultType<()> {
|
||||
if !virtual_display_manager::is_amyuni_idd() {
|
||||
return Ok(());
|
||||
}
|
||||
let mut fields: serde_json::Map<_, _> = serde_json::from_str(value)?;
|
||||
let display: Option<String> =
|
||||
serde_json::from_value(fields.remove(ADDED_DISPLAY).unwrap_or_default())?;
|
||||
if let Some(name) = display {
|
||||
virtual_display_manager::amyuni_idd::recover_privacy_monitor(&name)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn reset_config_reg_connectivity() {
|
||||
Config::set_option(CONFIG_KEY_REG_RECOVERY.to_owned(), "".to_owned());
|
||||
@@ -682,6 +717,9 @@ pub fn restore_reg_connectivity(plug_out_monitors: bool, force: bool) {
|
||||
}
|
||||
if plug_out_monitors {
|
||||
let _ = virtual_display_manager::plug_out_monitor(-1, true, false);
|
||||
if let Err(err) = recover_added_display(&config_recovery_value) {
|
||||
log::error!("Failed to recover the privacy mode display: {err}");
|
||||
}
|
||||
}
|
||||
if let Ok(reg_recovery) =
|
||||
serde_json::from_str::<reg_display_settings::RegRecovery>(&config_recovery_value)
|
||||
@@ -698,6 +736,33 @@ mod tests {
|
||||
use super::*;
|
||||
use hbb_common::tokio::sync::mpsc::{error::TryRecvError, unbounded_channel};
|
||||
|
||||
#[test]
|
||||
fn display_name_ends_at_the_first_nul() {
|
||||
let name = r"\\.\DISPLAY2";
|
||||
let mut buffer: Vec<_> = name.encode_utf16().collect();
|
||||
buffer.extend([0, 0xd800]);
|
||||
assert_eq!(decode_display_name(&buffer).unwrap(), name);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn display_recovery_preserves_legacy_registry_fields() {
|
||||
let name = "DISPLAY2";
|
||||
let combined = serde_json::json!({
|
||||
"path": "monitor", "key": "Recent", "old": [[1], 3], "new": [[2], 3],
|
||||
"added_display": name
|
||||
});
|
||||
let registry = serde_json::from_value(combined.clone()).unwrap();
|
||||
assert_eq!(
|
||||
display_recovery_value(Some(registry), Some(name)).unwrap(),
|
||||
combined
|
||||
);
|
||||
assert_eq!(
|
||||
display_recovery_value(None, Some(name)).unwrap(),
|
||||
serde_json::json!({"added_display": name})
|
||||
);
|
||||
assert!(display_recovery_value(None, None).unwrap().is_null());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn privacy_mode_checks_active_displays() {
|
||||
let virtual_name = virtual_display_manager::get_cur_device_string();
|
||||
|
||||
@@ -937,6 +937,29 @@ pub mod amyuni_idd {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn should_unplug_privacy_monitor(name: &str, active: &[String]) -> ResultType<bool> {
|
||||
let found = active
|
||||
.iter()
|
||||
.any(|device| windows::is_device_name(device, name));
|
||||
// The Amyuni unplug request takes no monitor identifier.
|
||||
if found && active.len() > 1 {
|
||||
bail!("Cannot select {name} among multiple active Amyuni displays");
|
||||
}
|
||||
Ok(found)
|
||||
}
|
||||
|
||||
pub(crate) fn recover_privacy_monitor(name: &str) -> ResultType<()> {
|
||||
let active = windows::get_device_names(Some(super::AMYUNI_IDD_DEVICE_STRING));
|
||||
if !should_unplug_privacy_monitor(name, &active)? {
|
||||
return Ok(());
|
||||
}
|
||||
plug_monitor_(false, Some(PRIVACY_MONITOR_TIMEOUT))?;
|
||||
if is_my_display(name) {
|
||||
bail!("Privacy mode display {name} is still attached");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_monitor_count() -> usize {
|
||||
windows::get_device_names(Some(super::AMYUNI_IDD_DEVICE_STRING)).len()
|
||||
@@ -954,6 +977,19 @@ pub mod amyuni_idd {
|
||||
use super::*;
|
||||
use std::sync::mpsc::{channel, RecvTimeoutError};
|
||||
|
||||
#[test]
|
||||
fn restart_unplugs_only_the_recorded_amyuni_display() {
|
||||
let saved = r"\\.\DISPLAY2";
|
||||
let display = format!("{saved}\0");
|
||||
let other = String::from("\\\\.\\DISPLAY3\0");
|
||||
let should_unplug = |active: &[String]| should_unplug_privacy_monitor(saved, active);
|
||||
assert!(should_unplug(std::slice::from_ref(&display)).unwrap());
|
||||
assert!(!should_unplug(&[]).unwrap());
|
||||
assert!(!should_unplug(std::slice::from_ref(&other)).unwrap());
|
||||
assert!(!should_unplug(&[other.clone(), String::from("\\\\.\\DISPLAY4\0")]).unwrap());
|
||||
assert!(should_unplug(&[display, other]).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn privacy_preparation_waits_and_can_be_cancelled() {
|
||||
const PENDING_CHECK: Duration = Duration::from_millis(100);
|
||||
|
||||
Reference in new issue
Block a user