mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-06 02:36:38 +00:00
Update gerbil with new sites and targets
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { z } from 'zod';
|
||||
import { db } from '@server/db';
|
||||
import { roles, userSites, sites, roleSites } from '@server/db/schema';
|
||||
import { roles, userSites, sites, roleSites, exitNodes } from '@server/db/schema';
|
||||
import response from "@server/utils/response";
|
||||
import HttpCode from '@server/types/HttpCode';
|
||||
import createHttpError from 'http-errors';
|
||||
import fetch from 'node-fetch';
|
||||
import { ActionsEnum, checkUserActionPermission } from '@server/auth/actions';
|
||||
import logger from '@server/logger';
|
||||
import { eq, and } from 'drizzle-orm';
|
||||
import { getUniqueSiteName } from '@server/db/names';
|
||||
import { addPeer } from '../gerbil/peers';
|
||||
|
||||
const API_BASE_URL = "http://localhost:3000";
|
||||
|
||||
@@ -113,6 +113,12 @@ export async function createSite(req: Request, res: Response, next: NextFunction
|
||||
});
|
||||
}
|
||||
|
||||
// Add the peer to the exit node
|
||||
await addPeer(exitNodeId, {
|
||||
publicKey: pubKey,
|
||||
allowedIps: [],
|
||||
});
|
||||
|
||||
return response(res, {
|
||||
data: {
|
||||
name: newSite.name,
|
||||
@@ -128,31 +134,7 @@ export async function createSite(req: Request, res: Response, next: NextFunction
|
||||
status: HttpCode.CREATED,
|
||||
});
|
||||
} catch (error) {
|
||||
throw error;
|
||||
logger.error(error);
|
||||
return next(createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred..."));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function addPeer(peer: string) {
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/peer`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(peer),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const data: any = await response.json();
|
||||
logger.info('Peer added successfully:', data.status);
|
||||
return data;
|
||||
} catch (error: any) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import HttpCode from '@server/types/HttpCode';
|
||||
import createHttpError from 'http-errors';
|
||||
import { ActionsEnum, checkUserActionPermission } from '@server/auth/actions';
|
||||
import logger from '@server/logger';
|
||||
import { deletePeer } from '../gerbil/peers';
|
||||
|
||||
const API_BASE_URL = "http://localhost:3000";
|
||||
|
||||
@@ -38,11 +39,11 @@ export async function deleteSite(req: Request, res: Response, next: NextFunction
|
||||
}
|
||||
|
||||
// Delete the site from the database
|
||||
const deletedSite = await db.delete(sites)
|
||||
const [deletedSite] = await db.delete(sites)
|
||||
.where(eq(sites.siteId, siteId))
|
||||
.returning();
|
||||
|
||||
if (deletedSite.length === 0) {
|
||||
if (!deletedSite) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.NOT_FOUND,
|
||||
@@ -51,6 +52,8 @@ export async function deleteSite(req: Request, res: Response, next: NextFunction
|
||||
);
|
||||
}
|
||||
|
||||
await deletePeer(deletedSite.exitNodeId!, deletedSite.pubKey);
|
||||
|
||||
return response(res, {
|
||||
data: null,
|
||||
success: true,
|
||||
|
||||
Reference in New Issue
Block a user