2015-12-29 18:32:37 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var http = require('http');
|
|
|
|
var https = require('https');
|
|
|
|
var express = require('express');
|
|
|
|
|
|
|
|
var middleware = module.exports.middleware = require('./middleware');
|
|
|
|
|
|
|
|
module.exports.create = function (opts) {
|
|
|
|
var httpsOptions = opts.httpsOptions || require('localhost.daplie.com-certificates');
|
|
|
|
var results = {
|
|
|
|
plainServers: []
|
|
|
|
, tlsServers: []
|
|
|
|
};
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
app.use('/', middleware(opts));
|
|
|
|
|
|
|
|
(opts.plainPorts||[]).forEach(function (plainPort) {
|
|
|
|
var plainServer = http.createServer();
|
|
|
|
plainServer.__plainPort = plainPort;
|
2015-12-30 03:36:14 +00:00
|
|
|
plainServer.on('request', app);
|
2015-12-29 18:32:37 +00:00
|
|
|
results.plainServers.push(plainServer);
|
|
|
|
});
|
|
|
|
|
|
|
|
(opts.tlsPorts||[]).forEach(function (tlsPort) {
|
|
|
|
var tlsServer = https.createServer(httpsOptions);
|
|
|
|
tlsServer.__tlsPort = tlsPort;
|
2015-12-30 03:36:14 +00:00
|
|
|
tlsServer.on('request', app);
|
2015-12-29 18:32:37 +00:00
|
|
|
results.tlsServers.push(tlsServer);
|
|
|
|
});
|
|
|
|
|
|
|
|
function onListen() {
|
|
|
|
/*jshint validthis: true*/
|
|
|
|
var server = this;
|
|
|
|
var addr = server.address();
|
|
|
|
var proto = 'honorCipherOrder' in server ? 'https' : 'http';
|
|
|
|
|
|
|
|
console.info('Listening on ' + proto + '://' + addr.address + ':' + addr.port);
|
|
|
|
}
|
|
|
|
|
|
|
|
process.nextTick(function () {
|
|
|
|
results.plainServers.forEach(function (plainServer) {
|
2015-12-30 16:48:33 +00:00
|
|
|
plainServer.on('error', function (err) {
|
|
|
|
plainServer.error = err;
|
|
|
|
console.warn("[HP loop] Error with plain HTTP server:");
|
|
|
|
console.warn(err.stack);
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
plainServer.listen(
|
|
|
|
plainServer.__plainPort.internal || plainServer.__plainPort.port
|
|
|
|
, plainServer.__plainPort.address || '0.0.0.0'
|
|
|
|
, onListen
|
|
|
|
);
|
|
|
|
} catch(e) {
|
|
|
|
plainServer.error = e;
|
|
|
|
console.warn("[HP loop] Could not create plain HTTP listener:");
|
|
|
|
console.warn(e.stack);
|
|
|
|
}
|
2015-12-29 18:32:37 +00:00
|
|
|
});
|
|
|
|
results.tlsServers.forEach(function (tlsServer) {
|
2015-12-30 16:48:33 +00:00
|
|
|
tlsServer.on('error', function (err) {
|
|
|
|
tlsServer.error = err;
|
|
|
|
console.warn("[HP loop] Error with HTTPS server:");
|
|
|
|
console.warn(err.stack);
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
tlsServer.listen(
|
|
|
|
tlsServer.__tlsPort.internal || tlsServer.__tlsPort.port
|
|
|
|
, tlsServer.__tlsPort.address || '0.0.0.0'
|
|
|
|
, onListen
|
|
|
|
);
|
|
|
|
} catch(e) {
|
|
|
|
tlsServer.error = e;
|
|
|
|
console.warn("[HP loop] Could not create HTTPS listener:");
|
|
|
|
console.warn(e.stack);
|
|
|
|
}
|
2015-12-29 18:32:37 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-12-30 06:35:21 +00:00
|
|
|
results.key = opts.key;
|
|
|
|
results.value = opts.value;
|
2015-12-30 08:46:22 +00:00
|
|
|
results.loopbackHostname = opts.loopbackHostname;
|
|
|
|
results.loopbackPrefix = opts.loopbackPrefix;
|
2015-12-30 06:35:21 +00:00
|
|
|
|
2015-12-29 18:32:37 +00:00
|
|
|
return results;
|
|
|
|
};
|