From e1a74d0652ecb6b5828683f318f180ecebfb6ff1 Mon Sep 17 00:00:00 2001 From: Mirko Golze Date: Fri, 10 Nov 2023 21:20:30 +0100 Subject: [PATCH 1/2] #937, #921 improve evaluation of proxy configuration, allow empty proxy port --- .../CollectionSettings/ProxySettings/index.js | 14 ++++----- .../Preferences/ProxySettings/index.js | 10 +++---- .../src/runner/run-single-request.js | 7 +++-- .../bruno-electron/src/ipc/network/index.js | 30 +++++-------------- 4 files changed, 21 insertions(+), 40 deletions(-) diff --git a/packages/bruno-app/src/components/CollectionSettings/ProxySettings/index.js b/packages/bruno-app/src/components/CollectionSettings/ProxySettings/index.js index 2ed25b1e..c8ccb103 100644 --- a/packages/bruno-app/src/components/CollectionSettings/ProxySettings/index.js +++ b/packages/bruno-app/src/components/CollectionSettings/ProxySettings/index.js @@ -11,22 +11,20 @@ const ProxySettings = ({ proxyConfig, onUpdate }) => { protocol: Yup.string().oneOf(['http', 'https', 'socks4', 'socks5']), hostname: Yup.string() .when('enabled', { - is: true, + 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.required('Specify port between 1 and 65535').typeError('Specify port between 1 and 65535'), - otherwise: (port) => port.nullable().transform((_, val) => (val ? Number(val) : null)) - }) .min(1) - .max(65535), + .max(65535) + .typeError('Specify port between 1 and 65535') + .nullable() + .transform((_, val) => (val ? Number(val) : null)), auth: Yup.object() .when('enabled', { - is: true, + is: 'true', then: Yup.object({ enabled: Yup.boolean(), username: Yup.string() diff --git a/packages/bruno-app/src/components/Preferences/ProxySettings/index.js b/packages/bruno-app/src/components/Preferences/ProxySettings/index.js index 6b3fb787..788f7927 100644 --- a/packages/bruno-app/src/components/Preferences/ProxySettings/index.js +++ b/packages/bruno-app/src/components/Preferences/ProxySettings/index.js @@ -22,13 +22,11 @@ const ProxySettings = ({ close }) => { }) .max(1024), port: Yup.number() - .when('enabled', { - is: true, - then: (port) => port.required('Specify port between 1 and 65535').typeError('Specify port between 1 and 65535'), - otherwise: (port) => port.nullable().transform((_, val) => (val ? Number(val) : null)) - }) .min(1) - .max(65535), + .max(65535) + .typeError('Specify port between 1 and 65535') + .nullable() + .transform((_, val) => (val ? Number(val) : null)), auth: Yup.object() .when('enabled', { is: true, diff --git a/packages/bruno-cli/src/runner/run-single-request.js b/packages/bruno-cli/src/runner/run-single-request.js index e047969f..32122f74 100644 --- a/packages/bruno-cli/src/runner/run-single-request.js +++ b/packages/bruno-cli/src/runner/run-single-request.js @@ -3,7 +3,7 @@ const qs = require('qs'); const chalk = require('chalk'); const decomment = require('decomment'); const fs = require('fs'); -const { forOwn, each, extend, get, compact } = require('lodash'); +const { forOwn, isUndefined, isNull, each, extend, get, compact } = require('lodash'); const FormData = require('form-data'); const prepareRequest = require('./prepare-request'); const interpolateVars = require('./interpolate-vars'); @@ -136,14 +136,15 @@ const runSingleRequest = async function ( const proxyAuthEnabled = get(brunoConfig, 'proxy.auth.enabled', false); const socksEnabled = proxyProtocol.includes('socks'); + let uriPort = isUndefined(proxyPort) || isNull(proxyPort) ? '' : `:${proxyPort}`; let proxyUri; if (proxyAuthEnabled) { const proxyAuthUsername = interpolateString(get(brunoConfig, 'proxy.auth.username'), interpolationOptions); const proxyAuthPassword = interpolateString(get(brunoConfig, 'proxy.auth.password'), interpolationOptions); - proxyUri = `${proxyProtocol}://${proxyAuthUsername}:${proxyAuthPassword}@${proxyHostname}:${proxyPort}`; + proxyUri = `${proxyProtocol}://${proxyAuthUsername}:${proxyAuthPassword}@${proxyHostname}${uriPort}`; } else { - proxyUri = `${proxyProtocol}://${proxyHostname}:${proxyPort}`; + proxyUri = `${proxyProtocol}://${proxyHostname}${uriPort}`; } if (socksEnabled) { diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index 7e4b7ee2..9419d455 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -6,11 +6,10 @@ const axios = require('axios'); const path = require('path'); const decomment = require('decomment'); const Mustache = require('mustache'); -const FormData = require('form-data'); const contentDispositionParser = require('content-disposition'); const mime = require('mime-types'); const { ipcMain } = require('electron'); -const { forOwn, extend, each, get, compact } = require('lodash'); +const { isEmpty, isUndefined, isNull, each, get, compact } = require('lodash'); const { VarsRuntime, AssertRuntime, ScriptRuntime, TestRuntime } = require('@usebruno/js'); const prepareRequest = require('./prepare-request'); const prepareGqlIntrospectionRequest = require('./prepare-gql-introspection-request'); @@ -72,22 +71,6 @@ const getEnvVars = (environment = {}) => { }; }; -const getSize = (data) => { - if (!data) { - return 0; - } - - if (typeof data === 'string') { - return Buffer.byteLength(data, 'utf8'); - } - - if (typeof data === 'object') { - return Buffer.byteLength(safeStringifyJSON(data), 'utf8'); - } - - return 0; -}; - const configureRequest = async ( collectionUid, request, @@ -143,7 +126,7 @@ const configureRequest = async ( // proxy configuration let proxyConfig = get(brunoConfig, 'proxy', {}); - let proxyEnabled = get(proxyConfig, 'enabled', false); + let proxyEnabled = isEmpty(proxyConfig) ? 'global' : get(proxyConfig, 'enabled', false); if (proxyEnabled === 'global') { proxyConfig = preferencesUtil.getGlobalProxyConfig(); proxyEnabled = get(proxyConfig, 'enabled', false); @@ -156,14 +139,15 @@ const configureRequest = async ( 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}:${proxyPort}`; + proxyUri = `${proxyProtocol}://${proxyAuthUsername}:${proxyAuthPassword}@${proxyHostname}${uriPort}`; } else { - proxyUri = `${proxyProtocol}://${proxyHostname}:${proxyPort}`; + proxyUri = `${proxyProtocol}://${proxyHostname}${uriPort}`; } if (socksEnabled) { @@ -203,10 +187,10 @@ const configureRequest = async ( const parseDataFromResponse = (response) => { const dataBuffer = Buffer.from(response.data); // Parse the charset from content type: https://stackoverflow.com/a/33192813 - const charset = /charset=([^()<>@,;:\"/[\]?.=\s]*)/i.exec(response.headers['Content-Type'] || ''); + const charset = /charset=([^()<>@,;:"/[\]?.=\s]*)/i.exec(response.headers['Content-Type'] || ''); // Overwrite the original data for backwards compatability let data = dataBuffer.toString(charset || 'utf-8'); - // Try to parse response to JSON, this can quitly fail + // Try to parse response to JSON, this can quietly fail try { data = JSON.parse(response.data); } catch {} From a5ce7e9c9ccb2c7a208e8d6c6207193b8a61ac21 Mon Sep 17 00:00:00 2001 From: Mirko Golze Date: Sun, 12 Nov 2023 21:41:25 +0100 Subject: [PATCH 2/2] #937, #921 improve evaluation of proxy configuration, allow empty proxy port --- packages/bruno-electron/src/ipc/network/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index 9419d455..d6afbe6f 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -9,7 +9,7 @@ const Mustache = require('mustache'); const contentDispositionParser = require('content-disposition'); const mime = require('mime-types'); const { ipcMain } = require('electron'); -const { isEmpty, isUndefined, isNull, each, get, compact } = require('lodash'); +const { isUndefined, isNull, each, get, compact } = require('lodash'); const { VarsRuntime, AssertRuntime, ScriptRuntime, TestRuntime } = require('@usebruno/js'); const prepareRequest = require('./prepare-request'); const prepareGqlIntrospectionRequest = require('./prepare-gql-introspection-request'); @@ -126,7 +126,7 @@ const configureRequest = async ( // proxy configuration let proxyConfig = get(brunoConfig, 'proxy', {}); - let proxyEnabled = isEmpty(proxyConfig) ? 'global' : get(proxyConfig, 'enabled', false); + let proxyEnabled = get(proxyConfig, 'enabled', 'global'); if (proxyEnabled === 'global') { proxyConfig = preferencesUtil.getGlobalProxyConfig(); proxyEnabled = get(proxyConfig, 'enabled', false);