@zukhruf/ssh 0.0.0-stage → 0.8.29

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 (139) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1123 -2
  3. package/dist/address.d.ts +8 -0
  4. package/dist/address.d.ts.map +1 -0
  5. package/dist/address.js +15 -0
  6. package/dist/address.js.map +1 -0
  7. package/dist/agent.d.ts +35 -0
  8. package/dist/agent.d.ts.map +1 -0
  9. package/dist/agent.js +288 -0
  10. package/dist/agent.js.map +1 -0
  11. package/dist/channel-stream.d.ts +16 -0
  12. package/dist/channel-stream.d.ts.map +1 -0
  13. package/dist/channel-stream.js +102 -0
  14. package/dist/channel-stream.js.map +1 -0
  15. package/dist/client.d.ts +114 -0
  16. package/dist/client.d.ts.map +1 -0
  17. package/dist/client.js +587 -0
  18. package/dist/client.js.map +1 -0
  19. package/dist/config.d.ts +82 -0
  20. package/dist/config.d.ts.map +1 -0
  21. package/dist/config.js +255 -0
  22. package/dist/config.js.map +1 -0
  23. package/dist/control/control-client.d.ts +12 -0
  24. package/dist/control/control-client.d.ts.map +1 -0
  25. package/dist/control/control-client.js +156 -0
  26. package/dist/control/control-client.js.map +1 -0
  27. package/dist/control/control-master.d.ts +12 -0
  28. package/dist/control/control-master.d.ts.map +1 -0
  29. package/dist/control/control-master.js +101 -0
  30. package/dist/control/control-master.js.map +1 -0
  31. package/dist/control/control-stream.d.ts +29 -0
  32. package/dist/control/control-stream.d.ts.map +1 -0
  33. package/dist/control/control-stream.js +54 -0
  34. package/dist/control/control-stream.js.map +1 -0
  35. package/dist/control/messages.d.ts +38 -0
  36. package/dist/control/messages.d.ts.map +1 -0
  37. package/dist/control/messages.js +92 -0
  38. package/dist/control/messages.js.map +1 -0
  39. package/dist/control/serve-dial.d.ts +11 -0
  40. package/dist/control/serve-dial.d.ts.map +1 -0
  41. package/dist/control/serve-dial.js +47 -0
  42. package/dist/control/serve-dial.js.map +1 -0
  43. package/dist/control/serve-session.d.ts +11 -0
  44. package/dist/control/serve-session.d.ts.map +1 -0
  45. package/dist/control/serve-session.js +78 -0
  46. package/dist/control/serve-session.js.map +1 -0
  47. package/dist/control/wire.d.ts +41 -0
  48. package/dist/control/wire.d.ts.map +1 -0
  49. package/dist/control/wire.js +100 -0
  50. package/dist/control/wire.js.map +1 -0
  51. package/dist/control.d.ts +4 -0
  52. package/dist/control.d.ts.map +1 -0
  53. package/dist/control.js +4 -0
  54. package/dist/control.js.map +1 -0
  55. package/dist/dial-route.d.ts +14 -0
  56. package/dist/dial-route.d.ts.map +1 -0
  57. package/dist/dial-route.js +87 -0
  58. package/dist/dial-route.js.map +1 -0
  59. package/dist/host-key-algorithms.d.ts +7 -0
  60. package/dist/host-key-algorithms.d.ts.map +1 -0
  61. package/dist/host-key-algorithms.js +16 -0
  62. package/dist/host-key-algorithms.js.map +1 -0
  63. package/dist/host-key-policy.d.ts +22 -0
  64. package/dist/host-key-policy.d.ts.map +1 -0
  65. package/dist/host-key-policy.js +72 -0
  66. package/dist/host-key-policy.js.map +1 -0
  67. package/dist/identities.d.ts +18 -0
  68. package/dist/identities.d.ts.map +1 -0
  69. package/dist/identities.js +81 -0
  70. package/dist/identities.js.map +1 -0
  71. package/dist/index.d.ts +9 -0
  72. package/dist/index.d.ts.map +1 -0
  73. package/dist/index.js +8 -0
  74. package/dist/index.js.map +1 -0
  75. package/dist/knownhosts.d.ts +33 -0
  76. package/dist/knownhosts.d.ts.map +1 -0
  77. package/dist/knownhosts.js +241 -0
  78. package/dist/knownhosts.js.map +1 -0
  79. package/dist/match-pattern.d.ts +3 -0
  80. package/dist/match-pattern.d.ts.map +1 -0
  81. package/dist/match-pattern.js +18 -0
  82. package/dist/match-pattern.js.map +1 -0
  83. package/dist/private-key.d.ts +42 -0
  84. package/dist/private-key.d.ts.map +1 -0
  85. package/dist/private-key.js +100 -0
  86. package/dist/private-key.js.map +1 -0
  87. package/dist/prompt.d.ts +6 -0
  88. package/dist/prompt.d.ts.map +1 -0
  89. package/dist/prompt.js +2 -0
  90. package/dist/prompt.js.map +1 -0
  91. package/dist/proxy-command.d.ts +33 -0
  92. package/dist/proxy-command.d.ts.map +1 -0
  93. package/dist/proxy-command.js +121 -0
  94. package/dist/proxy-command.js.map +1 -0
  95. package/dist/proxy-jump.d.ts +19 -0
  96. package/dist/proxy-jump.d.ts.map +1 -0
  97. package/dist/proxy-jump.js +63 -0
  98. package/dist/proxy-jump.js.map +1 -0
  99. package/dist/public-key.d.ts +22 -0
  100. package/dist/public-key.d.ts.map +1 -0
  101. package/dist/public-key.js +102 -0
  102. package/dist/public-key.js.map +1 -0
  103. package/dist/remote-listener.d.ts +14 -0
  104. package/dist/remote-listener.d.ts.map +1 -0
  105. package/dist/remote-listener.js +39 -0
  106. package/dist/remote-listener.js.map +1 -0
  107. package/dist/server-alive-check.d.ts +36 -0
  108. package/dist/server-alive-check.d.ts.map +1 -0
  109. package/dist/server-alive-check.js +103 -0
  110. package/dist/server-alive-check.js.map +1 -0
  111. package/dist/server.d.ts +114 -0
  112. package/dist/server.d.ts.map +1 -0
  113. package/dist/server.js +460 -0
  114. package/dist/server.js.map +1 -0
  115. package/dist/session.d.ts +97 -0
  116. package/dist/session.d.ts.map +1 -0
  117. package/dist/session.js +193 -0
  118. package/dist/session.js.map +1 -0
  119. package/dist/ssh-g.d.ts +19 -0
  120. package/dist/ssh-g.d.ts.map +1 -0
  121. package/dist/ssh-g.js +67 -0
  122. package/dist/ssh-g.js.map +1 -0
  123. package/dist/ssh2-agent-forwarding.d.ts +51 -0
  124. package/dist/ssh2-agent-forwarding.d.ts.map +1 -0
  125. package/dist/ssh2-agent-forwarding.js +56 -0
  126. package/dist/ssh2-agent-forwarding.js.map +1 -0
  127. package/dist/ssh2-channel-open-failure.d.ts +28 -0
  128. package/dist/ssh2-channel-open-failure.d.ts.map +1 -0
  129. package/dist/ssh2-channel-open-failure.js +35 -0
  130. package/dist/ssh2-channel-open-failure.js.map +1 -0
  131. package/dist/ssh2-keepalive-request.d.ts +31 -0
  132. package/dist/ssh2-keepalive-request.d.ts.map +1 -0
  133. package/dist/ssh2-keepalive-request.js +6 -0
  134. package/dist/ssh2-keepalive-request.js.map +1 -0
  135. package/dist/ssh2-server-channel.d.ts +63 -0
  136. package/dist/ssh2-server-channel.d.ts.map +1 -0
  137. package/dist/ssh2-server-channel.js +309 -0
  138. package/dist/ssh2-server-channel.js.map +1 -0
  139. package/package.json +75 -4
