2024-09-28 12:35:07 -04:00
|
|
|
import { Request, Response, NextFunction } from 'express';
|
2024-09-28 15:21:13 -04:00
|
|
|
import { DrizzleError, eq } from 'drizzle-orm';
|
2024-09-28 17:10:03 -04:00
|
|
|
import { sites, resources, targets, exitNodes } from '../../db/schema';
|
2024-09-28 14:46:36 -04:00
|
|
|
import db from '../../db';
|
2024-09-28 12:35:07 -04:00
|
|
|
|
2024-09-28 12:42:38 -04:00
|
|
|
export const getConfig = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
2024-09-28 17:10:03 -04:00
|
|
|
try {
|
|
|
|
if (!req.query.exitNodeId) {
|
|
|
|
throw new Error('Missing exitNodeId query parameter');
|
|
|
|
}
|
|
|
|
const exitNodeId = parseInt(req.query.exitNodeId as string);
|
2024-09-28 12:35:07 -04:00
|
|
|
|
2024-09-28 17:10:03 -04:00
|
|
|
// Fetch exit node
|
|
|
|
const exitNode = await db.query.exitNodes.findFirst({
|
|
|
|
where: eq(exitNodes.exitNodeId, exitNodeId),
|
|
|
|
});
|
2024-09-28 12:35:07 -04:00
|
|
|
|
2024-09-28 17:10:03 -04:00
|
|
|
if (!exitNode) {
|
|
|
|
throw new Error('Exit node not found');
|
2024-09-28 15:21:13 -04:00
|
|
|
}
|
|
|
|
|
2024-09-28 17:10:03 -04:00
|
|
|
// 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) {
|
|
|
|
console.error('Error querying database:', error);
|
|
|
|
if (error instanceof DrizzleError) {
|
|
|
|
res.status(500).json({ error: 'Database query error', message: error.message });
|
|
|
|
} else {
|
|
|
|
next(error);
|
|
|
|
}
|
2024-09-28 12:42:38 -04:00
|
|
|
}
|
2024-09-28 15:21:13 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
function calculateSubnet(index: number): string {
|
|
|
|
const baseIp = 10 << 24;
|
|
|
|
const subnetSize = 16;
|
|
|
|
return `${(baseIp | (index * subnetSize)).toString()}/28`;
|
2024-09-28 17:10:03 -04:00
|
|
|
}
|