diff --git a/lib/config.js b/lib/config.js index e753f42dc..6c2bf4652 100644 --- a/lib/config.js +++ b/lib/config.js @@ -38,6 +38,7 @@ config.HTTPS_FIELD = 'x-whistle-https-request'; config.DATA_ID = 'x-whistle-data-id' + '-' + uid; config.CLIENT_PORT_HEAD = 'x-whistle-client-port-' + uid; config.WEBUI_HEAD = 'x-forwarded-from-whistle-' + uid; +config.CLIENT_INFO_HEAD = 'x-whistle-client-info-' + uid; config.WEBUI_PATH = '/...whistle-path.5b6af7b9884e1165...///'; config.PREVIEW_PATH_RE = /\?\?\?WHISTLE_PREVIEW_CHARSET=([A-Z\d_-]+)\?\?\?$/; diff --git a/lib/https/index.js b/lib/https/index.js index 956f848de..06b695869 100644 --- a/lib/https/index.js +++ b/lib/https/index.js @@ -622,9 +622,15 @@ module.exports = function(socket, hostname, next, isWebPort) { if (!chunk) {//没有数据 return isWebPort ? socket.destroy() : next(chunk); } + var clientPort = socket.remotePort; if (HTTP_RE.test(headersStr)) { + socket.test = clientPort; + var statusLine = RegExp['$&']; + var len = Buffer.byteLength(statusLine); server.emit('connection', socket); - socket.emit('data', chunk); + chunk = chunk.slice(len); + statusLine += '\r\n' + config.CLIENT_INFO_HEAD + ': ' + clientIp + ',' + clientPort; + socket.emit('data', Buffer.concat([Buffer.from(statusLine), chunk])); } else if (chunk[0] != 22) { next(chunk); } else { diff --git a/lib/init.js b/lib/init.js index f0c685dbb..d665c5950 100644 --- a/lib/init.js +++ b/lib/init.js @@ -110,7 +110,8 @@ module.exports = function(req, res, next) { addTransforms(req, res); addErrorEvents(req, res); var socket = req.socket || {}; - var clientIp = util.getForwardedFor(req.headers); + var clientInfo = util.parseClientInfo(req); + var clientIp = clientInfo[0] || util.getForwardedFor(req.headers); if (clientIp && util.isLocalAddress(clientIp)) { delete req.headers[config.CLIENT_IP_HEAD]; clientIp = null; @@ -120,7 +121,7 @@ module.exports = function(req, res, next) { } req.clientIp = clientIp = clientIp || socket[config.CLIENT_IP_HEAD]; req.method = util.getMethod(req.method); - var clientPort = req.headers[config.CLIENT_PORT_HEAD]; + var clientPort = clientInfo[1] || req.headers[config.CLIENT_PORT_HEAD]; delete req.headers[config.CLIENT_PORT_HEAD]; if (!(clientPort > 0)) { clientPort = null; diff --git a/lib/upgrade.js b/lib/upgrade.js index 7393317cf..b53fdc89e 100644 --- a/lib/upgrade.js +++ b/lib/upgrade.js @@ -23,8 +23,9 @@ function upgradeHandler(req, socket) { socket.headers = req.headers; socket.rawHeaderNames = getRawHeaderNames(req.rawHeaders); socket.url = req.url; - var clientPort = util.getClientPort(socket); - var clientIp = util.getClientIp(socket); + var clientInfo = util.parseClientInfo(req); + var clientPort = clientInfo[1] || util.getClientPort(socket); + var clientIp = clientInfo[0] || util.getClientIp(socket); delete socket.headers[config.CLIENT_PORT_HEAD]; socket.getBuffer = function(headers, path) { var rawData = ['GET' + ' ' + (path || req.url) + ' ' + 'HTTP/1.1']; diff --git a/lib/util/index.js b/lib/util/index.js index ddb8ec814..0f69fa4f3 100644 --- a/lib/util/index.js +++ b/lib/util/index.js @@ -2243,3 +2243,15 @@ exports.parseRange = function(req, size) { end: end }; }; + +exports.parseClientInfo = function(req) { + var clientInfo = req.headers[config.CLIENT_INFO_HEAD] || ''; + if (clientInfo) { + delete req.headers[config.CLIENT_INFO_HEAD]; + clientInfo = String(clientInfo).split(','); + if (!net.isIP(clientInfo[0]) || !(clientInfo[1] > 0)) { + return ''; + } + } + return clientInfo; +};