mirror of
https://github.com/whyour/qinglong.git
synced 2025-07-27 14:46:06 +08:00
修复cron格式验证
This commit is contained in:
parent
c3797e70db
commit
fd26507bc5
|
@ -1,10 +1,9 @@
|
||||||
import { Router, Request, Response, NextFunction } from 'express';
|
import { Router, Request, Response, NextFunction } from 'express';
|
||||||
import { Container } from 'typedi';
|
import { Container } from 'typedi';
|
||||||
import { Logger } from 'winston';
|
import { Logger } from 'winston';
|
||||||
import * as fs from 'fs';
|
|
||||||
import config from '../config';
|
|
||||||
import CronService from '../services/cron';
|
import CronService from '../services/cron';
|
||||||
import { celebrate, Joi } from 'celebrate';
|
import { celebrate, Joi } from 'celebrate';
|
||||||
|
import cron_parser from 'cron-parser';
|
||||||
const route = Router();
|
const route = Router();
|
||||||
|
|
||||||
export default (app: Router) => {
|
export default (app: Router) => {
|
||||||
|
@ -32,15 +31,19 @@ export default (app: Router) => {
|
||||||
body: Joi.object({
|
body: Joi.object({
|
||||||
command: Joi.string().required(),
|
command: Joi.string().required(),
|
||||||
schedule: Joi.string().required(),
|
schedule: Joi.string().required(),
|
||||||
name: Joi.string(),
|
name: Joi.string().optional(),
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
async (req: Request, res: Response, next: NextFunction) => {
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
const logger: Logger = Container.get('logger');
|
const logger: Logger = Container.get('logger');
|
||||||
try {
|
try {
|
||||||
const cronService = Container.get(CronService);
|
if (cron_parser.parseExpression(req.body.schedule).hasNext()) {
|
||||||
const data = await cronService.create(req.body);
|
const cronService = Container.get(CronService);
|
||||||
return res.send({ code: 200, data });
|
const data = await cronService.create(req.body);
|
||||||
|
return res.send({ code: 200, data });
|
||||||
|
} else {
|
||||||
|
return res.send({ code: 400, message: 'param schedule error' });
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error('🔥 error: %o', e);
|
logger.error('🔥 error: %o', e);
|
||||||
return next(e);
|
return next(e);
|
||||||
|
@ -134,16 +137,20 @@ export default (app: Router) => {
|
||||||
body: Joi.object({
|
body: Joi.object({
|
||||||
command: Joi.string().required(),
|
command: Joi.string().required(),
|
||||||
schedule: Joi.string().required(),
|
schedule: Joi.string().required(),
|
||||||
name: Joi.string(),
|
name: Joi.string().optional(),
|
||||||
_id: Joi.string().required(),
|
_id: Joi.string().required(),
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
async (req: Request, res: Response, next: NextFunction) => {
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
const logger: Logger = Container.get('logger');
|
const logger: Logger = Container.get('logger');
|
||||||
try {
|
try {
|
||||||
const cronService = Container.get(CronService);
|
if (cron_parser.parseExpression(req.body.schedule).hasNext()) {
|
||||||
const data = await cronService.update(req.body);
|
const cronService = Container.get(CronService);
|
||||||
return res.send({ code: 200, data });
|
const data = await cronService.update(req.body);
|
||||||
|
return res.send({ code: 200, data });
|
||||||
|
} else {
|
||||||
|
return res.send({ code: 400, message: 'param schedule error' });
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error('🔥 error: %o', e);
|
logger.error('🔥 error: %o', e);
|
||||||
return next(e);
|
return next(e);
|
||||||
|
|
|
@ -49,7 +49,10 @@ export default ({ app }: { app: Application }) => {
|
||||||
next: NextFunction,
|
next: NextFunction,
|
||||||
) => {
|
) => {
|
||||||
if (err.name === 'UnauthorizedError') {
|
if (err.name === 'UnauthorizedError') {
|
||||||
return res.status(err.status).send({ message: err.message }).end();
|
return res
|
||||||
|
.status(err.status)
|
||||||
|
.send({ code: 401, message: err.message })
|
||||||
|
.end();
|
||||||
}
|
}
|
||||||
return next(err);
|
return next(err);
|
||||||
},
|
},
|
||||||
|
@ -64,6 +67,7 @@ export default ({ app }: { app: Application }) => {
|
||||||
) => {
|
) => {
|
||||||
res.status(err.status || 500);
|
res.status(err.status || 500);
|
||||||
res.json({
|
res.json({
|
||||||
|
code: err.status || 500,
|
||||||
message: err.message,
|
message: err.message,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
|
@ -229,11 +229,11 @@ export default class CronService {
|
||||||
var command = line.replace(regex, '').trim();
|
var command = line.replace(regex, '').trim();
|
||||||
var schedule = line.replace(command, '').trim();
|
var schedule = line.replace(command, '').trim();
|
||||||
|
|
||||||
var is_valid = false;
|
if (
|
||||||
try {
|
command &&
|
||||||
is_valid = cron_parser.parseString(line).expressions.length > 0;
|
schedule &&
|
||||||
} catch (e) {}
|
cron_parser.parseExpression(schedule).hasNext()
|
||||||
if (command && schedule && is_valid) {
|
) {
|
||||||
var name = namePrefix + '_' + index;
|
var name = namePrefix + '_' + index;
|
||||||
|
|
||||||
this.cronDb.findOne({ command, schedule }, (err, doc) => {
|
this.cronDb.findOne({ command, schedule }, (err, doc) => {
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
"body-parser": "^1.19.0",
|
"body-parser": "^1.19.0",
|
||||||
"celebrate": "^13.0.3",
|
"celebrate": "^13.0.3",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"cron-parser": "^3.3.0",
|
"cron-parser": "^3.5.0",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"express-jwt": "^6.0.0",
|
"express-jwt": "^6.0.0",
|
||||||
|
|
|
@ -80,7 +80,7 @@ const CronModal = ({
|
||||||
{ required: true },
|
{ required: true },
|
||||||
{
|
{
|
||||||
validator: (rule, value) => {
|
validator: (rule, value) => {
|
||||||
if (cronParse.parseString(value).expressions.length > 0) {
|
if (cronParse.parseExpression(value).hasNext()) {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
} else {
|
} else {
|
||||||
return Promise.reject('Cron表达式格式有误');
|
return Promise.reject('Cron表达式格式有误');
|
||||||
|
|
Loading…
Reference in New Issue
Block a user