server: add --use-ssl option

This commit is contained in:
bordun 2020-05-05 14:50:17 +02:00
parent ce8d8dcbd9
commit faab8d9ea1
8 changed files with 94 additions and 16 deletions

View file

@ -2,8 +2,8 @@ var ifaces = require('os').networkInterfaces(),
address = Object.values(ifaces)
.reduce((a,b)=>a.concat(b), [])
.filter(i=>i.family === 'IPv4')
.map(i=>'http://' + i.address + ':')
.map(i=>i.address + ':')
module.exports = (port)=>{
return address.map(x=>x + port)
module.exports = (proto, port)=>{
return address.map(x=>proto + x + port)
}

View file

@ -38,7 +38,7 @@ class DocsServer extends EventEmitter {
})
this.server.listen(0).on('listening', ()=>{
this.port = this.server.address().port
console.log('(INFO) Documentation server started, docs available at \n ' + address(this.port).join('\n '))
console.log('(INFO) Documentation server started, docs available at \n ' + address('http://', this.port).join('\n '))
this.emit('ready')
})
@ -51,11 +51,11 @@ class DocsServer extends EventEmitter {
if (!this.started || !this.port) {
this.start()
this.on('ready', ()=>{
open(address(this.port)[0] + this.home)
open(address('http://', this.port)[0] + this.home)
console.log('(INFO) Opening documentation with default browser...')
})
} else {
open(address(this.port)[0] + this.home)
open(address('http://', this.port)[0] + this.home)
console.log('(INFO) Opening documentation with default browser...')
}

View file

@ -57,6 +57,15 @@ module.exports = function(options={}) {
}
})
window.webContents.on('certificate-error', function(event, url, error, certificate, callback) {
if (error === 'net::ERR_CERT_AUTHORITY_INVALID') {
// self signed certificate is ok
event.preventDefault()
callback(true)
}
})
window.webContents.on('login', function(event, request, authInfo, callback) {
event.preventDefault()
var [name, pwd] = settings.read('authentication').split(':')

View file

@ -98,6 +98,7 @@ module.exports = {
}
},
'instance-name': {type: 'string', describe: 'used to differenciate multiple instances in a zeroconf network'},
'use-ssl': {type: 'boolean', describe: 'set to true to use HTTPS protocol instead of HTTP (a self-signed certificate will be created)'},
// cli only
'disable-gpu': {type: 'boolean', describe: 'disable hardware acceleration', launcher: false},

View file

@ -1,12 +1,24 @@
var settings = require('./settings'),
http = require('http')
function createServer(route) {
if (settings.read('use-ssl')) {
http = require('https')
require('./ssl')
return http.createServer(settings.read('ssl-certificate'), route)
} else {
return http.createServer(route)
}
}
var urlparser = require('url'),
path = require('path'),
fs = require('fs'),
send = require('send'),
http = require('http'),
replaceStream = require('replacestream'),
settings = require('./settings'),
auth = require('./auth'),
server = http.createServer(auth ? auth.check(httpRoute) : httpRoute),
server = createServer(auth ? auth.check(httpRoute) : httpRoute),
Ipc = require('./ipc/server'),
ipc = new Ipc(server),
theme = require('./theme').init(),
@ -26,9 +38,6 @@ if (settings.read('client-options')) {
}
function httpRoute(req, res) {
res.sendFile = (path)=>{
@ -109,15 +118,22 @@ server.on('error', (e)=>{
server.listen(settings.read('port') || 8080)
http.get(settings.appAddresses()[0] + '/osc-ping', {auth: settings.read('authentication')},()=>{}).on('error', ()=>{httpCheck(false)})
http.get(settings.appAddresses()[0] + '/osc-ping', {
auth: settings.read('authentication'),
rejectUnauthorized: false
},()=>{}).on('error', ()=>{httpCheck(false)})
httpCheckTimeout = setTimeout(()=>{httpCheck(false)}, 5000)
function httpCheck(ok){
function httpCheck(ok, error){
if (!httpCheckTimeout) return
clearTimeout(httpCheckTimeout)
httpCheckTimeout = null
if (ok) {
console.log('(INFO) Server started, app available at \n ' + settings.appAddresses().join('\n '))
} else {
if (error) {
console.error('(ERROR, HTTP) Server setup error: ' + error.message)
}
console.error('(ERROR, HTTP) Could not setup http server, maybe try a different port ?')
}
}

View file

@ -1,7 +1,7 @@
var path = require('path'),
fs = require('fs'),
yargs = require('yargs'),
infos = require('../package.json'),
infos = require('../../package.json'),
options = require('./options'),
address = require('./address')
@ -156,5 +156,5 @@ module.exports = {
cli: cli,
configPath: baseDir,
infos: infos,
appAddresses: ()=>address(settings.options.port || 8080)
appAddresses: ()=>address(settings.options.ssl ? 'https://' : 'http://', settings.options.port || 8080)
}

51
src/server/ssl.js Normal file
View file

@ -0,0 +1,51 @@
var settings = require('./settings'),
forge = require('node-forge'),
pki = forge.pki,
certificate = settings.read('ssl-certificate')
forge.options.usePureJavaScript = true
function createCertificate() {
console.log('(INFO) Creating self signed ssl certificate...')
var keys = pki.rsa.generateKeyPair(2048),
cert = pki.createCertificate()
cert.publicKey = keys.publicKey
cert.validity.notBefore = new Date()
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1)
var attrs = [
{name:'commonName',value: settings.infos.name},
{name:'subjectAltName',value:[]}
]
cert.setSubject(attrs)
cert.setIssuer(attrs)
cert.sign(keys.privateKey)
settings.write('ssl-certificate', {
key: pki.privateKeyToPem(keys.privateKey),
cert: pki.certificateToPem(cert)
})
}
if (!certificate) {
createCertificate()
} else {
var cert = pki.certificateFromPem(certificate.cert)
if (new Date() > new Date(cert.validity.notAfter)) {
console.log('(INFO) Self-signed ssl certificate has expired')
createCertificate()
} else {
console.log('(INFO) Using self-signed ssl certificate in cache')
}
}