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;
|
2022-12-27 12:30:23 +08:00
|
|
|
use std::net::ToSocketAddrs;
|
2022-01-05 13:21:14 +08:00
|
|
|
use tokio_socks::{IntoTargetAddr, TargetAddr};
|
2022-01-02 22:55:33 +08:00
|
|
|
|
2022-12-27 12:30:23 +08:00
|
|
|
fn to_socket_addr<T: ToSocketAddrs>(host: T) -> ResultType<SocketAddr> {
|
|
|
|
let mut addr_ipv4 = None;
|
|
|
|
let mut addr_ipv6 = None;
|
|
|
|
for addr in host.to_socket_addrs()? {
|
|
|
|
if addr.is_ipv4() && addr_ipv4.is_none() {
|
|
|
|
addr_ipv4 = Some(addr);
|
|
|
|
}
|
|
|
|
if addr.is_ipv6() && addr_ipv6.is_none() {
|
|
|
|
addr_ipv6 = Some(addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(addr) = addr_ipv4 {
|
|
|
|
Ok(addr)
|
|
|
|
} else {
|
|
|
|
addr_ipv6.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
|
|
|
|
2022-12-27 12:30:23 +08:00
|
|
|
pub trait IntoTargetAddr2<'a> {
|
|
|
|
/// Converts the value of self to a `TargetAddr`.
|
|
|
|
fn into_target_addr2(&self) -> ResultType<TargetAddr<'a>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IntoTargetAddr2<'a> for SocketAddr {
|
|
|
|
fn into_target_addr2(&self) -> ResultType<TargetAddr<'a>> {
|
|
|
|
Ok(TargetAddr::Ip(*self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IntoTargetAddr2<'a> for TargetAddr<'a> {
|
|
|
|
fn into_target_addr2(&self) -> ResultType<TargetAddr<'a>> {
|
|
|
|
Ok(self.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IntoTargetAddr2<'a> for String {
|
|
|
|
fn into_target_addr2(&self) -> ResultType<TargetAddr<'a>> {
|
|
|
|
Ok(to_socket_addr(self)?.into_target_addr()?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IntoTargetAddr2<'a> for &str {
|
|
|
|
fn into_target_addr2(&self) -> ResultType<TargetAddr<'a>> {
|
|
|
|
Ok(to_socket_addr(self)?.into_target_addr()?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn connect_tcp<'t, T: IntoTargetAddr2<'t> + std::fmt::Debug>(
|
2022-01-02 22:55:33 +08:00
|
|
|
target: T,
|
|
|
|
local: SocketAddr,
|
|
|
|
ms_timeout: u64,
|
|
|
|
) -> ResultType<FramedStream> {
|
2022-12-27 12:30:23 +08:00
|
|
|
let target_addr = target.into_target_addr2()?;
|
2022-01-04 00:44:50 +08:00
|
|
|
if let Some(conf) = Config::get_socks() {
|
2022-12-27 12:30:23 +08:00
|
|
|
return FramedStream::connect(
|
2022-01-02 22:55:33 +08:00
|
|
|
conf.proxy.as_str(),
|
|
|
|
target_addr,
|
|
|
|
local,
|
|
|
|
conf.username.as_str(),
|
|
|
|
conf.password.as_str(),
|
|
|
|
ms_timeout,
|
|
|
|
)
|
2022-12-27 12:30:23 +08:00
|
|
|
.await;
|
2022-01-02 22:55:33 +08:00
|
|
|
}
|
2022-12-27 12:30:23 +08:00
|
|
|
let addr = ToSocketAddrs::to_socket_addrs(&target_addr)?
|
|
|
|
.next()
|
|
|
|
.context(format!("Invalid target addr: {:?}", target))?;
|
|
|
|
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-12-27 12:30:23 +08:00
|
|
|
None => Ok(FramedSocket::new(to_socket_addr(&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(),
|
2022-12-27 12:30:23 +08:00
|
|
|
to_socket_addr(local)?,
|
2022-01-02 22:55:33 +08:00
|
|
|
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() {
|
2022-12-27 12:30:23 +08:00
|
|
|
NetworkType::Direct => Ok(Some(FramedSocket::new(to_socket_addr(local)?).await?)),
|
2022-01-04 00:44:50 +08:00
|
|
|
_ => Ok(None),
|
2022-01-02 22:55:33 +08:00
|
|
|
}
|
|
|
|
}
|
2022-12-27 12:30:23 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
#[test]
|
|
|
|
fn test_to_socket_addr() {
|
|
|
|
assert_eq!(to_socket_addr("127.0.0.1:8080").unwrap(), "127.0.0.1:8080".parse().unwrap());
|
|
|
|
assert!(to_socket_addr("[ff::]:0").unwrap().is_ipv6());
|
|
|
|
assert!(to_socket_addr("xx").is_err());
|
|
|
|
}
|
|
|
|
}
|