env-http-proxy-agent.js 4.1 KB

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