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