rustdesk/libs/hbb_common/src/socket_client.rs

92 lines
2.6 KiB
Rust
Raw Normal View History

use crate::{
2022-01-05 13:21:14 +08:00
config::{Config, NetworkType},
tcp::FramedStream,
udp::FramedSocket,
ResultType,
};
2022-01-05 15:59:01 +08:00
use anyhow::{bail, Context};
use std::net::SocketAddr;
use tokio::net::ToSocketAddrs;
2022-01-05 13:21:14 +08:00
use tokio_socks::{IntoTargetAddr, TargetAddr};
2022-01-05 13:21:14 +08:00
fn to_socket_addr(host: &str) -> ResultType<SocketAddr> {
use std::net::ToSocketAddrs;
2022-01-05 15:59:01 +08:00
host.to_socket_addrs()?.next().context("Failed to solve")
2022-01-05 13:21:14 +08:00
}
pub fn get_target_addr(host: &str) -> ResultType<TargetAddr<'static>> {
let addr = match Config::get_network_type() {
NetworkType::Direct => to_socket_addr(&host)?.into_target_addr()?,
NetworkType::ProxySocks => host.into_target_addr()?,
}
.to_owned();
Ok(addr)
}
pub fn test_if_valid_server(host: &str) -> String {
let mut host = host.to_owned();
if !host.contains(":") {
host = format!("{}:{}", host, 0);
}
match Config::get_network_type() {
NetworkType::Direct => match to_socket_addr(&host) {
Err(err) => err.to_string(),
Ok(_) => "".to_owned(),
},
NetworkType::ProxySocks => match &host.into_target_addr() {
Err(err) => err.to_string(),
Ok(_) => "".to_owned(),
},
}
}
pub async fn connect_tcp<'t, T: IntoTargetAddr<'t>>(
target: T,
local: SocketAddr,
ms_timeout: u64,
) -> ResultType<FramedStream> {
let target_addr = target.into_target_addr()?;
if let Some(conf) = Config::get_socks() {
FramedStream::connect(
conf.proxy.as_str(),
target_addr,
local,
conf.username.as_str(),
conf.password.as_str(),
ms_timeout,
)
.await
} else {
2022-01-05 15:59:01 +08:00
let addr = std::net::ToSocketAddrs::to_socket_addrs(&target_addr)?
.next()
.context("Invalid target addr")?;
Ok(FramedStream::new(addr, local, ms_timeout).await?)
}
}
2022-01-05 13:21:14 +08:00
pub async fn new_udp<T: ToSocketAddrs>(local: T, ms_timeout: u64) -> ResultType<FramedSocket> {
match Config::get_socks() {
2022-01-05 13:21:14 +08:00
None => Ok(FramedSocket::new(local).await?),
Some(conf) => {
2022-01-05 13:21:14 +08:00
let socket = FramedSocket::new_proxy(
conf.proxy.as_str(),
local,
conf.username.as_str(),
conf.password.as_str(),
ms_timeout,
)
.await?;
2022-01-05 13:21:14 +08:00
Ok(socket)
}
}
}
2022-01-05 13:21:14 +08:00
pub async fn rebind_udp<T: ToSocketAddrs>(local: T) -> ResultType<Option<FramedSocket>> {
match Config::get_network_type() {
NetworkType::Direct => Ok(Some(FramedSocket::new(local).await?)),
_ => Ok(None),
}
}