feat: add bark push for cc

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-02-13 13:29:03 +08:00
parent 8a723ea487
commit 76002c3f24
2 changed files with 25 additions and 10 deletions

View File

@@ -18,6 +18,7 @@ import { HTTP_REQUEST_TIME } from '~/constants/meta.constant'
import { LOG_DIR } from '~/constants/path.constant'
import { REFLECTOR } from '~/constants/system.constant'
import { isDev } from '~/global/env.global'
import { BarkPushService } from '~/processors/helper/helper.bark.service'
import { EventManagerService } from '~/processors/helper/helper.event.service'
import { getIp } from '../../utils/ip.util'
@@ -39,6 +40,7 @@ export class AllExceptionsFilter implements ExceptionFilter {
constructor(
@Inject(REFLECTOR) private reflector: Reflector,
private readonly eventManager: EventManagerService,
private readonly barkService: BarkPushService,
) {
this.registerCatchAllExceptionsHook()
}
@@ -49,14 +51,6 @@ export class AllExceptionsFilter implements ExceptionFilter {
}
process.on('unhandledRejection', (reason: any) => {
console.error('unhandledRejection: ', reason)
// this.eventManager.broadcast(
// EventBusEvents.SystemException,
// { message: reason?.message ?? reason, stack: reason?.stack || '' },
// {
// scope: EventScope.TO_SYSTEM,
// },
// )
})
process.on('uncaughtException', (err) => {
@@ -72,11 +66,13 @@ export class AllExceptionsFilter implements ExceptionFilter {
once = true
}
catch(exception: unknown, host: ArgumentsHost) {
async catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp()
const response = ctx.getResponse<FastifyReply>()
const request = ctx.getRequest<FastifyRequest>()
const ip = getIp(request)
const status =
exception instanceof HttpException
? exception.getStatus()
@@ -89,6 +85,18 @@ export class AllExceptionsFilter implements ExceptionFilter {
(exception as myError)?.message ||
''
if (status === HttpStatus.TOO_MANY_REQUESTS) {
this.barkService.throttlePush({
title: '疑似遭到攻击',
body: `IP: ${ip}`,
})
await sleep(10_000)
return response.status(444).send({
message: '请求过于频繁,请稍后再试',
})
}
const url = request.raw.url!
if (
@@ -125,7 +133,6 @@ export class AllExceptionsFilter implements ExceptionFilter {
)
}
} else {
const ip = getIp(request)
this.logger.warn(
`IP: ${ip} 错误信息:(${status}) ${message} Path: ${decodeURI(url)}`,
)

View File

@@ -1,3 +1,5 @@
import { throttle } from 'lodash'
import { Injectable } from '@nestjs/common'
import { OnEvent } from '@nestjs/event-emitter'
@@ -66,6 +68,7 @@ export class BarkPushService {
throw new Error('Bark key is not configured')
}
const { title, ...rest } = options
const response = await this.httpService.axiosRef.post(`${serverUrl}/push`, {
device_key: key,
title: `[${siteTitle}] ${title}`,
@@ -75,4 +78,9 @@ export class BarkPushService {
})
return response.data
}
throttlePush = throttle(
async (options: BarkPushOptions) => this.push(options),
1000 * 10,
)
}