From f4df6524c50b9b2098ab3cf1abad92f6e4f6977b Mon Sep 17 00:00:00 2001 From: TheBeastLT Date: Mon, 16 Oct 2023 20:33:39 +0300 Subject: [PATCH 1/4] emit results on tracker response without all infohashes --- client.js | 12 ++++++++++++ lib/client/http-tracker.js | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/client.js b/client.js index c37345f17..5eb7e630c 100644 --- a/client.js +++ b/client.js @@ -285,6 +285,18 @@ Client.scrape = (opts, cb) => { } }) + client.once('done', success => { + if (len !== 0) { + client.destroy() + const keys = Object.keys(results) + if (keys.length === 1) { + cb(null, results[keys[0]]) + } else { + cb(null, results) + } + } + }) + client.scrape({ infoHash: opts.infoHash }) return client } diff --git a/lib/client/http-tracker.js b/lib/client/http-tracker.js index 9eefd892a..613b50c1b 100644 --- a/lib/client/http-tracker.js +++ b/lib/client/http-tracker.js @@ -247,7 +247,7 @@ class HTTPTracker extends Tracker { keys.forEach(_infoHash => { // TODO: optionally handle data.flags.min_request_interval // (separate from announce interval) - const infoHash = _infoHash.length !== 20 ? arr2hex(text2arr(_infoHash)) : bin2hex(_infoHash) + const infoHash = _infoHash.length === 40 ? _infoHash : _infoHash.length !== 20 ? arr2hex(text2arr(_infoHash)) : bin2hex(_infoHash) const response = Object.assign(data[_infoHash], { announce: this.announceUrl, @@ -255,6 +255,8 @@ class HTTPTracker extends Tracker { }) this.client.emit('scrape', response) }) + + this.client.emit('done', true) } } From f7b73aaf37df43710012696639cb84231e29c6b3 Mon Sep 17 00:00:00 2001 From: TheBeastLT Date: Sat, 11 Nov 2023 14:02:40 +0200 Subject: [PATCH 2/4] support ann as announce url --- lib/client/http-tracker.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/client/http-tracker.js b/lib/client/http-tracker.js index 613b50c1b..cc1359db9 100644 --- a/lib/client/http-tracker.js +++ b/lib/client/http-tracker.js @@ -11,7 +11,7 @@ import Tracker from './tracker.js' import compact2string from 'compact2string' const debug = Debug('bittorrent-tracker:http-tracker') -const HTTP_SCRAPE_SUPPORT = /\/(announce)[^/]*$/ +const HTTP_SCRAPE_SUPPORT = /(\/ann(?:ounce)?)[^/]*$/ /** * HTTP torrent tracker client (for an individual tracker) @@ -32,7 +32,7 @@ class HTTPTracker extends Tracker { const match = this.announceUrl.match(HTTP_SCRAPE_SUPPORT) if (match) { const pre = this.announceUrl.slice(0, match.index) - const post = this.announceUrl.slice(match.index + 9) + const post = this.announceUrl.slice(match.index + match[1].length) this.scrapeUrl = `${pre}/scrape${post}` } From 0b6d72cce53faa62b9f48254dfd830403e52f350 Mon Sep 17 00:00:00 2001 From: TheBeastLT Date: Wed, 28 Feb 2024 17:01:30 +0200 Subject: [PATCH 3/4] catch scrape error on request --- lib/client/http-tracker.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/client/http-tracker.js b/lib/client/http-tracker.js index bd5be9f3c..7916eb83d 100644 --- a/lib/client/http-tracker.js +++ b/lib/client/http-tracker.js @@ -80,7 +80,7 @@ class HTTPTracker extends Tracker { this._request(this.scrapeUrl, params, (err, data) => { if (err) return this.client.emit('warning', err) this._onScrapeResponse(data) - }) + }).catch((err) => this.client.emit('warning', err)) } destroy (cb) { From f94075f073390f7fbb46d572ec350a059446d29e Mon Sep 17 00:00:00 2001 From: TheBeastLT Date: Sat, 13 Apr 2024 18:42:08 +0300 Subject: [PATCH 4/4] expose common config params to allow changing them --- index.js | 3 ++- lib/common-node.js | 4 ++-- lib/common.js | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index e812c41c8..bce36f390 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ /*! bittorrent-tracker. MIT License. WebTorrent LLC */ import Client from './client.js' import Server from './server.js' +import common from './lib/common.js' export default Client -export { Client, Server } +export { Client, Server, common } diff --git a/lib/common-node.js b/lib/common-node.js index 9fefa5c4d..af63eb107 100644 --- a/lib/common-node.js +++ b/lib/common-node.js @@ -32,13 +32,13 @@ export const EVENT_NAMES = { * Client request timeout. How long to wait before considering a request to a * tracker server to have timed out. */ -export const REQUEST_TIMEOUT = 15000 +export let REQUEST_TIMEOUT = 15000 /** * Client destroy timeout. How long to wait before forcibly cleaning up all * pending requests, open sockets, etc. */ -export const DESTROY_TIMEOUT = 1000 +export let DESTROY_TIMEOUT = 1000 export function toUInt32 (n) { const buf = new Uint8Array(4) diff --git a/lib/common.js b/lib/common.js index ef888579c..f82619f56 100644 --- a/lib/common.js +++ b/lib/common.js @@ -4,8 +4,8 @@ import * as common from './common-node.js' export * from './common-node.js' -export const DEFAULT_ANNOUNCE_PEERS = 50 -export const MAX_ANNOUNCE_PEERS = 82 +export let DEFAULT_ANNOUNCE_PEERS = 50 +export let MAX_ANNOUNCE_PEERS = 82 // HACK: Fix for WHATWG URL object not parsing non-standard URL schemes like // 'udp:'. Just replace it with 'http:' since we only need a few properties.