fix: test if valid server, control if try test with proxy (#7858)

* fix: test if valid server, control if try test with proxy

Signed-off-by: fufesou <shuanglongchen@yeah.net>

* fix: build

Signed-off-by: fufesou <shuanglongchen@yeah.net>

---------

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2024-04-28 14:22:21 +08:00 committed by GitHub
parent bd717349a7
commit 1dfbaa1e02
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 55 additions and 52 deletions

View File

@ -2978,16 +2978,16 @@ Future<bool> setServerConfig(
} }
// id // id
if (config.idServer.isNotEmpty && errMsgs != null) { if (config.idServer.isNotEmpty && errMsgs != null) {
errMsgs[0].value = errMsgs[0].value = translate(await bind.mainTestIfValidServer(
translate(await bind.mainTestIfValidServer(server: config.idServer)); server: config.idServer, testWithProxy: true));
if (errMsgs[0].isNotEmpty) { if (errMsgs[0].isNotEmpty) {
return false; return false;
} }
} }
// relay // relay
if (config.relayServer.isNotEmpty && errMsgs != null) { if (config.relayServer.isNotEmpty && errMsgs != null) {
errMsgs[1].value = errMsgs[1].value = translate(await bind.mainTestIfValidServer(
translate(await bind.mainTestIfValidServer(server: config.relayServer)); server: config.relayServer, testWithProxy: true));
if (errMsgs[1].isNotEmpty) { if (errMsgs[1].isNotEmpty) {
return false; return false;
} }

View File

@ -2079,7 +2079,12 @@ void changeSocks5Proxy() async {
password = pwdController.text.trim(); password = pwdController.text.trim();
if (proxy.isNotEmpty) { if (proxy.isNotEmpty) {
proxyMsg = translate(await bind.mainTestIfValidServer(server: proxy)); String domainPort = proxy;
if (domainPort.contains('://')) {
domainPort = domainPort.split('://')[1];
}
proxyMsg = translate(await bind.mainTestIfValidServer(
server: domainPort, testWithProxy: false));
if (proxyMsg.isEmpty) { if (proxyMsg.isEmpty) {
// ignore // ignore
} else { } else {

View File

@ -283,12 +283,3 @@ void setPrivacyModeDialog(
); );
}, backDismiss: true, clickMaskDismiss: true); }, backDismiss: true, clickMaskDismiss: true);
} }
Future<String?> validateAsync(String value) async {
value = value.trim();
if (value.isEmpty) {
return null;
}
final res = await bind.mainTestIfValidServer(server: value);
return res.isEmpty ? null : res;
}

View File

@ -680,7 +680,8 @@ class RustdeskImpl {
return Future(() => js.context.callMethod('setByName', ['options', json])); return Future(() => js.context.callMethod('setByName', ['options', json]));
} }
Future<String> mainTestIfValidServer({required String server, dynamic hint}) { Future<String> mainTestIfValidServer(
{required String server, required bool testWithProxy, dynamic hint}) {
// TODO: implement // TODO: implement
return Future.value(''); return Future.value('');
} }
@ -788,7 +789,7 @@ class RustdeskImpl {
throw UnimplementedError(); throw UnimplementedError();
} }
Future<String?> mainGetHttpStatus({required String url, dynamic hint}){ Future<String?> mainGetHttpStatus({required String url, dynamic hint}) {
throw UnimplementedError(); throw UnimplementedError();
} }

View File

