From 36acc3ea65fd3db8d06cf358ed4eafd93609e75d Mon Sep 17 00:00:00 2001 From: xtcnet Date: Sun, 8 Mar 2026 09:47:20 +0700 Subject: [PATCH] fix: resolve WireGuard server tab crash and enforce client creation server prerequisite --- backend/internal/wireguard.js | 33 ++++++++++++++++---------- backend/routes/wireguard.js | 6 ++--- frontend/src/pages/WireGuard/index.tsx | 18 ++++++++------ 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/backend/internal/wireguard.js b/backend/internal/wireguard.js index fae0dce..d856ec8 100644 --- a/backend/internal/wireguard.js +++ b/backend/internal/wireguard.js @@ -357,20 +357,27 @@ const internalWireguard = { }, /** - * Get the WireGuard interface info + * Get the WireGuard interfaces info */ - async getInterfaceInfo(knex) { - const iface = await this.getOrCreateInterface(knex); - return { - id: iface.id, - name: iface.name, - public_key: iface.public_key, - ipv4_cidr: iface.ipv4_cidr, - listen_port: iface.listen_port, - mtu: iface.mtu, - dns: iface.dns, - host: iface.host, - }; + async getInterfacesInfo(knex) { + const ifaces = await knex("wg_interface").select("*"); + const allLinks = await knex("wg_server_link").select("*"); + + return ifaces.map((i) => { + const links = allLinks.filter(l => l.interface_id_1 === i.id || l.interface_id_2 === i.id); + return { + id: i.id, + name: i.name, + public_key: i.public_key, + ipv4_cidr: i.ipv4_cidr, + listen_port: i.listen_port, + mtu: i.mtu, + dns: i.dns, + host: i.host, + isolate_clients: i.isolate_clients, + linked_servers: links.map(l => l.interface_id_1 === i.id ? l.interface_id_2 : l.interface_id_1), + }; + }); }, /** diff --git a/backend/routes/wireguard.js b/backend/routes/wireguard.js index 2189547..45abcae 100644 --- a/backend/routes/wireguard.js +++ b/backend/routes/wireguard.js @@ -10,13 +10,13 @@ const router = express.Router({ /** * GET /api/wireguard - * Get WireGuard interface info + * Get WireGuard interfaces info */ router.get("/", async (_req, res, next) => { try { const knex = db(); - const iface = await internalWireguard.getInterfaceInfo(knex); - res.status(200).json(iface); + const ifaces = await internalWireguard.getInterfacesInfo(knex); + res.status(200).json(ifaces); } catch (err) { next(err); } diff --git a/frontend/src/pages/WireGuard/index.tsx b/frontend/src/pages/WireGuard/index.tsx index d5e5822..2961b99 100644 --- a/frontend/src/pages/WireGuard/index.tsx +++ b/frontend/src/pages/WireGuard/index.tsx @@ -48,7 +48,7 @@ function timeAgo(date: string | null): string { } function WireGuard() { - const [activeTab, setActiveTab] = useState<"servers" | "clients">("clients"); + const [activeTab, setActiveTab] = useState<"servers" | "clients">("servers"); const { data: clients, isLoading: clientsLoading } = useWgClients(); const { data: interfaces, isLoading: ifacesLoading } = useWgInterfaces(); @@ -107,6 +107,10 @@ function WireGuard() { // Client Handlers const handleNewClient = async () => { + if (!interfaces || interfaces.length === 0) { + alert("Bạn phải tạo một WireGuard Server trước khi tạo Client."); + return; + } const result = (await EasyModal.show(WireGuardClientModal, { interfaces: interfaces || [] })) as any; if (result && result.name && result.interface_id) { createClient.mutate({ name: result.name, interface_id: result.interface_id }); @@ -174,18 +178,18 @@ function WireGuard() {