Add experimental tracker reconnect for paired devices
During a session authenticated by both device identities, one person proposes a WebTorrent tracker and the other confirms it. Both contribute a nonce over the data channel; HKDF binds both device keys, the session fingerprints and the tracker URL into a non-extractable base key stored with the connection. Forget device, a replaced paired device and Turn off reconnect clear it. Reconnect joins a daily swarm derived from that key. Offers, answers and codeless wakes travel as AES-GCM envelopes bound to the tracker routing fields. The lower device key offers; the other device wakes it when it arrives second. Retries are spaced and capped, one attempt per slot runs under a Web Lock, and fresh device proofs still authenticate the session. Checked against tracker.openwebtorrent.com and tracker.webtorrent.dev with the new opt-in browser check (--tracker=<url>). Signaling v5, midnight rollover, clock skew, CSP and real-network checks remain open. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a5935a8324
commit
3659956967
17 files changed
+930
-28
No files matched your search
@@ -0,0 +1,87 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { derivePairing, discoveryId, openSignal, parsePairing, pairingNonce, sealSignal, signalingKey, signalRole, trackerUrl, CLOCK_TOLERANCE, SIGNAL_LIFETIME, type PairingContext, type SignalRoute } from '../src/lib/reconnect.ts';
|
||||
import { emptyConnection, updateConnection } from '../src/lib/connections.ts';
|
||||
|
||||
const fingerprint = (byte: string) => Array(32).fill(byte).join(' ');
|
||||
const id = (char: string) => char.repeat(20);
|
||||
const context: PairingContext = {
|
||||
proposerNonce: pairingNonce(), acceptorNonce: pairingNonce(),
|
||||
proposerKey: '04' + 'a'.repeat(128), acceptorKey: '04' + 'b'.repeat(128),
|
||||
offerFingerprint: fingerprint('AA'), answerFingerprint: fingerprint('BB'),
|
||||
tracker: 'wss://tracker.example/announce',
|
||||
};
|
||||
const route: SignalRoute = { role: 'offer', infoHash: id('h'), offerId: id('o'), from: id('f'), to: '' };
|
||||
|
||||
test('both peers derive the same pairing; every input separates it', async () => {
|
||||
const [first, second] = await Promise.all([derivePairing(context), derivePairing({ ...context })]);
|
||||
assert.equal(first.extractable, false);
|
||||
assert.equal(await discoveryId(first, 100), await discoveryId(second, 100));
|
||||
const sealed = await sealSignal(await signalingKey(first), route, 'HC1.code');
|
||||
assert.equal(await openSignal(await signalingKey(second), route, sealed), 'HC1.code');
|
||||
for (const changed of [
|
||||
{ proposerNonce: pairingNonce() }, { acceptorNonce: pairingNonce() },
|
||||
{ proposerKey: context.acceptorKey, acceptorKey: context.proposerKey },
|
||||
{ offerFingerprint: fingerprint('CC') }, { answerFingerprint: fingerprint('CC') },
|
||||
{ tracker: 'wss://other.example/announce' },
|
||||
]) {
|
||||
const other = await derivePairing({ ...context, ...changed });
|
||||
assert.notEqual(await discoveryId(other, 100), await discoveryId(first, 100));
|
||||
await assert.rejects(openSignal(await signalingKey(other), route, sealed));
|
||||
}
|
||||
await assert.rejects(derivePairing({ ...context, acceptorKey: context.proposerKey }), /two different devices/);
|
||||
await assert.rejects(derivePairing({ ...context, proposerNonce: 'short' }));
|
||||
});
|
||||
|
||||
test('discovery identifiers are 20 binary bytes and change daily', async () => {
|
||||
const key = await derivePairing(context);
|
||||
const today = await discoveryId(key, 20000);
|
||||
assert.equal(today.length, 20);
|
||||
assert.ok([...today].every(char => char.charCodeAt(0) < 256));
|
||||
assert.notEqual(await discoveryId(key, 20001), today);
|
||||
});
|
||||
|
||||
test('envelopes bind role and every routing field, and expire', async () => {
|
||||
const key = await signalingKey(await derivePairing(context));
|
||||
const now = 1_000_000_000_000;
|
||||
const sealed = await sealSignal(key, route, 'HC1.code', now);
|
||||
assert.equal(signalRole(sealed), 'offer');
|
||||
assert.equal(await openSignal(key, route, sealed, now), 'HC1.code');
|
||||
for (const changed of [{ infoHash: id('x') }, { offerId: id('x') }, { from: id('x') }, { to: id('x') }]) {
|
||||
await assert.rejects(openSignal(key, { ...route, ...changed }, sealed, now));
|
||||
}
|
||||
await assert.rejects(openSignal(key, { ...route, role: 'answer' }, sealed, now));
|
||||
const tampered = sealed.slice(0, -2) + (sealed.endsWith('A') ? 'B' : 'A') + sealed.slice(-1);
|
||||
await assert.rejects(openSignal(key, route, tampered, now));
|
||||
await assert.rejects(openSignal(key, route, sealed, now + SIGNAL_LIFETIME + CLOCK_TOLERANCE + 1), /expired/);
|
||||
await assert.rejects(openSignal(key, route, 'x'.repeat(200_000), now));
|
||||
const wake = await sealSignal(key, { ...route, role: 'wake' }, '', now);
|
||||
assert.equal(await openSignal(key, { ...route, role: 'wake' }, wake, now), '');
|
||||
// A wake never carries a code, and offers always do.
|
||||
await assert.rejects(openSignal(key, route, await sealSignal(key, route, '', now), now));
|
||||
});
|
||||
|
||||
test('tracker addresses must be secure WebSocket URLs', () => {
|
||||
assert.equal(trackerUrl(' wss://tracker.example '), 'wss://tracker.example/');
|
||||
assert.equal(trackerUrl('ws://localhost:8000'), 'ws://localhost:8000/');
|
||||
for (const bad of ['https://tracker.example', 'ws://tracker.example', 'wss://user:pass@tracker.example', 'wss://tracker.example/#x', 'nonsense', 42]) {
|
||||
assert.throws(() => trackerUrl(bad));
|
||||
}
|
||||
});
|
||||
|
||||
test('stored pairings are dropped when the paired device changes', async () => {
|
||||
const peerKey = context.acceptorKey;
|
||||
const pairing = { version: 1 as const, trackers: ['wss://tracker.example/'], key: await derivePairing(context), localKey: context.proposerKey, peerKey };
|
||||
const paired = updateConnection(emptyConnection(), { peerKey });
|
||||
const reconnecting = updateConnection(paired, { reconnect: pairing });
|
||||
assert.equal(reconnecting.reconnect?.trackers[0], 'wss://tracker.example/');
|
||||
assert.equal(updateConnection(reconnecting, { name: 'Laptop' }).reconnect, reconnecting.reconnect);
|
||||
assert.equal(updateConnection(reconnecting, { peerKey }).reconnect, reconnecting.reconnect);
|
||||
assert.equal(updateConnection(reconnecting, { peerKey: '' }).reconnect, null);
|
||||
assert.equal(updateConnection(reconnecting, { peerKey: context.proposerKey }).reconnect, null);
|
||||
assert.equal(updateConnection(reconnecting, { reconnect: null }).reconnect, null);
|
||||
assert.throws(() => updateConnection(emptyConnection(), { reconnect: pairing }));
|
||||
assert.equal(parsePairing(structuredClone(pairing), peerKey)?.key.algorithm.name, 'HKDF');
|
||||
assert.equal(parsePairing({ ...pairing, trackers: ['http://x'] }, peerKey), null);
|
||||
assert.equal(parsePairing({ ...pairing, key: 'secret' }, peerKey), null);
|
||||
});
|
||||
Reference in new issue
Block a user