Faye and Nodejs Issue - javascript

Hi currently I'm trying to do a notification feature on a webapp and I decided to use Faye in order to make them live notifications. Now the feature works fine on my local computer, but when somebody else tries to connect to my network, they get a connection refused and faye doesn't work.
Current code working on my local machine:
var http = require('http');
var faye = require('faye');
var server = http.createServer();
var bayeux = new faye.NodeAdapter({mount: '/faye', timeout: 45});
bayeux.attach(server);
server.listen(8000);
I managed to read some other questions related to this issue, and people said to make the server listen to all interfaces by adding: '0.0.0.0' to the server.listen call. When I do this it even stops working on my local machine. Any help will be appreciated!, Nodejs is not the only server running on my machine but the port is open so thats not an issue. I get a get failed request towards the faye message request.

Related

Only allow computers on the same network using Express-ip-filter

So I'm using localtunnel to expose my ports over the internet, but I only want to let devices on the same network as the server access the server.
I'm using express-ip-filter to filter away anything that's on a different network. I tried a few things: first I tried using 192.168.1.0/24 as the only ips that could access the website, but that didn't work, as it didn't let anything in. I then tried using the ip I got from WhatsMyIp, but that wouldn't let any device in. I then found out that express-ip-filter spits out a message saying that a certain ip was not allowed and, on every device, independently on the network it was connected to, the address was 127.0.0.1. I tried confirming by only allowing 127.0.0.1, and then every device could access the server. Why would ip-filter only get 127.0.0.1 as ip? Here's my code as a reference:
// Init dependencies
var express = require('express'),
ipfilter = require('express-ipfilter').IpFilter
app = express()
// Blacklist the following IPs
var ips = ['192.168.1.0/24']
// Create the server
app.use(ipfilter(ips, { mode: "allow" }))
app.get('/', function (req, res) {
res.send('Hi')
})
app.listen(8080, () => console.log('Up'))
From my limited understanding of localtunnel it seems like it proxies users requests to you via the localtunnel software which causes all users to have the same IP. In laymans terms:
User connects to your site through localtunnel
localtunnel copies the users request and sends it to your computer
Your application receives the request but it looks like all traffic is coming from localtunnel because it's incredibly difficult if not impossible for localtunnel to imitate someone else's IP.
Why use localtunnel at all if you only want devices on the same network to connect, you don't need to do any port forwarding or DNS setup if you just want to access another machine on the same local network.
If you really do need to tunnel connections then there is a solution, not with localtunnel(Which as far as i can tell does not use forwading headers, although if someone knows if they do ill change my answer) but using https://ngrok.com instead which does exactly the same thing but also sends a little extra bit of data in every request which tells the application what the clients actual IP is.
Install ngrok
Run ngrok http -subdomain=(the subdomain you want) 80
Edit your application code to find the real client IP
var findProxyIP = function(req) {
var realIP = req.header('x-forwarded-for');
return realIP;
}
app.use(ipfilter(ips, {
mode: "allow",
detectIP: findProxyIP
}));
ngrok is much more complex and has a lot more features compared to localtunnel, however, it is freemium software and its free plan is quite limiting.

Check telnet connection with server IP and Port number in JScript

I need to test telnet connection to the server in my local pc. My pc don't have any server side languages like PHP, NodeJS,.. I want to do this in basic JavaScript or chrome extension from latest chrome. I have checked with websocket HTML5. But, it was not working. Here's my code,
<script>
var connection = new WebSocket('ws://10.0.30.1:80');
connection.onopen = function () {
connection.send('Ping'); // Send the message 'Ping' to the server
};
</script>
Lets explain what's my mistake and give me a solution if any other possible.
You can't. Unless you set up a proxy server, a websocket can only communicate with a websocket server.

Websocket Server, based on socketo.me not accepting connections from shared internet

I managed to configure a simple websocket server according to this tutorial in AWS EC2 instance and its working fine.
But only from my home internet connection which has a real IP and told as a dedicated internet line.
I tried with a very simple javascript example code from client side (using a HTML page) and it works perfectly if I use that dedicated internet connection from my PC/Mac. (I used autobahn.min.js) above the following script.
var conn = new ab.Session('ws://X.X.X.X:8080',
function() {
console.log("Connection established!");
// To Do: Subscribe with client ID
},
function() {
console.warn('Connection closed!');
},
{'skipSubprotocolCheck': false}
);
but it fails if I run the same simple file/script from under another shared internet connection such as cellular data or something like that. I get the following error in browser console.
WebSocket connection to 'ws://X.X.X.X:8080/ws' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT example.com Connection closed!
The server is in AWS EC2 instance. Yes, 8080 is enabled under security group. Actually all works fine except client connection goes from some specific types of internet connection based computer.
Thanks in advance for any help!

