From 0bc711510210eb2c50077e4aedda0b4111b1ee36 Mon Sep 17 00:00:00 2001 From: lbl8603 <49143209+lbl8603@users.noreply.github.com> Date: Sat, 15 Jun 2024 20:51:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=96=E7=BD=AE=E7=BD=91?= =?UTF-8?q?=E5=8D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt/src/core/conn.rs | 37 +++++++++++++++++++++++++++++- vnt/src/core/mod.rs | 18 ++++++++++++--- vnt/src/handle/callback.rs | 16 +++++++------ vnt/src/handle/mod.rs | 9 +++++++- vnt/src/handle/recv_data/client.rs | 7 +++++- vnt/src/handle/recv_data/mod.rs | 5 +++- vnt/src/handle/recv_data/server.rs | 4 +++- 7 files changed, 81 insertions(+), 15 deletions(-) diff --git a/vnt/src/core/conn.rs b/vnt/src/core/conn.rs index f7f03e0..3ecc368 100644 --- a/vnt/src/core/conn.rs +++ b/vnt/src/core/conn.rs @@ -10,10 +10,12 @@ use rand::Rng; use crate::channel::context::ChannelContext; use crate::channel::idle::Idle; use crate::channel::punch::{NatInfo, Punch}; +use crate::channel::sender::IpPacketSender; use crate::channel::{init_channel, init_context, Route, RouteKey}; use crate::cipher::Cipher; #[cfg(feature = "server_encrypt")] use crate::cipher::RsaCipher; +use crate::compression::Compressor; use crate::core::Config; use crate::external_route::{AllowExternalRoute, ExternalRoute}; use crate::handle::handshaker::Handshake; @@ -41,6 +43,9 @@ pub struct Vnt { down_count_watcher: WatchU64Adder, up_count_watcher: WatchSingleU64Adder, client_secret_hash: Option<[u8; 16]>, + compressor: Compressor, + client_cipher: Cipher, + external_route: ExternalRoute, } impl Vnt { @@ -99,8 +104,10 @@ impl Vnt { config.server_address_str.clone(), config.name_servers.clone(), config.mtu.unwrap_or(1420), + #[cfg(feature = "integrated_tun")] #[cfg(target_os = "windows")] config.tap, + #[cfg(feature = "integrated_tun")] #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] config.device_name.clone(), ); @@ -153,6 +160,7 @@ impl Vnt { let out_external_route = AllowExternalRoute::new(config.out_ips.clone()); #[cfg(feature = "ip_proxy")] + #[cfg(feature = "integrated_tun")] let proxy_map = if !config.out_ips.is_empty() && !config.no_proxy { Some(crate::ip_proxy::init_proxy( context.clone(), @@ -209,6 +217,7 @@ impl Vnt { external_route.clone(), out_external_route, #[cfg(feature = "ip_proxy")] + #[cfg(feature = "integrated_tun")] proxy_map.clone(), down_counter, handshake.clone(), @@ -259,6 +268,7 @@ impl Vnt { udp_socket_sender, ); } + let client_cipher = client_cipher.clone(); //延迟启动 scheduler.timeout(Duration::from_secs(3), move |scheduler| { start( @@ -278,7 +288,7 @@ impl Vnt { ); }); } - + let compressor = config.compressor; Ok(Self { stop_manager, config, @@ -290,6 +300,9 @@ impl Vnt { down_count_watcher, up_count_watcher, client_secret_hash: config_info.client_secret_hash, + compressor, + client_cipher, + external_route, }) } } @@ -386,6 +399,9 @@ impl Vnt { pub fn current_device(&self) -> CurrentDeviceInfo { self.current_device.load() } + pub fn current_device_info(&self) -> Arc> { + self.current_device.clone() + } pub fn peer_nat_info(&self, ip: &Ipv4Addr) -> Option { self.peer_nat_info_map.read().get(ip).cloned() } @@ -432,6 +448,12 @@ impl Vnt { let _ = self.context.lock().take(); self.stop_manager.stop() } + pub fn add_stop_listener(&self, name: String, f: F) -> anyhow::Result + where + F: FnOnce() + Send + 'static, + { + self.stop_manager.add_listener(name, f) + } pub fn wait(&self) { self.stop_manager.wait() } @@ -441,4 +463,17 @@ impl Vnt { pub fn config(&self) -> &Config { &self.config } + pub fn ipv4_packet_sender(&self) -> Option { + if let Some(c) = self.context.lock().as_ref() { + Some(IpPacketSender::new( + c.clone(), + self.current_device.clone(), + self.compressor.clone(), + self.client_cipher.clone(), + self.external_route.clone(), + )) + } else { + None + } + } } diff --git a/vnt/src/core/mod.rs b/vnt/src/core/mod.rs index f9c580e..f674cd5 100644 --- a/vnt/src/core/mod.rs +++ b/vnt/src/core/mod.rs @@ -14,6 +14,7 @@ mod conn; #[derive(Clone, Debug)] pub struct Config { + #[cfg(feature = "integrated_tun")] #[cfg(target_os = "windows")] pub tap: bool, pub token: String, @@ -30,6 +31,7 @@ pub struct Config { pub tcp: bool, pub ip: Option, #[cfg(feature = "ip_proxy")] + #[cfg(feature = "integrated_tun")] pub no_proxy: bool, pub server_encrypt: bool, pub cipher_model: CipherModel, @@ -37,6 +39,7 @@ pub struct Config { pub punch_model: PunchModel, pub ports: Option>, pub first_latency: bool, + #[cfg(feature = "integrated_tun")] #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] pub device_name: Option, pub use_channel_type: UseChannelType, @@ -51,7 +54,9 @@ pub struct Config { impl Config { pub fn new( - #[cfg(target_os = "windows")] tap: bool, + #[cfg(feature = "integrated_tun")] + #[cfg(target_os = "windows")] + tap: bool, token: String, device_id: String, name: String, @@ -64,14 +69,18 @@ impl Config { mtu: Option, tcp: bool, ip: Option, - #[cfg(feature = "ip_proxy")] no_proxy: bool, + #[cfg(feature = "integrated_tun")] + #[cfg(feature = "ip_proxy")] + no_proxy: bool, server_encrypt: bool, cipher_model: CipherModel, finger: bool, punch_model: PunchModel, ports: Option>, first_latency: bool, - #[cfg(not(target_os = "android"))] device_name: Option, + #[cfg(feature = "integrated_tun")] + #[cfg(not(target_os = "android"))] + device_name: Option, use_channel_type: UseChannelType, packet_loss_rate: Option, packet_delay: u32, @@ -110,6 +119,7 @@ impl Config { } in_ips.sort_by(|(dest1, _, _), (dest2, _, _)| dest2.cmp(dest1)); Ok(Self { + #[cfg(feature = "integrated_tun")] #[cfg(target_os = "windows")] tap, token, @@ -126,6 +136,7 @@ impl Config { tcp, ip, #[cfg(feature = "ip_proxy")] + #[cfg(feature = "integrated_tun")] no_proxy, server_encrypt, cipher_model, @@ -133,6 +144,7 @@ impl Config { punch_model, ports, first_latency, + #[cfg(feature = "integrated_tun")] #[cfg(not(target_os = "android"))] device_name, use_channel_type, diff --git a/vnt/src/handle/callback.rs b/vnt/src/handle/callback.rs index fe52fa3..7d33f61 100644 --- a/vnt/src/handle/callback.rs +++ b/vnt/src/handle/callback.rs @@ -191,8 +191,10 @@ impl Into for ErrorType { #[derive(Clone, Debug)] pub struct DeviceConfig { + #[cfg(feature = "integrated_tun")] #[cfg(target_os = "windows")] pub tap: bool, + #[cfg(feature = "integrated_tun")] #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] pub device_name: Option, //虚拟网卡mtu值 @@ -211,7 +213,10 @@ pub struct DeviceConfig { impl DeviceConfig { pub fn new( - #[cfg(target_os = "windows")] tap: bool, + #[cfg(feature = "integrated_tun")] + #[cfg(target_os = "windows")] + tap: bool, + #[cfg(feature = "integrated_tun")] #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] device_name: Option, mtu: u32, @@ -222,8 +227,10 @@ impl DeviceConfig { external_route: Vec<(Ipv4Addr, Ipv4Addr)>, ) -> Self { Self { + #[cfg(feature = "integrated_tun")] #[cfg(target_os = "windows")] tap, + #[cfg(feature = "integrated_tun")] #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] device_name, mtu, @@ -297,12 +304,7 @@ pub trait VntCallback: Clone + Send + Sync + 'static { true } #[cfg(not(feature = "integrated_tun"))] - fn create_device( - &self, - _channel_sender: crate::channel::sender::ChannelSender, - _info: DeviceConfig, - ) { - } + fn create_device(&self, _info: DeviceConfig) {} #[cfg(target_os = "android")] #[cfg(feature = "integrated_tun")] fn generate_tun(&self, _info: DeviceConfig) -> usize { diff --git a/vnt/src/handle/mod.rs b/vnt/src/handle/mod.rs index 047f691..3bcc193 100644 --- a/vnt/src/handle/mod.rs +++ b/vnt/src/handle/mod.rs @@ -60,8 +60,10 @@ pub struct BaseConfigInfo { pub server_addr: String, pub name_servers: Vec, pub mtu: u32, + #[cfg(feature = "integrated_tun")] #[cfg(target_os = "windows")] pub tap: bool, + #[cfg(feature = "integrated_tun")] #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] pub device_name: Option, } @@ -77,7 +79,10 @@ impl BaseConfigInfo { server_addr: String, name_servers: Vec, mtu: u32, - #[cfg(target_os = "windows")] tap: bool, + #[cfg(feature = "integrated_tun")] + #[cfg(target_os = "windows")] + tap: bool, + #[cfg(feature = "integrated_tun")] #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] device_name: Option, ) -> Self { @@ -91,8 +96,10 @@ impl BaseConfigInfo { server_addr, name_servers, mtu, + #[cfg(feature = "integrated_tun")] #[cfg(target_os = "windows")] tap, + #[cfg(feature = "integrated_tun")] #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] device_name, } diff --git a/vnt/src/handle/recv_data/client.rs b/vnt/src/handle/recv_data/client.rs index b28f31c..4e54610 100644 --- a/vnt/src/handle/recv_data/client.rs +++ b/vnt/src/handle/recv_data/client.rs @@ -40,6 +40,7 @@ pub struct ClientPacketHandler { nat_test: NatTest, route: AllowExternalRoute, #[cfg(feature = "ip_proxy")] + #[cfg(feature = "integrated_tun")] ip_proxy_map: Option, } @@ -51,7 +52,9 @@ impl ClientPacketHandler { peer_nat_info_map: Arc>>, nat_test: NatTest, route: AllowExternalRoute, - #[cfg(feature = "ip_proxy")] ip_proxy_map: Option, + #[cfg(feature = "integrated_tun")] + #[cfg(feature = "ip_proxy")] + ip_proxy_map: Option, ) -> Self { Self { device, @@ -60,6 +63,7 @@ impl ClientPacketHandler { peer_nat_info_map, nat_test, route, + #[cfg(feature = "integrated_tun")] #[cfg(feature = "ip_proxy")] ip_proxy_map, } @@ -181,6 +185,7 @@ impl ClientPacketHandler { _ => {} } #[cfg(feature = "ip_proxy")] + #[cfg(feature = "integrated_tun")] if let Some(ip_proxy_map) = &self.ip_proxy_map { if ip_proxy_map.recv_handle(&mut ipv4, source, destination)? { return Ok(()); diff --git a/vnt/src/handle/recv_data/mod.rs b/vnt/src/handle/recv_data/mod.rs index 41574b4..0883df4 100644 --- a/vnt/src/handle/recv_data/mod.rs +++ b/vnt/src/handle/recv_data/mod.rs @@ -90,7 +90,9 @@ impl RecvDataHandler { peer_nat_info_map: Arc>>, external_route: ExternalRoute, route: AllowExternalRoute, - #[cfg(feature = "ip_proxy")] ip_proxy_map: Option, + #[cfg(feature = "integrated_tun")] + #[cfg(feature = "ip_proxy")] + ip_proxy_map: Option, counter: U64Adder, handshake: Handshake, #[cfg(feature = "integrated_tun")] @@ -118,6 +120,7 @@ impl RecvDataHandler { peer_nat_info_map, nat_test.clone(), route, + #[cfg(feature = "integrated_tun")] #[cfg(feature = "ip_proxy")] ip_proxy_map, ); diff --git a/vnt/src/handle/recv_data/server.rs b/vnt/src/handle/recv_data/server.rs index 234b951..e746bb5 100644 --- a/vnt/src/handle/recv_data/server.rs +++ b/vnt/src/handle/recv_data/server.rs @@ -306,8 +306,10 @@ impl ServerPacketHandler { log::info!("ip发生变化,old:{:?},response={:?}", old, response); } let device_config = crate::handle::callback::DeviceConfig::new( + #[cfg(feature = "integrated_tun")] #[cfg(target_os = "windows")] self.config_info.tap, + #[cfg(feature = "integrated_tun")] #[cfg(any( target_os = "windows", target_os = "linux", @@ -322,7 +324,7 @@ impl ServerPacketHandler { self.external_route.to_route(), ); #[cfg(not(feature = "integrated_tun"))] - self.callback.create_device(context.sender(), device_config); + self.callback.create_device(device_config); #[cfg(feature = "integrated_tun")] { self.tun_device_helper.stop();