@anhlp/alp-cli 0.0.1-beta.4 → 0.0.1-beta.7

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.
Files changed (66) hide show
  1. package/dist/THIRD-PARTY-NOTICES.txt +26 -1
  2. package/dist/acp/main.js +1 -1
  3. package/dist/acp.js +152 -33
  4. package/dist/alp-web/THIRD-PARTY-NOTICES.txt +666 -0
  5. package/dist/alp-web/alp-boundary.json +1 -0
  6. package/dist/alp-web/assets/TerminalPanel-BeMws9CP.js +22 -0
  7. package/dist/alp-web/assets/highlighter-BnicKQ_d.js +18 -0
  8. package/dist/alp-web/assets/index-Dl7motn1.js +59 -0
  9. package/dist/alp-web/assets/index-DwqDSiIi.css +1 -0
  10. package/dist/alp-web/index.html +2 -2
  11. package/dist/alp-web.js +529 -172
  12. package/dist/alpd.js +10623 -2570
  13. package/dist/backup.js +1 -1
  14. package/dist/build-manifest.json +22 -2
  15. package/dist/cli.js +28 -3
  16. package/dist/daemon.d.ts +1 -1
  17. package/dist/daemon.js +5 -0
  18. package/dist/interfaces.d.ts +13 -1
  19. package/dist/interfaces.js +95 -11
  20. package/dist/paths.d.ts +1 -0
  21. package/dist/paths.js +2 -1
  22. package/dist/roles.d.ts +15 -0
  23. package/dist/roles.js +16 -0
  24. package/dist/self-update.d.ts +149 -0
  25. package/dist/self-update.js +399 -0
  26. package/dist/sessions.d.ts +4 -0
  27. package/dist/sessions.js +22 -1
  28. package/dist/tasks.js +22 -8
  29. package/dist/terminal-native/README.md +201 -0
  30. package/dist/terminal-native/prebuilt/darwin-arm64/manifest.json +11 -0
  31. package/dist/terminal-native/prebuilt/darwin-arm64/terminal-owner +0 -0
  32. package/dist/terminal-native/prebuilt/darwin-x64/manifest.json +11 -0
  33. package/dist/terminal-native/prebuilt/darwin-x64/terminal-owner +0 -0
  34. package/dist/terminal-native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
  35. package/dist/terminal-native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
  36. package/dist/terminal-native/prebuilt/linux-arm64-musl/manifest.json +10 -0
  37. package/dist/terminal-native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
  38. package/dist/terminal-native/prebuilt/linux-x64-glibc/manifest.json +10 -0
  39. package/dist/terminal-native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
  40. package/dist/terminal-native/prebuilt/linux-x64-musl/manifest.json +10 -0
  41. package/dist/terminal-native/prebuilt/linux-x64-musl/terminal-owner +0 -0
  42. package/dist/terminal-native/terminal-owner.c +389 -0
  43. package/dist/terminal.d.ts +5 -0
  44. package/dist/terminal.js +4 -3
  45. package/dist/usage.d.ts +1 -1
  46. package/dist/usage.js +14 -5
  47. package/dist/web-service.d.ts +22 -4
  48. package/dist/web-service.js +32 -8
  49. package/package.json +6 -5
  50. package/src/cli.js +35 -4
  51. package/src/daemon.js +3 -0
  52. package/src/interfaces.js +101 -12
  53. package/src/paths.js +3 -1
  54. package/src/roles.js +26 -0
  55. package/src/self-update.js +406 -0
  56. package/src/sessions.js +23 -2
  57. package/src/tasks.js +22 -8
  58. package/src/terminal.js +4 -3
  59. package/src/usage.js +14 -5
  60. package/src/web-service.js +56 -13
  61. package/dist/alp-web/assets/TerminalPanel-PrU2l3u5.js +0 -16
  62. package/dist/alp-web/assets/index-BJdBTDFX.css +0 -1
  63. package/dist/alp-web/assets/index-DpfiPv5s.js +0 -52
  64. package/src/web/grant.js +0 -204
  65. package/src/web/main.js +0 -285
  66. package/src/web/serve.js +0 -144