@@ -0,0 +1,101 @@
1
+ import { once } from 'node:events';
2
+ import { createServer } from 'node:net';
3
+ import { isRequest, toWire } from "./messages.js";
4
+ import { serveDial } from "./serve-dial.js";
5
+ import { serveSession } from "./serve-session.js";
6
+ import { FrameType, decodeJson, encodeJson, readFirstFrame, } from "./wire.js";
7
+ /** Serves the channels of a client to other processes on a Unix socket. OpenSSH's ControlMaster and ControlPath. */
8
+ export class ControlMaster {
9
+ path;
10
+ #server;
11
+ #connections;
12
+ constructor(path, server, connections) {
13
+ this.path = path;
14
+ this.#server = server;
15
+ this.#connections = connections;
16
+ }
17
+ /** Listens on `path`. Disposal stops the listener and closes each channel that the master serves; it does not close `client`. */
18
+ static async listen(client, path) {
19
+ const connections = new Set();
20
+ const server = createServer((socket) => {
21
+ connections.add(socket);
22
+ socket.once('close', () => connections.delete(socket));
23
+ accept(client, socket);
24
+ });
25
+ server.listen(path);
26
+ await once(server, 'listening');
27
+ return new ControlMaster(path, server, connections);
28
+ }
29
+ /** node:net removes the socket file when the server closes. */
30
+ async [Symbol.asyncDispose]() {
31
+ const closed = once(this.#server, 'close');
32
+ this.#server.close();
33
+ for (const socket of this.#connections)
34
+ socket.destroy();
35
+ await closed;
36
+ }
37
+ }
38
+ // A client that died leaves a write to fail; its close ends the channel.
39
+ function accept(client, socket) {
40
+ socket.on('error', () => socket.destroy());
41
+ void readFirstFrame(socket, new Error('ssh: the control client closed the connection')).then((frame) => open(client, socket, frame), () => undefined);
42
+ }
43
+ async function open(client, socket, frame) {
44
+ const request = requestOf(frame);
45
+ if (request === undefined) {
46
+ refuse(socket, new Error('ssh: the control client sent no valid request'));
47
+ return;
48
+ }
49
+ try {
50
+ if (request.kind === 'dial') {
51
+ const channel = await client.dial(request.network, request.address);
52
+ if (closedBefore(socket, () => channel.destroy()))
53
+ return;
54
+ reply(socket, { opened: true });
55
+ serveDial(channel, socket);
56
+ }
57
+ else {
58
+ const session = client.newSession();
59
+ const { command, env, pty } = request.start;
60
+ for (const [name, value] of Object.entries(env)) {
61
+ session.setenv(name, value);
62
+ }
63
+ if (pty !== undefined)
64
+ session.requestPty(pty.term, pty.size, pty.modes);
65
+ await (command === undefined ? session.shell() : session.start(command));
66
+ if (closedBefore(socket, () => void session.close()))
67
+ return;
68
+ reply(socket, { opened: true });
69
+ serveSession(session, socket);
70
+ }
71
+ }
72
+ catch (error) {
73
+ refuse(socket, error);
74
+ }
75
+ }
76
+ function requestOf(frame) {
77
+ if (frame.type !== FrameType.request)
78
+ return undefined;
79
+ const value = decodeJson(frame);
80
+ return isRequest(value) ? value : undefined;
81
+ }
82
+ /** The client went away while the channel opened. */
83
+ function closedBefore(socket, close) {
84
+ if (!socket.destroyed)
85
+ return false;
86
+ close();
87
+ return true;
88
+ }
89
+ function reply(socket, answer) {
90
+ socket.write(encodeJson(FrameType.reply, answer));
91
+ }
92
+ // readFirstFrame paused the socket: it reads again to see the client's end,
93
+ // and closes.
94
+ function refuse(socket, error) {
95
+ socket.end(encodeJson(FrameType.reply, {
96
+ opened: false,
97
+ error: toWire(error),
98
+ }));
99
+ socket.resume();
100
+ }
101
+ //# sourceMappingURL=control-master.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"control-master.js","sourceRoot":"","sources":["../../src/control/control-master.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,EAA4B,YAAY,EAAE,MAAM,UAAU,CAAC;AAGlE,OAAO,EAA4B,SAAS,EAAE,MAAM,EAAE,MAAM,eAAe,C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B;IACrD,IAAI,CAAC,MAAM,CAAC,SAAS;QAAE,OAAO,KAAK,CAAC;IACpC,KAAK,EAAE,CAAC;IACR,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,KAAK,CAAC,MAAc,EAAE,MAAa;IAC1C,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC;AACpD,CAAC;AAED,4EAA4E;AAC5E,cAAc;AACd,SAAS,MAAM,CAAC,MAAc,EAAE,KAAc;IAC5C,MAAM,CAAC,GAAG,CACR,UAAU,CAAC,SAAS,CAAC,KAAK,EAAE;QAC1B,MAAM,EAAE,KAAK;QACb,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC;KACL,CAAC,CACnB,CAAC;IACF,MAAM,CAAC,MAAM,EAAE,CAAC;AAClB,CAAC"}
@@ -0,0 +1,29 @@
1
+ import type { Socket } from 'node:net';
2
+ import { Duplex } from 'node:stream';
3
+ type Callback = (error?: Error | null) => void;
4
+ /** What the owner of the Unix connection does for the stream. */
5
+ export interface StreamOwner {
6
+ /** A write after the close of the Unix connection. */
7
+ writeAfterClose(callback: Callback): void;
8
+ /** The stream was destroyed. */
9
+ destroyed(): void;
10
+ }
11
+ /**
12
+ * The data of a channel over its Unix connection: a write goes out as a data
13
+ * frame and the end as an eof frame. The owner reads the frames of the master
14
+ * and gives the data and the eof to the stream.
15
+ */
16
+ export declare class ControlStream extends Duplex {
17
+ #private;
18
+ constructor(socket: Socket, owner: StreamOwner);
19
+ receive(chunk: Buffer): void;
20
+ receiveEof(): void;
21
+ /** The Unix connection closed, after the eof: with `error` the stream fails with it; with none it closes after its end. */
22
+ connectionClosed(error: Error | undefined): void;
23
+ _read(): void;
24
+ _write(chunk: Buffer, _encoding: BufferEncoding, callback: Callback): void;
25
+ _final(callback: Callback): void;
26
+ _destroy(error: Error | null, callback: Callback): void;
27
+ }
28
+ export {};
29
+ //# sourceMappingURL=control-stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"control-stream.d.ts","sourceRoot":"","sources":["../../src/control/control-stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAIrC,KAAK,QAAQ,GAAG,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,IAAI,KAAK,IAAI,CAAC;AAE/C,iEAAiE;AACjE,MAAM,WAAW,WAAW;IAC1B,sDAAsD;IACtD,eAAe,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI,CAAC;IAC1C,gCAAgC;IAChC,SAAS,IAAI,IAAI,CAAC;CACnB;AAED;;;;GAIG;AACH,qBAAa,aAAc,SAAQ,MAAM;;gBAI3B,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW;IAM9C,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAI5B,UAAU,IAAI,IAAI;IAIlB,2HAA2H;IAC3H,gBAAgB,CAAC,KAAK,EAAE,KAAK,GAAG,SAAS,GAAG,IAAI;IAMvC,KAAK,IAAI,IAAI;IAIb,MAAM,CACb,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,cAAc,EACzB,QAAQ,EAAE,QAAQ,GACjB,IAAI;IAKE,MAAM,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI;IAKhC,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,EAAE,QAAQ,EAAE,QAAQ,GAAG,IAAI;CAQjE"}
@@ -0,0 +1,54 @@
1
+ import { Duplex } from 'node:stream';
2
+ import { FrameType, encode } from "./wire.js";
3
+ /**
4
+ * The data of a channel over its Unix connection: a write goes out as a data
5
+ * frame and the end as an eof frame. The owner reads the frames of the master
6
+ * and gives the data and the eof to the stream.
7
+ */
8
+ export class ControlStream extends Duplex {
9
+ #socket;
10
+ #owner;
11
+ constructor(socket, owner) {
12
+ super({ allowHalfOpen: true });
13
+ this.#socket = socket;
14
+ this.#owner = owner;
15
+ }
16
+ receive(chunk) {
17
+ if (!this.push(chunk))
18
+ this.#socket.pause();
19
+ }
20
+ receiveEof() {
21
+ this.push(null);
22
+ }
23
+ /** The Unix connection closed, after the eof: with `error` the stream fails with it; with none it closes after its end. */
24
+ connectionClosed(error) {
25
+ if (error !== undefined)
26
+ this.destroy(error);
27
+ else if (this.readableEnded)
28
+ this.destroy();
29
+ else
30
+ this.once('end', () => this.destroy());
31
+ }
32
+ _read() {
33
+ this.#socket.resume();
34
+ }
35
+ _write(chunk, _encoding, callback) {
36
+ if (this.#isClosed())
37
+ this.#owner.writeAfterClose(callback);
38
+ else
39
+ this.#socket.write(encode(FrameType.data, chunk), callback);
40
+ }
41
+ _final(callback) {
42
+ if (!this.#isClosed())
43
+ this.#socket.write(encode(FrameType.eof));
44
+ callback();
45
+ }
46
+ _destroy(error, callback) {
47
+ this.#owner.destroyed();
48
+ callback(error);
49
+ }
50
+ #isClosed() {
51
+ return this.#socket.destroyed || this.#socket.writableEnded;
52
+ }
53
+ }
54
+ //# sourceMappingURL=control-stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"control-stream.js","sourceRoot":"","sources":["../../src/control/control-stream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAY9C;;;;GAIG;AACH,MAAM,OAAO,aAAc,SAAQ,MAAM;IAC9B,OAAO,CAAS;IAChB,MAAM,CAAc;IAE7B,YAAY,MAAc,EAAE,KAAkB;QAC5C,KAAK,CAAC,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC;QAC/B,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED,OAAO,CAAC,KAAa;QACnB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,K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"}
@@ -0,0 +1,38 @@
1
+ import { type Exit, type SessionStart, type WindowSize } from '../session.ts';
2
+ export type Request = {
3
+ readonly kind: 'dial';
4
+ readonly network: 'tcp' | 'unix';
5
+ readonly address: string;
6
+ } | {
7
+ readonly kind: 'session';
8
+ readonly start: SessionStart;
9
+ };
10
+ /** The answer to a request: the channel opened, or the error of its open. */
11
+ export type Reply = {
12
+ readonly opened: true;
13
+ } | {
14
+ readonly opened: false;
15
+ readonly error: WireError;
16
+ };
17
+ /** How the channel of a session ended: with an exit, or with none and the error that ended the master's connection when one did. */
18
+ export type Ending = {
19
+ readonly kind: 'exit';
20
+ readonly exit: Exit;
21
+ } | {
22
+ readonly kind: 'missing';
23
+ readonly cause?: WireError;
24
+ };
25
+ /** An error by its message, its `code` and its cause. */
26
+ export interface WireError {
27
+ readonly message: string;
28
+ readonly code?: string | number;
29
+ readonly cause?: WireError;
30
+ }
31
+ export declare function toWire(error: unknown): WireError;
32
+ export declare function fromWire(wire: WireError): Error;
33
+ export declare function isWireError(value: unknown): value is WireError;
34
+ export declare function isWindowSize(value: unknown): value is WindowSize;
35
+ export declare function isRequest(value: unknown): value is Request;
36
+ export declare function isReply(value: unknown): value is Reply;
37
+ export declare function isEnding(value: unknown): value is Ending;
38
+ //# sourceMappingURL=messages.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../../src/control/messages.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,IAAI,EACT,KAAK,YAAY,EAEjB,KAAK,UAAU,EAEhB,MAAM,eAAe,CAAC;AAKvB,MAAM,MAAM,OAAO,GACf;IACE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,KAAK,GAAG,MAAM,CAAC;IACjC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B,GACD;IAAE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IAAC,QAAQ,CAAC,K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Name, } from "../session.js";
2
+ export function toWire(error) {
3
+ if (!Error.isError(error))
4
+ return { message: String(error) };
5
+ const code = 'code' in error &&
6
+ (typeof error.code === 'string' || typeof error.code === 'number')
7
+ ? error.code
8
+ : undefined;
9
+ return {
10
+ message: error.message,
11
+ ...(code !== undefined && { code }),
12
+ ...(error.cause !== undefined && { cause: toWire(error.cause) }),
13
+ };
14
+ }
15
+ export function fromWire(wire) {
16
+ const error = new Error(wire.message, wire.cause && { cause: fromWire(wire.cause) });
17
+ return wire.code === undefined
18
+ ? error
19
+ : Object.assign(error, { code: wire.code });
20
+ }
21
+ function isFields(value) {
22
+ return typeof value === 'object' && value !== null;
23
+ }
24
+ function isOptional(value, is) {
25
+ return value === undefined || is(value);
26
+ }
27
+ const isString = (value) => typeof value === 'string';
28
+ const isNumber = (value) => typeof value === 'number';
29
+ export function isWireError(value) {
30
+ return (isFields(value) &&
31
+ isString(value['message']) &&
32
+ (value['code'] === undefined ||
33
+ isString(value['code']) ||
34
+ isNumber(value['code'])) &&
35
+ isOptional(value['cause'], isWireError));
36
+ }
37
+ export function isWindowSize(value) {
38
+ return (isFields(value) &&
39
+ isNumber(value['rows']) &&
40
+ isNumber(value['columns']) &&
41
+ isOptional(value['width'], isNumber) &&
42
+ isOptional(value['height'], isNumber));
43
+ }
44
+ function isTerminalModes(value) {
45
+ return isFields(value) && Object.values(value).every(isNumber);
46
+ }
47
+ function isStart(value) {
48
+ if (!isFields(value))
49
+ return false;
50
+ const { command, env, pty } = value;
51
+ return (isOptional(command, isString) &&
52
+ isFields(env) &&
53
+ Object.values(env).every(isString) &&
54
+ (pty === undefined ||
55
+ (isFields(pty) &&
56
+ isString(pty['term']) &&
57
+ isWindowSize(pty['size']) &&
58
+ isOptional(pty['modes'], isTerminalModes))));
59
+ }
60
+ export function isRequest(value) {
61
+ if (!isFields(value))
62
+ return false;
63
+ if (value['kind'] === 'dial') {
64
+ return ((value['network'] === 'tcp' || value['network'] === 'unix') &&
65
+ isString(value['address']));
66
+ }
67
+ return value['kind'] === 'session' && isStart(value['start']);
68
+ }
69
+ export function isReply(value) {
70
+ return (isFields(value) &&
71
+ (value['opened'] === true ||
72
+ (value['opened'] === false && isWireError(value['error']))));
73
+ }
74
+ function isExit(value) {
75
+ if (!isFields(value))
76
+ return false;
77
+ if (value['kind'] === 'status')
78
+ return isNumber(value['exitStatus']);
79
+ const signal = value['signal'];
80
+ return (value['kind'] === 'signal' &&
81
+ isSignalOrVendorName(signal) &&
82
+ typeof value['coreDumped'] === 'boolean' &&
83
+ isString(value['errorMessage']));
84
+ }
85
+ export function isEnding(value) {
86
+ if (!isFields(value))
87
+ return false;
88
+ if (value['kind'] === 'exit')
89
+ return isExit(value['exit']);
90
+ return value['kind'] === 'missing' && isOptional(value['cause'], isWireError);
91
+ }
92
+ //# sourceMappingURL=messages.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/control/messages.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,oBAAoB,GACrB,MAAM,eAAe,CAAC;AA8BvB,MAAM,UAAU,MAAM,CAAC,KAAc;IACnC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;IAC7D,MAAM,IAAI,GACR,MAAM,IAAI,KAAK;QACf,CAAC,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,CAAC;QAChE,CAAC,CAAC,KAAK,CAAC,IAAI;QACZ,CAAC,CAAC,SAAS,CAAC;IAChB,OAAO;QACL,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,GAAG,CAAC,IAAI,KAAK,SAAS,IAAI,EAAE,IAAI,EAAE,CAAC;QACnC,GAAG,CAAC,KAAK,CAAC,KAAK,KAAK,SAAS,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,KAAK,C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AAK,CAAC,MAAM,CAAC,KAAK,SAAS,IAAI,UAAU,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,WAAW,CAAC,CAAC;AAChF,CAAC"}
@@ -0,0 +1,11 @@
1
+ import type { Socket } from 'node:net';
2
+ import type { Duplex } from 'node:stream';
3
+ /**
4
+ * Carries a dialed channel over its Unix connection: data and eof frames both
5
+ * ways. The close of one closes the other; a channel that failed, as on the
6
+ * loss of the master's connection, sends its error as the last frame.
7
+ */
8
+ export declare function serveDial(channel: Duplex, socket: Socket): void;
9
+ /** Sends each chunk of `source` as a frame of `type`, and pauses it while the socket is full. */
10
+ export declare function relay(source: NodeJS.ReadableStream, socket: Socket, type: number): void;
11
+ //# sourceMappingURL=serve-dial.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serve-dial.d.ts","sourceRoot":"","sources":["../../src/control/serve-dial.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AACvC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAK1C;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CA6B/D;AAED,iGAAiG;AACjG,wBAAgB,KAAK,CACnB,MAAM,EAAE,MAAM,CAAC,cAAc,EAC7B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,GACX,IAAI,CAMN"}
@@ -0,0 +1,47 @@
1
+ import { toWire } from "./messages.js";
2
+ import { FrameType, encode, encodeJson, readFrames } from "./wire.js";
3
+ /**
4
+ * Carries a dialed channel over its Unix connection: data and eof frames both
5
+ * ways. The close of one closes the other; a channel that failed, as on the
6
+ * loss of the master's connection, sends its error as the last frame.
7
+ */
8
+ export function serveDial(channel, socket) {
9
+ readFrames(socket, (frame) => {
10
+ switch (frame.type) {
11
+ case FrameType.data:
12
+ if (!channel.write(frame.payload)) {
13
+ socket.pause();
14
+ channel.once('drain', () => socket.resume());
15
+ }
16
+ return;
17
+ case FrameType.eof:
18
+ channel.end();
19
+ return;
20
+ default:
21
+ socket.destroy();
22
+ }
23
+ });
24
+ relay(channel, socket, FrameType.data);
25
+ channel.once('end', () => socket.write(encode(FrameType.eof)));
26
+ let failure;
27
+ channel.once('error', (error) => {
28
+ failure = error;
29
+ });
30
+ channel.once('close', () => {
31
+ if (failure !== undefined) {
32
+ socket.write(encodeJson(FrameType.error, toWire(failure)));
33
+ }
34
+ socket.end();
35
+ });
36
+ socket.once('close', () => channel.destroy());
37
+ }
38
+ /** Sends each chunk of `source` as a frame of `type`, and pauses it while the socket is full. */
39
+ export function relay(source, socket, type) {
40
+ source.on('data', (chunk) => {
41
+ if (socket.write(encode(type, chunk)))
42
+ return;
43
+ source.pause();
44
+ socket.once('drain', () => source.resume());
45
+ });
46
+ }
47
+ //# sourceMappingURL=serve-dial.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serve-dial.js","sourceRoot":"","sources":["../../src/control/serve-dial.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAEtE;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAC,OAAe,EAAE,MAAc;IACvD,UAAU,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE;QAC3B,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;YACnB,KAAK,SAAS,CAAC,IAAI;gBACjB,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC;oBAClC,MAAM,CAAC,KAAK,EAAE,CAAC;oBACf,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;gBAC/C,CAAC;gBACD,OAAO;YACT,KAAK,SA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,CAAC,CAAC;IAC9C,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,11 @@
1
+ import type { Socket } from 'node:net';
2
+ import { type Session } from '../session.ts';
3
+ /**
4
+ * Carries a started session over its Unix connection: stdin, its end,
5
+ * window-change, signal and close from the client; stdout, stderr, their end
6
+ * and the exit to it. The exit frame goes after the last output, then the
7
+ * master closes the connection. The close of the connection closes the
8
+ * channel, and so does a frame that the protocol does not have.
9
+ */
10
+ export declare function serveSession(session: Session, socket: Socket): void;
11
+ //# sourceMappingURL=serve-session.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serve-session.d.ts","sourceRoot":"","sources":["../../src/control/serve-session.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAGvC,OAAO,EAGL,KAAK,OAAO,EAEb,MAAM,eAAe,CAAC;AAYvB;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAyBnE"}
@@ -0,0 +1,78 @@
1
+ import { finished } from 'node:stream/promises';
2
+ import { ExitError, ExitMissingError, isSignalName, } from "../session.js";
3
+ import { isWindowSize, toWire } from "./messages.js";
4
+ import { relay } from "./serve-dial.js";
5
+ import { FrameType, decodeJson, encode, encodeJson, readFrames, } from "./wire.js";
6
+ /**
7
+ * Carries a started session over its Unix connection: stdin, its end,
8
+ * window-change, signal and close from the client; stdout, stderr, their end
9
+ * and the exit to it. The exit frame goes after the last output, then the
10
+ * master closes the connection. The close of the connection closes the
11
+ * channel, and so does a frame that the protocol does not have.
12
+ */
13
+ export function serveSession(session, socket) {
14
+ readFrames(socket, (frame) => {
15
+ if (!apply(session, socket, frame))
16
+ socket.destroy();
17
+ });
18
+ // stdin fails when the channel closes with a write that it did not take.
19
+ // The client drops that write, as it drops a write after the end of the
20
+ // session, so the frames of the client go on.
21
+ session.stdin.once('error', () => socket.resume());
22
+ relay(session.stdout, socket, FrameType.data);
23
+ relay(session.stderr, socket, FrameType.extendedData);
24
+ const outputEnded = Promise.allSettled([
25
+ finished(session.stdout),
26
+ finished(session.stderr),
27
+ ]).then(() => socket.write(encode(FrameType.eof)));
28
+ socket.once('close', () => void session.close());
29
+ void session
30
+ .wait()
31
+ .then(() => ({ kind: 'exit', exit: { kind: 'status', exitStatus: 0 } }), endingOf)
32
+ .then(async (ending) => {
33
+ await outputEnded;
34
+ socket.end(encodeJson(FrameType.exit, ending));
35
+ });
36
+ }
37
+ /** False for a frame that the protocol does not have. */
38
+ function apply(session, socket, frame) {
39
+ switch (frame.type) {
40
+ case FrameType.data:
41
+ if (!session.stdin.write(frame.payload) && !session.stdin.destroyed) {
42
+ socket.pause();
43
+ session.stdin.once('drain', () => socket.resume());
44
+ }
45
+ return true;
46
+ case FrameType.eof:
47
+ session.stdin.end();
48
+ return true;
49
+ case FrameType.windowChange: {
50
+ const size = decodeJson(frame);
51
+ if (!isWindowSize(size))
52
+ return false;
53
+ void session.windowChange(size);
54
+ return true;
55
+ }
56
+ case FrameType.signal: {
57
+ const name = frame.payload.toString();
58
+ if (!isSignalName(name))
59
+ return false;
60
+ void session.signal(name);
61
+ return true;
62
+ }
63
+ case FrameType.close:
64
+ void session.close();
65
+ return true;
66
+ default:
67
+ return false;
68
+ }
69
+ }
70
+ function endingOf(error) {
71
+ if (error instanceof ExitError)
72
+ return { kind: 'exit', exit: error.exit };
73
+ const cause = error instanceof ExitMissingError ? error.cause : error;
74
+ return cause === undefined
75
+ ? { kind: 'missing' }
76
+ : { kind: 'missing', cause: toWire(cause) };
77
+ }
78
+ //# sourceMappingURL=serve-session.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serve-session.js","sourceRoot":"","sources":["../../src/control/serve-session.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAEhD,OAAO,EACL,SAAS,EACT,gBAAgB,EAEhB,YAAY,GACb,MAAM,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,EAAE,CAAC;YACtC,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC;gBAAE,OAAO,KAAK,CAAC;YACtC,KAAK,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YAC1B,OAAO,IAAI,CAAC;QACd,CAAC;QACD,KAAK,SAAS,CAAC,KAAK;YAClB,KAAK,OAAO,CAAC,KAAK,EAAE,CAAC;YACrB,OAAO,IAAI,CAAC;QACd;YACE,OAAO,KAAK,CAAC;IACjB,CAAC;AACH,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc;IAC9B,IAAI,KAAK,YAAY,SAAS;QAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC;IAC1E,MAAM,KAAK,GAAG,KAAK,YAAY,gBAAgB,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC;IACtE,OAAO,KAAK,KAAK,SAAS;QACxB,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE;QACrB,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;AAChD,CAAC"}
@@ -0,0 +1,41 @@
1
+ import type { Socket } from 'node:net';
2
+ export declare const FrameType: {
3
+ /** client → master, the first frame: JSON Request. */
4
+ readonly request: 0;
5
+ /** master → client, the answer to the request: JSON Reply. */
6
+ readonly reply: 1;
7
+ /** Both ways: SSH_MSG_CHANNEL_DATA; for a session, stdin or stdout. */
8
+ readonly data: 2;
9
+ /** master → client: SSH_MSG_CHANNEL_EXTENDED_DATA type 1, stderr. */
10
+ readonly extendedData: 3;
11
+ /** Both ways: SSH_MSG_CHANNEL_EOF. Empty. */
12
+ readonly eof: 4;
13
+ /** client → master: SSH_MSG_CHANNEL_CLOSE of a session. Empty. */
14
+ readonly close: 5;
15
+ /** client → master: window-change, JSON WindowSize. */
16
+ readonly windowChange: 6;
17
+ /** client → master: signal, its name in UTF-8. */
18
+ readonly signal: 7;
19
+ /** master → client, the last frame of a session: JSON Ending. */
20
+ readonly exit: 8;
21
+ /** master → client, the last frame of a dial that failed: JSON WireError. */
22
+ readonly error: 9;
23
+ };
24
+ export interface Frame {
25
+ readonly type: number;
26
+ readonly payload: Buffer;
27
+ }
28
+ export declare function encode(type: number, payload?: Buffer | string): Buffer;
29
+ export declare function encodeJson(type: number, value: unknown): Buffer;
30
+ /** The JSON of the payload; undefined when it is not JSON, which no check of messages.ts accepts. */
31
+ export declare function decodeJson(frame: Frame): unknown;
32
+ /**
33
+ * The first frame of a connection: the request, or the reply. The socket is
34
+ * paused after it, with the bytes after the frame put back, so the channel
35
+ * reads them when it opens. Rejects with `closed` when the socket closes
36
+ * first.
37
+ */
38
+ export declare function readFirstFrame(socket: Socket, closed: Error): Promise<Frame>;
39
+ /** Gives each frame of the socket to `handler`, from the bytes that readFirstFrame left on. */
40
+ export declare function readFrames(socket: Socket, handler: (frame: Frame) => void): void;
41
+ //# sourceMappingURL=wire.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wire.d.ts","sourceRoot":"","sources":["../../src/control/wire.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAMvC,eAAO,MAAM,SAAS;IACpB,sDAAsD;;IAEtD,8DAA8D;;IAE9D,uEAAuE;;IAEvE,qEAAqE;;IAErE,6CAA6C;;IAE7C,kEAAkE;;IAElE,uDAAuD;;IAEvD,kDAAkD;;IAElD,iEAAiE;;IAEjE,6EAA6E;;CAErE,CAAC;AAEX,MAAM,WAAW,KAAK;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAID,wBAAgB,MAAM,CACpB,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,MAAM,GAAG,MAAwB,GACzC,MAAM,CAMR;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,MAAM,CAE/D;AAED,qGAAqG;AACrG,wBAAgB,UAAU,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAMhD;AAkBD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,CAiB5E;AAED,+FAA+F;AAC/F,wBAAgB,UAAU,CACxB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,GAC9B,IAAI,CAcN"}