From 98a7097fdaf0bc3747ce6301c5c1b3c471b80a0c Mon Sep 17 00:00:00 2001 From: Unitwk Date: Fri, 23 Aug 2024 17:50:12 +0800 Subject: [PATCH] feat: getInstancesByUuid() support daemon id --- panel/src/app/routers/general_user_router.ts | 2 +- panel/src/app/routers/instance_exchange_router.ts | 3 +++ panel/src/app/service/exchange_service.ts | 5 +++-- panel/src/app/service/instance_service.ts | 9 +++++++-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/panel/src/app/routers/general_user_router.ts b/panel/src/app/routers/general_user_router.ts index 51dad05c..fc74ba0e 100755 --- a/panel/src/app/routers/general_user_router.ts +++ b/panel/src/app/routers/general_user_router.ts @@ -42,7 +42,7 @@ router.get("/", permission({ level: ROLE.USER, token: false, speedLimit: false } // Some and only Ajax requests grant access if (isAjax(ctx)) { - const res = await getInstancesByUuid(uuid, toBoolean(advanced) || false); + const res = await getInstancesByUuid(uuid, undefined, toBoolean(advanced) || false); res.token = getToken(ctx); ctx.body = res; } diff --git a/panel/src/app/routers/instance_exchange_router.ts b/panel/src/app/routers/instance_exchange_router.ts index 2fbe77ce..380113ca 100755 --- a/panel/src/app/routers/instance_exchange_router.ts +++ b/panel/src/app/routers/instance_exchange_router.ts @@ -9,6 +9,7 @@ import { RequestAction } from "../service/exchange_service"; import { toText } from "common"; +import { logger } from "../service/log"; const router = new Router({ prefix: "/exchange" }); @@ -24,6 +25,7 @@ router.post( try { const requestAction = toText(ctx.request.body.request_action) as RequestAction; const params = ctx.request.body.data ?? {}; + logger.info("Get exchange request, action: %s, params: %j", requestAction, params); if (requestAction === RequestAction.PING) { ctx.body = await getNodeStatus(params); return; @@ -37,6 +39,7 @@ router.post( return; } } catch (err) { + logger.error("Exchange request error: " + err); ctx.body = err; } } diff --git a/panel/src/app/service/exchange_service.ts b/panel/src/app/service/exchange_service.ts index 9c09b7b7..fed1c724 100644 --- a/panel/src/app/service/exchange_service.ts +++ b/panel/src/app/service/exchange_service.ts @@ -115,7 +115,7 @@ export async function buyOrRenewInstance( if (request_action === RequestAction.BUY) { payload.endTime = (payload.endTime ? payload.endTime : Date.now()) + hours * 3600 * 1000; - payload.nickname = payload.nickname + "-" + getNanoId(6); + payload.nickname = username + "-" + getNanoId(6); const { instanceUuid: newInstanceId, config: newInstanceConfig } = await remoteRequest.request( "instance/new", payload @@ -189,10 +189,11 @@ export async function queryInstanceByUserId( params: Record ): Promise { const name = parseUserName(params.username) || ""; + const targetDaemonId = toText(params.node_id) ?? undefined; const user = user_service.getUserByUserName(name); if (!user) throw new Error(t("TXT_CODE_903b6c50")); - const { instances = [] } = await getInstancesByUuid(user.uuid, true); + const { instances = [] } = await getInstancesByUuid(user.uuid, targetDaemonId, true); const newInstancesInfo = instances.map((v) => { return formatInstanceData(v); }); diff --git a/panel/src/app/service/instance_service.ts b/panel/src/app/service/instance_service.ts index baa529c4..3183a079 100755 --- a/panel/src/app/service/instance_service.ts +++ b/panel/src/app/service/instance_service.ts @@ -60,7 +60,11 @@ export function multiOperationForwarding( } } -export async function getInstancesByUuid(uuid: string, advanced: boolean = false) { +export async function getInstancesByUuid( + uuid: string, + targetDaemonId?: string, + advanced: boolean = false +) { const user = userSystem.getInstance(uuid); if (!user) throw new Error("The UID does not exist"); @@ -69,9 +73,10 @@ export async function getInstancesByUuid(uuid: string, advanced: boolean = false if (advanced) { const instances = user.instances; for (const iterator of instances) { + if (targetDaemonId && targetDaemonId !== iterator.daemonId) continue; const remoteService = RemoteServiceSubsystem.getInstance(iterator.daemonId); - // If the remote service doesn't exist at all, load a deleted prompt if (!remoteService) { + // If the remote service doesn't exist at all, load a deleted prompt resInstances.push({ hostIp: "-- Unknown --", instanceUuid: iterator.instanceUuid,