Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implicit support for PFX or PKCS12 encoded certificates #673

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 33 additions & 15 deletions bin/serve.js
Expand Up @@ -94,13 +94,13 @@ const getHelp = () => chalk`
--no-etag Send \`Last-Modified\` header instead of \`ETag\`

-S, --symlinks Resolve symlinks instead of showing 404 errors

--ssl-cert Optional path to an SSL/TLS certificate to serve with HTTPS

--ssl-key Optional path to the SSL/TLS certificate\'s private key

--ssl-pass Optional path to the SSL/TLS certificate\'s passphrase
--ssl-cert Optional path to an SSL/TLS certificate in PEM or PKCS #12 format
to serve with HTTPS
--ssl-key Optional path to the PEM certificate\'s private key

--ssl-pass Optional path to the PEM or PKCS #12 certificate\'s passphrase
or password for PEM or PKCS #12 crypto file
--no-port-switching Do not open a port other than the one specified when it\'s taken.

{bold ENDPOINTS}
Expand Down Expand Up @@ -189,8 +189,7 @@ const startEndpoint = (endpoint, config, args, previous) => {
const {isTTY} = process.stdout;
const clipboard = args['--no-clipboard'] !== true;
const compress = args['--no-compression'] !== true;
const httpMode = args['--ssl-cert'] && args['--ssl-key'] ? 'https' : 'http';

const httpMode = args['--ssl-cert'] ? 'https' : 'http';
const serverHandler = async (request, response) => {
if (args['--cors']) {
response.setHeader('Access-Control-Allow-Origin', '*');
Expand All @@ -202,15 +201,34 @@ const startEndpoint = (endpoint, config, args, previous) => {
return handler(request, response, config);
};

const sslPass = args['--ssl-pass'];
let sslPass = args['--ssl-pass'];
if (sslPass) {
try {
sslPass = fs.readFileSync(sslPass, 'utf8').trim();
} catch (err) {
console.log('keeping plain value in ssl-pass');
}
}

const server = httpMode === 'https'
? https.createServer({
key: fs.readFileSync(args['--ssl-key']),
cert: fs.readFileSync(args['--ssl-cert']),
passphrase: sslPass ? fs.readFileSync(sslPass) : ''
}, serverHandler)
: http.createServer(serverHandler);
const certPath = args['--ssl-cert'];

let server;
if (httpMode === 'https') {
if (certPath.match('\.(pfx|p12)$')) {
server = https.createServer({
pfx: fs.readFileSync(certPath),
passphrase: sslPass
}, serverHandler);
} else {
server = https.createServer({
key: fs.readFileSync(args['--ssl-key']),
cert: fs.readFileSync(certPath),
passphrase: sslPass
}, serverHandler);
}
} else {
server = http.createServer(serverHandler);
}

server.on('error', (err) => {
if (err.code === 'EADDRINUSE' && endpoint.length === 1 && !isNaN(endpoint[0]) && args['--no-port-switching'] !== true) {
Expand Down