From e3f328cc9bb3b8bf194f81c541e7b1381b5e3f3a Mon Sep 17 00:00:00 2001 From: lubeilin <1791778603@qq.com> Date: Sat, 2 Mar 2024 14:03:19 +0800 Subject: [PATCH] =?UTF-8?q?[mio]=20=E4=BF=AE=E5=A4=8D=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E9=80=9A=E9=81=93=E7=B1=BB=E5=9E=8B=E6=97=A0=E6=95=88=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt/src/channel/context.rs | 3 +++ vnt/src/core/conn.rs | 29 ++++++++++++++--------------- vnt/src/handle/recv_data/client.rs | 11 ++++------- vnt/src/handle/recv_data/mod.rs | 4 +--- vnt/src/handle/tun_tap/mod.rs | 4 +++- 5 files changed, 25 insertions(+), 26 deletions(-) diff --git a/vnt/src/channel/context.rs b/vnt/src/channel/context.rs index e0094cc..f67c443 100644 --- a/vnt/src/channel/context.rs +++ b/vnt/src/channel/context.rs @@ -73,6 +73,9 @@ pub struct ContextInner { } impl ContextInner { + pub fn use_channel_type(&self) -> UseChannelType { + self.route_table.use_channel_type + } pub fn is_stop(&self) -> bool { !self.state.load(Ordering::Acquire) } diff --git a/vnt/src/core/conn.rs b/vnt/src/core/conn.rs index 9b116b2..8d92817 100644 --- a/vnt/src/core/conn.rs +++ b/vnt/src/core/conn.rs @@ -14,7 +14,7 @@ use tun::device::IFace; use crate::channel::context::Context; use crate::channel::idle::Idle; use crate::channel::punch::{NatInfo, Punch}; -use crate::channel::{init_channel, init_context, Route, RouteKey, UseChannelType}; +use crate::channel::{init_channel, init_context, Route, RouteKey}; use crate::cipher::Cipher; #[cfg(feature = "server_encrypt")] use crate::cipher::RsaCipher; @@ -136,7 +136,8 @@ impl Vnt { let (punch_sender, punch_receiver) = sync_channel(3); let peer_nat_info_map: Arc>> = Arc::new(RwLock::new(HashMap::with_capacity(16))); - let down_counter = U64Adder::with_capacity(config.ports.as_ref().map(|v| v.len()).unwrap_or_default() + 8); + let down_counter = + U64Adder::with_capacity(config.ports.as_ref().map(|v| v.len()).unwrap_or_default() + 8); let down_count_watcher = down_counter.watch(); let handler = RecvDataHandler::new( #[cfg(feature = "server_encrypt")] @@ -149,7 +150,6 @@ impl Vnt { config_info.clone(), nat_test.clone(), callback.clone(), - config.use_channel_type, punch_sender, peer_nat_info_map.clone(), external_route.clone(), @@ -198,8 +198,7 @@ impl Vnt { let nat_test = nat_test.clone(); let device_list = device_list.clone(); let current_device = current_device.clone(); - let use_channel_type = config.use_channel_type; - if !use_channel_type.is_only_relay() { + if !config.use_channel_type.is_only_relay() { // 定时nat探测 maintain::retrieve_nat_type( &scheduler, @@ -222,7 +221,6 @@ impl Vnt { config_info, punch, callback, - use_channel_type, ); }); } @@ -253,7 +251,6 @@ pub fn start( config_info: BaseConfigInfo, punch: Punch, callback: Call, - use_channel_type: UseChannelType, ) { // 定时心跳 maintain::heartbeat( @@ -275,13 +272,15 @@ pub fn start( callback, ); // 定时客户端中继检测 - maintain::client_relay( - &scheduler, - context.clone(), - current_device.clone(), - device_list.clone(), - client_cipher.clone(), - ); + if !context.use_channel_type().is_only_p2p() { + maintain::client_relay( + &scheduler, + context.clone(), + current_device.clone(), + device_list.clone(), + client_cipher.clone(), + ); + } // 定时地址探测 maintain::addr_request( &scheduler, @@ -290,7 +289,7 @@ pub fn start( server_cipher.clone(), config_info.clone(), ); - if !use_channel_type.is_only_relay() { + if !context.use_channel_type().is_only_relay() { // 定时打洞 maintain::punch( &scheduler, diff --git a/vnt/src/handle/recv_data/client.rs b/vnt/src/handle/recv_data/client.rs index 58cb730..8441d81 100644 --- a/vnt/src/handle/recv_data/client.rs +++ b/vnt/src/handle/recv_data/client.rs @@ -15,7 +15,7 @@ use tun::Device; use crate::channel::context::Context; use crate::channel::punch::NatInfo; -use crate::channel::{Route, RouteKey, UseChannelType}; +use crate::channel::{Route, RouteKey}; use crate::cipher::Cipher; use crate::external_route::AllowExternalRoute; use crate::handle::recv_data::PacketHandler; @@ -35,7 +35,6 @@ use crate::protocol::{ pub struct ClientPacketHandler { device: Arc, client_cipher: Cipher, - use_channel_type: UseChannelType, punch_sender: SyncSender<(Ipv4Addr, NatInfo)>, peer_nat_info_map: Arc>>, nat_test: NatTest, @@ -48,7 +47,6 @@ impl ClientPacketHandler { pub fn new( device: Arc, client_cipher: Cipher, - use_channel_type: UseChannelType, punch_sender: SyncSender<(Ipv4Addr, NatInfo)>, peer_nat_info_map: Arc>>, nat_test: NatTest, @@ -58,7 +56,6 @@ impl ClientPacketHandler { Self { device, client_cipher, - use_channel_type, punch_sender, peer_nat_info_map, nat_test, @@ -189,7 +186,7 @@ impl ClientPacketHandler { context.route_table.add_route(source, route); } ControlPacket::PunchRequest => { - if self.use_channel_type.is_only_relay() { + if context.use_channel_type().is_only_relay() { return Ok(()); } //回应 @@ -203,7 +200,7 @@ impl ClientPacketHandler { context.route_table.add_route_if_absent(source, route); } ControlPacket::PunchResponse => { - if self.use_channel_type.is_only_relay() { + if context.use_channel_type().is_only_relay() { return Ok(()); } let route = Route::from(route_key, 1, 199); @@ -237,7 +234,7 @@ impl ClientPacketHandler { net_packet: NetPacket<&mut [u8]>, route_key: RouteKey, ) -> io::Result<()> { - if self.use_channel_type.is_only_relay() { + if context.use_channel_type().is_only_relay() { return Ok(()); } let source = net_packet.source(); diff --git a/vnt/src/handle/recv_data/mod.rs b/vnt/src/handle/recv_data/mod.rs index 5040ad6..0b91168 100644 --- a/vnt/src/handle/recv_data/mod.rs +++ b/vnt/src/handle/recv_data/mod.rs @@ -12,7 +12,7 @@ use tun::Device; use crate::channel::context::Context; use crate::channel::handler::RecvChannelHandler; use crate::channel::punch::NatInfo; -use crate::channel::{RouteKey, UseChannelType}; +use crate::channel::RouteKey; use crate::cipher::Cipher; #[cfg(feature = "server_encrypt")] use crate::cipher::RsaCipher; @@ -60,7 +60,6 @@ impl RecvDataHandler { config_info: BaseConfigInfo, nat_test: NatTest, callback: Call, - use_channel_type: UseChannelType, punch_sender: SyncSender<(Ipv4Addr, NatInfo)>, peer_nat_info_map: Arc>>, external_route: ExternalRoute, @@ -83,7 +82,6 @@ impl RecvDataHandler { let client = ClientPacketHandler::new( device.clone(), client_cipher, - use_channel_type, punch_sender, peer_nat_info_map, nat_test, diff --git a/vnt/src/handle/tun_tap/mod.rs b/vnt/src/handle/tun_tap/mod.rs index cfbbb11..3f557b7 100644 --- a/vnt/src/handle/tun_tap/mod.rs +++ b/vnt/src/handle/tun_tap/mod.rs @@ -151,7 +151,9 @@ pub fn base_handle( client_cipher.encrypt_ipv4(&mut net_packet)?; //优先发到直连到地址 if context.send_by_id(net_packet.buffer(), &dest_ip).is_err() { - context.send_default(net_packet.buffer(), current_device.connect_server)?; + if !context.use_channel_type().is_only_p2p() { + context.send_default(net_packet.buffer(), current_device.connect_server)?; + } } return Ok(()); }