From a5381d0e251803c14e5d453c487f49b8a3c4e444 Mon Sep 17 00:00:00 2001 From: greysoh Date: Sun, 2 Jun 2024 11:16:59 -0400 Subject: [PATCH] chore: Migrates rest of logging to fastify. --- api/src/backendimpl/ssh.ts | 11 ++++++++--- api/src/index.ts | 15 +++++++++------ api/src/libs/backendInit.ts | 19 +++++++++++++------ api/src/routes/backends/create.ts | 5 ++++- lom/src/index.ts | 6 +----- 5 files changed, 35 insertions(+), 21 deletions(-) diff --git a/api/src/backendimpl/ssh.ts b/api/src/backendimpl/ssh.ts index 028f529..5355e0f 100644 --- a/api/src/backendimpl/ssh.ts +++ b/api/src/backendimpl/ssh.ts @@ -104,7 +104,7 @@ export class SSHBackendProvider implements BackendBaseClass { } if (this.sshInstance.connection) { - this.sshInstance.connection.on("end", async() => { + this.sshInstance.connection.on("end", async () => { if (this.state != "started") return; this.logs.push("We disconnected from the SSH server. Restarting..."); @@ -121,10 +121,15 @@ export class SSHBackendProvider implements BackendBaseClass { for (const proxy of proxies) { if (!proxy.enabled) continue; - this.addConnection(proxy.sourceIP, proxy.sourcePort, proxy.destPort, "tcp"); + this.addConnection( + proxy.sourceIP, + proxy.sourcePort, + proxy.destPort, + "tcp", + ); } }); - }; + } this.state = "started"; this.logs.push("Successfully started SSHBackendProvider."); diff --git a/api/src/index.ts b/api/src/index.ts index 7122d63..60c4cf3 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -85,18 +85,21 @@ const routeOptions: RouteOptions = { backends: backends, }; -console.log("Initializing forwarding rules..."); +fastify.log.info("Initializing forwarding rules..."); const createdBackends = await prisma.desinationProvider.findMany(); -for (const backend of createdBackends) { - console.log(`Running init steps for ID '${backend.id}' (${backend.name})`); - const init = await backendInit(backend, backends, prisma); +const logWrapper = (arg: string) => fastify.log.info(arg); +const errorWrapper = (arg: string) => fastify.log.error(arg); - if (init) console.log("Init successful."); +for (const backend of createdBackends) { + fastify.log.info(`Running init steps for ID '${backend.id}' (${backend.name})`); + const init = await backendInit(backend, backends, prisma, logWrapper, errorWrapper); + + if (init) fastify.log.info("Init successful."); } -console.log("Done."); +fastify.log.info("Done."); getPermissions(routeOptions); diff --git a/api/src/libs/backendInit.ts b/api/src/libs/backendInit.ts index ca7613e..ec63d2e 100644 --- a/api/src/libs/backendInit.ts +++ b/api/src/libs/backendInit.ts @@ -1,3 +1,5 @@ +import { format } from "node:util"; + import type { PrismaClient } from "@prisma/client"; import type { BackendBaseClass } from "../backendimpl/base.js"; @@ -15,27 +17,32 @@ export async function backendInit( backend: Backend, backends: Record, prisma: PrismaClient, + logger?: (arg: string) => void, + errorOut?: (arg: string) => void ): Promise { + const log = (...args: any[]) => logger ? logger(format(...args)) : console.log(...args); + const error = (...args: any[]) => errorOut ? errorOut(format(...args)) : log(...args); + const ourProvider = backendProviders[backend.backend]; if (!ourProvider) { - console.log(" - Error: Invalid backend recieved!"); + error(" - Error: Invalid backend recieved!"); return false; } - console.log(" - Initializing backend..."); + log(" - Initializing backend..."); backends[backend.id] = new ourProvider(backend.connectionDetails); const ourBackend = backends[backend.id]; if (!(await ourBackend.start())) { - console.log(" - Error initializing backend!"); - console.log(" - " + ourBackend.logs.join("\n - ")); + error(" - Error initializing backend!"); + error(" - " + ourBackend.logs.join("\n - ")); return false; } - console.log(" - Initializing clients..."); + log(" - Initializing clients..."); const clients = await prisma.forwardRule.findMany({ where: { @@ -46,7 +53,7 @@ export async function backendInit( for (const client of clients) { if (client.protocol != "tcp" && client.protocol != "udp") { - console.error( + error( ` - Error: Client with ID of '${client.id}' has an invalid protocol! (must be either TCP or UDP)`, ); continue; diff --git a/api/src/routes/backends/create.ts b/api/src/routes/backends/create.ts index cd221f0..1726e87 100644 --- a/api/src/routes/backends/create.ts +++ b/api/src/routes/backends/create.ts @@ -7,6 +7,9 @@ import { backendInit } from "../../libs/backendInit.js"; export function route(routeOptions: RouteOptions) { const { fastify, prisma, tokens, backends } = routeOptions; + const logWrapper = (arg: string) => fastify.log.info(arg); + const errorWrapper = (arg: string) => fastify.log.error(arg); + function hasPermission( token: string, permissionList: string[], @@ -80,7 +83,7 @@ export function route(routeOptions: RouteOptions) { }, }); - const init = await backendInit(backend, backends, prisma); + const init = await backendInit(backend, backends, prisma, logWrapper, errorWrapper); if (!init) { // TODO: better error code diff --git a/lom/src/index.ts b/lom/src/index.ts index 69ed4da..c2d93ef 100644 --- a/lom/src/index.ts +++ b/lom/src/index.ts @@ -19,6 +19,7 @@ export type ClientKeys = { function checkValue(input: Buffer, allowed: Buffer): boolean { const autoReject = input.length !== allowed.length; if (autoReject) allowed = input; + const isMatch = timingSafeEqual(input, allowed); return !autoReject && isMatch; } @@ -69,9 +70,7 @@ const server: ssh2.Server = new ssh2.Server({ server.on("connection", client => { let token: string = ""; - // eslint-disable-next-line let username: string = ""; - // eslint-disable-next-line let password: string = ""; client.on("authentication", async auth => { @@ -105,8 +104,6 @@ server.on("connection", client => { continue; } - console.log(auth.signature, auth.blob); - if ( (rawKey.username == auth.username && auth.key.algo == key.type && @@ -114,7 +111,6 @@ server.on("connection", client => { (auth.signature && key.verify(auth.blob as Buffer, auth.signature, auth.key.algo)) ) { - console.log(" -- VERIFIED PUBLIC KEY --"); userData.username = rawKey.username; userData.password = rawKey.password; }