Files
cloudron-box/src/routes/mailserver.js

86 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-02-20 09:49:26 -08:00
'use strict';
exports = module.exports = {
proxy,
2020-11-19 17:10:45 -08:00
restart,
2022-08-31 07:42:51 +02:00
queueProxy,
2021-08-17 15:45:57 -07:00
setLocation,
getLocation
2020-02-20 09:49:26 -08:00
};
const assert = require('assert'),
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,
HttpSuccess = require('connect-lastmile').HttpSuccess,
mailServer = require('../mailserver.js'),
middleware = require('../middleware/index.js'),
2021-08-17 15:45:57 -07:00
safe = require('safetydance'),
services = require('../services.js'),
2020-02-20 09:49:26 -08:00
url = require('url');
// 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) {
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
});
}
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) {
const [error, result] = await safe(mailServer.getLocation());
2021-08-17 15:45:57 -07:00
if (error) return next(BoxError.toHttpError(error));
2021-08-17 15:45:57 -07:00
next(new HttpSuccess(200, { domain: result.domain, subdomain: result.subdomain }));
}
2021-08-17 15:45:57 -07:00
async function setLocation(req, res, next) {
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'));
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));
2021-08-17 15:45:57 -07:00
next(new HttpSuccess(202, { taskId }));
2021-12-21 12:29:46 -08:00
}