Major updates

This commit is contained in:
2026-09-14 07:56:13 +01:00
parent 42224c94d3
commit e0d105beb9
17 changed files with 405 additions and 277 deletions
@@ -11,20 +11,20 @@ import grabHostPublicIPAddress from "./grab-host-public-ip-address";
import setupWireguardHost from "./setup-wireguard-host";
import type { APIResponseObject } from "@moduletrace/bunext/types";
import BunSQLite from "@moduletrace/bun-sqlite";
import type { TableType } from "@/src/types";
import type { TableType, User } from "@/src/types";
const {
WGUI_LIB_CLIENTS_CONFIGS_DIR,
WGUI_LIB_HOSTS_CONFIGS_DIR,
WIREGUARD_PRIVATE_KEY_FILE_NAME,
WIREGUARD_PUBLIC_KEY_FILE_NAME,
WGUI_LIB_KEYS_DIR,
WGUI_LIB_HOST_CLIENTS_DIR_NAME,
WIREGUARD_CLIENT_CONFIG_FILE_NAME,
} = grabDirNames();
type Params = {
client: BUN_SQLITE_WGUI_CLIENTS;
host?: BUN_SQLITE_WGUI_HOSTS;
user: User;
};
/**
@@ -37,6 +37,7 @@ type Params = {
export default async function setupWireguardClient({
client,
host,
user,
}: Params): Promise<APIResponseObject> {
const host_id = host?.id || AppData["WireguardHostID"];
@@ -58,9 +59,21 @@ export default async function setupWireguardClient({
};
}
const HOST_WG_IP =
host?.wg_ip_address ||
variables?.find((v) => v.key == "main_host_wg_ip_address")?.value;
const HOST_CONFIG_DIR = path.join(
WGUI_LIB_HOSTS_CONFIGS_DIR,
String(host_id),
);
const HOST_CLIENTS_DIR = path.join(
WGUI_LIB_HOSTS_CONFIGS_DIR,
WGUI_LIB_HOST_CLIENTS_DIR_NAME,
);
const CLIENT_DIR = path.join(HOST_CLIENTS_DIR, `${client.id}`);
const HOST_WG_IP = host?.id
? host?.wg_ip_address
: variables?.find((v) => v.key == "main_host_wg_ip_address")?.value;
if (!HOST_WG_IP) {
return {
@@ -69,8 +82,6 @@ export default async function setupWireguardClient({
};
}
const CLIENT_DIR = path.join(WGUI_LIB_CLIENTS_CONFIGS_DIR, `${client.id}`);
const CLIENT_PRIVATE_KEY_FILE = path.join(
CLIENT_DIR,
WIREGUARD_PRIVATE_KEY_FILE_NAME,
@@ -101,66 +112,44 @@ export default async function setupWireguardClient({
}
try {
const CLIENT_PRIVATE_KEY =
client?.private_key ||
execSync(`cat ${CLIENT_PRIVATE_KEY_FILE}`, {
encoding: "utf-8",
}).trim();
const CLIENT_PRIVATE_KEY = execSync(`cat ${CLIENT_PRIVATE_KEY_FILE}`, {
encoding: "utf-8",
}).trim();
if (CLIENT_PRIVATE_KEY && !client.private_key && client.id) {
await BunSQLite.update<BUN_SQLITE_WGUI_CLIENTS, TableType>({
data: { private_key: CLIENT_PRIVATE_KEY },
table: "clients",
targetId: client.id,
});
}
const CLIENT_PUBLIC_KEY = execSync(`cat ${CLIENT_PUBLIC_KEY_FILE}`, {
encoding: "utf-8",
}).trim();
const CLIENT_PUBLIC_KEY =
client?.public_key ||
execSync(`cat ${CLIENT_PUBLIC_KEY_FILE}`, {
encoding: "utf-8",
}).trim();
if (CLIENT_PUBLIC_KEY && !client.public_key && client.id) {
await BunSQLite.update<BUN_SQLITE_WGUI_CLIENTS, TableType>({
data: { public_key: CLIENT_PUBLIC_KEY },
table: "clients",
targetId: client.id,
});
}
const HOST_PUBLIC_KEY =
host?.public_key ||
execSync(
`cat ${path.join(
WGUI_LIB_HOSTS_CONFIGS_DIR,
WIREGUARD_PUBLIC_KEY_FILE_NAME,
)}`,
{ encoding: "utf-8" },
).trim();
const ALLOWED_IPS = client?.allowed_ips || `0.0.0.0/0`;
const HOST_PUBLIC_KEY = execSync(
`cat ${path.join(HOST_CONFIG_DIR, WIREGUARD_PUBLIC_KEY_FILE_NAME)}`,
{ encoding: "utf-8" },
).trim();
const PUBLIC_IP_ADDRESS =
(await grabHostPublicIPAddress()) ||
client?.public_ip_address ||
HOST_WG_IP;
const sh = `
cd ${CLIENT_DIR}
let sh = ``;
cat > ${WIREGUARD_CLIENT_CONFIG_FILE_NAME} << EOF
[Interface]
Address = ${CLIENT_WG_IP}/32
PrivateKey = ${CLIENT_PRIVATE_KEY}
DNS = 1.1.1.1
sh += `cd ${CLIENT_DIR}\n`;
sh += `cat > ${WIREGUARD_CLIENT_CONFIG_FILE_NAME} << EOF\n`;
sh += `\n`;
sh += `[Interface]\n`;
sh += `Address = ${CLIENT_WG_IP}/32\n`;
sh += `PrivateKey = ${CLIENT_PRIVATE_KEY}\n`;
sh += `DNS = 1.1.1.1\n`;
sh += `\n`;
sh += `[Peer]\n`;
sh += `PublicKey = ${HOST_PUBLIC_KEY}\n`;
sh += `Endpoint = ${PUBLIC_IP_ADDRESS}:51820\n`;
[Peer]
PublicKey = ${HOST_PUBLIC_KEY}
Endpoint = ${PUBLIC_IP_ADDRESS}:51820
AllowedIPs = ${ALLOWED_IPS}
EOF
`;
if (client?.allowed_ips) {
sh += `AllowedIPs = ${client.allowed_ips}\n`;
}
sh += `EOF\n`;
sh += `\n`;
const exec = execSync(sh, { encoding: "utf-8" });
@@ -168,7 +157,7 @@ EOF
encoding: "utf-8",
});
const host_setup_res = await setupWireguardHost({ host });
const host_setup_res = await setupWireguardHost({ host, user });
if (!host_setup_res.success) {
return {