Skip to content

Commit 8b7535f

Browse files
committed
add passphrase option
add a shim to enable configuring ssl passphrase update readme
1 parent 940399b commit 8b7535f

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ Then you need to run the server with `-S` for enabling SSL and `-C` for your cer
102102
http-server -S -C cert.pem
103103
```
104104

105+
If you wish to use a passphrase with your private key you can include one in the openssl command via the -passout parameter (using password of foobar)
106+
107+
108+
e.g.
109+
`openssl req -newkey rsa:2048 -passout pass:foobar -keyout key.pem -x509 -days 365 -out cert.pem`
110+
111+
For security reasons rather than the command line http-server will read this from the `NODE_HTTP_SERVER_SSL_PASSPHRASE` environment variable.
112+
113+
105114
This is what should be output if successful:
106115

107116
``` sh

bin/http-server

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ if (argv.h || argv.help) {
6161
var port = argv.p || argv.port || parseInt(process.env.PORT, 10),
6262
host = argv.a || '0.0.0.0',
6363
ssl = argv.S || argv.ssl,
64+
sslPassphrase = process.env.NODE_HTTP_SERVER_SSL_PASSPHRASE,
6465
proxy = argv.P || argv.proxy,
6566
utc = argv.U || argv.utc,
6667
version = argv.v || argv.version,
@@ -143,7 +144,8 @@ function listen(port) {
143144
if (ssl) {
144145
options.https = {
145146
cert: argv.C || argv.cert || 'cert.pem',
146-
key: argv.K || argv.key || 'key.pem'
147+
key: argv.K || argv.key || 'key.pem',
148+
passphrase: sslPassphrase,
147149
};
148150
try {
149151
fs.lstatSync(options.https.cert);

lib/http-server.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ function HttpServer(options) {
173173
serverOptions.https = options.https;
174174
}
175175

176-
this.server = union.createServer(serverOptions);
176+
this.server = options.https.passphrase
177+
// if passphrase is set, shim must be used as union does not support
178+
? require('./shims/https-server-shim')(serverOptions)
179+
: union.createServer(serverOptions);
180+
177181
if (options.timeout !== undefined) {
178182
this.server.setTimeout(options.timeout);
179183
}

lib/shims/https-server-shim.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var https = require('https');
2+
var fs = require('fs');
3+
var RoutingStream = require('union/lib/routing-stream');
4+
5+
module.exports = function (options) {
6+
var isArray = Array.isArray(options.after),
7+
credentials;
8+
9+
if (!options) {
10+
throw new Error('options is required to create a server');
11+
}
12+
13+
function requestHandler(req, res) {
14+
var routingStream = new RoutingStream({
15+
before: options.before,
16+
buffer: options.buffer,
17+
after: isArray && options.after.map(function (After) {
18+
return new After;
19+
}),
20+
request: req,
21+
response: res,
22+
limit: options.limit,
23+
headers: options.headers
24+
});
25+
26+
routingStream.on('error', function (err) {
27+
var fn = options.onError || core.errorHandler;
28+
fn(err, routingStream, routingStream.target, function () {
29+
routingStream.target.emit('next');
30+
});
31+
});
32+
33+
req.pipe(routingStream);
34+
}
35+
36+
var serverOptions,
37+
credentials;
38+
39+
serverOptions = options.https;
40+
if (!serverOptions.key || !serverOptions.cert) {
41+
throw new Error('Both options.' + key + '.`key` and options.' + key + '.`cert` are required.');
42+
}
43+
44+
credentials = {
45+
key: fs.readFileSync(serverOptions.key),
46+
cert: fs.readFileSync(serverOptions.cert),
47+
passphrase: process.env.NODE_HTTP_SERVER_SSL_PASSPHRASE
48+
};
49+
50+
if (serverOptions.ca) {
51+
serverOptions.ca = !Array.isArray(serverOptions.ca)
52+
? [serverOptions.ca]
53+
: serverOptions.ca
54+
55+
credentials.ca = serverOptions.ca.map(function (ca) {
56+
return fs.readFileSync(ca);
57+
});
58+
}
59+
60+
return https.createServer(credentials, requestHandler);
61+
};

0 commit comments

Comments
 (0)