2018-08-15 23:08:56 -04:00
|
|
|
const Mn = require('backbone.marionette');
|
|
|
|
const App = require('../../main');
|
|
|
|
const DeadHostModel = require('../../../models/dead-host');
|
|
|
|
const template = require('./form.ejs');
|
|
|
|
const certListItemTemplate = require('../certificates-list-item.ejs');
|
|
|
|
const Helpers = require('../../../lib/helpers');
|
2020-10-04 17:56:02 -04:00
|
|
|
const i18n = require('../../i18n');
|
|
|
|
const dns_providers = require('../../../../../utils/certbot-dns-plugins');
|
2018-07-25 18:23:32 -04:00
|
|
|
|
|
|
|
require('jquery-serializejson');
|
|
|
|
require('selectize');
|
|
|
|
|
|
|
|
module.exports = Mn.View.extend({
|
2018-08-15 23:08:56 -04:00
|
|
|
template: template,
|
|
|
|
className: 'modal-dialog',
|
2018-07-25 18:23:32 -04:00
|
|
|
|
|
|
|
ui: {
|
2020-10-04 17:56:02 -04:00
|
|
|
form: 'form',
|
|
|
|
domain_names: 'input[name="domain_names"]',
|
|
|
|
buttons: '.modal-footer button',
|
|
|
|
cancel: 'button.cancel',
|
|
|
|
save: 'button.save',
|
2020-10-06 08:49:02 -04:00
|
|
|
le_error_info: '#le-error-info',
|
2020-10-04 17:56:02 -04:00
|
|
|
certificate_select: 'select[name="certificate_id"]',
|
|
|
|
ssl_forced: 'input[name="ssl_forced"]',
|
|
|
|
hsts_enabled: 'input[name="hsts_enabled"]',
|
|
|
|
hsts_subdomains: 'input[name="hsts_subdomains"]',
|
|
|
|
http2_support: 'input[name="http2_support"]',
|
|
|
|
dns_challenge_switch: 'input[name="meta[dns_challenge]"]',
|
|
|
|
dns_challenge_content: '.dns-challenge',
|
|
|
|
dns_provider: 'select[name="meta[dns_provider]"]',
|
|
|
|
credentials_file_content: '.credentials-file-content',
|
|
|
|
dns_provider_credentials: 'textarea[name="meta[dns_provider_credentials]"]',
|
|
|
|
propagation_seconds: 'input[name="meta[propagation_seconds]"]',
|
|
|
|
letsencrypt: '.letsencrypt'
|
2018-07-25 18:23:32 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
events: {
|
2018-08-15 23:08:56 -04:00
|
|
|
'change @ui.certificate_select': function () {
|
|
|
|
let id = this.ui.certificate_select.val();
|
|
|
|
if (id === 'new') {
|
|
|
|
this.ui.letsencrypt.show().find('input').prop('disabled', false);
|
2020-10-04 17:56:02 -04:00
|
|
|
this.ui.dns_challenge_content.hide();
|
2018-08-15 23:08:56 -04:00
|
|
|
} else {
|
|
|
|
this.ui.letsencrypt.hide().find('input').prop('disabled', true);
|
|
|
|
}
|
2018-07-25 18:23:32 -04:00
|
|
|
|
2020-08-24 05:00:00 -04:00
|
|
|
|
2018-08-15 23:08:56 -04:00
|
|
|
let enabled = id === 'new' || parseInt(id, 10) > 0;
|
2019-02-18 03:21:45 -05:00
|
|
|
|
|
|
|
let inputs = this.ui.ssl_forced.add(this.ui.http2_support);
|
|
|
|
inputs
|
2018-11-12 06:45:23 -05:00
|
|
|
.prop('disabled', !enabled)
|
|
|
|
.parents('.form-group')
|
|
|
|
.css('opacity', enabled ? 1 : 0.5);
|
2019-02-18 03:21:45 -05:00
|
|
|
|
|
|
|
if (!enabled) {
|
|
|
|
inputs.prop('checked', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
inputs.trigger('change');
|
|
|
|
},
|
|
|
|
|
|
|
|
'change @ui.ssl_forced': function () {
|
|
|
|
let checked = this.ui.ssl_forced.prop('checked');
|
|
|
|
this.ui.hsts_enabled
|
|
|
|
.prop('disabled', !checked)
|
|
|
|
.parents('.form-group')
|
|
|
|
.css('opacity', checked ? 1 : 0.5);
|
|
|
|
|
|
|
|
if (!checked) {
|
|
|
|
this.ui.hsts_enabled.prop('checked', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.ui.hsts_enabled.trigger('change');
|
|
|
|
},
|
|
|
|
|
|
|
|
'change @ui.hsts_enabled': function () {
|
|
|
|
let checked = this.ui.hsts_enabled.prop('checked');
|
|
|
|
this.ui.hsts_subdomains
|
|
|
|
.prop('disabled', !checked)
|
|
|
|
.parents('.form-group')
|
|
|
|
.css('opacity', checked ? 1 : 0.5);
|
|
|
|
|
|
|
|
if (!checked) {
|
|
|
|
this.ui.hsts_subdomains.prop('checked', false);
|
|
|
|
}
|
2018-07-25 18:23:32 -04:00
|
|
|
},
|
|
|
|
|
2020-10-04 17:56:02 -04:00
|
|
|
'change @ui.dns_challenge_switch': function () {
|
|
|
|
const checked = this.ui.dns_challenge_switch.prop('checked');
|
|
|
|
if (checked) {
|
|
|
|
this.ui.dns_provider.prop('required', 'required');
|
|
|
|
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){
|
|
|
|
this.ui.dns_provider_credentials.prop('required', 'required');
|
|
|
|
}
|
|
|
|
this.ui.dns_challenge_content.show();
|
|
|
|
} else {
|
|
|
|
this.ui.dns_provider.prop('required', false);
|
|
|
|
this.ui.dns_provider_credentials.prop('required', false);
|
|
|
|
this.ui.dns_challenge_content.hide();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'change @ui.dns_provider': function () {
|
|
|
|
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) {
|
|
|
|
this.ui.dns_provider_credentials.prop('required', 'required');
|
|
|
|
this.ui.dns_provider_credentials[0].value = dns_providers[selected_provider].credentials;
|
|
|
|
this.ui.credentials_file_content.show();
|
|
|
|
} else {
|
|
|
|
this.ui.dns_provider_credentials.prop('required', false);
|
|
|
|
this.ui.credentials_file_content.hide();
|
2020-08-24 05:00:00 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-07-25 18:23:32 -04:00
|
|
|
'click @ui.save': function (e) {
|
|
|
|
e.preventDefault();
|
2020-10-06 08:49:02 -04:00
|
|
|
this.ui.le_error_info.hide();
|
2018-07-25 18:23:32 -04:00
|
|
|
|
|
|
|
if (!this.ui.form[0].checkValidity()) {
|
|
|
|
$('<input type="submit">').hide().appendTo(this.ui.form).click().remove();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let view = this;
|
|
|
|
let data = this.ui.form.serializeJSON();
|
|
|
|
|
|
|
|
// Manipulate
|
2020-10-04 17:56:02 -04:00
|
|
|
data.hsts_enabled = !!data.hsts_enabled;
|
|
|
|
data.hsts_subdomains = !!data.hsts_subdomains;
|
|
|
|
data.http2_support = !!data.http2_support;
|
|
|
|
data.ssl_forced = !!data.ssl_forced;
|
2020-10-06 08:49:02 -04:00
|
|
|
|
|
|
|
if (typeof data.meta === 'undefined') data.meta = {};
|
|
|
|
data.meta.letsencrypt_agree = data.meta.letsencrypt_agree == 1;
|
|
|
|
data.meta.dns_challenge = data.meta.dns_challenge == 1;
|
|
|
|
|
|
|
|
if(!data.meta.dns_challenge){
|
|
|
|
data.meta.dns_provider = undefined;
|
|
|
|
data.meta.dns_provider_credentials = undefined;
|
|
|
|
data.meta.propagation_seconds = undefined;
|
|
|
|
} else {
|
|
|
|
if(data.meta.propagation_seconds === '') data.meta.propagation_seconds = undefined;
|
|
|
|
}
|
2018-11-12 06:45:23 -05:00
|
|
|
|
2018-07-25 18:23:32 -04:00
|
|
|
if (typeof data.domain_names === 'string' && data.domain_names) {
|
|
|
|
data.domain_names = data.domain_names.split(',');
|
|
|
|
}
|
|
|
|
|
2018-08-15 23:08:56 -04:00
|
|
|
// Check for any domain names containing wildcards, which are not allowed with letsencrypt
|
2020-08-24 05:00:00 -04:00
|
|
|
if (data.certificate_id === 'new') {
|
2018-08-15 23:08:56 -04:00
|
|
|
let domain_err = false;
|
2020-10-04 17:56:02 -04:00
|
|
|
if (!data.meta.dns_challenge) {
|
2020-08-24 05:00:00 -04:00
|
|
|
data.domain_names.map(function (name) {
|
|
|
|
if (name.match(/\*/im)) {
|
|
|
|
domain_err = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-08-15 23:08:56 -04:00
|
|
|
|
|
|
|
if (domain_err) {
|
2020-10-04 17:56:02 -04:00
|
|
|
alert(i18n('ssl', 'no-wildcard-without-dns'));
|
2018-08-15 23:08:56 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-11 18:47:12 -05:00
|
|
|
data.certificate_id = parseInt(data.certificate_id, 10);
|
2018-08-15 23:08:56 -04:00
|
|
|
}
|
2018-07-25 18:23:32 -04:00
|
|
|
|
2018-08-15 23:08:56 -04:00
|
|
|
let method = App.Api.Nginx.DeadHosts.create;
|
|
|
|
let is_new = true;
|
2018-07-25 18:23:32 -04:00
|
|
|
|
|
|
|
if (this.model.get('id')) {
|
|
|
|
// edit
|
|
|
|
is_new = false;
|
|
|
|
method = App.Api.Nginx.DeadHosts.update;
|
|
|
|
data.id = this.model.get('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
|
2020-08-24 05:00:00 -04:00
|
|
|
this.ui.save.addClass('btn-loading');
|
|
|
|
|
2018-07-25 18:23:32 -04:00
|
|
|
method(data)
|
|
|
|
.then(result => {
|
|
|
|
view.model.set(result);
|
|
|
|
|
|
|
|
App.UI.closeModal(function () {
|
|
|
|
if (is_new) {
|
|
|
|
App.Controller.showNginxDead();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2020-10-06 08:49:02 -04:00
|
|
|
let more_info = '';
|
|
|
|
if(err.code === 500){
|
|
|
|
try{
|
|
|
|
more_info = JSON.parse(err.debug).debug.stack.join("\n");
|
|
|
|
} 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.show();
|
|
|
|
this.ui.le_error_info[0].scrollIntoView();
|
2018-07-25 18:23:32 -04:00
|
|
|
this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
|
2020-08-24 05:00:00 -04:00
|
|
|
this.ui.save.removeClass('btn-loading');
|
2018-07-25 18:23:32 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
templateContext: {
|
|
|
|
getLetsencryptEmail: function () {
|
2018-08-15 23:08:56 -04:00
|
|
|
return App.Cache.User.get('email');
|
2020-10-04 17:56:02 -04:00
|
|
|
},
|
|
|
|
getUseDnsChallenge: function () {
|
|
|
|
return typeof this.meta.dns_challenge !== 'undefined' ? this.meta.dns_challenge : false;
|
|
|
|
},
|
|
|
|
getDnsProvider: function () {
|
|
|
|
return typeof this.meta.dns_provider !== 'undefined' && this.meta.dns_provider != '' ? this.meta.dns_provider : null;
|
|
|
|
},
|
|
|
|
getDnsProviderCredentials: function () {
|
|
|
|
return typeof this.meta.dns_provider_credentials !== 'undefined' ? this.meta.dns_provider_credentials : '';
|
|
|
|
},
|
|
|
|
getPropagationSeconds: function () {
|
|
|
|
return typeof this.meta.propagation_seconds !== 'undefined' ? this.meta.propagation_seconds : '';
|
|
|
|
},
|
|
|
|
dns_plugins: dns_providers,
|
2018-07-25 18:23:32 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
onRender: function () {
|
2018-08-15 23:08:56 -04:00
|
|
|
let view = this;
|
2018-07-25 18:23:32 -04:00
|
|
|
|
2018-08-15 23:08:56 -04:00
|
|
|
// Domain names
|
2018-07-25 18:23:32 -04:00
|
|
|
this.ui.domain_names.selectize({
|
|
|
|
delimiter: ',',
|
|
|
|
persist: false,
|
|
|
|
maxOptions: 15,
|
|
|
|
create: function (input) {
|
|
|
|
return {
|
|
|
|
value: input,
|
|
|
|
text: input
|
|
|
|
};
|
|
|
|
},
|
|
|
|
createFilter: /^(?:\*\.)?(?:[^.*]+\.?)+[^.]$/
|
|
|
|
});
|
2018-08-15 23:08:56 -04:00
|
|
|
|
|
|
|
// Certificates
|
2020-10-06 08:49:02 -04:00
|
|
|
this.ui.le_error_info.hide();
|
2020-10-04 17:56:02 -04:00
|
|
|
this.ui.dns_challenge_content.hide();
|
|
|
|
this.ui.credentials_file_content.hide();
|
2018-08-15 23:08:56 -04:00
|
|
|
this.ui.letsencrypt.hide();
|
|
|
|
this.ui.certificate_select.selectize({
|
|
|
|
valueField: 'id',
|
|
|
|
labelField: 'nice_name',
|
|
|
|
searchField: ['nice_name', 'domain_names'],
|
|
|
|
create: false,
|
|
|
|
preload: true,
|
|
|
|
allowEmptyOption: true,
|
|
|
|
render: {
|
|
|
|
option: function (item) {
|
|
|
|
item.i18n = App.i18n;
|
|
|
|
item.formatDbDate = Helpers.formatDbDate;
|
|
|
|
return certListItemTemplate(item);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
load: function (query, callback) {
|
|
|
|
App.Api.Nginx.Certificates.getAll()
|
|
|
|
.then(rows => {
|
|
|
|
callback(rows);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onLoad: function () {
|
|
|
|
view.ui.certificate_select[0].selectize.setValue(view.model.get('certificate_id'));
|
|
|
|
}
|
|
|
|
});
|
2018-07-25 18:23:32 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function (options) {
|
|
|
|
if (typeof options.model === 'undefined' || !options.model) {
|
|
|
|
this.model = new DeadHostModel.Model();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|