1 | 'use strict'
|
2 |
|
3 | function formatHostname(hostname) {
|
4 |
|
5 | return hostname.replace(/^\.*/, '.').toLowerCase()
|
6 | }
|
7 |
|
8 | function parseNoProxyZone(zone) {
|
9 | zone = zone.trim().toLowerCase()
|
10 |
|
11 | var zoneParts = zone.split(':', 2)
|
12 | , zoneHost = formatHostname(zoneParts[0])
|
13 | , zonePort = zoneParts[1]
|
14 | , hasPort = zone.indexOf(':') > -1
|
15 |
|
16 | return {hostname: zoneHost, port: zonePort, hasPort: hasPort}
|
17 | }
|
18 |
|
19 | function uriInNoProxy(uri, noProxy) {
|
20 | var port = uri.port || (uri.protocol === 'https:' ? '443' : '80')
|
21 | , hostname = formatHostname(uri.hostname)
|
22 | , noProxyList = noProxy.split(',')
|
23 |
|
24 |
|
25 | return noProxyList.map(parseNoProxyZone).some(function(noProxyZone) {
|
26 | var isMatchedAt = hostname.indexOf(noProxyZone.hostname)
|
27 | , hostnameMatched = (
|
28 | isMatchedAt > -1 &&
|
29 | (isMatchedAt === hostname.length - noProxyZone.hostname.length)
|
30 | )
|
31 |
|
32 | if (noProxyZone.hasPort) {
|
33 | return (port === noProxyZone.port) && hostnameMatched
|
34 | }
|
35 |
|
36 | return hostnameMatched
|
37 | })
|
38 | }
|
39 |
|
40 | function getProxyFromURI(uri) {
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | var noProxy = process.env.NO_PROXY || process.env.no_proxy || ''
|
46 |
|
47 |
|
48 |
|
49 | if (noProxy === '*') {
|
50 | return null
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 | if (noProxy !== '' && uriInNoProxy(uri, noProxy)) {
|
56 | return null
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 | if (uri.protocol === 'http:') {
|
62 | return process.env.HTTP_PROXY ||
|
63 | process.env.http_proxy || null
|
64 | }
|
65 |
|
66 | if (uri.protocol === 'https:') {
|
67 | return process.env.HTTPS_PROXY ||
|
68 | process.env.https_proxy ||
|
69 | process.env.HTTP_PROXY ||
|
70 | process.env.http_proxy || null
|
71 | }
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | return null
|
77 | }
|
78 |
|
79 | module.exports = getProxyFromURI
|