更新通知服务

This commit is contained in:
whyour 2021-09-16 21:01:29 +08:00
parent 27226e7222
commit 9534cda1f9
5 changed files with 35 additions and 20 deletions

View File

@ -11,7 +11,7 @@ export default defineConfig({
favicon: '/images/g5.ico', favicon: '/images/g5.ico',
proxy: { proxy: {
'/api': { '/api': {
target: 'http://127.0.0.1:5678/', target: 'http://127.0.0.1:5600/',
changeOrigin: true, changeOrigin: true,
}, },
}, },

View File

@ -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 ( if (
!headerToken && !headerToken &&
req.path && req.path &&
@ -71,6 +64,14 @@ export default ({ app }: { app: Application }) => {
return next(); 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'); const err: any = new Error('UnauthorizedError');
err.status = 401; err.status = 401;
next(err); next(err);

View File

@ -10,12 +10,16 @@ import { exec } from 'child_process';
import DataStore from 'nedb'; import DataStore from 'nedb';
import { AuthDataType, AuthInfo, LoginStatus } from '../data/auth'; import { AuthDataType, AuthInfo, LoginStatus } from '../data/auth';
import { NotificationInfo } from '../data/notify'; import { NotificationInfo } from '../data/notify';
import NotificationService from './notify';
@Service() @Service()
export default class AuthService { export default class AuthService {
private authDb = new DataStore({ filename: config.authDbFile }); 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) => { this.authDb.loadDatabase((err) => {
if (err) throw err; if (err) throw err;
}); });
@ -48,7 +52,7 @@ export default class AuthService {
} = content; } = content;
if ( if (
(cUsername === 'admin' && cPassword === 'adminadmin') || (cUsername === 'admin' && cPassword === 'admin') ||
!cUsername || !cUsername ||
!cPassword !cPassword
) { ) {
@ -94,10 +98,11 @@ export default class AuthService {
lastaddr: address, lastaddr: address,
isTwoFactorChecking: false, isTwoFactorChecking: false,
}); });
exec( await this.notificationService.notify(
`notify "登陆通知" "你于${new Date( '登陆通知',
`你于${new Date(
timestamp, timestamp,
).toLocaleString()}${address}ip地址${ip}"`, ).toLocaleString()} ${address} ip地址 ${ip}"`,
); );
await this.getLoginLog(); await this.getLoginLog();
await this.insertDb({ await this.insertDb({
@ -115,10 +120,11 @@ export default class AuthService {
lastip: ip, lastip: ip,
lastaddr: address, lastaddr: address,
}); });
exec( await this.notificationService.notify(
`notify "登陆通知" "你于${new Date( '登陆通知',
`你于${new Date(
timestamp, timestamp,
).toLocaleString()}${address}ip地址${ip}"`, ).toLocaleString()} ${address} ip地址 ${ip}"`,
); );
await this.getLoginLog(); await this.getLoginLog();
await this.insertDb({ await this.insertDb({

View File

@ -1,9 +1,10 @@
import { NotificationInfo } from '../data/notify';
import { Service, Inject } from 'typedi'; import { Service, Inject } from 'typedi';
import winston from 'winston'; import winston from 'winston';
import AuthService from './auth'; import AuthService from './auth';
@Service() @Service()
export default class NotifyService { export default class NotificationService {
private modeMap = new Map([ private modeMap = new Map([
['goCqHttpBot', this.goCqHttpBot], ['goCqHttpBot', this.goCqHttpBot],
['serverChan', this.serverChan], ['serverChan', this.serverChan],
@ -17,14 +18,21 @@ export default class NotifyService {
['email', this.email], ['email', this.email],
]); ]);
private title = '';
private content = '';
private params!: Omit<NotificationInfo, 'type'>;
constructor( constructor(
@Inject('logger') private logger: winston.Logger, @Inject('logger') private logger: winston.Logger,
private authService: AuthService, private authService: AuthService,
) {} ) {}
private async notify() { public async notify(title: string, content: string) {
const { type } = await this.authService.getNotificationMode(); const { type, ...rest } = await this.authService.getNotificationMode();
if (type) { if (type) {
this.title = title;
this.content = content;
this.params = rest;
const notificationModeAction = this.modeMap.get(type); const notificationModeAction = this.modeMap.get(type);
notificationModeAction?.call(this); notificationModeAction?.call(this);
} }

View File

@ -1 +1 @@
{ "username": "admin", "password": "adminadmin" } { "username": "admin", "password": "admin" }