@@ -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 { 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,19 @@ 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 alpWebEntry() {
28
+ export function alpWebScript() {
27
29
  const entry = fileURLToPath(distributionEntry('alp-web.js'));
28
30
  if (!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
31
  return entry;
30
32
  }
31
33
 
34
+ /** What starts alp-web: its script under Node (named through app/current in the ALP bundle). */
35
+ export const alpWebEntry = () => selfEntry(alpWebScript());
36
+
32
37
  const readJson = async file => JSON.parse(await readFile(file, 'utf8'));
33
38
 
34
- /** When `pid` started, in ms since the epoch (to about a second), or undefined when ps cannot say. @param {number} pid */
35
- async function processStart(pid) {
39
+ /** When `pid` started and elapsed seconds from ps, or undefined when ps cannot say. @param {number} pid */
40
+ async function processTimes(pid) {
36
41
  try {
37
42
  const { stdout } = await execFileAsync('ps', ['-o', 'etime=', '-p', String(pid)], { encoding: 'utf8', timeout: 5_000 });
38
43
  // [[dd-]hh:]mm:ss
@@ -40,7 +45,7 @@ async function processStart(pid) {
40
45
  if (!match) return undefined;
41
46
  const [, days = '0', hours = '0', minutes, seconds] = match;
42
47
  const elapsed = ((Number(days) * 24 + Number(hours)) * 60 + Number(minutes)) * 60 + Number(seconds);
43
- return Date.now() - elapsed * 1000;
48
+ return { started: Date.now() - elapsed * 1000, elapsed };
44
49
  } catch { return undefined; }
45
50
  }
46
51
 
@@ -50,9 +55,9 @@ async function processStart(pid) {
50
55
  * `foreign` (proven not to be the integration: another user's, another boot's, or a reused pid) or
51
56
  * `unknown` (it cannot be proven either way, for example without ps or on Windows).
52
57
  *
53
- * The reuse proof compares the lock's wall-clock `startedAt` with the process's start from ps: a
54
- * wall clock stepped back by more than two seconds after the lock was written could make a reused
55
- * pid look older than the lock. A step forward only refuses.
58
+ * The reuse proof compares the lock's creation time with the process's start from ps: when
59
+ * `startedUptime` is recorded, monotonic uptime comparison prevents clock step vulnerabilities.
60
+ * For legacy locks, wall-clock `startedAt` is compared.
56
61
  * @param {string} home
57
62
  * @returns {Promise<{ pid: number, app: unknown } | { reason: string, alive: boolean, kind?: 'transient' | 'foreign' | 'unknown' }>}
58
63
  */
@@ -74,12 +79,16 @@ export async function webOwner(home) {
74
79
  if (process.platform === 'win32') return refuse('alp does not stop it on Windows', 'unknown');
75
80
  const uid = process.getuid?.();
76
81
  if (lockInfo.uid !== uid) return refuse('its lock is not yours', 'foreign');
77
- if (Math.abs(lock.bootTime - bootTime()) > 30) return refuse('its lock is from another boot', 'foreign');
82
+ if (!isSameBoot(lock)) return refuse('its lock is from another boot', 'foreign');
78
83
  if (pid === process.pid || pid === (await readLock(home).catch(() => undefined))?.pid) return refuse(`pid ${pid} is not an integration`, 'foreign');
79
- const started = await processStart(pid);
80
- if (started === undefined) return refuse(`the start time of pid ${pid} is unknown`, 'unknown');
81
- // ps rounds the elapsed time down, so `started` may be up to a second late; allow two.
82
- if (started > Date.parse(lock.startedAt) + 2_000) return refuse(`pid ${pid} started after the lock was written (a reused pid)`, 'foreign');
84
+ const times = await processTimes(pid);
85
+ if (times === undefined) return refuse(`the start time of pid ${pid} is unknown`, 'unknown');
86
+ // When startedUptime is recorded, monotonic uptime comparison prevents clock step vulnerabilities.
87
+ // Otherwise, fallback to wall-clock startedAt comparison with 2s tolerance for ps resolution.
88
+ const isReused = typeof lock.startedUptime === 'number'
89
+ ? (Math.floor(os.uptime()) - times.elapsed) > lock.startedUptime + 2
90
+ : times.started > Date.parse(lock.startedAt) + 2_000;
91
+ if (isReused) return refuse(`pid ${pid} started after the lock was written (a reused pid)`, 'foreign');
83
92
  // The lock's owner; web.json must name it too (an owner still starting has not written it yet).
84
93
  let info, infoStat;
85
94
  try { infoStat = await lstat(infoFile); info = await readJson(infoFile); } catch { return refuse(`${infoFile} is missing or unreadable`, 'transient'); }
@@ -141,3 +150,37 @@ export async function stopWebService(home, timeoutMs = 30_000) {
141
150
  export function refusal(home) {
142
151
  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
152
  }
153
+
154
+ /**
155
+ * Probes whether the published web service URL is live and answering requests.
156
+ * Accepts only loopback HTTP/HTTPS addresses.
157
+ * @param {string | undefined} url
158
+ * @param {number} [timeoutMs]
159
+ */
160
+ export async function isWebReady(url, timeoutMs = 1_000) {
161
+ if (typeof url !== 'string' || !url) return false;
162
+ try {
163
+ const parsed = new URL(url);
164
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return false;
165
+ if (!['127.0.0.1', 'localhost', '::1', '[::1]'].includes(parsed.hostname)) return false;
166
+ const res = await fetch(parsed.href, { signal: AbortSignal.timeout(timeoutMs) });
167
+ return typeof res.status === 'number' && res.status > 0;
168
+ } catch {
169
+ return false;
170
+ }
171
+ }
172
+
173
+ /**
174
+ * Whether web metadata is genuinely claimed by a live alp-web process.
175
+ * If the process is proven foreign (e.g. a reused PID or from another boot) or
176
+ * the URL does not answer HTTP requests, it is never claimed.
177
+ * @param {any} published
178
+ * @param {number | undefined} lockPid
179
+ * @param {any} held
180
+ * @param {number} [timeoutMs]
181
+ */
182
+ export async function isWebClaimed(published, lockPid, held, timeoutMs = 1_000) {
183
+ if (published?.app !== 'alp-web' || published.pid !== lockPid) return false;
184
+ if (held?.kind === 'foreign') return false;
185
+ return isWebReady(published.url, timeoutMs);
186
+ }