Node.js - programmatically connect to VPN or route HTTP requests via VPN

This is probably a little long shot. But in Node.js I want to make an HTTP request (using, for example, a request packet on npm) through a VPN server. I already have a VPN server setup (using HideMyAss ), and I can connect to it at the operating system level by setting it in the network settings section of my OS. But I want to connect to it programmatically using Nodejs and request any HTTP requests using this program routed through this VPN.

This can be done using a proxy, for example:

var request = require('request'); var url = 'https://www.google.com'; var proxy = 'http://username: password@usa.proxyservice.com :1234'; request.get({ url: url, proxy: proxy }, function(err, res, body) { }); 

... but I want to do something similar with a VPN. Is it possible? Thanks!

+4
source share
1 answer

VPN (usually) tunnel layer 2 or 3 network packets through a specific channel. From the point of view of the application level, you do not need to worry about the lower levels, since the routing of network packets is performed by the OS. OpenVPN, for example, creates a virtual TUN or TAP interface and transmits all packets sent by the OS to these interfaces through a secure tunnel. A solution whose interface to use is implemented with respect to the routing table. A web proxy server is much simpler than a VPN because it simply accepts HTTP requests (application layer) and passes them.

It really depends on which VPN you are using. Suppose you are using OpenVPN and it creates a virtual interface for you (for example, tun0 ), this interface has its own IP address (for example, 10.124.1.6 ) and possibly (depending on the topology server) a P2P address (for example, 10.124.1.5 ). You can tell Node to connect network connections through a specific (local) network interface using localAdress :

 var options = { host: "example.com", path: "/path/to/web/service", localAddress: '10.124.1.6', // IP address network interface }; http.request(options); 

Hypothetically, this will be the solution to selective tunneling of specific requests through the VPN interface. If it works or not , I can not guarantee. But I would say that you will start with some ideas.

+4
source

All Articles