1 个不稳定版本
0.1.0 | 2024年6月28日 |
---|
#1799 在 网络编程
30KB
569 行
netwatcher
netwatcher
是一个跨平台Rust库,用于枚举网络接口及其IP地址,具有高效监视这些接口变化的特性。它使用平台特定的方法来检测接口变化,而不是轮询,这意味着您可以更快地发现变化,并且在没有任何活动时没有CPU或唤醒开销。
当前平台支持
平台 | 最小版本 | 列表 | 监视 | 注意 |
---|---|---|---|---|
Windows | - | ✅ | ✅ | |
Mac | 10.14 | ✅ | ✅ | |
Linux | - | ✅ | ✅ | 创建后台线程 |
iOS | 12.0 | ✅ | ✅ | |
Android | - | ✅ | ❌ | 由于隐私限制,Android 11+上的Linux风格的监视失败。替代方案正在开发中。 |
用法
列出接口
/// Returns a HashMap from ifindex (a `u32`) to an `Interface` struct
let interfaces = netwatcher::list_interfaces().unwrap();
for i in interfaces.values() {
println!("interface {} has {} IPs", i.name, i.ips.len());
}
监视接口变化
let handle = netwatcher::watch_interfaces(|update| {
// This callback will fire once immediately with the existing state
// Update includes the latest snapshot of all interfaces
println!("Current interface map: {:#?}", update.interfaces);
// The `UpdateDiff` describes changes since previous callback
// You can choose whether to use the snapshot, diff, or both
println!("ifindexes added: {:?}", update.diff.added);
println!("ifindexes removed: {:?}", update.diff.removed);
for (ifindex, if_diff) in update.diff.modified {
println!("Interface index {} has changed", ifindex);
println!("Added IPs: {:?}", if_diff.addrs_added);
println!("Removed IPs: {:?}", if_diff.addrs_removed);
}
});
// keep `handle` alive as long as you want callbacks
// ...
drop(handle);
许可证
Apache许可证版本2.0 - 请参阅 LICENSE
。
依赖项
~0–36MB
~540K SLoC