chore: Migrates rest of logging to fastify.
This commit is contained in:
parent
a99cf55aa1
commit
a5381d0e25
5 changed files with 35 additions and 21 deletions
|
@ -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.");
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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<number, BackendBaseClass>,
|
||||
prisma: PrismaClient,
|
||||
logger?: (arg: string) => void,
|
||||
errorOut?: (arg: string) => void
|
||||
): Promise<boolean> {
|
||||
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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue