1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const fs = require("fs");
|
4 | const path = require("path");
|
5 | const uri = require("url");
|
6 | class ProxyUtil {
|
7 | static get httpProxy() {
|
8 | return this.env.HTTP_PROXY || this.env.http_proxy;
|
9 | }
|
10 | static get httpsProxy() {
|
11 | return this.env.HTTPS_PROXY || this.env.https_proxy;
|
12 | }
|
13 | static get noProxy() {
|
14 | return this.env.NO_PROXY || this.env.no_proxy;
|
15 | }
|
16 | static shouldDodgeProxy(host) {
|
17 | if (!this.noProxy)
|
18 | return false;
|
19 | if (this.noProxy === '*')
|
20 | return true;
|
21 | return this.noProxy
|
22 | .split(',')
|
23 | .map(p => p.trim())
|
24 | .some(p => (p[0] === '.' && host.endsWith(p.substr(1))) || host.endsWith(p));
|
25 | }
|
26 | static usingProxy(host) {
|
27 | if (host && this.shouldDodgeProxy(host))
|
28 | return false;
|
29 | if (this.httpProxy || this.httpsProxy)
|
30 | return true;
|
31 | return false;
|
32 | }
|
33 | static get sslCertDir() {
|
34 | const certDir = this.env.SSL_CERT_DIR;
|
35 | if (certDir) {
|
36 | return fs.readdirSync(certDir)
|
37 | .map(f => path.join(certDir, f))
|
38 | .filter(f => fs.statSync(f).isFile());
|
39 | }
|
40 | else {
|
41 | return [];
|
42 | }
|
43 | }
|
44 | static get sslCertFile() {
|
45 | return this.env.SSL_CERT_FILE ? [this.env.SSL_CERT_FILE] : [];
|
46 | }
|
47 | static get certs() {
|
48 | let filenames = this.sslCertFile.concat(this.sslCertDir);
|
49 | return filenames.map((filename) => fs.readFileSync(filename));
|
50 | }
|
51 | static agent(https, host) {
|
52 | if (!this.usingProxy(host))
|
53 | return;
|
54 | const u = https ? this.httpsProxy || this.httpProxy : this.httpProxy;
|
55 | if (u) {
|
56 | let proxyParsed = uri.parse(u);
|
57 | let tunnel = require('tunnel-agent');
|
58 | let tunnelMethod = https ? tunnel.httpsOverHttp : tunnel.httpOverHttp;
|
59 | let opts = {
|
60 | proxy: {
|
61 | host: proxyParsed.hostname,
|
62 | port: proxyParsed.port || '8080',
|
63 | },
|
64 | };
|
65 | if (proxyParsed.auth) {
|
66 | opts.proxy.proxyAuth = proxyParsed.auth;
|
67 | }
|
68 | if (this.certs.length > 0) {
|
69 | opts.ca = this.certs;
|
70 | }
|
71 | let tunnelAgent = tunnelMethod(opts);
|
72 | if (https) {
|
73 | tunnelAgent.defaultPort = 443;
|
74 | }
|
75 | return tunnelAgent;
|
76 | }
|
77 | }
|
78 | }
|
79 | ProxyUtil.env = process.env;
|
80 | exports.default = ProxyUtil;
|