Files
cloudly/ts/connector.letsencrypt/connector.ts
T

55 lines
1.6 KiB
TypeScript
Raw Normal View History

import * as plugins from '../plugins.js';
import { Cloudly } from '../classes.cloudly.js';
2024-04-20 12:21:41 +02:00
export class LetsencryptConnector {
private cloudlyRef: Cloudly;
2026-05-08 13:56:20 +00:00
private smartacme!: plugins.smartacme.SmartAcme;
2024-04-20 12:21:41 +02:00
constructor(cloudlyArg: Cloudly) {
this.cloudlyRef = cloudlyArg;
}
public async getCertificateForDomain(domainName: string) {
const cert = await this.smartacme.getCertificateForDomain(domainName);
return cert;
}
/**
* inits letsencrypt
*/
public async init() {
2026-05-08 13:56:20 +00:00
if (!this.cloudlyRef.cloudflareConnector.cloudflare) {
throw new Error('Cloudflare token is required for letsencrypt DNS-01 challenges');
}
// Create DNS-01 challenge handler using Cloudflare
const dnsHandler = new plugins.smartacme.handlers.Dns01Handler(
this.cloudlyRef.cloudflareConnector.cloudflare
);
// Create MongoDB certificate manager
const certManager = new plugins.smartacme.certmanagers.MongoCertManager(
2026-05-08 13:56:20 +00:00
this.cloudlyRef.config.data.mongoDescriptor!
);
2024-04-20 12:21:41 +02:00
this.smartacme = new plugins.smartacme.SmartAcme({
2026-05-08 13:56:20 +00:00
accountEmail: this.cloudlyRef.config.data.letsEncryptEmail!,
2024-04-20 12:21:41 +02:00
accountPrivateKey: this.cloudlyRef.config.data.letsEncryptPrivateKey,
2026-05-08 13:56:20 +00:00
environment: this.cloudlyRef.config.data.environment!,
certManager: certManager,
challengeHandlers: [dnsHandler],
2024-04-20 12:21:41 +02:00
});
await this.smartacme.start().catch((err) => {
console.error('error in init', err);
console.log(`trying again in a few minutes`);
});
2024-04-20 12:21:41 +02:00
}
/**
* stops the instance
*/
public async stop() {
2026-05-08 13:56:20 +00:00
await this.smartacme?.stop();
2024-04-20 12:21:41 +02:00
}
}