Skip to content

Commit

Permalink
feat: distinguih unconfigured proxy log vs no_proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed May 7, 2019
1 parent e6f155f commit 84e5265
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 44 deletions.
106 changes: 62 additions & 44 deletions src/classes/Agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Logger from '../Logger';
import type {
AgentType,
GetUrlProxyMethodType,
IsProxyConfiguredMethodType,
MustUrlUseProxyMethodType,
ProtocolType
} from '../types';
Expand All @@ -22,19 +23,23 @@ class Agent {

fallbackAgent: AgentType;

isProxyConfigured: IsProxyConfiguredMethodType;

mustUrlUseProxy: MustUrlUseProxyMethodType;

getUrlProxy: GetUrlProxyMethodType;

eventEmitter: EventEmitter;

constructor (
isProxyConfigured: IsProxyConfiguredMethodType,
mustUrlUseProxy: MustUrlUseProxyMethodType,
getUrlProxy: GetUrlProxyMethodType,
fallbackAgent: AgentType,
eventEmitter: EventEmitter
) {
this.fallbackAgent = fallbackAgent;
this.isProxyConfigured = isProxyConfigured;
this.mustUrlUseProxy = mustUrlUseProxy;
this.getUrlProxy = getUrlProxy;
this.eventEmitter = eventEmitter;
Expand All @@ -43,59 +48,72 @@ class Agent {
addRequest (request: *, configuration: *) {
const requestUrl = this.protocol + '//' + configuration.hostname + (configuration.port === 80 || configuration.port === 443 ? '' : ':' + configuration.port) + request.path;

if (this.mustUrlUseProxy(requestUrl)) {
const currentRequestId = requestId++;

const proxy = this.getUrlProxy(requestUrl);

if (this.protocol === 'http:') {
request.path = requestUrl;

if (proxy.authorization) {
request.setHeader('Proxy-Authorization', 'Basic ' + Buffer.from(proxy.authorization).toString('base64'));
}
}

this.eventEmitter.emit('request', request);

if (!this.isProxyConfigured()) {
log.trace({
destination: requestUrl,
proxy: 'http://' + proxy.hostname + ':' + proxy.port,
requestId: currentRequestId
}, 'proxying request');

request.once('response', (response) => {
log.trace({
headers: response.headers,
requestId: currentRequestId,
statusCode: response.statusCode
}, 'proxying response');
});

request.shouldKeepAlive = false;

const connectionConfiguration = {
host: configuration.hostname || configuration.host,
port: configuration.port || 80,
proxy
};
destination: requestUrl
}, 'not proxying request; GLOBAL_AGENT.HTTP_PROXY is not configured');

// $FlowFixMe It appears that Flow is missing the method description.
this.createConnection(connectionConfiguration, (error, socket) => {
if (error) {
request.emit('error', error);
} else {
request.onSocket(socket);
}
});
} else {
this.fallbackAgent.addRequest(request, configuration);

return;
}

if (!this.mustUrlUseProxy(requestUrl)) {
log.trace({
destination: requestUrl
}, 'not proxying request; request URL matches NO_PROXY');
}, 'not proxying request; url matches GLOBAL_AGENT.NO_PROXY');

// $FlowFixMe It appears that Flow is missing the method description.
this.fallbackAgent.addRequest(request, configuration);

return;
}

const currentRequestId = requestId++;

const proxy = this.getUrlProxy(requestUrl);

if (this.protocol === 'http:') {
request.path = requestUrl;

if (proxy.authorization) {
request.setHeader('Proxy-Authorization', 'Basic ' + Buffer.from(proxy.authorization).toString('base64'));
}
}

this.eventEmitter.emit('request', request);

log.trace({
destination: requestUrl,
proxy: 'http://' + proxy.hostname + ':' + proxy.port,
requestId: currentRequestId
}, 'proxying request');

request.once('response', (response) => {
log.trace({
headers: response.headers,
requestId: currentRequestId,
statusCode: response.statusCode
}, 'proxying response');
});

request.shouldKeepAlive = false;

const connectionConfiguration = {
host: configuration.hostname || configuration.host,
port: configuration.port || 80,
proxy
};

// $FlowFixMe It appears that Flow is missing the method description.
this.createConnection(connectionConfiguration, (error, socket) => {
if (error) {
request.emit('error', error);
} else {
request.onSocket(socket);
}
});
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/routines/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export default () => {
configuration: global.GLOBAL_AGENT
}, 'global agent has been initialized');

const isProxyConfigured = () => {
return global.GLOBAL_AGENT.HTTP_PROXY;
};

const mustUrlUseProxy = (url) => {
if (!global.GLOBAL_AGENT.HTTP_PROXY) {
return false;
Expand All @@ -66,13 +70,15 @@ export default () => {
const eventEmitter = new EventEmitter();

const httpAgent = new HttpProxyAgent(
isProxyConfigured,
mustUrlUseProxy,
getUrlProxy,
http.globalAgent,
eventEmitter
);

const httpsAgent = new HttpsProxyAgent(
isProxyConfigured,
mustUrlUseProxy,
getUrlProxy,
https.globalAgent,
Expand Down
1 change: 1 addition & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type ConnectionConfigurationType = {|
export type ConnectionCallbackType = (error: Error | null, socket: Socket | TLSSocket) => void;

export type AgentType = HttpAgent | HttpsAgent;
export type IsProxyConfiguredMethodType = () => boolean;
export type MustUrlUseProxyMethodType = (url: string) => boolean;
export type GetUrlProxyMethodType = (url: string) => ProxyConfigurationType;
export type ProtocolType = 'http:' | 'https:';

0 comments on commit 84e5265

Please sign in to comment.