mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
Add new NetIf variant to NatResolver for IP resolution via network interface (#10922)
Co-authored-by: garwah <garwah@garwah> Co-authored-by: Emilia Hane <emiliaha95@gmail.com> Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -7547,6 +7547,7 @@ dependencies = [
|
||||
"serde_with",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@ -18,6 +18,7 @@ serde_with = { workspace = true, optional = true }
|
||||
thiserror.workspace = true
|
||||
tokio = { workspace = true, features = ["time"] }
|
||||
if-addrs.workspace = true
|
||||
tracing.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
reth-tracing.workspace = true
|
||||
|
||||
@ -25,7 +25,9 @@ use std::{
|
||||
task::{Context, Poll},
|
||||
time::Duration,
|
||||
};
|
||||
use tracing::error;
|
||||
|
||||
use crate::net_if::resolve_net_if_ip;
|
||||
#[cfg(feature = "serde")]
|
||||
use serde_with::{DeserializeFromStr, SerializeDisplay};
|
||||
|
||||
@ -48,6 +50,8 @@ pub enum NatResolver {
|
||||
PublicIp,
|
||||
/// Use the given [`IpAddr`]
|
||||
ExternalIp(IpAddr),
|
||||
/// Resolve external IP via the network interface.
|
||||
NetIf,
|
||||
/// Resolve nothing
|
||||
None,
|
||||
}
|
||||
@ -66,6 +70,7 @@ impl fmt::Display for NatResolver {
|
||||
Self::Upnp => f.write_str("upnp"),
|
||||
Self::PublicIp => f.write_str("publicip"),
|
||||
Self::ExternalIp(ip) => write!(f, "extip:{ip}"),
|
||||
Self::NetIf => f.write_str("netif"),
|
||||
Self::None => f.write_str("none"),
|
||||
}
|
||||
}
|
||||
@ -91,6 +96,7 @@ impl FromStr for NatResolver {
|
||||
"upnp" => Self::Upnp,
|
||||
"none" => Self::None,
|
||||
"publicip" | "public-ip" => Self::PublicIp,
|
||||
"netif" => Self::NetIf,
|
||||
s => {
|
||||
let Some(ip) = s.strip_prefix("extip:") else {
|
||||
return Err(ParseNatResolverError::UnknownVariant(format!(
|
||||
@ -185,6 +191,13 @@ pub async fn external_addr_with(resolver: NatResolver) -> Option<IpAddr> {
|
||||
match resolver {
|
||||
NatResolver::Any | NatResolver::Upnp | NatResolver::PublicIp => resolve_external_ip().await,
|
||||
NatResolver::ExternalIp(ip) => Some(ip),
|
||||
NatResolver::NetIf => match resolve_net_if_ip(DEFAULT_NET_IF_NAME) {
|
||||
Ok(ip) => Some(ip),
|
||||
Err(err) => {
|
||||
error!("Failed to resolve network interface IP: {}", err);
|
||||
None
|
||||
}
|
||||
},
|
||||
NatResolver::None => None,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user