diff --git a/e2e.spec.ts b/e2e.spec.ts index f270111..fb21cea 100644 --- a/e2e.spec.ts +++ b/e2e.spec.ts @@ -31,10 +31,11 @@ const SIZE = 5_000_000; const LARGE_SIZE = 2.5 * 2 ** 30; // Big enough that the transfer still runs while a test cancels or disconnects. const RUNNING_SIZE = 100_000_000; -// WINDOW, PROBE_TIMEOUT and PROOF_TIMEOUT in src/core.ts. +// WINDOW, PROBE_TIMEOUT, PROOF_TIMEOUT, and CONNECT_TIMEOUT in src/core.ts. const WINDOW = 8 * 2 ** 20; const PROBE_TIMEOUT = 2000; const PROOF_TIMEOUT = 10_000; +const CONNECT_TIMEOUT = 30_000; // Enough for a transfer whose receiver holds back its reads to pause at the window. const HELD_SIZE = 3 * WINDOW; // What the fake STUN server reports as public address, reserved for documentation. @@ -494,6 +495,24 @@ for (const name of Object.keys(BROWSERS)) { expect((await core())[1]).toContain('wss://tracker.webtorrent.dev'); }); + test(`servers: ${name} notes while no STUN server would be used`, async () => { + const page = await open(name); + await expect(page.locator('#no-stun')).toHaveCount(0); + await page.uncheck('#stun'); + await expect(page.locator('#no-stun')).toHaveText( + 'STUN is off: only devices on the same network or VPN can connect.', + ); + await page.check('#stun'); + await page.click('#servers summary'); + // A line this browser rejects is no server either. + await page.fill('#stun-servers', 'stun:stun.example:99999'); + await expect(page.locator('#no-stun')).toHaveText( + 'No STUN server under Servers: only devices on the same network or VPN can connect.', + ); + await page.click('#reset-servers'); + await expect(page.locator('#no-stun')).toHaveCount(0); + }); + // Only the STUN server in use learns the address. 192.0.2.1 is reserved for documentation and never // answers, a connection with it would wait ICE_TIMEOUT in src/core.ts. The probe passes it over // once, later connections go straight to the one that answered, until it stops. @@ -1170,6 +1189,63 @@ for (const name of Object.keys(BROWSERS)) { }); }); + // Between networks a side without a public address connects nobody. Chromium never fails such a + // connection, the timeouts end it. The sender has one from the fake STUN server, the receiver has + // STUN off. + test(`hint: ${name} manual exchange names the side without a public address`, async () => { + const [sender, receiver] = await apart(name); + + await withLogs(sender, receiver, async () => { + await sender.setInputFiles('input[type=file]', await payload(randomBytes(1000))); + await MODES.manual!(sender, receiver); + // The timeout starts once the answer is set. + await expect + .poll(() => + sender.evaluate(() => + (window as unknown as { peers: RTCPeerConnection[] }).peers.some((pc) => pc.signalingState === 'stable'), + ), + ) + .toBe(true); + await sender.clock.fastForward(CONNECT_TIMEOUT); + await expect(sender.locator('#send-error')).toHaveText(`could not connect: the receiver ${NO_PUBLIC}`); + await receiver.clock.fastForward('06:00'); + await expect(receiver.locator('#receive-error')).toHaveText(`could not connect: this side ${NO_PUBLIC}`); + await expectClosed(sender); + await expectClosed(receiver); + }); + }); + + // Each failed connection says why in the log, the expiry repeats the last reason. + test(`hint: ${name} tracker signaling names the side without a public address`, async () => { + const tracker = await ownTracker(); + const [sender, receiver] = await apart(name); + const fastForward = (time: number | string) => + Promise.all([sender, receiver].map((page) => page.clock.fastForward(time))); + + await withLogs(sender, receiver, async () => { + for (const page of [sender, receiver]) await page.fill('#trackers', tracker); + await sender.setInputFiles('input[type=file]', await payload(randomBytes(1000))); + await MODES.tracker!(sender, receiver); + await expect(receiver.locator('#log')).toContainText(`answer via ${tracker}`); + await fastForward(CONNECT_TIMEOUT); + await expect(sender.locator('#log')).toContainText(`receiver did not connect: the receiver ${NO_PUBLIC}`); + await expect(receiver.locator('#log')).toContainText( + `sender via ${tracker} did not connect: this side ${NO_PUBLIC}`, + ); + // Then once more for the turn a new offer may have taken meanwhile. + await fastForward('06:00'); + await fastForward(CONNECT_TIMEOUT); + await expect(sender.locator('#send-error')).toHaveText( + `no receiver connected within 6 minutes: the receiver ${NO_PUBLIC}`, + ); + await expect(receiver.locator('#receive-error')).toHaveText( + `no sender connected within 6 minutes: this side ${NO_PUBLIC}`, + ); + await expectClosed(sender); + await expectClosed(receiver); + }); + }); + // A peer that found the swarm without the code, here one that announces another code on the // sender's info_hash. Its offer isn't sealed with the code, the sender doesn't even answer it. test(`live check: ${name} sender shares on after a peer without the code`, async () => { @@ -1444,6 +1520,38 @@ const garbageOffer = (code?: string) => offer: { type: 'offer', sdp: code ? seal(code, 'offer', 'garbage') : 'garbage' }, }); +const NO_PUBLIC = 'has no public address, between different networks both sides need STUN'; + +// A sender and a receiver as if on different networks, with the servers open and the clock installed. +// Only the sender has a public address, from the fake STUN server, it answers nobody though. +async function apart(name: string) { + const pages = await Promise.all([open(name, { init: dropHosts }), open(name, { init: dropHosts })]); + for (const page of pages) { + await page.clock.install(); + await page.click('#servers summary'); + } + await pages[0]!.fill('#stun-servers', `stun:${LOCAL_ADDRESS}:${stun.address().port}`); + await pages[1]!.uncheck('#stun'); + return pages as [Page, Page]; +} + +// Init script: drops the local addresses from every description the peer sends, as if it were on +// another network. It runs in the hidden frames too, only the app's own window counts. +function dropHosts() { + if (window !== window.top) return; + const set = RTCPeerConnection.prototype.setRemoteDescription as ( + this: RTCPeerConnection, + description: RTCSessionDescriptionInit, + ) => Promise; + RTCPeerConnection.prototype.setRemoteDescription = function ( + this: RTCPeerConnection, + description: RTCSessionDescriptionInit, + ) { + const lines = description.sdp?.split('\r\n').filter((line) => !/^a=candidate:.* typ host\b/.test(line)); + return set.call(this, { ...description, sdp: lines?.join('\r\n') }); + }; +} + // Init script: window.abortListeners() counts the abort listeners on every signal. A session's signal // holds one for each thing it still has to close. It runs in the hidden frames too, only the app's // own window counts. diff --git a/src/App.vue b/src/App.vue index 8de5662..0686ca3 100644 --- a/src/App.vue +++ b/src/App.vue @@ -263,6 +263,12 @@ function onReceive() {
+

+ STUN is off: only devices on the same network or VPN can connect. +

+

+ No STUN server under Servers: only devices on the same network or VPN can connect. +