I need to able to balance websocket on application level. Lets say forward websocket request on the basis of message I received, decode it on proxy and then using that data send to another socket server using some logic.
But I am unable to do this. This is the initial code I wrote and trying to do that. This is the server
var http = require('http');
var httpProxy = require('http-proxy');
var WebSocket = require('ws');
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
var proxy = httpProxy.createServer({target: 'ws://localhost:8080', ws:true});
var server = httpProxy.createServer(function(req, res) {
//SOME LOGIC HERE TO PARSE WS DATA AND SEND TO WS SERVER
proxy.ws(req, res, { target: 'ws://localhost:8080'});
}).listen(8014);
CLIENT
var http = require('http');
var httpProxy = require('http-proxy');
var WebSocket = require('ws');
var ws = new WebSocket('ws://localhost:8014/');
ws.on('open', function () {
ws.send("CLIENT");
});
ws.on('message', function (msg) {
console.log(msg);
});
Here's an example. In this case the client connects directly to outer.js which then forwards connections to upstream servers (inner.js).
outer.js
var http = require('http');
var httpProxy = require('http-proxy');
var proxies = {
foo: new httpProxy.createProxyServer({
target: {
host: "foo.com",
port: 8080
}
}),
bar: new httpProxy.createProxyServer({
target: {
host: "bar.com",
port: 8080
}
})
// extend this...
};
var findUpstream = function(req){
// TODO return key for lookup in #proxies
};
var proxyServer = http.createServer(function (req, res){
var upstream = findUpstream(req);
proxies[upstream].web(req, res);
});
proxyServer.on('upgrade', function (req, socket, head) {
var upstream = findUpstream(req);
proxies[upstream].ws(req, socket, head);
});
proxyServer.listen(8014);
inner.js
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
In this example you'll need to fill in the findUpstream to return the key such as foo or bar based on data in the request. It'd also be worth putting some error handling in for when the proper upstream server isn't found, but this should illustrate the general idea.
Related
I am trying to setup a proxy for http and https. Here is my code,
const http = require('http');
var https = require('https');
const fs = require('fs');
var url = require('url');
var net = require('net');
const config = require('./config');
let proxify = function (req, res) {
var urlObj = url.parse(req.url);
var target = urlObj.protocol + '//' + urlObj.host;
if (!req.headers['x-target']) req.headers['x-target'] = target;
req.headers['x-proxy-username'] = config.username;
req.headers['x-proxy-password'] = config.password;
console.log(target);
console.log('Proxy HTTP request for:', target);
var proxy = httpProxy.createProxyServer({});
proxy.on('error', function (err, req, res) {
console.log('proxy error', err);
res.end();
});
proxy.web(req, res, { target: config.server, changeOrigin: true });
};
var httpserver = http.createServer(proxify).listen(2890); //this is the port your clients will connect to
const httpsserver = https
.createServer(
{
cert: fs.readFileSync('./ssl_cert/cert.pem'),
key: fs.readFileSync('./ssl_cert/key.pem'),
},
proxify
)
.listen(2891);
var regex_hostport = /^([^:]+)(:([0-9]+))?$/;
var getHostPortFromString = function (hostString, defaultPort) {
var host = hostString;
var port = defaultPort;
var result = regex_hostport.exec(hostString);
if (result != null) {
host = result[1];
if (result[2] != null) {
port = result[3];
}
}
return [host, port];
};
httpserver.addListener('connect', function (req, socket, bodyhead) {
var hostPort = getHostPortFromString(req.url, 443);
var hostDomain = hostPort[0];
var port = parseInt(hostPort[1]);
console.log('Proxying HTTPS request for:', hostDomain, port);
req.headers['x-target'] = 'http://' + hostDomain + ':' + port;
req.headers['x-proxy-username'] = config.username;
req.headers['x-proxy-password'] = config.password;
var proxyHost = new URL(config.server);
var proxySocket = new net.Socket();
proxySocket.connect(
{ port: proxyHost.port, host: proxyHost.hostname },
function () {
console.log('bodyhead', bodyhead.toString()); //debug
proxySocket.write(bodyhead);
socket.write(
'HTTP/' + req.httpVersion + ' 200 Connection established\r\n\r\n'
);
}
);
proxySocket.on('data', function (chunk) {
console.log('proxy data chunk', chunk.toString()); // debug
socket.write(chunk);
});
proxySocket.on('end', function () {
socket.end();
});
proxySocket.on('error', function () {
socket.write('HTTP/' + req.httpVersion + ' 500 Connection error\r\n\r\n');
socket.end();
});
socket.on('data', function (chunk) {
console.log('data chunk', chunk.toString('utf8')); // debug
proxySocket.write(chunk);
});
socket.on('end', function () {
proxySocket.end();
});
socket.on('error', function () {
proxySocket.end();
});
});
Don't judge me too hard, just trying to get it working first.
When proxying http with windows 10 proxy settings, it works fine. But when I am trying to proxy https, it logs encoded data like `↕►♦♦♦☺♣♣♣♠♠☺↕3+)/1.1♣♣☺
☺↔ \s☻�t�DQ��g}T�c\‼sO��♦��U��ޝ∟-☻☺☺+♂
→→♥♦♥♥♥☻♥☺♥☻☻j☺§�` and gives a 400 bad request.I don't know if its the encoding of https response or something else, I have no idea what i am doing at this point and need help.
it is because https uses tls/ssl to encrypt the data.
I have a webapplication on a bitnami lamp stack, the os is ubuntu and it uses Apache as a webserver.
The SSL Certificate is generated using Let's Encrypt. I can visit the site on http and https.
Furthermore I am using following stacks:
- nodejs
- socket.io
- redis
Sending notifications on my local machine work fine, the websocket works.
But on my production Server which runs on https it does not work. I Keep getting following error messages:
socket.io.js:2 GET https://mypage.com:3000/socket.io/?EIO=3&transport=polling&t=MNxGNEt
net::ERR_CONNECTION_TIMED_OUT
This is how my socket.js (Server) Looks like:
var app = require('http').createServer(handler);
var fs = require('fs');
var io = require('socket.io')(app);
var Redis = require('ioredis');
var redis = new Redis();
// class declaration
var Redis = require('ioredis');
function handler(req, res) {
res.writeHead(200);
res.end('');
}
io.on('connection', function(socket) {});
var redisUserSignedUp = new Redis();
redisUserSignedUp.subscribe('signed-up-channel');
redisUserSignedUp.on('message', function(channel, message) {
message = JSON.parse(message);
io.emit(channel + ':' + message.event, message.data);
});
// run server on port 3000
app.listen(3000, function () {
console.log('Server runs!');
});
In my production I currently am trying it like this:
var app = require('https').createServer({
key: fs.readFileSync('/opt/bitnami/apache2/conf/serverKey.pem'),
cert: fs.readFileSync('/opt/bitnami/apache2/conf/serverCrt.pem')
}, handler);
var fs = require('fs');
var io = require('socket.io')(app);
var Redis = require('ioredis');
var redis = new Redis();
// class declaration
var Redis = require('ioredis');
function handler(req, res) {
res.writeHead(200);
res.end('');
}
io.on('connection', function(socket) {});
var redisUserSignedUp = new Redis();
redisUserSignedUp.subscribe('signed-up-channel');
redisUserSignedUp.on('message', function(channel, message) {
message = JSON.parse(message);
io.emit(channel + ':' + message.event, message.data);
});
// run server on port 3000
app.listen(3000, function () {
console.log('Server runs!');
});
And I am trying to Access it like this:
var options = {
key: fs.readFileSync('/opt/bitnami/apache2/conf/serverKey.pem'),
cert: fs.readFileSync('/opt/bitnami/apache2/conf/serverCrt.pem'),
requestCert: true
}
var app = require('https').createServer(options, handler);
local:
const socket = io('http://myapp.test:3000'); // localhost dev Folder is mapped to this .test url
socket.on('signed-in-channel:App\\Events\\UserSignedIn', (data) => {
…
production:
var socket = io('mypage.com:3000', {secure: true});
You only create a http server but to connect with ssl you need a https server.
var options = {
key: fs.readFileSync('./file.pem'),
cert: fs.readFileSync('./file.crt')
};
var app = require('https').createServer(handler);
I'm trying to create simple https server with websockets support. I had some functional code for https server and websockets, but both standalone. Don't know how to combine it. I prefer to use free to use libraries (MIT,..)
I want to be able to serve http request and also websockets.
Https example with upgrade, but don't know how to handle upgraded websocket connection as my second example bellow this.
connection. process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var https = require('https');
var websocket = require('websocket');
var fs = require('fs');
var options = {
key:fs.readFileSync('./cert/server.key'),
cert:fs.readFileSync('./cert/server.crt')
};
var server = https.createServer(
options
, function(req,res) {
console.log('req');
res.writeHeader(200, { 'Content-Type': 'text/plain' });
res.write('test');
res.end();
}
);
server.on('upgrade', (req, socket, head) => {
console.log('upgrade ');
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
'Upgrade: WebSocket\r\n' +
'Connection: Upgrade\r\n' +
'\r\n');
socket.pipe(socket);
});
server.listen(8080, '127.0.0.1', () => {
// make a request
const options = {
port: 8080,
hostname: '127.0.0.1',
headers: {
'Connection': 'Upgrade',
'Upgrade': 'websocket'
}
};
const req = https.request(options);
req.end();
req.on('upgrade', (res, socket, upgradeHead) => {
console.log('got upgraded!');
*** so what to do here ***
/*
socket.end();
process.exit(0);
*/
});
});
This is functional websockets but without possibility to handle simple http(s) requests.
var https = require('https');
var ws = require('websocket').server;
var fs = require('fs');
var options = {
key:fs.readFileSync('./cert/server.key'),
cert:fs.readFileSync('./cert/server.crt')
};
var server = https.createServer(
options
, function(req,res) {
res.writeHeader(200);
res.end();
}
);
server.listen(8080);
var wss = new ws({httpServer:server});
var connectionNumber=0;
console.log('start ');
wss.on('request',function(req){
req.on('requestAccepted',function(conn){
conn.on('message',function(msg){
conn.send('test');
});
conn.on('close',function(msg){
});
});
req.accept(null,req.origin);
});
I was looking for functional example of combined solution but haven't any luck.
You can to have wss-connection trouble with self-signed certificate (code 1006). In this case you must add your certificate to root CA (Firefox, Chrome).
// app.js
'use strict'
const https = require('https');
const fs = require('fs');
const ws = require('ws');
const options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
const index = fs.readFileSync('./index.html');
let server = https.createServer(options, (req, res) => {
res.writeHead(200);
res.end(index);
});
server.addListener('upgrade', (req, res, head) => console.log('UPGRADE:', req.url));
server.on('error', (err) => console.error(err));
server.listen(8000, () => console.log('Https running on port 8000'));
const wss = new ws.Server({server, path: '/echo'});
wss.on('connection', function connection(ws) {
ws.send('Hello');
ws.on('message', (data) => ws.send('Receive: ' + data));
});
// index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<script>
var socket = new WebSocket('wss://127.0.0.1:8000/echo');
socket.onopen = () => console.log('Connected') || setInterval(() => socket.send(new Date().toLocaleString()), 1000);
socket.onclose = (event) => console.log((event.wasClean) ? 'Disconnected' : 'Connection break: ' + (event.reason || event.code));
socket.onmessage = (event) => console.log('DATA', event.data);
socket.onerror = (err) => console.error(err.message);
</script>
Press F12 to open console...
</body>
</html>
Since you are using Node, I suggest taking a look at Socket.io. It is easier to use than doing all the manual websocket connection logic yourself.
I'm new to nodejs and I'm trying to solve communication issue with external system.
There is a gateway to external system which can handle websocket requests on port 5000. In the example below, when you request homepage, the nodejs opens websocket connection, then on websocket open event it sends request and waits for response which is used for the HTTP response.
Do you know how to open websocket to external system only once and handle requests based on request id?
var ws = require('ws');
var express = require('express');
var async = require('async');
var uuid = require('node-uuid');
app = express();
app.get('/', function (req, res) {
var webSocket = new ws('ws://localhost:5000/');
async.series([
function (callback) {
webSocket.on('open', function () {
webSocket.send(JSON.stringify({query:'data query', requestid: uuid.v4()}));
callback(null, 'data query');
});
},
function (callback) {
webSocket.on('message', function (data, flags) {
callback(null, data);
})
}
], function (err, results) {
res.setHeader('content-type', 'text/javascript');
res.send(results[1]);
webSocket.terminate();
});
});
var server = app.listen(3000, function () {
var port = server.address().port
console.log('Listening at %s', port)
});
Thanks for the hints. I ended with the following solution which does what I expect:
var ws = require('ws');
var express = require('express');
var uuid = require('node-uuid');
var requests = {};
app = express();
var webSocket = new ws('ws://localhost:5000/');
webSocket.on('open', function () {
console.log('Connected!');
});
webSocket.on('message', function (data, flags) {
var json = JSON.parse(data);
console.log(json.requestId);
var res = requests[json.requestId];
res.setHeader('content-type', 'text/javascript');
res.send(json.data);
delete requests[json.requestId];
});
app.get('/', function (req, res) {
var rid = uuid.v4();
requests[rid] = res;
webSocket.send(JSON.stringify({query:'data query', requestId: rid}));
});
var server = app.listen(3000, function () {
var port = server.address().port
console.log('Listening at %s', port)
});
I'm trying to write a simple node.js program that proxies requests and writes the response to a file. I'm using http-proxy to do the proxying.
When I try to pipe the response to a file (or the process.stdout for testing), it's empty / zero bytes. I'm not sure why but I think it might be because the stream has been closed since the response has been sent back to the client.
How can I get this to work?
var httpProxy = require('http-proxy');
var fs = require('fs');
var server = httpProxy.createServer(function (req, res, proxy) {
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 80
});
});
server.proxy.on('end', function (response) {
response.pipe(process.stdout); // NOTHING IS WRITTEN TO THE STDOUT
});
server.listen(8000);
Try something like
var http = require('http'),
httpProxy = require('http-proxy');
var proxy = new httpProxy.RoutingProxy();
http.createServer(function (req, res) {
var _write = res.write;
res.write = function(data){
process.stdout.write(data); // here we get all incoming data
_write.apply(this, arguments);
}
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 80
});
}).listen(8000);