grantila / fetch-h2

HTTP/1+2 Fetch API client for Node.js
MIT License
336 stars 16 forks source link

Is there any way to specify localAdress? #51

Open gnostis opened 5 years ago

gnostis commented 5 years ago

It would be great if you could specify the network interface in the options of each instance. Similar to Node localAddress. But maybe I did not notice and there is a way to do this?

grantila commented 5 years ago

You can do this per-context using the session option.

So, if you want this globally for your app, before doing any fetch call, set it up using e.g.:

const { setup, fetch } = require( "fetch-h2" );
setup( { session: { localAddress: "1.2.3.4" } } );
fetch( ... ); // Bound to 1.2.3.4

or you create a new context where fetch requests are explicitly bound to the address:

const { context, fetch } = require( "fetch-h2" );
const { fetch: fetch1234 } = context( { session: { localAddress: "1.2.3.4" } } );
fetch( ... ); // Unspecified interface
fetch1234( ... ); // Bound to 1.2.3.4

I haven't tested this exact usage, please let me know if it works for you. I'll leave this open.

gnostis commented 5 years ago

OK thanks! I will report the results

gnostis commented 5 years ago

Does not work in both cases. Per context or via setup. Ignores and uses the route with the lowest metric. Tested on win64/node 12.2.0, lan, wlan and usb modem interfaces. Simple code with requests to server responding with external IP. I don't see localAddress option mentioned in node http2.connect options, only in http.request options.

grantila commented 5 years ago

I think it might work over https, but these properties aren't forwarded for plain http now that I look at it. I'll fix this as soon as I'm done with a bigger issue - #39

duongvanba commented 4 years ago

Did you fix this issue @grantila ? I really want to use my local address and ipv6 for http2 request, thank you