Skip to content

Commit a27cf9e

Browse files
abhigyankakashnimare
authored andcommitted
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.
1 parent 22d6c6a commit a27cf9e

File tree

5 files changed

+162
-20
lines changed

5 files changed

+162
-20
lines changed

app/main/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const { app, ipcMain } = electron;
1313

1414
const BadgeSettings = require('./../renderer/js/pages/preference/badge-settings.js');
1515
const ConfigUtil = require('./../renderer/js/utils/config-util.js');
16+
const ProxyUtil = require('./../renderer/js/utils/proxy-util.js');
1617

1718
// Adds debug features like hotkeys for triggering dev tools and reload
1819
// in development mode
@@ -153,6 +154,12 @@ app.on('ready', () => {
153154
});
154155
mainWindow = createMainWindow();
155156

157+
const isSystemProxy = ConfigUtil.getConfigItem('useSystemProxy');
158+
159+
if (isSystemProxy) {
160+
ProxyUtil.resolveSystemProxy(mainWindow);
161+
}
162+
156163
const page = mainWindow.webContents;
157164

158165
page.on('dom-ready', () => {

app/renderer/css/preference.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ img.server-info-icon {
258258
border: #4EBFAC 2px solid;
259259
}
260260

261-
.setting-block {
262-
width: 100%;
261+
.manual-proxy-block {
262+
width: 96%;
263263
}
264264

265265
.actions-container {

app/renderer/js/main.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,17 @@ class ServerManagerView {
6060

6161
loadProxy() {
6262
return new Promise(resolve => {
63-
const proxyEnabled = ConfigUtil.getConfigItem('useProxy', false);
63+
// To change proxyEnable to useManualProxy in older versions
64+
const proxyEnabledOld = ConfigUtil.isConfigItemExists('useProxy');
65+
if (proxyEnabledOld) {
66+
const proxyEnableOldState = ConfigUtil.getConfigItem('useProxy');
67+
if (proxyEnableOldState) {
68+
ConfigUtil.setConfigItem('useManualProxy', true);
69+
}
70+
ConfigUtil.removeConfigItem('useProxy');
71+
}
72+
73+
const proxyEnabled = ConfigUtil.getConfigItem('useManualProxy') || ConfigUtil.getConfigItem('useSystemProxy');
6474
if (proxyEnabled) {
6575
session.fromPartition('persist:webviewsession').setProxy({
6676
pacScript: ConfigUtil.getConfigItem('proxyPAC', ''),
@@ -84,7 +94,8 @@ class ServerManagerView {
8494
// Default settings which should be respected
8595
const settingOptions = {
8696
trayIcon: true,
87-
useProxy: false,
97+
useManualProxy: false,
98+
useSystemProxy: false,
8899
showSidebar: true,
89100
badgeOption: true,
90101
startAtLogin: false,
@@ -497,6 +508,7 @@ class ServerManagerView {
497508
this.loadProxy().then(() => {
498509
if (showAlert) {
499510
alert('Proxy settings saved!');
511+
ipcRenderer.send('reload-full-app');
500512
}
501513
});
502514
});

app/renderer/js/pages/preference/network-section.js

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ class NetworkSection extends BaseSection {
1616
<div class="settings-pane">
1717
<div class="title">Proxy</div>
1818
<div id="appearance-option-settings" class="settings-card">
19-
<div class="setting-row" id="use-proxy-option">
20-
<div class="setting-description">Connect servers through a proxy</div>
19+
<div class="setting-row" id="use-system-settings">
20+
<div class="setting-description">Use system proxy settings (requires restart)</div>
2121
<div class="setting-control"></div>
2222
</div>
23-
<div class="setting-block">
23+
<div class="setting-row" id="use-manual-settings">
24+
<div class="setting-description">Manual proxy configuration</div>
25+
<div class="setting-control"></div>
26+
</div>
27+
<div class="manual-proxy-block">
2428
<div class="setting-row" id="proxy-pac-option">
2529
<span class="setting-input-key">PAC script</span>
2630
<input class="setting-input-value" placeholder="e.g. foobar.com/pacfile.js"/>
@@ -51,7 +55,7 @@ class NetworkSection extends BaseSection {
5155
this.$proxyRules = document.querySelector('#proxy-rules-option .setting-input-value');
5256
this.$proxyBypass = document.querySelector('#proxy-bypass-option .setting-input-value');
5357
this.$proxySaveAction = document.getElementById('proxy-save-action');
54-
this.$settingBlock = this.props.$root.querySelector('.setting-block');
58+
this.$manualProxyBlock = this.props.$root.querySelector('.manual-proxy-block');
5559
this.initProxyOption();
5660

5761
this.$proxyPAC.value = ConfigUtil.getConfigItem('proxyPAC', '');
@@ -68,31 +72,54 @@ class NetworkSection extends BaseSection {
6872
}
6973

7074
initProxyOption() {
71-
const proxyEnabled = ConfigUtil.getConfigItem('useProxy', false);
72-
this.toggleProxySettings(proxyEnabled);
75+
const manualProxyEnabled = ConfigUtil.getConfigItem('useManualProxy', false);
76+
this.toggleManualProxySettings(manualProxyEnabled);
77+
7378
this.updateProxyOption();
7479
}
7580

76-
toggleProxySettings(option) {
81+
toggleManualProxySettings(option) {
7782
if (option) {
78-
this.$settingBlock.classList.remove('hidden');
83+
this.$manualProxyBlock.classList.remove('hidden');
7984
} else {
80-
this.$settingBlock.classList.add('hidden');
85+
this.$manualProxyBlock.classList.add('hidden');
8186
}
8287
}
8388

8489
updateProxyOption() {
8590
this.generateSettingOption({
86-
$element: document.querySelector('#use-proxy-option .setting-control'),
87-
value: ConfigUtil.getConfigItem('useProxy', false),
91+
$element: document.querySelector('#use-system-settings .setting-control'),
92+
value: ConfigUtil.getConfigItem('useSystemProxy', false),
8893
clickHandler: () => {
89-
const newValue = !ConfigUtil.getConfigItem('useProxy');
90-
ConfigUtil.setConfigItem('useProxy', newValue);
91-
this.toggleProxySettings(newValue);
94+
const newValue = !ConfigUtil.getConfigItem('useSystemProxy');
95+
const manualProxyValue = ConfigUtil.getConfigItem('useManualProxy');
96+
if (manualProxyValue && newValue) {
97+
ConfigUtil.setConfigItem('useManualProxy', !manualProxyValue);
98+
this.toggleManualProxySettings(!manualProxyValue);
99+
}
92100
if (newValue === false) {
93-
// Reload proxy if the proxy is turned off
94-
ipcRenderer.send('forward-message', 'reload-proxy', false);
101+
// Remove proxy system proxy settings
102+
ConfigUtil.setConfigItem('proxyRules', '');
103+
ipcRenderer.send('forward-message', 'reload-proxy', true);
104+
}
105+
ConfigUtil.setConfigItem('useSystemProxy', newValue);
106+
this.updateProxyOption();
107+
}
108+
});
109+
this.generateSettingOption({
110+
$element: document.querySelector('#use-manual-settings .setting-control'),
111+
value: ConfigUtil.getConfigItem('useManualProxy', false),
112+
clickHandler: () => {
113+
const newValue = !ConfigUtil.getConfigItem('useManualProxy');
114+
const systemProxyValue = ConfigUtil.getConfigItem('useSystemProxy');
115+
this.toggleManualProxySettings(newValue);
116+
if (systemProxyValue && newValue) {
117+
ConfigUtil.setConfigItem('useSystemProxy', !systemProxyValue);
95118
}
119+
ConfigUtil.setConfigItem('proxyRules', '');
120+
ConfigUtil.setConfigItem('useManualProxy', newValue);
121+
// Reload app only when turning manual proxy off, hence !newValue
122+
ipcRenderer.send('forward-message', 'reload-proxy', !newValue);
96123
this.updateProxyOption();
97124
}
98125
});
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
'use strict';
2+
3+
const ConfigUtil = require('./config-util.js');
4+
5+
let instance = null;
6+
7+
class ProxyUtil {
8+
constructor() {
9+
if (instance) {
10+
return instance;
11+
} else {
12+
instance = this;
13+
}
14+
15+
return instance;
16+
}
17+
18+
resolveSystemProxy(mainWindow) {
19+
const page = mainWindow.webContents;
20+
const ses = page.session;
21+
const resolveProxyUrl = 'www.google.com';
22+
23+
// Check HTTP Proxy
24+
const httpProxy = new Promise(resolve => {
25+
ses.resolveProxy('http://' + resolveProxyUrl, proxy => {
26+
let httpString = '';
27+
if (proxy !== 'DIRECT') {
28+
// in case of proxy HTTPS url:port, windows gives first word as HTTPS while linux gives PROXY
29+
// for all other HTTP or direct url:port both uses PROXY
30+
if (proxy.includes('PROXY') || proxy.includes('HTTPS')) {
31+
httpString = 'http=' + proxy.split('PROXY')[1] + ';';
32+
}
33+
}
34+
resolve(httpString);
35+
});
36+
});
37+
// Check HTTPS Proxy
38+
const httpsProxy = new Promise(resolve => {
39+
ses.resolveProxy('https://' + resolveProxyUrl, proxy => {
40+
let httpsString = '';
41+
if (proxy !== 'DIRECT' || proxy.includes('HTTPS')) {
42+
// in case of proxy HTTPS url:port, windows gives first word as HTTPS while linux gives PROXY
43+
// for all other HTTP or direct url:port both uses PROXY
44+
if (proxy.includes('PROXY' || proxy.includes('HTTPS'))) {
45+
httpsString += 'https=' + proxy.split('PROXY')[1] + ';';
46+
}
47+
}
48+
resolve(httpsString);
49+
});
50+
});
51+
52+
// Check FTP Proxy
53+
const ftpProxy = new Promise(resolve => {
54+
ses.resolveProxy('ftp://' + resolveProxyUrl, proxy => {
55+
let ftpString = '';
56+
if (proxy !== 'DIRECT') {
57+
if (proxy.includes('PROXY')) {
58+
ftpString += 'ftp=' + proxy.split('PROXY')[1] + ';';
59+
}
60+
}
61+
resolve(ftpString);
62+
});
63+
});
64+
65+
// Check SOCKS Proxy
66+
const socksProxy = new Promise(resolve => {
67+
ses.resolveProxy('socks4://' + resolveProxyUrl, proxy => {
68+
let socksString = '';
69+
if (proxy !== 'DIRECT') {
70+
if (proxy.includes('SOCKS5')) {
71+
socksString += 'socks=' + proxy.split('SOCKS5')[1] + ';';
72+
} else if (proxy.includes('SOCKS4')) {
73+
socksString += 'socks=' + proxy.split('SOCKS4')[1] + ';';
74+
} else if (proxy.includes('PROXY')) {
75+
socksString += 'socks=' + proxy.split('PROXY')[1] + ';';
76+
}
77+
}
78+
resolve(socksString);
79+
});
80+
});
81+
82+
Promise.all([httpProxy, httpsProxy, ftpProxy, socksProxy]).then(values => {
83+
let proxyString = '';
84+
values.forEach(proxy => {
85+
proxyString += proxy;
86+
});
87+
ConfigUtil.setConfigItem('systemProxyRules', proxyString);
88+
const useSystemProxy = ConfigUtil.getConfigItem('useSystemProxy');
89+
if (useSystemProxy) {
90+
ConfigUtil.setConfigItem('proxyRules', proxyString);
91+
}
92+
});
93+
}
94+
}
95+
96+
module.exports = new ProxyUtil();

0 commit comments

Comments
 (0)