Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Log an error when failing to resolve external ip address #11085

Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions crates/net/nat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod net_if;

pub use net_if::{NetInterfaceError, DEFAULT_NET_IF_NAME};

use reqwest::StatusCode;
use std::{
fmt,
future::{poll_fn, Future},
Expand All @@ -25,7 +26,7 @@ use std::{
task::{Context, Poll},
time::Duration,
};
use tracing::error;
use tracing::{debug, error};

use crate::net_if::resolve_net_if_ip;
#[cfg(feature = "serde")]
Expand Down Expand Up @@ -191,20 +192,26 @@ 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) {
NatResolver::NetIf => match resolve_net_if_ip(DEFAULT_NET_IF_NAME).inspect_err(|err| {
debug!(target: "net::nat", "Failed to resolve network interface IP: {}", err);
garwahl marked this conversation as resolved.
Show resolved Hide resolved
}) {
Ok(ip) => Some(ip),
Err(err) => {
error!("Failed to resolve network interface IP: {}", err);
None
}
Err(_) => None,
},
NatResolver::None => None,
}
}

async fn resolve_external_ip() -> Option<IpAddr> {
let futures = EXTERNAL_IP_APIS.iter().copied().map(resolve_external_ip_url_res).map(Box::pin);
futures_util::future::select_ok(futures).await.ok().map(|(res, _)| res)
match futures_util::future::select_ok(futures).await.inspect_err(|_| {
garwahl marked this conversation as resolved.
Show resolved Hide resolved
garwahl marked this conversation as resolved.
Show resolved Hide resolved
debug!(target: "net::nat",
external_ip_apis=?EXTERNAL_IP_APIS,
"Failed to resolve external IP from any API");
}) {
Ok((ip, _)) => Some(ip),
Err(_) => None,
}
}

async fn resolve_external_ip_url_res(url: &str) -> Result<IpAddr, ()> {
Expand Down
Loading