From 87303ba4577fa022bd021ec9032aee74994fd967 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Tue, 30 Jun 2020 01:08:18 +0900 Subject: [PATCH] refs #1542 Set proxy config for BrowserWindow --- src/main/index.ts | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) 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) => {