Skip to content

Commit

Permalink
Make network-interface an optional dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
blyxxyz committed Oct 21, 2023
1 parent c45cc0b commit a159996
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ memchr = "2.4.1"
mime = "0.3.16"
mime2ext = "0.1.0"
mime_guess = "2.0"
network-interface = "1.0.0"
network-interface = { version = "1.0.0", optional = true }
once_cell = "1.8.0"
os_display = "0.1.3"
pem = "0.8.2"
Expand Down Expand Up @@ -74,9 +74,12 @@ tokio = { version = "1", features = ["rt", "sync", "time"] }
tempfile = "3.2.0"

[features]
default = ["online-tests", "rustls"]
# Ideally network-interface would be disabled by default on certain platforms
# However: https://github.com/rust-lang/cargo/issues/1197
default = ["online-tests", "rustls", "network-interface"]
native-tls = ["reqwest/native-tls", "reqwest/native-tls-alpn"]
rustls = ["reqwest/rustls-tls", "reqwest/rustls-tls-webpki-roots", "reqwest/rustls-tls-native-roots"]
network-interface = ["dep:network-interface"]

online-tests = []
ipv6-tests = []
Expand Down
33 changes: 19 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use std::sync::Arc;
use anyhow::{anyhow, Context, Result};
use atty::Stream;
use cookie_store::{CookieStore, RawCookie};
#[cfg(feature = "network-interface")]
use network_interface::{NetworkInterface, NetworkInterfaceConfig};
use redirect::RedirectFollower;
use reqwest::blocking::Client;
Expand Down Expand Up @@ -287,26 +288,30 @@ fn run(args: Cli) -> Result<i32> {

if let Some(name_or_ip) = &args.interface {
let ip_addr = if let Ok(ip_addr) = IpAddr::from_str(name_or_ip) {
Some(ip_addr)
ip_addr
} else {
#[cfg(not(feature = "network-interface"))]
return Err(anyhow!(
"This binary was built without support for binding to interfaces. Enable the `network-interface` feature."
));

#[cfg(feature = "network-interface")]
// TODO: Directly bind to interface name once hyper/reqwest adds support for it.
// See https://github.com/seanmonstar/reqwest/issues/1336 and https://github.com/hyperium/hyper/pull/3076
let network_interfaces = NetworkInterface::show()?;
network_interfaces.iter().find_map(|interface| {
if &interface.name == name_or_ip {
if let Some(addr) = interface.addr.first() {
return Some(addr.ip());
NetworkInterface::show()?
.iter()
.find_map(|interface| {
if &interface.name == name_or_ip {
if let Some(addr) = interface.addr.first() {
return Some(addr.ip());
}
}
}
None
})
None
})
.with_context(|| format!("Couldn't bind to {:?}", name_or_ip))?
};

if let Some(ip_addr) = ip_addr {
client = client.local_address(ip_addr);
} else {
return Err(anyhow!("Couldn't bind to {:?}", name_or_ip));
}
client = client.local_address(ip_addr);
}

let client = client.build()?;
Expand Down

0 comments on commit a159996

Please sign in to comment.