Update hosts form
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import type { BUN_SQLITE_WGUI_HOSTS, BUN_SQLITE_WGUI_VARIABLES } from "@/db/types/db";
|
||||
import type {
|
||||
BUN_SQLITE_WGUI_HOSTS,
|
||||
BUN_SQLITE_WGUI_VARIABLES,
|
||||
} from "@/db/types/db";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
import type { TableType, User } from "@/src/types";
|
||||
@@ -9,7 +12,11 @@ type Params = {
|
||||
wg_ip_address?: string | null;
|
||||
public_ip_address?: string | null;
|
||||
interface?: string | null;
|
||||
name?: string | null;
|
||||
short_description?: string | null;
|
||||
listen_port?: number | null;
|
||||
user: User;
|
||||
is_main_host?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -24,7 +31,11 @@ export default async function createWireguardHost({
|
||||
wg_ip_address,
|
||||
public_ip_address,
|
||||
interface: iface,
|
||||
name,
|
||||
short_description,
|
||||
listen_port,
|
||||
user,
|
||||
is_main_host,
|
||||
}: Params): Promise<APIResponseObject> {
|
||||
const wg_ip = (wg_ip_address || "").trim();
|
||||
|
||||
@@ -38,24 +49,34 @@ export default async function createWireguardHost({
|
||||
const detected_public_ip = await grabHostPublicIPAddress();
|
||||
const final_public_ip = public_ip_address?.trim() || detected_public_ip;
|
||||
const final_interface = iface?.trim() || undefined;
|
||||
const final_name = name?.trim() || undefined;
|
||||
const final_short_description = short_description?.trim() || undefined;
|
||||
|
||||
try {
|
||||
const insert_data: BUN_SQLITE_WGUI_HOSTS = {
|
||||
user_id: user.id,
|
||||
wg_ip_address: wg_ip,
|
||||
public_ip_address: final_public_ip || undefined,
|
||||
interface: final_interface,
|
||||
name: final_name,
|
||||
short_description: final_short_description,
|
||||
listen_port: listen_port != null ? listen_port : undefined,
|
||||
};
|
||||
|
||||
if (is_main_host) {
|
||||
insert_data.id = 0;
|
||||
}
|
||||
|
||||
const insert_host = await BunSQLite.insert<
|
||||
BUN_SQLITE_WGUI_HOSTS,
|
||||
TableType
|
||||
>({
|
||||
table: "hosts",
|
||||
data: [
|
||||
{
|
||||
user_id: user.id,
|
||||
wg_ip_address: wg_ip,
|
||||
public_ip_address: final_public_ip || undefined,
|
||||
interface: final_interface,
|
||||
},
|
||||
],
|
||||
data: [insert_data],
|
||||
update_on_duplicate: true,
|
||||
});
|
||||
|
||||
if (!insert_host.success) {
|
||||
if (!insert_host.success || !insert_host.postInsertReturn?.insertId) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Could not create the host record: ${insert_host.msg}`,
|
||||
@@ -70,13 +91,13 @@ export default async function createWireguardHost({
|
||||
wg_ip_address: wg_ip,
|
||||
public_ip_address: final_public_ip || undefined,
|
||||
interface: final_interface,
|
||||
name: final_name,
|
||||
short_description: final_short_description,
|
||||
listen_port: listen_port != null ? listen_port : undefined,
|
||||
};
|
||||
|
||||
if (host_id === 0 && final_interface) {
|
||||
await BunSQLite.insert<
|
||||
BUN_SQLITE_WGUI_VARIABLES,
|
||||
TableType
|
||||
>({
|
||||
await BunSQLite.insert<BUN_SQLITE_WGUI_VARIABLES, TableType>({
|
||||
table: "variables",
|
||||
data: [
|
||||
{
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import { exec } from "node:child_process";
|
||||
import { promisify } from "node:util";
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
|
||||
const execAsync = promisify(exec);
|
||||
|
||||
/**
|
||||
* Function to get the next available listen port on the system.
|
||||
* Checks both UDP and TCP listening ports.
|
||||
*/
|
||||
export default async function getNextAvailableListenPort(): Promise<number> {
|
||||
const defaultPort = AppData["DefaultWGListenPort"] ?? 51820;
|
||||
|
||||
try {
|
||||
const { stdout } = await execAsync("ss -tuln");
|
||||
const usedPorts = new Set<number>();
|
||||
|
||||
for (const line of stdout.split("\n")) {
|
||||
const match = line.match(/[:](\d+)\s/);
|
||||
if (match && match[1]) {
|
||||
const port = Number.parseInt(match[1], 10);
|
||||
if (!Number.isNaN(port)) {
|
||||
usedPorts.add(port);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let port = defaultPort;
|
||||
while (usedPorts.has(port)) {
|
||||
port++;
|
||||
if (port > 65535) {
|
||||
return defaultPort;
|
||||
}
|
||||
}
|
||||
|
||||
return port;
|
||||
} catch (error) {
|
||||
return defaultPort;
|
||||
}
|
||||
}
|
||||
@@ -14,13 +14,8 @@ 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,
|
||||
WIREGUARD_PRIVATE_KEY_FILE_NAME,
|
||||
WIREGUARD_PUBLIC_KEY_FILE_NAME,
|
||||
WGUI_LIB_HOST_CLIENTS_DIR_NAME,
|
||||
WIREGUARD_CLIENT_CONFIG_FILE_NAME,
|
||||
} = grabDirNames();
|
||||
const { WIREGUARD_PUBLIC_KEY_FILE_NAME, WIREGUARD_CLIENT_CONFIG_FILE_NAME } =
|
||||
grabDirNames();
|
||||
|
||||
type Params = {
|
||||
client: BUN_SQLITE_WGUI_CLIENTS;
|
||||
@@ -135,7 +130,7 @@ export default async function setupWireguardClient({
|
||||
sh += `\n`;
|
||||
sh += `[Peer]\n`;
|
||||
sh += `PublicKey = ${HOST_PUBLIC_KEY}\n`;
|
||||
sh += `Endpoint = ${PUBLIC_IP_ADDRESS}:51820\n`;
|
||||
sh += `Endpoint = ${PUBLIC_IP_ADDRESS}:${host?.listen_port || AppData["DefaultWGListenPort"]}\n`;
|
||||
|
||||
if (client?.allow_all_ips == 1) {
|
||||
sh += `AllowedIPs = 0.0.0.0/0, ::/0\n`;
|
||||
|
||||
@@ -15,6 +15,7 @@ import manageWireguardHost from "./manage-wireguard-host";
|
||||
import grabHostDirnames from "./grab-host-dir-names";
|
||||
import grabClientDirnames from "./grab-client-dir-names";
|
||||
import buildHostIptablesScripts from "./build-host-iptables-scripts";
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
|
||||
const { WIREGUARD_PRIVATE_KEY_FILE_NAME, WIREGUARD_PUBLIC_KEY_FILE_NAME } =
|
||||
grabDirNames();
|
||||
@@ -100,7 +101,8 @@ export default async function setupWireguardHost({
|
||||
rules_by_client_id.set(rule.client_id, existing_rules);
|
||||
}
|
||||
|
||||
const TARGET_INTERFACE = host?.interface || await grabHostNetworkInterface();
|
||||
const TARGET_INTERFACE =
|
||||
host?.interface || (await grabHostNetworkInterface());
|
||||
const HOST_WG_IP =
|
||||
host?.wg_ip_address ||
|
||||
variables?.find((v) => v.key == "main_host_wg_ip_address")?.value ||
|
||||
@@ -246,7 +248,7 @@ export default async function setupWireguardHost({
|
||||
sh += `cat > ${INTERFACE_NAME}.conf << EOF\n`;
|
||||
sh += `[Interface]\n`;
|
||||
sh += `Address = ${HOST_WG_IP}/24\n`;
|
||||
sh += `ListenPort = 51820\n`;
|
||||
sh += `ListenPort = ${host?.listen_port || AppData["DefaultWGListenPort"]}\n`;
|
||||
sh += `PrivateKey = ${HOST_PRIVATE_KEY}\n`;
|
||||
sh += `PostUp = ${POST_UP_PATH}\n`;
|
||||
sh += `PostDown = ${POST_DOWN_PATH}\n`;
|
||||
@@ -284,6 +286,13 @@ export default async function setupWireguardHost({
|
||||
|
||||
const exec = execSync(sh, { encoding: "utf-8" });
|
||||
|
||||
if (!is_update_after_client_setup) {
|
||||
execSync(
|
||||
`ip link set ${INTERFACE_NAME} down 2>/dev/null; ip link del ${INTERFACE_NAME} 2>/dev/null; true`,
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
}
|
||||
|
||||
const manage_res = manageWireguardHost({
|
||||
host_id: HOST_ID,
|
||||
action: "restart",
|
||||
|
||||
Reference in New Issue
Block a user