Use the new version of tun-rs

This commit is contained in:
lbl
2025-02-13 17:02:06 +08:00
parent 274f4df43a
commit 563aca39aa
8 changed files with 37 additions and 51 deletions

29
Cargo.lock generated
View File

@@ -2083,9 +2083,9 @@ dependencies = [
[[package]] [[package]]
name = "tun-rs" name = "tun-rs"
version = "1.5.0" version = "2.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53141e64197ff7e758b8152615e50bb4a3b18c970738876e7906d31f242c7d6e" checksum = "57cff936a4d3a03480e462b2b384dabd5baf64794f10f172965b18e0223d5fc7"
dependencies = [ dependencies = [
"bitflags 2.6.0", "bitflags 2.6.0",
"byteorder", "byteorder",
@@ -2102,9 +2102,9 @@ dependencies = [
"nix 0.29.0", "nix 0.29.0",
"scopeguard", "scopeguard",
"thiserror 2.0.9", "thiserror 2.0.9",
"widestring",
"windows-sys 0.59.0", "windows-sys 0.59.0",
"winreg", "winreg",
"wintun-bindings",
] ]
[[package]] [[package]]
@@ -2407,6 +2407,12 @@ dependencies = [
"rustix", "rustix",
] ]
[[package]]
name = "widestring"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7219d36b6eac893fa81e84ebe06485e7dcbb616177469b142df14f1f4deb1311"
[[package]] [[package]]
name = "wildmatch" name = "wildmatch"
version = "1.1.0" version = "1.1.0"
@@ -2594,24 +2600,11 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]] [[package]]
name = "winreg" name = "winreg"
version = "0.52.0" version = "0.55.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a277a57398d4bfa075df44f501a17cfdf8542d224f0d36095a2adc7aee4ef0a5" checksum = "cb5a765337c50e9ec252c2069be9bf91c7df47afb103b642ba3a53bf8101be97"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"windows-sys 0.48.0",
]
[[package]]
name = "wintun-bindings"
version = "0.7.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e35d3911efde5ee25586385204127ff6a3f251477dcdd3b222775aaa4d95977"
dependencies = [
"c2rust-bitfields",
"libloading",
"log",
"thiserror 2.0.9",
"windows-sys 0.59.0", "windows-sys 0.59.0",
] ]

View File

