How do you connect to vpn using node.js - node.js

I am a newbie to node.js and looking for some example code or pointers on connecting through PPTP to a private virtual ip address using VPN connection. I have a node.js server running on aws that currently uses udp to connect to a public ip address. However, this needs to be changed to tunnel into the private vpn.
I have the uid,pwd and apn for the vpn. What are the steps I would need to take to tunnel in, and then connect to the private ip?
Appreciate any tips you might have.
Thanks
M

this is too old a question but for this, it has already several answers in StackOverflow, in that one of the best and I used methods is using the node-openvpn package.
first thing first we need to install the package
npm install node-openvpn
at your index.js or server.js use below code
const openvpnmanager = require('node-openvpn');
const opts = {
host: '127.0.0.1', // normally '127.0.0.1', will default to if undefined
port: 1337, //port openvpn management console
};
const auth = {
user: 'vpnUserName',
pass: 'vpnPassword',
};
const openvpn = openvpnmanager.connect(opts)
openvpn.on('connected', () => {
openvpnmanager.authorize(auth);
});
here you can read more about the node-openvpn

Related

Hostname in socket server

I'm using the nodejs library:
import net from 'net'
When creating a server via:
net.createServer
Then I can make the server listen using the next procedure:
server.listen({host: 'localhost', port: 8000, exclusive: true}, () => etc..)
My question is why do I need to specify a host for the server? I mean, I should not always be serving on the server/ip where the service is placed? When I deploy a service in spring or nginx I don't have to specify the actual host since it's assumed what host should use. Isn't it right?
To answer your question, you do not need to specify a host when deploying your server. It is an optional dependency for people who may be using a VPN and have 2 different IP addresses.
The following code is all you need to deploy your web server.
server.listen(port, function() {
// Do something
});

Run a VPN client on a remote website

