Skip to content

Commit a6f04a7

Browse files
committed
Allow configuring proactively refreshed cert domains
This allows you to specify domains which will always have their certificates refreshed & ready, even if they aren't getting a lot of traffic (refreshes are normally only triggered async, so if a domain isn't used at all for 90 days it's possible the cert will expire and won't be available on the first subsequent request).
1 parent d5a40b1 commit a6f04a7

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ declare module 'stream' {
1414
}
1515
}
1616

17-
1817
interface ServerOptions {
1918
domain?: string;
2019
acmeProvider?: AcmeProvider;
20+
proactiveCertDomains?: string[];
2121
certCacheDir?: string;
2222
eabConfig?: ExternalAccessBindingConfig;
2323
}
@@ -64,6 +64,7 @@ async function generateTlsConfig(options: ServerOptions) {
6464

6565
return {
6666
rootDomain,
67+
proactiveCertDomains: options.proactiveCertDomains,
6768
key: defaultCert.key,
6869
cert: defaultCert.cert,
6970
ca: caCert.cert,
@@ -130,6 +131,7 @@ if (wasRunDirectly) {
130131

131132
createTcpHandler({
132133
domain: process.env.ROOT_DOMAIN,
134+
proactiveCertDomains: process.env.PROACTIVE_CERT_DOMAINS,
133135
acmeProvider: process.env.ACME_PROVIDER as AcmeProvider | undefined,
134136
eabConfig: process.env.ACME_EAB_KID && process.env.ACME_EAB_HMAC
135137
? { kid: process.env.ACME_EAB_KID, hmacKey: process.env.ACME_EAB_HMAC }

src/tls-handler.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ import * as tls from 'tls';
22

33
import { ConnectionProcessor } from './process-connection.js';
44

5+
type CertGenerator = (domain: string) => {
6+
key: string,
7+
cert: string,
8+
ca?: string
9+
};
10+
511
interface TlsHandlerConfig {
612
rootDomain: string;
13+
proactiveCertDomains?: string[];
14+
715
key: string;
816
cert: string;
917
ca: string;
10-
generateCertificate: (domain: string) => {
11-
key: string,
12-
cert: string,
13-
ca?: string
14-
};
18+
generateCertificate: CertGenerator;
1519
}
1620

1721
const DEFAULT_ALPN_PROTOCOLS = ['http/1.1', 'h2'];
@@ -27,6 +31,20 @@ const getSNIPrefixParts = (servername: string, rootDomain: string) => {
2731
return serverNamePrefix.split('.');
2832
};
2933

34+
const PROACTIVE_DOMAIN_REFRESH_INTERVAL = 1000 * 60 * 60 * 24; // Daily cert check for proactive domains
35+
36+
function proactivelyRefreshDomains(domains: string[], certGenerator: CertGenerator) {
37+
domains.forEach(domain => {
38+
console.log(`Proactively checking cert at startup for ${domain}`);
39+
certGenerator(domain);
40+
41+
setInterval(() => {
42+
console.log(`Proactively checking cert for ${domain}`);
43+
certGenerator(domain);
44+
}, PROACTIVE_DOMAIN_REFRESH_INTERVAL);
45+
});
46+
}
47+
3048
export async function createTlsHandler(
3149
tlsConfig: TlsHandlerConfig,
3250
connProcessor: ConnectionProcessor
@@ -74,6 +92,8 @@ export async function createTlsHandler(
7492
}
7593
});
7694

95+
proactivelyRefreshDomains(tlsConfig.proactiveCertDomains ?? [], tlsConfig.generateCertificate);
96+
7797
server.on('secureConnection', (socket) => {
7898
connProcessor.processConnection(socket);
7999
});

0 commit comments

Comments
 (0)