mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
feat: webui config
This commit is contained in:
10
src/webui/src/api/Auth.ts
Normal file
10
src/webui/src/api/Auth.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
//实现一个登录 仅需判断密码 没有用户名字段
|
||||
export class Auth {
|
||||
public static async checkAuth(token: string) {
|
||||
if (token == "123456") {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@@ -1 +0,0 @@
|
||||
//待实现
|
51
src/webui/src/common/config.ts
Normal file
51
src/webui/src/common/config.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
|
||||
const net = require('net');
|
||||
|
||||
async function tryUsePort(port: number) {
|
||||
function portUsed(port: number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let server = net.createServer().listen(port);
|
||||
server.on('listening', function () {
|
||||
server.close();
|
||||
resolve(port);
|
||||
});
|
||||
server.on('error', function (err: any) {
|
||||
if (err.code == 'EADDRINUSE') {
|
||||
resolve(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
let res = await portUsed(port);
|
||||
if (res instanceof Error) {
|
||||
port++;
|
||||
return await tryUsePort(port);
|
||||
}
|
||||
return port;
|
||||
}
|
||||
|
||||
|
||||
//读取当前目录下名为 webui.json 的配置文件 如果不存在则创建初始化配置文件
|
||||
export interface WebUiConfig {
|
||||
port: number;
|
||||
}
|
||||
export async function config(): Promise<WebUiConfig> {
|
||||
try {
|
||||
let config: WebUiConfig = {
|
||||
port: 6099,
|
||||
};
|
||||
if (!existsSync(resolve(__dirname, "./webui.json"))) {
|
||||
writeFileSync(resolve(__dirname, "./webui.json"), JSON.stringify(config, null, 4));
|
||||
}
|
||||
config = JSON.parse(readFileSync(resolve(__dirname, "./webui.json"), "utf-8")) as WebUiConfig;
|
||||
//修正端口占用情况
|
||||
config.port = await tryUsePort(config.port);
|
||||
return config;
|
||||
} catch (e) {
|
||||
//console.error("读取配置文件失败", e);
|
||||
}
|
||||
return {} as WebUiConfig;
|
||||
}
|
Reference in New Issue
Block a user