Major updates
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import grabDirNames from "@/src/utils/grab-dir-names";
|
||||
import deriveWireguardInterfaceName from "@/src/utils/derive-wireguard-interface-name";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
import { execSync } from "node:child_process";
|
||||
import path from "node:path";
|
||||
|
||||
const { WGUI_LIB_HOSTS_CONFIGS_DIR, WGUI_WG_QUICK_MANAGE_SCRIPT } =
|
||||
grabDirNames();
|
||||
|
||||
export type WireguardHostAction = "up" | "down" | "restart";
|
||||
|
||||
type Params = {
|
||||
action: WireguardHostAction;
|
||||
host_id?: number;
|
||||
};
|
||||
|
||||
export default function manageWireguardHost({
|
||||
action,
|
||||
host_id = AppData["WireguardHostID"],
|
||||
}: Params): APIResponseObject {
|
||||
const INTERFACE_NAME = deriveWireguardInterfaceName({ host_id });
|
||||
const HOST_CONFIG_PATH = path.join(
|
||||
WGUI_LIB_HOSTS_CONFIGS_DIR,
|
||||
`${INTERFACE_NAME}.conf`,
|
||||
);
|
||||
|
||||
const IS_ROOT =
|
||||
typeof process.getuid === "function" && process.getuid() === 0;
|
||||
|
||||
if (!IS_ROOT) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "wg-ui must run as root to manage wireguard tunnels",
|
||||
};
|
||||
}
|
||||
|
||||
const MANAGE_COMMAND = `${WGUI_WG_QUICK_MANAGE_SCRIPT} ${action} ${INTERFACE_NAME} ${HOST_CONFIG_PATH}`;
|
||||
|
||||
try {
|
||||
const output = execSync(MANAGE_COMMAND, { encoding: "utf-8" }).trim();
|
||||
|
||||
return {
|
||||
success: true,
|
||||
msg: output,
|
||||
};
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Could not ${action} ${INTERFACE_NAME}: ${error.message}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user