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