From b3abb175c19b96e45f05996ab96ba5aa92e20e1f Mon Sep 17 00:00:00 2001 From: lbl8603 <49143209+lbl8603@users.noreply.github.com> Date: Tue, 7 Feb 2023 17:39:34 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=89=93=E6=B4=9E=E7=AD=96?= =?UTF-8?q?=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- switch/src/handle/punch_handler.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/switch/src/handle/punch_handler.rs b/switch/src/handle/punch_handler.rs index fe0f7fb..b8a9770 100644 --- a/switch/src/handle/punch_handler.rs +++ b/switch/src/handle/punch_handler.rs @@ -104,9 +104,10 @@ fn handle( // println!("punch {:?}", punch); match punch.nat_type.enum_value_or_default() { NatType::Symmetric => { - // 碰撞概率 p = 1 - e^(-(k^2+k)/(2n)) n = max_port-min_port 关键词:生日攻击 + // 碰撞概率 p = 1 - e^(-(k^2+k)/(2n)) n = max_port-min_port+1 关键词:生日攻击 let mut send_f = |min_port: u16, max_port: u16, k: usize| -> io::Result<()> { let mut nums: Vec = (min_port..max_port).collect(); + nums.push(max_port); let mut rng = rand::thread_rng(); nums.shuffle(&mut rng); for pub_ip in &punch.public_ip_list { @@ -131,10 +132,10 @@ fn handle( } else { punch.public_port + punch.public_port_range }; - let k = if max_port - min_port > 60 { + let k = if max_port - min_port +1 > 60 { 60 } else { - max_port - min_port + max_port - min_port +1 }; send_f(min_port as u16, max_port as u16, k as usize)?; } @@ -228,13 +229,9 @@ async fn res_symmetric_handle_loop( let mut punch_packet = PunchRequestPacket::new(packet.payload_mut())?; punch_packet.set_source(cur_info.virtual_ip); } - match tokio::time::timeout(Duration::from_secs(30), receiver.recv()).await { - Ok(_) => {} - Err(_e) => {} - } loop { tokio::select! { - rs = tokio::time::timeout(Duration::from_secs(30), receiver.recv()) =>{ + rs = tokio::time::timeout(Duration::from_secs(20), receiver.recv()) =>{ match rs { Ok(punch) => { if let Some(punch) = punch{ @@ -374,6 +371,7 @@ fn send_punch(udp: &UdpSocket, cur_info: &CurrentDeviceInfo, nat_info: NatInfo) let ip = peer_info.virtual_ip; //只向ip比自己大的发起打洞,避免双方同时发起打洞浪费流量 if ip > cur_info.virtual_ip && !DIRECT_ROUTE_TABLE.contains_key(&ip) { + log::info!("发起打洞 {:?}, peer_info:{:?}",nat_info,peer_info); let bytes = punch_packet(cur_info.virtual_ip, nat_info.clone(), ip)?; udp.send_to(&bytes, cur_info.connect_server)?; }