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,
|
2022-08-31 07:42:51 +02:00
|
|
|
queueProxy,
|
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'),
|
2021-09-30 09:50:30 -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,
|
2023-08-04 20:54:16 +05:30
|
|
|
mailServer = require('../mailserver.js'),
|
2020-08-15 23:17:47 -07:00
|
|
|
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');
|
|
|
|
|
|
2021-12-08 16:42:36 -08:00
|
|
|
// because of how the proxy middleware works, the http response is already sent by the time this function is called
|
|
|
|
|
async function restart(req, res, next) {
|
2023-08-21 15:19:42 +05:30
|
|
|
await safe(mailServer.restart(), { debug });
|
2020-11-19 17:10:45 -08:00
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 19:20:52 +02:00
|
|
|
async function proxyToMailContainer(port, pathname, req, res, next) {
|
2022-08-31 07:42:51 +02:00
|
|
|
const parsedUrl = url.parse(req.url, true /* parseQueryString */);
|
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-08-25 19:41:46 -07:00
|
|
|
const [error, addonDetails] = await safe(services.getContainerDetails('mail', 'CLOUDRON_MAIL_TOKEN'));
|
|
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2020-02-20 09:49:26 -08:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
parsedUrl.query['access_token'] = addonDetails.token;
|
2022-09-20 19:20:52 +02:00
|
|
|
req.url = url.format({ pathname, query: parsedUrl.query });
|
2020-02-20 09:49:26 -08:00
|
|
|
|
2022-08-31 07:42:51 +02:00
|
|
|
const proxyOptions = url.parse(`http://${addonDetails.ip}:${port}`);
|
2021-08-25 19:41:46 -07:00
|
|
|
const mailserverProxy = middleware.proxy(proxyOptions);
|
2020-02-20 09:49:26 -08:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
req.clearTimeout(); // TODO: add timeout to mail server proxy logic instead of this
|
|
|
|
|
mailserverProxy(req, res, function (error) {
|
2023-11-26 15:40:21 +01:00
|
|
|
if (!error) return; // response was already sent by proxy, do not proceed to connect-lastmile
|
2020-02-20 09:49:26 -08:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
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'));
|
2020-02-20 09:49:26 -08:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
next(new HttpError(500, error));
|
2020-02-20 09:49:26 -08:00
|
|
|
});
|
|
|
|
|
}
|
2020-08-15 23:17:47 -07:00
|
|
|
|
2022-08-31 07:42:51 +02:00
|
|
|
async function proxy(req, res, next) {
|
2022-09-20 19:20:52 +02:00
|
|
|
const pathname = req.path.split('/').pop();
|
|
|
|
|
|
|
|
|
|
proxyToMailContainer(3000, pathname, req, res, next);
|
2022-08-31 07:42:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function queueProxy(req, res, next) {
|
2022-09-22 19:30:05 +02:00
|
|
|
proxyToMailContainer(6000, req.path.replace('/', '/queue/'), req, res, next);
|
2022-08-31 07:42:51 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 15:45:57 -07:00
|
|
|
async function getLocation(req, res, next) {
|
2023-08-04 20:54:16 +05:30
|
|
|
const [error, result] = await safe(mailServer.getLocation());
|
2021-08-17 15:45:57 -07:00
|
|
|
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
|
|
|
|
2023-08-04 21:37:38 +05:30
|
|
|
const [error, taskId] = await safe(mailServer.startChangeLocation(req.body.subdomain, req.body.domain, AuditSource.fromRequest(req)));
|
2021-08-17 15:45:57 -07:00
|
|
|
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 }));
|
2021-12-21 12:29:46 -08:00
|
|
|
}
|