whistle 2.10.12 → 2.10.14

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.
@@ -1089,7 +1089,7 @@ function getOptions(req, res, type) {
1089
1089
  options.protocol = 'http:';
1090
1090
  options.host = LOCALHOST;
1091
1091
  options.hostname = null;
1092
- options.agent = false;
1092
+ options.agent = config.localAgent;
1093
1093
 
1094
1094
  return options;
1095
1095
  }
@@ -100,6 +100,14 @@ var mIndex = 0;
100
100
  var mNow = Date.now();
101
101
  var IS_JSON = Symbol('isJson');
102
102
  var removeComment = common.removeComment;
103
+ var ANDROID_RE = /\bAndroid\b/i;
104
+ var IOS_RE = /\b(iPhone|iPad|iPod)\b/i;
105
+ var MAC_RE = /\bMacintosh\b/i;
106
+
107
+ function checkUA(req, re) {
108
+ req = req.headers && req.headers['user-agent'];
109
+ return req && re.test(req);
110
+ }
103
111
 
104
112
  function getMFlag() {
105
113
  if (mIndex === Number.MAX_SAFE_INTEGER) {
@@ -1830,31 +1838,41 @@ function matchFilter(url, filter, req) {
1830
1838
  return true;
1831
1839
  }
1832
1840
  };
1833
- if (filter.from) {
1834
- if (filter.from === 'tunnel') {
1841
+ var from = filter.from;
1842
+ if (from) {
1843
+ if (from === 'tunnel') {
1835
1844
  return filter.not ? !req.fromTunnel : req.fromTunnel;
1836
1845
  }
1837
- if (filter.from === 'test') {
1846
+ if (from === 'test') {
1838
1847
  return filter.not ? !req._testId : req._testId;
1839
1848
  }
1840
- if (filter.from === 'composer') {
1849
+ if (from === 'composer') {
1841
1850
  return filter.not ? !req.fromComposer : req.fromComposer;
1842
1851
  }
1843
- if (filter.from === 'internalPath') {
1852
+ if (from === 'internalPath') {
1844
1853
  return filter.not ? !req.fromInternalPath : req.fromInternalPath;
1845
1854
  }
1846
- if (filter.from === 'sni') {
1855
+ if (from === 'sni') {
1847
1856
  return filter.not ? !req.useSNI : req.useSNI;
1848
1857
  }
1849
- if (filter.from === 'httpserver') {
1858
+ if (from === 'httpserver') {
1850
1859
  return filter.not ? !req.fromHttpServer : req.fromHttpServer;
1851
1860
  }
1852
- if (filter.from === 'httpsserver') {
1861
+ if (from === 'httpsserver') {
1853
1862
  return filter.not ? !req.fromHttpsServer : req.fromHttpsServer;
1854
1863
  }
1855
- if (filter.from === 'httpsport') {
1864
+ if (from === 'httpsport') {
1856
1865
  return filter.not ? !req.isHttpsServer : req.isHttpsServer;
1857
1866
  }
1867
+ if (from === 'android') {
1868
+ return filter.not ? !checkUA(req, ANDROID_RE) : checkUA(req, ANDROID_RE);
1869
+ }
1870
+ if (from === 'ios' || from === 'iphone') {
1871
+ return filter.not ? !checkUA(req, IOS_RE) : checkUA(req, IOS_RE);
1872
+ }
1873
+ if (from === 'macos' || from === 'mac') {
1874
+ return filter.not ? !checkUA(req, MAC_RE) : checkUA(req, MAC_RE);
1875
+ }
1858
1876
  return false;
1859
1877
  }
1860
1878
  var chance = filter.chance || filter.probability;
@@ -141,13 +141,9 @@ function handleConnect(options, cb, count, testId) {
141
141
  options.headers['x-whistle-policy'] = 'tunnel';
142
142
  var origOpts = options;
143
143
  var lastIndex = count - 1;
144
- for (var i = 0; i < count; i++) {
145
- var execCb;
146
- if (i === lastIndex) {
147
- execCb = cb;
148
- } else {
149
- options = extend({}, origOpts);
150
- }
144
+ // Each repetition gets its own options and callback: the callbacks run after the
145
+ // loop, and the sent body is cleared on the options object it was sent from.
146
+ var send = function(options, execCb) {
151
147
  common.connectInner({
152
148
  proxyHost: PROXY_OPTS.host,
153
149
  proxyPort: PROXY_OPTS.port,
@@ -168,6 +164,13 @@ function handleConnect(options, cb, count, testId) {
168
164
  }
169
165
  handleCallback(execCb, null, svrRes, testId);
170
166
  }, config).on('error', execCb || noop);
167
+ };
168
+ for (var i = 0; i < count; i++) {
169
+ if (i === lastIndex) {
170
+ send(origOpts, cb);
171
+ } else {
172
+ send(extend({}, origOpts));
173
+ }
171
174
  }
172
175
  }
173
176
 
@@ -180,13 +183,9 @@ function handleWebSocket(options, cb, count, testId) {
180
183
  delete options.headers['x-whistle-frame-binary'];
181
184
  var origOpts = options;
182
185
  var lastIndex = count - 1;
183
- for (var i = 0; i < count; i++) {
184
- var execCb;
185
- if (i === lastIndex) {
186
- execCb = cb;
187
- } else {
188
- options = extend({}, origOpts);
189
- }
186
+ // Each repetition gets its own options and callback: the callbacks run after the
187
+ // loop, and the sent body is cleared on the options object it was sent from.
188
+ var send = function(options, execCb) {
190
189
  common.connect(PROXY_OPTS, function(err, socket) {
191
190
  if (err) {
192
191
  handleCallback(execCb, err, null, testId);
@@ -223,6 +222,13 @@ function handleWebSocket(options, cb, count, testId) {
223
222
  }, true);
224
223
  }
225
224
  });
225
+ };
226
+ for (var i = 0; i < count; i++) {
227
+ if (i === lastIndex) {
228
+ send(origOpts, cb);
229
+ } else {
230
+ send(extend({}, origOpts));
231
+ }
226
232
  }
227
233
  }
228
234
 
@@ -237,13 +243,9 @@ function handleHttp(options, cb, count, reqId, testId) {
237
243
  options.port = PROXY_OPTS.port;
238
244
  var origOpts = options;
239
245
  var lastIndex = count - 1;
240
- for (var i = 0; i < count; i++) {
241
- var execCb;
242
- if (i === lastIndex) {
243
- execCb = cb;
244
- } else {
245
- options = extend({}, origOpts);
246
- }
246
+ // Each repetition gets its own options and callback: the callbacks run after the
247
+ // loop, and the sent body is cleared on the options object it was sent from.
248
+ var send = function(options, execCb) {
247
249
  var client = http.request(options, function(svrRes) {
248
250
  if (!execCb || testId) {
249
251
  handleCallback(execCb, null, svrRes, testId);
@@ -325,6 +327,13 @@ function handleHttp(options, cb, count, reqId, testId) {
325
327
  client.on('error', execCb || noop);
326
328
  client.end(options.body);
327
329
  options.body = null;
330
+ };
331
+ for (var i = 0; i < count; i++) {
332
+ if (i === lastIndex) {
333
+ send(origOpts, cb);
334
+ } else {
335
+ send(extend({}, origOpts));
336
+ }
328
337
  }
329
338
  }
330
339
 
@@ -369,6 +378,9 @@ exports.handleRequest = function(req, res) {
369
378
  }
370
379
  headers['x-whistle-rule-value'] = common.safeEncodeURIComponent(rules);
371
380
  }
381
+ if (common.isString(options.auth)) {
382
+ headers.authorization = 'Basic ' + Buffer.from(options.auth).toString('base64');
383
+ }
372
384
  var type = common.trimStr(reqBody.contentType);
373
385
  if (type) {
374
386
  headers['content-type'] = type;
@@ -420,7 +420,6 @@ function sessionsHandler() {
420
420
  });
421
421
  app.post('/cgi-bin/composer', jsonParser, composer.handleRequest);
422
422
  app.get('/cgi-bin/compose-data', handleComposeData);
423
- '';
424
423
  app.get('/cgi-bin/saved/list', function(req, res) {
425
424
  util.getSavedList(SAVED_SESSIONS_PATH, function(err, list) {
426
425
  if (err) {
package/lib/tunnel.js CHANGED
@@ -92,6 +92,7 @@ function tunnelProxy(server, proxy, type) {
92
92
  reqSocket.fromComposer = req.fromComposer;
93
93
  reqSocket._testId = req._testId;
94
94
  req.isPluginReq = reqSocket.isPluginReq = util.checkPluginReqOnce(req, true);
95
+ util.addClientIp(req.clientIp);
95
96
  util.onSocketEnd(reqSocket, function (err) {
96
97
  // 处理可能的证书校验出错
97
98
  if (config.captureData && !util.checkHideProp(req.enable || '', req.disable || '', 'CaptureError') && dispatch.getTunnelDataOnce(reqSocket)) {
@@ -726,28 +727,28 @@ function tunnelProxy(server, proxy, type) {
726
727
  }
727
728
 
728
729
  function sendEstablished(code, cb) {
730
+ var resHeaders;
729
731
  if (res) {
732
+ resHeaders = res.headers;
730
733
  code = res.statusCode || 200;
731
- if (!res.headers['proxy-agent']) {
732
- res.headers['proxy-agent'] = config.appName;
734
+ if (!resHeaders['proxy-agent']) {
735
+ resHeaders['proxy-agent'] = config.appName;
733
736
  res.rawHeaders = res.rawHeaders || [];
734
737
  res.rawHeaders.push('proxy-agent', 'Proxy-Agent');
735
738
  }
736
739
  } else {
737
740
  code = code || 200;
741
+ resHeaders = { 'proxy-agent': config.appName };
738
742
  res = {
739
743
  statusCode: code,
740
- headers: {
741
- 'proxy-agent': config.appName
742
- },
744
+ headers: resHeaders,
743
745
  rawHeaders: ['proxy-agent', 'Proxy-Agent']
744
746
  };
745
747
  }
746
748
  var tunnelAck = enableTunnelAck && cb && code == 200;
747
749
  if (tunnelAck) {
748
- res.headers[common.ALLOW_ACK] = 1;
750
+ resHeaders[common.ALLOW_ACK] = 1;
749
751
  }
750
- var resHeaders = res.headers;
751
752
  pluginMgr.getResRules(req, res, function () {
752
753
  if (util.needAbortRes(req)) {
753
754
  return reqSocket.destroy();
package/lib/upgrade.js CHANGED
@@ -129,6 +129,7 @@ function upgradeHandler(req, socket) {
129
129
  var clientInfo = util.parseClientInfo(req);
130
130
  var clientIp = clientInfo[0] || util.getClientIp(req);
131
131
  var clientPort = clientInfo[1] || util.getClientPort(req);
132
+ util.addClientIp(clientIp);
132
133
  socket._disabledProxyRules = req._disabledProxyRules;
133
134
  socket.clientIp = clientIp;
134
135
  socket.clientPort = clientPort;
@@ -896,13 +896,15 @@ module.exports = function init(_proxy) {
896
896
  var data = {};
897
897
  var count = options.count;
898
898
  var startTime = options.startTime;
899
+ var dumpCount = options.dumpCount;
900
+ var hasDump = dumpCount > 0;
899
901
  var clearNetwork =
900
- !(options.dumpCount > 0) && (count == 0 || startTime == -2);
902
+ !hasDump && (count == 0 || startTime == -2);
901
903
  if (!clearNetwork) {
902
904
  count = count > 0 && count < COUNT ? +count : COUNT;
903
- if (options.dumpCount > 0) {
905
+ if (hasDump) {
904
906
  var len = ids.length;
905
- startTime = ids[len > options.dumpCount ? len - options.dumpCount : 0];
907
+ startTime = ids[len > dumpCount ? len - dumpCount : 0];
906
908
  }
907
909
  }
908
910
  if (key && value) {
package/lib/util/drain.js CHANGED
@@ -1,13 +1,23 @@
1
1
  var common = require('./common');
2
2
 
3
+ var createTransform = common.createTransform;
3
4
  var noop = common.noop;
4
- var onReadEnd = common.onReadEnd;
5
5
 
6
6
  module.exports = function (stream, endHandler) {
7
7
  if (stream._hasAlreadyDrain || (!stream.noReqBody && stream.useH2)) {
8
- return typeof endHandler == 'function' && endHandler();
8
+ return endHandler && endHandler();
9
9
  }
10
10
  stream._hasAlreadyDrain = true;
11
- onReadEnd(stream, endHandler);
12
- stream.on('data', noop).on('error', noop);
11
+ // 需要跟 pipeStream 配合使用,确保流的结束事件能够被正确触发,不能直接用 common.onReadEnd
12
+ var emitEndStream = createTransform();
13
+ emitEndStream.on('data', noop).on('error', noop);
14
+ if (endHandler) {
15
+ // 204 + pipe 可能不触发 end 事件,需要手动调用 endHandler
16
+ if (stream.readableEnded) {
17
+ endHandler();
18
+ } else {
19
+ emitEndStream.on('end', endHandler);
20
+ }
21
+ }
22
+ stream.pipe(emitEndStream);
13
23
  };
package/lib/util/index.js CHANGED
@@ -731,6 +731,24 @@ var simpleHostname = '';
731
731
  var cpus = os.cpus();
732
732
  var addressList = [];
733
733
  var usiTimer;
734
+ var clientIpMap = new Map();
735
+ var clientIpList;
736
+
737
+ exports.getClientIpList = function() {
738
+ clientIpList = clientIpList || Array.from(clientIpMap.keys());
739
+ return clientIpList;
740
+ };
741
+
742
+ exports.addClientIp = function(ip) {
743
+ if (clientIpMap.has(ip)) {
744
+ if (clientIpMap.size === 1) {
745
+ return;
746
+ }
747
+ clientIpMap.delete(ip);
748
+ }
749
+ clientIpMap.set(ip, 1);
750
+ clientIpList = null;
751
+ };
734
752
 
735
753
  function updateServerInfo() {
736
754
  serverInfo = {ipv4: [], ipv6: []};
@@ -753,6 +771,14 @@ function updateSysInfo() {
753
771
  common.walkInterfaces(function (info) {
754
772
  addressList.push(info.address.toLowerCase());
755
773
  });
774
+ var excess = clientIpMap.size - 2000;
775
+ if (excess > 0) {
776
+ clientIpList = null;
777
+ var iter = clientIpMap.keys();
778
+ while (excess-- > 0) {
779
+ clientIpMap.delete(iter.next().value);
780
+ }
781
+ }
756
782
  usiTimer = setTimeout(updateSysInfo, 36000);
757
783
  }
758
784
 
@@ -2968,7 +2994,7 @@ exports.disableReqProps = function (req) {
2968
2994
  delete headers['user-agent'];
2969
2995
  }
2970
2996
 
2971
- if (disable.gzip || disable.zip || isEnable(req, 'captureStream')) {
2997
+ if (disable.gzip || disable.encoding || disable.zip || isEnable(req, 'captureStream')) {
2972
2998
  delete headers['accept-encoding'];
2973
2999
  }
2974
3000
 
@@ -15,6 +15,13 @@ module.exports = function (url) {
15
15
  var search = urlMatch[4] || '';
16
16
  var hash = urlMatch[5] || null;
17
17
  var port = null;
18
+ var auth = null;
19
+ // user:password@ belongs to auth, not to the host (as in url.parse)
20
+ var atIndex = host.lastIndexOf('@');
21
+ if (atIndex !== -1) {
22
+ auth = decodeAuth(host.substring(0, atIndex));
23
+ host = host.substring(atIndex + 1);
24
+ }
18
25
  var hostname = host;
19
26
  var hostMatch = HOST_RE.exec(host);
20
27
  if (hostMatch) {
@@ -25,7 +32,7 @@ module.exports = function (url) {
25
32
  return {
26
33
  protocol: protocol,
27
34
  slashes: true,
28
- auth: null,
35
+ auth: auth,
29
36
  host: host,
30
37
  port: port,
31
38
  hostname: formatHost(hostname),
@@ -38,4 +45,12 @@ module.exports = function (url) {
38
45
  };
39
46
  };
40
47
 
48
+ function decodeAuth(auth) {
49
+ try {
50
+ return decodeURIComponent(auth);
51
+ } catch (e) {
52
+ return auth;
53
+ }
54
+ }
55
+
41
56
  module.exports.formatHost = formatHost;
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "whistle",
3
3
  "description": "HTTP, HTTP2, HTTPS, Websocket debugging proxy",
4
- "version": "2.10.12",
4
+ "version": "2.10.14",
5
5
  "dataDirname": ".whistle",
6
6
  "localUIHost": "local.whistlejs.com",
7
7
  "port": 8899,
8
- "sockets": 256,
8
+ "sockets": 96,
9
9
  "timeout": 360000,
10
10
  "author": "avenwu <avwu@qq.com>",
11
11
  "contributors": [],
@@ -39,7 +39,7 @@
39
39
  "dependencies": {
40
40
  "adm-zip": "^0.6.1",
41
41
  "async-limiter": "2.0.0",
42
- "body-parser": "^1.20.6",
42
+ "body-parser": "^1.20.9",
43
43
  "colors": "1.1.2",
44
44
  "express": "^4.21.2",
45
45
  "extend": "^3.0.2",
@@ -51,7 +51,7 @@
51
51
  "lru-cache": "^4.1.1",
52
52
  "mime": "^1.6.0",
53
53
  "multer2": "^1.1.1",
54
- "node-forge": "^1.4.0",
54
+ "node-forge": "^1.4.2",
55
55
  "node-pac": "^0.5.1",
56
56
  "parseurl": "^1.3.1",
57
57
  "pfork": "^0.6.3",