From 27226e722288c830f515c664e50cf019f486ac94 Mon Sep 17 00:00:00 2001 From: whyour Date: Thu, 16 Sep 2021 18:01:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=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 --- back/data/auth.ts | 8 +++-- back/data/notify.ts | 80 +++++++++++++++++++++++++++++++++++++++++ back/services/auth.ts | 32 ++++++++++++++--- back/services/notify.ts | 51 ++++++++++++++++++++++++++ tsconfig.json | 8 ++++- 5 files changed, 171 insertions(+), 8 deletions(-) create mode 100644 back/data/notify.ts create mode 100644 back/services/notify.ts diff --git a/back/data/auth.ts b/back/data/auth.ts index bddccf79..7a760a74 100644 --- a/back/data/auth.ts +++ b/back/data/auth.ts @@ -1,6 +1,6 @@ export class AuthInfo { ip?: string; - type: AuthInfoType; + type: AuthDataType; info?: any; _id?: string; @@ -17,4 +17,8 @@ export enum LoginStatus { 'fail', } -export type AuthInfoType = 'loginLog' | 'authToken'; +export enum AuthDataType { + 'loginLog' = 'loginLog', + 'authToken' = 'authToken', + 'notification' = 'notification', +} diff --git a/back/data/notify.ts b/back/data/notify.ts new file mode 100644 index 00000000..5ebe9593 --- /dev/null +++ b/back/data/notify.ts @@ -0,0 +1,80 @@ +export enum NotificationMode { + 'goCqHttpBot' = 'goCqHttpBot', + 'serverChan' = 'serverChan', + 'bark' = 'bark', + 'telegramBot' = 'telegramBot', + 'dingtalkBot' = 'dingtalkBot', + 'weWorkBot' = 'weWorkBot', + 'weWorkApp' = 'weWorkApp', + 'iGot' = 'iGot', + 'pushPlus' = 'pushPlus', + 'email' = 'email', +} + +abstract class NotificationBaseInfo { + public type!: NotificationMode; +} + +export class GoCqHttpBotNotification extends NotificationBaseInfo { + public GOBOT_URL = ''; + public GOBOT_TOKEN = ''; + public GOBOT_QQ = ''; +} + +export class ServerChanNotification extends NotificationBaseInfo { + public SCKEY = ''; +} + +export class BarkNotification extends NotificationBaseInfo { + public BARK_PUSH = ''; + public BARK_SOUND = ''; + public BARK_GROUP = 'qinglong'; +} + +export class TelegramBotNotification extends NotificationBaseInfo { + public TG_BOT_TOKEN = ''; + public TG_USER_ID = ''; + public TG_PROXY_HOST = ''; + public TG_PROXY_PORT = ''; + public TG_PROXY_AUTH = ''; + public TG_API_HOST = 'api.telegram.org'; +} + +export class DingtalkBotNotification extends NotificationBaseInfo { + public DD_BOT_TOKEN = ''; + public DD_BOT_SECRET = ''; +} + +export class WeWorkBotNotification extends NotificationBaseInfo { + public QYWX_KEY = ''; +} + +export class WeWorkAppNotification extends NotificationBaseInfo { + public QYWX_AM = ''; +} + +export class IGotNotification extends NotificationBaseInfo { + public IGOT_PUSH_KEY = ''; +} + +export class PushPlusNotification extends NotificationBaseInfo { + public PUSH_PLUS_TOKEN = ''; + public PUSH_PLUS_USER = ''; +} + +export class EmailNotification extends NotificationBaseInfo { + public service: string = ''; + public user: string = ''; + public pass: string = ''; +} + +export type NotificationInfo = + | GoCqHttpBotNotification + | ServerChanNotification + | BarkNotification + | TelegramBotNotification + | DingtalkBotNotification + | WeWorkBotNotification + | IGotNotification + | PushPlusNotification + | EmailNotification; diff --git a/back/services/auth.ts b/back/services/auth.ts index b062fede..0a44309a 100644 --- a/back/services/auth.ts +++ b/back/services/auth.ts @@ -8,7 +8,8 @@ import jwt from 'jsonwebtoken'; import { authenticator } from '@otplib/preset-default'; import { exec } from 'child_process'; import DataStore from 'nedb'; -import { AuthInfo, LoginStatus } from '../data/auth'; +import { AuthDataType, AuthInfo, LoginStatus } from '../data/auth'; +import { NotificationInfo } from '../data/notify'; @Service() export default class AuthService { @@ -100,7 +101,7 @@ export default class AuthService { ); await this.getLoginLog(); await this.insertDb({ - type: 'loginLog', + type: AuthDataType.loginLog, info: { timestamp, address, ip, status: LoginStatus.success }, }); return { @@ -121,7 +122,7 @@ export default class AuthService { ); await this.getLoginLog(); await this.insertDb({ - type: 'loginLog', + type: AuthDataType.loginLog, info: { timestamp, address, ip, status: LoginStatus.fail }, }); return { code: 400, message: config.authError }; @@ -133,9 +134,9 @@ export default class AuthService { public async getLoginLog(): Promise { return new Promise((resolve) => { - this.authDb.find({ type: 'loginLog' }).exec((err, docs) => { + this.authDb.find({ type: AuthDataType.loginLog }).exec((err, docs) => { if (err || docs.length === 0) { - resolve(docs); + resolve([]); } else { const result = docs.sort( (a, b) => b.info.timestamp - a.info.timestamp, @@ -247,4 +248,25 @@ export default class AuthService { JSON.stringify({ ...authInfo, ...info }), ); } + + public async getNotificationMode(): Promise { + return new Promise((resolve) => { + this.authDb + .find({ type: AuthDataType.notification }) + .exec((err, docs) => { + if (err || docs.length === 0) { + resolve({} as NotificationInfo); + } else { + resolve(docs[0].info); + } + }); + }); + } + + public async updateNotificationMode(notificationInfo: NotificationInfo) { + return await this.insertDb({ + type: AuthDataType.notification, + info: { notificationInfo }, + }); + } } diff --git a/back/services/notify.ts b/back/services/notify.ts new file mode 100644 index 00000000..a04179e0 --- /dev/null +++ b/back/services/notify.ts @@ -0,0 +1,51 @@ +import { Service, Inject } from 'typedi'; +import winston from 'winston'; +import AuthService from './auth'; + +@Service() +export default class NotifyService { + private modeMap = new Map([ + ['goCqHttpBot', this.goCqHttpBot], + ['serverChan', this.serverChan], + ['bark', this.bark], + ['telegramBot', this.telegramBot], + ['dingtalkBot', this.dingtalkBot], + ['weWorkBot', this.weWorkBot], + ['weWorkApp', this.weWorkApp], + ['iGot', this.iGot], + ['pushPlus', this.pushPlus], + ['email', this.email], + ]); + + constructor( + @Inject('logger') private logger: winston.Logger, + private authService: AuthService, + ) {} + + private async notify() { + const { type } = await this.authService.getNotificationMode(); + if (type) { + const notificationModeAction = this.modeMap.get(type); + notificationModeAction?.call(this); + } + } + + private async goCqHttpBot() {} + + private async serverChan() {} + + private async bark() {} + + private async telegramBot() {} + + private async dingtalkBot() {} + + private async weWorkBot() {} + + private async weWorkApp() {} + private async iGot() {} + + private async pushPlus() {} + + private async email() {} +} diff --git a/tsconfig.json b/tsconfig.json index dbc38416..918b7f51 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -27,7 +27,13 @@ "allowJs": true, "noEmit": false }, - "include": ["src/**/*", "config/**/*", ".umirc.ts", "typings.d.ts"], + "include": [ + "src/**/*", + "config/**/*", + ".umirc.ts", + "typings.d.ts", + "back/**/*" + ], "exclude": [ "node_modules", "lib",