Skip to content

Commit

Permalink
feat(backend): client is now utf-8 only
Browse files Browse the repository at this point in the history
- removed environment variable
- negotiate only utf-8
- if utf-8 is not supported, throw an error
  • Loading branch information
mystiker committed Oct 3, 2024
1 parent 44a0b4c commit f1e1e16
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 273 deletions.
136 changes: 0 additions & 136 deletions backend/src/core/environment/environment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ describe('Environment', () => {

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

process.env.CHARSET = 'utf-8';

const env = await getFreshEnvironmentInstance();

const { Environment } = await import('./environment');
Expand All @@ -47,15 +45,11 @@ describe('Environment', () => {

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

process.env.CHARSET = 'utf-8';

const env = await getFreshEnvironmentInstance();

expect(env.telnetHost).toBe('localhost');

expect(env.telnetPort).toBe(3000);

expect(env.charset).toBe('utf-8');
});

it('should handle optional TLS configuration', async () => {
Expand All @@ -65,8 +59,6 @@ describe('Environment', () => {

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

process.env.CHARSET = 'utf-8';

process.env.TELNET_TLS = 'true';

const env = await getFreshEnvironmentInstance();
Expand All @@ -81,8 +73,6 @@ describe('Environment', () => {

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

process.env.CHARSET = 'utf-8';

process.env.TELNET_TLS = 'TRUE';

const env = await getFreshEnvironmentInstance();
Expand All @@ -97,137 +87,11 @@ describe('Environment', () => {

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

process.env.CHARSET = 'utf-8';

const env = await getFreshEnvironmentInstance();

expect(env.telnetTLS).toBe(false);
});

it('should use utf-8 charset when set to utf8', 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.charset).toBe('utf-8');
});

it('should use utf-8 charset when set to utf-8', async () => {
process.env.TELNET_HOST = 'localhost';

process.env.TELNET_PORT = '3000';

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

process.env.CHARSET = 'utf-8';

const env = await getFreshEnvironmentInstance();

expect(env.charset).toBe('utf-8');
});

it('should use latin1 charset when set to latin1', async () => {
process.env.TELNET_HOST = 'localhost';

process.env.TELNET_PORT = '3000';

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

process.env.CHARSET = 'latin1';

const env = await getFreshEnvironmentInstance();

expect(env.charset).toBe('latin1');
});

it('should use latin1 charset when set to iso-8859-1', async () => {
process.env.TELNET_HOST = 'localhost';

process.env.TELNET_PORT = '3000';

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

process.env.CHARSET = 'iso-8859-1';

const env = await getFreshEnvironmentInstance();

expect(env.charset).toBe('latin1');
});

it('should use ascii charset when set to ascii', async () => {
process.env.TELNET_HOST = 'localhost';

process.env.TELNET_PORT = '3000';

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

process.env.CHARSET = 'ascii';

const env = await getFreshEnvironmentInstance();

expect(env.charset).toBe('ascii');
});

it('should use ascii charset when set to us-ascii', async () => {
process.env.TELNET_HOST = 'localhost';

process.env.TELNET_PORT = '3000';

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

process.env.CHARSET = 'us-ascii';

const env = await getFreshEnvironmentInstance();

expect(env.charset).toBe('ascii');
});

it('should throw an error for invalid charset', async () => {
process.env.TELNET_HOST = 'localhost';

process.env.TELNET_PORT = '3000';

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

process.env.CHARSET = 'invalid-charset';

await expect(getFreshEnvironmentInstance()).rejects.toThrow();
});

it('should use utf-8 as the default charset when CHARSET is not set', async () => {
process.env.TELNET_HOST = 'localhost';

process.env.TELNET_PORT = '3000';

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

delete process.env.CHARSET; // Remove CHARSET from environment

const env = await getFreshEnvironmentInstance();

expect(env.charset).toBe('utf-8'); // Default value
});

it('should default to utf-8 when CHARSET is empty', async () => {
process.env.TELNET_HOST = 'localhost';

process.env.TELNET_PORT = '3000';

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

process.env.CHARSET = ''; // Set CHARSET to an empty string

const env = await getFreshEnvironmentInstance();

expect(env.charset).toBe('utf-8'); // Default value
});

it('should set projectRoot correctly', async () => {
process.env.TELNET_HOST = 'localhost';

Expand Down
25 changes: 0 additions & 25 deletions backend/src/core/environment/environment.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
import { config as configureEnvironment } from 'dotenv';

import { logger } from '../../shared/utils/logger.js';
import { mapToServerEncodings } from '../../shared/utils/supported-encodings.js';
import { IEnvironment } from './types/environment.js';
import { getEnvironmentVariable } from './utils/get-environment-variable.js';
import { resolveModulePath } from './utils/resolve-modulepath.js';

const ALLOWED_CHARSETS = [
'utf8',
'utf-8',
'latin1',
'iso-8859-1',
'ascii',
'us-ascii',
];

/**
* Environment class to handle environment variables and application settings.
* Reads the environment variables once oppon initialisation and provides them as properties.
Expand All @@ -27,7 +17,6 @@ export class Environment implements IEnvironment {
public readonly telnetHost: string;
public readonly telnetPort: number;
public readonly telnetTLS: boolean;
public readonly charset: BufferEncoding;
public readonly projectRoot: string;
public readonly socketRoot: string;
public readonly socketTimeout: number;
Expand Down Expand Up @@ -57,20 +46,6 @@ export class Environment implements IEnvironment {

this.socketRoot = String(getEnvironmentVariable('SOCKET_ROOT'));

const charset = String(
getEnvironmentVariable('CHARSET', false, 'utf-8'),
).toLocaleLowerCase();

const mappedCharset = mapToServerEncodings(charset);

if (mappedCharset === null) {
throw new Error(
`Environment variable "CHARSET" must be one of ${ALLOWED_CHARSETS.join(', ')}.`,
);
}

this.charset = mappedCharset;

this.socketTimeout = Number(
getEnvironmentVariable('SOCKET_TIMEOUT', false, '900000'),
);
Expand Down
22 changes: 0 additions & 22 deletions backend/src/core/environment/types/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,7 @@ export interface IEnvironment {
readonly projectRoot: string;
readonly socketRoot: string;

readonly charset: BufferEncoding;

readonly socketTimeout: number;

readonly environment: 'production' | 'development';

// backend: {
// host: string;
// port: number;
// };
// frontend: {
// host: string;
// port: number;
// };
// mudrpc: {
// socketfile: string;
// };
// webmud: {
// mudname: string;
// autoConnect: boolean;
// autoLogin: boolean;
// autoUser: string;
// autoToken: string;
// localEcho: boolean;
// };
}
8 changes: 2 additions & 6 deletions backend/src/core/sockets/socket-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,7 @@ export class SocketManager extends Server<
return;
}

telnetClient.sendMessage(
// data.toString(Environment.getInstance().charset) + '\r\n',
`${data}\r\n`,
);
telnetClient.sendMessage(`${data}\r\n`);
});

socket.on('mudConnect', () => {
Expand All @@ -147,7 +144,6 @@ export class SocketManager extends Server<
this.managerOptions.telnetHost,
this.managerOptions.telnetPort,
this.managerOptions.useTelnetTls,
Environment.getInstance().charset,
);

telnetClient.on('data', (data: string | Buffer) => {
Expand Down Expand Up @@ -192,7 +188,7 @@ export class SocketManager extends Server<
});

telnetClient.on('negotiationChanged', (negotiation) => {
logger.info(
logger.verbose(
`[Socket-Manager] [Client] ${socket.id} telnet negotiation changed. Emitting 'negotiationChanged'`,
negotiation,
);
Expand Down
14 changes: 2 additions & 12 deletions backend/src/features/telnet/telnet-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ export class TelnetClient extends EventEmitter<TelnetClientEvents> {
* @param {number} telnetPort - The port number of the Telnet server.
* @param {boolean} useTls - Indicates whether to use TLS encryption for the connection.
*/
constructor(
telnetHost: string,
telnetPort: number,
useTls: boolean,
encoding: BufferEncoding,
) {
constructor(telnetHost: string, telnetPort: number, useTls: boolean) {
super();

const telnetConnection = createTelnetConnection(
Expand All @@ -76,10 +71,7 @@ export class TelnetClient extends EventEmitter<TelnetClientEvents> {
});

this.optionsHandler = new Map([
[
TelnetOptions.TELOPT_CHARSET,
handleCharsetOption(this.telnetSocket, encoding),
],
[TelnetOptions.TELOPT_CHARSET, handleCharsetOption(this.telnetSocket)],
[TelnetOptions.TELOPT_ECHO, handleEchoOption(this.telnetSocket)],
]);

Expand All @@ -103,8 +95,6 @@ export class TelnetClient extends EventEmitter<TelnetClientEvents> {
this.emit('data', chunkData);
});

logger.info(`[Telnet-Client] Created`);

this.connected = true;
}

Expand Down
Loading

0 comments on commit f1e1e16

Please sign in to comment.