-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add new NetIf variant to NatResolver for IP resolution via network interface #10922
base: main
Are you sure you want to change the base?
Changes from 2 commits
43343a6
af17461
2b2acc0
7035ef0
422849c
81da7df
7896295
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ use std::{ | |
time::Duration, | ||
}; | ||
|
||
use crate::net_if::resolve_net_if_ip; | ||
#[cfg(feature = "serde")] | ||
use serde_with::{DeserializeFromStr, SerializeDisplay}; | ||
|
||
|
@@ -48,6 +49,9 @@ pub enum NatResolver { | |
PublicIp, | ||
/// Use the given [`IpAddr`] | ||
ExternalIp(IpAddr), | ||
/// Resolve external IP via the network interface. | ||
#[cfg(not(target_os = "windows"))] | ||
garwahl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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("networkinterface"), | ||
garwahl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Self::None => f.write_str("none"), | ||
} | ||
} | ||
|
@@ -185,6 +190,7 @@ 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 => resolve_net_if_ip(DEFAULT_NET_IF_NAME).ok(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @emhane I've decided to return Don't think we should be changing the signature of this method to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. defo think sig should change. becomes hard to use this tool otherwise if error isn't printed to log. the error could be logged here too, but it's wrong to use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah think I'd much rather log an error here or lower, than force consumers of Similar reasoning to how There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ye, would like it to panic already in cli as well if the error is logged, so if none is returned when the net if ip resolution flag is passed please open an issue for |
||
NatResolver::None => None, | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to not feature gate this variant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it only works on non-windows
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.rs/if-addrs/0.13.3/if_addrs/fn.get_if_addrs.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.rs/if-addrs/0.13.3/x86_64-pc-windows-msvc/if_addrs/fn.get_if_addrs.html
this also exists for windows,