|
import process from 'node:process'; |
|
import http from 'node:http'; |
|
import https from 'node:https'; |
|
import { ProxyAgent } from 'proxy-agent'; |
|
import { isValidUrl, color } from './util.js'; |
|
|
|
const LOG_HEADER = '[Request Proxy]'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default function initRequestProxy({ enabled, url, bypass }) { |
|
try { |
|
|
|
if (!enabled) { |
|
return; |
|
} |
|
|
|
if (!url) { |
|
console.error(color.red(LOG_HEADER), 'No proxy URL provided'); |
|
return; |
|
} |
|
|
|
if (!isValidUrl(url)) { |
|
console.error(color.red(LOG_HEADER), 'Invalid proxy URL provided'); |
|
return; |
|
} |
|
|
|
|
|
|
|
process.env.all_proxy = url; |
|
|
|
if (Array.isArray(bypass) && bypass.length > 0) { |
|
process.env.no_proxy = bypass.join(','); |
|
} |
|
|
|
const proxyAgent = new ProxyAgent(); |
|
http.globalAgent = proxyAgent; |
|
https.globalAgent = proxyAgent; |
|
|
|
console.info(); |
|
console.info(color.green(LOG_HEADER), 'Proxy URL is used:', color.blue(url)); |
|
console.info(); |
|
} catch (error) { |
|
console.error(color.red(LOG_HEADER), 'Failed to initialize request proxy:', error); |
|
} |
|
} |
|
|