Add host setup functions and client components
This commit is contained in:
@@ -11,6 +11,7 @@ import grabHostNetworkInterface from "./grab-host-network-interface";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { TableType } from "@/src/types";
|
||||
import checkPrivateIPAvailability from "./check-private-ip-availability";
|
||||
|
||||
const {
|
||||
WGUI_LIB_IP_TABLES_DIR,
|
||||
@@ -21,15 +22,24 @@ const {
|
||||
|
||||
type Params = {
|
||||
host?: BUN_SQLITE_WGUI_HOSTS;
|
||||
variables?: BUN_SQLITE_WGUI_VARIABLES[];
|
||||
wg_subnet_ip?: string;
|
||||
};
|
||||
|
||||
export default async function setupWireguardHost({
|
||||
host,
|
||||
variables,
|
||||
wg_subnet_ip,
|
||||
}: Params): Promise<APIResponseObject> {
|
||||
const host_id = host?.id || AppData["WireguardHostID"];
|
||||
|
||||
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
|
||||
@@ -49,7 +59,8 @@ export default async function setupWireguardHost({
|
||||
const TARGET_INTERFACE = await grabHostNetworkInterface();
|
||||
const HOST_WG_IP =
|
||||
host?.wg_ip_address ||
|
||||
variables?.find((v) => v.key == "main_host_wg_ip_address")?.value;
|
||||
variables?.find((v) => v.key == "main_host_wg_ip_address")?.value ||
|
||||
wg_subnet_ip;
|
||||
|
||||
if (!HOST_WG_IP) {
|
||||
return {
|
||||
@@ -58,6 +69,17 @@ export default async function setupWireguardHost({
|
||||
};
|
||||
}
|
||||
|
||||
const is_ip_available = await checkPrivateIPAvailability({
|
||||
ip_address: HOST_WG_IP,
|
||||
});
|
||||
|
||||
if (!is_ip_available.success) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `IP not available`,
|
||||
};
|
||||
}
|
||||
|
||||
let pre_sh = ``;
|
||||
|
||||
pre_sh += `cd ${WIREGUARD_HOST_CONFIG_DIR}\n`;
|
||||
@@ -67,6 +89,8 @@ export default async function setupWireguardHost({
|
||||
|
||||
try {
|
||||
const exec_pre_setup = execSync(pre_sh, { encoding: "utf-8" });
|
||||
|
||||
console.log("exec_pre_setup", exec_pre_setup);
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
@@ -155,6 +179,11 @@ export default async function setupWireguardHost({
|
||||
sh += `\n`;
|
||||
|
||||
const exec = execSync(sh, { encoding: "utf-8" });
|
||||
|
||||
return {
|
||||
success: true,
|
||||
msg: exec,
|
||||
};
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
|
||||
Reference in New Issue
Block a user