dsh-remote-go 0.1.0 → 0.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.
Files changed (133) hide show
  1. package/README.md +64 -95
  2. package/{client.js → dist/client.js} +7805 -5486
  3. package/dist/index.js +4 -0
  4. package/dist/src/admission.js +46 -0
  5. package/dist/src/api.js +442 -0
  6. package/dist/src/atomic-json.js +235 -0
  7. package/dist/src/bootstrap.js +106 -0
  8. package/dist/src/connect/attach.js +43 -0
  9. package/dist/src/connect/bootstrap.js +14 -0
  10. package/dist/src/connect/failures.js +44 -0
  11. package/dist/src/connect/guards.js +31 -0
  12. package/dist/src/connect/launch.js +80 -0
  13. package/dist/src/connect/links.js +70 -0
  14. package/dist/src/connect/ownership.js +220 -0
  15. package/dist/src/connect/pipeline.js +473 -0
  16. package/dist/src/connect/reattach.js +215 -0
  17. package/dist/src/connect/registry.js +11 -0
  18. package/dist/src/connect/repair.js +126 -0
  19. package/dist/src/connect/restart.js +316 -0
  20. package/dist/src/connect/stop.js +198 -0
  21. package/dist/src/connect/transport.js +109 -0
  22. package/dist/src/connect/verify.js +39 -0
  23. package/dist/src/connect/watchdog.js +76 -0
  24. package/dist/src/connect-log.js +162 -0
  25. package/dist/src/connect-progress.js +142 -0
  26. package/dist/src/constants.js +67 -0
  27. package/dist/src/context.js +117 -0
  28. package/dist/src/diagnostics.js +102 -0
  29. package/dist/src/download-jobs.js +230 -0
  30. package/dist/src/errors.js +243 -0
  31. package/dist/src/events.js +390 -0
  32. package/dist/src/host-lifecycle.js +504 -0
  33. package/dist/src/host-registry.js +232 -0
  34. package/dist/src/host-runtime.js +86 -0
  35. package/dist/src/host-view.js +75 -0
  36. package/dist/src/index.js +216 -0
  37. package/dist/src/kernel-adapter.js +434 -0
  38. package/dist/src/key-files.js +52 -0
  39. package/dist/src/known-hosts.js +125 -0
  40. package/dist/src/layout.js +43 -0
  41. package/dist/src/local-download.js +380 -0
  42. package/dist/src/local-ssh.js +30 -0
  43. package/dist/src/local-version.js +101 -0
  44. package/dist/src/node/download.js +160 -0
  45. package/dist/src/node-runtime.js +346 -0
  46. package/dist/src/probe.js +96 -0
  47. package/dist/src/prompts.js +270 -0
  48. package/dist/src/proxy/cookies.js +136 -0
  49. package/dist/src/proxy/headers.js +262 -0
  50. package/dist/src/proxy/http.js +142 -0
  51. package/dist/src/proxy/index.js +135 -0
  52. package/dist/src/proxy/limits.js +8 -0
  53. package/dist/src/proxy/routes.js +39 -0
  54. package/dist/src/proxy/upgrade.js +175 -0
  55. package/dist/src/redact.js +104 -0
  56. package/dist/src/remote-files.js +72 -0
  57. package/dist/src/remote-path.js +123 -0
  58. package/dist/src/remote-proc.js +339 -0
  59. package/dist/src/remote-web/registry.js +147 -0
  60. package/dist/src/remote-web/resolve.js +310 -0
  61. package/dist/src/remote-web/start.js +227 -0
  62. package/dist/src/remote-web/stop.js +93 -0
  63. package/dist/src/request-trust.js +87 -0
  64. package/dist/src/runtime/dispatch.js +310 -0
  65. package/dist/src/runtime/effects.js +91 -0
  66. package/dist/src/runtime/events.js +35 -0
  67. package/dist/src/runtime/fsm.js +120 -0
  68. package/dist/src/runtime/rules.js +565 -0
  69. package/dist/src/secrets.js +142 -0
  70. package/dist/src/service.js +406 -0
  71. package/dist/src/settings.js +144 -0
  72. package/dist/src/ssh-config.js +276 -0
  73. package/dist/src/ssh-failures.js +122 -0
  74. package/dist/src/ssh-port.js +4 -0
  75. package/dist/src/ssh-sftp.js +297 -0
  76. package/dist/src/ssh.js +477 -0
  77. package/dist/src/store.js +191 -0
  78. package/dist/src/tool-shared.js +181 -0
  79. package/dist/src/tools-exec-policy.js +167 -0
  80. package/dist/src/tools-exec.js +371 -0
  81. package/dist/src/tools-report.js +424 -0
  82. package/dist/src/tools.js +411 -0
  83. package/dist/src/tunnel.js +129 -0
  84. package/dist/src/util.js +149 -0
  85. package/dist/src/version.js +114 -0
  86. package/dist/src/watchdog-bus.js +29 -0
  87. package/dist/src/watchdog.js +156 -0
  88. package/index.js +5 -4
  89. package/locale/en.json +2 -2
  90. package/locale/zh.json +2 -2
  91. package/package.json +40 -21
  92. package/vendor/THIRD_PARTY_NOTICES.md +27 -1
  93. package/src/api.js +0 -340
  94. package/src/atomic-json.js +0 -208
  95. package/src/bootstrap.js +0 -100
  96. package/src/connect-log.js +0 -39
  97. package/src/connect-pipeline.js +0 -463
  98. package/src/connect-progress.js +0 -135
  99. package/src/constants.js +0 -31
  100. package/src/diagnostics.js +0 -157
  101. package/src/download-jobs.js +0 -217
  102. package/src/errors.js +0 -316
  103. package/src/host-lifecycle.js +0 -187
  104. package/src/host-registry.js +0 -145
  105. package/src/host-runtime.js +0 -53
  106. package/src/host-view.js +0 -51
  107. package/src/index.js +0 -162
  108. package/src/key-files.js +0 -49
  109. package/src/known-hosts.js +0 -94
  110. package/src/layout.js +0 -35
  111. package/src/local-download.js +0 -162
  112. package/src/local-ssh.js +0 -27
  113. package/src/local-version.js +0 -88
  114. package/src/node-runtime.js +0 -521
  115. package/src/probe.js +0 -89
  116. package/src/prompts.js +0 -132
  117. package/src/redact.js +0 -54
  118. package/src/remote-files.js +0 -71
  119. package/src/remote-path.js +0 -73
  120. package/src/remote-proc.js +0 -208
  121. package/src/remote-web.js +0 -300
  122. package/src/secrets.js +0 -79
  123. package/src/service.js +0 -250
  124. package/src/settings.js +0 -135
  125. package/src/ssh-config.js +0 -271
  126. package/src/ssh.js +0 -652
  127. package/src/store.js +0 -143
  128. package/src/tunnel.js +0 -73
  129. package/src/util.js +0 -153
  130. package/src/version.js +0 -103
  131. package/src/webapp.js +0 -28
  132. package/vendor/ssh2-entry.mjs +0 -12
  133. /package/{vendor → dist/vendor}/ssh2.bundle.cjs +0 -0
