This commit is contained in:
2026-09-20 04:14:32 +01:00
parent cda6cff34f
commit d5536b49d9
42 changed files with 1544 additions and 143 deletions
@@ -1,13 +1,23 @@
import _ from "lodash";
import { execSync } from "node:child_process";
import path from "node:path";
import type { AdminCrudAPIParams, TableType } from "@/src/types";
import type { APIResponseObject } from "@moduletrace/bunext/types";
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
import grabDirNames from "@/src/utils/grab-dir-names";
import grabHostDirnames from "@/src/functions/backend/setup/grab-host-dir-names";
import setupWireguardHost from "@/src/functions/backend/setup/setup-wireguard-host";
import type {
BUN_SQLITE_WGUI_ALL_TYPEDEFS,
BUN_SQLITE_WGUI_CLIENTS,
BUN_SQLITE_WGUI_HOSTS,
} from "@/db/types/db";
import BunSQLite from "@moduletrace/bun-sqlite";
import type { ServerQueryParam } from "@moduletrace/bun-sqlite/dist/types";
import grabClientDirnames from "@/src/functions/backend/setup/grab-client-dir-names";
const { WGUI_LIB_HOSTS_CONFIGS_DIR, WGUI_LIB_HOST_CLIENTS_DIR_NAME } =
grabDirNames();
export default async function (
params: AdminCrudAPIParams,
@@ -43,21 +53,78 @@ export default async function (
if (!clients_to_delete.payload) {
return {
success: false,
msg: `No Media to Delete.`,
msg: `No Client to Delete.`,
};
}
const affected_host_ids = new Set<number>();
for (let i = 0; i < clients_to_delete.payload.length; i++) {
const client = clients_to_delete.payload[i];
if (!client?.id) continue;
const host_id = Number(client.host_id || 0);
const { CLIENT_DIR } = grabClientDirnames({
client,
host_id,
});
try {
execSync(`rm -rf ${CLIENT_DIR}`, { encoding: "utf-8" });
} catch (error) {}
affected_host_ids.add(host_id);
await BunSQLite.delete<BUN_SQLITE_WGUI_CLIENTS, TableType>({
table: "clients",
targetId: client.id,
});
}
const host_setup_errors: string[] = [];
for (const host_id of affected_host_ids) {
try {
let host: BUN_SQLITE_WGUI_HOSTS | undefined;
if (host_id) {
const host_res = await BunSQLite.select<
BUN_SQLITE_WGUI_HOSTS,
TableType
>({
table: "hosts",
targetId: host_id,
});
host = host_res.singleRes || undefined;
}
const host_setup_res = await setupWireguardHost({
host,
user,
is_update_after_client_setup: true,
});
if (!host_setup_res.success) {
host_setup_errors.push(
`Host ${host_id}: ${host_setup_res.msg}`,
);
}
} catch (error: any) {
host_setup_errors.push(`Host ${host_id}: ${error.message}`);
}
}
if (host_setup_errors[0]) {
return {
success: false,
msg: `Client deleted but host config could not be regenerated: ${host_setup_errors.join("; ")}`,
};
}
return {
success: true,
msg: `${clients_to_delete.payload.length} client(s) deleted`,
};
}