forked from lx/netapp
Add async version of parse_and_resolve_peer_addr
This commit is contained in:
parent
8ac109e3a8
commit
1a413eef97
2 changed files with 18 additions and 2 deletions
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "netapp"
|
name = "netapp"
|
||||||
version = "0.5.0"
|
version = "0.5.1"
|
||||||
authors = ["Alex Auvolat <alex@adnab.me>"]
|
authors = ["Alex Auvolat <alex@adnab.me>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license-file = "LICENSE"
|
license-file = "LICENSE"
|
||||||
|
|
18
src/util.rs
18
src/util.rs
|
@ -1,5 +1,4 @@
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::net::ToSocketAddrs;
|
|
||||||
|
|
||||||
use log::info;
|
use log::info;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
@ -71,6 +70,8 @@ pub fn parse_peer_addr(peer: &str) -> Option<(NodeID, SocketAddr)> {
|
||||||
/// Parse and resolve a peer's address including public key, written in the format:
|
/// Parse and resolve a peer's address including public key, written in the format:
|
||||||
/// `<public key hex>@<ip or hostname>:<port>`
|
/// `<public key hex>@<ip or hostname>:<port>`
|
||||||
pub fn parse_and_resolve_peer_addr(peer: &str) -> Option<(NodeID, Vec<SocketAddr>)> {
|
pub fn parse_and_resolve_peer_addr(peer: &str) -> Option<(NodeID, Vec<SocketAddr>)> {
|
||||||
|
use std::net::ToSocketAddrs;
|
||||||
|
|
||||||
let delim = peer.find('@')?;
|
let delim = peer.find('@')?;
|
||||||
let (key, host) = peer.split_at(delim);
|
let (key, host) = peer.split_at(delim);
|
||||||
let pubkey = NodeID::from_slice(&hex::decode(&key).ok()?)?;
|
let pubkey = NodeID::from_slice(&hex::decode(&key).ok()?)?;
|
||||||
|
@ -80,3 +81,18 @@ pub fn parse_and_resolve_peer_addr(peer: &str) -> Option<(NodeID, Vec<SocketAddr
|
||||||
}
|
}
|
||||||
Some((pubkey, hosts))
|
Some((pubkey, hosts))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// async version of parse_and_resolve_peer_addr
|
||||||
|
pub async fn parse_and_resolve_peer_addr_async(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 = tokio::net::lookup_host(&host[1..])
|
||||||
|
.await
|
||||||
|
.ok()?
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
if hosts.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some((pubkey, hosts))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue