Permalink
Browse files

feat: improve ssl handling

  • Loading branch information...
adamkdean committed Jan 13, 2017
1 parent e1d6c58 commit 80073ebff51b00c5e789fde8d47455de9c44dad3
Showing with 57 additions and 10 deletions.
  1. +56 −9 dadi/lib/api/index.js
  2. +1 −1 dadi/lib/index.js
@@ -19,9 +19,17 @@ var Api = function () {
// permanently bind context to listener
this.listener = this.listener.bind(this)

this.protocol = config.get('server.protocol') || 'http'
this.protocol = config.get('server.protocol')
this.redirectPort = config.get('server.redirectPort')

if (this.protocol === 'http') {
this.httpInstance = http.createServer(this.listener)
} else if (this.protocol === 'https') {
// Redirect http to https
if (this.redirectPort > 0) {
this.redirectInstance = http.createServer(this.redirectListener)
}

if (this.protocol === 'https') {
var readFileSyncSafe = (path) => {
try { return fs.readFileSync(path) } catch (ex) {}
return null
@@ -52,7 +60,7 @@ var Api = function () {
// we need to catch any errors resulting from bad parameters
// such as incorrect passphrase or no passphrase provided
try {
this.serverInstance = https.createServer(serverOptions, this.listener)
this.httpsInstance = https.createServer(serverOptions, this.listener)
} catch (ex) {
var exPrefix = 'error starting https server: '
switch (ex.message) {
@@ -64,9 +72,6 @@ var Api = function () {
throw new Error(exPrefix + ex.message)
}
}
} else {
// default to http
this.serverInstance = http.createServer(this.listener)
}
}

@@ -117,8 +122,25 @@ Api.prototype.unuse = function (path) {
* @return http.Server
* @api public
*/
Api.prototype.listen = function (port, host, backlog, done) {
return this.serverInstance.listen(port, host, backlog, done)
Api.prototype.listen = function (backlog, done) {
var port = config.get('server.port')
var host = config.get('server.host')
var redirectPort = config.get('server.redirectPort')

// If http only, return the http instance
if (this.httpInstance) {
return this.httpInstance.listen(port, host, backlog, done)
}

// If http should redirect to https, listen but don't return
if (this.redirectInstance) {
this.redirectInstance.listen(redirectPort, host, backlog, done)
}

// If https enabled, return the https instance
if (this.httpsInstance) {
return this.httpsInstance.listen(port, host, backlog, done)
}
}

/**
@@ -129,7 +151,15 @@ Api.prototype.listen = function (port, host, backlog, done) {
*/
Api.prototype.close = function (done) {
try {
this.serverInstance.close(done)
if (this.redirectInstance) {
this.redirectInstance.close()
}

if (this.httpInstance) {
this.httpInstance.close(done)
} else if (this.httpsInstance) {
this.httpsInstance.close(done)
}
} catch (ex) {
console.log('error closing server:', ex)
done()
@@ -175,6 +205,23 @@ Api.prototype.listener = function (req, res) {
doStack(0)()
}

/**
* listener function to be passed to node's `createServer`
* @param {http.IncomingMessage} req
* @param {http.ServerResponse} res
* @return undefined
* @api public
*/
Api.prototype.redirectListener = function (req, res) {
var port = config.get('server.port')
var hostname = req.headers.host.split(':')[0]
var location = 'https://' + hostname + ':' + port + req.url

res.setHeader('Location', location)
res.statusCode = 301
res.end()
}

/**
* Check if any of the registered routes match the current url, if so populate `req.params`
* @param {String} path
@@ -192,7 +192,7 @@ Server.prototype.start = function (done) {
search(self)

// start listening
var server = this.server = app.listen(config.get('server.port'), config.get('server.host'))
var server = this.server = app.listen()

server.on('listening', function () { onListening(this) })
server.on('error', onError)

0 comments on commit 80073eb

Please sign in to comment.