server: add --authentication option

This commit is contained in:
bordun 2020-05-05 12:05:46 +02:00
parent 48807e9f85
commit 7cb729ff79
6 changed files with 57 additions and 5 deletions

View file

@ -41,6 +41,7 @@
"exorcist": "1.0.1",
"file-saver": "2.0.2",
"gyronorm": "2.0.6",
"http-auth": "^4.1.2",
"json5": "2.1.2",
"jsondiffpatch": "0.4.1",
"keyboardjs": "github:jean-emmanuel/KeyboardJS",

23
src/server/auth.js Normal file
View file

@ -0,0 +1,23 @@
var settings = require('./settings'),
auth = null
if (settings.read('authentication')) {
var [name, pwd] = settings.read('authentication').split(':'),
httpAuth = require("http-auth")
auth = httpAuth.basic(
{
realm: "Open Stage Control"
},
(username, password, callback) => {
// Custom authentication method.
callback(username === name && password === pwd);
}
)
}
module.exports = auth

View file

@ -57,6 +57,12 @@ module.exports = function(options={}) {
}
})
window.webContents.on('login', function(event, request, authInfo, callback) {
event.preventDefault()
var [name, pwd] = settings.read('authentication').split(':')
callback(name, pwd)
})
// window.webContents.on('will-navigate', (event)=>event.preventDefault())
window.webContents.on('new-window', (event, url)=>{
event.preventDefault()

View file

@ -1,6 +1,7 @@
var EventEmitter = require('events').EventEmitter,
WebSocketServer = require('ws').Server,
Client = require('./client')
Client = require('./client'),
settings = require('../settings')
class Ipc extends EventEmitter {
@ -15,6 +16,17 @@ class Ipc extends EventEmitter {
this.server.on('connection', (socket, req)=>{
if (settings.read('authentication')) {
if (!req.headers.authorization) return;
if (req.headers.authorization.search('Basic ') !== 0) return;
var auth = new Buffer(req.headers.authorization.split(' ')[1], 'base64').toString()
if (settings.read('authentication') !== auth) {
console.log(`(WARNING) Client connexion refused for ${req.connection.remoteAddress} (authentication failed)`)
socket.close()
return
}
}
var id = req.url.split('/').pop()
if (!id) return

View file

@ -21,7 +21,7 @@ module.exports = {
check: (arg)=>{
var customModule = arg.slice()
// consolidate path containing whitespaces
if (!customModule[0].includes('.js')) {
var index = customModule.findIndex(x => typeof x === 'string' && x.includes('.js'))
@ -92,6 +92,11 @@ module.exports = {
}
},
'remote-root': {type: 'string', describe: 'set remote file browsing root folder', file: {folder: true}},
'authentication': {type: 'string', describe: 'restrict access to "user:password"',
check: (a, argv)=>{
return a.match(/[^:]*:[^:]+/) ? true : 'Auth must be a user:password pair.'
}
},
'instance-name': {type: 'string', describe: 'used to differenciate multiple instances in a zeroconf network'},
// cli only

View file

@ -4,10 +4,11 @@ var urlparser = require('url'),
send = require('send'),
http = require('http'),
replaceStream = require('replacestream'),
server = http.createServer(httpRoute),
settings = require('./settings'),
auth = require('./auth'),
server = http.createServer(auth ? auth.check(httpRoute) : httpRoute),
Ipc = require('./ipc/server'),
ipc = new Ipc(server),
settings = require('./settings'),
theme = require('./theme').init(),
zeroconf = require('./zeroconf'),
osc = {},
@ -24,6 +25,10 @@ if (settings.read('client-options')) {
}
}
function httpRoute(req, res) {
res.sendFile = (path)=>{
@ -104,7 +109,7 @@ server.on('error', (e)=>{
server.listen(settings.read('port') || 8080)
http.get(settings.appAddresses()[0] + '/osc-ping', ()=>{}).on('error', ()=>{httpCheck(false)})
http.get(settings.appAddresses()[0] + '/osc-ping', {auth: settings.read('authentication')},()=>{}).on('error', ()=>{httpCheck(false)})
httpCheckTimeout = setTimeout(()=>{httpCheck(false)}, 5000)
function httpCheck(ok){
if (!httpCheckTimeout) return