mirror of
https://github.com/rustdesk/rustdesk.git
synced 2026-03-18 02:31:00 +03:00
nat64
This commit is contained in:
@@ -85,10 +85,21 @@ impl<'a> IntoTargetAddr2<'a> for &str {
|
||||
|
||||
pub async fn connect_tcp<'t, T: IntoTargetAddr2<'t> + std::fmt::Debug>(
|
||||
target: T,
|
||||
local: SocketAddr,
|
||||
ms_timeout: u64,
|
||||
) -> ResultType<FramedStream> {
|
||||
let target_addr = target.into_target_addr2()?;
|
||||
let local = Config::get_any_listen_addr(is_ipv4(&target_addr));
|
||||
connect_tcp_local(target_addr, local, ms_timeout)
|
||||
.await
|
||||
.context(format!("Invalid target addr: {:?}", target))
|
||||
}
|
||||
|
||||
pub async fn connect_tcp_local<'t, T: IntoTargetAddr<'t> + std::fmt::Debug>(
|
||||
target: T,
|
||||
local: SocketAddr,
|
||||
ms_timeout: u64,
|
||||
) -> ResultType<FramedStream> {
|
||||
let target_addr = target.into_target_addr()?;
|
||||
if let Some(conf) = Config::get_socks() {
|
||||
return FramedStream::connect(
|
||||
conf.proxy.as_str(),
|
||||
@@ -100,13 +111,43 @@ pub async fn connect_tcp<'t, T: IntoTargetAddr2<'t> + std::fmt::Debug>(
|
||||
)
|
||||
.await;
|
||||
}
|
||||
let addr = ToSocketAddrs::to_socket_addrs(&target_addr)?
|
||||
let mut addr = ToSocketAddrs::to_socket_addrs(&target_addr)?
|
||||
.next()
|
||||
.context(format!("Invalid target addr: {:?}", target))?;
|
||||
.context(format!("Invalid target addr: {:?}", target_addr))?;
|
||||
if local.is_ipv6() && addr.is_ipv4() {
|
||||
addr = query_nip_io(&addr)?;
|
||||
}
|
||||
Ok(FramedStream::new(addr, local, ms_timeout).await?)
|
||||
}
|
||||
|
||||
pub async fn new_udp<T: ToSocketAddrs>(local: T, ms_timeout: u64) -> ResultType<FramedSocket> {
|
||||
#[inline]
|
||||
pub fn is_ipv4(target: &TargetAddr<'_>) -> bool {
|
||||
match target {
|
||||
TargetAddr::Ip(addr) => addr.is_ipv4(),
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn query_nip_io(addr: &SocketAddr) -> ResultType<SocketAddr> {
|
||||
to_socket_addr(format!("{}.nip.io:{}", addr.ip(), addr.port()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn ipv4_to_ipv6(addr: String, ipv4: bool) -> String {
|
||||
if !ipv4 && crate::is_ipv4_str(&addr) {
|
||||
if let Some(ip) = addr.split(":").next() {
|
||||
return addr.replace(ip, &format!("{}.nip.io", ip));
|
||||
}
|
||||
}
|
||||
addr
|
||||
}
|
||||
|
||||
pub async fn new_udp_for(target: &TargetAddr<'_>, ms_timeout: u64) -> ResultType<FramedSocket> {
|
||||
new_udp(Config::get_any_listen_addr(is_ipv4(target)), ms_timeout).await
|
||||
}
|
||||
|
||||
async fn new_udp<T: ToSocketAddrs>(local: T, ms_timeout: u64) -> ResultType<FramedSocket> {
|
||||
match Config::get_socks() {
|
||||
None => Ok(FramedSocket::new(to_socket_addr(&local)?).await?),
|
||||
Some(conf) => {
|
||||
@@ -123,19 +164,49 @@ pub async fn new_udp<T: ToSocketAddrs>(local: T, ms_timeout: u64) -> ResultType<
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn rebind_udp<T: ToSocketAddrs>(local: T) -> ResultType<Option<FramedSocket>> {
|
||||
pub async fn rebind_udp_for(target: &TargetAddr<'_>) -> ResultType<Option<FramedSocket>> {
|
||||
match Config::get_network_type() {
|
||||
NetworkType::Direct => Ok(Some(FramedSocket::new(to_socket_addr(local)?).await?)),
|
||||
NetworkType::Direct => Ok(Some(
|
||||
FramedSocket::new(Config::get_any_listen_addr(is_ipv4(target))).await?,
|
||||
)),
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
#[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_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());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nat64() {
|
||||
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");
|
||||
assert_eq!(
|
||||
ipv4_to_ipv6("1.1.1.1:8080".to_owned(), false),
|
||||
"1.1.1.1.nip.io:8080"
|
||||
);
|
||||
assert_eq!(
|
||||
ipv4_to_ipv6("rustdesk.com".to_owned(), false),
|
||||
"rustdesk.com"
|
||||
);
|
||||
if to_socket_addr("rustdesk.com:80").unwrap().is_ipv6() {
|
||||
assert!(query_nip_io(&"1.1.1.1:80".parse().unwrap())
|
||||
.unwrap()
|
||||
.is_ipv6());
|
||||
return;
|
||||
}
|
||||
assert_eq!(
|
||||
query_nip_io(&"1.1.1.1:80".parse().unwrap()).unwrap(),
|
||||
"1.1.1.1:80".parse().unwrap()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user