mirror of
https://github.com/fosrl/pangolin.git
synced 2026-07-17 02:56:43 +02:00
Add handlers
This commit is contained in:
@@ -0,0 +1,74 @@
|
|||||||
|
import { db, sites } from "@server/db";
|
||||||
|
import { MessageHandler } from "@server/routers/ws";
|
||||||
|
import { clients, Olm } from "@server/db";
|
||||||
|
import { and, eq } from "drizzle-orm";
|
||||||
|
import { updatePeer as newtUpdatePeer } from "../newt/peers";
|
||||||
|
import logger from "@server/logger";
|
||||||
|
|
||||||
|
export const handleOlmLocalMessage: MessageHandler = async (context) => {
|
||||||
|
const { message, client: c, sendToClient } = context;
|
||||||
|
const olm = c as Olm;
|
||||||
|
|
||||||
|
logger.info("Handling local olm message!");
|
||||||
|
|
||||||
|
if (!olm) {
|
||||||
|
logger.warn("Olm not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!olm.clientId) {
|
||||||
|
logger.warn("Olm has no client!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const clientId = olm.clientId;
|
||||||
|
|
||||||
|
const [client] = await db
|
||||||
|
.select()
|
||||||
|
.from(clients)
|
||||||
|
.where(eq(clients.clientId, clientId))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!client) {
|
||||||
|
logger.warn("Client not found");
|
||||||
|
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("Client has no endpoint or listen port");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { siteId, chainId } = message.data;
|
||||||
|
|
||||||
|
// Get the site
|
||||||
|
const [site] = await db
|
||||||
|
.select()
|
||||||
|
.from(sites)
|
||||||
|
.where(eq(sites.siteId, siteId))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!site || !site.exitNodeId) {
|
||||||
|
logger.warn("Site not found or has no exit node");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// update the peer on the newt
|
||||||
|
await newtUpdatePeer(siteId, client.pubKey, {
|
||||||
|
endpoint: "" // this removes the endpoint so the newt knows to accept local
|
||||||
|
});
|
||||||
|
|
||||||
|
// Just ack the message, we don't keep sending it
|
||||||
|
return {
|
||||||
|
message: {
|
||||||
|
type: "olm/wg/peer/local",
|
||||||
|
data: {
|
||||||
|
siteId: siteId,
|
||||||
|
chainId
|
||||||
|
}
|
||||||
|
},
|
||||||
|
broadcast: false,
|
||||||
|
excludeSender: false
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -79,9 +79,9 @@ export const handleOlmRelayMessage: MessageHandler = async (context) => {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
// update the peer on the exit node
|
// update the peer on the newt
|
||||||
await newtUpdatePeer(siteId, client.pubKey, {
|
await newtUpdatePeer(siteId, client.pubKey, {
|
||||||
endpoint: "" // this removes the endpoint so the exit node knows to relay
|
endpoint: "" // this removes the endpoint so the newt knows to relay
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -0,0 +1,95 @@
|
|||||||
|
import { db, exitNodes, sites } from "@server/db";
|
||||||
|
import { MessageHandler } from "@server/routers/ws";
|
||||||
|
import { clients, clientSitesAssociationsCache, Olm } from "@server/db";
|
||||||
|
import { and, eq } from "drizzle-orm";
|
||||||
|
import { updatePeer as newtUpdatePeer } from "../newt/peers";
|
||||||
|
import logger from "@server/logger";
|
||||||
|
|
||||||
|
export const handleOlmUnLocalMessage: MessageHandler = async (context) => {
|
||||||
|
const { message, client: c, sendToClient } = context;
|
||||||
|
const olm = c as Olm;
|
||||||
|
|
||||||
|
logger.info("Handling unlocal olm message!");
|
||||||
|
|
||||||
|
if (!olm) {
|
||||||
|
logger.warn("Olm not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!olm.clientId) {
|
||||||
|
logger.warn("Olm has no client!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const clientId = olm.clientId;
|
||||||
|
|
||||||
|
const [client] = await db
|
||||||
|
.select()
|
||||||
|
.from(clients)
|
||||||
|
.where(eq(clients.clientId, clientId))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!client) {
|
||||||
|
logger.warn("Client not found");
|
||||||
|
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("Client has no endpoint or listen port");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { siteId, chainId } = message.data;
|
||||||
|
|
||||||
|
// Get the site
|
||||||
|
const [site] = await db
|
||||||
|
.select()
|
||||||
|
.from(sites)
|
||||||
|
.where(eq(sites.siteId, siteId))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!site) {
|
||||||
|
logger.warn("Site not found or has no exit node");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const [clientSiteAssociation] = await db
|
||||||
|
.select()
|
||||||
|
.from(clientSitesAssociationsCache)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(clientSitesAssociationsCache.clientId, olm.clientId),
|
||||||
|
eq(clientSitesAssociationsCache.siteId, siteId)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!clientSiteAssociation) {
|
||||||
|
logger.warn("Client-Site association not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!clientSiteAssociation.endpoint) {
|
||||||
|
logger.warn("Client-Site association has no endpoint, cannot unrelay");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// update the peer on the newt
|
||||||
|
await newtUpdatePeer(siteId, client.pubKey, {
|
||||||
|
endpoint: clientSiteAssociation.isRelayed
|
||||||
|
? ""
|
||||||
|
: clientSiteAssociation.endpoint // this is the endpoint of the client to connect directly to the newt
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
message: {
|
||||||
|
type: "olm/wg/peer/unlocal",
|
||||||
|
data: {
|
||||||
|
siteId: siteId,
|
||||||
|
chainId
|
||||||
|
}
|
||||||
|
},
|
||||||
|
broadcast: false,
|
||||||
|
excludeSender: false
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -77,9 +77,9 @@ export const handleOlmUnRelayMessage: MessageHandler = async (context) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// update the peer on the exit node
|
// update the peer on the newt
|
||||||
await newtUpdatePeer(siteId, client.pubKey, {
|
await newtUpdatePeer(siteId, client.pubKey, {
|
||||||
endpoint: clientSiteAssociation.endpoint // this is the endpoint of the client to connect directly to the exit node
|
endpoint: clientSiteAssociation.endpoint // this is the endpoint of the client to connect directly to the newt
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -13,3 +13,5 @@ export * from "./recoverOlmWithFingerprint";
|
|||||||
export * from "./handleOlmDisconnectingMessage";
|
export * from "./handleOlmDisconnectingMessage";
|
||||||
export * from "./handleOlmServerInitAddPeerHandshake";
|
export * from "./handleOlmServerInitAddPeerHandshake";
|
||||||
export * from "./offlineChecker";
|
export * from "./offlineChecker";
|
||||||
|
export * from "./handleOlmUnLocalMessage";
|
||||||
|
export * from "./handleOlmLocalMessage";
|
||||||
|
|||||||
@@ -20,7 +20,9 @@ import {
|
|||||||
handleOlmServerPeerAddMessage,
|
handleOlmServerPeerAddMessage,
|
||||||
handleOlmUnRelayMessage,
|
handleOlmUnRelayMessage,
|
||||||
handleOlmDisconnectingMessage,
|
handleOlmDisconnectingMessage,
|
||||||
handleOlmServerInitAddPeerHandshake
|
handleOlmServerInitAddPeerHandshake,
|
||||||
|
handleOlmLocalMessage,
|
||||||
|
handleOlmUnLocalMessage
|
||||||
} from "../olm";
|
} from "../olm";
|
||||||
import { handleHealthcheckStatusMessage } from "../target";
|
import { handleHealthcheckStatusMessage } from "../target";
|
||||||
import { handleRoundTripMessage } from "./handleRoundTripMessage";
|
import { handleRoundTripMessage } from "./handleRoundTripMessage";
|
||||||
@@ -32,6 +34,8 @@ export const messageHandlers: Record<string, MessageHandler> = {
|
|||||||
"olm/wg/register": handleOlmRegisterMessage,
|
"olm/wg/register": handleOlmRegisterMessage,
|
||||||
"olm/wg/relay": handleOlmRelayMessage,
|
"olm/wg/relay": handleOlmRelayMessage,
|
||||||
"olm/wg/unrelay": handleOlmUnRelayMessage,
|
"olm/wg/unrelay": handleOlmUnRelayMessage,
|
||||||
|
"olm/wg/local": handleOlmLocalMessage,
|
||||||
|
"olm/wg/unlocal": handleOlmUnLocalMessage,
|
||||||
"olm/ping": handleOlmPingMessage,
|
"olm/ping": handleOlmPingMessage,
|
||||||
"olm/disconnecting": handleOlmDisconnectingMessage,
|
"olm/disconnecting": handleOlmDisconnectingMessage,
|
||||||
"newt/disconnecting": handleNewtDisconnectingMessage,
|
"newt/disconnecting": handleNewtDisconnectingMessage,
|
||||||
|
|||||||
Reference in New Issue
Block a user