Express4 + Socket.io 0.9: different clients get the same non broadcast message

This is a web site using authentication via passport.js.
Two different users connect from different browsers and they request info about their username. The server gets the information and send them back using socket.io.
Everything works like a charm but if the two clients load the page at the same time, the information of one of them goes to both browsers, looks like the server is writing on the same socket.
Server Side:
server.js:
var express = require('express');
var app = express();
var http = require('http').createServer(app).listen(8000),
io = require('socket.io').listen(http);
socket.js:
module.exports = function(app, io) {
...
io.sockets.on('connection', function(socket) {
...
//Build the information about the user and send it back
var userData = userInfo();
socket.emit('userInfo', userData);
...
}
}
Client side (javascript file included in index.ejs):
var socket = io.connect('http://URL:8000');
...
socket.emit("all", {data}); //Hi, I need information about me.
...
socket.on('userInfo', function (data) {
// do some stuff...
});
Server debug in console gets info about the two sockets:
debug - client authorized
info - handshake authorized Cq71N34XLyAJBTIbHCZQ
debug - setting request GET /socket.io/1/websocket/Cq71N34XLyAJBTIbHCZQ
debug - set heartbeat interval for client Cq71N34XLyAJBTIbHCZQ
debug - client authorized for
debug - websocket writing 1::
...
debug - client authorized
info - handshake authorized UF6lOwOFzgjrWY54HCZP
debug - setting request GET /socket.io/1/websocket/UF6lOwOFzgjrWY54HCZP
debug - set heartbeat interval for client UF6lOwOFzgjrWY54HCZP
debug - client authorized for
debug - websocket writing 1::
I´ve been rewriting different parts of the app but I can´t get why the server answers same info to different sockets.

Chrome websockets, readystate always on 0

I'm trying to use websockets on a website, so first I developed a small and very simple websocket page just to test it.
I have a websocket server running on my localhost, it is based on the python Tornado "Chat" demo. For some reason the chat demo app runs perfectly but I can't seem to use the websocket with my own page although some form of connection is made.
I am testing this using the latest Chromium version, so implementing websockets version 13, which is supported by the Tornado implementation.
So here is the problem:
Load page, js executes and Upgrade request is sent to the server
Server receives request and answers
So here in my understanding Chrome should set readyState = 1 and I should be able to send messages from my page.
Only for some reason it doesn't work, readyState remains 0 and of course if I try to send a message I receive an INVALID_STATE_ERR.
Here are the Headers :
Request:
GET ws://127.0.0.1:8000/chatsocket HTTP/1.1
Origin: http://127.0.0.1
Cookie: _xsrf=9f73731fc2d544df864ce777bef0775a
Connection: Upgrade
Host: 127.0.0.1:8000
Sec-WebSocket-Key: pkwlpY+TtxfgUrm3M4WtTQ==
Upgrade: websocket
Sec-WebSocket-Version: 13
Response:
HTTP/1.1 101 Switching Protocols
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Accept: ur9KL2jBhYB38e2SgwOkjyBlQXk=
Any help is appreciated :)
--- EDIT ---
So I figured it out in the end, if you run into the same problem here is the reason:
WebSocket's readyState is updated when the Thread ENDS !
So running a code like:
var ws = new WebSocket(stuff);
while(ws.readyState==0){};
Will send the browser in an infinite loop...
Running code like:
var ws=new WebSocket(stuff);
do_other_stuf();
Might work, but you wont be able to use WS.
If the code that is supposed to run after the socket opens uses the socket this is the way it will have to be written:
var ws=new WebSocket(stuff);
ws.onopen = new function(){
// some code that need WS to be open
}
// rest of the code that doesn't require WS to be open
A better way to do this would be to let the thread end by using an asynchronous call:
var ws = new WebSocket(stuff);
setTimeout(whatever,500);
function whatever(){
if(ws.readyState==1){
// The code you want to run after WS is open
}
else
setTimeout(whatever,500);
}
In order to trigger some functionality when WebSockets are connected please use callbacks:
var socket = new WebSocket(...);
socket.onopen = function() {
// socket is connected
};
socket.onerror = function() {
// some error happened
};
socket.onmessage = function(evt) {
// you get a message: evt.data
};
Using readyState is well bad thing to do in such case, especially if you have to ask it multiple times (is just unnecessary).
Additionally take in account that each vendor of browsers implements WebSockets with very slight differences (unfortunately), so make sure you test it in most browsers.

Categories

Resources