Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serve https #169

Closed
dirkk0 opened this issue Sep 18, 2019 · 6 comments · Fixed by #365
Closed

serve https #169

dirkk0 opened this issue Sep 18, 2019 · 6 comments · Fixed by #365

Comments

@dirkk0
Copy link
Contributor

dirkk0 commented Sep 18, 2019

Hi,

first of all: thanks for this great library!

To test with multiple devices, it would be better to serve the files via https, because audio otherwise will silently (sic!) fail due to security reasons.

The only thing one needs to do is to insert:

const https = require("https");
const fs = require("fs");
var privateKey  = fs.readFileSync('key.pem', 'utf8');
var certificate = fs.readFileSync('cert.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate};

add some self created keys, change the http references to https and start the server with the credentials:

var webServer = https.createServer(credentials, app);

I would create a pull request but I am not sure if we should change the existing easyrtc-server.js or better create a new one.

Best,
Dirk

@TheBricktop
Copy link

easy-rtc is already in dire need of being replaced because of multiple security flaws, so go ahead and create pull.

@hthetiot
Copy link
Contributor

hthetiot commented Jan 13, 2020

See Open-EasyRTC SSL sample:

Self Signed certificates for testing:

Note: This is not related to easyrtc but to https.createServer that as nothing to do with EasyRTC technically.

@sikaar
Copy link

sikaar commented Dec 15, 2020

Hi,
I am not a developper but got this to work properly, serving https and redirecting http properly. by updating /server/index to the following (remember to update the path to for var privateKey & var certificate)

// Load required modules
const http = require("http"); // http server core module
const https = require("https");
const fs = require("fs");
const path = require("path");
const express = require("express"); // web framework external module


var privateKey  = fs.readFileSync('../key.pem', 'utf8');
var certificate = fs.readFileSync('../cert.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate};


// Set process name
process.title = "networked-aframe-server";

// Get port for http or default to 80 and redirect to https
const httpApp = express();
httpApp.set('port', process.env.PORT || 80);
httpApp.get("*", function (req, res, next) {
    res.redirect("https://" + req.headers.host + "/" + req.path);
});

// Setup and configure Express https server.
const app = express();
app.set('port', process.env.PORT || 443);

app.use(express.static(path.resolve(__dirname, "..", "examples")));

// Serve the example and build the bundle in development.
if (process.env.NODE_ENV === "development") {
  const webpackMiddleware = require("webpack-dev-middleware");
  const webpack = require("webpack");
  const config = require("../webpack.dev");

  app.use(
    webpackMiddleware(webpack(config), {
      publicPath: "/dist/"
    })
  );
}



// Start Express http server
const webServer = https.createServer(credentials,app);
const io = require("socket.io")(webServer);

const rooms = {};

io.on("connection", socket => {
  console.log("user connected", socket.id);

  let curRoom = null;

  socket.on("joinRoom", data => {
    const { room } = data;

    if (!rooms[room]) {
      rooms[room] = {
        name: room,
        occupants: {},
      };
    }

    const joinedTime = Date.now();
    rooms[room].occupants[socket.id] = joinedTime;
    curRoom = room;

    console.log(`${socket.id} joined room ${room}`);
    socket.join(room);

    socket.emit("connectSuccess", { joinedTime });
    const occupants = rooms[room].occupants;
    io.in(curRoom).emit("occupantsChanged", { occupants });
  });

  socket.on("send", data => {
    io.to(data.to).emit("send", data);
  });

  socket.on("broadcast", data => {
    socket.to(curRoom).broadcast.emit("broadcast", data);
  });

  socket.on("disconnect", () => {
    console.log('disconnected: ', socket.id, curRoom);
    if (rooms[curRoom]) {
      console.log("user disconnected", socket.id);

      delete rooms[curRoom].occupants[socket.id];
      const occupants = rooms[curRoom].occupants;
      socket.to(curRoom).broadcast.emit("occupantsChanged", { occupants });

      if (occupants == {}) {
        console.log("everybody left room");
        delete rooms[curRoom];
      }
    }
  });
});

http.createServer(httpApp).listen(httpApp.get('port'), function() {
    console.log('Express HTTP server listening on port ' + httpApp.get('port'));
});

webServer.listen(app.get('port'), () => {
  console.log('Express HTTPS server listening on port ' + app.get('port'));
});

@vincentfretin
Copy link
Member

If you want a self generated certificate that is valid, you can use https://github.com/FiloSottile/mkcert

@vincentfretin
Copy link
Member

I'm open to any PR that can add documentation and comments in the server file to enable https for the nodejs server.

@dirkk0
Copy link
Contributor Author

dirkk0 commented Feb 27, 2021

I created a PR.

Thanks, Dirk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants