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

View File

@ -10,19 +10,11 @@ import Container from 'typedi';
import OpenService from '../services/open'; import OpenService from '../services/open';
import rewrite from 'express-urlrewrite'; import rewrite from 'express-urlrewrite';
import UserService from '../services/user'; import UserService from '../services/user';
import handler from 'serve-handler';
import * as Sentry from '@sentry/node'; import * as Sentry from '@sentry/node';
import { EnvModel } from '../data/env'; import { EnvModel } from '../data/env';
import { errors } from 'celebrate'; import { errors } from 'celebrate';
import path from 'path';
import { createProxyMiddleware } from 'http-proxy-middleware'; import { createProxyMiddleware } from 'http-proxy-middleware';
import { serveEnv } from '../config/serverEnv'; 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 }) => { export default ({ app }: { app: Application }) => {
app.enable('trust proxy'); app.enable('trust proxy');
@ -131,7 +123,6 @@ export default ({ app }: { app: Application }) => {
}); });
app.use(rewrite('/open/*', '/api/$1')); app.use(rewrite('/open/*', '/api/$1'));
app.use('/api', limiter)
app.use(config.api.prefix, routes()); app.use(config.api.prefix, routes());
app.use((req, res, next) => { 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( app.use(
( (
err: Error & { status: number }, err: Error & { status: number },

View File

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

View File

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

File diff suppressed because it is too large Load Diff