Skip to content

Commit

Permalink
feat(backend): re-added new environment options
Browse files Browse the repository at this point in the history
- added TELNET_TLS: set to "true" or "false" for secure telnet connections
- removed TLS, TLS_KEY und TLS_CERT
- added SOCKET_ROOT: set to "<value>" for a fixed SOCKET URL
- reverted backendUrl
  • Loading branch information
myst committed Aug 11, 2024
1 parent 6ea4313 commit fc34841
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 44 deletions.
23 changes: 15 additions & 8 deletions backend/src/core/environment/environment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -59,37 +63,38 @@ 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 () => {
process.env.TELNET_HOST = 'localhost';

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 () => {
process.env.TELNET_HOST = 'localhost';

process.env.TELNET_PORT = '3000';

process.env.SOCKET_ROOT = '/socket.io';

const env = await getFreshEnvironmentInstance();

expect(env.charset).toBe('utf8');
Expand All @@ -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');
Expand Down
22 changes: 7 additions & 15 deletions backend/src/core/environment/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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'));
Expand All @@ -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(
Expand Down
5 changes: 2 additions & 3 deletions backend/src/core/environment/types/environment-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
7 changes: 2 additions & 5 deletions backend/src/core/environment/types/environment.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
3 changes: 2 additions & 1 deletion backend/src/core/middleware/use-sockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
};
13 changes: 7 additions & 6 deletions backend/src/core/sockets/socket-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) => {
Expand Down
4 changes: 1 addition & 3 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions frontend/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"budgets": [
{
"type": "initial",
"maximumWarning": "6mb",
"maximumError": "8mb"
"maximumWarning": "8mb",
"maximumError": "10mb"
},
{
"type": "anyComponentStyle",
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

/*
Expand Down

0 comments on commit fc34841

Please sign in to comment.