From b9d5c61b2ed5832c7f243b2e9a3c6aac5e338211 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Thu, 12 Sep 2024 20:25:58 +0300 Subject: [PATCH] Add CLI for request proxy Closes #364, #377, #1756 --- server.js | 22 +++++++++++++++++++++- src/request-proxy.js | 30 +++++++++++++++++------------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/server.js b/server.js index 03b678e3c..309ce0fc0 100644 --- a/server.js +++ b/server.js @@ -76,6 +76,10 @@ const DEFAULT_AVOID_LOCALHOST = false; const DEFAULT_AUTORUN_HOSTNAME = 'auto'; const DEFAULT_AUTORUN_PORT = -1; +const DEFAULT_PROXY_ENABLED = false; +const DEFAULT_PROXY_URL = ''; +const DEFAULT_PROXY_BYPASS = []; + const cliArguments = yargs(hideBin(process.argv)) .usage('Usage: [options]') .option('enableIPv6', { @@ -146,6 +150,18 @@ const cliArguments = yargs(hideBin(process.argv)) type: 'boolean', default: null, describe: 'Enables basic authentication', + }).option('requestProxyEnabled', { + type: 'boolean', + default: null, + describe: 'Enables request proxy', + }).option('requestProxyUrl', { + type: 'string', + default: null, + describe: 'Request proxy URL', + }).option('requestProxyBypasss', { + type: 'array', + default: null, + describe: 'Request proxy bypass', }).parseSync(); // change all relative paths @@ -182,6 +198,10 @@ const dnsPreferIPv6 = cliArguments.dnsPreferIPv6 ?? getConfigValue('dnsPreferIPv const avoidLocalhost = cliArguments.avoidLocalhost ?? getConfigValue('avoidLocalhost', DEFAULT_AVOID_LOCALHOST); +const proxyEnabled = cliArguments.requestProxyEnabled ?? getConfigValue('requestProxy.enabled', DEFAULT_PROXY_ENABLED); +const proxyUrl = cliArguments.requestProxyUrl ?? getConfigValue('requestProxy.url', DEFAULT_PROXY_URL); +const proxyBypass = cliArguments.requestProxyBypass ?? getConfigValue('requestProxy.bypass', DEFAULT_PROXY_BYPASS); + if (dnsPreferIPv6) { // Set default DNS resolution order to IPv6 first dns.setDefaultResultOrder('ipv6first'); @@ -665,7 +685,7 @@ const preSetupTasks = async function () { }); // Add request proxy. - initRequestProxy(); + initRequestProxy({ enabled: proxyEnabled, url: proxyUrl, bypass: proxyBypass }); }; /** diff --git a/src/request-proxy.js b/src/request-proxy.js index 002745584..815fa76a3 100644 --- a/src/request-proxy.js +++ b/src/request-proxy.js @@ -1,40 +1,44 @@ const http = require('node:http'); const https = require('node:https'); -const { getConfigValue, isValidUrl, color } = require('./util.js'); +const { isValidUrl, color } = require('./util.js'); const LOG_HEADER = '[Request Proxy]'; -function initRequestProxy() { +/** + * Initialize request proxy. + * @param {ProxySettings} settings Proxy settings. + * @typedef {object} ProxySettings + * @property {boolean} enabled Whether proxy is enabled. + * @property {string} url Proxy URL. + * @property {string[]} bypass List of URLs to bypass proxy. + */ +function initRequestProxy({ enabled, url, bypass }) { try { const { ProxyAgent } = require('proxy-agent'); - const proxyEnabled = getConfigValue('requestProxy.enabled', false); // No proxy is enabled, so return - if (!proxyEnabled) { + if (!enabled) { return; } - const proxyUrl = getConfigValue('requestProxy.url', ''); - - if (!proxyUrl) { + if (!url) { console.error(color.red(LOG_HEADER), 'No proxy URL provided'); return; } - if (!isValidUrl(proxyUrl)) { + if (!isValidUrl(url)) { console.error(color.red(LOG_HEADER), 'Invalid proxy URL provided'); return; } // ProxyAgent uses proxy-from-env under the hood // Reference: https://github.com/Rob--W/proxy-from-env - process.env.all_proxy = proxyUrl; + process.env.all_proxy = url; - const proxyBypass = getConfigValue('requestProxy.bypass', []); - if (Array.isArray(proxyBypass) && proxyBypass.length > 0) { - process.env.no_proxy = proxyBypass.join(','); + if (Array.isArray(bypass) && bypass.length > 0) { + process.env.no_proxy = bypass.join(','); } const proxyAgent = new ProxyAgent(); @@ -42,7 +46,7 @@ function initRequestProxy() { https.globalAgent = proxyAgent; console.log(); - console.log(color.green(LOG_HEADER), 'Proxy URL is used:', color.blue(proxyUrl)); + console.log(color.green(LOG_HEADER), 'Proxy URL is used:', color.blue(url)); console.log(); } catch (error) { console.error(color.red(LOG_HEADER), 'Failed to initialize request proxy:', error);