From b7b7f803081def4a52cfd1d580ba1174ed7c92be Mon Sep 17 00:00:00 2001 From: greysoh Date: Sun, 21 Apr 2024 17:31:16 -0400 Subject: [PATCH] feature: Adds forward creation. --- init.sh | 10 +- .../migration.sql | 8 ++ prisma/schema.prisma | 1 - routes/NextNet API/Create Forward.bru | 23 +++++ src/index.ts | 5 + src/routes/forward/create.ts | 93 +++++++++++++++++++ 6 files changed, 134 insertions(+), 6 deletions(-) create mode 100644 prisma/migrations/20240421210417_fix_remove_destip/migration.sql create mode 100644 routes/NextNet API/Create Forward.bru create mode 100644 src/routes/forward/create.ts diff --git a/init.sh b/init.sh index 83de5b3..f36707c 100755 --- a/init.sh +++ b/init.sh @@ -5,14 +5,14 @@ if [ ! -d ".tmp" ]; then mkdir .tmp fi -lsof -i:5432 | grep postgres +lsof -i:5432 | grep postgres 2> /dev/null > /dev/null IS_PG_RUNNING=$? if [ ! -f ".tmp/ispginit" ]; then if [[ "$IS_PG_RUNNING" == 0 ]]; then - kill -9 $(lsof -t -i:5432) + kill -9 $(lsof -t -i:5432) > /dev/null 2> /dev/null fi - + echo " - Database not initialized! Initializing database..." mkdir .tmp/pglock @@ -21,10 +21,10 @@ if [ ! -f ".tmp/ispginit" ]; then createdb -h localhost -p 5432 nextnet psql -h localhost -p 5432 nextnet -c "CREATE ROLE nextnet WITH LOGIN SUPERUSER PASSWORD 'nextnet';" - + npm install --save-dev npx prisma migrate dev - + touch .tmp/ispginit elif [[ "$IS_PG_RUNNING" == 1 ]]; then pg_ctl -D .tmp/db -l .tmp/logfile -o "--unix_socket_directories='$PWD/.tmp/pglock/'" start diff --git a/prisma/migrations/20240421210417_fix_remove_destip/migration.sql b/prisma/migrations/20240421210417_fix_remove_destip/migration.sql new file mode 100644 index 0000000..a673c64 --- /dev/null +++ b/prisma/migrations/20240421210417_fix_remove_destip/migration.sql @@ -0,0 +1,8 @@ +/* + Warnings: + + - You are about to drop the column `destIP` on the `ForwardRule` table. All the data in the column will be lost. + +*/ +-- AlterTable +ALTER TABLE "ForwardRule" DROP COLUMN "destIP"; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index d822d47..d5ce5d8 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -26,7 +26,6 @@ model ForwardRule { description String? sourceIP String sourcePort Int - destIP String destPort Int destProviderID Int enabled Boolean diff --git a/routes/NextNet API/Create Forward.bru b/routes/NextNet API/Create Forward.bru new file mode 100644 index 0000000..204cd6a --- /dev/null +++ b/routes/NextNet API/Create Forward.bru @@ -0,0 +1,23 @@ +meta { + name: Create Forward + type: http + seq: 4 +} + +post { + url: http://127.0.0.1:3000/api/v1/backends/create + body: json + auth: none +} + +body:json { + { + "token": "5e2cb92a338a832d385790861312eb85d69f46f82317bfa984ac5e3517368ab5a827897b0f9775a9181b02fa3b9cffed7e59e5b3111d5bdc37f729156caf5f", + "name": "PortCopier Route", + "description": "This is a test route for portcopier.", + "backend": "PortCopier", + "connectionDetails": { + "funny": true + } + } +} diff --git a/src/index.ts b/src/index.ts index 465afcf..8cc45a1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); diff --git a/src/routes/forward/create.ts b/src/routes/forward/create.ts new file mode 100644 index 0000000..3301251 --- /dev/null +++ b/src/routes/forward/create.ts @@ -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, options: ServerOptions) { + function hasPermission(token: string, permissionList: string[]): Promise { + 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 + } + }); +} \ No newline at end of file