Skip to content

Commit

Permalink
Merge pull request #115 from Tencent/dev
Browse files Browse the repository at this point in the history
merge dev to master
  • Loading branch information
RobinzZH authored Jun 11, 2018
2 parents e0bde17 + f2cf3bf commit 2b416bd
Show file tree
Hide file tree
Showing 59 changed files with 437 additions and 309 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
bin/wwwroot
bin/deps
node_modules

Empty file modified bin/proxy/dump.alpha.sh
100644 → 100755
Empty file.
Empty file modified bin/proxy/dump.config.sh
100644 → 100755
Empty file.
Empty file modified bin/proxy/dump.cpu.sh
100644 → 100755
Empty file.
Empty file modified bin/proxy/dump.global.sh
100644 → 100755
Empty file.
Empty file modified bin/proxy/dump.h5test.sh
100644 → 100755
Empty file.
Empty file modified bin/proxy/dump.heap.sh
100644 → 100755
Empty file.
Empty file modified bin/proxy/kill.sh
100644 → 100755
Empty file.
Empty file modified bin/proxy/reload.sh
100644 → 100755
Empty file.
Empty file modified bin/proxy/restart.sh
100644 → 100755
Empty file.
Empty file modified bin/proxy/shutdown.sh
100644 → 100755
Empty file.
17 changes: 3 additions & 14 deletions bin/proxy/so.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
echo

CUR_DIR=$(cd $(dirname $0); pwd)
LIBC_VERSION=$(ls -l /lib64/libc.so.6 | sed -e 's/.*libc-2.\(.*\).so/\1/g')

cd ${CUR_DIR}/../
pwd
Expand All @@ -12,23 +11,13 @@ BIN_DIR=$(pwd)

echo "BIN_DIR: ${BIN_DIR}"

echo "libc-2.${LIBC_VERSION}"

if [ -e "/usr/local/bin/node" ]
then
echo "/usr/local/bin/node"
ln -sf /usr/local/bin/node ./TSW
else
echo "/usr/bin/node"
ln -sf /usr/bin/node ./TSW
fi

NODE_PATH=$(node -p process.execPath)
echo "use node:$NODE_PATH"
ln -sf $NODE_PATH ./TSW
chmod +x ./TSW

./TSW -v

echo "copy so done"

if [ -e "../log" ]
then
echo "log exists"
Expand Down
Empty file modified bin/proxy/startup.sh
100644 → 100755
Empty file.
Empty file modified bin/proxy/top.sh
100644 → 100755
Empty file.
Empty file modified bin/proxy/top100.sh
100644 → 100755
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions bin/tsw/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
const Context = require('runtime/Context');
const Window = require('runtime/Window');

