proxy-middleware: no more a middleware
This commit is contained in:
@@ -13,39 +13,43 @@ const assert = require('assert'),
|
||||
AuditSource = require('../auditsource.js'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
debug = require('debug')('box:routes/mailserver'),
|
||||
http = require('http'),
|
||||
HttpError = require('connect-lastmile').HttpError,
|
||||
HttpSuccess = require('connect-lastmile').HttpSuccess,
|
||||
mailServer = require('../mailserver.js'),
|
||||
middleware = require('../middleware/index.js'),
|
||||
safe = require('safetydance'),
|
||||
services = require('../services.js'),
|
||||
url = require('url');
|
||||
services = require('../services.js');
|
||||
|
||||
async function proxyToMailContainer(port, pathname, req, res, next) {
|
||||
const parsedUrl = url.parse(req.url, true /* parseQueryString */);
|
||||
|
||||
// do not proxy protected values
|
||||
delete parsedUrl.query['access_token'];
|
||||
delete req.headers['authorization'];
|
||||
delete req.headers['cookies'];
|
||||
req.clearTimeout();
|
||||
|
||||
const [error, addonDetails] = await safe(services.getContainerDetails('mail', 'CLOUDRON_MAIL_TOKEN'));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
parsedUrl.query['access_token'] = addonDetails.token;
|
||||
req.url = url.format({ pathname, query: parsedUrl.query });
|
||||
const searchParams = new URLSearchParams(req.url.slice(req.url.indexOf('?')+1));
|
||||
searchParams.delete('access_token');
|
||||
searchParams.append('access_token', addonDetails.token);
|
||||
|
||||
const mailserverProxy = middleware.proxy(`http://${addonDetails.ip}:${port}`);
|
||||
const opts = {
|
||||
hostname: addonDetails.ip,
|
||||
port: 3000,
|
||||
path: `/${pathname}?${searchParams.toString()}`,
|
||||
method: req.method,
|
||||
headers: req.headers
|
||||
};
|
||||
|
||||
req.clearTimeout(); // TODO: add timeout to mail server proxy logic instead of this
|
||||
mailserverProxy(req, res, function (error) {
|
||||
if (!error) return next(); // note: response was already sent by proxy by this point
|
||||
|
||||
if (error.code === 'ECONNREFUSED') return next(new HttpError(424, 'Unable to connect to mail server'));
|
||||
if (error.code === 'ECONNRESET') return next(new HttpError(424, 'Unable to query mail server'));
|
||||
|
||||
next(new HttpError(500, error));
|
||||
const sftpReq = http.request(opts, function (sftpRes) {
|
||||
res.writeHead(sftpRes.statusCode, sftpRes.headers);
|
||||
sftpRes.on('error', (error) => next(new HttpError(500, `mailserver error: ${error.message} ${error.code}`)));
|
||||
sftpRes.on('end', () => next());
|
||||
sftpRes.pipe(res);
|
||||
});
|
||||
sftpReq.on('error', (error) => next(new HttpError(424, `Unable to connect to mailserver: ${error.message} ${error.code}`)));
|
||||
if (!req.readable) {
|
||||
sftpReq.end();
|
||||
} else {
|
||||
req.pipe(sftpReq);
|
||||
}
|
||||
}
|
||||
|
||||
async function proxy(req, res, next) {
|
||||
|
||||
Reference in New Issue
Block a user