Updates
This commit is contained in:
@@ -43,15 +43,16 @@ export default async function runClientSetup({
|
||||
let host: BUN_SQLITE_WGUI_HOSTS | undefined;
|
||||
|
||||
if (client?.host_id) {
|
||||
const host_res = await BunSQLite.select<BUN_SQLITE_WGUI_HOSTS, TableType>(
|
||||
{
|
||||
table: "hosts",
|
||||
targetId: client.host_id,
|
||||
},
|
||||
);
|
||||
const host_res = await BunSQLite.select<
|
||||
BUN_SQLITE_WGUI_HOSTS,
|
||||
TableType
|
||||
>({
|
||||
table: "hosts",
|
||||
targetId: client.host_id,
|
||||
});
|
||||
|
||||
host = host_res.singleRes || undefined;
|
||||
}
|
||||
|
||||
return await setupWireguardClient({ client, host, user });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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`,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ export default async function (
|
||||
|
||||
const PUT = await BunSQLite.update({
|
||||
table,
|
||||
data: body?.update_data,
|
||||
data: body.update_data,
|
||||
targetId,
|
||||
query: final_sql_query,
|
||||
});
|
||||
@@ -44,13 +44,9 @@ export default async function (
|
||||
});
|
||||
|
||||
if (!client_setup_res.success) {
|
||||
PUT.debug = {
|
||||
...(PUT.debug || {}),
|
||||
client_setup: client_setup_res,
|
||||
};
|
||||
PUT.error = client_setup_res.msg || client_setup_res.error;
|
||||
return client_setup_res;
|
||||
}
|
||||
}
|
||||
|
||||
return PUT;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
|
||||
import userAuth from "@/src/functions/backend/auth/user-auth";
|
||||
import updateWireguardHostPublicIP from "@/src/functions/backend/setup/update-wireguard-host-public-ip";
|
||||
import type { ApiReqParams } from "@/src/types";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
BunextAPIRouteHandler,
|
||||
} from "@moduletrace/bunext/types";
|
||||
|
||||
export const handler: BunextAPIRouteHandler<APIResponseObject> = async (
|
||||
params,
|
||||
) => {
|
||||
const req = params.req;
|
||||
const body = params.body as ApiReqParams;
|
||||
|
||||
if (req.method !== "POST") {
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
const { user, user_types } = await userAuth({ req });
|
||||
|
||||
if (!user?.logged_in_status) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Unauthorized`,
|
||||
logoutUser: true,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const is_super_admin = checkUserAccess({ user_types });
|
||||
|
||||
if (!is_super_admin.success) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Unauthorized`,
|
||||
};
|
||||
}
|
||||
|
||||
return await updateWireguardHostPublicIP({
|
||||
host_id: body?.host_id,
|
||||
public_ip_address: body?.public_ip_address,
|
||||
user,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
msg: error.message,
|
||||
};
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user