Skip to content

Commit

Permalink
proxy settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirko Golze committed Oct 15, 2023
1 parent 97a300f commit 470e9d0
Show file tree
Hide file tree
Showing 16 changed files with 390 additions and 140 deletions.
28 changes: 0 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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);
});
}
});

Expand All @@ -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">
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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"
Expand Down
63 changes: 47 additions & 16 deletions packages/bruno-app/src/providers/Preferences/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 1 addition & 3 deletions packages/bruno-cli/src/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
28 changes: 12 additions & 16 deletions packages/bruno-cli/src/runner/run-single-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit 470e9d0

Please sign in to comment.