Commit 20c1744f authored by 姜登's avatar 姜登

task创建增加ip限制

parent 1b4332f6
Pipeline #18479 passed with stage
in 6 seconds
/* /*
* @Descripttion: * @Descripttion:
* @version: * @version:
* @Author: jd * @Author: jd
* @Date: 2019-02-25 18:59:53 * @Date: 2019-02-25 18:59:53
* @LastEditors: jd * @LastEditors : jd
* @LastEditTime: 2019-11-27 16:35:42 * @LastEditTime : 2019-12-24 14:17:15
*/ */
'use strict'; 'use strict';
...@@ -49,6 +49,7 @@ class TaskController extends Controller { ...@@ -49,6 +49,7 @@ class TaskController extends Controller {
const { ctx, service } = this; const { ctx, service } = this;
ctx.validate(this.createRule); ctx.validate(this.createRule);
const { scriptId, orderId } = ctx.request.body; const { scriptId, orderId } = ctx.request.body;
await service.task.iplimit();
const taskId = await service.task.create(scriptId); const taskId = await service.task.create(scriptId);
await service.order.update({ orderId, cityId: scriptId, taskId: String(taskId) }); await service.order.update({ orderId, cityId: scriptId, taskId: String(taskId) });
await service.cache.set({ await service.cache.set({
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
* @Author: jd * @Author: jd
* @Date: 2019-02-25 18:59:53 * @Date: 2019-02-25 18:59:53
* @LastEditors : jd * @LastEditors : jd
* @LastEditTime : 2019-12-21 17:12:50 * @LastEditTime : 2019-12-24 14:17:22
*/ */
'use strict'; 'use strict';
...@@ -16,6 +16,7 @@ class CacheService extends Service { ...@@ -16,6 +16,7 @@ class CacheService extends Service {
super(ctx); super(ctx);
const { config } = this; const { config } = this;
const { taskPrefix } = config.lockKeys; const { taskPrefix } = config.lockKeys;
this.ipLimitNum = config.ipLimitNum;
this.taskPrefix = taskPrefix; this.taskPrefix = taskPrefix;
} }
...@@ -26,7 +27,7 @@ class CacheService extends Service { ...@@ -26,7 +27,7 @@ class CacheService extends Service {
* - exprie: 过期时间 * - exprie: 过期时间
*/ */
async set({ key, value = {}, exprie = 300 }) { async set({ key, value = {}, exprie = 300 }) {
const { ctx, app, taskPrefix } = this; const { ctx, app, taskPrefix, ipLimitNum } = this;
const data = await app.redis.get(taskPrefix + key); const data = await app.redis.get(taskPrefix + key);
if (!data || (data && data.status !== 'success')) { if (!data || (data && data.status !== 'success')) {
...@@ -38,24 +39,24 @@ class CacheService extends Service { ...@@ -38,24 +39,24 @@ class CacheService extends Service {
where: { taskId: key }, where: { taskId: key },
}); });
if (order && order.status !== 'success') { if (order && order.status !== 'success') {
if (order.appKey === '3BA01E15AC6742E2883ADA4817840D5F' && value.status === 'login') { if (order.appKey === '9FBBA75BBFC24401AA6A76AEFC277346' && value.status === 'login') {
const ip = ctx.request.ip.split(','); const ip = ctx.request.ip.split(',');
const iplimit = await app.redis.get(ip[0]); const iplimit = await app.redis.get(ip[0]);
if (ip[0] !== '101.71.242.179' && ip[0] !== '122.224.130.226') { // if (ip[0] !== '101.71.242.179' && ip[0] !== '122.224.130.226') {
if (!iplimit) { if (!iplimit) {
await app.redis.set(ip[0], JSON.stringify({ count: 1 }), 'EX', 15 * 3600); await app.redis.set(ip[0], JSON.stringify({ count: 1 }), 'EX', 15 * 3600);
ctx.logger.info(`ipLimit set ${ip[0]} value:`, 1); ctx.logger.info(`ipLimit set ${ip[0]} value:`, 1);
} else { } else {
const { count } = JSON.parse(iplimit); const { count } = JSON.parse(iplimit);
await app.redis.set(ip[0], JSON.stringify({ count: count + 1 }), 'EX', 15 * 3600); await app.redis.set(ip[0], JSON.stringify({ count: count + 1 }), 'EX', 15 * 3600);
ctx.logger.info(`ipLimit ADD ${ip[0]} value:`, count + 1); ctx.logger.info(`ipLimit ADD ${ip[0]} value:`, count + 1);
if (count + 1 > 21) { if (count + 1 > ipLimitNum) {
ctx.logger.info(`ipLimit ${ip[0]} MAX30LIMIT 提交任务失败`); ctx.logger.info(`ipLimit ${ip[0]} MAXLIMIT 提交任务失败`);
ctx.throw(400, { message: '提交任务失败' }); ctx.throw(400, { message: '提交任务失败' });
return; return;
}
} }
} }
// }
} }
await order.update({ ...value }); await order.update({ ...value });
} else { } else {
......
/*
* @Descripttion:
* @version:
* @Author: jd
* @Date: 2019-02-25 18:59:53
* @LastEditors : jd
* @LastEditTime : 2019-12-24 14:16:37
*/
'use strict'; 'use strict';
const Service = require('egg').Service; const Service = require('egg').Service;
...@@ -5,11 +13,12 @@ const Service = require('egg').Service; ...@@ -5,11 +13,12 @@ const Service = require('egg').Service;
class TaskService extends Service { class TaskService extends Service {
constructor(ctx) { constructor(ctx) {
super(ctx); super(ctx);
const { config: { taskAPI, LOCK_KEY } } = this; const { config: { taskAPI, LOCK_KEY, ipLimitNum } } = this;
this.taskAPI = taskAPI; this.taskAPI = taskAPI;
this.baseURL = taskAPI.host; this.baseURL = taskAPI.host;
this.citylistURL = taskAPI.citylist; this.citylistURL = taskAPI.citylist;
this.LOCK_KEY = LOCK_KEY; this.LOCK_KEY = LOCK_KEY;
this.ipLimitNum = ipLimitNum;
} }
...@@ -88,6 +97,23 @@ class TaskService extends Service { ...@@ -88,6 +97,23 @@ class TaskService extends Service {
ctx.logger.info(`【Task】fetchTask ${fetchTaskUrl} params`, JSON.stringify(data), JSON.stringify(result.data)); ctx.logger.info(`【Task】fetchTask ${fetchTaskUrl} params`, JSON.stringify(data), JSON.stringify(result.data));
return result.data; return result.data;
} }
async iplimit() {
const { ctx, app, ipLimitNum } = this;
const ip = ctx.request.ip.split(',');
// if (ip[0] !== '101.71.242.179' && ip[0] !== '122.224.130.226') {
const iplimit = await app.redis.get(ip[0]);
if (iplimit) {
const { count } = JSON.parse(iplimit);
await app.redis.set(ip[0], JSON.stringify({ count: count + 1 }), 'EX', 15 * 3600);
ctx.logger.info(`ipLimit ADD ${ip[0]} value:`, count + 1);
if (count + 1 > ipLimitNum) {
ctx.logger.info(`ipLimit ${ip[0]} MAXLIMIT 创建任务失败`);
ctx.throw(400, { message: '创建任务失败' });
return;
}
}
// }
}
} }
......
...@@ -28,7 +28,7 @@ module.exports = () => { ...@@ -28,7 +28,7 @@ module.exports = () => {
}; };
config.partnerAPI = { config.partnerAPI = {
host: 'https://uat-nginx.jianbing.com/zeus-api/v1', host: 'https://dev-nginx.jianbing.com/zeus-api/v1',
fetchTheme: '/chaos/theme', fetchTheme: '/chaos/theme',
fetchScripts: '/chaos/partners/scripts', fetchScripts: '/chaos/partners/scripts',
fetchAgreements: '/chaos/agreements', fetchAgreements: '/chaos/agreements',
...@@ -41,7 +41,7 @@ module.exports = () => { ...@@ -41,7 +41,7 @@ module.exports = () => {
}; };
config.scriptsAPI = { config.scriptsAPI = {
host: 'https://uat-nginx.jianbing.com/zeus-api/v1', host: 'https://dev-nginx.jianbing.com/zeus-api/v1',
fetchScriptsUrl: '/chaos/hf/two_dimension_array/queries', fetchScriptsUrl: '/chaos/hf/two_dimension_array/queries',
fetchOneScriptUrl: '/chaos/hf/two_dimension_array/info', fetchOneScriptUrl: '/chaos/hf/two_dimension_array/info',
fetchParamsInfoUrl: '/chaos/hf/login_param_map', fetchParamsInfoUrl: '/chaos/hf/login_param_map',
...@@ -86,6 +86,8 @@ module.exports = () => { ...@@ -86,6 +86,8 @@ module.exports = () => {
taskPrefix: 'URANUS.HF.TASK', taskPrefix: 'URANUS.HF.TASK',
}; };
config.ipLimitNum = 3;
config.sequelize = { config.sequelize = {
datasources: [{ datasources: [{
// 东八时区 // 东八时区
...@@ -105,7 +107,7 @@ module.exports = () => { ...@@ -105,7 +107,7 @@ module.exports = () => {
baseDir: 'model', baseDir: 'model',
dialect: 'mysql', dialect: 'mysql',
host: 'rm-bp1272001633qc0x9o.mysql.rds.aliyuncs.com', host: 'rm-bp1272001633qc0x9o.mysql.rds.aliyuncs.com',
database: 'data_service_uat', database: 'data_service_dev',
username: 'hexin', username: 'hexin',
password: 'gYUHszn9#q', password: 'gYUHszn9#q',
port: 3306, port: 3306,
......
...@@ -90,6 +90,8 @@ module.exports = () => { ...@@ -90,6 +90,8 @@ module.exports = () => {
taskPrefix: 'URANUS.HF.TASK', taskPrefix: 'URANUS.HF.TASK',
}; };
config.ipLimitNum = 3;
config.sequelize = { config.sequelize = {
datasources: [{ datasources: [{
// 东八时区 // 东八时区
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment