proxy.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict'
  2. // Taken from https://github.com/request/request/blob/212570b/lib/getProxyFromURI.js
  3. const url = require('url')
  4. function formatHostname (hostname) {
  5. // canonicalize the hostname, so that 'oogle.com' won't match 'google.com'
  6. return hostname.replace(/^\.*/, '.').toLowerCase()
  7. }
  8. function parseNoProxyZone (zone) {
  9. zone = zone.trim().toLowerCase()
  10. var zoneParts = zone.split(':', 2)
  11. var zoneHost = formatHostname(zoneParts[0])
  12. var zonePort = zoneParts[1]
  13. var hasPort = zone.indexOf(':') > -1
  14. return { hostname: zoneHost, port: zonePort, hasPort: hasPort }
  15. }
  16. function uriInNoProxy (uri, noProxy) {
  17. var port = uri.port || (uri.protocol === 'https:' ? '443' : '80')
  18. var hostname = formatHostname(uri.hostname)
  19. var noProxyList = noProxy.split(',')
  20. // iterate through the noProxyList until it finds a match.
  21. return noProxyList.map(parseNoProxyZone).some(function (noProxyZone) {
  22. var isMatchedAt = hostname.indexOf(noProxyZone.hostname)
  23. var hostnameMatched = (
  24. isMatchedAt > -1 &&
  25. (isMatchedAt === hostname.length - noProxyZone.hostname.length)
  26. )
  27. if (noProxyZone.hasPort) {
  28. return (port === noProxyZone.port) && hostnameMatched
  29. }
  30. return hostnameMatched
  31. })
  32. }
  33. function getProxyFromURI (gyp, env, uri) {
  34. // If a string URI/URL was given, parse it into a URL object
  35. if (typeof uri === 'string') {
  36. // eslint-disable-next-line
  37. uri = url.parse(uri)
  38. }
  39. // Decide the proper request proxy to use based on the request URI object and the
  40. // environmental variables (NO_PROXY, HTTP_PROXY, etc.)
  41. // respect NO_PROXY environment variables (see: https://lynx.invisible-island.net/lynx2.8.7/breakout/lynx_help/keystrokes/environments.html)
  42. var noProxy = gyp.opts.noproxy || env.NO_PROXY || env.no_proxy || env.npm_config_noproxy || ''
  43. // if the noProxy is a wildcard then return null
  44. if (noProxy === '*') {
  45. return null
  46. }
  47. // if the noProxy is not empty and the uri is found return null
  48. if (noProxy !== '' && uriInNoProxy(uri, noProxy)) {
  49. return null
  50. }
  51. // Check for HTTP or HTTPS Proxy in environment Else default to null
  52. if (uri.protocol === 'http:') {
  53. return gyp.opts.proxy ||
  54. env.HTTP_PROXY ||
  55. env.http_proxy ||
  56. env.npm_config_proxy || null
  57. }
  58. if (uri.protocol === 'https:') {
  59. return gyp.opts.proxy ||
  60. env.HTTPS_PROXY ||
  61. env.https_proxy ||
  62. env.HTTP_PROXY ||
  63. env.http_proxy ||
  64. env.npm_config_proxy || null
  65. }
  66. // if none of that works, return null
  67. // (What uri protocol are you using then?)
  68. return null
  69. }
  70. module.exports = getProxyFromURI