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.
- package/README.md +50 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +7 -0
- package/dist/protocols/index.js +35 -0
- package/dist/protocols/index.js.map +7 -0
- package/dist/protocols/rdp/buffer.js +170 -0
- package/dist/protocols/rdp/buffer.js.map +7 -0
- package/dist/protocols/rdp/caps.js +260 -0
- package/dist/protocols/rdp/caps.js.map +7 -0
- package/dist/protocols/rdp/cert.js +135 -0
- package/dist/protocols/rdp/cert.js.map +7 -0
- package/dist/protocols/rdp/client.js +1068 -0
- package/dist/protocols/rdp/client.js.map +7 -0
- package/dist/protocols/rdp/cliprdr.js +240 -0
- package/dist/protocols/rdp/cliprdr.js.map +7 -0
- package/dist/protocols/rdp/credssp.js +254 -0
- package/dist/protocols/rdp/credssp.js.map +7 -0
- package/dist/protocols/rdp/crypto.js +369 -0
- package/dist/protocols/rdp/crypto.js.map +7 -0
- package/dist/protocols/rdp/display.js +589 -0
- package/dist/protocols/rdp/display.js.map +7 -0
- package/dist/protocols/rdp/gcc.js +132 -0
- package/dist/protocols/rdp/gcc.js.map +7 -0
- package/dist/protocols/rdp/keymap.js +132 -0
- package/dist/protocols/rdp/keymap.js.map +7 -0
- package/dist/protocols/rdp/mcs.js +223 -0
- package/dist/protocols/rdp/mcs.js.map +7 -0
- package/dist/protocols/rdp/ntlm.js +305 -0
- package/dist/protocols/rdp/ntlm.js.map +7 -0
- package/dist/protocols/rdp/pdu.js +444 -0
- package/dist/protocols/rdp/pdu.js.map +7 -0
- package/dist/protocols/rdp/rail.js +383 -0
- package/dist/protocols/rdp/rail.js.map +7 -0
- package/dist/protocols/rdp/rle.js +276 -0
- package/dist/protocols/rdp/rle.js.map +7 -0
- package/dist/protocols/rdp/sec.js +156 -0
- package/dist/protocols/rdp/sec.js.map +7 -0
- package/dist/protocols/rdp/session.js +307 -0
- package/dist/protocols/rdp/session.js.map +7 -0
- package/dist/protocols/rdp/tls.js +354 -0
- package/dist/protocols/rdp/tls.js.map +7 -0
- package/dist/protocols/rdp/vchannel.js +59 -0
- package/dist/protocols/rdp/vchannel.js.map +7 -0
- package/dist/protocols/rdp/x224.js +110 -0
- package/dist/protocols/rdp/x224.js.map +7 -0
- package/dist/protocols/ssh/kex.js +177 -0
- package/dist/protocols/ssh/kex.js.map +7 -0
- package/dist/protocols/ssh/messages.js +55 -0
- package/dist/protocols/ssh/messages.js.map +7 -0
- package/dist/protocols/ssh/session.js +608 -0
- package/dist/protocols/ssh/session.js.map +7 -0
- package/dist/protocols/ssh/terminal.js +191 -0
- package/dist/protocols/ssh/terminal.js.map +7 -0
- package/dist/protocols/ssh/transport.js +611 -0
- package/dist/protocols/ssh/transport.js.map +7 -0
- package/dist/protocols/ssh/wire.js +189 -0
- package/dist/protocols/ssh/wire.js.map +7 -0
- package/dist/protocols/types.js +1 -0
- package/dist/protocols/types.js.map +7 -0
- package/dist/protocols/vnc/input.js +16 -0
- package/dist/protocols/vnc/input.js.map +7 -0
- package/dist/protocols/vnc/session.js +259 -0
- package/dist/protocols/vnc/session.js.map +7 -0
- package/dist/shared/connection.js +114 -0
- package/dist/shared/connection.js.map +7 -0
- package/dist/shared/device.js +1 -0
- package/dist/shared/device.js.map +7 -0
- package/dist/shared/hosts.js +133 -0
- package/dist/shared/hosts.js.map +7 -0
- package/dist/shared/icons.js +42 -0
- package/dist/shared/icons.js.map +7 -0
- package/dist/shared/protocol.js +40 -0
- package/dist/shared/protocol.js.map +7 -0
- package/dist/shared/signals.js +42 -0
- package/dist/shared/signals.js.map +7 -0
- package/package.json +82 -0
- package/src/index.ts +14 -0
- package/src/protocols/index.ts +72 -0
- package/src/protocols/rdp/buffer.ts +204 -0
- package/src/protocols/rdp/caps.ts +304 -0
- package/src/protocols/rdp/cert.ts +190 -0
- package/src/protocols/rdp/client.ts +1363 -0
- package/src/protocols/rdp/cliprdr.ts +344 -0
- package/src/protocols/rdp/credssp.ts +319 -0
- package/src/protocols/rdp/crypto.ts +416 -0
- package/src/protocols/rdp/display.ts +750 -0
- package/src/protocols/rdp/gcc.ts +162 -0
- package/src/protocols/rdp/keymap.ts +68 -0
- package/src/protocols/rdp/mcs.ts +283 -0
- package/src/protocols/rdp/ntlm.ts +380 -0
- package/src/protocols/rdp/pdu.ts +497 -0
- package/src/protocols/rdp/rail.ts +480 -0
- package/src/protocols/rdp/rle.ts +343 -0
- package/src/protocols/rdp/sec.ts +184 -0
- package/src/protocols/rdp/session.ts +393 -0
- package/src/protocols/rdp/tls.ts +452 -0
- package/src/protocols/rdp/vchannel.ts +89 -0
- package/src/protocols/rdp/x224.ts +139 -0
- package/src/protocols/ssh/kex.ts +227 -0
- package/src/protocols/ssh/messages.ts +54 -0
- package/src/protocols/ssh/session.ts +878 -0
- package/src/protocols/ssh/terminal.ts +242 -0
- package/src/protocols/ssh/transport.ts +732 -0
- package/src/protocols/ssh/wire.ts +220 -0
- package/src/protocols/types.ts +146 -0
- package/src/protocols/vnc/input.ts +13 -0
- package/src/protocols/vnc/novnc.d.ts +75 -0
- package/src/protocols/vnc/session.ts +367 -0
- package/src/shared/connection.ts +206 -0
- package/src/shared/device.ts +28 -0
- package/src/shared/hosts.ts +240 -0
- package/src/shared/icons.ts +49 -0
- package/src/shared/protocol.ts +55 -0
- package/src/shared/signals.ts +67 -0
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The gateway's own roster of machines, and the networks it is allowed to dial.
|
|
3
|
+
*
|
|
4
|
+
* Parsing lives here, with no reference to the Worker environment, so the same
|
|
5
|
+
* code shapes the list in `VNC_HOSTS`/`RDP_HOSTS` and the list the browser
|
|
6
|
+
* receives from `/api/hosts`. Reading those variables is the Worker's job --
|
|
7
|
+
* see `src/worker/config/hosts.ts`.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { RemoteAppLaunch } from './connection';
|
|
11
|
+
import {
|
|
12
|
+
DEFAULT_PORTS,
|
|
13
|
+
DEFAULT_SSH_PORT,
|
|
14
|
+
isRdpSecurity,
|
|
15
|
+
toPort,
|
|
16
|
+
toRemoteProtocol,
|
|
17
|
+
type RdpSecurity,
|
|
18
|
+
type RemoteProtocol,
|
|
19
|
+
} from './protocol';
|
|
20
|
+
|
|
21
|
+
/** Optional SSH access to the same machine, which publishes a terminal for it. */
|
|
22
|
+
export interface HostSshAccess {
|
|
23
|
+
port: number;
|
|
24
|
+
username?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** One machine as the gateway's configuration describes it. */
|
|
28
|
+
export interface RemoteHostTarget {
|
|
29
|
+
id: string;
|
|
30
|
+
name: string;
|
|
31
|
+
host: string;
|
|
32
|
+
port: number;
|
|
33
|
+
protocol: RemoteProtocol;
|
|
34
|
+
/** Windows domain or computer name pre-filled in the connect dialog. */
|
|
35
|
+
domain?: string;
|
|
36
|
+
security?: RdpSecurity;
|
|
37
|
+
remoteApp?: RemoteAppLaunch;
|
|
38
|
+
ssh?: HostSshAccess;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Raw JSON entries are whatever the operator typed; nothing is assumed. */
|
|
42
|
+
type RawHostEntry = Record<string, unknown>;
|
|
43
|
+
|
|
44
|
+
function normalizeRemoteApp(value: unknown): RemoteAppLaunch | undefined {
|
|
45
|
+
if (typeof value === 'string' && value.trim()) return { program: value.trim() };
|
|
46
|
+
if (!value || typeof value !== 'object') return undefined;
|
|
47
|
+
|
|
48
|
+
const candidate = value as RawHostEntry;
|
|
49
|
+
const program = String(candidate.program ?? '').trim();
|
|
50
|
+
if (!program) return undefined;
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
program,
|
|
54
|
+
...(candidate.workingDir ? { workingDir: String(candidate.workingDir) } : {}),
|
|
55
|
+
...(candidate.arguments ? { arguments: String(candidate.arguments) } : {}),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* `ssh` accepts `true`, a port number, or an object. Anything else means the
|
|
61
|
+
* machine has no terminal to offer and no panel is shown for it.
|
|
62
|
+
*/
|
|
63
|
+
function normalizeSsh(value: unknown, fallbackUsername: unknown): HostSshAccess | undefined {
|
|
64
|
+
const inherited = typeof fallbackUsername === 'string' && fallbackUsername.trim()
|
|
65
|
+
? fallbackUsername.trim()
|
|
66
|
+
: undefined;
|
|
67
|
+
|
|
68
|
+
if (value === true) {
|
|
69
|
+
return { port: DEFAULT_SSH_PORT, ...(inherited ? { username: inherited } : {}) };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (typeof value === 'number' || (typeof value === 'string' && /^\d+$/.test(value.trim()))) {
|
|
73
|
+
const port = toPort(value, 0);
|
|
74
|
+
if (!port) return undefined;
|
|
75
|
+
return { port, ...(inherited ? { username: inherited } : {}) };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (!value || typeof value !== 'object') return undefined;
|
|
79
|
+
|
|
80
|
+
const candidate = value as RawHostEntry;
|
|
81
|
+
if (candidate.enabled === false) return undefined;
|
|
82
|
+
|
|
83
|
+
const username = typeof candidate.username === 'string' && candidate.username.trim()
|
|
84
|
+
? candidate.username.trim()
|
|
85
|
+
: inherited;
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
port: toPort(candidate.port, DEFAULT_SSH_PORT),
|
|
89
|
+
...(username ? { username } : {}),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function parseHostList(
|
|
94
|
+
hostsJson: string | undefined,
|
|
95
|
+
defaultProtocol: RemoteProtocol,
|
|
96
|
+
defaultHost: string,
|
|
97
|
+
defaultPort: string,
|
|
98
|
+
idPrefix: string
|
|
99
|
+
): RemoteHostTarget[] {
|
|
100
|
+
if (!hostsJson) return [];
|
|
101
|
+
|
|
102
|
+
let parsed: unknown;
|
|
103
|
+
try {
|
|
104
|
+
parsed = JSON.parse(hostsJson);
|
|
105
|
+
} catch (error) {
|
|
106
|
+
console.warn(`Failed to parse ${idPrefix.toUpperCase()}_HOSTS JSON, ignoring it:`, error);
|
|
107
|
+
return [];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (!Array.isArray(parsed) || parsed.length === 0) return [];
|
|
111
|
+
|
|
112
|
+
return parsed.map((raw: unknown, index): RemoteHostTarget => {
|
|
113
|
+
const entry = (raw ?? {}) as RawHostEntry;
|
|
114
|
+
const protocol = toRemoteProtocol(entry.protocol, defaultProtocol);
|
|
115
|
+
const remoteApp = protocol === 'rdp' ? normalizeRemoteApp(entry.remoteApp) : undefined;
|
|
116
|
+
const ssh = normalizeSsh(entry.ssh ?? entry.sshPort, entry.sshUser ?? entry.sshUsername);
|
|
117
|
+
|
|
118
|
+
const host = String(entry.host ?? '') || defaultHost;
|
|
119
|
+
const name = String(entry.name ?? '').trim() || host;
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
id: String(entry.id ?? '') || `${idPrefix}-${index + 1}`,
|
|
123
|
+
name,
|
|
124
|
+
host,
|
|
125
|
+
port: toPort(entry.port ?? defaultPort, DEFAULT_PORTS[protocol]),
|
|
126
|
+
protocol,
|
|
127
|
+
...(entry.domain ? { domain: String(entry.domain) } : {}),
|
|
128
|
+
...(isRdpSecurity(entry.security) ? { security: entry.security } : {}),
|
|
129
|
+
...(remoteApp ? { remoteApp } : {}),
|
|
130
|
+
...(ssh ? { ssh } : {}),
|
|
131
|
+
};
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Build the target list offered in the connect dialog. `VNC_HOSTS` and
|
|
137
|
+
* `RDP_HOSTS` are merged into one list; each entry may override its protocol,
|
|
138
|
+
* so a single list can carry both kinds of target.
|
|
139
|
+
*/
|
|
140
|
+
export function parseConfiguredHosts(options: {
|
|
141
|
+
vncHostsJson?: string;
|
|
142
|
+
rdpHostsJson?: string;
|
|
143
|
+
defaultHost?: string;
|
|
144
|
+
defaultPort?: string;
|
|
145
|
+
defaultRdpHost?: string;
|
|
146
|
+
defaultRdpPort?: string;
|
|
147
|
+
}): RemoteHostTarget[] {
|
|
148
|
+
const defaultHost = options.defaultHost || '10.0.0.10';
|
|
149
|
+
const defaultPort = options.defaultPort || String(DEFAULT_PORTS.vnc);
|
|
150
|
+
|
|
151
|
+
const hosts = [
|
|
152
|
+
...parseHostList(options.vncHostsJson, 'vnc', defaultHost, defaultPort, 'vnc-host'),
|
|
153
|
+
...parseHostList(
|
|
154
|
+
options.rdpHostsJson,
|
|
155
|
+
'rdp',
|
|
156
|
+
options.defaultRdpHost || defaultHost,
|
|
157
|
+
options.defaultRdpPort || String(DEFAULT_PORTS.rdp),
|
|
158
|
+
'rdp-host'
|
|
159
|
+
),
|
|
160
|
+
];
|
|
161
|
+
|
|
162
|
+
if (hosts.length > 0) return hosts;
|
|
163
|
+
|
|
164
|
+
return [
|
|
165
|
+
{
|
|
166
|
+
id: 'default-host',
|
|
167
|
+
name: 'Primary Host',
|
|
168
|
+
host: defaultHost,
|
|
169
|
+
port: toPort(defaultPort, DEFAULT_PORTS.vnc),
|
|
170
|
+
protocol: 'vnc',
|
|
171
|
+
},
|
|
172
|
+
];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** One allowed network, as a base address and a mask. */
|
|
176
|
+
export interface Subnet {
|
|
177
|
+
base: number;
|
|
178
|
+
mask: number;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Dotted-quad to a 32-bit number, or null for anything that is not one. */
|
|
182
|
+
export function ipv4ToInt(value: string): number | null {
|
|
183
|
+
const parts = String(value).trim().split('.');
|
|
184
|
+
if (parts.length !== 4) return null;
|
|
185
|
+
|
|
186
|
+
let result = 0;
|
|
187
|
+
for (const part of parts) {
|
|
188
|
+
if (!/^\d{1,3}$/.test(part)) return null;
|
|
189
|
+
const octet = Number.parseInt(part, 10);
|
|
190
|
+
if (octet > 255) return null;
|
|
191
|
+
result = (result << 8) | octet;
|
|
192
|
+
}
|
|
193
|
+
return result >>> 0;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The networks this gateway may dial, beyond the machines named in the host
|
|
198
|
+
* lists. Written as `ALLOWED_SUBNETS`, comma-separated:
|
|
199
|
+
*
|
|
200
|
+
* 192.168.8.0/24 a CIDR block
|
|
201
|
+
* 192.168.8.* the same thing, written the way people say it
|
|
202
|
+
* 192.168.8.67 a single address
|
|
203
|
+
*
|
|
204
|
+
* This is what lets a terminal open on a machine that was never listed. Keep it
|
|
205
|
+
* to networks you own: anything inside one can be reached on any port by anyone
|
|
206
|
+
* who gets past the gateway's login.
|
|
207
|
+
*/
|
|
208
|
+
export function parseAllowedSubnets(value?: string): Subnet[] {
|
|
209
|
+
if (!value) return [];
|
|
210
|
+
|
|
211
|
+
const subnets: Subnet[] = [];
|
|
212
|
+
for (const entry of value.split(',')) {
|
|
213
|
+
const text = entry.trim();
|
|
214
|
+
if (!text) continue;
|
|
215
|
+
|
|
216
|
+
// `192.168.8.*` is the same shape as `192.168.8.0/24`, one octet per step.
|
|
217
|
+
const wildcard = text.split('.').findIndex((part) => part === '*');
|
|
218
|
+
const [address, bits] = wildcard >= 0
|
|
219
|
+
? [text.replaceAll('*', '0'), String(wildcard * 8)]
|
|
220
|
+
: text.split('/');
|
|
221
|
+
|
|
222
|
+
const base = ipv4ToInt(address ?? '');
|
|
223
|
+
if (base === null) continue;
|
|
224
|
+
|
|
225
|
+
const width = bits === undefined ? 32 : Number.parseInt(bits, 10);
|
|
226
|
+
if (!Number.isInteger(width) || width < 0 || width > 32) continue;
|
|
227
|
+
|
|
228
|
+
// A /0 would shift by 32, which JavaScript wraps back to no shift at all.
|
|
229
|
+
const mask = width === 0 ? 0 : (0xffffffff << (32 - width)) >>> 0;
|
|
230
|
+
subnets.push({ base: (base & mask) >>> 0, mask });
|
|
231
|
+
}
|
|
232
|
+
return subnets;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Whether a dotted-quad address falls inside one of the allowed networks. */
|
|
236
|
+
export function hostInAllowedSubnets(host: string, value?: string): boolean {
|
|
237
|
+
const address = ipv4ToInt(host);
|
|
238
|
+
if (address === null) return false;
|
|
239
|
+
return parseAllowedSubnets(value).some(({ base, mask }) => ((address & mask) >>> 0) === base);
|
|
240
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Icon path data, shared by the server-rendered login page and the React app.
|
|
3
|
+
*
|
|
4
|
+
* Each value is the inside of a 24x24 stroked `<svg>`; the wrapper differs
|
|
5
|
+
* between the two (a string on the Worker, a component in the browser) but the
|
|
6
|
+
* geometry should not.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const ICON_PATHS = {
|
|
10
|
+
monitor: '<rect x="2" y="3" width="20" height="14" rx="2"/><path d="M8 21h8M12 17v4"/>',
|
|
11
|
+
link: '<path d="M9 17H7A5 5 0 0 1 7 7h2M15 7h2a5 5 0 1 1 0 10h-2M8 12h8"/>',
|
|
12
|
+
power: '<path d="M12 2v10"/><path d="M18.4 6.6a9 9 0 1 1-12.8 0"/>',
|
|
13
|
+
maximize: '<path d="M8 3H5a2 2 0 0 0-2 2v3M21 8V5a2 2 0 0 0-2-2h-3M3 16v3a2 2 0 0 0 2 2h3M16 21h3a2 2 0 0 0 2-2v-3"/>',
|
|
14
|
+
minimize: '<path d="M8 3v3a2 2 0 0 1-2 2H3M21 8h-3a2 2 0 0 1-2-2V3M3 16h3a2 2 0 0 1 2 2v3M16 21v-3a2 2 0 0 1 2-2h3"/>',
|
|
15
|
+
keyboard: '<rect x="2" y="4" width="20" height="16" rx="2"/><path d="M6 8h.01M10 8h.01M14 8h.01M18 8h.01M8 12h.01M12 12h.01M16 12h.01M7 16h10"/>',
|
|
16
|
+
clipboard: '<rect x="8" y="2" width="8" height="4" rx="1"/><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"/>',
|
|
17
|
+
check: '<path d="m5 13 4 4 10-10"/>',
|
|
18
|
+
camera: '<path d="M14.5 4h-5L7 7H4a2 2 0 0 0-2 2v9a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2h-3l-2.5-3z"/><circle cx="12" cy="13" r="3"/>',
|
|
19
|
+
eye: '<path d="M2 12s3.5-6 10-6 10 6 10 6-3.5 6-10 6S2 12 2 12z"/><circle cx="12" cy="12" r="3"/>',
|
|
20
|
+
sliders: '<path d="M4 21v-7M4 10V3M12 21v-9M12 8V3M20 21v-5M20 12V3M2 14h4M10 8h4M18 16h4"/>',
|
|
21
|
+
expand: '<path d="M15 3h6v6M9 21H3v-6M21 3l-7 7M3 21l7-7"/>',
|
|
22
|
+
shield: '<path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/>',
|
|
23
|
+
chevron: '<path d="m6 9 6 6 6-6"/>',
|
|
24
|
+
back: '<path d="m15 18-6-6 6-6"/>',
|
|
25
|
+
zap: '<path d="M13 2 3 14h9l-1 8 10-12h-9l1-8z"/>',
|
|
26
|
+
cube: '<path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/><path d="m3.3 7 8.7 5 8.7-5M12 22V12"/>',
|
|
27
|
+
logout: '<path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4M16 17l5-5-5-5M21 12H9"/>',
|
|
28
|
+
clock: '<circle cx="12" cy="12" r="9"/><path d="M12 7v5l3.5 2"/>',
|
|
29
|
+
activity: '<path d="M22 12h-4l-3 9L9 3l-3 9H2"/>',
|
|
30
|
+
transfer: '<path d="m3 16 4 4 4-4M7 20V4M21 8l-4-4-4 4M17 4v16"/>',
|
|
31
|
+
screen: '<rect x="2" y="5" width="20" height="13" rx="2"/><path d="M7 21h10"/>',
|
|
32
|
+
command: '<path d="M15 6a3 3 0 1 1 3 3h-3zm0 0v12m0 0a3 3 0 1 0 3-3h-3zM9 6a3 3 0 1 0-3 3h3zm0 0v12m0 0a3 3 0 1 1-3-3h3z"/>',
|
|
33
|
+
apps: '<rect x="3" y="3" width="7" height="7" rx="1"/><rect x="14" y="3" width="7" height="7" rx="1"/><rect x="3" y="14" width="7" height="7" rx="1"/><rect x="14" y="14" width="7" height="7" rx="1"/>',
|
|
34
|
+
terminal: '<path d="m4 17 6-6-6-6M12 19h8"/>',
|
|
35
|
+
folder: '<path d="M3 7h6l2 2h10v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/><path d="M3 7V6a2 2 0 0 1 2-2h4l2 3"/>',
|
|
36
|
+
close: '<path d="M18 6 6 18M6 6l12 12"/>',
|
|
37
|
+
sparkles: '<path d="m12 3 1.9 5.1L19 10l-5.1 1.9L12 17l-1.9-5.1L5 10l5.1-1.9L12 3z"/><path d="m19 15 .8 2.2 2.2.8-2.2.8L19 21l-.8-2.2-2.2-.8 2.2-.8z"/>',
|
|
38
|
+
broom: '<path d="M10 10V4a2 2 0 0 1 4 0v6"/><path d="M6 10h12a2 2 0 0 1 2 2v2H4v-2a2 2 0 0 1 2-2Z"/><path d="m5 14-2 7h18l-2-7M9 18l-1 3M15 18l1 3"/>',
|
|
39
|
+
panel: '<rect x="3" y="4" width="18" height="16" rx="2"/><path d="M14 4v16"/>',
|
|
40
|
+
menu: '<path d="M4 7h16M4 12h16M4 17h16"/>',
|
|
41
|
+
plus: '<path d="M12 5v14M5 12h14"/>',
|
|
42
|
+
star: '<path d="m12 3 2.9 5.9 6.5.9-4.7 4.6 1.1 6.5L12 17.8 6.2 20.9l1.1-6.5L2.6 9.8l6.5-.9L12 3z"/>',
|
|
43
|
+
trash: '<path d="M3 6h18M8 6V4a1 1 0 0 1 1-1h6a1 1 0 0 1 1 1v2M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"/>',
|
|
44
|
+
} as const;
|
|
45
|
+
|
|
46
|
+
export type IconName = keyof typeof ICON_PATHS;
|
|
47
|
+
|
|
48
|
+
/** The tab icon: a monitor in Cloudflare orange. */
|
|
49
|
+
export const FAVICON_DATA_URI = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23f38020' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='2' y='3' width='20' height='14' rx='2'/%3E%3Cpath d='M8 21h8M12 17v4'/%3E%3C/svg%3E";
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The vocabulary both halves of the gateway share.
|
|
3
|
+
*
|
|
4
|
+
* The Worker and the browser have to agree on what a "protocol" is, what ports
|
|
5
|
+
* they default to and how an RDP security layer is named, so those live here
|
|
6
|
+
* rather than being re-declared on each side and drifting apart.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** A remote-desktop protocol the session workspace can display. */
|
|
10
|
+
export const REMOTE_PROTOCOLS = ['vnc', 'rdp'] as const;
|
|
11
|
+
export type RemoteProtocol = (typeof REMOTE_PROTOCOLS)[number];
|
|
12
|
+
|
|
13
|
+
/** Every protocol the gateway relays, including the terminal side panel. */
|
|
14
|
+
export const TRANSPORT_PROTOCOLS = ['vnc', 'rdp', 'ssh'] as const;
|
|
15
|
+
export type TransportProtocol = (typeof TRANSPORT_PROTOCOLS)[number];
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The RDP security layer. `auto` negotiates with the host, which is right for
|
|
19
|
+
* anything that answers honestly; the rest are for hosts that do not.
|
|
20
|
+
*/
|
|
21
|
+
export const RDP_SECURITY_MODES = ['auto', 'nla', 'tls', 'rdp'] as const;
|
|
22
|
+
export type RdpSecurity = (typeof RDP_SECURITY_MODES)[number];
|
|
23
|
+
|
|
24
|
+
export const DEFAULT_PORTS: Record<TransportProtocol, number> = {
|
|
25
|
+
vnc: 5900,
|
|
26
|
+
rdp: 3389,
|
|
27
|
+
ssh: 22,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const DEFAULT_SSH_PORT = DEFAULT_PORTS.ssh;
|
|
31
|
+
|
|
32
|
+
export function isRemoteProtocol(value: unknown): value is RemoteProtocol {
|
|
33
|
+
return REMOTE_PROTOCOLS.includes(value as RemoteProtocol);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function isRdpSecurity(value: unknown): value is RdpSecurity {
|
|
37
|
+
return RDP_SECURITY_MODES.includes(value as RdpSecurity);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Coerce anything to a protocol, falling back to what the caller opens on. */
|
|
41
|
+
export function toRemoteProtocol(value: unknown, fallback: RemoteProtocol = 'vnc'): RemoteProtocol {
|
|
42
|
+
const wanted = String(value ?? '').toLowerCase();
|
|
43
|
+
return isRemoteProtocol(wanted) ? wanted : fallback;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function toRdpSecurity(value: unknown, fallback: RdpSecurity = 'auto'): RdpSecurity {
|
|
47
|
+
const wanted = String(value ?? '').toLowerCase();
|
|
48
|
+
return isRdpSecurity(wanted) ? wanted : fallback;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A port number, or the protocol's default when the value is not one. */
|
|
52
|
+
export function toPort(value: unknown, fallback: number): number {
|
|
53
|
+
const port = typeof value === 'number' ? value : Number.parseInt(String(value ?? ''), 10);
|
|
54
|
+
return Number.isInteger(port) && port > 0 && port < 65536 ? port : fallback;
|
|
55
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Control frames the browser and the gateway exchange on the WebSocket that
|
|
3
|
+
* carries a remote-desktop stream.
|
|
4
|
+
*
|
|
5
|
+
* They are plain strings precisely so they can never be confused with the
|
|
6
|
+
* binary protocol payload flowing in either direction, and both sides import
|
|
7
|
+
* these same literals so the two halves cannot drift apart.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** Browser -> gateway: "my WebSocket is open, you may start reading the host". */
|
|
11
|
+
export const GATEWAY_READY_SIGNAL = 'VNC_GATEWAY_READY_v1';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Browser -> gateway: keep the edge from idling out a silent session.
|
|
15
|
+
* Cloudflare closes a WebSocket with no traffic in either direction for 100s,
|
|
16
|
+
* and an untouched desktop transmits nothing at all.
|
|
17
|
+
*/
|
|
18
|
+
export const GATEWAY_KEEPALIVE_SIGNAL = 'VNC_GATEWAY_KEEPALIVE_v1';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* How often the browser nudges the transport. Well inside the edge's 100s
|
|
22
|
+
* window, so a session that is merely being looked at is never torn down
|
|
23
|
+
* without a close frame -- which is what a bare WebSocket 1006 used to mean.
|
|
24
|
+
*/
|
|
25
|
+
export const KEEPALIVE_INTERVAL_MS = 25_000;
|
|
26
|
+
|
|
27
|
+
export const MAX_CLOSE_REASON_BYTES = 123;
|
|
28
|
+
|
|
29
|
+
/** Whether a live desktop ended for a transient transport reason worth retrying. */
|
|
30
|
+
export function isRetryableDisconnect(diagnostic: {
|
|
31
|
+
close: { code: number; reason: string } | null;
|
|
32
|
+
established: boolean;
|
|
33
|
+
}): boolean {
|
|
34
|
+
if (!diagnostic.established || !diagnostic.close) return false;
|
|
35
|
+
if (diagnostic.close.code === 1006) return true;
|
|
36
|
+
return diagnostic.close.code === 1011
|
|
37
|
+
&& /tcp|socket|network|host closed/i.test(diagnostic.close.reason);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** WebSocket close reasons are capped at 123 bytes; trim without splitting a code point. */
|
|
41
|
+
export function websocketCloseReason(reason: string): string {
|
|
42
|
+
const encoder = new TextEncoder();
|
|
43
|
+
if (encoder.encode(reason).byteLength <= MAX_CLOSE_REASON_BYTES) return reason;
|
|
44
|
+
|
|
45
|
+
let shortened = '';
|
|
46
|
+
for (const character of reason) {
|
|
47
|
+
if (encoder.encode(`${shortened}${character}…`).byteLength > MAX_CLOSE_REASON_BYTES) break;
|
|
48
|
+
shortened += character;
|
|
49
|
+
}
|
|
50
|
+
return `${shortened}…`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 1005/1006/1015 are reserved and cannot be transmitted in a close frame. */
|
|
54
|
+
export function sendableCloseCode(code: number): number {
|
|
55
|
+
return code === 1005 || code === 1006 || code === 1015 ? 1011 : code;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Normalize any WebSocket message payload to bytes. */
|
|
59
|
+
export async function messageToBytes(data: unknown): Promise<Uint8Array | null> {
|
|
60
|
+
if (typeof data === 'string') return new TextEncoder().encode(data);
|
|
61
|
+
if (data instanceof ArrayBuffer) return new Uint8Array(data);
|
|
62
|
+
if (data instanceof Blob) return new Uint8Array(await data.arrayBuffer());
|
|
63
|
+
if (ArrayBuffer.isView(data)) {
|
|
64
|
+
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
65
|
+
}
|
|
66
|
+
return null;
|
|
67
|
+
}
|