forked from domo-iot/libp2p-rust-dht
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06a96b8
commit 786ac5f
Showing
4 changed files
with
47 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,35 @@ | ||
use std::time::{SystemTime, UNIX_EPOCH}; | ||
use local_ip_address::list_afinet_netifas; | ||
use std::net::IpAddr; | ||
|
||
pub fn get_epoch_ms() -> u128 { | ||
SystemTime::now() | ||
.duration_since(UNIX_EPOCH) | ||
.unwrap() | ||
.as_millis() | ||
} | ||
|
||
pub fn get_addresses_of_local_interfaces() -> Vec<String> { | ||
let network_interfaces = list_afinet_netifas().unwrap(); | ||
|
||
let mut to_ret = Vec::new(); | ||
|
||
for (_name, ip) in network_interfaces.iter() { | ||
if ip.to_string() != "127.0.0.1" && matches!(ip, IpAddr::V4(_)) { | ||
to_ret.push(ip.to_string()); | ||
} | ||
} | ||
|
||
to_ret | ||
} | ||
|
||
pub fn is_local_peer(multiaddr: &str) -> bool { | ||
let my_addresses = get_addresses_of_local_interfaces(); | ||
log::info!("My addresses {:?}", my_addresses); | ||
for ip in my_addresses { | ||
if multiaddr.to_string().contains(&ip) { | ||
return true; | ||
} | ||
} | ||
false | ||
} |