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

98 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-02-20 09:49:26 -08:00
'use strict';
exports = module.exports = {
proxy,
proxyAndRestart,
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('node: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'),
http = require('node:http'),
2025-07-10 11:00:31 +02:00
HttpError = require('@cloudron/connect-lastmile').HttpError,
HttpSuccess = require('@cloudron/connect-lastmile').HttpSuccess,
mailServer = require('../mailserver.js'),
2021-08-17 15:45:57 -07:00
safe = require('safetydance'),
2024-07-30 12:11:07 +02:00
services = require('../services.js');
2020-02-20 09:49:26 -08:00
2022-09-20 19:20:52 +02:00
async function proxyToMailContainer(port, pathname, req, res, next) {
2024-07-30 12:11:07 +02:00
req.clearTimeout();
2020-02-20 09:49:26 -08:00
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
2024-07-30 12:11:07 +02:00
const searchParams = new URLSearchParams(req.url.slice(req.url.indexOf('?')+1));
searchParams.delete('access_token');
searchParams.append('access_token', addonDetails.token);
const opts = {
hostname: addonDetails.ip,
port: 3000,
path: `/${pathname}?${searchParams.toString()}`,
method: req.method,
headers: req.headers
};
const sftpReq = http.request(opts, function (sftpRes) {
2025-02-18 11:28:23 +01:00
res.writeHead(sftpRes.statusCode, sftpRes.headers);
2024-07-30 12:11:07 +02:00
sftpRes.on('error', (error) => next(new HttpError(500, `mailserver error: ${error.message} ${error.code}`)));
sftpRes.on('end', () => next());
sftpRes.pipe(res);
2020-02-20 09:49:26 -08:00
});
2024-07-30 12:11:07 +02:00
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);
}
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, function (httpError) {
if (httpError) return next(httpError);
// for success, the proxy already sent the response. do not proceed to connect-lastmile which will result in double headers
});
}
async function proxyAndRestart(req, res, next) {
const pathname = req.path.split('/').pop();
proxyToMailContainer(3000, pathname, req, res, async function (httpError) {
if (httpError) return next(httpError);
// for success, the proxy already sent the response. do not proceed to connect-lastmile which will result in double headers
await safe(mailServer.restart(), { debug });
});
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
}