form.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. const Mn = require('backbone.marionette');
  2. const App = require('../../main');
  3. const DeadHostModel = require('../../../models/dead-host');
  4. const template = require('./form.ejs');
  5. const certListItemTemplate = require('../certificates-list-item.ejs');
  6. const Helpers = require('../../../lib/helpers');
  7. const i18n = require('../../i18n');
  8. const dns_providers = require('../../../../../global/certbot-dns-plugins');
  9. require('jquery-serializejson');
  10. require('selectize');
  11. module.exports = Mn.View.extend({
  12. template: template,
  13. className: 'modal-dialog',
  14. ui: {
  15. form: 'form',
  16. domain_names: 'input[name="domain_names"]',
  17. buttons: '.modal-footer button',
  18. cancel: 'button.cancel',
  19. save: 'button.save',
  20. le_error_info: '#le-error-info',
  21. certificate_select: 'select[name="certificate_id"]',
  22. ssl_forced: 'input[name="ssl_forced"]',
  23. hsts_enabled: 'input[name="hsts_enabled"]',
  24. hsts_subdomains: 'input[name="hsts_subdomains"]',
  25. http2_support: 'input[name="http2_support"]',
  26. dns_challenge_switch: 'input[name="meta[dns_challenge]"]',
  27. dns_challenge_content: '.dns-challenge',
  28. dns_provider: 'select[name="meta[dns_provider]"]',
  29. credentials_file_content: '.credentials-file-content',
  30. dns_provider_credentials: 'textarea[name="meta[dns_provider_credentials]"]',
  31. propagation_seconds: 'input[name="meta[propagation_seconds]"]',
  32. letsencrypt: '.letsencrypt'
  33. },
  34. events: {
  35. 'change @ui.certificate_select': function () {
  36. let id = this.ui.certificate_select.val();
  37. if (id === 'new') {
  38. this.ui.letsencrypt.show().find('input').prop('disabled', false);
  39. this.ui.dns_challenge_content.hide();
  40. } else {
  41. this.ui.letsencrypt.hide().find('input').prop('disabled', true);
  42. }
  43. let enabled = id === 'new' || parseInt(id, 10) > 0;
  44. let inputs = this.ui.ssl_forced.add(this.ui.http2_support);
  45. inputs
  46. .prop('disabled', !enabled)
  47. .parents('.form-group')
  48. .css('opacity', enabled ? 1 : 0.5);
  49. if (!enabled) {
  50. inputs.prop('checked', false);
  51. }
  52. inputs.trigger('change');
  53. },
  54. 'change @ui.ssl_forced': function () {
  55. let checked = this.ui.ssl_forced.prop('checked');
  56. this.ui.hsts_enabled
  57. .prop('disabled', !checked)
  58. .parents('.form-group')
  59. .css('opacity', checked ? 1 : 0.5);
  60. if (!checked) {
  61. this.ui.hsts_enabled.prop('checked', false);
  62. }
  63. this.ui.hsts_enabled.trigger('change');
  64. },
  65. 'change @ui.hsts_enabled': function () {
  66. let checked = this.ui.hsts_enabled.prop('checked');
  67. this.ui.hsts_subdomains
  68. .prop('disabled', !checked)
  69. .parents('.form-group')
  70. .css('opacity', checked ? 1 : 0.5);
  71. if (!checked) {
  72. this.ui.hsts_subdomains.prop('checked', false);
  73. }
  74. },
  75. 'change @ui.dns_challenge_switch': function () {
  76. const checked = this.ui.dns_challenge_switch.prop('checked');
  77. if (checked) {
  78. this.ui.dns_provider.prop('required', 'required');
  79. const selected_provider = this.ui.dns_provider[0].options[this.ui.dns_provider[0].selectedIndex].value;
  80. if(selected_provider != '' && dns_providers[selected_provider].credentials !== false){
  81. this.ui.dns_provider_credentials.prop('required', 'required');
  82. }
  83. this.ui.dns_challenge_content.show();
  84. } else {
  85. this.ui.dns_provider.prop('required', false);
  86. this.ui.dns_provider_credentials.prop('required', false);
  87. this.ui.dns_challenge_content.hide();
  88. }
  89. },
  90. 'change @ui.dns_provider': function () {
  91. const selected_provider = this.ui.dns_provider[0].options[this.ui.dns_provider[0].selectedIndex].value;
  92. if (selected_provider != '' && dns_providers[selected_provider].credentials !== false) {
  93. this.ui.dns_provider_credentials.prop('required', 'required');
  94. this.ui.dns_provider_credentials[0].value = dns_providers[selected_provider].credentials;
  95. this.ui.credentials_file_content.show();
  96. } else {
  97. this.ui.dns_provider_credentials.prop('required', false);
  98. this.ui.credentials_file_content.hide();
  99. }
  100. },
  101. 'click @ui.save': function (e) {
  102. e.preventDefault();
  103. this.ui.le_error_info.hide();
  104. if (!this.ui.form[0].checkValidity()) {
  105. $('<input type="submit">').hide().appendTo(this.ui.form).click().remove();
  106. return;
  107. }
  108. let view = this;
  109. let data = this.ui.form.serializeJSON();
  110. // Manipulate
  111. data.hsts_enabled = !!data.hsts_enabled;
  112. data.hsts_subdomains = !!data.hsts_subdomains;
  113. data.http2_support = !!data.http2_support;
  114. data.ssl_forced = !!data.ssl_forced;
  115. if (typeof data.meta === 'undefined') data.meta = {};
  116. data.meta.letsencrypt_agree = data.meta.letsencrypt_agree == 1;
  117. data.meta.dns_challenge = data.meta.dns_challenge == 1;
  118. if(!data.meta.dns_challenge){
  119. data.meta.dns_provider = undefined;
  120. data.meta.dns_provider_credentials = undefined;
  121. data.meta.propagation_seconds = undefined;
  122. } else {
  123. if(data.meta.propagation_seconds === '') data.meta.propagation_seconds = undefined;
  124. }
  125. if (typeof data.domain_names === 'string' && data.domain_names) {
  126. data.domain_names = data.domain_names.split(',');
  127. }
  128. // Check for any domain names containing wildcards, which are not allowed with letsencrypt
  129. if (data.certificate_id === 'new') {
  130. let domain_err = false;
  131. if (!data.meta.dns_challenge) {
  132. data.domain_names.map(function (name) {
  133. if (name.match(/\*/im)) {
  134. domain_err = true;
  135. }
  136. });
  137. }
  138. if (domain_err) {
  139. alert(i18n('ssl', 'no-wildcard-without-dns'));
  140. return;
  141. }
  142. } else {
  143. data.certificate_id = parseInt(data.certificate_id, 10);
  144. }
  145. let method = App.Api.Nginx.DeadHosts.create;
  146. let is_new = true;
  147. if (this.model.get('id')) {
  148. // edit
  149. is_new = false;
  150. method = App.Api.Nginx.DeadHosts.update;
  151. data.id = this.model.get('id');
  152. }
  153. this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
  154. this.ui.save.addClass('btn-loading');
  155. method(data)
  156. .then(result => {
  157. view.model.set(result);
  158. App.UI.closeModal(function () {
  159. if (is_new) {
  160. App.Controller.showNginxDead();
  161. }
  162. });
  163. })
  164. .catch(err => {
  165. let more_info = '';
  166. if(err.code === 500 && err.debug){
  167. try{
  168. more_info = JSON.parse(err.debug).debug.stack.join("\n");
  169. } catch(e) {}
  170. }
  171. this.ui.le_error_info[0].innerHTML = `${err.message}${more_info !== '' ? `<pre class="mt-3">${more_info}</pre>`:''}`;
  172. this.ui.le_error_info.show();
  173. this.ui.le_error_info[0].scrollIntoView();
  174. this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
  175. this.ui.save.removeClass('btn-loading');
  176. });
  177. }
  178. },
  179. templateContext: {
  180. getLetsencryptEmail: function () {
  181. return App.Cache.User.get('email');
  182. },
  183. getUseDnsChallenge: function () {
  184. return typeof this.meta.dns_challenge !== 'undefined' ? this.meta.dns_challenge : false;
  185. },
  186. getDnsProvider: function () {
  187. return typeof this.meta.dns_provider !== 'undefined' && this.meta.dns_provider != '' ? this.meta.dns_provider : null;
  188. },
  189. getDnsProviderCredentials: function () {
  190. return typeof this.meta.dns_provider_credentials !== 'undefined' ? this.meta.dns_provider_credentials : '';
  191. },
  192. getPropagationSeconds: function () {
  193. return typeof this.meta.propagation_seconds !== 'undefined' ? this.meta.propagation_seconds : '';
  194. },
  195. dns_plugins: dns_providers,
  196. },
  197. onRender: function () {
  198. let view = this;
  199. // Domain names
  200. this.ui.domain_names.selectize({
  201. delimiter: ',',
  202. persist: false,
  203. maxOptions: 15,
  204. create: function (input) {
  205. return {
  206. value: input,
  207. text: input
  208. };
  209. },
  210. createFilter: /^(?:\*\.)?(?:[^.*]+\.?)+[^.]$/
  211. });
  212. // Certificates
  213. this.ui.le_error_info.hide();
  214. this.ui.dns_challenge_content.hide();
  215. this.ui.credentials_file_content.hide();
  216. this.ui.letsencrypt.hide();
  217. this.ui.certificate_select.selectize({
  218. valueField: 'id',
  219. labelField: 'nice_name',
  220. searchField: ['nice_name', 'domain_names'],
  221. create: false,
  222. preload: true,
  223. allowEmptyOption: true,
  224. render: {
  225. option: function (item) {
  226. item.i18n = App.i18n;
  227. item.formatDbDate = Helpers.formatDbDate;
  228. return certListItemTemplate(item);
  229. }
  230. },
  231. load: function (query, callback) {
  232. App.Api.Nginx.Certificates.getAll()
  233. .then(rows => {
  234. callback(rows);
  235. })
  236. .catch(err => {
  237. console.error(err);
  238. callback();
  239. });
  240. },
  241. onLoad: function () {
  242. view.ui.certificate_select[0].selectize.setValue(view.model.get('certificate_id'));
  243. }
  244. });
  245. },
  246. initialize: function (options) {
  247. if (typeof options.model === 'undefined' || !options.model) {
  248. this.model = new DeadHostModel.Model();
  249. }
  250. }
  251. });