From daf6da3a9d3c369b3d8c16efc681cdf175d0d943 Mon Sep 17 00:00:00 2001 From: Artem Derevnjuk Date: Thu, 8 Sep 2022 01:12:02 +0400 Subject: [PATCH] fix(plugin): take into account both `--remote-debugging-address` and `--remote-debugging-port` (#96) closes #84 --- src/Plugin.ts | 61 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/src/Plugin.ts b/src/Plugin.ts index e74e7c7..c4f2d85 100644 --- a/src/Plugin.ts +++ b/src/Plugin.ts @@ -20,10 +20,17 @@ export interface RecordOptions { includeHosts: string[]; } +interface Addr { + port?: number; + host?: string; +} + export class Plugin { - private rdpPort?: number; + private addr?: Addr; private entries: Entry[] = []; private connection?: CRIConnection; + private readonly PORT_OPTION_NAME = '--remote-debugging-port'; + private readonly ADDRESS_OPTION_NAME = '--remote-debugging-address'; constructor( private readonly logger: Logger, @@ -51,7 +58,7 @@ export class Plugin { await this.closeConnection(); this.connection = new CRIConnection( - { port: this.rdpPort }, + this.addr, this.logger, new RetryStrategy(20, 5, 100) ); @@ -142,24 +149,52 @@ export class Plugin { } private ensureRdpPort(args: string[]): string[] { - this.rdpPort = this.getRdpPortFromArgs(args); + const { + host = 'localhost', + port = 40000 + Math.round(Math.random() * 25000) + } = this.extractAddrFromArgs(args); + + this.addr = { host, port }; + + return [ + ...args, + `${this.PORT_OPTION_NAME}=${port}`, + `${this.ADDRESS_OPTION_NAME}=${host}` + ]; + } + + private extractAddrFromArgs(args: string[]): Addr { + const port: number | undefined = +this.findAndParseIfPossible( + args, + this.PORT_OPTION_NAME + ); + const host: string | undefined = this.findAndParseIfPossible( + args, + this.ADDRESS_OPTION_NAME + ); - if (this.rdpPort) { - return args; + let addr: { port?: number; host?: string } = {}; + + if (!isNaN(port) && isFinite(port)) { + addr = { port }; } - this.rdpPort = 40000 + Math.round(Math.random() * 25000); + if (host) { + addr = { ...addr, host }; + } - return [...args, `--remote-debugging-port=${this.rdpPort}`]; + return addr; } - private getRdpPortFromArgs(args: string[]): number | undefined { - const existing: string | undefined = args.find((arg: string): boolean => - arg.startsWith('--remote-debugging-port=') + private findAndParseIfPossible( + args: string[], + optionName: string + ): string | undefined { + const arg: string | undefined = args.find((x: string): boolean => + x.startsWith(optionName) ); + const [, value]: string[] = arg?.split('=', 2) ?? []; - if (existing) { - return +existing.split('=')[1]; - } + return value; } }