diff --git a/ForTunCli/Cargo.lock b/ForTunCli/Cargo.lock index fd04b93..c5bd110 100644 --- a/ForTunCli/Cargo.lock +++ b/ForTunCli/Cargo.lock @@ -35,12 +35,6 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" -[[package]] -name = "bytes" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" - [[package]] name = "cfg-if" version = "1.0.0" @@ -77,7 +71,7 @@ dependencies = [ "scopeguard", "tokio", "tracing", - "tracing-subscriber", + "version-compare", "windows", ] @@ -96,15 +90,6 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "hermit-abi" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7" -dependencies = [ - "libc", -] - [[package]] name = "hwaddr" version = "0.1.7" @@ -124,12 +109,6 @@ dependencies = [ "winapi-build", ] -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" - [[package]] name = "libc" version = "0.2.139" @@ -147,53 +126,12 @@ dependencies = [ "winapi 0.2.8", ] -[[package]] -name = "lock_api" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" -dependencies = [ - "autocfg", - "scopeguard", -] - -[[package]] -name = "log" -version = "0.4.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" -dependencies = [ - "cfg-if", -] - [[package]] name = "memchr" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" -[[package]] -name = "mio" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5d732bc30207a6423068df043e3d02e0735b155ad7ce1a6f76fe2baa5b158de" -dependencies = [ - "libc", - "log", - "wasi", - "windows-sys 0.42.0", -] - -[[package]] -name = "nu-ansi-term" -version = "0.46.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" -dependencies = [ - "overload", - "winapi 0.3.9", -] - [[package]] name = "num-bigint" version = "0.4.3" @@ -224,28 +162,12 @@ dependencies = [ "autocfg", ] -[[package]] -name = "num_cpus" -version = "1.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b" -dependencies = [ - "hermit-abi", - "libc", -] - [[package]] name = "once_cell" version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" -[[package]] -name = "overload" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" - [[package]] name = "packet" version = "0.1.4" @@ -258,29 +180,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "parking_lot" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" -dependencies = [ - "lock_api", - "parking_lot_core", -] - -[[package]] -name = "parking_lot_core" -version = "0.9.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9069cbb9f99e3a5083476ccb29ceb1de18b9118cafa53e90c9551235de2b9521" -dependencies = [ - "cfg-if", - "libc", - "redox_syscall", - "smallvec", - "windows-sys 0.45.0", -] - [[package]] name = "phf" version = "0.8.0" @@ -369,15 +268,6 @@ dependencies = [ "rand_core 0.3.1", ] -[[package]] -name = "redox_syscall" -version = "0.2.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" -dependencies = [ - "bitflags", -] - [[package]] name = "regex" version = "1.7.1" @@ -410,24 +300,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" -[[package]] -name = "sharded-slab" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" -dependencies = [ - "lazy_static", -] - -[[package]] -name = "signal-hook-registry" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" -dependencies = [ - "libc", -] - [[package]] name = "siphasher" version = "0.3.10" @@ -444,22 +316,6 @@ dependencies = [ "tempdir", ] -[[package]] -name = "smallvec" -version = "1.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" - -[[package]] -name = "socket2" -version = "0.4.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02e2d2db9033d13a1567121ddd7a095ee144db4e1ca1b1bda3419bc0da294ebd" -dependencies = [ - "libc", - "winapi 0.3.9", -] - [[package]] name = "syn" version = "1.0.107" @@ -501,16 +357,6 @@ dependencies = [ "syn", ] -[[package]] -name = "thread_local" -version = "1.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" -dependencies = [ - "cfg-if", - "once_cell", -] - [[package]] name = "tokio" version = "1.25.0" @@ -518,28 +364,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c8e00990ebabbe4c14c08aca901caed183ecd5c09562a12c824bb53d3c3fd3af" dependencies = [ "autocfg", - "bytes", - "libc", - "memchr", - "mio", - "num_cpus", - "parking_lot", "pin-project-lite", - "signal-hook-registry", - "socket2", - "tokio-macros", - "windows-sys 0.42.0", -] - -[[package]] -name = "tokio-macros" -version = "1.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8" -dependencies = [ - "proc-macro2", - "quote", - "syn", + "windows-sys", ] [[package]] @@ -572,32 +398,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a" dependencies = [ "once_cell", - "valuable", -] - -[[package]] -name = "tracing-log" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" -dependencies = [ - "lazy_static", - "log", - "tracing-core", -] - -[[package]] -name = "tracing-subscriber" -version = "0.3.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70" -dependencies = [ - "nu-ansi-term", - "sharded-slab", - "smallvec", - "thread_local", - "tracing-core", - "tracing-log", ] [[package]] @@ -613,16 +413,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" [[package]] -name = "valuable" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" - -[[package]] -name = "wasi" -version = "0.11.0+wasi-snapshot-preview1" +name = "version-compare" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29" [[package]] name = "winapi" @@ -660,9 +454,9 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows" -version = "0.44.0" +version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e745dab35a0c4c77aa3ce42d595e13d2003d6902d6b08c9ef5fc326d08da12b" +checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" dependencies = [ "windows-targets", ] @@ -673,44 +467,41 @@ version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.42.1", + "windows_aarch64_msvc 0.42.1", + "windows_i686_gnu 0.42.1", + "windows_i686_msvc 0.42.1", + "windows_x86_64_gnu 0.42.1", + "windows_x86_64_gnullvm 0.42.1", + "windows_x86_64_msvc 0.42.1", ] [[package]] -name = "windows-sys" -version = "0.45.0" +name = "windows-targets" +version = "0.48.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +checksum = "05d4b17490f70499f20b9e791dcf6a299785ce8af4d709018206dc5b4953e95f" dependencies = [ - "windows-targets", + "windows_aarch64_gnullvm 0.48.0", + "windows_aarch64_msvc 0.48.0", + "windows_i686_gnu 0.48.0", + "windows_i686_msvc 0.48.0", + "windows_x86_64_gnu 0.48.0", + "windows_x86_64_gnullvm 0.48.0", + "windows_x86_64_msvc 0.48.0", ] [[package]] -name = "windows-targets" +name = "windows_aarch64_gnullvm" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7" -dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] +checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608" [[package]] name = "windows_aarch64_gnullvm" -version = "0.42.1" +version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608" +checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" [[package]] name = "windows_aarch64_msvc" @@ -718,32 +509,68 @@ version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" + [[package]] name = "windows_i686_gnu" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640" +[[package]] +name = "windows_i686_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" + [[package]] name = "windows_i686_msvc" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605" +[[package]] +name = "windows_i686_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" + [[package]] name = "windows_x86_64_gnu" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" + [[package]] name = "windows_x86_64_gnullvm" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" + [[package]] name = "windows_x86_64_msvc" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" diff --git a/ForTunCli/Cargo.toml b/ForTunCli/Cargo.toml index 2995e6e..3c07fda 100644 --- a/ForTunCli/Cargo.toml +++ b/ForTunCli/Cargo.toml @@ -11,13 +11,14 @@ edition = "2021" [dependencies] -windows = { version = "0.44", features = ["Win32_System_IO", "Win32_Storage_FileSystem", "Win32_Devices_Enumeration_Pnp", "Win32_System_Threading" ,"Win32_Devices_Properties", "Win32_Foundation", "Win32_Security", "Win32_Devices_DeviceAndDriverInstallation", "Win32_System_Registry", "Win32_NetworkManagement_IpHelper"] } +windows = { version = "0.48.0", features = ["Win32_System_IO", "Win32_Storage_FileSystem", "Win32_Devices_Enumeration_Pnp", "Win32_System_Threading" ,"Win32_Devices_Properties", "Win32_Foundation", "Win32_Security", "Win32_Devices_DeviceAndDriverInstallation", "Win32_System_Registry", "Win32_NetworkManagement_IpHelper"] } anyhow = "1" scopeguard = "1" local-encoding = "*" -tokio = {version = "1", features = ["full"]} +tokio = {version = "1"} packet = "0.1" cidr-utils = "0.5" - -tracing-subscriber = "0.3" tracing = "0.1" +version-compare = "0.1.1" + +#tracing-subscriber = "0.3" \ No newline at end of file diff --git a/ForTunCli/src/device_ops.rs b/ForTunCli/src/device_ops.rs index cceb636..23f04a4 100644 --- a/ForTunCli/src/device_ops.rs +++ b/ForTunCli/src/device_ops.rs @@ -7,33 +7,20 @@ use anyhow::{anyhow, bail, Context}; use cidr_utils::cidr::IpCidr; use std::thread::sleep; use std::time::Duration; +use version_compare::Version; use windows::core::{wcslen, GUID, HRESULT, HSTRING, PCWSTR, PWSTR}; use windows::w; -use windows::Win32::Devices::DeviceAndDriverInstallation::{ - CM_Get_DevNode_PropertyW, CM_Get_Device_ID_ListW, CM_Get_Device_ID_List_SizeW, - CM_Get_Device_Interface_ListW, CM_Get_Device_Interface_List_SizeW, CM_Locate_DevNodeW, - SetupCopyOEMInfW, SetupDiSetClassInstallParamsW, CM_GETIDLIST_FILTER_CLASS, - CM_GET_DEVICE_INTERFACE_LIST_ALL_DEVICES, CM_LOCATE_DEVINST_NORMAL, CM_LOCATE_DEVNODE_PHANTOM, - CR_NO_SUCH_DEVNODE, CR_SUCCESS, DIF_REMOVE, DI_REMOVEDEVICE_GLOBAL, GUID_DEVCLASS_NET, - HDEVINFO, SPOST_PATH, SP_CLASSINSTALL_HEADER, SP_COPY_NEWER, SP_DEVINFO_DATA, - SP_REMOVEDEVICE_PARAMS, -}; +use windows::Win32::Devices::DeviceAndDriverInstallation::{CM_Get_DevNode_PropertyW, CM_Get_Device_ID_ListW, CM_Get_Device_ID_List_SizeW, CM_Get_Device_Interface_ListW, CM_Get_Device_Interface_List_SizeW, CM_Locate_DevNodeW, SetupCopyOEMInfW, SetupDiSetClassInstallParamsW, CM_GETIDLIST_FILTER_CLASS, CM_GET_DEVICE_INTERFACE_LIST_ALL_DEVICES, CM_LOCATE_DEVINST_NORMAL, CM_LOCATE_DEVNODE_PHANTOM, CR_NO_SUCH_DEVNODE, CR_SUCCESS, DIF_REMOVE, DI_REMOVEDEVICE_GLOBAL, GUID_DEVCLASS_NET, HDEVINFO, SPOST_PATH, SP_CLASSINSTALL_HEADER, SP_COPY_NEWER, SP_DEVINFO_DATA, SP_REMOVEDEVICE_PARAMS, SetupDiEnumDriverInfoW}; use windows::Win32::Devices::Enumeration::Pnp::{ SWDeviceCapabilitiesDriverRequired, SWDeviceCapabilitiesSilentInstall, SwDeviceClose, SwDeviceCreate, HSWDEVICE, SW_DEVICE_CREATE_INFO, }; -use windows::Win32::Devices::Properties::{ - DEVPKEY_Device_ClassGuid, DEVPKEY_Device_FriendlyName, DEVPKEY_Device_HardwareIds, - DEVPROPCOMPKEY, DEVPROPERTY, DEVPROP_STORE_SYSTEM, DEVPROP_TYPE_GUID, DEVPROP_TYPE_STRING, -}; +use windows::Win32::Devices::Properties::{DEVPKEY_Device_ClassGuid, DEVPKEY_Device_FriendlyName, DEVPKEY_Device_HardwareIds, DEVPROPCOMPKEY, DEVPROPERTY, DEVPROP_STORE_SYSTEM, DEVPROP_TYPE_GUID, DEVPROP_TYPE_STRING, DEVPROPTYPE, DEVPROPKEY, DEVPKEY_Device_DriverVersion}; use windows::Win32::Foundation::{ CloseHandle, GetLastError, ERROR_NO_MORE_ITEMS, HANDLE, NO_ERROR, WAIT_OBJECT_0, }; use windows::Win32::NetworkManagement::IpHelper::GetAdapterIndex; -use windows::Win32::Storage::FileSystem::{ - CreateFileW, FILE_ATTRIBUTE_SYSTEM, FILE_FLAG_OVERLAPPED, FILE_GENERIC_READ, - FILE_GENERIC_WRITE, FILE_SHARE_NONE, OPEN_EXISTING, -}; +use windows::Win32::Storage::FileSystem::{CreateFileW, FILE_ATTRIBUTE_SYSTEM, FILE_FLAG_OVERLAPPED, FILE_GENERIC_READ, FILE_GENERIC_WRITE, FILE_SHARE_NONE, OPEN_EXISTING}; use windows::Win32::System::Registry::{ RegCloseKey, RegEnumKeyExW, RegOpenKeyExW, RegQueryValueExW, HKEY, HKEY_LOCAL_MACHINE, KEY_ENUMERATE_SUB_KEYS, KEY_READ, @@ -108,7 +95,7 @@ impl AdapterDevice { let name = HSTRING::from(self.interface_id.clone()); CreateFileW( PCWSTR(name.as_ptr()), - FILE_GENERIC_READ | FILE_GENERIC_WRITE, + (FILE_GENERIC_READ | FILE_GENERIC_WRITE).0, FILE_SHARE_NONE, None, OPEN_EXISTING, @@ -140,19 +127,24 @@ impl AdapterDevice { } } -//TODO: inf_path change to compile path pub fn init_device( device_guid: &GUID, name: &str, inf_path: &str, ) -> anyhow::Result { - //let inf_path = "C:/DriverTest/Drivers/ForTun.inf"; let devices = enum_device(&FOR_TUN_DEV_CLASS, FOR_TUN_HWID)?; if devices.is_empty() { // There is no devices install_driver(inf_path)?; // TODO: this may install multiple times. need add more exact function to check if driver installed. } else { - //TODO: compare version, if old exists, stop and remove all devices and reinstall new driver. + let current_version = Version::from(env!( "CARGO_PKG_VERSION" )).unwrap(); + let has_old = devices.iter().find(|(_, version_str)| { + let working_driver_version:Version = Version::from(version_str).unwrap(); + current_version > working_driver_version + }).is_some(); + if has_old { + return bail!("There is running old driver device, please stop it before running app") + } } let (device_handler, device_instance_id) = @@ -630,12 +622,15 @@ fn remove_device(dev_info: HDEVINFO, dev_info_data: &SP_DEVINFO_DATA) { }; } -fn enum_device(device_class_id: &GUID, hwid: &str) -> anyhow::Result> { +fn enum_device(device_class_id: &GUID, hwid: &str) -> anyhow::Result> { let mut device_list_len = 0; let device_class_id = HSTRING::from(format!("{{{device_class_id:?}}}\0")); let device_class_id = PCWSTR(device_class_id.as_ptr()); let flag = CM_GETIDLIST_FILTER_CLASS; + unsafe { + + } let cr = unsafe { CM_Get_Device_ID_List_SizeW(&mut device_list_len, device_class_id, flag) }; if cr != CR_SUCCESS { @@ -651,7 +646,7 @@ fn enum_device(device_class_id: &GUID, hwid: &str) -> anyhow::Result } let mut dev_inst: u32 = 0; - let mut property_type: u32 = 0; + let mut property_type: DEVPROPTYPE = DEVPROPTYPE::default(); let mut property_value: Vec = Vec::with_capacity(2048); let mut property_value_length = 0; @@ -659,7 +654,7 @@ fn enum_device(device_class_id: &GUID, hwid: &str) -> anyhow::Result let mut index = 0; let mut device_id = PCWSTR::from_raw(buffer[index..].as_mut_ptr()); - let mut result: Vec = Vec::new(); + let mut result: Vec<(String,String)> = Vec::new(); while buffer[index] != 0 && !device_id.is_null() { let cr = unsafe { @@ -681,33 +676,13 @@ fn enum_device(device_class_id: &GUID, hwid: &str) -> anyhow::Result } } } else { - unsafe { - CM_Get_DevNode_PropertyW( - dev_inst, - &DEVPKEY_Device_HardwareIds, - &mut property_type, - Some(property_value.as_mut_ptr()), - &mut property_value_length, - 0, - ); - } + let name = _cm_get_string_property(dev_inst,&DEVPKEY_Device_HardwareIds, &mut property_value)?; - if property_value_length > 0 { + if name == hwid { unsafe { - property_value.set_len(property_value_length as usize); - }; - - let name = unsafe { - PCWSTR::from_raw(property_value.as_mut_ptr().cast::()).to_string() - }; - - if let Ok(name) = name { - if name == hwid { - unsafe { - if let Ok(device_id) = device_id.to_string() { - result.push(device_id); - } - } + if let Ok(device_id) = device_id.to_string() { + let version = _cm_get_string_property(dev_inst, &DEVPKEY_Device_DriverVersion, &mut property_value)?; + result.push((device_id, version)); } } } @@ -724,30 +699,31 @@ fn enum_device(device_class_id: &GUID, hwid: &str) -> anyhow::Result Ok(result) } -#[cfg(test)] -mod test { - use windows::core::PCSTR; - use windows::Win32::Storage::FileSystem::{ - CreateFileA, FILE_ATTRIBUTE_SYSTEM, FILE_FLAG_OVERLAPPED, FILE_GENERIC_READ, - FILE_GENERIC_WRITE, FILE_SHARE_MODE, FILE_SHARE_NONE, OPEN_EXISTING, - }; +fn _cm_get_string_property(dev_inst:u32, key:&DEVPROPKEY, property_value:&mut Vec) -> anyhow::Result { + let mut property_type: DEVPROPTYPE = DEVPROPTYPE::default(); - #[test] - fn test() { - let a = PCSTR::from_raw("\\\\.\\ovpn-dco\0".as_ptr()); + let mut property_value_length = 0; - let a = unsafe { - CreateFileA( - a, - FILE_GENERIC_READ | FILE_GENERIC_WRITE, - FILE_SHARE_NONE, - None, - OPEN_EXISTING, - FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, - None, - ) - } - .unwrap(); - println!("finish...."); + unsafe { + CM_Get_DevNode_PropertyW( + dev_inst, + key, + &mut property_type, + Some(buf.as_mut_ptr()), + &mut property_value_length, + 0, + ); } -} + if property_value_length > 0 { + unsafe { + property_value.set_len(property_value_length as usize); + }; + + let value = unsafe { + PCWSTR::from_raw(property_value.as_mut_ptr().cast::()).to_string() + }; + return Ok(value?); + + } + bail!("can not read device property key {:?}", key) +} \ No newline at end of file diff --git a/ForTunCli/src/lib.rs b/ForTunCli/src/lib.rs index d32d12e..a2fbb40 100644 --- a/ForTunCli/src/lib.rs +++ b/ForTunCli/src/lib.rs @@ -62,6 +62,7 @@ impl Drop for WriteFile { pub type TunSocket = (ReadFile, WriteFile, AdapterDevice); pub fn create_async_tun(device_id: &GUID, name: &str) -> anyhow::Result { + //TODO: inf_path change to compile path let device = init_device(device_id, name, "C:/DriverTest/Drivers/ForTun.inf")?; let file = device.start_adapter()?; let file = match WinOverlappedFile::new(file) { diff --git a/ForTunCli/src/overlapped_file.rs b/ForTunCli/src/overlapped_file.rs index dee85b3..54b0747 100644 --- a/ForTunCli/src/overlapped_file.rs +++ b/ForTunCli/src/overlapped_file.rs @@ -99,8 +99,7 @@ impl WinOverlappedFile { let r = unsafe { WriteFile( self.file, - Some(buf.as_ptr() as _), - buf.len() as u32, + Some(buf), Some(&mut size), Some(overlapped), ) diff --git a/README.md b/README.md index 543da0e..5348e9e 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,26 @@ -# 【WIP】simple-windows-tun +# simple-windows-tun Very simple windows tun, base on NetAdapterCx. -This is used for [ForNet](https://github.com/ForNetCode/fornet), It now passd simple test case in Windows 11 X86 Virtual Machine. But It has lots of job to do. +This is used for [ForNet](https://github.com/ForNetCode/fornet), It now passed simple test case in Windows 11 X86 Virtual Machine. But It has lots of job to do. -- [ ] Get a company to buy code sign certifact. +- [ ] Get a company to buy code sign. - [ ] Pass Windows Test. -- [ ] Release the signed driver files at Github Rlease. +- [ ] Release the signed driver files at GitHub Release. +- [ ] Release ForTunCli To crate. +## How to Use it +1. Download the signed driver from the release page. +2. run command `cargo add simple-windows-tun` +The Below is an example code: + +```rust + +``` ## Support Platform -Newer than Windows 10, version 2004. +Windows Version: Newer than Windows 10, version 2004. + Architecture Platform: x86_64. -We may support aarch64 later. +We may support `aarch64` later.