herdr-remote-relay 0.3.12 → 0.3.13
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/bin/herdr-remote-relay.js +20 -20
- package/dist/auth-store.d.ts +114 -0
- package/dist/auth-store.js +276 -0
- package/dist/metrics.d.ts +76 -0
- package/dist/metrics.js +249 -0
- package/dist/protocol/frames.d.ts +27 -0
- package/dist/protocol/frames.js +110 -0
- package/dist/protocol/http.d.ts +161 -0
- package/dist/protocol/http.js +3 -0
- package/dist/protocol/index.d.ts +5 -0
- package/dist/protocol/index.js +7 -0
- package/dist/protocol/messages.d.ts +321 -0
- package/dist/protocol/messages.js +18 -0
- package/dist/protocol/paste.d.ts +14 -0
- package/dist/protocol/paste.js +50 -0
- package/dist/protocol/terminal.d.ts +79 -0
- package/dist/protocol/terminal.js +150 -0
- package/dist/relay-config.d.ts +74 -0
- package/dist/relay-config.js +316 -0
- package/dist/relay-server.d.ts +64 -0
- package/dist/relay-server.js +194 -0
- package/dist/scroll-input.d.ts +17 -0
- package/dist/scroll-input.js +124 -0
- package/dist/server/client-channel.d.ts +3 -0
- package/dist/server/client-channel.js +365 -0
- package/dist/server/font-proxy.d.ts +19 -0
- package/dist/server/font-proxy.js +155 -0
- package/dist/server/host-broadcasts.d.ts +25 -0
- package/dist/server/host-broadcasts.js +79 -0
- package/dist/server/host-channel.d.ts +3 -0
- package/dist/server/host-channel.js +266 -0
- package/dist/server/http-api.d.ts +3 -0
- package/dist/server/http-api.js +214 -0
- package/dist/server/maintenance.d.ts +5 -0
- package/dist/server/maintenance.js +64 -0
- package/dist/server/requests.d.ts +34 -0
- package/dist/server/requests.js +142 -0
- package/dist/server/sessions.d.ts +36 -0
- package/dist/server/sessions.js +225 -0
- package/dist/server/sockets.d.ts +12 -0
- package/dist/server/sockets.js +64 -0
- package/dist/server/status.d.ts +11 -0
- package/dist/server/status.js +95 -0
- package/dist/server/transport.d.ts +16 -0
- package/dist/server/transport.js +110 -0
- package/dist/server/types.d.ts +123 -0
- package/dist/server/types.js +3 -0
- package/dist/server/version.d.ts +2 -0
- package/dist/server/version.js +5 -0
- package/dist/state.d.ts +6 -0
- package/dist/state.js +47 -0
- package/package.json +32 -8
- package/web/dist/assets/index-B-5zRmL_.js +61 -0
- package/web/dist/assets/{index-CPVUfBt4.css → index-DKt8KLog.css} +1 -1
- package/web/dist/assets/{vendor-xterm-Bbu8R_5E.js → vendor-xterm-BhCxil_4.js} +2 -2
- package/web/dist/index.html +3 -3
- package/src/auth-store.js +0 -256
- package/src/metrics.js +0 -241
- package/src/relay-config.js +0 -312
- package/src/relay-server.js +0 -1799
- package/src/scroll-input.js +0 -139
- package/src/state.js +0 -47
- package/src/stream-frame.js +0 -238
- package/web/dist/assets/index-6WucmdNP.js +0 -61
package/dist/metrics.js
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import os from 'node:os';
|
|
2
|
+
import { monitorEventLoopDelay } from 'node:perf_hooks';
|
|
3
|
+
function finite(value, fallback = 0) {
|
|
4
|
+
return Number.isFinite(value) ? value : fallback;
|
|
5
|
+
}
|
|
6
|
+
function bytesPerSecond(current, previous, elapsedMs) {
|
|
7
|
+
if (!elapsedMs || elapsedMs <= 0)
|
|
8
|
+
return 0;
|
|
9
|
+
return Math.max(0, ((current - previous) * 1000) / elapsedMs);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* How many people are attached, rather than how many sockets are open.
|
|
13
|
+
*
|
|
14
|
+
* A phone and a laptop belonging to one person are two connections but one
|
|
15
|
+
* paired device each, and one person opening three browser windows is three
|
|
16
|
+
* connections and one device — so the distinct paired device is the closest
|
|
17
|
+
* thing the relay has to a user. Connections that predate device pairing carry
|
|
18
|
+
* no `deviceId`; they fall back to their own connection id so they still count
|
|
19
|
+
* once instead of collapsing into a single anonymous user.
|
|
20
|
+
*/
|
|
21
|
+
function countActiveUsers(clients = []) {
|
|
22
|
+
const identities = new Set();
|
|
23
|
+
for (const client of clients) {
|
|
24
|
+
if (!client)
|
|
25
|
+
continue;
|
|
26
|
+
identities.add(typeof client.deviceId === 'string' && client.deviceId
|
|
27
|
+
? `device:${client.deviceId}`
|
|
28
|
+
: `client:${client.id}`);
|
|
29
|
+
}
|
|
30
|
+
return identities.size;
|
|
31
|
+
}
|
|
32
|
+
class RelayMetrics {
|
|
33
|
+
version;
|
|
34
|
+
protocolVersion;
|
|
35
|
+
startedAt;
|
|
36
|
+
bytesIn;
|
|
37
|
+
bytesOut;
|
|
38
|
+
framesIn;
|
|
39
|
+
framesOut;
|
|
40
|
+
cleanup;
|
|
41
|
+
lastSample;
|
|
42
|
+
hostTraffic;
|
|
43
|
+
cpuSampleAt;
|
|
44
|
+
cpuSample;
|
|
45
|
+
eventLoop;
|
|
46
|
+
constructor({ version = '0.1.0', protocolVersion = 1 } = {}) {
|
|
47
|
+
this.version = version;
|
|
48
|
+
this.protocolVersion = protocolVersion;
|
|
49
|
+
this.startedAt = Date.now();
|
|
50
|
+
this.bytesIn = 0;
|
|
51
|
+
this.bytesOut = 0;
|
|
52
|
+
this.framesIn = 0;
|
|
53
|
+
this.framesOut = 0;
|
|
54
|
+
this.cleanup = {
|
|
55
|
+
staleClientsPurged: 0,
|
|
56
|
+
closedPtysCleaned: 0,
|
|
57
|
+
deadConnectionsClosed: 0,
|
|
58
|
+
idleHostsTerminated: 0,
|
|
59
|
+
slowClientsDropped: 0,
|
|
60
|
+
lastCleanupAt: null,
|
|
61
|
+
};
|
|
62
|
+
this.lastSample = { at: this.startedAt, bytesIn: 0, bytesOut: 0, framesIn: 0, framesOut: 0 };
|
|
63
|
+
this.hostTraffic = new Map();
|
|
64
|
+
this.cpuSampleAt = process.hrtime.bigint();
|
|
65
|
+
this.cpuSample = process.cpuUsage();
|
|
66
|
+
this.eventLoop = monitorEventLoopDelay({ resolution: 20 });
|
|
67
|
+
this.eventLoop.enable();
|
|
68
|
+
}
|
|
69
|
+
hostCounter(hostId) {
|
|
70
|
+
if (typeof hostId !== 'string' || hostId.length === 0)
|
|
71
|
+
return null;
|
|
72
|
+
let counter = this.hostTraffic.get(hostId);
|
|
73
|
+
if (!counter) {
|
|
74
|
+
counter = {
|
|
75
|
+
bytesIn: 0,
|
|
76
|
+
bytesOut: 0,
|
|
77
|
+
framesIn: 0,
|
|
78
|
+
framesOut: 0,
|
|
79
|
+
sampleAt: Date.now(),
|
|
80
|
+
sampleBytesIn: 0,
|
|
81
|
+
sampleBytesOut: 0,
|
|
82
|
+
sampleFramesIn: 0,
|
|
83
|
+
sampleFramesOut: 0,
|
|
84
|
+
};
|
|
85
|
+
this.hostTraffic.set(hostId, counter);
|
|
86
|
+
}
|
|
87
|
+
return counter;
|
|
88
|
+
}
|
|
89
|
+
recordIn(bytes, hostId = null) {
|
|
90
|
+
const amount = Math.max(0, Number(bytes) || 0);
|
|
91
|
+
this.bytesIn += amount;
|
|
92
|
+
this.framesIn += 1;
|
|
93
|
+
const counter = this.hostCounter(hostId);
|
|
94
|
+
if (counter) {
|
|
95
|
+
counter.bytesIn += amount;
|
|
96
|
+
counter.framesIn += 1;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
recordOut(bytes, hostId = null) {
|
|
100
|
+
const amount = Math.max(0, Number(bytes) || 0);
|
|
101
|
+
this.bytesOut += amount;
|
|
102
|
+
this.framesOut += 1;
|
|
103
|
+
const counter = this.hostCounter(hostId);
|
|
104
|
+
if (counter) {
|
|
105
|
+
counter.bytesOut += amount;
|
|
106
|
+
counter.framesOut += 1;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
forgetHost(hostId) {
|
|
110
|
+
if (typeof hostId === 'string')
|
|
111
|
+
this.hostTraffic.delete(hostId);
|
|
112
|
+
}
|
|
113
|
+
hostThroughput(hostId, now = Date.now()) {
|
|
114
|
+
const counter = this.hostCounter(hostId);
|
|
115
|
+
if (!counter)
|
|
116
|
+
return {
|
|
117
|
+
bytesIn: 0,
|
|
118
|
+
bytesOut: 0,
|
|
119
|
+
bytesInPerSec: 0,
|
|
120
|
+
bytesOutPerSec: 0,
|
|
121
|
+
framesIn: 0,
|
|
122
|
+
framesOut: 0,
|
|
123
|
+
framesInPerSec: 0,
|
|
124
|
+
framesOutPerSec: 0,
|
|
125
|
+
};
|
|
126
|
+
const elapsedMs = now - counter.sampleAt;
|
|
127
|
+
const throughput = {
|
|
128
|
+
bytesIn: counter.bytesIn,
|
|
129
|
+
bytesOut: counter.bytesOut,
|
|
130
|
+
bytesInPerSec: bytesPerSecond(counter.bytesIn, counter.sampleBytesIn, elapsedMs),
|
|
131
|
+
bytesOutPerSec: bytesPerSecond(counter.bytesOut, counter.sampleBytesOut, elapsedMs),
|
|
132
|
+
framesIn: counter.framesIn,
|
|
133
|
+
framesOut: counter.framesOut,
|
|
134
|
+
framesInPerSec: bytesPerSecond(counter.framesIn, counter.sampleFramesIn, elapsedMs),
|
|
135
|
+
framesOutPerSec: bytesPerSecond(counter.framesOut, counter.sampleFramesOut, elapsedMs),
|
|
136
|
+
};
|
|
137
|
+
counter.sampleAt = now;
|
|
138
|
+
counter.sampleBytesIn = counter.bytesIn;
|
|
139
|
+
counter.sampleBytesOut = counter.bytesOut;
|
|
140
|
+
counter.sampleFramesIn = counter.framesIn;
|
|
141
|
+
counter.sampleFramesOut = counter.framesOut;
|
|
142
|
+
return throughput;
|
|
143
|
+
}
|
|
144
|
+
recordCleanup(name, amount = 1) {
|
|
145
|
+
if (Object.hasOwn(this.cleanup, name) && typeof this.cleanup[name] === 'number') {
|
|
146
|
+
this.cleanup[name] += amount;
|
|
147
|
+
}
|
|
148
|
+
this.cleanup.lastCleanupAt = new Date().toISOString();
|
|
149
|
+
}
|
|
150
|
+
cpuPercent() {
|
|
151
|
+
const now = process.hrtime.bigint();
|
|
152
|
+
const elapsedNs = Number(now - this.cpuSampleAt);
|
|
153
|
+
const usage = process.cpuUsage(this.cpuSample);
|
|
154
|
+
this.cpuSampleAt = now;
|
|
155
|
+
this.cpuSample = process.cpuUsage();
|
|
156
|
+
if (elapsedNs <= 0)
|
|
157
|
+
return 0;
|
|
158
|
+
const percent = ((usage.user + usage.system) * 100000) / elapsedNs;
|
|
159
|
+
return Math.max(0, Math.round(percent * 100) / 100);
|
|
160
|
+
}
|
|
161
|
+
eventLoopDelay() {
|
|
162
|
+
const percentile = (value) => Math.round(finite(value / 1e6) * 100) / 100;
|
|
163
|
+
return {
|
|
164
|
+
p50Ms: percentile(this.eventLoop.percentile(50)),
|
|
165
|
+
p90Ms: percentile(this.eventLoop.percentile(90)),
|
|
166
|
+
p99Ms: percentile(this.eventLoop.percentile(99)),
|
|
167
|
+
maxMs: percentile(this.eventLoop.max),
|
|
168
|
+
meanMs: percentile(this.eventLoop.mean),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* `activeUserCount` is passed in rather than derived from `clients`: the
|
|
173
|
+
* public `/api/status` rows deliberately omit `deviceId`, so only the caller
|
|
174
|
+
* still holding the full connection records can count devices correctly.
|
|
175
|
+
* Omitting it falls back to counting whatever the given rows identify.
|
|
176
|
+
*/
|
|
177
|
+
snapshot({ clients = [], hosts = [], ptys = [], sample = true, scopeHostId = null, activeUserCount = null, } = {}) {
|
|
178
|
+
const now = Date.now();
|
|
179
|
+
const elapsedMs = now - this.lastSample.at;
|
|
180
|
+
const throughput = {
|
|
181
|
+
bytesIn: this.bytesIn,
|
|
182
|
+
bytesOut: this.bytesOut,
|
|
183
|
+
bytesInPerSec: bytesPerSecond(this.bytesIn, this.lastSample.bytesIn, elapsedMs),
|
|
184
|
+
bytesOutPerSec: bytesPerSecond(this.bytesOut, this.lastSample.bytesOut, elapsedMs),
|
|
185
|
+
framesIn: this.framesIn,
|
|
186
|
+
framesOut: this.framesOut,
|
|
187
|
+
framesInPerSec: bytesPerSecond(this.framesIn, this.lastSample.framesIn, elapsedMs),
|
|
188
|
+
framesOutPerSec: bytesPerSecond(this.framesOut, this.lastSample.framesOut, elapsedMs),
|
|
189
|
+
};
|
|
190
|
+
if (sample) {
|
|
191
|
+
this.lastSample = {
|
|
192
|
+
at: now,
|
|
193
|
+
bytesIn: this.bytesIn,
|
|
194
|
+
bytesOut: this.bytesOut,
|
|
195
|
+
framesIn: this.framesIn,
|
|
196
|
+
framesOut: this.framesOut,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
const memory = process.memoryUsage();
|
|
200
|
+
const load = os.loadavg();
|
|
201
|
+
// Every attached window has full input, so there is no "active controller"
|
|
202
|
+
// to name: reporting the first of them as one had an operator reading a
|
|
203
|
+
// distinction that does not exist. The host is still worth naming, and any
|
|
204
|
+
// client knows which workstation it is on.
|
|
205
|
+
const anyClient = clients[0];
|
|
206
|
+
const scopedThroughput = scopeHostId ? this.hostThroughput(scopeHostId, now) : throughput;
|
|
207
|
+
return {
|
|
208
|
+
version: this.version,
|
|
209
|
+
protocolVersion: this.protocolVersion,
|
|
210
|
+
uptimeSeconds: Math.floor((now - this.startedAt) / 1000),
|
|
211
|
+
startTime: new Date(this.startedAt).toISOString(),
|
|
212
|
+
serverTime: new Date(now).toISOString(),
|
|
213
|
+
activeControllerId: null,
|
|
214
|
+
activeHostId: anyClient?.hostId || hosts[0]?.id || null,
|
|
215
|
+
clients,
|
|
216
|
+
hosts,
|
|
217
|
+
ptys,
|
|
218
|
+
clientCount: clients.length,
|
|
219
|
+
hostCount: hosts.length,
|
|
220
|
+
ptyCount: ptys.length,
|
|
221
|
+
activeUserCount: Number.isFinite(activeUserCount)
|
|
222
|
+
? activeUserCount
|
|
223
|
+
: countActiveUsers(clients),
|
|
224
|
+
throughput: scopedThroughput,
|
|
225
|
+
cpu: {
|
|
226
|
+
load1m: finite(load[0]),
|
|
227
|
+
load5m: finite(load[1]),
|
|
228
|
+
load15m: finite(load[2]),
|
|
229
|
+
cpuPercent: this.cpuPercent(),
|
|
230
|
+
cores: os.cpus().length,
|
|
231
|
+
},
|
|
232
|
+
memory: {
|
|
233
|
+
rssBytes: memory.rss,
|
|
234
|
+
heapUsedBytes: memory.heapUsed,
|
|
235
|
+
heapTotalBytes: memory.heapTotal,
|
|
236
|
+
externalBytes: memory.external,
|
|
237
|
+
systemTotalBytes: os.totalmem(),
|
|
238
|
+
systemFreeBytes: os.freemem(),
|
|
239
|
+
},
|
|
240
|
+
eventLoopDelay: this.eventLoopDelay(),
|
|
241
|
+
cleanup: { ...this.cleanup },
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
close() {
|
|
245
|
+
this.eventLoop.disable();
|
|
246
|
+
this.hostTraffic.clear();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
export { RelayMetrics, countActiveUsers };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export declare const PROTOCOL_VERSION = 1;
|
|
2
|
+
export declare const MAX_HEADER_BYTES: number;
|
|
3
|
+
export declare const FRAME_V2_MAGIC = 255;
|
|
4
|
+
export declare const FRAME_TYPE_OUTPUT = 0;
|
|
5
|
+
export declare const FRAME_TYPE_INPUT = 1;
|
|
6
|
+
export type FrameTypeCode = typeof FRAME_TYPE_OUTPUT | typeof FRAME_TYPE_INPUT;
|
|
7
|
+
export type FrameBytes = Buffer | Uint8Array | ArrayBuffer | readonly number[] | string;
|
|
8
|
+
/** A v1 frame routes by a JSON header naming the stream. */
|
|
9
|
+
export interface StreamFrameV1 {
|
|
10
|
+
version: 1;
|
|
11
|
+
type: string;
|
|
12
|
+
streamId: string;
|
|
13
|
+
payload: Buffer;
|
|
14
|
+
}
|
|
15
|
+
/** A v2 frame routes by a 16-bit stream index. */
|
|
16
|
+
export interface StreamFrameV2 {
|
|
17
|
+
version: 2;
|
|
18
|
+
type: 'output' | 'input';
|
|
19
|
+
typeCode: FrameTypeCode;
|
|
20
|
+
streamIndex: number;
|
|
21
|
+
payload: Buffer;
|
|
22
|
+
}
|
|
23
|
+
export type StreamFrame = StreamFrameV1 | StreamFrameV2;
|
|
24
|
+
export declare function packStreamFrame(type: string, streamId: string, payload?: FrameBytes): Buffer;
|
|
25
|
+
export declare function packStreamFrameV2(type: 'output' | 'input' | FrameTypeCode, streamIndex: number, payload?: FrameBytes): Buffer;
|
|
26
|
+
export declare function unpackStreamFrameV2(buffer: FrameBytes): StreamFrameV2;
|
|
27
|
+
export declare function unpackStreamFrame(value: FrameBytes): StreamFrame;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// Binary stream frames between the host connector and the relay. Node-only
|
|
2
|
+
// (Buffer); browsers receive raw terminal bytes and never parse these.
|
|
3
|
+
export const PROTOCOL_VERSION = 1;
|
|
4
|
+
export const MAX_HEADER_BYTES = 8 * 1024;
|
|
5
|
+
// Framing constants for compact binary frame protocol v2.
|
|
6
|
+
// With permessage-deflate already enabled on the WebSocket connection, the repeated
|
|
7
|
+
// JSON routing header in v1 frames was already compressed down to near-zero network overhead.
|
|
8
|
+
// The actual motivation for v2 framing is reducing CPU consumption: eliminating the per-frame
|
|
9
|
+
// JSON.stringify / JSON.parse serialization and string decoding overhead under heavy terminal output.
|
|
10
|
+
export const FRAME_V2_MAGIC = 0xff;
|
|
11
|
+
export const FRAME_TYPE_OUTPUT = 0;
|
|
12
|
+
export const FRAME_TYPE_INPUT = 1;
|
|
13
|
+
function toBuffer(value) {
|
|
14
|
+
if (Buffer.isBuffer(value))
|
|
15
|
+
return value;
|
|
16
|
+
if (typeof value === 'string')
|
|
17
|
+
return Buffer.from(value);
|
|
18
|
+
if (value instanceof ArrayBuffer)
|
|
19
|
+
return Buffer.from(value);
|
|
20
|
+
return Buffer.from(value);
|
|
21
|
+
}
|
|
22
|
+
export function packStreamFrame(type, streamId, payload = Buffer.alloc(0)) {
|
|
23
|
+
if (typeof type !== 'string' || !type || typeof streamId !== 'string' || !streamId) {
|
|
24
|
+
throw new TypeError('type and streamId must be non-empty strings');
|
|
25
|
+
}
|
|
26
|
+
const body = toBuffer(payload);
|
|
27
|
+
const header = Buffer.from(JSON.stringify({ type, streamId }), 'utf8');
|
|
28
|
+
if (header.length > MAX_HEADER_BYTES)
|
|
29
|
+
throw new RangeError('stream frame header is too large');
|
|
30
|
+
const length = Buffer.allocUnsafe(4);
|
|
31
|
+
length.writeUInt32BE(header.length, 0);
|
|
32
|
+
return Buffer.concat([length, header, body]);
|
|
33
|
+
}
|
|
34
|
+
function unpackStreamFrameV1(value) {
|
|
35
|
+
const frame = toBuffer(value);
|
|
36
|
+
if (frame.length < 4)
|
|
37
|
+
throw new Error('stream frame is truncated');
|
|
38
|
+
const headerLength = frame.readUInt32BE(0);
|
|
39
|
+
if (headerLength === 0 || headerLength > MAX_HEADER_BYTES || frame.length < 4 + headerLength) {
|
|
40
|
+
throw new Error('stream frame header is invalid');
|
|
41
|
+
}
|
|
42
|
+
let header;
|
|
43
|
+
try {
|
|
44
|
+
header = JSON.parse(frame.subarray(4, 4 + headerLength).toString('utf8'));
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
throw new Error(`stream frame header is not JSON: ${error.message}`);
|
|
48
|
+
}
|
|
49
|
+
if (!header || typeof header.type !== 'string' || typeof header.streamId !== 'string') {
|
|
50
|
+
throw new Error('stream frame header is missing routing fields');
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
version: 1,
|
|
54
|
+
type: header.type,
|
|
55
|
+
streamId: header.streamId,
|
|
56
|
+
payload: frame.subarray(4 + headerLength),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export function packStreamFrameV2(type, streamIndex, payload = Buffer.alloc(0)) {
|
|
60
|
+
let typeCode = type;
|
|
61
|
+
if (type === 'output')
|
|
62
|
+
typeCode = FRAME_TYPE_OUTPUT;
|
|
63
|
+
else if (type === 'input')
|
|
64
|
+
typeCode = FRAME_TYPE_INPUT;
|
|
65
|
+
if (typeCode !== FRAME_TYPE_OUTPUT && typeCode !== FRAME_TYPE_INPUT) {
|
|
66
|
+
throw new TypeError('type must be FRAME_TYPE_OUTPUT (0) or FRAME_TYPE_INPUT (1)');
|
|
67
|
+
}
|
|
68
|
+
if (!Number.isInteger(streamIndex) || streamIndex < 0 || streamIndex > 0xffff) {
|
|
69
|
+
throw new RangeError('streamIndex must be an unsigned 16-bit integer (0-65535)');
|
|
70
|
+
}
|
|
71
|
+
const body = toBuffer(payload);
|
|
72
|
+
const frame = Buffer.allocUnsafe(4 + body.length);
|
|
73
|
+
frame.writeUInt8(FRAME_V2_MAGIC, 0);
|
|
74
|
+
frame.writeUInt8(typeCode, 1);
|
|
75
|
+
frame.writeUInt16BE(streamIndex, 2);
|
|
76
|
+
body.copy(frame, 4);
|
|
77
|
+
return frame;
|
|
78
|
+
}
|
|
79
|
+
export function unpackStreamFrameV2(buffer) {
|
|
80
|
+
const frame = toBuffer(buffer);
|
|
81
|
+
if (frame.length < 4)
|
|
82
|
+
throw new Error('v2 stream frame is truncated');
|
|
83
|
+
const magic = frame.readUInt8(0);
|
|
84
|
+
if (magic !== FRAME_V2_MAGIC) {
|
|
85
|
+
throw new Error('invalid v2 stream frame magic byte');
|
|
86
|
+
}
|
|
87
|
+
const typeCode = frame.readUInt8(1);
|
|
88
|
+
if (typeCode !== FRAME_TYPE_OUTPUT && typeCode !== FRAME_TYPE_INPUT) {
|
|
89
|
+
throw new Error(`unknown v2 stream frame type: ${typeCode}`);
|
|
90
|
+
}
|
|
91
|
+
const streamIndex = frame.readUInt16BE(2);
|
|
92
|
+
return {
|
|
93
|
+
version: 2,
|
|
94
|
+
type: typeCode === FRAME_TYPE_OUTPUT ? 'output' : 'input',
|
|
95
|
+
typeCode,
|
|
96
|
+
streamIndex,
|
|
97
|
+
payload: frame.subarray(4),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
export function unpackStreamFrame(value) {
|
|
101
|
+
const frame = toBuffer(value);
|
|
102
|
+
if (frame.length < 4)
|
|
103
|
+
throw new Error('stream frame is truncated');
|
|
104
|
+
// v1's headerLength is a 32-bit big-endian integer bounded by MAX_HEADER_BYTES (8KB),
|
|
105
|
+
// so its first byte is always 0x00. A first byte of 0xFF unambiguously identifies v2 framing.
|
|
106
|
+
if (frame[0] === FRAME_V2_MAGIC) {
|
|
107
|
+
return unpackStreamFrameV2(frame);
|
|
108
|
+
}
|
|
109
|
+
return unpackStreamFrameV1(frame);
|
|
110
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
export interface RelayInfoResponse {
|
|
2
|
+
ok: boolean;
|
|
3
|
+
version: string;
|
|
4
|
+
protocol: number;
|
|
5
|
+
relayMode: 'local' | 'remote';
|
|
6
|
+
adminConfigured: boolean;
|
|
7
|
+
adminPath: string;
|
|
8
|
+
adminStatusPath: string;
|
|
9
|
+
publicUrl?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface RelayMetadata {
|
|
12
|
+
mode: 'local' | 'remote';
|
|
13
|
+
adminConfigured: boolean;
|
|
14
|
+
adminPath?: string;
|
|
15
|
+
adminStatusPath?: string;
|
|
16
|
+
dashboardPath?: string;
|
|
17
|
+
publicUrl?: string;
|
|
18
|
+
/** The address the relay listens on, and its port. */
|
|
19
|
+
bind?: string;
|
|
20
|
+
port?: number;
|
|
21
|
+
maxClientsPerHost?: number;
|
|
22
|
+
maxHosts?: number;
|
|
23
|
+
maxPendingHandshakes?: number;
|
|
24
|
+
maxBufferedBytesPerClient?: number;
|
|
25
|
+
hostReconnectGraceMs?: number;
|
|
26
|
+
}
|
|
27
|
+
export interface ConnectedClientInfo {
|
|
28
|
+
id: string;
|
|
29
|
+
role: 'controller' | 'viewer';
|
|
30
|
+
/** Absent from a host-scoped response, where every row belongs to one host. */
|
|
31
|
+
hostId?: string;
|
|
32
|
+
/** Operator-only: which paired device this connection authenticated as. */
|
|
33
|
+
deviceId?: string;
|
|
34
|
+
ip?: string | null;
|
|
35
|
+
userAgent?: string;
|
|
36
|
+
connectedAt: string;
|
|
37
|
+
lastPingAt?: string | null;
|
|
38
|
+
bytesReceived?: number;
|
|
39
|
+
bytesSent?: number;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* A device that completed pairing and holds a long-lived token. Served only on
|
|
43
|
+
* `/api/admin/status`, never on `/api/status`.
|
|
44
|
+
*/
|
|
45
|
+
export interface PairedDeviceInfo {
|
|
46
|
+
deviceId: string;
|
|
47
|
+
hostId: string;
|
|
48
|
+
createdAt: string;
|
|
49
|
+
lastSeenAt: string;
|
|
50
|
+
expiresAt: number;
|
|
51
|
+
expiresAtIso: string;
|
|
52
|
+
userAgent?: string | null;
|
|
53
|
+
lastIp?: string | null;
|
|
54
|
+
}
|
|
55
|
+
export type HostStatus = 'online' | 'busy' | 'reconnecting' | 'offline';
|
|
56
|
+
export interface HostInfo {
|
|
57
|
+
id: string;
|
|
58
|
+
hostname: string;
|
|
59
|
+
platform: string;
|
|
60
|
+
arch?: string;
|
|
61
|
+
status: HostStatus;
|
|
62
|
+
connectedAt: string;
|
|
63
|
+
activePtyCount: number;
|
|
64
|
+
/** Distinct devices attached to this host right now, not open sockets. */
|
|
65
|
+
connectedDeviceCount?: number;
|
|
66
|
+
/** Devices holding a valid pairing token for this host, online or not. */
|
|
67
|
+
pairedDeviceCount?: number;
|
|
68
|
+
}
|
|
69
|
+
export interface PtyInfo {
|
|
70
|
+
id: string;
|
|
71
|
+
/** Absent from a host-scoped response, where every row belongs to one host. */
|
|
72
|
+
hostId?: string;
|
|
73
|
+
pid: number | null;
|
|
74
|
+
command: string;
|
|
75
|
+
cols: number;
|
|
76
|
+
rows: number;
|
|
77
|
+
cwd?: string;
|
|
78
|
+
createdAt?: string;
|
|
79
|
+
activeClients: number;
|
|
80
|
+
}
|
|
81
|
+
export interface ThroughputMetrics {
|
|
82
|
+
bytesIn: number;
|
|
83
|
+
bytesOut: number;
|
|
84
|
+
bytesInPerSec: number;
|
|
85
|
+
bytesOutPerSec: number;
|
|
86
|
+
framesIn: number;
|
|
87
|
+
framesOut: number;
|
|
88
|
+
framesInPerSec: number;
|
|
89
|
+
framesOutPerSec: number;
|
|
90
|
+
}
|
|
91
|
+
export interface CpuLoadMetrics {
|
|
92
|
+
load1m: number;
|
|
93
|
+
load5m: number;
|
|
94
|
+
load15m: number;
|
|
95
|
+
cpuPercent: number;
|
|
96
|
+
cores: number;
|
|
97
|
+
}
|
|
98
|
+
export interface MemoryMetrics {
|
|
99
|
+
rssBytes: number;
|
|
100
|
+
heapUsedBytes: number;
|
|
101
|
+
heapTotalBytes: number;
|
|
102
|
+
externalBytes?: number;
|
|
103
|
+
systemTotalBytes?: number;
|
|
104
|
+
systemFreeBytes?: number;
|
|
105
|
+
}
|
|
106
|
+
export interface EventLoopDelayMetrics {
|
|
107
|
+
p50Ms: number;
|
|
108
|
+
p90Ms?: number;
|
|
109
|
+
p99Ms: number;
|
|
110
|
+
maxMs: number;
|
|
111
|
+
meanMs?: number;
|
|
112
|
+
}
|
|
113
|
+
export interface CleanupCounters {
|
|
114
|
+
staleClientsPurged: number;
|
|
115
|
+
closedPtysCleaned: number;
|
|
116
|
+
deadConnectionsClosed: number;
|
|
117
|
+
idleHostsTerminated: number;
|
|
118
|
+
slowClientsDropped?: number;
|
|
119
|
+
lastCleanupAt?: string | null;
|
|
120
|
+
}
|
|
121
|
+
export interface AdminStatusResponse {
|
|
122
|
+
version: string;
|
|
123
|
+
uptimeSeconds: number;
|
|
124
|
+
startTime: string;
|
|
125
|
+
serverTime: string;
|
|
126
|
+
activeControllerId?: string | null;
|
|
127
|
+
activeHostId?: string | null;
|
|
128
|
+
clients: ConnectedClientInfo[];
|
|
129
|
+
hosts: HostInfo[];
|
|
130
|
+
ptys: PtyInfo[];
|
|
131
|
+
/**
|
|
132
|
+
* Distinct people attached, counted by paired device rather than by socket,
|
|
133
|
+
* so several windows of one browser stay one user.
|
|
134
|
+
*/
|
|
135
|
+
activeUserCount?: number;
|
|
136
|
+
/** Operator-only roster of paired devices; absent from `/api/status`. */
|
|
137
|
+
devices?: PairedDeviceInfo[];
|
|
138
|
+
throughput: ThroughputMetrics;
|
|
139
|
+
cpu: CpuLoadMetrics;
|
|
140
|
+
memory: MemoryMetrics;
|
|
141
|
+
eventLoopDelay: EventLoopDelayMetrics;
|
|
142
|
+
cleanup: CleanupCounters;
|
|
143
|
+
protocolVersion: number;
|
|
144
|
+
relay?: RelayMetadata;
|
|
145
|
+
isRemoteRelay?: boolean;
|
|
146
|
+
remoteAdminUrl?: string;
|
|
147
|
+
relayMode?: 'local' | 'remote';
|
|
148
|
+
[key: string]: unknown;
|
|
149
|
+
}
|
|
150
|
+
/** POST /api/pair/start, answered to a workstation's own host token. */
|
|
151
|
+
export interface PairStartResponse {
|
|
152
|
+
ok: true;
|
|
153
|
+
code: string;
|
|
154
|
+
hostId: string;
|
|
155
|
+
publicUrl: string;
|
|
156
|
+
/** Epoch milliseconds. */
|
|
157
|
+
expiresAt: number;
|
|
158
|
+
expiresAtIso: string;
|
|
159
|
+
/** The web app's address with the code filled in. */
|
|
160
|
+
pairUrl: string;
|
|
161
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// `herdr-remote-relay/protocol`: everything that crosses the wire between the
|
|
2
|
+
// host connector, the relay and the browser. The single definition both
|
|
3
|
+
// packages build against; nothing here may be copied into the CLI or the web.
|
|
4
|
+
export * from './frames.js';
|
|
5
|
+
export * from './terminal.js';
|
|
6
|
+
export * from './messages.js';
|
|
7
|
+
export * from './paste.js';
|