hopwire 0.8.0 → 0.8.1
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/commands/connect.js +2 -2
- package/dist/commands/doctor.js +34 -11
- package/dist/commands/http.js +4 -1
- package/dist/commands/up.js +4 -1
- package/dist/commands/watch.js +2 -2
- package/dist/output.js +21 -0
- package/dist/p2p.js +96 -23
- package/dist/serve.js +3 -0
- package/package.json +1 -1
package/dist/commands/connect.js
CHANGED
|
@@ -2,7 +2,7 @@ import { once } from 'node:events';
|
|
|
2
2
|
import net, {} from 'node:net';
|
|
3
3
|
import { HopwireError, listenError } from '../errors.js';
|
|
4
4
|
import { loadOrCreateIdentity } from '../identity.js';
|
|
5
|
-
import { MODE_P2P, MODE_P2P_RELAYED, MODE_P2P_TEAM, connectGuide, printable, relayedLine, shortKey } from '../output.js';
|
|
5
|
+
import { MODE_P2P, MODE_P2P_RELAYED, MODE_P2P_TEAM, TEAMMATE_NETWORK_HINT, connectGuide, printable, relayedLine, shortKey, } from '../output.js';
|
|
6
6
|
import { Supervisor, unlessAborted } from '../supervisor.js';
|
|
7
7
|
/**
|
|
8
8
|
* `hopwire connect <peer-key> --port <p>`: a loopback listener whose every TCP connection becomes one end-to-end
|
|
@@ -214,7 +214,7 @@ function connectError(code, options, ownKey, relayProblem = null) {
|
|
|
214
214
|
case 'relay':
|
|
215
215
|
return new HopwireError(`No direct connection to the peer ${peer} came about, and the relay did not connect either: ${relayProblem ?? 'it failed'}.`);
|
|
216
216
|
case 'PEER_NOT_FOUND':
|
|
217
|
-
return new HopwireError(`No peer with the key ${peer} was found; check the key, your network connection and that your teammate's tunnel is running and shared (--share, or p2p.share in hopwire.yaml)
|
|
217
|
+
return new HopwireError(`No peer with the key ${peer} was found; check the key, your network connection and that your teammate's tunnel is running and shared (--share, or p2p.share in hopwire.yaml). ${TEAMMATE_NETWORK_HINT}`);
|
|
218
218
|
case 'unknown_service':
|
|
219
219
|
return new HopwireError(`The peer has no service named "${options.service ?? ''}"; ask your teammate for the name it was started with (--name, or the service name in hopwire.yaml).`);
|
|
220
220
|
case 'service_required':
|
package/dist/commands/doctor.js
CHANGED
|
@@ -15,7 +15,8 @@ import { printable } from '../output.js';
|
|
|
15
15
|
*
|
|
16
16
|
* Without --network it opens no connection at all. With --network it joins the DHT for a moment (the public one:
|
|
17
17
|
* its nodes see the IP address of this machine, as with --share) and says whether this network lets two machines
|
|
18
|
-
* connect directly, which is what private mode needs
|
|
18
|
+
* connect directly, which is what private mode needs: with a teammate on another network (from the NAT of this one)
|
|
19
|
+
* and with one on this same network (by a connection from this machine to itself through the outside address).
|
|
19
20
|
*/
|
|
20
21
|
export async function doctor(options) {
|
|
21
22
|
const { print, dataDir } = options;
|
|
@@ -29,7 +30,9 @@ export async function doctor(options) {
|
|
|
29
30
|
print(`Account ${await account(store)}`);
|
|
30
31
|
print(`Config ${await config(options.cwd)}`);
|
|
31
32
|
print(`Proxy ${proxy(options.env ?? process.env)}`);
|
|
32
|
-
|
|
33
|
+
const probe = options.network ? await network(options) : null;
|
|
34
|
+
print(`Network ${probe === null ? NOT_CHECKED : probe[0]}`);
|
|
35
|
+
print(`Same network ${probe === null ? SAME_NETWORK_NOT_CHECKED : probe[1]}`);
|
|
33
36
|
const edge = options.network ? await relayEdge(store) : null;
|
|
34
37
|
print(`Relay ${options.network ? await relay(edge) : RELAY_NOT_CHECKED}`);
|
|
35
38
|
// Only with --network: the list comes from the control plane, and without it nothing leaves the machine
|
|
@@ -39,7 +42,7 @@ export async function doctor(options) {
|
|
|
39
42
|
}
|
|
40
43
|
print('');
|
|
41
44
|
print(options.network
|
|
42
|
-
? `${edge === null && domains === null ? 'Nothing was sent to us. ' : ''}The network check joined the DHT for a moment;
|
|
45
|
+
? `${edge === null && domains === null ? 'Nothing was sent to us. ' : ''}The network check joined the DHT for a moment and connected this machine to itself under a throwaway key; DHT nodes saw the IP address of this machine.${edge === null ? '' : ' It also asked the edge of your account whether its relay answers, without a token: the edge saw the IP address, nothing else.'}${domains === null ? '' : ' It asked the control plane of your account for its custom domains, with your API token.'}`
|
|
43
46
|
: 'Nothing was sent anywhere: this report was put together on this machine without a network connection.');
|
|
44
47
|
print('It holds no keys, no tokens and no traffic. If you paste it into a bug report, read it first: the paths can name your user.');
|
|
45
48
|
}
|
|
@@ -62,6 +65,7 @@ function proxy(env) {
|
|
|
62
65
|
}
|
|
63
66
|
}
|
|
64
67
|
const RELAY_NOT_CHECKED = 'not checked. hopwire doctor --network also tests whether the relay of your edge (--team --relay, Team plan) can be reached from here, when this machine is logged in';
|
|
68
|
+
const SAME_NETWORK_NOT_CHECKED = 'not checked. hopwire doctor --network also tests whether two machines on this network can connect directly when the network keeps them apart locally';
|
|
65
69
|
const NOT_CHECKED = 'not checked. hopwire doctor --network tests whether this network allows direct P2P connections; for that it joins the public DHT, whose nodes see your IP address';
|
|
66
70
|
/** Whether a file or directory is there, and whether others than its owner can get at it. */
|
|
67
71
|
async function entry(file, expected, missing = 'missing') {
|
|
@@ -119,21 +123,40 @@ async function config(cwd) {
|
|
|
119
123
|
return `${HOPWIRE_YAML_FILE} cannot be used: ${message(error)}`;
|
|
120
124
|
}
|
|
121
125
|
}
|
|
122
|
-
/** What the DHT says about the network this machine is on. */
|
|
126
|
+
/** What the DHT says about the network this machine is on: the Network and the Same network line. */
|
|
123
127
|
async function network(options) {
|
|
124
128
|
// Loaded only for --network: the one module that creates a DHT node
|
|
125
129
|
const { probeNetwork } = await import('../p2p.js');
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
130
|
+
return networkLines(await probeNetwork({ bootstrap: options.bootstrap, signal: options.signal }));
|
|
131
|
+
}
|
|
132
|
+
/** What to do when direct P2P does not come about: the relay on the Team plan, else public mode. */
|
|
133
|
+
const RELAY_INSTEAD = 'On the Team plan both sides add --team --relay to go through the relay of the edge; without it, public mode works from here';
|
|
134
|
+
/** The Network and the Same network line of the report for what `probeNetwork` found. */
|
|
135
|
+
export function networkLines(probe) {
|
|
136
|
+
return [natLine(probe), sameNetworkLine(probe)];
|
|
137
|
+
}
|
|
138
|
+
function natLine({ nat }) {
|
|
139
|
+
if (nat === 'unreachable') {
|
|
140
|
+
return `no DHT node answered from this network within 15 seconds (UDP blocked?); direct P2P with teammates (--share, connect, watch) will not work from this network. ${RELAY_INSTEAD}. A tunnel on this machine alone does not need the DHT`;
|
|
129
141
|
}
|
|
130
|
-
if (
|
|
142
|
+
if (nat === 'open') {
|
|
131
143
|
return 'direct P2P works: this machine can be reached from outside without hole punching';
|
|
132
144
|
}
|
|
133
|
-
if (
|
|
134
|
-
return 'direct P2P should work: this network keeps one outside port per connection, which is what hole punching needs';
|
|
145
|
+
if (nat === 'consistent') {
|
|
146
|
+
return 'direct P2P with teammates on other networks should work: this network keeps one outside port per connection, which is what hole punching needs';
|
|
147
|
+
}
|
|
148
|
+
return `direct P2P will often fail from here: this network picks a new outside port for every destination (a symmetric NAT). It still works when the teammate is on a network that keeps its port; two networks of this kind cannot connect directly. ${RELAY_INSTEAD}`;
|
|
149
|
+
}
|
|
150
|
+
function sameNetworkLine({ nat, sameNetwork }) {
|
|
151
|
+
if (sameNetwork === 'works') {
|
|
152
|
+
return 'a teammate on this same network can connect directly: a connection to the outside address of this network comes back in';
|
|
153
|
+
}
|
|
154
|
+
if (sameNetwork === 'fails') {
|
|
155
|
+
return `direct P2P with a teammate on this same network will fail unless the network lets the two machines reach each other locally; guest and office Wi-Fi often do not, and this network does not send a connection to its own outside address back in. ${RELAY_INSTEAD}`;
|
|
135
156
|
}
|
|
136
|
-
return
|
|
157
|
+
return nat === 'unreachable'
|
|
158
|
+
? 'not tested: the DHT could not be reached'
|
|
159
|
+
: 'not tested: the test connection to this machine itself did not get as far as a hole punch; run hopwire doctor --network again';
|
|
137
160
|
}
|
|
138
161
|
/** The edge whose relay is checked: the one the logged-in account is bound to; null when there is none. */
|
|
139
162
|
async function relayEdge(store) {
|
package/dist/commands/http.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { formatDrift } from '../shapes.js';
|
|
2
|
-
import { MODE_LOCAL, MODE_P2P, MODE_P2P_TEAM, relayLine, MODE_PUBLIC, MODE_PUBLIC_P2P, MODE_PUBLIC_P2P_TEAM, AGENTS_ALLOWED_LINE, agentLine, agentOffLine, exchangeSink, formatPeerEvent, formatLimitReached, formatLogNotice, logLines, formatMeteredUsage, formatRelayEvent, formatTeamEvent, formatUsage, shareNotice, tunnelGuide, mainPublicUrl, publicUrlText, } from '../output.js';
|
|
2
|
+
import { MODE_LOCAL, MODE_P2P, MODE_P2P_TEAM, dhtUnreachableLine, relayLine, MODE_PUBLIC, MODE_PUBLIC_P2P, MODE_PUBLIC_P2P_TEAM, AGENTS_ALLOWED_LINE, agentLine, agentOffLine, exchangeSink, formatPeerEvent, formatLimitReached, formatLogNotice, logLines, formatMeteredUsage, formatRelayEvent, formatTeamEvent, formatUsage, shareNotice, tunnelGuide, mainPublicUrl, publicUrlText, } from '../output.js';
|
|
3
3
|
import { startServing } from '../serve.js';
|
|
4
4
|
export { INSPECTOR_FILES } from '../serve.js';
|
|
5
5
|
/**
|
|
@@ -77,6 +77,9 @@ export async function startHttpTunnel(options) {
|
|
|
77
77
|
if (peerKey !== null) {
|
|
78
78
|
options.print(`Peer key ${peerKey}`);
|
|
79
79
|
options.print(shareNotice(access));
|
|
80
|
+
if (serving.dhtReached === false) {
|
|
81
|
+
options.print(dhtUnreachableLine({ team: serving.team !== null, relay: serving.relay !== null }));
|
|
82
|
+
}
|
|
80
83
|
if (serving.relay !== null) {
|
|
81
84
|
options.print(relayLine(serving.relay.edgeUrl, serving.relay.problem));
|
|
82
85
|
}
|
package/dist/commands/up.js
CHANGED
|
@@ -3,7 +3,7 @@ import { randomBytes } from 'node:crypto';
|
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { LOCAL_ENTRY_DEFAULT_PORT, RANDOM_SUBDOMAIN_BYTES, randomSubdomain } from '@hopwire/protocol';
|
|
5
5
|
import { HopwireError } from '../errors.js';
|
|
6
|
-
import { MODE_LOCAL, MODE_P2P, MODE_P2P_TEAM, relayLine, MODE_PUBLIC, MODE_PUBLIC_P2P, MODE_PUBLIC_P2P_TEAM, AGENTS_ALLOWED_LINE, agentLine, agentOffLine, exchangeSink, formatPeerEvent, formatLogNotice, logLines, formatRelayEvent, formatTeamEvent, formatUsage, shareNotice, tunnelGuide, mainPublicUrl, publicUrlText, } from '../output.js';
|
|
6
|
+
import { MODE_LOCAL, MODE_P2P, MODE_P2P_TEAM, dhtUnreachableLine, relayLine, MODE_PUBLIC, MODE_PUBLIC_P2P, MODE_PUBLIC_P2P_TEAM, AGENTS_ALLOWED_LINE, agentLine, agentOffLine, exchangeSink, formatPeerEvent, formatLogNotice, logLines, formatRelayEvent, formatTeamEvent, formatUsage, shareNotice, tunnelGuide, mainPublicUrl, publicUrlText, } from '../output.js';
|
|
7
7
|
import { startServing } from '../serve.js';
|
|
8
8
|
import { formatBridgeEvent } from './http.js';
|
|
9
9
|
/**
|
|
@@ -100,6 +100,9 @@ export async function startUp(options) {
|
|
|
100
100
|
if (serving.peerKey !== null) {
|
|
101
101
|
options.print(`Peer key ${serving.peerKey}`);
|
|
102
102
|
options.print(shareNotice(access, 'p2p.allow in hopwire.yaml'));
|
|
103
|
+
if (serving.dhtReached === false) {
|
|
104
|
+
options.print(dhtUnreachableLine({ team: serving.team !== null, relay: serving.relay !== null, yaml: true }));
|
|
105
|
+
}
|
|
103
106
|
if (serving.relay !== null) {
|
|
104
107
|
options.print(relayLine(serving.relay.edgeUrl, serving.relay.problem));
|
|
105
108
|
}
|
package/dist/commands/watch.js
CHANGED
|
@@ -7,7 +7,7 @@ import { startInspectorServer } from '../inspector-server.js';
|
|
|
7
7
|
import { createInterceptor } from '../interceptor.js';
|
|
8
8
|
import { InFlightExchanges, followLogFile } from '../log-follow.js';
|
|
9
9
|
import { REMOTE_LOG_BUDGET_BYTES } from '../log.js';
|
|
10
|
-
import { MODE_P2P, MODE_P2P_RELAYED, MODE_P2P_TEAM, exchangeSink, formatLogNotice, logLines, printable, relayedLine, shortKey, watchAgentLine, watchGuide, } from '../output.js';
|
|
10
|
+
import { MODE_P2P, MODE_P2P_RELAYED, MODE_P2P_TEAM, TEAMMATE_NETWORK_HINT, exchangeSink, formatLogNotice, logLines, printable, relayedLine, shortKey, watchAgentLine, watchGuide, } from '../output.js';
|
|
11
11
|
import { INSPECTOR_FILES } from '../serve.js';
|
|
12
12
|
import { Supervisor, unlessAborted } from '../supervisor.js';
|
|
13
13
|
/**
|
|
@@ -213,7 +213,7 @@ function joinError(code, logKey, ownKey, team, relayProblem = null) {
|
|
|
213
213
|
: `The sharer of the log ${shortKey(logKey)} was found but no direct connection came about, and the relay did not connect either: ${relayProblem}.`);
|
|
214
214
|
}
|
|
215
215
|
if (code === 'not_found') {
|
|
216
|
-
return new HopwireError(`Nobody is sharing the log ${shortKey(logKey)} right now; check the key, your network connection and that your teammate's tunnel is running and shared (--share, or p2p.share in hopwire.yaml)
|
|
216
|
+
return new HopwireError(`Nobody is sharing the log ${shortKey(logKey)} right now; check the key, your network connection and that your teammate's tunnel is running and shared (--share, or p2p.share in hopwire.yaml). ${TEAMMATE_NETWORK_HINT}`);
|
|
217
217
|
}
|
|
218
218
|
return new HopwireError(team
|
|
219
219
|
? `The sharer of the log ${shortKey(logKey)} was found but no connection came about: either no direct connection was possible (on the Team plan --relay goes through the relay of the edge then, when the sharer uses it too), or it shares with another team than the one this machine is logged in to, or it runs with --allow, which then has to list this machine's key ${ownKey}.`
|
package/dist/output.js
CHANGED
|
@@ -21,6 +21,22 @@ export function relayLine(edgeUrl, problem) {
|
|
|
21
21
|
? `Relay on: teammates without a direct connection reach this machine through the edge at ${edgeUrl.origin}; the edge sees addresses, your organisation, keys and byte counts, never content`
|
|
22
22
|
: `Relay not connected yet (${problem}); trying again, direct P2P works meanwhile`;
|
|
23
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* The line of a sharer whose announcement no DHT node answered: the network drops the DHT's UDP, so teammates will not
|
|
26
|
+
* find this machine over P2P. What to do depends on what it already runs with.
|
|
27
|
+
*/
|
|
28
|
+
export function dhtUnreachableLine(options) {
|
|
29
|
+
const found = 'Network no DHT node answered from this network (UDP blocked?), so teammates will not find this machine over P2P';
|
|
30
|
+
if (options.relay) {
|
|
31
|
+
return `${found}; they reach it through the relay`;
|
|
32
|
+
}
|
|
33
|
+
if (options.yaml === true) {
|
|
34
|
+
const set = options.team ? 'p2p.relay: true' : 'p2p.team: true and p2p.relay: true';
|
|
35
|
+
return `${found}. On the Team plan set ${set} in hopwire.yaml; without it, make the service public (public: true). hopwire doctor --network says more`;
|
|
36
|
+
}
|
|
37
|
+
const restart = options.team ? 'restart with --relay' : 'restart with --team --relay';
|
|
38
|
+
return `${found}. On the Team plan ${restart}; without it, use public mode (--public). hopwire doctor --network says more`;
|
|
39
|
+
}
|
|
24
40
|
/** The line of watch or connect when the connection came through the relay. */
|
|
25
41
|
export function relayedLine(edgeUrl) {
|
|
26
42
|
return `Relay through the edge at ${edgeUrl.origin}: no direct connection came about; the edge sees addresses, your organisation, keys and byte counts, never content`;
|
|
@@ -144,6 +160,11 @@ export function printable(text) {
|
|
|
144
160
|
return text.replace(/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/gu, (character) => `\\u{${(character.codePointAt(0) ?? 0).toString(16).padStart(4, '0')}}`);
|
|
145
161
|
}
|
|
146
162
|
/** Start and end of a key, enough to recognise it in a sentence. */
|
|
163
|
+
/**
|
|
164
|
+
* Appended when a teammate's key was not found on the DHT: the sharer's network may keep it off the DHT (UDP blocked),
|
|
165
|
+
* which only doctor on the sharing machine can tell, and the relay is the way then.
|
|
166
|
+
*/
|
|
167
|
+
export const TEAMMATE_NETWORK_HINT = 'If it is, their network may block the DHT: ask your teammate to run hopwire doctor --network on the sharing machine. On the Team plan both sides then use --team --relay.';
|
|
147
168
|
export function shortKey(key) {
|
|
148
169
|
return `${key.slice(0, 8)}...${key.slice(-4)}`;
|
|
149
170
|
}
|
package/dist/p2p.js
CHANGED
|
@@ -316,7 +316,14 @@ export async function startSharing(options) {
|
|
|
316
316
|
await close().catch(() => { });
|
|
317
317
|
throw error;
|
|
318
318
|
}
|
|
319
|
-
return { peerKey: identity.publicKey.toString('hex'), relayProblem, close };
|
|
319
|
+
return { peerKey: identity.publicKey.toString('hex'), relayProblem, dhtReached: dhtAnswered(dht), close };
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Whether any DHT node ever answered this node. HyperDHT considers itself bootstrapped and its server listening even
|
|
323
|
+
* when no node answered (a network that drops UDP), so this is the one sign that the DHT was reached.
|
|
324
|
+
*/
|
|
325
|
+
export function dhtAnswered(dht) {
|
|
326
|
+
return dht.stats.requests.responses > 0;
|
|
320
327
|
}
|
|
321
328
|
/**
|
|
322
329
|
* Keeps a teammate's stream from staying open without doing anything: it is destroyed when it has no request in
|
|
@@ -386,43 +393,109 @@ export class PeerConnectError extends Error {
|
|
|
386
393
|
const SHARER_ANSWERS = ['unknown_service', 'service_required', 'bad_preface', 'busy'];
|
|
387
394
|
/** How long `probeNetwork` waits for the DHT. */
|
|
388
395
|
const PROBE_TIMEOUT_MS = 15_000;
|
|
396
|
+
/** How long `probeNetwork` waits for the connection to this machine itself; a failed hole punch takes about 11 seconds. */
|
|
397
|
+
const SAME_NETWORK_TIMEOUT_MS = 20_000;
|
|
389
398
|
/**
|
|
390
|
-
* What the DHT says about the network of this machine, for `hopwire doctor --network
|
|
391
|
-
*
|
|
392
|
-
*
|
|
393
|
-
*
|
|
399
|
+
* What the DHT says about the network of this machine, for `hopwire doctor --network`.
|
|
400
|
+
*
|
|
401
|
+
* `nat`: `open` (reachable from outside as it is), `consistent` (one outside port, so hole punching has something to
|
|
402
|
+
* aim at), `randomized` (a new outside port for every destination, a symmetric NAT) or `unreachable` (no answer from
|
|
403
|
+
* the DHT in time).
|
|
404
|
+
*
|
|
405
|
+
* `sameNetwork`: a second node on this machine connects to the first one with the LAN shortcut off, so the
|
|
406
|
+
* connection has to go out through the outside address of this network and come back in, as it does between two
|
|
407
|
+
* machines on one network that cannot reach each other locally (guest and office Wi-Fi often keep devices apart).
|
|
408
|
+
* The first node announces a throwaway key for that; both nodes are destroyed before this resolves.
|
|
394
409
|
*/
|
|
395
410
|
export async function probeNetwork(options) {
|
|
396
411
|
options.signal?.throwIfAborted();
|
|
397
412
|
const dht = new HyperDHT({ bootstrap: options.bootstrap });
|
|
398
|
-
let
|
|
413
|
+
let client;
|
|
399
414
|
let stop;
|
|
415
|
+
const aborted = new Promise((_, reject) => {
|
|
416
|
+
stop = () => reject(options.signal?.reason);
|
|
417
|
+
options.signal?.addEventListener('abort', stop, { once: true });
|
|
418
|
+
});
|
|
419
|
+
try {
|
|
420
|
+
// Bootstrapped is not reached: HyperDHT also counts itself bootstrapped when no node ever answered (UDP dropped)
|
|
421
|
+
if (!(await Promise.race([within(dht.fullyBootstrapped(), PROBE_TIMEOUT_MS), aborted])) || !dhtAnswered(dht)) {
|
|
422
|
+
return { nat: 'unreachable', sameNetwork: 'unknown' };
|
|
423
|
+
}
|
|
424
|
+
const nat = !dht.firewalled ? 'open' : dht.randomized ? 'randomized' : 'consistent';
|
|
425
|
+
client = new HyperDHT({ bootstrap: options.bootstrap });
|
|
426
|
+
const sameNetwork = await Promise.race([connectToSelf(dht, client), aborted]);
|
|
427
|
+
return { nat, sameNetwork };
|
|
428
|
+
}
|
|
429
|
+
finally {
|
|
430
|
+
if (stop !== undefined) {
|
|
431
|
+
options.signal?.removeEventListener('abort', stop);
|
|
432
|
+
}
|
|
433
|
+
await Promise.all([
|
|
434
|
+
dht.destroy({ force: true }).catch(() => { }),
|
|
435
|
+
client?.destroy({ force: true }).catch(() => { }),
|
|
436
|
+
]);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
/** Resolves true once `work` resolves, false when `ms` passed first or it rejected. */
|
|
440
|
+
async function within(work, ms) {
|
|
441
|
+
let timer;
|
|
400
442
|
try {
|
|
401
|
-
|
|
402
|
-
|
|
443
|
+
return await Promise.race([
|
|
444
|
+
work.then(() => true, () => false),
|
|
403
445
|
new Promise((resolve) => {
|
|
404
|
-
timer = setTimeout(() => resolve(false),
|
|
405
|
-
}),
|
|
406
|
-
new Promise((_, reject) => {
|
|
407
|
-
stop = () => reject(options.signal?.reason);
|
|
408
|
-
options.signal?.addEventListener('abort', stop, { once: true });
|
|
446
|
+
timer = setTimeout(() => resolve(false), ms);
|
|
409
447
|
}),
|
|
410
448
|
]);
|
|
411
|
-
if (!joined) {
|
|
412
|
-
return 'unreachable';
|
|
413
|
-
}
|
|
414
|
-
if (!dht.firewalled) {
|
|
415
|
-
return 'open';
|
|
416
|
-
}
|
|
417
|
-
return dht.randomized ? 'randomized' : 'consistent';
|
|
418
449
|
}
|
|
419
450
|
finally {
|
|
420
451
|
clearTimeout(timer);
|
|
421
|
-
|
|
422
|
-
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
/** HyperDHT errors that mean the hole punch itself did not come about, as opposed to a lookup that found nobody. */
|
|
455
|
+
const HOLEPUNCH_FAILURES = new Set([
|
|
456
|
+
'HOLEPUNCH_ABORTED',
|
|
457
|
+
'HOLEPUNCH_PROBE_TIMEOUT',
|
|
458
|
+
'HOLEPUNCH_DOUBLE_RANDOMIZED_NATS',
|
|
459
|
+
'CANNOT_HOLEPUNCH',
|
|
460
|
+
'REMOTE_NOT_HOLEPUNCHING',
|
|
461
|
+
'REMOTE_NOT_HOLEPUNCHABLE',
|
|
462
|
+
'HANDSHAKE_UNFINISHED',
|
|
463
|
+
'PEER_CONNECTION_FAILED',
|
|
464
|
+
]);
|
|
465
|
+
/** `server` listens on a throwaway key, `client` connects to it without the LAN shortcut. */
|
|
466
|
+
async function connectToSelf(server, client) {
|
|
467
|
+
const listener = server.createServer({}, (stream) => {
|
|
468
|
+
stream.on('error', () => { });
|
|
469
|
+
stream.end();
|
|
470
|
+
});
|
|
471
|
+
const keyPair = HyperDHT.keyPair();
|
|
472
|
+
let timer;
|
|
473
|
+
let stream;
|
|
474
|
+
try {
|
|
475
|
+
const ready = await within(Promise.all([listener.listen(keyPair), client.fullyBootstrapped()]), PROBE_TIMEOUT_MS);
|
|
476
|
+
if (!ready) {
|
|
477
|
+
return 'unknown';
|
|
423
478
|
}
|
|
424
|
-
|
|
479
|
+
const connecting = client.connect(keyPair.publicKey, { localConnection: false });
|
|
480
|
+
stream = connecting;
|
|
481
|
+
return await new Promise((resolve) => {
|
|
482
|
+
connecting.once('open', () => resolve('works'));
|
|
483
|
+
connecting.once('error', (error) => resolve(sameNetworkFailure(error)));
|
|
484
|
+
// A hole punch that neither opens nor fails in time did not come about
|
|
485
|
+
timer = setTimeout(() => resolve('fails'), SAME_NETWORK_TIMEOUT_MS);
|
|
486
|
+
});
|
|
425
487
|
}
|
|
488
|
+
finally {
|
|
489
|
+
clearTimeout(timer);
|
|
490
|
+
stream?.on('error', () => { });
|
|
491
|
+
stream?.destroy();
|
|
492
|
+
await listener.close().catch(() => { });
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
/** What a failed connection to this machine itself says about the network. */
|
|
496
|
+
export function sameNetworkFailure(error) {
|
|
497
|
+
const code = typeof error === 'object' && error !== null && 'code' in error ? error.code : undefined;
|
|
498
|
+
return typeof code === 'string' && HOLEPUNCH_FAILURES.has(code) ? 'fails' : 'unknown';
|
|
426
499
|
}
|
|
427
500
|
/**
|
|
428
501
|
* The connecting side of p2pConnect: every call of `open` is one stream to the peer key, end-to-end encrypted. Direct
|
package/dist/serve.js
CHANGED
|
@@ -144,6 +144,7 @@ export async function startServing(options) {
|
|
|
144
144
|
let peerKey = null;
|
|
145
145
|
let team = null;
|
|
146
146
|
let relay = null;
|
|
147
|
+
let dhtReached = null;
|
|
147
148
|
if (options.share) {
|
|
148
149
|
proceed();
|
|
149
150
|
const identity = await loadOrCreateIdentity(options.dataDir);
|
|
@@ -185,6 +186,7 @@ export async function startServing(options) {
|
|
|
185
186
|
});
|
|
186
187
|
supervisor.add('p2p', () => sharing.close());
|
|
187
188
|
peerKey = sharing.peerKey;
|
|
189
|
+
dhtReached = sharing.dhtReached;
|
|
188
190
|
if (options.relay !== undefined) {
|
|
189
191
|
relay = { edgeUrl: options.relay.edgeUrl, problem: sharing.relayProblem };
|
|
190
192
|
}
|
|
@@ -309,6 +311,7 @@ export async function startServing(options) {
|
|
|
309
311
|
peerKey,
|
|
310
312
|
team,
|
|
311
313
|
relay,
|
|
314
|
+
dhtReached,
|
|
312
315
|
ended: endings.length > 0 ? Promise.race(endings) : new Promise(() => { }),
|
|
313
316
|
close: supervisor.close,
|
|
314
317
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hopwire",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.1",
|
|
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",
|