From 9534cda1f9d025140140829be2dd3ab2adaf6a07 Mon Sep 17 00:00:00 2001 From: whyour Date: Thu, 16 Sep 2021 21:01:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=80=9A=E7=9F=A5=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .umirc.ts | 2 +- back/loaders/express.ts | 15 ++++++++------- back/services/auth.ts | 22 ++++++++++++++-------- back/services/notify.ts | 14 +++++++++++--- sample/auth.sample.json | 2 +- 5 files changed, 35 insertions(+), 20 deletions(-) diff --git a/.umirc.ts b/.umirc.ts index 0b49eabd..4c25a974 100644 --- a/.umirc.ts +++ b/.umirc.ts @@ -11,7 +11,7 @@ export default defineConfig({ favicon: '/images/g5.ico', proxy: { '/api': { - target: 'http://127.0.0.1:5678/', + target: 'http://127.0.0.1:5600/', changeOrigin: true, }, }, diff --git a/back/loaders/express.ts b/back/loaders/express.ts index ebe4d0d3..87eaf48d 100644 --- a/back/loaders/express.ts +++ b/back/loaders/express.ts @@ -47,13 +47,6 @@ export default ({ app }: { app: Application }) => { } } - const data = fs.readFileSync(config.authConfigFile, 'utf8'); - if (data) { - const { token } = JSON.parse(data); - if (token && headerToken === token) { - return next(); - } - } if ( !headerToken && req.path && @@ -71,6 +64,14 @@ export default ({ app }: { app: Application }) => { return next(); } + const data = fs.readFileSync(config.authConfigFile, 'utf8'); + if (data) { + const { token } = JSON.parse(data); + if (token && headerToken === token) { + return next(); + } + } + const err: any = new Error('UnauthorizedError'); err.status = 401; next(err); diff --git a/back/services/auth.ts b/back/services/auth.ts index 0a44309a..3a6ff2a4 100644 --- a/back/services/auth.ts +++ b/back/services/auth.ts @@ -10,12 +10,16 @@ import { exec } from 'child_process'; import DataStore from 'nedb'; import { AuthDataType, AuthInfo, LoginStatus } from '../data/auth'; import { NotificationInfo } from '../data/notify'; +import NotificationService from './notify'; @Service() export default class AuthService { private authDb = new DataStore({ filename: config.authDbFile }); - constructor(@Inject('logger') private logger: winston.Logger) { + constructor( + @Inject('logger') private logger: winston.Logger, + private notificationService: NotificationService, + ) { this.authDb.loadDatabase((err) => { if (err) throw err; }); @@ -48,7 +52,7 @@ export default class AuthService { } = content; if ( - (cUsername === 'admin' && cPassword === 'adminadmin') || + (cUsername === 'admin' && cPassword === 'admin') || !cUsername || !cPassword ) { @@ -94,10 +98,11 @@ export default class AuthService { lastaddr: address, isTwoFactorChecking: false, }); - exec( - `notify "登陆通知" "你于${new Date( + await this.notificationService.notify( + '登陆通知', + `你于${new Date( timestamp, - ).toLocaleString()}在${address}登陆成功,ip地址${ip}"`, + ).toLocaleString()}在 ${address} 登陆成功,ip地址 ${ip}"`, ); await this.getLoginLog(); await this.insertDb({ @@ -115,10 +120,11 @@ export default class AuthService { lastip: ip, lastaddr: address, }); - exec( - `notify "登陆通知" "你于${new Date( + await this.notificationService.notify( + '登陆通知', + `你于${new Date( timestamp, - ).toLocaleString()}在${address}登陆失败,ip地址${ip}"`, + ).toLocaleString()}在 ${address} 登陆失败,ip地址 ${ip}"`, ); await this.getLoginLog(); await this.insertDb({ diff --git a/back/services/notify.ts b/back/services/notify.ts index a04179e0..7eced8b7 100644 --- a/back/services/notify.ts +++ b/back/services/notify.ts @@ -1,9 +1,10 @@ +import { NotificationInfo } from '../data/notify'; import { Service, Inject } from 'typedi'; import winston from 'winston'; import AuthService from './auth'; @Service() -export default class NotifyService { +export default class NotificationService { private modeMap = new Map([ ['goCqHttpBot', this.goCqHttpBot], ['serverChan', this.serverChan], @@ -17,14 +18,21 @@ export default class NotifyService { ['email', this.email], ]); + private title = ''; + private content = ''; + private params!: Omit; + constructor( @Inject('logger') private logger: winston.Logger, private authService: AuthService, ) {} - private async notify() { - const { type } = await this.authService.getNotificationMode(); + public async notify(title: string, content: string) { + const { type, ...rest } = await this.authService.getNotificationMode(); if (type) { + this.title = title; + this.content = content; + this.params = rest; const notificationModeAction = this.modeMap.get(type); notificationModeAction?.call(this); } diff --git a/sample/auth.sample.json b/sample/auth.sample.json index 57cfabbe..bfbcfff8 100755 --- a/sample/auth.sample.json +++ b/sample/auth.sample.json @@ -1 +1 @@ -{ "username": "admin", "password": "adminadmin" } +{ "username": "admin", "password": "admin" }