newts not being on when olm is started

This commit is contained in:
Owen
2025-04-13 21:28:11 -04:00
parent b7ae712b63
commit 569635f3ed
7 changed files with 150 additions and 35 deletions

View File

@@ -3,7 +3,6 @@ import { MessageHandler } from "../ws";
import { clients, Olm } from "@server/db/schema";
import { eq, lt, isNull } from "drizzle-orm";
import logger from "@server/logger";
import { time } from "console";
// Track if the offline checker interval is running
let offlineCheckerInterval: NodeJS.Timeout | null = null;

View File

@@ -68,13 +68,26 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
return;
}
// Update the client's public key
await db
.update(clients)
.set({
pubKey: publicKey
})
.where(eq(clients.clientId, olm.clientId));
if (client.pubKey !== publicKey) {
logger.info(
"Public key mismatch. Updating public key and clearing session info..."
);
// Update the client's public key
await db
.update(clients)
.set({
pubKey: publicKey
})
.where(eq(clients.clientId, olm.clientId));
// set isRelay to false for all of the client's sites to reset the connection metadata
await db
.update(clientSites)
.set({
isRelayed: false
})
.where(eq(clientSites.clientId, olm.clientId));
}
// Get all sites data
const sitesData = await db
@@ -143,7 +156,7 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
endpoint: site.endpoint,
publicKey: site.publicKey,
serverIP: site.address,
serverPort: site.listenPort,
serverPort: site.listenPort
});
}

View File

@@ -0,0 +1,70 @@
import db from '@server/db';
import { clients, olms, newts } from '@server/db/schema';
import { eq } from 'drizzle-orm';
import { sendToClient } from '../ws';
import logger from '@server/logger';
export async function addPeer(clientId: number, peer: {
siteId: number,
publicKey: string;
allowedIps: string[];
endpoint: string;
serverIP: string | null;
serverPort: number | null;
}) {
const [olm] = await db.select().from(olms).where(eq(olms.clientId, clientId)).limit(1);
if (!olm) {
throw new Error(`Olm with ID ${clientId} not found`);
}
sendToClient(olm.olmId, {
type: 'olm/wg/peer/add',
data: {
publicKey: peer.publicKey,
allowedIps: peer.allowedIps,
endpoint: peer.endpoint,
serverIP: peer.serverIP,
serverPort: peer.serverPort
}
});
logger.info(`Added peer ${peer.publicKey} to olm ${olm.olmId}`);
}
export async function deletePeer(clientId: number, publicKey: string) {
const [olm] = await db.select().from(olms).where(eq(olms.clientId, clientId)).limit(1);
if (!olm) {
throw new Error(`Olm with ID ${clientId} not found`);
}
sendToClient(olm.olmId, {
type: 'olm/wg/peer/remove',
data: {
publicKey
}
});
logger.info(`Deleted peer ${publicKey} from olm ${olm.olmId}`);
}
export async function updatePeer(clientId: number, publicKey: string, peer: {
allowedIps?: string[];
endpoint?: string;
serverIP?: string;
serverPort?: number;
}) {
const [olm] = await db.select().from(olms).where(eq(olms.clientId, clientId)).limit(1);
if (!olm) {
throw new Error(`Olm with ID ${clientId} not found`);
}
sendToClient(olm.olmId, {
type: 'olm/wg/peer/update',
data: {
publicKey,
...peer
}
});
logger.info(`Updated peer ${publicKey} on olm ${olm.olmId}`);
}