From 3d8869066ad8226815b14dd444b997f300f6000f Mon Sep 17 00:00:00 2001 From: Owen Date: Tue, 12 Aug 2025 16:47:59 -0700 Subject: [PATCH] Adjust pulling in config --- server/db/queries/verifySessionQueries.ts | 28 +++++++++++------------ server/hybridClientServer.ts | 4 ++++ server/index.ts | 2 +- server/lib/config.ts | 4 ++++ server/lib/remoteProxy.ts | 14 +----------- server/routers/internal.ts | 6 ++--- 6 files changed, 26 insertions(+), 32 deletions(-) diff --git a/server/db/queries/verifySessionQueries.ts b/server/db/queries/verifySessionQueries.ts index 1e159304..44982f64 100644 --- a/server/db/queries/verifySessionQueries.ts +++ b/server/db/queries/verifySessionQueries.ts @@ -16,6 +16,7 @@ import { } from "@server/db"; import { and, eq } from "drizzle-orm"; import axios from "axios"; +import config from "@server/lib/config"; export type ResourceWithAuth = { resource: Resource | null; @@ -28,18 +29,15 @@ export type UserSessionWithUser = { user: any; }; -const MODE = "remote"; -const remoteEndpoint = "https://api.example.com"; - /** * Get resource by domain with pincode and password information */ export async function getResourceByDomain( domain: string ): Promise { - if (MODE === "remote") { + if (config.isHybridMode()) { try { - const response = await axios.get(`${remoteEndpoint}/resource/domain/${domain}`); + const response = await axios.get(`${config.getRawConfig().hybrid?.endpoint}/resource/domain/${domain}`); return response.data; } catch (error) { console.error("Error fetching resource by domain:", error); @@ -78,9 +76,9 @@ export async function getResourceByDomain( export async function getUserSessionWithUser( userSessionId: string ): Promise { - if (MODE === "remote") { + if (config.isHybridMode()) { try { - const response = await axios.get(`${remoteEndpoint}/session/${userSessionId}`); + const response = await axios.get(`${config.getRawConfig().hybrid?.endpoint}/session/${userSessionId}`); return response.data; } catch (error) { console.error("Error fetching user session:", error); @@ -108,9 +106,9 @@ export async function getUserSessionWithUser( * Get user organization role */ export async function getUserOrgRole(userId: string, orgId: string) { - if (MODE === "remote") { + if (config.isHybridMode()) { try { - const response = await axios.get(`${remoteEndpoint}/user/${userId}/org/${orgId}/role`); + const response = await axios.get(`${config.getRawConfig().hybrid?.endpoint}/user/${userId}/org/${orgId}/role`); return response.data; } catch (error) { console.error("Error fetching user org role:", error); @@ -136,9 +134,9 @@ export async function getUserOrgRole(userId: string, orgId: string) { * Check if role has access to resource */ export async function getRoleResourceAccess(resourceId: number, roleId: number) { - if (MODE === "remote") { + if (config.isHybridMode()) { try { - const response = await axios.get(`${remoteEndpoint}/role/${roleId}/resource/${resourceId}/access`); + const response = await axios.get(`${config.getRawConfig().hybrid?.endpoint}/role/${roleId}/resource/${resourceId}/access`); return response.data; } catch (error) { console.error("Error fetching role resource access:", error); @@ -164,9 +162,9 @@ export async function getRoleResourceAccess(resourceId: number, roleId: number) * Check if user has direct access to resource */ export async function getUserResourceAccess(userId: string, resourceId: number) { - if (MODE === "remote") { + if (config.isHybridMode()) { try { - const response = await axios.get(`${remoteEndpoint}/user/${userId}/resource/${resourceId}/access`); + const response = await axios.get(`${config.getRawConfig().hybrid?.endpoint}/user/${userId}/resource/${resourceId}/access`); return response.data; } catch (error) { console.error("Error fetching user resource access:", error); @@ -192,9 +190,9 @@ export async function getUserResourceAccess(userId: string, resourceId: number) * Get resource rules for a given resource */ export async function getResourceRules(resourceId: number): Promise { - if (MODE === "remote") { + if (config.isHybridMode()) { try { - const response = await axios.get(`${remoteEndpoint}/resource/${resourceId}/rules`); + const response = await axios.get(`${config.getRawConfig().hybrid?.endpoint}/resource/${resourceId}/rules`); return response.data; } catch (error) { console.error("Error fetching resource rules:", error); diff --git a/server/hybridClientServer.ts b/server/hybridClientServer.ts index 8a16f985..074fcd2e 100644 --- a/server/hybridClientServer.ts +++ b/server/hybridClientServer.ts @@ -56,6 +56,10 @@ export async function createHybridClientServer() { await deletePeer(exitNode.exitNodeId, pubKey); }); + client.registerHandler("remote/traefik/reload", async (message) => { + await monitor.HandleTraefikConfig(); + }); + // Listen to connection events client.on("connect", () => { console.log("Connected to WebSocket server"); diff --git a/server/index.ts b/server/index.ts index b0d6d3d7..42f85da6 100644 --- a/server/index.ts +++ b/server/index.ts @@ -20,7 +20,7 @@ async function startServers() { const nextServer = await createNextServer(); let hybridClientServer; - if (config.getRawConfig().hybrid) { + if (config.isHybridMode()) { hybridClientServer = createHybridClientServer(); } diff --git a/server/lib/config.ts b/server/lib/config.ts index 023ae054..c8c7b7c4 100644 --- a/server/lib/config.ts +++ b/server/lib/config.ts @@ -147,6 +147,10 @@ export class Config { return false; } + public isHybridMode() { + return this.rawConfig?.hybrid; + } + public async checkSupporterKey() { const [key] = await db.select().from(supporterKey).limit(1); diff --git a/server/lib/remoteProxy.ts b/server/lib/remoteProxy.ts index 4e70dd04..080c3bd3 100644 --- a/server/lib/remoteProxy.ts +++ b/server/lib/remoteProxy.ts @@ -17,20 +17,8 @@ export const proxyToRemote = async ( endpoint: string ): Promise => { try { - const remoteConfig = config.getRawConfig().hybrid; - - if (!remoteConfig?.endpoint) { - logger.error("Remote endpoint not configured in hybrid.endpoint config"); - return next( - createHttpError( - HttpCode.INTERNAL_SERVER_ERROR, - "Remote endpoint not configured" - ) - ); - } + const remoteUrl = `${config.getRawConfig().hybrid?.endpoint?.replace(/\/$/, '')}/api/v1/${endpoint}`; - const remoteUrl = `${remoteConfig.endpoint.replace(/\/$/, '')}/api/v1/${endpoint}`; - logger.debug(`Proxying request to remote server: ${remoteUrl}`); // Forward the request to the remote server diff --git a/server/routers/internal.ts b/server/routers/internal.ts index dc212b8b..a84f6976 100644 --- a/server/routers/internal.ts +++ b/server/routers/internal.ts @@ -7,7 +7,7 @@ import * as auth from "@server/routers/auth"; import * as supporterKey from "@server/routers/supporterKey"; import * as license from "@server/routers/license"; import * as idp from "@server/routers/idp"; -import { proxyToRemote } from "@server/remoteProxy"; +import { proxyToRemote } from "@server/lib/remoteProxy"; import config from "@server/lib/config"; import HttpCode from "@server/types/HttpCode"; import { @@ -51,7 +51,7 @@ internalRouter.get("/idp/:idpId", idp.getIdp); const gerbilRouter = Router(); internalRouter.use("/gerbil", gerbilRouter); -if (config.getRawConfig().hybrid) { +if (config.isHybridMode()) { // Use proxy router to forward requests to remote cloud server // Proxy endpoints for each gerbil route gerbilRouter.post("/get-config", (req, res, next) => @@ -83,7 +83,7 @@ internalRouter.use("/badger", badgerRouter); badgerRouter.post("/verify-session", badger.verifyResourceSession); -if (config.getRawConfig().hybrid) { +if (config.isHybridMode()) { badgerRouter.post("/exchange-session", (req, res, next) => proxyToRemote(req, res, next, "badger/exchange-session") );