hopwire 0.1.8 → 0.2.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 +2 -0
- package/dist/agent/redact.js +38 -0
- package/dist/bin.js +5 -1
- package/dist/cli.js +237 -10
- package/dist/commands/connect.js +23 -7
- package/dist/commands/doctor.js +63 -3
- package/dist/commands/http.js +25 -4
- package/dist/commands/init.js +355 -0
- package/dist/commands/up.js +22 -3
- package/dist/commands/watch.js +54 -9
- package/dist/config-template.js +80 -0
- package/dist/config.js +65 -5
- package/dist/control-plane-client.js +70 -0
- package/dist/inspector/assets/index-CIOfyIbK.js +5 -0
- package/dist/inspector/assets/index-Du4-DCNN.css +2 -0
- package/dist/inspector/index.html +2 -2
- package/dist/inspector-api.js +145 -0
- package/dist/interceptor.js +19 -4
- package/dist/log-follow.js +473 -0
- package/dist/log.js +178 -11
- package/dist/main.js +1 -0
- package/dist/mcp/logs.js +46 -0
- package/dist/mcp/tools.js +136 -3
- package/dist/output.js +67 -1
- package/dist/p2p.js +249 -24
- package/dist/prompt.js +63 -0
- package/dist/relay.js +545 -0
- package/dist/serve.js +62 -2
- package/node_modules/@hopwire/protocol/dist/constants.d.ts +10 -0
- package/node_modules/@hopwire/protocol/dist/constants.js +10 -0
- package/node_modules/@hopwire/protocol/dist/index.d.ts +2 -0
- package/node_modules/@hopwire/protocol/dist/index.js +2 -0
- package/node_modules/@hopwire/protocol/dist/inspector.d.ts +18 -0
- package/node_modules/@hopwire/protocol/dist/log-entries.d.ts +72 -0
- package/node_modules/@hopwire/protocol/dist/log-entries.js +147 -0
- package/node_modules/@hopwire/protocol/dist/relay.d.ts +81 -0
- package/node_modules/@hopwire/protocol/dist/relay.js +102 -0
- package/node_modules/@hopwire/protocol/dist/ws-messages.d.ts +2 -1
- package/package.json +2 -1
- package/dist/inspector/assets/index-D3yNjmSA.js +0 -2
- package/dist/inspector/assets/index-XotF7D1X.css +0 -2
|
@@ -54,6 +54,12 @@ export declare const CLOSE_CODE_IDLE = 4002;
|
|
|
54
54
|
* organisations: POST with a usage token, see USAGE_TOKEN_AUDIENCE.
|
|
55
55
|
*/
|
|
56
56
|
export declare const EDGE_USAGE_PATH = "/_hopwire/usage";
|
|
57
|
+
/**
|
|
58
|
+
* Path on the edge's own host (never on a tunnel or custom host) where the control plane asks the edge to issue,
|
|
59
|
+
* report or drop the certificate of a custom domain (spec phase 14): POST with a certificates token, see
|
|
60
|
+
* CERTIFICATES_TOKEN_AUDIENCE.
|
|
61
|
+
*/
|
|
62
|
+
export declare const EDGE_CERTIFICATES_PATH = "/_hopwire/certificates";
|
|
57
63
|
/**
|
|
58
64
|
* On the answer to the connect request of a tunnel with a plan: today's usage of its organisation, JSON
|
|
59
65
|
* (UsageSnapshot). Not sent for the dev token, which has no plan.
|
|
@@ -82,6 +88,10 @@ export declare const CONNECT_TOKEN_ISSUER = "hopwire-control-plane";
|
|
|
82
88
|
export declare const CONNECT_TOKEN_AUDIENCE = "hopwire-edge-connect";
|
|
83
89
|
/** Audience (`aud`) of a usage token, signed with the same key; the edge takes it only at EDGE_USAGE_PATH. */
|
|
84
90
|
export declare const USAGE_TOKEN_AUDIENCE = "hopwire-edge-usage";
|
|
91
|
+
/** Audience (`aud`) of a certificates token, signed with the same key; the edge takes it only at EDGE_CERTIFICATES_PATH. */
|
|
92
|
+
export declare const CERTIFICATES_TOKEN_AUDIENCE = "hopwire-edge-certs";
|
|
93
|
+
/** Most custom domains one connect token names in its `dom` claim (spec phase 14: 5 on Team). */
|
|
94
|
+
export declare const CUSTOM_DOMAINS_PER_TOKEN = 5;
|
|
85
95
|
/** Seconds a connect token is valid. */
|
|
86
96
|
export declare const CONNECT_TOKEN_TTL_SECONDS = 300;
|
|
87
97
|
/** Names nobody can reserve. */
|
|
@@ -52,6 +52,12 @@ export const CLOSE_CODE_IDLE = 4002;
|
|
|
52
52
|
* organisations: POST with a usage token, see USAGE_TOKEN_AUDIENCE.
|
|
53
53
|
*/
|
|
54
54
|
export const EDGE_USAGE_PATH = '/_hopwire/usage';
|
|
55
|
+
/**
|
|
56
|
+
* Path on the edge's own host (never on a tunnel or custom host) where the control plane asks the edge to issue,
|
|
57
|
+
* report or drop the certificate of a custom domain (spec phase 14): POST with a certificates token, see
|
|
58
|
+
* CERTIFICATES_TOKEN_AUDIENCE.
|
|
59
|
+
*/
|
|
60
|
+
export const EDGE_CERTIFICATES_PATH = '/_hopwire/certificates';
|
|
55
61
|
/**
|
|
56
62
|
* On the answer to the connect request of a tunnel with a plan: today's usage of its organisation, JSON
|
|
57
63
|
* (UsageSnapshot). Not sent for the dev token, which has no plan.
|
|
@@ -80,6 +86,10 @@ export const CONNECT_TOKEN_ISSUER = 'hopwire-control-plane';
|
|
|
80
86
|
export const CONNECT_TOKEN_AUDIENCE = 'hopwire-edge-connect';
|
|
81
87
|
/** Audience (`aud`) of a usage token, signed with the same key; the edge takes it only at EDGE_USAGE_PATH. */
|
|
82
88
|
export const USAGE_TOKEN_AUDIENCE = 'hopwire-edge-usage';
|
|
89
|
+
/** Audience (`aud`) of a certificates token, signed with the same key; the edge takes it only at EDGE_CERTIFICATES_PATH. */
|
|
90
|
+
export const CERTIFICATES_TOKEN_AUDIENCE = 'hopwire-edge-certs';
|
|
91
|
+
/** Most custom domains one connect token names in its `dom` claim (spec phase 14: 5 on Team). */
|
|
92
|
+
export const CUSTOM_DOMAINS_PER_TOKEN = 5;
|
|
83
93
|
/** Seconds a connect token is valid. */
|
|
84
94
|
export const CONNECT_TOKEN_TTL_SECONDS = 300;
|
|
85
95
|
/** Names nobody can reserve. */
|
|
@@ -6,6 +6,8 @@ export * from './headers.js';
|
|
|
6
6
|
export * from './inspector.js';
|
|
7
7
|
export * from './p2p.js';
|
|
8
8
|
export * from './plan.js';
|
|
9
|
+
export * from './relay.js';
|
|
9
10
|
export * from './subdomain.js';
|
|
10
11
|
export * from './websocket.js';
|
|
11
12
|
export * from './ws-messages.js';
|
|
13
|
+
export * from './log-entries.js';
|
|
@@ -6,6 +6,8 @@ export * from './headers.js';
|
|
|
6
6
|
export * from './inspector.js';
|
|
7
7
|
export * from './p2p.js';
|
|
8
8
|
export * from './plan.js';
|
|
9
|
+
export * from './relay.js';
|
|
9
10
|
export * from './subdomain.js';
|
|
10
11
|
export * from './websocket.js';
|
|
11
12
|
export * from './ws-messages.js';
|
|
13
|
+
export * from './log-entries.js';
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type CapturedExchange } from './exchange.js';
|
|
2
2
|
import { type HeaderList } from './headers.js';
|
|
3
|
+
import type { CapturedLogEntries } from './log-entries.js';
|
|
3
4
|
import { type CapturedWsMessages } from './ws-messages.js';
|
|
4
5
|
/** `own` is the log this CLI writes; `remote` is a teammate's log that `hopwire watch` replicates. */
|
|
5
6
|
export type ExchangeOrigin = 'own' | 'remote';
|
|
@@ -23,6 +24,23 @@ export interface WsMessagesPage {
|
|
|
23
24
|
/** True when more blocks follow; ask again with after set to the seq of the last one */
|
|
24
25
|
more: boolean;
|
|
25
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* A block of log entries as the event stream hands it out (event `log_entries`) and `GET /api/log-entries` answers
|
|
29
|
+
* it (phase 15). It has no index: the entries of a log that is not shared live in a core of their own.
|
|
30
|
+
*/
|
|
31
|
+
export interface LogEntriesRecord {
|
|
32
|
+
origin: ExchangeOrigin;
|
|
33
|
+
block: CapturedLogEntries;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Answer of `GET /api/log-entries`, oldest first. With `exchangeId` each block holds only the entries linked to that
|
|
37
|
+
* exchange; otherwise the newest entries up to `limit`, filtered by `since`, `source` and `contains`.
|
|
38
|
+
*/
|
|
39
|
+
export interface LogEntriesPage {
|
|
40
|
+
blocks: LogEntriesRecord[];
|
|
41
|
+
/** True when older entries matched than this answer holds */
|
|
42
|
+
more: boolean;
|
|
43
|
+
}
|
|
26
44
|
export type HopwireMode = 'private' | 'public';
|
|
27
45
|
export interface InspectorService {
|
|
28
46
|
name: string;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Application log entries (phase 15, spec decision 144). A service may name log files; the CLI follows them from the
|
|
3
|
+
* moment it starts and writes their new entries into a traffic log as blocks of their own kind, each a batch of
|
|
4
|
+
* entries of one file, linked to the exchanges that were in flight when an entry was read. Older CLIs skip the kind.
|
|
5
|
+
*/
|
|
6
|
+
/** Text of one entry that is copied into the log; the rest is flagged as truncated. */
|
|
7
|
+
export declare const LOG_ENTRY_CAPTURE_LIMIT_BYTES: number;
|
|
8
|
+
/** Captured text of one file in one run after which its entries are only counted. */
|
|
9
|
+
export declare const LOG_SOURCE_CAPTURE_LIMIT_BYTES: number;
|
|
10
|
+
/** Entries of one file per second after which the rest of that second is only counted. */
|
|
11
|
+
export declare const LOG_ENTRIES_PER_SECOND = 200;
|
|
12
|
+
/** A batch is written this long after its first entry at the latest. */
|
|
13
|
+
export declare const LOG_BATCH_INTERVAL_MS = 250;
|
|
14
|
+
/** A batch is written as soon as this much captured text waits. */
|
|
15
|
+
export declare const LOG_BATCH_MAX_BYTES: number;
|
|
16
|
+
/** A batch is written as soon as this many entries wait. */
|
|
17
|
+
export declare const LOG_BATCH_MAX_ENTRIES = 500;
|
|
18
|
+
/** An entry is closed when its file had no new bytes for this long. */
|
|
19
|
+
export declare const LOG_ENTRY_IDLE_MS = 250;
|
|
20
|
+
/** An entry read this long after an exchange ended is still linked to it (handlers that write after the response). */
|
|
21
|
+
export declare const LOG_LINK_GRACE_MS = 250;
|
|
22
|
+
/** Exchanges one entry is linked to at most. */
|
|
23
|
+
export declare const LOG_ENTRY_MAX_EXCHANGES = 64;
|
|
24
|
+
/** Longest name of a followed file (`source`), in UTF-16 code units. */
|
|
25
|
+
export declare const LOG_SOURCE_NAME_MAX_LENGTH = 255;
|
|
26
|
+
/** The header `capture.logRequestId` adds to every forwarded request (decision 144). */
|
|
27
|
+
export declare const EXCHANGE_ID_HEADER = "X-Hopwire-Exchange-Id";
|
|
28
|
+
/** The `kind` of a block of log entries. An exchange has no `kind`. */
|
|
29
|
+
export declare const LOG_ENTRIES_KIND = "log_entries";
|
|
30
|
+
export interface CapturedLogEntry {
|
|
31
|
+
/** When the CLI read the entry's first line, ms since the epoch */
|
|
32
|
+
readAt: number;
|
|
33
|
+
/** At most LOG_ENTRY_CAPTURE_LIMIT_BYTES of the entry as UTF-8; bytes that are not UTF-8 became U+FFFD */
|
|
34
|
+
text: string;
|
|
35
|
+
/** Full length of the entry in UTF-8 bytes */
|
|
36
|
+
length: number;
|
|
37
|
+
/** True when only the first LOG_ENTRY_CAPTURE_LIMIT_BYTES are captured */
|
|
38
|
+
truncated: boolean;
|
|
39
|
+
/** Ids of the exchanges the entry is linked to: the ones in flight when it was read, or the one its text names */
|
|
40
|
+
exchangeIds: string[];
|
|
41
|
+
/** True when more than one exchange was in flight */
|
|
42
|
+
ambiguous: boolean;
|
|
43
|
+
}
|
|
44
|
+
/** One block of entries of one followed file. */
|
|
45
|
+
export interface CapturedLogEntries {
|
|
46
|
+
kind: typeof LOG_ENTRIES_KIND;
|
|
47
|
+
/** Service the file belongs to */
|
|
48
|
+
serviceName: string;
|
|
49
|
+
/** Name of the file within the service; never its path */
|
|
50
|
+
source: string;
|
|
51
|
+
/** Position of this batch within the source in this run, from 0 */
|
|
52
|
+
seq: number;
|
|
53
|
+
entries: CapturedLogEntry[];
|
|
54
|
+
/** Entries and bytes left out by the limits since the last batch */
|
|
55
|
+
omitted?: {
|
|
56
|
+
entries: number;
|
|
57
|
+
bytes: number;
|
|
58
|
+
};
|
|
59
|
+
/** Decision 140: a teammate's coding agent may read this batch (--share-logs with --allow-agents) */
|
|
60
|
+
agents?: true;
|
|
61
|
+
}
|
|
62
|
+
/** Level of an entry as the common shapes write it (`local.ERROR:`, `"level":"error"`); for display only. */
|
|
63
|
+
export type LogLevel = 'debug' | 'info' | 'warning' | 'error';
|
|
64
|
+
/** The level of an entry, read from its first line; null when it names none. */
|
|
65
|
+
export declare function logEntryLevel(text: string): LogLevel | null;
|
|
66
|
+
/**
|
|
67
|
+
* Whether a block read from a traffic log is a batch of log entries. A teammate's log is written by another machine,
|
|
68
|
+
* so its blocks are checked like exchanges: shape, bounded lengths and text that fits its length and truncation flag.
|
|
69
|
+
*/
|
|
70
|
+
export declare function isCapturedLogEntries(value: unknown): value is CapturedLogEntries;
|
|
71
|
+
/** Bytes of a string as UTF-8, without encoding it; a lone surrogate counts as U+FFFD (3 bytes), as encoding writes it. */
|
|
72
|
+
export declare function utf8Length(text: string): number;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { CAPTURED_ID_MAX_LENGTH, CAPTURED_SERVICE_NAME_MAX_LENGTH } from './exchange.js';
|
|
2
|
+
/**
|
|
3
|
+
* Application log entries (phase 15, spec decision 144). A service may name log files; the CLI follows them from the
|
|
4
|
+
* moment it starts and writes their new entries into a traffic log as blocks of their own kind, each a batch of
|
|
5
|
+
* entries of one file, linked to the exchanges that were in flight when an entry was read. Older CLIs skip the kind.
|
|
6
|
+
*/
|
|
7
|
+
/** Text of one entry that is copied into the log; the rest is flagged as truncated. */
|
|
8
|
+
export const LOG_ENTRY_CAPTURE_LIMIT_BYTES = 16 * 1024;
|
|
9
|
+
/** Captured text of one file in one run after which its entries are only counted. */
|
|
10
|
+
export const LOG_SOURCE_CAPTURE_LIMIT_BYTES = 10 * 1024 * 1024;
|
|
11
|
+
/** Entries of one file per second after which the rest of that second is only counted. */
|
|
12
|
+
export const LOG_ENTRIES_PER_SECOND = 200;
|
|
13
|
+
/** A batch is written this long after its first entry at the latest. */
|
|
14
|
+
export const LOG_BATCH_INTERVAL_MS = 250;
|
|
15
|
+
/** A batch is written as soon as this much captured text waits. */
|
|
16
|
+
export const LOG_BATCH_MAX_BYTES = 64 * 1024;
|
|
17
|
+
/** A batch is written as soon as this many entries wait. */
|
|
18
|
+
export const LOG_BATCH_MAX_ENTRIES = 500;
|
|
19
|
+
/** An entry is closed when its file had no new bytes for this long. */
|
|
20
|
+
export const LOG_ENTRY_IDLE_MS = 250;
|
|
21
|
+
/** An entry read this long after an exchange ended is still linked to it (handlers that write after the response). */
|
|
22
|
+
export const LOG_LINK_GRACE_MS = 250;
|
|
23
|
+
/** Exchanges one entry is linked to at most. */
|
|
24
|
+
export const LOG_ENTRY_MAX_EXCHANGES = 64;
|
|
25
|
+
/** Longest name of a followed file (`source`), in UTF-16 code units. */
|
|
26
|
+
export const LOG_SOURCE_NAME_MAX_LENGTH = 255;
|
|
27
|
+
/** The header `capture.logRequestId` adds to every forwarded request (decision 144). */
|
|
28
|
+
export const EXCHANGE_ID_HEADER = 'X-Hopwire-Exchange-Id';
|
|
29
|
+
/** The `kind` of a block of log entries. An exchange has no `kind`. */
|
|
30
|
+
export const LOG_ENTRIES_KIND = 'log_entries';
|
|
31
|
+
const LEVELS = [
|
|
32
|
+
[/^(emergency|alert|critical|crit|error|err|fatal)$/i, 'error'],
|
|
33
|
+
[/^(warning|warn)$/i, 'warning'],
|
|
34
|
+
[/^(notice|info)$/i, 'info'],
|
|
35
|
+
[/^(debug|trace)$/i, 'debug'],
|
|
36
|
+
];
|
|
37
|
+
/** The level of an entry, read from its first line; null when it names none. */
|
|
38
|
+
export function logEntryLevel(text) {
|
|
39
|
+
const firstLine = text.slice(0, 2048).split('\n', 1)[0] ?? '';
|
|
40
|
+
const word = /\.(EMERGENCY|ALERT|CRITICAL|ERROR|WARNING|NOTICE|INFO|DEBUG):/.exec(firstLine)?.[1] ??
|
|
41
|
+
/"(?:level|level_name|severity)"\s*:\s*"([A-Za-z]+)"/.exec(firstLine)?.[1] ??
|
|
42
|
+
/^\S*\s*\[?\b(FATAL|ERROR|ERR|WARN|WARNING|INFO|DEBUG|TRACE)\b\]?/.exec(firstLine.replace(/^\[[^\]]*\]\s*/, ''))?.[1];
|
|
43
|
+
if (word === undefined) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
for (const [pattern, level] of LEVELS) {
|
|
47
|
+
if (pattern.test(word)) {
|
|
48
|
+
return level;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
/** Text one block may hold: a batch is written once LOG_BATCH_MAX_BYTES wait, so the entry that crossed adds one more limit. */
|
|
54
|
+
const BLOCK_TEXT_MAX_BYTES = LOG_BATCH_MAX_BYTES + LOG_ENTRY_CAPTURE_LIMIT_BYTES;
|
|
55
|
+
/**
|
|
56
|
+
* Whether a block read from a traffic log is a batch of log entries. A teammate's log is written by another machine,
|
|
57
|
+
* so its blocks are checked like exchanges: shape, bounded lengths and text that fits its length and truncation flag.
|
|
58
|
+
*/
|
|
59
|
+
export function isCapturedLogEntries(value) {
|
|
60
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
const { kind, serviceName, source, seq, entries, omitted, agents } = value;
|
|
64
|
+
if (kind !== LOG_ENTRIES_KIND ||
|
|
65
|
+
typeof serviceName !== 'string' ||
|
|
66
|
+
serviceName.length > CAPTURED_SERVICE_NAME_MAX_LENGTH ||
|
|
67
|
+
typeof source !== 'string' ||
|
|
68
|
+
source === '' ||
|
|
69
|
+
source.length > LOG_SOURCE_NAME_MAX_LENGTH ||
|
|
70
|
+
!isCount(seq) ||
|
|
71
|
+
!Array.isArray(entries) ||
|
|
72
|
+
entries.length > LOG_BATCH_MAX_ENTRIES ||
|
|
73
|
+
(agents !== undefined && agents !== true) ||
|
|
74
|
+
(omitted !== undefined && !isOmitted(omitted))) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
let text = 0;
|
|
78
|
+
for (const entry of entries) {
|
|
79
|
+
if (!isCapturedLogEntry(entry)) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
text += utf8Length(entry.text);
|
|
83
|
+
}
|
|
84
|
+
return text <= BLOCK_TEXT_MAX_BYTES;
|
|
85
|
+
}
|
|
86
|
+
function isCapturedLogEntry(value) {
|
|
87
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
const { readAt, text, length, truncated, exchangeIds, ambiguous } = value;
|
|
91
|
+
if (typeof readAt !== 'number' ||
|
|
92
|
+
!Number.isFinite(readAt) ||
|
|
93
|
+
readAt < 0 ||
|
|
94
|
+
typeof text !== 'string' ||
|
|
95
|
+
// A code unit is at least one byte: the cheap check first
|
|
96
|
+
text.length > LOG_ENTRY_CAPTURE_LIMIT_BYTES ||
|
|
97
|
+
!isCount(length) ||
|
|
98
|
+
typeof truncated !== 'boolean' ||
|
|
99
|
+
typeof ambiguous !== 'boolean' ||
|
|
100
|
+
!Array.isArray(exchangeIds) ||
|
|
101
|
+
exchangeIds.length > LOG_ENTRY_MAX_EXCHANGES ||
|
|
102
|
+
!exchangeIds.every((id) => typeof id === 'string' && id !== '' && id.length <= CAPTURED_ID_MAX_LENGTH)) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
const captured = utf8Length(text);
|
|
106
|
+
if (captured > LOG_ENTRY_CAPTURE_LIMIT_BYTES) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
return truncated ? captured < length : captured === length;
|
|
110
|
+
}
|
|
111
|
+
/** Bytes of a string as UTF-8, without encoding it; a lone surrogate counts as U+FFFD (3 bytes), as encoding writes it. */
|
|
112
|
+
export function utf8Length(text) {
|
|
113
|
+
let bytes = 0;
|
|
114
|
+
for (let index = 0; index < text.length; index++) {
|
|
115
|
+
const code = text.charCodeAt(index);
|
|
116
|
+
if (code < 0x80) {
|
|
117
|
+
bytes += 1;
|
|
118
|
+
}
|
|
119
|
+
else if (code < 0x800) {
|
|
120
|
+
bytes += 2;
|
|
121
|
+
}
|
|
122
|
+
else if (code >= 0xd800 && code <= 0xdbff && index + 1 < text.length) {
|
|
123
|
+
const next = text.charCodeAt(index + 1);
|
|
124
|
+
if (next >= 0xdc00 && next <= 0xdfff) {
|
|
125
|
+
bytes += 4;
|
|
126
|
+
index += 1;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
bytes += 3;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
bytes += 3;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return bytes;
|
|
137
|
+
}
|
|
138
|
+
function isOmitted(value) {
|
|
139
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
const { entries, bytes } = value;
|
|
143
|
+
return isCount(entries) && isCount(bytes);
|
|
144
|
+
}
|
|
145
|
+
function isCount(value) {
|
|
146
|
+
return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
|
|
147
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The relay for strict networks (spec phase 12, decision 136): with --relay on the Team plan, private mode goes
|
|
3
|
+
* through the edge when no direct connection comes about. Both CLIs open a WebSocket to the edge; the edge pairs a
|
|
4
|
+
* dial with a listener of the same organisation and pipes the bytes. Over the pipe both CLIs run the same Noise
|
|
5
|
+
* handshake as on the DHT path, so the edge passes on bytes it cannot read.
|
|
6
|
+
*
|
|
7
|
+
* - The sharer keeps a listen socket (RELAY_LISTEN_PATH) open for its topics: the public key of its machine (what
|
|
8
|
+
* `hopwire connect` dials) and the discovery key of its traffic log (what `hopwire watch` dials).
|
|
9
|
+
* - A peer opens a dial socket (RELAY_DIAL_PATH?target=<topic>). The edge tells the listener (a dial message), the
|
|
10
|
+
* listener opens an accept socket (RELAY_ACCEPT_PATH?dial=<id>), and the edge tells the dialer it is paired. From
|
|
11
|
+
* then on every binary message of one side is the next binary message of the other.
|
|
12
|
+
* - Every socket carries a relay token of the control plane (Authorization: Bearer), audience RELAY_TOKEN_AUDIENCE.
|
|
13
|
+
*/
|
|
14
|
+
/** The sharer's socket: `?topic=<hex>` once per topic. */
|
|
15
|
+
export declare const RELAY_LISTEN_PATH = "/_hopwire/relay/listen";
|
|
16
|
+
/** A peer's socket: `?target=<hex>`, the topic it wants to reach. */
|
|
17
|
+
export declare const RELAY_DIAL_PATH = "/_hopwire/relay/dial";
|
|
18
|
+
/** The sharer's answer to one dial: `?dial=<id>`, the id of the dial message. */
|
|
19
|
+
export declare const RELAY_ACCEPT_PATH = "/_hopwire/relay/accept";
|
|
20
|
+
/** Audience (`aud`) of a relay token; the edge takes it only on the relay paths, and nowhere else takes it. */
|
|
21
|
+
export declare const RELAY_TOKEN_AUDIENCE = "hopwire-relay";
|
|
22
|
+
/** Topics one listen socket may name: the machine key and the discovery key of the log, with room to spare. */
|
|
23
|
+
export declare const RELAY_MAX_TOPICS = 4;
|
|
24
|
+
/** How long the edge holds a dial for the listener to accept it. */
|
|
25
|
+
export declare const RELAY_DIAL_TIMEOUT_MS = 10000;
|
|
26
|
+
/** A pipe without a byte in either direction for this long is closed. */
|
|
27
|
+
export declare const RELAY_IDLE_TIMEOUT_MS: number;
|
|
28
|
+
/** Bytes waiting to go out on one side of a pipe above which the edge stops reading the other side. */
|
|
29
|
+
export declare const RELAY_BACKPRESSURE_BYTES: number;
|
|
30
|
+
/** Largest binary message on a relay socket; the CLI sends at most RELAY_CHUNK_BYTES of data in one. */
|
|
31
|
+
export declare const RELAY_MESSAGE_MAX_BYTES: number;
|
|
32
|
+
export declare const RELAY_CHUNK_BYTES: number;
|
|
33
|
+
/** Pipes one organisation may have open at once, unless the edge is configured otherwise (RELAY_PIPES_PER_ORG). */
|
|
34
|
+
export declare const RELAY_PIPES_PER_ORG_DEFAULT = 20;
|
|
35
|
+
/** Relayed bytes per organisation and UTC day on the Team plan, unless configured otherwise (provisional). */
|
|
36
|
+
export declare const RELAY_BYTES_PER_DAY_DEFAULT = 20000000000;
|
|
37
|
+
/** Close codes of relay sockets. */
|
|
38
|
+
export declare const RELAY_CLOSE: {
|
|
39
|
+
/** The token of a listener expired; the CLI comes back with a new one */
|
|
40
|
+
readonly tokenExpired: 4001;
|
|
41
|
+
/** A pipe had no byte for RELAY_IDLE_TIMEOUT_MS */
|
|
42
|
+
readonly idle: 4009;
|
|
43
|
+
/** A limit of the organisation: open pipes or relayed bytes of the day; the reason names which */
|
|
44
|
+
readonly limit: 4029;
|
|
45
|
+
/** Nobody of the organisation listens on the topic of a dial */
|
|
46
|
+
readonly noListener: 4404;
|
|
47
|
+
/** The listener did not accept a dial in time */
|
|
48
|
+
readonly dialTimeout: 4408;
|
|
49
|
+
};
|
|
50
|
+
/** The reasons of a RELAY_CLOSE.limit close. */
|
|
51
|
+
export declare const RELAY_LIMIT_REASONS: readonly ["relay_pipes", "relay_bytes"];
|
|
52
|
+
export type RelayLimitReason = (typeof RELAY_LIMIT_REASONS)[number];
|
|
53
|
+
/**
|
|
54
|
+
* A text message of the edge on a relay socket: `dial` on a listen socket (a peer wants a topic; answer with an
|
|
55
|
+
* accept socket for `dial`), `paired` on a dial socket (from here on the socket is the pipe).
|
|
56
|
+
*/
|
|
57
|
+
export type RelayControlMessage = {
|
|
58
|
+
type: 'dial';
|
|
59
|
+
dial: string;
|
|
60
|
+
topic: string;
|
|
61
|
+
} | {
|
|
62
|
+
type: 'paired';
|
|
63
|
+
};
|
|
64
|
+
export declare function isRelayDialId(value: string): boolean;
|
|
65
|
+
/** A topic as the relay takes it: a public key or discovery key as 64 hexadecimal characters, lower case. */
|
|
66
|
+
export declare function isRelayTopic(value: string): boolean;
|
|
67
|
+
export declare function encodeRelayControl(message: RelayControlMessage): string;
|
|
68
|
+
/** Reads a control message of the edge. Throws ProtocolError for anything else. */
|
|
69
|
+
export declare function decodeRelayControl(text: string): RelayControlMessage;
|
|
70
|
+
/**
|
|
71
|
+
* The first byte of every binary message between two CLIs on a pipe. The edge never looks at it: it passes whole
|
|
72
|
+
* messages on.
|
|
73
|
+
*/
|
|
74
|
+
export declare const RELAY_FRAME: {
|
|
75
|
+
/** The rest is bytes of the encrypted stream */
|
|
76
|
+
readonly data: 0;
|
|
77
|
+
/** The rest is one encrypted datagram next to the stream (the flow control of hopwire connect) */
|
|
78
|
+
readonly datagram: 1;
|
|
79
|
+
/** The sender will send no more stream bytes */
|
|
80
|
+
readonly end: 2;
|
|
81
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { ProtocolError } from './frames.js';
|
|
2
|
+
import { isPublicKeyHex } from './p2p.js';
|
|
3
|
+
/**
|
|
4
|
+
* The relay for strict networks (spec phase 12, decision 136): with --relay on the Team plan, private mode goes
|
|
5
|
+
* through the edge when no direct connection comes about. Both CLIs open a WebSocket to the edge; the edge pairs a
|
|
6
|
+
* dial with a listener of the same organisation and pipes the bytes. Over the pipe both CLIs run the same Noise
|
|
7
|
+
* handshake as on the DHT path, so the edge passes on bytes it cannot read.
|
|
8
|
+
*
|
|
9
|
+
* - The sharer keeps a listen socket (RELAY_LISTEN_PATH) open for its topics: the public key of its machine (what
|
|
10
|
+
* `hopwire connect` dials) and the discovery key of its traffic log (what `hopwire watch` dials).
|
|
11
|
+
* - A peer opens a dial socket (RELAY_DIAL_PATH?target=<topic>). The edge tells the listener (a dial message), the
|
|
12
|
+
* listener opens an accept socket (RELAY_ACCEPT_PATH?dial=<id>), and the edge tells the dialer it is paired. From
|
|
13
|
+
* then on every binary message of one side is the next binary message of the other.
|
|
14
|
+
* - Every socket carries a relay token of the control plane (Authorization: Bearer), audience RELAY_TOKEN_AUDIENCE.
|
|
15
|
+
*/
|
|
16
|
+
/** The sharer's socket: `?topic=<hex>` once per topic. */
|
|
17
|
+
export const RELAY_LISTEN_PATH = '/_hopwire/relay/listen';
|
|
18
|
+
/** A peer's socket: `?target=<hex>`, the topic it wants to reach. */
|
|
19
|
+
export const RELAY_DIAL_PATH = '/_hopwire/relay/dial';
|
|
20
|
+
/** The sharer's answer to one dial: `?dial=<id>`, the id of the dial message. */
|
|
21
|
+
export const RELAY_ACCEPT_PATH = '/_hopwire/relay/accept';
|
|
22
|
+
/** Audience (`aud`) of a relay token; the edge takes it only on the relay paths, and nowhere else takes it. */
|
|
23
|
+
export const RELAY_TOKEN_AUDIENCE = 'hopwire-relay';
|
|
24
|
+
/** Topics one listen socket may name: the machine key and the discovery key of the log, with room to spare. */
|
|
25
|
+
export const RELAY_MAX_TOPICS = 4;
|
|
26
|
+
/** How long the edge holds a dial for the listener to accept it. */
|
|
27
|
+
export const RELAY_DIAL_TIMEOUT_MS = 10_000;
|
|
28
|
+
/** A pipe without a byte in either direction for this long is closed. */
|
|
29
|
+
export const RELAY_IDLE_TIMEOUT_MS = 10 * 60_000;
|
|
30
|
+
/** Bytes waiting to go out on one side of a pipe above which the edge stops reading the other side. */
|
|
31
|
+
export const RELAY_BACKPRESSURE_BYTES = 1024 * 1024;
|
|
32
|
+
/** Largest binary message on a relay socket; the CLI sends at most RELAY_CHUNK_BYTES of data in one. */
|
|
33
|
+
export const RELAY_MESSAGE_MAX_BYTES = 256 * 1024;
|
|
34
|
+
export const RELAY_CHUNK_BYTES = 64 * 1024;
|
|
35
|
+
/** Pipes one organisation may have open at once, unless the edge is configured otherwise (RELAY_PIPES_PER_ORG). */
|
|
36
|
+
export const RELAY_PIPES_PER_ORG_DEFAULT = 20;
|
|
37
|
+
/** Relayed bytes per organisation and UTC day on the Team plan, unless configured otherwise (provisional). */
|
|
38
|
+
export const RELAY_BYTES_PER_DAY_DEFAULT = 20_000_000_000;
|
|
39
|
+
/** Close codes of relay sockets. */
|
|
40
|
+
export const RELAY_CLOSE = {
|
|
41
|
+
/** The token of a listener expired; the CLI comes back with a new one */
|
|
42
|
+
tokenExpired: 4001,
|
|
43
|
+
/** A pipe had no byte for RELAY_IDLE_TIMEOUT_MS */
|
|
44
|
+
idle: 4009,
|
|
45
|
+
/** A limit of the organisation: open pipes or relayed bytes of the day; the reason names which */
|
|
46
|
+
limit: 4029,
|
|
47
|
+
/** Nobody of the organisation listens on the topic of a dial */
|
|
48
|
+
noListener: 4404,
|
|
49
|
+
/** The listener did not accept a dial in time */
|
|
50
|
+
dialTimeout: 4408,
|
|
51
|
+
};
|
|
52
|
+
/** The reasons of a RELAY_CLOSE.limit close. */
|
|
53
|
+
export const RELAY_LIMIT_REASONS = ['relay_pipes', 'relay_bytes'];
|
|
54
|
+
/** What a dial id looks like: 32 hexadecimal characters the edge made up. */
|
|
55
|
+
const DIAL_ID = /^[0-9a-f]{32}$/;
|
|
56
|
+
export function isRelayDialId(value) {
|
|
57
|
+
return DIAL_ID.test(value);
|
|
58
|
+
}
|
|
59
|
+
/** A topic as the relay takes it: a public key or discovery key as 64 hexadecimal characters, lower case. */
|
|
60
|
+
export function isRelayTopic(value) {
|
|
61
|
+
return isPublicKeyHex(value) && value === value.toLowerCase();
|
|
62
|
+
}
|
|
63
|
+
export function encodeRelayControl(message) {
|
|
64
|
+
return JSON.stringify(message);
|
|
65
|
+
}
|
|
66
|
+
/** Reads a control message of the edge. Throws ProtocolError for anything else. */
|
|
67
|
+
export function decodeRelayControl(text) {
|
|
68
|
+
let value;
|
|
69
|
+
try {
|
|
70
|
+
value = JSON.parse(text);
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
throw new ProtocolError('A relay control message is not JSON');
|
|
74
|
+
}
|
|
75
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
76
|
+
throw new ProtocolError('A relay control message is not an object');
|
|
77
|
+
}
|
|
78
|
+
const message = value;
|
|
79
|
+
if (message.type === 'paired') {
|
|
80
|
+
return { type: 'paired' };
|
|
81
|
+
}
|
|
82
|
+
if (message.type === 'dial' &&
|
|
83
|
+
typeof message.dial === 'string' &&
|
|
84
|
+
isRelayDialId(message.dial) &&
|
|
85
|
+
typeof message.topic === 'string' &&
|
|
86
|
+
isRelayTopic(message.topic)) {
|
|
87
|
+
return { type: 'dial', dial: message.dial, topic: message.topic };
|
|
88
|
+
}
|
|
89
|
+
throw new ProtocolError('Unknown relay control message');
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The first byte of every binary message between two CLIs on a pipe. The edge never looks at it: it passes whole
|
|
93
|
+
* messages on.
|
|
94
|
+
*/
|
|
95
|
+
export const RELAY_FRAME = {
|
|
96
|
+
/** The rest is bytes of the encrypted stream */
|
|
97
|
+
data: 0,
|
|
98
|
+
/** The rest is one encrypted datagram next to the stream (the flow control of hopwire connect) */
|
|
99
|
+
datagram: 1,
|
|
100
|
+
/** The sender will send no more stream bytes */
|
|
101
|
+
end: 2,
|
|
102
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type CapturedExchange } from './exchange.js';
|
|
2
|
+
import type { CapturedLogEntries } from './log-entries.js';
|
|
2
3
|
/**
|
|
3
4
|
* Recorded WebSocket messages (phase 11, spec decision 135). The handshake of a WebSocket stays one exchange with
|
|
4
5
|
* status 101; its messages follow in the same traffic log as blocks of their own kind, each a batch of messages of
|
|
@@ -58,7 +59,7 @@ export interface CapturedWsMessages {
|
|
|
58
59
|
close?: CapturedWsClose;
|
|
59
60
|
}
|
|
60
61
|
/** What a block of a traffic log can be. */
|
|
61
|
-
export type LogBlock = CapturedExchange | CapturedWsMessages;
|
|
62
|
+
export type LogBlock = CapturedExchange | CapturedWsMessages | CapturedLogEntries;
|
|
62
63
|
/** Bytes a padded Base64 text decodes to. */
|
|
63
64
|
export declare function base64DecodedLength(value: string): number;
|
|
64
65
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hopwire",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"mcpName": "to.hopwire/hopwire",
|
|
5
5
|
"description": "Record, share and replay the HTTP traffic of your local services: private P2P sharing with teammates, and public HTTPS URLs.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@hopwire/protocol": "0.1.0",
|
|
35
|
+
"@hyperswarm/secret-stream": "^6.9.2",
|
|
35
36
|
"corestore": "^7.16.1",
|
|
36
37
|
"hypercore": "^11.40.5",
|
|
37
38
|
"hyperdht": "^6.37.0",
|