diff --git a/src/config.rs b/src/config.rs index 638ee0037..46267b6ec 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2966,6 +2966,7 @@ pub mod keys { pub const OPTION_ALLOW_NUMERNIC_ONE_TIME_PASSWORD: &str = "allow-numeric-one-time-password"; pub const OPTION_DIRECT_SERVER: &str = "direct-server"; pub const OPTION_ALLOW_WEBSOCKET: &str = "allow-websocket"; + pub const OPTION_WEBSOCKET_PORT: &str = "websocket-port"; pub const OPTION_TRACKPAD_SPEED: &str = "trackpad-speed"; pub const OPTION_REGISTER_DEVICE: &str = "register-device"; pub const OPTION_RELAY_SERVER: &str = "relay-server"; diff --git a/src/lib.rs b/src/lib.rs index 8bc7f0978..503954535 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -437,7 +437,7 @@ pub fn init_log(_is_async: bool, _name: &str) -> Option Option String { (format!("{}:{}", endpoint_host, dst_port), false) } else { let domain_path = if relay { "/ws/relay" } else { "/ws/id" }; - (format!("{}{}", endpoint_host, domain_path), true) + let port = websocket_port(&endpoint_host, &relay_server) + .map(|port| format!(":{}", port)) + .unwrap_or_default(); + (format!("{}{}{}", endpoint_host, port, domain_path), true) }; let protocol = if is_domain { let api_server = Config::get_option("api-server"); @@ -528,6 +533,45 @@ pub fn check_ws(endpoint: &str) -> String { format!("{}://{}", protocol, address) } +/// Port of the reverse proxy serving `/ws/id` and `/ws/relay` on this client's own servers, for +/// when it cannot listen on the scheme's default port. Any other host, such as a peer's server or +/// a relay on another machine, keeps the default port. IP endpoints never get here. +fn websocket_port(host: &str, relay_server: &str) -> Option { + let port = Config::get_option(OPTION_WEBSOCKET_PORT); + if port.is_empty() { + return None; + } + // Only servers this client is set up with, not the public fallback of get_rendezvous_server(). + let own_servers = [ + EXE_RENDEZVOUS_SERVER.read().unwrap().clone(), + Config::get_option("custom-rendezvous-server"), + PROD_RENDEZVOUS_SERVER.read().unwrap().clone(), + relay_server.to_owned(), + ]; + let is_own = own_servers.iter().any(|server| { + split_host_port(server) + .map(|(h, _)| h) + .unwrap_or_else(|| server.to_string()) + .eq_ignore_ascii_case(host) + }); + if !is_own { + return None; + } + match port.parse::() { + Ok(port) if port > 0 => Some(port), + _ => { + crate::throttled_log!( + Duration::from_secs(60), + warn, + "Ignoring invalid {}: {}", + OPTION_WEBSOCKET_PORT, + port + ); + None + } + } +} + #[cfg(test)] mod proxy_tests; @@ -709,6 +753,42 @@ mod tests { assert_eq!(check_ws("127.0.0.1:23455"), "ws://127.0.0.1:23458"); assert_eq!(check_ws("127.0.0.1:23456"), "ws://127.0.0.1:23458"); assert_eq!(check_ws("127.0.0.1:34567"), "ws://127.0.0.1:34569"); + + // set websocket-port, a reverse proxy off the default port on our own servers + Config::set_option( + "custom-rendezvous-server".to_string(), + "rustdesk.com".to_string(), + ); + Config::set_option("relay-server".to_string(), "relay.rustdesk.com".to_string()); + Config::set_option( + "api-server".to_string(), + "https://rustdesk.com:8443".to_string(), + ); + Config::set_option(keys::OPTION_WEBSOCKET_PORT.to_string(), "8443".to_string()); + assert_eq!( + check_ws("rustdesk.com:21116"), + "wss://rustdesk.com:8443/ws/id" + ); + assert_eq!( + check_ws("rustdesk.com:21117"), + "wss://rustdesk.com:8443/ws/relay" + ); + assert_eq!( + check_ws("relay.rustdesk.com:21117"), + "wss://relay.rustdesk.com:8443/ws/relay" + ); + // a peer's server or a relay on another host keeps the default port + assert_eq!(check_ws("example.com:21116"), "wss://example.com/ws/id"); + assert_eq!(check_ws("example.com:21117"), "wss://example.com/ws/relay"); + assert_eq!(check_ws("127.0.0.1:21116"), "ws://127.0.0.1:21118"); + // with no rendezvous server set, the public fallback is not one of ours + Config::set_option("custom-rendezvous-server".to_string(), "".to_string()); + let fallback = Config::get_rendezvous_server(); + let (fallback_host, _) = split_host_port(&fallback).unwrap(); + assert_eq!( + check_ws(&fallback), + format!("wss://{}/ws/id", fallback_host) + ); Config::set_options(options); } // A server-to-client frame is unmasked, so it can be put on the wire by hand: the header alone,