Add alias config

This commit is contained in:
Owen
2025-11-24 20:43:26 -05:00
parent d23f61d995
commit 73b0411e1c
9 changed files with 176 additions and 41 deletions

View File

@@ -1,6 +1,6 @@
import { sendToClient } from "#dynamic/routers/ws";
import { db, olms } from "@server/db";
import { SubnetProxyTarget } from "@server/lib/ip";
import { Alias, SubnetProxyTarget } from "@server/lib/ip";
import { eq } from "drizzle-orm";
export async function addTargets(newtId: string, targets: SubnetProxyTarget[]) {
@@ -33,10 +33,11 @@ export async function updateTargets(
});
}
export async function addRemoteSubnets(
export async function addPeerData(
clientId: number,
siteId: number,
remoteSubnets: string[],
aliases: Alias[],
olmId?: string
) {
if (!olmId) {
@@ -52,18 +53,20 @@ export async function addRemoteSubnets(
}
await sendToClient(olmId, {
type: `olm/wg/peer/add-remote-subnets`,
type: `olm/wg/peer/data/add`,
data: {
siteId: siteId,
remoteSubnets: remoteSubnets
remoteSubnets: remoteSubnets,
aliases: aliases
}
});
}
export async function removeRemoteSubnets(
export async function removePeerData(
clientId: number,
siteId: number,
remoteSubnets: string[],
aliases: Alias[],
olmId?: string
) {
if (!olmId) {
@@ -79,21 +82,26 @@ export async function removeRemoteSubnets(
}
await sendToClient(olmId, {
type: `olm/wg/peer/remove-remote-subnets`,
type: `olm/wg/peer/data/remove`,
data: {
siteId: siteId,
remoteSubnets: remoteSubnets
remoteSubnets: remoteSubnets,
aliases: aliases
}
});
}
export async function updateRemoteSubnets(
export async function updatePeerData(
clientId: number,
siteId: number,
remoteSubnets: {
oldRemoteSubnets: string[],
newRemoteSubnets: string[]
},
aliases: {
oldAliases: Alias[],
newAliases: Alias[]
},
olmId?: string
) {
if (!olmId) {
@@ -109,10 +117,11 @@ export async function updateRemoteSubnets(
}
await sendToClient(olmId, {
type: `olm/wg/peer/update-remote-subnets`,
type: `olm/wg/peer/data/update`,
data: {
siteId: siteId,
...remoteSubnets
...remoteSubnets,
...aliases
}
});
}

View File

@@ -275,6 +275,7 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
resource,
resourceClients
);
targetsToSend.push(...resourceTargets);
}

View File

@@ -3,6 +3,7 @@ import {
clientSiteResourcesAssociationsCache,
db,
ExitNode,
Org,
orgs,
roleClients,
roles,
@@ -25,7 +26,10 @@ import { and, eq, inArray, isNull } from "drizzle-orm";
import { addPeer, deletePeer } from "../newt/peers";
import logger from "@server/logger";
import { listExitNodes } from "#dynamic/lib/exitNodes";
import { getNextAvailableClientSubnet } from "@server/lib/ip";
import {
generateAliasConfig,
getNextAvailableClientSubnet
} from "@server/lib/ip";
import { generateRemoteSubnets } from "@server/lib/ip";
export const handleOlmRegisterMessage: MessageHandler = async (context) => {
@@ -42,18 +46,24 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
const { publicKey, relay, olmVersion, orgId, doNotCreateNewClient } =
message.data;
let client: Client;
let client: Client | undefined;
let org: Org | undefined;
if (orgId) {
try {
client = await getOrCreateOrgClient(
orgId,
olm.userId,
olm.olmId,
olm.name || "User Device",
// doNotCreateNewClient ? true : false
true // for now never create a new client automatically because we create the users clients when they are added to the org
);
const { client: clientRes, org: orgRes } =
await getOrCreateOrgClient(
orgId,
olm.userId,
olm.olmId,
olm.name || "User Device",
// doNotCreateNewClient ? true : false
true // for now never create a new client automatically because we create the users clients when they are added to the org
);
client = clientRes;
org = orgRes;
} catch (err) {
logger.error(
`Error switching olm client ${olm.olmId} to org ${orgId}: ${err}`
@@ -96,6 +106,11 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
return;
}
if (!org) {
logger.warn("Org not found");
return;
}
logger.debug(
`Olm client ID: ${client.clientId}, Public Key: ${publicKey}, Relay: ${relay}`
);
@@ -302,7 +317,12 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
publicKey: site.publicKey,
serverIP: site.address,
serverPort: site.listenPort,
remoteSubnets: generateRemoteSubnets(allSiteResources.map(({ siteResources }) => siteResources))
remoteSubnets: generateRemoteSubnets(
allSiteResources.map(({ siteResources }) => siteResources)
),
aliases: generateAliasConfig(
allSiteResources.map(({ siteResources }) => siteResources)
)
});
}
@@ -318,7 +338,8 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
type: "olm/wg/connect",
data: {
sites: siteConfigurations,
tunnelIP: client.subnet
tunnelIP: client.subnet,
utilitySubnet: org.utilitySubnet
}
},
broadcast: false,
@@ -333,7 +354,10 @@ async function getOrCreateOrgClient(
name: string,
doNotCreateNewClient: boolean,
trx: Transaction | typeof db = db
): Promise<Client> {
): Promise<{
client: Client;
org: Org;
}> {
// get the org
const [org] = await trx
.select()
@@ -441,5 +465,8 @@ async function getOrCreateOrgClient(
client = newClient;
}
return client;
return {
client: client,
org: org
};
}

View File

@@ -18,6 +18,7 @@ import logger from "@server/logger";
import { OpenAPITags, registry } from "@server/openApi";
import { getUniqueSiteResourceName } from "@server/db/names";
import { rebuildClientAssociations } from "@server/lib/rebuildClientAssociations";
import { getNextAvailableAliasAddress } from "@server/lib/ip";
const createSiteResourceParamsSchema = z.strictObject({
siteId: z.string().transform(Number).pipe(z.int().positive()),
@@ -193,6 +194,10 @@ export async function createSiteResource(
// }
const niceId = await getUniqueSiteResourceName(orgId);
let aliasAddress: string | null = null;
if (mode == "host") { // we can only have an alias on a host
aliasAddress = await getNextAvailableAliasAddress(orgId);
}
let newSiteResource: SiteResource | undefined;
await db.transaction(async (trx) => {
@@ -210,7 +215,8 @@ export async function createSiteResource(
// destinationPort: mode === "port" ? destinationPort : null,
destination,
enabled,
alias: alias || null
alias,
aliasAddress
})
.returning();

View File

@@ -17,11 +17,9 @@ import { eq, and, ne } from "drizzle-orm";
import { fromError } from "zod-validation-error";
import logger from "@server/logger";
import { OpenAPITags, registry } from "@server/openApi";
import { updatePeerData, updateTargets } from "@server/routers/client/targets";
import {
updateRemoteSubnets,
updateTargets
} from "@server/routers/client/targets";
import {
generateAliasConfig,
generateRemoteSubnets,
generateSubnetProxyTargets
} from "@server/lib/ip";
@@ -266,7 +264,7 @@ export async function updateSiteResource(
for (const client of mergedAllClients) {
// we also need to update the remote subnets on the olms for each client that has access to this site
olmJobs.push(
updateRemoteSubnets(
updatePeerData(
client.clientId,
updatedSiteResource.siteId,
{
@@ -276,6 +274,14 @@ export async function updateSiteResource(
newRemoteSubnets: generateRemoteSubnets([
updatedSiteResource
])
},
{
oldAliases: generateAliasConfig([
existingSiteResource
]),
newAliases: generateAliasConfig([
updatedSiteResource
])
}
)
);