diff options
author | Alex Auvolat <alex@adnab.me> | 2021-10-14 11:58:09 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2021-10-14 11:58:09 +0200 |
commit | 7e49d0dac847cc2a502038eb512255377ac6238a (patch) | |
tree | 84684b549067b7ad6390e4d1a311d25a0b39d629 /src | |
parent | d62b1610405d5f02b8725862fd4a4f1368bd684b (diff) | |
download | netapp-7e49d0dac847cc2a502038eb512255377ac6238a.tar.gz netapp-7e49d0dac847cc2a502038eb512255377ac6238a.zip |
Make a public function to parse peer addresses
Diffstat (limited to 'src')
-rw-r--r-- | src/util.rs | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs index bf8f4cb..fa2f37a 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,3 +1,5 @@ +use std::net::SocketAddr; + use serde::Serialize; use log::info; @@ -56,3 +58,13 @@ pub fn watch_ctrl_c() -> watch::Receiver<bool> { }); watch_cancel } + +/// Parse a peer's address including public key, written in the format: +/// `<public key hex>@<ip>:<port>` +pub fn parse_peer_addr(peer: &str) -> Option<(NodeID, SocketAddr)> { + let delim = peer.find('@')?; + let (key, ip) = peer.split_at(delim); + let pubkey = NodeID::from_slice(&hex::decode(&key).ok()?)?; + let ip = ip[1..].parse::<SocketAddr>().ok()?; + Some((pubkey, ip)) +} |