Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ First, you need to make sure that [openssl](https://github.com/openssl/openssl)
openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem
```

You will be prompted with a few questions after entering the command. Use `127.0.0.1` as value for `Common name` if you want to be able to install the certificate in your OS's root certificate store or browser so that it is trusted.
You will be prompted with a few questions after entering the command. Use `localhost` as value for `Common Name` if you want to be able to install the certificate in your OS's root certificate store or browser so that it is trusted.

This generates a cert-key pair and it will be valid for 3650 days (about 10 years).

Expand All @@ -126,9 +126,8 @@ This is what should be output if successful:
``` sh
Starting up http-server, serving ./ through https
Available on:
https:127.0.0.1:8080
https:192.168.1.101:8080
https:192.168.1.104:8080
https://localhost:8080
https://192.168.1.101:8080
Hit CTRL-C to stop the server
```

Expand Down
23 changes: 17 additions & 6 deletions bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var colors = require('colors/safe'),
portfinder = require('portfinder'),
opener = require('opener'),
fs = require('fs'),
x509 = require('x509.js'),
argv = require('minimist')(process.argv.slice(2));
var ifaces = os.networkInterfaces();

Expand Down Expand Up @@ -138,23 +139,25 @@ function listen(port) {
}
}

var commonName;
if (ssl) {
options.https = {
cert: argv.C || argv.cert || 'cert.pem',
key: argv.K || argv.key || 'key.pem'
};
try {
fs.lstatSync(options.https.cert);
var c = x509.parseCert(fs.readFileSync(options.https.cert));
commonName = c.subject.commonName;
}
catch (err) {
logger.info(colors.red('Error: Could not find certificate ' + options.https.cert));
logger.info(colors.red('Error: Could not read certificate ' + err));
process.exit(1);
}
try {
fs.lstatSync(options.https.key);
fs.readFileSync(options.https.key);
}
catch (err) {
logger.info(colors.red('Error: Could not find private key ' + options.https.key));
logger.info(colors.red('Error: Could not read private key ' + err));
process.exit(1);
}
}
Expand All @@ -170,19 +173,27 @@ function listen(port) {
colors.yellow('\nAvailable on:')
].join(''));

var hosts = [];
if (argv.a && host !== '0.0.0.0') {
logger.info((' ' + protocol + canonicalHost + ':' + colors.green(port.toString())));
hosts.push(canonicalHost);
}
else {
if (ssl && commonName) {
hosts.push(commonName);
}
Object.keys(ifaces).forEach(function (dev) {
ifaces[dev].forEach(function (details) {
if (details.family === 'IPv4') {
logger.info((' ' + protocol + details.address + ':' + colors.green(port.toString())));
hosts.push(details.address);
}
});
});
}

hosts.forEach(function (host) {
logger.info((' ' + protocol + host + ':' + colors.green(port.toString())));
});

if (typeof proxy === 'string') {
logger.info('Unhandled requests will be served from: ' + proxy);
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@
"opener": "^1.5.1",
"portfinder": "^1.0.25",
"secure-compare": "3.0.1",
"union": "~0.5.0"
"union": "~0.5.0",
"x509.js": "^1.0.0"
},
"devDependencies": {
"common-style": "^3.0.0",
Expand Down
32 changes: 32 additions & 0 deletions test/fixtures/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Directory of testing fixtures

## To create fake certificates

```shell
# Create a fake CA cert (with key and cert in the same file)
openssl req -x509 -new \
-subj "/CN=Fake CA" \
-days 3650 \
-newkey rsa:2048 -nodes \
-keyout ca.pem -out ca.pem

# Create CSR
openssl req -new \
-subj "/CN=localhost" \
-days 3650 \
-newkey rsa:2048 -nodes \
-keyout key.pem -out csr.pem

# Sign the CSR to generate a certificate (with additional SubjectAltNames)
openssl x509 -req \
-in csr.pem \
-CA ca.pem -CAcreateserial \
-extensions SAN \
-extfile <(cat /etc/ssl/openssl.cnf \
<(printf "\n[SAN]\nsubjectAltName=DNS:localhost,IP:127.0.0.1")) \
-days 3650 -sha256 \
-out cert.pem

# Cleanup
rm ca.srl csr.pem
```
45 changes: 45 additions & 0 deletions test/fixtures/ca.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDqL6v04hS17g90
CaqBQ16qx7GqTGM/29udshsW/YCgLCq0KHcl+T4SwVZIG0xHFSuU5SHafXh3CVIv
8ZuQzaYZwZRF80If6Ti5MuPkzMyGJ7IsqBYjhRYvo+wt2WYvcA6GHbHrEn2YD0OB
2sCaFizRiEnVLb9cBfBs4VWpvUj5oMaEK8veC1Ojwb9Uq9PD/ScgNOWvMWDiMoXC
xOJDt1jx38+isQ7jmr+RL44AKOd2Y64NYx1PQm5JWoq1/dZKyB1jk8fA90TEiDdY
aVBdM4Lj3GFDjwOYgX4zirQJcB4LzxX8kgis//WHtjtGXHDaCd4S3xPM4Zzl7tzl
+k//7rgtAgMBAAECggEBANFRvu9pTH24xVNAeIiFgQ5A48qF8IhZqZjwY0pPWDLS
h3D0YlssxpDZApf83lcC0yuJCpNSZuRvDmkQGa56QibvYeqMHeSL/0l59TzC2WRo
Atfrfa5N/KCkciwhDzcDf9fcnvSwWFYb/okIz/JqM7EtkmDbPRmxrU6Esp6/M4T6
u9fDjzYFqShKaYVd8ykSXUBN0utvBZwdV1gcS/lysnmRykbkOTSLh35HRhfHb5L6
TggwqjuQTG5EOwpKqct3H2/J+wgoK0p5u7DsIi1UdbxzhaPk7tEfb9UBt6QpXlXr
qkhufSvyQJwSEqyjtfgbRHx2TaQxMUp8LT/COoCjefkCgYEA+4g9WybRJitu0E5Q
rntUELxHzKH4THKWdUjj1RChwy81YI+O6M+vYMGX62wF9JGhvSmQsTGdNaDk+Hpc
PY1IRljtZFuZOWVmSzPYRoTNjw+fl68ecRA1K+4OXN4k4QOcL+OvlRMH1xw0Bgey
8Hw52kI0loNEBFHAslb7i4XzCmMCgYEA7liOkgcM3tu7OdglvS4inRz71DcAsvu9
24CenWI4A5wRDnsOOFMXqIC0w/Zzl2EBCvgoc/syY43KB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jrg1jHU9CbklairX91krIHWOTx8D3
RMSIN1hpUF0zguPcYUOPA5iBfjOKtAlwHgvPFfySCKz/9Ye2O0ZccNoJ3hLfE8zh
nOXu3OX6T//uuC0CAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAUAaeso28jVzeaN5N
ZeMB0DA2tE97AKIra9zgYXpjfFyXHQM8z9skrHvOjVAEGRi7IqrAuhvErbUoJ4r4
KNdjVH4ZIzbnMsm3dHkpc1pKJrDwHnmkOJ38RZPJH0V2WNnoqaJXEME0zuoq6LhO
L/dpxwXY9XWa5UZ6X/Xedxq1Mm8VBsLP3Viso617yhVbZG5YHQvcxI6DqfIZZhFD
SxcpZuzpqoPIfXvXJ0pu8nMDnbdEYsEKFkrA0iwAkDy/h2GLzxZVEOGew8Bltyo8
1eENM6kHAfZbuewiJLv0EvjpGwsA7cYm/5+3uUwIqsQSZxhyZn8jKFOqLwpNxbsr
HspwXw==
-----END CERTIFICATE-----
17 changes: 17 additions & 0 deletions test/fixtures/cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-----BEGIN CERTIFICATE-----
MIICxzCCAa+gAwIBAgIJAKXh5y4t6LPVMA0GCSqGSIb3DQEBCwUAMBIxEDAOBgNV
BAMMB0Zha2UgQ0EwHhcNMjEwMTMxMjIyNDQ3WhcNMzEwMTI5MjIyNDQ3WjAUMRIw
EAYDVQQDDAlsb2NhbGhvc3QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB
AQDC6GvrMFyBkcdDzoiKTj4MnxP+BwE3R68d7jU38r/wChsuvQ6V3HtketAD6Ocl
OUTj2i+DfXLzItOmUmL3PmynwtyKxlehwg7iDDFN/mvRThBmdclG9xvhCY+PaUzn
JJvV+CCGiYwPifNhnSRUsJqMQZ1yNkMrY+nuoO5SlhKTwISBNYACDyA6W8Qvx1rw
m5EHNdZ67Vetp7T9o6bhTHoqYSRtNRsAgu4xe0H40AlDR7xajmiaqX5u6FV4Rb7f
bkJPRjPsXMRIdN2tKS3yToL0SSoa0kGVmrSXS2Yo/mG6n3jb0/lqSqzRvQ3C1h0U
9VEpahfxi3ZDF3GvgWz3OB/jAgMBAAGjHjAcMBoGA1UdEQQTMBGCCWxvY2FsaG9z
dIcEfwAAATANBgkqhkiG9w0BAQsFAAOCAQEAgcBDvaPmpg9gJusAUo8edIAuFpQw
fp0bIJ0KY4fgCihiclAlhE7Jn0S/6Kr7XfK7ZTqxMArXygvDfOWHv4fWDlIQR1fv
pxMdQq5ef/TWGBlvSONC5Kdivmby7YSkxaCt/xcvKL5dculkpMcC24IeIamVSOFf
M/3++ma0w9K/xUSV0KwKCpLv/N4tRqlCAfv9Vk+DtwKKaloqisjwjFlxwWmjOChx
CDEfg8ys5zV6gVGcjn5Oa/SaY6FboD8t9H0janEkoFD57xTKHWBIfcbTufjeMjGT
ukU196yctwoQaDQ+fHk/brDN6h5w3HOKVj5AiMOuu9sKT5jJGbgU9NU9Ow==
-----END CERTIFICATE-----
28 changes: 28 additions & 0 deletions test/fixtures/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDC6GvrMFyBkcdD
zoiKTj4MnxP+BwE3R68d7jU38r/wChsuvQ6V3HtketAD6OclOUTj2i+DfXLzItOm
UmL3PmynwtyKxlehwg7iDDFN/mvRThBmdclG9xvhCY+PaUznJJvV+CCGiYwPifNh
nSRUsJqMQZ1yNkMrY+nuoO5SlhKTwISBNYACDyA6W8Qvx1rwm5EHNdZ67Vetp7T9
o6bhTHoqYSRtNRsAgu4xe0H40AlDR7xajmiaqX5u6FV4Rb7fbkJPRjPsXMRIdN2t
KS3yToL0SSoa0kGVmrSXS2Yo/mG6n3jb0/lqSqzRvQ3C1h0U9VEpahfxi3ZDF3Gv
gWz3OB/jAgMBAAECggEAMu+YnGsUEcxuHdtQtYxDDPtZty5PdAnoytKg19E5tdp+
RhWkRS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