Skip to content

Commit

Permalink
feat(iroh-net): Upnp port mapping (#1117)
Browse files Browse the repository at this point in the history
* add igd

* add log, use types for readability

* request mapping

* fix code placement + comments

* filling in the gaps

* some cleanup and refactor

* debugging

* debug cleanning

* merge cleanup

* easy doctor appointment

* debug cleanning

* use non zero to ensure port to map is positive

* spelling

* follow iroh code style

* log when probe fails

* update comment

* more logs, use localhost

* add portmap metrics to iroh-metrics

* use portmap metrics

* add client

* wip: closing for the day

* wip

* more wip

* connecting stuff

* coding with the heart lol

* fixes

* customize timeout of portmapping doctor command

* on_change for portmap

* improve docs and logs

* add mapping struct that handles maintenance

* add waker

* improve debug logs

* connect with mapping thing

* partially connect current mapping stream

* simplify mapping handling and add a sanity test

* remove hardcoded mapping half life

* remove unused field

* ensure external port is non zero

* simplify test types, reduce test timeout

* add external addr fn to mapping

* rework getting a new upnp mapping to reuse a known gateway and or known external port

* connect expiry and renew events

* split client functions

* update doctor command

* add service channel capacity

* remove unnecesary change

* leverage derive_more and the utils mod

* remove yet one more unnecesary clone

* Revert "remove yet one more unnecesary clone" might be controversial since technically the probe starts before the report

This reverts commit f558246.

* misc spelling

* misc docs

* cleanup

* more cleanup

* simplify logic updating a probe

* appease clippy

* expand comment

* expand metrics

* self review

* improve doctor docs

* unflatten portmap probe

* get interfaces

* get interfaces using dep already here
  • Loading branch information
divagant-martian authored Jul 4, 2023
1 parent b14049e commit 701e9b7
Show file tree
Hide file tree
Showing 13 changed files with 1,172 additions and 148 deletions.
98 changes: 98 additions & 0 deletions Cargo.lock

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

12 changes: 11 additions & 1 deletion iroh-metrics/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use once_cell::sync::Lazy;
use prometheus_client::{encoding::text::encode, registry::Registry};

use crate::{iroh, magicsock, netcheck};
use crate::{iroh, magicsock, netcheck, portmap};

pub static CORE: Lazy<Core> = Lazy::new(Core::default);

Expand All @@ -14,6 +14,7 @@ pub struct Core {
iroh_metrics: iroh::Metrics,
magicsock_metrics: magicsock::Metrics,
netcheck_metrics: netcheck::Metrics,
portmap_metrics: portmap::Metrics,
}

impl Default for Core {
Expand All @@ -24,6 +25,7 @@ impl Default for Core {
iroh_metrics: iroh::Metrics::new(&mut reg),
magicsock_metrics: magicsock::Metrics::new(&mut reg),
netcheck_metrics: netcheck::Metrics::new(&mut reg),
portmap_metrics: portmap::Metrics::new(&mut reg),
registry: reg,
}
}
Expand All @@ -46,6 +48,10 @@ impl Core {
&self.netcheck_metrics
}

pub fn portmap_metrics(&self) -> &portmap::Metrics {
&self.portmap_metrics
}

pub(crate) fn encode(&self) -> Result<String, std::fmt::Error> {
let mut buf = String::new();
encode(&mut buf, self.registry())?;
Expand Down Expand Up @@ -122,6 +128,7 @@ where
Collector::Iroh => CORE.iroh_metrics().record(m, v),
Collector::Magicsock => CORE.magicsock_metrics().record(m, v),
Collector::Netcheck => CORE.netcheck_metrics().record(m, v),
Collector::Portmap => CORE.portmap_metrics().record(m, v),
};
}
}
Expand All @@ -140,6 +147,7 @@ where
Collector::Iroh => CORE.iroh_metrics().observe(m, v),
Collector::Magicsock => CORE.magicsock_metrics().observe(m, v),
Collector::Netcheck => CORE.netcheck_metrics().observe(m, v),
Collector::Portmap => CORE.portmap_metrics().observe(m, v),
};
}
}
Expand All @@ -154,4 +162,6 @@ pub enum Collector {
Magicsock,
/// Netcheck related metrics.
Netcheck,
/// Portmap related metrics.
Portmap,
}
1 change: 1 addition & 0 deletions iroh-metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ mod service;
pub mod iroh;
pub mod magicsock;
pub mod netcheck;
pub mod portmap;
21 changes: 21 additions & 0 deletions iroh-metrics/src/portmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
super::make_metric_recorders! {
Portmap,

/*
* General port mapping metrics
*/
ProbesStarted: Counter: "Number of probing tasks started.",
LocalPortUpdates: Counter: "Number of updates to the local port.",
MappingAttempts: Counter: "Number of mapping tasks started.",
MappingFailures: Counter: "Number of failed mapping tasks",
ExternalAddressUpdated: Counter: "Number of times the external address obtained via port mapping was updated.",

/*
* UPnP metrics
*/
UpnpProbes: Counter: "Number of UPnP probes executed.",
UpnpProbesFailed: Counter: "Number of failed Upnp probes",
UpnpAvailable: Counter: "Number of UPnP probes that found it available.",
UpnpGatewayUpdated: Counter: "Number of UPnP probes that resulted in a gateway different to the previous one.",

}
5 changes: 4 additions & 1 deletion iroh-net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ curve25519-dalek = "=4.0.0-rc.3"
default-net = "0.15"
data-encoding = "2.3.3"
der = { version = "0.7", features = ["alloc", "derive"] }
derive_more = { version = "0.99.17", git = "/~https://github.com/JelteF/derive_more", features = ["debug", "display", "from", "try_into"] }
derive_more = { version = "0.99.17", git = "/~https://github.com/JelteF/derive_more", features = ["debug", "display", "from", "try_into", "deref"] }
ed25519-dalek = { version = "=2.0.0-rc.3", features = ["serde", "rand_core"] }
flume = "0.10.14"
futures = "0.3.25"
Expand All @@ -29,6 +29,7 @@ hostname = "0.3.1"
http = "0.2.9"
hyper = { version = "0.14.25", features = ["server", "client", "http1", "tcp"] }
governor = "0.5.1"
igd = { version = "0.12.1", features = ["aio"] }
libc = "0.2.139"
once_cell = "1.17.0"
os_info = "3.6.0"
Expand All @@ -52,6 +53,7 @@ tokio = { version = "1", features = ["io-util", "sync", "rt", "net", "fs"] }
tokio-util = { version = "0.7", features = ["io-util", "io"] }
tokio-rustls = { version = "0.24" }
tokio-rustls-acme = { version = "0.1" }
tokio-stream = { version = "0.1", features = ["sync"]}
url = { version = "2.4", features = ["serde"] }
webpki = { version = "0.22", features = ["std"] }
webpki-roots = "0.23.0"
Expand Down Expand Up @@ -83,6 +85,7 @@ wmi = "0.13"
clap = { version = "4", features = ["derive"] }
tokio = { version = "1", features = ["io-util", "sync", "rt", "net", "fs", "macros", "time", "test-util"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
ntest = "*"

[build-dependencies]
duct = "0.13.6"
Expand Down
16 changes: 4 additions & 12 deletions iroh-net/src/hp/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
net::{IpAddr, Ipv4Addr, SocketAddr},
};

use super::key;
use super::{key, portmapper};

/// Fake WireGuard endpoint IP address that means to
/// use DERP. When used (in the Node.DERP field), the port number of
Expand Down Expand Up @@ -68,14 +68,8 @@ pub struct NetInfo {
/// Whether we have an existing portmap open (UPnP, PMP, or PCP).
pub have_port_map: bool,

/// Whether UPnP appears present on the LAN. Empty means not checked.
pub upnp: Option<bool>,

/// Whether NAT-PMP appears present on the LAN. Empty means not checked.
pub pmp: Option<bool>,

/// Whether PCP appears present on the LAN. Empty means not checked.
pub pcp: Option<bool>,
/// Probe indicating the presence of port mapping protocols on the LAN.
pub portmap_probe: Option<portmapper::ProbeOutput>,

/// This node's preferred DERP server for incoming traffic. The node might be be temporarily
/// connected to multiple DERP servers (to send to other nodes)
Expand Down Expand Up @@ -104,9 +98,7 @@ impl NetInfo {
&& self.working_udp == other.working_udp
&& self.working_icm_pv4 == other.working_icm_pv4
&& self.have_port_map == other.have_port_map
&& self.upnp == other.upnp
&& self.pmp == other.pmp
&& self.pcp == other.pcp
&& self.portmap_probe == other.portmap_probe
&& self.preferred_derp == other.preferred_derp
&& self.link_type == other.link_type
}
Expand Down
Loading

0 comments on commit 701e9b7

Please sign in to comment.