fix: Fix the proxy address test function.

This commit is contained in:
yuluo
2024-04-12 02:13:11 +08:00
parent ff72cbf9a9
commit a8c843a0c4
2 changed files with 30 additions and 12 deletions

View File

@@ -251,6 +251,26 @@ impl ProxyScheme {
}
}
}
pub fn get_host_and_port(&self) -> Result<String, ProxyError> {
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)
}
}
}

View File

@@ -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<T: std::string::ToString>(host: T, port: i32) -> String {
@@ -50,19 +52,15 @@ pub fn increase_port<T: std::string::ToString>(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 {