Skip to content

Commit

Permalink
fix: ✨ show friendly name of the network interface on windows (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrzlgnm authored Mar 16, 2024
1 parent 71834c3 commit 98698a5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tauri-plugin-log = { git = "https://github.com/tauri-apps/plugins-workspace", br
tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
log = "^0.4.21"
if-addrs = "0.11.1"
network-interface = "1.1.1"

[features]
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!
Expand Down
38 changes: 31 additions & 7 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
use if_addrs::get_if_addrs;
use log::LevelFilter;
use mdns_sd::{IfKind, ServiceDaemon, ServiceEvent};
use network_interface::{NetworkInterface, NetworkInterfaceConfig};
use serde::Serialize;
use std::{
borrow::Borrow,
collections::HashMap,
net::IpAddr,
sync::{Arc, Mutex},
Expand Down Expand Up @@ -131,13 +133,24 @@ fn enum_service_types(state: State<Daemon>) -> Vec<String> {
found
}

fn get_all_interface_names_except_loopback() -> Vec<String> {
fn get_all_interface_names_except_loopback() -> Vec<(String, String)> {
let ifaces = get_if_addrs().unwrap();

// if_addrs unfortunately shows the GUID of the interface as name,
// so we workaround here by using network_interface in addition, as mdns_sd expects name from
// if_addrs
let nwifs = NetworkInterface::show().unwrap();
let mut index_to_name = HashMap::new();
for nwif in nwifs.iter() {
index_to_name.insert(nwif.index, nwif.name.clone());
}
ifaces
.into_iter()
.filter(|itf| !itf.is_loopback())
.map(|itf| itf.name)
.map(|itf| {
let idx = itf.index.unwrap();
(itf.name, index_to_name.get(&idx).unwrap().clone())
})
.collect()
}

Expand All @@ -147,23 +160,34 @@ fn get_interfaces() -> Vec<String> {

log::debug!("Got interfacs: {:#?}", itfs);

itfs
itfs.into_iter().map(|i| i.1).collect()
}

#[tauri::command]
fn set_interfaces(itfs: Vec<String>, state: State<Daemon>) {
if let Ok(mdns) = state.shared.lock() {
let itfs_to_disable = get_all_interface_names_except_loopback()
let all_ifpairs = get_all_interface_names_except_loopback();

let itfs_to_disable = all_ifpairs
.clone()
.into_iter()
.filter(|p| !itfs.contains(&p.1))
.map(|p| p.0)
.collect::<Vec<_>>();

let itfs_to_enable = all_ifpairs
.into_iter()
.filter(|itf| !itfs.contains(itf));
.filter(|p| itfs.contains(&p.1))
.map(|p| p.0)
.collect::<Vec<_>>();

log::debug!(
"Enabling interfaces: {:#?}, disabling interfaces {:#?}",
itfs,
itfs_to_enable,
itfs_to_disable
);
mdns.enable_interface(
itfs.clone()
itfs_to_enable
.into_iter()
.map(IfKind::Name)
.collect::<Vec<_>>(),
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"package": {
"productName": "mdns-browser",
"version": "0.2.0"
"version": "0.2.1"
},
"tauri": {
"allowlist": {
Expand Down

0 comments on commit 98698a5

Please sign in to comment.