Release 0.0.6 with reconnect fixes and regression coverage
This commit is contained in:
1 parent
acba97a7e1
commit
9f3f21884f
10 files changed
+279
-36
No files matched your search
@@ -571,13 +571,19 @@ function proposePairing() {
|
||||
async function acceptPairing() {
|
||||
const session = active, setup = pairingSetup;
|
||||
if (!session?.transfer || pairingState.value !== 'incoming' || !setup) return;
|
||||
// Claim this proposal before key derivation yields; a second click must not
|
||||
// replace its nonce or send a second acceptance while Vue is rerendering.
|
||||
pairingState.value = 'accepted';
|
||||
try {
|
||||
setup.acceptor = pairingNonce();
|
||||
setup.key = await derivePairingKey(session, setup, false);
|
||||
if (active !== session || pairingSetup !== setup) return;
|
||||
session.transfer.sendPairing({ step: 'accept', nonce: setup.acceptor });
|
||||
pairingState.value = 'accepted';
|
||||
} catch (err) { pairingState.value = ''; pairingError.value = asError(err).message; }
|
||||
} catch (err) {
|
||||
if (active !== session || pairingSetup !== setup) return;
|
||||
try { session.transfer.sendPairing({ step: 'error' }); } catch { /* the session ended */ }
|
||||
pairingState.value = ''; pairingSetup = null; pairingError.value = asError(err).message;
|
||||
}
|
||||
}
|
||||
function declinePairing() {
|
||||
try { active?.transfer?.sendPairing({ step: 'decline' }); } catch { /* the session ended */ }
|
||||
|
||||
+27
-9
@@ -26,6 +26,8 @@ export class TrackerClient {
|
||||
readonly peerId = randomId();
|
||||
private socket: WebSocket | null = null;
|
||||
private timer?: ReturnType<typeof setTimeout>;
|
||||
private connectTimer?: ReturnType<typeof setTimeout>;
|
||||
private rejectConnect?: (error: Error) => void;
|
||||
private closed = false;
|
||||
private readonly url: string;
|
||||
private readonly infoHash: string;
|
||||
@@ -36,18 +38,25 @@ export class TrackerClient {
|
||||
this.url = url; this.infoHash = infoHash; this.events = events; this.connectSocket = connectSocket;
|
||||
}
|
||||
connect() {
|
||||
if (this.closed) return Promise.reject(new Error('The tracker connection is closed.'));
|
||||
if (this.socket) return Promise.reject(new Error('The tracker connection has already started.'));
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
let socket: WebSocket;
|
||||
try { socket = this.connectSocket(this.url); }
|
||||
catch { reject(new Error('Could not open the tracker connection. Check the tracker address and the site’s connect-src policy.')); return; }
|
||||
this.socket = socket;
|
||||
const timeout = setTimeout(() => { reject(new Error('The tracker did not respond.')); this.close(); }, 15_000);
|
||||
socket.onopen = () => { clearTimeout(timeout); this.announce('started'); resolve(); };
|
||||
this.rejectConnect = reject;
|
||||
this.connectTimer = setTimeout(() => this.fail(new Error('The tracker did not respond.')), 15_000);
|
||||
socket.onopen = () => {
|
||||
clearTimeout(this.connectTimer);
|
||||
this.announce('started');
|
||||
this.rejectConnect = undefined;
|
||||
resolve();
|
||||
};
|
||||
socket.onclose = () => {
|
||||
clearTimeout(timeout);
|
||||
if (this.closed) return;
|
||||
const error = new Error('Could not reach the tracker, or it closed the connection.');
|
||||
reject(error); this.fail(error);
|
||||
this.fail(error);
|
||||
};
|
||||
socket.onmessage = event => this.receive(event.data);
|
||||
});
|
||||
@@ -66,7 +75,9 @@ export class TrackerClient {
|
||||
}
|
||||
close() {
|
||||
if (this.closed) return;
|
||||
this.closed = true; clearTimeout(this.timer);
|
||||
this.closed = true; clearTimeout(this.timer); clearTimeout(this.connectTimer);
|
||||
this.rejectConnect?.(new Error('Tracker connection cancelled.'));
|
||||
this.rejectConnect = undefined;
|
||||
const socket = this.socket;
|
||||
if (!socket) return;
|
||||
socket.onopen = socket.onclose = socket.onmessage = null;
|
||||
@@ -75,10 +86,13 @@ export class TrackerClient {
|
||||
}
|
||||
private send(data: object, closing = false) {
|
||||
if ((this.closed && !closing) || this.socket?.readyState !== WebSocket.OPEN) return;
|
||||
this.socket.send(JSON.stringify({ action: 'announce', info_hash: this.infoHash, peer_id: this.peerId, ...data }));
|
||||
try { this.socket.send(JSON.stringify({ action: 'announce', info_hash: this.infoHash, peer_id: this.peerId, ...data })); }
|
||||
catch { this.fail(new Error('Could not send to the tracker.')); }
|
||||
}
|
||||
private fail(error: Error) {
|
||||
if (this.closed) return;
|
||||
this.rejectConnect?.(error);
|
||||
this.rejectConnect = undefined;
|
||||
this.close(); this.events.onError(error);
|
||||
}
|
||||
private receive(raw: unknown) {
|
||||
@@ -164,6 +178,9 @@ export class ReconnectAttempt {
|
||||
this.signals++; this.working = true; this.lastSignal = Date.now();
|
||||
try {
|
||||
const { initiator, key } = this.options, offerId = randomId();
|
||||
// createOffer replaces the local peer immediately. Its previous answer
|
||||
// must stop being eligible before gathering or encryption yields.
|
||||
if (initiator) this.offerId = '';
|
||||
const code = initiator ? await this.options.hooks.createOffer() : '';
|
||||
if (this.closed) return;
|
||||
const sdp = await sealSignal(key, this.route(initiator ? 'offer' : 'wake', offerId, this.tracker.peerId), code);
|
||||
@@ -181,14 +198,15 @@ export class ReconnectAttempt {
|
||||
}
|
||||
private async receiveOffer({ peerId, offerId, sdp }: TrackerSignal) {
|
||||
const role = signalRole(sdp);
|
||||
if (this.closed || !this.fresh(offerId) || (role === 'wake') !== this.options.initiator || role === 'answer' || !role) return;
|
||||
if (this.closed || this.seen.has(offerId) || (role === 'wake') !== this.options.initiator || role === 'answer' || !role) return;
|
||||
let code: string;
|
||||
try { code = await openSignal(this.options.key, this.route(role, offerId, peerId), sdp); }
|
||||
catch { return; }
|
||||
if (this.closed) return;
|
||||
// Only authenticated messages consume replay slots. Invalid tracker
|
||||
// traffic must not suppress a valid offer or evict previously seen ones.
|
||||
if (this.closed || this.working || !this.fresh(offerId)) return;
|
||||
// A wake means the peer just arrived: offer now unless an offer is very recent.
|
||||
if (role === 'wake') { if (Date.now() - this.lastSignal > 3000) void this.signal(true); return; }
|
||||
if (this.working) return;
|
||||
this.working = true;
|
||||
try {
|
||||
const answer = await this.options.hooks.createAnswer(code);
|
||||
|
||||
Reference in new issue
Block a user