Authored by 王水玲

risk

@@ -4,72 +4,121 @@ const Router = require('koa-router'); @@ -4,72 +4,121 @@ const Router = require('koa-router');
4 const ApiCache = require('../../ci/api_cache'); 4 const ApiCache = require('../../ci/api_cache');
5 const _ = require('lodash'); 5 const _ = require('lodash');
6 6
7 -const getter = require('../../zookeeper/getter');  
8 -const setter = require('../../zookeeper/setter');  
9 -const tester = require('../../zookeeper/tester'); 7 +const ZookeeperModel = require('../models/zookeeperModel');
  8 +let zookeeperModel = new ZookeeperModel();
10 9
11 let r = new Router(); 10 let r = new Router();
  11 +var process = {
  12 + TO_VALIDATE_CODE: '跳转全局验证码解锁',
  13 + REDIRECT_403: '返回403'
  14 +};
12 15
13 const risk = { 16 const risk = {
14 index: async(ctx, next) => { 17 index: async(ctx, next) => {
15 - // const regexp = /pc:limiter:faker:(.*)/;  
16 - // const threshold = ctx.request.query.threshold || 100;  
17 - // const limit = ctx.request.query.limit || 10;  
18 -  
19 - // let hosts = await MemcachedHost.findAll();  
20 -  
21 - // const selectedHosts = _.filter(hosts, host => {  
22 - // const isCurrent = host.host === ctx.request.query.node;  
23 - // if (isCurrent) {  
24 - // host.isCurrent = true;  
25 - // }  
26 - // return isCurrent;  
27 - // });  
28 -  
29 - // let results = await Promise.all(_.map(selectedHosts, (h) => {  
30 - // return (new ApiCache(h.host)).find((key) => {  
31 - // return regexp.test(key);  
32 - // });  
33 - // }));  
34 -  
35 - // let list = [];  
36 -  
37 - // if (results && results[0]) {  
38 - // Object.keys(results[0]).forEach((key) => {  
39 - // const index = results[0][key];  
40 -  
41 - // if (index > threshold) {  
42 - // list.push({  
43 - // ip: ((key) => {  
44 - // const m = key.match(regexp);  
45 -  
46 - // return m && m.length > 0 ? m[1] : 'Unknown';  
47 - // })(key),  
48 - // index: index  
49 - // })  
50 - // }  
51 - // });  
52 - // }  
53 -  
54 - // list = _.orderBy(list, (item) => {  
55 - // return item.index;  
56 - // }, 'desc').slice(0, limit);  
57 - console.log(11111) 18 + let ret = await zookeeperModel.getPath('/risk');
  19 +
  20 + if (ret) {
  21 + ret = JSON.parse(ret);
  22 + }
  23 +
  24 + _.each(ret, function(o) {
  25 + o.process = process[o.process];
  26 + });
58 27
59 await ctx.render('action/risk_management', { 28 await ctx.render('action/risk_management', {
  29 + list: ret
60 }); 30 });
61 }, 31 },
62 add: async(ctx, next) => { 32 add: async(ctx, next) => {
  33 + var data = {};
  34 + var key = ctx.request.query.key;
  35 + var oldRet = '';
  36 +
  37 + if (key) {
  38 + oldRet = await zookeeperModel.getPath('/risk');
  39 +
  40 + if (oldRet) {
  41 + oldRet = JSON.parse(oldRet);
  42 + }
  43 +
  44 + data = _.find(oldRet, function(o) {
  45 + return `${o.app}/${o.route}` === key;
  46 + });
  47 +
  48 + data.state = data.state === 'on';
  49 + data.isJump = data.process === 'TO_VALIDATE_CODE';
  50 + }
  51 +
63 await ctx.render('action/add_risk', { 52 await ctx.render('action/add_risk', {
  53 + data: data
64 }); 54 });
65 }, 55 },
66 setter: async(ctx) => { 56 setter: async(ctx) => {
  57 + let params = ctx.request.body;
  58 + let path = '/risk';
  59 + let str = `${params.app}/${params.route}`;
  60 + let oldRet = await zookeeperModel.getPath('/risk');
  61 +
  62 + if (!oldRet) {
  63 + oldRet = [];
  64 + } else {
  65 + oldRet = JSON.parse(oldRet);
  66 + }
  67 +
  68 + _.remove(oldRet, function(o) {
  69 + return `${o.app}/${o.route}` === str;
  70 + });
  71 +
  72 + oldRet.push(params);
  73 +
  74 + let result = await zookeeperModel.setPath(path, JSON.stringify(oldRet));
  75 +
  76 +
  77 + if (result) {
  78 + ctx.body = {
  79 + code: 200,
  80 + message: 'update success'
  81 + };
  82 + } else {
  83 + ctx.body = {
  84 + code: 500,
  85 + message: 'update fail,Please retry'
  86 + }
  87 + }
  88 + },
  89 + remove: async(ctx) => {
  90 + let key = ctx.request.body.key;
  91 + let path = '/risk';
  92 + let oldRet = await zookeeperModel.getPath(path);
  93 +
  94 + if (oldRet) {
  95 + oldRet = JSON.parse(oldRet);
  96 + }
  97 +
  98 + _.remove(oldRet, function(o) {
  99 + return `${o.app}/${o.route}` === key;
  100 + });
67 101
  102 + let result = await zookeeperModel.setPath(path, JSON.stringify(oldRet));
  103 +
  104 +
  105 + if (result) {
  106 + ctx.body = {
  107 + code: 200,
  108 + message: 'remove success'
  109 + };
  110 + } else {
  111 + ctx.body = {
  112 + code: 500,
  113 + message: 'remove fail,Please retry'
  114 + }
  115 + }
68 } 116 }
69 }; 117 };
70 118
71 r.get('/risk_management', risk.index); 119 r.get('/risk_management', risk.index);
72 r.get('/add_risk', risk.add); 120 r.get('/add_risk', risk.add);
73 -r.post('/add_risk_data', risk.setter); 121 +r.post('/setter', risk.setter);
  122 +r.post('/remove', risk.remove);
