支持外置网卡

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::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<AtomicCell<CurrentDeviceInfo>> {
self.current_device.clone()
}
pub fn peer_nat_info(&self, ip: &Ipv4Addr) -> Option<NatInfo> {
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<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) {
self.stop_manager.wait()
}
@@ -441,4 +463,17 @@ impl Vnt {
pub fn 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)]
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<Ipv4Addr>,
#[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<Vec<u16>>,
pub first_latency: bool,
#[cfg(feature = "integrated_tun")]
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
pub device_name: Option<String>,
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<u32>,
tcp: bool,
ip: Option<Ipv4Addr>,
#[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<Vec<u16>>,
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,
packet_loss_rate: Option<f64>,
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,
+9 -7
View File
@@ -191,8 +191,10 @@ impl Into<u8> 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<String>,
//虚拟网卡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<String>,
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 {
+8 -1
View File
@@ -60,8 +60,10 @@ pub struct BaseConfigInfo {
pub server_addr: String,
pub name_servers: Vec<String>,
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<String>,
}
@@ -77,7 +79,10 @@ impl BaseConfigInfo {
server_addr: String,
name_servers: Vec<String>,
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<String>,
) -> 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,
}
+6 -1
View File
@@ -40,6 +40,7 @@ pub struct ClientPacketHandler<Device> {
nat_test: NatTest,
route: AllowExternalRoute,
#[cfg(feature = "ip_proxy")]
#[cfg(feature = "integrated_tun")]
ip_proxy_map: Option<IpProxyMap>,
}
@@ -51,7 +52,9 @@ impl<Device: DeviceWrite> ClientPacketHandler<Device> {
peer_nat_info_map: Arc<RwLock<HashMap<Ipv4Addr, NatInfo>>>,
nat_test: NatTest,
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 {
device,
@@ -60,6 +63,7 @@ impl<Device: DeviceWrite> ClientPacketHandler<Device> {
peer_nat_info_map,
nat_test,
route,
#[cfg(feature = "integrated_tun")]
#[cfg(feature = "ip_proxy")]
ip_proxy_map,
}
@@ -181,6 +185,7 @@ impl<Device: DeviceWrite> ClientPacketHandler<Device> {
_ => {}
}
#[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(());
+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>>>,
external_route: ExternalRoute,
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,
handshake: Handshake,
#[cfg(feature = "integrated_tun")]
@@ -118,6 +120,7 @@ impl<Call: VntCallback, Device: DeviceWrite> RecvDataHandler<Call, Device> {
peer_nat_info_map,
nat_test.clone(),
route,
#[cfg(feature = "integrated_tun")]
#[cfg(feature = "ip_proxy")]
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);
}
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<Call: VntCallback, Device: DeviceWrite> ServerPacketHandler<Call, Device> {
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();