Authored by 王水玲

risk

... ... @@ -4,72 +4,121 @@ const Router = require('koa-router');
const ApiCache = require('../../ci/api_cache');
const _ = require('lodash');
const getter = require('../../zookeeper/getter');
const setter = require('../../zookeeper/setter');
const tester = require('../../zookeeper/tester');
const ZookeeperModel = require('../models/zookeeperModel');
let zookeeperModel = new ZookeeperModel();
let r = new Router();
var process = {
TO_VALIDATE_CODE: '跳转全局验证码解锁',
REDIRECT_403: '返回403'
};
const risk = {
index: async(ctx, next) => {
// const regexp = /pc:limiter:faker:(.*)/;
// const threshold = ctx.request.query.threshold || 100;
// const limit = ctx.request.query.limit || 10;
// let hosts = await MemcachedHost.findAll();
// const selectedHosts = _.filter(hosts, host => {
// const isCurrent = host.host === ctx.request.query.node;
// if (isCurrent) {
// host.isCurrent = true;
// }
// return isCurrent;
// });
// let results = await Promise.all(_.map(selectedHosts, (h) => {
// return (new ApiCache(h.host)).find((key) => {
// return regexp.test(key);
// });
// }));
// let list = [];
// if (results && results[0]) {
// Object.keys(results[0]).forEach((key) => {
// const index = results[0][key];
// if (index > threshold) {
// list.push({
// ip: ((key) => {
// const m = key.match(regexp);
// return m && m.length > 0 ? m[1] : 'Unknown';
// })(key),
// index: index
// })
// }
// });
// }
// list = _.orderBy(list, (item) => {
// return item.index;
// }, 'desc').slice(0, limit);
console.log(11111)
let ret = await zookeeperModel.getPath('/risk');
if (ret) {
ret = JSON.parse(ret);
}
_.each(ret, function(o) {
o.process = process[o.process];
});
await ctx.render('action/risk_management', {
list: ret
});
},
add: async(ctx, next) => {
var data = {};
var key = ctx.request.query.key;
var oldRet = '';
if (key) {
oldRet = await zookeeperModel.getPath('/risk');
if (oldRet) {
oldRet = JSON.parse(oldRet);
}
data = _.find(oldRet, function(o) {
return `${o.app}/${o.route}` === key;
});
data.state = data.state === 'on';
data.isJump = data.process === 'TO_VALIDATE_CODE';
}
await ctx.render('action/add_risk', {
data: data
});
},
setter: async(ctx) => {
let params = ctx.request.body;
let path = '/risk';
let str = `${params.app}/${params.route}`;
let oldRet = await zookeeperModel.getPath('/risk');
if (!oldRet) {
oldRet = [];
} else {
oldRet = JSON.parse(oldRet);
}
_.remove(oldRet, function(o) {
return `${o.app}/${o.route}` === str;
});
oldRet.push(params);
let result = await zookeeperModel.setPath(path, JSON.stringify(oldRet));
if (result) {
ctx.body = {
code: 200,
message: 'update success'
};
} else {
ctx.body = {
code: 500,
message: 'update fail,Please retry'
}
}
},
remove: async(ctx) => {
let key = ctx.request.body.key;
let path = '/risk';
let oldRet = await zookeeperModel.getPath(path);
if (oldRet) {
oldRet = JSON.parse(oldRet);
}
_.remove(oldRet, function(o) {
return `${o.app}/${o.route}` === key;
});
let result = await zookeeperModel.setPath(path, JSON.stringify(oldRet));
if (result) {
ctx.body = {
code: 200,
message: 'remove success'
};
} else {
ctx.body = {
code: 500,
message: 'remove fail,Please retry'
}
}
}
};
r.get('/risk_management', risk.index);
r.get('/add_risk', risk.add);
r.post('/add_risk_data', risk.setter);
r.post('/setter', risk.setter);
r.post('/remove', risk.remove);
module.exports = r;
\ No newline at end of file
... ...
... ... @@ -27,6 +27,13 @@ class ZookeeperModel extends model {
return null;
}
async removePath(path) {
let degradeServer = await this.getConfigAll();
if (degradeServer[0]) {
return await zookeeperHelpers.remove(`${degradeServer[0].ip}:${degradeServer[0].port}`, path);
}
}
}
module.exports = ZookeeperModel;
... ...
... ... @@ -28,8 +28,6 @@ const base = new Router();
const file = require('./actions/file');
const riskManagement = require('./actions/risk_management');
console.log(riskManagement)
module.exports = function(app) {
... ...
... ... @@ -17,29 +17,30 @@
<!-- pageheader -->
<div class="contentpanel servers-page form-horizontal">
{{#data}}
<div class="form-group">
<label for="app" class="col-sm-2 control-label text-center">应用名称:</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="app" placeholder="请输入应用名称">
<input type="text" class="form-control" id="app" placeholder="请输入应用名称" value="{{app}}">
</div>
</div>
<div class="form-group">
<label for="router" class="col-sm-2 control-label text-center">路由路径:</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="router" placeholder="请输入路由路径">
<input type="text" class="form-control" id="router" placeholder="请输入路由路径" value="{{route}}">
</div>
</div>
<div class="form-group">
<label for="exampleFormControlInput1" class="col-sm-2 control-label text-center">时间间隔:</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="router" placeholder="请输入时间间隔">
<input type="text" class="form-control" id="interval" placeholder="请输入时间间隔" value="{{interval}}">
</div>
<div class="col-sm-2" style="line-height: 40px;"></div>
</div>
<div class="form-group">
<label for="exampleFormControlInput1" class="col-sm-2 control-label text-center">请求次数:</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="router" placeholder="请输入请求次数">
<input type="text" class="form-control" id="requests" placeholder="请输入请求次数" value="{{requests}}">
</div>
<div class="col-sm-2" style="line-height: 40px;"></div>
</div>
... ... @@ -47,8 +48,8 @@
<label for="process" class="col-sm-2 control-label text-center">处理方式:</label>
<div class="col-sm-6">
<select class="form-control" style="height: 40px; line-height: 40px;" id="process">
<option value="TO_VALIDATE_CODE">跳转全局验证码解锁</option>
<option value="REDIRECT_403">返回403</option>
<option value="TO_VALIDATE_CODE" {{#if isJump}}selected{{/if}}>跳转全局验证码解锁</option>
<option value="REDIRECT_403" {{#unless isJump}}selected{{/unless}}>返回403</option>
</select>
</div>
</div>
... ... @@ -56,13 +57,14 @@
<label class="col-sm-2 control-label text-center">状  态:</label>
<div class="col-sm-10">
<label class="radio-inline">
<input type="radio" name="status" id="status-on" value="on" checked>开启
<input type="radio" name="status" id="status-on" value="on" {{#if state}}checked{{/if}}>开启
</label>
<label class="radio-inline">
<input type="radio" name="status" id="status-off" value="off">关闭
<input type="radio" name="status" id="status-off" value="off"{{#unless state}}checked{{/unless}}>关闭
</label>
</div>
</div>
{{/data}}
<div class="row">
<label class="col-sm-2"></label>
<div class="col-sm-10">
... ... @@ -73,7 +75,21 @@
</div>
<script>
$('.save').on('click', function() {
$.post('/abuse_protection/unlock', {remoteIp: $(this).data('ip')}, function() {
$.post('/risk_management/setter', {
app: $('#app').val(),
route: $('#router').val(),
interval: $('#interval').val(),
requests: $('#requests').val(),
process: $('#process').val(),
state: $('input[type=radio]:checked').val(),
regRoute: $('#router').val().replace('/', '\\/')
}, function(ret) {
if (ret.code === 200) {
alert('添加成功!');
location.href = '/risk_management/risk_management';
} else {
alert('添加失败');
}
});
});
</script>
\ No newline at end of file
... ...
... ... @@ -32,13 +32,13 @@
<tbody>
{{#list}}
<tr>
<td>{{app}}</td>
<td>{{route}}</td>
<td class="app">{{app}}</td>
<td class="route">{{route}}</td>
<td>{{interval}}</td>
<td>{{requests}}</td>
<td>{{process}}</td>
<td>{{state}}</td>
<td><button>编辑</button><button>删除</button></td>
<td><button type="button" class="btn btn-success edit">编辑</button><button type="button" class="btn btn-danger ml10 del">删除</button></td>
</tr>
{{/list}}
... ... @@ -55,4 +55,25 @@
$('.add').on('click', function() {
location.href = '/risk_management/add_risk';
});
$('.del').on('click', function() {
var key = $(this).parents('tr').find('.app').html() + '/' + $(this).parents('tr').find('.route').html();
$.post('/risk_management/remove', {
key: key
}, function(ret) {
if (ret.code === 200) {
alert('删除成功!');
location.reload();
} else {
alert('删除失败!');
}
});
});
$('.edit').on('click', function() {
var key = $(this).parents('tr').find('.app').html() + '/' + $(this).parents('tr').find('.route').html();
location.href = '/risk_management/add_risk?key=' + key;
});
</script>
\ No newline at end of file
... ...