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) {
|
if (this.sshInstance.connection) {
|
||||||
this.sshInstance.connection.on("end", async() => {
|
this.sshInstance.connection.on("end", async () => {
|
||||||
if (this.state != "started") return;
|
if (this.state != "started") return;
|
||||||
this.logs.push("We disconnected from the SSH server. Restarting...");
|
this.logs.push("We disconnected from the SSH server. Restarting...");
|
||||||
|
|
||||||
|
@ -121,10 +121,15 @@ export class SSHBackendProvider implements BackendBaseClass {
|
||||||
|
|
||||||
for (const proxy of proxies) {
|
for (const proxy of proxies) {
|
||||||
if (!proxy.enabled) continue;
|
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.state = "started";
|
||||||
this.logs.push("Successfully started SSHBackendProvider.");
|
this.logs.push("Successfully started SSHBackendProvider.");
|
||||||
|
|
|
@ -85,18 +85,21 @@ const routeOptions: RouteOptions = {
|
||||||
backends: backends,
|
backends: backends,
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log("Initializing forwarding rules...");
|
fastify.log.info("Initializing forwarding rules...");
|
||||||
|
|
||||||
const createdBackends = await prisma.desinationProvider.findMany();
|
const createdBackends = await prisma.desinationProvider.findMany();
|
||||||
|
|
||||||
for (const backend of createdBackends) {
|
const logWrapper = (arg: string) => fastify.log.info(arg);
|
||||||
console.log(`Running init steps for ID '${backend.id}' (${backend.name})`);
|
const errorWrapper = (arg: string) => fastify.log.error(arg);
|
||||||
const init = await backendInit(backend, backends, prisma);
|
|
||||||
|
|
||||||
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);
|
getPermissions(routeOptions);
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import { format } from "node:util";
|
||||||
|
|
||||||
import type { PrismaClient } from "@prisma/client";
|
import type { PrismaClient } from "@prisma/client";
|
||||||
|
|
||||||
import type { BackendBaseClass } from "../backendimpl/base.js";
|
import type { BackendBaseClass } from "../backendimpl/base.js";
|
||||||
|
@ -15,27 +17,32 @@ export async function backendInit(
|
||||||
backend: Backend,
|
backend: Backend,
|
||||||
backends: Record<number, BackendBaseClass>,
|
backends: Record<number, BackendBaseClass>,
|
||||||
prisma: PrismaClient,
|
prisma: PrismaClient,
|
||||||
|
logger?: (arg: string) => void,
|
||||||
|
errorOut?: (arg: string) => void
|
||||||
): Promise<boolean> {
|
): 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];
|
const ourProvider = backendProviders[backend.backend];
|
||||||
|
|
||||||
if (!ourProvider) {
|
if (!ourProvider) {
|
||||||
console.log(" - Error: Invalid backend recieved!");
|
error(" - Error: Invalid backend recieved!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(" - Initializing backend...");
|
log(" - Initializing backend...");
|
||||||
|
|
||||||
backends[backend.id] = new ourProvider(backend.connectionDetails);
|
backends[backend.id] = new ourProvider(backend.connectionDetails);
|
||||||
const ourBackend = backends[backend.id];
|
const ourBackend = backends[backend.id];
|
||||||
|
|
||||||
if (!(await ourBackend.start())) {
|
if (!(await ourBackend.start())) {
|
||||||
console.log(" - Error initializing backend!");
|
error(" - Error initializing backend!");
|
||||||
console.log(" - " + ourBackend.logs.join("\n - "));
|
error(" - " + ourBackend.logs.join("\n - "));
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(" - Initializing clients...");
|
log(" - Initializing clients...");
|
||||||
|
|
||||||
const clients = await prisma.forwardRule.findMany({
|
const clients = await prisma.forwardRule.findMany({
|
||||||
where: {
|
where: {
|
||||||
|
@ -46,7 +53,7 @@ export async function backendInit(
|
||||||
|
|
||||||
for (const client of clients) {
|
for (const client of clients) {
|
||||||
if (client.protocol != "tcp" && client.protocol != "udp") {
|
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)`,
|
` - Error: Client with ID of '${client.id}' has an invalid protocol! (must be either TCP or UDP)`,
|
||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -7,6 +7,9 @@ import { backendInit } from "../../libs/backendInit.js";
|
||||||
export function route(routeOptions: RouteOptions) {
|
export function route(routeOptions: RouteOptions) {
|
||||||
const { fastify, prisma, tokens, backends } = 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(
|
function hasPermission(
|
||||||
token: string,
|
token: string,
|
||||||
permissionList: 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) {
|
if (!init) {
|
||||||
// TODO: better error code
|
// TODO: better error code
|
||||||
|
|
|
@ -19,6 +19,7 @@ export type ClientKeys = {
|
||||||
function checkValue(input: Buffer, allowed: Buffer): boolean {
|
function checkValue(input: Buffer, allowed: Buffer): boolean {
|
||||||
const autoReject = input.length !== allowed.length;
|
const autoReject = input.length !== allowed.length;
|
||||||
if (autoReject) allowed = input;
|
if (autoReject) allowed = input;
|
||||||
|
|
||||||
const isMatch = timingSafeEqual(input, allowed);
|
const isMatch = timingSafeEqual(input, allowed);
|
||||||
return !autoReject && isMatch;
|
return !autoReject && isMatch;
|
||||||
}
|
}
|
||||||
|
@ -69,9 +70,7 @@ const server: ssh2.Server = new ssh2.Server({
|
||||||
server.on("connection", client => {
|
server.on("connection", client => {
|
||||||
let token: string = "";
|
let token: string = "";
|
||||||
|
|
||||||
// eslint-disable-next-line
|
|
||||||
let username: string = "";
|
let username: string = "";
|
||||||
// eslint-disable-next-line
|
|
||||||
let password: string = "";
|
let password: string = "";
|
||||||
|
|
||||||
client.on("authentication", async auth => {
|
client.on("authentication", async auth => {
|
||||||
|
@ -105,8 +104,6 @@ server.on("connection", client => {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(auth.signature, auth.blob);
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
(rawKey.username == auth.username &&
|
(rawKey.username == auth.username &&
|
||||||
auth.key.algo == key.type &&
|
auth.key.algo == key.type &&
|
||||||
|
@ -114,7 +111,6 @@ server.on("connection", client => {
|
||||||
(auth.signature &&
|
(auth.signature &&
|
||||||
key.verify(auth.blob as Buffer, auth.signature, auth.key.algo))
|
key.verify(auth.blob as Buffer, auth.signature, auth.key.algo))
|
||||||
) {
|
) {
|
||||||
console.log(" -- VERIFIED PUBLIC KEY --");
|
|
||||||
userData.username = rawKey.username;
|
userData.username = rawKey.username;
|
||||||
userData.password = rawKey.password;
|
userData.password = rawKey.password;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue