opnsense: filter queried connections

This commit is contained in:
2024-12-22 00:55:33 -08:00
parent e03bf11fa5
commit bdea663178
12 changed files with 155 additions and 54 deletions

View File

@@ -1,9 +1,12 @@
import { error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { opnsenseAuth, opnsenseIfname, opnsenseUrl } from '$lib/server/opnsense';
import { opnsenseAuth, opnsenseUrl } from '$lib/server/opnsense';
import type { OpnsenseWgPeers } from '$lib/opnsense/wg';
export const GET: RequestHandler = async () => {
export const GET: RequestHandler = async (event) => {
if (!event.locals.user) {
return error(401, 'Unauthorized');
}
const apiUrl = `${opnsenseUrl}/api/wireguard/service/show`;
const options: RequestInit = {
method: 'POST',
@@ -13,21 +16,24 @@ export const GET: RequestHandler = async () => {
'Content-Type': 'application/json',
},
body: JSON.stringify({
"current": 1,
'current': 1,
// "rowCount": 7,
"sort": {},
"searchPhrase": "",
"type": ["peer"],
'sort': {},
// TODO: use a more unique search phrase
// unfortunately 64 character limit,
// but it should be fine if users can't change their own username
'searchPhrase': `vpgen-${event.locals.user.username}`,
'type': ['peer'],
}),
};
console.log("Fetching peers from OPNsense WireGuard API: ", apiUrl, options)
console.log('Fetching peers from OPNsense WireGuard API: ', apiUrl, options)
const res = await fetch(apiUrl, options);
const peers = await res.json() as OpnsenseWgPeers;
peers.rows = peers.rows.filter(peer => peer['latest-handshake'] && peer.ifname === opnsenseIfname)
peers.rows = peers.rows.filter(peer => peer['latest-handshake'])
if (!peers) {
error(500, "Error getting info from OPNsense API");
return error(500, 'Error getting info from OPNsense API');
}
return new Response(JSON.stringify(peers), {
headers: {