easy-devops 0.2.7 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/README.md +97 -53
  2. package/install.sh +34 -12
  3. package/package.json +11 -12
  4. package/{cli → src/cli}/index.js +28 -6
  5. package/{cli → src/cli}/managers/domain-manager.js +143 -35
  6. package/{cli → src/cli}/managers/nginx-manager.js +43 -10
  7. package/src/cli/managers/ssl-manager.js +678 -0
  8. package/{cli → src/cli}/menus/dashboard.js +1 -1
  9. package/{cli → src/cli}/menus/settings.js +27 -8
  10. package/{cli → src/cli}/menus/update.js +1 -1
  11. package/{core → src/core}/config.js +5 -2
  12. package/{core → src/core}/db.js +1 -1
  13. package/{core → src/core}/detector.js +50 -87
  14. package/{core → src/core}/nginx-conf-generator.js +101 -64
  15. package/src/core/platform.js +61 -0
  16. package/{core → src/core}/shell.js +38 -2
  17. package/src/core/validators.js +96 -0
  18. package/{dashboard → src/dashboard}/lib/cert-reader.js +5 -5
  19. package/{dashboard → src/dashboard}/lib/nginx-service.js +47 -77
  20. package/src/dashboard/public/img/icon.png +0 -0
  21. package/src/dashboard/public/img/icon_b.png +0 -0
  22. package/{dashboard → src/dashboard}/public/js/app.js +206 -30
  23. package/{dashboard → src/dashboard}/routes/domains.js +96 -72
  24. package/{dashboard → src/dashboard}/routes/settings.js +19 -11
  25. package/src/dashboard/routes/ssl.js +281 -0
  26. package/src/dashboard/views/index.ejs +652 -0
  27. package/src/dashboard/views/partials/domain-form.ejs +356 -0
  28. package/src/dashboard/views/partials/domains-panel.ejs +101 -0
  29. package/src/dashboard/views/partials/login.ejs +167 -0
  30. package/src/dashboard/views/partials/nginx-panel.ejs +153 -0
  31. package/src/dashboard/views/partials/overview.ejs +142 -0
  32. package/src/dashboard/views/partials/settings-panel.ejs +68 -0
  33. package/src/dashboard/views/partials/sidebar.ejs +96 -0
  34. package/src/dashboard/views/partials/ssl-panel.ejs +176 -0
  35. package/{lib → src/lib}/installer/nvm-bootstrap.sh +8 -8
  36. package/{lib → src/lib}/installer/picker.sh +33 -16
  37. package/cli/managers/ssl-manager.js +0 -963
  38. package/dashboard/routes/ssl.js +0 -140
  39. package/dashboard/views/index.ejs +0 -327
  40. package/dashboard/views/partials/domain-form.ejs +0 -257
  41. package/dashboard/views/partials/domains-panel.ejs +0 -66
  42. package/dashboard/views/partials/login.ejs +0 -50
  43. package/dashboard/views/partials/nginx-panel.ejs +0 -90
  44. package/dashboard/views/partials/overview.ejs +0 -67
  45. package/dashboard/views/partials/settings-panel.ejs +0 -37
  46. package/dashboard/views/partials/sidebar.ejs +0 -45
  47. package/dashboard/views/partials/ssl-panel.ejs +0 -95
  48. /package/{cli → src/cli}/managers/node-manager.js +0 -0
  49. /package/{cli → src/cli}/menus/.gitkeep +0 -0
  50. /package/{cli → src/cli}/menus/domains.js +0 -0
  51. /package/{cli → src/cli}/menus/nginx.js +0 -0
  52. /package/{cli → src/cli}/menus/nodejs.js +0 -0
  53. /package/{cli → src/cli}/menus/ssl.js +0 -0
  54. /package/{dashboard → src/dashboard}/lib/.gitkeep +0 -0
  55. /package/{dashboard → src/dashboard}/lib/domains-db.js +0 -0
  56. /package/{dashboard → src/dashboard}/lib/nginx-conf-generator.js +0 -0
  57. /package/{dashboard → src/dashboard}/routes/.gitkeep +0 -0
  58. /package/{dashboard → src/dashboard}/routes/auth.js +0 -0
  59. /package/{dashboard → src/dashboard}/routes/nginx.js +0 -0
  60. /package/{dashboard → src/dashboard}/server.js +0 -0
  61. /package/{lib → src/lib}/installer/.gitkeep +0 -0
  62. /package/{lib → src/lib}/installer/detect.sh +0 -0
  63. /package/{lib → src/lib}/installer/node-versions.sh +0 -0
  64. /package/{lib → src/lib}/installer/progress.sh +0 -0
  65. /package/{lib → src/lib}/installer/winget-install.ps1 +0 -0
