Files
cloudron-box/src/routes/domains.js
T

155 lines
8.1 KiB
JavaScript
Raw Normal View History

2017-10-28 22:18:07 +02:00
'use strict';
exports = module.exports = {
add: add,
get: get,
getAll: getAll,
update: update,
2018-09-05 22:58:43 -07:00
del: del,
verifyDomainLock: verifyDomainLock
2017-10-28 22:18:07 +02:00
};
var assert = require('assert'),
2019-03-25 15:07:06 -07:00
auditSource = require('../auditsource.js'),
2017-10-28 22:18:07 +02:00
domains = require('../domains.js'),
2018-04-29 11:20:12 -07:00
DomainsError = domains.DomainsError,
2017-10-28 22:18:07 +02:00
HttpError = require('connect-lastmile').HttpError,
2018-10-24 20:06:04 -07:00
HttpSuccess = require('connect-lastmile').HttpSuccess;
2018-10-24 13:01:45 -07:00
2018-09-05 22:58:43 -07:00
function verifyDomainLock(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
2019-01-25 14:18:39 -08:00
domains.get(req.params.domain, function (error, domain) {
if (error && error.reason === DomainsError.NOT_FOUND) return next(new HttpError(404, 'No such domain'));
if (error) return next(new HttpError(500, error));
2018-09-05 22:58:43 -07:00
2019-01-25 14:18:39 -08:00
if (domain.locked) return next(new HttpError(423, 'This domain is locked'));
next();
});
2018-09-05 22:58:43 -07:00
}
2017-10-28 22:18:07 +02:00
function add(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'));
2018-01-09 14:46:38 -08:00
if (typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider must be a string'));
2018-09-11 22:17:40 -07:00
if (!req.body.config || typeof req.body.config !== 'object') return next(new HttpError(400, 'config must be an object'));
if ('hyphenatedSubdomains' in req.body.config && typeof req.body.config.hyphenatedSubdomains !== 'boolean') return next(new HttpError(400, 'hyphenatedSubdomains must be a boolean'));
2018-09-11 22:46:17 -07:00
if ('wildcard' in req.body.config && typeof req.body.config.wildcard !== 'boolean') return next(new HttpError(400, 'wildcard must be a boolean'));
2018-09-11 22:17:40 -07:00
2017-11-07 02:15:55 +01:00
if ('zoneName' in req.body && typeof req.body.zoneName !== 'string') return next(new HttpError(400, 'zoneName must be a string'));
2017-11-09 02:06:36 +01:00
if ('fallbackCertificate' in req.body && typeof req.body.fallbackCertificate !== 'object') return next(new HttpError(400, 'fallbackCertificate must be a object with cert and key strings'));
if (req.body.fallbackCertificate) {
2018-11-05 21:13:34 -08:00
let fallbackCertificate = req.body.fallbackCertificate;
if (!fallbackCertificate.cert || typeof fallbackCertificate.cert !== 'string') return next(new HttpError(400, 'fallbackCertificate.cert must be a string'));
if (!fallbackCertificate.key || typeof fallbackCertificate.key !== 'string') return next(new HttpError(400, 'fallbackCertificate.key must be a string'));
if ('restricted' in fallbackCertificate && typeof fallbackCertificate.restricted !== 'boolean') return next(new HttpError(400, 'fallbackCertificate.restricted must be a boolean'));
}
2018-09-11 22:17:40 -07:00
if ('tlsConfig' in req.body) {
if (!req.body.tlsConfig || typeof req.body.tlsConfig !== 'object') return next(new HttpError(400, 'tlsConfig must be a object with a provider string property'));
if (!req.body.tlsConfig.provider || typeof req.body.tlsConfig.provider !== 'string') return next(new HttpError(400, 'tlsConfig.provider must be a string'));
}
2017-10-28 22:18:07 +02:00
// some DNS providers like DigitalOcean take a really long time to verify credentials (https://github.com/expressjs/timeout/issues/26)
req.clearTimeout();
2018-11-10 00:43:46 -08:00
let data = {
zoneName: req.body.zoneName || '',
provider: req.body.provider,
config: req.body.config,
fallbackCertificate: req.body.fallbackCertificate || null,
tlsConfig: req.body.tlsConfig || { provider: 'letsencrypt-prod' }
};
2019-03-25 15:07:06 -07:00
domains.add(req.body.domain, data, auditSource.fromRequest(req), function (error) {
2018-04-29 11:20:12 -07:00
if (error && error.reason === DomainsError.ALREADY_EXISTS) return next(new HttpError(409, error.message));
if (error && error.reason === DomainsError.BAD_FIELD) return next(new HttpError(400, error.message));
if (error && error.reason === DomainsError.INVALID_PROVIDER) return next(new HttpError(400, error.message));
2017-10-28 22:18:07 +02:00
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(201, { domain: req.body.domain, config: req.body.config }));
});
}
function get(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
domains.get(req.params.domain, function (error, result) {
2018-04-29 11:20:12 -07:00
if (error && error.reason === DomainsError.NOT_FOUND) return next(new HttpError(404, error.message));
2017-10-28 22:18:07 +02:00
if (error) return next(new HttpError(500, error));
2018-04-27 11:38:09 -07:00
next(new HttpSuccess(200, domains.removePrivateFields(result)));
2017-10-28 22:18:07 +02:00
});
}
function getAll(req, res, next) {
domains.getAll(function (error, result) {
if (error) return next(new HttpError(500, error));
2018-06-25 15:12:20 -07:00
result = result.map(domains.removeRestrictedFields);
2018-04-27 11:38:09 -07:00
2017-10-28 22:18:07 +02:00
next(new HttpSuccess(200, { domains: result }));
});
}
function update(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.body, 'object');
2018-01-09 14:46:38 -08:00
if (typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider must be an object'));
2018-09-11 22:17:40 -07:00
if (!req.body.config || typeof req.body.config !== 'object') return next(new HttpError(400, 'config must be an object'));
if ('hyphenatedSubdomains' in req.body.config && typeof req.body.config.hyphenatedSubdomains !== 'boolean') return next(new HttpError(400, 'hyphenatedSubdomains must be a boolean'));
2018-09-11 22:46:17 -07:00
if ('wildcard' in req.body.config && typeof req.body.config.wildcard !== 'boolean') return next(new HttpError(400, 'wildcard must be a boolean'));
2018-09-11 22:17:40 -07:00
if ('zoneName' in req.body && typeof req.body.zoneName !== 'string') return next(new HttpError(400, 'zoneName must be a string'));
2017-11-09 02:06:36 +01:00
if ('fallbackCertificate' in req.body && typeof req.body.fallbackCertificate !== 'object') return next(new HttpError(400, 'fallbackCertificate must be a object with cert and key strings'));
2018-11-05 21:13:34 -08:00
if (req.body.fallbackCertificate) {
let fallbackCertificate = req.body.fallbackCertificate;
if (!fallbackCertificate.cert || typeof fallbackCertificate.cert !== 'string') return next(new HttpError(400, 'fallbackCertificate.cert must be a string'));
if (!fallbackCertificate.key || typeof fallbackCertificate.key !== 'string') return next(new HttpError(400, 'fallbackCertificate.key must be a string'));
if ('restricted' in fallbackCertificate && typeof fallbackCertificate.restricted !== 'boolean') return next(new HttpError(400, 'fallbackCertificate.restricted must be a boolean'));
}
2018-09-11 22:17:40 -07:00
if ('tlsConfig' in req.body) {
if (!req.body.tlsConfig || typeof req.body.tlsConfig !== 'object') return next(new HttpError(400, 'tlsConfig must be a object with a provider string property'));
if (!req.body.tlsConfig.provider || typeof req.body.tlsConfig.provider !== 'string') return next(new HttpError(400, 'tlsConfig.provider must be a string'));
}
2017-10-28 22:18:07 +02:00
// some DNS providers like DigitalOcean take a really long time to verify credentials (https://github.com/expressjs/timeout/issues/26)
req.clearTimeout();
2018-11-10 00:43:46 -08:00
let data = {
zoneName: req.body.zoneName || '',
provider: req.body.provider,
config: req.body.config,
fallbackCertificate: req.body.fallbackCertificate || null,
tlsConfig: req.body.tlsConfig || { provider: 'letsencrypt-prod' }
};
2019-03-25 15:07:06 -07:00
domains.update(req.params.domain, data, auditSource.fromRequest(req), function (error) {
2018-04-29 11:20:12 -07:00
if (error && error.reason === DomainsError.NOT_FOUND) return next(new HttpError(404, error.message));
if (error && error.reason === DomainsError.BAD_FIELD) return next(new HttpError(400, error.message));
if (error && error.reason === DomainsError.INVALID_PROVIDER) return next(new HttpError(400, error.message));
2017-10-28 22:18:07 +02:00
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(204, {}));
});
}
function del(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
2019-03-25 15:07:06 -07:00
domains.del(req.params.domain, auditSource.fromRequest(req), function (error) {
2018-04-29 11:20:12 -07:00
if (error && error.reason === DomainsError.NOT_FOUND) return next(new HttpError(404, error.message));
if (error && error.reason === DomainsError.IN_USE) return next(new HttpError(409, 'Domain is still in use. Remove all apps and mailboxes using this domain'));
2017-10-28 22:18:07 +02:00
if (error) return next(new HttpError(500, error));
2018-01-25 10:46:15 -08:00
next(new HttpSuccess(204));
2017-10-28 22:18:07 +02:00
});
}