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

Support binding to a local IP address or interface #307

Merged
merged 4 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 15 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ memchr = "2.4.1"
mime = "0.3.16"
mime2ext = "0.1.0"
mime_guess = "2.0"
network-interface = "1.0.0"
once_cell = "1.8.0"
os_display = "0.1.3"
pem = "0.8.2"
Expand Down
22 changes: 14 additions & 8 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,6 @@ Example: --print=Hb"
#[clap(short = 'P', long, value_name = "FORMAT")]
pub history_print: Option<Print>,

/// Resolve hostname to ipv4 addresses only.
#[clap(short = '4', long)]
pub ipv4: bool,

/// Resolve hostname to ipv6 addresses only.
#[clap(short = '6', long)]
pub ipv6: bool,

/// Do not print to stdout or stderr.
#[clap(short = 'q', long)]
pub quiet: bool,
Expand Down Expand Up @@ -318,6 +310,20 @@ Example: --print=Hb"
)]
pub http_version: Option<HttpVersion>,

/// Bind to a network interface or local IP address.
///
/// Example: --interface=eth0 --interface=192.168.0.2
#[clap(long, value_name = "NAME")]
pub interface: Option<String>,

/// Resolve hostname to ipv4 addresses only.
#[clap(short = '4', long)]
pub ipv4: bool,

/// Resolve hostname to ipv6 addresses only.
#[clap(short = '6', long)]
pub ipv6: bool,

/// Do not attempt to read stdin.
///
/// This disables the default behaviour of reading the request body from stdin
Expand Down
25 changes: 25 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use std::sync::Arc;

use anyhow::{anyhow, Context, Result};
use atty::Stream;
use network_interface::{NetworkInterface, NetworkInterfaceConfig};
use redirect::RedirectFollower;
use reqwest::blocking::Client;
use reqwest::header::{
Expand Down Expand Up @@ -318,6 +319,30 @@ fn run(args: Cli) -> Result<i32> {
_ => client,
};

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)
} 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
})
};

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

let client = client.build()?;

let mut session = match &args.session {
Expand Down
5 changes: 5 additions & 0 deletions src/to_curl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ pub fn translate(args: Cli) -> Result<Command> {
_ => (),
};

if let Some(interface) = args.interface {
cmd.arg("--interface");
cmd.arg(interface);
};

// Payload
for (header, value) in headers.iter() {
cmd.opt("-H", "--header");
Expand Down