import { EndpointType } from "$lib/server/enums/endpoints" import type { NginxProtocol } from "$lib/server/enums/protocols" import { validatePort } from "$lib/server/utils/ports-utils" import type { IEndpoint } from "./endpoints-interfaces" // TODO: inherit from a super class export interface ISSLTerminationBroker { /** * Initialize the Broker and everything related to it */ init(): Promise // TODO: in the next version support // TODO: creation of endpoints // TODO: according to path // NOTES: it's useless to generate ports backend // NOTES: generate them frontend and validate backend createSSLTermination( name: string, sslPort: number, clearPort: number, servicePort: number, serviceEndpoint: string, protocol: NginxProtocol, certificateURI: string, privateKeyURI: string ): Promise activateEndpointByPath( path: string ): Promise deactivateEndpointByPath( path: string ): Promise // Getting endpoints may be null, react over them getSSLTerminationByPath( path: string ): Promise // Throw if something goes wrong modifySSLTerminationByPath( path: string, changes: SSLTerminationChanges ): Promise deleteSSLTerminationByPath( path: string ): Promise getAllSSLTerminations(): Promise } /** * This class represents an SSL Termination Endpoint. * * While it's possible to create it directly, it is * discouraged in favor of the Factory methods as it does * more checks than this class */ export class SSLTermination implements IEndpoint { private static __type = EndpointType.SSL_TERMINATION public get type() { return SSLTermination.__type } public name: string public path: string public sslPort: number public clearPort: number public servicePort: number public serviceEndpoint: string public protocol: NginxProtocol public certificateURI: string public privateKeyURI: string constructor( name: string, path: string, sslPort: number, clearPort: number, servicePort: number, serviceEndpoint: string, protocol: NginxProtocol, certificateURI: string, privateKeyURI: string ) { this.name = name this.path = path this.sslPort = sslPort this.clearPort = clearPort this.servicePort = servicePort this.serviceEndpoint = serviceEndpoint this.protocol = protocol this.certificateURI = certificateURI this.privateKeyURI = privateKeyURI } } export type SSLTerminationChanges = { name?: string, path?: string, sslPort?: number, clearPort?: number, servicePort?: number, serviceEndpoint?: string, protocol?: NginxProtocol, certificateURI?: string, privateKeyURI?: string } export class SSLTerminationEndpointApp { private static initialized: boolean = false private static broker: ISSLTerminationBroker public static get ready() { return SSLTerminationEndpointApp.initialized } public static init(broker: ISSLTerminationBroker) { SSLTerminationEndpointApp.assureNotInitialized() SSLTerminationEndpointApp.broker = broker broker.init() SSLTerminationEndpointApp.initialized = true } public static async createSSLTermination( name: string, sslPort: number, clearPort: number, servicePort: number, serviceEndpoint: string, protocol: NginxProtocol, certificateURI: string, privateKeyURI: string ): Promise { SSLTerminationEndpointApp.assureInitialized() return await this.broker.createSSLTermination( name, sslPort, clearPort, servicePort, serviceEndpoint, protocol, certificateURI, privateKeyURI ) } // Getting endpoints may be null, react over them public static async getSSLTerminationByPath( name: string ): Promise { SSLTerminationEndpointApp.assureInitialized() return await this.broker.getSSLTerminationByPath( name ) } // Throw if something goes wrong public static async modifySSLTerminationByPath( name: string, changes: SSLTerminationChanges ): Promise { SSLTerminationEndpointApp.assureInitialized() return await this.broker.modifySSLTerminationByPath( name, changes ) } public static async deleteSSLTerminationByPath( name: string ): Promise { SSLTerminationEndpointApp.assureInitialized() return await this.broker.deleteSSLTerminationByPath( name ) } public static async getAllSSLTerminations(): Promise { SSLTerminationEndpointApp.assureInitialized() return await SSLTerminationEndpointApp.broker.getAllSSLTerminations() } public static async activateEndpointByPath(path: string) { SSLTerminationEndpointApp.assureInitialized() return await SSLTerminationEndpointApp.broker.activateEndpointByPath( path ) } public static async deactivateEndpointByPath(path: string) { SSLTerminationEndpointApp.assureInitialized() return await SSLTerminationEndpointApp.broker.deactivateEndpointByPath( path ) } private static assureNotInitialized() { if (SSLTerminationEndpointApp.initialized) { // UGLY: more specific throw new Error("SSLTerminationEndpointApp has been already initialized") } } private static assureInitialized() { if (SSLTerminationEndpointApp.initialized) { // UGLY: more specific throw new Error("SSLTerminationEndpointApp has not been initialized yet") } } }