mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-04 10:05:53 +02:00
Add receive endpoint for megabytes transfered
This commit is contained in:
parent
0d91966609
commit
5a3e1444d1
7 changed files with 82 additions and 6 deletions
|
@ -1,5 +1,6 @@
|
|||
import { Router } from "express";
|
||||
import { getConfig } from "./getConfig";
|
||||
import { receiveBandwidth } from "./receiveBandwidth";
|
||||
|
||||
const badger = Router();
|
||||
|
||||
|
@ -8,5 +9,6 @@ badger.get("/", (_, res) => {
|
|||
});
|
||||
|
||||
badger.get("/getConfig", getConfig);
|
||||
badger.post("/receiveBandwidth", receiveBandwidth);
|
||||
|
||||
export default badger;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { Request, Response, NextFunction } from 'express';
|
||||
import { DrizzleError, eq } from 'drizzle-orm';
|
||||
import { sites, resources, targets, exitNodes } from '../../db/schema';
|
||||
import db from '../../db';
|
||||
import { sites, resources, targets, exitNodes } from '@server/db/schema';
|
||||
import db from '@server/db';
|
||||
|
||||
export const getConfig = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
||||
try {
|
||||
|
@ -51,7 +51,6 @@ export const getConfig = async (req: Request, res: Response, next: NextFunction)
|
|||
peers,
|
||||
};
|
||||
|
||||
|
||||
res.json(config);
|
||||
} catch (error) {
|
||||
console.error('Error querying database:', error);
|
||||
|
|
57
server/routers/badger/receiveBandwidth.ts
Normal file
57
server/routers/badger/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) {
|
||||
console.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