WIP: delete opnsense-specific stuff in lib
This commit is contained in:
parent
24ded15ac9
commit
7b76726e6e
@ -1,3 +0,0 @@
|
|||||||
export function opnsenseSanitezedUsername(username: string) {
|
|
||||||
return username.slice(0, 63).replace(/[^a-zA-Z0-9_-]/g, '_');
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
import { env } from '$env/dynamic/private';
|
|
||||||
import assert from 'node:assert';
|
|
||||||
import { encodeBasicCredentials } from 'arctic/dist/request';
|
|
||||||
import { dev } from '$app/environment';
|
|
||||||
import type { OpnsenseWgServers } from '$lib/opnsense/wg';
|
|
||||||
|
|
||||||
export const opnsenseUrl = env.OPNSENSE_API_URL;
|
|
||||||
export const opnsenseAuth =
|
|
||||||
'Basic ' + encodeBasicCredentials(env.OPNSENSE_API_KEY, env.OPNSENSE_API_SECRET);
|
|
||||||
export const opnsenseIfname = env.OPNSENSE_WG_IFNAME;
|
|
||||||
|
|
||||||
// unset secret for security
|
|
||||||
if (!dev) env.OPNSENSE_API_SECRET = '';
|
|
||||||
|
|
||||||
export let serverUuid: string, serverPublicKey: string;
|
|
||||||
|
|
||||||
export async function fetchOpnsenseServer() {
|
|
||||||
// this might be pretty bad if the server is down and in a bunch of other cases
|
|
||||||
// TODO: write a retry loop later
|
|
||||||
const resServers = await fetch(`${opnsenseUrl}/api/wireguard/client/list_servers`, {
|
|
||||||
method: 'GET',
|
|
||||||
headers: {
|
|
||||||
Authorization: opnsenseAuth,
|
|
||||||
Accept: 'application/json',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
assert(resServers.ok, 'Failed to fetch OPNsense WireGuard servers');
|
|
||||||
const servers = (await resServers.json()) as OpnsenseWgServers;
|
|
||||||
assert.equal(servers.status, 'ok', 'Failed to fetch OPNsense WireGuard servers');
|
|
||||||
const uuid = servers.rows.find((server) => server.name === opnsenseIfname)?.uuid;
|
|
||||||
assert(uuid, 'Failed to find server UUID for OPNsense WireGuard server');
|
|
||||||
serverUuid = uuid;
|
|
||||||
console.log('OPNsense WireGuard server UUID:', serverUuid);
|
|
||||||
|
|
||||||
const resServerInfo = await fetch(
|
|
||||||
`${opnsenseUrl}/api/wireguard/client/get_server_info/${serverUuid}`,
|
|
||||||
{
|
|
||||||
method: 'GET',
|
|
||||||
headers: {
|
|
||||||
Authorization: opnsenseAuth,
|
|
||||||
Accept: 'application/json',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
);
|
|
||||||
assert(resServerInfo.ok, 'Failed to fetch OPNsense WireGuard server info');
|
|
||||||
const serverInfo = await resServerInfo.json();
|
|
||||||
assert.equal(serverInfo.status, 'ok', 'Failed to fetch OPNsense WireGuard server info');
|
|
||||||
serverPublicKey = serverInfo['pubkey'];
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
import { WgProviderOpnsense } from '$lib/server/providers/opnsense';
|
import { WgProviderOpnsense } from '$lib/server/wg-providers/opnsense';
|
||||||
import { env } from '$env/dynamic/private';
|
import { env } from '$env/dynamic/private';
|
||||||
import type { IWgProvider } from '$lib/server/types';
|
import type { IWgProvider } from '$lib/server/types';
|
||||||
|
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
import type { ClientConnection, CreateClientParams, IWgProvider, WgKeys } from '$lib/server/types';
|
import type { ClientConnection, CreateClientParams, IWgProvider, WgKeys } from '$lib/server/types';
|
||||||
import { encodeBasicCredentials } from 'arctic/dist/request';
|
import { encodeBasicCredentials } from 'arctic/dist/request';
|
||||||
import { is } from 'typia';
|
import { is } from 'typia';
|
||||||
import type { OpnsenseWgPeers, OpnsenseWgServers } from '$lib/opnsense/wg';
|
import type { OpnsenseWgPeers, OpnsenseWgServers } from '$lib/server/wg-providers/opnsense/types';
|
||||||
import { err, ok, type Result } from '$lib/types';
|
import { err, ok, type Result } from '$lib/types';
|
||||||
import { opnsenseSanitezedUsername } from '$lib/opnsense';
|
|
||||||
import assert from 'node:assert';
|
import assert from 'node:assert';
|
||||||
import type { User } from '$lib/server/db/schema';
|
import type { User } from '$lib/server/db/schema';
|
||||||
|
|
||||||
@ -233,6 +232,10 @@ export class WgProviderOpnsense implements IWgProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function opnsenseSanitezedUsername(username: string) {
|
||||||
|
return username.slice(0, 63).replace(/[^a-zA-Z0-9_-]/g, '_');
|
||||||
|
}
|
||||||
|
|
||||||
export type OpnsenseParams = {
|
export type OpnsenseParams = {
|
||||||
opnsenseUrl: string;
|
opnsenseUrl: string;
|
||||||
opnsenseApiKey: string;
|
opnsenseApiKey: string;
|
Loading…
x
Reference in New Issue
Block a user