diff --git a/bin/tsw/ajax/ajax.js b/bin/tsw/ajax/ajax.js index 0e988eba..68aec9b9 100644 --- a/bin/tsw/ajax/ajax.js +++ b/bin/tsw/ajax/ajax.js @@ -14,6 +14,7 @@ const http = require('http'); const https = require('https'); const vm = require('vm'); const url = require('url'); +const net = require('net'); const qs = require('qs'); const form = require('./form.js'); const token = require('./token.js'); @@ -553,14 +554,21 @@ Ajax.prototype.doRequest = function(opt) { 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; - }); + if (socket.remoteAddress) { + this.remoteIp = socket.remoteAddress; + return; + } + + if (!net.isIP(opt.host)) { + socket.once('lookup', (err, address, family, host) => { + if (err) { + logger.error(logPre + err.stack); + this.emit('fail'); + return; + } + this.remoteIp = address; + }); + } }); defer.always(function() { diff --git a/bin/tsw/runtime/capturer.js b/bin/tsw/runtime/capturer.js index 32591f69..24a143ed 100644 --- a/bin/tsw/runtime/capturer.js +++ b/bin/tsw/runtime/capturer.js @@ -14,6 +14,7 @@ */ const http = require('http'); const https = require('https'); +const net = require('net'); let isFirstLoad = true; if (global[__filename]) { @@ -160,16 +161,28 @@ process.nextTick(function() { }; request.once('socket', function(socket) { - socket.once('lookup', (err, address, family, host) => { + if (socket.remoteAddress) { timeLookup = Date.now(); - if (err) { - logger.error(logPre + err.stack); - finish(); - return; - } + timeConnect = Date.now(); + remoteAddress = socket.remoteAddress; + remotePort = socket.remotePort; const cost = timeLookup - timeStart; - logger.debug(`${logPre}dns lookup ${host} -> ${address}, cost ${cost}ms`); - }); + logger.debug(`${logPre}socket reuse ${remoteAddress}:${remotePort}, cost ${cost}ms`); + return; + } + + if (!net.isIP(opt.host)) { + 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();