Search code examples
node.jsexpressvue.jssslpeerjs

Using SSL in an Express application


I've inherited an application that I'd like to develop further. It's based on Express and Peerjs and there is a server and a client (vue.js) application.

Part of the application is a video stream. In order to test the application I'd like to access it from different devices inside my own network. This doesn't work currently because the application doesn't use SSL but SSL is needed for getUserMedia().

However, I can't get SSL to work.

This is the app.js of the server:

const Express = require("express")();
const { ExpressPeerServer } = require('peer');
const Http = require("https").Server(Express);
const Socketio = require("socket.io")(Http);

const fs = require('fs');
var privateKey = fs.readFileSync('key.pem');
var certificate = fs.readFileSync('cert.pem');

const peerServer = ExpressPeerServer(Http, {
    debug: true,
    key: 'peerjs',
    allow_discovery: true,
    ssl: {
        key: privateKey,
        cert: certificate
    }
});


Express.use('/peerjs', peerServer);

/* some methods here */

Http.listen(3000, () => {
    console.log("Listening at: 3000");
});

And this is inside the client:

let Socket = io("https://192.168.178.28:3000");
let myPeer = new Peer(undefined, {
  host: "192.168.178.28",
  port: "3000",
  path: "/peerjs",
  key: "peerjs",
});

And this is the vue.config.js:

module.exports = {
    devServer: {
        host: '192.168.178.28',
        port: 443,
    },
};

This doesn't work for me. In Chrome I get a "ERR_SSL_PROTOCOL_ERROR" error if I open https://192.168.178.28:443 and "ERR_SSL_VERSION_OR_CIPHER_MISMATCH" if I use http://.

In Firefox I get a "SSL_ERROR_RX_RECORD_TOO_LONG" message.

Any ideas?


Solution

  • I got it. If anyone else has this problem in the future:

    Changed app.js to this:

    const app = require('express')();
    const http = require('http').createServer(app);
    const https = require('https');
    var server = https.createServer({
        key: fs.readFileSync('./key.pem'),
        cert: fs.readFileSync('./cert.pem')
    },app);
    
    const io = require("socket.io")(server);
    const { ExpressPeerServer } = require('peer');
    
    const customGenerationFunction = () => (Math.random().toString(36) + '0000000000000000000').substr(2, 16);
    
    const peerServer = ExpressPeerServer(server, {
        generateClientId: customGenerationFunction,
        debug: true,
        port: 3000,
        key: 'peerjs',
        ssl:{
            key: fs.readFileSync('./key.pem'),
            cert: fs.readFileSync('./cert.pem')
        },
        allow_discovery: true
    });
    
    server.listen(3000, () => {
        console.log("https listening at: 3000");
    });
    

    Inside the client:

    let Socket = io.connect("https://192.168.178.28:3000"); 
    let myPeer = new Peer(undefined, {
      host: "192.168.178.28",
      port: "3000",
      path: "/peerjs",
      key: "peerjs",
    });
    

    And finally, vue.config.js:

    module.exports = {
        devServer: {
            https: true
        },
    };