Files

475 lines
12 KiB
JavaScript
Raw Permalink Normal View History

2025-09-02 21:43:00 +10:00
import _ from "lodash";
import errs from "../lib/error.js";
import { castJsonIfNeed } from "../lib/helpers.js";
import utils from "../lib/utils.js";
import proxyHostModel from "../models/proxy_host.js";
import internalAuditLog from "./audit-log.js";
import internalCertificate from "./certificate.js";
import internalHost from "./host.js";
import internalNginx from "./nginx.js";
const omissions = () => {
return ["is_deleted", "owner.is_deleted"];
};
2020-02-19 15:55:06 +11:00
const internalProxyHost = {
/**
* @param {Access} access
* @param {Object} data
* @returns {Promise}
*/
create: (access, data) => {
2025-09-02 21:43:00 +10:00
let thisData = data;
const createCertificate = thisData.certificate_id === "new";
2020-02-19 15:55:06 +11:00
2025-09-02 21:43:00 +10:00
if (createCertificate) {
delete thisData.certificate_id;
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
return access
.can("proxy_hosts:create", thisData)
2020-02-19 15:55:06 +11:00
.then(() => {
// Get a list of the domain names and check each of them against existing records
2025-09-02 21:43:00 +10:00
const domain_name_check_promises = [];
2020-02-19 15:55:06 +11:00
2025-09-02 21:43:00 +10:00
thisData.domain_names.map((domain_name) => {
2020-02-19 15:55:06 +11:00
domain_name_check_promises.push(internalHost.isHostnameTaken(domain_name));
2025-09-02 21:43:00 +10:00
return true;
2020-02-19 15:55:06 +11:00
});
2025-09-02 21:43:00 +10:00
return Promise.all(domain_name_check_promises).then((check_results) => {
check_results.map((result) => {
if (result.is_taken) {
throw new errs.ValidationError(`${result.hostname} is already in use`);
}
return true;
2020-02-19 15:55:06 +11:00
});
2025-09-02 21:43:00 +10:00
});
2020-02-19 15:55:06 +11:00
})
.then(() => {
// At this point the domains should have been checked
2025-09-02 21:43:00 +10:00
thisData.owner_user_id = access.token.getUserId(1);
thisData = internalHost.cleanSslHstsData(thisData);
2020-02-19 15:55:06 +11:00
2024-10-09 18:05:15 +10:00
// Fix for db field not having a default value
// for this optional field.
2025-09-02 21:43:00 +10:00
if (typeof thisData.advanced_config === "undefined") {
thisData.advanced_config = "";
2024-10-09 18:05:15 +10:00
}
2025-09-02 21:43:00 +10:00
return proxyHostModel.query().insertAndFetch(thisData).then(utils.omitRow(omissions()));
2020-02-19 15:55:06 +11:00
})
.then((row) => {
2025-09-02 21:43:00 +10:00
if (createCertificate) {
return internalCertificate
.createQuickCertificate(access, thisData)
2020-02-19 15:55:06 +11:00
.then((cert) => {
// update host with cert id
return internalProxyHost.update(access, {
2025-09-02 21:43:00 +10:00
id: row.id,
certificate_id: cert.id,
2020-02-19 15:55:06 +11:00
});
})
.then(() => {
return row;
});
}
2025-09-02 21:43:00 +10:00
return row;
2020-02-19 15:55:06 +11:00
})
.then((row) => {
// re-fetch with cert
return internalProxyHost.get(access, {
2025-09-02 21:43:00 +10:00
id: row.id,
expand: ["certificate", "owner", "access_list.[clients,items]"],
2020-02-19 15:55:06 +11:00
});
})
.then((row) => {
// Configure nginx
2025-09-02 21:43:00 +10:00
return internalNginx.configure(proxyHostModel, "proxy_host", row).then(() => {
return row;
});
2020-02-19 15:55:06 +11:00
})
.then((row) => {
// Audit log
2025-09-02 21:43:00 +10:00
thisData.meta = _.assign({}, thisData.meta || {}, row.meta);
2020-02-19 15:55:06 +11:00
// Add to audit log
2025-09-02 21:43:00 +10:00
return internalAuditLog
.add(access, {
action: "created",
object_type: "proxy-host",
object_id: row.id,
meta: thisData,
})
2020-02-19 15:55:06 +11:00
.then(() => {
return row;
});
});
},
/**
* @param {Access} access
* @param {Object} data
* @param {Number} data.id
* @return {Promise}
*/
update: (access, data) => {
2025-09-02 21:43:00 +10:00
let thisData = data;
2026-03-03 08:44:42 +10:00
const createCertificate = thisData.certificate_id === "new";
2020-02-19 15:55:06 +11:00
2026-03-03 08:44:42 +10:00
if (createCertificate) {
2025-09-02 21:43:00 +10:00
delete thisData.certificate_id;
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
return access
.can("proxy_hosts:update", thisData.id)
2020-02-19 15:55:06 +11:00
.then((/*access_data*/) => {
// Get a list of the domain names and check each of them against existing records
2025-09-02 21:43:00 +10:00
const domain_name_check_promises = [];
2020-02-19 15:55:06 +11:00
2025-09-02 21:43:00 +10:00
if (typeof thisData.domain_names !== "undefined") {
thisData.domain_names.map((domain_name) => {
return domain_name_check_promises.push(
internalHost.isHostnameTaken(domain_name, "proxy", thisData.id),
);
2020-02-19 15:55:06 +11:00
});
2025-09-02 21:43:00 +10:00
return Promise.all(domain_name_check_promises).then((check_results) => {
check_results.map((result) => {
if (result.is_taken) {
throw new errs.ValidationError(`${result.hostname} is already in use`);
}
return true;
2020-02-19 15:55:06 +11:00
});
2025-09-02 21:43:00 +10:00
});
2020-02-19 15:55:06 +11:00
}
})
.then(() => {
2025-09-02 21:43:00 +10:00
return internalProxyHost.get(access, { id: thisData.id });
2020-02-19 15:55:06 +11:00
})
.then((row) => {
2025-09-02 21:43:00 +10:00
if (row.id !== thisData.id) {
2020-02-19 15:55:06 +11:00
// Sanity check that something crazy hasn't happened
2025-09-02 21:43:00 +10:00
throw new errs.InternalValidationError(
`Proxy Host could not be updated, IDs do not match: ${row.id} !== ${thisData.id}`,
);
2020-02-19 15:55:06 +11:00
}
2026-03-03 08:44:42 +10:00
if (createCertificate) {
2025-09-02 21:43:00 +10:00
return internalCertificate
.createQuickCertificate(access, {
domain_names: thisData.domain_names || row.domain_names,
meta: _.assign({}, row.meta, thisData.meta),
})
2020-02-19 15:55:06 +11:00
.then((cert) => {
// update host with cert id
2025-09-02 21:43:00 +10:00
thisData.certificate_id = cert.id;
2020-02-19 15:55:06 +11:00
})
.then(() => {
return row;
});
}
2025-09-02 21:43:00 +10:00
return row;
2020-02-19 15:55:06 +11:00
})
.then((row) => {
// Add domain_names to the data in case it isn't there, so that the audit log renders correctly. The order is important here.
2025-09-02 21:43:00 +10:00
thisData = _.assign(
{},
{
domain_names: row.domain_names,
},
data,
);
2020-02-19 15:55:06 +11:00
2025-09-02 21:43:00 +10:00
thisData = internalHost.cleanSslHstsData(thisData, row);
2020-02-19 15:55:06 +11:00
return proxyHostModel
.query()
2025-09-02 21:43:00 +10:00
.where({ id: thisData.id })
.patch(thisData)
.then(utils.omitRow(omissions()))
2020-02-19 15:55:06 +11:00
.then((saved_row) => {
// Add to audit log
2025-09-02 21:43:00 +10:00
return internalAuditLog
.add(access, {
action: "updated",
object_type: "proxy-host",
object_id: row.id,
meta: thisData,
})
2020-02-19 15:55:06 +11:00
.then(() => {
return saved_row;
2020-02-19 15:55:06 +11:00
});
});
})
.then(() => {
2025-09-02 21:43:00 +10:00
return internalProxyHost
.get(access, {
id: thisData.id,
expand: ["owner", "certificate", "access_list.[clients,items]"],
})
2020-02-19 15:55:06 +11:00
.then((row) => {
if (!row.enabled) {
// No need to add nginx config if host is disabled
return row;
}
2020-02-19 15:55:06 +11:00
// Configure nginx
2025-09-02 21:43:00 +10:00
return internalNginx.configure(proxyHostModel, "proxy_host", row).then((new_meta) => {
row.meta = new_meta;
return _.omit(internalHost.cleanRowCertificateMeta(row), omissions());
});
2020-02-19 15:55:06 +11:00
});
});
},
/**
* @param {Access} access
* @param {Object} data
* @param {Number} data.id
* @param {Array} [data.expand]
* @param {Array} [data.omit]
* @return {Promise}
*/
get: (access, data) => {
2025-09-02 21:43:00 +10:00
const thisData = data || {};
return access
.can("proxy_hosts:get", thisData.id)
2020-02-19 15:55:06 +11:00
.then((access_data) => {
2025-09-02 21:43:00 +10:00
const query = proxyHostModel
2020-02-19 15:55:06 +11:00
.query()
2025-09-02 21:43:00 +10:00
.where("is_deleted", 0)
.andWhere("id", thisData.id)
2026-03-03 08:44:42 +10:00
.allowGraph(proxyHostModel.defaultAllowGraph)
2020-02-19 15:55:06 +11:00
.first();
2025-09-02 21:43:00 +10:00
if (access_data.permission_visibility !== "all") {
query.andWhere("owner_user_id", access.token.getUserId(1));
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
if (typeof thisData.expand !== "undefined" && thisData.expand !== null) {
query.withGraphFetched(`[${thisData.expand.join(", ")}]`);
2020-02-19 15:55:06 +11:00
}
return query.then(utils.omitRow(omissions()));
2020-02-19 15:55:06 +11:00
})
.then((row) => {
2026-05-13 13:40:06 +10:00
if (!row?.id) {
2025-09-02 21:43:00 +10:00
throw new errs.ItemNotFoundError(thisData.id);
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
const thisRow = internalHost.cleanRowCertificateMeta(row);
// Custom omissions
2025-09-02 21:43:00 +10:00
if (typeof thisData.omit !== "undefined" && thisData.omit !== null) {
return _.omit(row, thisData.omit);
}
2025-09-02 21:43:00 +10:00
return thisRow;
2020-02-19 15:55:06 +11:00
});
},
/**
* @param {Access} access
* @param {Object} data
* @param {Number} data.id
* @param {String} [data.reason]
* @returns {Promise}
*/
delete: (access, data) => {
2025-09-02 21:43:00 +10:00
return access
.can("proxy_hosts:delete", data.id)
2020-02-19 15:55:06 +11:00
.then(() => {
2025-09-02 21:43:00 +10:00
return internalProxyHost.get(access, { id: data.id });
2020-02-19 15:55:06 +11:00
})
.then((row) => {
2026-05-13 13:40:06 +10:00
if (!row?.id) {
2025-09-02 21:43:00 +10:00
throw new errs.ItemNotFoundError(data.id);
2020-02-19 15:55:06 +11:00
}
return proxyHostModel
.query()
2025-09-02 21:43:00 +10:00
.where("id", row.id)
2020-02-19 15:55:06 +11:00
.patch({
2025-09-02 21:43:00 +10:00
is_deleted: 1,
2020-02-19 15:55:06 +11:00
})
.then(() => {
// Delete Nginx Config
2025-09-02 21:43:00 +10:00
return internalNginx.deleteConfig("proxy_host", row).then(() => {
return internalNginx.reload();
});
2020-02-19 15:55:06 +11:00
})
.then(() => {
// Add to audit log
return internalAuditLog.add(access, {
2025-09-02 21:43:00 +10:00
action: "deleted",
object_type: "proxy-host",
object_id: row.id,
meta: _.omit(row, omissions()),
2020-02-19 15:55:06 +11:00
});
});
})
.then(() => {
return true;
});
},
/**
* @param {Access} access
* @param {Object} data
* @param {Number} data.id
* @param {String} [data.reason]
* @returns {Promise}
*/
enable: (access, data) => {
2025-09-02 21:43:00 +10:00
return access
.can("proxy_hosts:update", data.id)
2020-02-19 15:55:06 +11:00
.then(() => {
return internalProxyHost.get(access, {
2025-09-02 21:43:00 +10:00
id: data.id,
expand: ["certificate", "owner", "access_list"],
2020-02-19 15:55:06 +11:00
});
})
.then((row) => {
2026-05-13 13:40:06 +10:00
if (!row?.id) {
2025-09-02 21:43:00 +10:00
throw new errs.ItemNotFoundError(data.id);
}
if (row.enabled) {
throw new errs.ValidationError("Host is already enabled");
2020-02-19 15:55:06 +11:00
}
row.enabled = 1;
return proxyHostModel
.query()
2025-09-02 21:43:00 +10:00
.where("id", row.id)
2020-02-19 15:55:06 +11:00
.patch({
2025-09-02 21:43:00 +10:00
enabled: 1,
2020-02-19 15:55:06 +11:00
})
.then(() => {
// Configure nginx
2025-09-02 21:43:00 +10:00
return internalNginx.configure(proxyHostModel, "proxy_host", row);
2020-02-19 15:55:06 +11:00
})
.then(() => {
// Add to audit log
return internalAuditLog.add(access, {
2025-09-02 21:43:00 +10:00
action: "enabled",
object_type: "proxy-host",
object_id: row.id,
meta: _.omit(row, omissions()),
2020-02-19 15:55:06 +11:00
});
});
})
.then(() => {
return true;
});
},
/**
* @param {Access} access
* @param {Object} data
* @param {Number} data.id
* @param {String} [data.reason]
* @returns {Promise}
*/
disable: (access, data) => {
2025-09-02 21:43:00 +10:00
return access
.can("proxy_hosts:update", data.id)
2020-02-19 15:55:06 +11:00
.then(() => {
2025-09-02 21:43:00 +10:00
return internalProxyHost.get(access, { id: data.id });
2020-02-19 15:55:06 +11:00
})
.then((row) => {
2026-05-13 13:40:06 +10:00
if (!row?.id) {
2025-09-02 21:43:00 +10:00
throw new errs.ItemNotFoundError(data.id);
}
if (!row.enabled) {
throw new errs.ValidationError("Host is already disabled");
2020-02-19 15:55:06 +11:00
}
row.enabled = 0;
return proxyHostModel
.query()
2025-09-02 21:43:00 +10:00
.where("id", row.id)
2020-02-19 15:55:06 +11:00
.patch({
2025-09-02 21:43:00 +10:00
enabled: 0,
2020-02-19 15:55:06 +11:00
})
.then(() => {
// Delete Nginx Config
2025-09-02 21:43:00 +10:00
return internalNginx.deleteConfig("proxy_host", row).then(() => {
return internalNginx.reload();
});
2020-02-19 15:55:06 +11:00
})
.then(() => {
// Add to audit log
return internalAuditLog.add(access, {
2025-09-02 21:43:00 +10:00
action: "disabled",
object_type: "proxy-host",
object_id: row.id,
meta: _.omit(row, omissions()),
2020-02-19 15:55:06 +11:00
});
});
})
.then(() => {
return true;
});
},
/**
* All Hosts
*
* @param {Access} access
* @param {Array} [expand]
* @param {String} [search_query]
* @returns {Promise}
*/
2025-09-10 22:39:00 +10:00
getAll: async (access, expand, searchQuery) => {
const accessData = await access.can("proxy_hosts:list");
2026-03-03 08:44:42 +10:00
2025-09-10 22:39:00 +10:00
const query = proxyHostModel
.query()
.where("is_deleted", 0)
.groupBy("id")
2026-03-03 08:44:42 +10:00
.allowGraph(proxyHostModel.defaultAllowGraph)
2025-09-10 22:39:00 +10:00
.orderBy(castJsonIfNeed("domain_names"), "ASC");
if (accessData.permission_visibility !== "all") {
query.andWhere("owner_user_id", access.token.getUserId(1));
}
2020-02-19 15:55:06 +11:00
2025-09-10 22:39:00 +10:00
// Query is used for searching
if (typeof searchQuery === "string" && searchQuery.length > 0) {
query.where(function () {
this.where(castJsonIfNeed("domain_names"), "like", `%${searchQuery}%`);
});
}
2020-02-19 15:55:06 +11:00
2025-09-10 22:39:00 +10:00
if (typeof expand !== "undefined" && expand !== null) {
query.withGraphFetched(`[${expand.join(", ")}]`);
}
2020-02-19 15:55:06 +11:00
2025-09-10 22:39:00 +10:00
const rows = await query.then(utils.omitRows(omissions()));
if (typeof expand !== "undefined" && expand !== null && expand.indexOf("certificate") !== -1) {
return internalHost.cleanAllRowsCertificateMeta(rows);
}
return rows;
2020-02-19 15:55:06 +11:00
},
/**
* Report use
*
* @param {Number} user_id
* @param {String} visibility
* @returns {Promise}
*/
getCount: (user_id, visibility) => {
2025-09-02 21:43:00 +10:00
const query = proxyHostModel.query().count("id as count").where("is_deleted", 0);
2020-02-19 15:55:06 +11:00
2025-09-02 21:43:00 +10:00
if (visibility !== "all") {
query.andWhere("owner_user_id", user_id);
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
return query.first().then((row) => {
return Number.parseInt(row.count, 10);
});
},
2020-02-19 15:55:06 +11:00
};
2025-09-02 21:43:00 +10:00
export default internalProxyHost;