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
|
@@ -1,963 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* cli/managers/ssl-manager.js
|
|
3
|
-
*
|
|
4
|
-
* SSL Manager — view certificate status, renew certificates, install certbot/win-acme.
|
|
5
|
-
*
|
|
6
|
-
* Exported functions:
|
|
7
|
-
* - showSslManager() — interactive menu for managing SSL certificates
|
|
8
|
-
*
|
|
9
|
-
* All shell calls go through core/shell.js (run / runLive).
|
|
10
|
-
* Platform differences (Windows/Linux) are handled via isWindows guards.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import chalk from 'chalk';
|
|
14
|
-
import inquirer from 'inquirer';
|
|
15
|
-
import ora from 'ora';
|
|
16
|
-
import fs from 'fs/promises';
|
|
17
|
-
import path from 'path';
|
|
18
|
-
import { fileURLToPath } from 'url';
|
|
19
|
-
import { run, runLive } from '../../core/shell.js';
|
|
20
|
-
import { loadConfig } from '../../core/config.js';
|
|
21
|
-
|
|
22
|
-
const isWindows = process.platform === 'win32';
|
|
23
|
-
|
|
24
|
-
// ─── getCertbotDir ────────────────────────────────────────────────────────────
|
|
25
|
-
|
|
26
|
-
function getCertbotDir() {
|
|
27
|
-
const config = loadConfig();
|
|
28
|
-
return path.join(config.certbotDir, 'live');
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// ─── parseCertExpiry ──────────────────────────────────────────────────────────
|
|
32
|
-
|
|
33
|
-
async function parseCertExpiry(certPath) {
|
|
34
|
-
const result = await run(`openssl x509 -enddate -noout -in "${certPath}"`);
|
|
35
|
-
|
|
36
|
-
if (result.success && result.stdout) {
|
|
37
|
-
const match = result.stdout.match(/notAfter=(.+)/);
|
|
38
|
-
if (match) {
|
|
39
|
-
const expiryDate = new Date(match[1].trim());
|
|
40
|
-
const daysLeft = Math.floor((expiryDate - Date.now()) / 86400000);
|
|
41
|
-
return { expiryDate, daysLeft };
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Fallback: use file mtime + 90 days
|
|
46
|
-
try {
|
|
47
|
-
const stat = await fs.stat(certPath);
|
|
48
|
-
const expiryDate = new Date(stat.mtime.getTime() + 90 * 86400000);
|
|
49
|
-
const daysLeft = Math.floor((expiryDate - Date.now()) / 86400000);
|
|
50
|
-
return { expiryDate, daysLeft, errorReason: 'expiry estimated from file date' };
|
|
51
|
-
} catch {
|
|
52
|
-
return null;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// ─── ACME Client Detection ────────────────────────────────────────────────────
|
|
57
|
-
// Supports both certbot and win-acme on Windows
|
|
58
|
-
|
|
59
|
-
const CERTBOT_WIN_EXE = 'C:\\Program Files\\Certbot\\bin\\certbot.exe';
|
|
60
|
-
const WINACME_EXE = 'C:\\Program Files\\win-acme\\wacs.exe';
|
|
61
|
-
|
|
62
|
-
async function getCertbotExe() {
|
|
63
|
-
if (!isWindows) {
|
|
64
|
-
const r = await run('which certbot');
|
|
65
|
-
return (r.exitCode === 0 && r.stdout.trim()) ? 'certbot' : null;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// 1. certbot on PATH?
|
|
69
|
-
const pathResult = await run('where.exe certbot');
|
|
70
|
-
if (pathResult.exitCode === 0 && pathResult.stdout.trim()) {
|
|
71
|
-
return 'certbot';
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// 2. certbot well-known location
|
|
75
|
-
const exeCheck = await run(`Test-Path "${CERTBOT_WIN_EXE}"`);
|
|
76
|
-
if (exeCheck.stdout.trim().toLowerCase() === 'true') {
|
|
77
|
-
return `& "${CERTBOT_WIN_EXE}"`;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// 3. win-acme on PATH?
|
|
81
|
-
const wacsPathResult = await run('where.exe wacs');
|
|
82
|
-
if (wacsPathResult.exitCode === 0 && wacsPathResult.stdout.trim()) {
|
|
83
|
-
return 'wacs';
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// 4. win-acme well-known location
|
|
87
|
-
const wacsCheck = await run(`Test-Path "${WINACME_EXE}"`);
|
|
88
|
-
if (wacsCheck.stdout.trim().toLowerCase() === 'true') {
|
|
89
|
-
return `& "${WINACME_EXE}"`;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return null;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
async function isCertbotInstalled() {
|
|
96
|
-
return (await getCertbotExe()) !== null;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
async function getAcmeClientType() {
|
|
100
|
-
const exe = await getCertbotExe();
|
|
101
|
-
if (!exe) return null;
|
|
102
|
-
if (exe.includes('wacs')) return 'winacme';
|
|
103
|
-
return 'certbot';
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// ─── isPort80Busy ─────────────────────────────────────────────────────────────
|
|
107
|
-
|
|
108
|
-
async function isPort80Busy() {
|
|
109
|
-
const cmd = isWindows
|
|
110
|
-
? 'netstat -ano | findstr ":80"'
|
|
111
|
-
: "ss -tlnp | grep ':80 '";
|
|
112
|
-
const result = await run(cmd);
|
|
113
|
-
const busy = result.success && result.stdout.length > 0;
|
|
114
|
-
return { busy, detail: busy ? result.stdout.split('\n')[0].trim() : null };
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// ─── stopNginx / startNginx ───────────────────────────────────────────────────
|
|
118
|
-
|
|
119
|
-
async function stopNginx() {
|
|
120
|
-
const { nginxDir } = loadConfig();
|
|
121
|
-
const cmd = isWindows
|
|
122
|
-
? 'taskkill /f /IM nginx.exe'
|
|
123
|
-
: 'systemctl stop nginx';
|
|
124
|
-
await run(cmd);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
async function startNginx() {
|
|
128
|
-
const { nginxDir } = loadConfig();
|
|
129
|
-
const cmd = isWindows
|
|
130
|
-
? `& "${nginxDir}\\nginx.exe"`
|
|
131
|
-
: 'systemctl start nginx';
|
|
132
|
-
await run(cmd);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// ─── listCerts ────────────────────────────────────────────────────────────────
|
|
136
|
-
|
|
137
|
-
async function listCerts(liveDir) {
|
|
138
|
-
let entries;
|
|
139
|
-
try {
|
|
140
|
-
entries = await fs.readdir(liveDir, { withFileTypes: true });
|
|
141
|
-
} catch {
|
|
142
|
-
return [];
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const domains = entries.filter(e => e.isDirectory()).map(e => e.name);
|
|
146
|
-
const certs = [];
|
|
147
|
-
|
|
148
|
-
for (const domain of domains) {
|
|
149
|
-
const certPath = path.join(liveDir, domain, 'cert.pem');
|
|
150
|
-
|
|
151
|
-
let status = 'error';
|
|
152
|
-
let expiryDate = null;
|
|
153
|
-
let daysLeft = null;
|
|
154
|
-
|
|
155
|
-
try {
|
|
156
|
-
await fs.stat(certPath);
|
|
157
|
-
const expiry = await parseCertExpiry(certPath);
|
|
158
|
-
if (expiry !== null) {
|
|
159
|
-
expiryDate = expiry.expiryDate;
|
|
160
|
-
daysLeft = expiry.daysLeft;
|
|
161
|
-
if (daysLeft > 30) {
|
|
162
|
-
status = 'healthy';
|
|
163
|
-
} else if (daysLeft >= 10) {
|
|
164
|
-
status = 'expiring';
|
|
165
|
-
} else {
|
|
166
|
-
status = 'critical';
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
} catch {
|
|
170
|
-
status = 'error';
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
certs.push({ domain, status, expiryDate, daysLeft });
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return certs;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// ─── renderCertRow ────────────────────────────────────────────────────────────
|
|
180
|
-
|
|
181
|
-
function renderCertRow(cert) {
|
|
182
|
-
const domainPadded = cert.domain.padEnd(35);
|
|
183
|
-
|
|
184
|
-
if (cert.status === 'error') {
|
|
185
|
-
console.log(` ${chalk.gray('❌')} ${chalk.gray(domainPadded)} ${chalk.gray('ERROR')}`);
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const expiryStr = cert.expiryDate
|
|
190
|
-
? cert.expiryDate.toDateString().replace(/^\S+\s/, '')
|
|
191
|
-
: '—';
|
|
192
|
-
const daysStr = cert.daysLeft !== null ? `${cert.daysLeft}d` : '—';
|
|
193
|
-
|
|
194
|
-
if (cert.status === 'healthy') {
|
|
195
|
-
console.log(` ${chalk.green('✅')} ${chalk.green(domainPadded)} ${chalk.green(daysStr.padEnd(6))} ${chalk.green(`(${expiryStr})`)}`);
|
|
196
|
-
} else if (cert.status === 'expiring') {
|
|
197
|
-
console.log(` ${chalk.yellow('⚠️')} ${chalk.yellow(domainPadded)} ${chalk.yellow(daysStr.padEnd(6))} ${chalk.yellow(`(${expiryStr})`)}`);
|
|
198
|
-
} else {
|
|
199
|
-
console.log(` ${chalk.red('❌')} ${chalk.red(domainPadded)} ${chalk.red(daysStr.padEnd(6))} ${chalk.red(`(${expiryStr})`)}`);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// ─── renewCert ────────────────────────────────────────────────────────────────
|
|
204
|
-
|
|
205
|
-
async function renewCert(domain) {
|
|
206
|
-
const certbotExe = await getCertbotExe();
|
|
207
|
-
if (!certbotExe) {
|
|
208
|
-
console.log(chalk.red('\n certbot/win-acme not found — install it first\n'));
|
|
209
|
-
return { domain, success: false, exitCode: null };
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
const clientType = await getAcmeClientType();
|
|
213
|
-
|
|
214
|
-
await stopNginx();
|
|
215
|
-
|
|
216
|
-
try {
|
|
217
|
-
const portCheck = await isPort80Busy();
|
|
218
|
-
if (portCheck.busy) {
|
|
219
|
-
console.log(chalk.yellow(`\n ⚠ Port 80 is in use: ${portCheck.detail}`));
|
|
220
|
-
console.log(chalk.yellow(' Stop that process before renewing.\n'));
|
|
221
|
-
return { domain, success: false, exitCode: null };
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
let cmd;
|
|
225
|
-
if (clientType === 'winacme') {
|
|
226
|
-
// win-acme interactive mode - needs manual input
|
|
227
|
-
console.log(chalk.cyan('\n win-acme will open. Follow the prompts to renew the certificate.'));
|
|
228
|
-
console.log(chalk.cyan(' Select: N) Create new certificate → 2) Manual input → enter domain\n'));
|
|
229
|
-
cmd = certbotExe;
|
|
230
|
-
} else {
|
|
231
|
-
cmd = `${certbotExe} certonly --standalone -d "${domain}"`;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const exitCode = await runLive(cmd, { timeout: 120000 });
|
|
235
|
-
return { domain, success: exitCode === 0, exitCode };
|
|
236
|
-
} finally {
|
|
237
|
-
await startNginx();
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
// ─── renewExpiring ────────────────────────────────────────────────────────────
|
|
242
|
-
|
|
243
|
-
async function renewExpiring(certs) {
|
|
244
|
-
const expiring = certs.filter(c => c.daysLeft !== null && c.daysLeft < 30);
|
|
245
|
-
if (expiring.length === 0) return [];
|
|
246
|
-
|
|
247
|
-
const certbotExe = await getCertbotExe();
|
|
248
|
-
if (!certbotExe) return [];
|
|
249
|
-
|
|
250
|
-
const clientType = await getAcmeClientType();
|
|
251
|
-
|
|
252
|
-
await stopNginx();
|
|
253
|
-
|
|
254
|
-
const results = [];
|
|
255
|
-
try {
|
|
256
|
-
for (const cert of expiring) {
|
|
257
|
-
let cmd;
|
|
258
|
-
if (clientType === 'winacme') {
|
|
259
|
-
console.log(chalk.cyan(`\n Renewing ${cert.domain} with win-acme - follow the prompts\n`));
|
|
260
|
-
cmd = certbotExe;
|
|
261
|
-
} else {
|
|
262
|
-
cmd = `${certbotExe} certonly --standalone -d "${cert.domain}"`;
|
|
263
|
-
}
|
|
264
|
-
const exitCode = await runLive(cmd, { timeout: 120000 });
|
|
265
|
-
results.push({ domain: cert.domain, success: exitCode === 0, exitCode });
|
|
266
|
-
}
|
|
267
|
-
} finally {
|
|
268
|
-
await startNginx();
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
return results;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
// ─── installCertbot ───────────────────────────────────────────────────────────
|
|
275
|
-
|
|
276
|
-
async function installCertbot() {
|
|
277
|
-
if (!isWindows) {
|
|
278
|
-
const exitCode = await runLive('sudo apt-get install -y certbot', { timeout: 180000 });
|
|
279
|
-
return { success: exitCode === 0 };
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
// ── Shared helpers ────────────────────────────────────────────────────────────
|
|
283
|
-
|
|
284
|
-
async function verifyCertbot() {
|
|
285
|
-
const whereResult = await run('where.exe certbot 2>$null');
|
|
286
|
-
if (whereResult.success && whereResult.stdout.trim()) return true;
|
|
287
|
-
const paths = [
|
|
288
|
-
CERTBOT_WIN_EXE,
|
|
289
|
-
'C:\\Program Files (x86)\\Certbot\\bin\\certbot.exe',
|
|
290
|
-
'C:\\Certbot\\bin\\certbot.exe',
|
|
291
|
-
];
|
|
292
|
-
for (const p of paths) {
|
|
293
|
-
const r = await run(`Test-Path '${p}'`);
|
|
294
|
-
if (r.stdout.trim().toLowerCase() === 'true') return true;
|
|
295
|
-
}
|
|
296
|
-
return false;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
async function verifyWinAcme() {
|
|
300
|
-
const paths = [
|
|
301
|
-
WINACME_EXE,
|
|
302
|
-
'C:\\Program Files (x86)\\win-acme\\wacs.exe',
|
|
303
|
-
'C:\\win-acme\\wacs.exe',
|
|
304
|
-
];
|
|
305
|
-
for (const p of paths) {
|
|
306
|
-
const r = await run(`Test-Path '${p}'`);
|
|
307
|
-
if (r.stdout.trim().toLowerCase() === 'true') return true;
|
|
308
|
-
}
|
|
309
|
-
const whereResult = await run('where.exe wacs 2>$null');
|
|
310
|
-
return whereResult.success && whereResult.stdout.trim();
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
const hasCurl = (await run('where.exe curl.exe 2>$null')).success;
|
|
314
|
-
let lastDownloadError = '';
|
|
315
|
-
|
|
316
|
-
async function downloadFile(url, dest, showErrors = false) {
|
|
317
|
-
const safeUrl = url.replace(/'/g, "''");
|
|
318
|
-
const safeDest = dest.replace(/'/g, "''");
|
|
319
|
-
|
|
320
|
-
// Enable all TLS versions (1.0, 1.1, 1.2, 1.3) for maximum compatibility
|
|
321
|
-
const tlsSetup = `[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13`;
|
|
322
|
-
|
|
323
|
-
// Helper to safely run a command and catch EPERM errors
|
|
324
|
-
const safeRun = async (cmd, opts) => {
|
|
325
|
-
try {
|
|
326
|
-
return await run(cmd, opts);
|
|
327
|
-
} catch (err) {
|
|
328
|
-
// Re-throw EPERM so caller can handle Windows Defender blocks
|
|
329
|
-
if (err.code === 'EPERM' || err.message?.includes('EPERM')) {
|
|
330
|
-
throw err;
|
|
331
|
-
}
|
|
332
|
-
return { success: false, stdout: '', stderr: err.message, exitCode: null };
|
|
333
|
-
}
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
// Method 1: Invoke-WebRequest with all TLS versions
|
|
337
|
-
let r = await safeRun(
|
|
338
|
-
`${tlsSetup}; $ProgressPreference='SilentlyContinue'; Invoke-WebRequest -Uri '${safeUrl}' -OutFile '${safeDest}' -UseBasicParsing -TimeoutSec 120`,
|
|
339
|
-
{ timeout: 130000 },
|
|
340
|
-
);
|
|
341
|
-
if (r.success) return true;
|
|
342
|
-
if (showErrors && r.stderr) lastDownloadError = `Invoke-WebRequest: ${r.stderr}`;
|
|
343
|
-
|
|
344
|
-
// Method 2: curl.exe (works on Windows 10+, often bypasses AV detection)
|
|
345
|
-
if (hasCurl) {
|
|
346
|
-
r = await safeRun(
|
|
347
|
-
`curl.exe -L --ssl-no-revoke --max-time 120 -o '${safeDest}' '${safeUrl}'`,
|
|
348
|
-
{ timeout: 130000 },
|
|
349
|
-
);
|
|
350
|
-
if (r.success) return true;
|
|
351
|
-
if (showErrors && r.stderr && !lastDownloadError) lastDownloadError = `curl: ${r.stderr}`;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
// Method 3: WebClient with TLS
|
|
355
|
-
r = await safeRun(
|
|
356
|
-
`${tlsSetup}; (New-Object System.Net.WebClient).DownloadFile('${safeUrl}','${safeDest}')`,
|
|
357
|
-
{ timeout: 130000 },
|
|
358
|
-
);
|
|
359
|
-
if (r.success) return true;
|
|
360
|
-
if (showErrors && r.stderr && !lastDownloadError) lastDownloadError = `WebClient: ${r.stderr}`;
|
|
361
|
-
|
|
362
|
-
// Method 4: HttpClient with custom handler
|
|
363
|
-
r = await safeRun(
|
|
364
|
-
`${tlsSetup}; $handler=[System.Net.Http.HttpClientHandler]::new(); $handler.ServerCertificateCustomValidationCallback={$true}; $handler.AllowAutoRedirect=$true; $hc=[System.Net.Http.HttpClient]::new($handler); $hc.DefaultRequestHeaders.Add('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64)'); $hc.Timeout=[TimeSpan]::FromSeconds(120); $bytes=$hc.GetByteArrayAsync('${safeUrl}').GetAwaiter().GetResult(); [System.IO.File]::WriteAllBytes('${safeDest}',$bytes)`,
|
|
365
|
-
{ timeout: 130000 },
|
|
366
|
-
);
|
|
367
|
-
if (r.success) return true;
|
|
368
|
-
if (showErrors && r.stderr && !lastDownloadError) lastDownloadError = `HttpClient: ${r.stderr}`;
|
|
369
|
-
|
|
370
|
-
// Method 5: BITS Transfer (background transfers)
|
|
371
|
-
r = await safeRun(
|
|
372
|
-
`Import-Module BitsTransfer -ErrorAction SilentlyContinue; Start-BitsTransfer -Source '${safeUrl}' -Destination '${safeDest}' -ErrorAction Stop`,
|
|
373
|
-
{ timeout: 130000 },
|
|
374
|
-
);
|
|
375
|
-
if (r.success) return true;
|
|
376
|
-
if (showErrors && r.stderr && !lastDownloadError) lastDownloadError = `BITS: ${r.stderr}`;
|
|
377
|
-
|
|
378
|
-
// Note: certutil.exe removed - it triggers Trojan:Win32/Ceprolad.A detection
|
|
379
|
-
|
|
380
|
-
return false;
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
async function runNsisInstaller(exePath) {
|
|
384
|
-
await run(
|
|
385
|
-
`$p = Start-Process -FilePath '${exePath}' -ArgumentList '/S' -PassThru -Wait; $p.ExitCode`,
|
|
386
|
-
{ timeout: 120000 },
|
|
387
|
-
);
|
|
388
|
-
await new Promise(res => setTimeout(res, 4000));
|
|
389
|
-
return verifyCertbot();
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
let methodNum = 0;
|
|
393
|
-
function step(label) {
|
|
394
|
-
methodNum++;
|
|
395
|
-
console.log(chalk.gray(`\n [${methodNum}] ${label}\n`));
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
// ── Check winget availability and offer to install if missing ─────────────────
|
|
399
|
-
let wingetAvailable = false;
|
|
400
|
-
const wingetCheck = await run('where.exe winget 2>$null');
|
|
401
|
-
wingetAvailable = wingetCheck.success && wingetCheck.stdout.trim();
|
|
402
|
-
|
|
403
|
-
if (!wingetAvailable) {
|
|
404
|
-
console.log(chalk.yellow('\n ⚠ winget is not installed on this system.'));
|
|
405
|
-
console.log(chalk.gray(' winget (Windows Package Manager) provides the easiest installation method.'));
|
|
406
|
-
|
|
407
|
-
let installWinget;
|
|
408
|
-
try {
|
|
409
|
-
({ installWinget } = await inquirer.prompt([{
|
|
410
|
-
type: 'confirm',
|
|
411
|
-
name: 'installWinget',
|
|
412
|
-
message: 'Would you like to install winget automatically?',
|
|
413
|
-
default: true,
|
|
414
|
-
}]));
|
|
415
|
-
} catch { /* user cancelled */ }
|
|
416
|
-
|
|
417
|
-
if (installWinget) {
|
|
418
|
-
console.log(chalk.cyan('\n Opening winget installer in a new window...'));
|
|
419
|
-
console.log(chalk.gray(' The installer will run in a separate PowerShell window.'));
|
|
420
|
-
console.log(chalk.gray(' Please complete the installation in that window.\n'));
|
|
421
|
-
|
|
422
|
-
// Use the embedded winget-install.ps1 script
|
|
423
|
-
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
|
|
424
|
-
const wingetScriptPath = path.join(scriptDir, '..', '..', 'lib', 'installer', 'winget-install.ps1');
|
|
425
|
-
|
|
426
|
-
// Check if the embedded script exists
|
|
427
|
-
const scriptExists = await run(`Test-Path "${wingetScriptPath}"`);
|
|
428
|
-
if (scriptExists.stdout.trim().toLowerCase() !== 'true') {
|
|
429
|
-
console.log(chalk.red('\n Embedded winget installer script not found.'));
|
|
430
|
-
console.log(chalk.gray(' Expected location: ' + wingetScriptPath + '\n'));
|
|
431
|
-
} else {
|
|
432
|
-
// Run the installer in a new PowerShell window with -NoExit so user can see output
|
|
433
|
-
// The script has a built-in -NoExit parameter we can use
|
|
434
|
-
await run(`Start-Process powershell.exe -ArgumentList '-ExecutionPolicy Bypass -NoProfile -File "${wingetScriptPath}" -NoExit' -Verb RunAs -Wait`);
|
|
435
|
-
|
|
436
|
-
console.log(chalk.gray('\n Winget installer window has closed.'));
|
|
437
|
-
console.log(chalk.cyan(' Checking if winget is now available...\n'));
|
|
438
|
-
|
|
439
|
-
// Give a moment for PATH to update
|
|
440
|
-
await new Promise(res => setTimeout(res, 2000));
|
|
441
|
-
|
|
442
|
-
// Re-check for winget
|
|
443
|
-
const recheck = await run('where.exe winget 2>$null');
|
|
444
|
-
if (recheck.success && recheck.stdout.trim()) {
|
|
445
|
-
console.log(chalk.green(' ✓ winget installed successfully!\n'));
|
|
446
|
-
wingetAvailable = true;
|
|
447
|
-
} else {
|
|
448
|
-
// Try refreshing PATH from registry and check again
|
|
449
|
-
console.log(chalk.yellow(' winget not immediately detected. Refreshing PATH...'));
|
|
450
|
-
await run(`$env:PATH = [System.Environment]::GetEnvironmentVariable('PATH', 'Machine') + ';' + [System.Environment]::GetEnvironmentVariable('PATH', 'User')`);
|
|
451
|
-
await new Promise(res => setTimeout(res, 1000));
|
|
452
|
-
|
|
453
|
-
const recheck2 = await run('where.exe winget 2>$null');
|
|
454
|
-
if (recheck2.success && recheck2.stdout.trim()) {
|
|
455
|
-
console.log(chalk.green('\n ✓ winget installed successfully!\n'));
|
|
456
|
-
wingetAvailable = true;
|
|
457
|
-
} else {
|
|
458
|
-
console.log(chalk.yellow('\n winget may have been installed but is not in PATH yet.'));
|
|
459
|
-
console.log(chalk.gray(' You may need to restart your terminal or computer.\n'));
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
// If still no winget, continue with other methods
|
|
465
|
-
if (!wingetAvailable) {
|
|
466
|
-
console.log(chalk.gray(' Continuing with alternative installation methods...\n'));
|
|
467
|
-
}
|
|
468
|
-
} else {
|
|
469
|
-
console.log(chalk.gray(' Skipping winget installation. Using alternative methods...\n'));
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
// ── Method 1: winget (certbot EFF - most reliable) ────────────────────────────
|
|
474
|
-
if (wingetAvailable) {
|
|
475
|
-
step('Trying winget (EFF.Certbot) ...');
|
|
476
|
-
console.log(chalk.gray(' Running: winget install EFF.Certbot\n'));
|
|
477
|
-
const exitCode = await runLive(
|
|
478
|
-
'winget install -e --id EFF.Certbot --accept-package-agreements --accept-source-agreements',
|
|
479
|
-
{ timeout: 180000 },
|
|
480
|
-
);
|
|
481
|
-
if (exitCode === 0 || await verifyCertbot()) return { success: true };
|
|
482
|
-
console.log(chalk.yellow(' winget certbot failed, trying next...\n'));
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
// ── Method 2: winget (win-acme) ───────────────────────────────────────────────
|
|
486
|
-
if (wingetAvailable) {
|
|
487
|
-
step('Trying winget (win-acme) ...');
|
|
488
|
-
console.log(chalk.gray(' Running: winget install win-acme.win-acme\n'));
|
|
489
|
-
const exitCode = await runLive(
|
|
490
|
-
'winget install -e --id win-acme.win-acme --accept-package-agreements --accept-source-agreements',
|
|
491
|
-
{ timeout: 180000 },
|
|
492
|
-
);
|
|
493
|
-
if (exitCode === 0 || await verifyWinAcme()) return { success: true, client: 'winacme' };
|
|
494
|
-
console.log(chalk.yellow(' winget win-acme failed, trying next...\n'));
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
// ── Method 3: Chocolatey (win-acme) ───────────────────────────────────────────
|
|
498
|
-
if ((await run('where.exe choco 2>$null')).success) {
|
|
499
|
-
step('Trying Chocolatey (win-acme) ...');
|
|
500
|
-
const exitCode = await runLive('choco install win-acme -y', { timeout: 180000 });
|
|
501
|
-
if (exitCode === 0 || await verifyWinAcme()) return { success: true, client: 'winacme' };
|
|
502
|
-
console.log(chalk.yellow(' Chocolatey win-acme failed, trying next...\n'));
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
// ── Method 4: Chocolatey (certbot) ────────────────────────────────────────────
|
|
506
|
-
if ((await run('where.exe choco 2>$null')).success) {
|
|
507
|
-
step('Trying Chocolatey (certbot) ...');
|
|
508
|
-
const exitCode = await runLive('choco install certbot -y', { timeout: 180000 });
|
|
509
|
-
if (exitCode === 0 || await verifyCertbot()) return { success: true };
|
|
510
|
-
console.log(chalk.yellow(' Chocolatey certbot failed, trying next...\n'));
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
// ── Method 5: pip (certbot) ───────────────────────────────────────────────────
|
|
514
|
-
for (const pip of ['pip', 'pip3']) {
|
|
515
|
-
const check = await run(`where.exe ${pip} 2>$null`);
|
|
516
|
-
if (check.success && check.stdout.trim()) {
|
|
517
|
-
step(`Trying ${pip} install certbot ...`);
|
|
518
|
-
const exitCode = await runLive(`${pip} install certbot`, { timeout: 180000 });
|
|
519
|
-
if (exitCode === 0 || await verifyCertbot()) return { success: true };
|
|
520
|
-
console.log(chalk.yellow(` ${pip} did not install certbot, trying next...\n`));
|
|
521
|
-
break;
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
// ── Method 6: Scoop (win-acme) ────────────────────────────────────────────────
|
|
526
|
-
if ((await run('where.exe scoop 2>$null')).success) {
|
|
527
|
-
step('Trying Scoop (win-acme) ...');
|
|
528
|
-
await runLive('scoop bucket add extras', { timeout: 60000 });
|
|
529
|
-
const exitCode = await runLive('scoop install win-acme', { timeout: 180000 });
|
|
530
|
-
if (exitCode === 0 || await verifyWinAcme()) return { success: true, client: 'winacme' };
|
|
531
|
-
console.log(chalk.yellow(' Scoop win-acme failed, trying next...\n'));
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
// ── Method 7: Direct download win-acme (ZIP - smaller, no installer) ──────────
|
|
535
|
-
const WINACME_DEST = 'C:\\Program Files\\win-acme';
|
|
536
|
-
|
|
537
|
-
step('Downloading win-acme from GitHub ...');
|
|
538
|
-
const winAcmeUrls = [
|
|
539
|
-
'https://github.com/win-acme/win-acme/releases/latest/download/win-acme.zip',
|
|
540
|
-
'https://github.com/win-acme/win-acme/releases/download/v2.2.9.1/win-acme.v2.2.9.1.zip',
|
|
541
|
-
];
|
|
542
|
-
|
|
543
|
-
for (const url of winAcmeUrls) {
|
|
544
|
-
const hostname = new URL(url).hostname;
|
|
545
|
-
console.log(chalk.gray(` Downloading from ${hostname} ...`));
|
|
546
|
-
|
|
547
|
-
const zipDest = `$env:TEMP\\win-acme.zip`;
|
|
548
|
-
lastDownloadError = '';
|
|
549
|
-
let dlOk = false;
|
|
550
|
-
try { dlOk = await downloadFile(url, zipDest, true); } catch (err) {
|
|
551
|
-
if (err.code === 'EPERM' || err.message?.includes('EPERM')) {
|
|
552
|
-
console.log(chalk.red('Windows Defender blocked this download.'));
|
|
553
|
-
console.log(chalk.gray('Use the manual installer option or add a Windows Defender exclusion.'));
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
if (dlOk) {
|
|
557
|
-
console.log(chalk.gray(' Extracting win-acme ...\n'));
|
|
558
|
-
await run(`New-Item -ItemType Directory -Force -Path '${WINACME_DEST}'`);
|
|
559
|
-
await run(`Expand-Archive -Path '${zipDest}' -DestinationPath '${WINACME_DEST}' -Force`);
|
|
560
|
-
await run(`Remove-Item -Force '${zipDest}' -ErrorAction SilentlyContinue`);
|
|
561
|
-
|
|
562
|
-
if (await verifyWinAcme()) {
|
|
563
|
-
console.log(chalk.green(` win-acme installed to ${WINACME_DEST}\n`));
|
|
564
|
-
return { success: true, client: 'winacme' };
|
|
565
|
-
}
|
|
566
|
-
console.log(chalk.yellow(' Extraction succeeded but verification failed, trying next...\n'));
|
|
567
|
-
} else {
|
|
568
|
-
console.log(chalk.yellow(` Could not download from ${hostname}`));
|
|
569
|
-
if (lastDownloadError) {
|
|
570
|
-
console.log(chalk.gray(` Error: ${lastDownloadError.substring(0, 200)}\n`));
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
// ── Method 8: Direct download certbot installer ────────────────────────────────
|
|
576
|
-
const INSTALLER_FILENAME = 'certbot-beta-installer-win_amd64_signed.exe';
|
|
577
|
-
const INSTALLER_DEST = `$env:TEMP\\${INSTALLER_FILENAME}`;
|
|
578
|
-
const certbotUrls = [
|
|
579
|
-
`https://dl.eff.org/${INSTALLER_FILENAME}`,
|
|
580
|
-
`https://github.com/certbot/certbot/releases/latest/download/${INSTALLER_FILENAME}`,
|
|
581
|
-
];
|
|
582
|
-
|
|
583
|
-
for (const url of certbotUrls) {
|
|
584
|
-
const hostname = new URL(url).hostname;
|
|
585
|
-
step(`Downloading certbot installer from ${hostname} ...`);
|
|
586
|
-
|
|
587
|
-
lastDownloadError = '';
|
|
588
|
-
let dlOk = false;
|
|
589
|
-
try { dlOk = await downloadFile(url, INSTALLER_DEST, true); } catch (err) {
|
|
590
|
-
if (err.code === 'EPERM' || err.message?.includes('EPERM')) {
|
|
591
|
-
console.log(chalk.red('Windows Defender blocked this download.'));
|
|
592
|
-
console.log(chalk.gray('Use the manual installer option or add a Windows Defender exclusion.'));
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
if (dlOk) {
|
|
596
|
-
console.log(chalk.gray(' Running installer silently ...\n'));
|
|
597
|
-
const ok = await runNsisInstaller(INSTALLER_DEST);
|
|
598
|
-
await run(`Remove-Item -Force '${INSTALLER_DEST}' -ErrorAction SilentlyContinue`);
|
|
599
|
-
if (ok) return { success: true };
|
|
600
|
-
console.log(chalk.yellow(' Installer ran but certbot not detected, trying next...\n'));
|
|
601
|
-
} else {
|
|
602
|
-
console.log(chalk.yellow(` Could not download from ${hostname}`));
|
|
603
|
-
if (lastDownloadError) {
|
|
604
|
-
console.log(chalk.gray(` Error: ${lastDownloadError.substring(0, 200)}\n`));
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
// ── Method 9: Manual installer path ───────────────────────────────────────────
|
|
610
|
-
console.log(chalk.yellow('\n All automatic methods failed.'));
|
|
611
|
-
console.log(chalk.gray(' You can manually download one of these on another PC:'));
|
|
612
|
-
console.log(chalk.gray(' • certbot: https://certbot.eff.org/instructions?ws=other&os=windows'));
|
|
613
|
-
console.log(chalk.gray(' • win-acme: https://github.com/win-acme/win-acme/releases'));
|
|
614
|
-
console.log(chalk.gray(' Then transfer to this server and use "Specify local installer" below.\n'));
|
|
615
|
-
|
|
616
|
-
let localChoice;
|
|
617
|
-
try {
|
|
618
|
-
({ localChoice } = await inquirer.prompt([{
|
|
619
|
-
type: 'list',
|
|
620
|
-
name: 'localChoice',
|
|
621
|
-
message: 'What would you like to do?',
|
|
622
|
-
choices: [
|
|
623
|
-
'Open download page in browser',
|
|
624
|
-
'Specify local installer path',
|
|
625
|
-
'Cancel'
|
|
626
|
-
],
|
|
627
|
-
}]));
|
|
628
|
-
} catch { return { success: false }; }
|
|
629
|
-
|
|
630
|
-
if (localChoice === 'Open download page in browser') {
|
|
631
|
-
console.log(chalk.cyan('\n Opening download pages in browser...'));
|
|
632
|
-
console.log(chalk.gray(' Download either win-acme.zip or certbot installer, then re-run this tool.\n'));
|
|
633
|
-
|
|
634
|
-
// Open win-acme releases
|
|
635
|
-
await run('Start-Process "https://github.com/win-acme/win-acme/releases/latest"');
|
|
636
|
-
// Also open EFF certbot page
|
|
637
|
-
await run('Start-Process "https://certbot.eff.org/instructions?ws=other&os=windows"');
|
|
638
|
-
|
|
639
|
-
console.log(chalk.green('✓ Browser opened. Download the installer, then run:'));
|
|
640
|
-
console.log(chalk.cyan(' easy-devops → SSL Manager → Install certbot → Specify local installer path\n'));
|
|
641
|
-
|
|
642
|
-
return { success: false };
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
if (localChoice === 'Specify local installer path') {
|
|
646
|
-
let localPath;
|
|
647
|
-
try {
|
|
648
|
-
({ localPath } = await inquirer.prompt([{
|
|
649
|
-
type: 'input',
|
|
650
|
-
name: 'localPath',
|
|
651
|
-
message: 'Full path to installer (.exe or .zip):',
|
|
652
|
-
validate: v => v.trim().length > 0 || 'Required',
|
|
653
|
-
}]));
|
|
654
|
-
} catch { return { success: false }; }
|
|
655
|
-
|
|
656
|
-
const exists = await run(`Test-Path '${localPath.trim()}'`);
|
|
657
|
-
if (exists.stdout.trim().toLowerCase() !== 'true') {
|
|
658
|
-
console.log(chalk.red(` File not found: ${localPath}\n`));
|
|
659
|
-
return { success: false };
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
const ext = path.extname(localPath.trim().toLowerCase());
|
|
663
|
-
|
|
664
|
-
if (ext === '.zip') {
|
|
665
|
-
step('Extracting ZIP archive ...');
|
|
666
|
-
await run(`New-Item -ItemType Directory -Force -Path '${WINACME_DEST}'`);
|
|
667
|
-
await run(`Expand-Archive -Path '${localPath.trim()}' -DestinationPath '${WINACME_DEST}' -Force`);
|
|
668
|
-
if (await verifyWinAcme()) {
|
|
669
|
-
return { success: true, client: 'winacme' };
|
|
670
|
-
}
|
|
671
|
-
console.log(chalk.red(' Extraction succeeded but win-acme not found.\n'));
|
|
672
|
-
} else {
|
|
673
|
-
step('Running installer silently ...');
|
|
674
|
-
const ok = await runNsisInstaller(localPath.trim());
|
|
675
|
-
if (ok) return { success: true };
|
|
676
|
-
console.log(chalk.red(' Installer ran but certbot was not detected.\n'));
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
return { success: false };
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
// ─── issueCert ────────────────────────────────────────────────────────────────
|
|
684
|
-
|
|
685
|
-
/**
|
|
686
|
-
* Issues a new SSL certificate using the installed ACME client.
|
|
687
|
-
* Stops nginx before the challenge and restarts it afterwards (always).
|
|
688
|
-
*
|
|
689
|
-
* @param {string} domainName - The primary domain name
|
|
690
|
-
* @param {{ www?: boolean }} options
|
|
691
|
-
* @returns {Promise<{ success: boolean, certPath: string|null, keyPath: string|null, error: object|null }>}
|
|
692
|
-
*/
|
|
693
|
-
export async function issueCert(domainName, { www = false } = {}) {
|
|
694
|
-
// Guard: ACME client must be installed
|
|
695
|
-
const certbotExe = await getCertbotExe();
|
|
696
|
-
if (!certbotExe) {
|
|
697
|
-
return {
|
|
698
|
-
success: false,
|
|
699
|
-
certPath: null,
|
|
700
|
-
keyPath: null,
|
|
701
|
-
error: {
|
|
702
|
-
step: 'ACME client detection',
|
|
703
|
-
cause: 'No ACME client (certbot or win-acme) is installed on this server.',
|
|
704
|
-
consequence: 'No certificate was issued. Install certbot or win-acme using the SSL Manager first.',
|
|
705
|
-
nginxRunning: true,
|
|
706
|
-
configSaved: false,
|
|
707
|
-
},
|
|
708
|
-
};
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
const clientType = await getAcmeClientType();
|
|
712
|
-
|
|
713
|
-
// Stop nginx (tolerate failure — nginx may not have been running)
|
|
714
|
-
await stopNginx();
|
|
715
|
-
|
|
716
|
-
try {
|
|
717
|
-
// Check port 80 is free for the standalone challenge
|
|
718
|
-
const portCheck = await isPort80Busy();
|
|
719
|
-
if (portCheck.busy) {
|
|
720
|
-
await startNginx();
|
|
721
|
-
return {
|
|
722
|
-
success: false,
|
|
723
|
-
certPath: null,
|
|
724
|
-
keyPath: null,
|
|
725
|
-
error: {
|
|
726
|
-
step: 'port 80 check',
|
|
727
|
-
cause: `Port 80 is still in use: ${portCheck.detail}. Stop that process before creating a certificate.`,
|
|
728
|
-
consequence: 'The standalone ACME challenge cannot bind to port 80. No certificate was issued. nginx has been restarted.',
|
|
729
|
-
nginxRunning: true,
|
|
730
|
-
configSaved: false,
|
|
731
|
-
},
|
|
732
|
-
};
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
// Build and run the ACME command
|
|
736
|
-
const domainArgs = www ? `-d "${domainName}" -d "www.${domainName}"` : `-d "${domainName}"`;
|
|
737
|
-
const cmd = clientType === 'winacme'
|
|
738
|
-
? certbotExe
|
|
739
|
-
: `${certbotExe} certonly --standalone --non-interactive --agree-tos ${domainArgs}`;
|
|
740
|
-
|
|
741
|
-
const exitCode = await runLive(cmd, { timeout: 120000 });
|
|
742
|
-
|
|
743
|
-
if (exitCode !== 0) {
|
|
744
|
-
await startNginx();
|
|
745
|
-
return {
|
|
746
|
-
success: false,
|
|
747
|
-
certPath: null,
|
|
748
|
-
keyPath: null,
|
|
749
|
-
error: {
|
|
750
|
-
step: 'ACME domain validation',
|
|
751
|
-
cause: 'The ACME challenge failed. Common causes: domain DNS does not point to this server, or port 80 is blocked by a firewall.',
|
|
752
|
-
consequence: 'No certificate was issued. nginx has been restarted.',
|
|
753
|
-
nginxRunning: true,
|
|
754
|
-
configSaved: false,
|
|
755
|
-
},
|
|
756
|
-
};
|
|
757
|
-
}
|
|
758
|
-
|
|
759
|
-
// Derive cert paths from the certbot live directory
|
|
760
|
-
const liveDir = getCertbotDir();
|
|
761
|
-
const certPath = path.join(liveDir, domainName, 'fullchain.pem');
|
|
762
|
-
const keyPath = path.join(liveDir, domainName, 'privkey.pem');
|
|
763
|
-
|
|
764
|
-
await startNginx();
|
|
765
|
-
|
|
766
|
-
return { success: true, certPath, keyPath, error: null };
|
|
767
|
-
} catch (err) {
|
|
768
|
-
try { await startNginx(); } catch { /* best effort */ }
|
|
769
|
-
return {
|
|
770
|
-
success: false,
|
|
771
|
-
certPath: null,
|
|
772
|
-
keyPath: null,
|
|
773
|
-
error: {
|
|
774
|
-
step: 'certificate issuance',
|
|
775
|
-
cause: err.message,
|
|
776
|
-
consequence: 'An unexpected error occurred during certificate issuance. nginx restart was attempted.',
|
|
777
|
-
nginxRunning: false,
|
|
778
|
-
configSaved: false,
|
|
779
|
-
},
|
|
780
|
-
};
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
|
|
784
|
-
// ─── showSslManager ───────────────────────────────────────────────────────────
|
|
785
|
-
|
|
786
|
-
export async function showSslManager() {
|
|
787
|
-
while (true) {
|
|
788
|
-
const liveDir = getCertbotDir();
|
|
789
|
-
|
|
790
|
-
const spinner = ora('Loading certificates…').start();
|
|
791
|
-
const certs = await listCerts(liveDir);
|
|
792
|
-
spinner.stop();
|
|
793
|
-
|
|
794
|
-
console.log(chalk.bold('\n SSL Manager'));
|
|
795
|
-
console.log(chalk.gray(' ' + '─'.repeat(40)));
|
|
796
|
-
|
|
797
|
-
if (certs.length === 0) {
|
|
798
|
-
console.log(chalk.gray(' No certificates found'));
|
|
799
|
-
} else {
|
|
800
|
-
for (const cert of certs) {
|
|
801
|
-
renderCertRow(cert);
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
console.log();
|
|
805
|
-
|
|
806
|
-
const choices = [
|
|
807
|
-
'Create new certificate',
|
|
808
|
-
'Renew a certificate',
|
|
809
|
-
'Renew all expiring (< 30 days)',
|
|
810
|
-
'Install certbot',
|
|
811
|
-
new inquirer.Separator(),
|
|
812
|
-
'← Back',
|
|
813
|
-
];
|
|
814
|
-
|
|
815
|
-
let choice;
|
|
816
|
-
try {
|
|
817
|
-
({ choice } = await inquirer.prompt([{
|
|
818
|
-
type: 'list',
|
|
819
|
-
name: 'choice',
|
|
820
|
-
message: 'Select an option:',
|
|
821
|
-
choices,
|
|
822
|
-
}]));
|
|
823
|
-
} catch (err) {
|
|
824
|
-
if (err.name === 'ExitPromptError') return;
|
|
825
|
-
throw err;
|
|
826
|
-
}
|
|
827
|
-
|
|
828
|
-
switch (choice) {
|
|
829
|
-
case 'Create new certificate': {
|
|
830
|
-
const installed = await isCertbotInstalled();
|
|
831
|
-
if (!installed) {
|
|
832
|
-
console.log(chalk.yellow('\n ⚠ certbot/win-acme not found — select "Install certbot" first\n'));
|
|
833
|
-
break;
|
|
834
|
-
}
|
|
835
|
-
|
|
836
|
-
let domainInput, wwwInput;
|
|
837
|
-
try {
|
|
838
|
-
({ domainInput } = await inquirer.prompt([{
|
|
839
|
-
type: 'input',
|
|
840
|
-
name: 'domainInput',
|
|
841
|
-
message: 'Domain name:',
|
|
842
|
-
validate: v => v.trim() ? true : 'Required',
|
|
843
|
-
}]));
|
|
844
|
-
({ wwwInput } = await inquirer.prompt([{
|
|
845
|
-
type: 'confirm',
|
|
846
|
-
name: 'wwwInput',
|
|
847
|
-
message: 'Include www subdomain?',
|
|
848
|
-
default: false,
|
|
849
|
-
}]));
|
|
850
|
-
} catch (err) {
|
|
851
|
-
if (err.name === 'ExitPromptError') return;
|
|
852
|
-
throw err;
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
const spinner = ora(`Creating certificate for ${domainInput.trim()}…`).start();
|
|
856
|
-
const result = await issueCert(domainInput.trim(), { www: wwwInput });
|
|
857
|
-
spinner.stop();
|
|
858
|
-
|
|
859
|
-
if (result.success) {
|
|
860
|
-
console.log(chalk.green('\n ✓ Certificate created successfully'));
|
|
861
|
-
console.log(chalk.gray(` Cert: ${result.certPath}`));
|
|
862
|
-
console.log(chalk.gray(` Key: ${result.keyPath}\n`));
|
|
863
|
-
} else {
|
|
864
|
-
const e = result.error;
|
|
865
|
-
console.log(chalk.red('\n ✗ Certificate creation failed'));
|
|
866
|
-
console.log(chalk.yellow(` Step: ${e.step}`));
|
|
867
|
-
console.log(chalk.yellow(` Cause: ${e.cause}`));
|
|
868
|
-
console.log(chalk.yellow(` Consequence: ${e.consequence}`));
|
|
869
|
-
console.log(chalk.gray(` nginx running: ${e.nginxRunning ? 'yes' : 'no'}\n`));
|
|
870
|
-
}
|
|
871
|
-
break;
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
case 'Renew a certificate': {
|
|
875
|
-
const installed = await isCertbotInstalled();
|
|
876
|
-
if (!installed) {
|
|
877
|
-
console.log(chalk.yellow('\n ⚠ certbot/win-acme not found — select "Install certbot" first\n'));
|
|
878
|
-
break;
|
|
879
|
-
}
|
|
880
|
-
|
|
881
|
-
if (certs.length === 0) {
|
|
882
|
-
console.log(chalk.gray('\n No certificates found to renew\n'));
|
|
883
|
-
break;
|
|
884
|
-
}
|
|
885
|
-
|
|
886
|
-
let selectedDomain;
|
|
887
|
-
try {
|
|
888
|
-
({ selectedDomain } = await inquirer.prompt([{
|
|
889
|
-
type: 'list',
|
|
890
|
-
name: 'selectedDomain',
|
|
891
|
-
message: 'Select domain to renew:',
|
|
892
|
-
choices: certs.map(c => c.domain),
|
|
893
|
-
}]));
|
|
894
|
-
} catch (err) {
|
|
895
|
-
if (err.name === 'ExitPromptError') return;
|
|
896
|
-
throw err;
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
const renewResult = await renewCert(selectedDomain);
|
|
900
|
-
if (renewResult.success) {
|
|
901
|
-
console.log(chalk.green('\n ✓ Renewed successfully\n'));
|
|
902
|
-
} else {
|
|
903
|
-
console.log(chalk.red('\n ✗ Renewal failed — see output above\n'));
|
|
904
|
-
}
|
|
905
|
-
break;
|
|
906
|
-
}
|
|
907
|
-
|
|
908
|
-
case 'Renew all expiring (< 30 days)': {
|
|
909
|
-
const installed = await isCertbotInstalled();
|
|
910
|
-
if (!installed) {
|
|
911
|
-
console.log(chalk.yellow('\n ⚠ certbot/win-acme not found — select "Install certbot" first\n'));
|
|
912
|
-
break;
|
|
913
|
-
}
|
|
914
|
-
|
|
915
|
-
const results = await renewExpiring(certs);
|
|
916
|
-
if (results.length === 0) {
|
|
917
|
-
console.log(chalk.gray('\n No certificates expiring within 30 days\n'));
|
|
918
|
-
} else {
|
|
919
|
-
console.log();
|
|
920
|
-
for (const r of results) {
|
|
921
|
-
if (r.success) {
|
|
922
|
-
console.log(` ${chalk.green('✓ ' + r.domain)}`);
|
|
923
|
-
} else {
|
|
924
|
-
console.log(` ${chalk.red('✗ ' + r.domain)}`);
|
|
925
|
-
}
|
|
926
|
-
}
|
|
927
|
-
console.log();
|
|
928
|
-
}
|
|
929
|
-
break;
|
|
930
|
-
}
|
|
931
|
-
|
|
932
|
-
case 'Install certbot': {
|
|
933
|
-
const alreadyInstalled = await isCertbotInstalled();
|
|
934
|
-
if (alreadyInstalled) {
|
|
935
|
-
const clientType = await getAcmeClientType();
|
|
936
|
-
console.log(chalk.gray(`\n ${clientType === 'winacme' ? 'win-acme' : 'certbot'} is already installed\n`));
|
|
937
|
-
break;
|
|
938
|
-
}
|
|
939
|
-
|
|
940
|
-
const installResult = await installCertbot();
|
|
941
|
-
if (installResult.success) {
|
|
942
|
-
const clientName = installResult.client === 'winacme' ? 'win-acme' : 'certbot';
|
|
943
|
-
console.log(chalk.green(`\n ✓ ${clientName} installed successfully\n`));
|
|
944
|
-
} else {
|
|
945
|
-
console.log(chalk.red('\n ✗ Installation failed\n'));
|
|
946
|
-
}
|
|
947
|
-
break;
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
case '← Back':
|
|
951
|
-
return;
|
|
952
|
-
}
|
|
953
|
-
|
|
954
|
-
if (choice !== '← Back') {
|
|
955
|
-
try {
|
|
956
|
-
await inquirer.prompt([{ type: 'input', name: '_', message: 'Press Enter to continue...' }]);
|
|
957
|
-
} catch (err) {
|
|
958
|
-
if (err.name === 'ExitPromptError') return;
|
|
959
|
-
throw err;
|
|
960
|
-
}
|
|
961
|
-
}
|
|
962
|
-
}
|
|
963
|
-
}
|