fix(certificate-management): Fix loss of route update callback during dynamic route updates in certificate manager

This commit is contained in:
2025-05-18 23:07:31 +00:00
parent ba1569ee21
commit 8fd861c9a3
6 changed files with 422 additions and 33 deletions

View File

@ -178,6 +178,36 @@ export class SmartProxy extends plugins.EventEmitter {
*/
public settings: ISmartProxyOptions;
/**
* Helper method to create and configure certificate manager
* This ensures consistent setup including the required ACME callback
*/
private async createCertificateManager(
routes: IRouteConfig[],
certStore: string = './certs',
acmeOptions?: any
): Promise<SmartCertManager> {
const certManager = new SmartCertManager(routes, certStore, acmeOptions);
// Always set up the route update callback for ACME challenges
certManager.setUpdateRoutesCallback(async (routes) => {
await this.updateRoutes(routes);
});
// Connect with NetworkProxy if available
if (this.networkProxyBridge.getNetworkProxy()) {
certManager.setNetworkProxy(this.networkProxyBridge.getNetworkProxy());
}
// Pass down the global ACME config if available
if (this.settings.acme) {
certManager.setGlobalAcmeDefaults(this.settings.acme);
}
await certManager.initialize();
return certManager;
}
/**
* Initialize certificate manager
*/
@ -230,28 +260,12 @@ export class SmartProxy extends plugins.EventEmitter {
);
}
this.certManager = new SmartCertManager(
// Use the helper method to create and configure the certificate manager
this.certManager = await this.createCertificateManager(
this.settings.routes,
this.settings.acme?.certificateStore || './certs',
acmeOptions
);
// Pass down the global ACME config to the cert manager
if (this.settings.acme) {
this.certManager.setGlobalAcmeDefaults(this.settings.acme);
}
// Connect with NetworkProxy
if (this.networkProxyBridge.getNetworkProxy()) {
this.certManager.setNetworkProxy(this.networkProxyBridge.getNetworkProxy());
}
// Set route update callback for ACME challenges
this.certManager.setUpdateRoutesCallback(async (routes) => {
await this.updateRoutes(routes);
});
await this.certManager.initialize();
}
/**
@ -520,19 +534,15 @@ export class SmartProxy extends plugins.EventEmitter {
// Update certificate manager with new routes
if (this.certManager) {
const existingAcmeOptions = this.certManager.getAcmeOptions();
await this.certManager.stop();
this.certManager = new SmartCertManager(
// Use the helper method to create and configure the certificate manager
this.certManager = await this.createCertificateManager(
newRoutes,
'./certs',
this.certManager.getAcmeOptions()
existingAcmeOptions
);
if (this.networkProxyBridge.getNetworkProxy()) {
this.certManager.setNetworkProxy(this.networkProxyBridge.getNetworkProxy());
}
await this.certManager.initialize();
}
}