I have been researching this for day and I haven't been able to find the way to do this.
I am building a react app, running express at the backend, that needs to access some data in a remote database that lives inside a VPN. At the moment the app lives on my localhost so its enough for me to connect my machine using openvpn client and everything works a beauty. The problem will rise when the app will be live and I will need it to have access to the vpn by (I'm guessing) having a vpn client running on the site/domain.
Has anyone done this before?
I have tried to install the node-openvpn package that seems could do the job but unfortunately I can't manage to make it work as the connection doesn't seem to be configured properly.
This is the function I call to connect to the vpn that systematically fails at the line
--> openvpnmanager.authorize(auth);
const openvpnmanager = require('node-openvpn');
...
const connectToVpn = () => {
var opts = {
host: 'wopr.remotedbserver.com',
port: 1337, //port openvpn management console
timeout: 1500, //timeout for connection - optional,
logpath: '/log.txt'
};
var auth = {
user: 'userName',
pass: 'passWord',
};
var openvpn = openvpnmanager.connect(opts);
openvpn.on('connected', function() {
console.log('connecting..');
openvpnmanager.authorize(auth); <-- Error: Unhandled "error" event. (Cannot connect)
});
openvpn.on('console-output', function(output) {
console.log(output)
});
openvpn.on('state-change', function(state) { //emits console output of openvpn state as a array
console.log(output)
});
};
Am I misusing this function? Is there a better way?
Any help will be extremely appreciated.
Thank You!
The problem will rise when the app will be live and I will need it to
have access to the vpn by (I'm guessing) having a OpenVPN client running
on the site/domain.
Thats correct, you will need an openvpn client instance on the server where you will run the backend.
The above library (node-openvpn) is simply a library to interact with the local OpenVPN client instance. It cannot create a connection on its own. It depends on the OpenVPN binary (which should be running).
The solution you need is simply run the OpenVPN client on your server (apt-get openvpn). And let the daemon run. Check out the references below.
node-openvpn issues that points out that a running instance of the client is needed
OpenVPN CLI tutorial

Connect AWS redis to node using node-redis

I am using node-redis and having a hard time connecting to external redis instance. I tried with redis-cli and it worked. However with node I am not able to figure out how to properly give the url and port.
With Redis-cli-
redis-cli -h mydomain.something.something.cache.amazonaws.com -p 6379
However with nodejs
Below didn't work
var client = redis.createClient('redis://mydomain.something.something.cache.amazonaws.com:6379'),
neither
var client = redis.createClient({host:'redis://mydomain.something.something.cache.amazonaws.com', port: 6379});
How do I configure it. Please help.
Following should work with node.js -
var client = require('redis').createClient(6379, 'elastichache endpoint string', {
no_ready_check: true
});
Also, make sure that your security group on AWS allows you to access the database.
var client = require('redis').createClient(6379, 'elastichache endpoint string', {
no_ready_check: true
});
With the above code, it was always trying to connect with localhost,
Below code worked for me.
var client = require('redis').createClient(
{
url: `redis://${elasticCacheConnectionString}`,
}
);
Please note, i have appended redis:// as communication protocol before actual connection string.
FYI: I am using redis#4.1.0 version.

How to connect to a Tor bridge from node.js?

I am new to Tor. I have recently managed to execute a query from node.js while running a tor server instance on my PC.
I have used the following piece of code:
var Agent = require('socks5-https-client/lib/Agent');
var request = require("request");
var q = "https://www.example.com/";
request({
url: q,
agentClass: Agent,
agentOptions: {
socksHost: 'localhost',
socksPort: 9050 // Defaults to 1080.
}
}, function(err, res) {
console.log(err || res.body);
});
I would like to connect to Tor without running a Tor server on my PC. I believe this is possible with a Tor bridge. I have retrieved an IP address from https://bridges.torproject.org/bridges:
2.91.117.71:443 3C2AAD50197ACE1A43C822BBE282E0534603A31F
I am not really sure how to use this information. I have tried to set:
agentOptions: {
socksHost: '2.91.117.71',
socksPort: 443
}
but I get a timeout:
{ [Error: connect ETIMEDOUT] code: 'ETIMEDOUT', errno: 'ETIMEDOUT', syscall: 'connect' }
My questions are:
Is it possible to connect to a public Tor server with https from node.js?
If yes how?
A Tor-Bridge is used for people who are not able to connect to the normal Tor-Network because their ISP is blocking the publicly known Tor-Servers.
If the first connection is done through a Bridge you wont have problems afterwards because usually these Bridges are not known to everyone so they are usually not blocked by the ISPs as they look like normal servers.
You still need a Tor-Client to connect to them.
There is no way to connect to the Tor network without having a Tor Client.
In your first code your Tor-Client is acting like a socks-proxy and just proxying your requests through the Tor network.
You could try if this library works.
Tor bridges are not what you want to use. What has worked for me is to setup a VPS (take a look at digitalocean $5 per month on SSD's) with Debian (Ubuntu 14.04 is what I prefer) and install tor & nodejs unless you want to try a language that already has a mature body of documentation connecting and manipulating tor like Python (my preferred language for Tor).
You can then run the same code from that VPS and it will work, that way you won't be executing the code on your local machine & you can change your IP address on the VPS whenever it suits you and use things like proxychains to bounce traffic that you are looking up through other proxies besides just TOR.
Hope that helps.

Giving public access to chat server on node.js

Now I am learning node.js and created a simple chat server using node.js.
My code:
net = require('net');
var sockets = [];
var s = net.Server(function(socket) {
sockets.push(socket);
socket.on('data', function(d) {
for (var i = 0; i < sockets.length; i++) {
if(sockets[i] == socket) continue;
sockets[i].write(d);
}
});
socket.on('end', function() {
var i = sockets.indexOf(socket);
sockets.splice(i, 1);
});
});
s.listen(8000);
How can I share this chat server on the Internet, so other people can use it?
On my local machine, I have an access through telnet:
telnet localhost 8000
If you really want to let others use this chat through telnet over the internet (which I recommend against), you will need to forward a port through your router (I assume you use a router) to port 8000 on your local machine. Give your friends your IP address and the port you mapped, and they should be able to telnet in as well.
It's hard to answer this question without more information, however. Do you use a firewall? Modem? Etc.
Its really hard to answer this question based on all the information.
It all depends if you are behind a router, firewall etc. If you are directly connected to the internet through a modem you can use ipconfig(in command prompt) to find your public ip. If you are behind a network router you will most likely have to setup port forwarding. If this is the case, just do a Google search on your router, I'm sure you will find a tutorial to set it up or you can refer to your manual.
Here is a explanation on port forwarding: http://en.wikipedia.org/wiki/Port_forwarding
Hope this helps or points you in the right direction!
EDIT 2022:
Easiest option these days would be to use NGROK (or something similar)

Resources