Update host update form for main host (host 0)
This commit is contained in:
@@ -42,7 +42,7 @@ export default function deriveHostConfig({ host, clients }: Params) {
|
|||||||
config_path: host?.dir_names?.HOST_CONFIG_FILE,
|
config_path: host?.dir_names?.HOST_CONFIG_FILE,
|
||||||
|
|
||||||
address: wg_ip_address ? `${wg_ip_address}/24` : undefined,
|
address: wg_ip_address ? `${wg_ip_address}/24` : undefined,
|
||||||
listen_port: AppData["DefaultWGListenPort"],
|
listen_port: host?.listen_port || AppData["DefaultWGListenPort"],
|
||||||
post_up: host?.dir_names?.POST_UP_PATH,
|
post_up: host?.dir_names?.POST_UP_PATH,
|
||||||
post_down: host?.dir_names?.POST_DOWN_PATH,
|
post_down: host?.dir_names?.POST_DOWN_PATH,
|
||||||
public_key: public_key,
|
public_key: public_key,
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export default function AddHostFormInterface({
|
|||||||
}: Props) {
|
}: Props) {
|
||||||
const interfaces = app_context.pageProps?.network_interfaces || [];
|
const interfaces = app_context.pageProps?.network_interfaces || [];
|
||||||
|
|
||||||
if (existing_full?.id) {
|
if (existing_full?.interface) {
|
||||||
return (
|
return (
|
||||||
<Input
|
<Input
|
||||||
value={existing_full.interface || "N/A"}
|
value={existing_full.interface || "N/A"}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ export default function AddHostFormListenPort({
|
|||||||
}: Props) {
|
}: Props) {
|
||||||
const [busy, setBusy] = useState(false);
|
const [busy, setBusy] = useState(false);
|
||||||
|
|
||||||
if (existing_full?.id) {
|
if (existing_full?.listen_port) {
|
||||||
return (
|
return (
|
||||||
<Input
|
<Input
|
||||||
value={existing_full.listen_port || "N/A"}
|
value={existing_full.listen_port || "N/A"}
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ export default function AddHostFormPublicIP({
|
|||||||
fetchIP(form.interface, useLocalIP);
|
fetchIP(form.interface, useLocalIP);
|
||||||
}, [form.interface, useLocalIP]);
|
}, [form.interface, useLocalIP]);
|
||||||
|
|
||||||
if (existing_full?.id) {
|
if (existing_full?.public_ip_address) {
|
||||||
return (
|
return (
|
||||||
<Input
|
<Input
|
||||||
value={existing_full.public_ip_address || "N/A"}
|
value={existing_full.public_ip_address || "N/A"}
|
||||||
|
|||||||
@@ -1,156 +0,0 @@
|
|||||||
import type { BUN_SQLITE_WGUI_CLIENT_RULES } from "@/db/types/db";
|
|
||||||
import expandClientRuleDestinations from "@/src/functions/backend/setup/expand-client-rule-destinations";
|
|
||||||
import sanitizeClientRule from "@/src/functions/backend/setup/sanitize-client-rule";
|
|
||||||
import validateClientRule from "@/src/functions/backend/setup/validate-client-rule";
|
|
||||||
import type { TableType } from "@/src/types";
|
|
||||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
|
||||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
|
||||||
|
|
||||||
type Params = {
|
|
||||||
client_id: number;
|
|
||||||
user_id?: number | "";
|
|
||||||
rules?: BUN_SQLITE_WGUI_CLIENT_RULES[];
|
|
||||||
};
|
|
||||||
|
|
||||||
export default async function syncClientRules({
|
|
||||||
client_id,
|
|
||||||
user_id,
|
|
||||||
rules,
|
|
||||||
}: Params): Promise<APIResponseObject> {
|
|
||||||
const existing_res = await BunSQLite.select<
|
|
||||||
BUN_SQLITE_WGUI_CLIENT_RULES,
|
|
||||||
TableType
|
|
||||||
>({
|
|
||||||
table: "client_rules",
|
|
||||||
query: {
|
|
||||||
query: {
|
|
||||||
client_id: {
|
|
||||||
value: String(client_id),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!existing_res.success) {
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
msg: existing_res.msg || `Could not load client rules`,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const incoming: BUN_SQLITE_WGUI_CLIENT_RULES[] = [];
|
|
||||||
|
|
||||||
for (let i = 0; i < (rules || []).length; i++) {
|
|
||||||
const sanitized = sanitizeClientRule({
|
|
||||||
rule: rules?.[i],
|
|
||||||
client_id,
|
|
||||||
user_id,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!sanitized) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const expanded = expandClientRuleDestinations({ rule: sanitized });
|
|
||||||
|
|
||||||
for (let e = 0; e < expanded.length; e++) {
|
|
||||||
const rule = expanded[e];
|
|
||||||
|
|
||||||
if (!rule) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const valid = validateClientRule({ rule });
|
|
||||||
|
|
||||||
if (!valid.success) {
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
msg: valid.msg,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
incoming.push(rule);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const incoming_ids = new Set(
|
|
||||||
incoming
|
|
||||||
.map((rule) => rule.id)
|
|
||||||
.filter((id): id is number => Boolean(id) && typeof id == "number"),
|
|
||||||
);
|
|
||||||
|
|
||||||
const existing = existing_res.payload || [];
|
|
||||||
|
|
||||||
for (let i = 0; i < existing.length; i++) {
|
|
||||||
const rule = existing[i];
|
|
||||||
|
|
||||||
if (!rule?.id || incoming_ids.has(rule.id)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const del_res = await BunSQLite.delete<
|
|
||||||
BUN_SQLITE_WGUI_CLIENT_RULES,
|
|
||||||
TableType
|
|
||||||
>({
|
|
||||||
table: "client_rules",
|
|
||||||
targetId: rule.id,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!del_res.success) {
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
msg: del_res.msg || `Could not delete client rule`,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const to_insert = incoming.filter((rule) => !rule.id);
|
|
||||||
const to_update = incoming.filter(
|
|
||||||
(rule) => rule.id && typeof rule.id == "number",
|
|
||||||
);
|
|
||||||
|
|
||||||
for (let i = 0; i < to_update.length; i++) {
|
|
||||||
const rule = to_update[i];
|
|
||||||
|
|
||||||
if (!rule?.id) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const update_res = await BunSQLite.update<
|
|
||||||
BUN_SQLITE_WGUI_CLIENT_RULES,
|
|
||||||
TableType
|
|
||||||
>({
|
|
||||||
table: "client_rules",
|
|
||||||
data: rule,
|
|
||||||
targetId: rule.id,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!update_res.success) {
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
msg: update_res.msg || `Could not update client rule`,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (to_insert[0]) {
|
|
||||||
const insert_res = await BunSQLite.insert<
|
|
||||||
BUN_SQLITE_WGUI_CLIENT_RULES,
|
|
||||||
TableType
|
|
||||||
>({
|
|
||||||
table: "client_rules",
|
|
||||||
data: to_insert,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!insert_res.success) {
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
msg: insert_res.msg || `Could not insert client rules`,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
success: true,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user