mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-04 10:05:53 +02:00
Adjust schema
This commit is contained in:
parent
7bb81af3bb
commit
2970088b29
6 changed files with 123 additions and 104 deletions
|
@ -1,66 +1,71 @@
|
|||
import { Request, Response, NextFunction } from 'express';
|
||||
import { DrizzleError, eq } from 'drizzle-orm';
|
||||
import { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';
|
||||
import { sites, Site } from '../../db/schema';
|
||||
import { sites, resources, targets, exitNodes } from '../../db/schema';
|
||||
import db from '../../db';
|
||||
|
||||
export const getConfig = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
||||
try {
|
||||
const exitNodeId = parseInt(req.query.exitNodeId as string);
|
||||
|
||||
if (!db) {
|
||||
throw new Error('Database is not attached to the request');
|
||||
}
|
||||
|
||||
|
||||
const exitNode = await db.query.exitNodes.findFirst({
|
||||
where: {
|
||||
exitNodeId: eq(exitNodeId)
|
||||
},
|
||||
with: {
|
||||
routes: true,
|
||||
sites: {
|
||||
with: {
|
||||
resources: {
|
||||
with: {
|
||||
targets: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
if (!req.query.exitNodeId) {
|
||||
throw new Error('Missing exitNodeId query parameter');
|
||||
}
|
||||
});
|
||||
|
||||
if (!exitNode) {
|
||||
throw new Error('Exit node not found');
|
||||
}
|
||||
|
||||
const config = {
|
||||
privateKey,
|
||||
listenPort,
|
||||
ipAddress: exitNode.address,
|
||||
peers: exitNode.sites.map((site, index) => ({
|
||||
publicKey: site.pubKey,
|
||||
allowedIps: site.resources.flatMap(resource =>
|
||||
resource.targets.map(target => target.ip)
|
||||
)
|
||||
}))
|
||||
};
|
||||
const exitNodeId = parseInt(req.query.exitNodeId as string);
|
||||
|
||||
res.json(config);
|
||||
} catch (error) {
|
||||
console.error('Error querying database:', error);
|
||||
if (error instanceof DrizzleError) {
|
||||
res.status(500).json({ error: 'Database query error', message: error.message });
|
||||
} else {
|
||||
next(error);
|
||||
// Fetch exit node
|
||||
const exitNode = await db.query.exitNodes.findFirst({
|
||||
where: eq(exitNodes.exitNodeId, exitNodeId),
|
||||
});
|
||||
|
||||
if (!exitNode) {
|
||||
throw new Error('Exit node not found');
|
||||
}
|
||||
|
||||
// Fetch sites for this exit node
|
||||
const sitesRes = await db.query.sites.findMany({
|
||||
where: eq(sites.exitNode, exitNodeId),
|
||||
});
|
||||
|
||||
const peers = await Promise.all(sitesRes.map(async (site) => {
|
||||
// Fetch resources for this site
|
||||
const resourcesRes = await db.query.resources.findMany({
|
||||
where: eq(resources.siteId, site.siteId),
|
||||
});
|
||||
|
||||
// Fetch targets for all resources of this site
|
||||
const targetIps = await Promise.all(resourcesRes.map(async (resource) => {
|
||||
const targetsRes = await db.query.targets.findMany({
|
||||
where: eq(targets.resourceId, resource.resourceId),
|
||||
});
|
||||
return targetsRes.map(target => `${target.ip}/32`);
|
||||
}));
|
||||
|
||||
return {
|
||||
publicKey: site.pubKey,
|
||||
allowedIps: targetIps.flat(),
|
||||
};
|
||||
}));
|
||||
|
||||
const config = {
|
||||
privateKey: exitNode.privateKey,
|
||||
listenPort: exitNode.listenPort,
|
||||
ipAddress: exitNode.address,
|
||||
peers,
|
||||
};
|
||||
|
||||
|
||||
res.json(config);
|
||||
} catch (error) {
|
||||
console.error('Error querying database:', error);
|
||||
if (error instanceof DrizzleError) {
|
||||
res.status(500).json({ error: 'Database query error', message: error.message });
|
||||
} else {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function calculateSubnet(index: number): string {
|
||||
const baseIp = 10 << 24;
|
||||
const subnetSize = 16;
|
||||
return `${(baseIp | (index * subnetSize)).toString()}/28`;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue