Add host setup functions and client components

This commit is contained in:
2026-09-13 10:52:30 +01:00
parent 76679aabe7
commit 86eb3a0b05
53 changed files with 1284 additions and 1325 deletions
@@ -0,0 +1,81 @@
import type { APIResponseObject } from "@moduletrace/bunext/types";
import { execSync } from "node:child_process";
type Params = {
ip_address: string;
};
/**
* Function to check the availability of a private
* IP address (like 10.1.0.1)
* @param param0
*/
export default async function checkPrivateIPAvailability({
ip_address,
}: Params): Promise<APIResponseObject> {
const ip_pattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
const matches = ip_address.match(ip_pattern);
if (!matches) {
return {
success: false,
msg: `Invalid IP address: ${ip_address}`,
};
}
const octets = matches.slice(1).map(Number);
if (octets.some((o) => o < 0 || o > 255)) {
return {
success: false,
msg: `Invalid IP address: ${ip_address}`,
};
}
if (octets[3] !== 1) {
return {
success: false,
msg: `IP address must end with 1: ${ip_address}`,
};
}
const [octet1, octet2, octet3] = octets;
try {
const addr_output = execSync(`ip -4 addr show`, {
encoding: "utf-8",
});
for (const match of addr_output.matchAll(
/inet\s+(\d+\.\d+\.\d+\.\d+)\/\d+/g,
)) {
if (!match[1]) continue;
const [existing1, existing2, existing3] = match[1]
.split(".")
.map(Number);
if (
existing1 === octet1 &&
existing2 === octet2 &&
existing3 === octet3
) {
return {
success: false,
msg: `IP address ${ip_address} is already in use`,
};
}
}
return {
success: true,
msg: `IP address ${ip_address} is available`,
};
} catch (error: any) {
return {
success: false,
msg: error.message,
};
}
}
@@ -0,0 +1,102 @@
import type {
BUN_SQLITE_WGUI_CLIENTS,
BUN_SQLITE_WGUI_HOSTS,
BUN_SQLITE_WGUI_VARIABLES,
} from "@/db/types/db";
import type { APIResponseObject } from "@moduletrace/bunext/types";
import BunSQLite from "@moduletrace/bun-sqlite";
import type { TableType } from "@/src/types";
type Params = {
host?: BUN_SQLITE_WGUI_HOSTS;
};
/**
* Function to grab the next available client
* private IP address within the host's subnet.
* Checks the clients records and increments
* the last octet of the last client
* @param param0
*/
export default async function grabNextAvailableClientIPAddress({
host,
}: Params): Promise<APIResponseObject> {
const variables_res = await BunSQLite.select<
BUN_SQLITE_WGUI_VARIABLES,
TableType
>({
table: "variables",
});
const variables = variables_res.payload;
const HOST_WG_IP =
host?.wg_ip_address ||
variables?.find((v) => v.key == "main_host_wg_ip_address")?.value;
if (!HOST_WG_IP) {
return {
success: false,
msg: `No Host Private IP address provided`,
};
}
const parts = HOST_WG_IP.split(".");
if (parts.length !== 4) {
return {
success: false,
msg: `Invalid Host Private IP address: ${HOST_WG_IP}`,
};
}
const host_last_octet = Number(parts[3]);
if (Number.isNaN(host_last_octet)) {
return {
success: false,
msg: `Invalid Host Private IP address: ${HOST_WG_IP}`,
};
}
const subnet = `${parts[0]}.${parts[1]}.${parts[2]}.`;
const clients_res = await BunSQLite.select<
BUN_SQLITE_WGUI_CLIENTS,
TableType
>({
table: "clients",
});
const clients = clients_res.payload || [];
let max_last_octet = host_last_octet;
for (const client of clients) {
const client_ip = client?.wg_ip_address;
if (!client_ip || !client_ip.startsWith(subnet)) continue;
const client_last_octet = Number(client_ip.split(".")[3]);
if (Number.isNaN(client_last_octet)) continue;
if (client_last_octet > max_last_octet) {
max_last_octet = client_last_octet;
}
}
const next_last_octet = max_last_octet + 1;
if (next_last_octet > 254) {
return {
success: false,
msg: `No available client IP address in subnet ${subnet}0/24`,
};
}
return {
success: true,
msg: `${subnet}${next_last_octet}`,
};
}
@@ -0,0 +1,47 @@
import { AppData } from "@/src/data/app-data";
import type { APIResponseObject } from "@moduletrace/bunext/types";
import checkPrivateIPAvailability from "./check-private-ip-availability";
/**
* Function to grab the next available private
* IP subnet. Beginning with `10.1.0.1`. If not
* available then `10.2.0.1`, etc.
* @param param0
*/
export default async function grabNextAvailablePrivateIPSubnet(): Promise<APIResponseObject> {
const default_ip = AppData["DefaultPrivateIP"];
const parts = default_ip.split(".");
if (parts.length !== 4) {
return {
success: false,
msg: `Invalid default private IP address: ${default_ip}`,
};
}
const [octet1, octet3, octet4] = [parts[0], parts[2], parts[3]];
const start_octet = Number(parts[1]);
const max_octet = Number.isNaN(start_octet) ? 254 : 255;
for (let i = start_octet; i <= max_octet; i++) {
const candidate = `${octet1}.${i}.${octet3}.${octet4}`;
const availability_res = await checkPrivateIPAvailability({
ip_address: candidate,
});
if (availability_res.success) {
return {
success: true,
msg: candidate,
};
}
}
return {
success: false,
msg: `No available private IP subnet found`,
};
}
@@ -25,7 +25,6 @@ const {
type Params = {
client: BUN_SQLITE_WGUI_CLIENTS;
host?: BUN_SQLITE_WGUI_HOSTS;
variables?: BUN_SQLITE_WGUI_VARIABLES[];
};
/**
@@ -38,10 +37,18 @@ type Params = {
export default async function setupWireguardClient({
client,
host,
variables,
}: 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 CLIENT_WG_IP = client?.wg_ip_address;
if (!CLIENT_WG_IP) {
@@ -161,7 +168,7 @@ EOF
encoding: "utf-8",
});
const host_setup_res = await setupWireguardHost({ host, variables });
const host_setup_res = await setupWireguardHost({ host });
if (!host_setup_res.success) {
return {
@@ -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,
@@ -0,0 +1,20 @@
import type { BUN_SQLITE_WGUI_VARIABLES } from "@/db/types/db";
type Params = {
variables?: BUN_SQLITE_WGUI_VARIABLES[];
};
export default function checkIfMainHostIsSet({ variables }: Params) {
const is_main_host_set =
Boolean(
variables?.find((v) => v.key == "main_host_wg_ip_address")?.value,
) &&
Boolean(
variables?.find((v) => v.key == "main_host_wg_private_key")?.value,
) &&
Boolean(
variables?.find((v) => v.key == "main_host_wg_public_key")?.value,
);
return is_main_host_set;
}