File size: 2,455 Bytes
80f4e3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { HttpsProxyAgent } from 'https-proxy-agent';
import { SocksProxyAgent } from 'socks-proxy-agent';

import { ProxyAgent } from 'undici'

type Proxy = {
  host: string;
  password?: string;
  port: string;
  protocol: string;
  username?: string;
};

function selectProxyAgent(proxyUrl: string): HttpsProxyAgent<string> | SocksProxyAgent {
  const url = new URL(proxyUrl);

  // NOTE: The following constants are not used in the function but are defined for clarity.
  // When a proxy URL is used to build the URL object, the protocol returned by procotol's property contains a `:` at
  // the end so, we add the protocol constants without the `:` to avoid confusion.
  const PROXY_HTTP_PROTOCOL = 'http:';
  const PROXY_SOCKS_PROTOCOL = 'socks:';

  switch (url.protocol) {
    case PROXY_HTTP_PROTOCOL:
      return new HttpsProxyAgent(url);
    case PROXY_SOCKS_PROTOCOL:
      return new SocksProxyAgent(url);
    default:
      throw new Error(`Unsupported proxy protocol: ${url.protocol}`);
  }
}

export function makeProxyAgent(proxy: Proxy | string): HttpsProxyAgent<string> | SocksProxyAgent {
  if (typeof proxy === 'string') {
    return selectProxyAgent(proxy);
  }

  const { host, password, port, protocol, username } = proxy;
  let proxyUrl = `${protocol}://${host}:${port}`;

  if (username && password) {
    proxyUrl = `${protocol}://${username}:${password}@${host}:${port}`;
  }

  return selectProxyAgent(proxyUrl);
}

export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent {
  let proxyUrl: string
  let protocol: string

  if (typeof proxy === 'string') {
    const url = new URL(proxy)
    protocol = url.protocol.replace(':', '')
    proxyUrl = proxy
  } else {
    const { host, password, port, protocol: proto, username } = proxy
    protocol = (proto || 'http').replace(':', '')

    if (protocol === 'socks') {
      protocol = 'socks5'
    }

    const auth = username && password ? `${username}:${password}@` : ''
    proxyUrl = `${protocol}://${auth}${host}:${port}`
  }

  const PROXY_HTTP_PROTOCOL = 'http'
  const PROXY_HTTPS_PROTOCOL = 'https'
  const PROXY_SOCKS4_PROTOCOL = 'socks4'
  const PROXY_SOCKS5_PROTOCOL = 'socks5'

  switch (protocol) {
    case PROXY_HTTP_PROTOCOL:
    case PROXY_HTTPS_PROTOCOL:
    case PROXY_SOCKS4_PROTOCOL:
    case PROXY_SOCKS5_PROTOCOL:
      return new ProxyAgent(proxyUrl)

    default:
      throw new Error(`Unsupported proxy protocol: ${protocol}`)
  }
}