@johnhenry/wsh 0.41.0 → 0.43.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.
@@ -60,6 +60,8 @@ export type WshClientEnv = boolean | string[] | ((name: string, value: string) =
60
60
  /** What a custom `exec` runner receives for one session. */
61
61
  export interface WshExecIo {
62
62
  user: string;
63
+ /** #121: the execution target this command runs on; undefined on the host's own. */
64
+ target?: string;
63
65
  /** Environment the client asked for (empty when `clientEnv: false`). */
64
66
  env: Record<string, string>;
65
67
  cols: number;
@@ -115,6 +117,8 @@ export interface WshGatewayEndpointDuplex {
115
117
  readable: ReadableStream<Uint8Array>;
116
118
  writable: WritableStream<Uint8Array>;
117
119
  who: WshGatewayWho;
120
+ /** #121: the execution target this endpoint belongs to; undefined for the host's own. */
121
+ target?: string;
118
122
  /** Half-close: nothing more will be sent. */
119
123
  close(reason?: string): void;
120
124
  /** Tear the connection down now. */
@@ -487,6 +491,15 @@ export interface WshServerOptions {
487
491
  onShare?: import('../host.js').WshOnShare;
488
492
  /** #163: told once per connection this host turned away (an operator, on a reverse host), with a stable reason code (`REFUSAL_REASONS`, from `@johnhenry/wsh/host`). */
489
493
  onRefused?: import('../host.js').WshOnRefused;
494
+ /**
495
+ * #121: execution targets (containers, VMs, workers, sandboxes), each with its own `exec` (`true` = a shell), `pty`, `fs`
496
+ * (`{ root }` or operations) and `endpoints`, and an `authorize(who, { target, kind })`. A client names one in `Open`,
497
+ * `FileOp` or `OpenEndpoint` and lists what it may use with `listTargets()`. Off unless given: the host then advertises no
498
+ * `targets` and refuses a request that names one. A refused target and one that does not exist look the same to the client.
499
+ */
500
+ targets?: Record<string, Omit<import('../host.js').WshTargetOptions, 'exec' | 'fs'> & { exec?: true | WshServerExecOptions | WshExecRunner; fs?: WshServerFsOptions | import('../host.js').WshFileBackend }>;
501
+ /** With `targets`: an `Open`, `FileOp` or `OpenEndpoint` that names no target is refused, so nothing runs on the host itself (`rpc` channels too). Does not cover the gateway, MCP or `Attach`. */
502
+ requireTarget?: boolean;
490
503
  onLog?: (line: string) => void;
491
504
  }
492
505
 
@@ -85,6 +85,15 @@ function tlsChannelBinding(socket) {
85
85
  * Answer the gateway opcodes (OpenTcp, ResolveDns, GatewayData/Close): the client opens TCP connections and resolves
86
86
  * names from this host. Default deny: a destination must match `allow` (`"*"`, `"host"`, `"host:port"`). `socks` dials
87
87
  * through a SOCKS5 proxy (Tor) with the name unresolved. UDP (`udp`: bounds, WebTransport datagrams) and TCP reverse tunnels (`reverse`, off unless given) are served. Off unless given. See `gateway.mjs`.
88
+ * @param {Record<string, { description?: string, authorize?: Function, exec?: true | object | Function, pty?: object, fs?: object, endpoints?: object }>} [options.targets]
89
+ * Execution targets (#121): the host fronts several (containers, VMs, workers, sandboxes), each with its own `exec`, `pty`,
90
+ * `fs` and `endpoints` (the same forms as the host's own) and an `authorize(who, { target, kind })` (`kind`: `exec`, `pty`,
91
+ * `file`, `endpoint`, `list`) deciding who may use it; a client names one in `Open`, `FileOp` or `OpenEndpoint` and lists
92
+ * what it may use with `ListTargets`. Off unless given (the host then advertises no `targets` and refuses a request that
93
+ * names one). A refused target and one that does not exist look the same to the client.
94
+ * @param {boolean} [options.requireTarget=false] - With `targets`: an `Open`, `FileOp` or `OpenEndpoint` that names no target is
95
+ * refused, so nothing runs on the host itself (`kind: 'rpc'` channels, which are not per target, are refused too). Does NOT
96
+ * cover the gateway (`OpenTcp`, `OpenUnix`, ...), MCP, or `Attach` to a session already open.
88
97
  * @param {number} [options.bindTimeoutMs=3000]
89
98
  * @param {number} [options.connectionTtlMs=0] - End every authenticated connection this long after login (0 = never).
90
99
  * Advertised as `AuthOk.ttl` in whole seconds, rounded up; the client reads it as `client.authTtl`.
@@ -106,7 +115,7 @@ function tlsChannelBinding(socket) {
106
115
  */
107
116
  export function createWshServer({
108
117
  host = '127.0.0.1', port = 0, tls, auth, exec, pty, fs, hostKey, mcp, rpc, rpcMaxMessageBytes, rpcMaxInflight, sessions, sessionSecret, relay, extensions, gateway, webTransport, bindTimeoutMs = 3000, connectionTtlMs = 0, requireChannelBinding = false, sealSession = false, onLog = () => {},
109
- onShare, onRefused,
118
+ onShare, onRefused, targets, requireTarget,
110
119
  } = {}) {
111
120
  validateOnShare(onShare, 'createWshServer');
112
121
  validateOnRefused(onRefused, 'createWshServer');
@@ -120,7 +129,7 @@ export function createWshServer({
120
129
  let httpsServer = null;
121
130
  let bound = null;
122
131
 
123
- const { execRunner, execOptions, pty: ptyConfig, files, mcp: mcpHost } = buildBackends({ exec, pty, fs, mcp });
132
+ const { execRunner, execOptions, pty: ptyConfig, files, mcp: mcpHost, targets: targetTable, requireTarget: mustHaveTarget } = buildBackends({ exec, pty, fs, mcp, targets, requireTarget });
124
133
  const methods = authMethods(auth);
125
134
  const rl = auth?.rateLimit ?? {};
126
135
  const rateLimit = {
@@ -136,7 +145,10 @@ export function createWshServer({
136
145
  let registry = null;
137
146
  let hub = null;
138
147
  let wt = null;
139
- const gatewayCfg = gateway === undefined ? null : normalizeGateway(gateway);
148
+ let gatewayCfg = gateway === undefined ? null : normalizeGateway(gateway);
149
+ // A target's own named endpoints (#121) are served by the gateway session even when the host has no gateway of its own:
150
+ // an endpoints-only one (no TCP, DNS or unix sockets), which advertises `gateway-endpoints`.
151
+ if (targetTable && [...targetTable.values()].some((t) => t.endpoints)) gatewayCfg = { ...(gatewayCfg ?? normalizeGateway({ tcp: false })), targetEndpoints: true };
140
152
  const sessionOptions = sessions ? (sessions === true ? {} : sessions) : null;
141
153
 
142
154
  /** Sockets handed to `accept()` that are still open. */
@@ -163,7 +175,7 @@ export function createWshServer({
163
175
  return createConnectionFactory({
164
176
  authorize, execRunner, execOptions, pty: ptyConfig, files, bindTimeoutMs, connectionTtlMs, requireChannelBinding, sealSession, log: onLog,
165
177
  methods, hostKey: host_, rateLimit, extensions, mcp: mcpHost, rpc: rpcHost, gateway: gatewayCfg, createGatewaySession, sessions: registry, relay: hub,
166
- onShare, onRefused,
178
+ onShare, onRefused, targets: targetTable, requireTarget: mustHaveTarget,
167
179
  });
168
180
  })();
169
181
  ready.catch(() => { ready = null; });