mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-26 06:46:40 +00:00
Update schema to include keys
This commit is contained in:
@@ -1,20 +1,53 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { DrizzleError } from 'drizzle-orm';
|
||||
import { DrizzleError, eq } from 'drizzle-orm';
|
||||
import { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';
|
||||
import { sites, Site } from '../../db/schema';
|
||||
import db from '../../db';
|
||||
|
||||
export const getConfig = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
||||
try {
|
||||
const exitNodeId = req.query.exitNodeId as string;
|
||||
const exitNodeId = parseInt(req.query.exitNodeId as string);
|
||||
|
||||
if (!db) {
|
||||
throw new Error('Database is not attached to the request');
|
||||
}
|
||||
|
||||
const results: Site[] = db.select().from(sites).all();
|
||||
|
||||
res.json(results);
|
||||
const exitNode = await db.query.exitNodes.findFirst({
|
||||
where: {
|
||||
exitNodeId: eq(exitNodeId)
|
||||
},
|
||||
with: {
|
||||
routes: true,
|
||||
sites: {
|
||||
with: {
|
||||
resources: {
|
||||
with: {
|
||||
targets: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
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)
|
||||
)
|
||||
}))
|
||||
};
|
||||
|
||||
res.json(config);
|
||||
} catch (error) {
|
||||
console.error('Error querying database:', error);
|
||||
if (error instanceof DrizzleError) {
|
||||
@@ -23,4 +56,11 @@ export const getConfig = async (req: Request, res: Response, next: NextFunction)
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
function calculateSubnet(index: number): string {
|
||||
const baseIp = 10 << 24;
|
||||
const subnetSize = 16;
|
||||
return `${(baseIp | (index * subnetSize)).toString()}/28`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user