36 lines
724 B
TypeScript
Raw Normal View History

2021-12-06 23:26:38 +08:00
import log from '../util/LogUtil';
2022-05-17 01:25:05 +08:00
import config from "../config";
2021-12-06 23:26:38 +08:00
let f = async (ctx, next) => {
try {
2022-05-17 01:25:05 +08:00
//检查是否有密码
if (checkToken(ctx)) {
await next();
} else {
ctx.status = 401;
ctx.body = "密钥验证错误";
}
2021-12-06 23:26:38 +08:00
} catch (error: any) {
if (error.status != undefined) {
ctx.status = error.status;
} else {
ctx.status = 500;
}
ctx.body = error.message;
log.error(error);
}
}
2022-05-17 01:25:05 +08:00
function checkToken(ctx) {
if (!config.token) {
return true;
}
let requestPath = ctx.method + ctx.path.replace(config.urlPrefix, "");
if (config.publicPath.has(requestPath)) {
return true;
}
return config.token == ctx.headers.token;
}
2021-06-21 16:32:10 +08:00
export default f;