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.
- package/README.md +97 -53
- package/install.sh +34 -12
- package/package.json +11 -12
- package/{cli → src/cli}/index.js +28 -6
- package/{cli → src/cli}/managers/domain-manager.js +143 -35
- package/{cli → src/cli}/managers/nginx-manager.js +43 -10
- package/src/cli/managers/ssl-manager.js +678 -0
- package/{cli → src/cli}/menus/dashboard.js +1 -1
- package/{cli → src/cli}/menus/settings.js +27 -8
- package/{cli → src/cli}/menus/update.js +1 -1
- package/{core → src/core}/config.js +5 -2
- package/{core → src/core}/db.js +1 -1
- package/{core → src/core}/detector.js +50 -87
- package/{core → src/core}/nginx-conf-generator.js +101 -64
- package/src/core/platform.js +61 -0
- package/{core → src/core}/shell.js +38 -2
- package/src/core/validators.js +96 -0
- package/{dashboard → src/dashboard}/lib/cert-reader.js +5 -5
- package/{dashboard → src/dashboard}/lib/nginx-service.js +47 -77
- package/src/dashboard/public/img/icon.png +0 -0
- package/src/dashboard/public/img/icon_b.png +0 -0
- package/{dashboard → src/dashboard}/public/js/app.js +206 -30
- package/{dashboard → src/dashboard}/routes/domains.js +96 -72
- package/{dashboard → src/dashboard}/routes/settings.js +19 -11
- package/src/dashboard/routes/ssl.js +281 -0
- package/src/dashboard/views/index.ejs +652 -0
- package/src/dashboard/views/partials/domain-form.ejs +356 -0
- package/src/dashboard/views/partials/domains-panel.ejs +101 -0
- package/src/dashboard/views/partials/login.ejs +167 -0
- package/src/dashboard/views/partials/nginx-panel.ejs +153 -0
- package/src/dashboard/views/partials/overview.ejs +142 -0
- package/src/dashboard/views/partials/settings-panel.ejs +68 -0
- package/src/dashboard/views/partials/sidebar.ejs +96 -0
- package/src/dashboard/views/partials/ssl-panel.ejs +176 -0
- package/{lib → src/lib}/installer/nvm-bootstrap.sh +8 -8
- package/{lib → src/lib}/installer/picker.sh +33 -16
- package/cli/managers/ssl-manager.js +0 -963
- package/dashboard/routes/ssl.js +0 -140
- package/dashboard/views/index.ejs +0 -327
- package/dashboard/views/partials/domain-form.ejs +0 -257
- package/dashboard/views/partials/domains-panel.ejs +0 -66
- package/dashboard/views/partials/login.ejs +0 -50
- package/dashboard/views/partials/nginx-panel.ejs +0 -90
- package/dashboard/views/partials/overview.ejs +0 -67
- package/dashboard/views/partials/settings-panel.ejs +0 -37
- package/dashboard/views/partials/sidebar.ejs +0 -45
- package/dashboard/views/partials/ssl-panel.ejs +0 -95
- /package/{cli → src/cli}/managers/node-manager.js +0 -0
- /package/{cli → src/cli}/menus/.gitkeep +0 -0
- /package/{cli → src/cli}/menus/domains.js +0 -0
- /package/{cli → src/cli}/menus/nginx.js +0 -0
- /package/{cli → src/cli}/menus/nodejs.js +0 -0
- /package/{cli → src/cli}/menus/ssl.js +0 -0
- /package/{dashboard → src/dashboard}/lib/.gitkeep +0 -0
- /package/{dashboard → src/dashboard}/lib/domains-db.js +0 -0
- /package/{dashboard → src/dashboard}/lib/nginx-conf-generator.js +0 -0
- /package/{dashboard → src/dashboard}/routes/.gitkeep +0 -0
- /package/{dashboard → src/dashboard}/routes/auth.js +0 -0
- /package/{dashboard → src/dashboard}/routes/nginx.js +0 -0
- /package/{dashboard → src/dashboard}/server.js +0 -0
- /package/{lib → src/lib}/installer/.gitkeep +0 -0
- /package/{lib → src/lib}/installer/detect.sh +0 -0
- /package/{lib → src/lib}/installer/node-versions.sh +0 -0
- /package/{lib → src/lib}/installer/progress.sh +0 -0
- /package/{lib → src/lib}/installer/winget-install.ps1 +0 -0
|
@@ -0,0 +1,678 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cli/managers/ssl-manager.js
|
|
3
|
+
*
|
|
4
|
+
* SSL Manager — view certificate status, issue and renew certificates using acme-client.
|
|
5
|
+
*
|
|
6
|
+
* Exported functions:
|
|
7
|
+
* - showSslManager() — interactive menu for managing SSL certificates
|
|
8
|
+
* - issueCert() — issue new certificate via Let's Encrypt (HTTP-01 or DNS-01)
|
|
9
|
+
* - renewCert() — renew an existing certificate
|
|
10
|
+
*
|
|
11
|
+
* Pure Node.js implementation — no external ACME binaries required.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import chalk from 'chalk';
|
|
15
|
+
import inquirer from 'inquirer';
|
|
16
|
+
import ora from 'ora';
|
|
17
|
+
import fs from 'fs/promises';
|
|
18
|
+
import path from 'path';
|
|
19
|
+
import http from 'http';
|
|
20
|
+
import net from 'net';
|
|
21
|
+
import * as acme from 'acme-client';
|
|
22
|
+
import { run, runLive } from '../../core/shell.js';
|
|
23
|
+
import { loadConfig } from '../../core/config.js';
|
|
24
|
+
import { isWindows } from '../../core/platform.js';
|
|
25
|
+
|
|
26
|
+
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
27
|
+
|
|
28
|
+
function getSslDir() {
|
|
29
|
+
const { sslDir } = loadConfig();
|
|
30
|
+
return sslDir;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Loads or generates the ACME account private key.
|
|
35
|
+
* The key is persisted at {sslDir}/.account/account.key
|
|
36
|
+
*/
|
|
37
|
+
async function getAccountKey(sslDir) {
|
|
38
|
+
const keyPath = path.join(sslDir, '.account', 'account.key');
|
|
39
|
+
await fs.mkdir(path.dirname(keyPath), { recursive: true });
|
|
40
|
+
try {
|
|
41
|
+
return await fs.readFile(keyPath);
|
|
42
|
+
} catch {
|
|
43
|
+
const key = await acme.crypto.createPrivateKey();
|
|
44
|
+
await fs.writeFile(keyPath, key);
|
|
45
|
+
return key;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Checks if port 80 is free (can be bound).
|
|
51
|
+
* Used for HTTP-01 challenge validation.
|
|
52
|
+
*/
|
|
53
|
+
async function isPort80Free() {
|
|
54
|
+
return new Promise((resolve) => {
|
|
55
|
+
const srv = net.createServer();
|
|
56
|
+
srv.listen(80, () => {
|
|
57
|
+
srv.close(() => resolve(true));
|
|
58
|
+
});
|
|
59
|
+
srv.on('error', () => resolve(false));
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Stops nginx to free port 80 for HTTP-01 challenge.
|
|
65
|
+
*/
|
|
66
|
+
async function stopNginx() {
|
|
67
|
+
const cmd = isWindows
|
|
68
|
+
? 'taskkill /f /IM nginx.exe'
|
|
69
|
+
: 'systemctl stop nginx';
|
|
70
|
+
await run(cmd);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Starts nginx after HTTP-01 challenge completes.
|
|
75
|
+
*/
|
|
76
|
+
async function startNginx() {
|
|
77
|
+
const { nginxDir } = loadConfig();
|
|
78
|
+
const cmd = isWindows
|
|
79
|
+
? `& "${nginxDir}\\nginx.exe"`
|
|
80
|
+
: 'systemctl start nginx';
|
|
81
|
+
await run(cmd);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ─── Certificate Listing ──────────────────────────────────────────────────────
|
|
85
|
+
|
|
86
|
+
function getLiveDir() {
|
|
87
|
+
return getSslDir();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function parseCertExpiry(certPath) {
|
|
91
|
+
const result = await run(`openssl x509 -enddate -noout -in "${certPath}"`);
|
|
92
|
+
|
|
93
|
+
if (result.success && result.stdout) {
|
|
94
|
+
const match = result.stdout.match(/notAfter=(.+)/);
|
|
95
|
+
if (match) {
|
|
96
|
+
const expiryDate = new Date(match[1].trim());
|
|
97
|
+
const daysLeft = Math.floor((expiryDate - Date.now()) / 86400000);
|
|
98
|
+
return { expiryDate, daysLeft };
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Fallback: use file mtime + 90 days
|
|
103
|
+
try {
|
|
104
|
+
const stat = await fs.stat(certPath);
|
|
105
|
+
const expiryDate = new Date(stat.mtime.getTime() + 90 * 86400000);
|
|
106
|
+
const daysLeft = Math.floor((expiryDate - Date.now()) / 86400000);
|
|
107
|
+
return { expiryDate, daysLeft, errorReason: 'expiry estimated from file date' };
|
|
108
|
+
} catch {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function listCerts(liveDir) {
|
|
114
|
+
let entries;
|
|
115
|
+
try {
|
|
116
|
+
entries = await fs.readdir(liveDir, { withFileTypes: true });
|
|
117
|
+
} catch {
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const domains = entries.filter(e => e.isDirectory()).map(e => e.name);
|
|
122
|
+
const certs = [];
|
|
123
|
+
|
|
124
|
+
for (const domain of domains) {
|
|
125
|
+
// Skip hidden directories like .account
|
|
126
|
+
if (domain.startsWith('.')) continue;
|
|
127
|
+
|
|
128
|
+
const certPath = path.join(liveDir, domain, 'fullchain.pem');
|
|
129
|
+
|
|
130
|
+
let status = 'error';
|
|
131
|
+
let expiryDate = null;
|
|
132
|
+
let daysLeft = null;
|
|
133
|
+
|
|
134
|
+
try {
|
|
135
|
+
await fs.stat(certPath);
|
|
136
|
+
const expiry = await parseCertExpiry(certPath);
|
|
137
|
+
if (expiry !== null) {
|
|
138
|
+
expiryDate = expiry.expiryDate;
|
|
139
|
+
daysLeft = expiry.daysLeft;
|
|
140
|
+
if (daysLeft > 30) {
|
|
141
|
+
status = 'healthy';
|
|
142
|
+
} else if (daysLeft >= 10) {
|
|
143
|
+
status = 'expiring';
|
|
144
|
+
} else {
|
|
145
|
+
status = 'critical';
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
} catch {
|
|
149
|
+
status = 'error';
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
certs.push({ domain, status, expiryDate, daysLeft });
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return certs;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function renderCertRow(cert) {
|
|
159
|
+
const domainPadded = cert.domain.padEnd(35);
|
|
160
|
+
|
|
161
|
+
if (cert.status === 'error') {
|
|
162
|
+
console.log(` ${chalk.gray('❌')} ${chalk.gray(domainPadded)} ${chalk.gray('ERROR')}`);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const expiryStr = cert.expiryDate
|
|
167
|
+
? cert.expiryDate.toDateString().replace(/^\S+\s/, '')
|
|
168
|
+
: '—';
|
|
169
|
+
const daysStr = cert.daysLeft !== null ? `${cert.daysLeft}d` : '—';
|
|
170
|
+
|
|
171
|
+
if (cert.status === 'healthy') {
|
|
172
|
+
console.log(` ${chalk.green('✅')} ${chalk.green(domainPadded)} ${chalk.green(daysStr.padEnd(6))} ${chalk.green(`(${expiryStr})`)}`);
|
|
173
|
+
} else if (cert.status === 'expiring') {
|
|
174
|
+
console.log(` ${chalk.yellow('⚠️')} ${chalk.yellow(domainPadded)} ${chalk.yellow(daysStr.padEnd(6))} ${chalk.yellow(`(${expiryStr})`)}`);
|
|
175
|
+
} else {
|
|
176
|
+
console.log(` ${chalk.red('❌')} ${chalk.red(domainPadded)} ${chalk.red(daysStr.padEnd(6))} ${chalk.red(`(${expiryStr})`)}`);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// ─── Certificate Issuance ─────────────────────────────────────────────────────
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Issues a new SSL certificate using Let's Encrypt (acme-client).
|
|
184
|
+
*
|
|
185
|
+
* HTTP-01: stops nginx, starts temp HTTP server on port 80, validates, restarts nginx.
|
|
186
|
+
* DNS-01: no nginx interaction; caller handles TXT record via onDnsChallenge callback.
|
|
187
|
+
*
|
|
188
|
+
* @param {string} domainName - The primary domain name
|
|
189
|
+
* @param {{
|
|
190
|
+
* www?: boolean,
|
|
191
|
+
* validationMethod?: 'http' | 'dns',
|
|
192
|
+
* email?: string | null,
|
|
193
|
+
* onDnsChallenge?: (txtName: string, txtValue: string) => Promise<void>
|
|
194
|
+
* }} options
|
|
195
|
+
* @returns {Promise<{ success: boolean, certPath: string|null, keyPath: string|null, error: object|null }>}
|
|
196
|
+
*/
|
|
197
|
+
export async function issueCert(domainName, {
|
|
198
|
+
www = false,
|
|
199
|
+
wildcard = false,
|
|
200
|
+
validationMethod = 'http',
|
|
201
|
+
email = null,
|
|
202
|
+
onDnsChallenge = null,
|
|
203
|
+
} = {}) {
|
|
204
|
+
const sslDir = getSslDir();
|
|
205
|
+
const config = loadConfig();
|
|
206
|
+
|
|
207
|
+
// Email is required for Let's Encrypt
|
|
208
|
+
const acmeEmail = email || config.acmeEmail;
|
|
209
|
+
if (!acmeEmail) {
|
|
210
|
+
return {
|
|
211
|
+
success: false,
|
|
212
|
+
certPath: null,
|
|
213
|
+
keyPath: null,
|
|
214
|
+
error: {
|
|
215
|
+
step: 'email configuration',
|
|
216
|
+
cause: 'No email address configured. Let\'s Encrypt requires an email for account creation.',
|
|
217
|
+
consequence: 'No certificate was issued. Configure acmeEmail in settings or provide email parameter.',
|
|
218
|
+
nginxRunning: true,
|
|
219
|
+
configSaved: false,
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Ensure certificate directory exists
|
|
225
|
+
const certDir = path.join(sslDir, domainName);
|
|
226
|
+
await fs.mkdir(certDir, { recursive: true });
|
|
227
|
+
|
|
228
|
+
// Load or create account key
|
|
229
|
+
const accountKey = await getAccountKey(sslDir);
|
|
230
|
+
|
|
231
|
+
// Create ACME client (Let's Encrypt production)
|
|
232
|
+
const client = new acme.Client({
|
|
233
|
+
directoryUrl: acme.directory.letsencrypt.production,
|
|
234
|
+
accountKey,
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
// Wildcard requires DNS-01 — enforce it here
|
|
238
|
+
if (wildcard && validationMethod === 'http') {
|
|
239
|
+
return {
|
|
240
|
+
success: false,
|
|
241
|
+
certPath: null,
|
|
242
|
+
keyPath: null,
|
|
243
|
+
error: {
|
|
244
|
+
step: 'validation method',
|
|
245
|
+
cause: 'Wildcard certificates require DNS-01 validation, not HTTP-01.',
|
|
246
|
+
consequence: 'No certificate was issued. Change validation method to DNS.',
|
|
247
|
+
nginxRunning: true,
|
|
248
|
+
configSaved: false,
|
|
249
|
+
},
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// altNames: wildcard includes both bare domain and *.domain
|
|
254
|
+
const altNames = wildcard
|
|
255
|
+
? [domainName, `*.${domainName}`]
|
|
256
|
+
: (www ? [domainName, `www.${domainName}`] : [domainName]);
|
|
257
|
+
const [domainKey, csr] = await acme.crypto.createCsr({
|
|
258
|
+
commonName: domainName,
|
|
259
|
+
altNames,
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
// ─── HTTP-01 Challenge ─────────────────────────────────────────────────────
|
|
263
|
+
if (validationMethod === 'http') {
|
|
264
|
+
// Stop nginx to free port 80
|
|
265
|
+
await stopNginx();
|
|
266
|
+
|
|
267
|
+
// Check port 80 is free
|
|
268
|
+
const portFree = await isPort80Free();
|
|
269
|
+
if (!portFree) {
|
|
270
|
+
await startNginx();
|
|
271
|
+
return {
|
|
272
|
+
success: false,
|
|
273
|
+
certPath: null,
|
|
274
|
+
keyPath: null,
|
|
275
|
+
error: {
|
|
276
|
+
step: 'port 80 check',
|
|
277
|
+
cause: 'Port 80 is still in use after stopping nginx. Another process may be binding it.',
|
|
278
|
+
consequence: 'No certificate was issued. nginx has been restarted.',
|
|
279
|
+
nginxRunning: true,
|
|
280
|
+
configSaved: false,
|
|
281
|
+
},
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Create temporary HTTP server for challenge
|
|
286
|
+
const challengeTokens = new Map();
|
|
287
|
+
let httpServer;
|
|
288
|
+
|
|
289
|
+
const challengeCreateFn = async (authz, challenge, keyAuthorization) => {
|
|
290
|
+
challengeTokens.set(challenge.token, keyAuthorization);
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
const challengeRemoveFn = async (authz, challenge, keyAuthorization) => {
|
|
294
|
+
challengeTokens.delete(challenge.token);
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
// Start temp HTTP server on port 80
|
|
298
|
+
httpServer = http.createServer((req, res) => {
|
|
299
|
+
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
300
|
+
if (url.pathname.startsWith('/.well-known/acme-challenge/')) {
|
|
301
|
+
const token = url.pathname.split('/').pop();
|
|
302
|
+
const keyAuth = challengeTokens.get(token);
|
|
303
|
+
if (keyAuth) {
|
|
304
|
+
res.writeHead(200, { 'Content-Type': 'application/octet-stream' });
|
|
305
|
+
res.end(keyAuth);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
res.writeHead(404);
|
|
310
|
+
res.end('Not found');
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
await new Promise((resolve, reject) => {
|
|
314
|
+
httpServer.listen(80, (err) => {
|
|
315
|
+
if (err) reject(err);
|
|
316
|
+
else resolve();
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
try {
|
|
321
|
+
// Execute ACME challenge — returns the signed certificate PEM
|
|
322
|
+
// challengePriority forces http-01 only so acme-client never falls back to dns-01
|
|
323
|
+
const cert = await client.auto({
|
|
324
|
+
csr,
|
|
325
|
+
email: acmeEmail,
|
|
326
|
+
termsOfServiceAgreed: true,
|
|
327
|
+
challengePriority: ['http-01'],
|
|
328
|
+
challengeCreateFn,
|
|
329
|
+
challengeRemoveFn,
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
// Close HTTP server
|
|
333
|
+
await new Promise((resolve) => httpServer.close(resolve));
|
|
334
|
+
|
|
335
|
+
// Restart nginx
|
|
336
|
+
await startNginx();
|
|
337
|
+
|
|
338
|
+
// Write certificate files
|
|
339
|
+
const certPath = path.join(certDir, 'fullchain.pem');
|
|
340
|
+
const keyPath = path.join(certDir, 'privkey.pem');
|
|
341
|
+
|
|
342
|
+
await fs.writeFile(keyPath, domainKey);
|
|
343
|
+
await fs.writeFile(certPath, cert);
|
|
344
|
+
|
|
345
|
+
return { success: true, certPath, keyPath, error: null };
|
|
346
|
+
} catch (err) {
|
|
347
|
+
// Ensure server is closed and nginx restarted on error
|
|
348
|
+
await new Promise((resolve) => httpServer.close(resolve));
|
|
349
|
+
await startNginx();
|
|
350
|
+
return {
|
|
351
|
+
success: false,
|
|
352
|
+
certPath: null,
|
|
353
|
+
keyPath: null,
|
|
354
|
+
error: {
|
|
355
|
+
step: 'ACME validation',
|
|
356
|
+
cause: err.message || 'HTTP-01 challenge failed.',
|
|
357
|
+
consequence: 'No certificate was issued. nginx has been restarted.',
|
|
358
|
+
nginxRunning: true,
|
|
359
|
+
configSaved: false,
|
|
360
|
+
},
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// ─── DNS-01 Challenge ──────────────────────────────────────────────────────
|
|
366
|
+
if (validationMethod === 'dns') {
|
|
367
|
+
const challengeCreateFn = async (authz, challenge, keyAuthorization) => {
|
|
368
|
+
const txtName = `_acme-challenge.${domainName}`;
|
|
369
|
+
const txtValue = keyAuthorization;
|
|
370
|
+
|
|
371
|
+
// Yield TXT record to caller and pause until they confirm DNS is set.
|
|
372
|
+
// onDnsChallenge is async — it returns only after the user confirms
|
|
373
|
+
// (dashboard: /create-confirm resolves the deferred; CLI: inquirer prompt).
|
|
374
|
+
if (onDnsChallenge) {
|
|
375
|
+
await onDnsChallenge(txtName, txtValue);
|
|
376
|
+
}
|
|
377
|
+
// challengeCreateFn returning here signals acme-client to proceed with validation.
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
const challengeRemoveFn = async () => {
|
|
381
|
+
// No-op: TXT record cleanup is the user's responsibility.
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
try {
|
|
385
|
+
// Execute ACME challenge — returns the signed certificate PEM
|
|
386
|
+
// challengePriority forces dns-01 only so acme-client never tries http-01
|
|
387
|
+
|
|
388
|
+
const cert = await client.auto({
|
|
389
|
+
csr,
|
|
390
|
+
email: acmeEmail,
|
|
391
|
+
termsOfServiceAgreed: true,
|
|
392
|
+
challengePriority: ['dns-01'],
|
|
393
|
+
challengeCreateFn,
|
|
394
|
+
challengeRemoveFn,
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
// Write certificate files
|
|
398
|
+
const certPath = path.join(certDir, 'fullchain.pem');
|
|
399
|
+
const keyPath = path.join(certDir, 'privkey.pem');
|
|
400
|
+
|
|
401
|
+
await fs.writeFile(keyPath, domainKey);
|
|
402
|
+
await fs.writeFile(certPath, cert);
|
|
403
|
+
|
|
404
|
+
return { success: true, certPath, keyPath, error: null };
|
|
405
|
+
} catch (err) {
|
|
406
|
+
return {
|
|
407
|
+
success: false,
|
|
408
|
+
certPath: null,
|
|
409
|
+
keyPath: null,
|
|
410
|
+
error: {
|
|
411
|
+
step: 'ACME validation',
|
|
412
|
+
cause: err.message || 'DNS-01 challenge failed.',
|
|
413
|
+
consequence: 'No certificate was issued. nginx was not affected.',
|
|
414
|
+
nginxRunning: true,
|
|
415
|
+
configSaved: false,
|
|
416
|
+
},
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// Invalid validation method
|
|
422
|
+
return {
|
|
423
|
+
success: false,
|
|
424
|
+
certPath: null,
|
|
425
|
+
keyPath: null,
|
|
426
|
+
error: {
|
|
427
|
+
step: 'validation method',
|
|
428
|
+
cause: `Invalid validation method: ${validationMethod}. Must be 'http' or 'dns'.`,
|
|
429
|
+
consequence: 'No certificate was issued.',
|
|
430
|
+
nginxRunning: true,
|
|
431
|
+
configSaved: false,
|
|
432
|
+
},
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Renews a certificate by re-issuing it.
|
|
438
|
+
* acme-client's auto() handles renewal transparently.
|
|
439
|
+
*/
|
|
440
|
+
export async function renewCert(domainName, options = {}) {
|
|
441
|
+
return issueCert(domainName, options);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Renews all certificates expiring within 30 days.
|
|
446
|
+
*/
|
|
447
|
+
export async function renewExpiring(certs) {
|
|
448
|
+
const expiring = certs.filter(c => c.daysLeft !== null && c.daysLeft < 30);
|
|
449
|
+
if (expiring.length === 0) return [];
|
|
450
|
+
|
|
451
|
+
const results = [];
|
|
452
|
+
for (const cert of expiring) {
|
|
453
|
+
const result = await issueCert(cert.domain, { validationMethod: 'http' });
|
|
454
|
+
results.push({
|
|
455
|
+
domain: cert.domain,
|
|
456
|
+
success: result.success,
|
|
457
|
+
exitCode: result.success ? 0 : 1,
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
return results;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// ─── CLI Menu ────────────────────────────────────────────────────────────────
|
|
464
|
+
|
|
465
|
+
export async function showSslManager() {
|
|
466
|
+
while (true) {
|
|
467
|
+
const liveDir = getLiveDir();
|
|
468
|
+
|
|
469
|
+
const spinner = ora('Loading certificates…').start();
|
|
470
|
+
const certs = await listCerts(liveDir);
|
|
471
|
+
spinner.stop();
|
|
472
|
+
|
|
473
|
+
console.log(chalk.bold('\n SSL Manager'));
|
|
474
|
+
console.log(chalk.gray(' ' + '─'.repeat(40)));
|
|
475
|
+
|
|
476
|
+
if (certs.length === 0) {
|
|
477
|
+
console.log(chalk.gray(' No certificates found'));
|
|
478
|
+
} else {
|
|
479
|
+
for (const cert of certs) {
|
|
480
|
+
renderCertRow(cert);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
console.log();
|
|
484
|
+
|
|
485
|
+
const choices = [
|
|
486
|
+
'Create new certificate',
|
|
487
|
+
'Renew a certificate',
|
|
488
|
+
'Renew all expiring (< 30 days)',
|
|
489
|
+
new inquirer.Separator(),
|
|
490
|
+
'← Back',
|
|
491
|
+
];
|
|
492
|
+
|
|
493
|
+
let choice;
|
|
494
|
+
try {
|
|
495
|
+
({ choice } = await inquirer.prompt([{
|
|
496
|
+
type: 'list',
|
|
497
|
+
name: 'choice',
|
|
498
|
+
message: 'Select an option:',
|
|
499
|
+
choices,
|
|
500
|
+
}]));
|
|
501
|
+
} catch (err) {
|
|
502
|
+
if (err.name === 'ExitPromptError') return;
|
|
503
|
+
throw err;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
switch (choice) {
|
|
507
|
+
case 'Create new certificate': {
|
|
508
|
+
const config = loadConfig();
|
|
509
|
+
|
|
510
|
+
// Prompt for email if not configured
|
|
511
|
+
let acmeEmail = config.acmeEmail;
|
|
512
|
+
if (!acmeEmail) {
|
|
513
|
+
let emailInput;
|
|
514
|
+
try {
|
|
515
|
+
({ emailInput } = await inquirer.prompt([{
|
|
516
|
+
type: 'input',
|
|
517
|
+
name: 'emailInput',
|
|
518
|
+
message: 'Email address (required for Let\'s Encrypt):',
|
|
519
|
+
validate: v => v.trim() && v.includes('@') ? true : 'Valid email required',
|
|
520
|
+
}]));
|
|
521
|
+
acmeEmail = emailInput;
|
|
522
|
+
// Save to config
|
|
523
|
+
config.acmeEmail = acmeEmail;
|
|
524
|
+
const { saveConfig } = await import('../../core/config.js');
|
|
525
|
+
saveConfig(config);
|
|
526
|
+
} catch (err) {
|
|
527
|
+
if (err.name === 'ExitPromptError') return;
|
|
528
|
+
throw err;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
let domainInput, wwwInput, method;
|
|
533
|
+
try {
|
|
534
|
+
({ domainInput } = await inquirer.prompt([{
|
|
535
|
+
type: 'input',
|
|
536
|
+
name: 'domainInput',
|
|
537
|
+
message: 'Domain name:',
|
|
538
|
+
validate: v => v.trim() ? true : 'Required',
|
|
539
|
+
}]));
|
|
540
|
+
({ wwwInput } = await inquirer.prompt([{
|
|
541
|
+
type: 'confirm',
|
|
542
|
+
name: 'wwwInput',
|
|
543
|
+
message: 'Include www subdomain?',
|
|
544
|
+
default: false,
|
|
545
|
+
}]));
|
|
546
|
+
({ method } = await inquirer.prompt([{
|
|
547
|
+
type: 'list',
|
|
548
|
+
name: 'method',
|
|
549
|
+
message: 'How should we validate domain ownership?',
|
|
550
|
+
choices: [
|
|
551
|
+
{ name: 'HTTP challenge (domain DNS points to this server)', value: 'http' },
|
|
552
|
+
{ name: 'DNS challenge (I\'ll add a TXT record manually)', value: 'dns' },
|
|
553
|
+
],
|
|
554
|
+
}]));
|
|
555
|
+
} catch (err) {
|
|
556
|
+
if (err.name === 'ExitPromptError') return;
|
|
557
|
+
throw err;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
let result;
|
|
561
|
+
if (method === 'dns') {
|
|
562
|
+
const onDnsChallenge = async (txtName, txtValue) => {
|
|
563
|
+
console.log(chalk.yellow('\n Add this DNS TXT record:'));
|
|
564
|
+
console.log(chalk.white(` Name: ${txtName}`));
|
|
565
|
+
console.log(chalk.white(` Value: ${txtValue}\n`));
|
|
566
|
+
try {
|
|
567
|
+
await inquirer.prompt([{
|
|
568
|
+
type: 'input',
|
|
569
|
+
name: '_',
|
|
570
|
+
message: 'Add the TXT record to your DNS, then press Enter to continue...',
|
|
571
|
+
}]);
|
|
572
|
+
} catch (err) {
|
|
573
|
+
if (err.name !== 'ExitPromptError') throw err;
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
console.log(chalk.cyan(`\n Starting DNS challenge for ${domainInput.trim()}…\n`));
|
|
577
|
+
result = await issueCert(domainInput.trim(), {
|
|
578
|
+
www: wwwInput,
|
|
579
|
+
validationMethod: 'dns',
|
|
580
|
+
email: acmeEmail,
|
|
581
|
+
onDnsChallenge,
|
|
582
|
+
});
|
|
583
|
+
} else {
|
|
584
|
+
const spinner = ora(`Creating certificate for ${domainInput.trim()}…`).start();
|
|
585
|
+
result = await issueCert(domainInput.trim(), {
|
|
586
|
+
www: wwwInput,
|
|
587
|
+
validationMethod: 'http',
|
|
588
|
+
email: acmeEmail,
|
|
589
|
+
});
|
|
590
|
+
spinner.stop();
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
if (result.success) {
|
|
594
|
+
console.log(chalk.green('\n ✓ Certificate created successfully'));
|
|
595
|
+
console.log(chalk.gray(` Cert: ${result.certPath}`));
|
|
596
|
+
console.log(chalk.gray(` Key: ${result.keyPath}\n`));
|
|
597
|
+
} else {
|
|
598
|
+
const e = result.error;
|
|
599
|
+
console.log(chalk.red('\n ✗ Certificate creation failed'));
|
|
600
|
+
console.log(chalk.yellow(` Step: ${e.step}`));
|
|
601
|
+
console.log(chalk.yellow(` Cause: ${e.cause}`));
|
|
602
|
+
console.log(chalk.yellow(` Consequence: ${e.consequence}`));
|
|
603
|
+
console.log(chalk.gray(` nginx running: ${e.nginxRunning ? 'yes' : 'no'}\n`));
|
|
604
|
+
}
|
|
605
|
+
break;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
case 'Renew a certificate': {
|
|
609
|
+
if (certs.length === 0) {
|
|
610
|
+
console.log(chalk.gray('\n No certificates found to renew\n'));
|
|
611
|
+
break;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
let selectedDomain;
|
|
615
|
+
try {
|
|
616
|
+
({ selectedDomain } = await inquirer.prompt([{
|
|
617
|
+
type: 'list',
|
|
618
|
+
name: 'selectedDomain',
|
|
619
|
+
message: 'Select domain to renew:',
|
|
620
|
+
choices: certs.map(c => c.domain),
|
|
621
|
+
}]));
|
|
622
|
+
} catch (err) {
|
|
623
|
+
if (err.name === 'ExitPromptError') return;
|
|
624
|
+
throw err;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
const config = loadConfig();
|
|
628
|
+
const spinner = ora(`Renewing certificate for ${selectedDomain}…`).start();
|
|
629
|
+
const result = await renewCert(selectedDomain, {
|
|
630
|
+
validationMethod: 'http',
|
|
631
|
+
email: config.acmeEmail,
|
|
632
|
+
});
|
|
633
|
+
spinner.stop();
|
|
634
|
+
|
|
635
|
+
if (result.success) {
|
|
636
|
+
console.log(chalk.green('\n ✓ Renewed successfully\n'));
|
|
637
|
+
} else {
|
|
638
|
+
const e = result.error;
|
|
639
|
+
console.log(chalk.red('\n ✗ Renewal failed'));
|
|
640
|
+
console.log(chalk.yellow(` Step: ${e.step}`));
|
|
641
|
+
console.log(chalk.yellow(` Cause: ${e.cause}\n`));
|
|
642
|
+
}
|
|
643
|
+
break;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
case 'Renew all expiring (< 30 days)': {
|
|
647
|
+
const config = loadConfig();
|
|
648
|
+
const results = await renewExpiring(certs, { email: config.acmeEmail });
|
|
649
|
+
if (results.length === 0) {
|
|
650
|
+
console.log(chalk.gray('\n No certificates expiring within 30 days\n'));
|
|
651
|
+
} else {
|
|
652
|
+
console.log();
|
|
653
|
+
for (const r of results) {
|
|
654
|
+
if (r.success) {
|
|
655
|
+
console.log(` ${chalk.green('✓ ' + r.domain)}`);
|
|
656
|
+
} else {
|
|
657
|
+
console.log(` ${chalk.red('✗ ' + r.domain)}`);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
console.log();
|
|
661
|
+
}
|
|
662
|
+
break;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
case '← Back':
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
if (choice !== '← Back') {
|
|
670
|
+
try {
|
|
671
|
+
await inquirer.prompt([{ type: 'input', name: '_', message: 'Press Enter to continue...' }]);
|
|
672
|
+
} catch (err) {
|
|
673
|
+
if (err.name === 'ExitPromptError') return;
|
|
674
|
+
throw err;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
}
|
|
@@ -14,7 +14,7 @@ import { run } from '../../core/shell.js';
|
|
|
14
14
|
|
|
15
15
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
16
16
|
const isWindows = process.platform === 'win32';
|
|
17
|
-
const LOG_PATH = path.resolve(__dirname, '
|
|
17
|
+
const LOG_PATH = path.resolve(__dirname, '../../../data/dashboard.log');
|
|
18
18
|
const SERVER_PATH = path.resolve(__dirname, '../../dashboard/server.js');
|
|
19
19
|
|
|
20
20
|
// ─── Port helpers ─────────────────────────────────────────────────────────────
|