How to connect to a Tor bridge from node.js? - 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.

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

How to configure mqtt.js to connect to iot.eclipse.org over websockets

Edit: My issue is now this. I can connect to iot.eclipse.org using http://www.hivemq.com/demos/websocket-client, using port 80. When I connect via a browsified mqtt.js client I am getting the following error :
WebSocket connection to 'ws://iot.eclipse.org/' failed: Error during
WebSocket handshake: Unexpected response code: 200
I've tried ports 8080, 8000, 1883 and 80, without any luck. Any suggestions?
------------ Original question below -----------
I want to connect with a mqtt broker using mqtt over websockets. My client will need to run in a browser.
TO achieve this I am using mqtt.js library and am following these instructions.
Everything works when running against the public broker at broker.mqttdashboard.com. However when I connect to the public brokers at iot.eclipse.org and test.mosquitto.org I get HTTP errors.
I think the problem is incorrect configuration of the client when running against the second two brokers, but I'm struggling to find any help.
Heres the configuration, is there anyone out there who can help me?
// Works fine
var options = {
host: "broker.mqttdashboard.com",
port: 8000
};
// Doesn't work
/*var options = {
host: "m2m.eclipse.org",
protocolId: 'MQIsdp',
protocolVersion: 3
};*/
// Doesn't work
/*var options = {
host: "test.mosquitto.org",
protocolId: 'mosqOtti',
protocolVersion: 3
};*/
var client = mqtt.connect(options);
Let me know if theres any more information you need!
Mark
Both test.mosquitto.org and iot.eclipse.org are both websockets enabled (for a long time now actually).
You already have got test.mosquitto.org working - the key there is using port 8080.
The current iot.eclipse.org configuration expects the connection url to be ws://iot.eclipse.org/mqtt.
I don't think m2m.eclipse.org / iot.eclipse.org or test.mosquitto.org have websockets enabled.
broker.mqttdashboard.com runs a HiveMQ underneath which has native websockets enabled.
So in short, I don't think this is a configuration problem on your side. To make sure, you can check this web application and see if the other brokers work with that: http://www.hivemq.com/demos/websocket-client/

How do you connect to vpn using 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

Node.js: Cannot access server from different device on the same network

NOTE: There are some others who have had similar problems, but those were solved by fixing small tidbits in the code involving how the server was listening (in examples that I've seen they put '127.0.0.1' as an argument in http.createServer(...).listen(). However, I do not have the same issue.
When I try to connect to my node.js server from a different machine on the same LAN network, Chrome says that it cannot connect.
This is testtesttest.js
var http = require('http');
http.createServer(function(req,res) {
res.writeHead(200,{'Content-Type': 'text/plain'});
res.end('Working');
}).listen(3000);
When I try inputting 192.168.1.73:3000 (of course 192.168.1.73 is the ip of the machine that I'm running the server on) into the browser (Chrome, although I've tried other browsers as well and I have similar problems) of my other machine, it gives the error "Oops! Google Chrome could not connect to 192.168.1.73:3000". When I type the same address onto the local machine, it works fine.
I'm not exactly sure what to do. I honestly hope this is just a stupid mistake on my part (I'm sorry for possibly wasting your time) and not something that I have to go into my router for.
Thanks very much for any help.
Try changing this
.listen(3000);
to this
.listen(3000, "0.0.0.0");
Just putting this here in case it saves anyone else. I had this problem for two full days when trying to connect my phone to my local machine... and it was because the wifi on my phone was turned off.
I too had this problem.
I solved it by allowing "node.js" in the network group.
Solution : Allowing "node.js" through the private network windows firewall
a. Go to Control Panel
b. Go to Windows Firewall
c. Click on the Allow an app or feature through windows firewall in the left sidebar
d. Search for "Node.js : Server Side JavaScript" and make sure both public and private column box is marked tick for "NodeJS"
e. Click "OK" and you are done
Below is the step i followed which Worked
My server code
var http=require('http');
http.createServer(function(request,response){
response.writeHead(200,{'Content-Type':'text/plain'});
response.end('Im Node.js.!\n');
console.log('Handled request');
}).listen(8080, "0.0.0.0");;
console.log('Server running a http://localhost:8080/');
Added inbound Rules.
Created a udp inbound rule(since i could'nt find any http protocol).
Once created go to properties for the created rule.
Choose Protocols and Properties tab.
Choose any in Port Type. Click apply and Ok. Now i tried from other
machine it worked!!!
I think you need to set Port Type to any to make it work.
Thanks
You have to open 3000 port so that it can be accessed from remote machines. You can do that in iptables file. Use below command to open file
vi /etc/sysconfig/iptables
now add below line before the reject lines in that file
-A INPUT -p tcp -m tcp --dport 3000 -j ACCEPT
Now restart iptables service using below command
service iptables restart
Now you restart your server and try again. It should work..
Chances are your firewall settings block incoming request on port 3000. You may want to add firewall inbound rule on this port to allow access to it.
For me, the culprit was a VirtualBox Host-only Network interface. The presence of this network interface was causing ipconfig on the server to report 192.168.56.1 instead of the router assigned address of 192.168.1.x. I was accessing the wrong IP all along.
To remove the VirtualBox Host-only Network interface:
Open VirtualBox
Go to File>Preference>Network>Host-only Networks
Remove the offending adapter(s)
My problem was that, I have used IP assigned to my ethernet adapter instead of wifi adapter...
And it now works when I connect from any device.
http.createServer(function(req,res) {
res.writeHead(200,{'Content-Type': 'text/plain'});
res.end('Working');
}).listen(3000, "192.168.1.36");
I found my IPv4 address on network settings, then specify with listen fun. put also 3000 port. I can reach http://192.168.1.36:3000/ via my tablet which is connected same wifi.
You have to run that on the terminal:
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
Explanation here
Like what Umamaheswaran answered, a new inbound rule needs to be created. But instead of using the UDP protocol, I have to use TCP. My application runs on a Windows Server 2012. The Inbound Rules are set in Windows Firewall with Advanced Security, under Administrative Tools
I solved this using this algorithm. What it does is get all ip info from os.networkInterfaces, get all keys and loop trough it, filter by objects that contains family == "IPv4" and get it address value. After, it looks for '192.168.0.xxx' pattern and put it in possible_ip array. Any other ip will be pushed to other_ips array.
The export sentence is to turn it into a module js file.
import by import {get_ip} from ./your_path/name_of_your_file.js, or in common js const { get_ip } = require("./your_path/name_of_your_file.js");
Hop it helps
const os = require('os');
const _ = os.networkInterfaces();
// if common js use
// exports.get_ip = () => {
export const get_ip = () => {
let ips = [];
Object.keys(_).map(key => {
let ipv4_obj = _[key].filter(obj => obj.family === "IPv4")
let ipv4 = ipv4_obj[0].address
ips.push(ipv4)
})
let other_ips = []
let possible_ip = []
ips.map(ip => {
ip.includes("192.168.0.") ?
possible_ip.push(ip) :
other_ips.push(ip)
})
return { possible_ip, other_ips }
}

Resources