easy-devops 1.2.0 → 1.2.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.
- package/README.md +1 -1
- package/package.json +3 -2
- package/src/cli/menus/dashboard.js +1 -1
- package/src/core/channels/discord.js +77 -0
- package/src/core/channels/telegram.js +120 -0
- package/src/core/db.js +8 -1
- package/src/core/domainNotifier.js +298 -0
- package/src/core/events.js +130 -0
- package/src/core/notifier.js +73 -0
- package/src/core/shell.js +59 -7
- package/src/dashboard/controllers/authController.js +29 -0
- package/src/dashboard/controllers/domainsController.js +416 -0
- package/src/dashboard/controllers/nginxController.js +128 -0
- package/src/dashboard/controllers/settingsController.js +335 -0
- package/src/dashboard/controllers/sslController.js +291 -0
- package/src/dashboard/public/img/icon.png +0 -0
- package/src/dashboard/public/img/icon_b.png +0 -0
- package/src/dashboard/public/js/app.js +108 -2
- package/src/dashboard/routes/auth.js +5 -24
- package/src/dashboard/routes/domains.js +11 -363
- package/src/dashboard/routes/nginx.js +13 -146
- package/src/dashboard/routes/settings.js +16 -232
- package/src/dashboard/routes/ssl.js +9 -276
- package/src/dashboard/server.js +2 -155
- package/src/dashboard/socketManager.js +282 -0
- package/src/dashboard/views/partials/domain-form.ejs +64 -1
- package/src/dashboard/views/partials/login.ejs +1 -1
- package/src/dashboard/views/partials/settings-panel.ejs +262 -118
- package/src/dashboard/views/partials/sidebar.ejs +1 -1
- /package/src/{dashboard/routes → core/channels}/.gitkeep +0 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Notification Dispatcher Core
|
|
3
|
+
*
|
|
4
|
+
* Named-channel registry model. Channels are stored as an array of objects:
|
|
5
|
+
* { id, name, type, ...typeSpecificFields }
|
|
6
|
+
*
|
|
7
|
+
* sendNotification(event, channelIds) looks up each ID, finds its type, and
|
|
8
|
+
* dispatches to src/core/channels/{type}.js — the send() interface is unchanged.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { dbGet, dbSet } from './db.js';
|
|
12
|
+
|
|
13
|
+
const CHANNELS_KEY = 'notification_channels';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Load the channel list from SQLite.
|
|
17
|
+
* @returns {Array} Array of channel objects (empty array if none saved)
|
|
18
|
+
*/
|
|
19
|
+
export function loadChannels() {
|
|
20
|
+
const stored = dbGet(CHANNELS_KEY);
|
|
21
|
+
return Array.isArray(stored) ? stored : [];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Persist the channel list to SQLite.
|
|
26
|
+
* @param {Array} channels
|
|
27
|
+
*/
|
|
28
|
+
export function saveChannels(channels) {
|
|
29
|
+
dbSet(CHANNELS_KEY, channels);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Find a single channel by ID.
|
|
34
|
+
* @param {string} id
|
|
35
|
+
* @returns {Object|null}
|
|
36
|
+
*/
|
|
37
|
+
export function getChannelById(id) {
|
|
38
|
+
return loadChannels().find((ch) => ch.id === id) ?? null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Send a notification to a specific set of channels identified by ID.
|
|
43
|
+
* Each channel's send() is called in a try/catch — one failure won't block others.
|
|
44
|
+
* Passing null or an empty array is a no-op (dashboard-only path).
|
|
45
|
+
*
|
|
46
|
+
* @param {Object} event - Notification event (type, message, severity, domain, timestamp)
|
|
47
|
+
* @param {string[]|null} channelIds - Array of channel IDs to dispatch to
|
|
48
|
+
*/
|
|
49
|
+
export async function sendNotification(event, channelIds = null) {
|
|
50
|
+
if (!channelIds || channelIds.length === 0) return;
|
|
51
|
+
|
|
52
|
+
const channels = loadChannels();
|
|
53
|
+
|
|
54
|
+
for (const id of channelIds) {
|
|
55
|
+
const channel = channels.find((ch) => ch.id === id);
|
|
56
|
+
if (!channel) continue;
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
const mod = await import(
|
|
60
|
+
new URL(`./channels/${channel.type}.js`, import.meta.url)
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
if (typeof mod.send !== 'function') {
|
|
64
|
+
console.error(`[notifier] channel type "${channel.type}" missing send()`);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
await mod.send(event, channel);
|
|
69
|
+
} catch (err) {
|
|
70
|
+
console.error(`[notifier] channel "${channel.name}" (${channel.type}):`, err.message);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
package/src/core/shell.js
CHANGED
|
@@ -6,17 +6,18 @@
|
|
|
6
6
|
* All modules must use this utility instead of calling child_process APIs directly.
|
|
7
7
|
*
|
|
8
8
|
* Exported functions:
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
9
|
+
* - getShell() — Returns OS-resolved shell descriptor { shell, flag }
|
|
10
|
+
* - run(cmd, options) — Executes a command and captures output; never throws
|
|
11
|
+
* - runLive(cmd, options) — Executes a command, streaming output to the terminal; never throws
|
|
12
|
+
* - runInteractive(cmd, options) — Executes a command with full stdio inheritance (Linux only); never throws
|
|
13
|
+
* - runWithStdin(cmd, argsArray, stdinData) — Spawns a process with stdin piping; never throws
|
|
13
14
|
*
|
|
14
15
|
* CommandResult shape (returned by run()):
|
|
15
|
-
*
|
|
16
|
+
* { success: boolean, stdout: string, stderr: string, exitCode: number|null, command: string }
|
|
16
17
|
*
|
|
17
18
|
* CommandOptions (accepted by run() and runLive()):
|
|
18
|
-
*
|
|
19
|
-
*
|
|
19
|
+
* { timeout?: number, cwd?: string }
|
|
20
|
+
* Defaults: timeout = 30 000 ms, cwd = process.cwd()
|
|
20
21
|
*/
|
|
21
22
|
|
|
22
23
|
import { spawn } from 'child_process';
|
|
@@ -185,3 +186,54 @@ export function runInteractive(cmd, options = {}) {
|
|
|
185
186
|
});
|
|
186
187
|
});
|
|
187
188
|
}
|
|
189
|
+
|
|
190
|
+
// ─── runWithStdin ─────────────────────────────────────────────────────────────
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Spawns a process directly (no shell wrapper) and pipes data to its stdin.
|
|
194
|
+
* Intended for commands like `sudo -S` that read input from stdin.
|
|
195
|
+
*
|
|
196
|
+
* Never throws. The returned Promise always resolves with a CommandResult.
|
|
197
|
+
*
|
|
198
|
+
* @param {string} cmd - The executable to spawn (e.g. 'sudo').
|
|
199
|
+
* @param {string[]} argsArray - Arguments passed directly to the executable.
|
|
200
|
+
* @param {string} [stdinData=''] - String to pipe to the process stdin.
|
|
201
|
+
* @returns {Promise<{ success: boolean, stdout: string, stderr: string, exitCode: number|null, command: string }>}
|
|
202
|
+
*/
|
|
203
|
+
export function runWithStdin(cmd, argsArray, stdinData = '') {
|
|
204
|
+
return new Promise((resolve) => {
|
|
205
|
+
const child = spawn(cmd, argsArray, {
|
|
206
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
207
|
+
windowsHide: true,
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
let stdoutBuf = '';
|
|
211
|
+
let stderrBuf = '';
|
|
212
|
+
|
|
213
|
+
child.stdout.on('data', (chunk) => { stdoutBuf += chunk; });
|
|
214
|
+
child.stderr.on('data', (chunk) => { stderrBuf += chunk; });
|
|
215
|
+
|
|
216
|
+
child.stdin.write(stdinData);
|
|
217
|
+
child.stdin.end();
|
|
218
|
+
|
|
219
|
+
child.on('close', (exitCode) => {
|
|
220
|
+
resolve({
|
|
221
|
+
success: exitCode === 0,
|
|
222
|
+
stdout: stripAnsi(stdoutBuf).trim(),
|
|
223
|
+
stderr: stripAnsi(stderrBuf).trim(),
|
|
224
|
+
exitCode,
|
|
225
|
+
command: cmd,
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
child.on('error', (err) => {
|
|
230
|
+
resolve({
|
|
231
|
+
success: false,
|
|
232
|
+
stdout: '',
|
|
233
|
+
stderr: err.message,
|
|
234
|
+
exitCode: null,
|
|
235
|
+
command: cmd,
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { loadConfig } from '../../core/config.js';
|
|
2
|
+
|
|
3
|
+
class AuthController {
|
|
4
|
+
login(req, res) {
|
|
5
|
+
const { password } = req.body;
|
|
6
|
+
const { dashboardPassword } = loadConfig();
|
|
7
|
+
if (password === dashboardPassword) {
|
|
8
|
+
req.session.authenticated = true;
|
|
9
|
+
res.json({ ok: true });
|
|
10
|
+
} else {
|
|
11
|
+
res.status(401).json({ ok: false, error: 'Invalid password' });
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
check(req, res) {
|
|
16
|
+
if (req.session.authenticated === true) {
|
|
17
|
+
res.json({ authenticated: true });
|
|
18
|
+
} else {
|
|
19
|
+
res.json({ authenticated: false });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
logout(req, res) {
|
|
24
|
+
req.session.destroy();
|
|
25
|
+
res.json({ ok: true });
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default AuthController;
|
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { run } from '../../core/shell.js';
|
|
4
|
+
import { loadConfig } from '../../core/config.js';
|
|
5
|
+
import { getDomains, saveDomains, findDomain, createDomain, DOMAIN_DEFAULTS } from '../lib/domains-db.js';
|
|
6
|
+
import { generateConf } from '../lib/nginx-conf-generator.js';
|
|
7
|
+
import { getCertExpiry } from '../lib/cert-reader.js';
|
|
8
|
+
import { isWindows, nginxTestCmd, nginxReloadCmd, isNginxTestOk } from '../../core/platform.js';
|
|
9
|
+
import {
|
|
10
|
+
validateDomainName, validatePort, validateUpstreamType,
|
|
11
|
+
validateMaxBodySize,
|
|
12
|
+
} from '../../core/validators.js';
|
|
13
|
+
import { getDomainNotifConfig, saveDomainNotifConfig } from '../../core/domainNotifier.js';
|
|
14
|
+
|
|
15
|
+
class DomainsController {
|
|
16
|
+
async list(req, res, next) {
|
|
17
|
+
try {
|
|
18
|
+
const domains = getDomains();
|
|
19
|
+
const result = await Promise.all(
|
|
20
|
+
domains.map(async (domain) => {
|
|
21
|
+
const { expiry, daysLeft } = await getCertExpiry(domain.name);
|
|
22
|
+
const notifications = getDomainNotifConfig(domain.name);
|
|
23
|
+
return { ...domain, certExpiry: expiry, daysLeft, notifications };
|
|
24
|
+
})
|
|
25
|
+
);
|
|
26
|
+
res.json(result);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
next(error);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async create(req, res, next) {
|
|
33
|
+
try {
|
|
34
|
+
const body = req.body ?? {};
|
|
35
|
+
|
|
36
|
+
// Validate required fields
|
|
37
|
+
const nameError = validateDomainName(body.name);
|
|
38
|
+
if (nameError) {
|
|
39
|
+
return res.status(400).json({ error: nameError });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Port validation is skipped when backendHost is a full external URL
|
|
43
|
+
const backendIsUrl = /^https?:\/\//i.test(body.backendHost ?? '');
|
|
44
|
+
if (!backendIsUrl) {
|
|
45
|
+
const portError = validatePort(body.port);
|
|
46
|
+
if (portError) {
|
|
47
|
+
return res.status(400).json({ error: portError });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Validate optional fields
|
|
52
|
+
const upstreamError = validateUpstreamType(body.upstreamType);
|
|
53
|
+
if (upstreamError) {
|
|
54
|
+
return res.status(400).json({ error: upstreamError });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const maxSizeError = validateMaxBodySize(body.performance?.maxBodySize);
|
|
58
|
+
if (maxSizeError) {
|
|
59
|
+
return res.status(400).json({ error: maxSizeError });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Check for duplicate
|
|
63
|
+
if (findDomain(body.name)) {
|
|
64
|
+
return res.status(409).json({ error: `Domain already exists: ${body.name}` });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Build domain object with defaults
|
|
68
|
+
const domain = createDomain({
|
|
69
|
+
name: body.name,
|
|
70
|
+
port: Number(body.port),
|
|
71
|
+
backendHost: body.backendHost ?? DOMAIN_DEFAULTS.backendHost,
|
|
72
|
+
upstreamType: body.upstreamType ?? DOMAIN_DEFAULTS.upstreamType,
|
|
73
|
+
www: body.www ?? false,
|
|
74
|
+
ssl: {
|
|
75
|
+
enabled: body.ssl?.enabled ?? false,
|
|
76
|
+
certPath: body.ssl?.certPath ?? '',
|
|
77
|
+
keyPath: body.ssl?.keyPath ?? '',
|
|
78
|
+
redirect: body.ssl?.redirect ?? true,
|
|
79
|
+
hsts: body.ssl?.hsts ?? false,
|
|
80
|
+
hstsMaxAge: body.ssl?.hstsMaxAge ?? DOMAIN_DEFAULTS.ssl.hstsMaxAge,
|
|
81
|
+
},
|
|
82
|
+
performance: {
|
|
83
|
+
maxBodySize: body.performance?.maxBodySize ?? DOMAIN_DEFAULTS.performance.maxBodySize,
|
|
84
|
+
readTimeout: body.performance?.readTimeout ?? DOMAIN_DEFAULTS.performance.readTimeout,
|
|
85
|
+
connectTimeout: body.performance?.connectTimeout ?? DOMAIN_DEFAULTS.performance.connectTimeout,
|
|
86
|
+
proxyBuffers: body.performance?.proxyBuffers ?? false,
|
|
87
|
+
gzip: body.performance?.gzip ?? true,
|
|
88
|
+
gzipTypes: body.performance?.gzipTypes ?? DOMAIN_DEFAULTS.performance.gzipTypes,
|
|
89
|
+
},
|
|
90
|
+
security: {
|
|
91
|
+
rateLimit: body.security?.rateLimit ?? false,
|
|
92
|
+
rateLimitRate: body.security?.rateLimitRate ?? DOMAIN_DEFAULTS.security.rateLimitRate,
|
|
93
|
+
rateLimitBurst: body.security?.rateLimitBurst ?? DOMAIN_DEFAULTS.security.rateLimitBurst,
|
|
94
|
+
securityHeaders: body.security?.securityHeaders ?? false,
|
|
95
|
+
custom404: body.security?.custom404 ?? false,
|
|
96
|
+
custom50x: body.security?.custom50x ?? false,
|
|
97
|
+
},
|
|
98
|
+
advanced: {
|
|
99
|
+
accessLog: body.advanced?.accessLog ?? true,
|
|
100
|
+
customLocations: body.advanced?.customLocations ?? '',
|
|
101
|
+
},
|
|
102
|
+
wildcard: body.wildcard ?? false,
|
|
103
|
+
enabled: true,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// Cert existence check (FR-001): prevent saving a config that references non-existent cert files
|
|
107
|
+
if (domain.ssl.enabled && domain.ssl.certPath) {
|
|
108
|
+
try {
|
|
109
|
+
await fs.access(domain.ssl.certPath, fs.constants.F_OK);
|
|
110
|
+
} catch {
|
|
111
|
+
return res.status(422).json({
|
|
112
|
+
error: 'cert_missing',
|
|
113
|
+
certPath: domain.ssl.certPath,
|
|
114
|
+
keyPath: domain.ssl.keyPath,
|
|
115
|
+
hint: 'The SSL certificate files do not exist at the configured paths. Create the certificate first, or disable SSL.',
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const { nginxDir } = loadConfig();
|
|
121
|
+
|
|
122
|
+
try {
|
|
123
|
+
await generateConf(domain);
|
|
124
|
+
} catch (err) {
|
|
125
|
+
return res.status(500).json({ error: 'Failed to write nginx conf', details: err.message });
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const testCmd = isWindows ? nginxTestCmd(nginxDir) : 'nginx -t';
|
|
129
|
+
const testResult = await run(testCmd, { cwd: nginxDir });
|
|
130
|
+
if (!isNginxTestOk(testResult)) {
|
|
131
|
+
try { await fs.unlink(domain.configFile); } catch { /* ignore */ }
|
|
132
|
+
const output = testResult.stderr || testResult.stdout;
|
|
133
|
+
const error = !isWindows && output.includes('password is required')
|
|
134
|
+
? 'Linux permissions not configured. Open Settings → Setup Linux Permissions.'
|
|
135
|
+
: 'nginx config test failed';
|
|
136
|
+
return res.status(500).json({ error, output });
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const domains = getDomains();
|
|
140
|
+
domains.push(domain);
|
|
141
|
+
saveDomains(domains);
|
|
142
|
+
|
|
143
|
+
// Save per-domain notification config if provided
|
|
144
|
+
if (body.notifications && typeof body.notifications === 'object') {
|
|
145
|
+
try {
|
|
146
|
+
saveDomainNotifConfig(domain.name, body.notifications);
|
|
147
|
+
} catch { /* ignore validation errors — fall back to defaults */ }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
res.status(201).json({ ...domain, notifications: getDomainNotifConfig(domain.name) });
|
|
151
|
+
} catch (error) {
|
|
152
|
+
next(error);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async update(req, res, next) {
|
|
157
|
+
try {
|
|
158
|
+
const { name } = req.params;
|
|
159
|
+
const existing = findDomain(name);
|
|
160
|
+
if (!existing) {
|
|
161
|
+
return res.status(404).json({ error: `Domain not found: ${name}` });
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const body = req.body ?? {};
|
|
165
|
+
|
|
166
|
+
// Validate port if provided (skip when backendHost is a full external URL)
|
|
167
|
+
const backendIsUrl = /^https?:\/\//i.test(body.backendHost ?? existing.backendHost ?? '');
|
|
168
|
+
if (body.port !== undefined && !backendIsUrl) {
|
|
169
|
+
const portError = validatePort(body.port);
|
|
170
|
+
if (portError) {
|
|
171
|
+
return res.status(400).json({ error: portError });
|
|
172
|
+
}
|
|
173
|
+
body.port = Number(body.port);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Validate upstreamType if provided
|
|
177
|
+
if (body.upstreamType !== undefined) {
|
|
178
|
+
const upstreamError = validateUpstreamType(body.upstreamType);
|
|
179
|
+
if (upstreamError) {
|
|
180
|
+
return res.status(400).json({ error: upstreamError });
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// name is immutable — merge everything except name, configFile, and notifications
|
|
185
|
+
const { name: _ignored, configFile: _cf, notifications, ...updates } = body;
|
|
186
|
+
|
|
187
|
+
// Deep merge nested objects
|
|
188
|
+
const updatedDomain = {
|
|
189
|
+
...existing,
|
|
190
|
+
...updates,
|
|
191
|
+
ssl: { ...existing.ssl, ...updates.ssl },
|
|
192
|
+
performance: { ...existing.performance, ...updates.performance },
|
|
193
|
+
security: { ...existing.security, ...updates.security },
|
|
194
|
+
advanced: { ...existing.advanced, ...updates.advanced },
|
|
195
|
+
wildcard: updates.wildcard ?? existing.wildcard ?? false,
|
|
196
|
+
updatedAt: new Date().toISOString(),
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const { nginxDir } = loadConfig();
|
|
200
|
+
const bakPath = existing.configFile ? `${existing.configFile}.bak` : null;
|
|
201
|
+
|
|
202
|
+
// Backup existing conf
|
|
203
|
+
if (bakPath && existing.configFile) {
|
|
204
|
+
try {
|
|
205
|
+
await fs.copyFile(existing.configFile, bakPath);
|
|
206
|
+
} catch { /* file absent — skip backup */ }
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
await generateConf(updatedDomain);
|
|
211
|
+
} catch (err) {
|
|
212
|
+
if (bakPath) {
|
|
213
|
+
try { await fs.copyFile(bakPath, existing.configFile); } catch { /* ignore restore failure */ }
|
|
214
|
+
}
|
|
215
|
+
return res.status(500).json({ error: 'Failed to write nginx conf', details: err.message });
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const testCmd = isWindows ? nginxTestCmd(nginxDir) : 'nginx -t';
|
|
219
|
+
const testResult = await run(testCmd, { cwd: nginxDir });
|
|
220
|
+
if (!isNginxTestOk(testResult)) {
|
|
221
|
+
if (bakPath) {
|
|
222
|
+
try { await fs.rename(bakPath, existing.configFile); } catch { /* ignore */ }
|
|
223
|
+
}
|
|
224
|
+
const output = testResult.stderr || testResult.stdout;
|
|
225
|
+
const error = !isWindows && output.includes('password is required')
|
|
226
|
+
? 'Linux permissions not configured. Open Settings → Setup Linux Permissions.'
|
|
227
|
+
: 'nginx config test failed';
|
|
228
|
+
return res.status(500).json({ error, output });
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const domains = getDomains();
|
|
232
|
+
const idx = domains.findIndex((d) => d.name === name);
|
|
233
|
+
domains[idx] = updatedDomain;
|
|
234
|
+
saveDomains(domains);
|
|
235
|
+
|
|
236
|
+
// Save per-domain notification config if provided
|
|
237
|
+
if (notifications && typeof notifications === 'object') {
|
|
238
|
+
try {
|
|
239
|
+
saveDomainNotifConfig(name, notifications);
|
|
240
|
+
} catch { /* ignore validation errors — keep existing config */ }
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
res.json({ ...updatedDomain, notifications: getDomainNotifConfig(name) });
|
|
244
|
+
} catch (error) {
|
|
245
|
+
next(error);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async delete(req, res, next) {
|
|
250
|
+
try {
|
|
251
|
+
const { name } = req.params;
|
|
252
|
+
const deleteCert = req.query.deleteCert === 'true';
|
|
253
|
+
const domain = findDomain(name);
|
|
254
|
+
if (!domain) {
|
|
255
|
+
return res.status(404).json({ error: `Domain not found: ${name}` });
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Delete nginx conf file
|
|
259
|
+
if (domain.configFile) {
|
|
260
|
+
try {
|
|
261
|
+
await fs.unlink(domain.configFile);
|
|
262
|
+
} catch (err) {
|
|
263
|
+
if (err.code !== 'ENOENT') {
|
|
264
|
+
return res.status(500).json({ error: 'Failed to delete conf file', details: err.message });
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Optionally delete the SSL certificate directory for this domain
|
|
270
|
+
if (deleteCert && domain.ssl?.enabled) {
|
|
271
|
+
const { sslDir } = loadConfig();
|
|
272
|
+
const certDir = path.join(sslDir, name);
|
|
273
|
+
try {
|
|
274
|
+
await fs.rm(certDir, { recursive: true, force: true });
|
|
275
|
+
} catch { /* ignore — cert dir may not exist */ }
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const domains = getDomains().filter((d) => d.name !== name);
|
|
279
|
+
saveDomains(domains);
|
|
280
|
+
|
|
281
|
+
res.json({ deleted: name, certDeleted: deleteCert && domain.ssl?.enabled });
|
|
282
|
+
} catch (error) {
|
|
283
|
+
next(error);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
async toggle(req, res, next) {
|
|
288
|
+
try {
|
|
289
|
+
const { name } = req.params;
|
|
290
|
+
const domain = findDomain(name);
|
|
291
|
+
if (!domain) {
|
|
292
|
+
return res.status(404).json({ error: `Domain not found: ${name}` });
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const { nginxDir } = loadConfig();
|
|
296
|
+
const isEnabled = domain.enabled !== false;
|
|
297
|
+
const confPath = domain.configFile;
|
|
298
|
+
|
|
299
|
+
if (!confPath) {
|
|
300
|
+
return res.status(400).json({ error: 'Domain has no config file path stored' });
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Normalise paths regardless of current stored extension
|
|
304
|
+
const basePath = confPath.replace(/\.disabled$/, '');
|
|
305
|
+
const enabledPath = basePath;
|
|
306
|
+
const disabledPath = `${basePath}.disabled`;
|
|
307
|
+
|
|
308
|
+
if (isEnabled) {
|
|
309
|
+
// Disable: rename .conf → .conf.disabled, then reload nginx
|
|
310
|
+
try {
|
|
311
|
+
await fs.rename(enabledPath, disabledPath);
|
|
312
|
+
} catch (err) {
|
|
313
|
+
return res.status(500).json({ error: 'Failed to rename config file', details: err.message });
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const domains = getDomains();
|
|
317
|
+
const idx = domains.findIndex(d => d.name === name);
|
|
318
|
+
domains[idx] = { ...domain, enabled: false, configFile: disabledPath, updatedAt: new Date().toISOString() };
|
|
319
|
+
saveDomains(domains);
|
|
320
|
+
|
|
321
|
+
// Reload so nginx stops serving this domain
|
|
322
|
+
await run(isWindows ? nginxReloadCmd(nginxDir) : 'sudo -n /usr/bin/systemctl reload nginx', { cwd: nginxDir });
|
|
323
|
+
|
|
324
|
+
return res.json({ enabled: false });
|
|
325
|
+
} else {
|
|
326
|
+
// Enable: rename .conf.disabled → .conf, test, reload
|
|
327
|
+
try {
|
|
328
|
+
await fs.rename(disabledPath, enabledPath);
|
|
329
|
+
} catch (err) {
|
|
330
|
+
return res.status(500).json({ error: 'Failed to rename config file', details: err.message });
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const testCmd = isWindows ? nginxTestCmd(nginxDir) : 'nginx -t';
|
|
334
|
+
const testResult = await run(testCmd, { cwd: nginxDir });
|
|
335
|
+
if (!isNginxTestOk(testResult)) {
|
|
336
|
+
// Roll back rename
|
|
337
|
+
await fs.rename(enabledPath, disabledPath).catch(() => {});
|
|
338
|
+
const output = testResult.stderr || testResult.stdout;
|
|
339
|
+
const error = !isWindows && output.includes('password is required')
|
|
340
|
+
? 'Linux permissions not configured. Open Settings → Setup Linux Permissions.'
|
|
341
|
+
: 'nginx config test failed';
|
|
342
|
+
return res.status(500).json({ error, output });
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const domains = getDomains();
|
|
346
|
+
const idx = domains.findIndex(d => d.name === name);
|
|
347
|
+
domains[idx] = { ...domain, enabled: true, configFile: enabledPath, updatedAt: new Date().toISOString() };
|
|
348
|
+
saveDomains(domains);
|
|
349
|
+
|
|
350
|
+
await run(isWindows ? nginxReloadCmd(nginxDir) : 'sudo -n /usr/bin/systemctl reload nginx', { cwd: nginxDir });
|
|
351
|
+
|
|
352
|
+
return res.json({ enabled: true });
|
|
353
|
+
}
|
|
354
|
+
} catch (error) {
|
|
355
|
+
next(error);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
async reload(req, res, next) {
|
|
360
|
+
try {
|
|
361
|
+
const { name } = req.params;
|
|
362
|
+
if (!findDomain(name)) {
|
|
363
|
+
return res.status(404).json({ error: `Domain not found: ${name}` });
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const { nginxDir } = loadConfig();
|
|
367
|
+
|
|
368
|
+
const testCmd = isWindows ? nginxTestCmd(nginxDir) : 'nginx -t';
|
|
369
|
+
const testResult = await run(testCmd, { cwd: nginxDir });
|
|
370
|
+
if (!isNginxTestOk(testResult)) {
|
|
371
|
+
const output = testResult.stderr || testResult.stdout;
|
|
372
|
+
const error = !isWindows && output.includes('password is required')
|
|
373
|
+
? 'Linux permissions not configured. Open Settings → Setup Linux Permissions.'
|
|
374
|
+
: 'nginx config test failed';
|
|
375
|
+
return res.status(500).json({ error, output });
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const reloadCmd = isWindows ? nginxReloadCmd(nginxDir) : 'sudo -n /usr/bin/systemctl reload nginx';
|
|
379
|
+
const reloadResult = await run(reloadCmd, { cwd: nginxDir });
|
|
380
|
+
if (!reloadResult.success) {
|
|
381
|
+
const output = reloadResult.stderr || reloadResult.stdout;
|
|
382
|
+
const error = !isWindows && output.includes('password is required')
|
|
383
|
+
? 'Linux permissions not configured. Open Settings → Setup Linux Permissions.'
|
|
384
|
+
: 'nginx reload failed';
|
|
385
|
+
return res.status(500).json({ error, output });
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
res.json({ message: 'nginx reloaded successfully' });
|
|
389
|
+
} catch (error) {
|
|
390
|
+
next(error);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
async getNotifications(req, res, next) {
|
|
395
|
+
try {
|
|
396
|
+
const config = getDomainNotifConfig(req.params.name);
|
|
397
|
+
res.json(config);
|
|
398
|
+
} catch (error) {
|
|
399
|
+
next(error);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
async updateNotifications(req, res, next) {
|
|
404
|
+
try {
|
|
405
|
+
saveDomainNotifConfig(req.params.name, req.body ?? {});
|
|
406
|
+
res.json(getDomainNotifConfig(req.params.name));
|
|
407
|
+
} catch (error) {
|
|
408
|
+
if (error.message?.startsWith('Invalid') || error.message?.startsWith('Config') || error.message?.startsWith('Entry')) {
|
|
409
|
+
return res.status(400).json({ error: error.message });
|
|
410
|
+
}
|
|
411
|
+
next(error);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
export default DomainsController;
|