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-05 23:55:46 -05:00
|
|
|
import { roles, userSites, sites, roleSites } from "@server/db/schema";
|
2024-10-01 21:34:07 -04:00
|
|
|
import response from "@server/utils/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 { eq, and } from "drizzle-orm";
|
|
|
|
import { getUniqueSiteName } from "@server/db/names";
|
|
|
|
import { addPeer } from "../gerbil/peers";
|
|
|
|
import { fromError } from "zod-validation-error";
|
2024-10-01 21:34:07 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const createSiteParamsSchema = z.object({
|
2024-11-03 13:57:51 -05:00
|
|
|
orgId: z.string(),
|
2024-10-02 22:05:21 -04:00
|
|
|
});
|
2024-10-01 21:34:07 -04:00
|
|
|
|
2024-10-02 22:05:21 -04:00
|
|
|
const createSiteSchema = z.object({
|
2024-10-06 18:05:20 -04:00
|
|
|
name: z.string().min(1).max(255),
|
2024-10-26 16:04:01 -04:00
|
|
|
exitNodeId: z.number().int().positive(),
|
2024-10-06 18:05:20 -04:00
|
|
|
subdomain: z.string().min(1).max(255).optional(),
|
2024-10-26 16:04:01 -04:00
|
|
|
pubKey: z.string(),
|
|
|
|
subnet: z.string(),
|
2024-10-02 22:05:21 -04:00
|
|
|
});
|
|
|
|
|
2024-10-20 12:55:28 -04:00
|
|
|
export type CreateSiteResponse = {
|
2024-10-14 21:59:35 -04:00
|
|
|
name: string;
|
|
|
|
siteId: number;
|
|
|
|
orgId: string;
|
|
|
|
niceId: string;
|
|
|
|
};
|
|
|
|
|
2024-11-03 13:57:51 -05:00
|
|
|
export async function createSite(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
|
|
|
): Promise<any> {
|
2024-10-06 18:05:20 -04:00
|
|
|
try {
|
|
|
|
const parsedBody = createSiteSchema.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-02 22:05:21 -04:00
|
|
|
|
2024-10-26 16:04:01 -04:00
|
|
|
const { name, subdomain, exitNodeId, pubKey, subnet } = parsedBody.data;
|
2024-10-02 22:05:21 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const parsedParams = createSiteParamsSchema.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-02 22:05:21 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const { orgId } = parsedParams.data;
|
|
|
|
|
2024-10-12 21:36:14 -04:00
|
|
|
if (!req.userOrgRoleId) {
|
2024-11-03 13:57:51 -05:00
|
|
|
return next(
|
|
|
|
createHttpError(HttpCode.FORBIDDEN, "User does not have a role")
|
|
|
|
);
|
2024-10-06 18:05:20 -04:00
|
|
|
}
|
2024-10-06 16:43:59 -04:00
|
|
|
|
2024-10-26 16:04:01 -04:00
|
|
|
const niceId = await getUniqueSiteName(orgId);
|
2024-10-14 21:59:35 -04:00
|
|
|
|
2024-11-03 13:57:51 -05:00
|
|
|
const [newSite] = await db
|
|
|
|
.insert(sites)
|
|
|
|
.values({
|
|
|
|
orgId,
|
|
|
|
exitNodeId,
|
|
|
|
name,
|
|
|
|
niceId,
|
|
|
|
pubKey,
|
|
|
|
subnet,
|
|
|
|
})
|
|
|
|
.returning();
|
2024-11-09 00:08:17 -05:00
|
|
|
|
2024-11-05 22:38:57 -05:00
|
|
|
const adminRole = await db
|
2024-11-03 13:57:51 -05:00
|
|
|
.select()
|
|
|
|
.from(roles)
|
2024-11-05 22:38:57 -05:00
|
|
|
.where(and(eq(roles.isAdmin, true), eq(roles.orgId, orgId)))
|
2024-11-03 13:57:51 -05:00
|
|
|
.limit(1);
|
2024-10-13 17:13:47 -04:00
|
|
|
|
2024-11-05 22:38:57 -05:00
|
|
|
if (adminRole.length === 0) {
|
2024-10-12 21:36:14 -04:00
|
|
|
return next(
|
2024-11-05 22:38:57 -05:00
|
|
|
createHttpError(HttpCode.NOT_FOUND, `Admin role not found`)
|
2024-10-12 21:36:14 -04:00
|
|
|
);
|
|
|
|
}
|
2024-10-13 17:13:47 -04:00
|
|
|
|
|
|
|
await db.insert(roleSites).values({
|
2024-11-05 22:38:57 -05:00
|
|
|
roleId: adminRole[0].roleId,
|
2024-10-14 21:59:35 -04:00
|
|
|
siteId: newSite.siteId,
|
2024-10-12 21:36:14 -04:00
|
|
|
});
|
2024-10-02 22:05:21 -04:00
|
|
|
|
2024-11-05 22:38:57 -05:00
|
|
|
if (req.userOrgRoleId != adminRole[0].roleId) {
|
2024-10-12 21:36:14 -04:00
|
|
|
// make sure the user can access the site
|
2024-10-13 17:13:47 -04:00
|
|
|
db.insert(userSites).values({
|
|
|
|
userId: req.user?.userId!,
|
2024-10-14 21:59:35 -04:00
|
|
|
siteId: newSite.siteId,
|
2024-10-12 21:36:14 -04:00
|
|
|
});
|
|
|
|
}
|
2024-10-13 17:13:47 -04:00
|
|
|
|
2024-11-09 00:08:17 -05:00
|
|
|
// add the peer to the exit node
|
2024-10-26 22:44:34 -04:00
|
|
|
await addPeer(exitNodeId, {
|
|
|
|
publicKey: pubKey,
|
|
|
|
allowedIps: [],
|
|
|
|
});
|
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
return response(res, {
|
2024-10-14 21:59:35 -04:00
|
|
|
data: {
|
|
|
|
name: newSite.name,
|
|
|
|
niceId: newSite.niceId,
|
|
|
|
siteId: newSite.siteId,
|
|
|
|
orgId: newSite.orgId,
|
|
|
|
},
|
2024-10-06 18:05:20 -04:00
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Site created successfully",
|
|
|
|
status: HttpCode.CREATED,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2024-10-26 22:44:34 -04:00
|
|
|
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-11-03 13:57:51 -05:00
|
|
|
}
|