this.currentContext = function() {
this.currentContext = function () {
return (process.domain && process.domain.currentContext) || new Context();
};

if (!global.context) {

Object.defineProperty(global, 'context', {
get: function() {
get: function () {
return module.exports.currentContext();
}
});

Object.defineProperty(global, 'window', {
get: function() {
get: function () {

if (global.windowHasDisabled) {
return undefined;
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ this.memoryLimit = 768 * 1024 * 1024 * (mpc >= 2 ? 2 : 1);
// 限制
this.CCIPLimit = 500;

// 超限后自动拉黑
this.CCIPLimitAutoBlock = false;

// allowHost
this.allowHost = [];

Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions bin/tsw/plug.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ if (!global.plug) {
plug.parent = path.join(__dirname, '..');
plug.paths = [
path.join(__dirname, '../deps'),
path.join(__dirname, '../tsw'),
path.join(__dirname, '../tencent'),
path.join(__dirname, '../lib')
path.join(__dirname, '../tsw')
];
module.paths = plug.paths.concat(module.paths);

Expand Down
83 changes: 56 additions & 27 deletions bin/tsw/runtime/CCFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ let ipConut = CCIPSize;
let cache = {
ipCache: {},
whiteList: {},
blackList: {},
ipCacheLast: {}
};

Expand All @@ -30,11 +31,16 @@ if (global[__filename]) {
global[__filename] = cache;
}

this.addWhiteList = function(userIp) {
this.addWhiteList = function (userIp) {
cache.whiteList[userIp] = true;
};

this.checkHost = function(req, res) {
this.addBlackList = function (userIp) {
cache.blackList[userIp] = true;
};


this.checkHost = function (req, res) {

const hostAllow = config.allowHost || [];
const host = req.headers['host'];
Expand Down Expand Up @@ -74,12 +80,12 @@ this.checkHost = function(req, res) {
};

// 计算标准方差
this.StdX10 = function(ipCache) {
this.StdX10 = function (ipCache) {

let res = 0;
let sum = 0;
let avg = 0;
const arr = Object.keys(ipCache).filter(function(item) {
const arr = Object.keys(ipCache).filter(function (item) {
const tmp = ipCache[item];
if (typeof tmp === 'object' && tmp.list) {
sum += tmp.list.length;
Expand All @@ -94,7 +100,7 @@ this.StdX10 = function(ipCache) {

avg = sum / arr.length;

const sumXsum = arr.reduce(function(pre, key) {
const sumXsum = arr.reduce(function (pre, key) {
const item = ipCache[key];
const value = item.list.length;
item.avg = avg;
Expand All @@ -107,19 +113,24 @@ this.StdX10 = function(ipCache) {
return res;
};

this.check = function(req, res) {
this.check = function (req, res) {

const userIp = httpUtil.getUserIp(req);
const userIp24 = httpUtil.getUserIp24(req);

let content;

const info = {
userIp: userIp,
hostname: req.headers.host,
pathname: req.REQUEST.pathname
};

if (cache.blackList[userIp]) {
tnm2.Attr_API('SUM_TSW_CC_LIMIT', 1);
logger.report();
res.writeHead(403, { 'Content-Type': 'text/plain; charset=UTF-8' });
res.end();
return false;
}

if (cache.whiteList[userIp]) {
return true;
}
Expand Down Expand Up @@ -195,55 +206,73 @@ this.check = function(req, res) {
return true;
}

// tnm2.Attr_API('SUM_TSW_CC_LIMIT', 1);

// 确认没发送过邮件
cache.ipCacheLast.hasSendMail = true;

// 发现目标,发邮件
const key = `[AVG_TSW_IP_STD_X10]:${serverInfo.intranetIp}`;

content = '';
let content = '';
const max = {
num: 0,
ip: ''
};

Object.keys(cache.ipCacheLast).forEach(function(ip, i) {
Object.keys(cache.ipCacheLast).forEach(function (ip, i) {

let num = '';
let tmp = '';

if (
cache.ipCacheLast[ip]
&& cache.ipCacheLast[ip].list
&& cache.ipCacheLast[ip].list.length > 1
) {
num = String(cache.ipCacheLast[ip].list.length);
num = (num + 'XXXXXX').slice(0, 8).replace(/X/g, ' ');
content += `<div style="font-size:12px;">${num}${ip}</div>`;
num = cache.ipCacheLast[ip].list.length;
tmp = (num + '--------').slice(0, 8);
content += `<div style="font-size:12px;">${tmp}${ip}</div>`;

if (num > max.num) {
max.num = num;
max.ip = ip;
}
}
});

const key = `[AVG_TSW_IP_STD_X10]:${max.ip}`;
let title = '';

if (config.CCIPLimitAutoBlock) {
title = `[IP聚集自动拉黑周知][${cache.ipCacheLast.StdX10}%]${max.ip}`;
this.addBlackList(max.ip);
} else {
title = `[IP聚集告警][${cache.ipCacheLast.StdX10}%]${max.ip}`;
}

mail.SendMail(key, 'TSW', 3600, {
'to': config.mailTo,
'cc': config.mailCC,
'title': `[IP聚集告警][${cache.ipCacheLast.StdX10}%]${serverInfo.intranetIp}`,
'title': title,
'content': '<p><strong>服务器IP:</strong>' + serverInfo.intranetIp + '</p>'
+ '<p><strong>IP聚集度:</strong>' + cache.ipCacheLast.StdX10 + '%</p>'
+ '<p><strong>告警阀值:</strong>' + CCIPLimit + '</p>'
+ '<p><strong>正常值:</strong>5-50</p>'
+ '<p><strong>检测耗时:</strong>' + parseInt((cache.ipCacheLast.end - cache.ipCacheLast.start) / 1000, 10) + 's</p>'
+ '<p><strong>证据列表:</strong></p>'
+ content
+ '<p><strong>恶意IP:</strong>' + max.ip + '</p>'
+ '<p><strong>自动拉黑:</strong>' + (config.CCIPLimitAutoBlock ? '是' : '否') + '</p>'
+ '<p><strong>IP聚集度:</strong>' + cache.ipCacheLast.StdX10 + '%</p>'
+ '<p><strong>告警阈值:</strong>' + CCIPLimit + '</p>'
+ '<p><strong>正常值:</strong>5-50</p>'
+ '<p><strong>检测耗时:</strong>' + parseInt((cache.ipCacheLast.end - cache.ipCacheLast.start) / 1000, 10) + 's</p>'
+ '<p><strong>证据列表:</strong></p>'
+ content
});


return true;
};


this.getIpCache = function() {
this.getIpCache = function () {
return cache.ipCacheLast;
};


this.getIpSize = function() {
this.getIpSize = function () {
return CCIPSize;
};

File renamed without changes.
39 changes: 27 additions & 12 deletions bin/tsw/util/auto-report/TEReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ this.report = function() {

const logKey = 'h5test' + logJson.group;

// 上报自己
post.report(logKey, logText, logJson);

// 开放平台上报,不用再分组了
if (config.appid && config.appkey) {
logReport.reportCloud({
Expand All @@ -87,13 +84,17 @@ this.report = function() {
pathname: '',
statusCode: ''
});
} else {
// 私有化部署上报
// 上报自己
post.report(logKey, logText, logJson);

// 上报分组
require('util/CD.js').check('h5test' + logJson.group, 1, 60).done(function() {
post.report('group.h5test', logText, logJson);
});
}

// 上报分组
require('util/CD.js').check('h5test' + logJson.group, 1, 60).done(function() {
post.report('group.h5test', logText, logJson);
});

});

};
Expand All @@ -119,6 +120,10 @@ this.list = function(group) {
const map = {};

arr.forEach(function(v) {
if (group && v.group !== group) {
return;
}

if (!map[v.ip]) {
map[v.ip] = true;
res.push(v);
Expand All @@ -134,9 +139,7 @@ this.list = function(group) {
order: -65536,
// owner: "TSW",
groupName: group,
ip: 'alpha',
moduleId: 0,
moduleName: 'null'
ip: 'alpha'
});

res.sort(function(a, b) {
Expand All @@ -153,12 +156,24 @@ this.list = function(group) {
this.getAllGroup = function() {

const defer = Deferred.create();
let getLogJsonDefer;

post.getLogJson('group.h5test').done(function(arr) {
// 开平对应的存储
if (context.appid && context.appkey) {
getLogJsonDefer = postOpenapi.getLogJson(`${context.appid}/tsw/h5test`);
} else {
getLogJsonDefer = post.getLogJson('group.h5test');
}

getLogJsonDefer.done(function(arr) {

const res = [];
const map = {};

arr.sort(function(a, b) {
return a.order - b.order;
});

arr.forEach(function(v) {
if (!map[v.group]) {
map[v.group] = true;
Expand Down
File renamed without changes.
Loading

0 comments on commit 2b416bd

Please sign in to comment.