env-http-proxy-agent.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. 'use strict'
  2. const DispatcherBase = require('./dispatcher-base')
  3. const { kClose, kDestroy, kClosed, kDestroyed, kDispatch, kNoProxyAgent, kHttpProxyAgent, kHttpsProxyAgent } = require('../core/symbols')
  4. const ProxyAgent = require('./proxy-agent')
  5. const Agent = require('./agent')
  6. const DEFAULT_PORTS = {
  7. 'http:': 80,
  8. 'https:': 443
  9. }
  10. let experimentalWarned = false
  11. class EnvHttpProxyAgent extends DispatcherBase {
  12. #noProxyValue = null
  13. #noProxyEntries = null
  14. #opts = null
  15. constructor (opts = {}) {
  16. super()
  17. this.#opts = opts
  18. if (!experimentalWarned) {
  19. experimentalWarned = true
  20. process.emitWarning('EnvHttpProxyAgent is experimental, expect them to change at any time.', {
  21. code: 'UNDICI-EHPA'
  22. })
  23. }
  24. const { httpProxy, httpsProxy, noProxy, ...agentOpts } = opts
  25. this[kNoProxyAgent] = new Agent(agentOpts)
  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. const HTTPS_PROXY = httpsProxy ?? process.env.https_proxy ?? process.env.HTTPS_PROXY
  33. if (HTTPS_PROXY) {
  34. this[kHttpsProxyAgent] = new ProxyAgent({ ...agentOpts, uri: HTTPS_PROXY })
  35. } else {
  36. this[kHttpsProxyAgent] = this[kHttpProxyAgent]
  37. }
  38. this.#parseNoProxy()
  39. }
  40. [kDispatch] (opts, handler) {
  41. const url = new URL(opts.origin)
  42. const agent = this.#getProxyAgentForUrl(url)
  43. return agent.dispatch(opts, handler)
  44. }
  45. async [kClose] () {
  46. await this[kNoProxyAgent].close()
  47. if (!this[kHttpProxyAgent][kClosed]) {
  48. await this[kHttpProxyAgent].close()
  49. }
  50. if (!this[kHttpsProxyAgent][kClosed]) {
  51. await this[kHttpsProxyAgent].close()
  52. }
  53. }
  54. async [kDestroy] (err) {
  55. await this[kNoProxyAgent].destroy(err)
  56. if (!this[kHttpProxyAgent][kDestroyed]) {
  57. await this[kHttpProxyAgent].destroy(err)
  58. }
  59. if (!this[kHttpsProxyAgent][kDestroyed]) {
  60. await this[kHttpsProxyAgent].destroy(err)
  61. }
  62. }
  63. #getProxyAgentForUrl (url) {
  64. let { protocol, host: hostname, port } = url
  65. // Stripping ports in this way instead of using parsedUrl.hostname to make
  66. // sure that the brackets around IPv6 addresses are kept.
  67. hostname = hostname.replace(/:\d*$/, '').toLowerCase()
  68. port = Number.parseInt(port, 10) || DEFAULT_PORTS[protocol] || 0
  69. if (!this.#shouldProxy(hostname, port)) {
  70. return this[kNoProxyAgent]
  71. }
  72. if (protocol === 'https:') {
  73. return this[kHttpsProxyAgent]
  74. }
  75. return this[kHttpProxyAgent]
  76. }
  77. #shouldProxy (hostname, port) {
  78. if (this.#noProxyChanged) {
  79. this.#parseNoProxy()
  80. }
  81. if (this.#noProxyEntries.length === 0) {
  82. return true // Always proxy if NO_PROXY is not set or empty.
  83. }
  84. if (this.#noProxyValue === '*') {
  85. return false // Never proxy if wildcard is set.
  86. }
  87. for (let i = 0; i < this.#noProxyEntries.length; i++) {
  88. const entry = this.#noProxyEntries[i]
  89. if (entry.port && entry.port !== port) {
  90. continue // Skip if ports don't match.
  91. }
  92. if (!/^[.*]/.test(entry.hostname)) {
  93. // No wildcards, so don't proxy only if there is not an exact match.
  94. if (hostname === entry.hostname) {
  95. return false
  96. }
  97. } else {
  98. // Don't proxy if the hostname ends with the no_proxy host.
  99. if (hostname.endsWith(entry.hostname.replace(/^\*/, ''))) {
  100. return false
  101. }
  102. }
  103. }
  104. return true
  105. }
  106. #parseNoProxy () {
  107. const noProxyValue = this.#opts.noProxy ?? this.#noProxyEnv
  108. const noProxySplit = noProxyValue.split(/[,\s]/)
  109. const noProxyEntries = []
  110. for (let i = 0; i < noProxySplit.length; i++) {
  111. const entry = noProxySplit[i]
  112. if (!entry) {
  113. continue
  114. }
  115. const parsed = entry.match(/^(.+):(\d+)$/)
  116. noProxyEntries.push({
  117. hostname: (parsed ? parsed[1] : entry).toLowerCase(),
  118. port: parsed ? Number.parseInt(parsed[2], 10) : 0
  119. })
  120. }
  121. this.#noProxyValue = noProxyValue
  122. this.#noProxyEntries = noProxyEntries
  123. }
  124. get #noProxyChanged () {
  125. if (this.#opts.noProxy !== undefined) {
  126. return false
  127. }
  128. return this.#noProxyValue !== this.#noProxyEnv
  129. }
  130. get #noProxyEnv () {
  131. return process.env.no_proxy ?? process.env.NO_PROXY ?? ''
  132. }
  133. }
  134. module.exports = EnvHttpProxyAgent