diff --git a/src/pages/admin/hosts/(functions)/derive-host-config.ts b/src/pages/admin/hosts/(functions)/derive-host-config.ts index 2886d42..9a42d34 100644 --- a/src/pages/admin/hosts/(functions)/derive-host-config.ts +++ b/src/pages/admin/hosts/(functions)/derive-host-config.ts @@ -42,7 +42,7 @@ export default function deriveHostConfig({ host, clients }: Params) { config_path: host?.dir_names?.HOST_CONFIG_FILE, 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_down: host?.dir_names?.POST_DOWN_PATH, public_key: public_key, diff --git a/src/pages/admin/hosts/add/(partials)/add-host-form/add-host-form-interface.tsx b/src/pages/admin/hosts/add/(partials)/add-host-form/add-host-form-interface.tsx index eec78e1..2047187 100644 --- a/src/pages/admin/hosts/add/(partials)/add-host-form/add-host-form-interface.tsx +++ b/src/pages/admin/hosts/add/(partials)/add-host-form/add-host-form-interface.tsx @@ -14,7 +14,7 @@ export default function AddHostFormInterface({ }: Props) { const interfaces = app_context.pageProps?.network_interfaces || []; - if (existing_full?.id) { + if (existing_full?.interface) { return ( { - 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, - }; -}