forked from lx/netapp
Add parse_and_resolve_peer_addr
This commit is contained in:
parent
e621ba49de
commit
238c0162c0
1 changed files with 14 additions and 0 deletions
14
src/util.rs
14
src/util.rs
|
@ -1,4 +1,5 @@
|
|||
use std::net::SocketAddr;
|
||||
use std::net::ToSocketAddrs;
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
|
@ -72,3 +73,16 @@ pub fn parse_peer_addr(peer: &str) -> Option<(NodeID, SocketAddr)> {
|
|||
let ip = ip[1..].parse::<SocketAddr>().ok()?;
|
||||
Some((pubkey, ip))
|
||||
}
|
||||
|
||||
/// Parse and resolve a peer's address including public key, written in the format:
|
||||
/// `<public key hex>@<ip or hostname>:<port>`
|
||||
pub fn parse_and_resolve_peer_addr(peer: &str) -> Option<(NodeID, Vec<SocketAddr>)> {
|
||||
let delim = peer.find('@')?;
|
||||
let (key, host) = peer.split_at(delim);
|
||||
let pubkey = NodeID::from_slice(&hex::decode(&key).ok()?)?;
|
||||
let hosts = host[1..]
|
||||
.to_socket_addrs()
|
||||
.ok()?
|
||||
.collect::<Vec<_>>();
|
||||
Some((pubkey, hosts))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue