Skip to content

Commit

Permalink
feat: improve proxy configuration handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kangfenmao committed Nov 23, 2024
1 parent 2e34b79 commit ad625b2
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/main/ipc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path'

import { ThemeMode } from '@types'
import { BrowserWindow, ipcMain, session, shell } from 'electron'
import { BrowserWindow, ipcMain, ProxyConfig, session, shell } from 'electron'
import log from 'electron-log'

import { titleBarOverlayDark, titleBarOverlayLight } from './config'
Expand Down Expand Up @@ -30,20 +30,12 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) {
}))

ipcMain.handle('app:proxy', async (_, proxy: string) => {
if (proxy === 'system') {
await session.defaultSession.setProxy({ mode: 'system' })
const webviewSession = session.fromPartition('persist:webview')
await webviewSession.setProxy({ mode: 'system' })
} else if (proxy) {
await session.defaultSession.setProxy({ proxyRules: proxy })
const webviewSession = session.fromPartition('persist:webview')
await webviewSession.setProxy({ proxyRules: proxy })
} else {
await session.defaultSession.setProxy({})
const webviewSession = session.fromPartition('persist:webview')
await webviewSession.setProxy({})
}
const sessions = [session.defaultSession, session.fromPartition('persist:webview')]
const proxyConfig: ProxyConfig =
proxy === 'system' ? { mode: 'system' } : proxy ? { proxyRules: proxy } : { mode: 'direct' }
await Promise.all(sessions.map((session) => session.setProxy(proxyConfig)))
})

ipcMain.handle('app:reload', () => mainWindow.reload())
ipcMain.handle('open:website', (_, url: string) => shell.openExternal(url))

Expand Down

0 comments on commit ad625b2

Please sign in to comment.