Handle types in handlers
This commit is contained in:
parent
346f2db5fb
commit
bec303821b
3 changed files with 28 additions and 15 deletions
|
@ -3,7 +3,7 @@ import { MessageHandler } from "../ws";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
import db from "@server/db";
|
import db from "@server/db";
|
||||||
import { clients, Site, sites } from "@server/db/schema";
|
import { clients, Newt, Site, sites } from "@server/db/schema";
|
||||||
import { eq, isNotNull } from "drizzle-orm";
|
import { eq, isNotNull } from "drizzle-orm";
|
||||||
import { findNextAvailableCidr } from "@server/lib/ip";
|
import { findNextAvailableCidr } from "@server/lib/ip";
|
||||||
import config from "@server/lib/config";
|
import config from "@server/lib/config";
|
||||||
|
@ -17,7 +17,8 @@ const inputSchema = z.object({
|
||||||
type Input = z.infer<typeof inputSchema>;
|
type Input = z.infer<typeof inputSchema>;
|
||||||
|
|
||||||
export const handleGetConfigMessage: MessageHandler = async (context) => {
|
export const handleGetConfigMessage: MessageHandler = async (context) => {
|
||||||
const { message, newt, sendToClient } = context;
|
const { message, client, sendToClient } = context;
|
||||||
|
const newt = client as Newt;
|
||||||
|
|
||||||
logger.debug("Handling Newt get config message!");
|
logger.debug("Handling Newt get config message!");
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ import db from "@server/db";
|
||||||
import { MessageHandler } from "../ws";
|
import { MessageHandler } from "../ws";
|
||||||
import {
|
import {
|
||||||
exitNodes,
|
exitNodes,
|
||||||
|
Newt,
|
||||||
resources,
|
resources,
|
||||||
sites,
|
sites,
|
||||||
Target,
|
Target,
|
||||||
|
@ -13,8 +14,7 @@ import logger from "@server/logger";
|
||||||
|
|
||||||
export const handleNewtRegisterMessage: MessageHandler = async (context) => {
|
export const handleNewtRegisterMessage: MessageHandler = async (context) => {
|
||||||
const { message, client, sendToClient } = context;
|
const { message, client, sendToClient } = context;
|
||||||
|
const newt = client as Newt;
|
||||||
const newt = client;
|
|
||||||
|
|
||||||
logger.info("Handling register newt message!");
|
logger.info("Handling register newt message!");
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import db from "@server/db";
|
import db from "@server/db";
|
||||||
import { MessageHandler } from "../ws";
|
import { MessageHandler } from "../ws";
|
||||||
import {
|
import {
|
||||||
|
clients,
|
||||||
|
Olm,
|
||||||
olms,
|
olms,
|
||||||
sites,
|
sites,
|
||||||
} from "@server/db/schema";
|
} from "@server/db/schema";
|
||||||
|
@ -9,9 +11,8 @@ import { addPeer, deletePeer } from "../newt/peers";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
|
|
||||||
export const handleOlmRegisterMessage: MessageHandler = async (context) => {
|
export const handleOlmRegisterMessage: MessageHandler = async (context) => {
|
||||||
const { message, client, sendToClient } = context;
|
const { message, client: c, sendToClient } = context;
|
||||||
|
const olm = c as Olm;
|
||||||
const olm = client;
|
|
||||||
|
|
||||||
logger.info("Handling register olm message!");
|
logger.info("Handling register olm message!");
|
||||||
|
|
||||||
|
@ -20,12 +21,12 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!olm.siteId) {
|
if (!olm.clientId) {
|
||||||
logger.warn("Olm has no site!"); // TODO: Maybe we create the site here?
|
logger.warn("Olm has no site!"); // TODO: Maybe we create the site here?
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const siteId = olm.siteId;
|
const clientId = olm.clientId;
|
||||||
|
|
||||||
const { publicKey } = message.data;
|
const { publicKey } = message.data;
|
||||||
if (!publicKey) {
|
if (!publicKey) {
|
||||||
|
@ -33,28 +34,39 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const [client] = await db
|
||||||
|
.select()
|
||||||
|
.from(clients)
|
||||||
|
.where(eq(clients.clientId, clientId))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!client || !client.siteId) {
|
||||||
|
logger.warn("Site not found or does not have exit node");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const [site] = await db
|
const [site] = await db
|
||||||
.select()
|
.select()
|
||||||
.from(sites)
|
.from(sites)
|
||||||
.where(eq(sites.siteId, siteId))
|
.where(eq(sites.siteId, client.siteId))
|
||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
if (!site) {
|
if (!client) {
|
||||||
logger.warn("Site not found or does not have exit node");
|
logger.warn("Site not found or does not have exit node");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await db
|
await db
|
||||||
.update(olms)
|
.update(clients)
|
||||||
.set({
|
.set({
|
||||||
pubKey: publicKey
|
pubKey: publicKey
|
||||||
})
|
})
|
||||||
.where(eq(olms.olmId, olm.olmId))
|
.where(eq(clients.clientId, olm.clientId))
|
||||||
.returning();
|
.returning();
|
||||||
|
|
||||||
if (olm.pubKey && olm.pubKey !== publicKey) {
|
if (client.pubKey && client.pubKey !== publicKey) {
|
||||||
logger.info("Public key mismatch. Deleting old peer...");
|
logger.info("Public key mismatch. Deleting old peer...");
|
||||||
await deletePeer(site.siteId, site.pubKey);
|
await deletePeer(site.siteId, client.pubKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!site.subnet) {
|
if (!site.subnet) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue