Skip to content

Commit

Permalink
Delete registry related to network card
Browse files Browse the repository at this point in the history
  • Loading branch information
vnt-dev committed Dec 3, 2024
1 parent bf9f0e5 commit f7d9189
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ windows-sys = { version = "0.59", features = [
"Win32_System_LibraryLoader",
"Win32_Security_WinTrust",
] }
winreg = "0.52.0"

[dev-dependencies]
dotenvy = "0.15"
Expand Down
59 changes: 59 additions & 0 deletions src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,68 @@ impl Adapter {

impl Drop for Adapter {
fn drop(&mut self) {
let name = self.get_name();
//Close adapter on drop
//This is why we need an Arc of wintun
unsafe { self.wintun.WintunCloseAdapter(self.adapter.0) };
self.adapter = UnsafeHandle(ptr::null_mut());
if let Ok(name) = name {
// Delete registry related to network card
_ = delete_reg(&name);
}
}
}
pub fn delete_reg(dev_name: &str) -> std::io::Result<()> {
use winreg::{enums::HKEY_LOCAL_MACHINE, enums::KEY_ALL_ACCESS, RegKey};
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
let profiles_key = hklm.open_subkey_with_flags(
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkList\\Profiles",
KEY_ALL_ACCESS,
)?;

for sub_key_name in profiles_key.enum_keys().filter_map(Result::ok) {
let sub_key = profiles_key.open_subkey(&sub_key_name)?;
match sub_key.get_value::<String, _>("ProfileName") {
Ok(profile_name) => {
if dev_name == profile_name {
match profiles_key.delete_subkey_all(&sub_key_name) {
Ok(_) => {
log::info!("Successfully deleted Profiles sub_key: {}", sub_key_name)
}
Err(e) => {
log::warn!("Failed to delete Profiles sub_key {}: {}", sub_key_name, e)
}
}
}
}
Err(e) => {
log::warn!("Failed to read ProfileName for sub_key {}: {}", sub_key_name, e);
}
}
}
let unmanaged_key = hklm.open_subkey_with_flags(
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkList\\Signatures\\Unmanaged",
KEY_ALL_ACCESS,
)?;
for sub_key_name in unmanaged_key.enum_keys().filter_map(Result::ok) {
let sub_key = unmanaged_key.open_subkey(&sub_key_name)?;
match sub_key.get_value::<String, _>("Description") {
Ok(description) => {
if dev_name == description {
match unmanaged_key.delete_subkey_all(&sub_key_name) {
Ok(_) => {
log::info!("Successfully deleted Unmanaged sub_key: {}", sub_key_name)
}
Err(e) => {
log::warn!("Failed to delete Unmanaged sub_key {}: {}", sub_key_name, e)
}
}
}
}
Err(e) => {
log::warn!("Failed to read Description for sub_key {}: {}", sub_key_name, e);
}
}
}
Ok(())
}

0 comments on commit f7d9189

Please sign in to comment.