Skip to content

Commit 108775e

Browse files
committed
Support LOCAL_CA_KEY/CERT to use a fixed local CA
1 parent c58163f commit 108775e

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

src/server.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ interface ServerOptions {
3535
acmeAccountKey?: string;
3636
proactiveCertDomains?: string[];
3737
certCacheDir?: string;
38+
localCaKey?: string;
39+
localCaCert?: string;
3840
}
3941

4042
async function generateTlsConfig(options: ServerOptions) {
@@ -43,12 +45,20 @@ async function generateTlsConfig(options: ServerOptions) {
4345
const certCache = options.certCacheDir
4446
? new PersistentCertCache(options.certCacheDir)
4547
: undefined;
46-
const [
47-
caCert
48-
] = await Promise.all([
49-
generateCACertificate(),
50-
certCache ? certCache.loadCache() : null
51-
]);
48+
49+
// Use provided CA key/cert if available, otherwise generate a fresh one
50+
let caCert: { key: string; cert: string };
51+
if (options.localCaKey && options.localCaCert) {
52+
console.log('Using provided local CA certificate');
53+
caCert = { key: options.localCaKey, cert: options.localCaCert };
54+
} else {
55+
console.log('Generating fresh local CA certificate');
56+
caCert = await generateCACertificate();
57+
}
58+
59+
if (certCache) {
60+
await certCache.loadCache();
61+
}
5262

5363
const ca = await LocalCA.create(caCert);
5464
const defaultCert = await ca.generateCertificate(rootDomain);
@@ -186,7 +196,9 @@ if (wasRunDirectly) {
186196
proactiveCertDomains: process.env.PROACTIVE_CERT_DOMAINS?.split(','),
187197
acmeProvider: process.env.ACME_PROVIDER as AcmeProvider | undefined,
188198
acmeAccountKey: process.env.ACME_ACCOUNT_KEY,
189-
certCacheDir: process.env.CERT_CACHE_DIR
199+
certCacheDir: process.env.CERT_CACHE_DIR,
200+
localCaKey: process.env.LOCAL_CA_KEY,
201+
localCaCert: process.env.LOCAL_CA_CERT
190202
}).then((tcpHandler) => {
191203
ports.forEach((port) => {
192204
const server = createTcpServer(tcpHandler);

0 commit comments

Comments
 (0)