dsh-remote-vs 0.29.0 → 0.31.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.en.md +53 -20
- package/README.md +41 -19
- package/THIRD-PARTY-NOTICES.md +6 -6
- package/cordis.patch.yml +31 -76
- package/lib/client.js +319 -91
- package/lib/index.js +845 -129
- package/lib/p2-router-fs.js +15 -1
- package/lib/types/changes/recorder.d.ts +65 -10
- package/lib/types/changes/status.d.ts +48 -3
- package/lib/types/client/ConnectionsSection.d.ts +73 -0
- package/lib/types/client/WorkspacesView.d.ts +2 -0
- package/lib/types/client/api.d.ts +41 -2
- package/lib/types/client/composer.d.ts +8 -1
- package/lib/types/client/scm/actions.d.ts +25 -1
- package/lib/types/client/selection-popup.d.ts +1 -1
- package/lib/types/client/ui.d.ts +7 -4
- package/lib/types/p2/helper-rpc.d.ts +48 -0
- package/lib/types/p2/router-fs.d.ts +9 -1
- package/lib/types/p2/worlds.d.ts +51 -0
- package/lib/types/pool.d.ts +245 -10
- package/lib/types/protocol.d.ts +96 -0
- package/lib/{worlds-RG8K5eom.js → worlds-BdxLeKul.js} +134 -7
- package/package.json +46 -46
- package/src/changes/recorder.ts +151 -15
- package/src/changes/status.ts +59 -4
- package/src/client/ConnectionsSection.tsx +359 -0
- package/src/client/DirectoryFlow.tsx +8 -0
- package/src/client/EditorPane.tsx +5 -1
- package/src/client/HostsView.tsx +2 -0
- package/src/client/MonitorView.tsx +14 -0
- package/src/client/RemoteExplorer.tsx +11 -2
- package/src/client/RemoteRoot.tsx +1 -0
- package/src/client/SettingsHosts.tsx +1 -0
- package/src/client/WorkspaceWizard.tsx +1 -0
- package/src/client/WorkspacesView.tsx +21 -1
- package/src/client/api.ts +72 -4
- package/src/client/composer.ts +8 -1
- package/src/client/i18n.ts +50 -2
- package/src/client/scm/DiffView.tsx +5 -2
- package/src/client/scm/actions.ts +63 -12
- package/src/client/selection-popup.ts +1 -1
- package/src/client/ui.tsx +9 -7
- package/src/client/vsc/vsc.css +231 -3
- package/src/index.ts +31 -0
- package/src/p2/helper-rpc.ts +73 -5
- package/src/p2/router-fs.ts +29 -1
- package/src/p2/worlds.ts +116 -1
- package/src/pool.ts +736 -111
- package/src/protocol.ts +100 -0
- package/src/routes.ts +85 -13
- package/vendor/dsh-remote-helpers/dsh-ssh-helper-0.2.3-alpha.3-linux-x64.helper.json +20 -0
- package/vendor/dsh-remote-helpers/dsh-ssh-helper-0.2.3-alpha.3-linux-x64.tar.gz +4 -0
- package/vendor/dsh-remote-helpers/dsh-ssh-helper-0.2.3-alpha.3-linux-x64.tar.gz.sha256 +1 -0
- package/vendor/dsh-remote-helpers/pkg-sea-bare-import.patch +30 -0
- package/vendor/dsh-remote-helpers/ssh-helper-runtime-deps.patch +4 -3
- package/vendor/dsh-remote-helpers/dsh-ssh-helper-0.2.2-rc.4-linux-x64.helper.json +0 -19
- package/vendor/dsh-remote-helpers/dsh-ssh-helper-0.2.2-rc.4-linux-x64.tar.gz +0 -4
- package/vendor/dsh-remote-helpers/dsh-ssh-helper-0.2.2-rc.4-linux-x64.tar.gz.sha256 +0 -1
package/src/pool.ts
CHANGED
|
@@ -14,7 +14,15 @@ import { Client, type ClientChannel, type SFTPWrapper } from 'ssh2'
|
|
|
14
14
|
import type { RemoteStore, ResolvedProxy } from './store.ts'
|
|
15
15
|
import { openProxySocket, ProxyError, type ProxyConfig, type ProxyFailure } from './proxy.ts'
|
|
16
16
|
import { DeadlineError } from './timeouts.ts'
|
|
17
|
-
import type {
|
|
17
|
+
import type {
|
|
18
|
+
EgressInfo,
|
|
19
|
+
RemoteConnectionStatus,
|
|
20
|
+
RemoteExecResult,
|
|
21
|
+
RemoteHostEntry,
|
|
22
|
+
RemoteHostPoolStats,
|
|
23
|
+
RemotePoolLane,
|
|
24
|
+
RemotePoolSlots,
|
|
25
|
+
} from './protocol.ts'
|
|
18
26
|
|
|
19
27
|
/**
|
|
20
28
|
* The budget-out type lives in `timeouts.ts` (both halves need it); re-exported
|
|
@@ -54,6 +62,33 @@ const FORWARD_DEDICATED_BUDGET_MS = (() => {
|
|
|
54
62
|
*/
|
|
55
63
|
const FORWARD_DEDICATED_BLOCK_MS = 60_000
|
|
56
64
|
|
|
65
|
+
/**
|
|
66
|
+
* How long a file operation waits for its DEDICATED SFTP connection before
|
|
67
|
+
* falling back to the shared one.
|
|
68
|
+
*
|
|
69
|
+
* Same argument as {@link FORWARD_DEDICATED_BUDGET_MS}, and it binds harder
|
|
70
|
+
* here: browsing fires one request per folder open (plus a stat per entry), so a
|
|
71
|
+
* server that silently DROPS the extra connection would otherwise stall the
|
|
72
|
+
* explorer for the whole connect watchdog on EVERY click. The dedicated
|
|
73
|
+
* connection is an optimization; it must never be what the user waits on.
|
|
74
|
+
*
|
|
75
|
+
* Override with DSH_REMOTE_VS_SFTP_BUDGET_MS (milliseconds, >= 10).
|
|
76
|
+
*/
|
|
77
|
+
const SFTP_DEDICATED_BUDGET_MS = (() => {
|
|
78
|
+
const raw = Number(process.env.DSH_REMOTE_VS_SFTP_BUDGET_MS)
|
|
79
|
+
return Number.isFinite(raw) && raw >= 10 ? raw : 3_000
|
|
80
|
+
})()
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* How long a refused dedicated SFTP connection is skipped per alias.
|
|
84
|
+
*
|
|
85
|
+
* Without it the fallback would be re-dialed by every browse request — the exact
|
|
86
|
+
* waiting the dedicated budget above just removed. One minute covers a burst of
|
|
87
|
+
* browsing; `drop(alias)` (host edited/deleted) and a successful dedicated
|
|
88
|
+
* connection clear it immediately.
|
|
89
|
+
*/
|
|
90
|
+
const SFTP_DEDICATED_BLOCK_MS = 60_000
|
|
91
|
+
|
|
57
92
|
/**
|
|
58
93
|
* Connection establishment timeout, including authentication.
|
|
59
94
|
*
|
|
@@ -133,21 +168,120 @@ const CONTROL_TIMEOUT_MS = 15_000
|
|
|
133
168
|
const GROUP_KILL_GRACE_MS = 3_000
|
|
134
169
|
|
|
135
170
|
/**
|
|
136
|
-
* Concurrent channels the pool opens per alias
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
171
|
+
* Concurrent EXEC channels the pool opens per alias on the SHARED connection.
|
|
172
|
+
*
|
|
173
|
+
* The server's own `MaxSessions` (10 on the hosts this was measured against) is
|
|
174
|
+
* the real ceiling, and a channel past it is refused HARD — ssh2 reports
|
|
175
|
+
* "administratively prohibited" and nobody retries, so the operation fails in a
|
|
176
|
+
* way that looks like a broken host. Queueing is the readable alternative, which
|
|
177
|
+
* is why every counted channel kind goes through a budget instead.
|
|
178
|
+
*
|
|
179
|
+
* The counting is now honest — every `conn.exec` channel takes a slot:
|
|
180
|
+
* - `runExec` (every ordinary command) and `execChannel` (the subprocess
|
|
181
|
+
* runtime's long-lived channels, see `p2/ssh-proc.ts`), both on this budget;
|
|
182
|
+
* - network verbs ({@link NET_CHANNEL_LIMIT}) and git commands
|
|
183
|
+
* ({@link GIT_CHANNEL_LIMIT}), on their own budgets on the same connection.
|
|
184
|
+
* PTY terminals, port forwards and SFTP sessions ride their OWN connections, so
|
|
185
|
+
* they cannot exhaust this allowance at all.
|
|
186
|
+
*
|
|
187
|
+
* **The arithmetic (shared connection, measured `MaxSessions` 10)**:
|
|
188
|
+
* counted channels are exec 4 + network 2 + git 3 = **9**. The tenth session is
|
|
189
|
+
* deliberately left free for the only channels that are still NOT counted: the
|
|
190
|
+
* pool's own housekeeping (`bare` — the one-shot `setsid` capability probe and
|
|
191
|
+
* the group kill). Those are transient, but they must never be refused, because
|
|
192
|
+
* a refused probe silently degrades the timeout path and a refused kill leaks a
|
|
193
|
+
* remote process. So 9 + 1 is a BOUNDED DESIGN, not spare headroom: a tenth
|
|
194
|
+
* counted channel would put the sum at 10 only as long as nothing transient ever
|
|
195
|
+
* fires — an assumption the pool cannot make.
|
|
196
|
+
*
|
|
197
|
+
* **Why 4 and not more** — this is a deliberate split of the same nine, not a
|
|
198
|
+
* reduction of it. The field call: "several workspaces / repositories scanning at
|
|
199
|
+
* once" is the common shape, while "an agent running many exec commands at once"
|
|
200
|
+
* is rarer, so one slot moved from exec to the git lane (git 2 → 3) and the
|
|
201
|
+
* network lane stayed at two (push and pull must not serialize). The known cost:
|
|
202
|
+
* exec concurrency drops from five to four, so an agent's background commands plus
|
|
203
|
+
* a monitor sample plus the process panel plus the listening-port enumeration can
|
|
204
|
+
* fill it and the fifth waits — bounded at 30 s with a readable message, and
|
|
205
|
+
* visible as `Shared 4/4 · N queued` in the monitor. If exec turns out to be the
|
|
206
|
+
* real bottleneck, move the slot back; the total must stay 9.
|
|
207
|
+
*
|
|
208
|
+
* The budget is PER CONNECTION, and the pool holds up to four connections per
|
|
209
|
+
* alias, so the other classes (PTY/forward/SFTP) keep their own ceilings.
|
|
210
|
+
*/
|
|
211
|
+
const CHANNEL_LIMIT = 4
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Concurrent SFTP sessions on the DEDICATED file connection.
|
|
215
|
+
*
|
|
216
|
+
* Deliberately its own number rather than {@link CHANNEL_LIMIT}: the shared
|
|
217
|
+
* connection's ceiling is tight because three budgets share it with the transient
|
|
218
|
+
* `bare` probe/kill, while the file connection carries nothing but SFTP sessions
|
|
219
|
+
* and has the whole `MaxSessions` (10) to itself. Six is what the transfer
|
|
220
|
+
* measurement landed on (the extra channel windows a download may open), and it is
|
|
221
|
+
* unchanged by the exec/network/git rebalance.
|
|
222
|
+
*/
|
|
223
|
+
const SFTP_CHANNEL_LIMIT = 6
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Concurrent NETWORK verbs (`git fetch` / `pull` / `push` / `sync`) per alias.
|
|
227
|
+
*
|
|
228
|
+
* They ride the SHARED connection (no fifth link: the traffic is not the problem,
|
|
229
|
+
* the HOLD TIME is) but get their own budget, because their timeout is measured
|
|
230
|
+
* in minutes — 20 by default. On the exec budget one stuck push pinned one of the
|
|
231
|
+
* exec slots for the whole budget, and a couple of them filled the table: status
|
|
232
|
+
* refreshes, the process panel, the monitor and every agent tool then queued 30 s
|
|
233
|
+
* and failed with "host is busy". With a separate table that can no longer happen.
|
|
234
|
+
*
|
|
235
|
+
* Two is enough for the real concurrency (a push and a pull side by side; `sync`
|
|
236
|
+
* runs its steps sequentially). A third verb waits, and says so in its own words
|
|
237
|
+
* (see `budgetOf`). The full per-connection arithmetic lives on
|
|
238
|
+
* {@link CHANNEL_LIMIT}: exec 4 + network 2 + git 3 = 9 counted, with one session
|
|
239
|
+
* left free for the transient `bare` probe/kill.
|
|
240
|
+
*/
|
|
241
|
+
const NET_CHANNEL_LIMIT = 2
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Concurrent GIT commands (`git status` / `diff` / `log` reads AND the local
|
|
245
|
+
* writes — commit, stage, discard, branch create/rename/delete, merge) per alias.
|
|
246
|
+
*
|
|
247
|
+
* Same argument as the network lane, from the other end of the timeout scale: a
|
|
248
|
+
* big repository's `git status` alone is allowed 300 s by default
|
|
249
|
+
* (`DEFAULT_GIT_TIMEOUT_MS`, up to 30 minutes), and every git command used to
|
|
250
|
+
* hold one of the exec slots for its whole run — while the monitor, the process
|
|
251
|
+
* panel, the ledger's own next scan and every agent tool queued behind it and
|
|
252
|
+
* answered "host is busy" after 30 s.
|
|
253
|
+
*
|
|
254
|
+
* NOTE the lane is "git commands", not "repository reads": the SCM panel sends its
|
|
255
|
+
* local writes (`git commit`, `git add`, `git restore`, branch verbs) here too,
|
|
256
|
+
* because they are git commands with a git timeout. They are fast, and they no
|
|
257
|
+
* longer occupy a slot out of the same few the monitor and the agent tools use.
|
|
258
|
+
*
|
|
259
|
+
* **Why three** (shared connection, measured `MaxSessions` 10):
|
|
260
|
+
* - counted channels: exec 4 + network 2 + git 3 = 9, plus the one session kept
|
|
261
|
+
* free for the transient `bare` probe/kill — see {@link CHANNEL_LIMIT};
|
|
262
|
+
* - three covers the load the field actually shows: several open workspaces or
|
|
263
|
+
* repositories scanning at once (each runs a tracked status AND, outside the
|
|
264
|
+
* client queue, an untracked walk), which is more common than an agent running
|
|
265
|
+
* many commands at the same instant — hence the extra slot came out of exec;
|
|
266
|
+
* - four would make 10 counted with the probe reserve gone; the surplus channel
|
|
267
|
+
* past `MaxSessions` is refused HARD by sshd ("administratively prohibited", no
|
|
268
|
+
* retry) — the exact failure these budgets exist to replace with a bounded
|
|
269
|
+
* queue. Raising this further means taking the slot from exec again.
|
|
142
270
|
*/
|
|
143
|
-
const
|
|
271
|
+
const GIT_CHANNEL_LIMIT = 3
|
|
144
272
|
|
|
145
273
|
/**
|
|
146
274
|
* How long an operation waits for a channel slot before failing. Bounded on
|
|
147
275
|
* purpose: queueing forever behind a peer that never finishes is worse than a
|
|
148
276
|
* readable error that tells the user to retry.
|
|
277
|
+
*
|
|
278
|
+
* Override with DSH_REMOTE_VS_CHANNEL_WAIT_MS (milliseconds, >= 10) so a gate can
|
|
279
|
+
* assert the "busy" message without waiting the production 30 s.
|
|
149
280
|
*/
|
|
150
|
-
const CHANNEL_WAIT_MS =
|
|
281
|
+
const CHANNEL_WAIT_MS = (() => {
|
|
282
|
+
const raw = Number(process.env.DSH_REMOTE_VS_CHANNEL_WAIT_MS)
|
|
283
|
+
return Number.isFinite(raw) && raw >= 10 ? raw : 30_000
|
|
284
|
+
})()
|
|
151
285
|
|
|
152
286
|
/**
|
|
153
287
|
* Idle time after which the shared BROWSE SFTP session is closed.
|
|
@@ -169,6 +303,16 @@ interface Pooled {
|
|
|
169
303
|
connecting: Promise<Client> | undefined
|
|
170
304
|
/** Live users (channels/sessions); idle eviction only fires at zero. */
|
|
171
305
|
held: number
|
|
306
|
+
/**
|
|
307
|
+
* SSH channels currently open on this connection (see `countChannel`).
|
|
308
|
+
*
|
|
309
|
+
* Tracked separately from `held` on purpose: the two happen to agree for the
|
|
310
|
+
* dedicated PTY/forward/SFTP connections (one hold per channel), but they do
|
|
311
|
+
* NOT for the shared one — a jump tunnel pins the jump host twice for its
|
|
312
|
+
* single channel — and `held` would otherwise be reported as a channel count
|
|
313
|
+
* that is simply wrong. Reported by `stats()`.
|
|
314
|
+
*/
|
|
315
|
+
channels: number
|
|
172
316
|
/**
|
|
173
317
|
* The route THIS connection was opened through (set on a successful dial,
|
|
174
318
|
* cleared with the entry). It answers "is this really going through the jump
|
|
@@ -193,9 +337,26 @@ interface BrowseChannel {
|
|
|
193
337
|
stale: boolean
|
|
194
338
|
/** This channel holds one channel slot and one connection hold. */
|
|
195
339
|
leased: boolean
|
|
340
|
+
/**
|
|
341
|
+
* Which connection class the lease sits on: the dedicated SFTP connection, or
|
|
342
|
+
* the shared one when its dial was refused (the fallback). The slot table and
|
|
343
|
+
* the hold map follow THIS, so a fallback session is accounted for on the
|
|
344
|
+
* shared connection it really rides — never on both.
|
|
345
|
+
*/
|
|
346
|
+
lane: SftpLane
|
|
196
347
|
idle: ReturnType<typeof setTimeout> | undefined
|
|
197
348
|
}
|
|
198
349
|
|
|
350
|
+
/**
|
|
351
|
+
* The two connection classes a file operation can ride.
|
|
352
|
+
*
|
|
353
|
+
* `sftp` is the dedicated connection (its own TCP link, its own channel budget);
|
|
354
|
+
* `shared` is the fallback everything used to ride. The distinction is
|
|
355
|
+
* load-bearing for accounting: hold, slot and health all depend on which one is
|
|
356
|
+
* really in use.
|
|
357
|
+
*/
|
|
358
|
+
type SftpLane = 'sftp' | 'shared'
|
|
359
|
+
|
|
199
360
|
/**
|
|
200
361
|
* Connection-level failures the status indicator must show.
|
|
201
362
|
*
|
|
@@ -246,6 +407,23 @@ export interface ExecOptions {
|
|
|
246
407
|
* recurse into the machinery that dispatched them.
|
|
247
408
|
*/
|
|
248
409
|
bare?: boolean
|
|
410
|
+
/**
|
|
411
|
+
* Which channel budget this command spends. Omitted means the shared exec
|
|
412
|
+
* budget — every caller that predates the split keeps that behaviour.
|
|
413
|
+
*
|
|
414
|
+
* `'network'` is for `git fetch/pull/push/sync`: those can hold a channel for
|
|
415
|
+
* the whole network timeout (20 minutes by default), so they get
|
|
416
|
+
* {@link NET_CHANNEL_LIMIT} slots of their own instead of eating the exec slots
|
|
417
|
+
* the rest of the app (status polls, monitor, agent tools) depends on.
|
|
418
|
+
*
|
|
419
|
+
* `'git'` is for GIT COMMANDS — the repository reads (`git status/diff/log`, the
|
|
420
|
+
* session ledger's scans) AND the local writes the SCM panel runs (`commit`,
|
|
421
|
+
* `stage`, `discard`, branch verbs, `merge`): a big tree's `git status` is
|
|
422
|
+
* allowed 300 s, so these get {@link GIT_CHANNEL_LIMIT} slots — otherwise one
|
|
423
|
+
* slow command parks an exec slot for minutes and the monitor/process panel/
|
|
424
|
+
* agent tools queue behind it.
|
|
425
|
+
*/
|
|
426
|
+
lane?: 'exec' | 'network' | 'git'
|
|
249
427
|
}
|
|
250
428
|
|
|
251
429
|
export interface ShellOptions {
|
|
@@ -253,6 +431,23 @@ export interface ShellOptions {
|
|
|
253
431
|
rows?: number
|
|
254
432
|
}
|
|
255
433
|
|
|
434
|
+
/**
|
|
435
|
+
* The transport one configured route hands to an ssh2 dial (see `openRoutedSocket`).
|
|
436
|
+
*
|
|
437
|
+
* `sock` is absent for a direct route, where ssh2 dials the target itself. When it
|
|
438
|
+
* IS present ssh2 must be given `{ sock }` and NOT `host`/`port`: supplying both
|
|
439
|
+
* lets ssh2 choose between "dial this host myself" and "ride the tunnel", and the
|
|
440
|
+
* choice is an implementation detail, not a feature.
|
|
441
|
+
*
|
|
442
|
+
* `release` is idempotent and is the OWNER's to call — a proxy socket has to be
|
|
443
|
+
* destroyed and a jump-host tunnel has to close its channel and give the jump
|
|
444
|
+
* host's hold back, or the next dial finds a pinned connection.
|
|
445
|
+
*/
|
|
446
|
+
export interface RoutedDial {
|
|
447
|
+
sock?: Duplex
|
|
448
|
+
release: () => void
|
|
449
|
+
}
|
|
450
|
+
|
|
256
451
|
export interface SftpOptions {
|
|
257
452
|
/**
|
|
258
453
|
* Wall-clock cap for the callback body. `0` disables it for an operation that
|
|
@@ -319,6 +514,38 @@ export class RemotePool {
|
|
|
319
514
|
* stops working because of it — it only loses the isolation.
|
|
320
515
|
*/
|
|
321
516
|
private readonly forwardConns = new Map<string, Pooled>()
|
|
517
|
+
/**
|
|
518
|
+
* Dedicated connections for FILE operations (one per alias).
|
|
519
|
+
*
|
|
520
|
+
* The third instance of the same argument. Browse/read/write/upload/download
|
|
521
|
+
* used to ride the shared connection, which meant a `git status` burst or a
|
|
522
|
+
* long exec could sit in front of a directory refresh (SSH offers no
|
|
523
|
+
* per-channel priority), AND that the shared link's channel budget was shared
|
|
524
|
+
* with file I/O: a few in-flight git/search commands and the explorer could not
|
|
525
|
+
* open a channel at all. A file operation is exactly the traffic a user is
|
|
526
|
+
* watching (a folder must open, a save must land), so it gets its own
|
|
527
|
+
* connection and its own channel budget (`sftpSlots`).
|
|
528
|
+
*
|
|
529
|
+
* Cost: one extra handshake per alias that touches files, plus one held TCP
|
|
530
|
+
* connection while any of its channels is open (the 30-minute idle eviction
|
|
531
|
+
* applies once the last one ends). `acquireSftp` falls back to the
|
|
532
|
+
* shared connection when this one cannot be established — a host that refuses
|
|
533
|
+
* extra connections still serves files, it only loses the isolation.
|
|
534
|
+
*/
|
|
535
|
+
private readonly sftpConns = new Map<string, Pooled>()
|
|
536
|
+
/** Aliases whose dedicated-SFTP downgrade was already logged (once per alias). */
|
|
537
|
+
private readonly sftpFallbackNoted = new Set<string>()
|
|
538
|
+
/** alias -> timestamp until which the dedicated SFTP connection is skipped. */
|
|
539
|
+
private readonly sftpBlocked = new Map<string, number>()
|
|
540
|
+
/**
|
|
541
|
+
* Aliases whose file operations are DOWNGRADED right now (sticky until a
|
|
542
|
+
* dedicated connection succeeds again or the host is edited).
|
|
543
|
+
*
|
|
544
|
+
* Purely diagnostic: `stats()` reports it so the monitor page can say "these
|
|
545
|
+
* files are riding the shared connection" instead of leaving the user to
|
|
546
|
+
* wonder why a browse refresh still queues behind git.
|
|
547
|
+
*/
|
|
548
|
+
private readonly sftpFallback = new Set<string>()
|
|
322
549
|
/** Aliases whose dedicated-forward downgrade was already logged (once per alias). */
|
|
323
550
|
private readonly forwardFallbackNoted = new Set<string>()
|
|
324
551
|
/** alias -> timestamp until which the dedicated forward connection is skipped. */
|
|
@@ -327,8 +554,35 @@ export class RemotePool {
|
|
|
327
554
|
private readonly hostKeys = new Map<string, string>()
|
|
328
555
|
/** Per-alias `setsid --wait` capability, probed once (see supportsSetsid). */
|
|
329
556
|
private readonly setsidProbes = new Map<string, Promise<boolean>>()
|
|
330
|
-
/**
|
|
557
|
+
/**
|
|
558
|
+
* Channel slots of the SHARED connection (see acquireSlot), and of the
|
|
559
|
+
* dedicated SFTP connection.
|
|
560
|
+
*
|
|
561
|
+
* Two tables because the budget is per connection (`MaxSessions` counts
|
|
562
|
+
* channels on one link): a fallback SFTP operation counts against the shared
|
|
563
|
+
* table — the connection it really rides — and never against both.
|
|
564
|
+
*/
|
|
331
565
|
private readonly slots = new Map<string, Slots>()
|
|
566
|
+
private readonly sftpSlots = new Map<string, Slots>()
|
|
567
|
+
/**
|
|
568
|
+
* Channel budget of the NETWORK verbs (see {@link NET_CHANNEL_LIMIT}).
|
|
569
|
+
*
|
|
570
|
+
* A third table on the SHARED connection: unlike the SFTP lane this is not about
|
|
571
|
+
* isolating bytes, it is about isolating TIME. A `git push` holds its channel
|
|
572
|
+
* for minutes, and the failure mode it caused — a full exec table, so every
|
|
573
|
+
* status refresh answered "host is busy" after 30 s — came from the two kinds
|
|
574
|
+
* sharing one ceiling.
|
|
575
|
+
*/
|
|
576
|
+
private readonly netSlots = new Map<string, Slots>()
|
|
577
|
+
/**
|
|
578
|
+
* Channel budget of the GIT COMMANDS (see {@link GIT_CHANNEL_LIMIT}).
|
|
579
|
+
*
|
|
580
|
+
* A fourth table on the SHARED connection, for the same reason as the network
|
|
581
|
+
* one — hold TIME, not traffic: a `git status` on a big tree may run for
|
|
582
|
+
* minutes, and a git command must not be the reason a monitor sample or an agent
|
|
583
|
+
* tool waits.
|
|
584
|
+
*/
|
|
585
|
+
private readonly gitSlots = new Map<string, Slots>()
|
|
332
586
|
/** Per-alias BROWSE SFTP session (see withBrowseSftp). */
|
|
333
587
|
private readonly browsed = new Map<string, BrowseChannel>()
|
|
334
588
|
/** Per-alias connection health for the browser's status chip (see status). */
|
|
@@ -344,21 +598,36 @@ export class RemotePool {
|
|
|
344
598
|
// The host entry can change under an alias, so the capability answer goes
|
|
345
599
|
// with the connection; the next exec probes again.
|
|
346
600
|
this.setsidProbes.delete(alias)
|
|
347
|
-
// All
|
|
601
|
+
// All four connections belong to the host: an edit/removal must end all.
|
|
348
602
|
// (A transient error on ONE of them uses `evictFrom` for that map only, so
|
|
349
603
|
// a shared-connection blip does not take live terminals/forwards down with it.)
|
|
350
604
|
this.evictFrom(this.conns, alias)
|
|
351
605
|
this.evictFrom(this.shellConns, alias)
|
|
352
606
|
this.evictFrom(this.forwardConns, alias)
|
|
607
|
+
this.evictFrom(this.sftpConns, alias)
|
|
353
608
|
// Verdicts about the OLD connection do not survive the host: an edited host
|
|
354
609
|
// (now allowed, or with a new address) must be retried and may be reported.
|
|
355
610
|
this.forwardBlocked.delete(alias)
|
|
356
611
|
this.forwardFallbackNoted.delete(alias)
|
|
612
|
+
this.sftpBlocked.delete(alias)
|
|
613
|
+
this.sftpFallbackNoted.delete(alias)
|
|
614
|
+
this.sftpFallback.delete(alias)
|
|
615
|
+
// The old connection's channel budget goes with it too: this call ends the
|
|
616
|
+
// connections, so their in-flight operations can only fail, and a budget row
|
|
617
|
+
// left behind would make the NEXT connection start out looking busy.
|
|
618
|
+
this.sftpSlots.delete(alias)
|
|
619
|
+
this.netSlots.delete(alias)
|
|
620
|
+
this.gitSlots.delete(alias)
|
|
357
621
|
}
|
|
358
622
|
|
|
359
623
|
/** Close every pooled connection. */
|
|
360
624
|
dispose(): void {
|
|
361
|
-
for (const alias of new Set([
|
|
625
|
+
for (const alias of new Set([
|
|
626
|
+
...this.conns.keys(),
|
|
627
|
+
...this.shellConns.keys(),
|
|
628
|
+
...this.forwardConns.keys(),
|
|
629
|
+
...this.sftpConns.keys(),
|
|
630
|
+
])) {
|
|
362
631
|
this.drop(alias)
|
|
363
632
|
}
|
|
364
633
|
}
|
|
@@ -405,12 +674,14 @@ export class RemotePool {
|
|
|
405
674
|
return {
|
|
406
675
|
alias,
|
|
407
676
|
state: entry?.state ?? 'idle',
|
|
408
|
-
// "open" = any of the
|
|
409
|
-
// fully dialed. `connecting` means a DIAL is in flight (the
|
|
410
|
-
// exists before the Client does), so that is not an open
|
|
677
|
+
// "open" = any of the FOUR connection classes (shared / PTY / forward /
|
|
678
|
+
// SFTP) is fully dialed. `connecting` means a DIAL is in flight (the
|
|
679
|
+
// pooled entry exists before the Client does), so that is not an open
|
|
680
|
+
// connection yet.
|
|
411
681
|
open: (pooled !== undefined && pooled.connecting === undefined)
|
|
412
682
|
|| (this.shellConns.get(alias)?.connecting === undefined && this.shellConns.has(alias))
|
|
413
|
-
|| (this.forwardConns.get(alias)?.connecting === undefined && this.forwardConns.has(alias))
|
|
683
|
+
|| (this.forwardConns.get(alias)?.connecting === undefined && this.forwardConns.has(alias))
|
|
684
|
+
|| (this.sftpConns.get(alias)?.connecting === undefined && this.sftpConns.has(alias)),
|
|
414
685
|
...(entry?.okAt === undefined ? {} : { okAt: entry.okAt }),
|
|
415
686
|
...(entry?.errorAt === undefined ? {} : { errorAt: entry.errorAt }),
|
|
416
687
|
...(entry?.error === undefined ? {} : { error: entry.error }),
|
|
@@ -420,15 +691,87 @@ export class RemotePool {
|
|
|
420
691
|
})
|
|
421
692
|
}
|
|
422
693
|
|
|
694
|
+
/**
|
|
695
|
+
* Live pool shape per host: connections, channels, and channel-budget
|
|
696
|
+
* occupancy (see `RemoteHostPoolStats`).
|
|
697
|
+
*
|
|
698
|
+
* Same contract as `status()`: read-only and dial-free, so the monitor's poll
|
|
699
|
+
* reports what the pool already knows instead of creating work. Answers every
|
|
700
|
+
* alias the store knows PLUS every alias the pool holds something for — a host
|
|
701
|
+
* removed from the store still has a connection for a moment, and a page that
|
|
702
|
+
* shows connections must not hide exactly those.
|
|
703
|
+
*/
|
|
704
|
+
stats(): RemoteHostPoolStats[] {
|
|
705
|
+
const aliases = new Set<string>([
|
|
706
|
+
...this.conns.keys(),
|
|
707
|
+
...this.shellConns.keys(),
|
|
708
|
+
...this.forwardConns.keys(),
|
|
709
|
+
...this.sftpConns.keys(),
|
|
710
|
+
...this.health.keys(),
|
|
711
|
+
])
|
|
712
|
+
try {
|
|
713
|
+
for (const host of this.store.list()) aliases.add(host.alias)
|
|
714
|
+
} catch {
|
|
715
|
+
// Unreadable store: answer what the pool knows rather than failing the route.
|
|
716
|
+
}
|
|
717
|
+
return [...aliases].sort().map((alias) => {
|
|
718
|
+
const shared = this.conns.get(alias)
|
|
719
|
+
const sharedOpen = shared !== undefined && shared.connecting === undefined
|
|
720
|
+
return {
|
|
721
|
+
alias,
|
|
722
|
+
connections: {
|
|
723
|
+
shared: this.laneOf(this.conns, alias),
|
|
724
|
+
pty: this.laneOf(this.shellConns, alias),
|
|
725
|
+
forward: this.laneOf(this.forwardConns, alias),
|
|
726
|
+
sftp: this.laneOf(this.sftpConns, alias),
|
|
727
|
+
},
|
|
728
|
+
slots: this.slotsOf(this.slots.get(alias)),
|
|
729
|
+
// Absent while the dedicated SFTP connection has never been used: a zero
|
|
730
|
+
// budget would read as "it exists and is empty", which is a different
|
|
731
|
+
// statement from "this lane is not in play".
|
|
732
|
+
...(this.sftpSlots.has(alias) ? { sftpSlots: this.slotsOf(this.sftpSlots.get(alias), SFTP_CHANNEL_LIMIT) } : {}),
|
|
733
|
+
// Same rule for the network lane: no network verb running and none queued
|
|
734
|
+
// means there is no row (a queued verb implies a live row, because a waiter
|
|
735
|
+
// is only enqueued behind a full table).
|
|
736
|
+
...(this.netSlots.has(alias) ? { netSlots: this.slotsOf(this.netSlots.get(alias), NET_CHANNEL_LIMIT) } : {}),
|
|
737
|
+
// …and for git commands.
|
|
738
|
+
...(this.gitSlots.has(alias) ? { gitSlots: this.slotsOf(this.gitSlots.get(alias), GIT_CHANNEL_LIMIT) } : {}),
|
|
739
|
+
// Only a DIALED shared connection has a route to report (the same rule as
|
|
740
|
+
// `status()`): a route belongs to the link that took it.
|
|
741
|
+
...(sharedOpen && shared.egress !== undefined ? { egress: shared.egress } : {}),
|
|
742
|
+
...(this.sftpFallback.has(alias) ? { sftpFallback: true } : {}),
|
|
743
|
+
}
|
|
744
|
+
})
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
/** One lane of one host: dialed or not, its open channels, its pinned rounds. */
|
|
748
|
+
private laneOf(map: Map<string, Pooled>, alias: string): RemotePoolLane {
|
|
749
|
+
const pooled = map.get(alias)
|
|
750
|
+
if (pooled === undefined) return { open: false, channels: 0, held: 0 }
|
|
751
|
+
return {
|
|
752
|
+
// A dialing entry exists before its Client does (same rule as `status`).
|
|
753
|
+
open: pooled.connecting === undefined,
|
|
754
|
+
// A gate seeds a pooled entry by hand, and that literal has no counter
|
|
755
|
+
// (the map is plain JS at runtime): start it at zero instead of NaN.
|
|
756
|
+
channels: (pooled.channels as number | undefined) ?? 0,
|
|
757
|
+
held: pooled.held,
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
/** One channel budget, as reported (a budget nobody touched is empty, not absent). */
|
|
762
|
+
private slotsOf(state: Slots | undefined, limit = CHANNEL_LIMIT): RemotePoolSlots {
|
|
763
|
+
return { active: state?.active ?? 0, queued: state?.queue.length ?? 0, limit }
|
|
764
|
+
}
|
|
765
|
+
|
|
423
766
|
/**
|
|
424
767
|
* The egress of the alias' LIVE connection, whichever class owns it.
|
|
425
768
|
*
|
|
426
|
-
* All
|
|
427
|
-
*
|
|
428
|
-
*
|
|
769
|
+
* All four classes open through the same resolved route, so the first one that
|
|
770
|
+
* is up answers for the alias; the shared connection is preferred because it is
|
|
771
|
+
* the one exec/browse (the surface the chip describes) rides.
|
|
429
772
|
*/
|
|
430
773
|
private liveEgress(alias: string): EgressInfo | undefined {
|
|
431
|
-
for (const map of [this.conns, this.forwardConns, this.shellConns]) {
|
|
774
|
+
for (const map of [this.conns, this.forwardConns, this.shellConns, this.sftpConns]) {
|
|
432
775
|
const pooled = map.get(alias)
|
|
433
776
|
if (pooled === undefined || pooled.connecting !== undefined) continue
|
|
434
777
|
if (pooled.egress !== undefined) return pooled.egress
|
|
@@ -536,12 +879,18 @@ export class RemotePool {
|
|
|
536
879
|
* of queueing. The pool's own housekeeping (`bare`: the setsid probe and the
|
|
537
880
|
* group kill) is exempt — a kill that waits behind user traffic would be a
|
|
538
881
|
* worse bug than the one it fixes.
|
|
882
|
+
*
|
|
883
|
+
* `opts.lane` picks the budget: network verbs (`git fetch/pull/push/sync`) have
|
|
884
|
+
* their own small one, so a 20-minute push cannot starve the exec slots.
|
|
885
|
+
* Acquisition and release read the SAME table — a mismatch would leak a slot on
|
|
886
|
+
* one budget and double-count on the other.
|
|
539
887
|
*/
|
|
540
888
|
async exec(alias: string, command: string, opts: ExecOptions = {}): Promise<RemoteExecResult> {
|
|
541
889
|
if (opts.bare === true) return this.runExec(alias, command, opts)
|
|
542
890
|
const started = Date.now()
|
|
891
|
+
const slots = this.execBudget(opts.lane)
|
|
543
892
|
try {
|
|
544
|
-
await this.acquireSlot(alias)
|
|
893
|
+
await this.acquireSlot(alias, slots)
|
|
545
894
|
} catch (error) {
|
|
546
895
|
return {
|
|
547
896
|
success: false, exitCode: null, timedOut: false, stdout: '', stderr: '',
|
|
@@ -561,10 +910,17 @@ export class RemotePool {
|
|
|
561
910
|
else if (result.success || result.exitCode !== null) this.markOk(alias)
|
|
562
911
|
return result
|
|
563
912
|
} finally {
|
|
564
|
-
this.releaseSlot(alias)
|
|
913
|
+
this.releaseSlot(alias, slots)
|
|
565
914
|
}
|
|
566
915
|
}
|
|
567
916
|
|
|
917
|
+
/** The channel budget an exec spends, by lane (see `ExecOptions.lane`). */
|
|
918
|
+
private execBudget(lane: ExecOptions['lane']): Map<string, Slots> {
|
|
919
|
+
if (lane === 'network') return this.netSlots
|
|
920
|
+
if (lane === 'git') return this.gitSlots
|
|
921
|
+
return this.slots
|
|
922
|
+
}
|
|
923
|
+
|
|
568
924
|
/** The exec body, run while the caller already holds a channel slot. */
|
|
569
925
|
private async runExec(alias: string, command: string, opts: ExecOptions = {}): Promise<RemoteExecResult> {
|
|
570
926
|
const started = Date.now()
|
|
@@ -696,6 +1052,7 @@ export class RemotePool {
|
|
|
696
1052
|
} else {
|
|
697
1053
|
stream = channel
|
|
698
1054
|
}
|
|
1055
|
+
this.countChannel(this.conns, alias, channel)
|
|
699
1056
|
channel.on('close', (code: number | null, signalName?: string | null) => {
|
|
700
1057
|
// An exit status is the server's proof the child is gone; the close
|
|
701
1058
|
// we send ourselves is not.
|
|
@@ -808,6 +1165,7 @@ export class RemotePool {
|
|
|
808
1165
|
if (dedicated) this.releaseIn(this.shellConns, alias)
|
|
809
1166
|
else this.release(alias)
|
|
810
1167
|
})
|
|
1168
|
+
this.countChannel(dedicated ? this.shellConns : this.conns, alias, channel)
|
|
811
1169
|
this.markOk(alias)
|
|
812
1170
|
return channel
|
|
813
1171
|
} catch (error) {
|
|
@@ -818,10 +1176,37 @@ export class RemotePool {
|
|
|
818
1176
|
}
|
|
819
1177
|
}
|
|
820
1178
|
|
|
821
|
-
/**
|
|
1179
|
+
/**
|
|
1180
|
+
* Open one exec channel. The caller owns the channel lifecycle.
|
|
1181
|
+
*
|
|
1182
|
+
* This channel spends an EXEC SLOT (same budget as `runExec`): it is a real
|
|
1183
|
+
* `conn.exec` session on the shared connection, so `MaxSessions` counts it
|
|
1184
|
+
* whether the pool does or not. The subprocess runtime opens these for every
|
|
1185
|
+
* command it runs (`p2/ssh-proc.ts`) and can have several in flight, which used
|
|
1186
|
+
* to be the one way to push the shared connection past its ceiling: sshd then
|
|
1187
|
+
* refuses the extra channel HARD — no retry, and the operation fails looking
|
|
1188
|
+
* like a broken host. A slot makes the surplus WAIT instead: bounded queue, then
|
|
1189
|
+
* a readable "busy" message naming the cause.
|
|
1190
|
+
*/
|
|
822
1191
|
async execChannel(alias: string, command: string): Promise<ClientChannel> {
|
|
823
|
-
const
|
|
1192
|
+
const slots = this.slots
|
|
1193
|
+
await this.acquireSlot(alias, slots)
|
|
1194
|
+
let conn: Client
|
|
1195
|
+
try {
|
|
1196
|
+
conn = await this.acquire(alias)
|
|
1197
|
+
} catch (error) {
|
|
1198
|
+
this.releaseSlot(alias, slots)
|
|
1199
|
+
throw error
|
|
1200
|
+
}
|
|
824
1201
|
this.hold(alias)
|
|
1202
|
+
let released = false
|
|
1203
|
+
/** Give the slot and the hold back exactly once (close and error may both fire). */
|
|
1204
|
+
const releaseOnce = (): void => {
|
|
1205
|
+
if (released) return
|
|
1206
|
+
released = true
|
|
1207
|
+
this.release(alias)
|
|
1208
|
+
this.releaseSlot(alias, slots)
|
|
1209
|
+
}
|
|
825
1210
|
try {
|
|
826
1211
|
const channel = await new Promise<ClientChannel>((resolve, reject) => {
|
|
827
1212
|
conn.exec(command, (error, stream) => {
|
|
@@ -829,11 +1214,13 @@ export class RemotePool {
|
|
|
829
1214
|
else resolve(stream)
|
|
830
1215
|
})
|
|
831
1216
|
})
|
|
832
|
-
channel.once('close',
|
|
1217
|
+
channel.once('close', releaseOnce)
|
|
1218
|
+
channel.once('error', releaseOnce)
|
|
1219
|
+
this.countChannel(this.conns, alias, channel)
|
|
833
1220
|
this.markOk(alias)
|
|
834
1221
|
return channel
|
|
835
1222
|
} catch (error) {
|
|
836
|
-
|
|
1223
|
+
releaseOnce()
|
|
837
1224
|
if (isConnectionFailure(error)) this.markError(alias, error)
|
|
838
1225
|
throw error
|
|
839
1226
|
}
|
|
@@ -934,6 +1321,7 @@ export class RemotePool {
|
|
|
934
1321
|
if (dedicated) this.releaseIn(this.forwardConns, alias)
|
|
935
1322
|
else this.release(alias)
|
|
936
1323
|
})
|
|
1324
|
+
this.countChannel(dedicated ? this.forwardConns : this.conns, alias, channel)
|
|
937
1325
|
// Only the SHARED path is evidence about the shared link. A dedicated
|
|
938
1326
|
// channel that opened says nothing about it (and the dedicated connection's
|
|
939
1327
|
// own failures never write health — see `acquireIn`'s `quiet`).
|
|
@@ -992,22 +1380,20 @@ export class RemotePool {
|
|
|
992
1380
|
|
|
993
1381
|
/** Run one callback with a fresh SFTP session (closed afterwards). */
|
|
994
1382
|
async withSftp<T>(alias: string, fn: (sftp: SFTPWrapper) => Promise<T>, opts: SftpOptions = {}): Promise<T> {
|
|
995
|
-
// An SFTP session IS a channel, so it takes a slot too
|
|
996
|
-
//
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
throw error
|
|
1004
|
-
}
|
|
1383
|
+
// An SFTP session IS a channel, so it takes a slot too — on the budget of the
|
|
1384
|
+
// connection it really rides (the dedicated SFTP one, or the shared one when
|
|
1385
|
+
// that dial was refused). Never both: the slot protects ONE connection's
|
|
1386
|
+
// `MaxSessions` allowance.
|
|
1387
|
+
const lane = await this.acquireSftp(alias)
|
|
1388
|
+
const slots = this.laneSlots(lane.lane)
|
|
1389
|
+
const connMap = this.laneConns(lane.lane)
|
|
1390
|
+
await this.acquireSlot(alias, slots)
|
|
1005
1391
|
// Hold BEFORE the first await: holding after it leaves a window where the
|
|
1006
1392
|
// connection has no hold count and is not yet recorded as in use, so the
|
|
1007
1393
|
// idle timer armed by a concurrent release() could evict it mid-call.
|
|
1008
|
-
this.
|
|
1394
|
+
this.holdIn(connMap, alias)
|
|
1009
1395
|
try {
|
|
1010
|
-
const sftp = await this.openSession(conn)
|
|
1396
|
+
const sftp = await this.openSession(lane.conn, alias, connMap)
|
|
1011
1397
|
try {
|
|
1012
1398
|
const limit = opts.timeoutMs ?? SFTP_OP_TIMEOUT_MS
|
|
1013
1399
|
const result = limit <= 0
|
|
@@ -1026,8 +1412,8 @@ export class RemotePool {
|
|
|
1026
1412
|
this.noteOpFailure(alias, error)
|
|
1027
1413
|
throw error
|
|
1028
1414
|
} finally {
|
|
1029
|
-
this.
|
|
1030
|
-
this.releaseSlot(alias)
|
|
1415
|
+
this.releaseIn(connMap, alias)
|
|
1416
|
+
this.releaseSlot(alias, slots)
|
|
1031
1417
|
}
|
|
1032
1418
|
}
|
|
1033
1419
|
|
|
@@ -1064,30 +1450,38 @@ export class RemotePool {
|
|
|
1064
1450
|
const sessions: SFTPWrapper[] = []
|
|
1065
1451
|
let slots = 0
|
|
1066
1452
|
let holds = 0
|
|
1453
|
+
/** Which lane the transfer got; undefined until the acquisition succeeded. */
|
|
1454
|
+
let lane: SftpLane | undefined
|
|
1067
1455
|
/** Set when the callback settles: a late `addChannel` must not leak resources. */
|
|
1068
1456
|
let closed = false
|
|
1069
|
-
let conn: Client
|
|
1070
1457
|
try {
|
|
1458
|
+
// ONE lane decision for the whole transfer: every extra channel must ride
|
|
1459
|
+
// the same connection as the first one (a second connection would double
|
|
1460
|
+
// the handshake the transfer exists to amortise), and every session counts
|
|
1461
|
+
// on that connection's own budget.
|
|
1462
|
+
const lease = await this.acquireSftp(alias)
|
|
1463
|
+
lane = lease.lane
|
|
1464
|
+
const slotTable = this.laneSlots(lease.lane)
|
|
1465
|
+
const connMap = this.laneConns(lease.lane)
|
|
1071
1466
|
// An SFTP session IS a channel, so each one takes a slot: the ceiling this
|
|
1072
1467
|
// protects is the server's MaxSessions, not our own bookkeeping.
|
|
1073
|
-
await this.acquireSlot(alias)
|
|
1468
|
+
await this.acquireSlot(alias, slotTable)
|
|
1074
1469
|
slots += 1
|
|
1075
|
-
conn = await this.acquire(alias)
|
|
1076
1470
|
// Hold BEFORE the first await (same reason as withSftp).
|
|
1077
|
-
this.
|
|
1471
|
+
this.holdIn(connMap, alias)
|
|
1078
1472
|
holds += 1
|
|
1079
|
-
sessions.push(await this.openSession(conn))
|
|
1473
|
+
sessions.push(await this.openSession(lease.conn, alias, connMap))
|
|
1080
1474
|
const addChannel = async (): Promise<SFTPWrapper | undefined> => {
|
|
1081
1475
|
if (closed) return undefined
|
|
1082
1476
|
// NEVER queue for an optional channel: a busy host would otherwise delay
|
|
1083
1477
|
// the body of a download that is already streaming by up to
|
|
1084
1478
|
// CHANNEL_WAIT_MS, which is worse than one channel.
|
|
1085
|
-
if (!this.tryAcquireSlot(alias)) return undefined
|
|
1479
|
+
if (!this.tryAcquireSlot(alias, slotTable)) return undefined
|
|
1086
1480
|
slots += 1
|
|
1087
|
-
this.
|
|
1481
|
+
this.holdIn(connMap, alias)
|
|
1088
1482
|
holds += 1
|
|
1089
1483
|
try {
|
|
1090
|
-
const extra = await this.openSession(conn)
|
|
1484
|
+
const extra = await this.openSession(lease.conn, alias, connMap)
|
|
1091
1485
|
if (closed) {
|
|
1092
1486
|
// The transfer settled while this channel was opening; the finally
|
|
1093
1487
|
// already returned its slot and hold, so close it here.
|
|
@@ -1121,8 +1515,12 @@ export class RemotePool {
|
|
|
1121
1515
|
// Ignore close errors.
|
|
1122
1516
|
}
|
|
1123
1517
|
}
|
|
1124
|
-
|
|
1125
|
-
|
|
1518
|
+
// The lane is decided inside the try; a failed acquisition took nothing, so
|
|
1519
|
+
// there is nothing to give back for it either.
|
|
1520
|
+
if (lane !== undefined) {
|
|
1521
|
+
for (let i = 0; i < holds; i += 1) this.releaseIn(this.laneConns(lane), alias)
|
|
1522
|
+
for (let i = 0; i < slots; i += 1) this.releaseSlot(alias, this.laneSlots(lane))
|
|
1523
|
+
}
|
|
1126
1524
|
}
|
|
1127
1525
|
}
|
|
1128
1526
|
|
|
@@ -1247,8 +1645,11 @@ export class RemotePool {
|
|
|
1247
1645
|
* place that can still see the session, and a late one would otherwise leak a
|
|
1248
1646
|
* server-side channel that no slot or hold accounts for (the caller has long
|
|
1249
1647
|
* returned its own and moved on).
|
|
1648
|
+
*
|
|
1649
|
+
* `alias`/`map` name the connection the session rides, so the channel it holds
|
|
1650
|
+
* is counted on the right lane (see `countChannel`).
|
|
1250
1651
|
*/
|
|
1251
|
-
private openSession(conn: Client): Promise<SFTPWrapper> {
|
|
1652
|
+
private openSession(conn: Client, alias: string, map: Map<string, Pooled>): Promise<SFTPWrapper> {
|
|
1252
1653
|
return new Promise<SFTPWrapper>((resolve, reject) => {
|
|
1253
1654
|
let settled = false
|
|
1254
1655
|
const timer = setTimeout(() => {
|
|
@@ -1269,8 +1670,14 @@ export class RemotePool {
|
|
|
1269
1670
|
}
|
|
1270
1671
|
settled = true
|
|
1271
1672
|
clearTimeout(timer)
|
|
1272
|
-
if (error !== undefined && error !== null)
|
|
1273
|
-
|
|
1673
|
+
if (error !== undefined && error !== null) {
|
|
1674
|
+
reject(error)
|
|
1675
|
+
return
|
|
1676
|
+
}
|
|
1677
|
+
// Counted only when it is really handed over: a session that answers
|
|
1678
|
+
// after the deadline is closed above and was never one of ours.
|
|
1679
|
+
this.countChannel(map, alias, session as SFTPWrapper)
|
|
1680
|
+
resolve(session as SFTPWrapper)
|
|
1274
1681
|
}
|
|
1275
1682
|
try {
|
|
1276
1683
|
conn.sftp((error, session) => finish(error, session))
|
|
@@ -1326,22 +1733,23 @@ export class RemotePool {
|
|
|
1326
1733
|
/**
|
|
1327
1734
|
* Open the browse session: a channel slot + a connection hold + the SFTP
|
|
1328
1735
|
* session itself, all released together by evictBrowse().
|
|
1736
|
+
*
|
|
1737
|
+
* The lane is chosen HERE, once per session, and recorded on the state: the
|
|
1738
|
+
* eviction path must give the slot and the hold back to the connection that
|
|
1739
|
+
* actually served the session (dedicated SFTP, or the shared fallback).
|
|
1329
1740
|
*/
|
|
1330
1741
|
private async openBrowse(alias: string, state: BrowseChannel): Promise<SFTPWrapper> {
|
|
1331
|
-
await this.
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
this.releaseSlot(alias)
|
|
1337
|
-
throw error
|
|
1338
|
-
}
|
|
1742
|
+
const lane = await this.acquireSftp(alias)
|
|
1743
|
+
state.lane = lane.lane
|
|
1744
|
+
const slots = this.laneSlots(lane.lane)
|
|
1745
|
+
const connMap = this.laneConns(lane.lane)
|
|
1746
|
+
await this.acquireSlot(alias, slots)
|
|
1339
1747
|
// Hold before the first await, for the same reason withSftp does: a
|
|
1340
1748
|
// concurrent release() must not arm idle eviction mid-open.
|
|
1341
|
-
this.
|
|
1749
|
+
this.holdIn(connMap, alias)
|
|
1342
1750
|
state.leased = true
|
|
1343
1751
|
try {
|
|
1344
|
-
const sftp = await this.openSession(conn)
|
|
1752
|
+
const sftp = await this.openSession(lane.conn, alias, connMap)
|
|
1345
1753
|
// The host can be dropped while the session is opening: the slot and hold
|
|
1346
1754
|
// are already returned and nobody would ever close this channel, so refuse
|
|
1347
1755
|
// it instead of handing out a session on a dead connection.
|
|
@@ -1419,27 +1827,41 @@ export class RemotePool {
|
|
|
1419
1827
|
}
|
|
1420
1828
|
if (state.leased) {
|
|
1421
1829
|
state.leased = false
|
|
1422
|
-
|
|
1423
|
-
|
|
1830
|
+
// Back to the connection that served it: the dedicated SFTP one, or the
|
|
1831
|
+
// shared fallback (never both — that was the lane decision in openBrowse).
|
|
1832
|
+
this.releaseIn(this.laneConns(state.lane), alias)
|
|
1833
|
+
this.releaseSlot(alias, this.laneSlots(state.lane))
|
|
1424
1834
|
}
|
|
1425
1835
|
}
|
|
1426
1836
|
|
|
1427
1837
|
private browseState(alias: string): BrowseChannel {
|
|
1428
1838
|
const existing = this.browsed.get(alias)
|
|
1429
1839
|
if (existing !== undefined) return existing
|
|
1430
|
-
const created: BrowseChannel = {
|
|
1840
|
+
const created: BrowseChannel = {
|
|
1841
|
+
session: undefined,
|
|
1842
|
+
opening: undefined,
|
|
1843
|
+
active: 0,
|
|
1844
|
+
stale: false,
|
|
1845
|
+
leased: false,
|
|
1846
|
+
lane: 'shared',
|
|
1847
|
+
idle: undefined,
|
|
1848
|
+
}
|
|
1431
1849
|
this.browsed.set(alias, created)
|
|
1432
1850
|
return created
|
|
1433
1851
|
}
|
|
1434
1852
|
|
|
1435
1853
|
/**
|
|
1436
|
-
* Take one channel slot for `alias
|
|
1437
|
-
* The wait is bounded: waiting forever behind a peer
|
|
1438
|
-
* worse than a readable error naming the cause
|
|
1854
|
+
* Take one channel slot for `alias` on the budget `slots` names, waiting FIFO
|
|
1855
|
+
* when the limit is reached. The wait is bounded: waiting forever behind a peer
|
|
1856
|
+
* that never finishes is worse than a readable error naming the cause.
|
|
1857
|
+
*
|
|
1858
|
+
* `slots` defaults to the SHARED connection's exec budget — the table every
|
|
1859
|
+
* caller meant before the lanes existed; dedicated-lane callers pass theirs.
|
|
1439
1860
|
*/
|
|
1440
|
-
private async acquireSlot(alias: string): Promise<void> {
|
|
1441
|
-
const
|
|
1442
|
-
|
|
1861
|
+
private async acquireSlot(alias: string, slots: Map<string, Slots> = this.slots): Promise<void> {
|
|
1862
|
+
const { limit, noun } = this.budgetOf(slots)
|
|
1863
|
+
const state = this.slotState(alias, slots)
|
|
1864
|
+
if (state.active < limit) {
|
|
1443
1865
|
state.active += 1
|
|
1444
1866
|
return
|
|
1445
1867
|
}
|
|
@@ -1454,7 +1876,7 @@ export class RemotePool {
|
|
|
1454
1876
|
const index = state.queue.indexOf(waiter)
|
|
1455
1877
|
if (index >= 0) state.queue.splice(index, 1)
|
|
1456
1878
|
reject(new Error(
|
|
1457
|
-
`remote host ${alias} is busy: ${
|
|
1879
|
+
`remote host ${alias} is busy: ${limit} ${noun} already hold channels `
|
|
1458
1880
|
+ `(this one waited ${CHANNEL_WAIT_MS / 1000}s — retry once the earlier work finishes)`,
|
|
1459
1881
|
))
|
|
1460
1882
|
}, CHANNEL_WAIT_MS)
|
|
@@ -1463,6 +1885,25 @@ export class RemotePool {
|
|
|
1463
1885
|
})
|
|
1464
1886
|
}
|
|
1465
1887
|
|
|
1888
|
+
/**
|
|
1889
|
+
* The ceiling and the wording of one budget.
|
|
1890
|
+
*
|
|
1891
|
+
* The TABLE is the budget's identity: a caller that hands in a table gets that
|
|
1892
|
+
* budget's own ceiling AND its own nouns, so a full network lane says "2
|
|
1893
|
+
* network operations already hold channels" and a full git lane says "2 git
|
|
1894
|
+
* commands already hold channels" instead of borrowing the exec sentence — a
|
|
1895
|
+
* user reading "5 operations" about a two-slot lane would go looking for
|
|
1896
|
+
* operations that do not exist. A new budget is described here, once.
|
|
1897
|
+
*/
|
|
1898
|
+
private budgetOf(slots: Map<string, Slots>): { limit: number; noun: string } {
|
|
1899
|
+
if (slots === this.netSlots) return { limit: NET_CHANNEL_LIMIT, noun: 'network operations' }
|
|
1900
|
+
if (slots === this.gitSlots) return { limit: GIT_CHANNEL_LIMIT, noun: 'git commands' }
|
|
1901
|
+
// The dedicated file connection is not part of the shared-connection ledger,
|
|
1902
|
+
// so it keeps the ceiling the transfer measurement landed on.
|
|
1903
|
+
if (slots === this.sftpSlots) return { limit: SFTP_CHANNEL_LIMIT, noun: 'operations' }
|
|
1904
|
+
return { limit: CHANNEL_LIMIT, noun: 'operations' }
|
|
1905
|
+
}
|
|
1906
|
+
|
|
1466
1907
|
/**
|
|
1467
1908
|
* Take one channel slot only if one is free RIGHT NOW; never queue.
|
|
1468
1909
|
*
|
|
@@ -1473,16 +1914,17 @@ export class RemotePool {
|
|
|
1473
1914
|
* the attempt on purpose — it has been waiting longer, and stealing its slot
|
|
1474
1915
|
* would just move the wait.
|
|
1475
1916
|
*/
|
|
1476
|
-
private tryAcquireSlot(alias: string): boolean {
|
|
1477
|
-
const
|
|
1478
|
-
|
|
1917
|
+
private tryAcquireSlot(alias: string, slots: Map<string, Slots> = this.slots): boolean {
|
|
1918
|
+
const { limit } = this.budgetOf(slots)
|
|
1919
|
+
const state = this.slotState(alias, slots)
|
|
1920
|
+
if (state.active >= limit || state.queue.length > 0) return false
|
|
1479
1921
|
state.active += 1
|
|
1480
1922
|
return true
|
|
1481
1923
|
}
|
|
1482
1924
|
|
|
1483
|
-
/** Give a slot back, handing it straight to the longest
|
|
1484
|
-
private releaseSlot(alias: string): void {
|
|
1485
|
-
const state =
|
|
1925
|
+
/** Give a slot back to its budget, handing it straight to the longest waiter. */
|
|
1926
|
+
private releaseSlot(alias: string, slots: Map<string, Slots> = this.slots): void {
|
|
1927
|
+
const state = slots.get(alias)
|
|
1486
1928
|
if (state === undefined) return
|
|
1487
1929
|
const waiter = state.queue.shift()
|
|
1488
1930
|
if (waiter !== undefined) {
|
|
@@ -1492,14 +1934,14 @@ export class RemotePool {
|
|
|
1492
1934
|
return
|
|
1493
1935
|
}
|
|
1494
1936
|
if (state.active > 0) state.active -= 1
|
|
1495
|
-
if (state.active === 0)
|
|
1937
|
+
if (state.active === 0) slots.delete(alias)
|
|
1496
1938
|
}
|
|
1497
1939
|
|
|
1498
|
-
private slotState(alias: string): Slots {
|
|
1499
|
-
const existing =
|
|
1940
|
+
private slotState(alias: string, slots: Map<string, Slots>): Slots {
|
|
1941
|
+
const existing = slots.get(alias)
|
|
1500
1942
|
if (existing !== undefined) return existing
|
|
1501
1943
|
const created: Slots = { active: 0, queue: [] }
|
|
1502
|
-
|
|
1944
|
+
slots.set(alias, created)
|
|
1503
1945
|
return created
|
|
1504
1946
|
}
|
|
1505
1947
|
|
|
@@ -1558,6 +2000,42 @@ export class RemotePool {
|
|
|
1558
2000
|
this.releaseIn(this.conns, alias)
|
|
1559
2001
|
}
|
|
1560
2002
|
|
|
2003
|
+
/**
|
|
2004
|
+
* Count one SSH channel that is now open on a pooled connection, and give the
|
|
2005
|
+
* number back when the channel closes.
|
|
2006
|
+
*
|
|
2007
|
+
* `held` is NOT this number (see `Pooled.channels`): a jump tunnel pins the
|
|
2008
|
+
* jump host twice for its single channel, and `stats()` reports both, so the
|
|
2009
|
+
* monitor page never prints a hold count as a channel count. The close listener
|
|
2010
|
+
* mutates the entry OBJECT it was registered on, so a channel that outlives its
|
|
2011
|
+
* entry cannot decrement the counter of a successor entry.
|
|
2012
|
+
*
|
|
2013
|
+
* The entry is looked up when the channel is COUNTED, so a session that answers
|
|
2014
|
+
* pathologically late (after `drop()` and a fresh dial) is charged to the
|
|
2015
|
+
* successor entry and given back when it closes. Accepted: the window needs a
|
|
2016
|
+
* reply that outlives a host edit, the number is diagnostic, and the pair is
|
|
2017
|
+
* still symmetric (one increment, one decrement).
|
|
2018
|
+
*/
|
|
2019
|
+
private countChannel(
|
|
2020
|
+
map: Map<string, Pooled>,
|
|
2021
|
+
alias: string,
|
|
2022
|
+
channel: { on?: (event: 'close', listener: () => void) => unknown },
|
|
2023
|
+
): void {
|
|
2024
|
+
const pooled = map.get(alias)
|
|
2025
|
+
// Nothing to count against, or a stand-in channel that cannot report its own
|
|
2026
|
+
// close: counting THAT would leave a number nothing can ever give back.
|
|
2027
|
+
if (pooled === undefined || typeof channel.on !== 'function') return
|
|
2028
|
+
// A gate seeds a pooled entry by hand, and that literal has no counter (the
|
|
2029
|
+
// map is plain JS at runtime): start it at zero instead of producing NaN.
|
|
2030
|
+
pooled.channels = ((pooled.channels as number | undefined) ?? 0) + 1
|
|
2031
|
+
let counted = true
|
|
2032
|
+
channel.on('close', () => {
|
|
2033
|
+
if (!counted) return
|
|
2034
|
+
counted = false
|
|
2035
|
+
if (pooled.channels > 0) pooled.channels -= 1
|
|
2036
|
+
})
|
|
2037
|
+
}
|
|
2038
|
+
|
|
1561
2039
|
private releaseIn(map: Map<string, Pooled>, alias: string): void {
|
|
1562
2040
|
const pooled = map.get(alias)
|
|
1563
2041
|
if (pooled === undefined) return
|
|
@@ -1612,6 +2090,91 @@ export class RemotePool {
|
|
|
1612
2090
|
}
|
|
1613
2091
|
}
|
|
1614
2092
|
|
|
2093
|
+
/**
|
|
2094
|
+
* Take the connection a FILE operation should ride: the dedicated SFTP one,
|
|
2095
|
+
* else the shared one (recorded as the fallback).
|
|
2096
|
+
*
|
|
2097
|
+
* Same three-part shape as the PTY/forward precedents, for the same reasons:
|
|
2098
|
+
*
|
|
2099
|
+
* 1. `quiet` on the dial — a dedicated connection is an optional isolation, so
|
|
2100
|
+
* the server refusing it (MaxStartups, a per-user limit) says nothing about
|
|
2101
|
+
* the shared link and must not paint the connection chip red.
|
|
2102
|
+
* 2. A short own budget ({@link SFTP_DEDICATED_BUDGET_MS}) — browsing is one
|
|
2103
|
+
* request per folder, so waiting out the 45 s connect watchdog before falling
|
|
2104
|
+
* back would freeze the explorer. The abandoned dial keeps running: if it
|
|
2105
|
+
* eventually succeeds the connection is pooled and the next request uses it.
|
|
2106
|
+
* 3. A cooldown ({@link SFTP_DEDICATED_BLOCK_MS}) — without it every browse
|
|
2107
|
+
* request would pay that budget again. A success or `drop()` clears it.
|
|
2108
|
+
*
|
|
2109
|
+
* The shared fallback's own dial failure is NOT quiet (it is a real link
|
|
2110
|
+
* failure and `acquire` reports it as one).
|
|
2111
|
+
*/
|
|
2112
|
+
private async acquireSftp(alias: string): Promise<{ conn: Client; lane: SftpLane }> {
|
|
2113
|
+
if (!this.sftpCoolingDown(alias)) {
|
|
2114
|
+
const dialing = this.acquireIn(this.sftpConns, alias, true)
|
|
2115
|
+
try {
|
|
2116
|
+
const conn = await withDeadline(
|
|
2117
|
+
dialing,
|
|
2118
|
+
SFTP_DEDICATED_BUDGET_MS,
|
|
2119
|
+
`dedicated file connection to ${alias} did not settle within ${SFTP_DEDICATED_BUDGET_MS / 1000}s`,
|
|
2120
|
+
)
|
|
2121
|
+
// A real dedicated connection clears the downgrade: the host accepts them.
|
|
2122
|
+
this.sftpBlocked.delete(alias)
|
|
2123
|
+
this.sftpFallback.delete(alias)
|
|
2124
|
+
return { conn, lane: 'sftp' }
|
|
2125
|
+
} catch (error) {
|
|
2126
|
+
// The race's loser must never surface as an unhandled rejection: the pool
|
|
2127
|
+
// still owns it (its own success/failure updates the map).
|
|
2128
|
+
void dialing.catch(() => undefined)
|
|
2129
|
+
this.blockSftp(alias, error)
|
|
2130
|
+
}
|
|
2131
|
+
}
|
|
2132
|
+
const conn = await this.acquire(alias)
|
|
2133
|
+
this.sftpFallback.add(alias)
|
|
2134
|
+
return { conn, lane: 'shared' }
|
|
2135
|
+
}
|
|
2136
|
+
|
|
2137
|
+
/** The pooled map behind a lane: the dedicated SFTP connection, or the shared one. */
|
|
2138
|
+
private laneConns(lane: SftpLane): Map<string, Pooled> {
|
|
2139
|
+
return lane === 'sftp' ? this.sftpConns : this.conns
|
|
2140
|
+
}
|
|
2141
|
+
|
|
2142
|
+
/** The channel budget behind a lane: each connection has its own `MaxSessions`. */
|
|
2143
|
+
private laneSlots(lane: SftpLane): Map<string, Slots> {
|
|
2144
|
+
return lane === 'sftp' ? this.sftpSlots : this.slots
|
|
2145
|
+
}
|
|
2146
|
+
|
|
2147
|
+
/**
|
|
2148
|
+
* Is this alias's dedicated SFTP connection in its short cooldown?
|
|
2149
|
+
*
|
|
2150
|
+
* Expired entries are dropped eagerly so the map cannot grow per alias forever.
|
|
2151
|
+
*/
|
|
2152
|
+
private sftpCoolingDown(alias: string): boolean {
|
|
2153
|
+
const until = this.sftpBlocked.get(alias)
|
|
2154
|
+
if (until === undefined) return false
|
|
2155
|
+
if (Date.now() >= until) {
|
|
2156
|
+
this.sftpBlocked.delete(alias)
|
|
2157
|
+
return false
|
|
2158
|
+
}
|
|
2159
|
+
return true
|
|
2160
|
+
}
|
|
2161
|
+
|
|
2162
|
+
/** Park the dedicated SFTP connection for one alias and log the downgrade, once. */
|
|
2163
|
+
private blockSftp(alias: string, error: unknown): void {
|
|
2164
|
+
this.sftpBlocked.set(alias, Date.now() + SFTP_DEDICATED_BLOCK_MS)
|
|
2165
|
+
this.sftpFallback.add(alias)
|
|
2166
|
+
this.noteSftpFallback(alias, error)
|
|
2167
|
+
}
|
|
2168
|
+
|
|
2169
|
+
/** Log one alias's file-operation downgrade to the shared connection, once. */
|
|
2170
|
+
private noteSftpFallback(alias: string, error: unknown): void {
|
|
2171
|
+
if (this.sftpFallbackNoted.has(alias)) return
|
|
2172
|
+
this.sftpFallbackNoted.add(alias)
|
|
2173
|
+
const detail = error instanceof Error ? error.message : String(error ?? '')
|
|
2174
|
+
// eslint-disable-next-line no-console
|
|
2175
|
+
console.warn(`[dsh-remote-vs] dedicated file connection to ${alias} failed; using the shared one:`, detail)
|
|
2176
|
+
}
|
|
2177
|
+
|
|
1615
2178
|
private async acquireIn(map: Map<string, Pooled>, alias: string, quiet = false): Promise<Client> {
|
|
1616
2179
|
const pooled = map.get(alias)
|
|
1617
2180
|
if (pooled !== undefined && pooled.connecting !== undefined) return pooled.connecting
|
|
@@ -1619,7 +2182,14 @@ export class RemotePool {
|
|
|
1619
2182
|
pooled.lastUse = Date.now()
|
|
1620
2183
|
return pooled.conn
|
|
1621
2184
|
}
|
|
1622
|
-
const entry: Pooled = {
|
|
2185
|
+
const entry: Pooled = {
|
|
2186
|
+
conn: undefined as unknown as Client,
|
|
2187
|
+
timer: undefined,
|
|
2188
|
+
lastUse: Date.now(),
|
|
2189
|
+
connecting: undefined,
|
|
2190
|
+
held: 0,
|
|
2191
|
+
channels: 0,
|
|
2192
|
+
}
|
|
1623
2193
|
map.set(alias, entry)
|
|
1624
2194
|
/**
|
|
1625
2195
|
* The entry object IS the ownership token: `drop()` deletes it and a later
|
|
@@ -1703,36 +2273,32 @@ export class RemotePool {
|
|
|
1703
2273
|
}
|
|
1704
2274
|
|
|
1705
2275
|
/**
|
|
1706
|
-
* Open
|
|
2276
|
+
* Open the transport `alias`'s configured route requires, WITHOUT dialing SSH.
|
|
1707
2277
|
*
|
|
1708
|
-
*
|
|
1709
|
-
*
|
|
1710
|
-
* -
|
|
1711
|
-
*
|
|
1712
|
-
*
|
|
1713
|
-
*
|
|
1714
|
-
*
|
|
1715
|
-
* close that channel and give the hold back — a leak here would pin the jump
|
|
1716
|
-
* host's connection forever.
|
|
2278
|
+
* This is the part of {@link dial} that turns a `ResolvedProxy` decision into a
|
|
2279
|
+
* socket: nothing for a direct route, a tunnel socket for a proxy, and a
|
|
2280
|
+
* `direct-tcpip` channel riding the jump host's pooled connection for a jump.
|
|
2281
|
+
* Extracted so a SECOND ssh2 dialer — p2's helper transport, which owns its own
|
|
2282
|
+
* `Client` — takes exactly the same route instead of a bare direct connection
|
|
2283
|
+
* (otherwise a host reachable only through a proxy or jump host answers exec and
|
|
2284
|
+
* SFTP but fails every file/Git fast path).
|
|
1717
2285
|
*
|
|
1718
|
-
*
|
|
1719
|
-
*
|
|
1720
|
-
*
|
|
2286
|
+
* `route` may be passed in when the caller has already resolved it and needs the
|
|
2287
|
+
* decision for its own purposes (`dial` reports the egress it settled on): the
|
|
2288
|
+
* default resolves it from the store.
|
|
1721
2289
|
*
|
|
1722
|
-
* `
|
|
1723
|
-
*
|
|
1724
|
-
*
|
|
1725
|
-
*
|
|
1726
|
-
*
|
|
2290
|
+
* The returned `release` is IDEMPOTENT and belongs to the caller: it tears down
|
|
2291
|
+
* the tunnel (destroys the proxy socket / closes the channel and gives the jump
|
|
2292
|
+
* host's hold back). It must be called exactly once by whoever owns the socket —
|
|
2293
|
+
* on failure, on close, and on dispose. A direct route has nothing to release,
|
|
2294
|
+
* so it gets a no-op.
|
|
1727
2295
|
*/
|
|
1728
|
-
private async
|
|
2296
|
+
private async openRoute(alias: string, route: ResolvedProxy = this.store.resolveProxy(alias)): Promise<RoutedDial> {
|
|
1729
2297
|
const host: RemoteHostEntry = this.store.require(alias)
|
|
1730
2298
|
/** The socket ssh2 will ride, when a proxy/jump built one. */
|
|
1731
2299
|
let sock: Duplex | undefined
|
|
1732
2300
|
/** Runs once: tears the tunnel down (close the channel, release the jump). */
|
|
1733
2301
|
let releaseTunnel: (() => void) | undefined
|
|
1734
|
-
const route = this.store.resolveProxy(alias)
|
|
1735
|
-
const egress = egressOf(route, host)
|
|
1736
2302
|
if (route.kind === 'proxy') {
|
|
1737
2303
|
try {
|
|
1738
2304
|
sock = await openProxySocket(route.proxy, host.host, host.port)
|
|
@@ -1780,6 +2346,65 @@ export class RemotePool {
|
|
|
1780
2346
|
// A channel is a duplex stream: ssh2 never learns it is not a raw socket.
|
|
1781
2347
|
sock = channel as unknown as Duplex
|
|
1782
2348
|
}
|
|
2349
|
+
return {
|
|
2350
|
+
...(sock === undefined ? {} : { sock }),
|
|
2351
|
+
// A direct route has nothing to tear down; every other one does.
|
|
2352
|
+
release: releaseTunnel ?? (() => {}),
|
|
2353
|
+
}
|
|
2354
|
+
}
|
|
2355
|
+
|
|
2356
|
+
/**
|
|
2357
|
+
* The routed transport for `alias`, for a caller that dials its OWN ssh2 client.
|
|
2358
|
+
*
|
|
2359
|
+
* Same decision, same error layering (`proxy auth failed: …`, `jump host "x"
|
|
2360
|
+
* failed: …`), same socket as a pooled connection would ride — p2's helper
|
|
2361
|
+
* transport uses this so "reachable only through the jump host" stops meaning
|
|
2362
|
+
* "exec works, files do not".
|
|
2363
|
+
*
|
|
2364
|
+
* **The caller owns the result**: when `sock` is set, ssh2 must be given `{ sock }`
|
|
2365
|
+
* and NOT `host`/`port` (both together let ssh2 choose between dialing the target
|
|
2366
|
+
* itself and riding the tunnel). `release` must be called exactly once — on
|
|
2367
|
+
* failure, on the connection's close, and on dispose — or the jump host's hold
|
|
2368
|
+
* leaks and the proxy socket stays open.
|
|
2369
|
+
*/
|
|
2370
|
+
async openRoutedSocket(alias: string): Promise<RoutedDial> {
|
|
2371
|
+
return this.openRoute(alias)
|
|
2372
|
+
}
|
|
2373
|
+
|
|
2374
|
+
/**
|
|
2375
|
+
* Open one SSH connection, through whatever route the store resolves.
|
|
2376
|
+
*
|
|
2377
|
+
* Three routes, and only the first is the pre-proxy code path:
|
|
2378
|
+
* - `direct`: unchanged (`host` / `port` in the connect config);
|
|
2379
|
+
* - `proxy`: the proxy module builds the tunnel and ssh2 rides the returned
|
|
2380
|
+
* socket (`sock`), so `host`/`port` are deliberately omitted (ssh2 ignores
|
|
2381
|
+
* them when `sock` is present);
|
|
2382
|
+
* - `jump`: the tunnel is a `direct-tcpip` channel on the JUMP host's pooled
|
|
2383
|
+
* connection, held for as long as the client lives. Its close/error handlers
|
|
2384
|
+
* close that channel and give the hold back — a leak here would pin the jump
|
|
2385
|
+
* host's connection forever.
|
|
2386
|
+
*
|
|
2387
|
+
* Every failure is a DIAL failure, so it keeps the existing `markError`
|
|
2388
|
+
* semantics; only the message is layered, so the user can tell which hop broke:
|
|
2389
|
+
* `proxy auth failed: …`, `proxy refused: …`, `jump host "x" failed: …`.
|
|
2390
|
+
*
|
|
2391
|
+
* `report` receives the route this dial ACTUALLY settled on, once and only on
|
|
2392
|
+
* success — a caller that asked "did this go through the jump host?" must not
|
|
2393
|
+
* be answered from a configuration that may have changed since. It is a
|
|
2394
|
+
* callback rather than a wider return type so the seam keeps returning the
|
|
2395
|
+
* `Client` the existing callers (and gates) hand around.
|
|
2396
|
+
*/
|
|
2397
|
+
private async dial(alias: string, report?: (egress: EgressInfo) => void): Promise<Client> {
|
|
2398
|
+
const host: RemoteHostEntry = this.store.require(alias)
|
|
2399
|
+
const route = this.store.resolveProxy(alias)
|
|
2400
|
+
const egress = egressOf(route, host)
|
|
2401
|
+
/**
|
|
2402
|
+
* The transport this connection rides. Resolved with the SAME route the egress
|
|
2403
|
+
* was computed from, so the reported hop and the socket can never disagree —
|
|
2404
|
+
* and `openRoute` builds it exactly the way this method used to inline
|
|
2405
|
+
* (proxy tunnel socket / jump-host `direct-tcpip` channel / nothing at all).
|
|
2406
|
+
*/
|
|
2407
|
+
const routed = await this.openRoute(alias, route)
|
|
1783
2408
|
return new Promise<Client>((resolve, reject) => {
|
|
1784
2409
|
const conn = this.makeClient()
|
|
1785
2410
|
let settled = false
|
|
@@ -1793,7 +2418,7 @@ export class RemotePool {
|
|
|
1793
2418
|
} catch {
|
|
1794
2419
|
// Ignore.
|
|
1795
2420
|
}
|
|
1796
|
-
|
|
2421
|
+
routed.release()
|
|
1797
2422
|
reject(explainAuthError(host, error))
|
|
1798
2423
|
}
|
|
1799
2424
|
conn.on('ready', () => {
|
|
@@ -1814,10 +2439,8 @@ export class RemotePool {
|
|
|
1814
2439
|
conn.on('close', () => fail(new Error('connection closed during SSH handshake')))
|
|
1815
2440
|
// The tunnel belongs to the connection: however the client ends, the channel
|
|
1816
2441
|
// and the jump host's hold go with it (idempotent; `fail` may have run).
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
conn.once('error', () => releaseTunnel?.())
|
|
1820
|
-
}
|
|
2442
|
+
conn.once('close', () => routed.release())
|
|
2443
|
+
conn.once('error', () => routed.release())
|
|
1821
2444
|
// Last resort: the library reports nothing at all (neither ready, nor
|
|
1822
2445
|
// error, nor close). `readyTimeout` cannot cover it because ssh2 clears
|
|
1823
2446
|
// that timer as soon as the socket closes without emitting an error.
|
|
@@ -1859,7 +2482,9 @@ export class RemotePool {
|
|
|
1859
2482
|
return true
|
|
1860
2483
|
}
|
|
1861
2484
|
try {
|
|
1862
|
-
const transport = sock === undefined
|
|
2485
|
+
const transport = routed.sock === undefined
|
|
2486
|
+
? { host: host.host, port: host.port }
|
|
2487
|
+
: { sock: routed.sock }
|
|
1863
2488
|
if (host.auth.kind === 'key') {
|
|
1864
2489
|
const keyPath = host.auth.keyPath as string
|
|
1865
2490
|
conn.connect({
|