Keep tracker shares alive for 6 minutes, then let them go
Both default trackers closed the socket of a peer that announced only once, after about 2 and 3 minutes, and the card then wrongly said no tracker was reachable. Sender and receiver now announce again on the interval the tracker names (at least 30 s apart, whatever it names), the receiver with its same offer. A share, or a receiver looking for one, gives up after 6 minutes without a download: its tracker sockets close and the card says nobody came. A peer connected by then still gets its turn, the transfer itself has no time limit. The sender's code shows until when it is valid and is marked no longer valid, without Copy, once it expired or the share failed. A receiver that lost every tracker now closes its peer connections too, and the expiry timer goes with the session when it is cancelled, fails or the download starts. Tests fast-forward the page clock: re-announcing and expiry on both sides against mocked trackers, no timer or connection left after cancelled and failed sessions, and a live download after the deadline passed during the security check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a35bd71ca7
commit
2167b528b2
5 files changed
+242
-21
No files matched your search
+20
-4
@@ -8,6 +8,7 @@ import {
|
||||
receiveViaTracker,
|
||||
sendViaTracker,
|
||||
settings as coreSettings,
|
||||
TRACKER_WAIT,
|
||||
type Check,
|
||||
type Events,
|
||||
type Incoming,
|
||||
@@ -80,6 +81,10 @@ const file = ref<File>();
|
||||
const offer = ref('');
|
||||
const answerInput = ref('');
|
||||
const sendCode = ref('');
|
||||
// Until when a receiver can find the share with its code, the time shown and whether it passed.
|
||||
const sendUntil = ref('');
|
||||
const sendExpired = ref(false);
|
||||
let sendTimer: number | undefined;
|
||||
|
||||
const offerInput = ref('');
|
||||
const answer = ref('');
|
||||
@@ -162,6 +167,8 @@ function begin(on: 'send' | 'receive') {
|
||||
incoming.value = undefined;
|
||||
received.value = undefined;
|
||||
sendCode.value = '';
|
||||
sendExpired.value = false;
|
||||
clearTimeout(sendTimer);
|
||||
offer.value = '';
|
||||
answer.value = '';
|
||||
acceptAnswer = undefined;
|
||||
@@ -223,9 +230,15 @@ async function onCreateAnswer() {
|
||||
async function onShare() {
|
||||
const signal = begin('send');
|
||||
const code = await attempt(signal, sendViaTracker(file.value!, sendEvents, signal));
|
||||
if (code) sendCode.value = code;
|
||||
if (!code) return;
|
||||
sendCode.value = code;
|
||||
sendUntil.value = new Date(Date.now() + TRACKER_WAIT).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
||||
sendTimer = window.setTimeout(() => (sendExpired.value = true), TRACKER_WAIT);
|
||||
}
|
||||
|
||||
// Also once the share failed, every error on the tracker ends it.
|
||||
const codeGone = computed(() => sendExpired.value || !!error.value);
|
||||
|
||||
function onReceive() {
|
||||
const signal = begin('receive');
|
||||
attempt(signal, receiveViaTracker(receiveCode.value, receiveEvents, signal));
|
||||
@@ -285,10 +298,13 @@ function onReceive() {
|
||||
<h3>Tracker</h3>
|
||||
<button id="share" :disabled="!file || !settings.trackers.length" @click="onShare">Share via tracker</button>
|
||||
<template v-if="sendCode">
|
||||
<label for="send-code">Code, give this to the receiver:</label>
|
||||
<label v-if="codeGone" for="send-code">Code, no longer valid:</label>
|
||||
<label v-else for="send-code">Code, give this to the receiver, valid until {{ sendUntil }}:</label>
|
||||
<div role="group">
|
||||
<output id="send-code">{{ sendCode }}</output>
|
||||
<button id="copy-send-code" data-copy @click="onCopy(sendCode)">{{ copied === sendCode ? 'Copied' : 'Copy' }}</button>
|
||||
<output id="send-code" :data-expired="codeGone || undefined">{{ sendCode }}</output>
|
||||
<button id="copy-send-code" data-copy :disabled="codeGone" @click="onCopy(sendCode)">
|
||||
{{ copied === sendCode ? 'Copied' : 'Copy' }}
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
</section>
|
||||
|
||||
+78
-14
@@ -28,6 +28,11 @@ const ICE_TIMEOUT = 5000;
|
||||
// A tracker peer that got an answer but never opens the data channel gives up its turn after this,
|
||||
// e.g. one whose SDP has no usable candidates: its connection never even fails.
|
||||
const CONNECT_TIMEOUT = 30_000;
|
||||
// A share, or a receiver looking for one, gives up after this without a download: nobody announces
|
||||
// all day for a code nobody uses. Until then both announce again on the interval the tracker names,
|
||||
// else it drops them after a few minutes, but not more often than MIN_INTERVAL whatever it names.
|
||||
export const TRACKER_WAIT = 6 * 60_000;
|
||||
const MIN_INTERVAL = 30_000;
|
||||
// Far beyond what a file system stores, but keeps an absurd name out of the page and the worker.
|
||||
const MAX_NAME = 1024;
|
||||
|
||||
@@ -83,6 +88,16 @@ function live<T extends Record<string, (...args: never[]) => void>>(events: T, s
|
||||
) as T;
|
||||
}
|
||||
|
||||
// Runs expire after TRACKER_WAIT unless closed before, cleared with the session.
|
||||
function deadline(signal: AbortSignal, expire: () => void) {
|
||||
const timer = setTimeout(() => {
|
||||
owned.close();
|
||||
expire();
|
||||
}, TRACKER_WAIT);
|
||||
const owned = own(signal, { close: () => clearTimeout(timer) });
|
||||
return owned;
|
||||
}
|
||||
|
||||
function createPeerConnection(signal: AbortSignal) {
|
||||
// An empty urls list is a syntax error.
|
||||
const stun = settings.stun && settings.stunServers.length;
|
||||
@@ -560,21 +575,24 @@ export async function answerOffer(offer: string, events: ReceiveEvents, signal:
|
||||
// A socket to the tracker at url, closed with the session. Messages arrive parsed. A failure reason
|
||||
// before anything else rejects the announcement, the tracker is no use then and its socket closes, a
|
||||
// later one only concerns that message and is logged. lost runs once it closed without close() being
|
||||
// called: unreachable, lost or rejected.
|
||||
// called: unreachable, lost or rejected. again announces once more, on the first interval it names.
|
||||
function trackerSocket(
|
||||
url: string,
|
||||
signal: AbortSignal,
|
||||
events: Events,
|
||||
message: (msg: Message) => void,
|
||||
lost: () => void,
|
||||
again: () => void,
|
||||
) {
|
||||
const ws = own(signal, new WebSocket(url));
|
||||
let opened = false;
|
||||
let accepted = false;
|
||||
let closed = false;
|
||||
let reannounce: ReturnType<typeof setInterval> | undefined;
|
||||
const end = (reason: string) => {
|
||||
if (closed) return;
|
||||
closed = true;
|
||||
clearInterval(reannounce);
|
||||
// Already closed unless rejected, this only lets go of it.
|
||||
ws.close();
|
||||
events.log(reason);
|
||||
@@ -586,7 +604,9 @@ function trackerSocket(
|
||||
rejected: undefined as string | undefined,
|
||||
alive: () => !closed,
|
||||
close: () => {
|
||||
if (closed) return;
|
||||
closed = true;
|
||||
clearInterval(reannounce);
|
||||
ws.close();
|
||||
},
|
||||
};
|
||||
@@ -597,6 +617,10 @@ function trackerSocket(
|
||||
if (typeof failure !== 'string') {
|
||||
if (!msg) return;
|
||||
accepted = true;
|
||||
// Seconds, JSON may give Infinity for a huge one. Beyond TRACKER_WAIT it would never come.
|
||||
if (!reannounce && !closed && typeof msg.interval === 'number') {
|
||||
reannounce = setInterval(again, Math.min(TRACKER_WAIT, Math.max(MIN_INTERVAL, msg.interval * 1000)));
|
||||
}
|
||||
return message(msg);
|
||||
}
|
||||
if (accepted) return events.log(`${url}: ${failure}`);
|
||||
@@ -619,7 +643,8 @@ function noTracker(trackers: (Tracker | undefined)[]) {
|
||||
// Tracker signaling (optional): sender joins the swarm on every tracker and answers one offer at a
|
||||
// time. Until a receiver passed the security check, the next one gets its turn: a peer that guessed
|
||||
// the code, or got the same one by chance, doesn't end the share. Neither does an offer that can't be
|
||||
// answered or never connects. Once the download started the share is used up, the trackers go.
|
||||
// answered or never connects. Once the download started the share is used up, the trackers go. After
|
||||
// TRACKER_WAIT they go as well, a receiver connected by then still gets its turn.
|
||||
export async function sendViaTracker(file: File, events: Events, signal: AbortSignal) {
|
||||
events = live(events, signal);
|
||||
const code = randomCode();
|
||||
@@ -628,16 +653,28 @@ export async function sendViaTracker(file: File, events: Events, signal: AbortSi
|
||||
const trackers: Tracker[] = [];
|
||||
let busy = false;
|
||||
let shared = false;
|
||||
let expired = false;
|
||||
// The latest offer that came while busy, answered once that peer is out.
|
||||
let waiting: (() => void) | undefined;
|
||||
|
||||
// Without a tracker no further receiver can find the share.
|
||||
const stranded = () => !shared && !busy && !trackers.some((tracker) => tracker.alive());
|
||||
const nobody = `nobody received the file within ${TRACKER_WAIT / 60_000} minutes, share it again`;
|
||||
const expiry = deadline(signal, () => {
|
||||
expired = true;
|
||||
for (const tracker of trackers) tracker.close();
|
||||
if (stranded()) events.error(nobody);
|
||||
});
|
||||
// Once stranded the share is over.
|
||||
const end = (reason: string) => {
|
||||
expiry.close();
|
||||
events.error(reason);
|
||||
};
|
||||
|
||||
function next() {
|
||||
busy = false;
|
||||
if (signal.aborted) return;
|
||||
if (stranded()) return events.error(noTracker(trackers));
|
||||
if (stranded()) return end(expired ? nobody : noTracker(trackers));
|
||||
events.log('waiting for the next receiver');
|
||||
const offer = waiting;
|
||||
waiting = undefined;
|
||||
@@ -668,6 +705,7 @@ export async function sendViaTracker(file: File, events: Events, signal: AbortSi
|
||||
handshake.start();
|
||||
shared = true;
|
||||
waiting = undefined;
|
||||
expiry.close();
|
||||
for (const tracker of trackers) tracker.close();
|
||||
};
|
||||
sendFile(pc, channel, file, events, { ...handshake, start }, signal);
|
||||
@@ -704,7 +742,8 @@ export async function sendViaTracker(file: File, events: Events, signal: AbortSi
|
||||
if (busy) waiting = () => answer(tracker.ws, url, offer);
|
||||
else answer(tracker.ws, url, offer);
|
||||
},
|
||||
() => stranded() && events.error(noTracker(trackers)),
|
||||
() => stranded() && end(noTracker(trackers)),
|
||||
() => announce(tracker.ws, hash, peerId, { left: 0 }),
|
||||
);
|
||||
tracker.ws.onopen = () => {
|
||||
announce(tracker.ws, hash, peerId, { left: 0, event: 'started' });
|
||||
@@ -719,7 +758,8 @@ export async function sendViaTracker(file: File, events: Events, signal: AbortSi
|
||||
// Receiver keeps one offer up per tracker; the first data channel that opens wins. If its check fails
|
||||
// or the sender leaves before, the used up offers are replaced. The others stay, a sender may have
|
||||
// queued one of them. An answer that can't be used or never connects is replaced right away. Once the
|
||||
// download started, every other offer and tracker socket goes.
|
||||
// download started, every other offer and tracker socket goes. After TRACKER_WAIT no offer is made or
|
||||
// kept any more, a sender connected by then still gets its turn.
|
||||
export async function receiveViaTracker(code: string, events: ReceiveEvents, signal: AbortSignal) {
|
||||
events = live(events, signal);
|
||||
await downloadsReady();
|
||||
@@ -731,10 +771,29 @@ export async function receiveViaTracker(code: string, events: ReceiveEvents, sig
|
||||
const offers = new Map<string, { pc: RTCPeerConnection; tracker?: Tracker; lost: boolean }>();
|
||||
let isConnected = false;
|
||||
let started = false;
|
||||
let expired = false;
|
||||
|
||||
const nobody = `no sender answered within ${TRACKER_WAIT / 60_000} minutes, check the code and try again`;
|
||||
// Without a sender connected the search is over, every offer goes.
|
||||
const end = (reason: string) => {
|
||||
expiry.close();
|
||||
for (const { pc, tracker } of offers.values()) {
|
||||
tracker?.close();
|
||||
pc.close();
|
||||
}
|
||||
events.error(reason);
|
||||
};
|
||||
// A connected sender keeps its connection, the other offers wait for next or start.
|
||||
const expiry = deadline(signal, () => {
|
||||
expired = true;
|
||||
if (!isConnected) return end(nobody);
|
||||
for (const { tracker } of offers.values()) tracker?.close();
|
||||
});
|
||||
|
||||
const next = () => {
|
||||
isConnected = false;
|
||||
if (signal.aborted) return;
|
||||
if (expired) return end(nobody);
|
||||
events.log('looking again');
|
||||
for (const [url, { pc }] of offers) if (pc.signalingState === 'closed') offer(url);
|
||||
};
|
||||
@@ -752,7 +811,7 @@ export async function receiveViaTracker(code: string, events: ReceiveEvents, sig
|
||||
if (pc.signalingState === 'closed') return;
|
||||
events.log(reason);
|
||||
pc.close();
|
||||
if (!isConnected && !started && !signal.aborted) offer(url);
|
||||
if (!isConnected && !started && !signal.aborted && !expired) offer(url);
|
||||
};
|
||||
channel.onopen = () => {
|
||||
clearTimeout(timeout);
|
||||
@@ -762,6 +821,7 @@ export async function receiveViaTracker(code: string, events: ReceiveEvents, sig
|
||||
const start = () => {
|
||||
handshake.start();
|
||||
started = true;
|
||||
expiry.close();
|
||||
for (const other of offers.values()) {
|
||||
other.tracker?.close();
|
||||
if (other.pc !== pc) other.pc.close();
|
||||
@@ -776,7 +836,15 @@ export async function receiveViaTracker(code: string, events: ReceiveEvents, sig
|
||||
pc.setLocalDescription()
|
||||
.then(() => gatherIce(pc))
|
||||
.then(() => {
|
||||
if (pc.signalingState === 'closed') return;
|
||||
if (pc.signalingState === 'closed' || expired) return;
|
||||
// Again with the same offer on the interval, a busy sender may have dropped it meanwhile.
|
||||
const hello = (event?: string) =>
|
||||
announce(tracker.ws, hash, peerId, {
|
||||
left: 1,
|
||||
numwant: 1,
|
||||
event,
|
||||
offers: [{ offer_id: offerId, offer: pc.localDescription }],
|
||||
});
|
||||
const tracker = trackerSocket(
|
||||
url,
|
||||
signal,
|
||||
@@ -794,18 +862,14 @@ export async function receiveViaTracker(code: string, events: ReceiveEvents, sig
|
||||
() => {
|
||||
entry.lost = true;
|
||||
if (!isConnected && !started && [...offers.values()].every((other) => other.lost)) {
|
||||
events.error(noTracker([...offers.values()].map((other) => other.tracker)));
|
||||
end(noTracker([...offers.values()].map((other) => other.tracker)));
|
||||
}
|
||||
},
|
||||
() => hello(),
|
||||
);
|
||||
entry.tracker = tracker;
|
||||
tracker.ws.onopen = () => {
|
||||
announce(tracker.ws, hash, peerId, {
|
||||
left: 1,
|
||||
numwant: 1,
|
||||
event: 'started',
|
||||
offers: [{ offer_id: offerId, offer: pc.localDescription }],
|
||||
});
|
||||
hello('started');
|
||||
events.log(`looking on ${url}`);
|
||||
};
|
||||
})
|
||||
|
||||
@@ -246,6 +246,11 @@ button:disabled {
|
||||
user-select: all;
|
||||
}
|
||||
|
||||
#send-code[data-expired] {
|
||||
text-decoration: line-through;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
/* Copy buttons, secondary next to the value they copy */
|
||||
|
||||
[data-copy] {
|
||||
|
||||
Reference in new issue
Block a user