From b3a4a4de5ea29a15dd2c8372920109c88909464f Mon Sep 17 00:00:00 2001 From: lubeilin <1791778603@qq.com> Date: Mon, 4 Sep 2023 20:35:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=97=A5=E5=BF=97=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt-cli/src/command/mod.rs | 11 ++++++--- vnt/src/cipher/cipher.rs | 2 +- vnt/src/handle/heartbeat_handler.rs | 2 +- vnt/src/handle/recv_handler.rs | 36 +++++++++++++++++------------ vnt/src/nat/mod.rs | 21 +++++------------ 5 files changed, 37 insertions(+), 35 deletions(-) diff --git a/vnt-cli/src/command/mod.rs b/vnt-cli/src/command/mod.rs index 531f342..ca45ad6 100644 --- a/vnt-cli/src/command/mod.rs +++ b/vnt-cli/src/command/mod.rs @@ -81,7 +81,7 @@ pub fn command_list(vnt: &Vnt) -> Vec { for peer in device_list { let name = peer.name; let virtual_ip = peer.virtual_ip.to_string(); - let (nat_type, public_ips, local_ip,ipv6) = + let (nat_type, public_ips, local_ip, ipv6) = if let Some(nat_info) = vnt.peer_nat_info(&peer.virtual_ip) { let nat_type = format!("{:?}", nat_info.nat_type); let public_ips: Vec = @@ -89,9 +89,14 @@ pub fn command_list(vnt: &Vnt) -> Vec { let public_ips = public_ips.join(","); let local_ip = nat_info.local_ipv4_addr.ip().to_string(); let ipv6 = nat_info.ipv6_addr.ip().to_string(); - (nat_type, public_ips, local_ip,ipv6) + (nat_type, public_ips, local_ip, ipv6) } else { - ("".to_string(), "".to_string(), "".to_string(), "".to_string()) + ( + "".to_string(), + "".to_string(), + "".to_string(), + "".to_string(), + ) }; let (nat_traversal_type, rt) = if let Some(route) = vnt.route(&peer.virtual_ip) { let nat_traversal_type = if route.metric == 1 { diff --git a/vnt/src/cipher/cipher.rs b/vnt/src/cipher/cipher.rs index 0a5b93a..4715b78 100644 --- a/vnt/src/cipher/cipher.rs +++ b/vnt/src/cipher/cipher.rs @@ -21,7 +21,7 @@ impl FromStr for CipherModel { type Err = String; fn from_str(s: &str) -> Result { - match s { + match s.to_lowercase().trim() { "aes_gcm" => Ok(CipherModel::AesGcm), "aes_cbc" => Ok(CipherModel::AesCbc), "aes_ecb" => Ok(CipherModel::AesEcb), diff --git a/vnt/src/handle/heartbeat_handler.rs b/vnt/src/handle/heartbeat_handler.rs index efa4a69..69c93ee 100644 --- a/vnt/src/handle/heartbeat_handler.rs +++ b/vnt/src/handle/heartbeat_handler.rs @@ -37,7 +37,7 @@ async fn start_idle_(idle: Idle, sender: ChannelSender) -> io::Result<()> { log::info!("启动空闲检查任务"); loop { let (peer_ip, route) = idle.next_idle().await?; - log::info!("peer_ip:{:?},route:{:?}", peer_ip, route); + log::info!("路由空闲 peer_ip:{:?},route:{:?}", peer_ip, route); sender.remove_route(&peer_ip, route); } } diff --git a/vnt/src/handle/recv_handler.rs b/vnt/src/handle/recv_handler.rs index 664e853..121f6d7 100644 --- a/vnt/src/handle/recv_handler.rs +++ b/vnt/src/handle/recv_handler.rs @@ -668,20 +668,26 @@ impl ChannelDataHandler { service_packet::Protocol::RegistrationRequest => {} service_packet::Protocol::RegistrationResponse => { let response = RegistrationResponse::parse_from_bytes(net_packet.payload())?; - let local_port = context.main_local_ipv4_port().unwrap_or(0); - let local_ipv4_addr = nat::local_ipv4_addr(local_port); - let local_port = context.main_local_ipv6_port().unwrap_or(0); - let ipv6_addr = nat::local_ipv6_addr(local_port); - let nat_info = self - .nat_test - .re_test( - Ipv4Addr::from(response.public_ip), - response.public_port as u16, - local_ipv4_addr, - ipv6_addr, - ) - .await; - context.switch(nat_info.nat_type); + + { + let context = context.clone(); + let nat_test = self.nat_test.clone(); + tokio::spawn(async move { + let local_port = context.main_local_ipv4_port().unwrap_or(0); + let local_ipv4_addr = nat::local_ipv4_addr(local_port); + let local_port = context.main_local_ipv6_port().unwrap_or(0); + let ipv6_addr = nat::local_ipv6_addr(local_port); + let nat_info = nat_test + .re_test( + Ipv4Addr::from(response.public_ip), + response.public_port as u16, + local_ipv4_addr, + ipv6_addr, + ) + .await; + context.switch(nat_info.nat_type); + }); + } let new_ip = Ipv4Addr::from(response.virtual_ip); let current_ip = current_device.virtual_ip(); if current_ip != new_ip { @@ -732,7 +738,7 @@ impl ChannelDataHandler { ) }) .collect(); - let route = Route::from(*route_key, 2, 99); + let route = Route::from(*route_key, 2, 199); for x in &ip_list { if x.status == PeerDeviceStatus::Online { context.add_route_if_absent(x.virtual_ip, route); diff --git a/vnt/src/nat/mod.rs b/vnt/src/nat/mod.rs index 3b6cc9d..a695b1f 100644 --- a/vnt/src/nat/mod.rs +++ b/vnt/src/nat/mod.rs @@ -84,25 +84,16 @@ impl NatTest { ) -> NatTest { let server = stun_server[0].clone(); stun_server.resize(3, server); - let nat_info = NatInfo::new( - vec![public_ip], + let nat_info = Self::re_test_( + &stun_server, + public_ip, public_port, - 0, local_ipv4_addr, ipv6_addr, - NatType::Cone, - ); + ) + .await; let info = Arc::new(Mutex::new(nat_info)); - let nat_test = NatTest { stun_server, info }; - { - let nat_test = nat_test.clone(); - tokio::spawn(async move { - let _ = nat_test - .re_test(public_ip, public_port, local_ipv4_addr, ipv6_addr) - .await; - }); - } - nat_test + NatTest { stun_server, info } } pub fn nat_info(&self) -> NatInfo { self.info.lock().clone()