Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
feat(config): read listen and external addresses from config
Browse files Browse the repository at this point in the history
- Read listen addresses from `config.addresses.swarm`.
- Read external addresses from `config.addresses.announce`.
- Ignore IPv6 QUIC addresses due to libp2p/rust-libp2p#4165.
  • Loading branch information
mxinden committed Jul 10, 2023
1 parent 424b823 commit 6476dc1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 22 deletions.
9 changes: 9 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use libp2p::Multiaddr;
use serde_derive::Deserialize;
use std::error::Error;
use std::path::Path;
Expand All @@ -6,6 +7,7 @@ use std::path::Path;
#[serde(rename_all = "PascalCase")]
pub struct Config {
pub identity: Identity,
pub addresses: Addresses,
}

impl Config {
Expand All @@ -22,6 +24,13 @@ pub struct Identity {
pub priv_key: String,
}

#[derive(Clone, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Addresses {
pub swarm: Vec<Multiaddr>,
pub announce: Vec<Multiaddr>,
}

impl zeroize::Zeroize for Config {
fn zeroize(&mut self) {
self.identity.peer_id.zeroize();
Expand Down
78 changes: 56 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ use libp2p::identity;
use libp2p::identity::PeerId;
use libp2p::kad;
use libp2p::metrics::{Metrics, Recorder};
use libp2p::multiaddr::Protocol;
use libp2p::noise;
use libp2p::swarm::{SwarmBuilder, SwarmEvent};
use libp2p::tcp;
use libp2p::yamux;
use libp2p::Multiaddr;
use libp2p::Transport;
use log::{debug, info};
use prometheus_client::metrics::info::Info;
Expand All @@ -40,7 +42,7 @@ const BOOTSTRAP_INTERVAL: Duration = Duration::from_secs(5 * 60);
struct Opt {
/// Path to IPFS config file.
#[structopt(long)]
config: Option<PathBuf>,
config: PathBuf,

/// Metric endpoint path.
#[structopt(long, default_value = "/metrics")]
Expand All @@ -60,28 +62,22 @@ fn main() -> Result<(), Box<dyn Error>> {

let opt = Opt::from_args();

let (local_peer_id, local_keypair) = match &opt.config {
Some(path) => {
let config = Zeroizing::new(config::Config::from_file(path.as_path())?);
let config = Zeroizing::new(config::Config::from_file(opt.config.as_path())?);

let keypair = identity::Keypair::from_protobuf_encoding(&Zeroizing::new(
base64::engine::general_purpose::STANDARD
.decode(config.identity.priv_key.as_bytes())?,
))?;
let (local_peer_id, local_keypair) = {
let keypair = identity::Keypair::from_protobuf_encoding(&Zeroizing::new(
base64::engine::general_purpose::STANDARD
.decode(config.identity.priv_key.as_bytes())?,
))?;

let peer_id = keypair.public().into();
assert_eq!(
PeerId::from_str(&config.identity.peer_id)?,
peer_id,
"Expect peer id derived from private key and peer id retrieved from config to match."
);
let peer_id = keypair.public().into();
assert_eq!(
PeerId::from_str(&config.identity.peer_id)?,
peer_id,
"Expect peer id derived from private key and peer id retrieved from config to match."
);

(peer_id, keypair)
}
None => {
let keypair = identity::Keypair::generate_ed25519();
(keypair.public().into(), keypair)
}
(peer_id, keypair)
};
println!("Local peer id: {local_peer_id:?}");

Expand Down Expand Up @@ -118,8 +114,26 @@ fn main() -> Result<(), Box<dyn Error>> {
);
let mut swarm =
SwarmBuilder::with_async_std_executor(transport, behaviour, local_peer_id).build();
swarm.listen_on("/ip4/0.0.0.0/tcp/4001".parse()?)?;
swarm.listen_on("/ip4/0.0.0.0/udp/4001/quic-v1".parse()?)?;

if config.addresses.swarm.is_empty() {
log::warn!("No listen addresses configured.");
}
for address in config.addresses.swarm.iter().filter(filter_out_ipv6_quic) {
println!("{address:?}");
swarm.listen_on(address.clone())?;
}
if config.addresses.announce.is_empty() {
log::warn!("No external addresses configured.");
}
for address in config
.addresses
.announce
.iter()
.filter(filter_out_ipv6_quic)
{
println!("external address {address:?}");
swarm.add_external_address(address.clone())
}

let mut metric_registry = Registry::default();
let metrics = Metrics::new(&mut metric_registry);
Expand Down Expand Up @@ -198,3 +212,23 @@ fn main() -> Result<(), Box<dyn Error>> {
}
})
}

fn filter_out_ipv6_quic(m: &&Multiaddr) -> bool {
let mut iter = m.iter();

let is_ipv6_quic = matches!(
(iter.next(), iter.next(), iter.next()),
(
Some(Protocol::Ip6(_)),
Some(Protocol::Udp(_)),
Some(Protocol::Quic | Protocol::QuicV1)
)
);

if is_ipv6_quic {
log::warn!("Ignoring IPv6 QUIC address {m}. Currently unsupported. See https://github.com/libp2p/rust-libp2p/issues/4165.");
return false;
}

true
}

0 comments on commit 6476dc1

Please sign in to comment.