diff --git a/libs/base/protos/message.proto b/libs/base/protos/message.proto index f9d9d68fb..146e44036 100644 --- a/libs/base/protos/message.proto +++ b/libs/base/protos/message.proto @@ -84,8 +84,6 @@ message LoginRequest { string my_platform = 13; bytes hwid = 14; string avatar = 17; - // The client types the saved OS password once logged in (Auto Login). - bool auto_login = 18; } message Terminal { @@ -136,8 +134,6 @@ message PeerInfo { // NOTE: Only support one-level dictionaries (for peer to update), and the key is of type string. string platform_additions = 12; WindowsSessions windows_sessions = 13; - // Answers LoginRequest.auto_login: the peer cannot confirm it is at a logon or lock screen. - bool skip_auto_login = 14; } message WindowsSession { @@ -1034,5 +1030,7 @@ message Message { TerminalAction terminal_action = 31; TerminalResponse terminal_response = 32; PortForwardChannel port_forward_channel = 33; + // Auto Login: the peer types the password only if it is at its logon or lock screen. + OSLogin os_login = 34; } } diff --git a/src/client.rs b/src/client.rs index 6133a04db..6d752d498 100644 --- a/src/client.rs +++ b/src/client.rs @@ -3056,12 +3056,6 @@ impl LoginConfigHandler { let a = !self.get_option("auto-login").is_empty(); let p = self.get_option("os-password"); if !p.is_empty() && l && a { - // The peer answers when it handles the login, so its screen can still lock or unlock - // before the password is typed. - if self.peer_info.as_ref().is_some_and(|pi| pi.skip_auto_login) { - log::info!("Skip auto login, the peer is not at the logon or lock screen"); - return "".to_owned(); - } p } else { "".to_owned() @@ -3875,11 +3869,6 @@ impl LoginConfigHandler { } else { Default::default() }; - // The conditions of `should_auto_login()`, minus the peer's answer to this request. - let auto_login = self.conn_type == ConnType::DEFAULT_CONN - && self.lock_after_session_end.v - && !self.get_option("auto-login").is_empty() - && !self.get_option("os-password").is_empty(); let mut lr = LoginRequest { username: pure_id, password: password.into(), @@ -3892,7 +3881,6 @@ impl LoginConfigHandler { os_login, hwid, avatar, - auto_login, ..Default::default() }; match self.conn_type { @@ -4479,6 +4467,32 @@ fn _input_os_password(p: String, activate: bool, interface: impl Interface) { interface.send(Data::Message(msg_out)); } +/// Auto Login. A peer that can check for its logon or lock screen gets the password and types it +/// itself, only if it is there right then. Keys typed from here land in whatever has focus if the +/// screen is not locked by the time they arrive. +pub fn auto_login(p: String, pi: &PeerInfo, interface: impl Interface) { + if !is_peer_handling_os_login(pi) { + input_os_password(p, true, interface); + return; + } + std::thread::spawn(move || { + activate_os(&interface, true); + std::thread::sleep(Duration::from_millis(1200)); + let mut msg_out = Message::new(); + msg_out.set_os_login(OSLogin { + password: p, + ..Default::default() + }); + interface.send(Data::Message(msg_out)); + }); +} + +/// Windows and macOS peers from the 1.5.1 release handle `os_login`. +fn is_peer_handling_os_login(pi: &PeerInfo) -> bool { + (pi.platform == crate::common::PLATFORM_WINDOWS || pi.platform == crate::common::PLATFORM_MACOS) + && hbb_common::get_version_number(&pi.version) >= hbb_common::get_version_number("1.5.1") +} + #[derive(Copy, Clone)] struct LoginErrorMsgBox { msgtype: &'static str, @@ -5233,36 +5247,6 @@ mod port_forward_mux_tests { } } -#[cfg(test)] -mod auto_login_tests { - use super::*; - - #[test] - fn auto_login_asks_the_peer_and_skips_when_told() { - let mut lc = LoginConfigHandler::default(); - let asks = |lc: &LoginConfigHandler| { - lc.create_login_msg(String::new(), String::new(), vec![]) - .login_request() - .auto_login - }; - assert!(!asks(&lc)); - - lc.config.lock_after_session_end.v = true; - for (k, v) in [("os-password", "os-pass"), ("auto-login", "Y")] { - lc.config.options.insert(k.to_owned(), v.to_owned()); - } - assert!(asks(&lc)); - - lc.peer_info = Some(PeerInfo::default()); - assert_eq!(lc.should_auto_login(), "os-pass"); - lc.peer_info = Some(PeerInfo { - skip_auto_login: true, - ..Default::default() - }); - assert_eq!(lc.should_auto_login(), ""); - } -} - pub async fn hc_connection( feedback: i32, rendezvous_server: String, diff --git a/src/server/connection.rs b/src/server/connection.rs index 3635af98c..15f5de02f 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -2020,17 +2020,6 @@ impl Connection { pi.platform_additions = serde_json::to_string(&platform_additions).unwrap_or("".into()); } - // A client's auto-login types the OS password into whatever has focus. - #[cfg(any(target_os = "windows", target_os = "macos"))] - if self.lr.auto_login { - pi.skip_auto_login = !tokio::task::spawn_blocking(crate::platform::is_logon_screen) - .await - .unwrap_or_else(|e| { - log::error!("Failed to check the logon screen: {}", e); - false - }); - } - if self.is_port_forward() { pi.features = Some(Features { port_forward_mux: self.port_forward_mux.is_some(), @@ -2474,6 +2463,41 @@ impl Connection { self.tx_input.send(MessageInput::Key((msg, press))).ok(); } + // Auto Login: the password and Return the client would otherwise type itself, typed only if a + // check right before finds the logon or lock screen. + #[cfg(any(target_os = "windows", target_os = "macos"))] + async fn type_auto_login_password(&self, password: String) { + // A peer can send this at any rate, hence the throttled logs. + let at_logon_screen = tokio::task::spawn_blocking(crate::platform::is_logon_screen) + .await + .unwrap_or_else(|e| { + hbb_common::throttled_log!( + Duration::from_secs(60), + error, + "Failed to check the logon screen: {}", + e + ); + false + }); + if !at_logon_screen { + hbb_common::throttled_log!( + Duration::from_secs(60), + info, + "Auto login dropped, not at the logon or lock screen" + ); + return; + } + let mut key_event = KeyEvent::new(); + key_event.mode = KeyboardMode::Legacy.into(); + key_event.press = true; + key_event.set_seq(password); + self.input_key(key_event.clone(), false); + key_event.set_control_key(ControlKey::Return); + CLICK_TIME.store(get_time(), Ordering::SeqCst); + MOUSE_MOVE_TIME.store(get_time(), Ordering::SeqCst); + self.input_key(key_event, true); + } + fn verify_h1(&self, h1: &[u8]) -> bool { let mut hasher2 = Sha256::new(); hasher2.update(h1); @@ -3366,6 +3390,13 @@ impl Connection { } self.update_auto_disconnect_timer(); } + #[cfg(any(target_os = "windows", target_os = "macos"))] + Some(message::Union::OsLogin(login)) => { + if self.peer_keyboard_enabled() { + self.type_auto_login_password(login.password).await; + } + self.update_auto_disconnect_timer(); + } Some(message::Union::Clipboard(cb)) => { if self.should_handle_text_clipboard_message() && self.clipboard_enabled() { #[cfg(not(any(target_os = "android", target_os = "ios")))] diff --git a/src/ui_session_interface.rs b/src/ui_session_interface.rs index f28752e77..abe38272f 100644 --- a/src/ui_session_interface.rs +++ b/src/ui_session_interface.rs @@ -1832,7 +1832,7 @@ impl Interface for Session { self.try_change_init_resolution(pi.current_display); let p = self.lc.read().unwrap().should_auto_login(); if !p.is_empty() { - input_os_password(p, true, self.clone()); + crate::client::auto_login(p, &pi, self.clone()); } } let current = &pi.displays[pi.current_display as usize];