Allow * (Wildcard) Domains to be used

This commit is contained in:
Thyke Adams 2021-05-08 19:34:28 -04:00
parent ba45705571
commit e67d9de6dc

View file

@ -1,35 +1,34 @@
const Mn = require('backbone.marionette'); const Mn = require("backbone.marionette");
const App = require('../../main'); const App = require("../../main");
const ProxyHostModel = require('../../../models/proxy-host'); const ProxyHostModel = require("../../../models/proxy-host");
const ProxyLocationModel = require('../../../models/proxy-host-location'); const ProxyLocationModel = require("../../../models/proxy-host-location");
const template = require('./form.ejs'); const template = require("./form.ejs");
const certListItemTemplate = require('../certificates-list-item.ejs'); const certListItemTemplate = require("../certificates-list-item.ejs");
const accessListItemTemplate = require('./access-list-item.ejs'); const accessListItemTemplate = require("./access-list-item.ejs");
const CustomLocation = require('./location'); const CustomLocation = require("./location");
const Helpers = require('../../../lib/helpers'); const Helpers = require("../../../lib/helpers");
const i18n = require('../../i18n'); const i18n = require("../../i18n");
const dns_providers = require('../../../../../global/certbot-dns-plugins'); const dns_providers = require("../../../../../global/certbot-dns-plugins");
require("jquery-serializejson");
require('jquery-serializejson'); require("selectize");
require('selectize');
module.exports = Mn.View.extend({ module.exports = Mn.View.extend({
template: template, template: template,
className: 'modal-dialog', className: "modal-dialog",
locationsCollection: new ProxyLocationModel.Collection(), locationsCollection: new ProxyLocationModel.Collection(),
ui: { ui: {
form: 'form', form: "form",
domain_names: 'input[name="domain_names"]', domain_names: 'input[name="domain_names"]',
forward_host: 'input[name="forward_host"]', forward_host: 'input[name="forward_host"]',
buttons: '.modal-footer button', buttons: ".modal-footer button",
cancel: 'button.cancel', cancel: "button.cancel",
save: 'button.save', save: "button.save",
add_location_btn: 'button.add_location', add_location_btn: "button.add_location",
locations_container: '.locations_container', locations_container: ".locations_container",
le_error_info: '#le-error-info', le_error_info: "#le-error-info",
certificate_select: 'select[name="certificate_id"]', certificate_select: 'select[name="certificate_id"]',
access_list_select: 'select[name="access_list_id"]', access_list_select: 'select[name="access_list_id"]',
ssl_forced: 'input[name="ssl_forced"]', ssl_forced: 'input[name="ssl_forced"]',
@ -37,106 +36,106 @@ module.exports = Mn.View.extend({
hsts_subdomains: 'input[name="hsts_subdomains"]', hsts_subdomains: 'input[name="hsts_subdomains"]',
http2_support: 'input[name="http2_support"]', http2_support: 'input[name="http2_support"]',
dns_challenge_switch: 'input[name="meta[dns_challenge]"]', dns_challenge_switch: 'input[name="meta[dns_challenge]"]',
dns_challenge_content: '.dns-challenge', dns_challenge_content: ".dns-challenge",
dns_provider: 'select[name="meta[dns_provider]"]', dns_provider: 'select[name="meta[dns_provider]"]',
credentials_file_content: '.credentials-file-content', credentials_file_content: ".credentials-file-content",
dns_provider_credentials: 'textarea[name="meta[dns_provider_credentials]"]', dns_provider_credentials: 'textarea[name="meta[dns_provider_credentials]"]',
propagation_seconds: 'input[name="meta[propagation_seconds]"]', propagation_seconds: 'input[name="meta[propagation_seconds]"]',
forward_scheme: 'select[name="forward_scheme"]', forward_scheme: 'select[name="forward_scheme"]',
letsencrypt: '.letsencrypt' letsencrypt: ".letsencrypt"
}, },
regions: { regions: {
locations_regions: '@ui.locations_container' locations_regions: "@ui.locations_container"
}, },
events: { events: {
'change @ui.certificate_select': function () { "change @ui.certificate_select": function () {
let id = this.ui.certificate_select.val(); let id = this.ui.certificate_select.val();
if (id === 'new') { if (id === "new") {
this.ui.letsencrypt.show().find('input').prop('disabled', false); this.ui.letsencrypt.show().find("input").prop("disabled", false);
this.ui.dns_challenge_content.hide(); this.ui.dns_challenge_content.hide();
} else { } else {
this.ui.letsencrypt.hide().find('input').prop('disabled', true); this.ui.letsencrypt.hide().find("input").prop("disabled", true);
} }
let enabled = id === 'new' || parseInt(id, 10) > 0; let enabled = id === "new" || parseInt(id, 10) > 0;
let inputs = this.ui.ssl_forced.add(this.ui.http2_support); let inputs = this.ui.ssl_forced.add(this.ui.http2_support);
inputs inputs
.prop('disabled', !enabled) .prop("disabled", !enabled)
.parents('.form-group') .parents(".form-group")
.css('opacity', enabled ? 1 : 0.5); .css("opacity", enabled ? 1 : 0.5);
if (!enabled) { if (!enabled) {
inputs.prop('checked', false); inputs.prop("checked", false);
} }
inputs.trigger('change'); inputs.trigger("change");
}, },
'change @ui.ssl_forced': function () { "change @ui.ssl_forced": function () {
let checked = this.ui.ssl_forced.prop('checked'); let checked = this.ui.ssl_forced.prop("checked");
this.ui.hsts_enabled this.ui.hsts_enabled
.prop('disabled', !checked) .prop("disabled", !checked)
.parents('.form-group') .parents(".form-group")
.css('opacity', checked ? 1 : 0.5); .css("opacity", checked ? 1 : 0.5);
if (!checked) { if (!checked) {
this.ui.hsts_enabled.prop('checked', false); this.ui.hsts_enabled.prop("checked", false);
} }
this.ui.hsts_enabled.trigger('change'); this.ui.hsts_enabled.trigger("change");
}, },
'change @ui.hsts_enabled': function () { "change @ui.hsts_enabled": function () {
let checked = this.ui.hsts_enabled.prop('checked'); let checked = this.ui.hsts_enabled.prop("checked");
this.ui.hsts_subdomains this.ui.hsts_subdomains
.prop('disabled', !checked) .prop("disabled", !checked)
.parents('.form-group') .parents(".form-group")
.css('opacity', checked ? 1 : 0.5); .css("opacity", checked ? 1 : 0.5);
if (!checked) { if (!checked) {
this.ui.hsts_subdomains.prop('checked', false); this.ui.hsts_subdomains.prop("checked", false);
} }
}, },
'change @ui.dns_challenge_switch': function () { "change @ui.dns_challenge_switch": function () {
const checked = this.ui.dns_challenge_switch.prop('checked'); const checked = this.ui.dns_challenge_switch.prop("checked");
if (checked) { if (checked) {
this.ui.dns_provider.prop('required', 'required'); this.ui.dns_provider.prop("required", "required");
const selected_provider = this.ui.dns_provider[0].options[this.ui.dns_provider[0].selectedIndex].value; const selected_provider = this.ui.dns_provider[0].options[this.ui.dns_provider[0].selectedIndex].value;
if(selected_provider != '' && dns_providers[selected_provider].credentials !== false){ if (selected_provider != "" && dns_providers[selected_provider].credentials !== false) {
this.ui.dns_provider_credentials.prop('required', 'required'); this.ui.dns_provider_credentials.prop("required", "required");
} }
this.ui.dns_challenge_content.show(); this.ui.dns_challenge_content.show();
} else { } else {
this.ui.dns_provider.prop('required', false); this.ui.dns_provider.prop("required", false);
this.ui.dns_provider_credentials.prop('required', false); this.ui.dns_provider_credentials.prop("required", false);
this.ui.dns_challenge_content.hide(); this.ui.dns_challenge_content.hide();
} }
}, },
'change @ui.dns_provider': function () { "change @ui.dns_provider": function () {
const selected_provider = this.ui.dns_provider[0].options[this.ui.dns_provider[0].selectedIndex].value; const selected_provider = this.ui.dns_provider[0].options[this.ui.dns_provider[0].selectedIndex].value;
if (selected_provider != '' && dns_providers[selected_provider].credentials !== false) { if (selected_provider != "" && dns_providers[selected_provider].credentials !== false) {
this.ui.dns_provider_credentials.prop('required', 'required'); this.ui.dns_provider_credentials.prop("required", "required");
this.ui.dns_provider_credentials[0].value = dns_providers[selected_provider].credentials; this.ui.dns_provider_credentials[0].value = dns_providers[selected_provider].credentials;
this.ui.credentials_file_content.show(); this.ui.credentials_file_content.show();
} else { } else {
this.ui.dns_provider_credentials.prop('required', false); this.ui.dns_provider_credentials.prop("required", false);
this.ui.credentials_file_content.hide(); this.ui.credentials_file_content.hide();
} }
}, },
'click @ui.add_location_btn': function (e) { "click @ui.add_location_btn": function (e) {
e.preventDefault(); e.preventDefault();
const model = new ProxyLocationModel.Model(); const model = new ProxyLocationModel.Model();
this.locationsCollection.add(model); this.locationsCollection.add(model);
}, },
'click @ui.save': function (e) { "click @ui.save": function (e) {
e.preventDefault(); e.preventDefault();
this.ui.le_error_info.hide(); this.ui.le_error_info.hide();
@ -168,7 +167,7 @@ module.exports = Mn.View.extend({
data.hsts_subdomains = !!data.hsts_subdomains; data.hsts_subdomains = !!data.hsts_subdomains;
data.ssl_forced = !!data.ssl_forced; data.ssl_forced = !!data.ssl_forced;
if (typeof data.meta === 'undefined') data.meta = {}; if (typeof data.meta === "undefined") data.meta = {};
data.meta.letsencrypt_agree = data.meta.letsencrypt_agree == 1; data.meta.letsencrypt_agree = data.meta.letsencrypt_agree == 1;
data.meta.dns_challenge = data.meta.dns_challenge == 1; data.meta.dns_challenge = data.meta.dns_challenge == 1;
@ -177,15 +176,15 @@ module.exports = Mn.View.extend({
data.meta.dns_provider_credentials = undefined; data.meta.dns_provider_credentials = undefined;
data.meta.propagation_seconds = undefined; data.meta.propagation_seconds = undefined;
} else { } else {
if(data.meta.propagation_seconds === '') data.meta.propagation_seconds = undefined; if (data.meta.propagation_seconds === "") data.meta.propagation_seconds = undefined;
} }
if (typeof data.domain_names === 'string' && data.domain_names) { if (typeof data.domain_names === "string" && data.domain_names) {
data.domain_names = data.domain_names.split(','); data.domain_names = data.domain_names.split(",");
} }
// Check for any domain names containing wildcards, which are not allowed with letsencrypt // Check for any domain names containing wildcards, which are not allowed with letsencrypt
if (data.certificate_id === 'new') { if (data.certificate_id === "new") {
let domain_err = false; let domain_err = false;
if (!data.meta.dns_challenge) { if (!data.meta.dns_challenge) {
data.domain_names.map(function (name) { data.domain_names.map(function (name) {
@ -196,7 +195,7 @@ module.exports = Mn.View.extend({
} }
if (domain_err) { if (domain_err) {
alert(i18n('ssl', 'no-wildcard-without-dns')); alert(i18n("ssl", "no-wildcard-without-dns"));
return; return;
} }
} else { } else {
@ -206,18 +205,18 @@ module.exports = Mn.View.extend({
let method = App.Api.Nginx.ProxyHosts.create; let method = App.Api.Nginx.ProxyHosts.create;
let is_new = true; let is_new = true;
if (this.model.get('id')) { if (this.model.get("id")) {
// edit // edit
is_new = false; is_new = false;
method = App.Api.Nginx.ProxyHosts.update; method = App.Api.Nginx.ProxyHosts.update;
data.id = this.model.get('id'); data.id = this.model.get("id");
} }
this.ui.buttons.prop('disabled', true).addClass('btn-disabled'); this.ui.buttons.prop("disabled", true).addClass("btn-disabled");
this.ui.save.addClass('btn-loading'); this.ui.save.addClass("btn-loading");
method(data) method(data)
.then(result => { .then((result) => {
view.model.set(result); view.model.set(result);
App.UI.closeModal(function () { App.UI.closeModal(function () {
@ -226,50 +225,50 @@ module.exports = Mn.View.extend({
} }
}); });
}) })
.catch(err => { .catch((err) => {
let more_info = ''; let more_info = "";
if (err.code === 500 && err.debug) { if (err.code === 500 && err.debug) {
try { try {
more_info = JSON.parse(err.debug).debug.stack.join("\n"); more_info = JSON.parse(err.debug).debug.stack.join("\n");
} catch (e) {} } catch (e) {}
} }
this.ui.le_error_info[0].innerHTML = `${err.message}${more_info !== '' ? `<pre class="mt-3">${more_info}</pre>`:''}`; this.ui.le_error_info[0].innerHTML = `${err.message}${more_info !== "" ? `<pre class="mt-3">${more_info}</pre>` : ""}`;
this.ui.le_error_info.show(); this.ui.le_error_info.show();
this.ui.le_error_info[0].scrollIntoView(); this.ui.le_error_info[0].scrollIntoView();
this.ui.buttons.prop('disabled', false).removeClass('btn-disabled'); this.ui.buttons.prop("disabled", false).removeClass("btn-disabled");
this.ui.save.removeClass('btn-loading'); this.ui.save.removeClass("btn-loading");
}); });
} }
}, },
templateContext: { templateContext: {
getLetsencryptEmail: function () { getLetsencryptEmail: function () {
return App.Cache.User.get('email'); return App.Cache.User.get("email");
}, },
getUseDnsChallenge: function () { getUseDnsChallenge: function () {
return typeof this.meta.dns_challenge !== 'undefined' ? this.meta.dns_challenge : false; return typeof this.meta.dns_challenge !== "undefined" ? this.meta.dns_challenge : false;
}, },
getDnsProvider: function () { getDnsProvider: function () {
return typeof this.meta.dns_provider !== 'undefined' && this.meta.dns_provider != '' ? this.meta.dns_provider : null; return typeof this.meta.dns_provider !== "undefined" && this.meta.dns_provider != "" ? this.meta.dns_provider : null;
}, },
getDnsProviderCredentials: function () { getDnsProviderCredentials: function () {
return typeof this.meta.dns_provider_credentials !== 'undefined' ? this.meta.dns_provider_credentials : ''; return typeof this.meta.dns_provider_credentials !== "undefined" ? this.meta.dns_provider_credentials : "";
}, },
getPropagationSeconds: function () { getPropagationSeconds: function () {
return typeof this.meta.propagation_seconds !== 'undefined' ? this.meta.propagation_seconds : ''; return typeof this.meta.propagation_seconds !== "undefined" ? this.meta.propagation_seconds : "";
}, },
dns_plugins: dns_providers, dns_plugins: dns_providers
}, },
onRender: function () { onRender: function () {
let view = this; let view = this;
this.ui.ssl_forced.trigger('change'); this.ui.ssl_forced.trigger("change");
this.ui.hsts_enabled.trigger('change'); this.ui.hsts_enabled.trigger("change");
// Domain names // Domain names
this.ui.domain_names.selectize({ this.ui.domain_names.selectize({
delimiter: ',', delimiter: ",",
persist: false, persist: false,
maxOptions: 15, maxOptions: 15,
create: function (input) { create: function (input) {
@ -278,14 +277,14 @@ module.exports = Mn.View.extend({
text: input text: input
}; };
}, },
createFilter: /^(?:\.)?(?:[^.*]+\.?)+[^.]$/ createFilter: /^(?:[^.]+\.?)+[^.]$/
}); });
// Access Lists // Access Lists
this.ui.access_list_select.selectize({ this.ui.access_list_select.selectize({
valueField: 'id', valueField: "id",
labelField: 'name', labelField: "name",
searchField: ['name'], searchField: ["name"],
create: false, create: false,
preload: true, preload: true,
allowEmptyOption: true, allowEmptyOption: true,
@ -297,17 +296,17 @@ module.exports = Mn.View.extend({
} }
}, },
load: function (query, callback) { load: function (query, callback) {
App.Api.Nginx.AccessLists.getAll(['items', 'clients']) App.Api.Nginx.AccessLists.getAll(["items", "clients"])
.then(rows => { .then((rows) => {
callback(rows); callback(rows);
}) })
.catch(err => { .catch((err) => {
console.error(err); console.error(err);
callback(); callback();
}); });
}, },
onLoad: function () { onLoad: function () {
view.ui.access_list_select[0].selectize.setValue(view.model.get('access_list_id')); view.ui.access_list_select[0].selectize.setValue(view.model.get("access_list_id"));
} }
}); });
@ -317,9 +316,9 @@ module.exports = Mn.View.extend({
this.ui.credentials_file_content.hide(); this.ui.credentials_file_content.hide();
this.ui.letsencrypt.hide(); this.ui.letsencrypt.hide();
this.ui.certificate_select.selectize({ this.ui.certificate_select.selectize({
valueField: 'id', valueField: "id",
labelField: 'nice_name', labelField: "nice_name",
searchField: ['nice_name', 'domain_names'], searchField: ["nice_name", "domain_names"],
create: false, create: false,
preload: true, preload: true,
allowEmptyOption: true, allowEmptyOption: true,
@ -332,31 +331,34 @@ module.exports = Mn.View.extend({
}, },
load: function (query, callback) { load: function (query, callback) {
App.Api.Nginx.Certificates.getAll() App.Api.Nginx.Certificates.getAll()
.then(rows => { .then((rows) => {
callback(rows); callback(rows);
}) })
.catch(err => { .catch((err) => {
console.error(err); console.error(err);
callback(); callback();
}); });
}, },
onLoad: function () { onLoad: function () {
view.ui.certificate_select[0].selectize.setValue(view.model.get('certificate_id')); view.ui.certificate_select[0].selectize.setValue(view.model.get("certificate_id"));
} }
}); });
}, },
initialize: function (options) { initialize: function (options) {
if (typeof options.model === 'undefined' || !options.model) { if (typeof options.model === "undefined" || !options.model) {
this.model = new ProxyHostModel.Model(); this.model = new ProxyHostModel.Model();
} }
this.locationsCollection = new ProxyLocationModel.Collection(); this.locationsCollection = new ProxyLocationModel.Collection();
// Custom locations // Custom locations
this.showChildView('locations_regions', new CustomLocation.LocationCollectionView({ this.showChildView(
"locations_regions",
new CustomLocation.LocationCollectionView({
collection: this.locationsCollection collection: this.locationsCollection
})); })
);
// Check wether there are any location defined // Check wether there are any location defined
if (options.model && Array.isArray(options.model.attributes.locations)) { if (options.model && Array.isArray(options.model.attributes.locations)) {