1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 | const url = require('url');
|
7 | const tunnelAgent = require('tunnel-agent');
|
8 |
|
9 | const is = require('./is');
|
10 |
|
11 | const proxies = [
|
12 | 'HTTPS_PROXY',
|
13 | 'https_proxy',
|
14 | 'HTTP_PROXY',
|
15 | 'http_proxy',
|
16 | 'npm_config_https_proxy',
|
17 | 'npm_config_proxy'
|
18 | ];
|
19 |
|
20 | function env (key) {
|
21 | return process.env[key];
|
22 | }
|
23 |
|
24 | module.exports = function (log) {
|
25 | try {
|
26 | const proxy = new url.URL(proxies.map(env).find(is.string));
|
27 | const tunnel = proxy.protocol === 'https:'
|
28 | ? tunnelAgent.httpsOverHttps
|
29 | : tunnelAgent.httpsOverHttp;
|
30 | const proxyAuth = proxy.username && proxy.password
|
31 | ? `${decodeURIComponent(proxy.username)}:${decodeURIComponent(proxy.password)}`
|
32 | : null;
|
33 | log(`Via proxy ${proxy.protocol}//${proxy.hostname}:${proxy.port} ${proxyAuth ? 'with' : 'no'} credentials`);
|
34 | return tunnel({
|
35 | proxy: {
|
36 | port: Number(proxy.port),
|
37 | host: proxy.hostname,
|
38 | proxyAuth
|
39 | }
|
40 | });
|
41 | } catch (err) {
|
42 | return null;
|
43 | }
|
44 | };
|