Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix(desktop): route update checks through the app's proxy (#1432) (#1433)

Tashfeend94b7655108c
diff --git a/desktop/src/__tests__/update-support.test.ts b/desktop/src/__tests__/update-support.test.ts
index 4f567c2d..9cb8f772 100644
--- a/desktop/src/__tests__/update-support.test.ts
+++ b/desktop/src/__tests__/update-support.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
-import { updateSupport } from '../update-support.js';
+import { updateSupport, updaterProxy } from '../update-support.js';
const base = { isPackaged: true, execPath: '/Applications/FreeLLMAPI.app/Contents/MacOS/FreeLLMAPI', productName: 'FreeLLMAPI' };
@@ -27,3 +27,23 @@ describe('updateSupport', () => {
expect(updateSupport({ ...base, platform: 'linux', execPath: '/opt/FreeLLMAPI/freellmapi-desktop' })).toEqual({ phase: 'unsupported', reason: 'package' });
});
});
+
+describe('updaterProxy', () => {
+ it('leaves the session on system settings when no proxy is set', () => {
+ expect(updaterProxy('')).toBeNull();
+ expect(updaterProxy('not a url')).toBeNull();
+ expect(updaterProxy('ftp://proxy:21')).toBeNull();
+ });
+
+ it('turns the app proxy into Chromium proxy rules', () => {
+ expect(updaterProxy('http://127.0.0.1:7890')).toEqual({ rules: 'http://127.0.0.1:7890', username: '', password: '' });
+ expect(updaterProxy('http://proxy.lan')).toEqual({ rules: 'http://proxy.lan:80', username: '', password: '' });
+ expect(updaterProxy('socks5h://127.0.0.1:1080')?.rules).toBe('socks5://127.0.0.1:1080');
+ expect(updaterProxy('socks4a://10.0.0.1:1080')?.rules).toBe('socks4://10.0.0.1:1080');
+ expect(updaterProxy('http://[::1]:3128')?.rules).toBe('http://[::1]:3128');
+ });
+
+ it('splits the credentials off for the login event', () => {
+ expect(updaterProxy('http://me:p%[email protected]:3128')).toEqual({ rules: 'http://proxy.lan:3128', username: 'me', password: 'p@ss' });
+ });
+});
diff --git a/desktop/src/main.ts b/desktop/src/main.ts
index a482742b..4be37570 100644
--- a/desktop/src/main.ts
+++ b/desktop/src/main.ts
@@ -1,7 +1,7 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { app, dialog, ipcMain, clipboard, nativeTheme, screen, shell, type Tray } from 'electron';
-import { startServer, ensureSessionToken, getUnifiedApiKey, isAutoUpdateCheckEnabled, backupBeforeUpdate } from './server.mjs';
+import { startServer, ensureSessionToken, getUnifiedApiKey, isAutoUpdateCheckEnabled, backupBeforeUpdate, outboundProxyUrl } from './server.mjs';
import { loadConfig, saveConfig } from './config.js';
import { installFileLogger } from './logger.js';
import { buildTray, refreshTrayLocale } from './tray.js';
@@ -281,6 +281,9 @@ if (!app.requestSingleInstanceLock()) {
try { return isAutoUpdateCheckEnabled(); } catch { return false; }
},
backupBeforeUpdate,
+ outboundProxyUrl: () => {
+ try { return outboundProxyUrl(); } catch { return ''; }
+ },
getLocale: () => locale,
});
diff --git a/desktop/src/server-host.ts b/desktop/src/server-host.ts
index 2d768254..27ec8d0e 100644
--- a/desktop/src/server-host.ts
+++ b/desktop/src/server-host.ts
@@ -12,7 +12,7 @@ import tls from 'node:tls';
import type { Server } from 'node:http';
import { createApp } from '../../server/src/app.js';
import { initDb, getDb, getUnifiedApiKey } from '../../server/src/db/index.js';
-import { restoreProxySettings, flushProxyCache } from '../../server/src/lib/proxy.js';
+import { restoreProxySettings, flushProxyCache, getProxyMode, getProxyUrl, isProxyActive } from '../../server/src/lib/proxy.js';
import { startHealthChecker, checkAllKeys } from '../../server/src/services/health.js';
import { startCatalogSync } from '../../server/src/services/catalog-sync.js';
import { startCooldownProbe } from '../../server/src/services/cooldown-probe.js';
@@ -32,6 +32,13 @@ export { getDb, getUnifiedApiKey, isAutoUpdateCheckEnabled };
// Desktop updater → a full dump before an update installs, listed on the
// Backups page as "Pre-update". Returns the file it wrote.
+/** The forward proxy the server's own requests use right now, or '' — read
+ * by the updater before every check so it leaves through the same route
+ * (#1432). A Fetch Relay is not a proxy Chromium can speak, so it is skipped. */
+export function outboundProxyUrl(): string {
+ return isProxyActive() && getProxyMode() === 'forward' ? getProxyUrl() : '';
+}
+
export function backupBeforeUpdate(): string {
return createBackup(getDb(), { source: 'pre-update' }).filename;
}
diff --git a/desktop/src/server.d.mts b/desktop/src/server.d.mts
index 43e8d346..6da0e775 100644
--- a/desktop/src/server.d.mts
+++ b/desktop/src/server.d.mts
@@ -22,3 +22,4 @@ export function getDb(): Database.Database;
export function getUnifiedApiKey(): string;
export function isAutoUpdateCheckEnabled(): boolean;
export function backupBeforeUpdate(): string;
+export function outboundProxyUrl(): string;
diff --git a/desktop/src/update-support.ts b/desktop/src/update-support.ts
index 087d60f5..46f1f2a7 100644
--- a/desktop/src/update-support.ts
+++ b/desktop/src/update-support.ts
@@ -39,3 +39,34 @@ export function updateSupport(
return { phase: 'unsupported', reason: 'package' };
}
+
+/**
+ * Turn the app's outbound proxy URL into what Electron's session.setProxy
+ * takes (#1432). The updater runs on Chromium's network stack, which never
+ * reads the dashboard proxy or HTTPS_PROXY, so on a network that only lets
+ * traffic out through that proxy every check died with net::ERR_ABORTED while
+ * curl (and the rest of the app) went through fine. Chromium takes the
+ * credentials separately, through the updater's 'login' event, so they are
+ * split off here. Returns null for an empty or unparseable URL, which leaves
+ * the session on the system proxy settings.
+ */
+export function updaterProxy(url: string): { rules: string; username: string; password: string } | null {
+ if (!url) return null;
+ let parsed: URL;
+ try {
+ parsed = new URL(url);
+ } catch {
+ return null;
+ }
+ // Chromium knows socks4/socks5 and always resolves names through a SOCKS
+ // proxy, so the "a"/"h" (remote DNS) spellings map onto the plain ones.
+ const scheme = ({ 'socks5h:': 'socks5', 'socks:': 'socks5', 'socks4a:': 'socks4' } as Record<string, string>)[parsed.protocol]
+ ?? parsed.protocol.slice(0, -1);
+ if (!['http', 'https', 'socks4', 'socks5'].includes(scheme) || !parsed.hostname) return null;
+ const port = parsed.port || (scheme === 'https' ? '443' : scheme === 'http' ? '80' : '1080');
+ return {
+ rules: `${scheme}://${parsed.host.replace(/:\d+$/, '')}:${port}`,
+ username: decodeURIComponent(parsed.username),
+ password: decodeURIComponent(parsed.password),
+ };
+}
diff --git a/desktop/src/updater.ts b/desktop/src/updater.ts
index 6b01200c..060ed673 100644
--- a/desktop/src/updater.ts
+++ b/desktop/src/updater.ts
@@ -15,7 +15,7 @@
import { app, BrowserWindow, dialog, Notification, shell } from 'electron';
import electronUpdater from 'electron-updater';
import { dt, type NativeLocale } from './i18n.js';
-import { updateSupport, type UpdateState } from './update-support.js';
+import { updateSupport, updaterProxy, type UpdateState } from './update-support.js';
export type { UpdateState };
@@ -32,6 +32,9 @@ export interface UpdaterHooks {
* install. Synchronous so it can also run inside 'before-quit'. */
backupBeforeUpdate: () => string | null;
getLocale: () => NativeLocale;
+ /** The proxy URL the app's own requests use ('' for none); applied to the
+ * updater's session before every check and download (#1432). */
+ outboundProxyUrl: () => string;
/** Called on every state change, e.g. so the tray can relabel itself. */
onChange?: (state: UpdateState) => void;
}
@@ -48,6 +51,10 @@ let stage: 'check' | 'download' | 'install' = 'check';
// Set once the pre-install backup for the downloaded update has been written,
// so the Restart button and the quit that follows don't write it twice.
let backedUp = false;
+// What useAppProxy last handed the session, so an unchanged setting is not
+// re-applied (setProxy also drops the session's open connections).
+let appliedProxy: string | null = null;
+let proxyCredentials = { username: '', password: '' };
export function getUpdateState(): UpdateState {
return state;
@@ -61,6 +68,21 @@ function setState(next: UpdateState): void {
hooks?.onChange?.(state);
}
+/** Route the updater through the dashboard / env proxy when one is set, and
+ * back to the system settings when it is cleared. */
+async function useAppProxy(): Promise<void> {
+ const url = hooks?.outboundProxyUrl() ?? '';
+ if (url === appliedProxy) return;
+ const proxy = updaterProxy(url);
+ try {
+ await autoUpdater.netSession.setProxy(proxy ? { proxyRules: proxy.rules } : { mode: 'system' });
+ proxyCredentials = { username: proxy?.username ?? '', password: proxy?.password ?? '' };
+ appliedProxy = url;
+ } catch (err) {
+ console.warn('[updater] could not apply the proxy to the update session:', err);
+ }
+}
+
function message(err: unknown): string {
const text = err instanceof Error ? err.message : String(err);
// electron-updater errors can carry a whole HTTP response; the first line is
@@ -113,6 +135,13 @@ export function initUpdater(options: UpdaterHooks): void {
}).show();
}
});
+ // Chromium asks for proxy credentials here rather than reading them from
+ // the proxy rules.
+ autoUpdater.on('login', (authInfo, callback) => {
+ if (authInfo.isProxy && proxyCredentials.username) callback(proxyCredentials.username, proxyCredentials.password);
+ // No arguments cancels the challenge, which Chromium reports as a 407.
+ else (callback as () => void)();
+ });
autoUpdater.on('error', (err) => setState({ phase: 'error', during: stage, message: message(err) }));
// A downloaded update also installs on an ordinary quit (autoInstallOnAppQuit);
@@ -142,7 +171,8 @@ export function checkForUpdates(): Promise<UpdateState> {
return Promise.resolve(state);
}
if (!inFlight) stage = 'check';
- inFlight ??= autoUpdater.checkForUpdates()
+ inFlight ??= useAppProxy()
+ .then(() => autoUpdater.checkForUpdates())
.then(() => state)
.catch((err) => {
setState({ phase: 'error', during: 'check', message: message(err) });
@@ -157,6 +187,7 @@ export async function downloadUpdate(): Promise<UpdateState> {
stage = 'download';
setState({ phase: 'downloading', version: state.version, percent: 0 });
try {
+ await useAppProxy();
await autoUpdater.downloadUpdate();
} catch (err) {
setState({ phase: 'error', during: 'download', message: message(err) });