-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
406 lines (344 loc) · 10.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
'use strict';
/*
* index.js: Create an http AND/OR an https server and call the same request handler.
*
* (C) 2013, Charlie Robbins.
*
*/
const
fs = require('fs').promises,
tls = require('tls'),
path = require('path'),
constants = require('constants'),
connected = require('connected'),
errs = require('errs'),
assign = require('object-assign');
const pemFormat = /-----BEGIN/;
const CIPHERS = [
'ECDHE-RSA-AES256-SHA384',
'DHE-RSA-AES256-SHA384',
'ECDHE-RSA-AES256-SHA256',
'DHE-RSA-AES256-SHA256',
'ECDHE-RSA-AES128-SHA256',
'DHE-RSA-AES128-SHA256',
'HIGH',
'!aNULL',
'!eNULL',
'!EXPORT',
'!DES',
'!RC4',
'!MD5',
'!PSK',
'!SRP',
'!CAMELLIA'
].join(':');
const secureOptions = constants.SSL_OP_NO_SSLv3;
/**
* function createServers (dispatch, options, callback)
* Creates and listens on both HTTP and HTTPS servers.
*/
module.exports = async function createServers(options, listening) {
try {
options = normalizeOptions(options);
} catch (err) {
return listening(err);
}
const [httpResult, httpsResult, http2Result] = await Promise.allSettled([
createHttp(options.http, options.log),
createHttps(options.https, options.log),
createHttps(options.http2, options.log, true)
])
const servers = {};
if (httpResult.value) servers.http = httpResult.value;
if (httpsResult.value) servers.https = httpsResult.value;
if (http2Result.value) servers.http2 = http2Result.value;
const errorSource = httpResult.reason || httpsResult.reason || http2Result.reason;
if (errorSource) {
if (Array.isArray(errorSource)) {
errorSource = errorSource[0];
}
return listening(
errs.create({
message: errorSource && errorSource.message,
http2: http2Result.reason,
https: httpsResult.reason,
http: httpResult.reason
}),
servers
);
}
listening(undefined, servers);
};
function normalizeOptions(options) {
const http = normalizeHttpOptions(options.http, options);
const https = normalizeHttpsOptions(options.https, options);
const http2 = normalizeHttpsOptions(options.http2, options);
if (!http && !https && !http2) {
throw new Error('http, https, and/or http2 are required options');
}
return {
http,
https,
http2,
log: options.log || function() {}
};
}
function normalizeHttpOptions(httpConfig, baseConfig) {
if (typeof httpConfig === 'undefined') return;
if (Array.isArray(httpConfig)) {
return httpConfig.map(cfg => normalizeHttpOptions(cfg, baseConfig));
}
let port =
typeof httpConfig === 'object' && 'port' in httpConfig
? httpConfig.port
: httpConfig;
if (typeof port === 'undefined') {
port = 80;
}
const http = {
host: httpConfig.host || baseConfig.host,
port: +port,
handler: httpConfig.handler || baseConfig.handler,
timeout: httpConfig.timeout || baseConfig.timeout,
keepAliveTimeout: httpConfig.keepAliveTimeout || baseConfig.keepAliveTimeout
};
if (!http.handler) {
throw new Error('handler option is required');
}
return http;
}
function normalizeHttpsOptions(httpsConfig, baseConfig) {
if (typeof httpsConfig === 'undefined') return;
if (Array.isArray(httpsConfig)) {
return httpsConfig.map(cfg => normalizeHttpsOptions(cfg, baseConfig));
}
const https = {
...httpsConfig,
host: httpsConfig.host || baseConfig.host,
port: +('port' in httpsConfig ? httpsConfig.port : 443),
handler: httpsConfig.handler || baseConfig.handler,
timeout: httpsConfig.timeout || baseConfig.timeout,
keepAliveTimeout: httpsConfig.keepAliveTimeout || baseConfig.keepAliveTimeout
};
if (!https.handler) {
throw new Error('handler option is required');
}
return https;
}
function normalizeCertContent(root, cert, key) {
// Node accepts an array of certs, which must match up with an array of keys.
// The user may instead intend for an array passed into cert to represent
// a cert chain they want to concatenate. Therefore, if key is not an array,
// we'll assume the latter.
if (Array.isArray(cert)) {
if (Array.isArray(key)) {
// This is an array of certs/chains with corresponding keys
return normalizeCertChainList(root, cert);
} else {
// This is a single cert chain
return normalizeCertChain(root, cert);
}
}
return normalizePEMContent(root, cert);
}
function normalizeCertChainList(root, data) {
// If this is an array, treat like an array of bundles, otherwise a single
// bundle
return Array.isArray(data)
? Promise.all(data.map(function(item) {
return normalizeCertChain(root, item);
}))
: normalizePEMContent(root, data);
}
async function normalizeCertChain(root, data) {
// A chain can be an array, which we concatenate together into one PEM,
// an already-concatenated chain, or a single PEM
const content = await normalizePEMContent(root, data);
return Array.isArray(content) ? content.join('\n') : content;
}
function normalizeCA(root, ca) {
if (ca && !Array.isArray(ca)) {
ca = [ca];
}
return ca && Promise.all(ca.map(normalizePEMContent.bind(null, root)));
}
/**
* function normalizePEMContent(root, file)
* Returns the contents of `file` verbatim if it is determined to be
* certificate material and not a file path. Otherwise, returns the
* certificate material read from that file path.
*/
function normalizePEMContent(root, file) {
if (Array.isArray(file))
return Promise.all(file.map(function map(item) {
return normalizePEMContent(root, item);
}));
//
// Assumption that this is a Buffer, a PEM file, or something broken
//
if (typeof file !== 'string' || pemFormat.test(file)) {
return file;
}
return fs.readFile(path.resolve(root, file));
}
function normalizeCiphers(ciphers) {
ciphers = ciphers || CIPHERS;
//
// Remark: If an array is passed in lets join it like we do the defaults
//
if (Array.isArray(ciphers)) {
ciphers = ciphers.join(':');
}
return ciphers;
}
async function getSNIHandler(sslOpts) {
const sniHosts = Object.keys(sslOpts.sni);
// Pre-compile regexps for the hostname
const hostRegexps = sniHosts.map(function(host) {
return host === '*' ? /.*/ : new RegExp(
'^' +
host
.replace('.', '\\.') // Match dots, not wildcards
.replace('*\\.', '(?:.*\\.)?') + // Handle optional wildcard sub-domains
'$',
'i'
);
});
// Prepare secure contexts ahead-of-time
const hostSecureContexts = await Promise.all(sniHosts.map(async function(host) {
var hostOpts = sslOpts.sni[host];
var root = hostOpts.root || sslOpts.root;
const [key, cert, ca] = await Promise.all([
normalizePEMContent(root, hostOpts.key),
normalizeCertContent(root, hostOpts.cert),
normalizeCA(root, hostOpts.ca || sslOpts.ca)
])
return tls.createSecureContext(
assign({}, sslOpts, hostOpts, {
key,
cert,
ca,
ciphers: normalizeCiphers(hostOpts.ciphers || sslOpts.ciphers),
honorCipherOrder: !!(
hostOpts.honorCipherOrder || sslOpts.honorCipherOrder
),
secureProtocol: 'SSLv23_method',
secureOptions: secureOptions
})
);
}));
return function(hostname, cb) {
var matchingHostIdx = sniHosts.findIndex(function(candidate, i) {
return hostRegexps[i].test(hostname);
});
if (matchingHostIdx === -1) {
return void cb(new Error('Unrecognized hostname: ' + hostname));
}
cb(null, hostSecureContexts[matchingHostIdx]);
};
}
//
// ### function createHttp (httpConfig)
// Attempts to create and listen on the the HTTP server.
//
async function createHttp(httpConfig, log) {
if (typeof httpConfig === 'undefined') {
log('http | no options.http; no server');
return null;
}
if (Array.isArray(httpConfig)) {
return await createMultiple(createHttp, httpConfig, log);
}
const
server = require('http').createServer(httpConfig.handler),
port = httpConfig.port;
commonPostCreateSetup(httpConfig, server);
const args = [server, port];
if (httpConfig.host) {
args.push(httpConfig.host);
}
log('http | try listen ' + port);
return new Promise((resolve, reject) => {
args.push(function listener(err) {
err ? reject(err) : resolve(server);
});
connected.apply(null, args);
});
}
//
// ### function createHttps ()
// Attempts to create and listen on the HTTPS server.
//
async function createHttps(ssl, log, h2) {
if (typeof ssl === 'undefined') {
log('https | no options.https; no server');
return null;
}
if (Array.isArray(ssl)) {
return await createMultiple(createHttps, ssl, log, h2);
}
const [key, cert, ca] = await Promise.all([
normalizePEMContent(ssl.root, ssl.key),
normalizeCertContent(ssl.root, ssl.cert, ssl.key),
normalizeCA(ssl.root, ssl.ca)
]);
const finalHttpsOptions = assign({}, ssl, {
key,
cert,
ca,
//
// Properly expose ciphers for an A+ SSL rating:
// https://certsimple.com/blog/a-plus-node-js-ssl
//
ciphers: normalizeCiphers(ssl.ciphers),
honorCipherOrder: !!ssl.honorCipherOrder,
//
// Protect against the POODLE attack by disabling SSLv3
// @see http://googleonlinesecurity.blogspot.nl/2014/10/this-poodle-bites-exploiting-ssl-30.html
//
secureProtocol: 'SSLv23_method',
secureOptions: secureOptions
});
if (ssl.sni && !finalHttpsOptions.SNICallback) {
finalHttpsOptions.SNICallback = await getSNIHandler(ssl);
}
const port = ssl.port;
log('https | listening on %d', port);
const server = h2
? require('http2').createSecureServer(finalHttpsOptions, ssl.handler)
: require('https').createServer(finalHttpsOptions, ssl.handler);
commonPostCreateSetup(ssl, server);
const args = [server, port];
if (ssl.host) {
args.push(ssl.host);
}
return new Promise((resolve, reject) => {
args.push(function listener(err) {
err ? reject(err) : resolve(server);
});
connected.apply(null, args);
});
}
async function createMultiple(createFn, configArray, log) {
const errorsOrServers = await Promise.allSettled(
configArray.map(cfg => createFn(cfg, log))
);
const errors = [], servers = [];
for (const result of errorsOrServers) {
result.reason && errors.push(result.reason);
result.value && servers.push(result.value);
}
if (errors.length) {
throw errors;
} else {
return servers;
}
}
function commonPostCreateSetup({ timeout, keepAliveTimeout }, server) {
if (typeof timeout === 'number') {
server.setTimeout(timeout);
}
if (typeof keepAliveTimeout === 'number') {
server.keepAliveTimeout = keepAliveTimeout;
}
}