feature: Adds forward creation.

This commit is contained in:
greysoh 2024-04-21 17:31:16 -04:00
parent c385d7b83b
commit b7b7f80308
No known key found for this signature in database
GPG key ID: FE0F173B8FC01571
6 changed files with 134 additions and 6 deletions

View file

@ -5,12 +5,12 @@ if [ ! -d ".tmp" ]; then
mkdir .tmp mkdir .tmp
fi fi
lsof -i:5432 | grep postgres lsof -i:5432 | grep postgres 2> /dev/null > /dev/null
IS_PG_RUNNING=$? IS_PG_RUNNING=$?
if [ ! -f ".tmp/ispginit" ]; then if [ ! -f ".tmp/ispginit" ]; then
if [[ "$IS_PG_RUNNING" == 0 ]]; 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 fi
echo " - Database not initialized! Initializing database..." echo " - Database not initialized! Initializing database..."

View file

@ -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";

View file

@ -26,7 +26,6 @@ model ForwardRule {
description String? description String?
sourceIP String sourceIP String
sourcePort Int sourcePort Int
destIP String
destPort Int destPort Int
destProviderID Int destProviderID Int
enabled Boolean enabled Boolean

View file

@ -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
}
}
}

View file

@ -4,8 +4,11 @@ import { PrismaClient } from '@prisma/client';
import Fastify from "fastify"; import Fastify from "fastify";
import { ServerOptions, SessionToken } from "./libs/types.js"; import { ServerOptions, SessionToken } from "./libs/types.js";
import { route as backendCreate } from "./routes/backends/create.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 userCreate } from "./routes/user/create.js";
import { route as userLogin } from "./routes/user/login.js"; import { route as userLogin } from "./routes/user/login.js";
@ -35,6 +38,8 @@ const fastify = Fastify({
backendCreate(fastify, prisma, sessionTokens, serverOptions); backendCreate(fastify, prisma, sessionTokens, serverOptions);
forwardCreate(fastify, prisma, sessionTokens, serverOptions);
userCreate(fastify, prisma, sessionTokens, serverOptions); userCreate(fastify, prisma, sessionTokens, serverOptions);
userLogin(fastify, prisma, sessionTokens, serverOptions); userLogin(fastify, prisma, sessionTokens, serverOptions);

View 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
}
});
}