Skip to content

How to put a password on peerflix server using basic auth

DarkVenusNet edited this page Jun 12, 2020 · 11 revisions

How to put a password on peerflix server using basic-auth

This is a guide for putting a password on peerflix-server without using squit or Nginx.

Tested on Ubuntu 16.04

Prerequirements

You need to install basic-auth and nano

npm install basic-auth

apt-get -y install nano

Close peerflix-server if it is running

  • if you have started peerflix-server with the command peerflix-server then click Ctrl + C

or

  • if you are running peerflix-server with forever forever stop $(which peerflix-server)

Adding Password

Open the file index.js with nano

nano /usr/local/lib/node_modules/peerflix-server/server/index.js

and replace

api.use(function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'OPTIONS, POST, GET, PUT, DELETE');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  next();
});

to

var basicAuth = require('basic-auth');
var dvnuser = 'your_user';
var dvnpass = 'your_pass';
api.use(function (req, res, next) {
  function unauthorized(res) {
    res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
    return res.send(401);
  }
  var user = basicAuth(req);
  if (!user || !user.name || !user.pass) {
    return unauthorized(res);
  };
  if (user.name === dvnuser && user.pass === dvnpass) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'OPTIONS, POST, GET, PUT, DELETE');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    return next();
  } else {
    return unauthorized(res);
  }
});

P.S.: replace your_ user and your_pass with your desired username and password

Click Ctrl + X then click two times enter to exit

Restarting peerflix-server

  • You can start peerflix-server normally with peerflix-server command

or

  • You can start peerflix-server using forever forever start $(which peerflix-server)