diff --git a/backend/internal/proxy-host.js b/backend/internal/proxy-host.js index 3299012a6b..9dfabecabf 100644 --- a/backend/internal/proxy-host.js +++ b/backend/internal/proxy-host.js @@ -266,6 +266,49 @@ const internalProxyHost = { }); }, + /** + * @param {Access} access + * @param {Object} data + * @param {String} data.domain + * @param {Array} [data.expand] + * @param {Array} [data.omit] + * @return {Promise} + */ + getByDomain: (access, data) => { + const thisData = data || {}; + + return access.can("proxy_hosts:get", thisData.domain) + .then((access_data) => { + const query = proxyHostModel + .query() + .where("is_deleted", 0) + .andWhere(castJsonIfNeed("domain_names"), "like", `%"${thisData.domain}"%`) + .allowGraph("[owner,access_list.[clients,items],certificate]") + .first(); + + if (access_data.permission_visibility !== "all") { + query.andWhere("owner_user_id", access.token.getUserId(1)); + } + + if (typeof thisData.expand !== "undefined" && thisData.expand !== null) { + query.withGraphFetched(`[${thisData.expand.join(", ")}]`); + } + + return query.then(utils.omitRow(omissions())); + }) + .then((row) => { + if (!row || !row.id) { + throw new errs.ItemNotFoundError(thisData.id); + } + const thisRow = internalHost.cleanRowCertificateMeta(row); + // Custom omissions + if (typeof thisData.omit !== "undefined" && thisData.omit !== null) { + return _.omit(row, thisData.omit); + } + return thisRow; + }); + }, + /** * @param {Access} access * @param {Object} data diff --git a/backend/routes/nginx/proxy_hosts.js b/backend/routes/nginx/proxy_hosts.js index 7045a195cc..b3b2ae4c72 100644 --- a/backend/routes/nginx/proxy_hosts.js +++ b/backend/routes/nginx/proxy_hosts.js @@ -206,4 +206,49 @@ router } }); +/** + * Specific proxy-host by domain name + * + * /api/nginx/proxy-hosts/domain/:domain + */ +router + .route("/domain/:domain") + .options((_, res) => { + res.sendStatus(204); + }) + .all(jwtdecode()) + + /** + * GET /api/nginx/proxy-hosts/domain/:domain + * + * Retrieve a specific proxy-host by domain name + */ + .get(async (req, res, next) => { + try { + const data = await validator({ + required: ["domain"], + additionalProperties: false, + properties: { + domain: { + type: "string", + }, + expand: { + $ref: "common#/properties/expand" + } + } + }, { + domain: req.params.domain, + expand: (typeof req.query.expand === "string" ? req.query.expand.split(",") : null) + }); + const row = await internalProxyHost.getByDomain(res.locals.access, { + domain: data.domain, + expand: data.expand + }); + res.status(200).send(row); + } catch (err) { + debug(logger, `${req.method.toUpperCase()} ${req.path}: ${err}`); + next(err); + } + }) + export default router;