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;