Files
vless-api/api/client/add.js
aixianling 1ac6b88e75 refactor(api): 优化客户端添加逻辑
- 修改了 expiryTime 的判断条件,使用不等于 0 替代大于 0,以处理所有非零值
- 优化了代码结构,提高了可读性
2025-02-28 17:08:54 +08:00

46 lines
1.7 KiB
JavaScript

const { randomUUID } = require("crypto");
const ins = require("../../utils/http");
const dayjs = require("dayjs");
const getCookie = require("../../auth/3xuiLogin");
const { log } = require("console");
module.exports = async (ctx) => {
let { id = 4, limitIp, expiryTime = 0, enable = !0, totalGB = 1, subId = "2rv0gb458kbfl532", email } = ctx.request.body;
if (!ctx.state.cookie) {
ctx.state.cookie = await getCookie();
ins.interceptors.request.use((config) => {
config.headers.Cookie = ctx.state.cookie;
return config;
});
}
const inbound = await ins.get(`/panel/api/inbounds/get/${id}`);
if (!inbound?.success) return (ctx.body = { code: "1", msg: "获取节点失败" });
const uuid = randomUUID();
email = email || uuid.split("-")[0];
if (expiryTime!=0) {
expiryTime = dayjs(expiryTime, "YYYY-MM-DD HH:mm:ss").valueOf();
}
totalGB = totalGB * 1024 * 1024 * 1024;
const settings = { clients: [{ id: uuid, flow: "", email, limitIp, enable, tgId: "", subId, reset: 0, totalGB, expiryTime }] };
const result = await ins.post("/panel/api/inbounds/addClient", { id, settings: JSON.stringify(settings) });
if (result?.success) {
const { remark, port, protocol, streamSettings = "{}" } = inbound.obj || {};
const {
network = "ws",
security = "none",
wsSettings: { host, path },
} = JSON.parse(streamSettings);
ctx.body = {
code: "0",
data: `${protocol}://${uuid}@vless.jjcp52.com:${port}?type=${network}&path=${path}&host=${host}&security=${security}#${remark}-${email}`,
message: "success",
};
} else {
ctx.body = {
code: 1,
message: "添加失败",
data: ctx.request.body,
};
}
};