Skip to content

Commit

Permalink
fix: get user IP from XXF first
Browse files Browse the repository at this point in the history
  • Loading branch information
huangyoukun committed Jun 18, 2019
1 parent 1713c1b commit 8d67878
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 60 deletions.
2 changes: 1 addition & 1 deletion bin/proxy/http.proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ logger.info('pid:${pid} createServer ok', {
});

// start RPC server
if(!config.closeRPC) {
if (!config.closeRPC) {
require('webapp/Server.js').startServer();
}

Expand Down
6 changes: 4 additions & 2 deletions bin/proxy/http.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ module.exports = function(req, res) {
setTimeout(function() {
logger.debug('clearing');

while (process.domain) {
process.domain.exit();
}

d.remove(req);
d.remove(res);

Expand All @@ -100,8 +104,6 @@ module.exports = function(req, res) {
res = null;
start = null;
clear = null;

logger.debug('cleared');
}, timeout);
});

Expand Down
25 changes: 16 additions & 9 deletions bin/proxy/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ function startServer() {
// 根据cpu数来初始化并启动子进程
if (config.runAtThisCpu === 'auto') {
cpuUtil.cpus().forEach(function(v, i) {
if (config.maxWorkerNum && i >= config.maxWorkerNum) {
logger.info(`hit maxWorkerNum = ${config.maxWorkerNum}`);
return;
}
cpuMap.push(0);
cluster.fork(process.env).cpuid = i;
});
Expand Down Expand Up @@ -293,15 +297,18 @@ function checkWorkerAlive() {

logger.error(worker.lastMessage);

mail.SendMail(key, 'js', 600, {
'to': config.mailTo,
'cc': config.mailCC,
'runtimeType': 'Memory',
'msgInfo': `${serverInfo.intranetIp} ${lang.__('mail.memoryExceedingTips')}`,
'title': `${serverInfo.intranetIp} ${lang.__('mail.memoryExceedingWaring')}`,
'content': `<p><strong>${serverInfo.intranetIp} ${lang.__('mail.memoryExceedingTips')}</strong></p>`
});

if (config.CCIPLimitQuiet) {
logger.error('hit CCIPLimitQuiet, no mail will be send');
} else {
mail.SendMail(key, 'js', 600, {
'to': config.mailTo,
'cc': config.mailCC,
'runtimeType': 'Memory',
'msgInfo': `${serverInfo.intranetIp} ${lang.__('mail.memoryExceedingTips')}`,
'title': `${serverInfo.intranetIp} ${lang.__('mail.memoryExceedingWaring')}`,
'content': `<p><strong>${serverInfo.intranetIp} ${lang.__('mail.memoryExceedingTips')}</strong></p>`
});
}
restartWorker(worker);
}
}
Expand Down
2 changes: 1 addition & 1 deletion bin/proxy/ws.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exports.doRoute = function(ws, type, opts) {
const mod_act = exports.getModAct(ws),
moduleObj = wsModMap.find(mod_act, ws);

if (typeof moduleObj !== 'object') {
if (!moduleObj || typeof moduleObj !== 'object') {
try {
ws.send('module ' + mod_act + ' is not object');
} catch (e) {
Expand Down
1 change: 1 addition & 0 deletions bin/tsw/ajax/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ Ajax.prototype.doRequest = function(opt) {
},
this._proxyRequest.headers,
{
'x-real-ip': httpUtil.getUserIp(this._proxyRequest),
'x-forwarded-for': httpUtil.getUserIp(this._proxyRequest)
},
opt.headers
Expand Down
49 changes: 13 additions & 36 deletions bin/tsw/api/fileCache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,10 @@ this.set = function(filepath, data) {
const basename = path.basename(filename);
const randomname = basename + '.tmp.' + String(Math.random()).slice(2);

logger.debug('[set] filename : ${filename}', {
filename: filename
});

logger.debug('[set] dirname : ${dirname}', {
dirname: dirname
});

logger.debug('[set] basename : ${basename}', {
basename: basename
});

logger.debug('[set] randomname : ${randomname}', {
randomname: randomname
});
logger.debug(`[set] filename : ${filename}`);
logger.debug(`[set] dirname : ${dirname}`);
logger.debug(`[set] basename : ${basename}`);
logger.debug(`[set] randomname : ${randomname}`);

this.mkdir(dirname);

Expand All @@ -103,10 +92,7 @@ this.set = function(filepath, data) {

fs.writeFile([dirname, randomname].join('/'), buffer, { mode: 0o666 }, function(err) {

logger.debug('[write] done: ${randomname}, size: ${size}', {
randomname: randomname,
size: buffer.length
});
logger.debug(`[write] done: ${randomname}, size: ${buffer.length}`);

if (err) {
logger.info(err.stack);
Expand All @@ -120,10 +106,7 @@ this.set = function(filepath, data) {
logger.debug(err);
}

logger.debug('[rename] done: ${basename}, cost: ${cost}ms', {
basename: basename,
cost: end - start
});
logger.debug(`[rename] done: ${basename}, cost: ${end - start}ms`);
});

});
Expand Down Expand Up @@ -156,12 +139,10 @@ this.getSync = function(filepath) {
}

const end = Date.now();
const size = buffer && buffer.length;
const cost = end - start;

logger.debug('[get] done: ${filename}, size: ${size}, cost: ${cost}ms', {
filename: filename,
size: buffer && buffer.length,
cost: end - start
});
logger.debug(`[get] done: ${filename}, size: ${size}, cost: ${cost}ms`);

tnm2.Attr_API('SUM_TSW_FILECACHE_READ', 1);
return res;
Expand All @@ -178,9 +159,7 @@ this.get = this.getAsync = function(filepath) {
data: null
};

logger.debug('[getAsync] ${filename}', {
filename: filename
});
logger.debug(`[getAsync] ${filename}`);

fs.readFile(filename, function(err, buffer) {

Expand All @@ -200,7 +179,7 @@ this.get = this.getAsync = function(filepath) {
return;
}

logger.debug('[getAsync] mtime: ${mtime}, size: ${size}', stats);
logger.debug(`[getAsync] mtime: ${stats.mtime}, size: ${stats.size}`);

res.stats = stats;
defer.resolve(res);
Expand All @@ -217,16 +196,14 @@ this.updateMtime = function(filepath, atime, mtime) {
const filename = this.getDir(filepath);


logger.debug('[updateMtime] ${filename}', {
filename: filename
});
logger.debug(`[updateMtime] ${filename}`);

fs.utimes(filename, atime || new Date(), mtime || new Date(), function(err) {
if (err) {
logger.debug(err.stack);
return;
}

logger.debug('[updateMtime] ${filename} succ');
logger.debug(`[updateMtime] ${filename} succ`);
});
};
4 changes: 2 additions & 2 deletions bin/tsw/runtime/capturer.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ process.nextTick(function() {
let remotePort = '';
let localAddress = '';
let localPort = '';
const host = (opt.headers && opt.headers.host) || opt.host;
const host = (opt.headers && opt.headers.host) || opt.host || opt.hostname;

if (context.requestCaptureSN) {
context.requestCaptureSN++;
Expand Down Expand Up @@ -115,7 +115,7 @@ process.nextTick(function() {
serverIp: remoteAddress || opt.host,
serverPort: remotePort || opt.port,
requestHeader: httpUtil.getClientRequestHeaderStr(request),
requestBody: request._body ? request._body.toString('base64') : '',
requestBody: request._body ? (request._body.length < maxBodySize ? request._body.toString('base64') : `body was too large too show, length: ${request._body.length}`) : '',
responseHeader: httpUtil.getClientResponseHeaderStr(response, bodySize),
responseBody: (buffer.toString('base64')) || '',
timestamps: {
Expand Down
3 changes: 2 additions & 1 deletion bin/tsw/util/auto-report/logReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const tnm2 = require('api/tnm2');
const format = require('webapp/utils/format');
const CD = require('util/CD.js');
const canIuse = /^[0-9a-zA-Z_-]{0,64}$/;
const maxBodySize = 512 * 1024;
const MAX_ALPHA_LOG = post.MAX_ALPHA_LOG;

const limit = {
Expand Down Expand Up @@ -714,7 +715,7 @@ module.exports.reportLog = function() {
serverIp: serverInfo.intranetIp,
serverPort: config.httpPort,
requestHeader: httpUtil.getRequestHeaderStr(req),
requestBody: req._body ? req._body.toString('base64') : '',
requestBody: req._body ? (req._body.length < maxBodySize ? req._body.toString('base64') : `body was too large too show, length: ${req._body.length}`) : '',
responseHeader: httpUtil.getResponseHeaderStr(res),
responseBody: res && res._body ? res._body.toString('base64') : '',
logText: logText,
Expand Down
16 changes: 8 additions & 8 deletions bin/tsw/util/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,21 +416,21 @@ this.getUserIp = function(request) {
const qvia = request.headers['qvia'] || '';
const realIp = request.headers['x-real-ip'] || '';

if (realIp) {

// x-real-ip
if (userIp && this.isInnerIP(userIp)) {
userIp = realIp;
}
} else if (xff) {
if (xff) {

// xff判断,注意只认内网ip带的xff,外网带的不算
if (userIp && this.isInnerIP(userIp)) {

xff = xff.split(',').slice(-1)[0] || userIp;
xff = xff.split(',')[0] || userIp;
userIp = xff.trim() || userIp;
}

} else if (realIp) {

// x-real-ip
if (userIp && this.isInnerIP(userIp)) {
userIp = realIp;
}
} else if (qvia) {

// 注意只认内网ip带的qvia,外网带的不算
Expand Down

0 comments on commit 8d67878

Please sign in to comment.