Skip to content

Commit

Permalink
Merge pull request #129 from huangyoukun/master
Browse files Browse the repository at this point in the history
抓包功能支持DNS信息
  • Loading branch information
RobinzZH authored Jun 15, 2018
2 parents 21ae449 + 28cf591 commit 2c31bc0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 38 deletions.
19 changes: 14 additions & 5 deletions bin/tsw/ajax/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,23 +402,21 @@ Ajax.prototype.doRequest = function(opt) {
times.start = new Date().getTime();

function report(opt, isFail, code) {

const toIp = request.remoteIp || opt.ip;

if (isTST.isTST(opt)) {
// 忽略安全中心请求
return;
return; // 忽略安全中心请求
}

if (isFail === 1 && opt.ignoreErrorReport) {
isFail = 2;
}

if (opt.dcapi) {

logger.debug(logPre + '返回码:' + code + ', isFail:' + isFail);

dcapi.report(Deferred.extend({}, opt.dcapi, {
toIp: opt.ip,
toIp: toIp,
code: code,
isFail: isFail,
delay: new Date() - times.start
Expand Down Expand Up @@ -554,6 +552,17 @@ Ajax.prototype.doRequest = function(opt) {
request.setNoDelay(true);
request.setSocketKeepAlive(true);

request.once('socket', function(socket) {
socket.once('lookup', (err, address, family, host) => {
if (err) {
logger.error(logPre + err.stack);
this.emit('fail');
return;
}
this.remoteIp = address;
});
});

defer.always(function() {
clearTimeout(tid);
request.removeAllListeners();
Expand Down
83 changes: 52 additions & 31 deletions bin/tsw/runtime/capturer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ process.nextTick(function() {
let bodySize = 0;
const maxBodySize = 1024 * 1024;
const timeStart = Date.now();
let timeEnd = 0;
let timeLookup = timeStart;
let timeConnect = timeStart;
let timeResponse = 0;
// var timeCurr = timeStart;
let timeEnd = 0;
let remoteAddress = '';
let remotePort = '';
let localAddress = '';
Expand Down Expand Up @@ -122,10 +123,10 @@ process.nextTick(function() {
GotRequestHeaders: new Date(timeStart),
ClientDoneRequest: new Date(timeStart),
GatewayTime: 0,
DNSTime: 0,
TCPConnectTime: 0,
DNSTime: timeLookup - timeStart,
TCPConnectTime: timeConnect - timeStart,
HTTPSHandshakeTime: 0,
ServerConnected: new Date(timeStart),
ServerConnected: new Date(timeConnect),
FiddlerBeginRequest: new Date(timeStart),
ServerGotRequest: new Date(timeStart),
ServerBeginResponse: new Date(timeResponse),
Expand All @@ -139,6 +140,51 @@ process.nextTick(function() {
logJson.ajax.push(curr);
};


const finish = function(maybeResponse) {
if (timeEnd) {
return;
}

timeEnd = new Date().getTime();

if (captureBody) {
buffer = Buffer.concat(result);
result = [];
}

// 上报
if (captureBody) {
report(maybeResponse);
}
};

request.once('socket', function(socket) {
socket.once('lookup', (err, address, family, host) => {
timeLookup = Date.now();
if (err) {
logger.error(logPre + err.stack);
finish();
return;
}
const cost = timeLookup - timeStart;
logger.debug(`${logPre}dns lookup ${host} -> ${address}, cost ${cost}ms`);
});

socket.once('connect', function() {
timeConnect = Date.now();
const cost = timeConnect - timeStart;
remoteAddress = this.remoteAddress;
remotePort = this.remotePort;
logger.debug(`${logPre}connect ${remoteAddress}:${remotePort}, cost ${cost}ms`);
});
});

request.once('error', function(err) {
logger.error(err.stack);
finish();
});

request.once('response', (response) => {
timeResponse = Date.now();

Expand All @@ -163,35 +209,10 @@ process.nextTick(function() {

const done = function() {
this.removeListener('data', data);

if (timeEnd) {
return;
}

timeEnd = new Date().getTime();

if (captureBody) {
buffer = Buffer.concat(result);
result = [];
}

// 上报
if (captureBody) {
report(response);
}
finish(response);
};

const data = function(chunk) {
// var cost = Date.now() - timeCurr;

// timeCurr = Date.now();

// logger.debug('${logPre}receive data: ${size},\tcost: ${cost}ms',{
// logPre: logPre,
// cost: cost,
// size: chunk.length
// });

bodySize += chunk.length;

if (captureBody && bodySize <= maxBodySize) {
Expand Down
4 changes: 2 additions & 2 deletions bin/tsw/util/auto-report/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,8 @@ const downloadHaz = function (request, response, opt) {
viewData.forEach(function(tmp, i) {
if (tmp.requestHeader) {
tmp.requestRaw = Buffer.concat([
Buffer.from(tmp.curr.requestHeader || '', 'utf-8'),
Buffer.from(tmp.curr.requestBody || '', 'base64')
Buffer.from(tmp.requestHeader || '', 'utf-8'),
Buffer.from(tmp.requestBody || '', 'base64')
]).toString('UTF-8');
}

Expand Down

0 comments on commit 2c31bc0

Please sign in to comment.