Skip to content

Commit

Permalink
proxy: Add proxy details in request module parameters.
Browse files Browse the repository at this point in the history
This commit adds proxy details to request module paramters from
the proxyRules so that the request module can use these rules while
sending a request. In case of no system proxy, set environment
variable NO_PROXY to handle all links.

Fixes: #534.
  • Loading branch information
abhigyank authored and akashnimare committed Aug 20, 2018
1 parent 89c35cb commit 9ba2792
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
5 changes: 4 additions & 1 deletion app/main/linuxupdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { Notification } = require('electron');
const request = require('request');
const semver = require('semver');
const ConfigUtil = require('../renderer/js/utils/config-util');
const ProxyUtil = require('../renderer/js/utils/proxy-util');
const LinuxUpdateUtil = require('../renderer/js/utils/linux-update-util');
const Logger = require('../renderer/js/utils/logger-util');

Expand All @@ -15,10 +16,12 @@ const logger = new Logger({
function linuxUpdateNotification() {
let url = 'https://api.github.com/repos/zulip/zulip-electron/releases';
url = ConfigUtil.getConfigItem('betaUpdate') ? url : url + '/latest';
const proxyEnabled = ConfigUtil.getConfigItem('useManualProxy') || ConfigUtil.getConfigItem('useSystemProxy');

const options = {
url,
headers: {'User-Agent': 'request'}
headers: {'User-Agent': 'request'},
proxy: proxyEnabled ? ProxyUtil.getProxy(url) : ''
};

request(options, (error, response, body) => {
Expand Down
27 changes: 23 additions & 4 deletions app/renderer/js/utils/domain-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const escape = require('escape-html');
const Logger = require('./logger-util');

const CertificateUtil = require(__dirname + '/certificate-util.js');
const ProxyUtil = require(__dirname + '/proxy-util.js');
const ConfigUtil = require(__dirname + '/config-util.js');

const logger = new Logger({
file: `domain-util.log`,
Expand Down Expand Up @@ -119,8 +121,16 @@ class DomainUtil {
logger.warn('Error while trying to get certificate: ' + err);
}
}

const proxyEnabled = ConfigUtil.getConfigItem('useManualProxy') || ConfigUtil.getConfigItem('useSystemProxy');

// If certificate for the domain exists add it as a ca key in the request's parameter else consider only domain as the parameter for request
const checkDomain = (certificateLocation) ? ({url: domain + '/static/audio/zulip.ogg', ca: certificateLocation}) : domain + '/static/audio/zulip.ogg';
// Add proxy as a parameter if it sbeing used.
const checkDomain = {
url: domain + '/static/audio/zulip.ogg',
ca: (certificateLocation) ? certificateLocation : '',
proxy: proxyEnabled ? ProxyUtil.getProxy(domain) : ''
};

const serverConf = {
icon: defaultIconUrl,
Expand Down Expand Up @@ -193,9 +203,13 @@ class DomainUtil {
}

getServerSettings(domain) {
const serverSettingsUrl = domain + '/api/v1/server_settings';
const proxyEnabled = ConfigUtil.getConfigItem('useManualProxy') || ConfigUtil.getConfigItem('useSystemProxy');
const serverSettingsOptions = {
url: domain + '/api/v1/server_settings',
proxy: proxyEnabled ? ProxyUtil.getProxy(domain) : ''
};
return new Promise((resolve, reject) => {
request(serverSettingsUrl, (error, response) => {
request(serverSettingsOptions, (error, response) => {
if (!error && response.statusCode === 200) {
const data = JSON.parse(response.body);
if (data.hasOwnProperty('realm_icon') && data.realm_icon) {
Expand All @@ -215,12 +229,17 @@ class DomainUtil {
}

saveServerIcon(url) {
const proxyEnabled = ConfigUtil.getConfigItem('useManualProxy') || ConfigUtil.getConfigItem('useSystemProxy');
const serverIconOptions = {
url,
proxy: proxyEnabled ? ProxyUtil.getProxy(url) : ''
};
// The save will always succeed. If url is invalid, downgrade to default icon.
return new Promise(resolve => {
const filePath = this.generateFilePath(url);
const file = fs.createWriteStream(filePath);
try {
request(url).on('response', response => {
request(serverIconOptions).on('response', response => {
response.on('error', err => {
logger.log('Could not get server icon.');
logger.log(err);
Expand Down
34 changes: 34 additions & 0 deletions app/renderer/js/utils/proxy-util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const url = require('url');
const ConfigUtil = require('./config-util.js');

let instance = null;
Expand All @@ -15,6 +16,39 @@ class ProxyUtil {
return instance;
}

// Return proxy to be used for a particular uri, to be used for request
getProxy(uri) {
uri = url.parse(uri);
const proxyRules = ConfigUtil.getConfigItem('proxyRules', '').split(';');
// If SPS is on and system uses no proxy then request should not try to use proxy from
// environment. NO_PROXY = '*' makes request ignore all environment proxy variables.
if (proxyRules[0] === '') {
process.env.NO_PROXY = '*';
return;
}

const proxyRule = {};
if (uri.protocol === 'http:') {
proxyRules.forEach(proxy => {
if (proxy.includes('http=')) {
proxyRule.hostname = proxy.split('http=')[1].trim().split(':')[0];
proxyRule.port = proxy.split('http=')[1].trim().split(':')[1];
}
});
return proxyRule;
}

if (uri.protocol === 'https:') {
proxyRules.forEach(proxy => {
if (proxy.includes('https=')) {
proxyRule.hostname = proxy.split('https=')[1].trim().split(':')[0];
proxyRule.port = proxy.split('https=')[1].trim().split(':')[1];
}
});
return proxyRule;
}
}

resolveSystemProxy(mainWindow) {
const page = mainWindow.webContents;
const ses = page.session;
Expand Down

0 comments on commit 9ba2792

Please sign in to comment.