mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-20 09:19:02 +02:00
Fixing stuff
This commit is contained in:
parent
0599421975
commit
d3b3667b72
5 changed files with 1765 additions and 382 deletions
1353
server/db/names.json
1353
server/db/names.json
File diff suppressed because it is too large
Load diff
18
server/db/names.ts
Normal file
18
server/db/names.ts
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import { fileURLToPath } from 'url';
|
||||||
|
import { dirname, join } from 'path';
|
||||||
|
import { readFileSync } from 'fs';
|
||||||
|
|
||||||
|
// Get the directory name of the current module
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = dirname(__filename);
|
||||||
|
|
||||||
|
// Load the names from the names.json file
|
||||||
|
const file = join(__dirname, 'names.json');
|
||||||
|
export const names = JSON.parse(readFileSync(file, 'utf-8'));
|
||||||
|
|
||||||
|
export function getUniqueName(): string {
|
||||||
|
return (
|
||||||
|
names.animals[Math.floor(Math.random() * names.animals.length)] +
|
||||||
|
names.descriptor[Math.floor(Math.random() * names.descriptor.length)]
|
||||||
|
).toLowerCase();
|
||||||
|
}
|
|
@ -12,6 +12,7 @@ export const sites = sqliteTable("sites", {
|
||||||
orgId: text("orgId").references(() => orgs.orgId, {
|
orgId: text("orgId").references(() => orgs.orgId, {
|
||||||
onDelete: "cascade",
|
onDelete: "cascade",
|
||||||
}),
|
}),
|
||||||
|
niceId: text("niceId"),
|
||||||
exitNode: integer("exitNode").references(() => exitNodes.exitNodeId, {
|
exitNode: integer("exitNode").references(() => exitNodes.exitNodeId, {
|
||||||
onDelete: "set null",
|
onDelete: "set null",
|
||||||
}),
|
}),
|
||||||
|
|
|
@ -9,6 +9,7 @@ import fetch from 'node-fetch';
|
||||||
import { ActionsEnum, checkUserActionPermission } from '@server/auth/actions';
|
import { ActionsEnum, checkUserActionPermission } from '@server/auth/actions';
|
||||||
import logger from '@server/logger';
|
import logger from '@server/logger';
|
||||||
import { eq, and } from 'drizzle-orm';
|
import { eq, and } from 'drizzle-orm';
|
||||||
|
import { getUniqueName } from '@server/db/names';
|
||||||
|
|
||||||
const API_BASE_URL = "http://localhost:3000";
|
const API_BASE_URL = "http://localhost:3000";
|
||||||
|
|
||||||
|
@ -24,6 +25,16 @@ const createSiteSchema = z.object({
|
||||||
subnet: z.string().optional(),
|
subnet: z.string().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export type GetSiteResponse = {
|
||||||
|
name: string;
|
||||||
|
siteId: number;
|
||||||
|
orgId: string;
|
||||||
|
niceId: string;
|
||||||
|
// niceId: string;
|
||||||
|
// subdomain: string;
|
||||||
|
// subnet: string;
|
||||||
|
};
|
||||||
|
|
||||||
export async function createSite(req: Request, res: Response, next: NextFunction): Promise<any> {
|
export async function createSite(req: Request, res: Response, next: NextFunction): Promise<any> {
|
||||||
try {
|
try {
|
||||||
// Validate request body
|
// Validate request body
|
||||||
|
@ -62,10 +73,15 @@ export async function createSite(req: Request, res: Response, next: NextFunction
|
||||||
return next(createHttpError(HttpCode.FORBIDDEN, 'User does not have a role'));
|
return next(createHttpError(HttpCode.FORBIDDEN, 'User does not have a role'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const niceId = getUniqueName();
|
||||||
|
|
||||||
|
// TODO: pick a subnet
|
||||||
|
|
||||||
// Create new site in the database
|
// Create new site in the database
|
||||||
const newSite = await db.insert(sites).values({
|
const [newSite] = await db.insert(sites).values({
|
||||||
orgId,
|
orgId,
|
||||||
name,
|
name,
|
||||||
|
niceId,
|
||||||
subdomain,
|
subdomain,
|
||||||
pubKey,
|
pubKey,
|
||||||
subnet,
|
subnet,
|
||||||
|
@ -87,19 +103,26 @@ export async function createSite(req: Request, res: Response, next: NextFunction
|
||||||
|
|
||||||
await db.insert(roleSites).values({
|
await db.insert(roleSites).values({
|
||||||
roleId: superuserRole[0].roleId,
|
roleId: superuserRole[0].roleId,
|
||||||
siteId: newSite[0].siteId,
|
siteId: newSite.siteId,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (req.userOrgRoleId != superuserRole[0].roleId) {
|
if (req.userOrgRoleId != superuserRole[0].roleId) {
|
||||||
// make sure the user can access the site
|
// make sure the user can access the site
|
||||||
db.insert(userSites).values({
|
db.insert(userSites).values({
|
||||||
userId: req.user?.userId!,
|
userId: req.user?.userId!,
|
||||||
siteId: newSite[0].siteId,
|
siteId: newSite.siteId,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return response(res, {
|
return response(res, {
|
||||||
data: newSite[0],
|
data: {
|
||||||
|
name: newSite.name,
|
||||||
|
niceId: newSite.niceId,
|
||||||
|
siteId: newSite.siteId,
|
||||||
|
orgId: newSite.orgId,
|
||||||
|
// subdomain: newSite.subdomain,
|
||||||
|
// subnet: newSite.subnet,
|
||||||
|
},
|
||||||
success: true,
|
success: true,
|
||||||
error: false,
|
error: false,
|
||||||
message: "Site created successfully",
|
message: "Site created successfully",
|
||||||
|
|
|
@ -53,16 +53,6 @@ const accountFormSchema = z.object({
|
||||||
.max(30, {
|
.max(30, {
|
||||||
message: "Name must not be longer than 30 characters.",
|
message: "Name must not be longer than 30 characters.",
|
||||||
}),
|
}),
|
||||||
subdomain: z
|
|
||||||
.string()
|
|
||||||
// cant be too long and cant have spaces or special characters
|
|
||||||
.regex(/^[a-zA-Z0-9-]+$/)
|
|
||||||
.min(2, {
|
|
||||||
message: "Subdomain must be at least 2 characters.",
|
|
||||||
})
|
|
||||||
.max(30, {
|
|
||||||
message: "Subdomain must not be longer than 30 characters.",
|
|
||||||
}),
|
|
||||||
method: z.enum(["wg", "newt"]),
|
method: z.enum(["wg", "newt"]),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -99,17 +89,17 @@ export function CreateSiteForm() {
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const name = form.watch("name");
|
// const name = form.watch("name");
|
||||||
useEffect(() => {
|
// useEffect(() => {
|
||||||
const subdomain = name.toLowerCase().replace(/\s+/g, "-");
|
// const subdomain = name.toLowerCase().replace(/\s+/g, "-");
|
||||||
form.setValue("subdomain", subdomain, { shouldValidate: true });
|
// form.setValue("subdomain", subdomain, { shouldValidate: true });
|
||||||
}, [name, form]);
|
// }, [name, form]);
|
||||||
|
|
||||||
async function onSubmit(data: AccountFormValues) {
|
async function onSubmit(data: AccountFormValues) {
|
||||||
const res = await api
|
const res = await api
|
||||||
.put(`/org/${orgId}/site/`, {
|
.put(`/org/${orgId}/site/`, {
|
||||||
name: data.name,
|
name: data.name,
|
||||||
subdomain: data.subdomain,
|
// subdomain: data.subdomain,
|
||||||
pubKey: keypair?.publicKey,
|
pubKey: keypair?.publicKey,
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
|
@ -161,7 +151,7 @@ sh get-docker.sh`;
|
||||||
</FormItem>
|
</FormItem>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
<FormField
|
{/* <FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="subdomain"
|
name="subdomain"
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
|
@ -176,7 +166,7 @@ sh get-docker.sh`;
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
)}
|
)}
|
||||||
/>
|
/> */}
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="method"
|
name="method"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue