diff --git a/src/middleware/proxy-middleware.js b/src/middleware/proxy-middleware.js index d8f7e809c..e38672005 100644 --- a/src/middleware/proxy-middleware.js +++ b/src/middleware/proxy-middleware.js @@ -1,33 +1,26 @@ +'use strict'; + // https://github.com/cloudron-io/node-proxy-middleware // MIT license // contains https://github.com/gonzalocasas/node-proxy-middleware/pull/59 const { request } = require('http'); -module.exports = function proxyMiddleware(options) { - //enable ability to quickly pass a url for shorthand setup - if(typeof options === 'string'){ - options = require('url').parse(options); - } +function slashJoin(p1, p2) { + var trailing_slash = false; - options = options || {}; - options.pathname = options.pathname || '/'; + if (p1.length && p1[p1.length - 1] === '/') { trailing_slash = true; } + if (trailing_slash && p2.length && p2[0] === '/') {p2 = p2.substring(1); } + + return p1 + p2; +} + +module.exports = function proxyMiddleware(targetUrl) { + const options = require('url').parse(targetUrl); return function (req, resp, next) { - var url = req.url; - // You can pass the route within the options, as well - if (typeof options.route === 'string') { - if (url === options.route) { - url = ''; - } else if (url.slice(0, options.route.length) === options.route) { - url = url.slice(options.route.length); - } else { - return next(); - } - } - - //options for this request - var opts = Object.assign({}, options); + const { url } = req; + const opts = Object.assign({}, options); if (url && url.charAt(0) === '?') { // prevent /api/resource/?offset=0 if (options.pathname.length > 1 && options.pathname.charAt(options.pathname.length - 1) === '/') { opts.path = options.pathname.substring(0, options.pathname.length - 1) + url; @@ -44,14 +37,6 @@ module.exports = function proxyMiddleware(options) { delete opts.headers.host; var myReq = request(opts, function (myRes) { - var statusCode = myRes.statusCode - , headers = myRes.headers - , location = headers.location; - // Fix the location - if (((statusCode > 300 && statusCode < 304) || statusCode === 201) && location && location.indexOf(options.href) > -1) { - // absoulte path - headers.location = location.replace(options.href, slashJoin('/', slashJoin((options.route || ''), ''))); - } resp.writeHead(myRes.statusCode, myRes.headers); myRes.on('error', function (err) { next(err); @@ -71,12 +56,3 @@ module.exports = function proxyMiddleware(options) { } }; }; - -function slashJoin(p1, p2) { - var trailing_slash = false; - - if (p1.length && p1[p1.length - 1] === '/') { trailing_slash = true; } - if (trailing_slash && p2.length && p2[0] === '/') {p2 = p2.substring(1); } - - return p1 + p2; -} diff --git a/src/routes/filemanager.js b/src/routes/filemanager.js index f01537252..621759887 100644 --- a/src/routes/filemanager.js +++ b/src/routes/filemanager.js @@ -33,9 +33,7 @@ function proxy(kind) { req.url = url.format({ pathname: `/files/${id}/${encodeURIComponent(req.params[0])}`, query: parsedUrl.query }); // params[0] already contains leading '/' - const proxyOptions = url.parse(`http://${result.ip}:3000`); - proxyOptions.rejectUnauthorized = false; - const fileManagerProxy = middleware.proxy(proxyOptions); + const fileManagerProxy = middleware.proxy(`http://${result.ip}:3000`); fileManagerProxy(req, res, function (error) { if (!error) return; // note: response was already sent by proxy by this point diff --git a/src/routes/mailserver.js b/src/routes/mailserver.js index d5f244422..c5483f630 100644 --- a/src/routes/mailserver.js +++ b/src/routes/mailserver.js @@ -35,8 +35,7 @@ async function proxyToMailContainer(port, pathname, req, res, next) { parsedUrl.query['access_token'] = addonDetails.token; req.url = url.format({ pathname, query: parsedUrl.query }); - const proxyOptions = url.parse(`http://${addonDetails.ip}:${port}`); - const mailserverProxy = middleware.proxy(proxyOptions); + const mailserverProxy = middleware.proxy(`http://${addonDetails.ip}:${port}`); req.clearTimeout(); // TODO: add timeout to mail server proxy logic instead of this mailserverProxy(req, res, function (error) {