From 028f5d4b1f3b47cf961356730ede4cb70d83cc57 Mon Sep 17 00:00:00 2001 From: Shubhan Chemburkar Date: Sat, 9 Jul 2022 13:58:39 +0530 Subject: [PATCH 1/4] Add support for PFX or PKCS12 encoded certificates Add support for PFX or PKCS12 encoded certificates --- source/utilities/cli.ts | 2 ++ source/utilities/server.ts | 39 ++++++++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/source/utilities/cli.ts b/source/utilities/cli.ts index f32aa8e0..19045677 100644 --- a/source/utilities/cli.ts +++ b/source/utilities/cli.ts @@ -83,8 +83,10 @@ const helpText = chalk` -S, --symlinks Resolve symlinks instead of showing 404 errors --ssl-cert Optional path to an SSL/TLS certificate to serve with HTTPS + {grey Supported formats: PEM (default) and PKCS12 (PFX)} --ssl-key Optional path to the SSL/TLS certificate\'s private key + {grey Applicable only for PEM certificates} --ssl-pass Optional path to the SSL/TLS certificate\'s passphrase diff --git a/source/utilities/server.ts b/source/utilities/server.ts index 360389c1..b44dd518 100644 --- a/source/utilities/server.ts +++ b/source/utilities/server.ts @@ -62,21 +62,31 @@ export const startServer = async ( }; // Create the server. - const useSsl = args['--ssl-cert'] && args['--ssl-key']; - const httpMode = useSsl ? 'https' : 'http'; + + const sslCert = args['--ssl-cert']; + const sslKey = args['--ssl-key']; const sslPass = args['--ssl-pass']; - const serverConfig = - httpMode === 'https' && args['--ssl-cert'] && args['--ssl-key'] - ? { - key: await readFile(args['--ssl-key']), - cert: await readFile(args['--ssl-cert']), - passphrase: sslPass ? await readFile(sslPass, 'utf8') : '', - } - : {}; - const server = - httpMode === 'https' - ? https.createServer(serverConfig, serverHandler) - : http.createServer(serverHandler); + const isPFXFormat = sslCert && /[.](?pfx|p12)$/.exec(sslCert); + const useSsl = sslCert && (sslKey || sslPass || isPFXFormat); + + let serverConfig: http.ServerOptions | https.ServerOptions = {}; + if (useSsl && sslCert && sslKey) { + // Format is PEM due to usagae of SSL Key and Optional Passphrase + serverConfig = { + key: await readFile(sslKey), + cert: await readFile(sslCert), + passphrase: sslPass ? await readFile(sslPass, 'utf8') : '', + }; + } else if (useSsl && sslCert && isPFXFormat) { + serverConfig = { + pfx: await readFile(sslCert), + passphrase: sslPass ? await readFile(sslPass, 'utf8') : '', + }; + } + + const server = useSsl + ? https.createServer(serverConfig, serverHandler) + : http.createServer(serverHandler); // Once the server starts, return the address it is running on so the CLI // can tell the user. @@ -101,6 +111,7 @@ export const startServer = async ( else address = details.address; const ip = getNetworkAddress(); + const httpMode = useSsl ? 'https' : 'http'; local = `${httpMode}://${address}:${details.port}`; network = ip ? `${httpMode}://${ip}:${details.port}` : undefined; } From a7ec55aea3ff539d7009715ff18b8ef9a26274aa Mon Sep 17 00:00:00 2001 From: Shubhan Chemburkar Date: Tue, 12 Jul 2022 22:24:03 +0530 Subject: [PATCH 2/4] Updated comments --- source/utilities/server.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/utilities/server.ts b/source/utilities/server.ts index b44dd518..cf736ddb 100644 --- a/source/utilities/server.ts +++ b/source/utilities/server.ts @@ -62,7 +62,6 @@ export const startServer = async ( }; // Create the server. - const sslCert = args['--ssl-cert']; const sslKey = args['--ssl-key']; const sslPass = args['--ssl-pass']; @@ -71,13 +70,14 @@ export const startServer = async ( let serverConfig: http.ServerOptions | https.ServerOptions = {}; if (useSsl && sslCert && sslKey) { - // Format is PEM due to usagae of SSL Key and Optional Passphrase + // Format detected is PEM due to usage of SSL Key and Optional Passphrase. serverConfig = { key: await readFile(sslKey), cert: await readFile(sslCert), passphrase: sslPass ? await readFile(sslPass, 'utf8') : '', }; } else if (useSsl && sslCert && isPFXFormat) { + // Format detected is PFX. serverConfig = { pfx: await readFile(sslCert), passphrase: sslPass ? await readFile(sslPass, 'utf8') : '', From 1260d47624f5d7311c326e978a893ab771be0bae Mon Sep 17 00:00:00 2001 From: Shubhan Chemburkar Date: Wed, 13 Jul 2022 09:19:00 +0530 Subject: [PATCH 3/4] Rename httpMode to protocol --- source/utilities/server.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/utilities/server.ts b/source/utilities/server.ts index cf736ddb..0afa53b4 100644 --- a/source/utilities/server.ts +++ b/source/utilities/server.ts @@ -111,9 +111,9 @@ export const startServer = async ( else address = details.address; const ip = getNetworkAddress(); - const httpMode = useSsl ? 'https' : 'http'; - local = `${httpMode}://${address}:${details.port}`; - network = ip ? `${httpMode}://${ip}:${details.port}` : undefined; + const protocol = useSsl ? 'https' : 'http'; + local = `${protocol}://${address}:${details.port}`; + network = ip ? `${protocol}://${ip}:${details.port}` : undefined; } return { From dfbb4afde3128472c8c3a50ad4ad84b9aa69763a Mon Sep 17 00:00:00 2001 From: Shubhan Chemburkar Date: Mon, 18 Jul 2022 20:25:19 +0530 Subject: [PATCH 4/4] Fix Tests: Updated cli snapshot Updated cli snapshot due to change in help test, using vitest -u option --- tests/__snapshots__/cli.test.ts.snap | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/__snapshots__/cli.test.ts.snap b/tests/__snapshots__/cli.test.ts.snap index e7a18109..830e6c1d 100644 --- a/tests/__snapshots__/cli.test.ts.snap +++ b/tests/__snapshots__/cli.test.ts.snap @@ -44,8 +44,10 @@ exports[`utilities/cli > render help text 1`] = ` -S, --symlinks Resolve symlinks instead of showing 404 errors --ssl-cert Optional path to an SSL/TLS certificate to serve with HTTPS + Supported formats: PEM (default) and PKCS12 (PFX) --ssl-key Optional path to the SSL/TLS certificate's private key + Applicable only for PEM certificates --ssl-pass Optional path to the SSL/TLS certificate's passphrase