@@ -8,6 +8,7 @@ const DEFAULT_FORM = {
8
8
  backendHost: '127.0.0.1',
9
9
  upstreamType: 'http',
10
10
  www: false,
11
+ wildcard: false,
11
12
  ssl: {
12
13
  enabled: false,
13
14
  certPath: '',
@@ -42,6 +43,8 @@ createApp({
42
43
  data() {
43
44
  return {
44
45
  theme: localStorage.getItem('ed-theme') || 'dark',
46
+ accent: localStorage.getItem('ed-accent') || 'teal',
47
+ sidebarCollapsed: localStorage.getItem('ed-sidebar') === 'true',
45
48
  authenticated: false,
46
49
  page: 'overview',
47
50
 
@@ -61,26 +64,34 @@ createApp({
61
64
  configs: [], selectedConfig: '',
62
65
  configContent: '', configSaving: false, configMsg: '',
63
66
  logs: [], logsLoading: false,
67
+ configDropdownOpen: false,
64
68
  },
65
69
 
66
70
  ssl: {
67
71
  certs: [], loading: false, error: '', renewingDomain: null,
68
72
  showCreateForm: false,
69
- createDomain: '', createWww: false,
73
+ createDomain: '', createWww: false, createEmail: '',
70
74
  creating: false, createResult: null, createError: null,
75
+ createMethod: 'http', // 'http' | 'dns'
76
+ dnsWaiting: null, // { txtName, txtValue, domain } | null
77
+ dnsConfirming: false, // true while confirm call is in-flight
71
78
  },
72
79
 
73
80
  // T017-T019: Updated domains state
74
81
  domains: {
75
82
  list: [], loading: false, error: '',
76
83
  showForm: false,
84
+ upstreamTypeOpen: false,
77
85
  editingName: null, // T017: null = adding, string = editing
78
86
  saving: false,
79
87
  dirty: false, // T019: Track unsaved changes
80
88
  form: JSON.parse(JSON.stringify(DEFAULT_FORM)), // T025: Nested v2 structure
81
- certMissing: null, // { certPath, keyPath, hint } set when POST /api/domains returns 422
89
+ certMissing: null, // { certPath, keyPath, hint } set when POST /api/domains returns 422
82
90
  certCreating: false, // true while POST /api/ssl/create is in-flight
83
91
  certCreateError: null, // structured error from a failed cert creation
92
+ certMethod: 'http', // 'http' | 'dns' — method selected in cert_missing section
93
+ certDnsWaiting: null, // { txtName, txtValue, domain } while DNS two-phase is in progress
94
+ certDnsConfirming: false, // true while /create-confirm call is in-flight
84
95
  },
85
96
 
86
97
  // T018: Collapsible section state
@@ -93,7 +104,7 @@ createApp({
93
104
  },
94
105
 
95
106
  settings: {
96
- dashboardPort: '', nginxDir: '', certbotDir: '',
107
+ dashboardPort: '', nginxDir: '', sslDir: '', acmeEmail: '',
97
108
  password: '', loading: false, msg: '', error: '',
98
109
  platform: 'linux', // T022: Platform from settings API
99
110
  },
@@ -103,6 +114,21 @@ createApp({
103
114
  computed: {
104
115
  isDark() { return this.theme === 'dark'; },
105
116
  expiringCount() { return this.ssl.certs.filter(c => c.daysLeft !== null && c.daysLeft < 30).length; },
117
+ swalTheme() {
118
+ // Create dynamically evaluated properties based on current CSS variables
119
+ const rootStyle = getComputedStyle(document.documentElement);
120
+ return {
121
+ background: 'var(--color-surface)',
122
+ color: 'var(--color-text)',
123
+ confirmButtonColor: rootStyle.getPropertyValue('--color-primary').trim(),
124
+ cancelButtonColor: 'transparent',
125
+ customClass: {
126
+ confirmButton: 'btn btn-primary',
127
+ cancelButton: 'btn btn-secondary',
128
+ popup: 'swal2-popup'
129
+ }
130
+ };
131
+ },
106
132
  },
107
133
 
108
134
  watch: {
@@ -110,6 +136,13 @@ createApp({
110
136
  localStorage.setItem('ed-theme', val);
111
137
  document.documentElement.classList.toggle('dark', val === 'dark');
112
138
  },
139
+ accent(val) {
140
+ localStorage.setItem('ed-accent', val);
141
+ document.documentElement.setAttribute('data-accent', val);
142
+ },
143
+ sidebarCollapsed(val) {
144
+ localStorage.setItem('ed-sidebar', val);
145
+ },
113
146
 
114
147
  // T019: Deep watch form for dirty tracking
115
148
  'domains.form': {
@@ -127,6 +160,13 @@ createApp({
127
160
  this.autoPopulateCertPaths();
128
161
  }
129
162
  },
163
+
164
+ // Wildcard: force DNS cert method when wildcard is enabled
165
+ 'domains.form.wildcard'(newVal) {
166
+ if (newVal) {
167
+ this.domains.certMethod = 'dns';
168
+ }
169
+ },
130
170
  },
131
171
 
132
172
  // T020: beforeunload guard for unsaved changes
@@ -141,6 +181,7 @@ createApp({
141
181
 
142
182
  async mounted() {
143
183
  document.documentElement.classList.toggle('dark', this.theme === 'dark');
184
+ document.documentElement.setAttribute('data-accent', this.accent);
144
185
 
145
186
  // Check if already authenticated
146
187
  try {
@@ -171,6 +212,7 @@ createApp({
171
212
 
172
213
  // ── Theme ─────────────────────────────────────────────────────────────────
173
214
  toggleTheme() { this.theme = this.theme === 'dark' ? 'light' : 'dark'; },
215
+ setAccent(color) { this.accent = color; },
174
216
 
175
217
  // ── Auth ──────────────────────────────────────────────────────────────────
176
218
  async doLogin() {
@@ -188,11 +230,17 @@ createApp({
188
230
 
189
231
  // ── Navigation (T021: Nav click guard) ────────────────────────────────────
190
232
  async navigateTo(pageId) {
191
- // T021: Check for unsaved changes before navigation
192
233
  if (this.domains.dirty && this.domains.showForm) {
193
- if (!confirm('You have unsaved changes. Discard them and continue?')) {
194
- return;
195
- }
234
+ const { isConfirmed } = await Swal.fire({
235
+ title: 'Unsaved changes',
236
+ text: 'Discard changes and continue?',
237
+ icon: 'warning',
238
+ showCancelButton: true,
239
+ confirmButtonText: 'Discard',
240
+ cancelButtonText: 'Stay',
241
+ ...this.swalTheme,
242
+ });
243
+ if (!isConfirmed) return;
196
244
  this.resetDomainForm();
197
245
  }
198
246
  await this.loadPage(pageId);
@@ -289,15 +337,21 @@ createApp({
289
337
  const r = await this.api('POST', '/api/ssl/create', {
290
338
  domain: this.ssl.createDomain.trim(),
291
339
  www: this.ssl.createWww,
340
+ email: this.ssl.createEmail.trim(),
341
+ validationMethod: this.ssl.createMethod,
292
342
  });
293
343
  this.ssl.creating = false;
294
- if (r.ok) {
344
+ if (r.status === 202 && r.data.status === 'waiting_dns') {
345
+ this.ssl.dnsWaiting = r.data; // { domain, txtName, txtValue }
346
+ // Do not hide the form or clear the domain — show DNS waiting state
347
+ } else if (r.ok) {
295
348
  this.ssl.createResult = r.data;
296
349
  this.ssl.createDomain = '';
297
350
  this.ssl.createWww = false;
351
+ this.ssl.createEmail = '';
298
352
  await this.loadSSL();
299
- } else if (r.status === 503) {
300
- this.ssl.createError = { step: 'ACME client detection', cause: r.data.hint, consequence: 'Install certbot or win-acme first using the SSL Manager CLI.', nginxRunning: true };
353
+ } else if (r.status === 400) {
354
+ this.ssl.createError = { step: 'email configuration', cause: r.data.hint, consequence: 'Configure acmeEmail in settings or provide email.', nginxRunning: true };
301
355
  } else if (r.status === 409) {
302
356
  this.ssl.createError = { step: 'port 80 check', cause: r.data.detail, consequence: r.data.hint, nginxRunning: true };
303
357
  } else {
@@ -305,6 +359,30 @@ createApp({
305
359
  }
306
360
  },
307
361
 
362
+ async confirmDns() {
363
+ if (!this.ssl.dnsWaiting) return;
364
+ this.ssl.dnsConfirming = true;
365
+ const r = await this.api('POST', '/api/ssl/create-confirm', { domain: this.ssl.dnsWaiting.domain });
366
+ this.ssl.dnsConfirming = false;
367
+ if (r.ok) {
368
+ this.ssl.createResult = r.data;
369
+ this.ssl.dnsWaiting = null;
370
+ this.ssl.createDomain = '';
371
+ this.ssl.createWww = false;
372
+ this.ssl.createEmail = '';
373
+ await this.loadSSL();
374
+ } else {
375
+ this.ssl.createError = r.data.error || { step: 'DNS validation', cause: 'Certificate issuance failed after DNS confirmation.', consequence: 'No certificate was issued.' };
376
+ this.ssl.dnsWaiting = null;
377
+ }
378
+ },
379
+
380
+ async cancelDns() {
381
+ if (!this.ssl.dnsWaiting) return;
382
+ await this.api('POST', '/api/ssl/create-cancel', { domain: this.ssl.dnsWaiting.domain });
383
+ this.ssl.dnsWaiting = null;
384
+ },
385
+
308
386
  certStatus(cert) {
309
387
  if (cert.daysLeft === null || cert.daysLeft === undefined) return 'Unknown';
310
388
  if (cert.daysLeft > 30) return 'Healthy';
@@ -332,7 +410,7 @@ createApp({
332
410
  return 'neutral';
333
411
  },
334
412
 
335
- // ── Domains ───────────────────────────────────────────────────────────────
413
+ // ── Domains ───────────────────────────────────────────────────────
336
414
  async loadDomains() {
337
415
  this.domains.loading = true;
338
416
  const r = await this.api('GET', '/api/domains');
@@ -340,9 +418,18 @@ createApp({
340
418
  if (r.ok) this.domains.list = r.data;
341
419
  },
342
420
 
343
- toggleDomainForm() {
421
+ async toggleDomainForm() {
344
422
  if (this.domains.showForm && this.domains.dirty) {
345
- if (!confirm('Discard unsaved changes?')) return;
423
+ const { isConfirmed } = await Swal.fire({
424
+ title: 'Unsaved changes',
425
+ text: 'Discard changes and close?',
426
+ icon: 'warning',
427
+ showCancelButton: true,
428
+ confirmButtonText: 'Discard',
429
+ cancelButtonText: 'Stay',
430
+ ...this.swalTheme,
431
+ });
432
+ if (!isConfirmed) return;
346
433
  }
347
434
  this.domains.showForm = !this.domains.showForm;
348
435
  if (!this.domains.showForm) {
@@ -357,6 +444,9 @@ createApp({
357
444
  this.domains.error = '';
358
445
  this.domains.certMissing = null;
359
446
  this.domains.certCreateError = null;
447
+ this.domains.certMethod = 'http';
448
+ this.domains.certDnsWaiting = null;
449
+ this.domains.certDnsConfirming = false;
360
450
  // Reset sections
361
451
  this.sections = {
362
452
  basic: true,
@@ -367,11 +457,18 @@ createApp({
367
457
  };
368
458
  },
369
459
 
370
- // T023: editDomain method
371
460
  async editDomain(name) {
372
- // Check for unsaved changes
373
461
  if (this.domains.dirty && this.domains.showForm) {
374
- if (!confirm('Discard unsaved changes?')) return;
462
+ const { isConfirmed } = await Swal.fire({
463
+ title: 'Unsaved changes',
464
+ text: 'Discard changes and edit this domain?',
465
+ icon: 'warning',
466
+ showCancelButton: true,
467
+ confirmButtonText: 'Discard',
468
+ cancelButtonText: 'Stay',
469
+ ...this.swalTheme,
470
+ });
471
+ if (!isConfirmed) return;
375
472
  }
376
473
 
377
474
  // Fetch domain data
@@ -394,6 +491,7 @@ createApp({
394
491
  backendHost: domain.backendHost || '127.0.0.1',
395
492
  upstreamType: domain.upstreamType || 'http',
396
493
  www: domain.www || false,
494
+ wildcard: domain.wildcard || false,
397
495
  ssl: {
398
496
  enabled: domain.ssl?.enabled || false,
399
497
  certPath: domain.ssl?.certPath || '',
@@ -469,18 +567,21 @@ createApp({
469
567
  this.domains.certCreateError = null;
470
568
  const domain = this.domains.form.name;
471
569
  const www = this.domains.form.www;
472
- const r = await this.api('POST', '/api/ssl/create', { domain, www });
570
+ const r = await this.api('POST', '/api/ssl/create', { domain, www, validationMethod: this.domains.certMethod });
473
571
  this.domains.certCreating = false;
474
572
 
475
- if (r.ok) {
476
- // Update cert paths in the form with what certbot actually wrote
573
+ if (r.status === 202 && r.data.status === 'waiting_dns') {
574
+ this.domains.certDnsWaiting = r.data; // { domain, txtName, txtValue }
575
+ // Stay in cert_missing section — show DNS waiting state
576
+ } else if (r.ok) {
577
+ // Update cert paths in the form with what was actually written
477
578
  this.domains.form.ssl.certPath = r.data.certPath;
478
579
  this.domains.form.ssl.keyPath = r.data.keyPath;
479
580
  this.domains.certMissing = null;
480
581
  // Retry the domain save now that the cert exists
481
582
  await this.saveDomain();
482
- } else if (r.status === 503) {
483
- this.domains.certCreateError = { step: 'ACME client detection', cause: r.data.hint, consequence: 'Install certbot or win-acme first.', nginxRunning: true };
583
+ } else if (r.status === 400) {
584
+ this.domains.certCreateError = { step: 'email configuration', cause: r.data.hint, consequence: 'Configure acmeEmail in settings.', nginxRunning: true };
484
585
  } else if (r.status === 409) {
485
586
  this.domains.certCreateError = { step: 'port 80 check', cause: r.data.detail, consequence: r.data.hint, nginxRunning: true };
486
587
  } else {
@@ -488,21 +589,61 @@ createApp({
488
589
  }
489
590
  },
490
591
 
592
+ async confirmCertDns() {
593
+ if (!this.domains.certDnsWaiting) return;
594
+ this.domains.certDnsConfirming = true;
595
+ const r = await this.api('POST', '/api/ssl/create-confirm', { domain: this.domains.certDnsWaiting.domain });
596
+ this.domains.certDnsConfirming = false;
597
+ if (r.ok) {
598
+ this.domains.form.ssl.certPath = r.data.certPath;
599
+ this.domains.form.ssl.keyPath = r.data.keyPath;
600
+ this.domains.certDnsWaiting = null;
601
+ this.domains.certMissing = null;
602
+ await this.saveDomain();
603
+ } else {
604
+ this.domains.certCreateError = r.data.error || { step: 'DNS validation', cause: 'Certificate issuance failed after DNS confirmation.', consequence: 'No certificate was issued.' };
605
+ this.domains.certDnsWaiting = null;
606
+ }
607
+ },
608
+
609
+ async cancelCertDns() {
610
+ if (!this.domains.certDnsWaiting) return;
611
+ await this.api('POST', '/api/ssl/create-cancel', { domain: this.domains.certDnsWaiting.domain });
612
+ this.domains.certDnsWaiting = null;
613
+ },
614
+
491
615
  disableSslAndSave() {
492
616
  this.domains.form.ssl.enabled = false;
493
617
  this.domains.certMissing = null;
494
618
  this.domains.certCreateError = null;
619
+ this.domains.certDnsWaiting = null;
495
620
  this.saveDomain();
496
621
  },
497
622
 
498
623
  dismissCertMissing() {
499
624
  this.domains.certMissing = null;
500
625
  this.domains.certCreateError = null;
626
+ this.domains.certDnsWaiting = null;
501
627
  },
502
628
 
503
629
  async deleteDomain(name) {
504
- if (!confirm(`Delete domain "${name}"? This cannot be undone.`)) return;
505
- await this.api('DELETE', `/api/domains/${name}`);
630
+ const domain = this.domains.list.find(d => d.name === name);
631
+ const hasCert = domain?.ssl?.enabled;
632
+ const { isConfirmed, value: deleteCertChecked } = await Swal.fire({
633
+ title: `Delete "${name}"?`,
634
+ text: 'This cannot be undone.',
635
+ icon: 'warning',
636
+ showCancelButton: true,
637
+ confirmButtonText: 'Delete',
638
+ cancelButtonText: 'Cancel',
639
+ input: hasCert ? 'checkbox' : undefined,
640
+ inputValue: 0,
641
+ inputPlaceholder: 'Also delete SSL certificate files',
642
+ ...this.swalTheme,
643
+ });
644
+ if (!isConfirmed) return;
645
+ const deleteCert = hasCert && deleteCertChecked === 1;
646
+ await this.api('DELETE', `/api/domains/${name}${deleteCert ? '?deleteCert=true' : ''}`);
506
647
  await this.loadDomains();
507
648
  },
508
649
 
@@ -510,6 +651,29 @@ createApp({
510
651
  await this.api('POST', `/api/domains/${name}/reload`);
511
652
  },
512
653
 
654
+ async toggleDomain(name) {
655
+ const domain = this.domains.list.find(d => d.name === name);
656
+ const isEnabled = domain?.enabled !== false;
657
+ const action = isEnabled ? 'disable' : 'enable';
658
+ const { isConfirmed } = await Swal.fire({
659
+ title: `${isEnabled ? 'Disable' : 'Enable'} "${name}"?`,
660
+ text: isEnabled
661
+ ? 'Nginx will stop serving this domain until re-enabled.'
662
+ : 'Nginx will resume serving this domain.',
663
+ icon: 'question',
664
+ showCancelButton: true,
665
+ confirmButtonText: isEnabled ? 'Disable' : 'Enable',
666
+ cancelButtonText: 'Cancel',
667
+ ...this.swalTheme,
668
+ });
669
+ if (!isConfirmed) return;
670
+ const r = await this.api('PUT', `/api/domains/${name}/toggle`);
671
+ if (!r.ok) {
672
+ await Swal.fire({ title: 'Error', text: r.data.error || `Failed to ${action} domain`, icon: 'error', ...this.swalTheme });
673
+ }
674
+ await this.loadDomains();
675
+ },
676
+
513
677
  // T024: Auto-populate SSL paths when SSL is enabled
514
678
  autoPopulateCertPaths() {
515
679
  if (this.domains.form.ssl.enabled) {
@@ -518,11 +682,11 @@ createApp({
518
682
  if (domain) {
519
683
  const platform = this.settings.platform;
520
684
  if (platform === 'win32') {
521
- this.domains.form.ssl.certPath = `C:\\Certbot\\live\\${domain}\\fullchain.pem`;
522
- this.domains.form.ssl.keyPath = `C:\\Certbot\\live\\${domain}\\privkey.pem`;
685
+ this.domains.form.ssl.certPath = `C:\\easy-devops\\ssl\\${domain}\\fullchain.pem`;
686
+ this.domains.form.ssl.keyPath = `C:\\easy-devops\\ssl\\${domain}\\privkey.pem`;
523
687
  } else {
524
- this.domains.form.ssl.certPath = `/etc/letsencrypt/live/${domain}/fullchain.pem`;
525
- this.domains.form.ssl.keyPath = `/etc/letsencrypt/live/${domain}/privkey.pem`;
688
+ this.domains.form.ssl.certPath = `/etc/easy-devops/ssl/${domain}/fullchain.pem`;
689
+ this.domains.form.ssl.keyPath = `/etc/easy-devops/ssl/${domain}/privkey.pem`;
526
690
  }
527
691
  }
528
692
  }
@@ -537,7 +701,8 @@ createApp({
537
701
  if (r.ok) {
538
702
  this.settings.dashboardPort = r.data.dashboardPort;
539
703
  this.settings.nginxDir = r.data.nginxDir;
540
- this.settings.certbotDir = r.data.certbotDir;
704
+ this.settings.sslDir = r.data.sslDir;
705
+ this.settings.acmeEmail = r.data.acmeEmail || '';
541
706
  this.settings.platform = r.data.platform || 'linux'; // T022
542
707
  }
543
708
  },
@@ -547,7 +712,8 @@ createApp({
547
712
  const payload = {
548
713
  dashboardPort: this.settings.dashboardPort,
549
714
  nginxDir: this.settings.nginxDir,
550
- certbotDir: this.settings.certbotDir,
715
+ sslDir: this.settings.sslDir,
716
+ acmeEmail: this.settings.acmeEmail,
551
717
  };
552
718
  if (this.settings.password) payload.dashboardPassword = this.settings.password;
553
719
  const r = await this.api('POST', '/api/settings', payload);
@@ -556,4 +722,14 @@ createApp({
556
722
  else { this.settings.error = r.data.error || 'Save failed'; }
557
723
  },
558
724
  },
559
- }).mount('#app');
725
+ })
726
+ .directive('click-outside', {
727
+ mounted(el, binding) {
728
+ el._clickOutside = (e) => { if (!el.contains(e.target)) binding.value(e); };
729
+ document.addEventListener('mousedown', el._clickOutside);
730
+ },
731
+ unmounted(el) {
732
+ document.removeEventListener('mousedown', el._clickOutside);
733
+ },
734
+ })
735
+ .mount('#app');
@@ -1,79 +1,19 @@
1
1
  import express from 'express';
2
2
  import fs from 'fs/promises';
3
+ import path from 'path';
3
4
  import { run } from '../../core/shell.js';
4
5
  import { loadConfig } from '../../core/config.js';
5
6
  import { getDomains, saveDomains, findDomain, createDomain, DOMAIN_DEFAULTS } from '../lib/domains-db.js';
6
7
  import { generateConf, getDefaultCertPaths } from '../lib/nginx-conf-generator.js';
7
8
  import { getCertExpiry } from '../lib/cert-reader.js';
9
+ import { nginxTestCmd, nginxReloadCmd } from '../../core/platform.js';
10
+ import {
11
+ validateDomainName, validatePort, validateUpstreamType,
12
+ validateMaxBodySize, validatePositiveInteger,
13
+ } from '../../core/validators.js';
8
14
 
9
15
  const router = express.Router();
10
16
 
11
- // ─── shared nginx helpers ─────────────────────────────────────────────────────
12
-
13
- const isWindows = process.platform === 'win32';
14
-
15
- function getNginxExe(nginxDir) {
16
- return isWindows ? `${nginxDir}\\nginx.exe` : 'nginx';
17
- }
18
-
19
- function nginxTestCmd(nginxDir) {
20
- const exe = getNginxExe(nginxDir);
21
- // Use explicit -c flag on Windows to avoid path issues
22
- if (isWindows) {
23
- const confPath = `${nginxDir}\\conf\\nginx.conf`;
24
- return `& "${exe}" -c "${confPath}" -t`;
25
- }
26
- return 'nginx -t';
27
- }
28
-
29
- function nginxReloadCmd(nginxDir) {
30
- const exe = getNginxExe(nginxDir);
31
- return isWindows ? `& "${exe}" -s reload` : 'nginx -s reload';
32
- }
33
-
34
- // ─── Validation helpers ───────────────────────────────────────────────────────
35
-
36
- function validateDomainName(name) {
37
- if (!name || typeof name !== 'string') {
38
- return 'name is required';
39
- }
40
- // Allow wildcards and standard hostnames
41
- const validPattern = /^(\*\.)?[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*$/;
42
- if (!validPattern.test(name) || name.includes('/') || name.includes(' ')) {
43
- return 'Invalid domain name format';
44
- }
45
- return null;
46
- }
47
-
48
- function validatePort(port) {
49
- const portNum = Number(port);
50
- if (!Number.isInteger(portNum) || portNum < 1 || portNum > 65535) {
51
- return 'Invalid port: must be 1-65535';
52
- }
53
- return null;
54
- }
55
-
56
- function validateUpstreamType(type) {
57
- if (type && !['http', 'https', 'ws'].includes(type)) {
58
- return 'Invalid upstreamType: must be http, https, or ws';
59
- }
60
- return null;
61
- }
62
-
63
- function validateMaxBodySize(size) {
64
- if (size && !/^\d+[kmgKMG]?$/.test(size)) {
65
- return 'Invalid maxBodySize format (e.g., 10m, 1g)';
66
- }
67
- return null;
68
- }
69
-
70
- function validatePositiveInteger(val, field) {
71
- if (val !== undefined && (!Number.isInteger(Number(val)) || Number(val) < 1)) {
72
- return `Invalid ${field}: must be a positive integer`;
73
- }
74
- return null;
75
- }
76
-
77
17
  // ─── GET /api/domains ─────────────────────────────────────────────────────────
78
18
 
79
19
  router.get('/', async (_req, res) => {
@@ -98,9 +38,13 @@ router.post('/', async (req, res) => {
98
38
  return res.status(400).json({ error: nameError });
99
39
  }
100
40
 
101
- const portError = validatePort(body.port);
102
- if (portError) {
103
- return res.status(400).json({ error: portError });
41
+ // Port validation is skipped when backendHost is a full external URL
42
+ const backendIsUrl = /^https?:\/\//i.test(body.backendHost ?? '');
43
+ if (!backendIsUrl) {
44
+ const portError = validatePort(body.port);
45
+ if (portError) {
46
+ return res.status(400).json({ error: portError });
47
+ }
104
48
  }
105
49
 
106
50
  // Validate optional fields
@@ -154,6 +98,8 @@ router.post('/', async (req, res) => {
154
98
  accessLog: body.advanced?.accessLog ?? true,
155
99
  customLocations: body.advanced?.customLocations ?? '',
156
100
  },
101
+ wildcard: body.wildcard ?? false,
102
+ enabled: true,
157
103
  });
158
104
 
159
105
  // Cert existence check (FR-001): prevent saving a config that references non-existent cert files
@@ -202,8 +148,9 @@ router.put('/:name', async (req, res) => {
202
148
 
203
149
  const body = req.body ?? {};
204
150
 
205
- // Validate port if provided
206
- if (body.port !== undefined) {
151
+ // Validate port if provided (skip when backendHost is a full external URL)
152
+ const backendIsUrl = /^https?:\/\//i.test(body.backendHost ?? existing.backendHost ?? '');
153
+ if (body.port !== undefined && !backendIsUrl) {
207
154
  const portError = validatePort(body.port);
208
155
  if (portError) {
209
156
  return res.status(400).json({ error: portError });
@@ -230,6 +177,7 @@ router.put('/:name', async (req, res) => {
230
177
  performance: { ...existing.performance, ...updates.performance },
231
178
  security: { ...existing.security, ...updates.security },
232
179
  advanced: { ...existing.advanced, ...updates.advanced },
180
+ wildcard: updates.wildcard ?? existing.wildcard ?? false,
233
181
  updatedAt: new Date().toISOString(),
234
182
  };
235
183
 
@@ -272,11 +220,13 @@ router.put('/:name', async (req, res) => {
272
220
 
273
221
  router.delete('/:name', async (req, res) => {
274
222
  const { name } = req.params;
223
+ const deleteCert = req.query.deleteCert === 'true';
275
224
  const domain = findDomain(name);
276
225
  if (!domain) {
277
226
  return res.status(404).json({ error: `Domain not found: ${name}` });
278
227
  }
279
228
 
229
+ // Delete nginx conf file
280
230
  if (domain.configFile) {
281
231
  try {
282
232
  await fs.unlink(domain.configFile);
@@ -287,10 +237,84 @@ router.delete('/:name', async (req, res) => {
287
237
  }
288
238
  }
289
239
 
240
+ // Optionally delete the SSL certificate directory for this domain
241
+ if (deleteCert && domain.ssl?.enabled) {
242
+ const { sslDir } = loadConfig();
243
+ const certDir = path.join(sslDir, name);
244
+ try {
245
+ await fs.rm(certDir, { recursive: true, force: true });
246
+ } catch { /* ignore — cert dir may not exist */ }
247
+ }
248
+
290
249
  const domains = getDomains().filter((d) => d.name !== name);
291
250
  saveDomains(domains);
292
251
 
293
- res.json({ message: `Domain deleted: ${name}` });
252
+ res.json({ deleted: name, certDeleted: deleteCert && domain.ssl?.enabled });
253
+ });
254
+
255
+ // ─── PUT /api/domains/:name/toggle ───────────────────────────────────────────
256
+
257
+ router.put('/:name/toggle', async (req, res) => {
258
+ const { name } = req.params;
259
+ const domain = findDomain(name);
260
+ if (!domain) {
261
+ return res.status(404).json({ error: `Domain not found: ${name}` });
262
+ }
263
+
264
+ const { nginxDir } = loadConfig();
265
+ const isEnabled = domain.enabled !== false;
266
+ const confPath = domain.configFile;
267
+
268
+ if (!confPath) {
269
+ return res.status(400).json({ error: 'Domain has no config file path stored' });
270
+ }
271
+
272
+ // Normalise paths regardless of current stored extension
273
+ const basePath = confPath.replace(/\.disabled$/, '');
274
+ const enabledPath = basePath;
275
+ const disabledPath = `${basePath}.disabled`;
276
+
277
+ if (isEnabled) {
278
+ // Disable: rename .conf → .conf.disabled, then reload nginx
279
+ try {
280
+ await fs.rename(enabledPath, disabledPath);
281
+ } catch (err) {
282
+ return res.status(500).json({ error: 'Failed to rename config file', details: err.message });
283
+ }
284
+
285
+ const domains = getDomains();
286
+ const idx = domains.findIndex(d => d.name === name);
287
+ domains[idx] = { ...domain, enabled: false, configFile: disabledPath, updatedAt: new Date().toISOString() };
288
+ saveDomains(domains);
289
+
290
+ // Reload so nginx stops serving this domain
291
+ await run(nginxReloadCmd(nginxDir), { cwd: nginxDir });
292
+
293
+ return res.json({ enabled: false });
294
+ } else {
295
+ // Enable: rename .conf.disabled → .conf, test, reload
296
+ try {
297
+ await fs.rename(disabledPath, enabledPath);
298
+ } catch (err) {
299
+ return res.status(500).json({ error: 'Failed to rename config file', details: err.message });
300
+ }
301
+
302
+ const testResult = await run(nginxTestCmd(nginxDir), { cwd: nginxDir });
303
+ if (!testResult.success) {
304
+ // Roll back rename
305
+ await fs.rename(enabledPath, disabledPath).catch(() => {});
306
+ return res.status(500).json({ error: 'nginx config test failed', output: testResult.stderr || testResult.stdout });
307
+ }
308
+
309
+ const domains = getDomains();
310
+ const idx = domains.findIndex(d => d.name === name);
311
+ domains[idx] = { ...domain, enabled: true, configFile: enabledPath, updatedAt: new Date().toISOString() };
312
+ saveDomains(domains);
313
+
314
+ await run(nginxReloadCmd(nginxDir), { cwd: nginxDir });
315
+
316
+ return res.json({ enabled: true });
317
+ }
294
318
  });
295
319
 
296
320
  // ─── POST /api/domains/:name/reload ──────────────────────────────────────────