proxy: Migration to use async/await.

This is required for the upgrade to Electron 7, which removes the old
callback-based form of these APIs.
This commit is contained in:
Tim Abbott
2020-02-29 20:38:12 -08:00
parent b34bf7236f
commit c4beedf740
2 changed files with 67 additions and 73 deletions

View File

@@ -153,33 +153,31 @@ class ServerManagerView {
}); });
} }
loadProxy(): Promise<boolean> { async loadProxy(): Promise<void> {
return new Promise(resolve => { // To change proxyEnable to useManualProxy in older versions
// To change proxyEnable to useManualProxy in older versions const proxyEnabledOld = ConfigUtil.isConfigItemExists('useProxy');
const proxyEnabledOld = ConfigUtil.isConfigItemExists('useProxy'); if (proxyEnabledOld) {
if (proxyEnabledOld) { const proxyEnableOldState = ConfigUtil.getConfigItem('useProxy');
const proxyEnableOldState = ConfigUtil.getConfigItem('useProxy'); if (proxyEnableOldState) {
if (proxyEnableOldState) { ConfigUtil.setConfigItem('useManualProxy', true);
ConfigUtil.setConfigItem('useManualProxy', true);
}
ConfigUtil.removeConfigItem('useProxy');
} }
ConfigUtil.removeConfigItem('useProxy');
}
const proxyEnabled = ConfigUtil.getConfigItem('useManualProxy') || ConfigUtil.getConfigItem('useSystemProxy'); const proxyEnabled = ConfigUtil.getConfigItem('useManualProxy') || ConfigUtil.getConfigItem('useSystemProxy');
if (proxyEnabled) { if (proxyEnabled) {
session.fromPartition('persist:webviewsession').setProxy({ await session.fromPartition('persist:webviewsession').setProxy({
pacScript: ConfigUtil.getConfigItem('proxyPAC', ''), pacScript: ConfigUtil.getConfigItem('proxyPAC', ''),
proxyRules: ConfigUtil.getConfigItem('proxyRules', ''), proxyRules: ConfigUtil.getConfigItem('proxyRules', ''),
proxyBypassRules: ConfigUtil.getConfigItem('proxyBypass', '') proxyBypassRules: ConfigUtil.getConfigItem('proxyBypass', '')
}, resolve); });
} else { } else {
session.fromPartition('persist:webviewsession').setProxy({ await session.fromPartition('persist:webviewsession').setProxy({
pacScript: '', pacScript: '',
proxyRules: '', proxyRules: '',
proxyBypassRules: '' proxyBypassRules: ''
}, resolve); });
} }
});
} }
// Settings are initialized only when user clicks on General/Server/Network section settings // Settings are initialized only when user clicks on General/Server/Network section settings

View File

@@ -67,63 +67,59 @@ class ProxyUtil {
const resolveProxyUrl = 'www.example.com'; const resolveProxyUrl = 'www.example.com';
// Check HTTP Proxy // Check HTTP Proxy
const httpProxy = new Promise(resolve => { const httpProxy = (async () => {
ses.resolveProxy('http://' + resolveProxyUrl, (proxy: string) => { const proxy = await ses.resolveProxy('http://' + resolveProxyUrl);
let httpString = ''; let httpString = '';
if (proxy !== 'DIRECT') { if (proxy !== 'DIRECT') {
// in case of proxy HTTPS url:port, windows gives first word as HTTPS while linux gives PROXY // in case of proxy HTTPS url:port, windows gives first word as HTTPS while linux gives PROXY
// for all other HTTP or direct url:port both uses PROXY // for all other HTTP or direct url:port both uses PROXY
if (proxy.includes('PROXY') || proxy.includes('HTTPS')) { if (proxy.includes('PROXY') || proxy.includes('HTTPS')) {
httpString = 'http=' + proxy.split('PROXY')[1] + ';'; httpString = 'http=' + proxy.split('PROXY')[1] + ';';
}
} }
resolve(httpString); }
}); return httpString;
}); })();
// Check HTTPS Proxy // Check HTTPS Proxy
const httpsProxy = new Promise(resolve => { const httpsProxy = (async () => {
ses.resolveProxy('https://' + resolveProxyUrl, (proxy: string) => { const proxy = await ses.resolveProxy('https://' + resolveProxyUrl);
let httpsString = ''; let httpsString = '';
if (proxy !== 'DIRECT' || proxy.includes('HTTPS')) { if (proxy !== 'DIRECT' || proxy.includes('HTTPS')) {
// in case of proxy HTTPS url:port, windows gives first word as HTTPS while linux gives PROXY // in case of proxy HTTPS url:port, windows gives first word as HTTPS while linux gives PROXY
// for all other HTTP or direct url:port both uses PROXY // for all other HTTP or direct url:port both uses PROXY
if (proxy.includes('PROXY') || proxy.includes('HTTPS')) { if (proxy.includes('PROXY') || proxy.includes('HTTPS')) {
httpsString += 'https=' + proxy.split('PROXY')[1] + ';'; httpsString += 'https=' + proxy.split('PROXY')[1] + ';';
}
} }
resolve(httpsString); }
}); return httpsString;
}); })();
// Check FTP Proxy // Check FTP Proxy
const ftpProxy = new Promise(resolve => { const ftpProxy = (async () => {
ses.resolveProxy('ftp://' + resolveProxyUrl, (proxy: string) => { const proxy = await ses.resolveProxy('ftp://' + resolveProxyUrl);
let ftpString = ''; let ftpString = '';
if (proxy !== 'DIRECT') { if (proxy !== 'DIRECT') {
if (proxy.includes('PROXY')) { if (proxy.includes('PROXY')) {
ftpString += 'ftp=' + proxy.split('PROXY')[1] + ';'; ftpString += 'ftp=' + proxy.split('PROXY')[1] + ';';
}
} }
resolve(ftpString); }
}); return ftpString;
}); })();
// Check SOCKS Proxy // Check SOCKS Proxy
const socksProxy = new Promise(resolve => { const socksProxy = (async () => {
ses.resolveProxy('socks4://' + resolveProxyUrl, (proxy: string) => { const proxy = await ses.resolveProxy('socks4://' + resolveProxyUrl);
let socksString = ''; let socksString = '';
if (proxy !== 'DIRECT') { if (proxy !== 'DIRECT') {
if (proxy.includes('SOCKS5')) { if (proxy.includes('SOCKS5')) {
socksString += 'socks=' + proxy.split('SOCKS5')[1] + ';'; socksString += 'socks=' + proxy.split('SOCKS5')[1] + ';';
} else if (proxy.includes('SOCKS4')) { } else if (proxy.includes('SOCKS4')) {
socksString += 'socks=' + proxy.split('SOCKS4')[1] + ';'; socksString += 'socks=' + proxy.split('SOCKS4')[1] + ';';
} else if (proxy.includes('PROXY')) { } else if (proxy.includes('PROXY')) {
socksString += 'socks=' + proxy.split('PROXY')[1] + ';'; socksString += 'socks=' + proxy.split('PROXY')[1] + ';';
}
} }
resolve(socksString); }
}); return socksString;
}); })();
Promise.all([httpProxy, httpsProxy, ftpProxy, socksProxy]).then(values => { Promise.all([httpProxy, httpsProxy, ftpProxy, socksProxy]).then(values => {
let proxyString = ''; let proxyString = '';