ai-remote 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/README.md +50 -0
  2. package/dist/index.js +7 -0
  3. package/dist/index.js.map +7 -0
  4. package/dist/protocols/index.js +35 -0
  5. package/dist/protocols/index.js.map +7 -0
  6. package/dist/protocols/rdp/buffer.js +170 -0
  7. package/dist/protocols/rdp/buffer.js.map +7 -0
  8. package/dist/protocols/rdp/caps.js +260 -0
  9. package/dist/protocols/rdp/caps.js.map +7 -0
  10. package/dist/protocols/rdp/cert.js +135 -0
  11. package/dist/protocols/rdp/cert.js.map +7 -0
  12. package/dist/protocols/rdp/client.js +1068 -0
  13. package/dist/protocols/rdp/client.js.map +7 -0
  14. package/dist/protocols/rdp/cliprdr.js +240 -0
  15. package/dist/protocols/rdp/cliprdr.js.map +7 -0
  16. package/dist/protocols/rdp/credssp.js +254 -0
  17. package/dist/protocols/rdp/credssp.js.map +7 -0
  18. package/dist/protocols/rdp/crypto.js +369 -0
  19. package/dist/protocols/rdp/crypto.js.map +7 -0
  20. package/dist/protocols/rdp/display.js +589 -0
  21. package/dist/protocols/rdp/display.js.map +7 -0
  22. package/dist/protocols/rdp/gcc.js +132 -0
  23. package/dist/protocols/rdp/gcc.js.map +7 -0
  24. package/dist/protocols/rdp/keymap.js +132 -0
  25. package/dist/protocols/rdp/keymap.js.map +7 -0
  26. package/dist/protocols/rdp/mcs.js +223 -0
  27. package/dist/protocols/rdp/mcs.js.map +7 -0
  28. package/dist/protocols/rdp/ntlm.js +305 -0
  29. package/dist/protocols/rdp/ntlm.js.map +7 -0
  30. package/dist/protocols/rdp/pdu.js +444 -0
  31. package/dist/protocols/rdp/pdu.js.map +7 -0
  32. package/dist/protocols/rdp/rail.js +383 -0
  33. package/dist/protocols/rdp/rail.js.map +7 -0
  34. package/dist/protocols/rdp/rle.js +276 -0
  35. package/dist/protocols/rdp/rle.js.map +7 -0
  36. package/dist/protocols/rdp/sec.js +156 -0
  37. package/dist/protocols/rdp/sec.js.map +7 -0
  38. package/dist/protocols/rdp/session.js +307 -0
  39. package/dist/protocols/rdp/session.js.map +7 -0
  40. package/dist/protocols/rdp/tls.js +354 -0
  41. package/dist/protocols/rdp/tls.js.map +7 -0
  42. package/dist/protocols/rdp/vchannel.js +59 -0
  43. package/dist/protocols/rdp/vchannel.js.map +7 -0
  44. package/dist/protocols/rdp/x224.js +110 -0
  45. package/dist/protocols/rdp/x224.js.map +7 -0
  46. package/dist/protocols/ssh/kex.js +177 -0
  47. package/dist/protocols/ssh/kex.js.map +7 -0
  48. package/dist/protocols/ssh/messages.js +55 -0
  49. package/dist/protocols/ssh/messages.js.map +7 -0
  50. package/dist/protocols/ssh/session.js +608 -0
  51. package/dist/protocols/ssh/session.js.map +7 -0
  52. package/dist/protocols/ssh/terminal.js +191 -0
  53. package/dist/protocols/ssh/terminal.js.map +7 -0
  54. package/dist/protocols/ssh/transport.js +611 -0
  55. package/dist/protocols/ssh/transport.js.map +7 -0
  56. package/dist/protocols/ssh/wire.js +189 -0
  57. package/dist/protocols/ssh/wire.js.map +7 -0
  58. package/dist/protocols/types.js +1 -0
  59. package/dist/protocols/types.js.map +7 -0
  60. package/dist/protocols/vnc/input.js +16 -0
  61. package/dist/protocols/vnc/input.js.map +7 -0
  62. package/dist/protocols/vnc/session.js +259 -0
  63. package/dist/protocols/vnc/session.js.map +7 -0
  64. package/dist/shared/connection.js +114 -0
  65. package/dist/shared/connection.js.map +7 -0
  66. package/dist/shared/device.js +1 -0
  67. package/dist/shared/device.js.map +7 -0
  68. package/dist/shared/hosts.js +133 -0
  69. package/dist/shared/hosts.js.map +7 -0
  70. package/dist/shared/icons.js +42 -0
  71. package/dist/shared/icons.js.map +7 -0
  72. package/dist/shared/protocol.js +40 -0
  73. package/dist/shared/protocol.js.map +7 -0
  74. package/dist/shared/signals.js +42 -0
  75. package/dist/shared/signals.js.map +7 -0
  76. package/package.json +82 -0
  77. package/src/index.ts +14 -0
  78. package/src/protocols/index.ts +72 -0
  79. package/src/protocols/rdp/buffer.ts +204 -0
  80. package/src/protocols/rdp/caps.ts +304 -0
  81. package/src/protocols/rdp/cert.ts +190 -0
  82. package/src/protocols/rdp/client.ts +1363 -0
  83. package/src/protocols/rdp/cliprdr.ts +344 -0
  84. package/src/protocols/rdp/credssp.ts +319 -0
  85. package/src/protocols/rdp/crypto.ts +416 -0
  86. package/src/protocols/rdp/display.ts +750 -0
  87. package/src/protocols/rdp/gcc.ts +162 -0
  88. package/src/protocols/rdp/keymap.ts +68 -0
  89. package/src/protocols/rdp/mcs.ts +283 -0
  90. package/src/protocols/rdp/ntlm.ts +380 -0
  91. package/src/protocols/rdp/pdu.ts +497 -0
  92. package/src/protocols/rdp/rail.ts +480 -0
  93. package/src/protocols/rdp/rle.ts +343 -0
  94. package/src/protocols/rdp/sec.ts +184 -0
  95. package/src/protocols/rdp/session.ts +393 -0
  96. package/src/protocols/rdp/tls.ts +452 -0
  97. package/src/protocols/rdp/vchannel.ts +89 -0
  98. package/src/protocols/rdp/x224.ts +139 -0
  99. package/src/protocols/ssh/kex.ts +227 -0
  100. package/src/protocols/ssh/messages.ts +54 -0
  101. package/src/protocols/ssh/session.ts +878 -0
  102. package/src/protocols/ssh/terminal.ts +242 -0
  103. package/src/protocols/ssh/transport.ts +732 -0
  104. package/src/protocols/ssh/wire.ts +220 -0
  105. package/src/protocols/types.ts +146 -0
  106. package/src/protocols/vnc/input.ts +13 -0
  107. package/src/protocols/vnc/novnc.d.ts +75 -0
  108. package/src/protocols/vnc/session.ts +367 -0
  109. package/src/shared/connection.ts +206 -0
  110. package/src/shared/device.ts +28 -0
  111. package/src/shared/hosts.ts +240 -0
  112. package/src/shared/icons.ts +49 -0
  113. package/src/shared/protocol.ts +55 -0
  114. package/src/shared/signals.ts +67 -0
