env-http-proxy-agent.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. [kClose] () {
  39. return Promise.all([
  40. this[kNoProxyAgent].close(),
  41. !this[kHttpProxyAgent][kClosed] && this[kHttpProxyAgent].close(),
  42. !this[kHttpsProxyAgent][kClosed] && this[kHttpsProxyAgent].close()
  43. ])
  44. }
  45. [kDestroy] (err) {
  46. return Promise.all([
  47. this[kNoProxyAgent].destroy(err),
  48. !this[kHttpProxyAgent][kDestroyed] && this[kHttpProxyAgent].destroy(err),
  49. !this[kHttpsProxyAgent][kDestroyed] && this[kHttpsProxyAgent].destroy(err)
  50. ])
  51. }
  52. #getProxyAgentForUrl (url) {
  53. let { protocol, host: hostname, port } = url
  54. // Stripping ports in this way instead of using parsedUrl.hostname to make
  55. // sure that the brackets around IPv6 addresses are kept.
  56. hostname = hostname.replace(/:\d*$/, '').toLowerCase()
  57. port = Number.parseInt(port, 10) || DEFAULT_PORTS[protocol] || 0
  58. if (!this.#shouldProxy(hostname, port)) {
  59. return this[kNoProxyAgent]
  60. }
  61. if (protocol === 'https:') {
  62. return this[kHttpsProxyAgent]
  63. }
  64. return this[kHttpProxyAgent]
  65. }
  66. #shouldProxy (hostname, port) {
  67. if (this.#noProxyChanged) {
  68. this.#parseNoProxy()
  69. }
  70. if (this.#noProxyEntries.length === 0) {
  71. return true // Always proxy if NO_PROXY is not set or empty.
  72. }
  73. if (this.#noProxyValue === '*') {
  74. return false // Never proxy if wildcard is set.
  75. }
  76. for (let i = 0; i < this.#noProxyEntries.length; i++) {
  77. const entry = this.#noProxyEntries[i]
  78. if (entry.port && entry.port !== port) {
  79. continue // Skip if ports don't match.
  80. }
  81. // Don't proxy if the hostname is equal with the no_proxy host.
  82. if (hostname === entry.hostname) {
  83. return false
  84. }
  85. // Don't proxy if the hostname is the subdomain of the no_proxy host.
  86. // Reference - https://github.com/denoland/deno/blob/6fbce91e40cc07fc6da74068e5cc56fdd40f7b4c/ext/fetch/proxy.rs#L485
  87. if (hostname.slice(-(entry.hostname.length + 1)) === `.${entry.hostname}`) {
  88. return false
  89. }
  90. }
  91. return true
  92. }
  93. #parseNoProxy () {
  94. const noProxyValue = this.#opts.noProxy ?? this.#noProxyEnv
  95. const noProxySplit = noProxyValue.split(/[,\s]/)
  96. const noProxyEntries = []
  97. for (let i = 0; i < noProxySplit.length; i++) {
  98. const entry = noProxySplit[i]
  99. if (!entry) {
  100. continue
  101. }
  102. const parsed = entry.match(/^(.+):(\d+)$/)
  103. noProxyEntries.push({
  104. // strip leading dot or asterisk with dot
  105. hostname: (parsed ? parsed[1] : entry).replace(/^\*?\./, '').toLowerCase(),
  106. port: parsed ? Number.parseInt(parsed[2], 10) : 0
  107. })
  108. }
  109. this.#noProxyValue = noProxyValue
  110. this.#noProxyEntries = noProxyEntries
  111. }
  112. get #noProxyChanged () {
  113. if (this.#opts.noProxy !== undefined) {
  114. return false
  115. }
  116. return this.#noProxyValue !== this.#noProxyEnv
  117. }
  118. get #noProxyEnv () {
  119. return process.env.no_proxy ?? process.env.NO_PROXY ?? ''
  120. }
  121. }
  122. module.exports = EnvHttpProxyAgent