2024-11-03 13:57:51 -05:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { db } from "@server/db";
|
2024-11-19 21:36:56 -05:00
|
|
|
import { newts, resources, sites, targets } from "@server/db/schema";
|
2024-11-03 13:57:51 -05:00
|
|
|
import { eq } from "drizzle-orm";
|
2025-01-01 21:41:31 -05:00
|
|
|
import response from "@server/lib/response";
|
2024-11-03 13:57:51 -05:00
|
|
|
import HttpCode from "@server/types/HttpCode";
|
|
|
|
import createHttpError from "http-errors";
|
|
|
|
import logger from "@server/logger";
|
|
|
|
import { fromError } from "zod-validation-error";
|
2024-11-18 22:10:03 -05:00
|
|
|
import { addPeer } from "../gerbil/peers";
|
2024-11-19 21:36:56 -05:00
|
|
|
import { addTargets } from "../newt/targets";
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-12-21 21:01:12 -05:00
|
|
|
const updateTargetParamsSchema = z
|
|
|
|
.object({
|
|
|
|
targetId: z.string().transform(Number).pipe(z.number().int().positive())
|
|
|
|
})
|
|
|
|
.strict();
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-11-03 13:57:51 -05:00
|
|
|
const updateTargetBodySchema = z
|
|
|
|
.object({
|
2025-01-04 22:33:25 -05:00
|
|
|
ip: z.string().ip().or(z.literal('localhost')).optional(), // for now we cant update the ip; you will have to delete
|
2024-11-03 13:57:51 -05:00
|
|
|
method: z.string().min(1).max(10).optional(),
|
|
|
|
port: z.number().int().min(1).max(65535).optional(),
|
2024-12-21 21:01:12 -05:00
|
|
|
enabled: z.boolean().optional()
|
2024-11-03 13:57:51 -05:00
|
|
|
})
|
2024-11-14 00:00:17 -05:00
|
|
|
.strict()
|
2024-11-03 13:57:51 -05:00
|
|
|
.refine((data) => Object.keys(data).length > 0, {
|
2024-12-21 21:01:12 -05:00
|
|
|
message: "At least one field must be provided for update"
|
2024-11-03 13:57:51 -05:00
|
|
|
});
|
2024-10-01 21:34:07 -04:00
|
|
|
|
2024-11-03 13:57:51 -05:00
|
|
|
export async function updateTarget(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
|
|
|
): Promise<any> {
|
2024-10-06 18:05:20 -04:00
|
|
|
try {
|
|
|
|
const parsedParams = updateTargetParamsSchema.safeParse(req.params);
|
|
|
|
if (!parsedParams.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
2024-11-03 13:57:51 -05:00
|
|
|
fromError(parsedParams.error).toString()
|
2024-10-06 18:05:20 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const parsedBody = updateTargetBodySchema.safeParse(req.body);
|
|
|
|
if (!parsedBody.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
2024-11-03 13:57:51 -05:00
|
|
|
fromError(parsedBody.error).toString()
|
2024-10-06 18:05:20 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const { targetId } = parsedParams.data;
|
|
|
|
const updateData = parsedBody.data;
|
2024-11-19 21:36:56 -05:00
|
|
|
|
2024-11-18 22:10:03 -05:00
|
|
|
const [updatedTarget] = await db
|
2024-11-03 13:57:51 -05:00
|
|
|
.update(targets)
|
2024-10-06 18:05:20 -04:00
|
|
|
.set(updateData)
|
|
|
|
.where(eq(targets.targetId, targetId))
|
|
|
|
.returning();
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-11-18 22:10:03 -05:00
|
|
|
if (!updatedTarget) {
|
2024-10-06 18:05:20 -04:00
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.NOT_FOUND,
|
|
|
|
`Target with ID ${targetId} not found`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-11-19 21:36:56 -05:00
|
|
|
// get the resource
|
|
|
|
const [resource] = await db
|
|
|
|
.select({
|
2024-12-21 21:01:12 -05:00
|
|
|
siteId: resources.siteId
|
2024-11-19 21:36:56 -05:00
|
|
|
})
|
|
|
|
.from(resources)
|
|
|
|
.where(eq(resources.resourceId, updatedTarget.resourceId!));
|
2024-11-18 22:10:03 -05:00
|
|
|
|
2024-11-19 21:36:56 -05:00
|
|
|
if (!resource) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.NOT_FOUND,
|
|
|
|
`Resource with ID ${updatedTarget.resourceId} not found`
|
|
|
|
)
|
2024-11-18 22:10:03 -05:00
|
|
|
);
|
2024-11-19 21:36:56 -05:00
|
|
|
}
|
2024-11-18 22:10:03 -05:00
|
|
|
|
2024-11-19 21:36:56 -05:00
|
|
|
const [site] = await db
|
|
|
|
.select()
|
|
|
|
.from(sites)
|
|
|
|
.where(eq(sites.siteId, resource.siteId!))
|
|
|
|
.limit(1);
|
|
|
|
|
|
|
|
if (!site) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.NOT_FOUND,
|
|
|
|
`Site with ID ${resource.siteId} not found`
|
|
|
|
)
|
|
|
|
);
|
2024-11-18 22:10:03 -05:00
|
|
|
}
|
2024-11-19 21:36:56 -05:00
|
|
|
if (site.pubKey) {
|
|
|
|
if (site.type == "wireguard") {
|
|
|
|
// TODO: is this all inefficient?
|
|
|
|
// Fetch resources for this site
|
|
|
|
const resourcesRes = await db.query.resources.findMany({
|
2024-12-21 21:01:12 -05:00
|
|
|
where: eq(resources.siteId, site.siteId)
|
2024-11-19 21:36:56 -05:00
|
|
|
});
|
2024-11-18 22:10:03 -05:00
|
|
|
|
2024-11-19 21:36:56 -05:00
|
|
|
// Fetch targets for all resources of this site
|
|
|
|
const targetIps = await Promise.all(
|
|
|
|
resourcesRes.map(async (resource) => {
|
|
|
|
const targetsRes = await db.query.targets.findMany({
|
2024-12-21 21:01:12 -05:00
|
|
|
where: eq(targets.resourceId, resource.resourceId)
|
2024-11-19 21:36:56 -05:00
|
|
|
});
|
|
|
|
return targetsRes.map((target) => `${target.ip}/32`);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
await addPeer(site.exitNodeId!, {
|
|
|
|
publicKey: site.pubKey,
|
2024-12-21 21:01:12 -05:00
|
|
|
allowedIps: targetIps.flat()
|
2024-11-19 21:36:56 -05:00
|
|
|
});
|
|
|
|
} else if (site.type == "newt") {
|
|
|
|
// get the newt on the site by querying the newt table for siteId
|
|
|
|
const [newt] = await db
|
|
|
|
.select()
|
|
|
|
.from(newts)
|
|
|
|
.where(eq(newts.siteId, site.siteId))
|
|
|
|
.limit(1);
|
|
|
|
|
|
|
|
addTargets(newt.newtId, [updatedTarget]);
|
|
|
|
}
|
|
|
|
}
|
2024-10-06 18:05:20 -04:00
|
|
|
return response(res, {
|
2024-11-18 22:10:03 -05:00
|
|
|
data: updatedTarget,
|
2024-10-06 18:05:20 -04:00
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Target updated successfully",
|
2024-12-21 21:01:12 -05:00
|
|
|
status: HttpCode.OK
|
2024-10-06 18:05:20 -04:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error);
|
2024-11-03 13:57:51 -05:00
|
|
|
return next(
|
2024-11-05 23:55:46 -05:00
|
|
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
2024-11-03 13:57:51 -05:00
|
|
|
);
|
2024-10-06 18:05:20 -04:00
|
|
|
}
|
2024-10-02 00:04:40 -04:00
|
|
|
}
|