hopwire 0.1.3 → 0.1.5
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/dist/agent/redact.js +28 -1
- package/dist/agent-socket.js +3 -3
- package/dist/cli.js +37 -11
- package/dist/commands/doctor.js +20 -0
- package/dist/commands/http.js +10 -3
- package/dist/commands/mcp.js +1 -1
- package/dist/commands/up.js +10 -3
- package/dist/commands/watch.js +24 -2
- package/dist/config.js +9 -3
- package/dist/control-plane-client.js +65 -7
- package/dist/edge-bridge.js +27 -1
- package/dist/inspector-api.js +30 -0
- package/dist/interceptor.js +1 -0
- package/dist/mcp/body.js +15 -5
- package/dist/mcp/client.js +2 -2
- package/dist/mcp/error-summary.js +278 -0
- package/dist/mcp/tools.js +28 -6
- package/dist/outbound-proxy.js +206 -0
- package/dist/output.js +14 -3
- package/dist/serve.js +20 -6
- package/node_modules/@hopwire/protocol/dist/exchange.d.ts +5 -0
- package/node_modules/@hopwire/protocol/dist/exchange.js +2 -1
- package/package.json +1 -1
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import net from 'node:net';
|
|
3
|
+
import tls from 'node:tls';
|
|
4
|
+
import { HopwireError } from './errors.js';
|
|
5
|
+
/** How long the proxy may take to open the tunnel to the target; the handshake timeout of the caller starts after it. */
|
|
6
|
+
const CONNECT_TIMEOUT_MS = 10_000;
|
|
7
|
+
/** The variables a proxy for a target of this scheme is read from, in this order (lower case first, as curl does). */
|
|
8
|
+
function variablesFor(protocol) {
|
|
9
|
+
if (protocol === 'https:' || protocol === 'wss:') {
|
|
10
|
+
return ['https_proxy', 'HTTPS_PROXY'];
|
|
11
|
+
}
|
|
12
|
+
if (protocol === 'http:' || protocol === 'ws:') {
|
|
13
|
+
return ['http_proxy', 'HTTP_PROXY'];
|
|
14
|
+
}
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
/** The default port of a scheme, for NO_PROXY entries with a port. */
|
|
18
|
+
function portOf(target) {
|
|
19
|
+
if (target.port !== '') {
|
|
20
|
+
return target.port;
|
|
21
|
+
}
|
|
22
|
+
return target.protocol === 'https:' || target.protocol === 'wss:' ? '443' : '80';
|
|
23
|
+
}
|
|
24
|
+
/** The host of a URL without the brackets of an IPv6 address, in lower case. */
|
|
25
|
+
function hostOf(target) {
|
|
26
|
+
return target.hostname.replace(/^\[(.*)\]$/, '$1').toLowerCase();
|
|
27
|
+
}
|
|
28
|
+
function isLoopback(host) {
|
|
29
|
+
return host === 'localhost' || host.endsWith('.localhost') || host === '::1' || /^127\.\d+\.\d+\.\d+$/.test(host);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Whether NO_PROXY exempts the target: `*` exempts everything; an entry exempts its host and every subdomain of it
|
|
33
|
+
* (a leading dot or `*.` changes nothing); an entry with a port only that port. IP addresses match exactly.
|
|
34
|
+
*/
|
|
35
|
+
function exempted(target, noProxy) {
|
|
36
|
+
const host = hostOf(target);
|
|
37
|
+
const port = portOf(target);
|
|
38
|
+
for (const raw of noProxy.split(/[\s,]+/)) {
|
|
39
|
+
let entry = raw.trim().toLowerCase();
|
|
40
|
+
if (entry === '') {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (entry === '*') {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
let entryPort = null;
|
|
47
|
+
const bracketed = /^\[([^\]]+)\](?::(\d+))?$/.exec(entry);
|
|
48
|
+
if (bracketed !== null) {
|
|
49
|
+
entry = bracketed[1] ?? '';
|
|
50
|
+
entryPort = bracketed[2] ?? null;
|
|
51
|
+
}
|
|
52
|
+
else if (net.isIP(entry) === 0) {
|
|
53
|
+
const withPort = /^(.*):(\d+)$/.exec(entry);
|
|
54
|
+
if (withPort !== null) {
|
|
55
|
+
entry = withPort[1] ?? '';
|
|
56
|
+
entryPort = withPort[2] ?? null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (entryPort !== null && entryPort !== port) {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
entry = entry.replace(/^\*?\./, '');
|
|
63
|
+
if (host === entry || (net.isIP(host) === 0 && host.endsWith(`.${entry}`))) {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The proxy for a connection to `target`, or null for a direct connection: without a proxy variable, for a loopback
|
|
71
|
+
* target (a local edge or control plane is never reached through a proxy) and for a target NO_PROXY exempts.
|
|
72
|
+
* A proxy URL that is not http:// is refused with the variable named.
|
|
73
|
+
*/
|
|
74
|
+
export function proxyFor(target, env = process.env) {
|
|
75
|
+
const variables = variablesFor(target.protocol);
|
|
76
|
+
if (variables === null || isLoopback(hostOf(target))) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
let source = null;
|
|
80
|
+
let value = '';
|
|
81
|
+
for (const name of variables) {
|
|
82
|
+
const candidate = env[name]?.trim() ?? '';
|
|
83
|
+
if (candidate !== '') {
|
|
84
|
+
source = name;
|
|
85
|
+
value = candidate;
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (source === null) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
if (exempted(target, env.no_proxy ?? env.NO_PROXY ?? '')) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
let url;
|
|
96
|
+
try {
|
|
97
|
+
// A bare host:port, as some systems set it, means http://
|
|
98
|
+
url = new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(value) ? value : `http://${value}`);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
throw new HopwireError(`${source} is not a proxy URL; set it to http://<host>:<port> or unset it.`);
|
|
102
|
+
}
|
|
103
|
+
if (url.protocol !== 'http:') {
|
|
104
|
+
throw new HopwireError(`${source} names a ${url.protocol.replace(/:$/, '')} proxy; Hopwire supports HTTP proxies only (http://<host>:<port>).`);
|
|
105
|
+
}
|
|
106
|
+
const authorization = url.username === '' && url.password === ''
|
|
107
|
+
? null
|
|
108
|
+
: `Basic ${Buffer.from(`${decodeURIComponent(url.username)}:${decodeURIComponent(url.password)}`, 'utf8').toString('base64')}`;
|
|
109
|
+
url.username = '';
|
|
110
|
+
url.password = '';
|
|
111
|
+
return { url, source, authorization };
|
|
112
|
+
}
|
|
113
|
+
/** The proxy as it may be printed: its origin, never its credentials. */
|
|
114
|
+
export function describeProxy(proxy) {
|
|
115
|
+
return `${proxy.url.origin} (${proxy.source}${proxy.authorization === null ? '' : ', with credentials'})`;
|
|
116
|
+
}
|
|
117
|
+
/** An error of the proxy step, with a code that says it happened there (PROXY_407, PROXY_ECONNREFUSED, PROXY_TIMEOUT). */
|
|
118
|
+
function proxyError(code, message) {
|
|
119
|
+
return Object.assign(new Error(message), { code: `PROXY_${code}` });
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Opens a tunnel to host:port through the proxy (HTTP CONNECT) and, for a secure target, TLS to the target over it;
|
|
123
|
+
* calls back with the socket. The TLS options are those of the caller (servername, ca, rejectUnauthorized), so
|
|
124
|
+
* certificate checks are the same as for a direct connection, NODE_EXTRA_CA_CERTS included.
|
|
125
|
+
*/
|
|
126
|
+
function tunnel(proxy, secure, options, callback) {
|
|
127
|
+
const host = (options.hostname ?? options.host ?? '').replace(/^\[(.*)\]$/, '$1');
|
|
128
|
+
const port = Number(options.port ?? (secure ? 443 : 80));
|
|
129
|
+
const authority = net.isIPv6(host) ? `[${host}]:${port}` : `${host}:${port}`;
|
|
130
|
+
let done = false;
|
|
131
|
+
const finish = (error, socket) => {
|
|
132
|
+
if (done) {
|
|
133
|
+
socket?.destroy();
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
done = true;
|
|
137
|
+
clearTimeout(timer);
|
|
138
|
+
callback(error, socket);
|
|
139
|
+
};
|
|
140
|
+
const request = http.request({
|
|
141
|
+
host: proxy.url.hostname.replace(/^\[(.*)\]$/, '$1'),
|
|
142
|
+
port: Number(proxy.url.port === '' ? 80 : proxy.url.port),
|
|
143
|
+
method: 'CONNECT',
|
|
144
|
+
path: authority,
|
|
145
|
+
headers: {
|
|
146
|
+
host: authority,
|
|
147
|
+
...(proxy.authorization === null ? {} : { 'proxy-authorization': proxy.authorization }),
|
|
148
|
+
},
|
|
149
|
+
agent: false,
|
|
150
|
+
});
|
|
151
|
+
const timer = setTimeout(() => {
|
|
152
|
+
request.destroy();
|
|
153
|
+
finish(proxyError('TIMEOUT', `the proxy did not open a tunnel to ${authority} in time`));
|
|
154
|
+
}, CONNECT_TIMEOUT_MS);
|
|
155
|
+
request.once('connect', (response, socket, head) => {
|
|
156
|
+
const status = response.statusCode ?? 0;
|
|
157
|
+
if (status < 200 || status > 299) {
|
|
158
|
+
socket.destroy();
|
|
159
|
+
finish(proxyError(String(status), `the proxy answered ${status} to CONNECT ${authority}`));
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (head.length > 0) {
|
|
163
|
+
// Bytes of the target before it was asked anything: not a tunnel this client can use
|
|
164
|
+
socket.destroy();
|
|
165
|
+
finish(proxyError('PROTOCOL', `the proxy sent data before the tunnel to ${authority} was used`));
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (!secure) {
|
|
169
|
+
finish(null, socket);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const servername = options.servername ?? (net.isIP(host) === 0 ? host : undefined);
|
|
173
|
+
const secured = tls.connect({
|
|
174
|
+
...options,
|
|
175
|
+
socket,
|
|
176
|
+
servername,
|
|
177
|
+
host: undefined,
|
|
178
|
+
port: undefined,
|
|
179
|
+
path: undefined,
|
|
180
|
+
});
|
|
181
|
+
// Errors of the TLS handshake reach the caller through the socket, as on a direct connection
|
|
182
|
+
finish(null, secured);
|
|
183
|
+
});
|
|
184
|
+
request.once('response', (response) => {
|
|
185
|
+
// A proxy that answers CONNECT with a body instead of switching
|
|
186
|
+
response.resume();
|
|
187
|
+
finish(proxyError(String(response.statusCode ?? 0), `the proxy answered ${response.statusCode ?? 0} to CONNECT ${authority}`));
|
|
188
|
+
});
|
|
189
|
+
request.once('error', (error) => {
|
|
190
|
+
const code = 'code' in error && typeof error.code === 'string' ? error.code : 'UNKNOWN';
|
|
191
|
+
finish(proxyError(code, `the proxy at ${proxy.url.origin} cannot be reached`));
|
|
192
|
+
});
|
|
193
|
+
request.end();
|
|
194
|
+
}
|
|
195
|
+
/** A `createConnection` that opens every connection to `target`'s scheme through the proxy. */
|
|
196
|
+
export function proxiedConnection(proxy, target) {
|
|
197
|
+
const secure = target.protocol === 'https:' || target.protocol === 'wss:';
|
|
198
|
+
return (options, callback) => {
|
|
199
|
+
tunnel(proxy, secure, options, callback);
|
|
200
|
+
return undefined;
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
/** The sentence that names the proxy in a message about a connection that failed, or nothing for a direct one. */
|
|
204
|
+
export function throughProxy(proxy) {
|
|
205
|
+
return proxy === null ? '' : ` through the proxy ${describeProxy(proxy)}`;
|
|
206
|
+
}
|
package/dist/output.js
CHANGED
|
@@ -31,6 +31,8 @@ export function shareNotice(allowed, restrictWith = '--allow <pubkey>') {
|
|
|
31
31
|
: `Only machines of the members of ${printable(allowed.team)} can read it or reach the service (${allowed.keys === 1 ? 'one key' : `${allowed.keys} keys`} now; the list follows the team).`;
|
|
32
32
|
return `Notice The shared log contains headers and bodies as captured, including credentials. It is the whole log of this data directory, earlier runs included. ${who}`;
|
|
33
33
|
}
|
|
34
|
+
/** The line of a tunnel shared with --allow-agents (decision 140): what a teammate's agent may read from now on. */
|
|
35
|
+
export const AGENTS_ALLOWED_LINE = 'Agents Teammates who watch this log can let their coding agents read the exchanges captured from now on (hopwire watch --agent); what an agent reads goes to its model provider. Exchanges captured before, or after a start without it, stay closed to agents.';
|
|
34
36
|
/** One line for a change of the list of a tunnel shared with --team. */
|
|
35
37
|
export function formatTeamEvent(event, organisation) {
|
|
36
38
|
switch (event.type) {
|
|
@@ -43,11 +45,20 @@ export function formatTeamEvent(event, organisation) {
|
|
|
43
45
|
}
|
|
44
46
|
}
|
|
45
47
|
/**
|
|
46
|
-
* The line of a tunnel
|
|
48
|
+
* The line of a tunnel with the agent socket (on by default, decision 139). It says what the socket gives away: a coding agent of this user reads
|
|
47
49
|
* the traffic (and the agent's model provider then receives what it reads) and can replay it to the local service.
|
|
48
50
|
*/
|
|
49
|
-
export function agentLine(socketPath, replayTo) {
|
|
50
|
-
return `Agent ${socketPath} (a coding agent on this machine can read this traffic and replay it to ${replayTo})`;
|
|
51
|
+
export function agentLine(socketPath, replayTo, turnOff) {
|
|
52
|
+
return `Agent ${socketPath} (a coding agent on this machine can read this traffic and replay it to ${replayTo}; ${turnOff} turns it off)`;
|
|
53
|
+
}
|
|
54
|
+
/** The agent line of hopwire watch --agent (decision 140): the teammate decides which exchanges an agent sees. */
|
|
55
|
+
export function watchAgentLine(socketPath, replayTo) {
|
|
56
|
+
const replay = replayTo === null ? 'not replay them (no --replay-target)' : `replay them to ${replayTo}`;
|
|
57
|
+
return `Agent ${socketPath} (a coding agent on this machine can read the exchanges your teammate allowed for agents with --allow-agents, and ${replay}; exchanges captured without it stay closed)`;
|
|
58
|
+
}
|
|
59
|
+
/** The line of a tunnel whose agent socket of the default could not open: it runs without one. */
|
|
60
|
+
export function agentOffLine(problem) {
|
|
61
|
+
return `Agent off: ${problem}`;
|
|
51
62
|
}
|
|
52
63
|
/**
|
|
53
64
|
* One line per exchange: METHOD path status duration. Method and path of a
|
package/dist/serve.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { once } from 'node:events';
|
|
2
2
|
import { fileURLToPath } from 'node:url';
|
|
3
|
-
import { errorCode, listenError } from './errors.js';
|
|
3
|
+
import { HopwireError, errorCode, listenError } from './errors.js';
|
|
4
4
|
import { loadOrCreateIdentity } from './identity.js';
|
|
5
5
|
import { startAgentSocket } from './agent-socket.js';
|
|
6
6
|
import { createInspectorApi } from './inspector-api.js';
|
|
@@ -45,6 +45,7 @@ export async function startServing(options) {
|
|
|
45
45
|
target: service.target,
|
|
46
46
|
serviceName: service.name,
|
|
47
47
|
log,
|
|
48
|
+
allowAgents: options.share && options.allowAgents === true,
|
|
48
49
|
onExchange: options.onExchange,
|
|
49
50
|
onLogError: () => {
|
|
50
51
|
if (!storeClosing) {
|
|
@@ -169,7 +170,10 @@ export async function startServing(options) {
|
|
|
169
170
|
};
|
|
170
171
|
let inspectorUrl = null;
|
|
171
172
|
let agentSocket = null;
|
|
172
|
-
|
|
173
|
+
let agentProblem = null;
|
|
174
|
+
// Windows has no Unix socket: by default its tunnels simply run without one
|
|
175
|
+
const agent = options.agent ?? (process.platform === 'win32' ? false : 'default');
|
|
176
|
+
if (options.inspectPort !== null || agent !== false) {
|
|
173
177
|
proceed();
|
|
174
178
|
// One API for both listeners: the same streams, the same replays
|
|
175
179
|
const api = createInspectorApi({
|
|
@@ -191,11 +195,20 @@ export async function startServing(options) {
|
|
|
191
195
|
supervisor.add('servers', () => inspector.close());
|
|
192
196
|
inspectorUrl = inspector.url;
|
|
193
197
|
}
|
|
194
|
-
if (
|
|
198
|
+
if (agent !== false) {
|
|
195
199
|
proceed();
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
200
|
+
try {
|
|
201
|
+
const socket = await startAgentSocket(api, options.dataDir);
|
|
202
|
+
supervisor.add('servers', () => socket.close());
|
|
203
|
+
agentSocket = socket.path;
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
// Asked for, a socket that cannot open stops the start; by default the tunnel runs without it
|
|
207
|
+
if (agent === true || !(error instanceof HopwireError)) {
|
|
208
|
+
throw error;
|
|
209
|
+
}
|
|
210
|
+
agentProblem = error.message;
|
|
211
|
+
}
|
|
199
212
|
}
|
|
200
213
|
}
|
|
201
214
|
// Stopped at the last step: nothing is reported as running
|
|
@@ -204,6 +217,7 @@ export async function startServing(options) {
|
|
|
204
217
|
services,
|
|
205
218
|
inspectorUrl,
|
|
206
219
|
agentSocket,
|
|
220
|
+
agentProblem,
|
|
207
221
|
logKey: log.key,
|
|
208
222
|
logLength: log.length,
|
|
209
223
|
peerKey,
|
|
@@ -15,6 +15,11 @@ export interface CapturedExchange {
|
|
|
15
15
|
source: ExchangeSource;
|
|
16
16
|
/** Id of the exchange a replay was made from; only set when the source is `replay` */
|
|
17
17
|
replayOf?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Set by a sharer started with --allow-agents (spec decision 140): a teammate's coding agent may read this
|
|
20
|
+
* exchange (hopwire watch --agent). Never false: absent means not allowed. CLIs before 0.1.5 ignore it
|
|
21
|
+
*/
|
|
22
|
+
agents?: true;
|
|
18
23
|
request: {
|
|
19
24
|
method: string;
|
|
20
25
|
/** Path including the query string */
|
|
@@ -31,7 +31,7 @@ export function isCapturedExchange(value) {
|
|
|
31
31
|
if (!isRecord(value)) {
|
|
32
32
|
return false;
|
|
33
33
|
}
|
|
34
|
-
const { id, timestamp, serviceName, source, replayOf, request, response } = value;
|
|
34
|
+
const { id, timestamp, serviceName, source, replayOf, agents, request, response } = value;
|
|
35
35
|
return (typeof id === 'string' &&
|
|
36
36
|
id !== '' &&
|
|
37
37
|
id.length <= CAPTURED_ID_MAX_LENGTH &&
|
|
@@ -41,6 +41,7 @@ export function isCapturedExchange(value) {
|
|
|
41
41
|
serviceName.length <= CAPTURED_SERVICE_NAME_MAX_LENGTH &&
|
|
42
42
|
EXCHANGE_SOURCES.some((known) => known === source) &&
|
|
43
43
|
(replayOf === undefined || (typeof replayOf === 'string' && replayOf.length <= CAPTURED_ID_MAX_LENGTH)) &&
|
|
44
|
+
(agents === undefined || agents === true) &&
|
|
44
45
|
isRecord(request) &&
|
|
45
46
|
typeof request.method === 'string' &&
|
|
46
47
|
request.method.length <= CAPTURED_METHOD_MAX_LENGTH &&
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hopwire",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Record, share and replay the HTTP traffic of your local services: private P2P sharing with teammates, and public HTTPS URLs.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://hopwire.to",
|