Skip to content

Commit

Permalink
disable interface name binding if not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
ducaale committed Oct 21, 2023
1 parent c45cc0b commit f345f5c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
20 changes: 3 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use std::sync::Arc;
use anyhow::{anyhow, Context, Result};
use atty::Stream;
use cookie_store::{CookieStore, RawCookie};
use network_interface::{NetworkInterface, NetworkInterfaceConfig};
use redirect::RedirectFollower;
use reqwest::blocking::Client;
use reqwest::header::{
Expand All @@ -45,7 +44,7 @@ use crate::middleware::ClientWithMiddleware;
use crate::printer::Printer;
use crate::request_items::{Body, FORM_CONTENT_TYPE, JSON_ACCEPT, JSON_CONTENT_TYPE};
use crate::session::Session;
use crate::utils::{test_mode, test_pretend_term, url_with_query};
use crate::utils::{interface_name_to_ip, test_mode, test_pretend_term, url_with_query};
use crate::vendored::reqwest_cookie_store;

#[cfg(not(any(feature = "native-tls", feature = "rustls")))]
Expand Down Expand Up @@ -95,10 +94,7 @@ fn run(args: Cli) -> Result<i32> {
return Ok(0);
}

let warn = {
let bin_name = &args.bin_name;
move |msg| eprintln!("{}: warning: {}", bin_name, msg)
};
let warn = |msg| eprintln!("{}: warning: {}", args.bin_name, msg);

let (mut headers, headers_to_unset) = args.request_items.headers()?;
let url = url_with_query(args.url, &args.request_items.query()?);
Expand Down Expand Up @@ -289,17 +285,7 @@ fn run(args: Cli) -> Result<i32> {
let ip_addr = if let Ok(ip_addr) = IpAddr::from_str(name_or_ip) {
Some(ip_addr)
} else {
// 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());
}
}
None
})
interface_name_to_ip(&name_or_ip)?

Check failure on line 288 in src/main.rs

View workflow job for this annotation

GitHub Actions / Rustfmt and clippy

this expression creates a reference which is immediately dereferenced by the compiler
};

if let Some(ip_addr) = ip_addr {
Expand Down
35 changes: 35 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::borrow::Cow;
use std::env::var_os;
use std::io::{self, Write};
use std::net::IpAddr;
use std::path::{Path, PathBuf};

use anyhow::Result;
Expand Down Expand Up @@ -171,3 +172,37 @@ pub fn copy_largebuf(
}
}
}

#[cfg(any(
target_os = "linux",
target_os = "macos",
target_os = "windows",
target_os = "android"
))]
pub fn interface_name_to_ip(name: &str) -> Result<Option<IpAddr>> {
use network_interface::{NetworkInterface, NetworkInterfaceConfig};
// 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()?;
Ok(network_interfaces.iter().find_map(|interface| {
if &interface.name == name {

Check failure on line 188 in src/utils.rs

View workflow job for this annotation

GitHub Actions / Rustfmt and clippy

needlessly taken reference of left operand
if let Some(addr) = interface.addr.first() {
return Some(addr.ip());
}
}
None
}))
}

#[cfg(not(any(
target_os = "linux",
target_os = "macos",
target_os = "windows",
target_os = "android"
)))]
pub fn interface_name_to_ip(_name: &str) -> Result<Option<IpAddr>> {
Err(anyhow::anyhow!(
"binding to interface name is not supported in {}",
std::env::consts::OS
))
}

0 comments on commit f345f5c

Please sign in to comment.