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 }),
|
exitNodeId: integer("exitNodeId").primaryKey({ autoIncrement: true }),
|
||||||
name: text("name").notNull(),
|
name: text("name").notNull(),
|
||||||
address: text("address").notNull(),
|
address: text("address").notNull(),
|
||||||
|
privateKey: text("privateKey"),
|
||||||
|
listenPort: integer("listenPort"),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Routes table
|
// Routes table
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
CREATE TABLE `exitNodes` (
|
CREATE TABLE `exitNodes` (
|
||||||
`exitNodeId` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
`exitNodeId` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||||
`name` text NOT NULL,
|
`name` text NOT NULL,
|
||||||
`address` text NOT NULL
|
`address` text NOT NULL,
|
||||||
|
`privateKey` text,
|
||||||
|
`listenPort` integer
|
||||||
);
|
);
|
||||||
--> statement-breakpoint
|
--> statement-breakpoint
|
||||||
CREATE TABLE `orgs` (
|
CREATE TABLE `orgs` (
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"version": "6",
|
"version": "6",
|
||||||
"dialect": "sqlite",
|
"dialect": "sqlite",
|
||||||
"id": "9b039f4c-6867-4b08-8aa9-bc184c37b910",
|
"id": "369f669c-f220-4706-9a5c-8a66ab5653b2",
|
||||||
"prevId": "00000000-0000-0000-0000-000000000000",
|
"prevId": "00000000-0000-0000-0000-000000000000",
|
||||||
"tables": {
|
"tables": {
|
||||||
"exitNodes": {
|
"exitNodes": {
|
||||||
|
@ -27,6 +27,20 @@
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": true,
|
"notNull": true,
|
||||||
"autoincrement": false
|
"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": {},
|
"indexes": {},
|
||||||
|
|
|
@ -5,8 +5,8 @@
|
||||||
{
|
{
|
||||||
"idx": 0,
|
"idx": 0,
|
||||||
"version": "6",
|
"version": "6",
|
||||||
"when": 1727544220529,
|
"when": 1727551266674,
|
||||||
"tag": "0000_short_ulik",
|
"tag": "0000_unique_killraven",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,20 +1,53 @@
|
||||||
import { Request, Response, NextFunction } from 'express';
|
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 { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';
|
||||||
import { sites, Site } from '../../db/schema';
|
import { sites, Site } from '../../db/schema';
|
||||||
import db from '../../db';
|
import db from '../../db';
|
||||||
|
|
||||||
export const getConfig = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
export const getConfig = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const exitNodeId = req.query.exitNodeId as string;
|
const exitNodeId = parseInt(req.query.exitNodeId as string);
|
||||||
|
|
||||||
if (!db) {
|
if (!db) {
|
||||||
throw new Error('Database is not attached to the request');
|
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) {
|
} catch (error) {
|
||||||
console.error('Error querying database:', error);
|
console.error('Error querying database:', error);
|
||||||
if (error instanceof DrizzleError) {
|
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