2022-01-02 22:55:33 +08:00
|
|
|
use crate::{
|
2022-01-05 13:21:14 +08:00
|
|
|
config::{Config, NetworkType},
|
2022-01-02 22:55:33 +08:00
|
|
|
tcp::FramedStream,
|
2022-01-04 00:44:50 +08:00
|
|
|
udp::FramedSocket,
|
2022-01-02 22:55:33 +08:00
|
|
|
ResultType,
|
|
|
|
};
|
2022-01-05 16:32:45 +08:00
|
|
|
use anyhow::Context;
|
2022-01-02 22:55:33 +08:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
use tokio::net::ToSocketAddrs;
|
2022-01-05 13:21:14 +08:00
|
|
|
use tokio_socks::{IntoTargetAddr, TargetAddr};
|
2022-01-02 22:55:33 +08:00
|
|
|
|
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(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-01-02 22:55:33 +08:00
|
|
|
|
|
|
|
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()?;
|
|
|
|
|
2022-01-04 00:44:50 +08:00
|
|
|
if let Some(conf) = Config::get_socks() {
|
2022-01-02 22:55:33 +08:00
|
|
|
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-02 22:55:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 13:21:14 +08:00
|
|
|
pub async fn new_udp<T: ToSocketAddrs>(local: T, ms_timeout: u64) -> ResultType<FramedSocket> {
|
2022-01-04 00:44:50 +08:00
|
|
|
match Config::get_socks() {
|
2022-01-05 13:21:14 +08:00
|
|
|
None => Ok(FramedSocket::new(local).await?),
|
2022-01-02 22:55:33 +08:00
|
|
|
Some(conf) => {
|
2022-01-05 13:21:14 +08:00
|
|
|
let socket = FramedSocket::new_proxy(
|
2022-01-02 22:55:33 +08:00
|
|
|
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-02 22:55:33 +08:00
|
|
|
}
|
2022-01-04 00:44:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 13:21:14 +08:00
|
|
|
pub async fn rebind_udp<T: ToSocketAddrs>(local: T) -> ResultType<Option<FramedSocket>> {
|
2022-01-04 00:44:50 +08:00
|
|
|
match Config::get_network_type() {
|
|
|
|
NetworkType::Direct => Ok(Some(FramedSocket::new(local).await?)),
|
|
|
|
_ => Ok(None),
|
2022-01-02 22:55:33 +08:00
|
|
|
}
|
|
|
|
}
|