mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-19 08:48:34 +02:00
Update schema to include keys
This commit is contained in:
parent
77d71de990
commit
7bb81af3bb
5 changed files with 98 additions and 40 deletions
|
@ -41,6 +41,8 @@ export const exitNodes = sqliteTable("exitNodes", {
|
|||
exitNodeId: integer("exitNodeId").primaryKey({ autoIncrement: true }),
|
||||
name: text("name").notNull(),
|
||||
address: text("address").notNull(),
|
||||
privateKey: text("privateKey"),
|
||||
listenPort: integer("listenPort"),
|
||||
});
|
||||
|
||||
// Routes table
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
CREATE TABLE `exitNodes` (
|
||||
`exitNodeId` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`name` text NOT NULL,
|
||||
`address` text NOT NULL
|
||||
`address` text NOT NULL,
|
||||
`privateKey` text,
|
||||
`listenPort` integer
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE `orgs` (
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"version": "6",
|
||||
"dialect": "sqlite",
|
||||
"id": "9b039f4c-6867-4b08-8aa9-bc184c37b910",
|
||||
"id": "369f669c-f220-4706-9a5c-8a66ab5653b2",
|
||||
"prevId": "00000000-0000-0000-0000-000000000000",
|
||||
"tables": {
|
||||
"exitNodes": {
|
||||
|
@ -27,6 +27,20 @@
|
|||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"privateKey": {
|
||||
"name": "privateKey",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"listenPort": {
|
||||
"name": "listenPort",
|
||||
"type": "integer",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
}
|
||||
},
|
||||
"indexes": {},
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
{
|
||||
"idx": 0,
|
||||
"version": "6",
|
||||
"when": 1727544220529,
|
||||
"tag": "0000_short_ulik",
|
||||
"when": 1727551266674,
|
||||
"tag": "0000_unique_killraven",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
|
|
|
@ -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) {
|
||||
|
@ -24,3 +57,10 @@ export const getConfig = async (req: Request, res: Response, next: NextFunction)
|
|||
}
|
||||
}
|
||||
};
|
||||
|
||||
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