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
|
@@ -1,237 +1,21 @@
|
|
|
1
1
|
import express from 'express';
|
|
2
|
-
import
|
|
3
|
-
import fs from 'fs/promises';
|
|
4
|
-
import path from 'path';
|
|
5
|
-
import os from 'os';
|
|
6
|
-
import { loadConfig, saveConfig } from '../../core/config.js';
|
|
7
|
-
import { validatePort, validateEmail } from '../../core/validators.js';
|
|
8
|
-
import { run } from '../../core/shell.js';
|
|
9
|
-
import { checkPermissionsConfigured } from '../../core/permissions.js';
|
|
10
|
-
import { dbGet, dbSet } from '../../core/db.js';
|
|
11
|
-
|
|
12
|
-
// Run a bash command with sudo -S (password read from stdin — no terminal needed)
|
|
13
|
-
function runSudoS(bashCmd, password) {
|
|
14
|
-
return new Promise((resolve) => {
|
|
15
|
-
const child = spawn('sudo', ['-S', 'bash', '-c', bashCmd], {
|
|
16
|
-
stdio: ['pipe', 'pipe', 'pipe'],
|
|
17
|
-
});
|
|
18
|
-
let stdout = '', stderr = '';
|
|
19
|
-
child.stdout.on('data', d => { stdout += d; });
|
|
20
|
-
child.stderr.on('data', d => { stderr += d; });
|
|
21
|
-
child.stdin.write(password + '\n');
|
|
22
|
-
child.stdin.end();
|
|
23
|
-
child.on('close', code => resolve({ success: code === 0, stdout: stdout.trim(), stderr: stderr.trim() }));
|
|
24
|
-
child.on('error', err => resolve({ success: false, stdout: '', stderr: err.message }));
|
|
25
|
-
});
|
|
26
|
-
}
|
|
2
|
+
import SettingsController from '../controllers/settingsController.js';
|
|
27
3
|
|
|
28
4
|
const router = express.Router();
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
router.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
} catch (err) {
|
|
45
|
-
res.status(500).json({ error: 'Internal server error' });
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
// ─── POST /api/settings ────────────────────────────────────────────────────
|
|
50
|
-
|
|
51
|
-
router.post('/settings', (req, res) => {
|
|
52
|
-
try {
|
|
53
|
-
const { dashboardPort, dashboardPassword, nginxDir, sslDir, acmeEmail } = req.body;
|
|
54
|
-
|
|
55
|
-
// Validate port if provided
|
|
56
|
-
if (dashboardPort !== undefined) {
|
|
57
|
-
const portError = validatePort(parseInt(dashboardPort, 10));
|
|
58
|
-
if (portError) return res.status(400).json({ error: portError });
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Validate password if provided
|
|
62
|
-
if (dashboardPassword !== undefined && typeof dashboardPassword !== 'string') {
|
|
63
|
-
return res.status(400).json({ error: 'Password must be a string' });
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// Validate directories if provided
|
|
67
|
-
if (nginxDir !== undefined && (typeof nginxDir !== 'string' || nginxDir.trim() === '')) {
|
|
68
|
-
return res.status(400).json({ error: 'Nginx directory must be a non-empty string' });
|
|
69
|
-
}
|
|
70
|
-
if (sslDir !== undefined && (typeof sslDir !== 'string' || sslDir.trim() === '')) {
|
|
71
|
-
return res.status(400).json({ error: 'SSL directory must be a non-empty string' });
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// Validate email if provided
|
|
75
|
-
if (acmeEmail !== undefined) {
|
|
76
|
-
const emailError = validateEmail(acmeEmail);
|
|
77
|
-
if (emailError) return res.status(400).json({ error: emailError });
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Load current config and merge updates
|
|
81
|
-
const currentConfig = loadConfig();
|
|
82
|
-
const updates = {};
|
|
83
|
-
|
|
84
|
-
if (dashboardPort !== undefined) {
|
|
85
|
-
updates.dashboardPort = parseInt(dashboardPort, 10);
|
|
86
|
-
}
|
|
87
|
-
if (dashboardPassword !== undefined) {
|
|
88
|
-
updates.dashboardPassword = dashboardPassword;
|
|
89
|
-
}
|
|
90
|
-
if (nginxDir !== undefined) {
|
|
91
|
-
updates.nginxDir = nginxDir.trim();
|
|
92
|
-
}
|
|
93
|
-
if (sslDir !== undefined) {
|
|
94
|
-
updates.sslDir = sslDir.trim();
|
|
95
|
-
}
|
|
96
|
-
if (acmeEmail !== undefined) {
|
|
97
|
-
updates.acmeEmail = acmeEmail.trim();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const newConfig = { ...currentConfig, ...updates };
|
|
101
|
-
saveConfig(newConfig);
|
|
102
|
-
|
|
103
|
-
res.json({ success: true });
|
|
104
|
-
} catch (err) {
|
|
105
|
-
res.status(500).json({ error: 'Internal server error' });
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
// ─── GET /api/settings/permissions ────────────────────────────────────────────
|
|
110
|
-
|
|
111
|
-
router.get('/settings/permissions', async (req, res) => {
|
|
112
|
-
if (process.platform === 'win32') return res.json({ configured: true });
|
|
113
|
-
try {
|
|
114
|
-
const configured = await checkPermissionsConfigured();
|
|
115
|
-
res.json({ configured });
|
|
116
|
-
} catch {
|
|
117
|
-
res.json({ configured: false });
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
// ─── POST /api/settings/permissions/setup ─────────────────────────────────────
|
|
122
|
-
|
|
123
|
-
router.post('/settings/permissions/setup', async (req, res) => {
|
|
124
|
-
if (process.platform === 'win32') {
|
|
125
|
-
return res.status(400).json({ error: 'Not applicable on Windows' });
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const { password } = req.body ?? {};
|
|
129
|
-
if (!password || typeof password !== 'string') {
|
|
130
|
-
return res.status(400).json({ error: 'Password is required' });
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Resolve nginx binary path
|
|
134
|
-
const whichResult = await run('which nginx');
|
|
135
|
-
if (!whichResult.success || !whichResult.stdout.trim()) {
|
|
136
|
-
return res.status(500).json({ error: 'nginx not found. Is nginx installed?' });
|
|
137
|
-
}
|
|
138
|
-
const nginxPath = whichResult.stdout.trim().split('\n')[0].trim();
|
|
139
|
-
const user = os.userInfo().username;
|
|
140
|
-
|
|
141
|
-
// Build sudoers rules with the detected nginx path
|
|
142
|
-
const sudoRules = [
|
|
143
|
-
'/usr/bin/systemctl start nginx',
|
|
144
|
-
'/usr/bin/systemctl stop nginx',
|
|
145
|
-
'/usr/bin/systemctl reload nginx',
|
|
146
|
-
'/usr/bin/systemctl restart nginx',
|
|
147
|
-
'/usr/bin/systemctl',
|
|
148
|
-
nginxPath,
|
|
149
|
-
`${nginxPath} -t`,
|
|
150
|
-
`${nginxPath} -s reload`,
|
|
151
|
-
`${nginxPath} -s stop`,
|
|
152
|
-
`${nginxPath} -s quit`,
|
|
153
|
-
'/usr/bin/certbot',
|
|
154
|
-
'/usr/bin/mkdir',
|
|
155
|
-
'/usr/bin/cp',
|
|
156
|
-
'/usr/bin/chmod',
|
|
157
|
-
'/usr/bin/chown',
|
|
158
|
-
'/usr/bin/tee',
|
|
159
|
-
].join(', ');
|
|
160
|
-
|
|
161
|
-
const sudoersContent = `${user} ALL=(ALL) NOPASSWD: ${sudoRules}\n`;
|
|
162
|
-
|
|
163
|
-
// Write content to a temp file (no sudo needed for /tmp)
|
|
164
|
-
const tmpFile = path.join('/tmp', `easy-devops-sudoers-${Date.now()}`);
|
|
165
|
-
try {
|
|
166
|
-
await fs.writeFile(tmpFile, sudoersContent, { mode: 0o600 });
|
|
167
|
-
} catch (err) {
|
|
168
|
-
return res.status(500).json({ error: 'Failed to write temp file', output: err.message });
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// Run all setup steps in a single sudo -S invocation
|
|
172
|
-
const setupCmd = [
|
|
173
|
-
`mkdir -p /etc/easy-devops /var/log/easy-devops`,
|
|
174
|
-
`chown ${user}:${user} /etc/easy-devops /var/log/easy-devops`,
|
|
175
|
-
`chown -R ${user}:${user} /etc/nginx/conf.d 2>/dev/null || true`,
|
|
176
|
-
`cp '${tmpFile}' /etc/sudoers.d/easy-devops`,
|
|
177
|
-
`chmod 440 /etc/sudoers.d/easy-devops`,
|
|
178
|
-
].join(' && ');
|
|
179
|
-
|
|
180
|
-
const result = await runSudoS(setupCmd, password);
|
|
181
|
-
await fs.unlink(tmpFile).catch(() => {});
|
|
182
|
-
|
|
183
|
-
if (!result.success) {
|
|
184
|
-
const output = result.stderr || result.stdout;
|
|
185
|
-
const wrongPassword = output.includes('incorrect password') || output.includes('Sorry, try again') || output.includes('3 incorrect');
|
|
186
|
-
return res.status(wrongPassword ? 401 : 500).json({
|
|
187
|
-
error: wrongPassword ? 'Incorrect password' : 'Setup failed',
|
|
188
|
-
output,
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
res.json({ success: true });
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
// ─── GET /api/settings/backup ──────────────────────────────────────────────────
|
|
196
|
-
|
|
197
|
-
router.get('/settings/backup', (req, res) => {
|
|
198
|
-
try {
|
|
199
|
-
const config = loadConfig();
|
|
200
|
-
const domains = dbGet('domains') || [];
|
|
201
|
-
const { dashboardPassword: _pw, ...safeConfig } = config; // exclude password from export
|
|
202
|
-
const backup = {
|
|
203
|
-
version: 1,
|
|
204
|
-
exportedAt: new Date().toISOString(),
|
|
205
|
-
config: safeConfig,
|
|
206
|
-
domains,
|
|
207
|
-
};
|
|
208
|
-
res.setHeader('Content-Disposition', `attachment; filename="easy-devops-backup-${Date.now()}.json"`);
|
|
209
|
-
res.setHeader('Content-Type', 'application/json');
|
|
210
|
-
res.json(backup);
|
|
211
|
-
} catch (err) {
|
|
212
|
-
res.status(500).json({ error: 'Export failed' });
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
// ─── POST /api/settings/restore ────────────────────────────────────────────────
|
|
217
|
-
|
|
218
|
-
router.post('/settings/restore', (req, res) => {
|
|
219
|
-
try {
|
|
220
|
-
const { config, domains } = req.body ?? {};
|
|
221
|
-
if (!config || typeof config !== 'object') {
|
|
222
|
-
return res.status(400).json({ error: 'Invalid backup: missing config' });
|
|
223
|
-
}
|
|
224
|
-
// Merge imported config with current (preserve password, merge rest)
|
|
225
|
-
const current = loadConfig();
|
|
226
|
-
const merged = { ...current, ...config, dashboardPassword: current.dashboardPassword };
|
|
227
|
-
saveConfig(merged);
|
|
228
|
-
if (Array.isArray(domains)) {
|
|
229
|
-
dbSet('domains', domains);
|
|
230
|
-
}
|
|
231
|
-
res.json({ success: true });
|
|
232
|
-
} catch (err) {
|
|
233
|
-
res.status(500).json({ error: 'Restore failed' });
|
|
234
|
-
}
|
|
235
|
-
});
|
|
5
|
+
const controller = new SettingsController();
|
|
6
|
+
|
|
7
|
+
router.get('/settings', (req, res, next) => controller.get(req, res, next));
|
|
8
|
+
router.post('/settings', (req, res, next) => controller.update(req, res, next));
|
|
9
|
+
router.get('/settings/permissions', (req, res, next) => controller.getPermissions(req, res, next));
|
|
10
|
+
router.post('/settings/permissions/setup', (req, res, next) => controller.setupPermissions(req, res, next));
|
|
11
|
+
router.get('/settings/backup', (req, res, next) => controller.backup(req, res, next));
|
|
12
|
+
router.post('/settings/restore', (req, res, next) => controller.restore(req, res, next));
|
|
13
|
+
|
|
14
|
+
// Notification channel CRUD
|
|
15
|
+
router.get('/settings/channels', (req, res, next) => controller.getChannels(req, res, next));
|
|
16
|
+
router.post('/settings/channels', (req, res, next) => controller.createChannel(req, res, next));
|
|
17
|
+
router.put('/settings/channels/:id', (req, res, next) => controller.updateChannel(req, res, next));
|
|
18
|
+
router.delete('/settings/channels/:id', (req, res, next) => controller.deleteChannel(req, res, next));
|
|
19
|
+
router.post('/settings/channels/:id/test', (req, res, next) => controller.testChannel(req, res, next));
|
|
236
20
|
|
|
237
21
|
export default router;
|
|
@@ -1,281 +1,14 @@
|
|
|
1
1
|
import express from 'express';
|
|
2
|
-
import
|
|
3
|
-
import { issueCert, renewCert } from '../../cli/managers/ssl-manager.js';
|
|
4
|
-
import { loadConfig } from '../../core/config.js';
|
|
5
|
-
import { promises as dns } from 'dns';
|
|
2
|
+
import SslController from '../controllers/sslController.js';
|
|
6
3
|
|
|
7
4
|
const router = express.Router();
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const now = Date.now();
|
|
17
|
-
for (const [domain, state] of pendingDnsChallenges) {
|
|
18
|
-
if (now - state.createdAt > 10 * 60 * 1000) {
|
|
19
|
-
state.confirmDeferred.reject(new Error('DNS challenge timed out after 10 minutes'));
|
|
20
|
-
pendingDnsChallenges.delete(domain);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}, 60000);
|
|
24
|
-
|
|
25
|
-
// ─── POST /api/ssl/create ─────────────────────────────────────────────────────
|
|
26
|
-
|
|
27
|
-
router.post('/create', async (req, res) => {
|
|
28
|
-
const { domain, www = false, validationMethod = 'http', email = null } = req.body ?? {};
|
|
29
|
-
|
|
30
|
-
if (!domain || typeof domain !== 'string' || !domain.trim()) {
|
|
31
|
-
return res.status(400).json({ error: 'domain is required' });
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const domainKey = domain.trim();
|
|
35
|
-
const config = loadConfig();
|
|
36
|
-
const acmeEmail = email || config.acmeEmail;
|
|
37
|
-
|
|
38
|
-
if (validationMethod === 'dns') {
|
|
39
|
-
// Two-phase flow: spawn ACME process, wait for TXT record, respond 202, pause until confirm.
|
|
40
|
-
let confirmResolve, confirmReject;
|
|
41
|
-
const confirmPromise = new Promise((resolve, reject) => {
|
|
42
|
-
confirmResolve = resolve;
|
|
43
|
-
confirmReject = reject;
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
let resultResolve;
|
|
47
|
-
const resultPromise = new Promise((resolve) => { resultResolve = resolve; });
|
|
48
|
-
|
|
49
|
-
// Track whether a response has already been sent (202 from onDnsChallenge)
|
|
50
|
-
let responseSent = false;
|
|
51
|
-
|
|
52
|
-
const onDnsChallenge = async (txtName, txtValue) => {
|
|
53
|
-
responseSent = true;
|
|
54
|
-
pendingDnsChallenges.set(domainKey, {
|
|
55
|
-
domain: domainKey,
|
|
56
|
-
txtName,
|
|
57
|
-
txtValue,
|
|
58
|
-
confirmDeferred: { resolve: confirmResolve, reject: confirmReject },
|
|
59
|
-
resultPromise,
|
|
60
|
-
createdAt: Date.now(),
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
res.status(202).json({
|
|
64
|
-
status: 'waiting_dns',
|
|
65
|
-
domain: domainKey,
|
|
66
|
-
txtName,
|
|
67
|
-
txtValue,
|
|
68
|
-
hint: 'Add a DNS TXT record with the name and value above, then call POST /api/ssl/create-confirm',
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
// Pause until user calls /create-confirm (resolves) or /create-cancel (rejects)
|
|
72
|
-
await confirmPromise;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
// Run issueCert in the background — it will call onDnsChallenge which sends 202 and pauses.
|
|
76
|
-
issueCert(domainKey, { www: !!www, validationMethod: 'dns', email: acmeEmail, onDnsChallenge })
|
|
77
|
-
.then(result => {
|
|
78
|
-
resultResolve(result);
|
|
79
|
-
pendingDnsChallenges.delete(domainKey);
|
|
80
|
-
if (!responseSent) {
|
|
81
|
-
responseSent = true;
|
|
82
|
-
if (result.success) {
|
|
83
|
-
return res.json({ success: true, certPath: result.certPath, keyPath: result.keyPath });
|
|
84
|
-
}
|
|
85
|
-
const { step } = result.error ?? {};
|
|
86
|
-
if (step === 'email configuration') {
|
|
87
|
-
return res.status(400).json({ error: 'email_required', hint: 'Configure acmeEmail in settings or provide email parameter.' });
|
|
88
|
-
}
|
|
89
|
-
return res.status(500).json({ success: false, error: result.error });
|
|
90
|
-
}
|
|
91
|
-
})
|
|
92
|
-
.catch(err => {
|
|
93
|
-
const errResult = {
|
|
94
|
-
success: false,
|
|
95
|
-
certPath: null,
|
|
96
|
-
keyPath: null,
|
|
97
|
-
error: {
|
|
98
|
-
step: 'certificate issuance',
|
|
99
|
-
cause: err.message,
|
|
100
|
-
consequence: 'Unexpected error during DNS certificate issuance.',
|
|
101
|
-
nginxRunning: true,
|
|
102
|
-
configSaved: false,
|
|
103
|
-
},
|
|
104
|
-
};
|
|
105
|
-
resultResolve(errResult);
|
|
106
|
-
pendingDnsChallenges.delete(domainKey);
|
|
107
|
-
if (!responseSent) {
|
|
108
|
-
responseSent = true;
|
|
109
|
-
return res.status(500).json({ success: false, error: errResult.error });
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
// The response is sent either by onDnsChallenge (202) or by the .then()/.catch() above.
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// HTTP validation path (synchronous — responds when complete)
|
|
118
|
-
const result = await issueCert(domainKey, { www: !!www, validationMethod: 'http', email: acmeEmail });
|
|
119
|
-
|
|
120
|
-
if (result.success) {
|
|
121
|
-
return res.json({ success: true, certPath: result.certPath, keyPath: result.keyPath });
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const { step } = result.error;
|
|
125
|
-
if (step === 'email configuration') {
|
|
126
|
-
return res.status(400).json({
|
|
127
|
-
error: 'email_required',
|
|
128
|
-
hint: 'Configure acmeEmail in settings or provide email parameter.',
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
if (step === 'port 80 check') {
|
|
132
|
-
return res.status(409).json({
|
|
133
|
-
error: 'port_busy',
|
|
134
|
-
detail: result.error.cause,
|
|
135
|
-
hint: 'Stop the process using port 80 and try again.',
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
return res.status(500).json({ success: false, error: result.error });
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
// ─── POST /api/ssl/create-confirm ────────────────────────────────────────────
|
|
142
|
-
|
|
143
|
-
router.post('/create-confirm', async (req, res) => {
|
|
144
|
-
const { domain } = req.body ?? {};
|
|
145
|
-
const domainKey = domain?.trim();
|
|
146
|
-
const state = domainKey ? pendingDnsChallenges.get(domainKey) : null;
|
|
147
|
-
|
|
148
|
-
if (!state) {
|
|
149
|
-
return res.status(404).json({
|
|
150
|
-
error: 'no_pending_challenge',
|
|
151
|
-
hint: 'No DNS challenge is pending for this domain. Start a new certificate issuance.',
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
try {
|
|
156
|
-
const records = await dns.resolveTxt(`_acme-challenge.${domainKey}`);
|
|
157
|
-
const flatRecords = records.flat();
|
|
158
|
-
|
|
159
|
-
const found = flatRecords.includes(state.txtValue);
|
|
160
|
-
if (!found) {
|
|
161
|
-
return res.status(400).json({
|
|
162
|
-
error: 'dns_not_propagated',
|
|
163
|
-
hint: 'The expected TXT record value was not found. Make sure you added the correct TXT record and wait a few minutes for DNS propagation.',
|
|
164
|
-
expected: state.txtValue,
|
|
165
|
-
found: flatRecords,
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
} catch (err) {
|
|
169
|
-
return res.status(400).json({
|
|
170
|
-
error: 'dns_lookup_failed',
|
|
171
|
-
hint: 'Failed to lookup the expected TXT record. Make sure you added the correct TXT record.',
|
|
172
|
-
detail: err.message,
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
// Signal issueCert() to continue with the ACME process
|
|
176
|
-
await state.confirmDeferred.resolve();
|
|
177
|
-
|
|
178
|
-
// Wait for the ACME process to complete
|
|
179
|
-
const result = await state.resultPromise;
|
|
180
|
-
pendingDnsChallenges.delete(domainKey);
|
|
181
|
-
|
|
182
|
-
if (result.success) {
|
|
183
|
-
return res.json({ success: true, certPath: result.certPath, keyPath: result.keyPath });
|
|
184
|
-
}
|
|
185
|
-
return res.status(500).json({ success: false, error: result.error });
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
// ─── POST /api/ssl/create-cancel ─────────────────────────────────────────────
|
|
189
|
-
|
|
190
|
-
router.post('/create-cancel', async (req, res) => {
|
|
191
|
-
const { domain } = req.body ?? {};
|
|
192
|
-
const domainKey = domain?.trim();
|
|
193
|
-
const state = domainKey ? pendingDnsChallenges.get(domainKey) : null;
|
|
194
|
-
|
|
195
|
-
if (!state) {
|
|
196
|
-
return res.status(404).json({
|
|
197
|
-
error: 'no_pending_challenge',
|
|
198
|
-
hint: 'No DNS challenge is pending for this domain.',
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// Reject the deferred — issueCert() will return a failure result
|
|
203
|
-
state.confirmDeferred.reject(new Error('cancelled by user'));
|
|
204
|
-
pendingDnsChallenges.delete(domainKey);
|
|
205
|
-
|
|
206
|
-
return res.json({ cancelled: true });
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
// ─── GET /api/ssl ─────────────────────────────────────────────────────────────
|
|
210
|
-
|
|
211
|
-
router.get('/', async (req, res) => {
|
|
212
|
-
const certs = await listAllCerts();
|
|
213
|
-
res.json(certs);
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
// ─── POST /api/ssl/renew/:domain ─────────────────────────────────────────────
|
|
217
|
-
|
|
218
|
-
router.post('/renew/:domain', async (req, res) => {
|
|
219
|
-
const domain = req.params.domain;
|
|
220
|
-
const config = loadConfig();
|
|
221
|
-
|
|
222
|
-
if (!config.acmeEmail) {
|
|
223
|
-
return res.status(400).json({
|
|
224
|
-
error: 'email_required',
|
|
225
|
-
hint: 'Configure acmeEmail in settings first.',
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const certs = await listAllCerts();
|
|
230
|
-
const found = certs.find(cert => cert.domain === domain);
|
|
231
|
-
if (!found) {
|
|
232
|
-
return res.status(404).json({ error: `Domain '${domain}' not found` });
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
const result = await renewCert(domain, { validationMethod: 'http', email: config.acmeEmail });
|
|
236
|
-
|
|
237
|
-
if (result.success) {
|
|
238
|
-
return res.json({ success: true, certPath: result.certPath, keyPath: result.keyPath });
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
const { step } = result.error;
|
|
242
|
-
if (step === 'port 80 check') {
|
|
243
|
-
return res.status(409).json({
|
|
244
|
-
error: 'port_busy',
|
|
245
|
-
detail: result.error.cause,
|
|
246
|
-
hint: 'Stop the process using port 80 and try again.',
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
return res.status(500).json({ success: false, error: result.error });
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
// ─── POST /api/ssl/renew-all ─────────────────────────────────────────────────
|
|
253
|
-
|
|
254
|
-
router.post('/renew-all', async (req, res) => {
|
|
255
|
-
const config = loadConfig();
|
|
256
|
-
|
|
257
|
-
if (!config.acmeEmail) {
|
|
258
|
-
return res.status(400).json({
|
|
259
|
-
error: 'email_required',
|
|
260
|
-
hint: 'Configure acmeEmail in settings first.',
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
const certs = await listAllCerts();
|
|
265
|
-
const expiring = certs.filter(c => c.daysLeft !== null && c.daysLeft < 30);
|
|
266
|
-
const results = [];
|
|
267
|
-
|
|
268
|
-
for (const cert of expiring) {
|
|
269
|
-
const result = await renewCert(cert.domain, { validationMethod: 'http', email: config.acmeEmail });
|
|
270
|
-
results.push({
|
|
271
|
-
domain: cert.domain,
|
|
272
|
-
success: result.success,
|
|
273
|
-
certPath: result.certPath,
|
|
274
|
-
keyPath: result.keyPath,
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
res.json(results);
|
|
279
|
-
});
|
|
5
|
+
const controller = new SslController();
|
|
6
|
+
|
|
7
|
+
router.post('/create', (req, res, next) => controller.create(req, res, next));
|
|
8
|
+
router.post('/create-confirm', (req, res, next) => controller.createConfirm(req, res, next));
|
|
9
|
+
router.post('/create-cancel', (req, res, next) => controller.createCancel(req, res, next));
|
|
10
|
+
router.get('/', (req, res, next) => controller.list(req, res, next));
|
|
11
|
+
router.post('/renew/:domain', (req, res, next) => controller.renew(req, res, next));
|
|
12
|
+
router.post('/renew-all', (req, res, next) => controller.renewAll(req, res, next));
|
|
280
13
|
|
|
281
14
|
export default router;
|