-
-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
proxy-setting: Feature to use system proxy settings.
This PR uses resolveProxy to read system proxy settings and store them in proper proxy format string using ConfigUtil. It removes the previous use proxy option and replaces it with use system proxy and manual proxy options. Fixes: #296.
- Loading branch information
1 parent
22d6c6a
commit a27cf9e
Showing
5 changed files
with
162 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
'use strict'; | ||
|
||
const ConfigUtil = require('./config-util.js'); | ||
|
||
let instance = null; | ||
|
||
class ProxyUtil { | ||
constructor() { | ||
if (instance) { | ||
return instance; | ||
} else { | ||
instance = this; | ||
} | ||
|
||
return instance; | ||
} | ||
|
||
resolveSystemProxy(mainWindow) { | ||
const page = mainWindow.webContents; | ||
const ses = page.session; | ||
const resolveProxyUrl = 'www.google.com'; | ||
|
||
// Check HTTP Proxy | ||
const httpProxy = new Promise(resolve => { | ||
ses.resolveProxy('http://' + resolveProxyUrl, proxy => { | ||
let httpString = ''; | ||
if (proxy !== 'DIRECT') { | ||
// 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 | ||
if (proxy.includes('PROXY') || proxy.includes('HTTPS')) { | ||
httpString = 'http=' + proxy.split('PROXY')[1] + ';'; | ||
} | ||
} | ||
resolve(httpString); | ||
}); | ||
}); | ||
// Check HTTPS Proxy | ||
const httpsProxy = new Promise(resolve => { | ||
ses.resolveProxy('https://' + resolveProxyUrl, proxy => { | ||
let httpsString = ''; | ||
if (proxy !== 'DIRECT' || proxy.includes('HTTPS')) { | ||
// 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 | ||
if (proxy.includes('PROXY' || proxy.includes('HTTPS'))) { | ||
httpsString += 'https=' + proxy.split('PROXY')[1] + ';'; | ||
} | ||
} | ||
resolve(httpsString); | ||
}); | ||
}); | ||
|
||
// Check FTP Proxy | ||
const ftpProxy = new Promise(resolve => { | ||
ses.resolveProxy('ftp://' + resolveProxyUrl, proxy => { | ||
let ftpString = ''; | ||
if (proxy !== 'DIRECT') { | ||
if (proxy.includes('PROXY')) { | ||
ftpString += 'ftp=' + proxy.split('PROXY')[1] + ';'; | ||
} | ||
} | ||
resolve(ftpString); | ||
}); | ||
}); | ||
|
||
// Check SOCKS Proxy | ||
const socksProxy = new Promise(resolve => { | ||
ses.resolveProxy('socks4://' + resolveProxyUrl, proxy => { | ||
let socksString = ''; | ||
if (proxy !== 'DIRECT') { | ||
if (proxy.includes('SOCKS5')) { | ||
socksString += 'socks=' + proxy.split('SOCKS5')[1] + ';'; | ||
} else if (proxy.includes('SOCKS4')) { | ||
socksString += 'socks=' + proxy.split('SOCKS4')[1] + ';'; | ||
} else if (proxy.includes('PROXY')) { | ||
socksString += 'socks=' + proxy.split('PROXY')[1] + ';'; | ||
} | ||
} | ||
resolve(socksString); | ||
}); | ||
}); | ||
|
||
Promise.all([httpProxy, httpsProxy, ftpProxy, socksProxy]).then(values => { | ||
let proxyString = ''; | ||
values.forEach(proxy => { | ||
proxyString += proxy; | ||
}); | ||
ConfigUtil.setConfigItem('systemProxyRules', proxyString); | ||
const useSystemProxy = ConfigUtil.getConfigItem('useSystemProxy'); | ||
if (useSystemProxy) { | ||
ConfigUtil.setConfigItem('proxyRules', proxyString); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
module.exports = new ProxyUtil(); |