Skip to content

Commit

Permalink
fix(plugin): take into account both --remote-debugging-address and …
Browse files Browse the repository at this point in the history
…`--remote-debugging-port` (#96)

closes #84
  • Loading branch information
derevnjuk authored Sep 7, 2022
1 parent 91726aa commit daf6da3
Showing 1 changed file with 48 additions and 13 deletions.
61 changes: 48 additions & 13 deletions src/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
);
Expand Down Expand Up @@ -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;
}
}

0 comments on commit daf6da3

Please sign in to comment.