Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(repeater): add --proxy-domains-bypass flag #616

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading