Debian Codegraph @

lib/dispatcher/env-http-proxy-agent.js

raw
1 'use strict'
2
3 const DispatcherBase = require('./dispatcher-base')
4 const { kClose, kDestroy, kClosed, kDestroyed, kDispatch, kNoProxyAgent, kHttpProxyAgent, kHttpsProxyAgent } = require('../core/symbols')
5 const ProxyAgent = require('./proxy-agent')
6 const Agent = require('./agent')
7
8 const DEFAULT_PORTS = {
9 'http:': 80,
10 'https:': 443
11 }
12
13 class EnvHttpProxyAgent extends DispatcherBase {
14 #noProxyValue = null
15 #noProxyEntries = null
16 #opts = null
17
18 constructor (opts = {}) {
19 super()
20 this.#opts = opts
21
22 const { httpProxy, httpsProxy, noProxy, ...agentOpts } = opts
23
24 this[kNoProxyAgent] = new Agent(agentOpts)
25
26 const HTTP_PROXY = httpProxy ?? process.env.http_proxy ?? process.env.HTTP_PROXY
27 if (HTTP_PROXY) {
28 this[kHttpProxyAgent] = new ProxyAgent({ ...agentOpts, uri: HTTP_PROXY })
29 } else {
30 this[kHttpProxyAgent] = this[kNoProxyAgent]
31 }
32
33 const HTTPS_PROXY = httpsProxy ?? process.env.https_proxy ?? process.env.HTTPS_PROXY
34 if (HTTPS_PROXY) {
35 this[kHttpsProxyAgent] = new ProxyAgent({ ...agentOpts, uri: HTTPS_PROXY })
36 } else {
37 this[kHttpsProxyAgent] = this[kHttpProxyAgent]
38 }
39
40 this.#parseNoProxy()
41 }
42
43 [kDispatch] (opts, handler) {
44 const url = new URL(opts.origin)
45 const agent = this.#getProxyAgentForUrl(url)
46 return agent.dispatch(opts, handler)
47 }
48
49 [kClose] () {
50 return Promise.all([
51 this[kNoProxyAgent].close(),
52 !this[kHttpProxyAgent][kClosed] && this[kHttpProxyAgent].close(),
53 !this[kHttpsProxyAgent][kClosed] && this[kHttpsProxyAgent].close()
54 ])
55 }
56
57 [kDestroy] (err) {
58 return Promise.all([
59 this[kNoProxyAgent].destroy(err),
60 !this[kHttpProxyAgent][kDestroyed] && this[kHttpProxyAgent].destroy(err),
61 !this[kHttpsProxyAgent][kDestroyed] && this[kHttpsProxyAgent].destroy(err)
62 ])
63 }
64
65 #getProxyAgentForUrl (url) {
66 let { protocol, host: hostname, port } = url
67
68 // Stripping ports in this way instead of using parsedUrl.hostname to make
69 // sure that the brackets around IPv6 addresses are kept.
70 hostname = hostname.replace(/:\d*$/, '').toLowerCase()
71 port = Number.parseInt(port, 10) || DEFAULT_PORTS[protocol] || 0
72 if (!this.#shouldProxy(hostname, port)) {
73 return this[kNoProxyAgent]
74 }
75 if (protocol === 'https:') {
76 return this[kHttpsProxyAgent]
77 }
78 return this[kHttpProxyAgent]
79 }
80
81 #shouldProxy (hostname, port) {
82 if (this.#noProxyChanged) {
83 this.#parseNoProxy()
84 }
85
86 if (this.#noProxyEntries.length === 0) {
87 return true // Always proxy if NO_PROXY is not set or empty.
88 }
89 if (this.#noProxyValue === '*') {
90 return false // Never proxy if wildcard is set.
91 }
92
93 for (let i = 0; i < this.#noProxyEntries.length; i++) {
94 const entry = this.#noProxyEntries[i]
95 if (entry.port && entry.port !== port) {
96 continue // Skip if ports don't match.
97 }
98 // Don't proxy if the hostname is equal with the no_proxy host.
99 if (hostname === entry.hostname) {
100 return false
101 }
102 // Don't proxy if the hostname is the subdomain of the no_proxy host.
103 // Reference - https://github.com/denoland/deno/blob/6fbce91e40cc07fc6da74068e5cc56fdd40f7b4c/ext/fetch/proxy.rs#L485
104 if (hostname.slice(-(entry.hostname.length + 1)) === `.${entry.hostname}`) {
105 return false
106 }
107 }
108
109 return true
110 }
111
112 #parseNoProxy () {
113 const noProxyValue = this.#opts.noProxy ?? this.#noProxyEnv
114 const noProxySplit = noProxyValue.split(/[,\s]/)
115 const noProxyEntries = []
116
117 for (let i = 0; i < noProxySplit.length; i++) {
118 const entry = noProxySplit[i]
119 if (!entry) {
120 continue
121 }
122 const parsed = entry.match(/^(.+):(\d+)$/)
123 noProxyEntries.push({
124 // strip leading dot or asterisk with dot
125 hostname: (parsed ? parsed[1] : entry).replace(/^\*?\./, '').toLowerCase(),
126 port: parsed ? Number.parseInt(parsed[2], 10) : 0
127 })
128 }
129
130 this.#noProxyValue = noProxyValue
131 this.#noProxyEntries = noProxyEntries
132 }
133
134 get #noProxyChanged () {
135 if (this.#opts.noProxy !== undefined) {
136 return false
137 }
138 return this.#noProxyValue !== this.#noProxyEnv
139 }
140
141 get #noProxyEnv () {
142 return process.env.no_proxy ?? process.env.NO_PROXY ?? ''
143 }
144 }
145
146 module.exports = EnvHttpProxyAgent