@anhlp/alp-cli 0.0.1-beta.4 → 0.0.1-beta.6
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/dist/THIRD-PARTY-NOTICES.txt +26 -1
- package/dist/acp/main.js +1 -1
- package/dist/acp.js +267 -92
- package/dist/alp-web/THIRD-PARTY-NOTICES.txt +666 -0
- package/dist/alp-web/alp-boundary.json +1 -0
- package/dist/alp-web/assets/TerminalPanel-CqTsgY-W.js +22 -0
- package/dist/alp-web/assets/highlighter-BnicKQ_d.js +18 -0
- package/dist/alp-web/assets/index-BMbDUCHW.css +1 -0
- package/dist/alp-web/assets/index-B_RFaj2C.js +59 -0
- package/dist/alp-web/index.html +2 -2
- package/dist/alp-web.js +666 -187
- package/dist/alpd.js +10768 -2592
- package/dist/backup.js +1 -1
- package/dist/build-manifest.json +22 -2
- package/dist/cli.js +32 -3
- package/dist/daemon.d.ts +1 -1
- package/dist/daemon.js +5 -0
- package/dist/interfaces.d.ts +13 -1
- package/dist/interfaces.js +96 -11
- package/dist/paths.d.ts +1 -0
- package/dist/paths.js +2 -1
- package/dist/roles.d.ts +15 -0
- package/dist/roles.js +18 -0
- package/dist/self-update.d.ts +152 -0
- package/dist/self-update.js +390 -0
- package/dist/sessions.d.ts +4 -0
- package/dist/sessions.js +22 -1
- package/dist/tasks.js +22 -8
- package/dist/terminal-native/README.md +201 -0
- package/dist/terminal-native/prebuilt/darwin-arm64/manifest.json +11 -0
- package/dist/terminal-native/prebuilt/darwin-arm64/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/darwin-x64/manifest.json +11 -0
- package/dist/terminal-native/prebuilt/darwin-x64/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-arm64-musl/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-x64-glibc/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-x64-musl/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-x64-musl/terminal-owner +0 -0
- package/dist/terminal-native/terminal-owner.c +389 -0
- package/dist/terminal.d.ts +5 -0
- package/dist/terminal.js +4 -3
- package/dist/usage.d.ts +1 -1
- package/dist/usage.js +14 -5
- package/dist/web-service.d.ts +22 -4
- package/dist/web-service.js +33 -9
- package/package.json +6 -5
- package/src/cli.js +38 -4
- package/src/daemon.js +3 -0
- package/src/interfaces.js +102 -12
- package/src/paths.js +3 -1
- package/src/roles.js +28 -0
- package/src/self-update.js +406 -0
- package/src/sessions.js +23 -2
- package/src/tasks.js +22 -8
- package/src/terminal.js +4 -3
- package/src/usage.js +14 -5
- package/src/web-service.js +58 -14
- package/dist/alp-web/assets/TerminalPanel-PrU2l3u5.js +0 -16
- package/dist/alp-web/assets/index-BJdBTDFX.css +0 -1
- package/dist/alp-web/assets/index-DpfiPv5s.js +0 -52
- package/src/web/grant.js +0 -204
- package/src/web/main.js +0 -285
- package/src/web/serve.js +0 -144
package/src/web-service.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { execFile } from 'node:child_process';
|
|
2
2
|
import { existsSync } from 'node:fs';
|
|
3
3
|
import { lstat, readFile } from 'node:fs/promises';
|
|
4
|
+
import os from 'node:os';
|
|
4
5
|
import path from 'node:path';
|
|
5
6
|
import { fileURLToPath } from 'node:url';
|
|
6
7
|
import { promisify } from 'node:util';
|
|
7
|
-
import { bootTime, readLock } from '@anhlp/alp-client/index';
|
|
8
|
+
import { bootTime, isSameBoot, readLock } from '@anhlp/alp-client/index';
|
|
8
9
|
import { distributionEntry } from './paths.js';
|
|
10
|
+
import { isPackaged, selfEntry } from '@anhlp/alp-core/packaged';
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* The CLI's side of alp-web (packages/cli/src/web), the service behind `alp web`: where it is, which
|
|
@@ -23,16 +25,20 @@ const execFileAsync = promisify(execFile);
|
|
|
23
25
|
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
|
|
24
26
|
|
|
25
27
|
/** The alp-web service script beside this CLI; a clear error when this CLI was built without it. */
|
|
26
|
-
export function
|
|
28
|
+
export function alpWebScript() {
|
|
27
29
|
const entry = fileURLToPath(distributionEntry('alp-web.js'));
|
|
28
|
-
|
|
30
|
+
// The ALP executable carries alp-web inside itself.
|
|
31
|
+
if (!isPackaged() && !existsSync(entry)) throw new Error(`alp web is not built: ${entry} is missing. Build the CLI (npm run build in the ALP checkout) or reinstall ALP.`);
|
|
29
32
|
return entry;
|
|
30
33
|
}
|
|
31
34
|
|
|
35
|
+
/** What starts alp-web: its script under Node, or the ALP executable (`alp web serve`). */
|
|
36
|
+
export const alpWebEntry = () => selfEntry(alpWebScript());
|
|
37
|
+
|
|
32
38
|
const readJson = async file => JSON.parse(await readFile(file, 'utf8'));
|
|
33
39
|
|
|
34
|
-
/** When `pid` started
|
|
35
|
-
async function
|
|
40
|
+
/** When `pid` started and elapsed seconds from ps, or undefined when ps cannot say. @param {number} pid */
|
|
41
|
+
async function processTimes(pid) {
|
|
36
42
|
try {
|
|
37
43
|
const { stdout } = await execFileAsync('ps', ['-o', 'etime=', '-p', String(pid)], { encoding: 'utf8', timeout: 5_000 });
|
|
38
44
|
// [[dd-]hh:]mm:ss
|
|
@@ -40,7 +46,7 @@ async function processStart(pid) {
|
|
|
40
46
|
if (!match) return undefined;
|
|
41
47
|
const [, days = '0', hours = '0', minutes, seconds] = match;
|
|
42
48
|
const elapsed = ((Number(days) * 24 + Number(hours)) * 60 + Number(minutes)) * 60 + Number(seconds);
|
|
43
|
-
return Date.now() - elapsed * 1000;
|
|
49
|
+
return { started: Date.now() - elapsed * 1000, elapsed };
|
|
44
50
|
} catch { return undefined; }
|
|
45
51
|
}
|
|
46
52
|
|
|
@@ -50,9 +56,9 @@ async function processStart(pid) {
|
|
|
50
56
|
* `foreign` (proven not to be the integration: another user's, another boot's, or a reused pid) or
|
|
51
57
|
* `unknown` (it cannot be proven either way, for example without ps or on Windows).
|
|
52
58
|
*
|
|
53
|
-
* The reuse proof compares the lock's
|
|
54
|
-
*
|
|
55
|
-
*
|
|
59
|
+
* The reuse proof compares the lock's creation time with the process's start from ps: when
|
|
60
|
+
* `startedUptime` is recorded, monotonic uptime comparison prevents clock step vulnerabilities.
|
|
61
|
+
* For legacy locks, wall-clock `startedAt` is compared.
|
|
56
62
|
* @param {string} home
|
|
57
63
|
* @returns {Promise<{ pid: number, app: unknown } | { reason: string, alive: boolean, kind?: 'transient' | 'foreign' | 'unknown' }>}
|
|
58
64
|
*/
|
|
@@ -74,12 +80,16 @@ export async function webOwner(home) {
|
|
|
74
80
|
if (process.platform === 'win32') return refuse('alp does not stop it on Windows', 'unknown');
|
|
75
81
|
const uid = process.getuid?.();
|
|
76
82
|
if (lockInfo.uid !== uid) return refuse('its lock is not yours', 'foreign');
|
|
77
|
-
if (
|
|
83
|
+
if (!isSameBoot(lock)) return refuse('its lock is from another boot', 'foreign');
|
|
78
84
|
if (pid === process.pid || pid === (await readLock(home).catch(() => undefined))?.pid) return refuse(`pid ${pid} is not an integration`, 'foreign');
|
|
79
|
-
const
|
|
80
|
-
if (
|
|
81
|
-
//
|
|
82
|
-
|
|
85
|
+
const times = await processTimes(pid);
|
|
86
|
+
if (times === undefined) return refuse(`the start time of pid ${pid} is unknown`, 'unknown');
|
|
87
|
+
// When startedUptime is recorded, monotonic uptime comparison prevents clock step vulnerabilities.
|
|
88
|
+
// Otherwise, fallback to wall-clock startedAt comparison with 2s tolerance for ps resolution.
|
|
89
|
+
const isReused = typeof lock.startedUptime === 'number'
|
|
90
|
+
? (Math.floor(os.uptime()) - times.elapsed) > lock.startedUptime + 2
|
|
91
|
+
: times.started > Date.parse(lock.startedAt) + 2_000;
|
|
92
|
+
if (isReused) return refuse(`pid ${pid} started after the lock was written (a reused pid)`, 'foreign');
|
|
83
93
|
// The lock's owner; web.json must name it too (an owner still starting has not written it yet).
|
|
84
94
|
let info, infoStat;
|
|
85
95
|
try { infoStat = await lstat(infoFile); info = await readJson(infoFile); } catch { return refuse(`${infoFile} is missing or unreadable`, 'transient'); }
|
|
@@ -141,3 +151,37 @@ export async function stopWebService(home, timeoutMs = 30_000) {
|
|
|
141
151
|
export function refusal(home) {
|
|
142
152
|
return `${path.join(home, 'integrations', 'web.lock')} is held by a process that alp cannot prove is alp web or the Paseo integration from before its rename, so it was left running`;
|
|
143
153
|
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Probes whether the published web service URL is live and answering requests.
|
|
157
|
+
* Accepts only loopback HTTP/HTTPS addresses.
|
|
158
|
+
* @param {string | undefined} url
|
|
159
|
+
* @param {number} [timeoutMs]
|
|
160
|
+
*/
|
|
161
|
+
export async function isWebReady(url, timeoutMs = 1_000) {
|
|
162
|
+
if (typeof url !== 'string' || !url) return false;
|
|
163
|
+
try {
|
|
164
|
+
const parsed = new URL(url);
|
|
165
|
+
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return false;
|
|
166
|
+
if (!['127.0.0.1', 'localhost', '::1', '[::1]'].includes(parsed.hostname)) return false;
|
|
167
|
+
const res = await fetch(parsed.href, { signal: AbortSignal.timeout(timeoutMs) });
|
|
168
|
+
return typeof res.status === 'number' && res.status > 0;
|
|
169
|
+
} catch {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Whether web metadata is genuinely claimed by a live alp-web process.
|
|
176
|
+
* If the process is proven foreign (e.g. a reused PID or from another boot) or
|
|
177
|
+
* the URL does not answer HTTP requests, it is never claimed.
|
|
178
|
+
* @param {any} published
|
|
179
|
+
* @param {number | undefined} lockPid
|
|
180
|
+
* @param {any} held
|
|
181
|
+
* @param {number} [timeoutMs]
|
|
182
|
+
*/
|
|
183
|
+
export async function isWebClaimed(published, lockPid, held, timeoutMs = 1_000) {
|
|
184
|
+
if (published?.app !== 'alp-web' || published.pid !== lockPid) return false;
|
|
185
|
+
if (held?.kind === 'foreign') return false;
|
|
186
|
+
return isWebReady(published.url, timeoutMs);
|
|
187
|
+
}
|