diff options
author | Noah Loomans <noahloomans@gmail.com> | 2017-03-01 12:37:31 +0100 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2017-03-01 12:37:31 +0100 |
commit | 2e24274e598533ce2b3c4df861ba30fc187fda06 (patch) | |
tree | ce08b4ba08e204d8145a8853f72bb91ee230600c /bin | |
parent | bd669ae4c098c6fc4d8327e6f3f4bdb5397d87f2 (diff) | |
parent | 569828f54965979e6f98b7646a125584b157f071 (diff) |
Merge branch 'useHTTPS' into beta
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/www | 124 |
1 files changed, 59 insertions, 65 deletions
@@ -1,68 +1,45 @@ #!/usr/bin/env node -/** - * Module dependencies. - */ +const fs = require('fs') +const app = require('../app') +const http = require('http') +const https = require('https') -var fs = require('fs') -var app = require('../app') -var debug = require('debug')('rooster:server') -var http = require('http') -var https = require('https') +function setupHTTPS () { + const certificate = fs.readFileSync('/etc/letsencrypt/live/rooster.hetmml.nl/fullchain.pem', 'utf8') + const privateKey = fs.readFileSync('/etc/letsencrypt/live/rooster.hetmml.nl/privkey.pem', 'utf8') + const credentials = { key: privateKey, cert: certificate } -var shouldUseHTTPS = true -try { - fs.accessSync('/etc/letsencrypt/live/rooster.hetmml.nl/privkey.pem') -} catch (e) { - shouldUseHTTPS = false -} + const httpsPort = normalizePort(process.env.PORT_HTTPS || '3001') + const httpsServer = https.createServer(credentials, app) -var privateKey = '' -var certificate = '' -var credentials = {} + httpsServer.listen(httpsPort) + httpsServer.on('error', error => onError(error, httpsPort)) + httpsServer.on('listening', _ => onListening(httpsServer)) -if (shouldUseHTTPS) { - privateKey = fs.readFileSync('/etc/letsencrypt/live/rooster.hetmml.nl/privkey.pem', 'utf8') - certificate = fs.readFileSync('/etc/letsencrypt/live/rooster.hetmml.nl/fullchain.pem', 'utf8') - credentials = {key: privateKey, cert: certificate} + app.set('port', httpsPort) } -/** - * Get port from environment and store in Express. - */ - -var port = normalizePort(process.env.PORT || '3000') -var httpsPort -if (shouldUseHTTPS) httpsPort = normalizePort(process.env.PORT_HTTPS || '3001') -app.set('port', port) +function setupHTTPSRedirect () { + const httpPort = normalizePort(process.env.PORT || '3000') + const httpServer = http.createServer(redirectToHTTPS) -/** - * Create HTTP server. - */ - -var server = http.createServer(app) -var httpsServer -if (shouldUseHTTPS) httpsServer = https.createServer(credentials, app) + httpServer.listen(httpPort) + httpServer.on('error', error => onError(error, httpPort)) + httpServer.on('listening', _ => onListening(httpServer)) +} -/** - * Listen on provided port, on all network interfaces. - */ +function setupHTTP () { + const httpPort = normalizePort(process.env.PORT || '3000') + const httpServer = http.createServer(app) -server.listen(port) -server.on('error', onError) -server.on('listening', onListening) -if (shouldUseHTTPS) { - httpsServer.listen(httpsPort) - httpsServer.on('error', onError) - httpsServer.on('listening', onListening) + httpServer.listen(httpPort) + httpServer.on('error', error => onError(error, httpPort)) + httpServer.on('listening', _ => onListening(httpServer)) } -/** - * Normalize a port into a number, string, or false. - */ - function normalizePort (val) { - var port = parseInt(val, 10) + const port = parseInt(val, 10) if (isNaN(port)) { // named pipe @@ -77,16 +54,12 @@ function normalizePort (val) { return false } -/** - * Event listener for HTTP server "error" event. - */ - -function onError (error) { +function onError (error, port) { if (error.syscall !== 'listen') { throw error } - var bind = typeof port === 'string' + const bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port @@ -105,14 +78,35 @@ function onError (error) { } } -/** - * Event listener for HTTP server "listening" event. - */ - -function onListening () { - var addr = server.address() - var bind = typeof addr === 'string' +function onListening (server) { + const addr = server.address() + const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port - debug('Listening on ' + bind) + console.log('Listening on ' + bind) +} + +let useHTTPS = true +try { + fs.accessSync('/etc/letsencrypt/live/rooster.hetmml.nl/privkey.pem') +} catch (e) { + useHTTPS = false +} + +if (useHTTPS) { + try { + setupHTTPS() + setupHTTPSRedirect() + } catch (e) { + console.warn('NOT USING HTTPS! Error occured while setting up HTTPS') + setupHTTP() + } +} else { + console.warn('NOT USING HTTPS! Could not read /etc/letsencrypt/live/rooster.hetmml.nl/privkey.pem') + setupHTTP() +} + +function redirectToHTTPS (req, res) { + res.writeHead(302, { 'Location': 'https://' + req.headers['host'] + req.url }) + res.end() } |