Refactor main host setup
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import type {
|
||||
BUN_SQLITE_WGUI_CLIENTS,
|
||||
BUN_SQLITE_WGUI_HOSTS,
|
||||
BUN_SQLITE_WGUI_VARIABLES,
|
||||
} from "@/db/types/db";
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import grabDirNames from "@/src/utils/grab-dir-names";
|
||||
@@ -42,16 +41,20 @@ export default async function setupWireguardClient({
|
||||
user,
|
||||
skip_host_setup,
|
||||
}: Params): Promise<APIResponseObject> {
|
||||
const host_id = host?.id || AppData["WireguardHostID"];
|
||||
const host_id = host?.id || AppData["WireguardMainHostID"];
|
||||
|
||||
const variables_res = await BunSQLite.select<
|
||||
BUN_SQLITE_WGUI_VARIABLES,
|
||||
TableType
|
||||
>({
|
||||
table: "variables",
|
||||
});
|
||||
let resolved_host = host;
|
||||
|
||||
const variables = variables_res.payload;
|
||||
if (!resolved_host) {
|
||||
const host_res = await BunSQLite.select<
|
||||
BUN_SQLITE_WGUI_HOSTS,
|
||||
TableType
|
||||
>({
|
||||
table: "hosts",
|
||||
targetId: host_id,
|
||||
});
|
||||
resolved_host = host_res.singleRes || undefined;
|
||||
}
|
||||
|
||||
const {
|
||||
CLIENT_WG_IP,
|
||||
@@ -69,9 +72,7 @@ export default async function setupWireguardClient({
|
||||
};
|
||||
}
|
||||
|
||||
const HOST_WG_IP = host?.id
|
||||
? host?.wg_ip_address
|
||||
: variables?.find((v) => v.key == "main_host_wg_ip_address")?.value;
|
||||
const HOST_WG_IP = resolved_host?.wg_ip_address;
|
||||
|
||||
if (!HOST_WG_IP) {
|
||||
return {
|
||||
@@ -98,9 +99,7 @@ export default async function setupWireguardClient({
|
||||
|
||||
try {
|
||||
const PUBLIC_IP_ADDRESS =
|
||||
host?.public_ip_address ||
|
||||
variables?.find((v) => v.key == "main_host_public_ip_address")
|
||||
?.value ||
|
||||
resolved_host?.public_ip_address ||
|
||||
(await grabHostPublicIPAddress()) ||
|
||||
client?.public_ip_address ||
|
||||
HOST_WG_IP;
|
||||
@@ -130,7 +129,7 @@ export default async function setupWireguardClient({
|
||||
sh += `\n`;
|
||||
sh += `[Peer]\n`;
|
||||
sh += `PublicKey = ${HOST_PUBLIC_KEY}\n`;
|
||||
sh += `Endpoint = ${PUBLIC_IP_ADDRESS}:${host?.listen_port || AppData["DefaultWGListenPort"]}\n`;
|
||||
sh += `Endpoint = ${PUBLIC_IP_ADDRESS}:${resolved_host?.listen_port || AppData["DefaultWGListenPort"]}\n`;
|
||||
|
||||
if (client?.allow_all_ips == 1) {
|
||||
sh += `AllowedIPs = 0.0.0.0/0, ::/0\n`;
|
||||
@@ -154,7 +153,7 @@ export default async function setupWireguardClient({
|
||||
const host_setup_res = skip_host_setup
|
||||
? { success: true }
|
||||
: await setupWireguardHost({
|
||||
host,
|
||||
host: resolved_host,
|
||||
user,
|
||||
is_update_after_client_setup: true,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user