forked from teralomaniac/miaomiaomiao
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathproxyAgent.mjs
More file actions
111 lines (95 loc) · 3.5 KB
/
proxyAgent.mjs
File metadata and controls
111 lines (95 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { SocksProxyAgent } from 'socks-proxy-agent';
import HttpsProxyAgent from 'https-proxy-agent';
import { URL } from 'url';
import http from 'http';
import https from 'https';
let globalProxyAgent = null;
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) {
console.log('Proxy environment variable not set, will not use proxy.');
return null;
}
const parsedProxy = parseProxyUrl(proxyUrl);
if (!parsedProxy) return null;
console.log(`Using proxy: ${proxyUrl}`);
if (parsedProxy.protocol === 'socks5') {
console.log('Using SOCKS5 proxy');
return new SocksProxyAgent({
hostname: parsedProxy.host,
port: parsedProxy.port,
userId: parsedProxy.username,
password: parsedProxy.password,
protocol: 'socks5:'
});
} else {
console.log('Using HTTP/HTTPS proxy');
return new HttpsProxyAgent.HttpsProxyAgent(proxyUrl);
}
}
export function setGlobalProxy() {
const proxyUrl = getProxyUrl();
if (proxyUrl) {
globalProxyAgent = createProxyAgent();
// 重写 http 和 https 模块的 request 方法
const originalHttpRequest = http.request;
const originalHttpsRequest = https.request;
http.request = function(options, callback) {
if (typeof options === 'string') {
options = new URL(options);
}
options.agent = globalProxyAgent;
return originalHttpRequest.call(this, options, callback);
};
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();