|
@@ -1,49 +1,41 @@
|
|
|
'use strict';
|
|
'use strict';
|
|
|
|
|
|
|
|
-const _ = require('underscore');
|
|
|
|
|
const Mn = require('backbone.marionette');
|
|
const Mn = require('backbone.marionette');
|
|
|
const App = require('../../main');
|
|
const App = require('../../main');
|
|
|
const RedirectionHostModel = require('../../../models/redirection-host');
|
|
const RedirectionHostModel = require('../../../models/redirection-host');
|
|
|
const template = require('./form.ejs');
|
|
const template = require('./form.ejs');
|
|
|
|
|
+const certListItemTemplate = require('../certificates-list-item.ejs');
|
|
|
|
|
+const Helpers = require('../../../lib/helpers');
|
|
|
|
|
|
|
|
require('jquery-serializejson');
|
|
require('jquery-serializejson');
|
|
|
require('selectize');
|
|
require('selectize');
|
|
|
|
|
|
|
|
module.exports = Mn.View.extend({
|
|
module.exports = Mn.View.extend({
|
|
|
- template: template,
|
|
|
|
|
- className: 'modal-dialog',
|
|
|
|
|
- max_file_size: 5120,
|
|
|
|
|
|
|
+ template: template,
|
|
|
|
|
+ className: 'modal-dialog',
|
|
|
|
|
|
|
|
ui: {
|
|
ui: {
|
|
|
- form: 'form',
|
|
|
|
|
- domain_names: 'input[name="domain_names"]',
|
|
|
|
|
- buttons: '.modal-footer button',
|
|
|
|
|
- cancel: 'button.cancel',
|
|
|
|
|
- save: 'button.save',
|
|
|
|
|
- ssl_enabled: 'input[name="ssl_enabled"]',
|
|
|
|
|
- ssl_options: '#ssl-options input',
|
|
|
|
|
- ssl_provider: 'input[name="ssl_provider"]',
|
|
|
|
|
- other_ssl_certificate: '#other_ssl_certificate',
|
|
|
|
|
- other_ssl_certificate_key: '#other_ssl_certificate_key',
|
|
|
|
|
-
|
|
|
|
|
- // SSL hiding and showing
|
|
|
|
|
- all_ssl: '.letsencrypt-ssl, .other-ssl',
|
|
|
|
|
- letsencrypt_ssl: '.letsencrypt-ssl',
|
|
|
|
|
- other_ssl: '.other-ssl'
|
|
|
|
|
|
|
+ form: 'form',
|
|
|
|
|
+ domain_names: 'input[name="domain_names"]',
|
|
|
|
|
+ buttons: '.modal-footer button',
|
|
|
|
|
+ cancel: 'button.cancel',
|
|
|
|
|
+ save: 'button.save',
|
|
|
|
|
+ certificate_select: 'select[name="certificate_id"]',
|
|
|
|
|
+ ssl_forced: 'input[name="ssl_forced"]',
|
|
|
|
|
+ letsencrypt: '.letsencrypt'
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
events: {
|
|
events: {
|
|
|
- 'change @ui.ssl_enabled': function () {
|
|
|
|
|
- let enabled = this.ui.ssl_enabled.prop('checked');
|
|
|
|
|
- this.ui.ssl_options.not(this.ui.ssl_enabled).prop('disabled', !enabled).parents('.form-group').css('opacity', enabled ? 1 : 0.5);
|
|
|
|
|
- this.ui.ssl_provider.trigger('change');
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ 'change @ui.certificate_select': function () {
|
|
|
|
|
+ let id = this.ui.certificate_select.val();
|
|
|
|
|
+ if (id === 'new') {
|
|
|
|
|
+ this.ui.letsencrypt.show().find('input').prop('disabled', false);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ this.ui.letsencrypt.hide().find('input').prop('disabled', true);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- 'change @ui.ssl_provider': function () {
|
|
|
|
|
- let enabled = this.ui.ssl_enabled.prop('checked');
|
|
|
|
|
- let provider = this.ui.ssl_provider.filter(':checked').val();
|
|
|
|
|
- this.ui.all_ssl.hide().find('input').prop('disabled', true);
|
|
|
|
|
- this.ui[provider + '_ssl'].show().find('input').prop('disabled', !enabled);
|
|
|
|
|
|
|
+ let enabled = id === 'new' || parseInt(id, 10) > 0;
|
|
|
|
|
+ this.ui.ssl_forced.prop('disabled', !enabled).parents('.form-group').css('opacity', enabled ? 1 : 0.5);
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
'click @ui.save': function (e) {
|
|
'click @ui.save': function (e) {
|
|
@@ -60,23 +52,36 @@ module.exports = Mn.View.extend({
|
|
|
// Manipulate
|
|
// Manipulate
|
|
|
data.block_exploits = !!data.block_exploits;
|
|
data.block_exploits = !!data.block_exploits;
|
|
|
data.preserve_path = !!data.preserve_path;
|
|
data.preserve_path = !!data.preserve_path;
|
|
|
- data.ssl_enabled = !!data.ssl_enabled;
|
|
|
|
|
- data.ssl_forced = !!data.ssl_forced;
|
|
|
|
|
|
|
|
|
|
- if (typeof data.meta !== 'undefined' && typeof data.meta.letsencrypt_agree !== 'undefined') {
|
|
|
|
|
- data.meta.letsencrypt_agree = !!data.meta.letsencrypt_agree;
|
|
|
|
|
|
|
+ if (typeof data.ssl_forced !== 'undefined' && data.ssl_forced === '1') {
|
|
|
|
|
+ data.ssl_forced = true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
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(',');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- let require_ssl_files = typeof data.ssl_enabled !== 'undefined' && data.ssl_enabled && typeof data.ssl_provider !== 'undefined' && data.ssl_provider === 'other';
|
|
|
|
|
- let ssl_files = [];
|
|
|
|
|
- let method = App.Api.Nginx.RedirectionHosts.create;
|
|
|
|
|
- let is_new = true;
|
|
|
|
|
|
|
+ // Check for any domain names containing wildcards, which are not allowed with letsencrypt
|
|
|
|
|
+ if (data.certificate_id === 'new') {
|
|
|
|
|
+ let domain_err = false;
|
|
|
|
|
+ data.domain_names.map(function (name) {
|
|
|
|
|
+ if (name.match(/\*/im)) {
|
|
|
|
|
+ domain_err = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ if (domain_err) {
|
|
|
|
|
+ alert('Cannot request Let\'s Encrypt Certificate for wildcard domains');
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ data.meta.letsencrypt_agree = data.meta.letsencrypt_agree === '1';
|
|
|
|
|
+ } else {
|
|
|
|
|
+ data.certificate_id = parseInt(data.certificate_id, 0);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- let must_require_ssl_files = require_ssl_files && !view.model.hasSslFiles('other');
|
|
|
|
|
|
|
+ let method = App.Api.Nginx.RedirectionHosts.create;
|
|
|
|
|
+ let is_new = true;
|
|
|
|
|
|
|
|
if (this.model.get('id')) {
|
|
if (this.model.get('id')) {
|
|
|
// edit
|
|
// edit
|
|
@@ -85,55 +90,11 @@ module.exports = Mn.View.extend({
|
|
|
data.id = this.model.get('id');
|
|
data.id = this.model.get('id');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // check files are attached
|
|
|
|
|
- if (require_ssl_files) {
|
|
|
|
|
- if (!this.ui.other_ssl_certificate[0].files.length || !this.ui.other_ssl_certificate[0].files[0].size) {
|
|
|
|
|
- if (must_require_ssl_files) {
|
|
|
|
|
- alert('certificate file is not attached');
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- if (this.ui.other_ssl_certificate[0].files[0].size > this.max_file_size) {
|
|
|
|
|
- alert('certificate file is too large (> 5kb)');
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- ssl_files.push({name: 'other_certificate', file: this.ui.other_ssl_certificate[0].files[0]});
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (!this.ui.other_ssl_certificate_key[0].files.length || !this.ui.other_ssl_certificate_key[0].files[0].size) {
|
|
|
|
|
- if (must_require_ssl_files) {
|
|
|
|
|
- alert('certificate key file is not attached');
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- if (this.ui.other_ssl_certificate_key[0].files[0].size > this.max_file_size) {
|
|
|
|
|
- alert('certificate key file is too large (> 5kb)');
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- ssl_files.push({name: 'other_certificate_key', file: this.ui.other_ssl_certificate_key[0].files[0]});
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
|
|
this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
|
|
|
method(data)
|
|
method(data)
|
|
|
.then(result => {
|
|
.then(result => {
|
|
|
view.model.set(result);
|
|
view.model.set(result);
|
|
|
|
|
|
|
|
- // Now upload the certs if we need to
|
|
|
|
|
- if (ssl_files.length) {
|
|
|
|
|
- let form_data = new FormData();
|
|
|
|
|
-
|
|
|
|
|
- ssl_files.map(function (file) {
|
|
|
|
|
- form_data.append(file.name, file.file);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- return App.Api.Nginx.RedirectionHosts.setCerts(view.model.get('id'), form_data)
|
|
|
|
|
- .then(result => {
|
|
|
|
|
- view.model.set('meta', _.assign({}, view.model.get('meta'), result));
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
- .then(() => {
|
|
|
|
|
App.UI.closeModal(function () {
|
|
App.UI.closeModal(function () {
|
|
|
if (is_new) {
|
|
if (is_new) {
|
|
|
App.Controller.showNginxRedirection();
|
|
App.Controller.showNginxRedirection();
|
|
@@ -149,18 +110,14 @@ module.exports = Mn.View.extend({
|
|
|
|
|
|
|
|
templateContext: {
|
|
templateContext: {
|
|
|
getLetsencryptEmail: function () {
|
|
getLetsencryptEmail: function () {
|
|
|
- return typeof this.meta.letsencrypt_email !== 'undefined' ? this.meta.letsencrypt_email : App.Cache.User.get('email');
|
|
|
|
|
- },
|
|
|
|
|
-
|
|
|
|
|
- getLetsencryptAgree: function () {
|
|
|
|
|
- return typeof this.meta.letsencrypt_agree !== 'undefined' ? this.meta.letsencrypt_agree : false;
|
|
|
|
|
|
|
+ return App.Cache.User.get('email');
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
onRender: function () {
|
|
onRender: function () {
|
|
|
- this.ui.ssl_enabled.trigger('change');
|
|
|
|
|
- this.ui.ssl_provider.trigger('change');
|
|
|
|
|
|
|
+ let view = this;
|
|
|
|
|
|
|
|
|
|
+ // Domain names
|
|
|
this.ui.domain_names.selectize({
|
|
this.ui.domain_names.selectize({
|
|
|
delimiter: ',',
|
|
delimiter: ',',
|
|
|
persist: false,
|
|
persist: false,
|
|
@@ -173,6 +130,37 @@ module.exports = Mn.View.extend({
|
|
|
},
|
|
},
|
|
|
createFilter: /^(?:\*\.)?(?:[^.*]+\.?)+[^.]$/
|
|
createFilter: /^(?:\*\.)?(?:[^.*]+\.?)+[^.]$/
|
|
|
});
|
|
});
|
|
|
|
|
+
|
|
|
|
|
+ // Certificates
|
|
|
|
|
+ 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'));
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
initialize: function (options) {
|
|
initialize: function (options) {
|