refs #1542 Set proxy config for BrowserWindow

This commit is contained in:
AkiraFukushima 2020-06-30 01:08:18 +09:00
parent e52b29b363
commit 87303ba457
1 changed files with 24 additions and 13 deletions

View File

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