easy-devops 1.2.0 → 1.3.0
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 +97 -6
- package/dist/ui/assets/index-B6zdwEnG.css +1 -0
- package/dist/ui/assets/index-Cy0oNmul.js +442 -0
- package/dist/ui/assets/socket-DwLPrgsr.js +1 -0
- package/dist/ui/assets/socket-K5ttLD2q.js +65 -0
- package/dist/ui/brand/apple-touch-icon.png +0 -0
- package/dist/ui/brand/favicon-16.png +0 -0
- package/dist/ui/brand/favicon-32.png +0 -0
- package/dist/ui/brand/favicon-light-16.png +0 -0
- package/dist/ui/brand/favicon-light-32.png +0 -0
- package/dist/ui/brand/favicon.ico +0 -0
- package/dist/ui/brand/icon-192.png +0 -0
- package/dist/ui/brand/icon-48.png +0 -0
- package/dist/ui/brand/icon-512.png +0 -0
- package/dist/ui/brand/icon-64-light.png +0 -0
- package/dist/ui/brand/icon-64.png +0 -0
- package/dist/ui/brand/logo-dark.png +0 -0
- package/dist/ui/brand/logo-light.png +0 -0
- package/dist/ui/brand/mark-dark.png +0 -0
- package/dist/ui/brand/mark-light.png +0 -0
- package/dist/ui/index.html +71 -0
- package/install.ps1 +188 -1
- package/install.sh +254 -29
- package/package.json +17 -5
- package/src/cli/dash.js +110 -0
- package/src/cli/index.js +237 -106
- package/src/cli/managers/domain-manager.js +60 -40
- package/src/cli/managers/ssl-manager.js +47 -330
- package/src/cli/menu.js +156 -0
- package/src/cli/menus/cleanup.js +205 -0
- package/src/cli/menus/dashboard.js +93 -2
- package/src/cli/menus/runtime.js +236 -0
- package/src/cli/menus/service.js +764 -0
- package/src/cli/menus/settings.js +176 -8
- package/src/cli/menus/uninstall.js +167 -0
- package/src/cli/menus/update.js +221 -65
- package/src/core/acme-challenge.js +273 -0
- package/src/core/cert-operations.js +189 -0
- package/src/{dashboard/lib → core}/cert-reader.js +2 -2
- package/src/core/cert-renewal.js +494 -0
- package/src/core/channels/discord.js +77 -0
- package/src/core/channels/telegram.js +120 -0
- package/src/core/cleanup.js +600 -0
- package/src/core/config.js +169 -2
- package/src/core/daemon/ipc.js +292 -0
- package/src/core/daemon/lock.js +294 -0
- package/src/core/daemon/run.js +241 -0
- package/src/core/daemon/stopRequest.js +162 -0
- package/src/core/db.js +44 -8
- package/src/core/deploy/apps.js +491 -0
- package/src/core/deploy/domainLink.js +200 -0
- package/src/core/deploy/engine.js +973 -0
- package/src/core/deploy/ezfile.js +361 -0
- package/src/core/deploy/github.js +239 -0
- package/src/core/deploy/githubApi.js +261 -0
- package/src/core/deploy/hookServer.js +325 -0
- package/src/core/deploy/hubCommands.js +267 -0
- package/src/core/deploy/launcher.js +57 -0
- package/src/core/deploy/status.js +91 -0
- package/src/core/deploy/store.js +398 -0
- package/src/core/domainNotifier.js +364 -0
- package/src/core/domains-db.js +234 -0
- package/src/core/events.js +222 -0
- package/src/core/hub/agent.js +842 -0
- package/src/core/hubLink.js +306 -0
- package/src/core/hubPermissions.js +430 -0
- package/src/core/installSource.js +162 -0
- package/src/core/jobs/bus.js +120 -0
- package/src/core/jobs/index.js +371 -0
- package/src/core/jobs/monitors.js +368 -0
- package/src/core/jobs/owner.js +217 -0
- package/src/core/jobs/revisionWatcher.js +106 -0
- package/src/core/nginx-apply.js +213 -0
- package/src/core/nginx-conf-generator.js +253 -17
- package/src/{dashboard/lib → core}/nginx-service.js +18 -9
- package/src/core/notifications/claim.js +214 -0
- package/src/core/notifications/dispatch.js +154 -0
- package/src/core/notifier.js +103 -0
- package/src/core/passwords.js +172 -0
- package/src/core/permissions.js +8 -4
- package/src/core/platform.js +709 -2
- package/src/core/probeCache.js +258 -0
- package/src/core/purge.js +372 -0
- package/src/core/runner.js +488 -0
- package/src/core/service/acl.js +226 -0
- package/src/core/service/dataDir.js +345 -0
- package/src/core/service/index.js +1128 -0
- package/src/core/service/legacy.js +218 -0
- package/src/core/service/runtime.js +446 -0
- package/src/core/service/runtimeUpdate.js +800 -0
- package/src/core/service/systemd.js +458 -0
- package/src/core/service/windowsPersonal.js +530 -0
- package/src/core/service/windowsService.js +576 -0
- package/src/core/service/winsw.js +260 -0
- package/src/core/setupCode.js +331 -0
- package/src/core/shell.js +128 -13
- package/src/core/ssl-issuer.js +627 -0
- package/src/core/updater.js +295 -0
- package/src/core/validators.js +99 -0
- package/src/dashboard/controllers/authController.js +316 -0
- package/src/dashboard/controllers/deploymentsController.js +479 -0
- package/src/dashboard/controllers/domainsController.js +403 -0
- package/src/dashboard/controllers/hubController.js +135 -0
- package/src/dashboard/controllers/nginxController.js +146 -0
- package/src/dashboard/controllers/processesController.js +368 -0
- package/src/dashboard/controllers/settingsController.js +503 -0
- package/src/dashboard/controllers/sslController.js +452 -0
- package/src/dashboard/lib/daemonBridge.js +163 -0
- package/src/dashboard/lib/localRequest.js +235 -0
- package/src/dashboard/lib/loginRateLimit.js +119 -0
- package/src/dashboard/lib/requireAuth.js +80 -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 +595 -19
- package/src/dashboard/routes/auth.js +14 -33
- package/src/dashboard/routes/deployments.js +46 -0
- package/src/dashboard/routes/domains.js +20 -363
- package/src/dashboard/routes/hub.js +22 -0
- package/src/dashboard/routes/nginx.js +22 -146
- package/src/dashboard/routes/processes.js +29 -0
- package/src/dashboard/routes/settings.js +25 -232
- package/src/dashboard/routes/ssl.js +18 -276
- package/src/dashboard/server.js +279 -165
- package/src/dashboard/socketManager.js +246 -0
- package/src/dashboard/views/index.ejs +1 -0
- package/src/dashboard/views/partials/domain-form.ejs +64 -1
- package/src/dashboard/views/partials/login.ejs +69 -8
- package/src/dashboard/views/partials/processes-panel.ejs +261 -0
- package/src/dashboard/views/partials/settings-panel.ejs +549 -118
- package/src/dashboard/views/partials/sidebar.ejs +3 -1
- package/src/dashboard/lib/domains-db.js +0 -51
- /package/src/{dashboard/routes → core/channels}/.gitkeep +0 -0
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import os from 'os';
|
|
4
|
+
import crypto from 'crypto';
|
|
5
|
+
import {
|
|
6
|
+
loadConfig, saveConfig, BIND_ALL, BIND_LOOPBACK, BIND_LOOPBACK_V6,
|
|
7
|
+
AUTH_MODES, UI_MODES, isLoopbackBind, effectiveAuthMode,
|
|
8
|
+
} from '../../core/config.js';
|
|
9
|
+
import { isLoopbackAddress } from '../lib/localRequest.js';
|
|
10
|
+
import { hashPassword, validateNewPassword } from '../../core/passwords.js';
|
|
11
|
+
import { validatePort, validateEmail } from '../../core/validators.js';
|
|
12
|
+
import {
|
|
13
|
+
getAutoRenewSettings, normalizeAutoRenewDays, MIN_AUTO_RENEW_DAYS, MAX_AUTO_RENEW_DAYS,
|
|
14
|
+
} from '../../core/cert-renewal.js';
|
|
15
|
+
import { run, runWithStdin } from '../../core/shell.js';
|
|
16
|
+
import { checkPermissionsConfigured } from '../../core/permissions.js';
|
|
17
|
+
import { dbGet, dbSet } from '../../core/db.js';
|
|
18
|
+
import { deployRoot, defaultDeployRoot, validateDeployDir } from '../../core/deploy/store.js';
|
|
19
|
+
import { saveDomains } from '../../core/domains-db.js';
|
|
20
|
+
import { loadChannels, saveChannels, sendNotification } from '../../core/notifier.js';
|
|
21
|
+
|
|
22
|
+
class SettingsController {
|
|
23
|
+
get(req, res) {
|
|
24
|
+
try {
|
|
25
|
+
const config = loadConfig();
|
|
26
|
+
const { dashboardPort, nginxDir, sslDir, acmeEmail, dashboardBindAddress } = config;
|
|
27
|
+
const hasPassword = Boolean(config.dashboardPasswordHash || config.dashboardPassword);
|
|
28
|
+
const autoRenew = getAutoRenewSettings(config);
|
|
29
|
+
// Neither the password nor its hash is ever returned.
|
|
30
|
+
res.json({
|
|
31
|
+
dashboardPort,
|
|
32
|
+
nginxDir,
|
|
33
|
+
sslDir,
|
|
34
|
+
acmeEmail,
|
|
35
|
+
// Background renewal of HTTP-01 certificates (core/cert-renewal.js).
|
|
36
|
+
// The limits are published so no client hard-codes them.
|
|
37
|
+
sslAutoRenew: autoRenew.enabled,
|
|
38
|
+
sslAutoRenewDays: autoRenew.days,
|
|
39
|
+
sslAutoRenewDaysMin: MIN_AUTO_RENEW_DAYS,
|
|
40
|
+
sslAutoRenewDaysMax: MAX_AUTO_RENEW_DAYS,
|
|
41
|
+
// Where NEW deployed apps are created. Existing apps stay where they
|
|
42
|
+
// were created; moving one is an action on the Deployments page.
|
|
43
|
+
deployDir: deployRoot(),
|
|
44
|
+
deployDirDefault: defaultDeployRoot(),
|
|
45
|
+
deployDirIsDefault: !config.deployDir,
|
|
46
|
+
dashboardBindAddress,
|
|
47
|
+
bindsToLoopbackOnly: isLoopbackBind(dashboardBindAddress),
|
|
48
|
+
dashboardAuth: effectiveAuthMode(config),
|
|
49
|
+
// Anything unrecognised reads as classic, which is also the
|
|
50
|
+
// default. The value only takes effect on restart.
|
|
51
|
+
dashboardUi: config.dashboardUi === UI_MODES.NEW ? UI_MODES.NEW : UI_MODES.CLASSIC,
|
|
52
|
+
// Whether the UI may offer 'none' at all: the bind must be loopback
|
|
53
|
+
// and the person asking must be on this machine.
|
|
54
|
+
canUseNoPassword: isLoopbackBind(dashboardBindAddress) &&
|
|
55
|
+
isLoopbackAddress(req.ip ?? req.socket?.remoteAddress),
|
|
56
|
+
hasPassword,
|
|
57
|
+
platform: process.platform === 'win32' ? 'win32' : 'linux',
|
|
58
|
+
});
|
|
59
|
+
} catch (err) {
|
|
60
|
+
res.status(500).json({ error: 'Internal server error' });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async update(req, res) {
|
|
65
|
+
try {
|
|
66
|
+
const {
|
|
67
|
+
dashboardPort, dashboardPassword, nginxDir, sslDir, acmeEmail, dashboardBindAddress,
|
|
68
|
+
dashboardAuth, dashboardUi, sslAutoRenew, sslAutoRenewDays, deployDir,
|
|
69
|
+
} = req.body;
|
|
70
|
+
|
|
71
|
+
const current = loadConfig();
|
|
72
|
+
|
|
73
|
+
if (dashboardPort !== undefined) {
|
|
74
|
+
const portError = validatePort(parseInt(dashboardPort, 10));
|
|
75
|
+
if (portError) return res.status(400).json({ error: portError });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// A password set from here is held to the same minimum as the
|
|
79
|
+
// first-run one, and is never stored in clear.
|
|
80
|
+
if (dashboardPassword !== undefined) {
|
|
81
|
+
const problem = validateNewPassword(dashboardPassword);
|
|
82
|
+
if (problem) return res.status(400).json({ error: problem });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (dashboardBindAddress !== undefined &&
|
|
86
|
+
![BIND_ALL, BIND_LOOPBACK, BIND_LOOPBACK_V6].includes(dashboardBindAddress)) {
|
|
87
|
+
return res.status(400).json({
|
|
88
|
+
error: `Bind address must be ${BIND_LOOPBACK} (this machine only) or ${BIND_ALL} (all interfaces)`,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (dashboardAuth !== undefined &&
|
|
93
|
+
![AUTH_MODES.PASSWORD, AUTH_MODES.NONE].includes(dashboardAuth)) {
|
|
94
|
+
return res.status(400).json({
|
|
95
|
+
error: `Authentication must be '${AUTH_MODES.PASSWORD}' or '${AUTH_MODES.NONE}'`,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (dashboardUi !== undefined &&
|
|
100
|
+
![UI_MODES.CLASSIC, UI_MODES.NEW].includes(dashboardUi)) {
|
|
101
|
+
return res.status(400).json({
|
|
102
|
+
error: `Dashboard UI must be '${UI_MODES.CLASSIC}' or '${UI_MODES.NEW}'`,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// ── The auth/bind interlock ──────────────────────────────────────
|
|
107
|
+
//
|
|
108
|
+
// 'none' is only safe while nothing but this machine can reach the
|
|
109
|
+
// listener, so the two settings are validated TOGETHER against the
|
|
110
|
+
// state this request would produce — not against the current state.
|
|
111
|
+
// Otherwise a single request could set both and slip through by
|
|
112
|
+
// checking each against the old value.
|
|
113
|
+
const nextAuth = dashboardAuth ?? current.dashboardAuth;
|
|
114
|
+
const nextBind = dashboardBindAddress ?? current.dashboardBindAddress;
|
|
115
|
+
|
|
116
|
+
if (nextAuth === AUTH_MODES.NONE && !isLoopbackBind(nextBind)) {
|
|
117
|
+
// Which half the user is changing decides which message helps.
|
|
118
|
+
const changingBind = dashboardBindAddress !== undefined &&
|
|
119
|
+
dashboardBindAddress !== current.dashboardBindAddress;
|
|
120
|
+
|
|
121
|
+
return res.status(400).json({
|
|
122
|
+
error: changingBind
|
|
123
|
+
? 'This dashboard has no password, so it can only listen on this machine. Set a password first, then change where it listens.'
|
|
124
|
+
: `Running without a password requires the dashboard to listen on this machine only (currently ${nextBind}).`,
|
|
125
|
+
reason: changingBind ? 'bind_requires_password' : 'no_auth_requires_loopback',
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Turning auth OFF is only ever done from the machine itself.
|
|
130
|
+
if (dashboardAuth === AUTH_MODES.NONE &&
|
|
131
|
+
current.dashboardAuth !== AUTH_MODES.NONE &&
|
|
132
|
+
!isLoopbackAddress(req.ip ?? req.socket?.remoteAddress)) {
|
|
133
|
+
return res.status(403).json({
|
|
134
|
+
error: 'Removing the password can only be done from this machine.',
|
|
135
|
+
reason: 'no_auth_requires_local_request',
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (nginxDir !== undefined && (typeof nginxDir !== 'string' || nginxDir.trim() === '')) {
|
|
140
|
+
return res.status(400).json({ error: 'Nginx directory must be a non-empty string' });
|
|
141
|
+
}
|
|
142
|
+
if (sslDir !== undefined && (typeof sslDir !== 'string' || sslDir.trim() === '')) {
|
|
143
|
+
return res.status(400).json({ error: 'SSL directory must be a non-empty string' });
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (acmeEmail !== undefined) {
|
|
147
|
+
const emailError = validateEmail(acmeEmail);
|
|
148
|
+
if (emailError) return res.status(400).json({ error: emailError });
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (sslAutoRenew !== undefined && typeof sslAutoRenew !== 'boolean') {
|
|
152
|
+
return res.status(400).json({ error: 'sslAutoRenew must be true or false' });
|
|
153
|
+
}
|
|
154
|
+
if (sslAutoRenewDays !== undefined && normalizeAutoRenewDays(sslAutoRenewDays) === null) {
|
|
155
|
+
return res.status(400).json({
|
|
156
|
+
error: `Auto-renew days must be a whole number from ${MIN_AUTO_RENEW_DAYS} to ${MAX_AUTO_RENEW_DAYS}`,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// An empty value means "back to the default".
|
|
161
|
+
const resetDeployDir = deployDir === null || (typeof deployDir === 'string' && deployDir.trim() === '');
|
|
162
|
+
if (deployDir !== undefined && !resetDeployDir) {
|
|
163
|
+
const dirError = validateDeployDir(deployDir);
|
|
164
|
+
if (dirError) return res.status(400).json({ error: dirError });
|
|
165
|
+
try {
|
|
166
|
+
await fs.mkdir(path.resolve(deployDir.trim()), { recursive: true });
|
|
167
|
+
} catch (err) {
|
|
168
|
+
return res.status(400).json({ error: `That folder cannot be created or written to: ${err.message}` });
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const currentConfig = current;
|
|
173
|
+
const updates = {};
|
|
174
|
+
|
|
175
|
+
if (dashboardPort !== undefined) updates.dashboardPort = parseInt(dashboardPort, 10);
|
|
176
|
+
if (dashboardPassword !== undefined) {
|
|
177
|
+
updates.dashboardPasswordHash = await hashPassword(dashboardPassword);
|
|
178
|
+
// Drop any legacy plain-text copy at the same time.
|
|
179
|
+
updates.dashboardPassword = '';
|
|
180
|
+
}
|
|
181
|
+
if (dashboardBindAddress !== undefined) updates.dashboardBindAddress = dashboardBindAddress;
|
|
182
|
+
if (dashboardAuth !== undefined) updates.dashboardAuth = dashboardAuth;
|
|
183
|
+
if (dashboardUi !== undefined) updates.dashboardUi = dashboardUi;
|
|
184
|
+
|
|
185
|
+
// Setting a password implies wanting one; switching to 'password'
|
|
186
|
+
// without one leaves the first-run screen as the way back in, which
|
|
187
|
+
// is recoverable but not what anyone means by saving a password.
|
|
188
|
+
if (dashboardPassword !== undefined && dashboardAuth === undefined) {
|
|
189
|
+
updates.dashboardAuth = AUTH_MODES.PASSWORD;
|
|
190
|
+
}
|
|
191
|
+
if (nginxDir !== undefined) updates.nginxDir = nginxDir.trim();
|
|
192
|
+
if (sslDir !== undefined) updates.sslDir = sslDir.trim();
|
|
193
|
+
if (acmeEmail !== undefined) updates.acmeEmail = acmeEmail.trim();
|
|
194
|
+
if (deployDir !== undefined) updates.deployDir = resetDeployDir ? null : path.resolve(deployDir.trim());
|
|
195
|
+
if (sslAutoRenew !== undefined) updates.sslAutoRenew = sslAutoRenew;
|
|
196
|
+
if (sslAutoRenewDays !== undefined) updates.sslAutoRenewDays = normalizeAutoRenewDays(sslAutoRenewDays);
|
|
197
|
+
|
|
198
|
+
saveConfig({ ...currentConfig, ...updates });
|
|
199
|
+
|
|
200
|
+
// The port, the bind address and the UI choice only take effect on
|
|
201
|
+
// restart, so say so rather than letting the user think the change is
|
|
202
|
+
// already live.
|
|
203
|
+
const needsRestart = updates.dashboardPort !== undefined ||
|
|
204
|
+
updates.dashboardBindAddress !== undefined ||
|
|
205
|
+
updates.dashboardUi !== undefined;
|
|
206
|
+
|
|
207
|
+
res.json({
|
|
208
|
+
success: true,
|
|
209
|
+
needsRestart,
|
|
210
|
+
dashboardAuth: effectiveAuthMode({ ...currentConfig, ...updates }),
|
|
211
|
+
dashboardUi: updates.dashboardUi ?? currentConfig.dashboardUi,
|
|
212
|
+
});
|
|
213
|
+
} catch (err) {
|
|
214
|
+
res.status(500).json({ error: 'Internal server error' });
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async getPermissions(req, res) {
|
|
219
|
+
if (process.platform === 'win32') return res.json({ configured: true });
|
|
220
|
+
try {
|
|
221
|
+
const configured = await checkPermissionsConfigured();
|
|
222
|
+
res.json({ configured });
|
|
223
|
+
} catch {
|
|
224
|
+
res.json({ configured: false });
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async setupPermissions(req, res) {
|
|
229
|
+
if (process.platform === 'win32') {
|
|
230
|
+
return res.status(400).json({ error: 'Not applicable on Windows' });
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const { password } = req.body ?? {};
|
|
234
|
+
if (!password || typeof password !== 'string') {
|
|
235
|
+
return res.status(400).json({ error: 'Password is required' });
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const whichResult = await run('which nginx');
|
|
239
|
+
if (!whichResult.success || !whichResult.stdout.trim()) {
|
|
240
|
+
return res.status(500).json({ error: 'nginx not found. Is nginx installed?' });
|
|
241
|
+
}
|
|
242
|
+
const nginxPath = whichResult.stdout.trim().split('\n')[0].trim();
|
|
243
|
+
const user = os.userInfo().username;
|
|
244
|
+
|
|
245
|
+
const sudoRules = [
|
|
246
|
+
'/usr/bin/systemctl start nginx',
|
|
247
|
+
'/usr/bin/systemctl stop nginx',
|
|
248
|
+
'/usr/bin/systemctl reload nginx',
|
|
249
|
+
'/usr/bin/systemctl restart nginx',
|
|
250
|
+
'/usr/bin/systemctl',
|
|
251
|
+
nginxPath,
|
|
252
|
+
`${nginxPath} -t`,
|
|
253
|
+
`${nginxPath} -s reload`,
|
|
254
|
+
`${nginxPath} -s stop`,
|
|
255
|
+
`${nginxPath} -s quit`,
|
|
256
|
+
'/usr/bin/certbot',
|
|
257
|
+
'/usr/bin/mkdir',
|
|
258
|
+
'/usr/bin/cp',
|
|
259
|
+
'/usr/bin/chmod',
|
|
260
|
+
'/usr/bin/chown',
|
|
261
|
+
'/usr/bin/tee',
|
|
262
|
+
].join(', ');
|
|
263
|
+
|
|
264
|
+
const sudoersContent = `${user} ALL=(ALL) NOPASSWD: ${sudoRules}\n`;
|
|
265
|
+
|
|
266
|
+
const tmpFile = path.join('/tmp', `easy-devops-sudoers-${Date.now()}`);
|
|
267
|
+
try {
|
|
268
|
+
await fs.writeFile(tmpFile, sudoersContent, { mode: 0o600 });
|
|
269
|
+
} catch (err) {
|
|
270
|
+
return res.status(500).json({ error: 'Failed to write temp file', output: err.message });
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const setupCmd = [
|
|
274
|
+
`mkdir -p /etc/easy-devops /var/log/easy-devops`,
|
|
275
|
+
`chown ${user}:${user} /etc/easy-devops /var/log/easy-devops`,
|
|
276
|
+
`chown -R ${user}:${user} /etc/nginx/conf.d 2>/dev/null || true`,
|
|
277
|
+
`cp '${tmpFile}' /etc/sudoers.d/easy-devops`,
|
|
278
|
+
`chmod 440 /etc/sudoers.d/easy-devops`,
|
|
279
|
+
].join(' && ');
|
|
280
|
+
|
|
281
|
+
const result = await runWithStdin('sudo', ['-S', 'bash', '-c', setupCmd], password + '\n');
|
|
282
|
+
await fs.unlink(tmpFile).catch(() => {});
|
|
283
|
+
|
|
284
|
+
if (!result.success) {
|
|
285
|
+
const output = result.stderr || result.stdout;
|
|
286
|
+
const wrongPassword = output.includes('incorrect password') || output.includes('Sorry, try again') || output.includes('3 incorrect');
|
|
287
|
+
return res.status(wrongPassword ? 401 : 500).json({
|
|
288
|
+
error: wrongPassword ? 'Incorrect password' : 'Setup failed',
|
|
289
|
+
output,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
res.json({ success: true });
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
backup(req, res) {
|
|
297
|
+
try {
|
|
298
|
+
const config = loadConfig();
|
|
299
|
+
const domains = dbGet('domains') || [];
|
|
300
|
+
// Strip BOTH the legacy plain-text password and the hash: a backup is
|
|
301
|
+
// a file the user downloads and may share, and a scrypt hash is still
|
|
302
|
+
// a credential worth cracking offline.
|
|
303
|
+
const {
|
|
304
|
+
dashboardPassword: _pw,
|
|
305
|
+
dashboardPasswordHash: _pwHash,
|
|
306
|
+
...safeConfig
|
|
307
|
+
} = config;
|
|
308
|
+
const backup = {
|
|
309
|
+
version: 1,
|
|
310
|
+
exportedAt: new Date().toISOString(),
|
|
311
|
+
config: safeConfig,
|
|
312
|
+
domains,
|
|
313
|
+
};
|
|
314
|
+
res.setHeader('Content-Disposition', `attachment; filename="easy-devops-backup-${Date.now()}.json"`);
|
|
315
|
+
res.setHeader('Content-Type', 'application/json');
|
|
316
|
+
res.json(backup);
|
|
317
|
+
} catch (err) {
|
|
318
|
+
res.status(500).json({ error: 'Export failed' });
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
restore(req, res) {
|
|
323
|
+
try {
|
|
324
|
+
const { config, domains } = req.body ?? {};
|
|
325
|
+
if (!config || typeof config !== 'object') {
|
|
326
|
+
return res.status(400).json({ error: 'Invalid backup: missing config' });
|
|
327
|
+
}
|
|
328
|
+
const current = loadConfig();
|
|
329
|
+
// A backup may predate hashed passwords and the bind setting. Never let
|
|
330
|
+
// one restore a plain-text password or silently re-open the listener.
|
|
331
|
+
const merged = {
|
|
332
|
+
...current,
|
|
333
|
+
...config,
|
|
334
|
+
dashboardPassword: current.dashboardPassword,
|
|
335
|
+
dashboardPasswordHash: current.dashboardPasswordHash,
|
|
336
|
+
dashboardBindAddress: current.dashboardBindAddress,
|
|
337
|
+
dashboardAuth: current.dashboardAuth,
|
|
338
|
+
};
|
|
339
|
+
saveConfig(merged);
|
|
340
|
+
// Through saveDomains(), not dbSet: restoring a backup replaces the
|
|
341
|
+
// whole domain set, which the Hub must learn about like any other
|
|
342
|
+
// domain write. A bare dbSet skipped the revision bump and left the
|
|
343
|
+
// Hub showing the pre-restore list until the next restart.
|
|
344
|
+
if (Array.isArray(domains)) {
|
|
345
|
+
saveDomains(domains);
|
|
346
|
+
}
|
|
347
|
+
res.json({ success: true });
|
|
348
|
+
} catch (err) {
|
|
349
|
+
res.status(500).json({ error: 'Restore failed' });
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// ── Notification Channel CRUD ─────────────────────────────────────────────
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* GET /api/settings/channels
|
|
357
|
+
* Returns the full channel list.
|
|
358
|
+
*/
|
|
359
|
+
getChannels(req, res) {
|
|
360
|
+
try {
|
|
361
|
+
res.json(loadChannels());
|
|
362
|
+
} catch (err) {
|
|
363
|
+
res.status(500).json({ error: 'Internal server error' });
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* POST /api/settings/channels
|
|
369
|
+
* Creates a new named channel.
|
|
370
|
+
*/
|
|
371
|
+
createChannel(req, res) {
|
|
372
|
+
try {
|
|
373
|
+
const { name, type, webhookUrl, botToken, chatId, message } = req.body ?? {};
|
|
374
|
+
|
|
375
|
+
if (!name || typeof name !== 'string' || !name.trim()) {
|
|
376
|
+
return res.status(400).json({ error: 'Channel name is required' });
|
|
377
|
+
}
|
|
378
|
+
if (!['discord', 'telegram'].includes(type)) {
|
|
379
|
+
return res.status(400).json({ error: 'Channel type must be discord or telegram' });
|
|
380
|
+
}
|
|
381
|
+
if (type === 'discord' && (!webhookUrl || !webhookUrl.trim())) {
|
|
382
|
+
return res.status(400).json({ error: 'Discord channels require a webhookUrl' });
|
|
383
|
+
}
|
|
384
|
+
if (type === 'telegram') {
|
|
385
|
+
if (!botToken || !botToken.trim()) {
|
|
386
|
+
return res.status(400).json({ error: 'Telegram channels require a botToken' });
|
|
387
|
+
}
|
|
388
|
+
if (!chatId || !chatId.trim()) {
|
|
389
|
+
return res.status(400).json({ error: 'Telegram channels require a chatId' });
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const channel = {
|
|
394
|
+
id: crypto.randomUUID(),
|
|
395
|
+
name: name.trim(),
|
|
396
|
+
type,
|
|
397
|
+
...(type === 'discord' ? { webhookUrl: webhookUrl.trim() } : {}),
|
|
398
|
+
...(type === 'telegram' ? { botToken: botToken.trim(), chatId: chatId.trim() } : {}),
|
|
399
|
+
...(message && message.trim() ? { message: message.trim() } : {}),
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
const channels = loadChannels();
|
|
403
|
+
channels.push(channel);
|
|
404
|
+
saveChannels(channels);
|
|
405
|
+
|
|
406
|
+
res.status(201).json(channel);
|
|
407
|
+
} catch (err) {
|
|
408
|
+
res.status(500).json({ error: 'Internal server error' });
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* PUT /api/settings/channels/:id
|
|
414
|
+
* Updates name and/or credentials for an existing channel.
|
|
415
|
+
* Type is immutable after creation.
|
|
416
|
+
*/
|
|
417
|
+
updateChannel(req, res) {
|
|
418
|
+
try {
|
|
419
|
+
const { id } = req.params;
|
|
420
|
+
const channels = loadChannels();
|
|
421
|
+
const idx = channels.findIndex((ch) => ch.id === id);
|
|
422
|
+
if (idx === -1) return res.status(404).json({ error: 'Channel not found' });
|
|
423
|
+
|
|
424
|
+
const existing = channels[idx];
|
|
425
|
+
const { name, webhookUrl, botToken, chatId, message } = req.body ?? {};
|
|
426
|
+
|
|
427
|
+
const updated = { ...existing };
|
|
428
|
+
if (name !== undefined) {
|
|
429
|
+
if (!name.trim()) return res.status(400).json({ error: 'Channel name cannot be empty' });
|
|
430
|
+
updated.name = name.trim();
|
|
431
|
+
}
|
|
432
|
+
if (existing.type === 'discord' && webhookUrl !== undefined) {
|
|
433
|
+
updated.webhookUrl = webhookUrl.trim();
|
|
434
|
+
}
|
|
435
|
+
if (existing.type === 'telegram') {
|
|
436
|
+
if (botToken !== undefined) updated.botToken = botToken.trim();
|
|
437
|
+
if (chatId !== undefined) updated.chatId = chatId.trim();
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// Handle message field - allow clearing by setting to empty string
|
|
441
|
+
if (message !== undefined) {
|
|
442
|
+
if (message && message.trim()) {
|
|
443
|
+
updated.message = message.trim();
|
|
444
|
+
} else {
|
|
445
|
+
delete updated.message;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
channels[idx] = updated;
|
|
450
|
+
saveChannels(channels);
|
|
451
|
+
res.json(updated);
|
|
452
|
+
} catch (err) {
|
|
453
|
+
res.status(500).json({ error: 'Internal server error' });
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* DELETE /api/settings/channels/:id
|
|
459
|
+
*/
|
|
460
|
+
deleteChannel(req, res) {
|
|
461
|
+
try {
|
|
462
|
+
const { id } = req.params;
|
|
463
|
+
const channels = loadChannels();
|
|
464
|
+
const idx = channels.findIndex((ch) => ch.id === id);
|
|
465
|
+
if (idx === -1) return res.status(404).json({ error: 'Channel not found' });
|
|
466
|
+
|
|
467
|
+
channels.splice(idx, 1);
|
|
468
|
+
saveChannels(channels);
|
|
469
|
+
res.json({ deleted: id });
|
|
470
|
+
} catch (err) {
|
|
471
|
+
res.status(500).json({ error: 'Internal server error' });
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* POST /api/settings/channels/:id/test
|
|
477
|
+
* Sends a test notification to a single channel.
|
|
478
|
+
*/
|
|
479
|
+
async testChannel(req, res) {
|
|
480
|
+
try {
|
|
481
|
+
const { id } = req.params;
|
|
482
|
+
const channels = loadChannels();
|
|
483
|
+
if (!channels.find((ch) => ch.id === id)) {
|
|
484
|
+
return res.status(404).json({ error: 'Channel not found' });
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
await sendNotification(
|
|
488
|
+
{
|
|
489
|
+
type: 'test',
|
|
490
|
+
message: 'Test notification from Easy DevOps. This channel is working correctly.',
|
|
491
|
+
timestamp: new Date().toISOString(),
|
|
492
|
+
},
|
|
493
|
+
[id]
|
|
494
|
+
);
|
|
495
|
+
|
|
496
|
+
res.json({ success: true });
|
|
497
|
+
} catch (err) {
|
|
498
|
+
res.status(500).json({ error: err.message || 'Failed to send test notification' });
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
export default SettingsController;
|