network chunk v12

This commit is contained in:
andrei-mihnea-cerbu
2024-11-13 13:33:03 +02:00
parent 664b987269
commit 39d007e34f
12 changed files with 182 additions and 436 deletions
@@ -22,16 +22,19 @@ export class TcpClientCommunicator extends SocketCommunicatorBase {
}
setServerPublicKey(publicKey: string): void {
console.log('\n\nSetting server public key\n\n');
this.publicKey = publicKey;
}
setAesKey(aesKey: string, aesIv: string): void {
console.log('\n\nSetting AES key\n\n');
this.aesKey = Buffer.from(aesKey, 'base64');
this.aesIv = Buffer.from(aesIv, 'base64');
}
async handleIncomingMessage(incomingMessage: string): Promise<void> {
let messageToProcess;
if (this.aesKey && this.aesIv) {
messageToProcess = this.decryptWithAes(incomingMessage);
} else if (this.publicKey) {
@@ -2,6 +2,7 @@ import { Socket } from 'net';
import { MessageHandler } from '../message_handler';
import { SocketCommunicatorBase } from './socket_communicator_base';
import { OperationHandler } from '../operations_base/operation_handler';
import {operationCodes} from "../operation_codes";
export class TcpServerCommunicator extends SocketCommunicatorBase {
private readonly socket: Socket;
@@ -17,7 +18,7 @@ export class TcpServerCommunicator extends SocketCommunicatorBase {
throw new Error('RSA key pair is not available. Please generate RSA key pair.');
}
await this.sendMessage('SET_PUBLIC_KEY', { publicKey: this.publicKey });
await this.sendMessage(operationCodes.SET_PUBLIC_KEY, { publicKey: this.publicKey });
}
async sendAesKey(): Promise<void> {
@@ -28,7 +29,7 @@ export class TcpServerCommunicator extends SocketCommunicatorBase {
const aesKeyBase64 = this.aesKey.toString('base64');
const aesIvBase64 = this.aesIv.toString('base64');
await this.sendMessage('SET_AES_KEY', { aesKey: aesKeyBase64, aesIv: aesIvBase64 });
await this.sendMessage(operationCodes.SET_AES_KEY, { aesKey: aesKeyBase64, aesIv: aesIvBase64 });
}
async handleIncomingMessage(incomingMessage: string): Promise<void> {
@@ -45,10 +46,10 @@ export class TcpServerCommunicator extends SocketCommunicatorBase {
let outgoingMessage: string;
switch (operationCode) {
case 'SET_PUBLIC_KEY':
case operationCodes.SET_PUBLIC_KEY:
outgoingMessage = Buffer.from(message, 'utf-8').toString('base64');
break;
case 'SET_AES_KEY':
case operationCodes.SET_AES_KEY:
outgoingMessage = this.encryptWithRsa(message);
break;
default: