1 |
|
2 |
|
3 | import { __extends } from "tslib";
|
4 | import { BaseRequestPolicy, } from "./requestPolicy";
|
5 | import { Constants } from "../util/constants";
|
6 | import { URLBuilder } from "../url";
|
7 |
|
8 |
|
9 |
|
10 | export var noProxyList = loadNoProxy();
|
11 | var byPassedList = new Map();
|
12 |
|
13 |
|
14 |
|
15 | export function getEnvironmentValue(name) {
|
16 | if (process.env[name]) {
|
17 | return process.env[name];
|
18 | }
|
19 | else if (process.env[name.toLowerCase()]) {
|
20 | return process.env[name.toLowerCase()];
|
21 | }
|
22 | return undefined;
|
23 | }
|
24 | function loadEnvironmentProxyValue() {
|
25 | if (!process) {
|
26 | return undefined;
|
27 | }
|
28 | var httpsProxy = getEnvironmentValue(Constants.HTTPS_PROXY);
|
29 | var allProxy = getEnvironmentValue(Constants.ALL_PROXY);
|
30 | var httpProxy = getEnvironmentValue(Constants.HTTP_PROXY);
|
31 | return httpsProxy || allProxy || httpProxy;
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 | function isBypassed(uri) {
|
37 | if (noProxyList.length === 0) {
|
38 | return false;
|
39 | }
|
40 | var host = URLBuilder.parse(uri).getHost();
|
41 | if (byPassedList.has(host)) {
|
42 | return byPassedList.get(host);
|
43 | }
|
44 | var isBypassedFlag = false;
|
45 | for (var _i = 0, noProxyList_1 = noProxyList; _i < noProxyList_1.length; _i++) {
|
46 | var pattern = noProxyList_1[_i];
|
47 | if (pattern[0] === ".") {
|
48 |
|
49 |
|
50 | if (host.endsWith(pattern)) {
|
51 | isBypassedFlag = true;
|
52 | }
|
53 | else {
|
54 | if (host.length === pattern.length - 1 && host === pattern.slice(1)) {
|
55 | isBypassedFlag = true;
|
56 | }
|
57 | }
|
58 | }
|
59 | else {
|
60 | if (host === pattern) {
|
61 | isBypassedFlag = true;
|
62 | }
|
63 | }
|
64 | }
|
65 | byPassedList.set(host, isBypassedFlag);
|
66 | return isBypassedFlag;
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 | export function loadNoProxy() {
|
72 | var noProxy = getEnvironmentValue(Constants.NO_PROXY);
|
73 | if (noProxy) {
|
74 | return noProxy
|
75 | .split(",")
|
76 | .map(function (item) { return item.trim(); })
|
77 | .filter(function (item) { return item.length; });
|
78 | }
|
79 | return [];
|
80 | }
|
81 |
|
82 |
|
83 |
|
84 | function extractAuthFromUrl(url) {
|
85 | var atIndex = url.indexOf("@");
|
86 | if (atIndex === -1) {
|
87 | return { urlWithoutAuth: url };
|
88 | }
|
89 | var schemeIndex = url.indexOf("://");
|
90 | var authStart = schemeIndex !== -1 ? schemeIndex + 3 : 0;
|
91 | var auth = url.substring(authStart, atIndex);
|
92 | var colonIndex = auth.indexOf(":");
|
93 | var hasPassword = colonIndex !== -1;
|
94 | var username = hasPassword ? auth.substring(0, colonIndex) : auth;
|
95 | var password = hasPassword ? auth.substring(colonIndex + 1) : undefined;
|
96 | var urlWithoutAuth = url.substring(0, authStart) + url.substring(atIndex + 1);
|
97 | return {
|
98 | username: username,
|
99 | password: password,
|
100 | urlWithoutAuth: urlWithoutAuth,
|
101 | };
|
102 | }
|
103 | export function getDefaultProxySettings(proxyUrl) {
|
104 | if (!proxyUrl) {
|
105 | proxyUrl = loadEnvironmentProxyValue();
|
106 | if (!proxyUrl) {
|
107 | return undefined;
|
108 | }
|
109 | }
|
110 | var _a = extractAuthFromUrl(proxyUrl), username = _a.username, password = _a.password, urlWithoutAuth = _a.urlWithoutAuth;
|
111 | var parsedUrl = URLBuilder.parse(urlWithoutAuth);
|
112 | var schema = parsedUrl.getScheme() ? parsedUrl.getScheme() + "://" : "";
|
113 | return {
|
114 | host: schema + parsedUrl.getHost(),
|
115 | port: Number.parseInt(parsedUrl.getPort() || "80"),
|
116 | username: username,
|
117 | password: password,
|
118 | };
|
119 | }
|
120 | export function proxyPolicy(proxySettings) {
|
121 | if (!proxySettings) {
|
122 | proxySettings = getDefaultProxySettings();
|
123 | }
|
124 | return {
|
125 | create: function (nextPolicy, options) {
|
126 | return new ProxyPolicy(nextPolicy, options, proxySettings);
|
127 | },
|
128 | };
|
129 | }
|
130 | var ProxyPolicy = (function (_super) {
|
131 | __extends(ProxyPolicy, _super);
|
132 | function ProxyPolicy(nextPolicy, options, proxySettings) {
|
133 | var _this = _super.call(this, nextPolicy, options) || this;
|
134 | _this.proxySettings = proxySettings;
|
135 | return _this;
|
136 | }
|
137 | ProxyPolicy.prototype.sendRequest = function (request) {
|
138 | if (!request.proxySettings && !isBypassed(request.url)) {
|
139 | request.proxySettings = this.proxySettings;
|
140 | }
|
141 | return this._nextPolicy.sendRequest(request);
|
142 | };
|
143 | return ProxyPolicy;
|
144 | }(BaseRequestPolicy));
|
145 | export { ProxyPolicy };
|
146 |
|
\ | No newline at end of file |