mail: mount mail data directory into sftp container

fixes #794
This commit is contained in:
Girish Ramakrishnan
2021-09-25 17:19:58 -07:00
parent d4edd771b5
commit 8255623874
4 changed files with 38 additions and 30 deletions

View File

@@ -1,8 +1,7 @@
'use strict';
exports = module.exports = {
proxyAppData,
proxyVolumeData
proxy,
};
const assert = require('assert'),
@@ -13,37 +12,38 @@ const assert = require('assert'),
services = require('../services.js'),
url = require('url');
async function proxy(id, req, res, next) {
assert.strictEqual(typeof id, 'string');
function proxy(kind) {
assert(kind === 'mail' || kind === 'volume' || kind === 'app');
req.clearTimeout();
return async function (req, res, next) {
req.clearTimeout();
const [error, result] = await safe(services.getContainerDetails('sftp', 'CLOUDRON_SFTP_TOKEN'));
if (error) return next(BoxError.toHttpError(error));
let id = null;
switch (kind) {
case 'app': id = `app-${req.params.id}`; break;
case 'volume': id = `volume-${req.params.id}`; break;
case 'mail': id = 'mail'; break;
}
let parsedUrl = url.parse(req.url, true /* parseQueryString */);
parsedUrl.query['access_token'] = result.token;
const [error, result] = await safe(services.getContainerDetails('sftp', 'CLOUDRON_SFTP_TOKEN'));
if (error) return next(BoxError.toHttpError(error));
req.url = url.format({ pathname: `/files/${id}/${encodeURIComponent(req.params[0])}`, query: parsedUrl.query }); // params[0] already contains leading '/'
let parsedUrl = url.parse(req.url, true /* parseQueryString */);
parsedUrl.query['access_token'] = result.token;
const proxyOptions = url.parse(`https://${result.ip}:3000`);
proxyOptions.rejectUnauthorized = false;
const fileManagerProxy = middleware.proxy(proxyOptions);
req.url = url.format({ pathname: `/files/${id}/${encodeURIComponent(req.params[0])}`, query: parsedUrl.query }); // params[0] already contains leading '/'
fileManagerProxy(req, res, function (error) {
if (!error) return next();
const proxyOptions = url.parse(`https://${result.ip}:3000`);
proxyOptions.rejectUnauthorized = false;
const fileManagerProxy = middleware.proxy(proxyOptions);
if (error.code === 'ECONNREFUSED') return next(new HttpError(424, 'Unable to connect to filemanager server'));
if (error.code === 'ECONNRESET') return next(new HttpError(424, 'Unable to query filemanager server'));
fileManagerProxy(req, res, function (error) {
if (!error) return next();
next(new HttpError(500, error));
});
if (error.code === 'ECONNREFUSED') return next(new HttpError(424, 'Unable to connect to filemanager server'));
if (error.code === 'ECONNRESET') return next(new HttpError(424, 'Unable to query filemanager server'));
next(new HttpError(500, error));
});
};
}
function proxyAppData(req, res, next) {
proxy(`app-${req.params.id}`, req, res, next);
}
function proxyVolumeData(req, res, next) {
proxy(`volume-${req.params.id}`, req, res, next);
}