Able to connect multi site on olm - POC 1

This commit is contained in:
Owen 2025-04-01 12:49:02 -04:00
parent 96d6ad8142
commit 5ff4215bde
No known key found for this signature in database
GPG key ID: 8271FDFFD9E0CCBD
5 changed files with 39 additions and 30 deletions

View file

@ -218,6 +218,11 @@ export function isIpInCidr(ip: string, cidr: string): boolean {
}
export async function getNextAvailableClientSubnet(orgId: string): Promise<string> {
const [org] = await db
.select()
.from(orgs)
.where(eq(orgs.orgId, orgId));
const existingAddressesSites = await db
.select({
address: sites.address
@ -233,14 +238,14 @@ export async function getNextAvailableClientSubnet(orgId: string): Promise<strin
.where(and(isNotNull(clients.subnet), eq(clients.orgId, orgId)));
const addresses = [
...existingAddressesSites.map((site) => site.address),
...existingAddressesClients.map((client) => client.address)
...existingAddressesSites.map((site) => `${site.address?.split("/")[0]}/32`), // we are overriding the 32 so that we pick individual addresses in the subnet of the org for the site and the client even though they are stored with the /block_size of the org
...existingAddressesClients.map((client) => `${client.address.split("/")}/32`)
].filter((address) => address !== null) as string[];
let subnet = findNextAvailableCidr(
addresses,
32,
config.getRawConfig().orgs.subnet_group
org.subnet
); // pick the sites address in the org
if (!subnet) {
throw new Error("No available subnets remaining in space");

View file

@ -51,32 +51,32 @@ export async function getAllRelays(
}
}
// get the clients on each site and map them to the site
const sitesAndClients = await Promise.all(sitesRes.map(async (site) => {
const clientsRes = await db.select().from(clients).where(eq(clients.siteId, site.siteId));
return {
site,
clients: clientsRes
};
}));
// // get the clients on each site and map them to the site
// const sitesAndClients = await Promise.all(sitesRes.map(async (site) => {
// const clientsRes = await db.select().from(clients).where(eq(clients.siteId, site.siteId));
// return {
// site,
// clients: clientsRes
// };
// }));
let mappings: { [key: string]: {
destinationIp: string;
destinationPort: number;
} } = {};
for (const siteAndClients of sitesAndClients) {
const { site, clients } = siteAndClients;
for (const client of clients) {
if (!client.endpoint || !site.endpoint || !site.subnet) {
continue;
}
mappings[client.endpoint] = {
destinationIp: site.subnet.split("/")[0],
destinationPort: parseInt(site.endpoint.split(":")[1])
};
}
}
// for (const siteAndClients of sitesAndClients) {
// const { site, clients } = siteAndClients;
// for (const client of clients) {
// if (!client.endpoint || !site.endpoint || !site.subnet) {
// continue;
// }
// mappings[client.endpoint] = {
// destinationIp: site.subnet.split("/")[0],
// destinationPort: parseInt(site.endpoint.split(":")[1])
// };
// }
// }
return res.status(HttpCode.OK).send({ mappings });
} catch (error) {

View file

@ -80,10 +80,10 @@ export async function updateHolePunch(
.where(eq(clients.clientId, olm.clientId))
.returning();
[site] = await db
.select()
.from(sites)
.where(eq(sites.siteId, client.siteId));
// [site] = await db
// .select()
// .from(sites)
// .where(eq(sites.siteId, client.siteId));
} else if (newtId) {
const { session, newt: newtSession } =

View file

@ -6,6 +6,7 @@ import db from "@server/db";
import { clients, clientSites, Newt, Site, sites } from "@server/db/schema";
import { eq } from "drizzle-orm";
import { getNextAvailableClientSubnet } from "@server/lib/ip";
import config from "@server/lib/config";
const inputSchema = z.object({
publicKey: z.string(),
@ -58,7 +59,12 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
let site: Site | undefined;
if (!siteRes.address) {
let address = await getNextAvailableClientSubnet(siteRes.orgId);
address = address.split("/")[0]; // get the first part of the CIDR
if (!address) {
logger.error("handleGetConfigMessage: No available address");
return;
}
address = `${address.split("/")[0]}/${config.getRawConfig().orgs.block_size}` // we want the block size of the whole org
// create a new exit node
const [updateRes] = await db

View file

@ -36,8 +36,6 @@ export default async function ClientsPage(props: ClientsPageProps) {
const clientRows: ClientRow[] = clients.map((client) => {
return {
name: client.name,
siteName: client.siteName,
siteId: client.siteNiceId,
id: client.clientId,
mbIn: formatSize(client.megabytesIn || 0),
mbOut: formatSize(client.megabytesOut || 0),