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-29 20:34:52 +08:00
|
|
|
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
|
|
|
|
2023-01-09 18:28:11 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn check_port<T: std::string::ToString>(host: T, port: i32) -> String {
|
|
|
|
let host = host.to_string();
|
|
|
|
if crate::is_ipv6_str(&host) {
|
2023-02-08 17:26:44 +08:00
|
|
|
if host.starts_with('[') {
|
2023-01-09 18:28:11 +08:00
|
|
|
return host;
|
|
|
|
}
|
2023-02-08 17:26:44 +08:00
|
|
|
return format!("[{host}]:{port}");
|
2023-01-09 18:28:11 +08:00
|
|
|
}
|
2023-02-08 17:26:44 +08:00
|
|
|
if !host.contains(':') {
|
|
|
|
return format!("{host}:{port}");
|
2023-01-09 18:28:11 +08:00
|
|
|
}
|
2023-02-08 17:26:44 +08:00
|
|
|
host
|
2023-01-09 18:28:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn increase_port<T: std::string::ToString>(host: T, offset: i32) -> String {
|
|
|
|
let host = host.to_string();
|
|
|
|
if crate::is_ipv6_str(&host) {
|
2023-02-08 17:26:44 +08:00
|
|
|
if host.starts_with('[') {
|
2023-01-09 18:28:11 +08:00
|
|
|
let tmp: Vec<&str> = host.split("]:").collect();
|
|
|
|
if tmp.len() == 2 {
|
|
|
|
let port: i32 = tmp[1].parse().unwrap_or(0);
|
|
|
|
if port > 0 {
|
|
|
|
return format!("{}]:{}", tmp[0], port + offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-08 17:26:44 +08:00
|
|
|
} else if host.contains(':') {
|
|
|
|
let tmp: Vec<&str> = host.split(':').collect();
|
2023-01-09 18:28:11 +08:00
|
|
|
if tmp.len() == 2 {
|
|
|
|
let port: i32 = tmp[1].parse().unwrap_or(0);
|
|
|
|
if port > 0 {
|
|
|
|
return format!("{}:{}", tmp[0], port + offset);
|
|
|
|
}
|
|
|
|
}
|
2022-01-05 13:21:14 +08:00
|
|
|
}
|
2023-02-08 17:26:44 +08:00
|
|
|
host
|
2023-01-09 18:28:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test_if_valid_server(host: &str) -> String {
|
|
|
|
let host = check_port(host, 0);
|
2022-01-05 13:21:14 +08:00
|
|
|
|
2022-12-29 20:34:52 +08:00
|
|
|
use std::net::ToSocketAddrs;
|
2022-01-05 13:21:14 +08:00
|
|
|
match Config::get_network_type() {
|
2022-12-29 20:34:52 +08:00
|
|
|
NetworkType::Direct => match host.to_socket_addrs() {
|
2022-01-05 13:21:14 +08:00
|
|
|
Err(err) => err.to_string(),
|
2023-01-09 18:45:38 +08:00
|
|
|
Ok(_) => "".to_owned(),
|
2022-01-05 13:21:14 +08:00
|
|
|
},
|
|
|
|
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-29 20:34:52 +08:00
|
|
|
pub trait IsResolvedSocketAddr {
|
|
|
|
fn resolve(&self) -> Option<&SocketAddr>;
|
2022-12-27 12:30:23 +08:00
|
|
|
}
|
|
|
|
|
2022-12-29 20:34:52 +08:00
|
|
|
impl IsResolvedSocketAddr for SocketAddr {
|
|
|
|
fn resolve(&self) -> Option<&SocketAddr> {
|
2023-01-27 11:42:08 +08:00
|
|
|
Some(self)
|
2022-12-27 12:30:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 20:34:52 +08:00
|
|
|
impl IsResolvedSocketAddr for String {
|
|
|
|
fn resolve(&self) -> Option<&SocketAddr> {
|
|
|
|
None
|
2022-12-27 12:30:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 20:34:52 +08:00
|
|
|
impl IsResolvedSocketAddr for &str {
|
|
|
|
fn resolve(&self) -> Option<&SocketAddr> {
|
|
|
|
None
|
2022-12-27 12:30:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 20:34:52 +08:00
|
|
|
#[inline]
|
|
|
|
pub async fn connect_tcp<
|
|
|
|
't,
|
|
|
|
T: IntoTargetAddr<'t> + ToSocketAddrs + IsResolvedSocketAddr + std::fmt::Display,
|
|
|
|
>(
|
2022-01-02 22:55:33 +08:00
|
|
|
target: T,
|
|
|
|
ms_timeout: u64,
|
|
|
|
) -> ResultType<FramedStream> {
|
2022-12-29 20:34:52 +08:00
|
|
|
connect_tcp_local(target, None, ms_timeout).await
|
2022-12-28 13:52:13 +08:00
|
|
|
}
|
|
|
|
|
2022-12-29 20:34:52 +08:00
|
|
|
pub async fn connect_tcp_local<
|
|
|
|
't,
|
|
|
|
T: IntoTargetAddr<'t> + ToSocketAddrs + IsResolvedSocketAddr + std::fmt::Display,
|
|
|
|
>(
|
2022-12-28 13:52:13 +08:00
|
|
|
target: T,
|
2022-12-29 20:34:52 +08:00
|
|
|
local: Option<SocketAddr>,
|
2022-12-28 13:52:13 +08:00
|
|
|
ms_timeout: u64,
|
|
|
|
) -> ResultType<FramedStream> {
|
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(),
|
2022-12-29 20:34:52 +08:00
|
|
|
target,
|
2022-01-02 22:55:33 +08:00
|
|
|
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-29 20:34:52 +08:00
|
|
|
if let Some(target) = target.resolve() {
|
|
|
|
if let Some(local) = local {
|
|
|
|
if local.is_ipv6() && target.is_ipv4() {
|
2023-01-27 11:42:08 +08:00
|
|
|
let target = query_nip_io(target).await?;
|
|
|
|
return FramedStream::new(target, Some(local), ms_timeout).await;
|
2022-12-29 20:34:52 +08:00
|
|
|
}
|
|
|
|
}
|
2022-12-28 13:52:13 +08:00
|
|
|
}
|
2023-01-27 11:42:08 +08:00
|
|
|
FramedStream::new(target, local, ms_timeout).await
|
2022-01-02 22:55:33 +08:00
|
|
|
}
|
|
|
|
|
2022-12-28 13:52:13 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn is_ipv4(target: &TargetAddr<'_>) -> bool {
|
|
|
|
match target {
|
|
|
|
TargetAddr::Ip(addr) => addr.is_ipv4(),
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-12-29 20:34:52 +08:00
|
|
|
pub async fn query_nip_io(addr: &SocketAddr) -> ResultType<SocketAddr> {
|
|
|
|
tokio::net::lookup_host(format!("{}.nip.io:{}", addr.ip(), addr.port()))
|
|
|
|
.await?
|
2023-01-27 11:42:08 +08:00
|
|
|
.find(|x| x.is_ipv6())
|
2022-12-29 20:34:52 +08:00
|
|
|
.context("Failed to get ipv6 from nip.io")
|
2022-12-28 13:52:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn ipv4_to_ipv6(addr: String, ipv4: bool) -> String {
|
|
|
|
if !ipv4 && crate::is_ipv4_str(&addr) {
|
2023-01-27 11:42:08 +08:00
|
|
|
if let Some(ip) = addr.split(':').next() {
|
2023-02-08 17:26:44 +08:00
|
|
|
return addr.replace(ip, &format!("{ip}.nip.io"));
|
2022-12-28 13:52:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
addr
|
|
|
|
}
|
|
|
|
|
2022-12-29 22:31:01 +08:00
|
|
|
async fn test_target(target: &str) -> ResultType<SocketAddr> {
|
2022-12-29 20:34:52 +08:00
|
|
|
if let Ok(Ok(s)) = super::timeout(1000, tokio::net::TcpStream::connect(target)).await {
|
2022-12-29 22:31:01 +08:00
|
|
|
if let Ok(addr) = s.peer_addr() {
|
|
|
|
return Ok(addr);
|
|
|
|
}
|
2022-12-29 20:34:52 +08:00
|
|
|
}
|
2022-12-29 22:31:01 +08:00
|
|
|
tokio::net::lookup_host(target)
|
|
|
|
.await?
|
|
|
|
.next()
|
2023-02-08 17:26:44 +08:00
|
|
|
.context(format!("Failed to look up host for {target}"))
|
2022-12-29 20:34:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-12-29 22:31:01 +08:00
|
|
|
pub async fn new_udp_for(
|
|
|
|
target: &str,
|
|
|
|
ms_timeout: u64,
|
|
|
|
) -> ResultType<(FramedSocket, TargetAddr<'static>)> {
|
|
|
|
let (ipv4, target) = if NetworkType::Direct == Config::get_network_type() {
|
|
|
|
let addr = test_target(target).await?;
|
|
|
|
(addr.is_ipv4(), addr.into_target_addr()?)
|
|
|
|
} else {
|
|
|
|
(true, target.into_target_addr()?)
|
|
|
|
};
|
|
|
|
Ok((
|
|
|
|
new_udp(Config::get_any_listen_addr(ipv4), ms_timeout).await?,
|
|
|
|
target.to_owned(),
|
|
|
|
))
|
2022-12-28 13:52:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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-29 20:34:52 +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(),
|
2022-12-29 20:34:52 +08:00
|
|
|
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-12-29 22:31:01 +08:00
|
|
|
pub async fn rebind_udp_for(
|
|
|
|
target: &str,
|
|
|
|
) -> ResultType<Option<(FramedSocket, TargetAddr<'static>)>> {
|
|
|
|
if Config::get_network_type() != NetworkType::Direct {
|
|
|
|
return Ok(None);
|
2022-01-02 22:55:33 +08:00
|
|
|
}
|
2022-12-29 22:31:01 +08:00
|
|
|
let addr = test_target(target).await?;
|
|
|
|
let v4 = addr.is_ipv4();
|
|
|
|
Ok(Some((
|
|
|
|
FramedSocket::new(Config::get_any_listen_addr(v4)).await?,
|
|
|
|
addr.into_target_addr()?.to_owned(),
|
|
|
|
)))
|
2022-01-02 22:55:33 +08:00
|
|
|
}
|
2022-12-28 13:52:13 +08:00
|
|
|
|
2022-12-27 12:30:23 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-12-29 20:34:52 +08:00
|
|
|
use std::net::ToSocketAddrs;
|
|
|
|
|
2022-12-27 12:30:23 +08:00
|
|
|
use super::*;
|
2022-12-28 13:52:13 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nat64() {
|
2022-12-29 20:34:52 +08:00
|
|
|
test_nat64_async();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
|
|
async fn test_nat64_async() {
|
2022-12-28 13:52:13 +08:00
|
|
|
assert_eq!(ipv4_to_ipv6("1.1.1.1".to_owned(), true), "1.1.1.1");
|
|
|
|
assert_eq!(ipv4_to_ipv6("1.1.1.1".to_owned(), false), "1.1.1.1.nip.io");
|
2023-05-18 13:06:49 +08:00
|
|
|
assert_eq!(
|
|
|
|
ipv4_to_ipv6("1.1.1.1:8080".to_owned(), false),
|
|
|
|
"1.1.1.1.nip.io:8080"
|
|
|
|
);
|
2022-12-28 13:52:13 +08:00
|
|
|
assert_eq!(
|
|
|
|
ipv4_to_ipv6("rustdesk.com".to_owned(), false),
|
|
|
|
"rustdesk.com"
|
|
|
|
);
|
2022-12-29 20:34:52 +08:00
|
|
|
if ("rustdesk.com:80")
|
|
|
|
.to_socket_addrs()
|
|
|
|
.unwrap()
|
|
|
|
.next()
|
|
|
|
.unwrap()
|
|
|
|
.is_ipv6()
|
|
|
|
{
|
2022-12-28 13:52:13 +08:00
|
|
|
assert!(query_nip_io(&"1.1.1.1:80".parse().unwrap())
|
2022-12-29 20:34:52 +08:00
|
|
|
.await
|
2022-12-28 13:52:13 +08:00
|
|
|
.unwrap()
|
|
|
|
.is_ipv6());
|
|
|
|
return;
|
|
|
|
}
|
2022-12-29 20:34:52 +08:00
|
|
|
assert!(query_nip_io(&"1.1.1.1:80".parse().unwrap()).await.is_err());
|
2022-12-28 13:52:13 +08:00
|
|
|
}
|
2023-01-09 18:28:11 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_test_if_valid_server() {
|
|
|
|
assert!(!test_if_valid_server("a").is_empty());
|
|
|
|
// 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:1").is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_check_port() {
|
|
|
|
assert_eq!(check_port("[1:2]:12", 32), "[1:2]:12");
|
|
|
|
assert_eq!(check_port("1:2", 32), "[1:2]:32");
|
|
|
|
assert_eq!(check_port("z1:2", 32), "z1:2");
|
|
|
|
assert_eq!(check_port("1.1.1.1", 32), "1.1.1.1:32");
|
|
|
|
assert_eq!(check_port("1.1.1.1:32", 32), "1.1.1.1:32");
|
|
|
|
assert_eq!(check_port("test.com:32", 0), "test.com:32");
|
|
|
|
assert_eq!(increase_port("[1:2]:12", 1), "[1:2]:13");
|
|
|
|
assert_eq!(increase_port("1.2.2.4:12", 1), "1.2.2.4:13");
|
|
|
|
assert_eq!(increase_port("1.2.2.4", 1), "1.2.2.4");
|
|
|
|
assert_eq!(increase_port("test.com", 1), "test.com");
|
|
|
|
assert_eq!(increase_port("test.com:13", 4), "test.com:17");
|
|
|
|
assert_eq!(increase_port("1:13", 4), "1:13");
|
|
|
|
assert_eq!(increase_port("22:1:13", 4), "22:1:13");
|
|
|
|
assert_eq!(increase_port("z1:2", 1), "z1:3");
|
|
|
|
}
|
2022-12-28 13:52:13 +08:00
|
|
|
}
|