74 123
75 module.exports = r; 124 module.exports = r;
@@ -27,6 +27,13 @@ class ZookeeperModel extends model { @@ -27,6 +27,13 @@ class ZookeeperModel extends model {
27 27
28 return null; 28 return null;
29 } 29 }
  30 + async removePath(path) {
  31 + let degradeServer = await this.getConfigAll();
  32 +
  33 + if (degradeServer[0]) {
  34 + return await zookeeperHelpers.remove(`${degradeServer[0].ip}:${degradeServer[0].port}`, path);
  35 + }
  36 + }
30 } 37 }
31 38
32 module.exports = ZookeeperModel; 39 module.exports = ZookeeperModel;
@@ -28,8 +28,6 @@ const base = new Router(); @@ -28,8 +28,6 @@ const base = new Router();
28 const file = require('./actions/file'); 28 const file = require('./actions/file');
29 const riskManagement = require('./actions/risk_management'); 29 const riskManagement = require('./actions/risk_management');
30 30
31 -console.log(riskManagement)  
32 -  
33 31
34 module.exports = function(app) { 32 module.exports = function(app) {
35 33
@@ -17,29 +17,30 @@ @@ -17,29 +17,30 @@
17 <!-- pageheader --> 17 <!-- pageheader -->
18 18
19 <div class="contentpanel servers-page form-horizontal"> 19 <div class="contentpanel servers-page form-horizontal">
  20 + {{#data}}
20 <div class="form-group"> 21 <div class="form-group">
21 <label for="app" class="col-sm-2 control-label text-center">应用名称:</label> 22 <label for="app" class="col-sm-2 control-label text-center">应用名称:</label>
22 <div class="col-sm-6"> 23 <div class="col-sm-6">
23 - <input type="text" class="form-control" id="app" placeholder="请输入应用名称"> 24 + <input type="text" class="form-control" id="app" placeholder="请输入应用名称" value="{{app}}">
24 </div> 25 </div>
25 </div> 26 </div>
26 <div class="form-group"> 27 <div class="form-group">
27 <label for="router" class="col-sm-2 control-label text-center">路由路径:</label> 28 <label for="router" class="col-sm-2 control-label text-center">路由路径:</label>
28 <div class="col-sm-6"> 29 <div class="col-sm-6">
29 - <input type="text" class="form-control" id="router" placeholder="请输入路由路径"> 30 + <input type="text" class="form-control" id="router" placeholder="请输入路由路径" value="{{route}}">
30 </div> 31 </div>
31 </div> 32 </div>
32 <div class="form-group"> 33 <div class="form-group">
33 <label for="exampleFormControlInput1" class="col-sm-2 control-label text-center">时间间隔:</label> 34 <label for="exampleFormControlInput1" class="col-sm-2 control-label text-center">时间间隔:</label>
34 <div class="col-sm-6"> 35 <div class="col-sm-6">
35 - <input type="text" class="form-control" id="router" placeholder="请输入时间间隔"> 36 + <input type="text" class="form-control" id="interval" placeholder="请输入时间间隔" value="{{interval}}">
36 </div> 37 </div>
37 <div class="col-sm-2" style="line-height: 40px;"></div> 38 <div class="col-sm-2" style="line-height: 40px;"></div>
38 </div> 39 </div>
39 <div class="form-group"> 40 <div class="form-group">
40 <label for="exampleFormControlInput1" class="col-sm-2 control-label text-center">请求次数:</label> 41 <label for="exampleFormControlInput1" class="col-sm-2 control-label text-center">请求次数:</label>
41 <div class="col-sm-6"> 42 <div class="col-sm-6">
42 - <input type="text" class="form-control" id="router" placeholder="请输入请求次数"> 43 + <input type="text" class="form-control" id="requests" placeholder="请输入请求次数" value="{{requests}}">
43 </div> 44 </div>
44 <div class="col-sm-2" style="line-height: 40px;"></div> 45 <div class="col-sm-2" style="line-height: 40px;"></div>
45 </div> 46 </div>
@@ -47,8 +48,8 @@ @@ -47,8 +48,8 @@
47 <label for="process" class="col-sm-2 control-label text-center">处理方式:</label> 48 <label for="process" class="col-sm-2 control-label text-center">处理方式:</label>
48 <div class="col-sm-6"> 49 <div class="col-sm-6">
49 <select class="form-control" style="height: 40px; line-height: 40px;" id="process"> 50 <select class="form-control" style="height: 40px; line-height: 40px;" id="process">
50 - <option value="TO_VALIDATE_CODE">跳转全局验证码解锁</option>  
51 - <option value="REDIRECT_403">返回403</option> 51 + <option value="TO_VALIDATE_CODE" {{#if isJump}}selected{{/if}}>跳转全局验证码解锁</option>
  52 + <option value="REDIRECT_403" {{#unless isJump}}selected{{/unless}}>返回403</option>
52 </select> 53 </select>
53 </div> 54 </div>
54 </div> 55 </div>
@@ -56,13 +57,14 @@ @@ -56,13 +57,14 @@
56 <label class="col-sm-2 control-label text-center">状  态:</label> 57 <label class="col-sm-2 control-label text-center">状  态:</label>
57 <div class="col-sm-10"> 58 <div class="col-sm-10">
58 <label class="radio-inline"> 59 <label class="radio-inline">
59 - <input type="radio" name="status" id="status-on" value="on" checked>开启 60 + <input type="radio" name="status" id="status-on" value="on" {{#if state}}checked{{/if}}>开启
60 </label> 61 </label>
61 <label class="radio-inline"> 62 <label class="radio-inline">
62 - <input type="radio" name="status" id="status-off" value="off">关闭 63 + <input type="radio" name="status" id="status-off" value="off"{{#unless state}}checked{{/unless}}>关闭
63 </label> 64 </label>
64 </div> 65 </div>
65 </div> 66 </div>
  67 + {{/data}}
66 <div class="row"> 68 <div class="row">
67 <label class="col-sm-2"></label> 69 <label class="col-sm-2"></label>
68 <div class="col-sm-10"> 70 <div class="col-sm-10">
@@ -73,7 +75,21 @@ @@ -73,7 +75,21 @@
73 </div> 75 </div>
74 <script> 76 <script>
75 $('.save').on('click', function() { 77 $('.save').on('click', function() {
76 - $.post('/abuse_protection/unlock', {remoteIp: $(this).data('ip')}, function() { 78 + $.post('/risk_management/setter', {
  79 + app: $('#app').val(),
  80 + route: $('#router').val(),
  81 + interval: $('#interval').val(),
  82 + requests: $('#requests').val(),
  83 + process: $('#process').val(),
  84 + state: $('input[type=radio]:checked').val(),
  85 + regRoute: $('#router').val().replace('/', '\\/')
  86 + }, function(ret) {
  87 + if (ret.code === 200) {
  88 + alert('添加成功!');
  89 + location.href = '/risk_management/risk_management';
  90 + } else {
  91 + alert('添加失败');
  92 + }
77 }); 93 });
78 }); 94 });
79 </script> 95 </script>
@@ -32,13 +32,13 @@ @@ -32,13 +32,13 @@
32 <tbody> 32 <tbody>
33 {{#list}} 33 {{#list}}
34 <tr> 34 <tr>
35 - <td>{{app}}</td>  
36 - <td>{{route}}</td> 35 + <td class="app">{{app}}</td>
  36 + <td class="route">{{route}}</td>
37 <td>{{interval}}</td> 37 <td>{{interval}}</td>
38 <td>{{requests}}</td> 38 <td>{{requests}}</td>
39 <td>{{process}}</td> 39 <td>{{process}}</td>
40 <td>{{state}}</td> 40 <td>{{state}}</td>
41 - <td><button>编辑</button><button>删除</button></td> 41 + <td><button type="button" class="btn btn-success edit">编辑</button><button type="button" class="btn btn-danger ml10 del">删除</button></td>
42 </tr> 42 </tr>
43 {{/list}} 43 {{/list}}
44 44
@@ -55,4 +55,25 @@ @@ -55,4 +55,25 @@
55 $('.add').on('click', function() { 55 $('.add').on('click', function() {
56 location.href = '/risk_management/add_risk'; 56 location.href = '/risk_management/add_risk';
57 }); 57 });
  58 +
  59 + $('.del').on('click', function() {
  60 + var key = $(this).parents('tr').find('.app').html() + '/' + $(this).parents('tr').find('.route').html();
  61 +
  62 + $.post('/risk_management/remove', {
  63 + key: key
  64 + }, function(ret) {
  65 + if (ret.code === 200) {
  66 + alert('删除成功!');
  67 + location.reload();
  68 + } else {
  69 + alert('删除失败!');
  70 + }
  71 + });
  72 + });
  73 +
  74 + $('.edit').on('click', function() {
  75 + var key = $(this).parents('tr').find('.app').html() + '/' + $(this).parents('tr').find('.route').html();
  76 +
  77 + location.href = '/risk_management/add_risk?key=' + key;
  78 + });
58 </script> 79 </script>