@ -1,13 +1,11 @@
use crate::{ use crate::{
config::{Config, NetworkType}, config::{Config, NetworkType},
proxy::IntoProxyScheme,
tcp::FramedStream, tcp::FramedStream,
udp::FramedSocket, udp::FramedSocket,
ResultType, ResultType,
}; };
use anyhow::Context; use anyhow::Context;
use std::net::SocketAddr; use std::net::SocketAddr;
use log::info;
use tokio::net::ToSocketAddrs; use tokio::net::ToSocketAddrs;
use tokio_socks::{IntoTargetAddr, TargetAddr}; use tokio_socks::{IntoTargetAddr, TargetAddr};
@ -51,25 +49,28 @@ pub fn increase_port<T: std::string::ToString>(host: T, offset: i32) -> String {
host host
} }
pub fn test_if_valid_server(host: &str) -> String { pub fn test_if_valid_server(host: &str, test_with_proxy: bool) -> String {
info!("Testing server validity for host: {}", host); let host = check_port(host, 0);
use std::net::ToSocketAddrs; use std::net::ToSocketAddrs;
let host = if !host.contains("://") { if test_with_proxy && NetworkType::ProxySocks == Config::get_network_type() {
// We just add a scheme for testing the domain and port parts, test_if_valid_server_for_proxy_(&host)
// we don't care about the scheme, so "http://" is used for simple.
format!("http://{}", host)
} else { } else {
host.to_string() match host.to_socket_addrs() {
}; Err(err) => err.to_string(),
Ok(_) => "".to_owned(),
}
}
}
// Even if the current network type is a proxy type, #[inline]
// the system DNS should be used to resolve the proxy server address. pub fn test_if_valid_server_for_proxy_(host: &str) -> String {
host.into_proxy_scheme() // `&host.into_target_addr()` is defined in `tokio-socs`, but is a common pattern for testing,
.and_then(|scheme| scheme.get_host_and_port()) // it can be used for both `socks` and `http` proxy.
.and_then(|domain| domain.to_socket_addrs().map_err(Into::into)) match &host.into_target_addr() {
.map(|_| "".to_owned()) // on success, return an empty string Err(err) => err.to_string(),
.unwrap_or_else(|e| e.to_string()) // on error, convert the error into a string Ok(_) => "".to_owned(),
}
} }
pub trait IsResolvedSocketAddr { pub trait IsResolvedSocketAddr {
@ -254,15 +255,20 @@ mod tests {
#[test] #[test]
fn test_test_if_valid_server() { fn test_test_if_valid_server() {
assert!(!test_if_valid_server("a").is_empty()); assert!(!test_if_valid_server("a", false).is_empty());
// on Linux, "1" is resolved to "0.0.0.1" // on Linux, "1" is resolved to "0.0.0.1"
assert!(test_if_valid_server("1.1.1.1").is_empty()); assert!(test_if_valid_server("1.1.1.1", false).is_empty());
assert!(test_if_valid_server("1.1.1.1:1").is_empty()); assert!(test_if_valid_server("1.1.1.1:1", false).is_empty());
assert!(test_if_valid_server("abcd.com:1").is_empty()); assert!(test_if_valid_server("abcd.com", false).is_empty());
assert!(test_if_valid_server("http://abcd.com:1").is_empty()); assert!(test_if_valid_server("abcd.com:1", false).is_empty());
assert!(test_if_valid_server("https://abcd.com:1").is_empty());
assert!(test_if_valid_server("socks5://abcd.com:1").is_empty()); // with proxy
assert!(test_if_valid_server("https://1.1.1.1:1").is_empty()); // `:0` indicates `let host = check_port(host, 0);` is called.
assert!(test_if_valid_server_for_proxy_("a:0").is_empty());
assert!(test_if_valid_server_for_proxy_("1.1.1.1:0").is_empty());
assert!(test_if_valid_server_for_proxy_("1.1.1.1:1").is_empty());
assert!(test_if_valid_server_for_proxy_("abc.com:0").is_empty());
assert!(test_if_valid_server_for_proxy_("abcd.com:1").is_empty());
} }
#[test] #[test]

View File

@ -809,8 +809,8 @@ pub fn main_set_options(json: String) {
} }
} }
pub fn main_test_if_valid_server(server: String) -> String { pub fn main_test_if_valid_server(server: String, test_with_proxy: bool) -> String {
test_if_valid_server(server) test_if_valid_server(server, test_with_proxy)
} }
pub fn main_set_socks(proxy: String, username: String, password: String) { pub fn main_set_socks(proxy: String, username: String, password: String) {
@ -895,7 +895,7 @@ pub fn main_get_api_server() -> String {
} }
pub fn main_http_request(url: String, method: String, body: Option<String>, header: String) { pub fn main_http_request(url: String, method: String, body: Option<String>, header: String) {
http_request(url,method, body, header) http_request(url, method, body, header)
} }
pub fn main_get_local_option(key: String) -> SyncReturn<String> { pub fn main_get_local_option(key: String) -> SyncReturn<String> {

View File

@ -272,8 +272,8 @@ impl UI {
m m
} }
fn test_if_valid_server(&self, host: String) -> String { fn test_if_valid_server(&self, host: String, test_with_proxy: bool) -> String {
test_if_valid_server(host) test_if_valid_server(host, test_with_proxy)
} }
fn get_sound_inputs(&self) -> Value { fn get_sound_inputs(&self) -> Value {
@ -689,7 +689,7 @@ impl sciter::EventHandler for UI {
fn forget_password(String); fn forget_password(String);
fn set_peer_option(String, String, String); fn set_peer_option(String, String, String);
fn get_license(); fn get_license();
fn test_if_valid_server(String); fn test_if_valid_server(String, bool);
fn get_sound_inputs(); fn get_sound_inputs();
fn set_options(Value); fn set_options(Value);
fn set_option(String, String); fn set_option(String, String);

View File

@ -441,11 +441,11 @@ class MyIdMenu: Reactor.Component {
var key = (res.key || "").trim(); var key = (res.key || "").trim();
if (id == old_id && relay == old_relay && key == old_key && api == old_api) return; if (id == old_id && relay == old_relay && key == old_key && api == old_api) return;
if (id) { if (id) {
var err = handler.test_if_valid_server(id); var err = handler.test_if_valid_server(id, true);
if (err) return translate("ID Server") + ": " + err; if (err) return translate("ID Server") + ": " + err;
} }
if (relay) { if (relay) {
var err = handler.test_if_valid_server(relay); var err = handler.test_if_valid_server(relay, true);
if (err) return translate("Relay Server") + ": " + err; if (err) return translate("Relay Server") + ": " + err;
} }
if (api) { if (api) {
@ -476,7 +476,7 @@ class MyIdMenu: Reactor.Component {
var password = (res.password || "").trim(); var password = (res.password || "").trim();
if (proxy == old_proxy && username == old_username && password == old_password) return; if (proxy == old_proxy && username == old_username && password == old_password) return;
if (proxy) { if (proxy) {
var err = handler.test_if_valid_server(proxy); var err = handler.test_if_valid_server(proxy, false);
if (err) return translate("Server") + ": " + err; if (err) return translate("Server") + ": " + err;
} }
handler.set_socks(proxy, username, password); handler.set_socks(proxy, username, password);

View File

@ -280,8 +280,8 @@ pub fn get_options() -> String {
} }
#[inline] #[inline]
pub fn test_if_valid_server(host: String) -> String { pub fn test_if_valid_server(host: String, test_with_proxy: bool) -> String {
hbb_common::socket_client::test_if_valid_server(&host) hbb_common::socket_client::test_if_valid_server(&host, test_with_proxy)
} }
#[inline] #[inline]