From 33ff338fd23c02289aada79e8e142141790419cc Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 30 Jun 2026 11:50:54 +0800 Subject: [PATCH] windows changed to tun-rs for better performance --- Cargo.lock | 581 +++++++++++++++++++++++++++++++------- Cargo.toml | 1 + src/network/async_main.rs | 64 ++--- src/network/tun_win.rs | 160 ++++++++++- 4 files changed, 658 insertions(+), 148 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 926c77e..6b992b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -76,22 +76,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" dependencies = [ "anstyle", - "anstyle-parse 0.2.7", - "anstyle-query", - "anstyle-wincon", - "colorchoice", - "is_terminal_polyfill", - "utf8parse", -] - -[[package]] -name = "anstream" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" -dependencies = [ - "anstyle", - "anstyle-parse 1.0.0", + "anstyle-parse", "anstyle-query", "anstyle-wincon", "colorchoice", @@ -114,15 +99,6 @@ dependencies = [ "utf8parse", ] -[[package]] -name = "anstyle-parse" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" -dependencies = [ - "utf8parse", -] - [[package]] name = "anstyle-query" version = "1.1.5" @@ -170,6 +146,24 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" +[[package]] +name = "async-channel" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "924ed96dd52d1b75e9c1a3e6275715fd320f5f9439fb5a4a11fa51f4221158d2" +dependencies = [ + "concurrent-queue", + "event-listener-strategy", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-task" +version = "4.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de" + [[package]] name = "atoi" version = "2.0.0" @@ -275,6 +269,19 @@ dependencies = [ "generic-array", ] +[[package]] +name = "blocking" +version = "1.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e83f8d02be6967315521be875afa792a316e28d57b5a2d401897e2a7921b7f21" +dependencies = [ + "async-channel", + "async-task", + "futures-io", + "futures-lite", + "piper", +] + [[package]] name = "bumpalo" version = "3.20.2" @@ -299,7 +306,16 @@ version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b43c3f07ab0ef604fa6f595aa46ec2f8a22172c975e186f6f5bf9829a3b72c41" dependencies = [ - "c2rust-bitfields-derive", + "c2rust-bitfields-derive 0.18.0", +] + +[[package]] +name = "c2rust-bitfields" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91b9a85391b09c224053f3c45344f0639822e641f9d2b4f351bb3152600742d" +dependencies = [ + "c2rust-bitfields-derive 0.22.1", ] [[package]] @@ -313,17 +329,29 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "c2rust-bitfields-derive" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "153187c06ed005d5cb75ed9f0a2f274836f54f6b2efdc1f45136e728764875f9" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.106", + "unicode-ident", +] + [[package]] name = "cargo-deb" version = "3.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab061e8ad1ccc158be6465aa4152d5fff95b834509b9ffaafa42e1ae38095165" dependencies = [ - "anstream 0.6.21", + "anstream", "anstyle", "ar", "cargo_toml", - "clap 4.6.0", + "clap 4.5.60", "elf", "env_logger", "glob", @@ -424,7 +452,7 @@ dependencies = [ "js-sys", "num-traits", "wasm-bindgen", - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -455,9 +483,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.6.0" +version = "4.5.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b193af5b67834b676abd72466a96c1024e6a6ad978a1f484bd90b85c94041351" +checksum = "2797f34da339ce31042b27d23607e051786132987f595b02ba4f6a6dffb7030a" dependencies = [ "clap_builder", "clap_derive", @@ -465,11 +493,11 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.6.0" +version = "4.5.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f" +checksum = "24a241312cea5059b13574bb9b3861cabf758b879c15190b37b6d6fd63ab6876" dependencies = [ - "anstream 1.0.0", + "anstream", "anstyle", "clap_lex", "strsim 0.11.1", @@ -477,14 +505,14 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.6.0" +version = "4.5.55" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1110bd8a634a1ab8cb04345d8d878267d57c3cf1b38d91b71af6686408bbca6a" +checksum = "a92793da1a46a5f2a02a6f4c46c6496b28c43638adea8306fcb0caa1634f24e5" dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -518,6 +546,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "concurrent-queue" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "const-oid" version = "0.9.6" @@ -667,7 +704,7 @@ dependencies = [ "proc-macro2", "quote", "strsim 0.11.1", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -678,7 +715,7 @@ checksum = "fc34b93ccb385b40dc71c6fceac4b2ad23662c7eeb248cf10d529b7e055b6ead" dependencies = [ "darling_core", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -717,8 +754,8 @@ dependencies = [ "dlopen2", "libc", "memalloc", - "netlink-packet-core", - "netlink-packet-route", + "netlink-packet-core 0.7.0", + "netlink-packet-route 0.17.1", "netlink-sys", "once_cell", "system-configuration", @@ -763,7 +800,7 @@ dependencies = [ "darling", "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -773,7 +810,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab63b0e2bf4d5928aff72e83a7dace85d7bba5fe12dcc3c5a572d78caffd3f3c" dependencies = [ "derive_builder_core", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -796,7 +833,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -849,6 +886,15 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55dd888a213fc57e957abf2aa305ee3e8a28dbe05687a251f33b637cd46b0070" +[[package]] +name = "encoding_rs" +version = "0.8.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3" +dependencies = [ + "cfg-if", +] + [[package]] name = "env_filter" version = "1.0.0" @@ -865,7 +911,7 @@ version = "0.11.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b2daee4ea451f429a58296525ddf28b45a3b64f1acf6587e2067437bb11e218d" dependencies = [ - "anstream 0.6.21", + "anstream", "anstyle", "env_filter", "log", @@ -913,6 +959,27 @@ version = "2.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" +[[package]] +name = "event-listener" +version = "5.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13b66accf52311f30a0db42147dadea9850cb48cd070028831ae5f5d4b856ab" +dependencies = [ + "concurrent-queue", + "parking", + "pin-project-lite", +] + +[[package]] +name = "event-listener-strategy" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8be9f3dfaaffdae2972880079a491a1a8bb7cbed0b8dd7a347f668b4150a3b93" +dependencies = [ + "event-listener 5.4.1", + "pin-project-lite", +] + [[package]] name = "fastbloom" version = "0.14.1" @@ -930,6 +997,9 @@ name = "fastrand" version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" +dependencies = [ + "getrandom 0.2.17", +] [[package]] name = "filetime" @@ -965,6 +1035,18 @@ dependencies = [ "spin", ] +[[package]] +name = "flume" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e139bc46ca777eb5efaf62df0ab8cc5fd400866427e56c68b22e414e53bd3be" +dependencies = [ + "fastrand", + "futures-core", + "futures-sink", + "spin", +] + [[package]] name = "fnv" version = "1.0.7" @@ -1051,6 +1133,16 @@ version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cecba35d7ad927e23624b22ad55235f2239cfa44fd10428eecbeba6d6a717718" +[[package]] +name = "futures-lite" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f78e10609fe0e0b3f4157ffab1876319b5b0db102a2c60dc4626306dc46b44ad" +dependencies = [ + "futures-core", + "pin-project-lite", +] + [[package]] name = "futures-macro" version = "0.3.32" @@ -1059,7 +1151,7 @@ checksum = "e835b70203e41293343137df5c0664546da5745f82ec9b84d40be8336958447b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -1101,6 +1193,17 @@ dependencies = [ "version_check", ] +[[package]] +name = "getifaddrs" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a542e1b7ac1f3d62c5777d430d66eca9cb59e813c46b86e29fa9ce94ff9a4810" +dependencies = [ + "bitflags 2.11.0", + "libc", + "windows-sys 0.61.2", +] + [[package]] name = "getrandom" version = "0.2.17" @@ -1150,7 +1253,7 @@ dependencies = [ "proc-macro-error2", "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -1267,7 +1370,7 @@ checksum = "617aaa3557aef3810a6369d0a99fac8a080891b68bd9f9812a1eeda0c0730cbd" dependencies = [ "cfg-if", "libc", - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -1635,9 +1738,9 @@ checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" [[package]] name = "libc" -version = "0.2.183" +version = "0.2.186" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5b646652bf6661599e1da8901b3b9522896f01e736bad5f723fe7a3a27f899d" +checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" [[package]] name = "libloading" @@ -1646,7 +1749,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55" dependencies = [ "cfg-if", - "windows-link", + "windows-link 0.2.1", +] + +[[package]] +name = "libloading" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "754ca22de805bb5744484a5b151a9e1a8e837d5dc232c2d7d8c2e3492edc8b60" +dependencies = [ + "cfg-if", + "windows-link 0.2.1", ] [[package]] @@ -1755,6 +1868,15 @@ version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" +[[package]] +name = "memoffset" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +dependencies = [ + "autocfg", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -1813,7 +1935,28 @@ dependencies = [ "proc-macro2", "quote", "serde", - "syn 2.0.117", + "syn 2.0.106", +] + +[[package]] +name = "netconfig-rs" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "733ad7a1352f0748a620368f2ea5cd94f0e449c5108fbac86ace31df1601bf6f" +dependencies = [ + "cfg-if", + "core-foundation 0.10.1", + "ipnet", + "libc", + "netlink-packet-core 0.8.1", + "netlink-packet-route 0.25.1", + "netlink-sys", + "nix 0.30.1", + "scopeguard", + "system-configuration-sys 0.6.0", + "thiserror 2.0.18", + "widestring", + "windows 0.61.3", ] [[package]] @@ -1827,6 +1970,15 @@ dependencies = [ "netlink-packet-utils", ] +[[package]] +name = "netlink-packet-core" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3463cbb78394cb0141e2c926b93fc2197e473394b761986eca3b9da2c63ae0f4" +dependencies = [ + "paste", +] + [[package]] name = "netlink-packet-route" version = "0.17.1" @@ -1837,10 +1989,34 @@ dependencies = [ "bitflags 1.3.2", "byteorder", "libc", - "netlink-packet-core", + "netlink-packet-core 0.7.0", "netlink-packet-utils", ] +[[package]] +name = "netlink-packet-route" +version = "0.25.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ec2f5b6839be2a19d7fa5aab5bc444380f6311c2b693551cb80f45caaa7b5ef" +dependencies = [ + "bitflags 2.11.0", + "libc", + "log", + "netlink-packet-core 0.8.1", +] + +[[package]] +name = "netlink-packet-route" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ce3636fa715e988114552619582b530481fd5ef176a1e5c1bf024077c2c9445" +dependencies = [ + "bitflags 2.11.0", + "libc", + "log", + "netlink-packet-core 0.8.1", +] + [[package]] name = "netlink-packet-utils" version = "0.5.2" @@ -1864,6 +2040,32 @@ dependencies = [ "log", ] +[[package]] +name = "nix" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6" +dependencies = [ + "bitflags 2.11.0", + "cfg-if", + "cfg_aliases", + "libc", + "memoffset", +] + +[[package]] +name = "nix" +version = "0.31.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf20d2fde8ff38632c426f1165ed7436270b44f199fc55284c38276f9db47c3d" +dependencies = [ + "bitflags 2.11.0", + "cfg-if", + "cfg_aliases", + "libc", + "memoffset", +] + [[package]] name = "nom" version = "7.1.3" @@ -1963,7 +2165,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -2009,6 +2211,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe" +[[package]] +name = "parking" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" + [[package]] name = "parking_lot" version = "0.12.5" @@ -2029,7 +2237,7 @@ dependencies = [ "libc", "redox_syscall 0.5.18", "smallvec", - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -2075,6 +2283,17 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "piper" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c835479a4443ded371d6c535cbfd8d31ad92c5d23ae9770a61bc155e4992a3c1" +dependencies = [ + "atomic-waker", + "fastrand", + "futures-io", +] + [[package]] name = "pkcs1" version = "0.7.5" @@ -2150,7 +2369,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" dependencies = [ "proc-macro2", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -2205,14 +2424,14 @@ dependencies = [ "proc-macro-error-attr2", "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] name = "proc-macro2" -version = "1.0.106" +version = "1.0.103" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8" dependencies = [ "unicode-ident", ] @@ -2244,7 +2463,7 @@ dependencies = [ "prost", "prost-types", "regex", - "syn 2.0.117", + "syn 2.0.106", "tempfile", ] @@ -2258,7 +2477,7 @@ dependencies = [ "itertools 0.12.1", "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -2279,7 +2498,7 @@ dependencies = [ "bytes", "cargo-deb", "chacha20poly1305", - "clap 4.6.0", + "clap 4.5.60", "crc", "crc32fast", "daemonize", @@ -2318,6 +2537,7 @@ dependencies = [ "tokio", "tokio-util", "tracing", + "tun-rs", "winapi", "windows-sys 0.61.2", "wintun", @@ -2388,9 +2608,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.45" +version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" dependencies = [ "proc-macro2", ] @@ -2598,6 +2818,20 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "route_manager" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "319bb478ff9aae1dc7a544fa599e9eb47e2521621dc3921fba6abcaaa312092c" +dependencies = [ + "flume 0.12.0", + "libc", + "netlink-packet-core 0.8.1", + "netlink-packet-route 0.28.0", + "netlink-sys", + "windows-sys 0.61.2", +] + [[package]] name = "rpassword" version = "7.4.0" @@ -2861,7 +3095,7 @@ checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" dependencies = [ "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -2885,7 +3119,7 @@ checksum = "175ee3e80ae9982737ca543e96133087cbd9a485eecc3bc4de9c1a37b47ea59c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -3064,7 +3298,7 @@ dependencies = [ "crc", "crossbeam-queue", "either", - "event-listener", + "event-listener 2.5.3", "futures-channel", "futures-core", "futures-intrusive", @@ -3212,7 +3446,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b244ef0a8414da0bed4bb1910426e890b19e5e9bccc27ada6b797d05c55ae0aa" dependencies = [ "atoi", - "flume", + "flume 0.11.1", "futures-channel", "futures-core", "futures-executor", @@ -3300,9 +3534,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.117" +version = "2.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6" dependencies = [ "proc-macro2", "quote", @@ -3326,7 +3560,7 @@ checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -3351,7 +3585,7 @@ checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7" dependencies = [ "bitflags 1.3.2", "core-foundation 0.9.4", - "system-configuration-sys", + "system-configuration-sys 0.5.0", ] [[package]] @@ -3364,6 +3598,16 @@ dependencies = [ "libc", ] +[[package]] +name = "system-configuration-sys" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e1d1b10ced5ca923a1fcb8d03e96b8d3268065d724548c0211415ff6ac6bac4" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "tar" version = "0.4.45" @@ -3422,7 +3666,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -3433,7 +3677,7 @@ checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" dependencies = [ "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -3526,7 +3770,7 @@ checksum = "5c55a2eff8b69ce66c84f85e1da1c233edc36ceb85a2058d11b0d6a3c7e7569c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -3689,7 +3933,7 @@ checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" dependencies = [ "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -3734,6 +3978,32 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "tun-rs" +version = "2.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63271eef24ceec4e06b121a757ace951681196950825e6f29d955a151363eb6b" +dependencies = [ + "blocking", + "byteorder", + "bytes", + "c2rust-bitfields 0.22.1", + "encoding_rs", + "getifaddrs", + "ipnet", + "libc", + "libloading 0.9.0", + "log", + "netconfig-rs", + "nix 0.31.3", + "route_manager", + "scopeguard", + "tokio", + "widestring", + "windows-sys 0.61.2", + "winreg", +] + [[package]] name = "typenum" version = "1.19.0" @@ -3748,9 +4018,9 @@ checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5" [[package]] name = "unicode-ident" -version = "1.0.24" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" [[package]] name = "unicode-normalization" @@ -3967,7 +4237,7 @@ dependencies = [ "bumpalo", "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", "wasm-bindgen-shared", ] @@ -4062,6 +4332,12 @@ dependencies = [ "wasite", ] +[[package]] +name = "widestring" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72069c3113ab32ab29e5584db3c6ec55d416895e60715417b5b883a357c3e471" + [[package]] name = "winapi" version = "0.3.9" @@ -4112,16 +4388,38 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows" +version = "0.61.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9babd3a767a4c1aef6900409f85f5d53ce2544ccdfaa86dad48c91782c6d6893" +dependencies = [ + "windows-collections 0.2.0", + "windows-core 0.61.2", + "windows-future 0.2.1", + "windows-link 0.1.3", + "windows-numerics 0.2.0", +] + [[package]] name = "windows" version = "0.62.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "527fadee13e0c05939a6a05d5bd6eec6cd2e3dbd648b9f8e447c6518133d8580" dependencies = [ - "windows-collections", + "windows-collections 0.3.2", "windows-core 0.62.2", - "windows-future", - "windows-numerics", + "windows-future 0.3.2", + "windows-numerics 0.3.1", +] + +[[package]] +name = "windows-collections" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3beeceb5e5cfd9eb1d76b381630e82c4241ccd0d27f1a39ed41b2760b255c5e8" +dependencies = [ + "windows-core 0.61.2", ] [[package]] @@ -4142,6 +4440,19 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows-core" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0fdd3ddb90610c7638aa2b3a3ab2904fb9e5cdbecc643ddb3647212781c4ae3" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link 0.1.3", + "windows-result 0.3.4", + "windows-strings 0.4.2", +] + [[package]] name = "windows-core" version = "0.62.2" @@ -4150,9 +4461,20 @@ checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" dependencies = [ "windows-implement", "windows-interface", - "windows-link", - "windows-result", - "windows-strings", + "windows-link 0.2.1", + "windows-result 0.4.1", + "windows-strings 0.5.1", +] + +[[package]] +name = "windows-future" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc6a41e98427b19fe4b73c550f060b59fa592d7d686537eebf9385621bfbad8e" +dependencies = [ + "windows-core 0.61.2", + "windows-link 0.1.3", + "windows-threading 0.1.0", ] [[package]] @@ -4162,8 +4484,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1d6f90251fe18a279739e78025bd6ddc52a7e22f921070ccdc67dde84c605cb" dependencies = [ "windows-core 0.62.2", - "windows-link", - "windows-threading", + "windows-link 0.2.1", + "windows-threading 0.2.1", ] [[package]] @@ -4174,7 +4496,7 @@ checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" dependencies = [ "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -4185,15 +4507,31 @@ checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" dependencies = [ "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] +[[package]] +name = "windows-link" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a" + [[package]] name = "windows-link" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-numerics" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9150af68066c4c5c07ddc0ce30421554771e528bde427614c61038bc2c92c2b1" +dependencies = [ + "windows-core 0.61.2", + "windows-link 0.1.3", +] + [[package]] name = "windows-numerics" version = "0.3.1" @@ -4201,7 +4539,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e2e40844ac143cdb44aead537bbf727de9b044e107a0f1220392177d15b0f26" dependencies = [ "windows-core 0.62.2", - "windows-link", + "windows-link 0.2.1", +] + +[[package]] +name = "windows-result" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56f42bd332cc6c8eac5af113fc0c1fd6a8fd2aa08a0119358686e5160d0586c6" +dependencies = [ + "windows-link 0.1.3", ] [[package]] @@ -4210,7 +4557,16 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" dependencies = [ - "windows-link", + "windows-link 0.2.1", +] + +[[package]] +name = "windows-strings" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56e6c93f3a0c3b36176cb1327a4958a0353d5d166c2a35cb268ace15e91d3b57" +dependencies = [ + "windows-link 0.1.3", ] [[package]] @@ -4219,7 +4575,7 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" dependencies = [ - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -4273,7 +4629,7 @@ version = "0.61.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" dependencies = [ - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -4328,7 +4684,7 @@ version = "0.53.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" dependencies = [ - "windows-link", + "windows-link 0.2.1", "windows_aarch64_gnullvm 0.53.1", "windows_aarch64_msvc 0.53.1", "windows_i686_gnu 0.53.1", @@ -4339,13 +4695,22 @@ dependencies = [ "windows_x86_64_msvc 0.53.1", ] +[[package]] +name = "windows-threading" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b66463ad2e0ea3bbf808b7f1d371311c80e115c0b71d60efc142cafbcfb057a6" +dependencies = [ + "windows-link 0.1.3", +] + [[package]] name = "windows-threading" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3949bd5b99cafdf1c7ca86b43ca564028dfe27d66958f2470940f73d86d75b37" dependencies = [ - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -4543,14 +4908,24 @@ dependencies = [ "memchr", ] +[[package]] +name = "winreg" +version = "0.56.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d6f32a0ff4a9f6f01231eb2059cc85479330739333e0e58cadf03b6af2cca10" +dependencies = [ + "cfg-if", + "windows-sys 0.61.2", +] + [[package]] name = "wintun" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b3c8c8876c686f8a2d6376999ac1c9a24c74d2968551c9394f7e89127783685" dependencies = [ - "c2rust-bitfields", - "libloading", + "c2rust-bitfields 0.18.0", + "libloading 0.8.9", "log", "thiserror 1.0.69", "windows 0.52.0", @@ -4586,7 +4961,7 @@ dependencies = [ "heck 0.5.0", "indexmap", "prettyplease", - "syn 2.0.117", + "syn 2.0.106", "wasm-metadata", "wit-bindgen-core", "wit-component", @@ -4602,7 +4977,7 @@ dependencies = [ "prettyplease", "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", "wit-bindgen-core", "wit-bindgen-rust", ] @@ -4678,7 +5053,7 @@ checksum = "b659052874eb698efe5b9e8cf382204678a0086ebf46982b79d6ca3182927e5d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", "synstructure", ] @@ -4699,7 +5074,7 @@ checksum = "0e8bc7269b54418e7aeeef514aa68f8690b8c0489a06b0136e5f57c4c5ccab89" dependencies = [ "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] @@ -4719,7 +5094,7 @@ checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" dependencies = [ "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", "synstructure", ] @@ -4759,7 +5134,7 @@ checksum = "eadce39539ca5cb3985590102671f2567e659fca9666581ad3411d59207951f3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.117", + "syn 2.0.106", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 193b440..b86bf10 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,7 @@ default-net = "0.22.0" socket2 = "0.6.3" hostname = "0.4.2" sysinfo = "0.38.4" +tun-rs = {version="2.8.5", features=["async"]} # rolling-file = { path = "../rolling-file" } [target.'cfg(unix)'.dependencies] diff --git a/src/network/async_main.rs b/src/network/async_main.rs index e5329b2..e30714c 100755 --- a/src/network/async_main.rs +++ b/src/network/async_main.rs @@ -1,22 +1,20 @@ use std::net::SocketAddr; -use std::sync::atomic::{Ordering}; +use std::sync::atomic::Ordering; use std::sync::Arc; use std::time::Duration; -use crate::config::{TCP_PING_TIME}; +use crate::config::TCP_PING_TIME; use crate::network::ipv6::run_ipv6; -use crate::network::{ - get_edge, ping_to_sn, read_and_parse_packet, TunTapPacketHandler, -}; +use crate::network::{get_edge, ping_to_sn, read_and_parse_packet, TunTapPacketHandler}; use crate::tcp::{init_quic_conn, send_stun_request}; use crate::utils::{send_to_sock, CommandLine}; -use crate::{ConnectionInfo}; +use crate::ConnectionInfo; use bytes::BytesMut; -use etherparse::{PacketBuilder}; -use sdlan_sn_rs::peer::{SdlanSock}; +use etherparse::PacketBuilder; +use sdlan_sn_rs::peer::SdlanSock; use sdlan_sn_rs::utils::{get_current_timestamp, ip_to_string, is_multi_broadcast}; use sdlan_sn_rs::utils::{Mac, Result}; -use tokio::net::{UdpSocket}; +use tokio::net::UdpSocket; use tokio::sync::mpsc::{channel, Receiver, Sender}; use tokio_util::sync::CancellationToken; @@ -120,7 +118,11 @@ pub async fn async_main( Ok(()) } -async fn run_edge_loop(eee: &'static Node, global_dns_rx: Receiver<(Vec, SocketAddr)>, cancel: CancellationToken) { +async fn run_edge_loop( + eee: &'static Node, + global_dns_rx: Receiver<(Vec, SocketAddr)>, + cancel: CancellationToken, +) { ping_to_sn().await; { let cancel2 = cancel.clone(); @@ -202,7 +204,7 @@ pub async fn loop_socket_v4( } async fn receive_dns_reply(sock: &Arc) -> Option> { - let mut reply = vec![0;1024]; + let mut reply = vec![0; 1024]; if let Ok((size, _from)) = sock.recv_from(&mut reply).await { if size == 0 { // closed @@ -214,7 +216,11 @@ async fn receive_dns_reply(sock: &Arc) -> Option> { None } -async fn loop_tap(eee: &'static Node, mut dns_rx: Receiver<(Vec, SocketAddr)>, cancel: CancellationToken) { +async fn loop_tap( + eee: &'static Node, + mut dns_rx: Receiver<(Vec, SocketAddr)>, + cancel: CancellationToken, +) { debug!("loop tap"); let (tx, mut rx) = channel(10); tokio::spawn(async { @@ -233,10 +239,10 @@ async fn loop_tap(eee: &'static Node, mut dns_rx: Receiver<(Vec, SocketAddr) if let Ok(mut dns) = simple_dns::Packet::parse(&data.0) { let transaction_id = dns.id(); if let Some((ip, port, origin_transaction_id)) = eee.dns_matcher.get_client_info(transaction_id) { - warn!("got dns reply from global 223.5.5.5, will send to {}:{}", + warn!("got dns reply from global 223.5.5.5, will send to {}:{}", ip_to_string(&ip), port); - + let dstmac = eee.device_config.get_mac(); let srcmac = eee.device_config.dns_mac; @@ -293,23 +299,23 @@ async fn loop_tap(eee: &'static Node, mut dns_rx: Receiver<(Vec, SocketAddr) } #[cfg(any(feature = "tun", target_os = "windows"))] -fn get_data_from_tun_with_layer2_zeroed(eee: &Node) -> BytesMut { +async fn get_data_from_tun_with_layer2_zeroed(eee: &Node) -> BytesMut { let mut temp = BytesMut::zeroed(1514); // let mut temp = BytesMut::with_capacity(1514); let mut data_buf = temp.split_off(14); - - let Ok(size) = eee.device.recv(&mut data_buf) else { + let Ok(size) = eee.device.recv(&mut data_buf).await else { return BytesMut::new(); }; + data_buf.truncate(size); temp.unsplit(data_buf); temp } #[cfg(not(feature = "tun"))] -fn get_data_from_tap_with_layer2(eee: &Node) -> BytesMut { +async fn get_data_from_tap_with_layer2(eee: &Node) -> BytesMut { let mut buf = BytesMut::zeroed(1514); - let Ok(size) = eee.device.recv(&mut buf) else { + let Ok(size) = eee.device.recv(&mut buf).await else { return BytesMut::new(); }; buf.truncate(size); @@ -318,16 +324,14 @@ fn get_data_from_tap_with_layer2(eee: &Node) -> BytesMut { async fn get_tun_flow(eee: &'static Node, tx: Sender) { loop { - let buf = tokio::task::spawn_blocking(|| { + let buf = { #[cfg(any(feature = "tun", target_os = "windows"))] - let data = get_data_from_tun_with_layer2_zeroed(eee); - #[cfg(all(not(feature = "tun"), not(target_os="windows")))] - let data = get_data_from_tap_with_layer2(eee); + let data = get_data_from_tun_with_layer2_zeroed(eee).await; + #[cfg(all(not(feature = "tun"), not(target_os = "windows")))] + let data = get_data_from_tap_with_layer2(eee).await; data - }) - .await - .unwrap(); + }; if buf.len() == 0 { return; @@ -360,18 +364,14 @@ async fn read_and_parse_tun_packet(eee: &'static Node, buf: BytesMut) { async fn edge_send_packet_to_net(eee: &Node, data: BytesMut) { // debug!("edge send packet to net({} bytes): {:?}", data.len(), data); - /* + /* let encrypt_key = eee.get_encrypt_key(); if encrypt_key.len() == 0 { error!("drop tun packet due to encrypt key len is 0"); return; } */ - if let Err(e) = eee - .device - .handle_packet_from_device(data) - .await - { + if let Err(e) = eee.device.handle_packet_from_device(data).await { error!("failed to handle packet from device: {}", e.to_string()); } } diff --git a/src/network/tun_win.rs b/src/network/tun_win.rs index 0961f27..637f922 100755 --- a/src/network/tun_win.rs +++ b/src/network/tun_win.rs @@ -4,7 +4,8 @@ use etherparse::{Ethernet2Header, IpHeaders, NetSlice, SlicedPacket, TransportSl use ipnet::Ipv4Net; use sdlan_sn_rs::config::SDLAN_DEFAULT_TTL; use sdlan_sn_rs::utils::{ - BROADCAST_MAC, Result, SDLanError, aes_encrypt, ip_to_string, is_multi_broadcast, net_bit_len_to_mask + aes_encrypt, ip_to_string, is_multi_broadcast, net_bit_len_to_mask, Result, SDLanError, + BROADCAST_MAC, }; use std::io::{Error, ErrorKind}; use std::net::Ipv4Addr; @@ -13,6 +14,7 @@ use std::process::Command; use std::sync::atomic::Ordering; use std::sync::Arc; use tracing::{debug, error, info}; +use tun_rs::AsyncDevice; use wintun; use crate::network::{ @@ -28,13 +30,130 @@ use super::device::{DeviceConfig, Mode}; use super::TunTapPacketHandler; pub struct Iface { + device: AsyncDevice, + if_idx: u32, + name: String, +} + +impl Iface { + fn new(path: &str, name: &str) -> Self { + let dev = tun_rs::DeviceBuilder::new() + .wintun_file(path.to_string()) + .name(name) + .layer(tun_rs::Layer::L3) + .build_async() + .expect("failed to create tun"); + + let idx = dev.if_index().expect("failed to get if index"); + Self { + device: dev, + if_idx: idx, + name: name.to_string(), + } + } +} + +impl Iface { + pub fn get_if_idx(&self) -> u32 { + self.if_idx + } + + pub async fn recv(&self, buf: &mut [u8]) -> std::io::Result { + self.device.recv(buf).await + } + + pub async fn send(&self, content: &[u8]) -> std::io::Result { + self.device.send(content).await + } + + pub fn reload_config(&self, node: &Node, device_config: &DeviceConfig, network_domain: &str) { + let netbit = device_config.get_net_bit(); + let ip = device_config.get_ip(); + if netbit == 0 || ip == 0 { + error!("reload config's ip is 0"); + return; + } + + let mask = net_bit_len_to_mask(netbit); + let ip = ip_to_string(&ip); + + let netbit = ip_to_string(&net_bit_len_to_mask(netbit)); + + let mut cmd = Command::new("netsh"); + debug!("name={}, addr={}, mask={}", self.name, ip, netbit); + let command = cmd + .creation_flags(0x08000000) + .arg("interface") + .arg("ip") + .arg("set") + .arg("address") + .arg(&format!("name=\"{}\"", self.name)) + .arg("source=static") + .arg(&format!("addr={}", ip)) + .arg(&format!("mask={}", netbit)); + + let res = command.status(); + // let res = command.output(); + + match res { + Ok(r) => { + if r.success() { + debug!("netsh ok"); + } else { + error!("failed to run netsh, returned {:?}", r.code()) + } + } + Err(e) => { + error!("failed to run netsh: {}", e.to_string()); + } + } + + let mut cmd = Command::new("netsh"); + let command = cmd + .creation_flags(0x08000000) + .arg("interface") + .arg("ipv4") + .arg("set") + .arg("subinterface") + .arg(&format!("\"{}\"", self.name)) + .arg(format!("mtu={}", device_config.mtu)) + .arg("store=persistent"); + + let res = command.status(); + + match res { + Ok(r) => { + if r.success() { + debug!("netsh2 ok"); + } else { + error!("failed to run netsh set mtu, returned {:?}", r.code()) + } + } + Err(e) => { + error!("failed to run netsh2: {}", e.to_string()); + } + } + + // let gw = ip_to_string(&default_gw); + // debug!("gw = {}", ip); + if let Err(e) = set_dns(&self.name, network_domain, &ip, self.if_idx) { + error!("failed to set dns: {:?}", e); + } else { + debug!("set dns ok"); + } + + node.route_table.apply_system(self.if_idx); + } +} + +pub struct IfaceOld { if_idx: u32, name: String, _adapter: Arc, session: Arc, } -impl Iface { +impl IfaceOld { pub fn get_if_idx(&self) -> u32 { self.if_idx } @@ -55,11 +174,12 @@ impl Iface { } pub fn send(&self, content: &[u8]) -> std::io::Result { - let Ok(mut pkt) = self - .session - .allocate_send_packet(content.len() as u16) else { - error!("failed to allocate send packet"); - return Err(std::io::Error::new(std::io::ErrorKind::Other, "failed to allocate send packet")); + let Ok(mut pkt) = self.session.allocate_send_packet(content.len() as u16) else { + error!("failed to allocate send packet"); + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + "failed to allocate send packet", + )); }; let buf: &mut [u8] = pkt.bytes_mut(); buf.copy_from_slice(content); @@ -310,7 +430,7 @@ impl TunTapPacketHandler for Iface { // println!("got ip packet"); // println!("got data: {:?}", rest); - match edge.device.send(rest) { + match edge.device.send(rest).await { Ok(size) => { debug!("send to tun {} bytes", size); } @@ -730,13 +850,18 @@ impl TunTapPacketHandler for Iface { } fn create_wintun(path: &str, name: &str) -> std::io::Result { + Ok(Iface::new(path, name)) + /* let wt = unsafe { wintun::load_from_path(path) }.expect("failed to load wintun"); let adapter = match wintun::Adapter::open(&wt, name) { Ok(a) => a, Err(_e) => { - let Ok(adapt) = wintun::Adapter::create(&wt, name, "Punchnet", None) else { - return Err(std::io::Error::new(std::io::ErrorKind::Other, "failed to create Punch adapter")); + let Ok(adapt) = wintun::Adapter::create(&wt, name, "Punchnet", None) else { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + "failed to create Punch adapter", + )); }; adapt } @@ -746,7 +871,10 @@ fn create_wintun(path: &str, name: &str) -> std::io::Result { .expect("failed to get adapter index"); // println!("idx = {}", idx); let Ok(sess) = adapter.start_session(wintun::MAX_RING_CAPACITY) else { - return Err(std::io::Error::new(std::io::ErrorKind::Other, "failed to start session, maybe one process is running, or not running with admin?")); + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + "failed to start session, maybe one process is running, or not running with admin?", + )); }; let session = Arc::new(sess); Ok(Iface { @@ -755,6 +883,7 @@ fn create_wintun(path: &str, name: &str) -> std::io::Result { session, name: name.to_owned(), }) + */ } pub fn new_iface(name: &str, _mode: Mode) -> std::io::Result { @@ -778,8 +907,13 @@ pub fn set_dns(name: &str, _network_domain: &str, gw: &str, ifidx: u32) -> Resul .creation_flags(0x08000000) .status()?; if !res.success() { - error!("failed to add route for dns 100.100.100.100: {:?}", res.code()); - return Err(SDLanError::IOError("failed to add route for dns".to_owned())); + error!( + "failed to add route for dns 100.100.100.100: {:?}", + res.code() + ); + return Err(SDLanError::IOError( + "failed to add route for dns".to_owned(), + )); } //println!("res1: {}", res.status.success());