Skip to content

Commit

Permalink
feat(repeater): add --proxy-domains-bypass flag (#616)
Browse files Browse the repository at this point in the history
this flag allows specific requests with URLs matching any of the hosts
provided in the flag’s input to bypass the proxy when it is enabled.
  • Loading branch information
maksadbek authored Nov 25, 2024
1 parent 557bd5d commit 4a8084c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/Commands/RunRepeater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,19 @@ export class RunRepeater implements CommandModule {
requiresArg: true,
array: true,
describe:
'Comma-separated list of domains that should be routed through the proxy. This option is only applicable when using the --proxy option'
'Space-separated list of domains that should be routed through the proxy. This option is only applicable when using the --proxy option'
})
.option('proxy-domains-bypass', {
requiresArg: true,
array: true,
describe:
'Space-separated list of domains that should not be routed through the proxy. This option is only applicable when using the --proxy option'
})
.conflicts({
daemon: 'remove-daemon',
ntlm: [
'proxy',
'proxy-bright',
'proxy-target',
'experimental-connection-reuse'
]
ntlm: ['proxy', 'experimental-connection-reuse']
})
.conflicts('proxy-domains', 'proxy-domains-bypass')
.env('REPEATER')
.middleware((args: Arguments) => {
if (Object.hasOwnProperty.call(args, '')) {
Expand Down Expand Up @@ -196,7 +198,8 @@ export class RunRepeater implements CommandModule {
{ type: 'application/ld+json', allowTruncation: false },
{ type: 'application/graphql', allowTruncation: false }
],
proxyDomains: args.proxyDomains as string[]
proxyDomains: args.proxyDomains as string[],
proxyDomainsBypass: args.proxyDomainsBypass as string[]
}
})
.register<DefaultRepeaterServerOptions>(
Expand Down
28 changes: 28 additions & 0 deletions src/RequestExecutor/HttpRequestExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class HttpRequestExecutor implements RequestExecutor {
private readonly httpAgent?: http.Agent;
private readonly httpsAgent?: https.Agent;
private readonly proxyDomains?: RegExp[];
private readonly proxyDomainsBypass?: RegExp[];

get protocol(): Protocol {
return Protocol.HTTP;
Expand Down Expand Up @@ -67,11 +68,23 @@ export class HttpRequestExecutor implements RequestExecutor {
this.httpAgent = new http.Agent(agentOptions);
}

if (this.options.proxyDomains && this.options.proxyDomainsBypass) {
throw new Error(
'cannot use both proxyDomains and proxyDomainsBypass at the same time'
);
}

if (this.options.proxyDomains) {
this.proxyDomains = this.options.proxyDomains.map((domain) =>
Helpers.wildcardToRegExp(domain)
);
}

if (this.options.proxyDomainsBypass) {
this.proxyDomainsBypass = this.options.proxyDomainsBypass.map((domain) =>
Helpers.wildcardToRegExp(domain)
);
}
}

public async execute(options: Request): Promise<Response> {
Expand Down Expand Up @@ -197,12 +210,27 @@ export class HttpRequestExecutor implements RequestExecutor {
}

private getRequestAgent(options: Request) {
// do not use proxy for domains that are not in the list
if (
this.proxyDomains &&
!this.proxyDomains.some((domain) =>
domain.test(parseUrl(options.url).hostname)
)
) {
logger.debug("Not using proxy for URL '%s'", options.url);

return options.secureEndpoint ? this.httpsAgent : this.httpAgent;
}

// do not use proxy for domains that are in the bypass list
if (
this.proxyDomainsBypass &&
this.proxyDomainsBypass.some((domain) =>
domain.test(parseUrl(options.url).hostname)
)
) {
logger.debug("Bypassing proxy for URL '%s'", options.url);

return options.secureEndpoint ? this.httpsAgent : this.httpAgent;
}

Expand Down
1 change: 1 addition & 0 deletions src/RequestExecutor/RequestExecutorOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface RequestExecutorOptions {
maxContentLength?: number;
reuseConnection?: boolean;
proxyDomains?: string[];
proxyDomainsBypass?: string[];
}

export const RequestExecutorOptions: unique symbol = Symbol(
Expand Down

0 comments on commit 4a8084c

Please sign in to comment.