MCPcopy Create free account
hub / github.com/DIYgod/RSSHub / unifyProxy

Function unifyProxy

lib/utils/proxy/unify-proxy.ts:7–111  ·  view source on GitHub ↗
(proxyUri: Config['proxyUri'] | string, proxyObj: Config['proxy'])

Source from the content-addressed store, hash-verified

5const possibleProtocol = ['http', 'https', 'socks', 'socks4', 'socks4a', 'socks5', 'socks5h'];
6
7const unifyProxy = (proxyUri: Config['proxyUri'] | string, proxyObj: Config['proxy']) => {
8 proxyObj ||= {};
9 const [oriProxyUri, oriProxyObj] = [proxyUri, proxyObj];
10 proxyObj = { ...proxyObj };
11
12 let proxyUrlHandler: URL | null = null;
13
14 // PROXY_URI
15 if (proxyUri && typeof proxyUri === 'string') {
16 if (!proxyUri.includes('://')) {
17 logger.warn(`PROXY_URI contains no protocol, assuming ${defaultProtocol}`);
18 proxyUri = `${defaultProtocol}://${proxyUri}`;
19 }
20 try {
21 proxyUrlHandler = new URL(proxyUri);
22 } catch (error: any) {
23 logger.error(`Parse PROXY_URI error: ${error.stack}`);
24 }
25 }
26
27 // PROXY_{PROTOCOL,HOST,PORT}
28 if (proxyObj.protocol || proxyObj.host || proxyObj.port) {
29 if (proxyUrlHandler) {
30 logger.warn('PROXY_URI is set, ignoring PROXY_{PROTOCOL,HOST,PORT}');
31 } else if (proxyObj.host) {
32 let tempProxyStr = proxyObj.host;
33
34 if (tempProxyStr.includes('://')) {
35 logger.warn('PROXY_HOST contains protocol, ignoring PROXY_PROTOCOL');
36 } else if (proxyObj.protocol) {
37 tempProxyStr = `${proxyObj.protocol}://${tempProxyStr}`;
38 } else {
39 logger.warn(`PROXY_PROTOCOL is not set, assuming '${defaultProtocol}'`);
40 tempProxyStr = `${defaultProtocol}://${tempProxyStr}`;
41 }
42 try {
43 proxyUrlHandler = new URL(tempProxyStr);
44 if (proxyUrlHandler.port && proxyObj.port) {
45 logger.warn('PROXY_HOST contains port, ignoring PROXY_PORT');
46 } else if (proxyObj.port) {
47 if (Number.parseInt(proxyObj.port)) {
48 proxyUrlHandler.port = proxyObj.port;
49 } else {
50 logger.warn('PROXY_PORT is not a number, ignoring');
51 }
52 } else {
53 logger.warn('PROXY_PORT is not set, leaving proxy agent to determine');
54 }
55 } catch (error: any) {
56 logger.error(`Parse PROXY_HOST error: ${error.stack}`);
57 }
58 } else {
59 logger.warn('Either PROXY_{PROTOCOL,PORT} is set, but PROXY_HOST is missing, ignoring');
60 }
61 }
62
63 // PROXY_AUTH
64 if (proxyObj.auth && proxyUrlHandler) {

Callers 4

unifyProxiesFunction · 0.85
index.tsFile · 0.85
createMultiProxyFunction · 0.85

Calls 1

sliceMethod · 0.80

Tested by

no test coverage detected