reqwest icon indicating copy to clipboard operation
reqwest copied to clipboard

Forcing IPv4/IPv6

Open xfbs opened this issue 6 years ago • 12 comments

Is it possible for force IPv4 or IPv6 connections?

xfbs avatar Aug 01 '19 11:08 xfbs

up

anton-dutov avatar Nov 26 '20 14:11 anton-dutov

wow it has been some time since this issue opened with no reply

ta3pks avatar Dec 19 '20 18:12 ta3pks

I'd also love to have an example of this.

Technically using AF_INET6 should be enough, but i couldn't find a struct in the library that i can build my own HttpConnector to create a client or something close to that.

You must be incredibly busy @seanmonstar and you don't have to answer this but it would be awesome to have a minimal example or a little guiding.

ycd avatar Feb 17 '21 20:02 ycd

It's not immediately obvious, but if you specify the local_address option, the connection will use the same IP version as the locally bound address.

seanmonstar avatar Feb 17 '21 23:02 seanmonstar

Thanks for the response, i'm leaving an example for future visitors.

use std::net::IpAddr;
use std::{collections::HashMap, str::FromStr};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = reqwest::Client::builder()
        .local_address(IpAddr::from_str("2001:0db8:85a3:0000:0000:8a2e:0370:7334")?) 
        .build()?;

    let resp = client
        .get("https://httpbin.org/ip")
        .send()
        .await?
        .json::<HashMap<String, String>>()
        .await?;

    println!("{:#?}", resp);
    Ok(())
}

ycd avatar Mar 07 '21 12:03 ycd

I think we can close this @seanmonstar :+1:

ycd avatar Mar 07 '21 12:03 ycd

I think we can close this

please no, i would like to run my code on any machine :) (& force ipv4).

univerz avatar May 07 '21 23:05 univerz

@univerz how to force ipv4 is explained above just add .local_addr("0.0.0.0:0".parse().unwrap()) and you are good to go

ta3pks avatar May 08 '21 13:05 ta3pks

and if you need to force ipv6, just do .local_address("[::]:0".parse().ok()).

redactedontop avatar Jul 09 '24 21:07 redactedontop

Maybe we can use a custom dns_resolver to only resolve IPv4 or IPv6 addresses?

flisky avatar Jul 12 '24 14:07 flisky

Just use the methods me and ta3pks said.

redactedontop avatar Jul 12 '24 18:07 redactedontop

I may be mistaken, but this does not appear to be working with a SOCKS5 proxy. Even when trying to force an IPv4 connection with the method above, it appears to still try the IPv6 address over the proxy.

I have a proxy server I wish to use that does not do DNS by itself and cannot connect through IPv6, but my machine I'm using reqwest from does and can. I don't want to muck with my machine's DNS settings to globally exclude IPv6 connectivity just for this one use case of mine, but other than that, I have no idea how to get this to work.

Edit: it appears just removing IPv6 DNS servers doesn't work. My machine uses systemd-resolved and I think it just uses IPv4 DNS servers over TLS and gets IPv6 addresses from them anyway. Killing any IPv6 connectivity from my machine entirely seems to make it work, but that's really painfully unacceptable lol

Architector4 avatar May 12 '25 20:05 Architector4