Skip to content

Commit b93d144

Browse files
authored
Proxy for Plugins fix (#7655)
1 parent 0874ec5 commit b93d144

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

pluginHandler.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,8 @@ module.exports.pluginHandler = function (parent) {
252252
var http = (configUrl.indexOf('https://') >= 0) ? require('https') : require('http');
253253
if (configUrl.indexOf('://') === -1) reject("Unable to fetch the config: Bad URL (" + configUrl + ")");
254254
var options = require('url').parse(configUrl);
255-
if (typeof parent.config.settings.plugins.proxy == 'string') { // Proxy support
256-
const HttpsProxyAgent = require('https-proxy-agent');
257-
options.agent = new HttpsProxyAgent(require('url').parse(parent.config.settings.plugins.proxy));
255+
if (typeof parent.config.settings.plugins.proxy == 'string' || process.env['HTTP_PROXY'] || process.env['HTTPS_PROXY'] || process.env['http_proxy'] || process.env['https_proxy']) { // Proxy support
256+
options.agent = new (require('https-proxy-agent').HttpsProxyAgent)(require('url').parse(parent.config.settings.plugins.proxy) || process.env['HTTP_PROXY'] || process.env['HTTPS_PROXY'] || process.env['http_proxy'] || process.env['https_proxy']);
258257
}
259258
http.get(options, function (res) {
260259
var configStr = '';
@@ -276,7 +275,6 @@ module.exports.pluginHandler = function (parent) {
276275
reject("Error getting plugin config. Check that you have valid JSON.");
277276
}
278277
});
279-
280278
}).on('error', function (e) {
281279
reject("Error getting plugin config: " + e.message);
282280
});
@@ -437,16 +435,17 @@ module.exports.pluginHandler = function (parent) {
437435
followRedirects: true,
438436
method: 'GET'
439437
};
440-
if (typeof parent.config.settings.plugins.proxy == 'string') { // Proxy support
441-
const HttpsProxyAgent = require('https-proxy-agent');
442-
opts.agent = new HttpsProxyAgent(require('url').parse(parent.config.settings.plugins.proxy));
438+
if (typeof parent.config.settings.plugins.proxy == 'string' || process.env['HTTP_PROXY'] || process.env['HTTPS_PROXY'] || process.env['http_proxy'] || process.env['https_proxy']) { // Proxy support
439+
opts.agent = new (require('https-proxy-agent').HttpsProxyAgent)(require('url').parse(parent.config.settings.plugins.proxy) || process.env['HTTP_PROXY'] || process.env['HTTPS_PROXY'] || process.env['http_proxy'] || process.env['https_proxy']);
443440
}
441+
var done = false;
444442
var request = http.get(opts, function (response) {
445443
// handle redirections with grace
446444
if (response.headers.location) {
447445
file.close(() => obj.fs.unlink(fileName, () => {}));
448446
return obj.installPlugin(id, version_only, response.headers.location, func);
449447
}
448+
if ((response.statusCode != null) && (response.statusCode >= 400)) { return console.log('Error downloading plugin: HTTP ' + response.statusCode); }
450449
response.pipe(file);
451450
file.on('finish', function () {
452451
file.close(function () {
@@ -486,11 +485,11 @@ module.exports.pluginHandler = function (parent) {
486485
});
487486
zipfile.on('end', function () {
488487
setTimeout(function () {
489-
obj.fs.unlinkSync(fileName);
488+
try { obj.fs.unlinkSync(fileName); } catch (ex) { }
490489
if (version_only == null || version_only === false) {
491-
parent.db.setPluginStatus(id, 1, func);
490+
parent.db.setPluginStatus(id, 1, function () { if (done) return; done = true; if (typeof func == 'function') { func(null); } });
492491
} else {
493-
parent.db.updatePlugin(id, { status: 1, version: version_only.name }, func);
492+
parent.db.updatePlugin(id, { status: 1, version: version_only.name }, function () { if (done) return; done = true; if (typeof func == 'function') { func(null); } });
494493
}
495494
try {
496495
obj.plugins[plugin.shortName] = require(obj.pluginPath + '/' + plugin.shortName + '/' + plugin.shortName + '.js')[plugin.shortName](obj);
@@ -505,10 +504,13 @@ module.exports.pluginHandler = function (parent) {
505504
parent.updateMeshCore();
506505
});
507506
});
507+
zipfile.on('error', function (e) { console.log('Error extracting plugin ZIP: ' + e.message); });
508508
});
509509
});
510510
});
511511
});
512+
request.on('error', function (e) { console.log('Error downloading plugin: ' + e.message); });
513+
request.setTimeout(30000, function () { request.destroy(new Error('Timed out while downloading plugin')); });
512514
} else if (plugin.repository.type == 'npm') {
513515
// @TODO npm support? (need a test plugin)
514516
}
@@ -532,9 +534,8 @@ module.exports.pluginHandler = function (parent) {
532534
'Accept': 'application/vnd.github.v3+json'
533535
}
534536
};
535-
if (typeof parent.config.settings.plugins.proxy == 'string') { // Proxy support
536-
const HttpsProxyAgent = require('https-proxy-agent');
537-
options.agent = new HttpsProxyAgent(require('url').parse(parent.config.settings.plugins.proxy));
537+
if (typeof parent.config.settings.plugins.proxy == 'string' || process.env['HTTP_PROXY'] || process.env['HTTPS_PROXY'] || process.env['http_proxy'] || process.env['https_proxy']) { // Proxy support
538+
options.agent = new (require('https-proxy-agent').HttpsProxyAgent)(require('url').parse(parent.config.settings.plugins.proxy) || process.env['HTTP_PROXY'] || process.env['HTTPS_PROXY'] || process.env['http_proxy'] || process.env['https_proxy']);
538539
}
539540
http.get(opts, function (res) {
540541
var versStr = '';

0 commit comments

Comments
 (0)