Skip to content

Commit

Permalink
moved setupProxyAgents to until function
Browse files Browse the repository at this point in the history
  • Loading branch information
naman-bruno committed Jan 22, 2025
1 parent 7f2b06b commit b23f1ae
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 91 deletions.
16 changes: 2 additions & 14 deletions packages/bruno-electron/src/ipc/network/axios-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const electronApp = require("electron");
const { get } = require('lodash');
const { preferencesUtil } = require('../../store/preferences');
const { getCookieStringForUrl, addCookieToJar } = require('../../utils/cookies');
const { setupProxyAgents } = require('../../utils/proxy-util');

const LOCAL_IPV6 = '::1';
const LOCAL_IPV4 = '127.0.0.1';
Expand Down Expand Up @@ -68,7 +69,7 @@ const checkConnection = (host, port) =>
* @see https://github.com/axios/axios/issues/695
* @returns {axios.AxiosInstance}
*/
function makeAxiosInstance({ brunoConfig, MAX_REDIRECTS, httpsAgentRequestFields, interpolationOptions, setupProxyAgents }) {
function makeAxiosInstance({ proxyMode, proxyConfig, MAX_REDIRECTS, httpsAgentRequestFields, interpolationOptions }) {
/** @type {axios.AxiosInstance} */
const instance = axios.create({
transformRequest: function transformRequest(data, headers) {
Expand Down Expand Up @@ -144,19 +145,6 @@ function makeAxiosInstance({ brunoConfig, MAX_REDIRECTS, httpsAgentRequestFields
};
}

let proxyMode = 'off';
let proxyConfig = {};

const collectionProxyConfig = get(brunoConfig, 'proxy', {});
const collectionProxyEnabled = get(collectionProxyConfig, 'enabled', 'global');
if (collectionProxyEnabled === true) {
proxyConfig = collectionProxyConfig;
proxyMode = 'on';
} else if (collectionProxyEnabled === 'global') {
proxyConfig = preferencesUtil.getGlobalProxyConfig();
proxyMode = get(proxyConfig, 'mode', 'off');
}

// Increase redirect count
redirectCount++;

Expand Down
79 changes: 4 additions & 75 deletions packages/bruno-electron/src/ipc/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const { SocksProxyAgent } = require('socks-proxy-agent');
const { makeAxiosInstance } = require('./axios-instance');
const { addAwsV4Interceptor, resolveAwsV4Credentials } = require('./awsv4auth-helper');
const { addDigestInterceptor } = require('./digestauth-helper');
const { shouldUseProxy, PatchedHttpsProxyAgent } = require('../../utils/proxy-util');
const { shouldUseProxy, PatchedHttpsProxyAgent, setupProxyAgents } = require('../../utils/proxy-util');
const { chooseFileToSave, writeBinaryFile, writeFile } = require('../../utils/filesystem');
const { getCookieStringForUrl, addCookieToJar, getDomainsWithCookies } = require('../../utils/cookies');
const {
Expand Down Expand Up @@ -102,77 +102,6 @@ const saveCookies = (url, headers) => {
}
}

function setupProxyAgents({
requestConfig,
proxyMode,
proxyConfig,
httpsAgentRequestFields,
interpolationOptions
}) {
if (proxyMode === 'on') {
const shouldProxy = shouldUseProxy(requestConfig.url, get(proxyConfig, 'bypassProxy', ''));
if (shouldProxy) {
const proxyProtocol = interpolateString(get(proxyConfig, 'protocol'), interpolationOptions);
const proxyHostname = interpolateString(get(proxyConfig, 'hostname'), interpolationOptions);
const proxyPort = interpolateString(get(proxyConfig, 'port'), interpolationOptions);
const proxyAuthEnabled = get(proxyConfig, 'auth.enabled', false);
const socksEnabled = proxyProtocol.includes('socks');

let uriPort = isUndefined(proxyPort) || isNull(proxyPort) ? '' : `:${proxyPort}`;
let proxyUri;
if (proxyAuthEnabled) {
const proxyAuthUsername = interpolateString(get(proxyConfig, 'auth.username'), interpolationOptions);
const proxyAuthPassword = interpolateString(get(proxyConfig, 'auth.password'), interpolationOptions);
proxyUri = `${proxyProtocol}://${proxyAuthUsername}:${proxyAuthPassword}@${proxyHostname}${uriPort}`;
} else {
proxyUri = `${proxyProtocol}://${proxyHostname}${uriPort}`;
}

if (socksEnabled) {
requestConfig.httpAgent = new SocksProxyAgent(proxyUri);
requestConfig.httpsAgent = new SocksProxyAgent(proxyUri, httpsAgentRequestFields);
} else {
requestConfig.httpAgent = new HttpProxyAgent(proxyUri);
requestConfig.httpsAgent = new PatchedHttpsProxyAgent(
proxyUri,
Object.keys(httpsAgentRequestFields).length > 0 ? { ...httpsAgentRequestFields } : undefined
);
}
} else {
// If proxy should not be used, set default HTTPS agent
requestConfig.httpsAgent = new https.Agent(httpsAgentRequestFields);
}
} else if (proxyMode === 'system') {
const { http_proxy, https_proxy, no_proxy } = preferencesUtil.getSystemProxyEnvVariables();
const shouldUseSystemProxy = shouldUseProxy(url, no_proxy || '');
if (shouldUseSystemProxy) {
try {
if (http_proxy?.length) {
new URL(http_proxy);
requestConfig.httpAgent = new HttpProxyAgent(http_proxy);
}
} catch (error) {
throw new Error('Invalid system http_proxy');
}
try {
if (https_proxy?.length) {
new URL(https_proxy);
requestConfig.httpsAgent = new PatchedHttpsProxyAgent(
https_proxy,
Object.keys(httpsAgentRequestFields).length > 0 ? { ...httpsAgentRequestFields } : undefined
);
}
} catch (error) {
throw new Error('Invalid system https_proxy');
}
} else {
requestConfig.httpsAgent = new https.Agent(httpsAgentRequestFields);
}
} else if (Object.keys(httpsAgentRequestFields).length > 0) {
requestConfig.httpsAgent = new https.Agent(httpsAgentRequestFields);
}
}

const configureRequest = async (
collectionUid,
request,
Expand Down Expand Up @@ -287,11 +216,11 @@ const configureRequest = async (
request.maxRedirects = 0

let axiosInstance = makeAxiosInstance({
brunoConfig,
proxyMode,
proxyConfig,
MAX_REDIRECTS,
httpsAgentRequestFields,
interpolationOptions,
setupProxyAgents
interpolationOptions
});

if (request.ntlmConfig) {
Expand Down
81 changes: 79 additions & 2 deletions packages/bruno-electron/src/utils/proxy-util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const parseUrl = require('url').parse;
const { isEmpty } = require('lodash');
const https = require('https');
const { isEmpty, get, isUndefined, isNull } = require('lodash');
const { HttpsProxyAgent } = require('https-proxy-agent');
const { interpolateString } = require('../ipc/network/interpolate-string');
const { SocksProxyAgent } = require('socks-proxy-agent');
const { HttpProxyAgent } = require('http-proxy-agent');
const { preferencesUtil } = require('../store/preferences');

const DEFAULT_PORTS = {
ftp: 21,
Expand Down Expand Up @@ -79,7 +84,79 @@ class PatchedHttpsProxyAgent extends HttpsProxyAgent {
}
}

function setupProxyAgents({
requestConfig,
proxyMode,
proxyConfig,
httpsAgentRequestFields,
interpolationOptions
}) {
if (proxyMode === 'on') {
const shouldProxy = shouldUseProxy(requestConfig.url, get(proxyConfig, 'bypassProxy', ''));
if (shouldProxy) {
const proxyProtocol = interpolateString(get(proxyConfig, 'protocol'), interpolationOptions);
const proxyHostname = interpolateString(get(proxyConfig, 'hostname'), interpolationOptions);
const proxyPort = interpolateString(get(proxyConfig, 'port'), interpolationOptions);
const proxyAuthEnabled = get(proxyConfig, 'auth.enabled', false);
const socksEnabled = proxyProtocol.includes('socks');

let uriPort = isUndefined(proxyPort) || isNull(proxyPort) ? '' : `:${proxyPort}`;
let proxyUri;
if (proxyAuthEnabled) {
const proxyAuthUsername = interpolateString(get(proxyConfig, 'auth.username'), interpolationOptions);
const proxyAuthPassword = interpolateString(get(proxyConfig, 'auth.password'), interpolationOptions);
proxyUri = `${proxyProtocol}://${proxyAuthUsername}:${proxyAuthPassword}@${proxyHostname}${uriPort}`;
} else {
proxyUri = `${proxyProtocol}://${proxyHostname}${uriPort}`;
}

if (socksEnabled) {
requestConfig.httpAgent = new SocksProxyAgent(proxyUri);
requestConfig.httpsAgent = new SocksProxyAgent(proxyUri, httpsAgentRequestFields);
} else {
requestConfig.httpAgent = new HttpProxyAgent(proxyUri);
requestConfig.httpsAgent = new PatchedHttpsProxyAgent(
proxyUri,
Object.keys(httpsAgentRequestFields).length > 0 ? { ...httpsAgentRequestFields } : undefined
);
}
} else {
// If proxy should not be used, set default HTTPS agent
requestConfig.httpsAgent = new https.Agent(httpsAgentRequestFields);
}
} else if (proxyMode === 'system') {
const { http_proxy, https_proxy, no_proxy } = preferencesUtil.getSystemProxyEnvVariables();
const shouldUseSystemProxy = shouldUseProxy(url, no_proxy || '');
if (shouldUseSystemProxy) {
try {
if (http_proxy?.length) {
new URL(http_proxy);
requestConfig.httpAgent = new HttpProxyAgent(http_proxy);
}
} catch (error) {
throw new Error('Invalid system http_proxy');
}
try {
if (https_proxy?.length) {
new URL(https_proxy);
requestConfig.httpsAgent = new PatchedHttpsProxyAgent(
https_proxy,
Object.keys(httpsAgentRequestFields).length > 0 ? { ...httpsAgentRequestFields } : undefined
);
}
} catch (error) {
throw new Error('Invalid system https_proxy');
}
} else {
requestConfig.httpsAgent = new https.Agent(httpsAgentRequestFields);
}
} else if (Object.keys(httpsAgentRequestFields).length > 0) {
requestConfig.httpsAgent = new https.Agent(httpsAgentRequestFields);
}
}

module.exports = {
shouldUseProxy,
PatchedHttpsProxyAgent
PatchedHttpsProxyAgent,
setupProxyAgents
};

0 comments on commit b23f1ae

Please sign in to comment.