Skip to content

Commit

Permalink
feat(backend): enabled status requests on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
mystiker committed Nov 4, 2024
1 parent 1aa74dc commit d9b23c1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum TelnetStatusSubnogiation {
STATUS_SEND = 1,
}
39 changes: 39 additions & 0 deletions backend/src/features/telnet/telnet-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import tls from 'tls';

import { logger } from '../../shared/utils/logger.js';
import { TelnetOptions } from './models/telnet-options.js';
import { TelnetStatusSubnogiation } from './models/telnet-status-subnogiation.js';
import { TelnetControlSequences } from './types/telnet-control-sequences.js';
import { TelnetNegotiations } from './types/telnet-negotiations.js';
import { TelnetOptionHandler } from './types/telnet-option-handler.js';
Expand Down Expand Up @@ -99,12 +100,50 @@ export class TelnetClient extends EventEmitter<TelnetClientEvents> {
this.telnetSocket.on('data', (chunkData: string | Buffer) => {
this.emit('data', chunkData);
});

this.on('negotiationChanged', (negotiation) => {
// Request initial status data after negotiation - we use TTYPE since this is subnegotiated the last
// Todo[myst]: Find a better way to do this but its not that easy, since everything is async
if (negotiation.option === TelnetOptions.TELOPT_TTYPE) {
this.requestStatus();
}
});
}

public sendMessage(data: string): void {
this.telnetSocket.write(data);
}

public requestStatus(): void {
const buffer = Buffer.from([TelnetStatusSubnogiation.STATUS_SEND]);

if (!this.connected) {
return;
}

const clientOption =
this._negotiations[TelnetOptions.TELOPT_STATUS]?.client;

const serverOption =
this._negotiations[TelnetOptions.TELOPT_STATUS]?.server;

if (
clientOption === undefined ||
clientOption !== TelnetControlSequences.DO
) {
return;
}

if (
serverOption === undefined ||
serverOption !== TelnetControlSequences.WILL
) {
return;
}

this.telnetSocket.writeSub(TelnetOptions.TELOPT_STATUS, buffer);
}

public disconnect(): void {
logger.info(`[Telnet-Client] Disconnect`);

Expand Down
8 changes: 0 additions & 8 deletions backend/src/features/telnet/utils/handle-status-option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import { TelnetControlSequences } from '../types/telnet-control-sequences.js';
import { TelnetOptionHandler } from '../types/telnet-option-handler.js';
import { TelnetOptionResult } from '../types/telnet-option-result.js';

enum TelnetStatusSubnogiation {
STATUS_SEND = 1,
}

const handleStatusDo = (socket: TelnetSocket) => (): TelnetOptionResult => {
socket.writeWill(TelnetOptions.TELOPT_STATUS);

Expand All @@ -24,10 +20,6 @@ const handleStatusDont = (socket: TelnetSocket) => (): TelnetOptionResult => {
const handleStatusWill = (socket: TelnetSocket) => (): TelnetOptionResult => {
socket.writeDo(TelnetOptions.TELOPT_STATUS);

const buffer = Buffer.from([TelnetStatusSubnogiation.STATUS_SEND]);

socket.writeSub(TelnetOptions.TELOPT_STATUS, buffer);

return { controlSequence: TelnetControlSequences.DO };
};

Expand Down

0 comments on commit d9b23c1

Please sign in to comment.