From 2347d4bd2f74e4cd5eb9473f82c93aac116de1cd Mon Sep 17 00:00:00 2001 From: LukeGus Date: Fri, 12 Sep 2025 13:04:51 -0500 Subject: [PATCH] Update API to work on devs and remove random letter --- src/backend/database/database.ts | 2 +- src/backend/ssh/file-manager.ts | 2 +- src/backend/ssh/server-stats.ts | 4 +- src/backend/ssh/tunnel.ts | 2 +- src/ui/Mobile/Homepage/HomepageAuth.tsx | 1 - src/ui/main-axios.ts | 79 +++++++++++++++---------- 6 files changed, 54 insertions(+), 36 deletions(-) diff --git a/src/backend/database/database.ts b/src/backend/database/database.ts index d3aa6e41..9a7564b3 100644 --- a/src/backend/database/database.ts +++ b/src/backend/database/database.ts @@ -16,7 +16,7 @@ app.use( cors({ origin: "*", methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"], - allowedHeaders: ["Content-Type", "Authorization"], + allowedHeaders: ["Content-Type", "Authorization", "User-Agent", "X-Electron-App"], }), ); diff --git a/src/backend/ssh/file-manager.ts b/src/backend/ssh/file-manager.ts index 200081bd..8f6296b1 100644 --- a/src/backend/ssh/file-manager.ts +++ b/src/backend/ssh/file-manager.ts @@ -12,7 +12,7 @@ app.use( cors({ origin: "*", methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], - allowedHeaders: ["Content-Type", "Authorization"], + allowedHeaders: ["Content-Type", "Authorization", "User-Agent", "X-Electron-App"], }), ); app.use(express.json({ limit: "100mb" })); diff --git a/src/backend/ssh/server-stats.ts b/src/backend/ssh/server-stats.ts index c36a3d25..e675f4cc 100644 --- a/src/backend/ssh/server-stats.ts +++ b/src/backend/ssh/server-stats.ts @@ -277,12 +277,12 @@ app.use( cors({ origin: "*", methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"], - allowedHeaders: ["Content-Type", "Authorization"], + allowedHeaders: ["Content-Type", "Authorization", "User-Agent", "X-Electron-App"], }), ); app.use((req, res, next) => { res.header("Access-Control-Allow-Origin", "*"); - res.header("Access-Control-Allow-Headers", "Content-Type, Authorization"); + res.header("Access-Control-Allow-Headers", "Content-Type, Authorization, User-Agent, X-Electron-App"); res.header( "Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS", diff --git a/src/backend/ssh/tunnel.ts b/src/backend/ssh/tunnel.ts index 07edaa76..6f2be764 100644 --- a/src/backend/ssh/tunnel.ts +++ b/src/backend/ssh/tunnel.ts @@ -21,7 +21,7 @@ app.use( cors({ origin: "*", methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], - allowedHeaders: "Origin,X-Requested-With,Content-Type,Accept,Authorization", + allowedHeaders: ["Origin", "X-Requested-With", "Content-Type", "Accept", "Authorization", "User-Agent", "X-Electron-App"], }), ); app.use(express.json()); diff --git a/src/ui/Mobile/Homepage/HomepageAuth.tsx b/src/ui/Mobile/Homepage/HomepageAuth.tsx index 0706a902..2c01c9a8 100644 --- a/src/ui/Mobile/Homepage/HomepageAuth.tsx +++ b/src/ui/Mobile/Homepage/HomepageAuth.tsx @@ -656,7 +656,6 @@ export function HomepageAuth({ {resetStep === "verify" && ( <> - o

{t("auth.enterResetCode")}{" "} diff --git a/src/ui/main-axios.ts b/src/ui/main-axios.ts index 43f37333..ad09b8e7 100644 --- a/src/ui/main-axios.ts +++ b/src/ui/main-axios.ts @@ -173,7 +173,7 @@ function createApiInstance( ); } - if (isElectron) { + if (isElectron()) { config.headers["X-Electron-App"] = "true"; config.headers["User-Agent"] = "Termix-Electron/1.6.0"; } @@ -289,17 +289,22 @@ function createApiInstance( // API INSTANCES // ============================================================================ -const isDev = - process.env.NODE_ENV === "development" && - (window.location.port === "3000" || - window.location.port === "5173" || - window.location.port === ""); +function isDev(): boolean { + return ( + process.env.NODE_ENV === "development" && + (window.location.port === "3000" || + window.location.port === "5173" || + window.location.port === "" || + window.location.hostname === "localhost" || + window.location.hostname === "127.0.0.1") + ); +} let apiHost = import.meta.env.VITE_API_HOST || "localhost"; let apiPort = 8081; let configuredServerUrl: string | null = null; -if (isElectron) { +if (isElectron()) { apiPort = 8081; } @@ -309,7 +314,7 @@ export interface ServerConfig { } export async function getServerConfig(): Promise { - if (!isElectron) return null; + if (!isElectron()) return null; try { const result = await (window as any).electronAPI?.invoke( @@ -323,7 +328,7 @@ export async function getServerConfig(): Promise { } export async function saveServerConfig(config: ServerConfig): Promise { - if (!isElectron) return false; + if (!isElectron()) return false; try { const result = await (window as any).electronAPI?.invoke( @@ -345,7 +350,7 @@ export async function saveServerConfig(config: ServerConfig): Promise { export async function testServerConnection( serverUrl: string, ): Promise<{ success: boolean; error?: string }> { - if (!isElectron) + if (!isElectron()) return { success: false, error: "Not in Electron environment" }; try { @@ -360,7 +365,7 @@ export async function testServerConnection( } } -if (isElectron) { +if (isElectron()) { getServerConfig().then((config) => { if (config?.serverUrl) { configuredServerUrl = config.serverUrl; @@ -370,36 +375,57 @@ if (isElectron) { } function getApiUrl(path: string, defaultPort: number): string { - if (isElectron()) { + if (isDev()) { + return `http://${apiHost}:${defaultPort}${path}`; + } else if (isElectron()) { if (configuredServerUrl) { const baseUrl = configuredServerUrl.replace(/\/$/, ""); return `${baseUrl}${path}`; } return "http://no-server-configured"; - } else if (isDev) { - return `http://${apiHost}:${defaultPort}${path}`; } else { return path; } } +// Initialize API instances +function initializeApiInstances() { + // SSH Host Management API (port 8081) + sshHostApi = createApiInstance(getApiUrl("/ssh", 8081), "SSH_HOST"); + + // Tunnel Management API (port 8083) + tunnelApi = createApiInstance(getApiUrl("/ssh", 8083), "TUNNEL"); + + // File Manager Operations API (port 8084) + fileManagerApi = createApiInstance( + getApiUrl("/ssh/file_manager", 8084), + "FILE_MANAGER", + ); + + // Server Statistics API (port 8085) + statsApi = createApiInstance(getApiUrl("", 8085), "STATS"); + + // Authentication API (port 8081) + authApi = createApiInstance(getApiUrl("", 8081), "AUTH"); +} + // SSH Host Management API (port 8081) -export let sshHostApi = createApiInstance(getApiUrl("/ssh", 8081), "SSH_HOST"); +export let sshHostApi: AxiosInstance; // Tunnel Management API (port 8083) -export let tunnelApi = createApiInstance(getApiUrl("/ssh", 8083), "TUNNEL"); +export let tunnelApi: AxiosInstance; // File Manager Operations API (port 8084) -export let fileManagerApi = createApiInstance( - getApiUrl("/ssh/file_manager", 8084), - "FILE_MANAGER", -); +export let fileManagerApi: AxiosInstance; // Server Statistics API (port 8085) -export let statsApi = createApiInstance(getApiUrl("", 8085), "STATS"); +export let statsApi: AxiosInstance; // Authentication API (port 8081) -export let authApi = createApiInstance(getApiUrl("", 8081), "AUTH"); +export let authApi: AxiosInstance; + +// Initialize API instances immediately +initializeApiInstances(); function updateApiInstances() { systemLogger.info("Updating API instances with new server configuration", { @@ -407,14 +433,7 @@ function updateApiInstances() { configuredServerUrl, }); - sshHostApi = createApiInstance(getApiUrl("/ssh", 8081), "SSH_HOST"); - tunnelApi = createApiInstance(getApiUrl("/ssh", 8083), "TUNNEL"); - fileManagerApi = createApiInstance( - getApiUrl("/ssh/file_manager", 8084), - "FILE_MANAGER", - ); - statsApi = createApiInstance(getApiUrl("", 8085), "STATS"); - authApi = createApiInstance(getApiUrl("", 8081), "AUTH"); + initializeApiInstances(); // Make configuredServerUrl available globally for components that need it (window as any).configuredServerUrl = configuredServerUrl;