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 support for PFX or PKCS12 encoded certificates #580

Closed
wants to merge 11 commits into from
36 changes: 26 additions & 10 deletions bin/serve.js 100755 → 100644
Expand Up @@ -101,6 +101,8 @@ const getHelp = () => chalk`

--ssl-pass Optional path to the SSL/TLS certificate\'s passphrase

--ssl-format Optional format of the SSL/TLS certificate.
{grey Supported formats: pem (default) and pfx}
--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 +191,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'] && (args['--ssl-key'] || args['--ssl-pass']) ? 'https' : 'http';
const serverHandler = async (request, response) => {
if (args['--cors']) {
response.setHeader('Access-Control-Allow-Origin', '*');
Expand All @@ -203,14 +204,28 @@ const startEndpoint = (endpoint, config, args, previous) => {
};

const sslPass = args['--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);
let server;
if (httpMode === 'https') {
switch (args['--ssl-format']) {
case 'pfx':
server = https.createServer({
pfx: fs.readFileSync(args['--ssl-cert']),
passphrase: sslPass ? fs.readFileSync(sslPass) : ''
}, serverHandler);
break;
case 'pem':
default: {
https.createServer({
key: fs.readFileSync(args['--ssl-key']),
cert: fs.readFileSync(args['--ssl-cert']),
passphrase: sslPass ? fs.readFileSync(sslPass) : ''
}, serverHandler);
break;
}
}
} 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 Expand Up @@ -384,6 +399,7 @@ const loadConfig = async (cwd, entry, args) => {
'--no-port-switching': Boolean,
'--ssl-cert': String,
'--ssl-key': String,
'--ssl-format': String,
'--ssl-pass': String,
'-h': '--help',
'-v': '--version',
Expand Down