mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-04 10:05:53 +02:00
Rename gerbil -> badger
This commit is contained in:
parent
8a009f7fbc
commit
819a0ecb43
6 changed files with 9 additions and 9 deletions
|
@ -1,4 +1,6 @@
|
|||
import { Router } from "express";
|
||||
import { getConfig } from "./getConfig";
|
||||
import { receiveBandwidth } from "./receiveBandwidth";
|
||||
|
||||
const gerbil = Router();
|
||||
|
||||
|
@ -6,4 +8,7 @@ gerbil.get("/", (_, res) => {
|
|||
res.status(200).json({ message: "Healthy" });
|
||||
});
|
||||
|
||||
gerbil.get("/getConfig", getConfig);
|
||||
gerbil.post("/receiveBandwidth", receiveBandwidth);
|
||||
|
||||
export default gerbil;
|
||||
|
|
70
server/routers/gerbil/getConfig.ts
Normal file
70
server/routers/gerbil/getConfig.ts
Normal file
|
@ -0,0 +1,70 @@
|
|||
import { Request, Response, NextFunction } from 'express';
|
||||
import { DrizzleError, eq } from 'drizzle-orm';
|
||||
import { sites, resources, targets, exitNodes } from '@server/db/schema';
|
||||
import db from '@server/db';
|
||||
import logger from '@server/logger';
|
||||
|
||||
export const getConfig = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
||||
try {
|
||||
if (!req.query.exitNodeId) {
|
||||
throw new Error('Missing exitNodeId query parameter');
|
||||
}
|
||||
const exitNodeId = parseInt(req.query.exitNodeId as string);
|
||||
|
||||
// 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) {
|
||||
logger.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`;
|
||||
}
|
57
server/routers/gerbil/receiveBandwidth.ts
Normal file
57
server/routers/gerbil/receiveBandwidth.ts
Normal file
|
@ -0,0 +1,57 @@
|
|||
import { Request, Response, NextFunction } from 'express';
|
||||
import { DrizzleError, eq } from 'drizzle-orm';
|
||||
import { sites, resources, targets, exitNodes } from '@server/db/schema';
|
||||
import db from '@server/db';
|
||||
import logger from '@server/logger';
|
||||
|
||||
interface PeerBandwidth {
|
||||
publicKey: string;
|
||||
bytesIn: number;
|
||||
bytesOut: number;
|
||||
}
|
||||
|
||||
export const receiveBandwidth = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
||||
try {
|
||||
const bandwidthData: PeerBandwidth[] = req.body;
|
||||
|
||||
if (!Array.isArray(bandwidthData)) {
|
||||
throw new Error('Invalid bandwidth data');
|
||||
}
|
||||
|
||||
for (const peer of bandwidthData) {
|
||||
const { publicKey, bytesIn, bytesOut } = peer;
|
||||
|
||||
// Find the site by public key
|
||||
const site = await db.query.sites.findFirst({
|
||||
where: eq(sites.pubKey, publicKey),
|
||||
});
|
||||
|
||||
if (!site) {
|
||||
logger.warn(`Site not found for public key: ${publicKey}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Update the site's bandwidth usage
|
||||
await db.update(sites)
|
||||
.set({
|
||||
megabytesIn: (site.megabytesIn || 0) + bytesIn,
|
||||
megabytesOut: (site.megabytesOut || 0) + bytesOut,
|
||||
})
|
||||
.where(eq(sites.siteId, site.siteId));
|
||||
|
||||
logger.debug(`Updated bandwidth for site: ${site.siteId}: megabytesIn: ${(site.megabytesIn || 0) + bytesIn}, megabytesOut: ${(site.megabytesOut || 0) + bytesOut}`);
|
||||
|
||||
}
|
||||
|
||||
res.status(200).json({ message: 'Bandwidth data updated successfully' });
|
||||
} catch (error) {
|
||||
console.error('Error updating bandwidth data:', error);
|
||||
res.status(500).json({ error: 'Internal server 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