修改 api 限流策略,修复检查检查日志

This commit is contained in:
whyour 2023-06-07 22:45:37 +08:00
parent f64ff83414
commit 5b1accffb7
5 changed files with 1792 additions and 1879 deletions

View File

@ -6,6 +6,7 @@ import { celebrate, Joi } from 'celebrate';
import multer from 'multer';
import path from 'path';
import { v4 as uuidV4 } from 'uuid';
import rateLimit from 'express-rate-limit';
import config from '../config';
const route = Router();
@ -26,6 +27,10 @@ export default (app: Router) => {
route.post(
'/login',
rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
}),
celebrate({
body: Joi.object({
username: Joi.string().required(),

View File

@ -10,19 +10,11 @@ import Container from 'typedi';
import OpenService from '../services/open';
import rewrite from 'express-urlrewrite';
import UserService from '../services/user';
import handler from 'serve-handler';
import * as Sentry from '@sentry/node';
import { EnvModel } from '../data/env';
import { errors } from 'celebrate';
import path from 'path';
import { createProxyMiddleware } from 'http-proxy-middleware';
import { serveEnv } from '../config/serverEnv';
import rateLimit from 'express-rate-limit'
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
})
export default ({ app }: { app: Application }) => {
app.enable('trust proxy');
@ -131,7 +123,6 @@ export default ({ app }: { app: Application }) => {
});
app.use(rewrite('/open/*', '/api/$1'));
app.use('/api', limiter)
app.use(config.api.prefix, routes());
app.use((req, res, next) => {
@ -180,18 +171,6 @@ export default ({ app }: { app: Application }) => {
},
);
app.use(
Sentry.Handlers.errorHandler({
shouldHandleError(error) {
// 排除 SequelizeUniqueConstraintError / NotFound
return (
!['SequelizeUniqueConstraintError'].includes(error.name) ||
!['Not Found'].includes(error.message)
);
},
}),
);
app.use(
(
err: Error & { status: number },

View File

@ -10,12 +10,18 @@ export default async ({ expressApp }: { expressApp: Application }) => {
const { version } = await parseVersion(config.versionFile);
Sentry.init({
ignoreErrors: [
/SequelizeUniqueConstraintError/i,
/Validation error/i,
/UnauthorizedError/i,
/celebrate request validation failed/i,
],
dsn: 'https://f4b5b55fb3c645b29a5dc2d70a1a4ef4@o1098464.ingest.sentry.io/6122819',
integrations: [
new Sentry.Integrations.Http({ tracing: true }),
new Tracing.Integrations.Express({ app: expressApp }),
],
tracesSampleRate: 0.1,
tracesSampleRate: 0.8,
release: version,
});

View File

@ -10,19 +10,22 @@ const check = async (
switch (call.request.service) {
case 'cron':
const res = await promiseExec(
`curl -sf http://localhost:${config.port}/api/system`,
`curl -s http://localhost:${config.port}/api/system`,
);
if (res.includes('200')) {
return callback(null, { status: 1 });
}
const panelErrLog = await promiseExec(
`tail -n 300 ~/.pm2/logs/panel-error.log`,
);
const scheduleErrLog = await promiseExec(
`tail -n 300 ~/.pm2/logs/schedule-error.log`,
);
return callback(new Error(`${scheduleErrLog}\n${panelErrLog}`));
return callback(
new Error(`${scheduleErrLog || ''}\n${panelErrLog || ''}\n${res}`),
);
default:
return callback(null, { status: 1 });

File diff suppressed because it is too large Load Diff