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]]
name = "tun-rs"
version = "1.5.0"
version = "2.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53141e64197ff7e758b8152615e50bb4a3b18c970738876e7906d31f242c7d6e"
checksum = "57cff936a4d3a03480e462b2b384dabd5baf64794f10f172965b18e0223d5fc7"
dependencies = [
"bitflags 2.6.0",
"byteorder",
@@ -2102,9 +2102,9 @@ dependencies = [
"nix 0.29.0",
"scopeguard",
"thiserror 2.0.9",
"widestring",
"windows-sys 0.59.0",
"winreg",
"wintun-bindings",
]
[[package]]
@@ -2407,6 +2407,12 @@ dependencies = [
"rustix",
]
[[package]]
name = "widestring"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7219d36b6eac893fa81e84ebe06485e7dcbb616177469b142df14f1f4deb1311"
[[package]]
name = "wildmatch"
version = "1.1.0"
@@ -2594,24 +2600,11 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "winreg"
version = "0.52.0"
version = "0.55.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a277a57398d4bfa075df44f501a17cfdf8542d224f0d36095a2adc7aee4ef0a5"
checksum = "cb5a765337c50e9ec252c2069be9bf91c7df47afb103b642ba3a53bf8101be97"
dependencies = [
"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",
]

View File

@@ -511,7 +511,7 @@ fn print_usage(program: &str, _opts: Options) {
" --use-channel <p2p> {}",
get_description("--use-channel <p2p>", &language)
);
#[cfg(not(feature = "vn-link-model"))]
#[cfg(feature = "integrated_tun")]
println!(
" --nic <tun0> {}",
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
[dependencies]
tun-rs = { version = "1.5.0", optional = true,features = ["experimental"] }
tun-rs = { version = "2", optional = true, features = ["experimental"] }
packet = { path = "./packet" }
bytes = "1.5.0"
log = "0.4.17"
@@ -52,13 +52,13 @@ network-interface = "2.0.0"
futures-util = "0.3.30"
[target.'cfg(target_os = "windows")'.dependencies]
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_IpHelper",
"Win32_Networking_WinSock",
"Win32_System_IO",
"Win32_System_Threading",
"Win32_System_WindowsProgramming",]}
"Win32_System_WindowsProgramming", ] }
[build-dependencies]
protobuf-codegen = "=3.2.0"

View File

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

View File

@@ -13,12 +13,12 @@ use parking_lot::Mutex;
use std::collections::HashMap;
use std::net::Ipv4Addr;
use std::sync::Arc;
use tun_rs::platform::Device;
use tun_rs::SyncDevice;
pub(crate) fn start_simple(
stop_manager: StopManager,
context: &ChannelContext,
device: Arc<Device>,
device: Arc<SyncDevice>,
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
ip_route: ExternalRoute,
#[cfg(feature = "ip_proxy")] ip_proxy_map: Option<IpProxyMap>,
@@ -71,7 +71,7 @@ pub(crate) fn start_simple(
fn start_simple0(
context: &ChannelContext,
device: Arc<Device>,
device: Arc<SyncDevice>,
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
ip_route: ExternalRoute,
#[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::sync::Arc;
use std::{io, thread};
use tun_rs::platform::Device;
use tun_rs::SyncDevice;
use crate::channel::context::ChannelContext;
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, NetPacket, MAX_TTL};
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 {
let mut icmp = IcmpPacket::new(ipv4_packet.payload_mut())?;
if icmp.kind() == Kind::EchoRequest {
@@ -45,7 +45,7 @@ fn icmp(device_writer: &Device, mut ipv4_packet: IpV4Packet<&mut [u8]>) -> anyho
pub fn start(
stop_manager: StopManager,
context: ChannelContext,
device: Arc<Device>,
device: Arc<SyncDevice>,
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
ip_route: ExternalRoute,
#[cfg(feature = "ip_proxy")] ip_proxy_map: Option<IpProxyMap>,
@@ -158,7 +158,7 @@ pub(crate) fn handle(
buf: &mut [u8],
data_len: usize, //数据总长度=12+ip包长度
extend: &mut [u8],
device_writer: &Device,
device_writer: &SyncDevice,
current_device: CurrentDeviceInfo,
ip_route: &ExternalRoute,
#[cfg(feature = "ip_proxy")] proxy_map: &Option<IpProxyMap>,

View File

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

View File

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