package/src/index.ts ADDED
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The vocabulary every host of this stack shares.
3
+ *
4
+ * Deliberately no protocol engines: a Worker that only needs to know what a
5
+ * host roster is should not pull noVNC or an RDP state machine into its
6
+ * bundle. The engines live behind `@remotectl/core/protocols`.
7
+ */
8
+
9
+ export * from './shared/connection';
10
+ export * from './shared/device';
11
+ export * from './shared/hosts';
12
+ export * from './shared/icons';
13
+ export * from './shared/protocol';
14
+ export * from './shared/signals';
@@ -0,0 +1,72 @@
1
+ /**
2
+ * The protocol registry the workspace talks to.
3
+ *
4
+ * The UI owns the toolbar, the dialogs, the stats rail and the on-screen
5
+ * keyboard, none of which care whether a session is VNC or RDP. Everything that
6
+ * does differ -- the endpoint, how a client is constructed, the wording of a
7
+ * failure, the labels in the connect dialog -- is looked up here instead of
8
+ * branching at each site.
9
+ */
10
+
11
+ import { rdpDriver } from './rdp/session';
12
+ import { vncDriver } from './vnc/session';
13
+ import type { DisconnectContext, ProtocolDriver } from './types';
14
+ import type { RemoteProtocol } from '../shared/protocol';
15
+
16
+ export type { ProtocolDriver, RemoteSession, SessionOptions, ScreenGeometry } from './types';
17
+
18
+ export const DRIVERS: Record<RemoteProtocol, ProtocolDriver> = {
19
+ vnc: vncDriver,
20
+ rdp: rdpDriver,
21
+ };
22
+
23
+ /** Unknown names fall back to VNC, which is what the dialog opens on. */
24
+ export function driverFor(protocol: string | undefined): ProtocolDriver {
25
+ return DRIVERS[protocol as RemoteProtocol] ?? DRIVERS.vnc;
26
+ }
27
+
28
+ /** What the workspace knows about a session that has just ended. */
29
+ export interface DisconnectDiagnostic {
30
+ protocol: RemoteProtocol;
31
+ /** The transport close, when one arrived. A 1006 means it did not. */
32
+ close: { code: number; reason: string } | null;
33
+ /** True once the session had reached the desktop. */
34
+ established: boolean;
35
+ }
36
+
37
+ /**
38
+ * Explain a disconnect in terms someone can act on. The protocol gets first
39
+ * say, then the close codes that mean the same thing either way.
40
+ */
41
+ export function explainDisconnect(
42
+ diagnostic: DisconnectDiagnostic,
43
+ clean: boolean,
44
+ protocolMessage: string
45
+ ): string {
46
+ // A client that reports its own failure states it in the protocol's terms,
47
+ // which is always more specific than anything the transport can say.
48
+ if (protocolMessage) return protocolMessage;
49
+
50
+ const code = diagnostic.close?.code;
51
+ const reason = diagnostic.close?.reason.trim() ?? '';
52
+ const suffix = code ? ` (WebSocket ${code}${reason ? `: ${reason}` : ''})` : '';
53
+ const context: DisconnectContext = { ...(code === undefined ? {} : { code }), reason, suffix };
54
+
55
+ const driver = driverFor(diagnostic.protocol);
56
+ const specific = driver.describeDisconnect(context);
57
+ if (specific) return specific;
58
+
59
+ const name = driver.protocol.toUpperCase();
60
+ if (/tcp (reached )?eof/i.test(reason)) {
61
+ return `The ${name} host accepted the connection and then closed it${suffix}. `
62
+ + 'Check its remote-access permissions and the account name.';
63
+ }
64
+ if (code === 1006) {
65
+ return 'The connection was interrupted before a close reason arrived (WebSocket 1006). '
66
+ + 'This can happen during a deployment or a brief network change. Automatic reconnect '
67
+ + 'did not restore the session; reconnect once, then check Worker live logs if it repeats.';
68
+ }
69
+ if (reason) return `The remote session ended${suffix}.`;
70
+ if (clean) return `The remote server closed the session cleanly${suffix || ' (no reason supplied)'}.`;
71
+ return `The ${name} negotiation failed${suffix || ' before the server supplied a close reason'}.`;
72
+ }
@@ -0,0 +1,204 @@
1
+ // Binary reader/writer used by every RDP layer. RDP mixes endianness freely --
2
+ // TPKT and MCS are big endian, everything above them is little endian -- so the
3
+ // byte order is part of each call rather than a mode on the object.
4
+
5
+ /**
6
+ * Plain `Uint8Array` throughout. Pinning the buffer type parameter would be
7
+ * more precise, but every layer above this one passes byte slices around and
8
+ * the variance between `ArrayBuffer` and `ArrayBufferLike` then has to be
9
+ * restated at each boundary for no benefit the protocol code can use.
10
+ */
11
+ function allocate(length: number): Uint8Array {
12
+ return new Uint8Array(length);
13
+ }
14
+
15
+ export class Writer {
16
+ private bytes: Uint8Array;
17
+ private view: DataView;
18
+ private offset = 0;
19
+
20
+ constructor(capacity = 512) {
21
+ this.bytes = allocate(capacity);
22
+ this.view = new DataView(this.bytes.buffer);
23
+ }
24
+
25
+ #ensure(extra: number): void {
26
+ if (this.offset + extra <= this.bytes.length) return;
27
+ let capacity = this.bytes.length * 2;
28
+ while (capacity < this.offset + extra) capacity *= 2;
29
+ const grown = allocate(capacity);
30
+ grown.set(this.bytes.subarray(0, this.offset));
31
+ this.bytes = grown;
32
+ this.view = new DataView(grown.buffer);
33
+ }
34
+
35
+ u8(value: number): this {
36
+ this.#ensure(1);
37
+ this.view.setUint8(this.offset, value);
38
+ this.offset += 1;
39
+ return this;
40
+ }
41
+
42
+ u16le(value: number): this {
43
+ this.#ensure(2);
44
+ this.view.setUint16(this.offset, value, true);
45
+ this.offset += 2;
46
+ return this;
47
+ }
48
+
49
+ u16be(value: number): this {
50
+ this.#ensure(2);
51
+ this.view.setUint16(this.offset, value, false);
52
+ this.offset += 2;
53
+ return this;
54
+ }
55
+
56
+ u32le(value: number): this {
57
+ this.#ensure(4);
58
+ this.view.setUint32(this.offset, value >>> 0, true);
59
+ this.offset += 4;
60
+ return this;
61
+ }
62
+
63
+ u32be(value: number): this {
64
+ this.#ensure(4);
65
+ this.view.setUint32(this.offset, value >>> 0, false);
66
+ this.offset += 4;
67
+ return this;
68
+ }
69
+
70
+ raw(bytes: Uint8Array): this {
71
+ this.#ensure(bytes.length);
72
+ this.bytes.set(bytes, this.offset);
73
+ this.offset += bytes.length;
74
+ return this;
75
+ }
76
+
77
+ zeros(count: number): this {
78
+ this.#ensure(count);
79
+ this.offset += count;
80
+ return this;
81
+ }
82
+
83
+ /** UTF-16LE, optionally padded or truncated to a fixed byte width. */
84
+ utf16le(text: string, fixedBytes?: number): this {
85
+ const start = this.offset;
86
+ for (const unit of text) {
87
+ const code = unit.codePointAt(0) ?? 0;
88
+ // Fixed-width fields must keep room for the terminating NUL.
89
+ if (fixedBytes !== undefined && this.offset - start >= fixedBytes - 2) break;
90
+ if (code > 0xffff) {
91
+ // Surrogate pair: both halves are already what UTF-16 needs.
92
+ for (let i = 0; i < unit.length; i++) this.u16le(unit.charCodeAt(i));
93
+ } else {
94
+ this.u16le(code);
95
+ }
96
+ }
97
+ if (fixedBytes !== undefined) {
98
+ this.zeros(fixedBytes - (this.offset - start));
99
+ }
100
+ return this;
101
+ }
102
+
103
+ /** Overwrite an earlier 16-bit slot, for length fields known only later. */
104
+ patchU16le(position: number, value: number): this {
105
+ this.view.setUint16(position, value, true);
106
+ return this;
107
+ }
108
+
109
+ patchU16be(position: number, value: number): this {
110
+ this.view.setUint16(position, value, false);
111
+ return this;
112
+ }
113
+
114
+ get length(): number {
115
+ return this.offset;
116
+ }
117
+
118
+ take(): Uint8Array {
119
+ return this.bytes.slice(0, this.offset);
120
+ }
121
+ }
122
+
123
+ export class Reader {
124
+ private readonly bytes: Uint8Array;
125
+ private readonly view: DataView;
126
+ offset: number;
127
+
128
+ constructor(bytes: Uint8Array, offset = 0) {
129
+ this.bytes = bytes;
130
+ this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
131
+ this.offset = offset;
132
+ }
133
+
134
+ get remaining(): number {
135
+ return this.bytes.length - this.offset;
136
+ }
137
+
138
+ #need(count: number): void {
139
+ if (this.remaining < count) {
140
+ throw new Error(`RDP: truncated frame (needed ${count} bytes, ${this.remaining} left)`);
141
+ }
142
+ }
143
+
144
+ u8(): number {
145
+ this.#need(1);
146
+ return this.view.getUint8(this.offset++);
147
+ }
148
+
149
+ u16le(): number {
150
+ this.#need(2);
151
+ const value = this.view.getUint16(this.offset, true);
152
+ this.offset += 2;
153
+ return value;
154
+ }
155
+
156
+ u16be(): number {
157
+ this.#need(2);
158
+ const value = this.view.getUint16(this.offset, false);
159
+ this.offset += 2;
160
+ return value;
161
+ }
162
+
163
+ u32le(): number {
164
+ this.#need(4);
165
+ const value = this.view.getUint32(this.offset, true);
166
+ this.offset += 4;
167
+ return value;
168
+ }
169
+
170
+ u32be(): number {
171
+ this.#need(4);
172
+ const value = this.view.getUint32(this.offset, false);
173
+ this.offset += 4;
174
+ return value;
175
+ }
176
+
177
+ raw(count: number): Uint8Array {
178
+ this.#need(count);
179
+ const slice = this.bytes.subarray(this.offset, this.offset + count);
180
+ this.offset += count;
181
+ return slice;
182
+ }
183
+
184
+ skip(count: number): this {
185
+ this.#need(count);
186
+ this.offset += count;
187
+ return this;
188
+ }
189
+
190
+ rest(): Uint8Array {
191
+ return this.bytes.subarray(this.offset);
192
+ }
193
+ }
194
+
195
+ export function concatBytes(...parts: readonly Uint8Array[]): Uint8Array {
196
+ const total = parts.reduce((sum, part) => sum + part.length, 0);
197
+ const out = allocate(total);
198
+ let offset = 0;
199
+ for (const part of parts) {
200
+ out.set(part, offset);
201
+ offset += part.length;
202
+ }
203
+ return out;
204
+ }
@@ -0,0 +1,304 @@
1
+ // Capability exchange (MS-RDPBCGR 2.2.7).
2
+ //
3
+ // This client deliberately advertises no drawing-order support and no bitmap
4
+ // cache, which makes the host fall back to plain bitmap updates for the whole
5
+ // screen. That is the one output path a from-scratch client can implement
6
+ // correctly, and it is what keeps the rest of this module small.
7
+
8
+ import { Reader, Writer, concatBytes } from './buffer';
9
+
10
+ export const CAPSET = {
11
+ GENERAL: 1,
12
+ BITMAP: 2,
13
+ ORDER: 3,
14
+ BITMAPCACHE: 4,
15
+ CONTROL: 5,
16
+ ACTIVATION: 7,
17
+ POINTER: 8,
18
+ SHARE: 9,
19
+ COLORCACHE: 10,
20
+ SOUND: 12,
21
+ INPUT: 13,
22
+ FONT: 14,
23
+ BRUSH: 15,
24
+ GLYPHCACHE: 16,
25
+ OFFSCREENCACHE: 17,
26
+ VIRTUALCHANNEL: 20,
27
+ RAIL: 23,
28
+ WINDOW: 24,
29
+ MULTIFRAGMENTUPDATE: 26,
30
+ LARGE_POINTER: 27,
31
+ SURFACE_COMMANDS: 28,
32
+ BITMAP_CODECS: 29,
33
+ };
34
+
35
+ const INPUT_FLAG_SCANCODES = 0x0001;
36
+ const INPUT_FLAG_MOUSEX = 0x0004;
37
+ const INPUT_FLAG_UNICODE = 0x0010;
38
+
39
+ const FASTPATH_OUTPUT_SUPPORTED = 0x0001;
40
+ const LONG_CREDENTIALS_SUPPORTED = 0x0004;
41
+ const NEGOTIATE_ORDER_SUPPORT = 0x0002;
42
+ const ZERO_BOUNDS_DELTA_SUPPORT = 0x0008;
43
+
44
+ function capabilitySet(type: number, body: Uint8Array) {
45
+ const writer = new Writer(body.length + 4);
46
+ writer.u16le(type).u16le(body.length + 4).raw(body);
47
+ return writer.take();
48
+ }
49
+
50
+ function generalCapability() {
51
+ const writer = new Writer(24);
52
+ writer.u16le(1); // osMajorType: Windows
53
+ writer.u16le(3); // osMinorType: Windows NT
54
+ writer.u16le(0x0200); // protocolVersion
55
+ writer.u16le(0); // pad
56
+ writer.u16le(0); // generalCompressionTypes
57
+ // Fast-path output has to be advertised. Windows 8 and later send graphics
58
+ // only on the fast path, so a client that asks for the slow path alone gets a
59
+ // clean session that never paints anything.
60
+ writer.u16le(FASTPATH_OUTPUT_SUPPORTED | LONG_CREDENTIALS_SUPPORTED); // extraFlags
61
+ writer.u16le(0); // updateCapabilityFlag
62
+ writer.u16le(0); // remoteUnshareFlag
63
+ writer.u16le(0); // generalCompressionLevel
64
+ // Both are declarations that the client may SEND these PDUs. The initial
65
+ // full-screen paint depends on it: a host that thinks the client cannot ask
66
+ // for a refresh has no reason to honour the request.
67
+ writer.u8(1); // refreshRectSupport
68
+ writer.u8(1); // suppressOutputSupport
69
+ return capabilitySet(CAPSET.GENERAL, writer.take());
70
+ }
71
+
72
+ function bitmapCapability(width: number, height: number) {
73
+ const writer = new Writer(28);
74
+ writer.u16le(16); // preferredBitsPerPixel
75
+ writer.u16le(1); // receive1BitPerPixel
76
+ writer.u16le(1); // receive4BitsPerPixel
77
+ writer.u16le(1); // receive8BitsPerPixel
78
+ writer.u16le(width);
79
+ writer.u16le(height);
80
+ writer.u16le(0); // pad
81
+ writer.u16le(1); // desktopResizeFlag
82
+ writer.u16le(1); // bitmapCompressionFlag
83
+ writer.u8(0); // highColorFlags
84
+ writer.u8(0); // drawingFlags
85
+ writer.u16le(1); // multipleRectangleSupport
86
+ writer.u16le(0); // pad
87
+ return capabilitySet(CAPSET.BITMAP, writer.take());
88
+ }
89
+
90
+ function orderCapability() {
91
+ const writer = new Writer(88);
92
+ writer.zeros(16); // terminalDescriptor
93
+ writer.u32le(0); // pad
94
+ writer.u16le(1); // desktopSaveXGranularity
95
+ writer.u16le(20); // desktopSaveYGranularity
96
+ writer.u16le(0); // pad
97
+ writer.u16le(1); // maximumOrderLevel
98
+ writer.u16le(0); // numberFonts
99
+ writer.u16le(NEGOTIATE_ORDER_SUPPORT | ZERO_BOUNDS_DELTA_SUPPORT);
100
+ writer.zeros(32); // orderSupport: none
101
+ writer.u16le(0); // textFlags
102
+ writer.u16le(0); // orderSupportExFlags
103
+ writer.u32le(0); // pad
104
+ writer.u32le(230400); // desktopSaveSize
105
+ writer.u16le(0); // pad
106
+ writer.u16le(0); // pad
107
+ writer.u16le(0); // textANSICodePage
108
+ writer.u16le(0); // pad
109
+ return capabilitySet(CAPSET.ORDER, writer.take());
110
+ }
111
+
112
+ /** Zero entries in every cache: the host must send bitmaps, never cache hits. */
113
+ function bitmapCacheCapability() {
114
+ const writer = new Writer(40);
115
+ writer.zeros(24); // pad1..pad6
116
+ writer.u16le(0).u16le(0);
117
+ writer.u16le(0).u16le(0);
118
+ writer.u16le(0).u16le(0);
119
+ return capabilitySet(CAPSET.BITMAPCACHE, writer.take());
120
+ }
121
+
122
+ function controlCapability() {
123
+ const writer = new Writer(12);
124
+ writer.u16le(0); // controlFlags
125
+ writer.u16le(0); // remoteDetachFlag
126
+ writer.u16le(2); // controlInterest: never
127
+ writer.u16le(2); // detachInterest: never
128
+ return capabilitySet(CAPSET.CONTROL, writer.take());
129
+ }
130
+
131
+ function activationCapability() {
132
+ const writer = new Writer(12);
133
+ writer.u16le(0).u16le(0).u16le(0).u16le(0);
134
+ return capabilitySet(CAPSET.ACTIVATION, writer.take());
135
+ }
136
+
137
+ function pointerCapability() {
138
+ const writer = new Writer(10);
139
+ writer.u16le(1); // colorPointerFlag
140
+ writer.u16le(20); // colorPointerCacheSize
141
+ writer.u16le(20); // pointerCacheSize
142
+ return capabilitySet(CAPSET.POINTER, writer.take());
143
+ }
144
+
145
+ function shareCapability() {
146
+ const writer = new Writer(8);
147
+ writer.u16le(0).u16le(0);
148
+ return capabilitySet(CAPSET.SHARE, writer.take());
149
+ }
150
+
151
+ function colorCacheCapability() {
152
+ const writer = new Writer(8);
153
+ writer.u16le(6).u16le(0);
154
+ return capabilitySet(CAPSET.COLORCACHE, writer.take());
155
+ }
156
+
157
+ function soundCapability() {
158
+ const writer = new Writer(8);
159
+ writer.u16le(0).u16le(0);
160
+ return capabilitySet(CAPSET.SOUND, writer.take());
161
+ }
162
+
163
+ function inputCapability(keyboardLayout: number) {
164
+ const writer = new Writer(88);
165
+ writer.u16le(INPUT_FLAG_SCANCODES | INPUT_FLAG_MOUSEX | INPUT_FLAG_UNICODE);
166
+ writer.u16le(0); // pad
167
+ writer.u32le(keyboardLayout);
168
+ writer.u32le(4); // keyboardType: IBM enhanced
169
+ writer.u32le(0); // keyboardSubType
170
+ writer.u32le(12); // keyboardFunctionKey
171
+ writer.zeros(64); // imeFileName
172
+ return capabilitySet(CAPSET.INPUT, writer.take());
173
+ }
174
+
175
+ function fontCapability() {
176
+ const writer = new Writer(8);
177
+ writer.u16le(1).u16le(0);
178
+ return capabilitySet(CAPSET.FONT, writer.take());
179
+ }
180
+
181
+ function brushCapability() {
182
+ const writer = new Writer(8);
183
+ writer.u32le(0); // BRUSH_DEFAULT
184
+ return capabilitySet(CAPSET.BRUSH, writer.take());
185
+ }
186
+
187
+ function glyphCacheCapability() {
188
+ const writer = new Writer(52);
189
+ for (let i = 0; i < 10; i++) writer.u16le(0).u16le(0); // GlyphCache entries
190
+ writer.u32le(0); // FragCache
191
+ writer.u16le(0); // GLYPH_SUPPORT_NONE
192
+ writer.u16le(0); // pad
193
+ return capabilitySet(CAPSET.GLYPHCACHE, writer.take());
194
+ }
195
+
196
+ function offscreenCacheCapability() {
197
+ const writer = new Writer(12);
198
+ writer.u32le(0); // offscreenSupportLevel: none
199
+ writer.u16le(0); // offscreenCacheSize
200
+ writer.u16le(0); // offscreenCacheEntries
201
+ return capabilitySet(CAPSET.OFFSCREENCACHE, writer.take());
202
+ }
203
+
204
+ function remoteProgramsCapability() {
205
+ const writer = new Writer(4);
206
+ writer.u32le(0x00000001); // TS_RAIL_LEVEL_SUPPORTED
207
+ return capabilitySet(CAPSET.RAIL, writer.take());
208
+ }
209
+
210
+ function windowListCapability(iconCaches = 0, iconCacheEntries = 0) {
211
+ const writer = new Writer(7);
212
+ writer.u32le(0x00000001); // TS_WINDOW_LEVEL_SUPPORTED
213
+ writer.u8(iconCaches);
214
+ writer.u16le(iconCacheEntries);
215
+ return capabilitySet(CAPSET.WINDOW, writer.take());
216
+ }
217
+
218
+ export function buildClientCapabilities({ width, height, keyboardLayout, remoteApp = false, remoteAppCaps = null }) {
219
+ const sets = [
220
+ generalCapability(),
221
+ bitmapCapability(width, height),
222
+ orderCapability(),
223
+ bitmapCacheCapability(),
224
+ colorCacheCapability(),
225
+ activationCapability(),
226
+ controlCapability(),
227
+ pointerCapability(),
228
+ shareCapability(),
229
+ inputCapability(keyboardLayout),
230
+ brushCapability(),
231
+ glyphCacheCapability(),
232
+ soundCapability(),
233
+ fontCapability(),
234
+ offscreenCacheCapability(),
235
+ ];
236
+
237
+ if (remoteApp) {
238
+ sets.push(remoteProgramsCapability());
239
+ sets.push(windowListCapability(
240
+ remoteAppCaps?.numIconCaches || 0,
241
+ remoteAppCaps?.numIconCacheEntries || 0
242
+ ));
243
+ }
244
+
245
+ return { bytes: concatBytes(...sets), count: sets.length };
246
+ }
247
+
248
+ /**
249
+ * Read the host's Demand Active capabilities. The desktop size it reports wins
250
+ * over anything the client asked for.
251
+ */
252
+ export function parseServerCapabilities(bytes: Uint8Array) {
253
+ const reader = new Reader(bytes);
254
+ const numberCapabilities = reader.u16le();
255
+ reader.u16le(); // pad
256
+
257
+ const result = {
258
+ desktopWidth: 0,
259
+ desktopHeight: 0,
260
+ preferredBitsPerPixel: 16,
261
+ refreshRectSupport: false,
262
+ railSupportLevel: 0,
263
+ windowSupportLevel: 0,
264
+ numIconCaches: 0,
265
+ numIconCacheEntries: 0,
266
+ types: [],
267
+ };
268
+
269
+ for (let i = 0; i < numberCapabilities && reader.remaining >= 4; i++) {
270
+ const type = reader.u16le();
271
+ const length = reader.u16le();
272
+ if (length < 4 || length - 4 > reader.remaining) break;
273
+ const body = new Reader(reader.raw(length - 4));
274
+ result.types.push(type);
275
+
276
+ if (type === CAPSET.BITMAP && body.remaining >= 20) {
277
+ result.preferredBitsPerPixel = body.u16le();
278
+ body.skip(6); // receive1/4/8BitsPerPixel
279
+ result.desktopWidth = body.u16le();
280
+ result.desktopHeight = body.u16le();
281
+ continue;
282
+ }
283
+
284
+ if (type === CAPSET.GENERAL && body.remaining >= 20) {
285
+ body.skip(18);
286
+ result.refreshRectSupport = body.u8() !== 0;
287
+ continue;
288
+ }
289
+
290
+ if (type === CAPSET.RAIL && body.remaining >= 4) {
291
+ result.railSupportLevel = body.u32le();
292
+ continue;
293
+ }
294
+
295
+ if (type === CAPSET.WINDOW && body.remaining >= 7) {
296
+ result.windowSupportLevel = body.u32le();
297
+ result.numIconCaches = body.u8();
298
+ result.numIconCacheEntries = body.u16le();
299
+ continue;
300
+ }
301
+ }
302
+
303
+ return result;
304
+ }