4

I have 2 network interfaces on a MacOS machine: "ZTE Mobile Broadband" and "Ethernet". Both can be used to access the Internet. Is it possible to influence how a specific URL will be requested from node.js? E.g. got('https://ifconfig.co', {/* something to mark traffic to make the OS send this specific request over eth or zte */})?

I know you can add routes to request specific destinations over specific interfaces, and that you can mark traffic from a certain process id and then make all traffic from this process go over specific interface, but what about single requests from a process?

  • 1
    Hi, @user2226755 or Henlo , could you please let me know if almost one of the the two solution proposed in my answer works for you? this way we can possibly formulate the answer more clearly also for those who will have the same problem and will read this thread in the future – Franco Rondini Jan 13 '23 at 18:48
  • Or you can use npm package [node-curl](https://www.npmjs.com/package/node-curl). – Mostafa Fakhraei Jan 17 '23 at 06:51
  • 1
    @FrancoRondini Hi, thank you for your answer. I got a simple tips…: if the mask and network ip does not cross themself we can use the 2 networks in same time! But your two links seem to be interesting if I need to do more (and if I cannot use the mask I want), I will keep this link in mind and dig more if I need more informations about it. – user2226755 Jan 17 '23 at 21:30

2 Answers2

2

@user2226755 for the bounty "Do you know a way to do a HTTP request in node over a specific interface ?"

the "specific" interface is specified in the form of the provided ip address.

you could get it with somthing like

import { networkInterfaces } from 'os';
const interfaces = networkInterfaces();
...

or

const interface_ip = require('os').networkInterfaces()['Ethernet']['address'] // Ethernet being NIC name.

then, like Franco Rondini said, use a custom agent, per this github issue thread.

const http = require('http')
const https = require('https')
const options = { localAddress: interface_ip, /* taken from networkInterfaces() */ /* family: 4 // optional for ipv4, but set to 6 if you use ipv6 */ }
const httpAgent = new http.Agent(options)
const httpsAgent = new https.Agent(options)
axios.get('https://example.com/', { httpAgent, httpsAgent })
// you don't have to use both http and https if you know which of these protocols the website is using

and without axios:

https://stackoverflow.com/a/20996813/4935162

Yarin_007
  • 1,449
  • 1
  • 10
  • 17
1

now I can't test your exact scenario, but you could try setting the IP of the interface you'd like to use into the localAddress option as pointed in this answer

or providing a custom agent as exemplified in this workaround

Franco Rondini
  • 10,841
  • 8
  • 51
  • 77