Skip to content

Commit

Permalink
Merge pull request #2193 from input-output-hk/trusted-peers-dns-1
Browse files Browse the repository at this point in the history
DNS for trusted peers
  • Loading branch information
NicolasDP authored May 13, 2020
2 parents aea40ec + 5990e64 commit 1747395
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions doc/configuration/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ rest:
## P2P configuration

- `trusted_peers`: (optional) the list of nodes' [multiaddr][multiaddr] to connect to in order to
bootstrap the p2p topology (and bootstrap our local blockchain) with the associated `id` (24 bytes
in hexadecimal given by the trusted peers to allow initial connection to it).
bootstrap the p2p topology (and bootstrap our local blockchain). Note that you can use a DNS
name in the following format: `/dns4/node.example.com/tcp/3000`. Use `dns6` instead of `dns4`
if you want the peer to connect with IPv6.
- `public_address`: [multiaddr][multiaddr] the address to listen from and accept connection
from. This is the public address that will be distributed to other peers
of the network that may find interest into participating to the blockchain
Expand Down
75 changes: 59 additions & 16 deletions jormungandr-lib/src/multiaddr.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,67 @@
use multiaddr::{AddrComponent, Multiaddr};
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
use multiaddr::{AddrComponent, Multiaddr, ToMultiaddr};
use std::{
io,
net::{SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs},
};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
#[error("Not enough components in multiaddr")]
InvalidMultiaddr,
#[error("Cannot find DNS record")]
NotFound(#[source] io::Error),
#[error("No IPv4 address found")]
NoIP4,
#[error("No IPv6 address found")]
NoIP6,
}

pub fn multiaddr_to_socket_addr(addr: &Multiaddr) -> Option<SocketAddr> {
let mut components = addr.iter();

match components.next()? {
AddrComponent::IP4(ipv4) => {
if let AddrComponent::TCP(port) = components.next()? {
Some(SocketAddr::V4(SocketAddrV4::new(ipv4, port)))
} else {
None
}
}
AddrComponent::IP6(ipv6) => {
if let AddrComponent::TCP(port) = components.next()? {
Some(SocketAddr::V6(SocketAddrV6::new(ipv6, port, 0, 0)))
let ip = components.next()?;
let port = if let AddrComponent::TCP(port) = components.next()? {
port
} else {
return None;
};

match ip {
AddrComponent::IP4(ipv4) => Some(SocketAddr::V4(SocketAddrV4::new(ipv4, port))),
AddrComponent::IP6(ipv6) => Some(SocketAddr::V6(SocketAddrV6::new(ipv6, port, 0, 0))),
_ => None,
}
}

pub fn multiaddr_resolve_dns(addr: &Multiaddr) -> Result<Option<Multiaddr>, Error> {
let mut components = addr.iter();

let ip_or_fqdn = components.next().ok_or(Error::InvalidMultiaddr)?;
let port = components
.next()
.and_then(|ac| {
if let AddrComponent::TCP(port) = ac {
Some(port)
} else {
None
}
}
_ => None,
}
})
.ok_or(Error::InvalidMultiaddr)?;

let socket_addr = match ip_or_fqdn {
AddrComponent::DNS4(fqdn) => (fqdn.as_str(), port)
.to_socket_addrs()
.map_err(Error::NotFound)?
.find(|addr| matches!(addr, SocketAddr::V4(_)))
.ok_or(Error::NoIP4)?,
AddrComponent::DNS6(fqdn) => (fqdn.as_str(), port)
.to_socket_addrs()
.map_err(Error::NotFound)?
.find(|addr| matches!(addr, SocketAddr::V6(_)))
.ok_or(Error::NoIP6)?,
_ => return Ok(None),
};

Ok(Some(socket_addr.to_multiaddr().unwrap()))
}
1 change: 1 addition & 0 deletions jormungandr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ async-trait = "0.1"
lru = "^0.4.3"
warp = { version = "0.2.2", features = ["tls"] }
pin-project = "0.4"
multiaddr = "0.3.1"

[target.'cfg(not(target_os = "android"))'.dependencies.reqwest]
version = "0.10.4"
Expand Down
24 changes: 23 additions & 1 deletion jormungandr/src/settings/start/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ fn generate_network(
config: &Option<Config>,
logger: &Logger,
) -> Result<network::Configuration, Error> {
use jormungandr_lib::multiaddr::multiaddr_to_socket_addr;
use jormungandr_lib::multiaddr::{multiaddr_resolve_dns, multiaddr_to_socket_addr};

let (mut p2p, http_fetch_block0_service, skip_bootstrap, bootstrap_from_trusted_peers) =
if let Some(cfg) = config {
Expand All @@ -218,6 +218,28 @@ fn generate_network(
p2p.trusted_peers = Some(command_arguments.trusted_peer.clone())
}

p2p.trusted_peers.as_mut().map(|peers| {
*peers = peers.iter().filter_map(|peer| {
match multiaddr_resolve_dns(peer.address.multi_address()) {
Ok(Some(address)) => {
info!(logger, "DNS resolved"; "fqdn" => peer.address.multi_address().to_string(), "resolved" => address.to_string());
let address = poldercast::Address::from(address);
Some(config::TrustedPeer {
address,
id: peer.id.clone(),
})
}
Ok(None) => {
Some(peer.clone())
},
Err(e) => {
warn!(logger, "failed to resolve dns"; "fqdn" => peer.address.multi_address().to_string(), "error" => e.to_string());
return None;
}
}
}).collect();
});

let mut profile = poldercast::NodeProfileBuilder::new();

if let Some(address) = p2p.public_address {
Expand Down

0 comments on commit 1747395

Please sign in to comment.