This commit is contained in:
2026-09-20 04:14:32 +01:00
parent cda6cff34f
commit d5536b49d9
42 changed files with 1544 additions and 143 deletions
@@ -12,6 +12,8 @@ import type { TableType, User } from "@/src/types";
import checkPrivateIPAvailability from "./check-private-ip-availability";
import manageWireguardHost from "./manage-wireguard-host";
import grabHostDirnames from "./grab-host-dir-names";
import path from "node:path";
import grabClientDirnames from "./grab-client-dir-names";
const { WIREGUARD_PRIVATE_KEY_FILE_NAME, WIREGUARD_PUBLIC_KEY_FILE_NAME } =
grabDirNames();
@@ -20,12 +22,14 @@ type Params = {
host?: BUN_SQLITE_WGUI_HOSTS;
wg_subnet_ip?: string;
user: User;
is_update_after_client_setup?: boolean;
};
export default async function setupWireguardHost({
host,
wg_subnet_ip,
user,
is_update_after_client_setup,
}: Params): Promise<APIResponseObject> {
const {
HOST_CLIENTS_DIR,
@@ -57,7 +61,7 @@ export default async function setupWireguardHost({
query: {
query: {
host_id: {
value: HOST_ID,
value: String(HOST_ID),
},
},
},
@@ -78,35 +82,39 @@ export default async function setupWireguardHost({
};
}
const is_ip_available = await checkPrivateIPAvailability({
ip_address: HOST_WG_IP,
});
const is_ip_available = is_update_after_client_setup
? { success: true }
: await checkPrivateIPAvailability({
ip_address: HOST_WG_IP,
});
if (!is_ip_available.success) {
if (!is_ip_available.success && !is_update_after_client_setup) {
return {
success: false,
msg: `IP not available`,
};
}
let pre_sh = ``;
if (!is_update_after_client_setup) {
let pre_sh = ``;
pre_sh += `set -e\n`;
pre_sh += `mkdir -p ${HOST_CONFIG_DIR}\n`;
pre_sh += `mkdir -p ${HOST_CLIENTS_DIR}\n`;
pre_sh += `mkdir -p ${HOST_IPTABLES_DIR}\n`;
pre_sh += `cd ${HOST_CONFIG_DIR}\n`;
pre_sh += `if [ ! -f ${WIREGUARD_PRIVATE_KEY_FILE_NAME} ]; then\n`;
pre_sh += ` wg genkey | tee ${WIREGUARD_PRIVATE_KEY_FILE_NAME} | wg pubkey > ${WIREGUARD_PUBLIC_KEY_FILE_NAME}\n`;
pre_sh += `fi\n`;
pre_sh += `set -e\n`;
pre_sh += `mkdir -p ${HOST_CONFIG_DIR}\n`;
pre_sh += `mkdir -p ${HOST_CLIENTS_DIR}\n`;
pre_sh += `mkdir -p ${HOST_IPTABLES_DIR}\n`;
pre_sh += `cd ${HOST_CONFIG_DIR}\n`;
pre_sh += `if [ ! -f ${WIREGUARD_PRIVATE_KEY_FILE_NAME} ]; then\n`;
pre_sh += ` wg genkey | tee ${WIREGUARD_PRIVATE_KEY_FILE_NAME} | wg pubkey > ${WIREGUARD_PUBLIC_KEY_FILE_NAME}\n`;
pre_sh += `fi\n`;
try {
const exec_pre_setup = execSync(pre_sh, { encoding: "utf-8" });
} catch (error: any) {
return {
success: false,
msg: error.message,
};
try {
const exec_pre_setup = execSync(pre_sh, { encoding: "utf-8" });
} catch (error: any) {
return {
success: false,
msg: error.message,
};
}
}
try {
@@ -118,7 +126,7 @@ export default async function setupWireguardHost({
encoding: "utf-8",
}).trim();
if (HOST_ID == 0) {
if (HOST_ID == 0 && !is_update_after_client_setup) {
const update_variables = await BunSQLite.insert<
BUN_SQLITE_WGUI_VARIABLES,
TableType
@@ -140,7 +148,7 @@ export default async function setupWireguardHost({
if (!update_variables.success) {
throw new Error(`Couldn't update host variables`);
}
} else if (HOST_ID) {
} else if (HOST_ID && !is_update_after_client_setup) {
const update_host = await BunSQLite.insert<
BUN_SQLITE_WGUI_HOSTS,
TableType
@@ -208,10 +216,15 @@ export default async function setupWireguardHost({
if (clients[0]) {
for (let i = 0; i < clients.length; i++) {
const client = clients[i];
if (!client?.id || !client.public_key) continue;
if (!client?.id) continue;
const { CLIENT_PUBLIC_KEY } = grabClientDirnames({
client,
host_id: HOST_ID,
});
sh += `[Peer]\n`;
sh += `PublicKey = ${client.public_key}\n`;
sh += `PublicKey = ${CLIENT_PUBLIC_KEY}\n`;
sh += `AllowedIPs = ${client.allowed_ips}\n`;
sh += `\n`;
}
@@ -219,6 +232,8 @@ export default async function setupWireguardHost({
sh += `EOF\n`;
sh += `chmod 600 ${INTERFACE_NAME}.conf\n`;
sh += `\n`;
const exec = execSync(sh, { encoding: "utf-8" });
@@ -240,6 +255,11 @@ export default async function setupWireguardHost({
msg: [exec.trim(), manage_res.msg].join("\n\n"),
};
} catch (error: any) {
console.log(
`Error setting up wireguard host ${HOST_ID} =>`,
error.message,
);
return {
success: false,
msg: error.message,