Skip to content

Commit 8f72868

Browse files
committed
src/node/cli.ts: Add --cert-host to configure generated certificate hostname
1 parent 68d3752 commit 8f72868

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

src/node/cli.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface Args extends VsArgs {
2626
readonly auth?: AuthType
2727
readonly password?: string
2828
readonly cert?: OptionalString
29+
readonly "cert-host"?: string
2930
readonly "cert-key"?: string
3031
readonly "disable-telemetry"?: boolean
3132
readonly help?: boolean
@@ -101,7 +102,11 @@ const options: Options<Required<Args>> = {
101102
cert: {
102103
type: OptionalString,
103104
path: true,
104-
description: "Path to certificate. Generated if no path is provided.",
105+
description: "Path to certificate. A self signed certificate is generated if none is provided.",
106+
},
107+
"cert-host": {
108+
type: "string",
109+
description: "Hostname to use when generating a self signed certificate.",
105110
},
106111
"cert-key": { type: "string", path: true, description: "Path to certificate key when using non-generated cert." },
107112
"disable-telemetry": { type: "boolean", description: "Disable telemetry." },

src/node/entry.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ const main = async (args: Args, configArgs: Args): Promise<void> => {
160160
proxyDomains: args["proxy-domain"],
161161
socket: args.socket,
162162
...(args.cert && !args.cert.value
163-
? await generateCertificate()
163+
? await generateCertificate(args["cert-host"] || "localhost")
164164
: {
165165
cert: args.cert && args.cert.value,
166166
certKey: args["cert-key"],

src/node/util.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ export function humanPath(p?: string): string {
5454
return p.replace(os.homedir(), "~")
5555
}
5656

57-
export const generateCertificate = async (): Promise<{ cert: string; certKey: string }> => {
58-
const certPath = path.join(paths.data, "self-signed.crt")
59-
const certKeyPath = path.join(paths.data, "self-signed.key")
57+
export const generateCertificate = async (hostname: string): Promise<{ cert: string; certKey: string }> => {
58+
const certPath = path.join(paths.data, `${hostname.replace(/\./g, "_")}.crt`)
59+
const certKeyPath = path.join(paths.data, `${hostname.replace(/\./g, "_")}.key`)
6060

6161
const checks = await Promise.all([fs.pathExists(certPath), fs.pathExists(certKeyPath)])
6262
if (!checks[0] || !checks[1]) {
@@ -67,6 +67,7 @@ export const generateCertificate = async (): Promise<{ cert: string; certKey: st
6767
pem.createCertificate(
6868
{
6969
selfSigned: true,
70+
commonName: hostname,
7071
config: `
7172
[req]
7273
req_extensions = v3_req
@@ -76,7 +77,7 @@ extendedKeyUsage = serverAuth
7677
subjectAltName = @alt_names
7778
7879
[alt_names]
79-
DNS.1 = localhost
80+
DNS.1 = ${hostname}
8081
`,
8182
},
8283
(error, result) => {

test/socket.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe("SocketProxyProvider", () => {
4545
}
4646

4747
before(async () => {
48-
const cert = await generateCertificate()
48+
const cert = await generateCertificate("localhost")
4949
const options = {
5050
cert: fs.readFileSync(cert.cert),
5151
key: fs.readFileSync(cert.certKey),

0 commit comments

Comments
 (0)