From 34846eef9f1937d9fd953a91d3f1ef516277a8f9 Mon Sep 17 00:00:00 2001 From: rustdesk Date: Thu, 22 Oct 2026 16:05:24 +0800 Subject: [PATCH] feat(websocket): add websocket-port for a proxy off the default port check_ws turns a domain endpoint into ws(s):///ws/id or /ws/relay with no port, so a reverse proxy that cannot take 443 (or 80) is unreachable, even when api-server already points at it, for example https://example.com:8443. websocket-port adds that port for the hosts this client is configured with: custom-rendezvous-server and relay-server. A relay that hbbs hands out on another host, or a peer's server, keeps the default port, since another machine has its own proxy. IP endpoints are unchanged, and an empty option gives the same URL as before. https://github.com/rustdesk/rustdesk-server-pro/discussions/1029 Co-Authored-By: Claude Opus 5.5 --- src/config.rs | 1 + src/websocket.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index 6fe100455..31a539b07 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2869,6 +2869,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/websocket.rs b/src/websocket.rs index af797acc1..bc4eef271 100644 --- a/src/websocket.rs +++ b/src/websocket.rs @@ -513,7 +513,10 @@ pub fn check_ws(endpoint: &str) -> 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, &[&custom_rendezvous_server, &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 +531,33 @@ 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, own_servers: &[&str]) -> Option { + use crate::config::keys::OPTION_WEBSOCKET_PORT; + let port = Config::get_option(OPTION_WEBSOCKET_PORT); + if port.is_empty() { + return None; + } + 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), + _ => { + log::warn!("Ignoring invalid {}: {}", OPTION_WEBSOCKET_PORT, port); + None + } + } +} + #[cfg(test)] mod proxy_tests; @@ -709,6 +739,34 @@ 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"); Config::set_options(options); } // A server-to-client frame is unmasked, so it can be put on the wire by hand: the header alone,