feature: Adds forward creation.
This commit is contained in:
parent
c385d7b83b
commit
b7b7f80308
6 changed files with 134 additions and 6 deletions
|
@ -4,8 +4,11 @@ import { PrismaClient } from '@prisma/client';
|
|||
import Fastify from "fastify";
|
||||
|
||||
import { ServerOptions, SessionToken } from "./libs/types.js";
|
||||
|
||||
import { route as backendCreate } from "./routes/backends/create.js";
|
||||
|
||||
import { route as forwardCreate } from "./routes/forward/create.js";
|
||||
|
||||
import { route as userCreate } from "./routes/user/create.js";
|
||||
import { route as userLogin } from "./routes/user/login.js";
|
||||
|
||||
|
@ -35,6 +38,8 @@ const fastify = Fastify({
|
|||
|
||||
backendCreate(fastify, prisma, sessionTokens, serverOptions);
|
||||
|
||||
forwardCreate(fastify, prisma, sessionTokens, serverOptions);
|
||||
|
||||
userCreate(fastify, prisma, sessionTokens, serverOptions);
|
||||
userLogin(fastify, prisma, sessionTokens, serverOptions);
|
||||
|
||||
|
|
93
src/routes/forward/create.ts
Normal file
93
src/routes/forward/create.ts
Normal file
|
@ -0,0 +1,93 @@
|
|||
import type { PrismaClient } from "@prisma/client";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
import { ServerOptions, SessionToken } from "../../libs/types.js";
|
||||
import { hasPermissionByToken } from "../../libs/permissions.js";
|
||||
|
||||
export function route(fastify: FastifyInstance, prisma: PrismaClient, tokens: Record<number, SessionToken[]>, options: ServerOptions) {
|
||||
function hasPermission(token: string, permissionList: string[]): Promise<boolean> {
|
||||
return hasPermissionByToken(permissionList, token, tokens, prisma);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new route to use
|
||||
*/
|
||||
fastify.post("/api/v1/forward/create", {
|
||||
schema: {
|
||||
body: {
|
||||
type: "object",
|
||||
required: ["token", "name", "sourceIP", "sourcePort", "destinationPort", "providerID"],
|
||||
|
||||
properties: {
|
||||
token: { type: "string" },
|
||||
|
||||
name: { type: "string" },
|
||||
description: { type: "string" },
|
||||
|
||||
sourceIP: { type: "string" },
|
||||
sourcePort: { type: "number" },
|
||||
|
||||
destinationPort: { type: "number" },
|
||||
|
||||
providerID: { type: "number" },
|
||||
enabled: { type: "boolean"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, async(req, res) => {
|
||||
// @ts-ignore
|
||||
const body: {
|
||||
token: string,
|
||||
|
||||
name: string,
|
||||
description?: string,
|
||||
|
||||
sourceIP: string,
|
||||
sourcePort: number,
|
||||
|
||||
destinationPort: number,
|
||||
|
||||
providerID: number,
|
||||
|
||||
enabled?: boolean
|
||||
} = req.body;
|
||||
|
||||
if (!await hasPermission(body.token, [
|
||||
"routes.add"
|
||||
])) {
|
||||
return res.status(403).send({
|
||||
error: "Unauthorized"
|
||||
});
|
||||
};
|
||||
|
||||
const lookupIDForDestProvider = await prisma.desinationProvider.findUnique({
|
||||
where: {
|
||||
id: body.providerID
|
||||
}
|
||||
});
|
||||
|
||||
if (!lookupIDForDestProvider) return res.status(400).send({
|
||||
error: "Could not find provider"
|
||||
});
|
||||
|
||||
await prisma.forwardRule.create({
|
||||
data: {
|
||||
name: body.name,
|
||||
description: body.description,
|
||||
|
||||
sourceIP: body.sourceIP,
|
||||
sourcePort: body.sourcePort,
|
||||
|
||||
destPort: body.destinationPort,
|
||||
|
||||
destProviderID: body.providerID,
|
||||
|
||||
enabled: Boolean(body.enabled)
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
success: true
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue