whistle 2.10.10 → 2.10.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/api.js +1 -83
- package/bin/whistle.js +5 -0
- package/biz/webui/cgi-bin/get-data.js +0 -1
- package/biz/webui/cgi-bin/init.js +0 -1
- package/biz/webui/cgi-bin/status.js +2 -1
- package/biz/webui/htdocs/index.html +1 -1
- package/biz/webui/htdocs/js/index.js +49 -49
- package/index.js +10 -12
- package/lib/config.js +15 -66
- package/lib/https/ca.js +1 -1
- package/lib/https/h2.js +11 -7
- package/lib/https/index.js +111 -97
- package/lib/index.js +1 -1
- package/lib/inspectors/data.js +2 -0
- package/lib/inspectors/req.js +3 -6
- package/lib/inspectors/res.js +3 -6
- package/lib/plugins/index.js +1 -1
- package/lib/plugins/proxy.js +1 -5
- package/lib/rules/index.js +2 -1
- package/lib/rules/protocols.js +2 -4
- package/lib/rules/rules.js +21 -21
- package/lib/rules/util.js +2 -4
- package/lib/service/compose-data.js +2 -1
- package/lib/service/composer.js +5 -9
- package/lib/socket-mgr.js +4 -1
- package/lib/tunnel.js +17 -13
- package/lib/util/common.js +147 -13
- package/lib/util/index.js +42 -57
- package/lib/util/perf.js +3 -2
- package/lib/util/zlib.js +13 -1
- package/package.json +3 -3
package/lib/rules/rules.js
CHANGED
|
@@ -54,10 +54,8 @@ var REG_URL_SYMBOL_RE = /^(\^+)/;
|
|
|
54
54
|
var PATTERN_FILTER_RE = /^(?:filter|ignore):\/\/(.+)\/(i)?$/;
|
|
55
55
|
var LINE_PROPS_RE = /^lineProps:\/\/(.*)$/;
|
|
56
56
|
var FILTER_RE = /^(?:excludeFilter|includeFilter):\/\/(.*)$/;
|
|
57
|
-
var PROPS_FILTER_RE =
|
|
58
|
-
|
|
59
|
-
var PURE_FILTER_RE =
|
|
60
|
-
/^(?:excludeFilter|includeFilter):\/\/(statusCode|from|env|clientIp|clientIP|clientPort|remoteAddress|remotePort|serverIp|serverIP|chance|probability|serverPort|host|re[qs](?:H(?:eaders?)?)?)[.=](.+)$/;
|
|
57
|
+
var PROPS_FILTER_RE = /^(?:filter|excludeFilter|includeFilter|ignore):\/\/(m(?:ethod)?|i(?:p)?|h(?:eader)?|env|s(?:tatusCode)?|sni|from|b(?:ody)?|clientIp|clientIP|clientPort|remoteAddress|remotePort|serverIp|serverIP|serverPort|chance|probability|host|re[qs](?:H(?:eaders?)?)?)[:=](.+)$/;
|
|
58
|
+
var PURE_FILTER_RE = /^(?:excludeFilter|includeFilter):\/\/(re[qs](?:H(?:eaders?)?)?)\.(.+)$/;
|
|
61
59
|
var PATTERN_WILD_FILTER_RE = /^(?:filter|ignore):\/\/(!)?(\*+\/)/;
|
|
62
60
|
var INLINE_RE = /^((?:filter|excludeFilter|includeFilter|ignore):\/\/)(\(.*\)|\<.*\>)$/;
|
|
63
61
|
var CHROME_PATH_RE = /^file:\/\/\/[A-Z]:\//;
|
|
@@ -900,9 +898,14 @@ function removePort(url) {
|
|
|
900
898
|
urlPath = url.substring(end);
|
|
901
899
|
url = url.substring(0, end);
|
|
902
900
|
}
|
|
903
|
-
end = url.indexOf('
|
|
901
|
+
end = url.indexOf(']', index);
|
|
904
902
|
if (end !== -1) {
|
|
905
|
-
url = url.substring(0, end);
|
|
903
|
+
url = url.substring(0, end).replace('[', '');
|
|
904
|
+
} else {
|
|
905
|
+
end = url.indexOf(':', index);
|
|
906
|
+
if (end !== -1 && url.indexOf('/', index + 1) === -1) {
|
|
907
|
+
url = url.substring(0, end);
|
|
908
|
+
}
|
|
906
909
|
}
|
|
907
910
|
return url + urlPath;
|
|
908
911
|
}
|
|
@@ -1070,11 +1073,7 @@ function resolveRuleList(req, list, vals, index, isFilter, isEnableProxy, host,
|
|
|
1070
1073
|
} else if (rule.isExact) {
|
|
1071
1074
|
matchedRes = pattern === url || pattern === curUrl;
|
|
1072
1075
|
if ((not ? !matchedRes : matchedRes) && checkFilter() && --index < 0) {
|
|
1073
|
-
if (
|
|
1074
|
-
(result = getExactRule(
|
|
1075
|
-
getRelativePath(pattern, curUrl, rule.matcher)
|
|
1076
|
-
))
|
|
1077
|
-
) {
|
|
1076
|
+
if (result = getExactRule(getRelativePath(pattern, curUrl, rule.matcher))) {
|
|
1078
1077
|
return result;
|
|
1079
1078
|
}
|
|
1080
1079
|
}
|
|
@@ -1105,7 +1104,7 @@ function resolveRuleList(req, list, vals, index, isFilter, isEnableProxy, host,
|
|
|
1105
1104
|
} else if (rule.isDomain && rule.lineProps.originUrl) {
|
|
1106
1105
|
filePath = '/';
|
|
1107
1106
|
}
|
|
1108
|
-
if (
|
|
1107
|
+
if (result = getPathRule()) {
|
|
1109
1108
|
return result;
|
|
1110
1109
|
}
|
|
1111
1110
|
}
|
|
@@ -1901,12 +1900,14 @@ function matchFilter(url, filter, req) {
|
|
|
1901
1900
|
}
|
|
1902
1901
|
|
|
1903
1902
|
var reqBody = req._reqBody;
|
|
1904
|
-
|
|
1903
|
+
var bodyPattern = filter.bodyPattern;
|
|
1904
|
+
var body = filter.body;
|
|
1905
|
+
if (bodyPattern || body) {
|
|
1905
1906
|
if (typeof reqBody !== 'string') {
|
|
1906
1907
|
return false;
|
|
1907
1908
|
}
|
|
1908
|
-
if (
|
|
1909
|
-
if (result =
|
|
1909
|
+
if (bodyPattern) {
|
|
1910
|
+
if (result = bodyPattern.exec(reqBody)) {
|
|
1910
1911
|
req._bodySubVals = [reqBody];
|
|
1911
1912
|
for (var i = 1, len = result.length; i < len; i++) {
|
|
1912
1913
|
req._bodySubVals[i] = (result[i] || '');
|
|
@@ -1914,9 +1915,7 @@ function matchFilter(url, filter, req) {
|
|
|
1914
1915
|
}
|
|
1915
1916
|
} else {
|
|
1916
1917
|
reqBody = reqBody.toLowerCase();
|
|
1917
|
-
result =
|
|
1918
|
-
reqBody.indexOf(filter.body.value) !== -1 ||
|
|
1919
|
-
reqBody.indexOf(filter.body.orgVal) !== -1;
|
|
1918
|
+
result = reqBody.indexOf(body.value) !== -1 || reqBody.indexOf(body.orgVal) !== -1;
|
|
1920
1919
|
}
|
|
1921
1920
|
|
|
1922
1921
|
return filter.not ? !result : result;
|
|
@@ -1934,13 +1933,14 @@ function matchFilter(url, filter, req) {
|
|
|
1934
1933
|
return true;
|
|
1935
1934
|
}
|
|
1936
1935
|
}
|
|
1936
|
+
var hPattern = filterVal.hPattern;
|
|
1937
1937
|
if (!value) {
|
|
1938
|
-
result = !
|
|
1938
|
+
result = !hPattern && !filterVal.value;
|
|
1939
1939
|
return true;
|
|
1940
1940
|
}
|
|
1941
1941
|
value = String(value);
|
|
1942
|
-
if (
|
|
1943
|
-
result =
|
|
1942
|
+
if (hPattern) {
|
|
1943
|
+
result = hPattern.test(value);
|
|
1944
1944
|
} else {
|
|
1945
1945
|
value = value.toLowerCase();
|
|
1946
1946
|
result =
|
package/lib/rules/util.js
CHANGED
|
@@ -72,18 +72,16 @@ if (config.networkMode && !config.shadowRulesMode) {
|
|
|
72
72
|
if (text) {
|
|
73
73
|
var file = (type || 'File: ') + name;
|
|
74
74
|
if (backRulesFirst && !type) {
|
|
75
|
-
text = resolveRemoteRules(reverseRules(text, file), file);
|
|
76
75
|
rulesList.unshift({
|
|
77
76
|
resolved: true,
|
|
78
77
|
file: file,
|
|
79
|
-
text: text
|
|
78
|
+
text: resolveRemoteRules(reverseRules(text, file), file)
|
|
80
79
|
});
|
|
81
80
|
} else {
|
|
82
|
-
text = resolveRemoteRules(handleInlineValues(text, file), file);
|
|
83
81
|
rulesList.push({
|
|
84
82
|
resolved: true,
|
|
85
83
|
file: file,
|
|
86
|
-
text: text
|
|
84
|
+
text: resolveRemoteRules(handleInlineValues(text, file), file)
|
|
87
85
|
});
|
|
88
86
|
}
|
|
89
87
|
}
|
|
@@ -22,8 +22,9 @@ exports = module.exports = function(req, res) {
|
|
|
22
22
|
} else {
|
|
23
23
|
cache.set(reqId, '');
|
|
24
24
|
}
|
|
25
|
+
curBuf = curBuf.toString('base64');
|
|
25
26
|
}
|
|
26
|
-
result[reqId] = curBuf
|
|
27
|
+
result[reqId] = curBuf;
|
|
27
28
|
});
|
|
28
29
|
res.json(result);
|
|
29
30
|
};
|
package/lib/service/composer.js
CHANGED
|
@@ -149,15 +149,10 @@ function handleConnect(options, cb, count, testId) {
|
|
|
149
149
|
options = extend({}, origOpts);
|
|
150
150
|
}
|
|
151
151
|
common.connectInner({
|
|
152
|
-
host: options.hostname,
|
|
153
|
-
port: options.port || 443,
|
|
154
152
|
proxyHost: PROXY_OPTS.host,
|
|
155
153
|
proxyPort: PROXY_OPTS.port,
|
|
156
154
|
headers: options.headers
|
|
157
|
-
}, function(socket, svrRes
|
|
158
|
-
if (err) {
|
|
159
|
-
return handleCallback(execCb, err, null, testId);
|
|
160
|
-
}
|
|
155
|
+
}, function(socket, svrRes) {
|
|
161
156
|
if (TLS_PROTOS.indexOf(options.protocol) !== -1) {
|
|
162
157
|
socket = tls.connect({
|
|
163
158
|
rejectUnauthorized: config.rejectUnauthorized,
|
|
@@ -171,7 +166,7 @@ function handleConnect(options, cb, count, testId) {
|
|
|
171
166
|
socket.write(data);
|
|
172
167
|
options.body = data = null;
|
|
173
168
|
}
|
|
174
|
-
handleCallback(execCb,
|
|
169
|
+
handleCallback(execCb, null, svrRes, testId);
|
|
175
170
|
}, config).on('error', execCb || noop);
|
|
176
171
|
}
|
|
177
172
|
}
|
|
@@ -277,8 +272,9 @@ function handleHttp(options, cb, count, reqId, testId) {
|
|
|
277
272
|
}
|
|
278
273
|
clearTimeout(timer);
|
|
279
274
|
var headers = svrRes.headers;
|
|
280
|
-
|
|
281
|
-
|
|
275
|
+
var trailer = headers.trailer;
|
|
276
|
+
if (typeof trailer === 'string' && trailer.indexOf(',') !== -1) {
|
|
277
|
+
headers.trailer = trailer.split(',');
|
|
282
278
|
}
|
|
283
279
|
var result = {
|
|
284
280
|
statusCode: svrRes.statusCode,
|
package/lib/socket-mgr.js
CHANGED
|
@@ -657,6 +657,9 @@ function getFrameSender(options, toServer) {
|
|
|
657
657
|
function getFrameCtx(options, callback) {
|
|
658
658
|
var req = options.req;
|
|
659
659
|
rules.getFrameScriptCtx(req.rules.frameScript, req, options.res, {
|
|
660
|
+
destroy: function() {
|
|
661
|
+
destroy(options.reqId);
|
|
662
|
+
},
|
|
660
663
|
sendToClient: getFrameSender(options),
|
|
661
664
|
sendToServer: getFrameSender(options, true)
|
|
662
665
|
}, callback);
|
|
@@ -703,7 +706,7 @@ exports.handleUpgrade = function (req, res) {
|
|
|
703
706
|
resTrans.on('error', emitError);
|
|
704
707
|
res.headers = res.headers || {};
|
|
705
708
|
req.wsExts = res.headers['sec-websocket-extensions'] || '';
|
|
706
|
-
var options = { req: req, res: res };
|
|
709
|
+
var options = { req: req, res: res, reqId: reqId };
|
|
707
710
|
pluginMgr.getWsPipe(
|
|
708
711
|
req,
|
|
709
712
|
res,
|
package/lib/tunnel.js
CHANGED
|
@@ -60,13 +60,13 @@ function tunnelProxy(server, proxy, type) {
|
|
|
60
60
|
var _parseUrl = function (_url, port) {
|
|
61
61
|
_url = _url || tunnelUrl;
|
|
62
62
|
options = req.options = parseUrl(_url);
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (
|
|
67
|
-
options.host
|
|
68
|
-
} else if (net.isIPv6(
|
|
69
|
-
options.host = '[' +
|
|
63
|
+
if (!options.port) {
|
|
64
|
+
var curHost = options.host || '';
|
|
65
|
+
options.port = port || 443;
|
|
66
|
+
if (curHost.indexOf(':') === -1) {
|
|
67
|
+
options.host = curHost + ':' + options.port;
|
|
68
|
+
} else if (net.isIPv6(curHost)) {
|
|
69
|
+
options.host = '[' + curHost + ']:' + options.port;
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
return options;
|
|
@@ -206,10 +206,9 @@ function tunnelProxy(server, proxy, type) {
|
|
|
206
206
|
if (!isIPHost || !hasCustomCerts()) {
|
|
207
207
|
return false;
|
|
208
208
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
return reqSocket.useProxifier;
|
|
209
|
+
var useProxifier = (config.proxifier2 || (config.proxifier && isWebPort)) && !disable.proxifier;
|
|
210
|
+
reqSocket.useProxifier = useProxifier;
|
|
211
|
+
return useProxifier;
|
|
213
212
|
};
|
|
214
213
|
var resolvedPlugin;
|
|
215
214
|
if (isIntercept()) {
|
|
@@ -248,14 +247,19 @@ function tunnelProxy(server, proxy, type) {
|
|
|
248
247
|
pluginMgr.getTunnelRules(req, function (_rulesMgr) {
|
|
249
248
|
handlePluginRules(_rulesMgr);
|
|
250
249
|
policy = headers[config.WHISTLE_POLICY_HEADER];
|
|
250
|
+
var useSniAsTunnelDomain = util.isEnable(req, 'useSniAsTunnelDomain');
|
|
251
|
+
var noCapture;
|
|
251
252
|
if (!reqSocket._hasError && !req._authForbidden &&
|
|
252
|
-
!req.fromComposer && (req._forceCapture || isIntercept())) {
|
|
253
|
+
!req.fromComposer && (req._forceCapture || isIntercept() || (noCapture = useSniAsTunnelDomain))) {
|
|
253
254
|
reqSocket.rulesHeaders = req.rulesHeaders;
|
|
254
255
|
reqSocket.enable = req.enable;
|
|
255
256
|
reqSocket.disable = req.disable;
|
|
256
257
|
reqSocket.tunnelHostname = hostname;
|
|
258
|
+
reqSocket.tunnelPort = options.port;
|
|
257
259
|
reqSocket.rules = _rules;
|
|
258
260
|
reqSocket._pluginVars = req._pluginVars;
|
|
261
|
+
reqSocket._noCaptureW2 = noCapture;
|
|
262
|
+
reqSocket._useSniAsTunnelDomain = useSniAsTunnelDomain;
|
|
259
263
|
dispatch(
|
|
260
264
|
reqSocket,
|
|
261
265
|
function (chunk) {
|
|
@@ -423,7 +427,7 @@ function tunnelProxy(server, proxy, type) {
|
|
|
423
427
|
if (TUNNEL_RE.test(realUrl) && realUrl != tunnelUrl) {
|
|
424
428
|
_parseUrl(realUrl, isHttp ? 80 : 443);
|
|
425
429
|
tunnelUrl = 'tunnel://' + options.host;
|
|
426
|
-
data.realUrl =
|
|
430
|
+
data.realUrl = options.host;
|
|
427
431
|
}
|
|
428
432
|
}
|
|
429
433
|
originPort = options.port;
|
package/lib/util/common.js
CHANGED
|
@@ -62,6 +62,7 @@ var AUTH_RE = /^Basic /i;
|
|
|
62
62
|
var STREAM_OPTS = { highWaterMark: 1 };
|
|
63
63
|
var REAL_HOST_HEADER = 'x-whistle-real-host';
|
|
64
64
|
var supportsBr = zlib.createBrotliDecompress && zlib.createBrotliCompress;
|
|
65
|
+
var supportsZstd = zlib.createZstdDecompress && zlib.createZstdCompress;
|
|
65
66
|
var TIMEOUT_ERR = new Error('Timeout');
|
|
66
67
|
var noop = function () {};
|
|
67
68
|
var PATH_SEP_RE = /[\\/]/;
|
|
@@ -74,6 +75,13 @@ var PROP_SEP_RE = /(\\*)([|&]|\\[stnrfv])/g;
|
|
|
74
75
|
var TEMP_FILE_RE = /^(?:\/?temp\/)?([\da-f]{64})(?:\.[\w.-]+)?$/;
|
|
75
76
|
var TEMP_FILES_PATH = path.join(getWhistlePath(), 'temp_files');
|
|
76
77
|
var SAVED_SESSIONS_FILE_RE = /_([1-9]\d*)_(\d+)$/;
|
|
78
|
+
var REG_EXP_RE = /^\/(.+)\/(i?u?|ui)$/;
|
|
79
|
+
|
|
80
|
+
function isRegExp(regExp) {
|
|
81
|
+
return REG_EXP_RE.test(regExp);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
exports.isRegExp = isRegExp;
|
|
77
85
|
|
|
78
86
|
exports.getSavedFileItem = function(filename) {
|
|
79
87
|
var result = SAVED_SESSIONS_FILE_RE.exec(filename);
|
|
@@ -160,6 +168,7 @@ exports.CONN_TIMEOUT = CONN_TIMEOUT;
|
|
|
160
168
|
exports.TGZ_FILE_NAME_RE = /^(?:[\w.~-]+-)?whistle\.[a-z\d_-]+-\d+\.\d+\.\d+[\w.-]*\.(?:tgz|tar(?:\.gz)?)$/;
|
|
161
169
|
exports.parseUrl = parseUrl;
|
|
162
170
|
exports.supportsBr = supportsBr;
|
|
171
|
+
exports.supportsZstd = supportsZstd;
|
|
163
172
|
exports.SERVICE_HOST = 'admin.weso.pro';
|
|
164
173
|
exports.REMOTE_URL_RE = REMOTE_URL_RE;
|
|
165
174
|
exports.WHISTLE_PLUGIN_RE = WHISTLE_PLUGIN_RE;
|
|
@@ -1558,7 +1567,7 @@ function getContentEncoding(headers) {
|
|
|
1558
1567
|
(headers && headers['content-encoding']) || headers
|
|
1559
1568
|
);
|
|
1560
1569
|
return encoding === 'gzip' || (supportsBr && encoding === 'br') ||
|
|
1561
|
-
encoding === 'deflate' ? encoding : null;
|
|
1570
|
+
(supportsZstd && encoding === 'zstd') || encoding === 'deflate' ? encoding : null;
|
|
1562
1571
|
}
|
|
1563
1572
|
|
|
1564
1573
|
exports.getContentEncoding = getContentEncoding;
|
|
@@ -1572,6 +1581,8 @@ function getUnzipStream(headers) {
|
|
|
1572
1581
|
return supportsBr && zlib.createBrotliDecompress();
|
|
1573
1582
|
case 'deflate':
|
|
1574
1583
|
return zlib.createInflate();
|
|
1584
|
+
case 'zstd':
|
|
1585
|
+
return supportsZstd && zlib.createZstdDecompress();
|
|
1575
1586
|
}
|
|
1576
1587
|
}
|
|
1577
1588
|
|
|
@@ -1905,16 +1916,14 @@ exports.connectInner = function (options, cb, config) {
|
|
|
1905
1916
|
auth = 'Basic ' + auth.toString('base64');
|
|
1906
1917
|
setRawHeader(headers, 'proxy-authorization', auth);
|
|
1907
1918
|
}
|
|
1919
|
+
var req;
|
|
1908
1920
|
var timer = setTimeout(function () {
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
req.destroy();
|
|
1912
|
-
}
|
|
1921
|
+
req.emit('error', TIMEOUT_ERR);
|
|
1922
|
+
req.destroy();
|
|
1913
1923
|
}, CONN_TIMEOUT);
|
|
1914
|
-
var req;
|
|
1915
1924
|
var clearTimer = function (err) {
|
|
1916
1925
|
if (timer) {
|
|
1917
|
-
err && req
|
|
1926
|
+
err && req.destroy();
|
|
1918
1927
|
clearTimeout(timer);
|
|
1919
1928
|
timer = null;
|
|
1920
1929
|
}
|
|
@@ -1929,14 +1938,15 @@ exports.connectInner = function (options, cb, config) {
|
|
|
1929
1938
|
socket.on('error', function() {
|
|
1930
1939
|
socket.destroy();
|
|
1931
1940
|
});
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
'Tunneling socket could not be established, statusCode=' +
|
|
1935
|
-
res.statusCode
|
|
1936
|
-
);
|
|
1937
|
-
err.statusCode = res.statusCode;
|
|
1941
|
+
var status = res.statusCode;
|
|
1942
|
+
if (status !== 200) {
|
|
1938
1943
|
socket.destroy();
|
|
1939
1944
|
process.nextTick(function () {
|
|
1945
|
+
var err = new Error(
|
|
1946
|
+
'Tunneling socket could not be established, statusCode=' +
|
|
1947
|
+
status
|
|
1948
|
+
);
|
|
1949
|
+
err.statusCode = status;
|
|
1940
1950
|
req.emit('error', err);
|
|
1941
1951
|
});
|
|
1942
1952
|
return;
|
|
@@ -2135,3 +2145,127 @@ function getHttpMeta(f1, f2, f3, headers, rawHeaders) {
|
|
|
2135
2145
|
}
|
|
2136
2146
|
|
|
2137
2147
|
exports.getHttpMeta = getHttpMeta;
|
|
2148
|
+
|
|
2149
|
+
|
|
2150
|
+
var PATTERN_FIELDS = ['url', 'm', 'method', 'reqH', 'reqHeader', 'reqHeaders',
|
|
2151
|
+
'b', 'body', 'reqBody', 's', 'statusCode', 'status', 'resH', 'resHeader', 'resHeaders'];
|
|
2152
|
+
|
|
2153
|
+
function getHeaderKey(key) {
|
|
2154
|
+
return !key || typeof key !== 'string' || /[^\w!#$%&'*+.^`|~-]/.test(key) ? '' : key.toLowerCase();
|
|
2155
|
+
}
|
|
2156
|
+
|
|
2157
|
+
function createFilter(item, name, key) {
|
|
2158
|
+
var protocol = (item.exclude ? 'ex' : 'in') + 'cludeFilter://';
|
|
2159
|
+
name = name ? name + (key ? '.' : ':') : '';
|
|
2160
|
+
key = key ? key + ':' : '';
|
|
2161
|
+
return protocol + name + key + item.value;
|
|
2162
|
+
}
|
|
2163
|
+
|
|
2164
|
+
function wrapRegexp(value, item) {
|
|
2165
|
+
var flags = item.ignoreCase || item.matchCase === false ? 'i' : '';
|
|
2166
|
+
return { value: '/' + value.replace(/\//g, '\\$&') + '/' + flags, exclude: item.exclude};
|
|
2167
|
+
}
|
|
2168
|
+
|
|
2169
|
+
function formatPath(url) {
|
|
2170
|
+
if (/^\^?\/\//.test(url)) {
|
|
2171
|
+
return url;
|
|
2172
|
+
}
|
|
2173
|
+
return url.replace(/^(\^?(?:https?:\/\/|wss?:\/\/)?)([/?.])/, function(all, prefix, flag) {
|
|
2174
|
+
return flag === '/' ? prefix + '**/' : (prefix[0] === '^' ? prefix : '^' + prefix) + '**/**' + flag;
|
|
2175
|
+
});
|
|
2176
|
+
}
|
|
2177
|
+
|
|
2178
|
+
function formatUrlPattern(item) {
|
|
2179
|
+
var value = item.value;
|
|
2180
|
+
var type = item.type;
|
|
2181
|
+
var prefix;
|
|
2182
|
+
if (/^\s*(keyword|contains?)\s*$/i.test(type)) {
|
|
2183
|
+
return wrapRegexp(item.value.replace(/[|/\\{}()[\]^$+?*.]/g, '\\$&'), item);
|
|
2184
|
+
}
|
|
2185
|
+
if (/^\s*regexp?\s*$/i.test(type)) {
|
|
2186
|
+
return wrapRegexp(item.value.replace(/\//g, '\\$&'), item);
|
|
2187
|
+
}
|
|
2188
|
+
if (/^\s*(wildcard|star)\s*$/i.test(type)) {
|
|
2189
|
+
return { value: formatPath(item.value.replace(/^\^*/, '^')), exclude: item.exclude };
|
|
2190
|
+
}
|
|
2191
|
+
if (value[0] === '?') {
|
|
2192
|
+
prefix = '^**/**';
|
|
2193
|
+
} else if (value[0] !== '/' || value[1] === '/' || isRegExp(value)) {
|
|
2194
|
+
return { value: formatPath(value) };
|
|
2195
|
+
}
|
|
2196
|
+
return { value: (prefix || '**') + value };
|
|
2197
|
+
}
|
|
2198
|
+
|
|
2199
|
+
function createPattern(list) {
|
|
2200
|
+
if (!list) {
|
|
2201
|
+
return '';
|
|
2202
|
+
}
|
|
2203
|
+
if (!Array.isArray(list)) {
|
|
2204
|
+
list = [list];
|
|
2205
|
+
}
|
|
2206
|
+
var len = list.length;
|
|
2207
|
+
if (!len) {
|
|
2208
|
+
return '';
|
|
2209
|
+
}
|
|
2210
|
+
var result = [];
|
|
2211
|
+
var urlPattern;
|
|
2212
|
+
var key;
|
|
2213
|
+
var add = function(item, name, key) {
|
|
2214
|
+
var p = createFilter(item, name, key);
|
|
2215
|
+
if (result.indexOf(p) === -1) {
|
|
2216
|
+
result.push(p);
|
|
2217
|
+
}
|
|
2218
|
+
};
|
|
2219
|
+
list.forEach(function(item) {
|
|
2220
|
+
if (!item || !item.value) {
|
|
2221
|
+
return;
|
|
2222
|
+
}
|
|
2223
|
+
var name = item.name || 'url';
|
|
2224
|
+
if (PATTERN_FIELDS.indexOf(name) === -1) {
|
|
2225
|
+
return;
|
|
2226
|
+
}
|
|
2227
|
+
if (typeof item.value !== 'string') {
|
|
2228
|
+
item.value = String(item.value);
|
|
2229
|
+
}
|
|
2230
|
+
if (/\s/.test(item.value)) {
|
|
2231
|
+
return;
|
|
2232
|
+
}
|
|
2233
|
+
switch(name) {
|
|
2234
|
+
case 'url':
|
|
2235
|
+
item = formatUrlPattern(item);
|
|
2236
|
+
if (urlPattern || item.exclude) {
|
|
2237
|
+
add(item);
|
|
2238
|
+
} else {
|
|
2239
|
+
urlPattern = item.value;
|
|
2240
|
+
}
|
|
2241
|
+
return;
|
|
2242
|
+
case 'm':
|
|
2243
|
+
case 'method':
|
|
2244
|
+
return add(item, 'm');
|
|
2245
|
+
case 'reqH':
|
|
2246
|
+
case 'reqHeader':
|
|
2247
|
+
case 'reqHeaders':
|
|
2248
|
+
key = getHeaderKey(item.key);
|
|
2249
|
+
return key && add(item, 'reqH', key);
|
|
2250
|
+
case 'b':
|
|
2251
|
+
case 'body':
|
|
2252
|
+
case 'reqBody':
|
|
2253
|
+
return add(item, 'b');
|
|
2254
|
+
case 's':
|
|
2255
|
+
case 'statusCode':
|
|
2256
|
+
case 'status':
|
|
2257
|
+
return add(item, 's');
|
|
2258
|
+
case 'resH':
|
|
2259
|
+
case 'resHeader':
|
|
2260
|
+
case 'resHeaders':
|
|
2261
|
+
key = getHeaderKey(item.key);
|
|
2262
|
+
return key && add(item, 'resH', key);
|
|
2263
|
+
}
|
|
2264
|
+
});
|
|
2265
|
+
if (urlPattern || result.length) {
|
|
2266
|
+
result.unshift(urlPattern || '*');
|
|
2267
|
+
}
|
|
2268
|
+
return result.join(' ');
|
|
2269
|
+
}
|
|
2270
|
+
|
|
2271
|
+
exports.createPattern = createPattern;
|