From 7c799589df030d130ac4be1f149ad29a18c56877 Mon Sep 17 00:00:00 2001 From: lbl8603 <49143209+lbl8603@users.noreply.github.com> Date: Wed, 24 Apr 2024 23:01:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4stun=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt/src/nat/stun.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/vnt/src/nat/stun.rs b/vnt/src/nat/stun.rs index d2de85d..fc6cf7f 100644 --- a/vnt/src/nat/stun.rs +++ b/vnt/src/nat/stun.rs @@ -38,7 +38,7 @@ pub fn stun_test_nat(stun_servers: Vec) -> io::Result<(NatType, Vec) -> io::Result<(NatType, Vec, u16)> { let udp = UdpSocket::bind("0.0.0.0:0")?; - udp.set_read_timeout(Some(Duration::from_millis(300)))?; + udp.set_read_timeout(Some(Duration::from_millis(500)))?; let mut nat_type = NatType::Cone; let mut port_range = 0; let mut hash_set = HashSet::new(); @@ -76,7 +76,9 @@ fn test_nat( let mut port_range = 0; let mut hash_set = HashSet::new(); let mut nat_type = NatType::Cone; - let (mapped_addr1, changed_addr1) = test_nat_(&udp, true, true)?; + // 随便搞个当id + let tid = stun_server.as_ptr() as u128; + let (mapped_addr1, changed_addr1) = test_nat_(&udp, true, true, tid)?; match mapped_addr1.ip() { IpAddr::V4(ip) => { hash_set.insert(ip); @@ -84,7 +86,7 @@ fn test_nat( IpAddr::V6(_) => {} } if udp.connect(changed_addr1).is_ok() { - match test_nat_(&udp, false, false) { + match test_nat_(&udp, false, false, tid + 1) { Ok((mapped_addr2, _)) => { match mapped_addr2.ip() { IpAddr::V4(ip) => { @@ -120,12 +122,13 @@ fn test_nat_( udp: &UdpSocket, change_ip: bool, change_port: bool, + tid: u128, ) -> io::Result<(SocketAddr, SocketAddr)> { for _ in 0..2 { let mut buf = [0u8; 28]; let mut msg = stun_format::MsgBuilder::from(buf.as_mut_slice()); msg.typ(stun_format::MsgType::BindingRequest).unwrap(); - msg.tid(1).unwrap(); + msg.tid(tid).unwrap(); msg.add_attr(Attr::ChangeRequest { change_ip, change_port,