From 0cbc3e0f6376ba0a0891aa4999170a47d0b4d6f8 Mon Sep 17 00:00:00 2001 From: lbl8603 <49143209+lbl8603@users.noreply.github.com> Date: Sat, 20 Apr 2024 16:15:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=8C=E9=87=8D=E6=8E=A2=E6=B5=8BNAT?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=EF=BC=8C=E7=94=A8=E4=BA=8E=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E6=9F=90=E4=BA=9B=E5=AF=B9=E7=A7=B0=E7=BD=91=E7=BB=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt/src/nat/stun.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/vnt/src/nat/stun.rs b/vnt/src/nat/stun.rs index 1278443..d2de85d 100644 --- a/vnt/src/nat/stun.rs +++ b/vnt/src/nat/stun.rs @@ -6,8 +6,37 @@ use std::time::Duration; use crate::channel::punch::NatType; use std::net::UdpSocket; use stun_format::Attr; - pub fn stun_test_nat(stun_servers: Vec) -> io::Result<(NatType, Vec, u16)> { + let mut th = Vec::new(); + for _ in 0..2 { + let stun_servers = stun_servers.clone(); + let handle = std::thread::spawn(move || stun_test_nat0(stun_servers)); + th.push(handle); + } + let mut nat_type = NatType::Cone; + let mut port_range = 0; + let mut hash_set = HashSet::new(); + for x in th { + match x.join().unwrap() { + Ok((nat_type_t, ip_list_t, port_range_t)) => { + if nat_type_t == NatType::Symmetric { + nat_type = NatType::Symmetric; + } + for x in ip_list_t { + hash_set.insert(x); + } + if port_range < port_range_t { + port_range = port_range_t; + } + } + Err(e) => { + log::warn!("{:?}", e); + } + } + } + Ok((nat_type, hash_set.into_iter().collect(), port_range)) +} +pub fn stun_test_nat0(stun_servers: Vec) -> io::Result<(NatType, Vec, u16)> { let udp = UdpSocket::bind("0.0.0.0:0")?; udp.set_read_timeout(Some(Duration::from_millis(300)))?; let mut nat_type = NatType::Cone;