import { wgIfaceDtoSchema, wgImportResponseSchema } from "@mmapp/contracts/wireguard" import type { WgCreateInterface, WgCreatePeerRequest, WgExportRequest, WgExportResponse, WgImportRequest, WgImportResponse, WgListResponse, WgPatchInterface, WgPatchPeer, } from "@mmapp/contracts/wireguard" import { requestJson } from "@/shared/api/http-client" export async function listWireGuard( baseUrl: string, opts?: { serverId?: string; includePrivateKey?: boolean }, ): Promise { const q = new URLSearchParams() if (opts?.serverId) q.set("serverId", opts.serverId) if (opts?.includePrivateKey) q.set("includePrivateKey", "1") const qs = q.toString() return requestJson(baseUrl, `/api/wireguard${qs ? `?${qs}` : ""}`) } export async function createWireGuardInterface( baseUrl: string, payload: WgCreateInterface, ): Promise { const result = await requestJson(baseUrl, "/api/wireguard/interfaces", { method: "POST", body: JSON.stringify(payload), }) const iface = wgIfaceDtoSchema.safeParse(result) const confirmed = iface.success ? iface.data.name === payload.name && String(iface.data.serverId) === String(payload.serverId) : typeof result === "object" && result !== null && "ok" in result && result.ok === true && "name" in result && result.name === payload.name if (!confirmed) throw new Error("Сервер не подтвердил создание интерфейса") return result } export async function patchWireGuardInterface( baseUrl: string, serverId: string, rosId: string, payload: WgPatchInterface, ): Promise<{ ok: boolean }> { return requestJson(baseUrl, `/api/wireguard/interfaces/${encodeURIComponent(serverId)}/${encodeURIComponent(rosId)}`, { method: "PATCH", body: JSON.stringify(payload), }) } export async function deleteWireGuardInterface( baseUrl: string, serverId: string, rosId: string, ): Promise<{ ok: boolean }> { return requestJson(baseUrl, `/api/wireguard/interfaces/${encodeURIComponent(serverId)}/${encodeURIComponent(rosId)}`, { method: "DELETE", }) } export async function createWireGuardPeer( baseUrl: string, payload: WgCreatePeerRequest, ): Promise<{ ok: boolean }> { const result = await requestJson<{ ok: boolean }>(baseUrl, "/api/wireguard/peers", { method: "POST", body: JSON.stringify(payload), }) if (result?.ok !== true) throw new Error("Сервер не подтвердил создание пира") return result } export async function patchWireGuardPeer( baseUrl: string, serverId: string, rosId: string, payload: WgPatchPeer, ): Promise<{ ok: boolean }> { return requestJson(baseUrl, `/api/wireguard/peers/${encodeURIComponent(serverId)}/${encodeURIComponent(rosId)}`, { method: "PATCH", body: JSON.stringify(payload), }) } export async function deleteWireGuardPeer( baseUrl: string, serverId: string, rosId: string, ): Promise<{ ok: boolean }> { return requestJson(baseUrl, `/api/wireguard/peers/${encodeURIComponent(serverId)}/${encodeURIComponent(rosId)}`, { method: "DELETE", }) } export async function importWireGuard( baseUrl: string, payload: WgImportRequest, ): Promise { const response = await requestJson(baseUrl, "/api/wireguard/import", { method: "POST", body: JSON.stringify(payload), }) const parsed = wgImportResponseSchema.safeParse(response) if (!parsed.success) throw new Error("Некорректный ответ импорта WireGuard") const result = parsed.data if (result.dryRun !== (payload.dryRun ?? false) || (!result.dryRun && !result.applied)) { throw new Error("Сервер не подтвердил применение конфигурации") } return result } export async function exportWireGuard( baseUrl: string, payload: WgExportRequest, ): Promise { return requestJson(baseUrl, "/api/wireguard/export", { method: "POST", body: JSON.stringify(payload), }) }