Skip to content

Commit

Permalink
feat(nat): use color-eyre and uniform error msg
Browse files Browse the repository at this point in the history
  • Loading branch information
b-zee committed May 15, 2024
1 parent 8ca4f3a commit 09246c2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 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.

1 change: 1 addition & 0 deletions sn_nat_detection/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ path = "src/main.rs"
[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
clap-verbosity-flag = "2.2.0"
color-eyre = { version = "0.6", default-features = false }
futures = "~0.3.13"
libp2p = { version = "0.53", features = ["tokio", "tcp", "noise", "yamux", "autonat", "identify", "macros", "upnp"] }
sn_networking = { path = "../sn_networking", version = "0.15.2" }
Expand Down
8 changes: 4 additions & 4 deletions sn_nat_detection/src/behaviour/upnp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ impl App {
pub(crate) fn on_event_upnp(&mut self, event: upnp::Event) {
match event {
upnp::Event::NewExternalAddr(addr) => {
info!(%addr, "UPnP: New external address detected");
info!(%addr, "Successfully mapped UPnP port");
}
upnp::Event::ExpiredExternalAddr(addr) => {
debug!(%addr, "UPnP: External address expired");
debug!(%addr, "External UPnP port mapping expired");
}
upnp::Event::GatewayNotFound => {
error!("UPnP: No gateway not found");
error!("No UPnP gateway not found");
}
upnp::Event::NonRoutableGateway => {
error!("UPnP: Gateway is not routable");
error!("UPnP gateway is not routable");
}
}
}
Expand Down
22 changes: 12 additions & 10 deletions sn_nat_detection/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
// permissions and limitations relating to use of the SAFE Network Software.

use clap::Parser;
use color_eyre::eyre::{eyre, Result};
use futures::StreamExt;
use libp2p::autonat::NatStatus;
use libp2p::core::{multiaddr::Protocol, Multiaddr};
use libp2p::swarm::SwarmEvent;
use libp2p::{noise, tcp, yamux};
use std::collections::HashSet;
use std::error::Error;
use std::net::Ipv4Addr;
use std::time::Duration;
use tracing::{debug, info, warn};
Expand Down Expand Up @@ -60,7 +60,9 @@ struct Opt {
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
async fn main() -> Result<()> {
color_eyre::install()?;

// Process command line arguments.
let opt = Opt::parse();

Expand Down Expand Up @@ -104,10 +106,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
builder = builder.upnp(true);
running_with_upnp = true;
} else {
break Err("NAT is private".into());
break Err(eyre!("NAT is private"));
}
}
NatStatus::Unknown => break Err("NAT is unknown".into()),
NatStatus::Unknown => break Err(eyre!("NAT is unknown")),
}
}
}
Expand Down Expand Up @@ -180,9 +182,9 @@ impl App {
// `SwarmEvent::Dialing` is only triggered when peer ID is included, so
// we log here too to make sure we log that we're dialing a server.
if let Err(e) = self.swarm.dial(addr.clone()) {
warn!(%addr, ?e, "failed to dial server");
warn!(%addr, ?e, "Failed to dial server");
} else {
info!(%addr, "dialing server");
info!(%addr, "Dialing server");
}
}
}
Expand All @@ -206,7 +208,7 @@ impl App {
info!(
?status,
%confidence,
"confidence in NAT status {}",
"Confidence in NAT status {}",
if confidence > old_confidence {
"increased"
} else {
Expand Down Expand Up @@ -330,7 +332,7 @@ fn parse_peer_addr(addr: &str) -> Result<Multiaddr, &'static str> {
return Ok(addr);
}

Err("could not parse address")
Err("Could not parse address")
}

struct AppBuilder {
Expand Down Expand Up @@ -363,7 +365,7 @@ impl AppBuilder {
self
}

fn build(&self) -> Result<App, Box<dyn Error>> {
fn build(&self) -> Result<App> {
// If no servers are provided, we are in server mode. Conversely, with servers
// provided, we are in client mode.
let client_mode = !self.servers.is_empty();
Expand Down Expand Up @@ -391,7 +393,7 @@ impl AppBuilder {

info!(
peer_id=%swarm.local_peer_id(),
"starting in {} mode",
"Starting in {} mode",
if client_mode { "client" } else { "server" }
);

Expand Down

0 comments on commit 09246c2

Please sign in to comment.