diff --git a/libs/hbb_common/src/proxy.rs b/libs/hbb_common/src/proxy.rs index 1fe41b593..3418e4db1 100644 --- a/libs/hbb_common/src/proxy.rs +++ b/libs/hbb_common/src/proxy.rs @@ -251,6 +251,26 @@ impl ProxyScheme { } } } + pub fn get_host_and_port(&self) -> Result { + match self { + ProxyScheme::Http { host, .. } => { + Ok(self.append_default_port(host, 80)) + }, + ProxyScheme::Https { host, .. } => { + Ok(self.append_default_port(host, 443)) + }, + ProxyScheme::Socks5 { addr, .. } => { + Ok(format!("{}", addr)) + }, + } + } + fn append_default_port(&self, host: &str, default_port: u16) -> String { + if host.contains(':') { + host.to_string() + } else { + format!("{}:{}", host, default_port) + } + } } diff --git a/libs/hbb_common/src/socket_client.rs b/libs/hbb_common/src/socket_client.rs index 634f49a65..fbd7ab3c5 100644 --- a/libs/hbb_common/src/socket_client.rs +++ b/libs/hbb_common/src/socket_client.rs @@ -6,8 +6,10 @@ use crate::{ }; use anyhow::Context; use std::net::SocketAddr; +use log::info; use tokio::net::ToSocketAddrs; use tokio_socks::{IntoTargetAddr, TargetAddr}; +use crate::proxy::IntoProxyScheme; #[inline] pub fn check_port(host: T, port: i32) -> String { @@ -50,19 +52,15 @@ pub fn increase_port(host: T, offset: i32) -> String { } pub fn test_if_valid_server(host: &str) -> String { - let host = check_port(host, 0); - + info!("Testing server validity for host: {}", host); use std::net::ToSocketAddrs; - match Config::get_network_type() { - NetworkType::Direct => match host.to_socket_addrs() { - Err(err) => err.to_string(), - Ok(_) => "".to_owned(), - }, - NetworkType::ProxySocks => match &host.into_target_addr() { - Err(err) => err.to_string(), - Ok(_) => "".to_owned(), - }, - } + // Even if the current network type is a proxy type, + // the system DNS should be used to resolve the proxy server address. + host.into_proxy_scheme() + .and_then(|scheme| scheme.get_host_and_port()) + .and_then(|domain| domain.to_socket_addrs().map_err(Into::into)) + .map(|_| "".to_owned()) // on success, return an empty string + .unwrap_or_else(|e| e.to_string()) // on error, convert the error into a string } pub trait IsResolvedSocketAddr {