@@ -511,7 +511,7 @@ fn print_usage(program: &str, _opts: Options) {
" --use-channel <p2p> {}", " --use-channel <p2p> {}",
get_description("--use-channel <p2p>", &language) get_description("--use-channel <p2p>", &language)
); );
#[cfg(not(feature = "vn-link-model"))] #[cfg(feature = "integrated_tun")]
println!( println!(
" --nic <tun0> {}", " --nic <tun0> {}",
get_description("--nic <tun0>", &language) get_description("--nic <tun0>", &language)

View File

@@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
tun-rs = { version = "1.5.0", optional = true,features = ["experimental"] } tun-rs = { version = "2", optional = true, features = ["experimental"] }
packet = { path = "./packet" } packet = { path = "./packet" }
bytes = "1.5.0" bytes = "1.5.0"
log = "0.4.17" log = "0.4.17"
@@ -52,13 +52,13 @@ network-interface = "2.0.0"
futures-util = "0.3.30" futures-util = "0.3.30"
[target.'cfg(target_os = "windows")'.dependencies] [target.'cfg(target_os = "windows")'.dependencies]
libloading = "0.8.0" libloading = "0.8.0"
windows-sys = {version = "0.59.0",features = [ "Win32_Foundation", windows-sys = { version = "0.59.0", features = ["Win32_Foundation",
"Win32_NetworkManagement", "Win32_NetworkManagement",
"Win32_NetworkManagement_IpHelper", "Win32_NetworkManagement_IpHelper",
"Win32_Networking_WinSock", "Win32_Networking_WinSock",
"Win32_System_IO", "Win32_System_IO",
"Win32_System_Threading", "Win32_System_Threading",
"Win32_System_WindowsProgramming",]} "Win32_System_WindowsProgramming", ] }
[build-dependencies] [build-dependencies]
protobuf-codegen = "=3.2.0" protobuf-codegen = "=3.2.0"

View File

@@ -12,8 +12,6 @@ use packet::ip::ipv4;
use packet::ip::ipv4::packet::IpV4Packet; use packet::ip::ipv4::packet::IpV4Packet;
use parking_lot::Mutex; use parking_lot::Mutex;
use protobuf::Message; use protobuf::Message;
#[cfg(feature = "integrated_tun")]
use tun_rs::AbstractDevice;
use crate::channel::context::ChannelContext; use crate::channel::context::ChannelContext;
use crate::channel::{Route, RouteKey}; use crate::channel::{Route, RouteKey};

View File

@@ -13,12 +13,12 @@ use parking_lot::Mutex;
use std::collections::HashMap; use std::collections::HashMap;
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
use std::sync::Arc; use std::sync::Arc;
use tun_rs::platform::Device; use tun_rs::SyncDevice;
pub(crate) fn start_simple( pub(crate) fn start_simple(
stop_manager: StopManager, stop_manager: StopManager,
context: &ChannelContext, context: &ChannelContext,
device: Arc<Device>, device: Arc<SyncDevice>,
current_device: Arc<AtomicCell<CurrentDeviceInfo>>, current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
ip_route: ExternalRoute, ip_route: ExternalRoute,
#[cfg(feature = "ip_proxy")] ip_proxy_map: Option<IpProxyMap>, #[cfg(feature = "ip_proxy")] ip_proxy_map: Option<IpProxyMap>,
@@ -71,7 +71,7 @@ pub(crate) fn start_simple(
fn start_simple0( fn start_simple0(
context: &ChannelContext, context: &ChannelContext,
device: Arc<Device>, device: Arc<SyncDevice>,
current_device: Arc<AtomicCell<CurrentDeviceInfo>>, current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
ip_route: ExternalRoute, ip_route: ExternalRoute,
#[cfg(feature = "ip_proxy")] ip_proxy_map: Option<IpProxyMap>, #[cfg(feature = "ip_proxy")] ip_proxy_map: Option<IpProxyMap>,

View File

@@ -8,7 +8,7 @@ use std::collections::HashMap;
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
use std::sync::Arc; use std::sync::Arc;
use std::{io, thread}; use std::{io, thread};
use tun_rs::platform::Device; use tun_rs::SyncDevice;
use crate::channel::context::ChannelContext; use crate::channel::context::ChannelContext;
use crate::channel::sender::{send_to_wg, send_to_wg_broadcast}; use crate::channel::sender::{send_to_wg, send_to_wg_broadcast};
@@ -26,7 +26,7 @@ use crate::protocol::body::ENCRYPTION_RESERVED;
use crate::protocol::ip_turn_packet::BroadcastPacket; use crate::protocol::ip_turn_packet::BroadcastPacket;
use crate::protocol::{ip_turn_packet, NetPacket, MAX_TTL}; use crate::protocol::{ip_turn_packet, NetPacket, MAX_TTL};
use crate::util::StopManager; use crate::util::StopManager;
fn icmp(device_writer: &Device, mut ipv4_packet: IpV4Packet<&mut [u8]>) -> anyhow::Result<()> { fn icmp(device_writer: &SyncDevice, mut ipv4_packet: IpV4Packet<&mut [u8]>) -> anyhow::Result<()> {
if ipv4_packet.protocol() == Protocol::Icmp { if ipv4_packet.protocol() == Protocol::Icmp {
let mut icmp = IcmpPacket::new(ipv4_packet.payload_mut())?; let mut icmp = IcmpPacket::new(ipv4_packet.payload_mut())?;
if icmp.kind() == Kind::EchoRequest { if icmp.kind() == Kind::EchoRequest {
@@ -45,7 +45,7 @@ fn icmp(device_writer: &Device, mut ipv4_packet: IpV4Packet<&mut [u8]>) -> anyho
pub fn start( pub fn start(
stop_manager: StopManager, stop_manager: StopManager,
context: ChannelContext, context: ChannelContext,
device: Arc<Device>, device: Arc<SyncDevice>,
current_device: Arc<AtomicCell<CurrentDeviceInfo>>, current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
ip_route: ExternalRoute, ip_route: ExternalRoute,
#[cfg(feature = "ip_proxy")] ip_proxy_map: Option<IpProxyMap>, #[cfg(feature = "ip_proxy")] ip_proxy_map: Option<IpProxyMap>,
@@ -158,7 +158,7 @@ pub(crate) fn handle(
buf: &mut [u8], buf: &mut [u8],
data_len: usize, //数据总长度=12+ip包长度 data_len: usize, //数据总长度=12+ip包长度
extend: &mut [u8], extend: &mut [u8],
device_writer: &Device, device_writer: &SyncDevice,
current_device: CurrentDeviceInfo, current_device: CurrentDeviceInfo,
ip_route: &ExternalRoute, ip_route: &ExternalRoute,
#[cfg(feature = "ip_proxy")] proxy_map: &Option<IpProxyMap>, #[cfg(feature = "ip_proxy")] proxy_map: &Option<IpProxyMap>,

View File

@@ -1,8 +1,7 @@
use std::io; use std::io;
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
use std::sync::Arc; use std::sync::Arc;
use tun_rs::platform::Device; use tun_rs::SyncDevice;
use tun_rs::AbstractDevice;
use crate::{DeviceConfig, ErrorInfo, ErrorType, VntCallback}; use crate::{DeviceConfig, ErrorInfo, ErrorType, VntCallback};
@@ -12,7 +11,7 @@ const DEFAULT_TUN_NAME: &str = "vnt-tun";
pub fn create_device<Call: VntCallback>( pub fn create_device<Call: VntCallback>(
config: DeviceConfig, config: DeviceConfig,
call: &Call, call: &Call,
) -> Result<Arc<Device>, ErrorInfo> { ) -> Result<Arc<SyncDevice>, ErrorInfo> {
let device = match create_device0(&config) { let device = match create_device0(&config) {
Ok(device) => device, Ok(device) => device,
Err(e) => { Err(e) => {
@@ -53,28 +52,25 @@ pub fn create_device<Call: VntCallback>(
Ok(device) Ok(device)
} }
fn create_device0(config: &DeviceConfig) -> io::Result<Arc<Device>> { fn create_device0(config: &DeviceConfig) -> io::Result<Arc<SyncDevice>> {
let mut tun_config = tun_rs::Configuration::default(); let mut tun_builder = tun_rs::DeviceBuilder::default();
let netmask = u32::from_be_bytes(config.virtual_netmask.octets()); tun_builder = tun_builder.ipv4(config.virtual_ip, config.virtual_netmask, None);
tun_config
.address_with_prefix(config.virtual_ip, netmask.count_ones() as u8)
.up();
match &config.device_name { match &config.device_name {
None => { None => {
#[cfg(any(target_os = "windows", target_os = "linux"))] #[cfg(any(target_os = "windows", target_os = "linux"))]
tun_config.name(DEFAULT_TUN_NAME); {
tun_builder = tun_builder.name(DEFAULT_TUN_NAME);
}
} }
Some(name) => { Some(name) => {
tun_config.name(name); tun_builder = tun_builder.name(name);
} }
} }
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
{ {
tun_config.platform_config(|v| { tun_builder = tun_builder.metric(0).ring_capacity(4 * 1024 * 1024);
v.metric(0).ring_capacity(4 * 1024 * 1024);
});
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
@@ -88,8 +84,7 @@ fn create_device0(config: &DeviceConfig) -> io::Result<Arc<Device>> {
} }
} }
tun_config.mtu(config.mtu as u16); let device = tun_builder.mtu(config.mtu as u16).build_sync()?;
let device = tun_rs::create(&tun_config)?;
Ok(Arc::new(device)) Ok(Arc::new(device))
} }

View File

@@ -15,16 +15,16 @@ use crate::tun_tap_device::vnt_device::DeviceWrite;
use crate::util::StopManager; use crate::util::StopManager;
use crossbeam_utils::atomic::AtomicCell; use crossbeam_utils::atomic::AtomicCell;
use parking_lot::Mutex; use parking_lot::Mutex;
use tun_rs::platform::Device; use tun_rs::SyncDevice;
#[repr(transparent)] #[repr(transparent)]
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct DeviceAdapter { pub struct DeviceAdapter {
tun: Arc<Mutex<Option<Arc<Device>>>>, tun: Arc<Mutex<Option<Arc<SyncDevice>>>>,
} }
impl DeviceAdapter { impl DeviceAdapter {
pub fn insert(&self, device: Arc<Device>) { pub fn insert(&self, device: Arc<SyncDevice>) {
let r = self.tun.lock().replace(device); let r = self.tun.lock().replace(device);
assert!(r.is_none()); assert!(r.is_none());
} }
@@ -116,7 +116,7 @@ impl TunDeviceHelper {
} }
} }
/// 要保证先stop 再start /// 要保证先stop 再start
pub fn start(&self, device: Arc<Device>, allow_wire_guard: bool) -> io::Result<()> { pub fn start(&self, device: Arc<SyncDevice>, allow_wire_guard: bool) -> io::Result<()> {
self.device_adapter.insert(device.clone()); self.device_adapter.insert(device.clone());
let device_stop = DeviceStop::default(); let device_stop = DeviceStop::default();
let s = self.device_stop.lock().replace(device_stop.clone()); let s = self.device_stop.lock().replace(device_stop.clone());