From 9df34207f754d90500320fd8c477440e901652d7 Mon Sep 17 00:00:00 2001 From: lubeilin <1791778603@qq.com> Date: Sun, 8 Oct 2023 17:46:31 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E4=B8=8D=E5=90=8Cfeatures?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E6=97=B6=E5=91=8A=E8=AD=A6=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt-cli/src/main.rs | 15 ++++ vnt/Cargo.toml | 2 +- vnt/src/channel/channel.rs | 9 +-- vnt/src/cipher/cipher.rs | 110 +++++++++++++++++++++++++- vnt/src/cipher/mod.rs | 14 ++++ vnt/src/core/mod.rs | 23 +++--- vnt/src/handle/recv_handler.rs | 19 +++-- vnt/src/handle/tun_tap/mod.rs | 3 +- vnt/src/handle/tun_tap/tap_handler.rs | 9 +-- vnt/src/handle/tun_tap/tun_handler.rs | 9 +-- vnt/src/igmp_server/mod.rs | 21 +++-- 11 files changed, 178 insertions(+), 56 deletions(-) diff --git a/vnt-cli/src/main.rs b/vnt-cli/src/main.rs index 45d7323..06d42f8 100644 --- a/vnt-cli/src/main.rs +++ b/vnt-cli/src/main.rs @@ -483,6 +483,21 @@ fn print_usage(program: &str, _opts: Options) { println!(" -i 配置点对网(IP代理)时使用,-i 192.168.0.0/24,10.26.0.3表示允许接收网段192.168.0.0/24的数据"); println!(" 并转发到10.26.0.3,可指定多个网段"); println!(" -o 配置点对网时使用,-o 192.168.0.0/24表示允许将数据转发到192.168.0.0/24,可指定多个网段"); + #[cfg(not(any( + feature = "aes_gcm", + feature = "server_encrypt", + feature = "aes_cbc", + feature = "aes_ecb", + feature = "sm4_cbc" + )))] + let enums = String::new(); + #[cfg(any( + feature = "aes_gcm", + feature = "server_encrypt", + feature = "aes_cbc", + feature = "aes_ecb", + feature = "sm4_cbc" + ))] let mut enums = String::new(); #[cfg(any(feature = "aes_gcm", feature = "server_encrypt"))] enums.push_str("/aes_gcm"); diff --git a/vnt/Cargo.toml b/vnt/Cargo.toml index 949d3fc..55e9e39 100644 --- a/vnt/Cargo.toml +++ b/vnt/Cargo.toml @@ -53,5 +53,5 @@ aes_cbc=["cbc"] aes_ecb=["ecb"] sm4_cbc=["libsm"] aes_gcm=["aes-gcm"] -server_encrypt =["rsa","spki"] +server_encrypt =["aes-gcm","rsa","spki"] ip_proxy=["dashmap"] diff --git a/vnt/src/channel/channel.rs b/vnt/src/channel/channel.rs index 39acc43..377794f 100644 --- a/vnt/src/channel/channel.rs +++ b/vnt/src/channel/channel.rs @@ -298,12 +298,7 @@ impl Context { } fn get_udp_by_route(&self, route_key: &RouteKey) -> Option> { let guard = &crossbeam_epoch::pin(); - let udp_map = unsafe { - self.inner - .udp_map - .load(Ordering::Relaxed, guard) - .deref() - }; + let udp_map = unsafe { self.inner.udp_map.load(Ordering::Relaxed, guard).deref() }; udp_map.get(&route_key.index).cloned() } @@ -500,7 +495,7 @@ impl Context { table_share = e.current; } } - }else{ + } else { return; } } diff --git a/vnt/src/cipher/cipher.rs b/vnt/src/cipher/cipher.rs index ba77d08..ad416be 100644 --- a/vnt/src/cipher/cipher.rs +++ b/vnt/src/cipher/cipher.rs @@ -15,8 +15,22 @@ use crate::cipher::openssl_aes_ecb::AesEcbCipher; use crate::cipher::ring_aes_gcm_cipher::AesGcmCipher; #[cfg(feature = "sm4_cbc")] use crate::cipher::sm4_cbc::Sm4CbcCipher; +#[cfg(any( + feature = "aes_gcm", + feature = "server_encrypt", + feature = "aes_cbc", + feature = "aes_ecb", + feature = "sm4_cbc" +))] use crate::cipher::Finger; use crate::protocol::NetPacket; +#[cfg(any( + feature = "aes_gcm", + feature = "server_encrypt", + feature = "aes_cbc", + feature = "aes_ecb", + feature = "sm4_cbc" +))] use sha2::Digest; use std::io; use std::str::FromStr; @@ -38,6 +52,21 @@ impl FromStr for CipherModel { type Err = String; fn from_str(s: &str) -> Result { + #[cfg(not(any( + feature = "aes_gcm", + feature = "server_encrypt", + feature = "aes_cbc", + feature = "aes_ecb", + feature = "sm4_cbc" + )))] + return Err(format!("not match '{}', no encrypt", s)); + #[cfg(any( + feature = "aes_gcm", + feature = "server_encrypt", + feature = "aes_cbc", + feature = "aes_ecb", + feature = "sm4_cbc" + ))] match s.to_lowercase().trim() { #[cfg(any(feature = "aes_gcm", feature = "server_encrypt"))] "aes_gcm" => Ok(CipherModel::AesGcm), @@ -49,7 +78,7 @@ impl FromStr for CipherModel { "sm4_cbc" => Ok(CipherModel::Sm4Cbc), _ => { let mut enums = String::new(); - #[cfg(feature = "aes_gcm")] + #[cfg(any(feature = "aes_gcm", feature = "server_encrypt"))] enums.push_str("/aes_gcm"); #[cfg(feature = "aes_cbc")] enums.push_str("/aes_cbc"); @@ -80,8 +109,28 @@ pub enum Cipher { Sm4Cbc(Sm4CbcCipher), None, } - impl Cipher { + #[cfg(not(any( + feature = "aes_gcm", + feature = "server_encrypt", + feature = "aes_cbc", + feature = "aes_ecb", + feature = "sm4_cbc" + )))] + pub fn new_password( + _model: CipherModel, + _password: Option, + _token: Option, + ) -> Self { + Cipher::None + } + #[cfg(any( + feature = "aes_gcm", + feature = "server_encrypt", + feature = "aes_cbc", + feature = "aes_ecb", + feature = "sm4_cbc" + ))] pub fn new_password( model: CipherModel, password: Option, @@ -134,6 +183,23 @@ impl Cipher { Cipher::None } } + #[cfg(not(any( + feature = "aes_gcm", + feature = "server_encrypt", + feature = "aes_cbc", + feature = "aes_ecb", + feature = "sm4_cbc" + )))] + pub fn new_key(_key: [u8; 32], _token: String) -> io::Result { + Err(io::Error::new(io::ErrorKind::Other, "key error")) + } + #[cfg(any( + feature = "aes_gcm", + feature = "server_encrypt", + feature = "aes_cbc", + feature = "aes_ecb", + feature = "sm4_cbc" + ))] pub fn new_key(key: [u8; 32], token: String) -> io::Result { let finger = Some(Finger::new(&token)); match key.len() { @@ -171,6 +237,26 @@ impl Cipher { } } } + #[cfg(not(any( + feature = "aes_gcm", + feature = "server_encrypt", + feature = "aes_cbc", + feature = "aes_ecb", + feature = "sm4_cbc" + )))] + pub fn encrypt_ipv4 + AsMut<[u8]>>( + &self, + _net_packet: &mut NetPacket, + ) -> io::Result<()> { + Ok(()) + } + #[cfg(any( + feature = "aes_gcm", + feature = "server_encrypt", + feature = "aes_cbc", + feature = "aes_ecb", + feature = "sm4_cbc" + ))] pub fn encrypt_ipv4 + AsMut<[u8]>>( &self, net_packet: &mut NetPacket, @@ -187,6 +273,26 @@ impl Cipher { Cipher::None => Ok(()), } } + #[cfg(not(any( + feature = "aes_gcm", + feature = "server_encrypt", + feature = "aes_cbc", + feature = "aes_ecb", + feature = "sm4_cbc" + )))] + pub fn check_finger + AsMut<[u8]>>( + &self, + _net_packet: &NetPacket, + ) -> io::Result<()> { + Ok(()) + } + #[cfg(any( + feature = "aes_gcm", + feature = "server_encrypt", + feature = "aes_cbc", + feature = "aes_ecb", + feature = "sm4_cbc" + ))] pub fn check_finger>(&self, net_packet: &NetPacket) -> io::Result<()> { match self { #[cfg(any(feature = "aes_gcm", feature = "server_encrypt"))] diff --git a/vnt/src/cipher/mod.rs b/vnt/src/cipher/mod.rs index c75834c..a2f9bbc 100644 --- a/vnt/src/cipher/mod.rs +++ b/vnt/src/cipher/mod.rs @@ -7,6 +7,13 @@ mod aes_ecb; #[cfg(not(feature = "ring-cipher"))] mod aes_gcm_cipher; mod cipher; +#[cfg(any( + feature = "aes_gcm", + feature = "server_encrypt", + feature = "aes_cbc", + feature = "aes_ecb", + feature = "sm4_cbc" +))] mod finger; #[cfg(feature = "aes_ecb")] #[cfg(any(feature = "openssl-vendored", feature = "openssl"))] @@ -19,5 +26,12 @@ mod rsa_cipher; mod sm4_cbc; pub use cipher::Cipher; pub use cipher::CipherModel; +#[cfg(any( + feature = "aes_gcm", + feature = "server_encrypt", + feature = "aes_cbc", + feature = "aes_ecb", + feature = "sm4_cbc" +))] pub use finger::Finger; pub use rsa_cipher::RsaCipher; diff --git a/vnt/src/core/mod.rs b/vnt/src/core/mod.rs index 154b457..16de099 100644 --- a/vnt/src/core/mod.rs +++ b/vnt/src/core/mod.rs @@ -1,10 +1,10 @@ use std::collections::HashMap; use std::io; -use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; use std::net::TcpStream; use std::net::UdpSocket; -use std::sync::Arc; +use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; use std::sync::atomic::Ordering; +use std::sync::Arc; use std::time::Duration; use crossbeam_epoch::Atomic; @@ -13,25 +13,25 @@ use parking_lot::Mutex; use rand::Rng; use tokio::sync::mpsc::channel; -use crate::channel::{Route, RouteKey}; use crate::channel::channel::{Channel, Context}; use crate::channel::idle::Idle; use crate::channel::punch::{NatInfo, Punch, PunchModel}; use crate::channel::sender::ChannelSender; +use crate::channel::{Route, RouteKey}; use crate::cipher::{Cipher, CipherModel, RsaCipher}; use crate::core::status::VntStatusManger; use crate::error::Error; use crate::external_route::{AllowExternalRoute, ExternalRoute}; -use crate::handle::{ - ConnectStatus, CurrentDeviceInfo, handshake_handler, heartbeat_handler, PeerDeviceInfo, - punch_handler, registration_handler, -}; use crate::handle::handshake_handler::HandshakeEnum; use crate::handle::recv_handler::ChannelDataHandler; use crate::handle::registration_handler::{RegResponse, ReqEnum}; #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] use crate::handle::tun_tap::tap_handler; use crate::handle::tun_tap::tun_handler; +use crate::handle::{ + handshake_handler, heartbeat_handler, punch_handler, registration_handler, ConnectStatus, + CurrentDeviceInfo, PeerDeviceInfo, +}; use crate::igmp_server::IgmpServer; use crate::nat::NatTest; use crate::tun_tap_device; @@ -88,7 +88,7 @@ impl VntUtil { None }; let server_cipher = if config.server_encrypt { - let mut key = [0 as u8; 32]; + let mut key = [0u8; 32]; rand::thread_rng().fill(&mut key); Cipher::new_key(key, config.token.clone())? } else { @@ -278,7 +278,8 @@ impl VntUtil { )); let device_list: Arc)>> = Arc::new(Mutex::new((response.epoch, response.device_info_list))); - let peer_nat_info_map: Arc>> = Arc::new(Atomic::new(HashMap::new())); + let peer_nat_info_map: Arc>> = + Arc::new(Atomic::new(HashMap::new())); let connect_status = Arc::new(AtomicCell::new(ConnectStatus::Connected)); let public_ip = response.public_ip; let public_port = response.public_port; @@ -504,8 +505,8 @@ impl Vnt { } pub fn peer_nat_info(&self, ip: &Ipv4Addr) -> Option { let guard = &crossbeam_epoch::pin(); - let shared = self.peer_nat_info_map.load(Ordering::Acquire,guard); - let map = unsafe{shared.deref()}; + let shared = self.peer_nat_info_map.load(Ordering::Acquire, guard); + let map = unsafe { shared.deref() }; map.get(ip).map(|e| e.clone()) } pub fn connection_status(&self) -> ConnectStatus { diff --git a/vnt/src/handle/recv_handler.rs b/vnt/src/handle/recv_handler.rs index dd3e576..3232b6e 100644 --- a/vnt/src/handle/recv_handler.rs +++ b/vnt/src/handle/recv_handler.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6}; -use std::sync::Arc; use std::sync::atomic::Ordering; +use std::sync::Arc; use std::time::{Duration, Instant}; use crossbeam_epoch::{Atomic, Owned}; @@ -14,28 +14,28 @@ use packet::icmp::{icmp, Kind}; use packet::ip::ipv4; use packet::ip::ipv4::packet::IpV4Packet; -use crate::channel::{Route, RouteKey}; use crate::channel::channel::Context; use crate::channel::punch::{NatInfo, NatType}; +use crate::channel::{Route, RouteKey}; use crate::cipher::{Cipher, RsaCipher}; use crate::error::Error; use crate::external_route::AllowExternalRoute; -use crate::handle::{ConnectStatus, CurrentDeviceInfo, PeerDeviceInfo, PeerDeviceStatus}; use crate::handle::handshake_handler::secret_handshake_req; use crate::handle::registration_handler::Register; +use crate::handle::{ConnectStatus, CurrentDeviceInfo, PeerDeviceInfo, PeerDeviceStatus}; use crate::igmp_server::IgmpServer; #[cfg(feature = "ip_proxy")] use crate::ip_proxy::{IpProxyMap, ProxyHandler}; use crate::nat; use crate::nat::NatTest; use crate::proto::message::{DeviceList, PunchInfo, PunchNatType, RegistrationResponse}; -use crate::protocol::{ - control_packet, ip_turn_packet, MAX_TTL, NetPacket, other_turn_packet, Protocol, - service_packet, Version, -}; use crate::protocol::body::ENCRYPTION_RESERVED; use crate::protocol::control_packet::ControlPacket; use crate::protocol::error_packet::InErrorPacket; +use crate::protocol::{ + control_packet, ip_turn_packet, other_turn_packet, service_packet, NetPacket, Protocol, + Version, MAX_TTL, +}; use crate::tun_tap_device::DeviceWriter; #[derive(Clone)] @@ -71,8 +71,7 @@ impl ChannelDataHandler { device_writer: DeviceWriter, connect_status: Arc>, peer_nat_info_map: Arc>>, - #[cfg(feature = "ip_proxy")] - ip_proxy_map: Option, + #[cfg(feature = "ip_proxy")] ip_proxy_map: Option, out_external_route: AllowExternalRoute, cone_sender: Sender<(Ipv4Addr, NatInfo)>, symmetric_sender: Sender<(Ipv4Addr, NatInfo)>, @@ -476,7 +475,7 @@ impl ChannelDataHandler { let nat_map_shared = nat_map.load(Ordering::Acquire, guard); let mut map = unsafe { nat_map_shared.deref().clone() }; map.insert(source, peer_nat_info.clone()); - nat_map.store(Owned::new(map),Ordering::Release); + nat_map.store(Owned::new(map), Ordering::Release); unsafe { guard.defer_destroy(nat_map_shared); } diff --git a/vnt/src/handle/tun_tap/mod.rs b/vnt/src/handle/tun_tap/mod.rs index 1de31fb..ae65da5 100644 --- a/vnt/src/handle/tun_tap/mod.rs +++ b/vnt/src/handle/tun_tap/mod.rs @@ -98,8 +98,7 @@ pub fn base_handle( igmp_server: &Option, current_device: CurrentDeviceInfo, ip_route: &Option, - #[cfg(feature = "ip_proxy")] - proxy_map: &Option, + #[cfg(feature = "ip_proxy")] proxy_map: &Option, client_cipher: &Cipher, server_cipher: &Cipher, ) -> Result<()> { diff --git a/vnt/src/handle/tun_tap/tap_handler.rs b/vnt/src/handle/tun_tap/tap_handler.rs index cbc04d6..54bcd9f 100644 --- a/vnt/src/handle/tun_tap/tap_handler.rs +++ b/vnt/src/handle/tun_tap/tap_handler.rs @@ -30,8 +30,7 @@ pub fn start( igmp_server: Option, current_device: Arc>, ip_route: Option, - #[cfg(feature = "ip_proxy")] - ip_proxy_map: Option, + #[cfg(feature = "ip_proxy")] ip_proxy_map: Option, client_cipher: Cipher, server_cipher: Cipher, parallel: usize, @@ -138,8 +137,7 @@ fn start_simple( igmp_server: Option, current_device: Arc>, ip_route: Option, - #[cfg(feature = "ip_proxy")] - ip_proxy_map: Option, + #[cfg(feature = "ip_proxy")] ip_proxy_map: Option, client_cipher: Cipher, server_cipher: Cipher, ) -> io::Result<()> { @@ -175,8 +173,7 @@ fn handle( device_writer: &DeviceWriter, sender: &ChannelSender, ip_route: &Option, - #[cfg(feature = "ip_proxy")] - proxy_map: &Option, + #[cfg(feature = "ip_proxy")] proxy_map: &Option, client_cipher: &Cipher, server_cipher: &Cipher, ) -> crate::Result<()> { diff --git a/vnt/src/handle/tun_tap/tun_handler.rs b/vnt/src/handle/tun_tap/tun_handler.rs index fd6dd8d..257d07f 100644 --- a/vnt/src/handle/tun_tap/tun_handler.rs +++ b/vnt/src/handle/tun_tap/tun_handler.rs @@ -45,8 +45,7 @@ fn handle( igmp_server: &Option, current_device: CurrentDeviceInfo, ip_route: &Option, - #[cfg(feature = "ip_proxy")] - proxy_map: &Option, + #[cfg(feature = "ip_proxy")] proxy_map: &Option, client_cipher: &Cipher, server_cipher: &Cipher, ) -> Result<()> { @@ -78,8 +77,7 @@ pub fn start( igmp_server: Option, current_device: Arc>, ip_route: Option, - #[cfg(feature = "ip_proxy")] - ip_proxy_map: Option, + #[cfg(feature = "ip_proxy")] ip_proxy_map: Option, client_cipher: Cipher, server_cipher: Cipher, parallel: usize, @@ -190,8 +188,7 @@ fn start_simple( igmp_server: Option, current_device: Arc>, ip_route: Option, - #[cfg(feature = "ip_proxy")] - ip_proxy_map: Option, + #[cfg(feature = "ip_proxy")] ip_proxy_map: Option, client_cipher: Cipher, server_cipher: Cipher, ) -> io::Result<()> { diff --git a/vnt/src/igmp_server/mod.rs b/vnt/src/igmp_server/mod.rs index ef4dd6d..fd32ee8 100644 --- a/vnt/src/igmp_server/mod.rs +++ b/vnt/src/igmp_server/mod.rs @@ -1,7 +1,7 @@ use std::collections::{HashMap, HashSet}; use std::net::Ipv4Addr; -use std::sync::Arc; use std::sync::atomic::Ordering; +use std::sync::Arc; use std::time::{Duration, Instant}; use crossbeam_epoch::{Atomic, Owned}; @@ -56,7 +56,8 @@ pub struct IgmpServer { impl IgmpServer { pub fn new(device_writer: DeviceWriter) -> Self { - let multicast: Arc>>>> = Arc::new(Atomic::new(HashMap::with_capacity(16))); + let multicast: Arc>>>> = + Arc::new(Atomic::new(HashMap::with_capacity(16))); std::thread::spawn(move || { //预留以太网帧头和ip头 let mut buf = [0; 14 + 24 + 12]; @@ -98,7 +99,7 @@ impl IgmpServer { } pub fn load(&self, multicast_addr: &Ipv4Addr) -> Option>> { let guard = &crossbeam_epoch::pin(); - let multicast = unsafe{self.multicast.load(Ordering::Relaxed, guard).deref()}; + let multicast = unsafe { self.multicast.load(Ordering::Relaxed, guard).deref() }; if let Some(entry) = multicast.get(multicast_addr) { Some(entry.clone()) } else { @@ -107,8 +108,8 @@ impl IgmpServer { } pub fn handle(&self, buf: &[u8], source: Ipv4Addr) -> crate::Result<()> { let guard = &crossbeam_epoch::pin(); - let multicast = unsafe{self.multicast.load(Ordering::Relaxed, guard).deref()}; - for (_,v) in multicast.iter() { + let multicast = unsafe { self.multicast.load(Ordering::Relaxed, guard).deref() }; + for (_, v) in multicast.iter() { let mut list = Vec::new(); let mut write_guard = v.write(); for (ip, time) in &write_guard.members { @@ -233,22 +234,20 @@ impl IgmpServer { } Ok(()) } - fn get_multicast(&self,multicast_addr:&Ipv4Addr)->Option>>{ + fn get_multicast(&self, multicast_addr: &Ipv4Addr) -> Option>> { let guard = &crossbeam_epoch::pin(); let multicast = &self.multicast; let table_share = multicast.load(Ordering::Acquire, guard); - unsafe { - table_share.deref().get(multicast_addr).map(|v|v.clone()) - } + unsafe { table_share.deref().get(multicast_addr).map(|v| v.clone()) } } - fn add_multicast(&self,multicast_addr:Ipv4Addr)->Arc>{ + fn add_multicast(&self, multicast_addr: Ipv4Addr) -> Arc> { let guard = &crossbeam_epoch::pin(); let multicast = &self.multicast; let mut table_share = multicast.load(Ordering::Acquire, guard); let value = Arc::new(RwLock::new(Multicast::new())); loop { let mut table = unsafe { table_share.deref().clone() }; - table.insert(multicast_addr,value.clone()); + table.insert(multicast_addr, value.clone()); match self.multicast.compare_exchange( table_share, Owned::new(table.clone()),