nat地址判断
This commit is contained in:
@@ -52,7 +52,13 @@ impl NatInfo {
|
|||||||
ipv6_addr: SocketAddrV6,
|
ipv6_addr: SocketAddrV6,
|
||||||
mut nat_type: NatType,
|
mut nat_type: NatType,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
public_ips.retain(|ip| !ip.is_loopback() && !ip.is_private() && !ip.is_unspecified());
|
public_ips.retain(|ip| {
|
||||||
|
!ip.is_multicast()
|
||||||
|
&& !ip.is_broadcast()
|
||||||
|
&& !ip.is_unspecified()
|
||||||
|
&& !ip.is_loopback()
|
||||||
|
&& !ip.is_private()
|
||||||
|
});
|
||||||
if public_ips.len() > 1 {
|
if public_ips.len() > 1 {
|
||||||
nat_type = NatType::Symmetric;
|
nat_type = NatType::Symmetric;
|
||||||
}
|
}
|
||||||
@@ -65,6 +71,20 @@ impl NatInfo {
|
|||||||
nat_type,
|
nat_type,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn update_addr(&mut self, ip: Ipv4Addr, port: u16) {
|
||||||
|
if !ip.is_multicast()
|
||||||
|
&& !ip.is_broadcast()
|
||||||
|
&& !ip.is_unspecified()
|
||||||
|
&& !ip.is_loopback()
|
||||||
|
&& !ip.is_private()
|
||||||
|
&& port != 0
|
||||||
|
{
|
||||||
|
self.public_port = port;
|
||||||
|
if !self.public_ips.contains(&ip) {
|
||||||
|
self.public_ips.push(ip);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -165,15 +185,17 @@ impl Punch {
|
|||||||
self.port_index.insert(id, index);
|
self.port_index.insert(id, index);
|
||||||
}
|
}
|
||||||
NatType::Cone => {
|
NatType::Cone => {
|
||||||
let is_cone = self.context.is_cone();
|
if nat_info.public_port != 0 {
|
||||||
for ip in nat_info.public_ips {
|
let is_cone = self.context.is_cone();
|
||||||
let addr = SocketAddr::V4(SocketAddrV4::new(ip, nat_info.public_port));
|
for ip in nat_info.public_ips {
|
||||||
self.context.send_main_udp(buf, addr)?;
|
let addr = SocketAddr::V4(SocketAddrV4::new(ip, nat_info.public_port));
|
||||||
if !is_cone {
|
self.context.send_main_udp(buf, addr)?;
|
||||||
//只有一方是对称,则对称方要使用全部端口发送数据,符合上述计算的概率
|
if !is_cone {
|
||||||
self.context.try_send_all(buf, addr)?;
|
//只有一方是对称,则对称方要使用全部端口发送数据,符合上述计算的概率
|
||||||
|
self.context.try_send_all(buf, addr)?;
|
||||||
|
}
|
||||||
|
tokio::time::sleep(Duration::from_millis(2)).await;
|
||||||
}
|
}
|
||||||
tokio::time::sleep(Duration::from_millis(2)).await;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-13
@@ -111,19 +111,8 @@ impl NatTest {
|
|||||||
self.info.lock().clone()
|
self.info.lock().clone()
|
||||||
}
|
}
|
||||||
pub fn update_addr(&self, ip: Ipv4Addr, port: u16) {
|
pub fn update_addr(&self, ip: Ipv4Addr, port: u16) {
|
||||||
if !ip.is_multicast()
|
let mut guard = self.info.lock();
|
||||||
&& !ip.is_broadcast()
|
guard.update_addr(ip, port)
|
||||||
&& !ip.is_unspecified()
|
|
||||||
&& !ip.is_loopback()
|
|
||||||
&& !ip.is_private()
|
|
||||||
&& port != 0
|
|
||||||
{
|
|
||||||
let mut guard = self.info.lock();
|
|
||||||
guard.public_port = port;
|
|
||||||
if !guard.public_ips.contains(&ip) {
|
|
||||||
guard.public_ips.push(ip);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pub async fn re_test(
|
pub async fn re_test(
|
||||||
&self,
|
&self,
|
||||||
|
|||||||
Reference in New Issue
Block a user