diff --git a/src/main/index.ts b/src/main/index.ts index bd413b92..b5522f8c 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -297,10 +297,6 @@ async function createWindow() { mainWindowState.manage(mainWindow) - mainWindow.loadURL(winURL) - - mainWindow.webContents.on('will-navigate', event => event.preventDefault()) - /** * Get system proxy configuration. */ @@ -310,6 +306,17 @@ async function createWindow() { log.info(`System proxy configuration: ${proxyInfo}`) } + /** + * Set proxy for BrowserWindow + */ + const proxyConfig = await proxyConfiguration.forMastodon() + if (proxyConfig) { + await mainWindow.webContents.session.setProxy({ proxyRules: `${proxyConfig.protocol}://${proxyConfig.host}:${proxyConfig.port}` }) + } + mainWindow.loadURL(winURL) + + mainWindow.webContents.on('will-navigate', event => event.preventDefault()) + mainWindow.on('closed', () => { mainWindow = null }) @@ -1042,18 +1049,22 @@ ipcMain.on('get-global-header', (event: IpcMainEvent) => { }) // proxy -ipcMain.on('update-proxy-config', (event: IpcMainEvent, proxy: Proxy) => { +ipcMain.on('update-proxy-config', async (event: IpcMainEvent, proxy: Proxy) => { const preferences = new Preferences(preferencesDBPath) - preferences - .update({ + try { + const conf = await preferences.update({ proxy: proxy }) - .then(conf => { - event.sender.send('response-update-proxy-config', conf) - }) - .catch(err => { - log.error(err) - }) + const proxyConfig = await proxyConfiguration.forMastodon() + if (proxyConfig) { + await mainWindow?.webContents.session.setProxy({ proxyRules: `${proxyConfig.protocol}://${proxyConfig.host}:${proxyConfig.port}` }) + } else { + await mainWindow?.webContents.session.setProxy({}) + } + event.sender.send('response-update-proxy-config', conf) + } catch (err) { + log.error(err) + } }) ipcMain.on('get-proxy-configuration', async (event: IpcMainEvent) => {