proxy settings

This commit is contained in:
Mirko Golze
2023-10-15 16:40:50 +02:00
parent 97a300fbfc
commit 470e9d0442
16 changed files with 390 additions and 140 deletions

View File

@ -162,9 +162,7 @@ const getCollectionRoot = (dir) => {
}
const content = fs.readFileSync(collectionRootPath, 'utf8');
const json = collectionBruToJson(content);
return json;
return collectionBruToJson(content);
};
const builder = async (yargs) => {

View File

@ -16,6 +16,7 @@ const { HttpsProxyAgent } = require('https-proxy-agent');
const { HttpProxyAgent } = require('http-proxy-agent');
const { SocksProxyAgent } = require('socks-proxy-agent');
const { makeAxiosInstance } = require('../utils/axios-instance');
const { shouldUseProxy } = require('../utils/proxy-util');
const runSingleRequest = async function (
filename,
@ -86,22 +87,22 @@ const runSingleRequest = async function (
const httpsAgentRequestFields = {};
if (insecure) {
httpsAgentRequestFields['rejectUnauthorized'] = false;
} else {
const cacertArray = [options['cacert'], process.env.SSL_CERT_FILE, process.env.NODE_EXTRA_CA_CERTS];
const cacert = cacertArray.find((el) => el);
if (cacert && cacert.length > 1) {
try {
caCrt = fs.readFileSync(cacert);
httpsAgentRequestFields['ca'] = caCrt;
} catch (err) {
console.log('Error reading CA cert file:' + cacert, err);
}
}
const caCertArray = [options['cacert'], process.env.SSL_CERT_FILE, process.env.NODE_EXTRA_CA_CERTS];
const caCert = caCertArray.find((el) => el);
if (caCert && caCert.length > 1) {
try {
httpsAgentRequestFields['ca'] = fs.readFileSync(caCert);
} catch (err) {
console.log('Error reading CA cert file:' + caCert, err);
}
}
// set proxy if enabled
const proxyEnabled = get(brunoConfig, 'proxy.enabled', false);
if (proxyEnabled) {
const proxyByPass = shouldUseProxy(request.url, get(brunoConfig, 'proxy.noProxy', ''));
if (proxyEnabled && !proxyByPass) {
let proxyUri;
const interpolationOptions = {
envVars: envVariables,
@ -115,8 +116,6 @@ const runSingleRequest = async function (
const proxyAuthEnabled = get(brunoConfig, 'proxy.auth.enabled', false);
const socksEnabled = proxyProtocol.includes('socks');
interpolateString;
if (proxyAuthEnabled) {
const proxyAuthUsername = interpolateString(get(brunoConfig, 'proxy.auth.username'), interpolationOptions);
const proxyAuthPassword = interpolateString(get(brunoConfig, 'proxy.auth.password'), interpolationOptions);
@ -128,16 +127,13 @@ const runSingleRequest = async function (
if (socksEnabled) {
const socksProxyAgent = new SocksProxyAgent(proxyUri);
request.httpsAgent = socksProxyAgent;
request.httpAgent = socksProxyAgent;
} else {
request.httpsAgent = new HttpsProxyAgent(
proxyUri,
Object.keys(httpsAgentRequestFields).length > 0 ? { ...httpsAgentRequestFields } : undefined
);
request.httpAgent = new HttpProxyAgent(proxyUri);
}
} else if (Object.keys(httpsAgentRequestFields).length > 0) {

View File

@ -4,10 +4,10 @@ const axios = require('axios');
* Function that configures axios with timing interceptors
* Important to note here that the timings are not completely accurate.
* @see https://github.com/axios/axios/issues/695
* @returns {import('axios').AxiosStatic}
* @returns {axios.AxiosInstance}
*/
function makeAxiosInstance() {
/** @type {import('axios').AxiosStatic} */
/** @type {axios.AxiosInstance} */
const instance = axios.create();
instance.interceptors.request.use((config) => {
@ -26,9 +26,7 @@ function makeAxiosInstance() {
if (error.response) {
const end = Date.now();
const start = error.config.headers['request-start-time'];
if (error.response) {
error.response.headers['request-duration'] = end - start;
}
error.response.headers['request-duration'] = end - start;
}
return Promise.reject(error);
}

View File

@ -0,0 +1,65 @@
const parseUrl = require('url').parse;
const DEFAULT_PORTS = {
ftp: 21,
gopher: 70,
http: 80,
https: 443,
ws: 80,
wss: 443
};
/**
* check for proxy bypass, Copied form 'proxy-from-env'
*/
const shouldUseProxy = (url, proxyByPass) => {
if (proxyByPass === '*') {
return false; // Never proxy if wildcard is set.
}
if (!proxyByPass) {
return true; // use proxy if enabled
}
const parsedUrl = typeof url === 'string' ? parseUrl(url) : url || {};
let proto = parsedUrl.protocol;
let hostname = parsedUrl.host;
let port = parsedUrl.port;
if (typeof hostname !== 'string' || !hostname || typeof proto !== 'string') {
return false; // Don't proxy URLs without a valid scheme or host.
}
proto = proto.split(':', 1)[0];
// Stripping ports in this way instead of using parsedUrl.hostname to make
// sure that the brackets around IPv6 addresses are kept.
hostname = hostname.replace(/:\d*$/, '');
port = parseInt(port) || DEFAULT_PORTS[proto] || 0;
return proxyByPass.split(/[,;\s]/).every(function (dontProxyFor) {
if (!dontProxyFor) {
return true; // Skip zero-length hosts.
}
const parsedProxy = dontProxyFor.match(/^(.+):(\d+)$/);
let parsedProxyHostname = parsedProxy ? parsedProxy[1] : dontProxyFor;
const parsedProxyPort = parsedProxy ? parseInt(parsedProxy[2]) : 0;
if (parsedProxyPort && parsedProxyPort !== port) {
return true; // Skip if ports don't match.
}
if (!/^[.*]/.test(parsedProxyHostname)) {
// No wildcards, so stop proxying if there is an exact match.
return hostname !== parsedProxyHostname;
}
if (parsedProxyHostname.charAt(0) === '*') {
// Remove leading wildcard.
parsedProxyHostname = parsedProxyHostname.slice(1);
}
// Stop proxying if the hostname ends with the no_proxy host.
return !hostname.endsWith(parsedProxyHostname);
});
};
module.exports = {
shouldUseProxy
};