feature: Adds forward creation.
This commit is contained in:
parent
c385d7b83b
commit
b7b7f80308
6 changed files with 134 additions and 6 deletions
10
init.sh
10
init.sh
|
@ -5,14 +5,14 @@ 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..."
|
||||||
mkdir .tmp/pglock
|
mkdir .tmp/pglock
|
||||||
|
|
||||||
|
@ -21,10 +21,10 @@ if [ ! -f ".tmp/ispginit" ]; then
|
||||||
createdb -h localhost -p 5432 nextnet
|
createdb -h localhost -p 5432 nextnet
|
||||||
|
|
||||||
psql -h localhost -p 5432 nextnet -c "CREATE ROLE nextnet WITH LOGIN SUPERUSER PASSWORD 'nextnet';"
|
psql -h localhost -p 5432 nextnet -c "CREATE ROLE nextnet WITH LOGIN SUPERUSER PASSWORD 'nextnet';"
|
||||||
|
|
||||||
npm install --save-dev
|
npm install --save-dev
|
||||||
npx prisma migrate dev
|
npx prisma migrate dev
|
||||||
|
|
||||||
touch .tmp/ispginit
|
touch .tmp/ispginit
|
||||||
elif [[ "$IS_PG_RUNNING" == 1 ]]; then
|
elif [[ "$IS_PG_RUNNING" == 1 ]]; then
|
||||||
pg_ctl -D .tmp/db -l .tmp/logfile -o "--unix_socket_directories='$PWD/.tmp/pglock/'" start
|
pg_ctl -D .tmp/db -l .tmp/logfile -o "--unix_socket_directories='$PWD/.tmp/pglock/'" start
|
||||||
|
|
|
@ -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";
|
|
@ -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
|
||||||
|
|
23
routes/NextNet API/Create Forward.bru
Normal file
23
routes/NextNet API/Create Forward.bru
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
|
||||||
|
|
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