Updates
This commit is contained in:
@@ -12,6 +12,7 @@ import setupWireguardHost from "./setup-wireguard-host";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { TableType, User } from "@/src/types";
|
||||
import grabClientDirnames from "./grab-client-dir-names";
|
||||
|
||||
const {
|
||||
WGUI_LIB_HOSTS_CONFIGS_DIR,
|
||||
@@ -25,6 +26,12 @@ type Params = {
|
||||
client: BUN_SQLITE_WGUI_CLIENTS;
|
||||
host?: BUN_SQLITE_WGUI_HOSTS;
|
||||
user: User;
|
||||
/**
|
||||
* Skip the host setup/restart at the end. Use when
|
||||
* regenerating multiple clients and restarting the
|
||||
* host once afterwards instead.
|
||||
*/
|
||||
skip_host_setup?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -38,6 +45,7 @@ export default async function setupWireguardClient({
|
||||
client,
|
||||
host,
|
||||
user,
|
||||
skip_host_setup,
|
||||
}: Params): Promise<APIResponseObject> {
|
||||
const host_id = host?.id || AppData["WireguardHostID"];
|
||||
|
||||
@@ -50,7 +58,18 @@ export default async function setupWireguardClient({
|
||||
|
||||
const variables = variables_res.payload;
|
||||
|
||||
const CLIENT_WG_IP = client?.wg_ip_address;
|
||||
const {
|
||||
CLIENT_WG_IP,
|
||||
CLIENT_CONFIG_FILE,
|
||||
CLIENT_DIR,
|
||||
CLIENT_PRIVATE_KEY_FILE,
|
||||
CLIENT_PUBLIC_KEY_FILE,
|
||||
HOST_CLIENTS_DIR,
|
||||
HOST_CONFIG_DIR,
|
||||
CLIENT_PRIVATE_KEY,
|
||||
CLIENT_PUBLIC_KEY,
|
||||
HOST_PUBLIC_KEY,
|
||||
} = grabClientDirnames({ client, host_id });
|
||||
|
||||
if (!CLIENT_WG_IP) {
|
||||
return {
|
||||
@@ -59,18 +78,6 @@ export default async function setupWireguardClient({
|
||||
};
|
||||
}
|
||||
|
||||
const HOST_CONFIG_DIR = path.join(
|
||||
WGUI_LIB_HOSTS_CONFIGS_DIR,
|
||||
String(host_id),
|
||||
);
|
||||
|
||||
const HOST_CLIENTS_DIR = path.join(
|
||||
HOST_CONFIG_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;
|
||||
@@ -82,19 +89,6 @@ export default async function setupWireguardClient({
|
||||
};
|
||||
}
|
||||
|
||||
const CLIENT_PRIVATE_KEY_FILE = path.join(
|
||||
CLIENT_DIR,
|
||||
WIREGUARD_PRIVATE_KEY_FILE_NAME,
|
||||
);
|
||||
const CLIENT_PUBLIC_KEY_FILE = path.join(
|
||||
CLIENT_DIR,
|
||||
WIREGUARD_PUBLIC_KEY_FILE_NAME,
|
||||
);
|
||||
const CLIENT_CONFIG_FILE = path.join(
|
||||
CLIENT_DIR,
|
||||
WIREGUARD_CLIENT_CONFIG_FILE_NAME,
|
||||
);
|
||||
|
||||
let pre_sh = ``;
|
||||
|
||||
pre_sh += `mkdir -p ${CLIENT_DIR}\n`;
|
||||
@@ -112,20 +106,10 @@ export default async function setupWireguardClient({
|
||||
}
|
||||
|
||||
try {
|
||||
const CLIENT_PRIVATE_KEY = execSync(`cat ${CLIENT_PRIVATE_KEY_FILE}`, {
|
||||
encoding: "utf-8",
|
||||
}).trim();
|
||||
|
||||
const CLIENT_PUBLIC_KEY = execSync(`cat ${CLIENT_PUBLIC_KEY_FILE}`, {
|
||||
encoding: "utf-8",
|
||||
}).trim();
|
||||
|
||||
const HOST_PUBLIC_KEY = execSync(
|
||||
`cat ${path.join(HOST_CONFIG_DIR, WIREGUARD_PUBLIC_KEY_FILE_NAME)}`,
|
||||
{ encoding: "utf-8" },
|
||||
).trim();
|
||||
|
||||
const PUBLIC_IP_ADDRESS =
|
||||
host?.public_ip_address ||
|
||||
variables?.find((v) => v.key == "main_host_public_ip_address")
|
||||
?.value ||
|
||||
(await grabHostPublicIPAddress()) ||
|
||||
client?.public_ip_address ||
|
||||
HOST_WG_IP;
|
||||
@@ -138,26 +122,38 @@ export default async function setupWireguardClient({
|
||||
sh += `[Interface]\n`;
|
||||
sh += `Address = ${CLIENT_WG_IP}/32\n`;
|
||||
sh += `PrivateKey = ${CLIENT_PRIVATE_KEY}\n`;
|
||||
sh += `DNS = 1.1.1.1\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`;
|
||||
|
||||
if (client?.allowed_ips) {
|
||||
if (client?.allow_all_ips == 1) {
|
||||
sh += `AllowedIPs = 0.0.0.0/0, ::/0\n`;
|
||||
} else if (client?.allowed_ips) {
|
||||
sh += `AllowedIPs = ${client.allowed_ips}\n`;
|
||||
}
|
||||
|
||||
sh += `PersistentKeepalive = 25\n`;
|
||||
|
||||
sh += `EOF\n`;
|
||||
sh += `\n`;
|
||||
|
||||
const exec = execSync(sh, { encoding: "utf-8" });
|
||||
const exec = execSync(sh, {
|
||||
encoding: "utf-8",
|
||||
});
|
||||
|
||||
const CLIENT_CONFIG = execSync(`cat ${CLIENT_CONFIG_FILE}`, {
|
||||
encoding: "utf-8",
|
||||
});
|
||||
|
||||
const host_setup_res = await setupWireguardHost({ host, user });
|
||||
const host_setup_res = skip_host_setup
|
||||
? { success: true }
|
||||
: await setupWireguardHost({
|
||||
host,
|
||||
user,
|
||||
is_update_after_client_setup: true,
|
||||
});
|
||||
|
||||
if (!host_setup_res.success) {
|
||||
return {
|
||||
@@ -172,6 +168,11 @@ export default async function setupWireguardClient({
|
||||
stringRes: CLIENT_CONFIG,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.log(
|
||||
`Error setting up wireguard client ${client.id} for host ${host_id} =>`,
|
||||
error.message,
|
||||
);
|
||||
|
||||
return {
|
||||
success: false,
|
||||
msg: error.message,
|
||||
|
||||
Reference in New Issue
Block a user