From d50f2507cf83262adbc6bd5ce9680ed3e0a3c019 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 12 Dec 2015 07:11:31 +0000 Subject: [PATCH] static server updates --- tests/serve-acme-challenges.js | 96 ++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 45 deletions(-) diff --git a/tests/serve-acme-challenges.js b/tests/serve-acme-challenges.js index ef5709b..c23c1fe 100644 --- a/tests/serve-acme-challenges.js +++ b/tests/serve-acme-challenges.js @@ -2,13 +2,10 @@ var fs = require('fs'); var path = require('path'); -var localCerts = require('localhost.daplie.com-certificates'); var https = require('https'); var http = require('http'); var express = require('express'); var app = express(); -var server; -var insecureServer; var config = require('./config'); @@ -32,32 +29,10 @@ function getSecureContext(domainname, opts, cb) { } -// -// SSL Certificates -// -var options = { - requestCert: false -, rejectUnauthorized: true - - // If you need to use SNICallback you should be using io.js >= 1.x (possibly node >= 0.12) -, SNICallback: function (domainname, cb) { - var secureContext = getSecureContext(domainname); - cb(null, secureContext); - } - // If you need to support HTTP2 this is what you need to work with -//, NPNProtocols: ['http/2.0', 'http/1.1', 'http/1.0'] -//, NPNProtocols: ['http/1.1'] -, key: null -, cert: null -//, ca: null -}; -options.key = localCerts.key; -options.cert = localCerts.cert; - - // log the requests app.use('/', function (req, res, next) { - console.log(req.method + ' ' + req.headers['host'], req.protocol + req.url); + console.log('[' + req.ip + ']', req.method + ' ' + req.headers.host, req.protocol + req.url); + next(); }); // handle static requests to /.well-known/acme-challenge app.use( @@ -66,22 +41,53 @@ app.use( ); -// Start the tls sni server -server = https.createServer(options); -server.on('error', function (err) { - console.error(err); -}); -server.listen(config.tlsSni01Port, function () { - console.log('Listening'); -}); -server.on('request', app); +function serveHttps() { + // + // SSL Certificates + // + var server; + var localCerts = require('localhost.daplie.com-certificates'); + var options = { + requestCert: false + , rejectUnauthorized: true -// Start the http server -insecureServer = http.createServer(); -insecureServer.on('error', function (err) { - console.error(err); -}); -insecureServer.listen(config.http01Port, function () { - console.log('Listening'); -}); -insecureServer.on('request', app); + // If you need to use SNICallback you should be using io.js >= 1.x (possibly node >= 0.12) + , SNICallback: function (domainname, cb) { + var secureContext = getSecureContext(domainname); + cb(null, secureContext); + } + // If you need to support HTTP2 this is what you need to work with + //, NPNProtocols: ['http/2.0', 'http/1.1', 'http/1.0'] + //, NPNProtocols: ['http/1.1'] + , key: localCerts.key + , cert: localCerts.cert + //, ca: null + }; + + // Start the tls sni server4 + server = https.createServer(options); + server.on('error', function (err) { + console.error(err); + }); + server.on('request', app); + server.listen(config.tlsSni01Port, function () { + console.log('[https] Listening', server.address()); + }); +} + +function serveHttp() { + // Start the http server4 + var insecureServer = http.createServer(); + insecureServer.on('error', function (err) { + console.error(err); + }); + // note that request handler must be attached *before* and handle comes in + insecureServer.on('request', app); + insecureServer.listen(config.http01Port, function () { + console.log('[http] Listening', insecureServer.address()); + }); +} + + +serveHttps(); +serveHttp();