global proxy
This commit is contained in:
+92
-37
@@ -1,56 +1,111 @@
|
|||||||
import { SocksProxyAgent } from 'socks-proxy-agent';
|
import { SocksProxyAgent } from 'socks-proxy-agent';
|
||||||
import HttpsProxyAgent from 'https-proxy-agent';
|
import HttpsProxyAgent from 'https-proxy-agent';
|
||||||
import { parse as parseUrl } from 'url';
|
import { URL } from 'url';
|
||||||
|
import http from 'http';
|
||||||
|
import https from 'https';
|
||||||
|
|
||||||
export function createProxyAgent() {
|
let globalProxyAgent = null;
|
||||||
const proxyUrl = process.env.https_proxy || process.env.HTTPS_PROXY;
|
|
||||||
|
|
||||||
|
function getProxyUrl() {
|
||||||
|
const proxyUrl = process.env.https_proxy || process.env.HTTPS_PROXY || process.env.http_proxy || process.env.HTTP_PROXY;
|
||||||
|
return proxyUrl ? proxyUrl.trim() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseProxyUrl(proxyUrl) {
|
||||||
|
if (!proxyUrl) return null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
let protocol, host, port, username, password;
|
||||||
|
if (proxyUrl.startsWith('socks5://')) {
|
||||||
|
const parts = proxyUrl.slice(9).split(':');
|
||||||
|
if (parts.length === 4) {
|
||||||
|
[host, port, username, password] = parts;
|
||||||
|
protocol = 'socks5:';
|
||||||
|
} else {
|
||||||
|
throw new Error('Invalid SOCKS5 proxy URL format');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const url = new URL(proxyUrl);
|
||||||
|
protocol = url.protocol;
|
||||||
|
host = url.hostname;
|
||||||
|
port = url.port;
|
||||||
|
username = url.username;
|
||||||
|
password = url.password;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { protocol: protocol.replace(':', ''), host, port, username, password };
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Invalid proxy URL: ${proxyUrl}`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function createProxyAgent() {
|
||||||
|
const proxyUrl = getProxyUrl();
|
||||||
if (!proxyUrl) {
|
if (!proxyUrl) {
|
||||||
console.log('https_proxy 环境变量未设置,将不使用代理。');
|
console.log('Proxy environment variable not set, will not use proxy.');
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否是 SOCKS5 代理
|
const parsedProxy = parseProxyUrl(proxyUrl);
|
||||||
if (proxyUrl.startsWith('socks5://')) {
|
if (!parsedProxy) return null;
|
||||||
const [host, port, username, password] = proxyUrl.slice(9).split(':');
|
|
||||||
|
console.log(`Using proxy: ${proxyUrl}`);
|
||||||
|
|
||||||
|
if (parsedProxy.protocol === 'socks5') {
|
||||||
|
console.log('Using SOCKS5 proxy');
|
||||||
return new SocksProxyAgent({
|
return new SocksProxyAgent({
|
||||||
host,
|
hostname: parsedProxy.host,
|
||||||
port,
|
port: parsedProxy.port,
|
||||||
userId: username,
|
userId: parsedProxy.username,
|
||||||
password,
|
password: parsedProxy.password,
|
||||||
|
protocol: 'socks5:'
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// 处理 HTTP/HTTPS 代理
|
console.log('Using HTTP/HTTPS proxy');
|
||||||
return new HttpsProxyAgent.HttpsProxyAgent(proxyUrl);
|
return new HttpsProxyAgent.HttpsProxyAgent(proxyUrl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getProxyArgs() {
|
export function setGlobalProxy() {
|
||||||
const proxyUrl = process.env.https_proxy || process.env.HTTPS_PROXY;
|
const proxyUrl = getProxyUrl();
|
||||||
|
if (proxyUrl) {
|
||||||
|
globalProxyAgent = createProxyAgent();
|
||||||
|
|
||||||
if (!proxyUrl) {
|
// 重写 http 和 https 模块的 request 方法
|
||||||
return [];
|
const originalHttpRequest = http.request;
|
||||||
|
const originalHttpsRequest = https.request;
|
||||||
|
|
||||||
|
http.request = function(options, callback) {
|
||||||
|
if (typeof options === 'string') {
|
||||||
|
options = new URL(options);
|
||||||
}
|
}
|
||||||
|
options.agent = globalProxyAgent;
|
||||||
if (proxyUrl.startsWith('socks5://')) {
|
return originalHttpRequest.call(this, options, callback);
|
||||||
const [host, port] = proxyUrl.slice(9).split(':');
|
|
||||||
return [`--proxy-server=socks5://${host}:${port}`];
|
|
||||||
} else {
|
|
||||||
const parsedUrl = parseUrl(proxyUrl);
|
|
||||||
return [`--proxy-server=${parsedUrl.host}`];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getProxyEnv() {
|
|
||||||
const proxyUrl = process.env.https_proxy || process.env.HTTPS_PROXY;
|
|
||||||
|
|
||||||
if (!proxyUrl || !proxyUrl.startsWith('socks5://')) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
const [, , username, password] = proxyUrl.slice(9).split(':');
|
|
||||||
return {
|
|
||||||
SOCKS_USERNAME: username,
|
|
||||||
SOCKS_PASSWORD: password,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
https.request = function(options, callback) {
|
||||||
|
if (typeof options === 'string') {
|
||||||
|
options = new URL(options);
|
||||||
|
}
|
||||||
|
options.agent = globalProxyAgent;
|
||||||
|
return originalHttpsRequest.call(this, options, callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log(`Global proxy set to: ${proxyUrl}`);
|
||||||
|
} else {
|
||||||
|
console.log('No proxy environment variable set, global proxy not configured.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function setProxyEnvironmentVariables() {
|
||||||
|
const proxyUrl = getProxyUrl();
|
||||||
|
if (proxyUrl) {
|
||||||
|
process.env.HTTP_PROXY = proxyUrl;
|
||||||
|
process.env.HTTPS_PROXY = proxyUrl;
|
||||||
|
console.log(`Set proxy environment variables to: ${proxyUrl}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 全局代理
|
||||||
|
setGlobalProxy();
|
||||||
|
|||||||
Reference in New Issue
Block a user