mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-21 20:36:37 +00:00
fix issues from test deploy
This commit is contained in:
@@ -12,9 +12,14 @@ import { isIpInCidr } from "@server/utils/ip";
|
||||
import { fromError } from "zod-validation-error";
|
||||
import { addTargets } from "../newt/targets";
|
||||
|
||||
const createTargetParamsSchema = z.object({
|
||||
resourceId: z.string().transform(Number).pipe(z.number().int().positive()),
|
||||
});
|
||||
const createTargetParamsSchema = z
|
||||
.object({
|
||||
resourceId: z
|
||||
.string()
|
||||
.transform(Number)
|
||||
.pipe(z.number().int().positive())
|
||||
})
|
||||
.strict();
|
||||
|
||||
const createTargetSchema = z
|
||||
.object({
|
||||
@@ -22,7 +27,7 @@ const createTargetSchema = z
|
||||
method: z.string().min(1).max(10),
|
||||
port: z.number().int().min(1).max(65535),
|
||||
protocol: z.string().optional(),
|
||||
enabled: z.boolean().default(true),
|
||||
enabled: z.boolean().default(true)
|
||||
})
|
||||
.strict();
|
||||
|
||||
@@ -61,7 +66,7 @@ export async function createTarget(
|
||||
// get the resource
|
||||
const [resource] = await db
|
||||
.select({
|
||||
siteId: resources.siteId,
|
||||
siteId: resources.siteId
|
||||
})
|
||||
.from(resources)
|
||||
.where(eq(resources.resourceId, resourceId));
|
||||
@@ -91,7 +96,10 @@ export async function createTarget(
|
||||
}
|
||||
|
||||
// make sure the target is within the site subnet
|
||||
if (site.type == "wireguard" && !isIpInCidr(targetData.ip, site.subnet!)) {
|
||||
if (
|
||||
site.type == "wireguard" &&
|
||||
!isIpInCidr(targetData.ip, site.subnet!)
|
||||
) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
@@ -102,7 +110,7 @@ export async function createTarget(
|
||||
|
||||
// Fetch resources for this site
|
||||
const resourcesRes = await db.query.resources.findMany({
|
||||
where: eq(resources.siteId, site.siteId),
|
||||
where: eq(resources.siteId, site.siteId)
|
||||
});
|
||||
|
||||
// TODO: is this all inefficient?
|
||||
@@ -112,7 +120,7 @@ export async function createTarget(
|
||||
await Promise.all(
|
||||
resourcesRes.map(async (resource) => {
|
||||
const targetsRes = await db.query.targets.findMany({
|
||||
where: eq(targets.resourceId, resource.resourceId),
|
||||
where: eq(targets.resourceId, resource.resourceId)
|
||||
});
|
||||
targetsRes.forEach((target) => {
|
||||
targetIps.push(`${target.ip}/32`);
|
||||
@@ -147,7 +155,7 @@ export async function createTarget(
|
||||
resourceId,
|
||||
protocol: "tcp", // hard code for now
|
||||
internalPort,
|
||||
...targetData,
|
||||
...targetData
|
||||
})
|
||||
.returning();
|
||||
|
||||
@@ -155,7 +163,7 @@ export async function createTarget(
|
||||
if (site.type == "wireguard") {
|
||||
await addPeer(site.exitNodeId!, {
|
||||
publicKey: site.pubKey,
|
||||
allowedIps: targetIps.flat(),
|
||||
allowedIps: targetIps.flat()
|
||||
});
|
||||
} else if (site.type == "newt") {
|
||||
// get the newt on the site by querying the newt table for siteId
|
||||
@@ -174,7 +182,7 @@ export async function createTarget(
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Target created successfully",
|
||||
status: HttpCode.CREATED,
|
||||
status: HttpCode.CREATED
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
|
||||
@@ -11,9 +11,11 @@ import { addPeer } from "../gerbil/peers";
|
||||
import { fromError } from "zod-validation-error";
|
||||
import { removeTargets } from "../newt/targets";
|
||||
|
||||
const deleteTargetSchema = z.object({
|
||||
targetId: z.string().transform(Number).pipe(z.number().int().positive()),
|
||||
});
|
||||
const deleteTargetSchema = z
|
||||
.object({
|
||||
targetId: z.string().transform(Number).pipe(z.number().int().positive())
|
||||
})
|
||||
.strict();
|
||||
|
||||
export async function deleteTarget(
|
||||
req: Request,
|
||||
@@ -49,7 +51,7 @@ export async function deleteTarget(
|
||||
// get the resource
|
||||
const [resource] = await db
|
||||
.select({
|
||||
siteId: resources.siteId,
|
||||
siteId: resources.siteId
|
||||
})
|
||||
.from(resources)
|
||||
.where(eq(resources.resourceId, deletedTarget.resourceId!));
|
||||
@@ -83,14 +85,14 @@ export async function deleteTarget(
|
||||
// TODO: is this all inefficient?
|
||||
// Fetch resources for this site
|
||||
const resourcesRes = await db.query.resources.findMany({
|
||||
where: eq(resources.siteId, site.siteId),
|
||||
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),
|
||||
where: eq(targets.resourceId, resource.resourceId)
|
||||
});
|
||||
return targetsRes.map((target) => `${target.ip}/32`);
|
||||
})
|
||||
@@ -98,7 +100,7 @@ export async function deleteTarget(
|
||||
|
||||
await addPeer(site.exitNodeId!, {
|
||||
publicKey: site.pubKey,
|
||||
allowedIps: targetIps.flat(),
|
||||
allowedIps: targetIps.flat()
|
||||
});
|
||||
} else if (site.type == "newt") {
|
||||
// get the newt on the site by querying the newt table for siteId
|
||||
@@ -117,7 +119,7 @@ export async function deleteTarget(
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Target deleted successfully",
|
||||
status: HttpCode.OK,
|
||||
status: HttpCode.OK
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
|
||||
@@ -9,9 +9,11 @@ import createHttpError from "http-errors";
|
||||
import logger from "@server/logger";
|
||||
import { fromError } from "zod-validation-error";
|
||||
|
||||
const getTargetSchema = z.object({
|
||||
targetId: z.string().transform(Number).pipe(z.number().int().positive()),
|
||||
});
|
||||
const getTargetSchema = z
|
||||
.object({
|
||||
targetId: z.string().transform(Number).pipe(z.number().int().positive())
|
||||
})
|
||||
.strict();
|
||||
|
||||
export async function getTarget(
|
||||
req: Request,
|
||||
@@ -51,7 +53,7 @@ export async function getTarget(
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Target retrieved successfully",
|
||||
status: HttpCode.OK,
|
||||
status: HttpCode.OK
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
|
||||
@@ -9,9 +9,14 @@ import { z } from "zod";
|
||||
import { fromError } from "zod-validation-error";
|
||||
import logger from "@server/logger";
|
||||
|
||||
const listTargetsParamsSchema = z.object({
|
||||
resourceId: z.string().transform(Number).pipe(z.number().int().positive()),
|
||||
});
|
||||
const listTargetsParamsSchema = z
|
||||
.object({
|
||||
resourceId: z
|
||||
.string()
|
||||
.transform(Number)
|
||||
.pipe(z.number().int().positive())
|
||||
})
|
||||
.strict();
|
||||
|
||||
const listTargetsSchema = z.object({
|
||||
limit: z
|
||||
@@ -25,7 +30,7 @@ const listTargetsSchema = z.object({
|
||||
.optional()
|
||||
.default("0")
|
||||
.transform(Number)
|
||||
.pipe(z.number().int().nonnegative()),
|
||||
.pipe(z.number().int().nonnegative())
|
||||
});
|
||||
|
||||
function queryTargets(resourceId: number) {
|
||||
@@ -37,7 +42,7 @@ function queryTargets(resourceId: number) {
|
||||
port: targets.port,
|
||||
protocol: targets.protocol,
|
||||
enabled: targets.enabled,
|
||||
resourceId: targets.resourceId,
|
||||
resourceId: targets.resourceId
|
||||
// resourceName: resources.name,
|
||||
})
|
||||
.from(targets)
|
||||
@@ -97,13 +102,13 @@ export async function listTargets(
|
||||
pagination: {
|
||||
total: totalCount,
|
||||
limit,
|
||||
offset,
|
||||
},
|
||||
offset
|
||||
}
|
||||
},
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Targets retrieved successfully",
|
||||
status: HttpCode.OK,
|
||||
status: HttpCode.OK
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
|
||||
@@ -11,20 +11,22 @@ import { fromError } from "zod-validation-error";
|
||||
import { addPeer } from "../gerbil/peers";
|
||||
import { addTargets } from "../newt/targets";
|
||||
|
||||
const updateTargetParamsSchema = z.object({
|
||||
targetId: z.string().transform(Number).pipe(z.number().int().positive()),
|
||||
});
|
||||
const updateTargetParamsSchema = z
|
||||
.object({
|
||||
targetId: z.string().transform(Number).pipe(z.number().int().positive())
|
||||
})
|
||||
.strict();
|
||||
|
||||
const updateTargetBodySchema = z
|
||||
.object({
|
||||
ip: z.string().ip().optional(), // for now we cant update the ip; you will have to delete
|
||||
method: z.string().min(1).max(10).optional(),
|
||||
port: z.number().int().min(1).max(65535).optional(),
|
||||
enabled: z.boolean().optional(),
|
||||
enabled: z.boolean().optional()
|
||||
})
|
||||
.strict()
|
||||
.refine((data) => Object.keys(data).length > 0, {
|
||||
message: "At least one field must be provided for update",
|
||||
message: "At least one field must be provided for update"
|
||||
});
|
||||
|
||||
export async function updateTarget(
|
||||
@@ -74,7 +76,7 @@ export async function updateTarget(
|
||||
// get the resource
|
||||
const [resource] = await db
|
||||
.select({
|
||||
siteId: resources.siteId,
|
||||
siteId: resources.siteId
|
||||
})
|
||||
.from(resources)
|
||||
.where(eq(resources.resourceId, updatedTarget.resourceId!));
|
||||
@@ -107,14 +109,14 @@ export async function updateTarget(
|
||||
// TODO: is this all inefficient?
|
||||
// Fetch resources for this site
|
||||
const resourcesRes = await db.query.resources.findMany({
|
||||
where: eq(resources.siteId, site.siteId),
|
||||
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),
|
||||
where: eq(targets.resourceId, resource.resourceId)
|
||||
});
|
||||
return targetsRes.map((target) => `${target.ip}/32`);
|
||||
})
|
||||
@@ -122,7 +124,7 @@ export async function updateTarget(
|
||||
|
||||
await addPeer(site.exitNodeId!, {
|
||||
publicKey: site.pubKey,
|
||||
allowedIps: targetIps.flat(),
|
||||
allowedIps: targetIps.flat()
|
||||
});
|
||||
} else if (site.type == "newt") {
|
||||
// get the newt on the site by querying the newt table for siteId
|
||||
@@ -140,7 +142,7 @@ export async function updateTarget(
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Target updated successfully",
|
||||
status: HttpCode.OK,
|
||||
status: HttpCode.OK
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
|
||||
Reference in New Issue
Block a user