11

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.

Vicary
  • 953
  • 13
  • 32

3 Answers3

11

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.

jpillora
  • 4,885
  • 2
  • 41
  • 55
  • 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(). – Vicary Jul 11 '14 at 05:17
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.

Laurent Perrin
  • 13,603
  • 4
  • 45
  • 48
  • 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. :D – Vicary Nov 29 '12 at 17:50
  • It is indeed possible to manually specify routes. What OS are you targeting ? – Laurent Perrin Nov 29 '12 at 17:58
  • My case is CentOS and OSX (Darwin), but more may come, mostly unix-like. – Vicary Nov 29 '12 at 18:02
5

You can use node cURL wrapper

curl = require('node-curl')
curl('www.google.com', { INTERFACE: 'eth1', RAW: 1 }, function(err) {
    console.info(this);
});
mak
  • 12,357
  • 5
  • 37
  • 46
  • 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. – Vicary Nov 29 '12 at 17:47