2025-06-01 19:46:10 +00:00
|
|
|
import type DcRouter from '../classes.dcrouter.js';
|
|
|
|
|
import * as plugins from '../plugins.js';
|
|
|
|
|
import * as paths from '../paths.js';
|
2025-06-08 07:04:35 +00:00
|
|
|
import * as handlers from './handlers/index.js';
|
2026-03-03 21:39:20 +00:00
|
|
|
import * as interfaces from '../../ts_interfaces/index.js';
|
|
|
|
|
import { requireValidIdentity, requireAdminIdentity } from './helpers/guards.js';
|
2025-06-01 19:46:10 +00:00
|
|
|
|
|
|
|
|
export class OpsServer {
|
|
|
|
|
public dcRouterRef: DcRouter;
|
|
|
|
|
public server: plugins.typedserver.utilityservers.UtilityWebsiteServer;
|
2026-03-03 21:39:20 +00:00
|
|
|
|
|
|
|
|
// Main TypedRouter — unauthenticated endpoints (login/logout/verify) and own-auth handlers
|
2025-06-08 07:04:35 +00:00
|
|
|
public typedrouter = new plugins.typedrequest.TypedRouter();
|
2026-03-03 21:39:20 +00:00
|
|
|
|
|
|
|
|
// Auth-enforced routers — middleware validates identity before any handler runs
|
|
|
|
|
public viewRouter = new plugins.typedrequest.TypedRouter<{ request: { identity: interfaces.data.IIdentity } }>();
|
|
|
|
|
public adminRouter = new plugins.typedrequest.TypedRouter<{ request: { identity: interfaces.data.IIdentity } }>();
|
|
|
|
|
|
2025-06-08 07:04:35 +00:00
|
|
|
// Handler instances
|
2025-06-08 07:19:31 +00:00
|
|
|
public adminHandler: handlers.AdminHandler;
|
2025-06-08 07:04:35 +00:00
|
|
|
private configHandler: handlers.ConfigHandler;
|
|
|
|
|
private logsHandler: handlers.LogsHandler;
|
|
|
|
|
private securityHandler: handlers.SecurityHandler;
|
|
|
|
|
private statsHandler: handlers.StatsHandler;
|
2026-02-01 19:21:37 +00:00
|
|
|
private radiusHandler: handlers.RadiusHandler;
|
2026-02-02 00:36:19 +00:00
|
|
|
private emailOpsHandler: handlers.EmailOpsHandler;
|
2026-02-13 17:05:33 +00:00
|
|
|
private certificateHandler: handlers.CertificateHandler;
|
2026-02-16 11:25:16 +00:00
|
|
|
private remoteIngressHandler: handlers.RemoteIngressHandler;
|
2026-02-23 12:40:26 +00:00
|
|
|
private routeManagementHandler: handlers.RouteManagementHandler;
|
|
|
|
|
private apiTokenHandler: handlers.ApiTokenHandler;
|
2025-06-01 19:46:10 +00:00
|
|
|
|
|
|
|
|
constructor(dcRouterRefArg: DcRouter) {
|
|
|
|
|
this.dcRouterRef = dcRouterRefArg;
|
2026-03-03 21:39:20 +00:00
|
|
|
|
2025-06-08 07:04:35 +00:00
|
|
|
// Add our typedrouter to the dcRouter's main typedrouter
|
|
|
|
|
this.dcRouterRef.typedrouter.addTypedRouter(this.typedrouter);
|
2025-06-01 19:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async start() {
|
|
|
|
|
this.server = new plugins.typedserver.utilityservers.UtilityWebsiteServer({
|
|
|
|
|
domain: 'localhost',
|
|
|
|
|
feedMetadata: null,
|
|
|
|
|
serveDir: paths.distServe,
|
|
|
|
|
});
|
2025-06-08 07:04:35 +00:00
|
|
|
|
|
|
|
|
// The server has a built-in typedrouter at /typedrequest
|
|
|
|
|
// Add the main dcRouter typedrouter to the server's typedrouter
|
|
|
|
|
this.server.typedrouter.addTypedRouter(this.dcRouterRef.typedrouter);
|
|
|
|
|
|
|
|
|
|
// Set up handlers
|
2025-06-08 07:19:31 +00:00
|
|
|
await this.setupHandlers();
|
2025-06-01 19:46:10 +00:00
|
|
|
|
|
|
|
|
await this.server.start(3000);
|
|
|
|
|
}
|
2025-06-08 07:04:35 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set up all TypedRequest handlers
|
|
|
|
|
*/
|
2025-06-08 07:19:31 +00:00
|
|
|
private async setupHandlers(): Promise<void> {
|
2026-03-03 21:39:20 +00:00
|
|
|
// AdminHandler must be initialized first (JWT setup needed for guards)
|
2025-06-08 07:04:35 +00:00
|
|
|
this.adminHandler = new handlers.AdminHandler(this);
|
2026-03-03 21:39:20 +00:00
|
|
|
await this.adminHandler.initialize();
|
|
|
|
|
|
|
|
|
|
// viewRouter middleware: requires valid identity (any logged-in user)
|
|
|
|
|
this.viewRouter.addMiddleware(async (typedRequest) => {
|
|
|
|
|
await requireValidIdentity(this.adminHandler, typedRequest.request);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// adminRouter middleware: requires admin identity
|
|
|
|
|
this.adminRouter.addMiddleware(async (typedRequest) => {
|
|
|
|
|
await requireAdminIdentity(this.adminHandler, typedRequest.request);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Connect auth routers to the main typedrouter
|
|
|
|
|
this.typedrouter.addTypedRouter(this.viewRouter);
|
|
|
|
|
this.typedrouter.addTypedRouter(this.adminRouter);
|
|
|
|
|
|
|
|
|
|
// Instantiate all handlers — they self-register with the appropriate router
|
2025-06-08 07:04:35 +00:00
|
|
|
this.configHandler = new handlers.ConfigHandler(this);
|
|
|
|
|
this.logsHandler = new handlers.LogsHandler(this);
|
|
|
|
|
this.securityHandler = new handlers.SecurityHandler(this);
|
|
|
|
|
this.statsHandler = new handlers.StatsHandler(this);
|
2026-02-01 19:21:37 +00:00
|
|
|
this.radiusHandler = new handlers.RadiusHandler(this);
|
2026-02-02 00:36:19 +00:00
|
|
|
this.emailOpsHandler = new handlers.EmailOpsHandler(this);
|
2026-02-13 17:05:33 +00:00
|
|
|
this.certificateHandler = new handlers.CertificateHandler(this);
|
2026-02-16 11:25:16 +00:00
|
|
|
this.remoteIngressHandler = new handlers.RemoteIngressHandler(this);
|
2026-02-23 12:40:26 +00:00
|
|
|
this.routeManagementHandler = new handlers.RouteManagementHandler(this);
|
|
|
|
|
this.apiTokenHandler = new handlers.ApiTokenHandler(this);
|
2026-02-01 19:21:37 +00:00
|
|
|
|
2025-06-08 07:04:35 +00:00
|
|
|
console.log('✅ OpsServer TypedRequest handlers initialized');
|
|
|
|
|
}
|
2025-06-01 19:46:10 +00:00
|
|
|
|
2025-06-08 07:04:35 +00:00
|
|
|
public async stop() {
|
2026-02-26 17:14:51 +00:00
|
|
|
// Clean up log handler streams and push destination before stopping the server
|
|
|
|
|
if (this.logsHandler) {
|
|
|
|
|
this.logsHandler.cleanup();
|
|
|
|
|
}
|
2025-06-08 07:04:35 +00:00
|
|
|
if (this.server) {
|
|
|
|
|
await this.server.stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-01 19:46:10 +00:00
|
|
|
}
|