@@ -0,0 +1,220 @@
1
+ import { commandMatchesProfile } from '../remote-web/resolve.js';
2
+ import { isPid } from '../util.js';
3
+ import { readRegistryQuietly } from './registry.js';
4
+ /**
5
+ * Record a pid this connection is allowed to stop later: it must have been launched by the
6
+ * plugin's own start script (or adopted from a `launch` registry record) and its `/proc`
7
+ * identity must show this DSH home and profile. Anything that fails this check is still
8
+ * hosted and displayed, but never signalled.
9
+ */
10
+ export async function rememberLaunched(ctx, runtime, pid, layout, profile) {
11
+ if (!isPid(pid))
12
+ return null;
13
+ if (!(runtime.launched instanceof Map))
14
+ runtime.launched = new Map();
15
+ const identity = await ctx.deps.processIdentity(runtime.ssh, pid);
16
+ if (identity.ok !== true) {
17
+ ctx.progress.log(runtime, 'debug', `launch: pid ${pid} identity unreadable (${identity.reason}); it will not be stopped`);
18
+ return null;
19
+ }
20
+ if (identity.dshHome !== '' && identity.dshHome !== layout.dshHome) {
21
+ ctx.progress.log(runtime, 'debug', `launch: pid ${pid} runs with another DSH_HOME; it will not be stopped`);
22
+ return null;
23
+ }
24
+ if (!commandMatchesProfile(identity.command, profile)) {
25
+ ctx.progress.log(runtime, 'debug', `launch: pid ${pid} does not run profile ${profile}; it will not be stopped`);
26
+ return null;
27
+ }
28
+ const record = {
29
+ startTime: identity.startTime,
30
+ command: identity.command,
31
+ dshHome: identity.dshHome,
32
+ };
33
+ runtime.launched.set(pid, record);
34
+ runtime.bound = { pid, startTime: identity.startTime, origin: 'launch' };
35
+ return record;
36
+ }
37
+ /**
38
+ * Remember the remote web this connection is bound to without granting restart kill rights.
39
+ * A re-attach (or the adoption of a re-attached record) names a process this plugin did not
40
+ * launch: a restart must leave it running, but the user's explicit "Stop remote DSH" may stop
41
+ * exactly this pid. The identity recorded here is what that action re-proves before signalling.
42
+ * @param ctx - service context.
43
+ * @param runtime - host runtime with a live transport.
44
+ * @param pid - the bound remote web pid.
45
+ * @param layout - remote layout.
46
+ * @param profile - remote DSH profile.
47
+ * @param origin - how this connection came to host the process.
48
+ * @returns the recorded binding, or null when it cannot be proven.
49
+ */
50
+ export async function rememberBound(ctx, runtime, pid, layout, profile, origin) {
51
+ if (!isPid(pid))
52
+ return null;
53
+ const identity = await ctx.deps.processIdentity(runtime.ssh, pid);
54
+ if (identity.ok !== true) {
55
+ ctx.progress.log(runtime, 'debug', `bind: pid ${pid} identity unreadable (${identity.reason}); it will not be stopped on request`);
56
+ return null;
57
+ }
58
+ if (identity.dshHome !== '' && identity.dshHome !== layout.dshHome) {
59
+ ctx.progress.log(runtime, 'debug', `bind: pid ${pid} runs with another DSH_HOME; it cannot be stopped here`);
60
+ return null;
61
+ }
62
+ if (!commandMatchesProfile(identity.command, profile)) {
63
+ ctx.progress.log(runtime, 'debug', `bind: pid ${pid} does not run profile ${profile}; it cannot be stopped here`);
64
+ return null;
65
+ }
66
+ runtime.bound = { pid, startTime: identity.startTime, origin };
67
+ return runtime.bound;
68
+ }
69
+ /**
70
+ * The three call sites' option sets (design-arch §4.3). They live here so the differences are a
71
+ * table the tests can read instead of three copies of the same nine checks:
72
+ *
73
+ * - `restart` is an *implicit* kill: only a process this connection launched may be stopped. The
74
+ * registry check belongs to the caller, because a registry mismatch is a conflict prompt, not a
75
+ * refusal (design.md §5.5). `strict` because a `launched` record always carries the `/proc`
76
+ * start time; one without it is a broken record, and an unprovable pid must not be stopped.
77
+ * It proves the record before `alongsidePids`, which is the order its pre-merge form used, and it
78
+ * always knows the profile it manages.
79
+ * - `stop` is the user pointing at the instance this connection is bound to: it may stop a
80
+ * re-attached pid (design.md §5.7), but only that exact pid, and only while `run/web.json` still
81
+ * names it with the same home and profile — the written evidence of "the instance this
82
+ * connection manages". It knows the profile too; `kept-alongside` stays its first refusal.
83
+ * - `idle` has nobody pointing at anything, only a timer: no registry (it may be stale by then),
84
+ * but it needs a record of its own; `bound-first` preserves the order the timer always used, and
85
+ * an empty profile (a teardown that forgot it) skips the profile comparison instead of refusing.
86
+ *
87
+ * `noProofReason` and `prefer` exist only to keep the existing wording and order (DECISIONS H5);
88
+ * a fourth call site must be registered here with its reason.
89
+ */
90
+ export const OWNERSHIP_OPTIONS = {
91
+ restart: {
92
+ sources: ['launched'],
93
+ startTime: 'strict',
94
+ noProofReason: 'not-launched-by-this-connection',
95
+ proofBeforeAlongside: true,
96
+ requireProfile: true,
97
+ },
98
+ stop: {
99
+ sources: ['launched', 'bound'],
100
+ requireBoundPid: true,
101
+ requireRegistry: true,
102
+ startTime: 'when-recorded',
103
+ prefer: 'launched-first',
104
+ noProofReason: 'not-launched-by-this-connection',
105
+ requireProfile: true,
106
+ },
107
+ idle: {
108
+ sources: ['launched', 'bound'],
109
+ startTime: 'when-recorded',
110
+ prefer: 'bound-first',
111
+ noProofReason: 'not-owned',
112
+ },
113
+ };
114
+ /**
115
+ * Prove that this connection may signal `pid`, and say why not when it may not.
116
+ *
117
+ * The judgment order is itself part of the contract (§4.2), and every step is a refusal that
118
+ * never signals anything:
119
+ *
120
+ * ```
121
+ * 1 pid is a pid → pid-unknown
122
+ * 2 requireProfile && profile is empty → profile-mismatch
123
+ * 3 requireBoundPid && remote.pid !== pid → not-the-bound-instance
124
+ * 4 !proofBeforeAlongside && forbidAlongside && alongside has pid → kept-alongside
125
+ * 5 no record of the allowed sources → noProofReason
126
+ * 6 proofBeforeAlongside && forbidAlongside && alongside has pid → kept-alongside
127
+ * 7 requireRegistry && the registry does not name it → registry-mismatch
128
+ * 8 /proc identity unreadable → identity-unreadable
129
+ * /proc says it is gone → identity-missing
130
+ * any other read failure → identity-<reason>
131
+ * 9 start time differs → pid-reused
132
+ * 10 the process runs under another DSH home → dsh-home-mismatch
133
+ * 11 the command line carries another profile → profile-mismatch
134
+ * ```
135
+ *
136
+ * @param ctx - transport (the `/proc` reader) and, when `requireRegistry` is set, the registry.
137
+ * @param runtime - host runtime the records live on.
138
+ * @param options - one of {@link OWNERSHIP_OPTIONS} plus the pid, layout and profile.
139
+ * @returns `{ ok: true }` with the evidence, or the frozen reason it refused.
140
+ */
141
+ export async function proveOwnership(ctx, runtime, options) {
142
+ const { pid, layout, profile } = options;
143
+ const sources = options.sources ?? ['launched'];
144
+ const prefer = options.prefer ?? 'launched-first';
145
+ const startTimeMode = options.startTime ?? 'when-recorded';
146
+ const noProofReason = options.noProofReason ?? 'not-launched-by-this-connection';
147
+ const forbidAlongside = options.forbidAlongside !== false;
148
+ const alongside = Array.isArray(runtime.alongsidePids) ? runtime.alongsidePids : [];
149
+ const launched = sources.includes('launched') && runtime.launched instanceof Map ? runtime.launched.get(pid) : undefined;
150
+ const bound = sources.includes('bound') &&
151
+ runtime.bound !== null &&
152
+ runtime.bound !== undefined &&
153
+ Number(runtime.bound.pid) === pid
154
+ ? runtime.bound
155
+ : null;
156
+ const launchedProof = launched ?? null;
157
+ const proof = prefer === 'bound-first' ? (bound ?? launchedProof) : (launchedProof ?? bound);
158
+ const source = proof === null ? null : proof === launchedProof ? 'launched' : 'bound';
159
+ const evidence = {
160
+ pid,
161
+ source,
162
+ recordedStartTime: proof === null ? null : typeof proof.startTime === 'string' ? proof.startTime : null,
163
+ identity: null,
164
+ registry: null,
165
+ layout,
166
+ profile,
167
+ };
168
+ const refuse = (reason) => ({ ok: false, reason, evidence });
169
+ if (!isPid(pid))
170
+ return refuse('pid-unknown');
171
+ // Only the shapes that always know their profile enforce this; the idle timer may have lost it
172
+ // in a teardown, which must stay "unknown" rather than become a refusal.
173
+ if (options.requireProfile === true && profile === '')
174
+ return refuse('profile-mismatch');
175
+ if (options.requireBoundPid === true && Number(runtime.remote.pid) !== pid)
176
+ return refuse('not-the-bound-instance');
177
+ const keptAlongside = forbidAlongside && alongside.includes(pid);
178
+ if (keptAlongside && options.proofBeforeAlongside !== true)
179
+ return refuse('kept-alongside');
180
+ if (proof === null)
181
+ return refuse(noProofReason);
182
+ if (keptAlongside)
183
+ return refuse('kept-alongside');
184
+ if (options.requireRegistry === true) {
185
+ const registry = await readRegistryQuietly(ctx, runtime, layout);
186
+ evidence.registry = registry;
187
+ const registered = registry !== null &&
188
+ registry.dshHome === layout.dshHome &&
189
+ registry.profile === profile &&
190
+ Number(registry.pid) === pid;
191
+ if (!registered)
192
+ return refuse('registry-mismatch');
193
+ }
194
+ let identity;
195
+ try {
196
+ identity = await ctx.deps.processIdentity(runtime.ssh, pid);
197
+ }
198
+ catch {
199
+ // swallow-ok: an identity read that throws cannot prove ownership either, so nothing is signalled
200
+ return refuse('identity-unreadable');
201
+ }
202
+ evidence.identity = identity;
203
+ if (identity.ok !== true) {
204
+ // "The process is gone" and "the process could not be read" are different refusals: the
205
+ // explicit stop clears a stale registry only for the first one.
206
+ return refuse(identity.reason === 'missing' ? 'identity-missing' : `identity-${identity.reason}`);
207
+ }
208
+ if (startTimeMode === 'strict') {
209
+ if (identity.startTime !== proof.startTime)
210
+ return refuse('pid-reused');
211
+ }
212
+ else if (typeof proof.startTime === 'string' && identity.startTime !== proof.startTime) {
213
+ return refuse('pid-reused');
214
+ }
215
+ if (identity.dshHome !== '' && identity.dshHome !== layout.dshHome)
216
+ return refuse('dsh-home-mismatch');
217
+ if (profile !== '' && !commandMatchesProfile(identity.command, profile))
218
+ return refuse('profile-mismatch');
219
+ return { ok: true, evidence };
220
+ }