easy-devops 1.2.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +97 -6
- package/dist/ui/assets/index-B6zdwEnG.css +1 -0
- package/dist/ui/assets/index-Cy0oNmul.js +442 -0
- package/dist/ui/assets/socket-DwLPrgsr.js +1 -0
- package/dist/ui/assets/socket-K5ttLD2q.js +65 -0
- package/dist/ui/brand/apple-touch-icon.png +0 -0
- package/dist/ui/brand/favicon-16.png +0 -0
- package/dist/ui/brand/favicon-32.png +0 -0
- package/dist/ui/brand/favicon-light-16.png +0 -0
- package/dist/ui/brand/favicon-light-32.png +0 -0
- package/dist/ui/brand/favicon.ico +0 -0
- package/dist/ui/brand/icon-192.png +0 -0
- package/dist/ui/brand/icon-48.png +0 -0
- package/dist/ui/brand/icon-512.png +0 -0
- package/dist/ui/brand/icon-64-light.png +0 -0
- package/dist/ui/brand/icon-64.png +0 -0
- package/dist/ui/brand/logo-dark.png +0 -0
- package/dist/ui/brand/logo-light.png +0 -0
- package/dist/ui/brand/mark-dark.png +0 -0
- package/dist/ui/brand/mark-light.png +0 -0
- package/dist/ui/index.html +71 -0
- package/install.ps1 +188 -1
- package/install.sh +254 -29
- package/package.json +15 -4
- package/src/cli/dash.js +110 -0
- package/src/cli/index.js +237 -106
- package/src/cli/managers/domain-manager.js +60 -40
- package/src/cli/managers/ssl-manager.js +47 -330
- package/src/cli/menu.js +156 -0
- package/src/cli/menus/cleanup.js +205 -0
- package/src/cli/menus/dashboard.js +92 -1
- package/src/cli/menus/runtime.js +236 -0
- package/src/cli/menus/service.js +764 -0
- package/src/cli/menus/settings.js +176 -8
- package/src/cli/menus/uninstall.js +167 -0
- package/src/cli/menus/update.js +221 -65
- package/src/core/acme-challenge.js +273 -0
- package/src/core/cert-operations.js +189 -0
- package/src/{dashboard/lib → core}/cert-reader.js +2 -2
- package/src/core/cert-renewal.js +494 -0
- package/src/core/cleanup.js +600 -0
- package/src/core/config.js +169 -2
- package/src/core/daemon/ipc.js +292 -0
- package/src/core/daemon/lock.js +294 -0
- package/src/core/daemon/run.js +241 -0
- package/src/core/daemon/stopRequest.js +162 -0
- package/src/core/db.js +37 -8
- package/src/core/deploy/apps.js +491 -0
- package/src/core/deploy/domainLink.js +200 -0
- package/src/core/deploy/engine.js +973 -0
- package/src/core/deploy/ezfile.js +361 -0
- package/src/core/deploy/github.js +239 -0
- package/src/core/deploy/githubApi.js +261 -0
- package/src/core/deploy/hookServer.js +325 -0
- package/src/core/deploy/hubCommands.js +267 -0
- package/src/core/deploy/launcher.js +57 -0
- package/src/core/deploy/status.js +91 -0
- package/src/core/deploy/store.js +398 -0
- package/src/core/domainNotifier.js +94 -28
- package/src/core/domains-db.js +234 -0
- package/src/core/events.js +94 -2
- package/src/core/hub/agent.js +842 -0
- package/src/core/hubLink.js +306 -0
- package/src/core/hubPermissions.js +430 -0
- package/src/core/installSource.js +162 -0
- package/src/core/jobs/bus.js +120 -0
- package/src/core/jobs/index.js +371 -0
- package/src/core/jobs/monitors.js +368 -0
- package/src/core/jobs/owner.js +217 -0
- package/src/core/jobs/revisionWatcher.js +106 -0
- package/src/core/nginx-apply.js +213 -0
- package/src/core/nginx-conf-generator.js +253 -17
- package/src/{dashboard/lib → core}/nginx-service.js +18 -9
- package/src/core/notifications/claim.js +214 -0
- package/src/core/notifications/dispatch.js +154 -0
- package/src/core/notifier.js +31 -1
- package/src/core/passwords.js +172 -0
- package/src/core/permissions.js +8 -4
- package/src/core/platform.js +709 -2
- package/src/core/probeCache.js +258 -0
- package/src/core/purge.js +372 -0
- package/src/core/runner.js +488 -0
- package/src/core/service/acl.js +226 -0
- package/src/core/service/dataDir.js +345 -0
- package/src/core/service/index.js +1128 -0
- package/src/core/service/legacy.js +218 -0
- package/src/core/service/runtime.js +446 -0
- package/src/core/service/runtimeUpdate.js +800 -0
- package/src/core/service/systemd.js +458 -0
- package/src/core/service/windowsPersonal.js +530 -0
- package/src/core/service/windowsService.js +576 -0
- package/src/core/service/winsw.js +260 -0
- package/src/core/setupCode.js +331 -0
- package/src/core/shell.js +70 -7
- package/src/core/ssl-issuer.js +627 -0
- package/src/core/updater.js +295 -0
- package/src/core/validators.js +99 -0
- package/src/dashboard/controllers/authController.js +302 -15
- package/src/dashboard/controllers/deploymentsController.js +479 -0
- package/src/dashboard/controllers/domainsController.js +39 -52
- package/src/dashboard/controllers/hubController.js +135 -0
- package/src/dashboard/controllers/nginxController.js +21 -3
- package/src/dashboard/controllers/processesController.js +368 -0
- package/src/dashboard/controllers/settingsController.js +180 -12
- package/src/dashboard/controllers/sslController.js +171 -10
- package/src/dashboard/lib/daemonBridge.js +163 -0
- package/src/dashboard/lib/localRequest.js +235 -0
- package/src/dashboard/lib/loginRateLimit.js +119 -0
- package/src/dashboard/lib/requireAuth.js +80 -0
- package/src/dashboard/public/js/app.js +489 -19
- package/src/dashboard/routes/auth.js +10 -10
- package/src/dashboard/routes/deployments.js +46 -0
- package/src/dashboard/routes/domains.js +9 -0
- package/src/dashboard/routes/hub.js +22 -0
- package/src/dashboard/routes/nginx.js +9 -0
- package/src/dashboard/routes/processes.js +29 -0
- package/src/dashboard/routes/settings.js +9 -0
- package/src/dashboard/routes/ssl.js +9 -0
- package/src/dashboard/server.js +288 -21
- package/src/dashboard/socketManager.js +171 -207
- package/src/dashboard/views/index.ejs +1 -0
- package/src/dashboard/views/partials/login.ejs +68 -7
- package/src/dashboard/views/partials/processes-panel.ejs +261 -0
- package/src/dashboard/views/partials/settings-panel.ejs +287 -0
- package/src/dashboard/views/partials/sidebar.ejs +2 -0
- package/src/dashboard/lib/domains-db.js +0 -51
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* core/nginx-apply.js
|
|
3
|
+
*
|
|
4
|
+
* The single place where a generated nginx config is written, tested and —
|
|
5
|
+
* if the test fails — rolled back. The CLI, the dashboard API and the Hub
|
|
6
|
+
* command path all go through `writeConfWithTest()`, so the write/test/restore
|
|
7
|
+
* behaviour cannot drift between them.
|
|
8
|
+
*
|
|
9
|
+
* Why this is separate from nginx-conf-generator.js: the generator stays a
|
|
10
|
+
* pure config builder with no subprocess execution. This module is the part
|
|
11
|
+
* that touches nginx.
|
|
12
|
+
*
|
|
13
|
+
* Windows certificate-path handling
|
|
14
|
+
* ---------------------------------
|
|
15
|
+
* nginx's config lexer treats `\` as an escape character, so an unquoted
|
|
16
|
+
* `C:\nginx\test\certs\a.pem` has its \n and \t read as a newline and a tab.
|
|
17
|
+
* Configs are therefore written with forward slashes first. Only if nginx
|
|
18
|
+
* rejects THIS config file for a certificate-loading reason do we retry once
|
|
19
|
+
* with quoted, escaped backslashes. Linux never retries and never changes.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import fs from 'fs/promises';
|
|
23
|
+
import path from 'path';
|
|
24
|
+
import { loadConfig } from './config.js';
|
|
25
|
+
import { run } from './shell.js';
|
|
26
|
+
import {
|
|
27
|
+
isWindows,
|
|
28
|
+
nginxTestCmd,
|
|
29
|
+
combineOutput,
|
|
30
|
+
isNginxTestOk,
|
|
31
|
+
isCertPathErrorFor,
|
|
32
|
+
parseNginxErrorFiles,
|
|
33
|
+
} from './platform.js';
|
|
34
|
+
import { generateConf, getConfDDir, CERT_PATH_STYLES } from './nginx-conf-generator.js';
|
|
35
|
+
|
|
36
|
+
/** Runs `nginx -t` and returns { ok, output }. Never inspects the exit code directly. */
|
|
37
|
+
async function runNginxTest(nginxDir) {
|
|
38
|
+
const cmd = isWindows ? nginxTestCmd(nginxDir) : 'nginx -t';
|
|
39
|
+
const result = await run(cmd, { cwd: nginxDir });
|
|
40
|
+
return { ok: isNginxTestOk(result), output: combineOutput(result) };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Reads a file's current contents, or null when it does not exist. */
|
|
44
|
+
async function snapshot(confPath) {
|
|
45
|
+
try {
|
|
46
|
+
return await fs.readFile(confPath, 'utf8');
|
|
47
|
+
} catch {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Restores a config file to a previous state.
|
|
54
|
+
* @param {string} confPath
|
|
55
|
+
* @param {string|null} previous Contents to restore, or null to delete the file
|
|
56
|
+
*/
|
|
57
|
+
async function restore(confPath, previous) {
|
|
58
|
+
try {
|
|
59
|
+
if (previous === null) {
|
|
60
|
+
await fs.unlink(confPath);
|
|
61
|
+
} else {
|
|
62
|
+
await fs.writeFile(confPath, previous, 'utf8');
|
|
63
|
+
}
|
|
64
|
+
return true;
|
|
65
|
+
} catch {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Builds the error message for a failed apply, naming the file nginx blamed.
|
|
72
|
+
* @param {string} confPath
|
|
73
|
+
* @param {Array<{ style: string, output: string }>} attempts
|
|
74
|
+
* @returns {string}
|
|
75
|
+
*/
|
|
76
|
+
function describeFailure(confPath, attempts) {
|
|
77
|
+
const last = attempts[attempts.length - 1];
|
|
78
|
+
const blamed = parseNginxErrorFiles(last.output);
|
|
79
|
+
|
|
80
|
+
// Prefer the file nginx actually named; fall back to the one we wrote.
|
|
81
|
+
const file = blamed.length > 0 ? blamed.join(', ') : confPath;
|
|
82
|
+
const sameFile =
|
|
83
|
+
blamed.length === 0 ||
|
|
84
|
+
blamed.some((f) => f.replace(/\\/g, '/').toLowerCase() === confPath.replace(/\\/g, '/').toLowerCase());
|
|
85
|
+
|
|
86
|
+
const lines = [`nginx rejected the configuration in ${file}.`];
|
|
87
|
+
|
|
88
|
+
if (!sameFile) {
|
|
89
|
+
lines.push(
|
|
90
|
+
`Note: the error is in a different file than the one just written (${confPath}), ` +
|
|
91
|
+
'so it is probably pre-existing and unrelated to this change.'
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (attempts.length > 1) {
|
|
96
|
+
lines.push(
|
|
97
|
+
'Both certificate path forms were tried (forward slashes, then escaped backslashes) ' +
|
|
98
|
+
'and both were rejected, so the path format is not the cause — check that the ' +
|
|
99
|
+
'certificate files exist and are readable.'
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
lines.push('', 'nginx output:', last.output || '(no output)');
|
|
104
|
+
return lines.join('\n');
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Writes a domain's config, tests it, and restores the previous content on failure.
|
|
109
|
+
*
|
|
110
|
+
* On Windows, if the test fails specifically because nginx could not load this
|
|
111
|
+
* config's certificates, the config is rewritten once with escaped backslash
|
|
112
|
+
* paths and tested again.
|
|
113
|
+
*
|
|
114
|
+
* Always targets the configured nginx directory: generateConf() resolves it
|
|
115
|
+
* from loadConfig(), so an override here would silently disagree with the file
|
|
116
|
+
* actually written.
|
|
117
|
+
*
|
|
118
|
+
* @param {Object} domain Domain object (v2 schema); its configFile/updatedAt are set on success
|
|
119
|
+
* @returns {Promise<{
|
|
120
|
+
* ok: boolean,
|
|
121
|
+
* confPath: string|null,
|
|
122
|
+
* style: string|null,
|
|
123
|
+
* output: string,
|
|
124
|
+
* error: string|null,
|
|
125
|
+
* restored: boolean,
|
|
126
|
+
* existed: boolean
|
|
127
|
+
* }>}
|
|
128
|
+
*/
|
|
129
|
+
export async function writeConfWithTest(domain) {
|
|
130
|
+
const { nginxDir } = loadConfig();
|
|
131
|
+
|
|
132
|
+
// Resolve the target path without writing, so we can snapshot it first.
|
|
133
|
+
const confPath = path.join(getConfDDir(nginxDir), `${domain.name}.conf`);
|
|
134
|
+
|
|
135
|
+
const previous = await snapshot(confPath);
|
|
136
|
+
const existed = previous !== null;
|
|
137
|
+
|
|
138
|
+
// Windows gets a fallback attempt; Linux behaviour is unchanged.
|
|
139
|
+
const styles = isWindows
|
|
140
|
+
? [CERT_PATH_STYLES.FORWARD, CERT_PATH_STYLES.ESCAPED]
|
|
141
|
+
: [CERT_PATH_STYLES.FORWARD];
|
|
142
|
+
|
|
143
|
+
const attempts = [];
|
|
144
|
+
|
|
145
|
+
for (let i = 0; i < styles.length; i++) {
|
|
146
|
+
const style = styles[i];
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
await generateConf(domain, { certPathStyle: style });
|
|
150
|
+
} catch (err) {
|
|
151
|
+
await restore(confPath, previous);
|
|
152
|
+
return {
|
|
153
|
+
ok: false,
|
|
154
|
+
confPath: null,
|
|
155
|
+
style: null,
|
|
156
|
+
output: '',
|
|
157
|
+
error: `Failed to write ${confPath}: ${err.message}`,
|
|
158
|
+
restored: true,
|
|
159
|
+
existed,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const { ok, output } = await runNginxTest(nginxDir);
|
|
164
|
+
attempts.push({ style, output });
|
|
165
|
+
|
|
166
|
+
if (ok) {
|
|
167
|
+
return { ok: true, confPath, style, output, error: null, restored: false, existed };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const isLastAttempt = i === styles.length - 1;
|
|
171
|
+
// Only retry when nginx blamed THIS file's certificate directives — a cert
|
|
172
|
+
// error in someone else's config must not trigger a path rewrite here.
|
|
173
|
+
if (isLastAttempt || !isCertPathErrorFor(output, confPath)) break;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const restored = await restore(confPath, previous);
|
|
177
|
+
return {
|
|
178
|
+
ok: false,
|
|
179
|
+
confPath,
|
|
180
|
+
style: null,
|
|
181
|
+
output: attempts[attempts.length - 1]?.output ?? '',
|
|
182
|
+
error: describeFailure(confPath, attempts),
|
|
183
|
+
restored,
|
|
184
|
+
existed,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Lists stored domains whose certificate paths still contain backslashes.
|
|
190
|
+
*
|
|
191
|
+
* Report-only: existing configs are never rewritten automatically. Regenerating
|
|
192
|
+
* such a domain through the normal update path applies the fix, because that
|
|
193
|
+
* path goes through writeConfWithTest().
|
|
194
|
+
*
|
|
195
|
+
* @param {Array<Object>} domains Domain objects (as stored)
|
|
196
|
+
* @returns {Array<{ name: string, certPath: string, keyPath: string }>}
|
|
197
|
+
*/
|
|
198
|
+
export function findBackslashCertPaths(domains) {
|
|
199
|
+
if (!Array.isArray(domains)) return [];
|
|
200
|
+
|
|
201
|
+
return domains
|
|
202
|
+
.filter((d) => d?.ssl?.enabled)
|
|
203
|
+
.filter((d) => hasBackslash(d.ssl.certPath) || hasBackslash(d.ssl.keyPath))
|
|
204
|
+
.map((d) => ({
|
|
205
|
+
name: d.name,
|
|
206
|
+
certPath: d.ssl.certPath ?? '',
|
|
207
|
+
keyPath: d.ssl.keyPath ?? '',
|
|
208
|
+
}));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function hasBackslash(p) {
|
|
212
|
+
return typeof p === 'string' && p.includes('\\');
|
|
213
|
+
}
|
|
@@ -10,7 +10,174 @@
|
|
|
10
10
|
import fs from 'fs/promises';
|
|
11
11
|
import path from 'path';
|
|
12
12
|
import { loadConfig } from './config.js';
|
|
13
|
-
import { isWindows } from './platform.js';
|
|
13
|
+
import { isWindows, toNginxPath, toEscapedNginxPath } from './platform.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* How certificate paths are written into a generated config.
|
|
17
|
+
* 'forward' — C:/nginx/certs/a.pem (default everywhere; the only form on Linux)
|
|
18
|
+
* 'escaped' — "C:\\nginx\\certs\\a.pem" (Windows fallback, quoted + doubled)
|
|
19
|
+
*/
|
|
20
|
+
export const CERT_PATH_STYLES = Object.freeze({ FORWARD: 'forward', ESCAPED: 'escaped' });
|
|
21
|
+
|
|
22
|
+
/** Comment written above the SSL directives, explaining the path form in use. */
|
|
23
|
+
const CERT_PATH_COMMENTS = {
|
|
24
|
+
[CERT_PATH_STYLES.FORWARD]:
|
|
25
|
+
'# Windows: certificate paths use forward slashes (backslashes can be read as escape sequences by nginx).',
|
|
26
|
+
[CERT_PATH_STYLES.ESCAPED]:
|
|
27
|
+
'# Windows: certificate paths use escaped backslashes in quotes (nginx reads each \\\\ as one literal backslash).',
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** Path prefix Let's Encrypt fetches during an HTTP-01 challenge. */
|
|
31
|
+
export const ACME_CHALLENGE_PATH = '/.well-known/acme-challenge/';
|
|
32
|
+
|
|
33
|
+
/** Default loopback port the temporary challenge listener binds. */
|
|
34
|
+
export const DEFAULT_ACME_CHALLENGE_PORT = 8089;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Builds the location block that forwards ACME challenge requests to the
|
|
38
|
+
* temporary listener this process starts during issuance.
|
|
39
|
+
*
|
|
40
|
+
* `^~` makes it a prefix match that wins outright, so neither `location /`
|
|
41
|
+
* nor any regex location can intercept the challenge.
|
|
42
|
+
*
|
|
43
|
+
* @param {number} [port] Loopback port of the challenge listener
|
|
44
|
+
* @returns {string} Indented nginx location block
|
|
45
|
+
*/
|
|
46
|
+
export function buildChallengeLocation(port = DEFAULT_ACME_CHALLENGE_PORT) {
|
|
47
|
+
const target = `http://127.0.0.1:${Number(port) || DEFAULT_ACME_CHALLENGE_PORT}`;
|
|
48
|
+
return [
|
|
49
|
+
` # ACME HTTP-01 challenge — answered by Easy DevOps so nginx never stops.`,
|
|
50
|
+
` location ^~ ${ACME_CHALLENGE_PATH} {`,
|
|
51
|
+
` proxy_pass ${target};`,
|
|
52
|
+
` proxy_set_header Host $host;`,
|
|
53
|
+
` }`,
|
|
54
|
+
].join('\n');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Path prefix the deploy hook answers on. Shared with core/deploy/hookServer.js. */
|
|
58
|
+
export const DEPLOY_HOOK_PATH = '/.ez-deploy/';
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Builds the location block that exposes the deploy hook through a domain.
|
|
62
|
+
*
|
|
63
|
+
* The hook listener binds to loopback; this is what lets GitHub Actions reach
|
|
64
|
+
* it over the domain's own HTTPS without opening another port. Only this one
|
|
65
|
+
* prefix is forwarded — nothing else on the listener, and nothing at all on
|
|
66
|
+
* the dashboard, becomes reachable.
|
|
67
|
+
*
|
|
68
|
+
* `X-Forwarded-For` is overwritten with `$remote_addr`, not appended to: the
|
|
69
|
+
* hook rate-limits failed attempts per client, and an appended header would
|
|
70
|
+
* let a caller choose its own identity.
|
|
71
|
+
*
|
|
72
|
+
* @param {number} port Loopback port of the deploy hook listener
|
|
73
|
+
* @returns {string} Indented nginx location block
|
|
74
|
+
*/
|
|
75
|
+
export function buildDeployHookLocation(port) {
|
|
76
|
+
return [
|
|
77
|
+
` # Easy DevOps deploy hook — lets CI trigger a deployment over this domain.`,
|
|
78
|
+
` location ^~ ${DEPLOY_HOOK_PATH} {`,
|
|
79
|
+
` proxy_pass http://127.0.0.1:${Number(port)};`,
|
|
80
|
+
` proxy_set_header Host $host;`,
|
|
81
|
+
` proxy_set_header X-Forwarded-For $remote_addr;`,
|
|
82
|
+
` proxy_set_header X-Forwarded-Proto $scheme;`,
|
|
83
|
+
` client_max_body_size 16k;`,
|
|
84
|
+
` }`,
|
|
85
|
+
].join('\n');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The deploy hook port to expose on `domainName`, or null.
|
|
90
|
+
*
|
|
91
|
+
* @param {string} domainName
|
|
92
|
+
* @param {Object} config Result of loadConfig()
|
|
93
|
+
* @returns {number|null}
|
|
94
|
+
*/
|
|
95
|
+
export function deployHookPortFor(domainName, config) {
|
|
96
|
+
const hook = config?.deployHook;
|
|
97
|
+
if (!hook?.enabled || !hook.domain || hook.domain !== domainName) return null;
|
|
98
|
+
const port = Number(hook.port);
|
|
99
|
+
return Number.isInteger(port) && port > 0 && port < 65536 ? port : null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* True when a config already contains an ACME challenge location.
|
|
104
|
+
* Used to detect configs generated before this feature existed.
|
|
105
|
+
*
|
|
106
|
+
* @param {string} confText
|
|
107
|
+
* @returns {boolean}
|
|
108
|
+
*/
|
|
109
|
+
export function hasChallengeLocation(confText) {
|
|
110
|
+
if (typeof confText !== 'string') return false;
|
|
111
|
+
return new RegExp(
|
|
112
|
+
`location\\s+\\^~\\s+${ACME_CHALLENGE_PATH.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`
|
|
113
|
+
).test(confText);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Extracts the addresses a config's port-80 server blocks listen on.
|
|
118
|
+
*
|
|
119
|
+
* The preflight needs this: a config that says `listen 203.0.113.5:80` is not
|
|
120
|
+
* reachable on 127.0.0.1, and probing the wrong address would look like a
|
|
121
|
+
* broken challenge setup when nothing is actually wrong.
|
|
122
|
+
*
|
|
123
|
+
* @param {string} confText
|
|
124
|
+
* @param {number} [port] Port to filter on
|
|
125
|
+
* @returns {Array<{ host: string, port: number, wildcard: boolean }>}
|
|
126
|
+
*/
|
|
127
|
+
export function parseListenAddresses(confText, port = 80) {
|
|
128
|
+
if (typeof confText !== 'string') return [];
|
|
129
|
+
|
|
130
|
+
// Strip comments so a commented-out `# listen 80;` is never counted, then
|
|
131
|
+
// match anywhere rather than at line start — directives are not always on
|
|
132
|
+
// their own line in hand-edited configs.
|
|
133
|
+
const stripped = confText.replace(/#[^\n]*/g, '');
|
|
134
|
+
|
|
135
|
+
const found = [];
|
|
136
|
+
const re = /\blisten\s+([^;]+);/g;
|
|
137
|
+
let match;
|
|
138
|
+
|
|
139
|
+
while ((match = re.exec(stripped)) !== null) {
|
|
140
|
+
// Strip option words such as `default_server`, `ssl`, `http2`.
|
|
141
|
+
const spec = match[1].trim().split(/\s+/)[0];
|
|
142
|
+
if (!spec) continue;
|
|
143
|
+
|
|
144
|
+
let host = null;
|
|
145
|
+
let listenPort = null;
|
|
146
|
+
|
|
147
|
+
if (/^\[(.+)\]:(\d+)$/.test(spec)) {
|
|
148
|
+
const m = spec.match(/^\[(.+)\]:(\d+)$/); // [::]:80
|
|
149
|
+
host = m[1];
|
|
150
|
+
listenPort = Number(m[2]);
|
|
151
|
+
} else if (/^\d+$/.test(spec)) {
|
|
152
|
+
listenPort = Number(spec); // 80
|
|
153
|
+
host = '0.0.0.0';
|
|
154
|
+
} else if (/^([^:]+):(\d+)$/.test(spec)) {
|
|
155
|
+
const m = spec.match(/^([^:]+):(\d+)$/); // 1.2.3.4:80
|
|
156
|
+
host = m[1];
|
|
157
|
+
listenPort = Number(m[2]);
|
|
158
|
+
} else {
|
|
159
|
+
continue; // unix: sockets etc.
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (listenPort !== port) continue;
|
|
163
|
+
|
|
164
|
+
const wildcard = host === '0.0.0.0' || host === '*' || host === '::' || host === '[::]';
|
|
165
|
+
found.push({ host, port: listenPort, wildcard });
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return found;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Formats a certificate path for inclusion in a config file.
|
|
173
|
+
* @param {string} p
|
|
174
|
+
* @param {string} style One of CERT_PATH_STYLES
|
|
175
|
+
* @returns {string}
|
|
176
|
+
*/
|
|
177
|
+
function formatCertPath(p, style) {
|
|
178
|
+
if (isWindows && style === CERT_PATH_STYLES.ESCAPED) return toEscapedNginxPath(p);
|
|
179
|
+
return toNginxPath(p);
|
|
180
|
+
}
|
|
14
181
|
|
|
15
182
|
/** Returns the conf.d directory for domain config files. */
|
|
16
183
|
export function getConfDDir(nginxDir) {
|
|
@@ -160,9 +327,12 @@ export function migrateDomain(d) {
|
|
|
160
327
|
* @param {Object} domain - Domain configuration object (v2 schema)
|
|
161
328
|
* @param {string} nginxDir - Nginx directory path
|
|
162
329
|
* @param {string} sslDir - SSL certificate directory path
|
|
330
|
+
* @param {Object} [options]
|
|
331
|
+
* @param {string} [options.certPathStyle] - One of CERT_PATH_STYLES (default 'forward')
|
|
163
332
|
* @returns {string} Complete nginx conf content for the domain
|
|
164
333
|
*/
|
|
165
|
-
export function buildConf(domain, nginxDir, sslDir) {
|
|
334
|
+
export function buildConf(domain, nginxDir, sslDir, options = {}) {
|
|
335
|
+
const certPathStyle = options.certPathStyle ?? CERT_PATH_STYLES.FORWARD;
|
|
166
336
|
const {
|
|
167
337
|
name,
|
|
168
338
|
port,
|
|
@@ -207,9 +377,14 @@ export function buildConf(domain, nginxDir, sslDir) {
|
|
|
207
377
|
// Build rate limiting zone name (dots → underscores)
|
|
208
378
|
const rateLimitZone = name.replace(/\./g, '_').replace(/\*/g, 'wildcard');
|
|
209
379
|
|
|
210
|
-
// SSL cert/key paths
|
|
211
|
-
|
|
212
|
-
|
|
380
|
+
// SSL cert/key paths — fall back to the sslDir convention when unset.
|
|
381
|
+
// sslDir is normalised first so a Windows value (C:\ssl) does not produce a
|
|
382
|
+
// mixed-separator path like C:\ssl/example.com/fullchain.pem.
|
|
383
|
+
const sslBase = toNginxPath(sslDir).replace(/\/+$/, '');
|
|
384
|
+
const rawCertPath = ssl?.certPath || `${sslBase}/${name}/fullchain.pem`;
|
|
385
|
+
const rawKeyPath = ssl?.keyPath || `${sslBase}/${name}/privkey.pem`;
|
|
386
|
+
const certPath = formatCertPath(rawCertPath, certPathStyle);
|
|
387
|
+
const keyPath = formatCertPath(rawKeyPath, certPathStyle);
|
|
213
388
|
|
|
214
389
|
// Build the config sections
|
|
215
390
|
const sections = [];
|
|
@@ -225,21 +400,54 @@ export function buildConf(domain, nginxDir, sslDir) {
|
|
|
225
400
|
const mainServerNames = wildcardEnabled ? `*.${name} ${name}` : (www ? `${name} www.${name}` : name);
|
|
226
401
|
const redirectServerNames = wildcardEnabled ? `*.${name} ${name}` : (www ? `${name} www.${name}` : name);
|
|
227
402
|
|
|
403
|
+
// The ACME HTTP-01 challenge location, emitted into every port-80 server
|
|
404
|
+
// block so certificates can be issued and renewed without stopping nginx.
|
|
405
|
+
const challengeLocation = buildChallengeLocation(options.acmeChallengePort);
|
|
406
|
+
|
|
228
407
|
// ─── WWW Redirect (non-SSL, non-wildcard only) ───────────────────────────────
|
|
408
|
+
//
|
|
409
|
+
// NOTE: the redirect lives in `location /`, NOT at server level. nginx runs a
|
|
410
|
+
// server-level `return` during the rewrite phase, before location matching,
|
|
411
|
+
// so a server-level redirect would swallow the challenge request and answer
|
|
412
|
+
// 301 instead of serving the token.
|
|
229
413
|
if (www && !ssl?.enabled && !wildcardEnabled) {
|
|
230
414
|
sections.push(`server {
|
|
231
415
|
listen 80;
|
|
232
416
|
server_name www.${name};
|
|
417
|
+
${challengeLocation}
|
|
418
|
+
location / {
|
|
233
419
|
return 301 http://${name}$request_uri;
|
|
420
|
+
}
|
|
234
421
|
}`);
|
|
235
422
|
}
|
|
236
423
|
|
|
237
424
|
// ─── HTTP → HTTPS Redirect ──────────────────────────────────────────────────
|
|
425
|
+
// Same rule as above: the redirect must sit inside `location /`.
|
|
238
426
|
if (ssl?.enabled && ssl?.redirect) {
|
|
239
427
|
sections.push(`server {
|
|
240
428
|
listen 80;
|
|
241
429
|
server_name ${redirectServerNames};
|
|
430
|
+
${challengeLocation}
|
|
431
|
+
location / {
|
|
242
432
|
return 301 https://${name}$request_uri;
|
|
433
|
+
}
|
|
434
|
+
}`);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// ─── Challenge-only port 80 (SSL on, redirect off) ──────────────────────────
|
|
438
|
+
//
|
|
439
|
+
// Without this the domain has no port-80 listener at all, so an HTTP-01
|
|
440
|
+
// renewal could never reach us and every renewal would be forced into
|
|
441
|
+
// standalone mode (stopping nginx). This block serves the ACME path and
|
|
442
|
+
// nothing else — plain HTTP traffic still gets a 404, as before.
|
|
443
|
+
if (ssl?.enabled && !ssl?.redirect) {
|
|
444
|
+
sections.push(`server {
|
|
445
|
+
listen 80;
|
|
446
|
+
server_name ${redirectServerNames};
|
|
447
|
+
${challengeLocation}
|
|
448
|
+
location / {
|
|
449
|
+
return 404;
|
|
450
|
+
}
|
|
243
451
|
}`);
|
|
244
452
|
}
|
|
245
453
|
|
|
@@ -253,9 +461,18 @@ export function buildConf(domain, nginxDir, sslDir) {
|
|
|
253
461
|
mainBlock.push(` listen ${listenPort};`);
|
|
254
462
|
mainBlock.push(` server_name ${serverNames};`);
|
|
255
463
|
|
|
464
|
+
// When this block is the port-80 listener (SSL off) it must answer the ACME
|
|
465
|
+
// challenge too. `location ^~` outranks the `location /` proxy below.
|
|
466
|
+
if (!ssl?.enabled) {
|
|
467
|
+
mainBlock.push(``);
|
|
468
|
+
mainBlock.push(challengeLocation);
|
|
469
|
+
}
|
|
470
|
+
|
|
256
471
|
// SSL configuration
|
|
257
472
|
if (ssl?.enabled) {
|
|
258
473
|
mainBlock.push(``);
|
|
474
|
+
// The comment is Windows-only: on Linux the path form never varies.
|
|
475
|
+
if (isWindows) mainBlock.push(` ${CERT_PATH_COMMENTS[certPathStyle]}`);
|
|
259
476
|
mainBlock.push(` ssl_certificate ${certPath};`);
|
|
260
477
|
mainBlock.push(` ssl_certificate_key ${keyPath};`);
|
|
261
478
|
}
|
|
@@ -281,6 +498,14 @@ export function buildConf(domain, nginxDir, sslDir) {
|
|
|
281
498
|
mainBlock.push(` access_log ${logDir}/${name}.access.log;`);
|
|
282
499
|
}
|
|
283
500
|
|
|
501
|
+
// The deploy hook, when this domain is the one chosen to expose it. It goes
|
|
502
|
+
// in the block that actually serves the site (443 with SSL on, 80 without),
|
|
503
|
+
// so the hook is reached over HTTPS whenever the domain has a certificate.
|
|
504
|
+
if (options.deployHookPort) {
|
|
505
|
+
mainBlock.push(``);
|
|
506
|
+
mainBlock.push(buildDeployHookLocation(options.deployHookPort));
|
|
507
|
+
}
|
|
508
|
+
|
|
284
509
|
// ─── Location Block ─────────────────────────────────────────────────────────
|
|
285
510
|
mainBlock.push(``);
|
|
286
511
|
mainBlock.push(` location / {`);
|
|
@@ -367,11 +592,16 @@ export function buildConf(domain, nginxDir, sslDir) {
|
|
|
367
592
|
* @param {Object} domain - Domain configuration object
|
|
368
593
|
* @returns {Promise<string>} Path to the generated conf file
|
|
369
594
|
*/
|
|
370
|
-
export async function generateConf(domain) {
|
|
371
|
-
const
|
|
595
|
+
export async function generateConf(domain, options = {}) {
|
|
596
|
+
const config = loadConfig();
|
|
597
|
+
const { nginxDir, sslDir, acmeChallengePort } = config;
|
|
372
598
|
const confDir = getConfDDir(nginxDir);
|
|
373
599
|
const confPath = path.join(confDir, `${domain.name}.conf`);
|
|
374
|
-
const confContent = buildConf(domain, nginxDir, sslDir
|
|
600
|
+
const confContent = buildConf(domain, nginxDir, sslDir, {
|
|
601
|
+
acmeChallengePort,
|
|
602
|
+
deployHookPort: deployHookPortFor(domain.name, config),
|
|
603
|
+
...options,
|
|
604
|
+
});
|
|
375
605
|
|
|
376
606
|
// Ensure conf.d directory exists
|
|
377
607
|
await fs.mkdir(confDir, { recursive: true });
|
|
@@ -388,21 +618,27 @@ export async function generateConf(domain) {
|
|
|
388
618
|
}
|
|
389
619
|
|
|
390
620
|
/**
|
|
391
|
-
* Generates
|
|
621
|
+
* Generates the default SSL certificate paths for a domain.
|
|
622
|
+
*
|
|
623
|
+
* Always returns forward slashes on Windows, matching what is written into the
|
|
624
|
+
* config. The sslDir argument is honoured on every platform: certificates are
|
|
625
|
+
* actually issued into `path.join(sslDir, domainName)`, so suggesting a
|
|
626
|
+
* different fixed directory pointed users at files that were never there.
|
|
627
|
+
*
|
|
392
628
|
* @param {string} domainName - Domain name
|
|
393
629
|
* @param {string} platform - 'win32' or 'linux'
|
|
394
630
|
* @param {string} sslDir - SSL directory from config
|
|
395
631
|
* @returns {Object} { certPath, keyPath }
|
|
396
632
|
*/
|
|
397
633
|
export function getDefaultCertPaths(domainName, platform, sslDir) {
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
634
|
+
const fallback = platform === 'win32' ? 'C:\\easy-devops\\ssl' : '/etc/easy-devops/ssl';
|
|
635
|
+
const base = String(sslDir || fallback)
|
|
636
|
+
.replace(/\\/g, '/')
|
|
637
|
+
.replace(/\/{2,}/g, '/')
|
|
638
|
+
.replace(/\/+$/, '');
|
|
639
|
+
|
|
404
640
|
return {
|
|
405
|
-
certPath: `${
|
|
406
|
-
keyPath: `${
|
|
641
|
+
certPath: `${base}/${domainName}/fullchain.pem`,
|
|
642
|
+
keyPath: `${base}/${domainName}/privkey.pem`,
|
|
407
643
|
};
|
|
408
644
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import fs from 'fs/promises';
|
|
2
2
|
import path from 'path';
|
|
3
|
-
import { run } from '
|
|
4
|
-
import { loadConfig } from '
|
|
5
|
-
import { ensureNginxInclude, getConfDDir } from '
|
|
6
|
-
import { isWindows, getNginxExe, nginxTestCmd, combineOutput, isNginxTestOk } from '
|
|
3
|
+
import { run } from './shell.js';
|
|
4
|
+
import { loadConfig } from './config.js';
|
|
5
|
+
import { ensureNginxInclude, getConfDDir } from './nginx-conf-generator.js';
|
|
6
|
+
import { isWindows, getNginxExe, nginxTestCmd, combineOutput, isNginxTestOk, nginxLogPath } from './platform.js';
|
|
7
7
|
|
|
8
8
|
// ─── Error Types ──────────────────────────────────────────────────────────────
|
|
9
9
|
|
|
@@ -197,9 +197,7 @@ export async function start() {
|
|
|
197
197
|
}
|
|
198
198
|
|
|
199
199
|
// Didn't start — surface error log
|
|
200
|
-
const logPath =
|
|
201
|
-
? path.join(nginxDir, 'logs', 'error.log')
|
|
202
|
-
: '/var/log/nginx/error.log';
|
|
200
|
+
const logPath = nginxLogPath(nginxDir, 'error');
|
|
203
201
|
const logCmd = isWindows
|
|
204
202
|
? `Get-Content -Tail 20 "${logPath}" -ErrorAction SilentlyContinue`
|
|
205
203
|
: `tail -n 20 "${logPath}" 2>/dev/null`;
|
|
@@ -306,9 +304,20 @@ export async function saveConfig(filename, content) {
|
|
|
306
304
|
|
|
307
305
|
// ─── US5: Logs ────────────────────────────────────────────────────────────────
|
|
308
306
|
|
|
309
|
-
|
|
307
|
+
/**
|
|
308
|
+
* Reads the tail of an nginx log.
|
|
309
|
+
*
|
|
310
|
+
* `which` is additive: the previous signature took only a line count and
|
|
311
|
+
* always read the error log, so every existing caller keeps working. The
|
|
312
|
+
* access log was unreachable before, which is what the UI needed.
|
|
313
|
+
*
|
|
314
|
+
* @param {number} [lines]
|
|
315
|
+
* @param {'error'|'access'} [which]
|
|
316
|
+
* @returns {Promise<{ lines: string[] }>}
|
|
317
|
+
*/
|
|
318
|
+
export async function getLogs(lines = 100, which = 'error') {
|
|
310
319
|
const nginxDir = getNginxDir();
|
|
311
|
-
const logPath =
|
|
320
|
+
const logPath = nginxLogPath(nginxDir, which === 'access' ? 'access' : 'error');
|
|
312
321
|
let content;
|
|
313
322
|
try {
|
|
314
323
|
content = await fs.readFile(logPath, 'utf8');
|