fosrl.pangolin/server/routers/olm/handleOlmRelayMessage.ts

62 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-02-23 20:18:03 -05:00
import db from "@server/db";
import { MessageHandler } from "../ws";
2025-03-31 15:45:51 -04:00
import { clients, clientSites, Olm, olms, sites } from "@server/db/schema";
2025-02-23 20:18:03 -05:00
import { eq } from "drizzle-orm";
2025-03-31 15:45:51 -04:00
import { updatePeer } from "../newt/peers";
2025-02-23 20:18:03 -05:00
import logger from "@server/logger";
export const handleOlmRelayMessage: MessageHandler = async (context) => {
const { message, client: c, sendToClient } = context;
const olm = c as Olm;
logger.info("Handling relay olm message!");
if (!olm) {
logger.warn("Olm not found");
return;
}
if (!olm.clientId) {
logger.warn("Olm has no site!"); // TODO: Maybe we create the site here?
return;
}
const clientId = olm.clientId;
const [client] = await db
.select()
.from(clients)
.where(eq(clients.clientId, clientId))
.limit(1);
if (!client) {
logger.warn("Site not found or does not have exit node");
return;
}
// make sure we hand endpoints for both the site and the client and the lastHolePunch is not too old
if (!client.pubKey) {
logger.warn("Site or client has no endpoint or listen port");
return;
}
2025-03-31 15:45:51 -04:00
const sitesData = await db
.select()
.from(sites)
.innerJoin(clientSites, eq(sites.siteId, clientSites.siteId))
.where(eq(clientSites.clientId, client.clientId));
let jobs: Array<Promise<void>> = [];
for (const site of sitesData) {
// update the peer on the exit node
const job = updatePeer(site.sites.siteId, client.pubKey, {
endpoint: "" // this removes the endpoint
});
jobs.push(job);
2025-02-23 20:18:03 -05:00
}
2025-03-31 15:45:51 -04:00
await Promise.all(jobs);
2025-02-23 20:18:03 -05:00
2025-03-31 15:45:51 -04:00
return;
2025-02-23 20:18:03 -05:00
};