Path: blob/main/mitm-socket/lib/MitmSocketSession.ts
1030 views
import Log from '@secret-agent/commons/Logger';1import { IBoundLog } from '@secret-agent/interfaces/ILog';2import MitmSocket from '..';3import BaseIpcHandler, { IGoIpcOpts } from './BaseIpcHandler';45const { log } = Log(module);67export default class MitmSocketSession extends BaseIpcHandler {8protected logger: IBoundLog;910private readonly socketsById = new Map<number, MitmSocket>();1112constructor(readonly sessionId: string, options: IGoIpcOpts) {13super({ ...options, mode: 'proxy' });14this.logger = log.createChild(module, { sessionId });15}1617public async requestSocket(socket: MitmSocket): Promise<void> {18const id = socket.id;19this.socketsById.set(id, socket);2021socket.once('close', () => this.socketsById.delete(id));2223await this.waitForConnected;2425try {26await this.sendIpcMessage({27id,28socketPath: socket.socketPath,29...socket.connectOpts,30});31} catch (error) {32if (this.isClosing) {33return null;34}35this.logger.info('MitmSocketSession.requestSocketError', {36error,37});38}39}4041protected onMessage(rawMessage: string): void {42if (this.isClosing) return;43const message = JSON.parse(rawMessage);44if (this.options.debug) {45this.logger.info('MitmSocketSession.onMessage', {46...message,47});48}49if (message?.id) {50this.socketsById.get(message.id)?.onMessage(message);51}52}5354protected beforeExit(): void {55for (const socket of this.socketsById.values()) {56socket.onExit();57}58this.socketsById.clear();59}60}616263