This commit is contained in:
Owen
2025-11-19 20:03:57 -05:00
parent 937b36e756
commit fa5facdf33
4 changed files with 39 additions and 16 deletions

View File

@@ -338,7 +338,7 @@ export type SubnetProxyTarget = {
}[]; }[];
}; };
export function generateSingleSubnetProxyTargets( export function generateSubnetProxyTargets(
siteResource: SiteResource, siteResource: SiteResource,
clients: { clients: {
clientId: number; clientId: number;

View File

@@ -31,7 +31,7 @@ import { sendToExitNode } from "#dynamic/lib/exitNodes";
import logger from "@server/logger"; import logger from "@server/logger";
import { import {
generateRemoteSubnetsStr, generateRemoteSubnetsStr,
generateSingleSubnetProxyTargets, generateSubnetProxyTargets,
SubnetProxyTarget SubnetProxyTarget
} from "@server/lib/ip"; } from "@server/lib/ip";
import { import {
@@ -610,7 +610,7 @@ async function handleSubnetProxyTargetUpdates(
); );
if (addedClients.length > 0) { if (addedClients.length > 0) {
const targetsToAdd = generateSingleSubnetProxyTargets( const targetsToAdd = generateSubnetProxyTargets(
siteResource, siteResource,
addedClients addedClients
); );
@@ -631,7 +631,7 @@ async function handleSubnetProxyTargetUpdates(
); );
if (removedClients.length > 0) { if (removedClients.length > 0) {
const targetsToRemove = generateSingleSubnetProxyTargets( const targetsToRemove = generateSubnetProxyTargets(
siteResource, siteResource,
removedClients removedClients
); );

View File

@@ -6,10 +6,8 @@ import {
db, db,
ExitNode, ExitNode,
exitNodes, exitNodes,
resources,
siteResources, siteResources,
Target, clientSiteResourcesAssociationsCache,
targets
} from "@server/db"; } from "@server/db";
import { clients, clientSitesAssociationsCache, Newt, sites } from "@server/db"; import { clients, clientSitesAssociationsCache, Newt, sites } from "@server/db";
import { eq, and, inArray } from "drizzle-orm"; import { eq, and, inArray } from "drizzle-orm";
@@ -17,7 +15,8 @@ import { updatePeer } from "../olm/peers";
import { sendToExitNode } from "#dynamic/lib/exitNodes"; import { sendToExitNode } from "#dynamic/lib/exitNodes";
import { import {
generateRemoteSubnetsStr, generateRemoteSubnetsStr,
generateSubnetProxyTargets generateSubnetProxyTargets,
SubnetProxyTarget,
} from "@server/lib/ip"; } from "@server/lib/ip";
const inputSchema = z.object({ const inputSchema = z.object({
@@ -163,7 +162,7 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
return null; return null;
} }
let endpoint = site.endpoint; let endpoint = site.endpoint;
if (client.clientSites.isRelayed) { if (client.clientSitesAssociationsCache.isRelayed) {
if (!site.exitNodeId) { if (!site.exitNodeId) {
logger.warn( logger.warn(
`Site ${site.siteId} has no exit node, skipping` `Site ${site.siteId} has no exit node, skipping`
@@ -210,9 +209,9 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
return { return {
publicKey: client.clients.pubKey!, publicKey: client.clients.pubKey!,
allowedIps: [`${client.clients.subnet.split("/")[0]}/32`], // we want to only allow from that client allowedIps: [`${client.clients.subnet.split("/")[0]}/32`], // we want to only allow from that client
endpoint: client.clientSites.isRelayed endpoint: client.clientSitesAssociationsCache.isRelayed
? "" ? ""
: client.clientSites.endpoint! // if its relayed it should be localhost : client.clientSitesAssociationsCache.endpoint! // if its relayed it should be localhost
}; };
}) })
); );
@@ -220,13 +219,37 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
// Filter out any null values from peers that didn't have an olm // Filter out any null values from peers that didn't have an olm
const validPeers = peers.filter((peer) => peer !== null); const validPeers = peers.filter((peer) => peer !== null);
// Get all enabled targets with their resource protocol information // Get all enabled site resources for this site
const allSiteResources = await db const allSiteResources = await db
.select() .select()
.from(siteResources) .from(siteResources)
.where(eq(siteResources.siteId, siteId)); .where(eq(siteResources.siteId, siteId));
const targetsToSend = await generateSubnetProxyTargets(allSiteResources); let targetsToSend: SubnetProxyTarget[] = [];
for (const resource of allSiteResources) {
// Get clients associated with this specific resource
const resourceClients = await db
.select({
clientId: clients.clientId,
pubKey: clients.pubKey,
subnet: clients.subnet
})
.from(clients)
.innerJoin(
clientSiteResourcesAssociationsCache,
eq(clients.clientId, clientSiteResourcesAssociationsCache.clientId)
)
.where(
eq(
clientSiteResourcesAssociationsCache.siteResourceId,
resource.siteResourceId
)
);
const resourceTargets = generateSubnetProxyTargets(resource, resourceClients);
targetsToSend.push(...resourceTargets);
}
// Build the configuration response // Build the configuration response
const configResponse = { const configResponse = {

View File

@@ -18,7 +18,7 @@ import { fromError } from "zod-validation-error";
import logger from "@server/logger"; import logger from "@server/logger";
import { OpenAPITags, registry } from "@server/openApi"; import { OpenAPITags, registry } from "@server/openApi";
import { updateTargets } from "@server/routers/client/targets"; import { updateTargets } from "@server/routers/client/targets";
import { generateSingleSubnetProxyTargets } from "@server/lib/ip"; import { generateSubnetProxyTargets } from "@server/lib/ip";
import { import {
getClientSiteResourceAccess, getClientSiteResourceAccess,
rebuildClientAssociations rebuildClientAssociations
@@ -242,11 +242,11 @@ export async function updateSiteResource(
); );
} }
const oldTargets = generateSingleSubnetProxyTargets( const oldTargets = generateSubnetProxyTargets(
existingSiteResource, existingSiteResource,
mergedAllClients mergedAllClients
); );
const newTargets = generateSingleSubnetProxyTargets( const newTargets = generateSubnetProxyTargets(
updatedSiteResource, updatedSiteResource,
mergedAllClients mergedAllClients
); );