diff --git a/bin/tsw/ajax/ajax.js b/bin/tsw/ajax/ajax.js index 427bdc0c..0e988eba 100644 --- a/bin/tsw/ajax/ajax.js +++ b/bin/tsw/ajax/ajax.js @@ -402,11 +402,10 @@ 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) { @@ -414,11 +413,10 @@ Ajax.prototype.doRequest = function(opt) { } 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 @@ -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(); diff --git a/bin/tsw/runtime/capturer.js b/bin/tsw/runtime/capturer.js index abb516bc..32591f69 100644 --- a/bin/tsw/runtime/capturer.js +++ b/bin/tsw/runtime/capturer.js @@ -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 = ''; @@ -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), @@ -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(); @@ -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) { diff --git a/bin/tsw/util/auto-report/download.js b/bin/tsw/util/auto-report/download.js index 86cd3b68..9fe2e3eb 100644 --- a/bin/tsw/util/auto-report/download.js +++ b/bin/tsw/util/auto-report/download.js @@ -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'); }