Skip to content

Commit

Permalink
feat: enable://requestWithMatchedRules enable://responseWithMatchedRules
Browse files Browse the repository at this point in the history
  • Loading branch information
avwo committed Mar 22, 2024
1 parent 8b6d3bb commit d3c613e
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 15 deletions.
3 changes: 3 additions & 0 deletions lib/https/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ function resolveWebsocket(socket, wss) {
data.requestTime = data.dnsTime = Date.now();
getResRules(socket, resData, function () {
status = curStatus || status;
util.addMatchedRules(socket, resData);
var isSuccess = status == 101;
if (isSuccess) {
statusMsg = 'HTTP/1.1 101 Switching Protocols';
Expand Down Expand Up @@ -636,6 +637,7 @@ function resolveWebsocket(socket, wss) {
}
}
util.deleteReqHeaders(socket);
util.addMatchedRules(socket);
reqSocket.write(socket.getBuffer(headers, options.path));
reqSocket.resume();
delete headers['x-whistle-frame-parser'];
Expand Down Expand Up @@ -757,6 +759,7 @@ function resolveWebsocket(socket, wss) {
reqSocket.reqId = data.id;
var code = curStatus || res.statusCode;
socket.statusCode = res.statusCode = code;
util.addMatchedRules(socket, res);
var curHeaders = curResHeaders;
if (socket.fromComposer) {
curHeaders = extend({}, curResHeaders);
Expand Down
2 changes: 2 additions & 0 deletions lib/inspectors/res.js
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,7 @@ module.exports = function (req, res, next) {
req.options = options;
isHttps &&
util.setClientCert(options, key, cert, isPfx, cacheKey);
util.addMatchedRules(req);
h2.request(req, res, send);
}
},
Expand Down Expand Up @@ -1255,6 +1256,7 @@ module.exports = function (req, res, next) {
curHeaders = extend({}, _resHeaders);
curHeaders['x-whistle-req-id'] = req.reqId;
}
util.addMatchedRules(req, _res);
try {
res.writeHead(
_res.statusCode,
Expand Down
3 changes: 3 additions & 0 deletions lib/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,9 @@ function getPipe(type, hookName) {
var options = getOptions(req, res, isRes && 'resRules', true);
options.headers[PLUGIN_HOOK_NAME_HEADER] = hookName;
options.headers['x-whistle-request-tunnel-ack'] = 1;
if (req._noDecompress) {
options.headers['x-whistle-disable-ws-decompress'] = 1;
}
if (req._isUpgrade) {
options.headers[UPGRADE_HEADER] = 1;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/load-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ var initReq = function (req, res, isServer) {
req.isHttps = oReq.isHttps = HTTPS_RE.test(fullUrl);
oReq.remoteAddress = req.headers[REMOTE_ADDR_HEAD] || '127.0.0.1';
oReq.remotePort = parseInt(req.headers[REMOTE_PORT_HEAD], 10) || 0;
req.notDecompressed = oReq.notDecompressed = req.headers['x-whistle-disable-ws-decompress'] === '1';
req.fromTunnel = oReq.fromTunnel = req.headers[FROM_TUNNEL_HEADER] === '1';
delete req.headers[FROM_TUNNEL_HEADER];
delete req.headers[REMOTE_ADDR_HEAD];
Expand Down Expand Up @@ -1017,7 +1018,7 @@ function setReqRules(uri, reqRules) {

function addFrameHandler(req, socket, maxWsPayload, fromClient, toServer) {
socket.wsExts = req.headers['sec-websocket-extensions'] || '';
var receiver = wsParser.getReceiver(socket, !fromClient, maxWsPayload);
var receiver = wsParser.getReceiver(socket, !fromClient, maxWsPayload, req.notDecompressed);
var emit = socket.emit;
var write = socket.write;
var end = socket.end;
Expand Down
1 change: 1 addition & 0 deletions lib/service/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ function resolveFrames(res, frames, callback) {
base64: chunk.toString('base64'),
compressed: opts.compressed,
length: opts.length,
unzipLen: chunk && opts.compressed ? chunk.length : undefined,
opcode: opts.opcode
});
}
Expand Down
16 changes: 12 additions & 4 deletions lib/socket-mgr.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ function handleWsSend(ctx, reqTrans, sendStatus, eventName) {
var reqReceiver;
if (!hideWs) {
try {
reqReceiver = wsParser.getReceiver(res);
reqReceiver = wsParser.getReceiver(res, false, undefined, ctx.noDecompress);
} catch (e) {
hideWs = true;
}
Expand Down Expand Up @@ -368,7 +368,9 @@ function handleWsSend(ctx, reqTrans, sendStatus, eventName) {
ignore: opts.data ? undefined : sendStatus.ignoring,
bin: getBinary(data, opts.length),
compressed: eventName ? undefined : opts.compressed,
notDecompressed: opts.notDecompressed,
length: opts.length,
unzipLen: data && opts.compressed ? data.length : undefined,
opcode: eventName ? undefined : opcode
});
};
Expand Down Expand Up @@ -399,7 +401,7 @@ function handleWsReceive(ctx, resTrans, receiveStatus, eventName) {
var resReceiver;
if (!hideWs) {
try {
resReceiver = wsParser.getReceiver(res, true);
resReceiver = wsParser.getReceiver(res, true, undefined, ctx.noDecompress);
} catch (e) {
hideWs = true;
}
Expand Down Expand Up @@ -459,7 +461,9 @@ function handleWsReceive(ctx, resTrans, receiveStatus, eventName) {
mask: eventName ? undefined : opts.mask,
ignore: opts.data ? undefined : receiveStatus.ignoring,
compressed: eventName ? undefined : opts.compressed,
notDecompressed: opts.notDecompressed,
length: opts.length,
unzipLen: data && opts.compressed ? data.length : undefined,
opcode: eventName ? undefined : opcode
});
};
Expand All @@ -481,14 +485,15 @@ function handleWsReceive(ctx, resTrans, receiveStatus, eventName) {
};
}

function getContext(req, res, hasEvent, sendStatus, receiveStatus) {
function getContext(req, res, hasEvent, sendStatus, receiveStatus, noDecompress) {
var reqId = req.reqId;
var ctx = (conns[reqId] = {
customParser: req.customParser,
req: req,
res: res,
hasEvent: hasEvent,
url: req.fullUrl,
noDecompress: noDecompress,
charset: util.getCharset(res.headers['content-type']) || '',
clearup: function () {
clearupStatus(conns, reqId, sendStatus, receiveStatus);
Expand Down Expand Up @@ -525,11 +530,13 @@ exports.handleUpgrade = function (req, res) {
};
var reqTrans = util.createTransform();
var resTrans = util.createTransform();
var noDecompress = req.disable.wsDecompress && !req.enable.wsDecompress;

reqTrans.on('error', emitError);
resTrans.on('error', emitError);
res.headers = res.headers || {};
req.wsExts = res.headers['sec-websocket-extensions'] || '';
req._noDecompress = noDecompress;
handleClose(req, res);
pluginMgr.getWsPipe(
req,
Expand All @@ -545,7 +552,8 @@ exports.handleUpgrade = function (req, res) {
res,
hasEvent,
sendStatus,
receiveStatus
receiveStatus,
noDecompress
));
if (customParser) {
var handleInspect = function (chunk, _, cb) {
Expand Down
4 changes: 2 additions & 2 deletions lib/util/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,8 @@ function queryToString(query, req) {

exports.toBuffer = function(data, req) {
data = queryToString(data, req);
if (!data || Buffer.isBuffer(data)) {
return data && data.length && data;
if (data == null || Buffer.isBuffer(data)) {
return data;
}
if (typeof data !== 'string') {
try {
Expand Down
24 changes: 24 additions & 0 deletions lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3885,3 +3885,27 @@ exports.removeSpecPath = function(req) {
req.url = req.url.replace(specPath, '/');
}
};

function getRulesText(req) {
var rules = req.rules;
var keys = rules && Object.keys(rules);
if (!keys || !keys.length) {
return;
}
return safeEncodeURIComponent(keys.map(function(key) {
var rule = rules[key];
return rule.rawPattern + ' ' + (rule.rawMatcher || rule.matcher);
}).join('\n'));
}

exports.addMatchedRules = function(req, res) {
var enable = req.enable || '';
if (!enable || (res ? !isEnable(req, 'responseWithMatchedRules') : !isEnable(req, 'requestWithMatchedRules'))) {
return;
}
var rules = getRulesText(req);
if (rules) {
var headers = res ? res.headers : req.headers;
headers['x-whistle-matched-rules'] = rules;
}
};
15 changes: 9 additions & 6 deletions lib/util/log-server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var MAX_LENGTH = 512;
var MIN_LENGTH = 420;
var MAX_LENGTH = 800;
var MIN_LENGTH = 720;
var SIZE = 1024 * 64;
var COUNT = 100;
var count = 0;
var logIndex = 0;
var logs = [];

function sliceLogs(index, count, logId) {
Expand Down Expand Up @@ -57,19 +57,22 @@ module.exports = function init(proxy) {
} else if (typeof text != 'string') {
text += '';
}
var overflow = text.length - SIZE;
logs.push({
id: now + '-' + ++count,
id: now + '-' + ++logIndex,
logId: log.id,
date: (log.t && parseInt(log.t, 10)) || now,
level: /^fatal|error|warn|info|debug$/.test(log.level)
? log.level
: 'info',
text: text.substring(0, SIZE)
text: overflow > 9 ? text.substring(0, SIZE) + '...(' + overflow + ')' : text
});

var len = logs.length;
if (len > MAX_LENGTH) {
logs = logs.slice(len - MIN_LENGTH, len);
if (logIndex > 100000) {
logIndex = 0;
}
}
};
proxy.getLogs = getLogs;
Expand Down
7 changes: 5 additions & 2 deletions lib/util/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var util = require('util');
var MAX_LENGTH = 360;
var MIN_LENGTH = 280;
var COUNT = 100;
var count = 0;
var logIndex = 0;
var logs = [];
var LEVELS = ['fatal', 'error', 'warn', 'info', 'debug'];

Expand Down Expand Up @@ -33,14 +33,17 @@ function getLogs(startTime, count) {
function log(text, level) {
var now = Date.now();
logs.push({
id: now + '-' + ++count,
id: now + '-' + ++logIndex,
date: now,
level: level,
text: text
});
var len = logs.length;
if (len > MAX_LENGTH) {
logs = logs.slice(len - MIN_LENGTH, len);
if (logIndex > 100000) {
logIndex = 0;
}
}
}

Expand Down

0 comments on commit d3c613e

Please sign in to comment.