Refactor main host setup
This commit is contained in:
@@ -2,7 +2,6 @@ import type {
|
||||
BUN_SQLITE_WGUI_CLIENTS,
|
||||
BUN_SQLITE_WGUI_CLIENT_RULES,
|
||||
BUN_SQLITE_WGUI_HOSTS,
|
||||
BUN_SQLITE_WGUI_VARIABLES,
|
||||
} from "@/db/types/db";
|
||||
import grabDirNames from "@/src/utils/grab-dir-names";
|
||||
import { execSync } from "node:child_process";
|
||||
@@ -46,15 +45,6 @@ export default async function setupWireguardHost({
|
||||
POST_UP_PATH,
|
||||
} = grabHostDirnames({ host });
|
||||
|
||||
const variables_res = await BunSQLite.select<
|
||||
BUN_SQLITE_WGUI_VARIABLES,
|
||||
TableType
|
||||
>({
|
||||
table: "variables",
|
||||
});
|
||||
|
||||
const variables = variables_res.payload;
|
||||
|
||||
const host_clients_res = await BunSQLite.select<
|
||||
BUN_SQLITE_WGUI_CLIENTS,
|
||||
TableType
|
||||
@@ -103,10 +93,7 @@ export default async function setupWireguardHost({
|
||||
|
||||
const TARGET_INTERFACE =
|
||||
host?.interface || (await grabHostNetworkInterface());
|
||||
const HOST_WG_IP =
|
||||
host?.wg_ip_address ||
|
||||
variables?.find((v) => v.key == "main_host_wg_ip_address")?.value ||
|
||||
wg_subnet_ip;
|
||||
const HOST_WG_IP = host?.wg_ip_address || wg_subnet_ip;
|
||||
|
||||
if (!HOST_WG_IP) {
|
||||
return {
|
||||
@@ -159,37 +146,7 @@ export default async function setupWireguardHost({
|
||||
encoding: "utf-8",
|
||||
}).trim();
|
||||
|
||||
if (HOST_ID == 0 && !is_update_after_client_setup) {
|
||||
const variables_data: Array<{ key: string; value: string }> = [
|
||||
{
|
||||
key: "main_host_wg_ip_address",
|
||||
value: HOST_WG_IP,
|
||||
},
|
||||
{
|
||||
key: "main_host_wg_public_key",
|
||||
value: HOST_PUBLIC_KEY,
|
||||
},
|
||||
];
|
||||
if (host?.interface) {
|
||||
variables_data.push({
|
||||
key: "main_host_public_interface",
|
||||
value: host.interface,
|
||||
});
|
||||
}
|
||||
|
||||
const update_variables = await BunSQLite.insert<
|
||||
BUN_SQLITE_WGUI_VARIABLES,
|
||||
TableType
|
||||
>({
|
||||
table: "variables",
|
||||
data: variables_data as BUN_SQLITE_WGUI_VARIABLES[],
|
||||
update_on_duplicate: true,
|
||||
});
|
||||
|
||||
if (!update_variables.success) {
|
||||
throw new Error(`Couldn't update host variables`);
|
||||
}
|
||||
} else if (HOST_ID && !is_update_after_client_setup) {
|
||||
if (!is_update_after_client_setup) {
|
||||
const update_host = await BunSQLite.insert<
|
||||
BUN_SQLITE_WGUI_HOSTS,
|
||||
TableType
|
||||
|
||||
Reference in New Issue
Block a user