2024-10-02 22:05:21 -04:00
|
|
|
import { Request, Response, NextFunction } from 'express';
|
|
|
|
import { z } from 'zod';
|
|
|
|
import { db } from '@server/db';
|
2024-10-26 22:44:34 -04:00
|
|
|
import { roles, userSites, sites, roleSites, exitNodes } from '@server/db/schema';
|
2024-10-01 21:34:07 -04:00
|
|
|
import response from "@server/utils/response";
|
2024-10-02 22:05:21 -04:00
|
|
|
import HttpCode from '@server/types/HttpCode';
|
|
|
|
import createHttpError from 'http-errors';
|
2024-10-06 16:43:59 -04:00
|
|
|
import { ActionsEnum, checkUserActionPermission } from '@server/auth/actions';
|
2024-10-06 18:05:20 -04:00
|
|
|
import logger from '@server/logger';
|
2024-10-12 21:36:14 -04:00
|
|
|
import { eq, and } from 'drizzle-orm';
|
2024-10-26 16:04:01 -04:00
|
|
|
import { getUniqueSiteName } from '@server/db/names';
|
2024-10-26 22:44:34 -04:00
|
|
|
import { addPeer } from '../gerbil/peers';
|
2024-10-03 22:31:20 -04:00
|
|
|
|
|
|
|
const API_BASE_URL = "http://localhost:3000";
|
2024-10-01 21:34:07 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const createSiteParamsSchema = z.object({
|
2024-10-14 15:11:18 -04: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
|
|
|
// Define Zod schema for request body validation
|
|
|
|
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;
|
|
|
|
// niceId: string;
|
|
|
|
// subdomain: string;
|
|
|
|
// subnet: string;
|
|
|
|
};
|
|
|
|
|
2024-10-02 22:05:21 -04:00
|
|
|
export async function createSite(req: Request, res: Response, next: NextFunction): Promise<any> {
|
2024-10-06 18:05:20 -04:00
|
|
|
try {
|
|
|
|
// Validate request body
|
|
|
|
const parsedBody = createSiteSchema.safeParse(req.body);
|
|
|
|
if (!parsedBody.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
parsedBody.error.errors.map(e => e.message).join(', ')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
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
|
|
|
// Validate request params
|
|
|
|
const parsedParams = createSiteParamsSchema.safeParse(req.params);
|
|
|
|
if (!parsedParams.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
parsedParams.error.errors.map(e => e.message).join(', ')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-02 22:05:21 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const { orgId } = parsedParams.data;
|
|
|
|
|
|
|
|
// Check if the user has permission to list sites
|
|
|
|
const hasPermission = await checkUserActionPermission(ActionsEnum.createSite, req);
|
|
|
|
if (!hasPermission) {
|
2024-10-12 21:36:14 -04:00
|
|
|
return next(createHttpError(HttpCode.FORBIDDEN, 'User does not have permission perform this action'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!req.userOrgRoleId) {
|
|
|
|
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-10-06 18:05:20 -04:00
|
|
|
// Create new site in the database
|
2024-10-14 21:59:35 -04:00
|
|
|
const [newSite] = await db.insert(sites).values({
|
2024-10-06 18:05:20 -04:00
|
|
|
orgId,
|
2024-10-26 16:04:01 -04:00
|
|
|
exitNodeId,
|
2024-10-06 18:05:20 -04:00
|
|
|
name,
|
2024-10-14 21:59:35 -04:00
|
|
|
niceId,
|
2024-10-06 18:05:20 -04:00
|
|
|
pubKey,
|
|
|
|
subnet,
|
|
|
|
}).returning();
|
2024-10-12 21:36:14 -04:00
|
|
|
// find the superuser roleId and also add the resource to the superuser role
|
|
|
|
const superuserRole = await db.select()
|
|
|
|
.from(roles)
|
|
|
|
.where(and(eq(roles.isSuperuserRole, true), eq(roles.orgId, orgId)))
|
|
|
|
.limit(1);
|
2024-10-13 17:13:47 -04:00
|
|
|
|
2024-10-12 21:36:14 -04:00
|
|
|
if (superuserRole.length === 0) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.NOT_FOUND,
|
|
|
|
`Superuser role not found`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-13 17:13:47 -04:00
|
|
|
|
|
|
|
await db.insert(roleSites).values({
|
2024-10-12 21:36:14 -04:00
|
|
|
roleId: superuserRole[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-10-12 21:36:14 -04:00
|
|
|
if (req.userOrgRoleId != superuserRole[0].roleId) {
|
|
|
|
// 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-10-26 22:44:34 -04:00
|
|
|
// Add the peer to the exit node
|
|
|
|
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,
|
|
|
|
// subdomain: newSite.subdomain,
|
|
|
|
// subnet: newSite.subnet,
|
|
|
|
},
|
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-10-06 18:05:20 -04:00
|
|
|
return next(createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred..."));
|
|
|
|
}
|
2024-10-26 22:44:34 -04:00
|
|
|
}
|