From fc3484151f2859b7d347842574751cf4802c8590 Mon Sep 17 00:00:00 2001 From: myst Date: Sun, 11 Aug 2024 23:59:49 +0200 Subject: [PATCH] feat(backend): re-added new environment options - added TELNET_TLS: set to "true" or "false" for secure telnet connections - removed TLS, TLS_KEY und TLS_CERT - added SOCKET_ROOT: set to "" for a fixed SOCKET URL - reverted backendUrl --- .../src/core/environment/environment.spec.ts | 23 ++++++++++++------- backend/src/core/environment/environment.ts | 22 ++++++------------ .../environment/types/environment-keys.ts | 5 ++-- .../src/core/environment/types/environment.ts | 7 ++---- backend/src/core/middleware/use-sockets.ts | 3 ++- backend/src/core/sockets/socket-manager.ts | 13 ++++++----- backend/src/main.ts | 4 +--- frontend/angular.json | 4 ++-- frontend/src/environments/environment.ts | 2 +- 9 files changed, 39 insertions(+), 44 deletions(-) diff --git a/backend/src/core/environment/environment.spec.ts b/backend/src/core/environment/environment.spec.ts index 3dd48aba..a99a322a 100644 --- a/backend/src/core/environment/environment.spec.ts +++ b/backend/src/core/environment/environment.spec.ts @@ -29,6 +29,8 @@ describe('Environment', () => { process.env.TELNET_PORT = '3000'; + process.env.SOCKET_ROOT = '/socket.io'; + process.env.CHARSET = 'utf8'; const env = await getFreshEnvironmentInstance(); @@ -43,6 +45,8 @@ describe('Environment', () => { process.env.TELNET_PORT = '3000'; + process.env.SOCKET_ROOT = '/socket.io'; + process.env.CHARSET = 'utf8'; const env = await getFreshEnvironmentInstance(); @@ -59,18 +63,15 @@ describe('Environment', () => { process.env.TELNET_PORT = '3000'; - process.env.CHARSET = 'utf8'; + process.env.SOCKET_ROOT = '/socket.io'; - process.env.TLS_CERT = 'cert_value'; + process.env.CHARSET = 'utf8'; - process.env.TLS_KEY = 'key_value'; + process.env.TELNET_TLS = 'true'; const env = await getFreshEnvironmentInstance(); - expect(env.tls).toEqual({ - cert: 'cert_value', - key: 'key_value', - }); + expect(env.telnetTLS).toBe(true); }); it('should handle missing TLS configuration gracefully', async () => { @@ -78,11 +79,13 @@ describe('Environment', () => { process.env.TELNET_PORT = '3000'; + process.env.SOCKET_ROOT = '/socket.io'; + process.env.CHARSET = 'utf8'; const env = await getFreshEnvironmentInstance(); - expect(env.tls).toBeUndefined(); + expect(env.telnetTLS).toBe(false); }); it('should use default charset if not set', async () => { @@ -90,6 +93,8 @@ describe('Environment', () => { process.env.TELNET_PORT = '3000'; + process.env.SOCKET_ROOT = '/socket.io'; + const env = await getFreshEnvironmentInstance(); expect(env.charset).toBe('utf8'); @@ -102,6 +107,8 @@ describe('Environment', () => { process.env.CHARSET = 'utf8'; + process.env.SOCKET_ROOT = '/socket.io'; + const env = await getFreshEnvironmentInstance(); expect(env.projectRoot).toBe('/path/to/project/root'); diff --git a/backend/src/core/environment/environment.ts b/backend/src/core/environment/environment.ts index ba111b08..b1810f6d 100644 --- a/backend/src/core/environment/environment.ts +++ b/backend/src/core/environment/environment.ts @@ -16,12 +16,10 @@ export class Environment implements IEnvironment { public readonly port: number; public readonly telnetHost: string; public readonly telnetPort: number; - public readonly tls?: { - cert: string; - key: string; - }; + public readonly telnetTLS: boolean; public readonly charset: string; public readonly projectRoot: string; + public readonly socketRoot: string; public readonly socketTimeout: number; /** @@ -31,17 +29,6 @@ export class Environment implements IEnvironment { private constructor() { configureEnvironment(); - const tls_cert = getEnvironmentVariable('TLS_CERT', false); - - const tls_key = getEnvironmentVariable('TLS_KEY', false); - - if (tls_cert !== null && tls_key !== null) { - this.tls = { - cert: tls_cert, - key: tls_key, - }; - } - this.host = String(getEnvironmentVariable('HOST', false, '0.0.0.0')); this.port = Number(getEnvironmentVariable('PORT', false, '5000')); @@ -50,6 +37,11 @@ export class Environment implements IEnvironment { this.telnetPort = Number(getEnvironmentVariable('TELNET_PORT')); + this.telnetTLS = + getEnvironmentVariable('TELNET_TLS', false, 'false') === 'true'; + + this.socketRoot = String(getEnvironmentVariable('SOCKET_ROOT')); + this.charset = String(getEnvironmentVariable('CHARSET', false, 'utf8')); this.socketTimeout = Number( diff --git a/backend/src/core/environment/types/environment-keys.ts b/backend/src/core/environment/types/environment-keys.ts index b5a19c0d..5c008371 100644 --- a/backend/src/core/environment/types/environment-keys.ts +++ b/backend/src/core/environment/types/environment-keys.ts @@ -3,8 +3,7 @@ export type EnvironmentKeys = | 'PORT' | 'TELNET_HOST' | 'TELNET_PORT' + | 'TELNET_TLS' | 'SOCKET_TIMEOUT' // in milliseconds | default: 900000 (15 min) | determines how long messages are buffed for the disconnected frontend and when the telnet connection is closed - | 'TLS' - | 'TLS_CERT' - | 'TLS_KEY' + | 'SOCKET_ROOT' | 'CHARSET'; diff --git a/backend/src/core/environment/types/environment.ts b/backend/src/core/environment/types/environment.ts index fe17b889..21f4653f 100644 --- a/backend/src/core/environment/types/environment.ts +++ b/backend/src/core/environment/types/environment.ts @@ -1,13 +1,10 @@ export interface IEnvironment { readonly telnetHost: string; readonly telnetPort: number; - - readonly tls?: { - cert: string; - key: string; - }; + readonly telnetTLS: boolean; readonly projectRoot: string; + readonly socketRoot: string; readonly charset: string; diff --git a/backend/src/core/middleware/use-sockets.ts b/backend/src/core/middleware/use-sockets.ts index 619bb253..b62d5e01 100644 --- a/backend/src/core/middleware/use-sockets.ts +++ b/backend/src/core/middleware/use-sockets.ts @@ -11,6 +11,7 @@ export const useSockets = ( new SocketManager(httpServer, { telnetHost: environment.telnetHost, telnetPort: environment.telnetPort, - useTls: environment.tls !== undefined, + useTelnetTls: environment.telnetTLS, + socketRoot: environment.socketRoot, }); }; diff --git a/backend/src/core/sockets/socket-manager.ts b/backend/src/core/sockets/socket-manager.ts index c8773292..536d438f 100644 --- a/backend/src/core/sockets/socket-manager.ts +++ b/backend/src/core/sockets/socket-manager.ts @@ -21,14 +21,15 @@ export class SocketManager extends Server< public constructor( server: HttpServer | HttpsServer, - private readonly telnetOptions: { + private readonly managerOptions: { telnetHost: string; telnetPort: number; - useTls: boolean; + useTelnetTls: boolean; + socketRoot: string; }, ) { super(server, { - path: '/socket.io', + path: managerOptions.socketRoot, transports: ['websocket'], connectionStateRecovery: { maxDisconnectionDuration: Environment.getInstance().socketTimeout, @@ -142,9 +143,9 @@ export class SocketManager extends Server< ); const telnetClient = new TelnetClient( - this.telnetOptions.telnetHost, - this.telnetOptions.telnetPort, - this.telnetOptions.useTls, + this.managerOptions.telnetHost, + this.managerOptions.telnetPort, + this.managerOptions.useTelnetTls, ); telnetClient.on('data', (data: string | Buffer) => { diff --git a/backend/src/main.ts b/backend/src/main.ts index 0e7a2191..7cd2fda4 100644 --- a/backend/src/main.ts +++ b/backend/src/main.ts @@ -18,9 +18,7 @@ const app = express(); const UNIQUE_SERVER_ID = uuidv4(); -const httpServer = createHttpServer(app, { - tls: environment.tls, -}); +const httpServer = createHttpServer(app, {}); useBodyParser(app); diff --git a/frontend/angular.json b/frontend/angular.json index f2596394..767dfb22 100644 --- a/frontend/angular.json +++ b/frontend/angular.json @@ -27,8 +27,8 @@ "budgets": [ { "type": "initial", - "maximumWarning": "6mb", - "maximumError": "8mb" + "maximumWarning": "8mb", + "maximumError": "10mb" }, { "type": "anyComponentStyle", diff --git a/frontend/src/environments/environment.ts b/frontend/src/environments/environment.ts index acf2c3c6..486bb076 100644 --- a/frontend/src/environments/environment.ts +++ b/frontend/src/environments/environment.ts @@ -7,7 +7,7 @@ import { Environment } from './environment.interface'; export const environment: Environment = { production: false, // Change this to your local IP if you want to test on a mobile device in the same network - backendUrl: () => 'http://192.168.178.76:5000', + backendUrl: () => window.location.origin, }; /*