Skip to content

Commit

Permalink
feat: excludeProxyList Interceptor AnWeber/vscode-httpyac#176
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed Feb 9, 2023
1 parent 5371f77 commit 68df971
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

- support Intellij Request Body separator
- support Intellij TextResponseStreaming (onEachLIne, onEachMessage)
- add `proxyExcludeList` config (AnWeber/vscode-httpyac#176)

#### Fixes

Expand Down
1 change: 1 addition & 0 deletions src/models/environmentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface EnvironmentConfig {

request?: ConfigRequest;
proxy?: string;
proxyExcludeList?: Array<string>;
/** count auf characters before pretty print is ommited (default: 1000000)*/
requestPrettyPrintBodyMaxSize?: number;
requestBodyInjectVariablesExtensions?: Array<string>;
Expand Down
1 change: 1 addition & 0 deletions src/plugins/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ function initOnRequestHook(api: models.HttpyacHooksApi) {
api.hooks.onRequest.addHook('encodeRequestBody', request.encodeRequestBody);

api.hooks.onRequest.addInterceptor(request.isTrustedInterceptor);
api.hooks.onRequest.addInterceptor(request.excludeProxyInterceptor);
}
25 changes: 25 additions & 0 deletions src/plugins/core/request/excludeProxyInterceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as models from '../../../models';
import { HookTriggerContext } from 'hookpoint';

export const excludeProxyInterceptor = {
id: 'excludeProxy',
afterLoop: async function excludeProxy(
hookContext: HookTriggerContext<[models.Request, models.ProcessorContext], void>
): Promise<boolean> {
const [request, context] = hookContext.args;
if (!request.proxy) {
request.proxy = context.config?.proxy;
}

if (request.proxy) {
if (context.httpRegion.metaData.noProxy) {
delete request.proxy;
} else if (context.config?.proxyExcludeList) {
if (context.config.proxyExcludeList.some(proxyExclude => request.url.startsWith(proxyExclude))) {
delete request.proxy;
}
}
}
return true;
},
};
1 change: 1 addition & 0 deletions src/plugins/core/request/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './attachDefaultHeaders';
export * from './encodeRequestBody';
export * from './excludeProxyInterceptor';
export * from './isTrustedInterceptor';
export * from './requestVariableReplacer';
export * from './setEnvRequestOptions';
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/core/request/isTrustedInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { HookTriggerContext } from 'hookpoint';

export const isTrustedInterceptor = {
id: 'isTrusted',
afterLoop: async function escapeVariable(
afterLoop: async function isTrusted(
hookContext: HookTriggerContext<[models.Request, models.ProcessorContext], void>
): Promise<boolean> {
if (!io.userInteractionProvider.isTrusted()) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/http/gotUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function getClientOptions(
options.https = request.options?.https || {};
options.https.rejectUnauthorized = false;
}
initProxy(options, request.proxy || config?.proxy);
initProxy(options, request.proxy);
ensureStringHeaders(options.headers);

log.debug('request', options);
Expand Down
19 changes: 19 additions & 0 deletions src/plugins/websocket/websocketRequestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import * as store from '../../store';
import * as utils from '../../utils';
import { isWebsocketRequest, WebsocketRequest } from './websocketRequest';
import { IncomingMessage } from 'http';
import { HttpProxyAgent } from 'http-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { SocksProxyAgent } from 'socks-proxy-agent';
import WebSocket, { ClientOptions } from 'ws';

const WEBSOCKET_CLOSE_NORMAL = 1000;
Expand Down Expand Up @@ -175,6 +178,9 @@ export class WebsocketRequestClient extends models.AbstractRequestClient<WebSock
if (request.noRejectUnauthorized) {
metaDataOptions.rejectUnauthorized = false;
}
if (request.proxy) {
this.initProxy(configOptions, request.proxy);
}
return Object.assign({}, config?.request, request.options, metaDataOptions);
}

Expand Down Expand Up @@ -202,4 +208,17 @@ export class WebsocketRequestClient extends models.AbstractRequestClient<WebSock
store.userSessionStore.removeUserSession(this.getWebsocketId(this.request));
}
}

private initProxy(options: ClientOptions, proxy: string | undefined) {
if (proxy) {
if (proxy.startsWith('socks://')) {
const socksProxy = new SocksProxyAgent(proxy);
options.agent = socksProxy;
} else if (proxy.startsWith('http://')) {
options.agent = new HttpProxyAgent(proxy);
} else {
options.agent = new HttpsProxyAgent(proxy);
}
}
}
}
4 changes: 4 additions & 0 deletions src/utils/parserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ export const knownMetaData: Array<{
name: 'noClientCert',
description: 'SSL client certificate is not send for this request',
},
{
name: 'noProxy',
description: 'disable proxy for this request',
},
{
name: 'noRejectUnauthorized',
description: 'all invalid SSL certificates will be ignored and no error will be thrown.',
Expand Down

0 comments on commit 68df971

Please sign in to comment.