Skip to content

Commit

Permalink
Merge pull request #93 from 1000TurquoisePogs/bugfix/proxying
Browse files Browse the repository at this point in the history
Proxy fixes
  • Loading branch information
1000TurquoisePogs authored Apr 29, 2019
2 parents 16f1e61 + 8fc0caf commit ec52b51
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
29 changes: 19 additions & 10 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,26 @@ function makeSimpleProxy(host, port, options, pluginID, serviceName) {
const headers = res2.headers;
for (const header of Object.keys(headers)) {
if (header == 'location') {
var pattern = /^http.+\/ZLUX\/plugins\/.+/;
const location = headers[header];
let pattern = /^http.+\/ZLUX\/plugins\/.+/;
if (!pattern.test(headers[header])) {
/* Find :{port} and take all that is after */
var x = headers[header];
var y = x.split(":");
var z = y[2];
var t = z.indexOf('/');
var newEnd = z.substring(t);
var newRedirect = req1.protocol + '://' + req1.get('host') + "/ZLUX/plugins/" + pluginID + "/services/" + serviceName + newEnd;
proxyLog.debug('Redirecting to: ' + newRedirect);
res1.set(header, newRedirect);
if (location.startsWith('/')) {
res1.set(header, `${req1.protocol}://${req1.get('host')}/ZLUX/plugins/${pluginID}/services/${serviceName}/_current${location}`);
}
else if (location.startsWith('http')) {
const locationParts = location.split(":");
let part;
if (locationParts.length > 2) {
part = locationParts[2];
} else {
part = locationParts[1]
}
const t = part.indexOf('/');
const newEnd = part.substring(t);
const newRedirect = req1.protocol + '://' + req1.get('host') + "/ZLUX/plugins/" + pluginID + "/services/_current" + serviceName + newEnd;
proxyLog.debug('Redirecting to: ' + newRedirect);
res1.set(header, newRedirect);
}
}
}
else {
Expand Down
22 changes: 13 additions & 9 deletions lib/webapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -1002,20 +1002,22 @@ WebApp.prototype = {
installErrorHanders() {
this.expressApp.use((req, res, next) => {
const headers = req.headers
let referrerPresent = false;
for (const header of Object.keys(headers)) {
/* Try to find a referer header and try to
* redirect to our server,
*/
if (header == 'referer') {
referrerPresent = true;
let referrer = headers[header];
var pattern = new RegExp('^http.+\/'+this.options.productCode+'\/plugins\/.+');
if (pattern.test(referrer)) {
var parts = headers[header].split("/");
var zluxIndex = parts.indexOf(this.options.productCode);
var pluginID = parts[zluxIndex + 2];
var serviceName = parts[zluxIndex + 4];
var myProxy = proxyMap.get(pluginID + ":" + serviceName);
var fullUrl = req.originalUrl;
const parts = headers[header].split("/");
const zluxIndex = parts.indexOf(this.options.productCode);
const pluginID = parts[zluxIndex + 2];
const serviceName = parts[zluxIndex + 4];
const myProxy = proxyMap.get(pluginID + ":" + serviceName);
const fullUrl = req.originalUrl;
req.url = fullUrl;
if (myProxy != undefined) {
utilLog.debug("About to call myProxy");
Expand All @@ -1032,11 +1034,13 @@ WebApp.prototype = {
+ ` 404 because referrer (${referrer}) didn't match a plugin pattern`);
return do404(req.url, res, this.options.productCode + ": unknown resource requested");
}
} else {
return do404(req.url, res, this.options.productCode
+ ": unknown resource requested");
break;
}
}
if (!referrerPresent) {
return do404(req.url, res, this.options.productCode
+ ": unknown resource requested");
}
});
}
};
Expand Down

0 comments on commit ec52b51

Please sign in to comment.