nginx-proxy-manager/frontend/js/app/nginx/proxy/form.js

313 lines
11 KiB
JavaScript
Raw Normal View History

2018-08-20 18:33:51 -04:00
const Mn = require('backbone.marionette');
const App = require('../../main');
const ProxyHostModel = require('../../../models/proxy-host');
const ProxyLocationModel = require('../../../models/proxy-host-location');
2018-08-20 18:33:51 -04:00
const template = require('./form.ejs');
const certListItemTemplate = require('../certificates-list-item.ejs');
const accessListItemTemplate = require('./access-list-item.ejs');
const CustomLocation = require('./location');
2018-08-20 18:33:51 -04:00
const Helpers = require('../../../lib/helpers');
2018-07-08 22:21:03 -04:00
2018-07-08 22:21:03 -04:00
require('jquery-serializejson');
2018-07-17 18:35:49 -04:00
require('selectize');
2018-07-08 22:21:03 -04:00
module.exports = Mn.View.extend({
template: template,
className: 'modal-dialog',
2018-07-08 22:21:03 -04:00
locationsCollection: new ProxyLocationModel.Collection(),
2018-07-08 22:21:03 -04:00
ui: {
2018-08-13 05:50:28 -04:00
form: 'form',
domain_names: 'input[name="domain_names"]',
forward_host: 'input[name="forward_host"]',
2018-08-13 05:50:28 -04:00
buttons: '.modal-footer button',
cancel: 'button.cancel',
save: 'button.save',
add_location_btn: 'button.add_location',
locations_container:'.locations_container',
2018-08-13 05:50:28 -04:00
certificate_select: 'select[name="certificate_id"]',
2018-08-20 18:33:51 -04:00
access_list_select: 'select[name="access_list_id"]',
2018-08-13 08:51:36 -04:00
ssl_forced: 'input[name="ssl_forced"]',
2019-02-18 03:21:45 -05:00
hsts_enabled: 'input[name="hsts_enabled"]',
hsts_subdomains: 'input[name="hsts_subdomains"]',
http2_support: 'input[name="http2_support"]',
cloudflare_switch: 'input[name="meta[cloudflare_use]"]',
cloudflare_token: 'input[name="meta[cloudflare_token]"',
cloudflare: '.cloudflare',
2018-12-11 18:47:12 -05:00
forward_scheme: 'select[name="forward_scheme"]',
2018-08-13 05:50:28 -04:00
letsencrypt: '.letsencrypt'
2018-07-08 22:21:03 -04:00
},
regions: {
locations_regions: '@ui.locations_container'
},
2018-07-08 22:21:03 -04:00
events: {
2018-08-13 05:50:28 -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);
this.ui.cloudflare.hide();
2018-08-13 05:50:28 -04:00
} else {
this.ui.letsencrypt.hide().find('input').prop('disabled', true);
}
2018-08-13 05:50:28 -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
.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-08 22:21:03 -04:00
'change @ui.cloudflare_switch': function() {
let checked = this.ui.cloudflare_switch.prop('checked');
if (checked) {
this.ui.cloudflare_token.prop('required', 'required');
this.ui.cloudflare.show();
} else {
this.ui.cloudflare_token.prop('required', false);
this.ui.cloudflare.hide();
}
},
'click @ui.add_location_btn': function (e) {
e.preventDefault();
const model = new ProxyLocationModel.Model();
this.locationsCollection.add(model);
},
2018-07-08 22:21:03 -04:00
'click @ui.save': function (e) {
e.preventDefault();
if (!this.ui.form[0].checkValidity()) {
$('<input type="submit">').hide().appendTo(this.ui.form).click().remove();
return;
}
2018-07-08 22:21:03 -04:00
let view = this;
let data = this.ui.form.serializeJSON();
// Add locations
data.locations = [];
this.locationsCollection.models.forEach((location) => {
data.locations.push(location.toJSON());
});
// Serialize collects path from custom locations
// This field must be removed from root object
delete data.path;
2018-07-08 22:21:03 -04:00
// Manipulate
data.forward_port = parseInt(data.forward_port, 10);
data.block_exploits = !!data.block_exploits;
data.caching_enabled = !!data.caching_enabled;
data.allow_websocket_upgrade = !!data.allow_websocket_upgrade;
data.http2_support = !!data.http2_support;
2019-02-18 03:21:45 -05:00
data.hsts_enabled = !!data.hsts_enabled;
data.hsts_subdomains = !!data.hsts_subdomains;
data.ssl_forced = !!data.ssl_forced;
2018-07-17 18:35:49 -04:00
if (typeof data.domain_names === 'string' && data.domain_names) {
data.domain_names = data.domain_names.split(',');
}
2018-08-13 05:50:28 -04:00
// Check for any domain names containing wildcards, which are not allowed with letsencrypt
if (data.certificate_id === 'new') {
2018-08-13 05:50:28 -04:00
let domain_err = false;
if (!data.meta.cloudflare_use) {
data.domain_names.map(function (name) {
if (name.match(/\*/im)) {
domain_err = true;
}
});
}
2018-08-13 05:50:28 -04:00
if (domain_err) {
alert('Cannot request Let\'s Encrypt Certificate for wildcard domains without CloudFlare DNS.');
2018-08-13 05:50:28 -04:00
return;
}
data.meta.cloudflare_use = data.meta.cloudflare_use === '1';
data.meta.letsencrypt_agree = data.meta.letsencrypt_agree === '1';
2018-08-13 05:50:28 -04:00
} else {
2018-12-11 18:47:12 -05:00
data.certificate_id = parseInt(data.certificate_id, 10);
2018-08-13 05:50:28 -04:00
}
2018-07-23 01:12:24 -04:00
2018-08-13 05:50:28 -04:00
let method = App.Api.Nginx.ProxyHosts.create;
let is_new = true;
2018-07-08 22:21:03 -04:00
if (this.model.get('id')) {
// edit
2018-07-23 01:12:24 -04:00
is_new = false;
2018-07-18 02:55:09 -04:00
method = App.Api.Nginx.ProxyHosts.update;
2018-07-08 22:21:03 -04:00
data.id = this.model.get('id');
}
2018-07-23 01:12:24 -04:00
this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
this.ui.save.addClass('btn-loading');
2018-07-08 22:21:03 -04:00
method(data)
.then(result => {
view.model.set(result);
2018-07-23 01:12:24 -04:00
2018-07-08 22:21:03 -04:00
App.UI.closeModal(function () {
2018-07-23 01:12:24 -04:00
if (is_new) {
2018-07-18 02:55:09 -04:00
App.Controller.showNginxProxy();
2018-07-08 22:21:03 -04:00
}
});
})
.catch(err => {
alert(err.message);
2018-07-08 22:21:03 -04:00
this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
this.ui.save.removeClass('btn-loading');
2018-07-08 22:21:03 -04:00
});
}
},
templateContext: {
getLetsencryptEmail: function () {
2018-08-13 05:50:28 -04:00
return App.Cache.User.get('email');
}
},
2018-07-08 22:21:03 -04:00
onRender: function () {
2018-08-13 05:50:28 -04:00
let view = this;
2019-02-18 03:21:45 -05:00
this.ui.ssl_forced.trigger('change');
this.ui.hsts_enabled.trigger('change');
2018-08-13 05:50:28 -04:00
// Domain names
2018-07-17 18:35:49 -04:00
this.ui.domain_names.selectize({
delimiter: ',',
persist: false,
maxOptions: 15,
create: function (input) {
return {
value: input,
text: input
};
},
createFilter: /^(?:\.)?(?:[^.*]+\.?)+[^.]$/
2018-07-17 18:35:49 -04:00
});
2018-08-13 05:50:28 -04:00
2018-08-20 18:33:51 -04:00
// Access Lists
this.ui.access_list_select.selectize({
valueField: 'id',
labelField: 'name',
searchField: ['name'],
create: false,
preload: true,
allowEmptyOption: true,
render: {
option: function (item) {
item.i18n = App.i18n;
item.formatDbDate = Helpers.formatDbDate;
return accessListItemTemplate(item);
}
},
load: function (query, callback) {
2020-04-10 23:22:01 -04:00
App.Api.Nginx.AccessLists.getAll(['items', 'clients'])
2018-08-20 18:33:51 -04:00
.then(rows => {
callback(rows);
})
.catch(err => {
console.error(err);
callback();
});
},
onLoad: function () {
view.ui.access_list_select[0].selectize.setValue(view.model.get('access_list_id'));
}
});
2018-08-13 05:50:28 -04:00
// 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'));
}
});
2018-07-08 22:21:03 -04:00
},
initialize: function (options) {
if (typeof options.model === 'undefined' || !options.model) {
this.model = new ProxyHostModel.Model();
}
this.locationsCollection = new ProxyLocationModel.Collection();
// Custom locations
this.showChildView('locations_regions', new CustomLocation.LocationCollectionView({
collection: this.locationsCollection
}));
// Check wether there are any location defined
if (options.model && Array.isArray(options.model.attributes.locations)) {
options.model.attributes.locations.forEach((location) => {
let m = new ProxyLocationModel.Model(location);
this.locationsCollection.add(m);
});
}
2018-07-08 22:21:03 -04:00
}
});