domain-util: add requestOptions to replace request instances.

This simplifies the request instances.

Fixes #603.
This commit is contained in:
ViPuL
2019-04-02 16:11:23 +05:30
committed by Akash Nimare
parent 54a6903236
commit fefb7247d8
2 changed files with 66 additions and 64 deletions

View File

@@ -9,10 +9,7 @@ const escape = require('escape-html');
const Logger = require('./logger-util'); const Logger = require('./logger-util');
const CertificateUtil = require(__dirname + '/certificate-util.js'); const RequestUtil = require(__dirname + '/../utils/request-util.js');
const ProxyUtil = require(__dirname + '/proxy-util.js');
const ConfigUtil = require(__dirname + '/config-util.js');
const SystemUtil = require(__dirname + '/../utils/system-util.js');
const logger = new Logger({ const logger = new Logger({
file: `domain-util.log`, file: `domain-util.log`,
@@ -113,30 +110,9 @@ class DomainUtil {
} }
domain = this.formatUrl(domain); domain = this.formatUrl(domain);
const certificate = CertificateUtil.getCertificate(encodeURIComponent(domain));
let certificateLocation = '';
if (certificate) {
// To handle case where certificate has been moved from the location in certificates.json
try {
certificateLocation = fs.readFileSync(certificate);
} catch (err) {
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
// Add proxy as a parameter if it sbeing used.
const checkDomain = { const checkDomain = {
url: domain + '/static/audio/zulip.ogg', url: domain + '/static/audio/zulip.ogg',
ca: (certificateLocation) ? certificateLocation : '', ...RequestUtil.requestOptions(domain, ignoreCerts)
proxy: proxyEnabled ? ProxyUtil.getProxy(domain) : '',
ecdhCurve: 'auto',
headers: { 'User-Agent': SystemUtil.getUserAgent() },
rejectUnauthorized: !ignoreCerts
}; };
const serverConf = { const serverConf = {
@@ -213,26 +189,9 @@ class DomainUtil {
} }
getServerSettings(domain, ignoreCerts = false) { getServerSettings(domain, ignoreCerts = false) {
const certificate = CertificateUtil.getCertificate(encodeURIComponent(domain));
let certificateLocation = '';
if (certificate) {
// To handle case where certificate has been moved from the location in certificates.json
try {
certificateLocation = fs.readFileSync(certificate);
} catch (err) {
logger.warn('Error while trying to get certificate: ' + err);
}
}
const proxyEnabled = ConfigUtil.getConfigItem('useManualProxy') || ConfigUtil.getConfigItem('useSystemProxy');
const serverSettingsOptions = { const serverSettingsOptions = {
url: domain + '/api/v1/server_settings', url: domain + '/api/v1/server_settings',
ca: (certificateLocation) ? certificateLocation : '', ...RequestUtil.requestOptions(domain, ignoreCerts)
proxy: proxyEnabled ? ProxyUtil.getProxy(domain) : '',
ecdhCurve: 'auto',
headers: { 'User-Agent': SystemUtil.getUserAgent() },
rejectUnauthorized: !ignoreCerts
}; };
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@@ -260,28 +219,9 @@ class DomainUtil {
const url = server.icon; const url = server.icon;
const domain = server.url; const domain = server.url;
const certificate = CertificateUtil.getCertificate(encodeURIComponent(domain));
let certificateLocation = '';
if (certificate) {
// To handle case where certificate has been moved from the location in certificates.json
try {
certificateLocation = fs.readFileSync(certificate);
} catch (err) {
logger.warn('Error while trying to get certificate: ' + err);
}
}
const proxyEnabled = ConfigUtil.getConfigItem('useManualProxy') || ConfigUtil.getConfigItem('useSystemProxy');
// Add proxy and certificate as a parameter if its being used.
const serverIconOptions = { const serverIconOptions = {
url, url,
ca: (certificateLocation) ? certificateLocation : '', ...RequestUtil.requestOptions(domain, ignoreCerts)
proxy: proxyEnabled ? ProxyUtil.getProxy(url) : '',
ecdhCurve: 'auto',
headers: { 'User-Agent': SystemUtil.getUserAgent() },
rejectUnauthorized: !ignoreCerts
}; };
// The save will always succeed. If url is invalid, downgrade to default icon. // The save will always succeed. If url is invalid, downgrade to default icon.

View File

@@ -0,0 +1,62 @@
const fs = require('fs');
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 SystemUtil = require(__dirname + '/../utils/system-util.js');
const logger = new Logger({
file: `request-util.log`,
timestamp: true
});
let instance = null;
class RequestUtil {
constructor() {
if (!instance) {
instance = this;
}
return instance;
}
// ignoreCerts parameter helps in fetching server icon and
// other server details when user chooses to ignore certificate warnings
requestOptions(domain, ignoreCerts) {
domain = this.formatUrl(domain);
const certificate = CertificateUtil.getCertificate(
encodeURIComponent(domain)
);
let certificateLocation = '';
if (certificate) {
// To handle case where certificate has been moved from the location in certificates.json
try {
certificateLocation = fs.readFileSync(certificate);
} catch (err) {
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
// Add proxy as a parameter if it is being used.
return {
ca: certificateLocation ? certificateLocation : '',
proxy: proxyEnabled ? ProxyUtil.getProxy(domain) : '',
ecdhCurve: 'auto',
headers: { 'User-Agent': SystemUtil.getUserAgent() },
rejectUnauthorized: !ignoreCerts
};
}
formatUrl(domain) {
const hasPrefix = (domain.indexOf('http') === 0);
if (hasPrefix) {
return domain;
} else {
return (domain.indexOf('localhost:') >= 0) ? `http://${domain}` : `https://${domain}`;
}
}
}
module.exports = new RequestUtil();