From c9a2cad6709b586a5ffe4094fc3fe1e2dd9037ba Mon Sep 17 00:00:00 2001 From: aixianling Date: Wed, 26 Feb 2025 15:50:30 +0800 Subject: [PATCH] =?UTF-8?q?feat(api):=20=E4=BC=98=E5=8C=96=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E6=B7=BB=E5=8A=A0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加用户认证和 Cookie 设置 - 重构 API 请求逻辑,提高可维护性 - 优化错误处理和响应结构 - 调整实例配置和响应拦截器 --- api/client/add.js | 56 ++++++++++++++++++++++++++--------------------- utils/http.js | 7 +++--- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/api/client/add.js b/api/client/add.js index 7a85c79..37832c8 100644 --- a/api/client/add.js +++ b/api/client/add.js @@ -1,36 +1,42 @@ const { randomUUID } = require("crypto"); -const http = require("../../utils/http"); +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, enable = !0, totalGB = 1, subId = "2rv0gb458kbfl532" } = ctx.request.body; - const inbound = await http.get(`/api/inbounds/get/${id}`); + 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 = 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, - }; - }); + const result = await ins.post("/panel/api/inbounds/addClient", { id, settings: JSON.stringify(settings) }); + if (result?.success) { + const { remark, port, protocol, streamSettings = "{}" } = inbound.data?.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, + }; + } }; diff --git a/utils/http.js b/utils/http.js index 0e0c0d0..803dee2 100644 --- a/utils/http.js +++ b/utils/http.js @@ -3,9 +3,10 @@ const { default: axios } = require("axios"); const instance = axios.create({ baseURL: process.env.BASE_URL }); instance.interceptors.response.use( (response) => { - if (response.data.code === 200) { - return response.data.data; - } else return response; + if (response.status === 200 && !["/login"].includes(response.config.url)) { + return response.data; + } + return response; }, (error) => { console.log(error);