Make a public function to parse peer addresses

This commit is contained in:
Alex 2021-10-14 11:58:09 +02:00
parent d62b161040
commit 7e49d0dac8
No known key found for this signature in database
GPG key ID: EDABF9711E244EB1
3 changed files with 16 additions and 15 deletions

View file

@ -1,5 +1,4 @@
use std::io::Write; use std::io::Write;
use std::net::SocketAddr;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
@ -17,6 +16,7 @@ use tokio::sync::watch;
use netapp::endpoint::*; use netapp::endpoint::*;
use netapp::peering::basalt::*; use netapp::peering::basalt::*;
use netapp::proto::*; use netapp::proto::*;
use netapp::util::parse_peer_addr;
use netapp::{NetApp, NodeID}; use netapp::{NetApp, NodeID};
#[derive(StructOpt, Debug)] #[derive(StructOpt, Debug)]
@ -98,12 +98,7 @@ async fn main() {
let mut bootstrap_peers = vec![]; let mut bootstrap_peers = vec![];
for peer in opt.bootstrap_peers.iter() { for peer in opt.bootstrap_peers.iter() {
if let Some(delim) = peer.find('@') { bootstrap_peers.push(parse_peer_addr(peer).expect("Invalid peer address"));
let (key, ip) = peer.split_at(delim);
let pubkey = ed25519::PublicKey::from_slice(&hex::decode(&key).unwrap()).unwrap();
let ip = ip[1..].parse::<SocketAddr>().unwrap();
bootstrap_peers.push((pubkey, ip));
}
} }
let basalt_params = BasaltParams { let basalt_params = BasaltParams {

View file

@ -1,5 +1,4 @@
use std::io::Write; use std::io::Write;
use std::net::SocketAddr;
use log::info; use log::info;
@ -9,8 +8,8 @@ use sodiumoxide::crypto::auth;
use sodiumoxide::crypto::sign::ed25519; use sodiumoxide::crypto::sign::ed25519;
use netapp::peering::fullmesh::*; use netapp::peering::fullmesh::*;
use netapp::util::*;
use netapp::NetApp; use netapp::NetApp;
use netapp::NodeID;
#[derive(StructOpt, Debug)] #[derive(StructOpt, Debug)]
#[structopt(name = "netapp")] #[structopt(name = "netapp")]
@ -70,12 +69,7 @@ async fn main() {
let mut bootstrap_peers = vec![]; let mut bootstrap_peers = vec![];
for peer in opt.bootstrap_peers.iter() { for peer in opt.bootstrap_peers.iter() {
if let Some(delim) = peer.find('@') { bootstrap_peers.push(parse_peer_addr(peer).expect("Invalid peer address"));
let (key, ip) = peer.split_at(delim);
let pubkey = NodeID::from_slice(&hex::decode(&key).unwrap()).unwrap();
let ip = ip[1..].parse::<SocketAddr>().unwrap();
bootstrap_peers.push((pubkey, ip));
}
} }
let peering = FullMeshPeeringStrategy::new(netapp.clone(), bootstrap_peers); let peering = FullMeshPeeringStrategy::new(netapp.clone(), bootstrap_peers);

View file

@ -1,3 +1,5 @@
use std::net::SocketAddr;
use serde::Serialize; use serde::Serialize;
use log::info; use log::info;
@ -56,3 +58,13 @@ pub fn watch_ctrl_c() -> watch::Receiver<bool> {
}); });
watch_cancel 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))
}