2020-02-20 09:49:26 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2020-08-15 23:17:47 -07:00
|
|
|
proxy,
|
2020-11-19 17:10:45 -08:00
|
|
|
restart,
|
2020-08-15 23:17:47 -07:00
|
|
|
|
2021-08-17 15:45:57 -07:00
|
|
|
setLocation,
|
|
|
|
|
getLocation
|
2020-02-20 09:49:26 -08:00
|
|
|
};
|
|
|
|
|
|
2021-01-21 11:31:35 -08:00
|
|
|
const assert = require('assert'),
|
2020-08-15 23:17:47 -07:00
|
|
|
auditSource = require('../auditsource.js'),
|
2020-02-20 09:49:26 -08:00
|
|
|
BoxError = require('../boxerror.js'),
|
2020-11-19 17:10:45 -08:00
|
|
|
debug = require('debug')('box:routes/mailserver'),
|
2020-02-20 09:49:26 -08:00
|
|
|
HttpError = require('connect-lastmile').HttpError,
|
2020-08-15 23:17:47 -07:00
|
|
|
HttpSuccess = require('connect-lastmile').HttpSuccess,
|
|
|
|
|
mail = require('../mail.js'),
|
|
|
|
|
middleware = require('../middleware/index.js'),
|
2021-08-17 15:45:57 -07:00
|
|
|
safe = require('safetydance'),
|
2021-01-21 11:31:35 -08:00
|
|
|
services = require('../services.js'),
|
2020-02-20 09:49:26 -08:00
|
|
|
url = require('url');
|
|
|
|
|
|
2020-11-19 17:10:45 -08:00
|
|
|
function restart(req, res, next) {
|
|
|
|
|
mail.restartMail((error) => debug('Error restarting mail container', error));
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-20 09:49:26 -08:00
|
|
|
function proxy(req, res, next) {
|
|
|
|
|
let parsedUrl = url.parse(req.url, true /* parseQueryString */);
|
2020-08-15 22:15:06 -07:00
|
|
|
const pathname = req.path.split('/').pop();
|
2020-02-20 09:49:26 -08:00
|
|
|
|
|
|
|
|
// do not proxy protected values
|
|
|
|
|
delete parsedUrl.query['access_token'];
|
|
|
|
|
delete req.headers['authorization'];
|
|
|
|
|
delete req.headers['cookies'];
|
|
|
|
|
|
2021-01-21 11:31:35 -08:00
|
|
|
services.getContainerDetails('mail', 'CLOUDRON_MAIL_TOKEN', function (error, addonDetails) {
|
2020-02-20 09:49:26 -08:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
|
|
|
|
|
|
|
|
|
parsedUrl.query['access_token'] = addonDetails.token;
|
2020-08-15 22:15:06 -07:00
|
|
|
req.url = url.format({ pathname: pathname, query: parsedUrl.query });
|
2020-02-20 09:49:26 -08:00
|
|
|
|
|
|
|
|
const proxyOptions = url.parse(`https://${addonDetails.ip}:3000`);
|
|
|
|
|
proxyOptions.rejectUnauthorized = false;
|
|
|
|
|
const mailserverProxy = middleware.proxy(proxyOptions);
|
|
|
|
|
|
2020-12-06 23:31:54 -08:00
|
|
|
req.clearTimeout(); // TODO: add timeout to mail server proxy logic instead of this
|
2020-02-20 09:49:26 -08:00
|
|
|
mailserverProxy(req, res, function (error) {
|
|
|
|
|
if (!error) return next();
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-08-15 23:17:47 -07:00
|
|
|
|
2021-08-17 15:45:57 -07:00
|
|
|
async function getLocation(req, res, next) {
|
|
|
|
|
const [error, result] = await safe(mail.getLocation());
|
|
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2020-08-15 23:17:47 -07:00
|
|
|
|
2021-08-17 15:45:57 -07:00
|
|
|
next(new HttpSuccess(200, { domain: result.domain, subdomain: result.subdomain }));
|
2020-08-15 23:17:47 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 15:45:57 -07:00
|
|
|
async function setLocation(req, res, next) {
|
2020-08-15 23:17:47 -07:00
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
if (typeof req.body.domain !== 'string') return next(new HttpError(400, 'domain must be a string'));
|
2020-08-20 23:05:52 -07:00
|
|
|
if (typeof req.body.subdomain !== 'string') return next(new HttpError(400, 'subdomain must be a string'));
|
2020-08-15 23:17:47 -07:00
|
|
|
|
2021-08-17 15:45:57 -07:00
|
|
|
const [error, taskId] = await safe(mail.setLocation(req.body.subdomain, req.body.domain, auditSource.fromRequest(req)));
|
|
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2020-08-15 23:17:47 -07:00
|
|
|
|
2021-08-17 15:45:57 -07:00
|
|
|
next(new HttpSuccess(202, { taskId }));
|
2020-08-15 23:17:47 -07:00
|
|
|
}
|