131 lines
3.7 KiB
TypeScript
131 lines
3.7 KiB
TypeScript
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,
|
|
): Promise<APIResponseObject> {
|
|
const { user, user_types, body, id } = params;
|
|
|
|
const can_delete_client = checkUserAccess({
|
|
user_types,
|
|
// includes: ["admin"],
|
|
});
|
|
|
|
if (!can_delete_client.success) {
|
|
return {
|
|
success: false,
|
|
msg: `Can't delete client`,
|
|
};
|
|
}
|
|
|
|
let query = _.merge<
|
|
ServerQueryParam<BUN_SQLITE_WGUI_ALL_TYPEDEFS>,
|
|
ServerQueryParam<BUN_SQLITE_WGUI_ALL_TYPEDEFS>
|
|
>(body?.sql_query || {}, {});
|
|
|
|
const clients_to_delete = await BunSQLite.select<
|
|
BUN_SQLITE_WGUI_CLIENTS,
|
|
TableType
|
|
>({
|
|
table: "clients",
|
|
query,
|
|
targetId: id,
|
|
});
|
|
|
|
if (!clients_to_delete.payload) {
|
|
return {
|
|
success: false,
|
|
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`,
|
|
};
|
|
}
|