How to specify a network interface when making net requests from Node.js? - node.js

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.

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.

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.

You can use node cURL wrapper
curl = require('node-curl')
curl('www.google.com', { INTERFACE: 'eth1', RAW: 1 }, function(err) {
console.info(this);
});

Related

Why linux route config failed with two NIC in redundancy (Primary/secondary)?

I've two nic (eth0/eth1) sending data to a router.
Only one is sending data at a time. I also need a static conf that doesn't change regarding if the NIC are up/down.
If I down eth0, I expect eth1 to make the job.
But destroying eth0 destroy also my default route to the router. So I'm done.
Adding 2 default route seem to work by hand, but I don't want this solution.
So I've tried to add "full" route for eth0 and eth1?
By hand it work but I failed to add them in route-eth0 and route-eth1.
$cat route-eth0
178.12.34.240 via 10.0.10.1 dev eth0
$cat route-eth1
178.12.34.240 via 10.0.10.1 dev eth1
$service network restart
...
Bringing up interface eth1:
Determining IP information for eth1... done.
RTNETLINK answers: File exists
...
$route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
178.12.34.240 10.00.10.1 255.255.255.255 UGH 0 0 0 eth0
...
As you can see route for eth1 failed to be applied. For sure because of the File exist error.
Question:
What is the conf to set to always have correct route whatever the NIC are up or down.
Or another best practice ? What about VIP?
Yes, Ron metric is the answer. But I did it on a static route.
It seems having several default route should be banned.
$cat route-eth0
178.12.34.240 via 10.0.10.1 metric 1 dev eth0
$cat route-eth1
178.12.34.240 via 10.0.10.1 metric 2 dev eth1

Redirect only web requirements through VPN

I have a linux server with Debian and I have set a VPN. This is the route output
10.129.21.229 * 255.255.255.255 UH 0 0 0 tun0
10.129.0.1 10.129.21.229 255.255.255.255 UGH 0 0 0 tun0
167.114.184.0 * 255.255.255.0 U 0 0 0 venet0
default * 0.0.0.0 U 0 0 0 venet0
For yet I can use this command after launched the VPN to pass all the connection for a single ip through the vpn
route add -net 37.187.134.184 netmask 255.255.255.255 dev tun0
but i want to pass through the VPN all the web requirements (all the ip with port 80).
I can't pass all my traffic because if I do that than I couldn't no more connect to my server using SSH.
I can't think of how to do this with a single interface. You could try plumbing up a virtual interface and then do your routing that way.
Basically, create a new interface with something along the lines of
ifconfig eth0:1 10.0.0.10
and then take a look at this page here.
http://www.linuxhorizon.ro/iproute2.html
There is a pretty good walkthrough there. Set up your VPN route over the virtual interface and direct ports 80 and 443 to that. The rest should be able to default route. You can add ports as you find they're needed, like for IM.
-mS

manually set gateway using static routing

For study purpose I want to setup the following networking
A:
10.130.247.92/16(private on eth1)
128.199.227.240(public on eth0)
B:
10.130.237.218/16(private on eth1)
128.199.211.182/18(public on eth0)
And use B as gateway for private route.
in A
root#privte2:~# ip route list
default via 128.199.192.1 dev eth0
10.130.0.0/16 dev eth1 scope link
128.199.192.0/18 dev eth0 scope link
and
root#privte2:~# ip route add 10.130.0.0/16 via 10.130.247.92 dev eth1
RTNETLINK answers: File exists
Can anyone tell me where i did wrong?
I tried something like this before. See if this can help you. First enable the ipforwarding in both the machines.
locate the line containing net.ipv4.ip_forward in the /etc/syscl.conf file and make the following change.
net.ipv4.ip_forward=1
May be you need to restart for this.
Not really sure, Try this route command and see if it works.
Machine>$ route add -net 10.130.0.0 netmask 255.255.255.0 gw 128.199.211.182
For instance see here.

strange routing on one of local network servers

I have 2 local networks (2 offices connected with openvpn)
Network 1:
network 192.168.1.0/24
gateway 192.168.1.101
Network 2:
network 192.168.2.0/24
gateway 192.168.2.10
in network 192.168.1.0/24 I have 2 servers - 192.168.1.180 and 192.168.1.183
first run on Debian 6 and second one on Archlinux (yes, I know it is bad choice for servers). I have no firewall on the servers.
everything works fine, clients from 192.168.2.0 can connect to computers in 192.168.1.0 and back. Except one of servers - 192.168.1.180 (Archlinux). I can't connect to it and can't connect from it to any computer in 192.168.2.0. No ping, no traceroute.
output from 192.168.1.180:
route -n
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.101 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
i run tcpdump on the gateway (192.168.1.101) and ping on 192.168.1.180 and get this:
tcpdump src 192.168.1.180
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
14:23:57.354061 arp who-has 172.19.155.1 tell 192.168.1.180
14:23:58.357354 arp who-has 172.19.155.1 tell 192.168.1.180
...
why it looks for 172.19.155.1? I don't know what this strange IP means. I have no networks like this, no GW, nothing.
if I run ping on all other computer (for example ping from 192.168.1.183) I get:
tcpdump icmp
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
14:29:36.834731 IP 192.168.1.183 > 192.168.2.10: ICMP echo request, id 3216, seq 1, length 64
14:29:36.956211 IP 192.168.2.10 > 192.168.1.183: ICMP echo reply, id 3216, seq 1, length 64
No one looks for this strange IP except 192.168.1.180. I cleaned arp table on 192.168.1.180 but nothing helps.
it looks like you have a default gw configuration exists on your server x.180/24 which points to 172.19.155.1. So, the server assumes all the traffic has to go via 172.19.155.1 and trying to resolve the gw. Look for a default gw configuration in your system, something like this
0.0.0.0 0.0.0.0 172.19.155.1
use ip route xxx
You are able to see the packets in x.101/24 because it is an ARP request, which is broadcast MAC and you will see it on all directly connected devices.
Things I would do/check:
Make absolutely sure that I don't have a second interface going somewhere (ifconfig -a should do it)
I know, it's unlikely... but any chance a routing protocol is enabled on that server? You would see the routes in your routing table, though... which I understand is not the case.
Run a grep on /etc and wherever you have config files for 172.19.55.1
Just my 2c.
I've solved the problem. Not really solved, but at least it works now. The server must work 24/7 but it was my last chance and I rebooted it )) All work was stopped for 30 min but it was worthy of it.
I really don't understand wat happened with it. Some mystic problem.

multicasting on linux

I am testing a simple multicast application on linux. I got it working on my pc by doing the following:
disabling the firewall
running the command route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
if you are interested in the details, see this post
why are these two steps required? is there an alternative.
i am testing the code on localhost.
on another pc I tried running the program after disabling the firewall. it did not work.
when I run the route command I get the following message
route: netmask 0fffffff doesn't make sense with host route
Usage: route [-nNvee] [-FC] [<AF>] List kernel routing tables
route [-v] [-FC] {add|del|flush} ... Modify routing table for AF.
route {-h|--help} [<AF>] Detailed usage syntax for specified AF.
route {-V|--version} Display version/author and exit.
-v, --verbose be verbose
-n, --numeric don't resolve names
-e, --extend display other/more information
-F, --fib display Forwarding Information Base (default)
-C, --cache display routing cache instead of FIB
<AF>=Use '-A <af>' or '--<af>'; default: inet
List of possible address families (which support routing):
inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25)
netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP)
x25 (CCITT X.25)
what is the solution?
got it to work without the route command.
just some firewall configuration problem.
i saw this recommendation to use route command for running ping on a multicast address. it is not required here.

Resources