diff --git a/surfsense_desktop/src/modules/deep-links.ts b/surfsense_desktop/src/modules/deep-links.ts index 11b7bfcff..635e242fe 100644 --- a/surfsense_desktop/src/modules/deep-links.ts +++ b/surfsense_desktop/src/modules/deep-links.ts @@ -1,7 +1,7 @@ import { app } from 'electron'; import path from 'path'; import { getMainWindow } from './window'; -import { getServerPort } from './server'; +import { getServerOrigin } from './server'; import { trackEvent } from './analytics'; const PROTOCOL = 'surfsense'; @@ -23,7 +23,7 @@ function handleDeepLink(url: string) { }); if (parsed.hostname === 'auth' && parsed.pathname === '/callback') { const params = parsed.searchParams.toString(); - win.loadURL(`http://localhost:${getServerPort()}/auth/callback?${params}`); + win.loadURL(`${getServerOrigin()}/auth/callback?${params}`); } win.show(); diff --git a/surfsense_desktop/src/modules/quick-ask.ts b/surfsense_desktop/src/modules/quick-ask.ts index b31ae1bcd..d7602f470 100644 --- a/surfsense_desktop/src/modules/quick-ask.ts +++ b/surfsense_desktop/src/modules/quick-ask.ts @@ -2,7 +2,7 @@ import { BrowserWindow, clipboard, globalShortcut, ipcMain, screen, shell } from import path from 'path'; import { IPC_CHANNELS } from '../ipc/channels'; import { checkAccessibilityPermission, getFrontmostApp, simulateCopy, simulatePaste } from './platform'; -import { getServerPort } from './server'; +import { getServerOrigin } from './server'; import { getShortcuts } from './shortcuts'; import { getActiveSearchSpaceId } from './active-search-space'; import { trackEvent } from './analytics'; @@ -58,7 +58,7 @@ function createQuickAskWindow(x: number, y: number): BrowserWindow { const spaceId = pendingSearchSpaceId; const route = spaceId ? `/dashboard/${spaceId}/new-chat` : '/dashboard'; - quickAskWindow.loadURL(`http://localhost:${getServerPort()}${route}?quickAssist=true`); + quickAskWindow.loadURL(`${getServerOrigin()}${route}?quickAssist=true`); quickAskWindow.once('ready-to-show', () => { quickAskWindow?.show(); @@ -69,7 +69,7 @@ function createQuickAskWindow(x: number, y: number): BrowserWindow { }); quickAskWindow.webContents.setWindowOpenHandler(({ url }) => { - if (url.startsWith('http://localhost')) { + if (url.startsWith(getServerOrigin())) { return { action: 'allow' }; } shell.openExternal(url); diff --git a/surfsense_desktop/src/modules/server.ts b/surfsense_desktop/src/modules/server.ts index daec9bed0..55753d8fb 100644 --- a/surfsense_desktop/src/modules/server.ts +++ b/surfsense_desktop/src/modules/server.ts @@ -3,6 +3,7 @@ import { app, utilityProcess } from 'electron'; import { getPort } from 'get-port-please'; const isDev = !app.isPackaged; +const SERVER_HOST = '127.0.0.1'; let serverPort = 3000; let nextServerProcess: ReturnType | null = null; @@ -10,6 +11,10 @@ export function getServerPort(): number { return serverPort; } +export function getServerOrigin(): string { + return `http://${SERVER_HOST}:${serverPort}`; +} + function getStandalonePath(): string { if (isDev) { return path.join(__dirname, '..', '..', 'surfsense_web', '.next', 'standalone', 'surfsense_web'); @@ -44,7 +49,7 @@ export async function startNextServer(): Promise { env: { ...process.env, PORT: String(serverPort), - HOSTNAME: '127.0.0.1', + HOSTNAME: SERVER_HOST, NODE_ENV: 'production', }, serviceName: 'SurfSense Next Server', @@ -75,7 +80,7 @@ export async function startNextServer(): Promise { child.once('exit', startupExitHandler); }); - const ready = await Promise.race([waitForServer(`http://localhost:${serverPort}`), exited]); + const ready = await Promise.race([waitForServer(getServerOrigin()), exited]); if (startupExitHandler) { child.removeListener('exit', startupExitHandler); } diff --git a/surfsense_desktop/src/modules/window.ts b/surfsense_desktop/src/modules/window.ts index 7ee7eb8db..153fa0879 100644 --- a/surfsense_desktop/src/modules/window.ts +++ b/surfsense_desktop/src/modules/window.ts @@ -2,7 +2,7 @@ import { app, BrowserWindow, shell, session } from 'electron'; import path from 'path'; import { trackEvent } from './analytics'; import { showErrorDialog } from './errors'; -import { getServerPort } from './server'; +import { getServerOrigin } from './server'; import { setActiveSearchSpaceId } from './active-search-space'; const isDev = !app.isPackaged; @@ -58,10 +58,10 @@ export function createMainWindow(initialPath = '/dashboard'): BrowserWindow { mainWindow?.setTitle(WINDOW_TITLE); }); - mainWindow.loadURL(`http://localhost:${getServerPort()}${initialPath}`); + mainWindow.loadURL(`${getServerOrigin()}${initialPath}`); mainWindow.webContents.setWindowOpenHandler(({ url }) => { - if (url.startsWith('http://localhost')) { + if (url.startsWith(getServerOrigin())) { return { action: 'allow' }; } shell.openExternal(url); @@ -70,7 +70,7 @@ export function createMainWindow(initialPath = '/dashboard'): BrowserWindow { const filter = { urls: [`${HOSTED_FRONTEND_URL}/*`] }; session.defaultSession.webRequest.onBeforeRequest(filter, (details, callback) => { - const rewritten = details.url.replace(HOSTED_FRONTEND_URL, `http://localhost:${getServerPort()}`); + const rewritten = details.url.replace(HOSTED_FRONTEND_URL, getServerOrigin()); callback({ redirectURL: rewritten }); });