SSL-Sniffer/src/lib/server/classes/endpoints/ssl-termination-endpoint.ts

276 lines
6.9 KiB
TypeScript
Raw Normal View History

2025-07-02 14:49:23 +00:00
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<void>
// TODO: in the next version support
// TODO: creation of endpoints
// TODO: according to path
// Creation should throw if something goes wrong
// with reasons why
createSSLTerminationSimple(
name: string,
servicePort: number,
serviceEndpoint: string,
certificateURI: string,
privateKeyURI: string
): Promise<SSLTermination>
createSSLTerminationComplete(
name: string,
sslPort: number,
clearPort: number,
servicePort: number,
serviceEndpoint: string,
certificateURI: string,
privateKeyURI: string
): Promise<SSLTermination>
activateEndpointByPath(
path: string
): Promise<boolean>
deactivateEndpointByPath(
path: string
): Promise<boolean>
// Getting endpoints may be null, react over them
getSSLTerminationByPath(
path: string
): Promise<SSLTermination|null>
// Throw if something goes wrong
modifySSLTerminationByPath(
path: string,
changes: SSLTerminationChanges
): Promise<SSLTermination>
deleteSSLTerminationByPath(
path: string
): Promise<SSLTermination|null>
getAllSSLTerminations(): Promise<SSLTermination[]>
}
/**
* 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
}
// Creation should throw if something goes wrong
// with reasons why
public static async createSSLTerminationSimple(
name: string,
servicePort: number,
serviceEndpoint: string,
certificateURI: string,
privateKeyURI: string
): Promise<SSLTermination> {
SSLTerminationEndpointApp.assureInitialized()
return await this.broker.createSSLTerminationSimple(
name,
servicePort,
serviceEndpoint,
certificateURI,
privateKeyURI
)
}
public static async createSSLTerminationComplete(
name: string,
sslPort: number,
clearPort: number,
servicePort: number,
serviceEndpoint: string,
certificateURI: string,
privateKeyURI: string
): Promise<SSLTermination> {
SSLTerminationEndpointApp.assureInitialized()
return await this.broker.createSSLTerminationComplete(
name,
sslPort,
clearPort,
servicePort,
serviceEndpoint,
certificateURI,
privateKeyURI
)
}
// Getting endpoints may be null, react over them
public static async getSSLTerminationByPath(
name: string
): Promise<SSLTermination|null> {
SSLTerminationEndpointApp.assureInitialized()
return await this.broker.getSSLTerminationByPath(
name
)
}
// Throw if something goes wrong
public static async modifySSLTerminationByPath(
name: string,
changes: SSLTerminationChanges
): Promise<SSLTermination> {
SSLTerminationEndpointApp.assureInitialized()
return await this.broker.modifySSLTerminationByPath(
name,
changes
)
}
public static async deleteSSLTerminationByPath(
name: string
): Promise<SSLTermination|null> {
SSLTerminationEndpointApp.assureInitialized()
return await this.broker.deleteSSLTerminationByPath(
name
)
}
public static async getAllSSLTerminations(): Promise<SSLTermination[]> {
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")
}
}
}