import * as plugins from '../plugins.js'; import { Cloudly } from '../classes.cloudly.js'; export class LetsencryptConnector { private cloudlyRef: Cloudly; private smartacme!: plugins.smartacme.SmartAcme; 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() { 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( this.cloudlyRef.config.data.mongoDescriptor! ); this.smartacme = new plugins.smartacme.SmartAcme({ accountEmail: this.cloudlyRef.config.data.letsEncryptEmail!, accountPrivateKey: this.cloudlyRef.config.data.letsEncryptPrivateKey, environment: this.cloudlyRef.config.data.environment!, certManager: certManager, challengeHandlers: [dnsHandler], }); await this.smartacme.start().catch((err) => { console.error('error in init', err); console.log(`trying again in a few minutes`); }); } /** * stops the instance */ public async stop() { await this.smartacme?.stop(); } }