增加初始化过程

This commit is contained in:
hanhh
2021-10-03 20:58:55 +08:00
parent bd1ca7e975
commit 14b20873c7
18 changed files with 714 additions and 223 deletions
+2 -2
View File
@@ -1,5 +1,5 @@
import { Router } from 'express';
import auth from './auth';
import user from './user';
import env from './env';
import config from './config';
import log from './log';
@@ -9,7 +9,7 @@ import open from './open';
export default () => {
const app = Router();
auth(app);
user(app);
env(app);
config(app);
log(app);
+98 -41
View File
@@ -3,7 +3,7 @@ import { Container } from 'typedi';
import { Logger } from 'winston';
import * as fs from 'fs';
import config from '../config';
import AuthService from '../services/auth';
import UserService from '../services/user';
import { celebrate, Joi } from 'celebrate';
const route = Router();
@@ -20,8 +20,8 @@ export default (app: Router) => {
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const authService = Container.get(AuthService);
const data = await authService.login({ ...req.body }, req);
const userService = Container.get(UserService);
const data = await userService.login({ ...req.body }, req);
return res.send(data);
} catch (e) {
logger.error('🔥 error: %o', e);
@@ -35,18 +35,9 @@ export default (app: Router) => {
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
fs.readFile(config.authConfigFile, 'utf8', function (err, data) {
if (err) console.log(err);
const authInfo = JSON.parse(data);
fs.writeFileSync(
config.authConfigFile,
JSON.stringify({
...authInfo,
token: '',
}),
);
res.send({ code: 200 });
});
const userService = Container.get(UserService);
await userService.logout(req.platform);
res.send({ code: 200 });
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
@@ -54,20 +45,20 @@ export default (app: Router) => {
},
);
route.post(
route.put(
'/user',
celebrate({
body: Joi.object({
username: Joi.string().required(),
password: Joi.string().required(),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const content = fs.readFileSync(config.authConfigFile, 'utf8');
fs.writeFile(
config.authConfigFile,
JSON.stringify({ ...JSON.parse(content || '{}'), ...req.body }),
(err) => {
if (err) console.log(err);
res.send({ code: 200, message: '更新成功' });
},
);
const userService = Container.get(UserService);
await userService.updateUsernameAndPassword(req.body);
res.send({ code: 200, message: '更新成功' });
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
@@ -80,8 +71,8 @@ export default (app: Router) => {
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const authService = Container.get(AuthService);
const authInfo = await authService.getUserInfo();
const userService = Container.get(UserService);
const authInfo = await userService.getUserInfo();
res.send({
code: 200,
data: {
@@ -101,8 +92,8 @@ export default (app: Router) => {
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const authService = Container.get(AuthService);
const data = await authService.initTwoFactor();
const userService = Container.get(UserService);
const data = await userService.initTwoFactor();
res.send({ code: 200, data });
} catch (e) {
logger.error('🔥 error: %o', e);
@@ -121,8 +112,8 @@ export default (app: Router) => {
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const authService = Container.get(AuthService);
const data = await authService.activeTwoFactor(req.body.code);
const userService = Container.get(UserService);
const data = await userService.activeTwoFactor(req.body.code);
res.send({ code: 200, data });
} catch (e) {
logger.error('🔥 error: %o', e);
@@ -136,8 +127,8 @@ export default (app: Router) => {
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const authService = Container.get(AuthService);
const data = await authService.deactiveTwoFactor();
const userService = Container.get(UserService);
const data = await userService.deactiveTwoFactor();
res.send({ code: 200, data });
} catch (e) {
logger.error('🔥 error: %o', e);
@@ -158,8 +149,8 @@ export default (app: Router) => {
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const authService = Container.get(AuthService);
const data = await authService.twoFactorLogin(req.body, req);
const userService = Container.get(UserService);
const data = await userService.twoFactorLogin(req.body, req);
res.send(data);
} catch (e) {
logger.error('🔥 error: %o', e);
@@ -173,8 +164,8 @@ export default (app: Router) => {
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const authService = Container.get(AuthService);
const data = await authService.getLoginLog();
const userService = Container.get(UserService);
const data = await userService.getLoginLog();
res.send({ code: 200, data });
} catch (e) {
logger.error('🔥 error: %o', e);
@@ -188,8 +179,8 @@ export default (app: Router) => {
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const authService = Container.get(AuthService);
const data = await authService.getNotificationMode();
const userService = Container.get(UserService);
const data = await userService.getNotificationMode();
res.send({ code: 200, data });
} catch (e) {
logger.error('🔥 error: %o', e);
@@ -203,8 +194,74 @@ export default (app: Router) => {
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const authService = Container.get(AuthService);
const result = await authService.updateNotificationMode(req.body);
const userService = Container.get(UserService);
const result = await userService.updateNotificationMode(req.body);
res.send(result);
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
route.get(
'/system',
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const userService = Container.get(UserService);
const authInfo = await userService.getUserInfo();
let isInitialized = true;
if (
!authInfo ||
(Object.keys(authInfo).length === 2 &&
authInfo.username === 'admin' &&
authInfo.password === 'admin')
) {
isInitialized = false;
}
res.send({
code: 200,
data: {
isInitialized,
},
});
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
// 初始化api
route.put(
'/init/user',
celebrate({
body: Joi.object({
username: Joi.string().required(),
password: Joi.string().required(),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const userService = Container.get(UserService);
await userService.updateUsernameAndPassword(req.body);
res.send({ code: 200, message: '更新成功' });
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
route.put(
'/init/notification',
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const userService = Container.get(UserService);
const result = await userService.updateNotificationMode(req.body);
res.send(result);
} catch (e) {
logger.error('🔥 error: %o', e);
+8
View File
@@ -76,4 +76,12 @@ export default {
],
writePathList: [configPath, scriptPath],
bakPath,
apiWhiteList: [
'/api/login',
'/open/auth/token',
'/api/user/two-factor/login',
'/api/system',
'/api/init/user',
'/api/init/notification',
],
};
+27 -9
View File
@@ -9,6 +9,7 @@ import { getPlatform, getToken } from '../config/util';
import Container from 'typedi';
import OpenService from '../services/open';
import rewrite from 'express-urlrewrite';
import UserService from '../services/user';
export default ({ app }: { app: Application }) => {
app.enable('trust proxy');
@@ -21,12 +22,7 @@ export default ({ app }: { app: Application }) => {
secret: config.secret as string,
algorithms: ['HS384'],
}).unless({
path: [
'/api/login',
'/api/crons/status',
/^\/open\//,
'/api/user/two-factor/login',
],
path: [...config.apiWhiteList, /^\/open\//],
}),
);
@@ -62,9 +58,8 @@ export default ({ app }: { app: Application }) => {
if (
!headerToken &&
req.path &&
(req.path === '/api/login' ||
req.path === '/open/auth/token' ||
req.path === '/api/user/two-factor/login')
config.apiWhiteList.includes(req.path) &&
req.path !== '/api/crons/status'
) {
return next();
}
@@ -89,6 +84,29 @@ export default ({ app }: { app: Application }) => {
next(err);
});
app.use(async (req, res, next) => {
if (!['/api/init/user', '/api/init/notification'].includes(req.path)) {
return next();
}
const userService = Container.get(UserService);
const authInfo = await userService.getUserInfo();
let isInitialized = true;
if (
!authInfo ||
(Object.keys(authInfo).length === 2 &&
authInfo.username === 'admin' &&
authInfo.password === 'admin')
) {
isInitialized = false;
}
if (isInitialized) {
return res.send({ code: 450, message: '未知错误' });
} else {
return next();
}
});
app.use(rewrite('/open/*', '/api/$1'));
app.use(config.api.prefix, routes());
+10 -6
View File
@@ -1,7 +1,7 @@
import { NotificationInfo } from '../data/notify';
import { Service, Inject } from 'typedi';
import winston from 'winston';
import AuthService from './auth';
import UserService from './user';
import got from 'got';
import nodemailer from 'nodemailer';
import crypto from 'crypto';
@@ -9,8 +9,8 @@ import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent';
@Service()
export default class NotificationService {
@Inject((type) => AuthService)
private authService!: AuthService;
@Inject((type) => UserService)
private userService!: UserService;
private modeMap = new Map([
['goCqHttpBot', this.goCqHttpBot],
@@ -32,8 +32,11 @@ export default class NotificationService {
constructor(@Inject('logger') private logger: winston.Logger) {}
public async notify(title: string, content: string) {
const { type, ...rest } = await this.authService.getNotificationMode();
public async notify(
title: string,
content: string,
): Promise<boolean | undefined> {
const { type, ...rest } = await this.userService.getNotificationMode();
if (type) {
this.title = title;
this.content = content;
@@ -42,9 +45,10 @@ export default class NotificationService {
try {
return await notificationModeAction?.call(this);
} catch (error: any) {
return error.message;
return false;
}
}
return false;
}
public async testNotify(
@@ -13,7 +13,7 @@ import NotificationService from './notify';
import { Request } from 'express';
@Service()
export default class AuthService {
export default class UserService {
@Inject((type) => NotificationService)
private notificationService!: NotificationService;
private authDb = new DataStore({ filename: config.authDbFile });
@@ -160,6 +160,14 @@ export default class AuthService {
}
}
public async logout(platform: string): Promise<any> {
const authInfo = this.getAuthInfo();
this.updateAuthInfo(authInfo, {
token: '',
tokens: { ...authInfo.tokens, [platform]: '' },
});
}
public async getLoginLog(): Promise<AuthInfo[]> {
return new Promise((resolve) => {
this.authDb.find({ type: AuthDataType.loginLog }).exec((err, docs) => {
@@ -205,6 +213,18 @@ export default class AuthService {
};
}
public async updateUsernameAndPassword({
username,
password,
}: {
username: string;
password: string;
}) {
const authInfo = this.getAuthInfo();
this.updateAuthInfo(authInfo, { username, password });
return { code: 200, message: '更新成功' };
}
public getUserInfo(): Promise<any> {
return new Promise((resolve) => {
fs.readFile(config.authConfigFile, 'utf8', (err, data) => {