How to specify a network interface when making net requests from Node.js?
Asked Answered
H

3

13

In either http.request or net.connect, is there an option that I can specify a network interface to initiate a connection?

EDIT: AFAIK in OS level I can specify address level, or load balancing into routing tables. But the way of interface choosing in my software is more than that, I wanna know if I can do that in codes.

Hawkins answered 28/11, 2012 at 9:42 Comment(0)
T
15

Node has this built in:

http://nodejs.org/api/net.html#net_net_connect_options_connectionlistener

http://nodejs.org/api/http.html#http_http_request_options_callback

See localAddress, just set that to the IP of the interface you'd like to use.

Trafalgar answered 8/1, 2014 at 13:17 Comment(1)
After the time I asked this question, I found this is most likely the best approach. I can group the addresses by os.networkInterfaces().Hawkins
D
6

EDIT: As mak pointed out, it is indeed possible to specify a network interface from a user process. I stand corrected. However, I haven't yet found a module that allows it with node.

By default, the network interface is determined by the OS routing table.

You can list this table with netstat -r on Unix systems (OSX included). Just open a terminal and type the command. You will get a listing like:

laurent ~ $ netstat -r
Routing tables

Internet:
Destination        Gateway            Flags        Refs      Use   Netif Expire
default            192.168.1.1        UGSc          153        0     en0
127                localhost          UCS             0        0     lo0
localhost          localhost          UH              2       42     lo0
...

The Netif field gives you the network interface used for the route. You can also get the interface used to reach a hostname with route:

laurent ~ $ route get google.fr
   route to: par03s02-in-f23.1e100.net
destination: default
       mask: default
    gateway: 192.168.1.1
  interface: en0
      flags: <UP,GATEWAY,DONE,STATIC,PRCLONING>
 recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire
       0         0         0         0         0         0      1500         0 

This is more a serverfault thing, but you can change routes with the route command. For example, this will route traffic to X.Y.Z.[0-254] to X.Y.Z.254 on eth0:

route add -net X.Y.Z.0/24 gw X.Y.Z.254 dev eth0

If you want routes to persist a reboot, you can add them to /etc/network/interfaces. If you want to load balance between several different routes, you should also check MPLS.

Denizen answered 28/11, 2012 at 11:20 Comment(3)
Thanks for the explanation on how to get the info. So how can I specify a route? I will update my question to give more info, what I am asking is a method that can specify with code. I understand it might be a simple NO. :DHawkins
It is indeed possible to manually specify routes. What OS are you targeting ?Denizen
My case is CentOS and OSX (Darwin), but more may come, mostly unix-like.Hawkins
S
5

You can use node cURL wrapper

curl = require('node-curl')
curl('www.google.com', { INTERFACE: 'eth1', RAW: 1 }, function(err) {
    console.info(this);
});
Sere answered 28/11, 2012 at 12:12 Comment(1)
Thanks for the answer, curl is a good way to start. But porting all of my codes into node-curl don't looks like an option. I really want to keep the original net.connect and http.request things.Hawkins

© 2022 - 2024 — McMap. All rights reserved.