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

28
package-lock.json generated
View File

@ -2418,11 +2418,6 @@
"dev": true,
"license": "MIT"
},
"node_modules/@coolaj86/urequest": {
"version": "1.3.7",
"resolved": "https://registry.npmjs.org/@coolaj86/urequest/-/urequest-1.3.7.tgz",
"integrity": "sha512-PPrVYra9aWvZjSCKl/x1pJ9ZpXda1652oJrPBYy5rQumJJMkmTBN3ux+sK2xAUwVvv2wnewDlaQaHLxLwSHnIA=="
},
"node_modules/@develar/schema-utils": {
"version": "2.6.5",
"dev": true,
@ -14796,14 +14791,6 @@
"node": ">=0.10.0"
}
},
"node_modules/ssl-root-cas": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/ssl-root-cas/-/ssl-root-cas-1.3.1.tgz",
"integrity": "sha512-KR8J210Wfvjh+iNE9jcQEgbG0VG2713PHreItx6aNCPnkFO8XChz1cJ4iuCGeBj0+8wukLmgHgJqX+O5kRjPkQ==",
"dependencies": {
"@coolaj86/urequest": "^1.3.6"
}
},
"node_modules/stable": {
"version": "0.1.8",
"dev": true,
@ -16761,7 +16748,6 @@
"node-machine-id": "^1.1.12",
"qs": "^6.11.0",
"socks-proxy-agent": "^8.0.2",
"ssl-root-cas": "^1.3.1",
"uuid": "^9.0.0",
"vm2": "^3.9.13",
"yup": "^0.32.11"
@ -18765,11 +18751,6 @@
"version": "0.2.3",
"dev": true
},
"@coolaj86/urequest": {
"version": "1.3.7",
"resolved": "https://registry.npmjs.org/@coolaj86/urequest/-/urequest-1.3.7.tgz",
"integrity": "sha512-PPrVYra9aWvZjSCKl/x1pJ9ZpXda1652oJrPBYy5rQumJJMkmTBN3ux+sK2xAUwVvv2wnewDlaQaHLxLwSHnIA=="
},
"@develar/schema-utils": {
"version": "2.6.5",
"dev": true,
@ -21658,7 +21639,6 @@
"node-machine-id": "^1.1.12",
"qs": "^6.11.0",
"socks-proxy-agent": "^8.0.2",
"ssl-root-cas": "^1.3.1",
"uuid": "^9.0.0",
"vm2": "^3.9.13",
"yup": "^0.32.11"
@ -27279,14 +27259,6 @@
"tweetnacl": "~0.14.0"
}
},
"ssl-root-cas": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/ssl-root-cas/-/ssl-root-cas-1.3.1.tgz",
"integrity": "sha512-KR8J210Wfvjh+iNE9jcQEgbG0VG2713PHreItx6aNCPnkFO8XChz1cJ4iuCGeBj0+8wukLmgHgJqX+O5kRjPkQ==",
"requires": {
"@coolaj86/urequest": "^1.3.6"
}
},
"stable": {
"version": "0.1.8",
"dev": true

View File

@ -1,7 +1,7 @@
import 'github-markdown-css/github-markdown.css';
import get from 'lodash/get';
import { updateCollectionDocs } from 'providers/ReduxStore/slices/collections';
import { useTheme } from 'providers/Theme/index';
import { useTheme } from 'providers/Theme';
import { useState } from 'react';
import { useDispatch } from 'react-redux';
import { saveCollectionRoot } from 'providers/ReduxStore/slices/collections/actions';

View File

@ -1,10 +1,52 @@
import React, { useEffect } from 'react';
import { useFormik } from 'formik';
import * as Yup from 'yup';
import StyledWrapper from './StyledWrapper';
import * as Yup from 'yup';
import toast from 'react-hot-toast';
const ProxySettings = ({ proxyConfig, onUpdate }) => {
const proxySchema = Yup.object({
enabled: Yup.string().oneOf(['global', 'enabled', 'disabled']),
protocol: Yup.string().oneOf(['http', 'https', 'socks4', 'socks5']),
hostname: Yup.string()
.when('enabled', {
is: 'enabled',
then: (hostname) => hostname.required('Specify the hostname for your proxy.'),
otherwise: (hostname) => hostname.nullable()
})
.max(1024),
port: Yup.number()
.when('enabled', {
is: 'enabled',
then: (port) => port.typeError('Specify port between 1 and 65535'),
otherwise: (port) => port.nullable().transform((_, val) => (val ? Number(val) : null))
})
.min(1)
.max(65535),
auth: Yup.object()
.when('enabled', {
is: 'enabled',
then: Yup.object({
enabled: Yup.boolean(),
username: Yup.string()
.when(['enabled'], {
is: true,
then: (username) => username.required('Specify username for proxy authentication.')
})
.max(1024),
password: Yup.string()
.when('enabled', {
is: true,
then: (password) => password.required('Specify password for proxy authentication.')
})
.max(1024)
})
})
.optional(),
noProxy: Yup.string().optional().max(1024)
});
const formik = useFormik({
initialValues: {
enabled: proxyConfig.enabled || 'global',
@ -18,20 +60,17 @@ const ProxySettings = ({ proxyConfig, onUpdate }) => {
},
noProxy: proxyConfig.noProxy || ''
},
validationSchema: Yup.object({
enabled: Yup.string().oneOf(['global', 'enabled', 'disabled']),
protocol: Yup.string().oneOf(['http', 'https', 'socks5']),
hostname: Yup.string().max(1024),
port: Yup.number().min(0).max(65535),
auth: Yup.object({
enabled: Yup.boolean(),
username: Yup.string().max(1024),
password: Yup.string().max(1024)
}),
noProxy: Yup.string().max(1024)
}),
validationSchema: proxySchema,
onSubmit: (values) => {
onUpdate(values);
proxySchema
.validate(values, { abortEarly: true })
.then((validatedProxy) => {
onUpdate(validatedProxy);
})
.catch((error) => {
let errMsg = error.message || 'Preferences validation error';
toast.error(errMsg);
});
}
});
@ -55,15 +94,15 @@ const ProxySettings = ({ proxyConfig, onUpdate }) => {
<h1 className="font-medium mb-3">Proxy Settings</h1>
<label className="settings-label">
<ul className="mb-3">
<li>To use the global proxy configuration, choose 'use global setting'</li>
<li>To use collection level configuration, choose 'enabled'</li>
<li>To disable the proxy for this collection, choose 'disabled'</li>
<li>global - use global config</li>
<li>enabled - use collection config</li>
<li>disable - disable proxy</li>
</ul>
</label>
<form className="bruno-form" onSubmit={formik.handleSubmit}>
<div className="mb-3 flex items-center">
<label className="settings-label" htmlFor="enabled">
Usage
Config
</label>
<div className="flex items-center">
<label className="flex items-center">
@ -75,7 +114,7 @@ const ProxySettings = ({ proxyConfig, onUpdate }) => {
onChange={formik.handleChange}
className="mr-1"
/>
use global settings
global
</label>
<label className="flex items-center ml-4">
<input
@ -128,6 +167,17 @@ const ProxySettings = ({ proxyConfig, onUpdate }) => {
/>
https
</label>
<label className="flex items-center ml-4">
<input
type="radio"
name="protocol"
value="socks5"
checked={formik.values.protocol === 'socks4'}
onChange={formik.handleChange}
className="mr-1"
/>
socks4
</label>
<label className="flex items-center ml-4">
<input
type="radio"

View File

@ -24,7 +24,7 @@ const ProxySettings = () => {
},
validationSchema: Yup.object({
enabled: Yup.boolean(),
protocol: Yup.string().oneOf(['http', 'https', 'socks5']),
protocol: Yup.string().oneOf(['http', 'https', 'socks4', 'socks5']),
hostname: Yup.string().max(1024),
port: Yup.number().min(0).max(65535),
auth: Yup.object({
@ -106,6 +106,17 @@ const ProxySettings = () => {
/>
https
</label>
<label className="flex items-center ml-4">
<input
type="radio"
name="protocol"
value="socks5"
checked={formik.values.protocol === 'socks4'}
onChange={formik.handleChange}
className="mr-1"
/>
socks4
</label>
<label className="flex items-center ml-4">
<input
type="radio"

View File

@ -12,23 +12,54 @@ import * as Yup from 'yup';
import useLocalStorage from 'hooks/useLocalStorage/index';
import toast from 'react-hot-toast';
const requestSchema = Yup.object({
sslVerification: Yup.boolean(),
caCert: Yup.string().max(1024)
});
const proxySchema = Yup.object({
enabled: Yup.boolean(),
protocol: Yup.string().oneOf(['http', 'https', 'socks5']),
hostname: Yup.string()
.when('enabled', {
is: true,
then: (hostname) => hostname.required('Specify the hostname for your proxy.'),
otherwise: (hostname) => hostname.nullable()
})
.max(1024),
port: Yup.number()
.when('enabled', {
is: true,
then: (port) => port.typeError('Specify port between 1 and 65535'),
otherwise: (port) => port.nullable().transform((_, val) => (val ? Number(val) : null))
})
.min(1)
.max(65535),
auth: Yup.object()
.when('enabled', {
is: true,
then: Yup.object({
enabled: Yup.boolean(),
username: Yup.string()
.when(['enabled'], {
is: true,
then: (username) => username.required('Specify username for proxy authentication.')
})
.max(1024),
password: Yup.string()
.when('enabled', {
is: true,
then: (password) => password.required('Specify password for proxy authentication.')
})
.max(1024)
})
})
.optional(),
noProxy: Yup.string().optional().max(1024)
});
const preferencesSchema = Yup.object({
request: Yup.object({
sslVerification: Yup.boolean(),
caCert: Yup.string().max(1024)
}),
proxy: Yup.object({
enabled: Yup.boolean(),
protocol: Yup.string().oneOf(['http', 'https', 'socks5']),
hostname: Yup.string().max(1024),
port: Yup.number().min(0).max(65535),
auth: Yup.object({
enabled: Yup.boolean(),
username: Yup.string().max(1024),
password: Yup.string().max(1024)
}),
noProxy: Yup.string().max(1024)
})
request: requestSchema,
proxy: proxySchema
});
export const PreferencesContext = createContext();

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
};

View File

@ -48,7 +48,6 @@
"node-machine-id": "^1.1.12",
"qs": "^6.11.0",
"socks-proxy-agent": "^8.0.2",
"ssl-root-cas": "^1.3.1",
"uuid": "^9.0.0",
"vm2": "^3.9.13",
"yup": "^0.32.11"

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) => {

View File

@ -2,6 +2,7 @@ const os = require('os');
const qs = require('qs');
const https = require('https');
const axios = require('axios');
const fs = require('fs');
const decomment = require('decomment');
const Mustache = require('mustache');
const FormData = require('form-data');
@ -23,6 +24,7 @@ const { HttpProxyAgent } = require('http-proxy-agent');
const { SocksProxyAgent } = require('socks-proxy-agent');
const { makeAxiosInstance } = require('./axios-instance');
const { addAwsV4Interceptor, resolveCredentials } = require('./awsv4auth-helper');
const { shouldUseProxy } = require('../../utils/proxy-util');
// override the default escape function to prevent escaping
Mustache.escape = function (value) {
@ -82,6 +84,24 @@ const getSize = (data) => {
return 0;
};
function getHttpsAgentRequestFields() {
const httpsAgentRequestFields = {};
if (!preferences.isTlsVerification()) {
httpsAgentRequestFields['rejectUnauthorized'] = false;
}
const cacCrtArray = [preferences.getCaCert(), process.env.SSL_CERT_FILE, process.env.NODE_EXTRA_CA_CERTS];
let caCertFile = cacCrtArray.find((el) => el);
if (caCertFile && caCertFile.length > 1) {
try {
httpsAgentRequestFields['ca'] = fs.readFileSync(caCertFile);
} catch (err) {
console.log('Error reading CA cert file:' + caCertFile, err);
}
}
return httpsAgentRequestFields;
}
const registerNetworkIpc = (mainWindow) => {
// handler for sending http request
ipcMain.handle('send-http-request', async (event, item, collection, environment, collectionVariables) => {
@ -204,43 +224,34 @@ const registerNetworkIpc = (mainWindow) => {
cancelTokenUid
});
const httpsAgentRequestFields = {};
if (!preferences.isTlsVerification()) {
httpsAgentRequestFields['rejectUnauthorized'] = false;
}
const cacertArray = [preferences.getCaCert(), process.env.SSL_CERT_FILE, process.env.NODE_EXTRA_CA_CERTS];
let cacertFile = cacertArray.find((el) => el);
if (cacertFile && cacertFile.length > 1) {
try {
const sslRootCas = require('ssl-root-cas').inject();
sslRootCas.addFile(cacertFile);
} catch (err) {
console.log('Error reading CA cert file:' + cacertFile, err);
}
}
const httpsAgentRequestFields = getHttpsAgentRequestFields();
// proxy configuration
const brunoConfig = getBrunoConfig(collectionUid);
const proxyEnabled = get(brunoConfig, 'proxy.enabled', 'disabled');
if (proxyEnabled === 'enabled') {
let proxyConfig = get(brunoConfig, 'proxy', {});
let proxyEnabled = get(proxyConfig, 'enabled', 'disabled');
if (proxyEnabled === 'global') {
proxyConfig = preferences.getProxyConfig();
proxyEnabled = get(proxyConfig, 'enabled', false);
}
const proxyByPass = shouldUseProxy(request.url, get(proxyConfig, 'noProxy', ''));
if ((proxyEnabled === true || proxyEnabled === 'enabled') && !proxyByPass) {
let proxyUri;
const interpolationOptions = {
envVars,
collectionVariables,
processEnvVars
};
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 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');
if (proxyAuthEnabled) {
const proxyAuthUsername = interpolateString(get(brunoConfig, 'proxy.auth.username'), interpolationOptions);
const proxyAuthPassword = interpolateString(get(brunoConfig, 'proxy.auth.password'), interpolationOptions);
const proxyAuthUsername = interpolateString(get(proxyConfig, 'auth.username'), interpolationOptions);
const proxyAuthPassword = interpolateString(get(proxyConfig, 'auth.password'), interpolationOptions);
proxyUri = `${proxyProtocol}://${proxyAuthUsername}:${proxyAuthPassword}@${proxyHostname}:${proxyPort}`;
} else {
@ -673,10 +684,18 @@ const registerNetworkIpc = (mainWindow) => {
...eventData
});
const httpsAgentRequestFields = getHttpsAgentRequestFields();
// proxy configuration
const brunoConfig = getBrunoConfig(collectionUid);
const proxyEnabled = get(brunoConfig, 'proxy.enabled', false);
if (proxyEnabled) {
let proxyConfig = get(brunoConfig, 'proxy', {});
let proxyEnabled = get(proxyConfig, 'enabled', 'disabled');
if (proxyEnabled === 'global') {
proxyConfig = preferences.getProxyConfig();
proxyEnabled = get(proxyConfig, 'enabled', false);
}
const proxyByPass = shouldUseProxy(request.url, get(proxyConfig, 'noProxy', ''));
if ((proxyEnabled === true || proxyEnabled === 'enabled') && !proxyByPass) {
let proxyUri;
const interpolationOptions = {
envVars,
@ -684,22 +703,15 @@ const registerNetworkIpc = (mainWindow) => {
processEnvVars
};
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 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');
if (proxyAuthEnabled) {
const proxyAuthUsername = interpolateString(
get(brunoConfig, 'proxy.auth.username'),
interpolationOptions
);
const proxyAuthPassword = interpolateString(
get(brunoConfig, 'proxy.auth.password'),
interpolationOptions
);
const proxyAuthUsername = interpolateString(get(proxyConfig, 'auth.username'), interpolationOptions);
const proxyAuthPassword = interpolateString(get(proxyConfig, 'auth.password'), interpolationOptions);
proxyUri = `${proxyProtocol}://${proxyAuthUsername}:${proxyAuthPassword}@${proxyHostname}:${proxyPort}`;
} else {
@ -708,19 +720,18 @@ const registerNetworkIpc = (mainWindow) => {
if (socksEnabled) {
const socksProxyAgent = new SocksProxyAgent(proxyUri);
request.httpsAgent = socksProxyAgent;
request.httpAgent = socksProxyAgent;
} else {
request.httpsAgent = new HttpsProxyAgent(proxyUri, {
rejectUnauthorized: preferences.isTlsVerification()
});
request.httpsAgent = new HttpsProxyAgent(
proxyUri,
Object.keys(httpsAgentRequestFields).length > 0 ? { ...httpsAgentRequestFields } : undefined
);
request.httpAgent = new HttpProxyAgent(proxyUri);
}
} else if (!preferences.isTlsVerification()) {
} else if (Object.keys(httpsAgentRequestFields).length > 0) {
request.httpsAgent = new https.Agent({
rejectUnauthorized: false
...httpsAgentRequestFields
});
}

View File

@ -76,12 +76,17 @@ const preferences = {
},
isTlsVerification: () => {
return get(getPreferences(), ['request.tlsVerification'], true);
return get(getPreferences(), 'request.tlsVerification', true);
},
getCaCert: () => {
return get(getPreferences(), 'request.cacert');
},
getProxyConfig: () => {
return get(getPreferences(), 'proxy', {});
},
setPreferences: (validatedPreferences) => {
const updatedPreferences = {
...getPreferences(),

View File

@ -0,0 +1,64 @@
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
};

View File

@ -0,0 +1,50 @@
const { shouldUseProxy } = require('../../src/utils/proxy-util');
test('no proxy necessary - star', () => {
const url = 'http://wwww.example.org/test';
const noProxy = '*';
expect(shouldUseProxy(url, noProxy)).toEqual(false);
});
test('no proxy necessary - no noProxy bypass', () => {
const url = 'http://wwww.example.org/test';
const noProxy = '';
expect(shouldUseProxy(url, noProxy)).toEqual(true);
});
test('no proxy necessary - wildcard match', () => {
const url = 'http://wwww.example.org/test';
const noProxy = '*example.org';
expect(shouldUseProxy(url, noProxy)).toEqual(false);
});
test('no proxy necessary - direct proxy', () => {
const url = 'http://wwww.example.org/test';
const noProxy = 'wwww.example.org';
expect(shouldUseProxy(url, noProxy)).toEqual(false);
});
test('no proxy necessary - multiple proxy', () => {
const url = 'http://wwww.example.org/test';
const noProxy = 'www.example.com,wwww.example.org';
expect(shouldUseProxy(url, noProxy)).toEqual(false);
});
test('proxy necessary - no proxy match multiple', () => {
const url = 'https://wwww.example.test/test';
const noProxy = 'www.example.com,wwww.example.org';
expect(shouldUseProxy(url, noProxy)).toEqual(true);
});
test('proxy necessary - no proxy match', () => {
const url = 'https://wwww.example.test/test';
const noProxy = 'www.example.com';
expect(shouldUseProxy(url, noProxy)).toEqual(true);
});

View File

@ -1,6 +1,6 @@
const { test, expect } = require('@playwright/test');
const { HomePage } = require('../tests/pages/home.page');
import * as faker from './utils/data-faker';
const { faker } = require('./utils/data-faker');
test.describe('bruno e2e test', () => {
let homePage;