From 3675650864681836933a49a79609296de5fd2802 Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Fri, 1 Aug 2025 10:44:05 +0200 Subject: [PATCH] holepuching fixes --- src/client/net.rs | 29 +++++++++++------------------ src/lib.rs | 2 ++ src/server/net.rs | 5 ++--- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/client/net.rs b/src/client/net.rs index 5c736e1..f372fc9 100644 --- a/src/client/net.rs +++ b/src/client/net.rs @@ -620,7 +620,7 @@ pub async fn handle_incoming_connection( ); #[cfg(debug_assertions)] - eprintln!( + eprintln!( "registering network:\niv: {}\nsockaddr: {}", &buf[P2PStandardDataPositions::IV as usize ..P2PStandardDataPositions::IV as usize + BLOCK_SIZE].iter().map(|x| format!("{:02X} ", x)).collect::(), @@ -637,7 +637,7 @@ pub async fn handle_incoming_connection( &network.read().unwrap().key, &buf[P2PStandardDataPositions::IV as usize ..P2PStandardDataPositions::IV as usize + BLOCK_SIZE], - &buf[P2PStandardDataPositions::DATA as usize..data_lenght as usize /*compensate for size and index diference*/], + &buf[P2PStandardDataPositions::DATA as usize..data_lenght as usize /*compensate for size and index diference*/], ) { Ok(v) => { data_tmp = v.into_boxed_slice(); @@ -676,22 +676,15 @@ pub async fn handle_incoming_connection( return; } }; - - match P2P_query( - // create NAT mapping - &mut buf, - &peer_addr, - &socket, - network.read().unwrap().encrypted, - network.read().unwrap().key, - ) { - Ok(_) => {} - Err(e) => eprintln!( - "{} failed to create NAT mapping to peer connection may not work Error: {}", - "[ERROR]".red(), - e - ), - }; + for _ in 0..MAPPING_SHOT_COUNT { + match socket.send_to(&[P2PMethods::PEER_QUERY as u8], peer_addr) { + Ok(s) => { + #[cfg(debug_assertions)] + eprintln!("send {} bytes", s); + } + Err(e) => eprintln!("{} failed to send puching packet: {}", "[ERROR]".red(), e), + } + } } _ => { eprintln!( diff --git a/src/lib.rs b/src/lib.rs index c418d9e..dd19c81 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,8 @@ pub const STANDARD_RETRY_MAX: usize = 10; pub const DEST_IN_IPV4_OFFSET: usize = 16; pub const IPV4_SIZE: usize = 4; +pub const MAPPING_SHOT_COUNT: u8 = 5; + pub const DEFAULT_NETWORK_PREFIX: [u8; 3] = [172, 22, 44]; #[repr(u8)] diff --git a/src/server/net.rs b/src/server/net.rs index f8e255f..7f00ad1 100644 --- a/src/server/net.rs +++ b/src/server/net.rs @@ -333,9 +333,8 @@ pub async fn handle_request( send_buf[0] = P2PMethods::NEW_CLIENT_NOTIFY as u8; send_buf[P2PStandardDataPositions::IV as usize..P2PStandardDataPositions::IV as usize+ BLOCK_SIZE].copy_from_slice(&iv); send_buf[P2PStandardDataPositions::DATA as usize..P2PStandardDataPositions::DATA as usize + sock_addr_len as usize].copy_from_slice(&sock_addr); - let mut resp_buf: [u8; UDP_BUFFER_SIZE] = [0u8; UDP_BUFFER_SIZE]; - match shared::net::send_and_recv_with_retry(&mut resp_buf, &send_buf, &c.src, &socket, STANDARD_RETRY_MAX) { - Ok((data_lenght, _)) => { + match socket.send_to(&send_buf, src) { + Ok(data_lenght) => { #[cfg(debug_assertions)] eprintln!("send {} bytes", data_lenght); },