mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2025-09-14 10:52:34 +00:00
Convert backend to ESM
- About 5 years overdue - Remove eslint, use bomejs instead
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
const _ = require('lodash');
|
||||
const proxyHostModel = require('../models/proxy_host');
|
||||
const redirectionHostModel = require('../models/redirection_host');
|
||||
const deadHostModel = require('../models/dead_host');
|
||||
const {castJsonIfNeed} = require('../lib/helpers');
|
||||
import _ from "lodash";
|
||||
import { castJsonIfNeed } from "../lib/helpers.js";
|
||||
import deadHostModel from "../models/dead_host.js";
|
||||
import proxyHostModel from "../models/proxy_host.js";
|
||||
import redirectionHostModel from "../models/redirection_host.js";
|
||||
|
||||
const internalHost = {
|
||||
|
||||
/**
|
||||
* Makes sure that the ssl_* and hsts_* fields play nicely together.
|
||||
* ie: if there is no cert, then force_ssl is off.
|
||||
@@ -15,25 +14,23 @@ const internalHost = {
|
||||
* @param {object} [existing_data]
|
||||
* @returns {object}
|
||||
*/
|
||||
cleanSslHstsData: function (data, existing_data) {
|
||||
existing_data = existing_data === undefined ? {} : existing_data;
|
||||
cleanSslHstsData: (data, existingData) => {
|
||||
const combinedData = _.assign({}, existingData || {}, data);
|
||||
|
||||
const combined_data = _.assign({}, existing_data, data);
|
||||
|
||||
if (!combined_data.certificate_id) {
|
||||
combined_data.ssl_forced = false;
|
||||
combined_data.http2_support = false;
|
||||
if (!combinedData.certificate_id) {
|
||||
combinedData.ssl_forced = false;
|
||||
combinedData.http2_support = false;
|
||||
}
|
||||
|
||||
if (!combined_data.ssl_forced) {
|
||||
combined_data.hsts_enabled = false;
|
||||
if (!combinedData.ssl_forced) {
|
||||
combinedData.hsts_enabled = false;
|
||||
}
|
||||
|
||||
if (!combined_data.hsts_enabled) {
|
||||
combined_data.hsts_subdomains = false;
|
||||
if (!combinedData.hsts_enabled) {
|
||||
combinedData.hsts_subdomains = false;
|
||||
}
|
||||
|
||||
return combined_data;
|
||||
return combinedData;
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -42,11 +39,12 @@ const internalHost = {
|
||||
* @param {Array} rows
|
||||
* @returns {Array}
|
||||
*/
|
||||
cleanAllRowsCertificateMeta: function (rows) {
|
||||
rows.map(function (row, idx) {
|
||||
if (typeof rows[idx].certificate !== 'undefined' && rows[idx].certificate) {
|
||||
cleanAllRowsCertificateMeta: (rows) => {
|
||||
rows.map((_, idx) => {
|
||||
if (typeof rows[idx].certificate !== "undefined" && rows[idx].certificate) {
|
||||
rows[idx].certificate.meta = {};
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
return rows;
|
||||
@@ -58,8 +56,8 @@ const internalHost = {
|
||||
* @param {Object} row
|
||||
* @returns {Object}
|
||||
*/
|
||||
cleanRowCertificateMeta: function (row) {
|
||||
if (typeof row.certificate !== 'undefined' && row.certificate) {
|
||||
cleanRowCertificateMeta: (row) => {
|
||||
if (typeof row.certificate !== "undefined" && row.certificate) {
|
||||
row.certificate.meta = {};
|
||||
}
|
||||
|
||||
@@ -73,48 +71,44 @@ const internalHost = {
|
||||
* @param {Array} domain_names
|
||||
* @returns {Promise}
|
||||
*/
|
||||
getHostsWithDomains: function (domain_names) {
|
||||
getHostsWithDomains: (domain_names) => {
|
||||
const promises = [
|
||||
proxyHostModel
|
||||
.query()
|
||||
.where('is_deleted', 0),
|
||||
redirectionHostModel
|
||||
.query()
|
||||
.where('is_deleted', 0),
|
||||
deadHostModel
|
||||
.query()
|
||||
.where('is_deleted', 0)
|
||||
proxyHostModel.query().where("is_deleted", 0),
|
||||
redirectionHostModel.query().where("is_deleted", 0),
|
||||
deadHostModel.query().where("is_deleted", 0),
|
||||
];
|
||||
|
||||
return Promise.all(promises)
|
||||
.then((promises_results) => {
|
||||
let response_object = {
|
||||
total_count: 0,
|
||||
dead_hosts: [],
|
||||
proxy_hosts: [],
|
||||
redirection_hosts: []
|
||||
};
|
||||
return Promise.all(promises).then((promises_results) => {
|
||||
const response_object = {
|
||||
total_count: 0,
|
||||
dead_hosts: [],
|
||||
proxy_hosts: [],
|
||||
redirection_hosts: [],
|
||||
};
|
||||
|
||||
if (promises_results[0]) {
|
||||
// Proxy Hosts
|
||||
response_object.proxy_hosts = internalHost._getHostsWithDomains(promises_results[0], domain_names);
|
||||
response_object.total_count += response_object.proxy_hosts.length;
|
||||
}
|
||||
if (promises_results[0]) {
|
||||
// Proxy Hosts
|
||||
response_object.proxy_hosts = internalHost._getHostsWithDomains(promises_results[0], domain_names);
|
||||
response_object.total_count += response_object.proxy_hosts.length;
|
||||
}
|
||||
|
||||
if (promises_results[1]) {
|
||||
// Redirection Hosts
|
||||
response_object.redirection_hosts = internalHost._getHostsWithDomains(promises_results[1], domain_names);
|
||||
response_object.total_count += response_object.redirection_hosts.length;
|
||||
}
|
||||
if (promises_results[1]) {
|
||||
// Redirection Hosts
|
||||
response_object.redirection_hosts = internalHost._getHostsWithDomains(
|
||||
promises_results[1],
|
||||
domain_names,
|
||||
);
|
||||
response_object.total_count += response_object.redirection_hosts.length;
|
||||
}
|
||||
|
||||
if (promises_results[2]) {
|
||||
// Dead Hosts
|
||||
response_object.dead_hosts = internalHost._getHostsWithDomains(promises_results[2], domain_names);
|
||||
response_object.total_count += response_object.dead_hosts.length;
|
||||
}
|
||||
if (promises_results[2]) {
|
||||
// Dead Hosts
|
||||
response_object.dead_hosts = internalHost._getHostsWithDomains(promises_results[2], domain_names);
|
||||
response_object.total_count += response_object.dead_hosts.length;
|
||||
}
|
||||
|
||||
return response_object;
|
||||
});
|
||||
return response_object;
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -125,112 +119,133 @@ const internalHost = {
|
||||
* @param {Integer} [ignore_id] Must be supplied if type was also supplied
|
||||
* @returns {Promise}
|
||||
*/
|
||||
isHostnameTaken: function (hostname, ignore_type, ignore_id) {
|
||||
isHostnameTaken: (hostname, ignore_type, ignore_id) => {
|
||||
const promises = [
|
||||
proxyHostModel
|
||||
.query()
|
||||
.where('is_deleted', 0)
|
||||
.andWhere(castJsonIfNeed('domain_names'), 'like', '%' + hostname + '%'),
|
||||
.where("is_deleted", 0)
|
||||
.andWhere(castJsonIfNeed("domain_names"), "like", `%${hostname}%`),
|
||||
redirectionHostModel
|
||||
.query()
|
||||
.where('is_deleted', 0)
|
||||
.andWhere(castJsonIfNeed('domain_names'), 'like', '%' + hostname + '%'),
|
||||
.where("is_deleted", 0)
|
||||
.andWhere(castJsonIfNeed("domain_names"), "like", `%${hostname}%`),
|
||||
deadHostModel
|
||||
.query()
|
||||
.where('is_deleted', 0)
|
||||
.andWhere(castJsonIfNeed('domain_names'), 'like', '%' + hostname + '%')
|
||||
.where("is_deleted", 0)
|
||||
.andWhere(castJsonIfNeed("domain_names"), "like", `%${hostname}%`),
|
||||
];
|
||||
|
||||
return Promise.all(promises)
|
||||
.then((promises_results) => {
|
||||
let is_taken = false;
|
||||
return Promise.all(promises).then((promises_results) => {
|
||||
let is_taken = false;
|
||||
|
||||
if (promises_results[0]) {
|
||||
// Proxy Hosts
|
||||
if (internalHost._checkHostnameRecordsTaken(hostname, promises_results[0], ignore_type === 'proxy' && ignore_id ? ignore_id : 0)) {
|
||||
is_taken = true;
|
||||
}
|
||||
if (promises_results[0]) {
|
||||
// Proxy Hosts
|
||||
if (
|
||||
internalHost._checkHostnameRecordsTaken(
|
||||
hostname,
|
||||
promises_results[0],
|
||||
ignore_type === "proxy" && ignore_id ? ignore_id : 0,
|
||||
)
|
||||
) {
|
||||
is_taken = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (promises_results[1]) {
|
||||
// Redirection Hosts
|
||||
if (internalHost._checkHostnameRecordsTaken(hostname, promises_results[1], ignore_type === 'redirection' && ignore_id ? ignore_id : 0)) {
|
||||
is_taken = true;
|
||||
}
|
||||
if (promises_results[1]) {
|
||||
// Redirection Hosts
|
||||
if (
|
||||
internalHost._checkHostnameRecordsTaken(
|
||||
hostname,
|
||||
promises_results[1],
|
||||
ignore_type === "redirection" && ignore_id ? ignore_id : 0,
|
||||
)
|
||||
) {
|
||||
is_taken = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (promises_results[2]) {
|
||||
// Dead Hosts
|
||||
if (internalHost._checkHostnameRecordsTaken(hostname, promises_results[2], ignore_type === 'dead' && ignore_id ? ignore_id : 0)) {
|
||||
is_taken = true;
|
||||
}
|
||||
if (promises_results[2]) {
|
||||
// Dead Hosts
|
||||
if (
|
||||
internalHost._checkHostnameRecordsTaken(
|
||||
hostname,
|
||||
promises_results[2],
|
||||
ignore_type === "dead" && ignore_id ? ignore_id : 0,
|
||||
)
|
||||
) {
|
||||
is_taken = true;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
hostname: hostname,
|
||||
is_taken: is_taken
|
||||
};
|
||||
});
|
||||
return {
|
||||
hostname: hostname,
|
||||
is_taken: is_taken,
|
||||
};
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Private call only
|
||||
*
|
||||
* @param {String} hostname
|
||||
* @param {Array} existing_rows
|
||||
* @param {Integer} [ignore_id]
|
||||
* @param {Array} existingRows
|
||||
* @param {Integer} [ignoreId]
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
_checkHostnameRecordsTaken: function (hostname, existing_rows, ignore_id) {
|
||||
let is_taken = false;
|
||||
_checkHostnameRecordsTaken: (hostname, existingRows, ignoreId) => {
|
||||
let isTaken = false;
|
||||
|
||||
if (existing_rows && existing_rows.length) {
|
||||
existing_rows.map(function (existing_row) {
|
||||
existing_row.domain_names.map(function (existing_hostname) {
|
||||
if (existingRows?.length) {
|
||||
existingRows.map((existingRow) => {
|
||||
existingRow.domain_names.map((existingHostname) => {
|
||||
// Does this domain match?
|
||||
if (existing_hostname.toLowerCase() === hostname.toLowerCase()) {
|
||||
if (!ignore_id || ignore_id !== existing_row.id) {
|
||||
is_taken = true;
|
||||
if (existingHostname.toLowerCase() === hostname.toLowerCase()) {
|
||||
if (!ignoreId || ignoreId !== existingRow.id) {
|
||||
isTaken = true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
return is_taken;
|
||||
return isTaken;
|
||||
},
|
||||
|
||||
/**
|
||||
* Private call only
|
||||
*
|
||||
* @param {Array} hosts
|
||||
* @param {Array} domain_names
|
||||
* @param {Array} domainNames
|
||||
* @returns {Array}
|
||||
*/
|
||||
_getHostsWithDomains: function (hosts, domain_names) {
|
||||
let response = [];
|
||||
_getHostsWithDomains: (hosts, domainNames) => {
|
||||
const response = [];
|
||||
|
||||
if (hosts && hosts.length) {
|
||||
hosts.map(function (host) {
|
||||
let host_matches = false;
|
||||
if (hosts?.length) {
|
||||
hosts.map((host) => {
|
||||
let hostMatches = false;
|
||||
|
||||
domain_names.map(function (domain_name) {
|
||||
host.domain_names.map(function (host_domain_name) {
|
||||
if (domain_name.toLowerCase() === host_domain_name.toLowerCase()) {
|
||||
host_matches = true;
|
||||
domainNames.map((domainName) => {
|
||||
host.domain_names.map((hostDomainName) => {
|
||||
if (domainName.toLowerCase() === hostDomainName.toLowerCase()) {
|
||||
hostMatches = true;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
return true;
|
||||
});
|
||||
|
||||
if (host_matches) {
|
||||
if (hostMatches) {
|
||||
response.push(host);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = internalHost;
|
||||
export default internalHost;
|
||||
|
Reference in New Issue
Block a user