Verified Commit 7ae96cf3 authored by Aral Balkan's avatar Aral Balkan
Browse files

Implement https-server

parent 41a32b33
var async = require('async-collection')
var mkdirp = require('mkdirp')
var http = require('http')
var path = require('path')
var pump = require('pump')
var net = require('net')
var fs = require('fs')
var os = require('os')
var selfsigned = require('selfsigned')
var getPort = require('get-port')
var CONFIG_DIR = path.join(os.homedir(), '.config/bankai')
var CERT_NAME = 'cert.pem'
var KEY_NAME = 'key.pem'
var CERT_LOCATION = path.join(CONFIG_DIR, CERT_NAME)
var KEY_LOCATION = path.join(CONFIG_DIR, KEY_NAME)
var httpsServer = require('@ind.ie/https-server')
exports.createServer = createDevServer
......@@ -23,9 +13,9 @@ function createDevServer (connectionHandler) {
var createSecureServer
try {
createSecureServer = require('http2').createSecureServer
createSecureServer = httpsServer.createSecureServer
} catch (e) {
createSecureServer = require('https').createServer
createSecureServer = httpsServer.createServer
}
return {
......@@ -50,25 +40,19 @@ function createDevServer (connectionHandler) {
}
function onHttpListen () {
createKeys(function (err, keys) {
if (err) throw err
var cert = keys.cert
var key = keys.key
getPort({port: 4443}).then(function (port) {
httpsPort = port
var serverOpts = { cert, key, allowHTTP1: true }
var http2Server = createSecureServer(serverOpts, connectionHandler)
http2Server.keepAliveTimeout = 0
http2Server.timeout = 0
http2Server.listen(httpsPort, function () {
if (onlisten) onlisten()
})
getPort({port: 4443}).then(function (port) {
httpsPort = port
var serverOpts = { allowHTTP1: true }
var http2Server = createSecureServer(serverOpts, connectionHandler)
http2Server.keepAliveTimeout = 0
http2Server.timeout = 0
http2Server.listen(httpsPort, function () {
if (onlisten) onlisten()
})
.catch(function (err) {
throw err
})
})
.catch(function (err) {
throw err
})
}
}
......@@ -105,75 +89,3 @@ function createDevServer (connectionHandler) {
}
}
}
// Read keys from ~/.config/bankai, or create new ones if they don't exist.
function createKeys (cb) {
mkdirp(CONFIG_DIR, function (err) {
if (err) return cb(err)
fs.readdir(CONFIG_DIR, function (err, files) {
if (err) return cb(err)
var keys = {}
// check if both files exist
if (files.indexOf(KEY_NAME) !== -1 && files.indexOf(CERT_NAME) !== -1) {
return async.parallel([
function (done) {
fs.readFile(CERT_LOCATION, function (err, buf) {
if (err) return done(err)
keys.cert = buf
done()
})
},
function (done) {
fs.readFile(KEY_LOCATION, function (err, buf) {
if (err) return done(err)
keys.key = buf
done()
})
}
], function (err) {
if (err) return cb(err)
cb(null, keys)
})
}
var opts = {
days: 2048,
algorithm: 'sha256',
extensions: [
{
name: 'subjectAltName',
altNames: [
{
type: 2, // DNSName
value: 'localhost'
}
]
}
]
}
selfsigned.generate([{ name: 'commonName', value: 'localhost' }], opts, function (err, keys) {
if (err) return cb(err)
keys = {
key: keys.private,
cert: keys.cert
}
async.parallel([
function (done) {
fs.writeFile(KEY_LOCATION, keys.key, done)
},
function (done) {
fs.writeFile(CERT_LOCATION, keys.cert, done)
}
], function (err) {
if (err) return cb(err)
cb(null, keys)
})
})
})
})
}
......@@ -17,6 +17,7 @@
"keywords": [],
"dependencies": {
"@choojs/findup": "^0.2.0",
"@ind.ie/https-server": "^2.0.0",
"ansi-diff": "^1.0.10",
"ansi-escape-sequences": "^4.0.0",
"ansi-scrollbox": "^0.2.0",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment