lib/server/devices: refactor
This commit is contained in:
parent
80acec720c
commit
e764f78501
@ -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<ReturnType<typeof findDevices>>[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;
|
56
src/lib/server/devices/find.ts
Normal file
56
src/lib/server/devices/find.ts
Normal file
@ -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<ReturnType<typeof findDevices>>[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,
|
||||
};
|
||||
}
|
3
src/lib/server/devices/index.ts
Normal file
3
src/lib/server/devices/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export { findDevices, findDevice, mapDeviceToDetails } from './find';
|
||||
export { createDevice } from './create';
|
||||
export { getIpsFromIndex } from './utils';
|
14
src/lib/server/devices/utils.ts
Normal file
14
src/lib/server/devices/utils.ts
Normal file
@ -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];
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user