支持外置网卡

This commit is contained in:
lbl8603
2024-06-15 20:51:00 +08:00
parent dca33a3ec1
commit 0bc7115102
7 changed files with 81 additions and 15 deletions
+36 -1
View File
@@ -10,10 +10,12 @@ use rand::Rng;
use crate::channel::context::ChannelContext; use crate::channel::context::ChannelContext;
use crate::channel::idle::Idle; use crate::channel::idle::Idle;
use crate::channel::punch::{NatInfo, Punch}; use crate::channel::punch::{NatInfo, Punch};
use crate::channel::sender::IpPacketSender;
use crate::channel::{init_channel, init_context, Route, RouteKey}; use crate::channel::{init_channel, init_context, Route, RouteKey};
use crate::cipher::Cipher; use crate::cipher::Cipher;
#[cfg(feature = "server_encrypt")] #[cfg(feature = "server_encrypt")]
use crate::cipher::RsaCipher; use crate::cipher::RsaCipher;
use crate::compression::Compressor;
use crate::core::Config; use crate::core::Config;
use crate::external_route::{AllowExternalRoute, ExternalRoute}; use crate::external_route::{AllowExternalRoute, ExternalRoute};
use crate::handle::handshaker::Handshake; use crate::handle::handshaker::Handshake;
@@ -41,6 +43,9 @@ pub struct Vnt {
down_count_watcher: WatchU64Adder, down_count_watcher: WatchU64Adder,
up_count_watcher: WatchSingleU64Adder, up_count_watcher: WatchSingleU64Adder,
client_secret_hash: Option<[u8; 16]>, client_secret_hash: Option<[u8; 16]>,
compressor: Compressor,
client_cipher: Cipher,
external_route: ExternalRoute,
} }
impl Vnt { impl Vnt {
@@ -99,8 +104,10 @@ impl Vnt {
config.server_address_str.clone(), config.server_address_str.clone(),
config.name_servers.clone(), config.name_servers.clone(),
config.mtu.unwrap_or(1420), config.mtu.unwrap_or(1420),
#[cfg(feature = "integrated_tun")]
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
config.tap, config.tap,
#[cfg(feature = "integrated_tun")]
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
config.device_name.clone(), config.device_name.clone(),
); );
@@ -153,6 +160,7 @@ impl Vnt {
let out_external_route = AllowExternalRoute::new(config.out_ips.clone()); let out_external_route = AllowExternalRoute::new(config.out_ips.clone());
#[cfg(feature = "ip_proxy")] #[cfg(feature = "ip_proxy")]
#[cfg(feature = "integrated_tun")]
let proxy_map = if !config.out_ips.is_empty() && !config.no_proxy { let proxy_map = if !config.out_ips.is_empty() && !config.no_proxy {
Some(crate::ip_proxy::init_proxy( Some(crate::ip_proxy::init_proxy(
context.clone(), context.clone(),
@@ -209,6 +217,7 @@ impl Vnt {
external_route.clone(), external_route.clone(),
out_external_route, out_external_route,
#[cfg(feature = "ip_proxy")] #[cfg(feature = "ip_proxy")]
#[cfg(feature = "integrated_tun")]
proxy_map.clone(), proxy_map.clone(),
down_counter, down_counter,
handshake.clone(), handshake.clone(),
@@ -259,6 +268,7 @@ impl Vnt {
udp_socket_sender, udp_socket_sender,
); );
} }
let client_cipher = client_cipher.clone();
//延迟启动 //延迟启动
scheduler.timeout(Duration::from_secs(3), move |scheduler| { scheduler.timeout(Duration::from_secs(3), move |scheduler| {
start( start(
@@ -278,7 +288,7 @@ impl Vnt {
); );
}); });
} }
let compressor = config.compressor;
Ok(Self { Ok(Self {
stop_manager, stop_manager,
config, config,
@@ -290,6 +300,9 @@ impl Vnt {
down_count_watcher, down_count_watcher,
up_count_watcher, up_count_watcher,
client_secret_hash: config_info.client_secret_hash, 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 { pub fn current_device(&self) -> CurrentDeviceInfo {
self.current_device.load() self.current_device.load()
} }
pub fn current_device_info(&self) -> Arc<AtomicCell<CurrentDeviceInfo>> {
self.current_device.clone()
}
pub fn peer_nat_info(&self, ip: &Ipv4Addr) -> Option<NatInfo> { pub fn peer_nat_info(&self, ip: &Ipv4Addr) -> Option<NatInfo> {
self.peer_nat_info_map.read().get(ip).cloned() self.peer_nat_info_map.read().get(ip).cloned()
} }
@@ -432,6 +448,12 @@ impl Vnt {
let _ = self.context.lock().take(); let _ = self.context.lock().take();
self.stop_manager.stop() self.stop_manager.stop()
} }
pub fn add_stop_listener<F>(&self, name: String, f: F) -> anyhow::Result<crate::util::Worker>
where
F: FnOnce() + Send + 'static,
{
self.stop_manager.add_listener(name, f)
}
pub fn wait(&self) { pub fn wait(&self) {
self.stop_manager.wait() self.stop_manager.wait()
} }
@@ -441,4 +463,17 @@ impl Vnt {
pub fn config(&self) -> &Config { pub fn config(&self) -> &Config {
&self.config &self.config
} }
pub fn ipv4_packet_sender(&self) -> Option<IpPacketSender> {
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
}
}
} }
+15 -3
View File
@@ -14,6 +14,7 @@ mod conn;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Config { pub struct Config {
#[cfg(feature = "integrated_tun")]
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
pub tap: bool, pub tap: bool,
pub token: String, pub token: String,
@@ -30,6 +31,7 @@ pub struct Config {
pub tcp: bool, pub tcp: bool,
pub ip: Option<Ipv4Addr>, pub ip: Option<Ipv4Addr>,
#[cfg(feature = "ip_proxy")] #[cfg(feature = "ip_proxy")]
#[cfg(feature = "integrated_tun")]
pub no_proxy: bool, pub no_proxy: bool,
pub server_encrypt: bool, pub server_encrypt: bool,
pub cipher_model: CipherModel, pub cipher_model: CipherModel,
@@ -37,6 +39,7 @@ pub struct Config {
pub punch_model: PunchModel, pub punch_model: PunchModel,
pub ports: Option<Vec<u16>>, pub ports: Option<Vec<u16>>,
pub first_latency: bool, pub first_latency: bool,
#[cfg(feature = "integrated_tun")]
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
pub device_name: Option<String>, pub device_name: Option<String>,
pub use_channel_type: UseChannelType, pub use_channel_type: UseChannelType,
@@ -51,7 +54,9 @@ pub struct Config {
impl Config { impl Config {
pub fn new( pub fn new(
#[cfg(target_os = "windows")] tap: bool, #[cfg(feature = "integrated_tun")]
#[cfg(target_os = "windows")]
tap: bool,
token: String, token: String,
device_id: String, device_id: String,
name: String, name: String,
@@ -64,14 +69,18 @@ impl Config {
mtu: Option<u32>, mtu: Option<u32>,
tcp: bool, tcp: bool,
ip: Option<Ipv4Addr>, ip: Option<Ipv4Addr>,
#[cfg(feature = "ip_proxy")] no_proxy: bool, #[cfg(feature = "integrated_tun")]
#[cfg(feature = "ip_proxy")]
no_proxy: bool,
server_encrypt: bool, server_encrypt: bool,
cipher_model: CipherModel, cipher_model: CipherModel,
finger: bool, finger: bool,
punch_model: PunchModel, punch_model: PunchModel,
ports: Option<Vec<u16>>, ports: Option<Vec<u16>>,
first_latency: bool, first_latency: bool,
#[cfg(not(target_os = "android"))] device_name: Option<String>, #[cfg(feature = "integrated_tun")]
#[cfg(not(target_os = "android"))]
device_name: Option<String>,
use_channel_type: UseChannelType, use_channel_type: UseChannelType,
packet_loss_rate: Option<f64>, packet_loss_rate: Option<f64>,
packet_delay: u32, packet_delay: u32,
@@ -110,6 +119,7 @@ impl Config {
} }
in_ips.sort_by(|(dest1, _, _), (dest2, _, _)| dest2.cmp(dest1)); in_ips.sort_by(|(dest1, _, _), (dest2, _, _)| dest2.cmp(dest1));
Ok(Self { Ok(Self {
#[cfg(feature = "integrated_tun")]
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
tap, tap,
token, token,
@@ -126,6 +136,7 @@ impl Config {
tcp, tcp,
ip, ip,
#[cfg(feature = "ip_proxy")] #[cfg(feature = "ip_proxy")]
#[cfg(feature = "integrated_tun")]
no_proxy, no_proxy,
server_encrypt, server_encrypt,
cipher_model, cipher_model,
@@ -133,6 +144,7 @@ impl Config {
punch_model, punch_model,
ports, ports,
first_latency, first_latency,
#[cfg(feature = "integrated_tun")]
#[cfg(not(target_os = "android"))] #[cfg(not(target_os = "android"))]
device_name, device_name,
use_channel_type, use_channel_type,
+9 -7
View File
@@ -191,8 +191,10 @@ impl Into<u8> for ErrorType {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct DeviceConfig { pub struct DeviceConfig {
#[cfg(feature = "integrated_tun")]
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
pub tap: bool, pub tap: bool,
#[cfg(feature = "integrated_tun")]
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
pub device_name: Option<String>, pub device_name: Option<String>,
//虚拟网卡mtu值 //虚拟网卡mtu值
@@ -211,7 +213,10 @@ pub struct DeviceConfig {
impl DeviceConfig { impl DeviceConfig {
pub fn new( 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"))] #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
device_name: Option<String>, device_name: Option<String>,
mtu: u32, mtu: u32,
@@ -222,8 +227,10 @@ impl DeviceConfig {
external_route: Vec<(Ipv4Addr, Ipv4Addr)>, external_route: Vec<(Ipv4Addr, Ipv4Addr)>,
) -> Self { ) -> Self {
Self { Self {
#[cfg(feature = "integrated_tun")]
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
tap, tap,
#[cfg(feature = "integrated_tun")]
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
device_name, device_name,
mtu, mtu,
@@ -297,12 +304,7 @@ pub trait VntCallback: Clone + Send + Sync + 'static {
true true
} }
#[cfg(not(feature = "integrated_tun"))] #[cfg(not(feature = "integrated_tun"))]
fn create_device( fn create_device(&self, _info: DeviceConfig) {}
&self,
_channel_sender: crate::channel::sender::ChannelSender,
_info: DeviceConfig,
) {
}
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
#[cfg(feature = "integrated_tun")] #[cfg(feature = "integrated_tun")]
fn generate_tun(&self, _info: DeviceConfig) -> usize { fn generate_tun(&self, _info: DeviceConfig) -> usize {
+8 -1
View File
@@ -60,8 +60,10 @@ pub struct BaseConfigInfo {
pub server_addr: String, pub server_addr: String,
pub name_servers: Vec<String>, pub name_servers: Vec<String>,
pub mtu: u32, pub mtu: u32,
#[cfg(feature = "integrated_tun")]
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
pub tap: bool, pub tap: bool,
#[cfg(feature = "integrated_tun")]
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
pub device_name: Option<String>, pub device_name: Option<String>,
} }
@@ -77,7 +79,10 @@ impl BaseConfigInfo {
server_addr: String, server_addr: String,
name_servers: Vec<String>, name_servers: Vec<String>,
mtu: u32, 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"))] #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
device_name: Option<String>, device_name: Option<String>,
) -> Self { ) -> Self {
@@ -91,8 +96,10 @@ impl BaseConfigInfo {
server_addr, server_addr,
name_servers, name_servers,
mtu, mtu,
#[cfg(feature = "integrated_tun")]
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
tap, tap,
#[cfg(feature = "integrated_tun")]
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
device_name, device_name,
} }
+6 -1
View File
@@ -40,6 +40,7 @@ pub struct ClientPacketHandler<Device> {
nat_test: NatTest, nat_test: NatTest,
route: AllowExternalRoute, route: AllowExternalRoute,
#[cfg(feature = "ip_proxy")] #[cfg(feature = "ip_proxy")]
#[cfg(feature = "integrated_tun")]
ip_proxy_map: Option<IpProxyMap>, ip_proxy_map: Option<IpProxyMap>,
} }
@@ -51,7 +52,9 @@ impl<Device: DeviceWrite> ClientPacketHandler<Device> {
peer_nat_info_map: Arc<RwLock<HashMap<Ipv4Addr, NatInfo>>>, peer_nat_info_map: Arc<RwLock<HashMap<Ipv4Addr, NatInfo>>>,
nat_test: NatTest, nat_test: NatTest,
route: AllowExternalRoute, route: AllowExternalRoute,
#[cfg(feature = "ip_proxy")] ip_proxy_map: Option<IpProxyMap>, #[cfg(feature = "integrated_tun")]
#[cfg(feature = "ip_proxy")]
ip_proxy_map: Option<IpProxyMap>,
) -> Self { ) -> Self {
Self { Self {
device, device,
@@ -60,6 +63,7 @@ impl<Device: DeviceWrite> ClientPacketHandler<Device> {
peer_nat_info_map, peer_nat_info_map,
nat_test, nat_test,
route, route,
#[cfg(feature = "integrated_tun")]
#[cfg(feature = "ip_proxy")] #[cfg(feature = "ip_proxy")]
ip_proxy_map, ip_proxy_map,
} }
@@ -181,6 +185,7 @@ impl<Device: DeviceWrite> ClientPacketHandler<Device> {
_ => {} _ => {}
} }
#[cfg(feature = "ip_proxy")] #[cfg(feature = "ip_proxy")]
#[cfg(feature = "integrated_tun")]
if let Some(ip_proxy_map) = &self.ip_proxy_map { if let Some(ip_proxy_map) = &self.ip_proxy_map {
if ip_proxy_map.recv_handle(&mut ipv4, source, destination)? { if ip_proxy_map.recv_handle(&mut ipv4, source, destination)? {
return Ok(()); return Ok(());
+4 -1
View File
@@ -90,7 +90,9 @@ impl<Call: VntCallback, Device: DeviceWrite> RecvDataHandler<Call, Device> {
peer_nat_info_map: Arc<RwLock<HashMap<Ipv4Addr, NatInfo>>>, peer_nat_info_map: Arc<RwLock<HashMap<Ipv4Addr, NatInfo>>>,
external_route: ExternalRoute, external_route: ExternalRoute,
route: AllowExternalRoute, route: AllowExternalRoute,
#[cfg(feature = "ip_proxy")] ip_proxy_map: Option<IpProxyMap>, #[cfg(feature = "integrated_tun")]
#[cfg(feature = "ip_proxy")]
ip_proxy_map: Option<IpProxyMap>,
counter: U64Adder, counter: U64Adder,
handshake: Handshake, handshake: Handshake,
#[cfg(feature = "integrated_tun")] #[cfg(feature = "integrated_tun")]
@@ -118,6 +120,7 @@ impl<Call: VntCallback, Device: DeviceWrite> RecvDataHandler<Call, Device> {
peer_nat_info_map, peer_nat_info_map,
nat_test.clone(), nat_test.clone(),
route, route,
#[cfg(feature = "integrated_tun")]
#[cfg(feature = "ip_proxy")] #[cfg(feature = "ip_proxy")]
ip_proxy_map, ip_proxy_map,
); );
+3 -1
View File
@@ -306,8 +306,10 @@ impl<Call: VntCallback, Device: DeviceWrite> ServerPacketHandler<Call, Device> {
log::info!("ip发生变化,old:{:?},response={:?}", old, response); log::info!("ip发生变化,old:{:?},response={:?}", old, response);
} }
let device_config = crate::handle::callback::DeviceConfig::new( let device_config = crate::handle::callback::DeviceConfig::new(
#[cfg(feature = "integrated_tun")]
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
self.config_info.tap, self.config_info.tap,
#[cfg(feature = "integrated_tun")]
#[cfg(any( #[cfg(any(
target_os = "windows", target_os = "windows",
target_os = "linux", target_os = "linux",
@@ -322,7 +324,7 @@ impl<Call: VntCallback, Device: DeviceWrite> ServerPacketHandler<Call, Device> {
self.external_route.to_route(), self.external_route.to_route(),
); );
#[cfg(not(feature = "integrated_tun"))] #[cfg(not(feature = "integrated_tun"))]
self.callback.create_device(context.sender(), device_config); self.callback.create_device(device_config);
#[cfg(feature = "integrated_tun")] #[cfg(feature = "integrated_tun")]
{ {
self.tun_device_helper.stop(); self.tun_device_helper.stop();