From e764f78501765b2555638d366d1f5cefe6a59357 Mon Sep 17 00:00:00 2001 From: Yuri Tatishchev Date: Thu, 9 Jan 2025 16:08:26 -0800 Subject: [PATCH] lib/server/devices: refactor --- .../server/{devices.ts => devices/create.ts} | 75 ++----------------- src/lib/server/devices/find.ts | 56 ++++++++++++++ src/lib/server/devices/index.ts | 3 + src/lib/server/devices/utils.ts | 14 ++++ 4 files changed, 79 insertions(+), 69 deletions(-) rename src/lib/server/{devices.ts => devices/create.ts} (69%) create mode 100644 src/lib/server/devices/find.ts create mode 100644 src/lib/server/devices/index.ts create mode 100644 src/lib/server/devices/utils.ts diff --git a/src/lib/server/devices.ts b/src/lib/server/devices/create.ts similarity index 69% rename from src/lib/server/devices.ts rename to src/lib/server/devices/create.ts index 6366dca..5dd5280 100644 --- a/src/lib/server/devices.ts +++ b/src/lib/server/devices/create.ts @@ -1,62 +1,11 @@ -import type { User } from '$lib/server/db/schema'; -import { ipAllocations, devices } from '$lib/server/db/schema'; -import { db } from '$lib/server/db'; -import { opnsenseAuth, opnsenseUrl, serverPublicKey, serverUuid } from '$lib/server/opnsense'; -import { Address4, Address6 } from 'ip-address'; -import { env } from '$env/dynamic/private'; -import { and, count, eq, isNull } from 'drizzle-orm'; +import { devices, ipAllocations, type User } from '$lib/server/db/schema'; import { err, ok, type Result } from '$lib/types'; -import type { DeviceDetails } from '$lib/devices'; +import { db } from '$lib/server/db'; +import { count, eq, isNull } from 'drizzle-orm'; +import { env } from '$env/dynamic/private'; +import { opnsenseAuth, opnsenseUrl, serverUuid } from '$lib/server/opnsense'; import { opnsenseSanitezedUsername } from '$lib/opnsense'; - -export async function findDevices(userId: string) { - return db.query.devices.findMany({ - columns: { - id: true, - name: true, - publicKey: true, - privateKey: true, - preSharedKey: true, - }, - with: { - ipAllocation: true, - }, - where: eq(devices.userId, userId), - }); -} - -export async function findDevice(userId: string, deviceId: number) { - return db.query.devices.findFirst({ - columns: { - id: true, - name: true, - publicKey: true, - privateKey: true, - preSharedKey: true, - }, - with: { - ipAllocation: true, - }, - where: and(eq(devices.userId, userId), eq(devices.id, deviceId)), - }); -} - -export function mapDeviceToDetails( - device: Awaited>[0], -): DeviceDetails { - const ips = getIpsFromIndex(device.ipAllocation.id); - return { - id: device.id, - name: device.name, - publicKey: device.publicKey, - privateKey: device.privateKey, - preSharedKey: device.preSharedKey, - ips, - vpnPublicKey: serverPublicKey, - vpnEndpoint: env.VPN_ENDPOINT, - vpnDns: env.VPN_DNS, - }; -} +import { getIpsFromIndex } from './utils'; export async function createDevice(params: { name: string; @@ -169,18 +118,6 @@ async function getKeys() { }; } -export function getIpsFromIndex(ipIndex: number) { - ipIndex -= 1; // 1-indexed in the db - const v4StartingAddr = new Address4(env.IPV4_STARTING_ADDR); - const v6StartingAddr = new Address6(env.IPV6_STARTING_ADDR); - const v4Allowed = Address4.fromBigInt(v4StartingAddr.bigInt() + BigInt(ipIndex)); - const v6Offset = BigInt(ipIndex) << (128n - BigInt(env.IPV6_CLIENT_PREFIX_SIZE)); - const v6Allowed = Address6.fromBigInt(v6StartingAddr.bigInt() + v6Offset); - const v6AllowedShort = v6Allowed.parsedAddress.join(':'); - - return [v4Allowed.address + '/32', v6AllowedShort + '/' + env.IPV6_CLIENT_PREFIX_SIZE]; -} - async function opnsenseCreateClient(params: { username: string; pubkey: string; diff --git a/src/lib/server/devices/find.ts b/src/lib/server/devices/find.ts new file mode 100644 index 0000000..f3b3810 --- /dev/null +++ b/src/lib/server/devices/find.ts @@ -0,0 +1,56 @@ +import { db } from '$lib/server/db'; +import { and, eq } from 'drizzle-orm'; +import { devices } from '$lib/server/db/schema'; +import type { DeviceDetails } from '$lib/devices'; +import { serverPublicKey } from '$lib/server/opnsense'; +import { env } from '$env/dynamic/private'; +import { getIpsFromIndex } from '$lib/server/devices/index'; + +export async function findDevices(userId: string) { + return db.query.devices.findMany({ + columns: { + id: true, + name: true, + publicKey: true, + privateKey: true, + preSharedKey: true, + }, + with: { + ipAllocation: true, + }, + where: eq(devices.userId, userId), + }); +} + +export async function findDevice(userId: string, deviceId: number) { + return db.query.devices.findFirst({ + columns: { + id: true, + name: true, + publicKey: true, + privateKey: true, + preSharedKey: true, + }, + with: { + ipAllocation: true, + }, + where: and(eq(devices.userId, userId), eq(devices.id, deviceId)), + }); +} + +export function mapDeviceToDetails( + device: Awaited>[0], +): DeviceDetails { + const ips = getIpsFromIndex(device.ipAllocation.id); + return { + id: device.id, + name: device.name, + publicKey: device.publicKey, + privateKey: device.privateKey, + preSharedKey: device.preSharedKey, + ips, + vpnPublicKey: serverPublicKey, + vpnEndpoint: env.VPN_ENDPOINT, + vpnDns: env.VPN_DNS, + }; +} diff --git a/src/lib/server/devices/index.ts b/src/lib/server/devices/index.ts new file mode 100644 index 0000000..0c9b4a6 --- /dev/null +++ b/src/lib/server/devices/index.ts @@ -0,0 +1,3 @@ +export { findDevices, findDevice, mapDeviceToDetails } from './find'; +export { createDevice } from './create'; +export { getIpsFromIndex } from './utils'; diff --git a/src/lib/server/devices/utils.ts b/src/lib/server/devices/utils.ts new file mode 100644 index 0000000..52f516c --- /dev/null +++ b/src/lib/server/devices/utils.ts @@ -0,0 +1,14 @@ +import { Address4, Address6 } from 'ip-address'; +import { env } from '$env/dynamic/private'; + +export function getIpsFromIndex(ipIndex: number) { + ipIndex -= 1; // 1-indexed in the db + const v4StartingAddr = new Address4(env.IPV4_STARTING_ADDR); + const v6StartingAddr = new Address6(env.IPV6_STARTING_ADDR); + const v4Allowed = Address4.fromBigInt(v4StartingAddr.bigInt() + BigInt(ipIndex)); + const v6Offset = BigInt(ipIndex) << (128n - BigInt(env.IPV6_CLIENT_PREFIX_SIZE)); + const v6Allowed = Address6.fromBigInt(v6StartingAddr.bigInt() + v6Offset); + const v6AllowedShort = v6Allowed.parsedAddress.join(':'); + + return [v4Allowed.address + '/32', v6AllowedShort + '/' + env.IPV6_CLIENT_PREFIX_SIZE]; +}