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

task创建增加ip限制

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