feat: cli -- system level proxy fix

This commit is contained in:
lohxt1 2024-12-12 14:09:06 +05:30
parent 57d86eb118
commit a55ed9bd50
3 changed files with 90 additions and 32 deletions

View File

@ -17,7 +17,7 @@ const { HttpProxyAgent } = require('http-proxy-agent');
const { SocksProxyAgent } = require('socks-proxy-agent'); const { SocksProxyAgent } = require('socks-proxy-agent');
const { makeAxiosInstance } = require('../utils/axios-instance'); const { makeAxiosInstance } = require('../utils/axios-instance');
const { addAwsV4Interceptor, resolveAwsV4Credentials } = require('./awsv4auth-helper'); const { addAwsV4Interceptor, resolveAwsV4Credentials } = require('./awsv4auth-helper');
const { shouldUseProxy, PatchedHttpsProxyAgent } = require('../utils/proxy-util'); const { shouldUseProxy, PatchedHttpsProxyAgent, getSystemProxyEnvVariables } = require('../utils/proxy-util');
const path = require('path'); const path = require('path');
const { createFormData } = require('../utils/common'); const { createFormData } = require('../utils/common');
const { getCookieStringForUrl, saveCookies, shouldUseCookies } = require('../utils/cookies'); const { getCookieStringForUrl, saveCookies, shouldUseCookies } = require('../utils/cookies');
@ -140,39 +140,85 @@ const runSingleRequest = async function (
} }
} }
// set proxy if enabled let proxyMode = 'off';
const proxyEnabled = get(brunoConfig, 'proxy.enabled', false); let proxyConfig = {};
const shouldProxy = shouldUseProxy(request.url, get(brunoConfig, 'proxy.bypassProxy', ''));
if (proxyEnabled && shouldProxy) {
const proxyProtocol = interpolateString(get(brunoConfig, 'proxy.protocol'), interpolationOptions);
const proxyHostname = interpolateString(get(brunoConfig, 'proxy.hostname'), interpolationOptions);
const proxyPort = interpolateString(get(brunoConfig, 'proxy.port'), interpolationOptions);
const proxyAuthEnabled = get(brunoConfig, 'proxy.auth.enabled', false);
const socksEnabled = proxyProtocol.includes('socks');
let uriPort = isUndefined(proxyPort) || isNull(proxyPort) ? '' : `:${proxyPort}`; const collectionProxyConfig = get(brunoConfig, 'proxy', {});
let proxyUri; const collectionProxyEnabled = get(collectionProxyConfig, 'enabled', false);
if (proxyAuthEnabled) { if (collectionProxyEnabled === true) {
const proxyAuthUsername = interpolateString(get(brunoConfig, 'proxy.auth.username'), interpolationOptions); proxyConfig = collectionProxyConfig;
const proxyAuthPassword = interpolateString(get(brunoConfig, 'proxy.auth.password'), interpolationOptions); proxyMode = 'on';
} else {
proxyUri = `${proxyProtocol}://${proxyAuthUsername}:${proxyAuthPassword}@${proxyHostname}${uriPort}`; // if the collection level proxy is not set, pick the system level proxy by default, to maintain backward compatibility
} else { const { http_proxy, https_proxy } = getSystemProxyEnvVariables();
proxyUri = `${proxyProtocol}://${proxyHostname}${uriPort}`; if (http_proxy?.length || https_proxy?.length) {
proxyMode = 'system';
} }
}
if (socksEnabled) { if (proxyMode === 'on') {
request.httpsAgent = new SocksProxyAgent( const shouldProxy = shouldUseProxy(request.url, get(proxyConfig, 'bypassProxy', ''));
proxyUri, if (shouldProxy) {
Object.keys(httpsAgentRequestFields).length > 0 ? { ...httpsAgentRequestFields } : undefined const proxyProtocol = interpolateString(get(proxyConfig, 'protocol'), interpolationOptions);
); const proxyHostname = interpolateString(get(proxyConfig, 'hostname'), interpolationOptions);
request.httpAgent = new SocksProxyAgent(proxyUri); 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) {
request.httpsAgent = new SocksProxyAgent(
proxyUri,
Object.keys(httpsAgentRequestFields).length > 0 ? { ...httpsAgentRequestFields } : undefined
);
request.httpAgent = new SocksProxyAgent(proxyUri);
} else {
request.httpsAgent = new PatchedHttpsProxyAgent(
proxyUri,
Object.keys(httpsAgentRequestFields).length > 0 ? { ...httpsAgentRequestFields } : undefined
);
request.httpAgent = new HttpProxyAgent(proxyUri);
}
} else { } else {
request.httpsAgent = new PatchedHttpsProxyAgent( request.httpsAgent = new https.Agent({
proxyUri, ...httpsAgentRequestFields
Object.keys(httpsAgentRequestFields).length > 0 ? { ...httpsAgentRequestFields } : undefined });
); }
request.httpAgent = new HttpProxyAgent(proxyUri); } else if (proxyMode === 'system') {
const { http_proxy, https_proxy, no_proxy } = getSystemProxyEnvVariables();
const shouldUseSystemProxy = shouldUseProxy(request.url, no_proxy || '');
if (shouldUseSystemProxy) {
try {
if (http_proxy?.length) {
new URL(http_proxy);
request.httpAgent = new HttpProxyAgent(http_proxy);
}
} catch (error) {
throw new Error('Invalid system http_proxy');
}
try {
if (https_proxy?.length) {
new URL(https_proxy);
request.httpsAgent = new PatchedHttpsProxyAgent(
https_proxy,
Object.keys(httpsAgentRequestFields).length > 0 ? { ...httpsAgentRequestFields } : undefined
);
}
} catch (error) {
throw new Error('Invalid system https_proxy');
}
} else {
request.httpsAgent = new https.Agent({
...httpsAgentRequestFields
});
} }
} else if (Object.keys(httpsAgentRequestFields).length > 0) { } else if (Object.keys(httpsAgentRequestFields).length > 0) {
request.httpsAgent = new https.Agent({ request.httpsAgent = new https.Agent({

View File

@ -10,6 +10,7 @@ const { CLI_VERSION } = require('../constants');
function makeAxiosInstance() { function makeAxiosInstance() {
/** @type {axios.AxiosInstance} */ /** @type {axios.AxiosInstance} */
const instance = axios.create({ const instance = axios.create({
proxy: false,
headers: { headers: {
"User-Agent": `bruno-runtime/${CLI_VERSION}` "User-Agent": `bruno-runtime/${CLI_VERSION}`
} }

View File

@ -79,7 +79,18 @@ class PatchedHttpsProxyAgent extends HttpsProxyAgent {
} }
} }
const getSystemProxyEnvVariables = () => {
const { http_proxy, HTTP_PROXY, https_proxy, HTTPS_PROXY, no_proxy, NO_PROXY } = process.env;
return {
http_proxy: http_proxy || HTTP_PROXY,
https_proxy: https_proxy || HTTPS_PROXY,
no_proxy: no_proxy || NO_PROXY
};
}
module.exports = { module.exports = {
shouldUseProxy, shouldUseProxy,
PatchedHttpsProxyAgent PatchedHttpsProxyAgent,
getSystemProxyEnvVariables
}; };