Merge remote-tracking branch 'origin/feature/avoid-crawler'
# Conflicts: # apps/web/routers.js # apps/web/views/partials/common/menu.hbs
Showing
4 changed files
with
292 additions
and
1 deletions
apps/web/actions/crawler.js
0 → 100644
1 | +'use strict'; | ||
2 | + | ||
3 | +const Router = require('koa-router'); | ||
4 | + | ||
5 | +const Operation = require('../../logger/operation'); | ||
6 | +const { | ||
7 | + Server | ||
8 | +} = require('../../models'); | ||
9 | + | ||
10 | +const r = new Router; | ||
11 | + | ||
12 | +const {DegradeServer} = require('../../models'); | ||
13 | +const zookeeper = require('node-zookeeper-client'); | ||
14 | +const tester = require('../../zookeeper/tester'); | ||
15 | +const getter = require('../../zookeeper/getter'); | ||
16 | +const Model = require('../../models/model'); | ||
17 | +const _ = require('lodash'); | ||
18 | + | ||
19 | +const envs = { | ||
20 | + production: '线上环境', | ||
21 | + preview: '灰度环境', | ||
22 | + test: '测试环境' | ||
23 | +}; | ||
24 | + | ||
25 | +const servers = { | ||
26 | + ua:async(ctx, next)=>{ | ||
27 | + let ua_lists=await servers.getLists('/crawler/ua_blacklists');console.log(typeof ua_lists,ua_lists.length); | ||
28 | + try{ | ||
29 | + ua_lists=JSON.parse(ua_lists); | ||
30 | + }catch(e){ | ||
31 | + ua_lists=''; | ||
32 | + } | ||
33 | + await ctx.render('action/crawler', { | ||
34 | + blacklists: (ua_lists?ua_lists.map((item)=>{return {name:item}}):''), | ||
35 | + main_name:'UA' | ||
36 | + }); | ||
37 | + }, | ||
38 | + ip:async(ctx, next)=>{ | ||
39 | + let ip_lists=await servers.getLists('/crawler/ip_blacklists'); | ||
40 | + try{ | ||
41 | + ip_lists=JSON.parse(ip_lists); | ||
42 | + }catch(e){ | ||
43 | + ip_lists=''; | ||
44 | + } | ||
45 | + await ctx.render('action/crawler', { | ||
46 | + blacklists: (ip_lists?ip_lists.map((item)=>{return {name:item}}):''), | ||
47 | + main_name:'IP' | ||
48 | + }); | ||
49 | + }, | ||
50 | + change_ua:async(ctx, next)=>{ | ||
51 | + let result=await servers.setLists(ctx,'qcloud'); | ||
52 | + result=await servers.setLists(ctx,'aws')||result; | ||
53 | + if (result) { | ||
54 | + ctx.body = { | ||
55 | + code: 200, | ||
56 | + message: 'update success' | ||
57 | + }; | ||
58 | + } else { | ||
59 | + ctx.body = { | ||
60 | + code: 500, | ||
61 | + message: 'update fail,Please retry' | ||
62 | + } | ||
63 | + } | ||
64 | + }, | ||
65 | + change_ip:async(ctx, next)=>{ | ||
66 | + let result=await servers.setLists(ctx,'qcloud'); | ||
67 | + result=await servers.setLists(ctx,'aws')||result; | ||
68 | + if (result) { | ||
69 | + ctx.body = { | ||
70 | + code: 200, | ||
71 | + message: 'update success' | ||
72 | + }; | ||
73 | + } else { | ||
74 | + ctx.body = { | ||
75 | + code: 500, | ||
76 | + message: 'update fail,Please retry' | ||
77 | + } | ||
78 | + } | ||
79 | + }, | ||
80 | + getLists:async(path)=>{ | ||
81 | + let degrades=[]; | ||
82 | + | ||
83 | + let qcloudServer = await DegradeServer.findOne({ | ||
84 | + type: 'qcloud' | ||
85 | + }); | ||
86 | + | ||
87 | + if (qcloudServer) { | ||
88 | + degrades=degrades.concat(await servers.connect(qcloudServer.ip,qcloudServer.port,path)); | ||
89 | + } | ||
90 | + | ||
91 | + let awsServer = await DegradeServer.findOne({ | ||
92 | + type: 'aws' | ||
93 | + }); | ||
94 | + | ||
95 | + if (awsServer) { | ||
96 | + degrades=degrades.concat(await servers.connect(qcloudServer.ip,qcloudServer.port,path)); | ||
97 | + } | ||
98 | + | ||
99 | + return degrades[0]; | ||
100 | + }, | ||
101 | + async connect(ip,port,path) { | ||
102 | + let server = `${ip}:${port}`; | ||
103 | + | ||
104 | + let connected = await tester(server); | ||
105 | + | ||
106 | + if (!connected) { | ||
107 | + console.log('连接失败'); | ||
108 | + return; | ||
109 | + } | ||
110 | + | ||
111 | + const client = zookeeper.createClient(server); | ||
112 | + return new Promise((resolve,reject)=>{ | ||
113 | + client.once('connected', function (err) { | ||
114 | + if(err){ | ||
115 | + reject(err); | ||
116 | + }else{ | ||
117 | + client.getData( | ||
118 | + path, | ||
119 | + function (event) { | ||
120 | + console.log('Got event: %s.', event); | ||
121 | + }, | ||
122 | + function (error, data, stat) { | ||
123 | + if (error) { | ||
124 | + console.log(error.stack); | ||
125 | + reject(error); | ||
126 | + return; | ||
127 | + } | ||
128 | + resolve(data.toString('utf8')); | ||
129 | + } | ||
130 | + ); | ||
131 | + } | ||
132 | + }); | ||
133 | + client.connect(); | ||
134 | + | ||
135 | + }).then(data=>data).catch(()=>''); | ||
136 | + }, | ||
137 | + async setLists(ctx,type) { | ||
138 | + let {path, val} = ctx.query; | ||
139 | + | ||
140 | + let server = await DegradeServer.findOne({ | ||
141 | + type: type | ||
142 | + }); | ||
143 | + | ||
144 | + const client = zookeeper.createClient(`${server.ip}:${server.port}`); | ||
145 | + | ||
146 | + const mkdirp=()=>{ | ||
147 | + return new Promise((resolve,reject)=>{ | ||
148 | + client.mkdirp(path, new Buffer('false'), (err, path) => { | ||
149 | + if (err) { | ||
150 | + reject(err); | ||
151 | + } else { | ||
152 | + resolve(); | ||
153 | + } | ||
154 | + }); | ||
155 | + }); | ||
156 | + }; | ||
157 | + const setData=()=>{ | ||
158 | + return new Promise((resolve,reject)=>{ | ||
159 | + client.setData(path, new Buffer(val.toString()), function(err, data, stat) { | ||
160 | + if (err) { | ||
161 | + reject(err); | ||
162 | + } else { | ||
163 | + resolve(true); | ||
164 | + } | ||
165 | + }); | ||
166 | + }); | ||
167 | + } | ||
168 | + | ||
169 | + | ||
170 | + let result= new Promise((resolve,reject)=>{ | ||
171 | + client.once('connected', function (err) { | ||
172 | + if(err)reject(err); | ||
173 | + else resolve(); | ||
174 | + }); | ||
175 | + }) | ||
176 | + .then(mkdirp) | ||
177 | + .then(setData) | ||
178 | + .then((ok)=>{ | ||
179 | + client.close(); | ||
180 | + if(ok)return true; | ||
181 | + }) | ||
182 | + .catch(function(err){ | ||
183 | + console.log(err); | ||
184 | + client.close(); | ||
185 | + return false; | ||
186 | + }); | ||
187 | + | ||
188 | + client.connect(); | ||
189 | + return result; | ||
190 | + } | ||
191 | +}; | ||
192 | + | ||
193 | +r.get('/ua', servers.ua); | ||
194 | +r.get('/ip', servers.ip); | ||
195 | +r.get('/change_ua', servers.change_ua); | ||
196 | +r.get('/change_ip', servers.change_ip); | ||
197 | + | ||
198 | +module.exports = r; |
@@ -18,6 +18,7 @@ const degrade = require('./actions/degrade'); | @@ -18,6 +18,7 @@ const degrade = require('./actions/degrade'); | ||
18 | const deploy = require('./actions/deploy'); | 18 | const deploy = require('./actions/deploy'); |
19 | const api = require('./actions/api'); | 19 | const api = require('./actions/api'); |
20 | const abuseProtection = require('./actions/abuse_protection'); | 20 | const abuseProtection = require('./actions/abuse_protection'); |
21 | +const crawler = require('./actions/crawler'); | ||
21 | 22 | ||
22 | const noAuth = new Router(); | 23 | const noAuth = new Router(); |
23 | const base = new Router(); | 24 | const base = new Router(); |
@@ -50,6 +51,7 @@ module.exports = function (app) { | @@ -50,6 +51,7 @@ module.exports = function (app) { | ||
50 | base.use('/api_cache', apiCache.routes(), apiCache.allowedMethods()); | 51 | base.use('/api_cache', apiCache.routes(), apiCache.allowedMethods()); |
51 | base.use('/degrade', degrade.routes(), degrade.allowedMethods()); | 52 | base.use('/degrade', degrade.routes(), degrade.allowedMethods()); |
52 | base.use('/deploys', deploy.routes(), deploy.allowedMethods()); | 53 | base.use('/deploys', deploy.routes(), deploy.allowedMethods()); |
54 | + base.use('/crawler', crawler.routes(), crawler.allowedMethods()); | ||
53 | base.use('/abuse_protection', abuseProtection.routes(), degrade.allowedMethods()); | 55 | base.use('/abuse_protection', abuseProtection.routes(), degrade.allowedMethods()); |
54 | 56 | ||
55 | base.use('', index.routes(), index.allowedMethods()); | 57 | base.use('', index.routes(), index.allowedMethods()); |
apps/web/views/action/crawler.hbs
0 → 100644
1 | +<div class="pageheader"> | ||
2 | + <div class="media"> | ||
3 | + <div class="pageicon pull-left"> | ||
4 | + <i class="fa fa-th-list"></i> | ||
5 | + </div> | ||
6 | + <div class="media-body"> | ||
7 | + <ul class="breadcrumb"> | ||
8 | + <li><a href=""><i class="glyphicon glyphicon-home"></i></a></li> | ||
9 | + <li><a href="">防爬虫设置</a></li> | ||
10 | + <li>{{main_name}}黑名单</li> | ||
11 | + </ul> | ||
12 | + <h4>{{main_name}}黑名单设置</h4> | ||
13 | + </div> | ||
14 | + </div> | ||
15 | + <!-- media --> | ||
16 | +</div> | ||
17 | +<!-- pageheader --> | ||
18 | + | ||
19 | +<div class="contentpanel page-servers"> | ||
20 | + <div class="panel panel-primary-head"> | ||
21 | + <div class="panel-heading"> | ||
22 | + <h4 class="panel-title">{{main_name}}黑名单设置</h4> | ||
23 | + <p>配置黑名单列表</p> | ||
24 | + | ||
25 | + </div> | ||
26 | + <!-- panel-heading --> | ||
27 | + <div class="input-group" style="margin: 20px 20px 20px 0"> | ||
28 | + <input id="val" type="text" class="form-control" placeholder="请输入..." style="width: 500px;"> | ||
29 | + <span class="input-group-btn" style="float: left;"> | ||
30 | + <button class="btn btn-default" type="button" id="add_black">add</button> | ||
31 | + </span> | ||
32 | + </div> | ||
33 | + <table id="table-servers" class="table table-striped table-bordered responsive" style="border: 1px solid #ddd;"> | ||
34 | + <thead class=""> | ||
35 | + <tr> | ||
36 | + <th>{{main_name}}_name</th> | ||
37 | + <th>操作</th> | ||
38 | + </tr> | ||
39 | + </thead> | ||
40 | + | ||
41 | + <tbody> | ||
42 | + {{#each blacklists}} | ||
43 | + <tr> | ||
44 | + <td>{{name}}</td> | ||
45 | + <td> | ||
46 | + <button class="btn btn-danger btn-xs server-del">删除</button> | ||
47 | + </td> | ||
48 | + </tr> | ||
49 | + {{/each}} | ||
50 | + </tbody> | ||
51 | + </table> | ||
52 | + </div> | ||
53 | + <!-- panel --> | ||
54 | +</div> | ||
55 | + | ||
56 | + | ||
57 | +<script> | ||
58 | + $('#add_black').on('click',function (){ | ||
59 | + var val=$('#val').val(); | ||
60 | + if(!val)return; | ||
61 | + var dataId=$($("tbody").find("tr")[$("tbody").find("tr").length-1]).find('[data-id]').attr('data-id'); | ||
62 | + $('tbody').append('<tr><td>'+val+'</td><td><button class="btn btn-danger btn-xs server-del">删除</button></td></tr>'); | ||
63 | + $('#val').val(''); | ||
64 | + var path=location.pathname.match(/\/crawler\/(.*)/)[1]; | ||
65 | + | ||
66 | + var vallists=[]; | ||
67 | + $('tr td:first-child').each(function(){ | ||
68 | + vallists.push($(this).html()); | ||
69 | + }); | ||
70 | + vallists=JSON.stringify(vallists); | ||
71 | + $.get('/crawler/change_'+path+'?path=/crawler/'+path+'_blacklists&val='+vallists); | ||
72 | + }); | ||
73 | + | ||
74 | + $('tbody').on('click','.server-del',function(){ | ||
75 | + $(this).parent().parent().remove(); | ||
76 | + var path=location.pathname.match(/\/crawler\/(.*)/)[1]; | ||
77 | + | ||
78 | + var vallists=[]; | ||
79 | + $('tr td:first-child').each(function(){ | ||
80 | + vallists.push($(this).html()); | ||
81 | + }); | ||
82 | + vallists=(vallists.length?JSON.stringify(vallists):''); | ||
83 | + $.get('/crawler/change_'+path+'?path=/crawler/'+path+'_blacklists&val='+vallists);// | ||
84 | + }); | ||
85 | + | ||
86 | +</script> |
@@ -43,9 +43,14 @@ | @@ -43,9 +43,14 @@ | ||
43 | </li> | 43 | </li> |
44 | {{/if}} | 44 | {{/if}} |
45 | <li><a href="/degrade"><i class="fa fa-hand-o-down"></i> <span>降级配置</span></a></li> | 45 | <li><a href="/degrade"><i class="fa fa-hand-o-down"></i> <span>降级配置</span></a></li> |
46 | - | ||
47 | <li><a href="/abuse_protection/abuse_protection"><i class="fa fa-shield"></i> <span>滥用防护</span></a></li> | 46 | <li><a href="/abuse_protection/abuse_protection"><i class="fa fa-shield"></i> <span>滥用防护</span></a></li> |
48 | 47 | ||
48 | + <li class="parent"><a><i class="fa fa-eye"></i> <span>防爬虫设置</span></a> | ||
49 | + <ul class="children"> | ||
50 | + <li><a href="/crawler/ua">UA黑名单</a></li> | ||
51 | + <li><a href="/crawler/ip">IP黑名单</a></li> | ||
52 | + </ul> | ||
53 | + </li> | ||
49 | </ul> | 54 | </ul> |
50 | 55 | ||
51 | </div> | 56 | </div> |
-
Please register or login to post a comment