From d1293276ce7c8f33a9f801975e10f17b736c5fa7 Mon Sep 17 00:00:00 2001 From: "sijie.sun" Date: Tue, 6 Aug 2024 22:16:20 +0800 Subject: [PATCH] use tun2 meh/rust-tun has bug on windows and may stuck on large traffic. --- Cargo.lock | 131 +++++++++--------- easytier-gui/src/composables/mobile_vpn.ts | 3 - easytier/Cargo.toml | 4 +- easytier/src/instance/virtual_nic.rs | 52 +++---- .../android/src/main/java/TauriVpnService.kt | 2 +- 5 files changed, 86 insertions(+), 106 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6045475..10f8051 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -713,7 +713,7 @@ dependencies = [ "num-traits", "serde", "wasm-bindgen", - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -2605,12 +2605,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "ioctl-sys" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bd11f3a29434026f5ff98c730b668ba74b1033637b8817940b54d040696133c" - [[package]] name = "ip_network" version = "0.4.1" @@ -2874,7 +2868,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19" dependencies = [ "cfg-if", - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -3248,6 +3242,18 @@ dependencies = [ "memoffset", ] +[[package]] +name = "nix" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46" +dependencies = [ + "bitflags 2.5.0", + "cfg-if", + "cfg_aliases", + "libc", +] + [[package]] name = "no-std-net" version = "0.6.0" @@ -3656,7 +3662,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -6332,19 +6338,22 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" [[package]] name = "tun-easytier" -version = "0.7.1" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "060078376b7da6986f3050f23cdb123b05371f11802c1bdc5c0af919db9292bc" +checksum = "b6af220acb94ba7f897d449ff2520e346d28ad5530ff05c5863d60642b7a08d2" dependencies = [ - "byteorder", "bytes", + "cfg-if", "futures-core", - "ioctl-sys", + "ipnet", "libc", + "libloading 0.8.3", "log", + "nix 0.29.0", "thiserror", "tokio", "tokio-util", + "windows-sys 0.59.0", "wintun", ] @@ -6861,16 +6870,6 @@ dependencies = [ "windows-targets 0.48.5", ] -[[package]] -name = "windows" -version = "0.51.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca229916c5ee38c2f2bc1e9d8f04df975b4bd93f9955dc69fabb5d91270045c9" -dependencies = [ - "windows-core 0.51.1", - "windows-targets 0.48.5", -] - [[package]] name = "windows" version = "0.57.0" @@ -6878,16 +6877,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "12342cb4d8e3b046f3d80effd474a7a02447231330ef77d71daa6fbc40681143" dependencies = [ "windows-core 0.57.0", - "windows-targets 0.52.5", -] - -[[package]] -name = "windows-core" -version = "0.51.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64" -dependencies = [ - "windows-targets 0.48.5", + "windows-targets 0.52.6", ] [[package]] @@ -6896,7 +6886,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" dependencies = [ - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -6908,7 +6898,7 @@ dependencies = [ "windows-implement", "windows-interface", "windows-result", - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -6939,7 +6929,7 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8" dependencies = [ - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -6966,7 +6956,16 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.5", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", ] [[package]] @@ -7001,18 +7000,18 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.5", - "windows_aarch64_msvc 0.52.5", - "windows_i686_gnu 0.52.5", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", - "windows_i686_msvc 0.52.5", - "windows_x86_64_gnu 0.52.5", - "windows_x86_64_gnullvm 0.52.5", - "windows_x86_64_msvc 0.52.5", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] [[package]] @@ -7021,7 +7020,7 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6998aa457c9ba8ff2fb9f13e9d2a930dabcea28f1d0ab94d687d8b3654844515" dependencies = [ - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -7038,9 +7037,9 @@ checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" @@ -7056,9 +7055,9 @@ checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_aarch64_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" @@ -7074,15 +7073,15 @@ checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_gnu" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" [[package]] name = "windows_i686_gnullvm" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" @@ -7098,9 +7097,9 @@ checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_i686_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" @@ -7116,9 +7115,9 @@ checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnu" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" @@ -7134,9 +7133,9 @@ checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" @@ -7152,9 +7151,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "windows_x86_64_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" @@ -7215,15 +7214,15 @@ dependencies = [ [[package]] name = "wintun" -version = "0.3.2" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29b83b0eca06dd125dbcd48a45327c708a6da8aada3d95a3f06db0ce4b17e0d4" +checksum = "b196f9328341b035820c54beebca487823e2e20a5977f284f2af2a0ee8f04400" dependencies = [ "c2rust-bitfields", "libloading 0.8.3", "log", "thiserror", - "windows 0.51.1", + "windows-sys 0.52.0", ] [[package]] diff --git a/easytier-gui/src/composables/mobile_vpn.ts b/easytier-gui/src/composables/mobile_vpn.ts index 3de5eeb..73695c1 100644 --- a/easytier-gui/src/composables/mobile_vpn.ts +++ b/easytier-gui/src/composables/mobile_vpn.ts @@ -159,14 +159,11 @@ async function watchNetworkInstance() { if (subscribe_running) { return } - console.log('network instance change') - subscribe_running = true try { await onNetworkInstanceChange() } catch (_) { } - console.log('network instance change done') subscribe_running = false }) } diff --git a/easytier/Cargo.toml b/easytier/Cargo.toml index fc8778b..5ae1fd9 100644 --- a/easytier/Cargo.toml +++ b/easytier/Cargo.toml @@ -84,9 +84,7 @@ http = { version = "1", default-features = false, features = [ tokio-rustls = { version = "0.26", default-features = false, optional = true } # for tap device -tun = { package = "tun-easytier", version = "0.7.1", features = [ - "async", -], optional = true } +tun = { package = "tun-easytier", version = "1.0.0", features = [ "async" ], optional = true } # for net ns nix = { version = "0.27", features = ["sched", "socket", "ioctl"] } diff --git a/easytier/src/instance/virtual_nic.rs b/easytier/src/instance/virtual_nic.rs index 5836b9c..481ba46 100644 --- a/easytier/src/instance/virtual_nic.rs +++ b/easytier/src/instance/virtual_nic.rs @@ -31,7 +31,7 @@ use tokio::{ task::JoinSet, }; use tokio_util::bytes::Bytes; -use tun::{create_as_async, AsyncDevice, Configuration, Device as _, Layer}; +use tun::{AbstractDevice, AsyncDevice, Configuration, Layer}; use zerocopy::{NativeEndian, NetworkEndian}; pin_project! { @@ -237,8 +237,6 @@ impl AsyncWrite for TunAsyncWrite { } pub struct VirtualNic { - queue_num: usize, - global_ctx: ArcGlobalCtx, ifname: Option, @@ -318,19 +316,13 @@ pub fn checkreg() -> io::Result<()> { impl VirtualNic { pub fn new(global_ctx: ArcGlobalCtx) -> Self { Self { - queue_num: 1, global_ctx, ifname: None, ifcfg: Box::new(IfConfiger {}), } } - pub fn set_queue_num(mut self, queue_num: usize) -> Result { - self.queue_num = queue_num; - Ok(self) - } - - async fn create_tun(&mut self) -> Result { + async fn create_tun(&mut self) -> Result { let mut config = Configuration::default(); config.layer(Layer::L3); @@ -338,14 +330,16 @@ impl VirtualNic { { let dev_name = self.global_ctx.get_flags().dev_name; if !dev_name.is_empty() { - config.name(format!("{}", dev_name)); + config.tun_name(format!("{}", dev_name)); } - config.platform(|config| { - // detect protocol by ourselves for cross platform - config.packet_information(false); - }); } + #[cfg(target_os = "macos")] + config.platform_config(|config| { + // disable packet information so we can process the header by ourselves, see tun2 impl for more details + config.packet_information(false); + }); + #[cfg(target_os = "windows")] { match checkreg() { @@ -353,7 +347,6 @@ impl VirtualNic { Err(e) => tracing::error!("An error occurred: {}", e), } use rand::distributions::Distribution as _; - use std::net::IpAddr; let c = crate::arch::windows::interface_count()?; let mut rng = rand::thread_rng(); let s: String = rand::distributions::Alphanumeric @@ -365,16 +358,12 @@ impl VirtualNic { let dev_name = self.global_ctx.get_flags().dev_name; if !dev_name.is_empty() { - config.name(format!("{}", dev_name)); + config.tun_name(format!("{}", dev_name)); } else { - config.name(format!("et{}_{}", c, s)); + config.tun_name(format!("et{}_{}", c, s)); } - // set a temporary address - config.address(format!("172.0.{}.3", c).parse::().unwrap()); - config.platform(|config| { - config.skip_config(true); - config.guid(None); + config.platform_config(|config| { config.ring_cap(Some(std::cmp::min( config.min_ring_cap() * 32, config.max_ring_cap(), @@ -382,14 +371,10 @@ impl VirtualNic { }); } - if self.queue_num != 1 { - todo!("queue_num != 1") - } - config.queues(self.queue_num); config.up(); let _g = self.global_ctx.net_ns.guard(); - Ok(create_as_async(&config)?) + Ok(tun::create(&config)?) } #[cfg(target_os = "android")] @@ -401,12 +386,11 @@ impl VirtualNic { let mut config = Configuration::default(); config.layer(Layer::L3); config.raw_fd(tun_fd); - config.platform(|config| { - config.no_close_fd_on_drop(true); - }); + config.close_fd_on_drop(false); config.up(); - let dev = create_as_async(&config)?; + let dev = tun::create(&config)?; + let dev = AsyncDevice::new(dev)?; let (a, b) = BiLock::new(dev); let ft = TunnelWrapper::new( TunStream::new(a, false), @@ -424,9 +408,11 @@ impl VirtualNic { pub async fn create_dev(&mut self) -> Result, Error> { let dev = self.create_tun().await?; - let ifname = dev.get_ref().name()?; + let ifname = dev.tun_name()?; self.ifcfg.wait_interface_show(ifname.as_str()).await?; + let dev = AsyncDevice::new(dev)?; + let flags = self.global_ctx.config.get_flags(); let mut mtu_in_config = flags.mtu; if flags.enable_encryption { diff --git a/tauri-plugin-vpnservice/android/src/main/java/TauriVpnService.kt b/tauri-plugin-vpnservice/android/src/main/java/TauriVpnService.kt index b4022da..a68a445 100644 --- a/tauri-plugin-vpnservice/android/src/main/java/TauriVpnService.kt +++ b/tauri-plugin-vpnservice/android/src/main/java/TauriVpnService.kt @@ -88,7 +88,7 @@ class TauriVpnService : VpnService() { builder.addDnsServer(dns) for (route in routes) { - val ipParts = ipv4Addr.split("/") + val ipParts = route.split("/") if (ipParts.size != 2) throw IllegalArgumentException("Invalid IP addr string") builder.addRoute(ipParts[0], ipParts[1].toInt()) }