Add CLI for request proxy Closes #364, #377, #1756

b9d5c61b2ed5832c7f243b2e9a3c6aac5e338211

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +38 -14Showing whitespace changes
server.js+21 -1
@@ -76,6 +76,10 @@ const DEFAULT_AVOID_LOCALHOST = false;
7676const DEFAULT_AUTORUN_HOSTNAME = 'auto';
7777const DEFAULT_AUTORUN_PORT = -1;
7878
79+const DEFAULT_PROXY_ENABLED = false;
80+const DEFAULT_PROXY_URL = '';
81+const DEFAULT_PROXY_BYPASS = [];
82+
7983const cliArguments = yargs(hideBin(process.argv))
8084 .usage('Usage: <your-start-script> <command> [options]')
8185 .option('enableIPv6', {
@@ -146,6 +150,18 @@ const cliArguments = yargs(hideBin(process.argv))
146150 type: 'boolean',
147151 default: null,
148152 describe: 'Enables basic authentication',
153+ }).option('requestProxyEnabled', {
154+ type: 'boolean',
155+ default: null,
156+ describe: 'Enables request proxy',
157+ }).option('requestProxyUrl', {
158+ type: 'string',
159+ default: null,
160+ describe: 'Request proxy URL',
161+ }).option('requestProxyBypasss', {
162+ type: 'array',
163+ default: null,
164+ describe: 'Request proxy bypass',
149165 }).parseSync();
150166
151167// change all relative paths
@@ -182,6 +198,10 @@ const dnsPreferIPv6 = cliArguments.dnsPreferIPv6 ?? getConfigValue('dnsPreferIPv
182198
183199const avoidLocalhost = cliArguments.avoidLocalhost ?? getConfigValue('avoidLocalhost', DEFAULT_AVOID_LOCALHOST);
184200
201+const proxyEnabled = cliArguments.requestProxyEnabled ?? getConfigValue('requestProxy.enabled', DEFAULT_PROXY_ENABLED);
202+const proxyUrl = cliArguments.requestProxyUrl ?? getConfigValue('requestProxy.url', DEFAULT_PROXY_URL);
203+const proxyBypass = cliArguments.requestProxyBypass ?? getConfigValue('requestProxy.bypass', DEFAULT_PROXY_BYPASS);
204+
185205if (dnsPreferIPv6) {
186206 // Set default DNS resolution order to IPv6 first
187207 dns.setDefaultResultOrder('ipv6first');
@@ -665,7 +685,7 @@ const preSetupTasks = async function () {
665685 });
666686
667687 // Add request proxy.
668- initRequestProxy();
688+ initRequestProxy({ enabled: proxyEnabled, url: proxyUrl, bypass: proxyBypass });
669689};
670690
671691/**
src/request-proxy.js+17 -13
@@ -1,40 +1,44 @@
11const http = require('node:http');
22const https = require('node:https');
33
44const { getConfigValue, isValidUrl, color } = require('./util.js');
55
66const LOG_HEADER = '[Request Proxy]';
77
8-function initRequestProxy() {
8+/**
9+ * Initialize request proxy.
10+ * @param {ProxySettings} settings Proxy settings.
11+ * @typedef {object} ProxySettings
12+ * @property {boolean} enabled Whether proxy is enabled.
13+ * @property {string} url Proxy URL.
14+ * @property {string[]} bypass List of URLs to bypass proxy.
15+ */
16+function initRequestProxy({ enabled, url, bypass }) {
917 try {
1018 const { ProxyAgent } = require('proxy-agent');
11- const proxyEnabled = getConfigValue('requestProxy.enabled', false);
1219
1320 // No proxy is enabled, so return
1421 if (!proxyEnabledenabled) {
1522 return;
1623 }
1724
18- const proxyUrl = getConfigValue('requestProxy.url', '');
25+ if (!url) {
19-
20- if (!proxyUrl) {
2126 console.error(color.red(LOG_HEADER), 'No proxy URL provided');
2227 return;
2328 }
2429
2530 if (!isValidUrl(proxyUrlurl)) {
2631 console.error(color.red(LOG_HEADER), 'Invalid proxy URL provided');
2732 return;
2833 }
2934
3035 // ProxyAgent uses proxy-from-env under the hood
3136 // Reference: https://github.com/Rob--W/proxy-from-env
3237 process.env.all_proxy = proxyUrlurl;
3338
34- const proxyBypass = getConfigValue('requestProxy.bypass', []);
3539
3640 if (Array.isArray(proxyBypassbypass) && proxyBypassbypass.length > 0) {
3741 process.env.no_proxy = proxyBypassbypass.join(',');
3842 }
3943
4044 const proxyAgent = new ProxyAgent();
@@ -42,7 +46,7 @@ function initRequestProxy() {
4246 https.globalAgent = proxyAgent;
4347
4448 console.log();
4549 console.log(color.green(LOG_HEADER), 'Proxy URL is used:', color.blue(proxyUrlurl));
4650 console.log();
4751 } catch (error) {
4852 console.error(color.red(LOG_HEADER), 'Failed to initialize request proxy:', error);