feat(api): 新增客户端添加功能并优化路由加载

- 新增客户端添加 API,实现客户端信息的添加和处理
- 重构路由加载函数,支持递归加载子目录中的路由文件
- 添加日志记录,便于调试和监控
- 优化 HTTP 请求处理,增加错误处理和状态码检查
This commit is contained in:
aixianling
2025-02-26 14:16:26 +08:00
parent 7ab5d7dbbf
commit b958beaf2f
5 changed files with 87 additions and 21 deletions

36
api/client/add.js Normal file
View File

@@ -0,0 +1,36 @@
const { randomUUID } = require("crypto");
const http = require("../../utils/http");
const dayjs = require("dayjs");
module.exports = async (ctx) => {
let { id = 4, limitIp, expiryTime, enable = !0, totalGB = 1, subId = "2rv0gb458kbfl532" } = ctx.request.body;
const inbound = await http.get(`/api/inbounds/get/${id}`);
if (!inbound?.success) return (ctx.body = { code: "1", msg: "获取节点失败" });
const uuid = randomUUID(),
email = uuid.split("-")[0];
expiryTime = dayjs(expiryTime, "YYYY-MM-DD HH:mm:ss").valueOf();
const settings = { clients: [{ id: uuid, flow: "", email, limitIp, enable, tgId: "", subId, reset: 0, totalGB, expiryTime }] };
http
.post("/api/inbounds/addClient", { id, settings: JSON.stringify(settings) })
.then((res) => {
if (res?.success) {
const { remark, port, protocol, streamSettings = "{}" } = inbound.obj || {};
const {
network = "ws",
security = "none",
wsSettings: { host, path },
} = JSON.parse(streamSettings);
return ctx.body = {
code: "0",
data: `${protocol}://${uuid}@vless.jjcp52.com:${port}?type=${network}&path=${path}&host=${host}&security=${security}#${remark}-${email}`,
message: "success",
};
}
})
.finally(() => {
ctx.body = {
message: "Example POST API",
data: ctx.request.body,
};
});
};