Using ws and wss simultaneously - node.js

I am trying to get both ws and wss working, on one WebSocket server.
This is what I currently have, only wss
var WebSocket = require('ws');
var https = require('https');
var privateKey = fs.readFileSync('cert/key.key');
var certificate = fs.readFileSync('cert/cert.crt');
var httpsServer = https.createServer({
ca: ca,
key: privateKey,
cert: certificate
}, this.app);
var options = {
server: httpsServer,
perMessageDeflate: false,
maxPayload: 4096
};
var wss = new WebSocket.Server(options);
I know using Socket.io it is really easy, you can just do io.attach(httpServer) and io.attach(httpsServer).
var httpServer = http.createServer(this.app);
var httpsServer = https.createServer({
key: privateKey,
cert: certificate
}, this.app);
httpServer.listen(3002, function(){
console.log('httpServer listening on port 3002');
});
httpsServer.listen(3003, function(){
console.log('httpsServer listening on port 3003');
});
this.io = new ioServer();
this.io.attach(httpServer);
this.io.attach(httpsServer);
Is this even possible, using this WebSocket library?

Related

Adding SSL Certificate to Nodejs

I am trying to add an SSL certificate to my Nodejs website.
const fs = require('fs');
const https = require('https');
const options = {
key: fs.readFileSync('./ssl/private.key', 'utf8'),
cert: fs.readFileSync('./ssl/certificate.crt', 'utf8'),
requestCert:true,
rejectUnauthorized: false
};
var server = https.createServer(options, app);
app.listen(process.env.PORT || 443, () => {
console.log('Server is running on 3000!')
})
The app does not throw any error but if I try to connec, I still get the connection is not secure in chrome.
I changed my code to:
https.createServer({
key: fs.readFileSync('./ssl/private.key'),
ca:fs.readFileSync('./ssl/ca_bundle.crt'),
cert: fs.readFileSync('./ssl/certificate.crt')
}, app).listen(443);
and it worked

How can i connect to node server with letsencrypt https activated

I have a problem since i activated letsencrypt on my domain and did'nt have problem with http server before.
Here is my app.js code:
var app = require('express')();
var fs = require('fs');
var https = require('https');
var secureServer = https.createServer({
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.cert'),
ca: fs.readFileSync('server.cacert'),
requestCert: true,
rejectUnauthorized: false
}, app).listen(5221, function() {
console.log("Secure Express server listening on port "+ 5221);
});
var io = require('socket.io')(secureServer);
The Secure Express server listening on port 5221 prints out but nothing more and the codes in:
io.on('connection', function (socket) {
console.log(`Socket ${socket.id} connected.`);
}
Is not working at all.
I've also tested with .pem files, with ca.crt or without that... but nothing changes.

nodejs and SSL: how to specify the SSL in http.createServer creation?

I have found that snippet that works fine:
var ssl = {
key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/example.com/cert.pem')
};
var httpsServer = https.createServer(ssl).listen(port);
However my site uses that server construction:
var server = http.createServer(httpRequestsHandler).listen(_port_, function() {
process.setgid('www-data');
process.setuid('user1');
});
Question: how/where do I specify the ssl in this case ?
Based on the documentation your code has to look like this:
const https = require('https');
const fs = require('fs');
var ssl = {
key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/example.com/cert.pem')
};
var server = https.createServer(ssl, httpRequestsHandler).listen(_port_, function() {
process.setgid('www-data');
process.setuid('user1');
});
I recommend that you look at this site

Create Websocket Secure on a HTTPS web server in express app

I have a application in express.js. Am unable to create wss on a HTTPS web server.
var fs = require('fs');
var express = require('express');
var app = express();
var cfg = {
ssl: true,
port: 8001,
ssl_key: 'sslcert/ssl.key',
ssl_cert: 'sslcert/ssl.crt'
};
var httpServ = (cfg.ssl) ? require('https') : require('http');
if (cfg.ssl) {
httpsServer = httpServ.createServer({
key: fs.readFileSync(cfg.ssl_key),
cert: fs.readFileSync(cfg.ssl_cert)
}, app)
.listen(cfg.port, function () {
console.log('Magic happening at https://Secure');
});
} else {
httpsServer = httpServ.createServer(app)
.listen(cfg.port, function () {
console.log('Magic happening at http://InSecure');
});
}
var WebSocketServer = require('websocket')
.server;
var wsServer = new WebSocketServer({
httpServer: httpsServer
});
wsServer.on('connection', function connection(ws) {
console.log('Socket Connected');
});
I'm under the impression that passing a reference of the web server to WebSocket, then WebSocket would know the port and SSL capabilities of the server. But every time I get an error in the console
WebSocket connection to 'ws://localhost:8001/?trackme=TUKOCVPAF' failed: Connection closed before receiving a handshake response
How can I create a wss://localhost:8001/?trackme=TUKOCVPAF' when creating the Websocket..??

Starting HTTPS in Express 4.x

I'm trying to start the server in HTTPS using Express 4.x. I tried
var credentials = {key: privateKey, cert: certificate};
var app = express(credentials);
But it's not working.
You could easily just do something like this:
var app = express(),
credentials = {key: privateKey, cert: certificate},
server = https.createServer(credentials, app);
server.listen(8443);

Resources