mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-10 20:02:26 +00:00
Add relay message
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { handleNewtRegisterMessage, handleReceiveBandwidthMessage } from "./newt";
|
import { handleNewtRegisterMessage, handleReceiveBandwidthMessage } from "./newt";
|
||||||
import { handleOlmRegisterMessage } from "./olm";
|
import { handleOlmRegisterMessage, handleOlmRelayMessage } from "./olm";
|
||||||
import { handleGetConfigMessage } from "./newt/handleGetConfigMessage";
|
import { handleGetConfigMessage } from "./newt/handleGetConfigMessage";
|
||||||
import { MessageHandler } from "./ws";
|
import { MessageHandler } from "./ws";
|
||||||
|
|
||||||
@@ -7,5 +7,6 @@ export const messageHandlers: Record<string, MessageHandler> = {
|
|||||||
"newt/wg/register": handleNewtRegisterMessage,
|
"newt/wg/register": handleNewtRegisterMessage,
|
||||||
"olm/wg/register": handleOlmRegisterMessage,
|
"olm/wg/register": handleOlmRegisterMessage,
|
||||||
"newt/wg/get-config": handleGetConfigMessage,
|
"newt/wg/get-config": handleGetConfigMessage,
|
||||||
"newt/receive-bandwidth": handleReceiveBandwidthMessage
|
"newt/receive-bandwidth": handleReceiveBandwidthMessage,
|
||||||
|
"olm/wg/relay": handleOlmRelayMessage
|
||||||
};
|
};
|
||||||
|
|||||||
81
server/routers/olm/handleOlmRelayMessage.ts
Normal file
81
server/routers/olm/handleOlmRelayMessage.ts
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
import db from "@server/db";
|
||||||
|
import { MessageHandler } from "../ws";
|
||||||
|
import { clients, Olm, olms, sites } from "@server/db/schema";
|
||||||
|
import { eq } from "drizzle-orm";
|
||||||
|
import { addPeer, deletePeer } from "../newt/peers";
|
||||||
|
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 || !client.siteId) {
|
||||||
|
logger.warn("Site not found or does not have exit node");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const [site] = await db
|
||||||
|
.select()
|
||||||
|
.from(sites)
|
||||||
|
.where(eq(sites.siteId, client.siteId))
|
||||||
|
.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!site.subnet) {
|
||||||
|
logger.warn("Site has no subnet");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await deletePeer(site.siteId, client.pubKey);
|
||||||
|
|
||||||
|
// add the peer to the exit node
|
||||||
|
await addPeer(site.siteId, {
|
||||||
|
publicKey: client.pubKey,
|
||||||
|
allowedIps: [client.subnet],
|
||||||
|
endpoint: ""
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
message: {
|
||||||
|
type: "olm/wg/connect",
|
||||||
|
data: {
|
||||||
|
endpoint: site.endpoint,
|
||||||
|
publicKey: site.publicKey,
|
||||||
|
serverIP: site.address!.split("/")[0],
|
||||||
|
tunnelIP: client.subnet
|
||||||
|
}
|
||||||
|
},
|
||||||
|
broadcast: false, // Send to all olms
|
||||||
|
excludeSender: false // Include sender in broadcast
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
export * from "./handleOlmRegisterMessage";
|
export * from "./handleOlmRegisterMessage";
|
||||||
export * from "./getOlmToken";
|
export * from "./getOlmToken";
|
||||||
export * from "./createOlm";
|
export * from "./createOlm";
|
||||||
|
export * from "./handleOlmRelayMessage";
|
||||||
Reference in New Issue
Block a user