more udp punch

This commit is contained in:
RustDesk
2025-06-10 12:01:43 +08:00
committed by GitHub
parent b69b097c6f
commit df95f44499
6 changed files with 31 additions and 138 deletions

View File

@@ -6,8 +6,8 @@ use crate::{
ResultType, Stream,
};
use anyhow::Context;
use std::net::SocketAddr;
use tokio::net::ToSocketAddrs;
use std::{net::SocketAddr, sync::Arc};
use tokio::net::{ToSocketAddrs, UdpSocket};
use tokio_socks::{IntoTargetAddr, TargetAddr};
#[inline]
@@ -207,6 +207,14 @@ async fn test_target(target: &str) -> ResultType<SocketAddr> {
.context(format!("Failed to look up host for {target}"))
}
#[inline]
pub async fn new_direct_udp_for(target: &str) -> ResultType<(Arc<UdpSocket>, SocketAddr)> {
let peer_addr = test_target(target).await?;
let local_addr = Config::get_any_listen_addr(peer_addr.is_ipv4());
let socket = UdpSocket::bind(local_addr).await?;
Ok((Arc::new(socket), peer_addr))
}
#[inline]
pub async fn new_udp_for(
target: &str,