Skip to content

Commit 32505de

Browse files
committed
Merge branch 'master' of https://github.com/webtorrent/bittorrent-tracker into drop-simple-get
2 parents 227ddb2 + a6922f7 commit 32505de

File tree

14 files changed

+161
-110
lines changed

14 files changed

+161
-110
lines changed

.github/workflows/stale.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
pull-requests: write
1313

1414
steps:
15-
- uses: actions/stale@v7
15+
- uses: actions/stale@v8
1616
with:
1717
repo-token: ${{ secrets.GITHUB_TOKEN }}
1818
stale-issue-message: 'Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?'

CHANGELOG.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,66 @@
1+
## [10.0.12](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.11...v10.0.12) (2023-08-09)
2+
3+
4+
### Bug Fixes
5+
6+
* **deps:** update dependency bencode to v4 ([#487](https://github.com/webtorrent/bittorrent-tracker/issues/487)) ([aeccf9c](https://github.com/webtorrent/bittorrent-tracker/commit/aeccf9c1c4b9115fd23b4fe1a0ab990b5add0f17))
7+
8+
## [10.0.11](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.10...v10.0.11) (2023-08-01)
9+
10+
11+
### Bug Fixes
12+
13+
* mangled scrape infohashes ([#486](https://github.com/webtorrent/bittorrent-tracker/issues/486)) ([11cce83](https://github.com/webtorrent/bittorrent-tracker/commit/11cce83ddd858813f5684da8a116de4bee6e518b))
14+
15+
## [10.0.10](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.9...v10.0.10) (2023-06-16)
16+
17+
18+
### Performance Improvements
19+
20+
* use simple-peer/lite ([#475](https://github.com/webtorrent/bittorrent-tracker/issues/475)) ([5b8db06](https://github.com/webtorrent/bittorrent-tracker/commit/5b8db067e48cc81796728ff538d7ff6efafc59b8))
21+
22+
## [10.0.9](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.8...v10.0.9) (2023-06-16)
23+
24+
25+
### Performance Improvements
26+
27+
* use peer/lite ([#474](https://github.com/webtorrent/bittorrent-tracker/issues/474)) ([7c845f0](https://github.com/webtorrent/bittorrent-tracker/commit/7c845f030d07b1bf7060ab880b790ee85a8c7ac0))
28+
29+
## [10.0.8](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.7...v10.0.8) (2023-06-07)
30+
31+
32+
### Bug Fixes
33+
34+
* bigInt ([#472](https://github.com/webtorrent/bittorrent-tracker/issues/472)) ([d7061f7](https://github.com/webtorrent/bittorrent-tracker/commit/d7061f73b2ebff072e064971a5960749a7335bae))
35+
36+
## [10.0.7](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.6...v10.0.7) (2023-06-05)
37+
38+
39+
### Bug Fixes
40+
41+
* imports ([#471](https://github.com/webtorrent/bittorrent-tracker/issues/471)) ([a12022a](https://github.com/webtorrent/bittorrent-tracker/commit/a12022ac2c81d7fa3ecb81163852161e64199cf4))
42+
43+
## [10.0.6](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.5...v10.0.6) (2023-05-27)
44+
45+
46+
### Bug Fixes
47+
48+
* replace simple-peer with maintained one ([#466](https://github.com/webtorrent/bittorrent-tracker/issues/466)) ([3b2dedb](https://github.com/webtorrent/bittorrent-tracker/commit/3b2dedb4151615831ca12d3d0a830354b1c04e68))
49+
50+
## [10.0.5](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.4...v10.0.5) (2023-05-27)
51+
52+
53+
### Bug Fixes
54+
55+
* only stringify views ([#467](https://github.com/webtorrent/bittorrent-tracker/issues/467)) ([52f5502](https://github.com/webtorrent/bittorrent-tracker/commit/52f55020f38894e4d45e12c87184540d8b0acad3))
56+
57+
## [10.0.4](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.3...v10.0.4) (2023-05-26)
58+
59+
60+
### Bug Fixes
61+
62+
* drop buffer ([#465](https://github.com/webtorrent/bittorrent-tracker/issues/465)) ([c99eb89](https://github.com/webtorrent/bittorrent-tracker/commit/c99eb892088ef3c67ea5bf014dfdd86799251a7e))
63+
164
## [10.0.3](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.2...v10.0.3) (2023-05-25)
265

366

client.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import Debug from 'debug'
22
import EventEmitter from 'events'
33
import once from 'once'
44
import parallel from 'run-parallel'
5-
import Peer from 'simple-peer'
5+
import Peer from '@thaunknown/simple-peer/lite.js'
66
import queueMicrotask from 'queue-microtask'
7+
import { hex2arr, hex2bin, text2arr, arr2hex, arr2text } from 'uint8-util'
78

89
import common from './lib/common.js'
910
import HTTPTracker from './lib/client/http-tracker.js' // empty object in browser
@@ -18,8 +19,8 @@ const debug = Debug('bittorrent-tracker:client')
1819
* Find torrent peers, to help a torrent client participate in a torrent swarm.
1920
*
2021
* @param {Object} opts options object
21-
* @param {string|Buffer} opts.infoHash torrent info hash
22-
* @param {string|Buffer} opts.peerId peer id
22+
* @param {string|Uint8Array} opts.infoHash torrent info hash
23+
* @param {string|Uint8Array} opts.peerId peer id
2324
* @param {string|Array.<string>} opts.announce announce
2425
* @param {number} opts.port torrent client listening port
2526
* @param {function} opts.getAnnounceOpts callback to provide data to tracker
@@ -39,15 +40,15 @@ class Client extends EventEmitter {
3940

4041
this.peerId = typeof opts.peerId === 'string'
4142
? opts.peerId
42-
: opts.peerId.toString('hex')
43-
this._peerIdBuffer = Buffer.from(this.peerId, 'hex')
44-
this._peerIdBinary = this._peerIdBuffer.toString('binary')
43+
: arr2hex(opts.peerId)
44+
this._peerIdBuffer = hex2arr(this.peerId)
45+
this._peerIdBinary = hex2bin(this.peerId)
4546

4647
this.infoHash = typeof opts.infoHash === 'string'
4748
? opts.infoHash.toLowerCase()
48-
: opts.infoHash.toString('hex')
49-
this._infoHashBuffer = Buffer.from(this.infoHash, 'hex')
50-
this._infoHashBinary = this._infoHashBuffer.toString('binary')
49+
: arr2hex(opts.infoHash)
50+
this._infoHashBuffer = hex2arr(this.infoHash)
51+
this._infoHashBinary = hex2bin(this.infoHash)
5152

5253
debug('new client %s', this.infoHash)
5354

@@ -69,7 +70,7 @@ class Client extends EventEmitter {
6970

7071
// Remove trailing slash from trackers to catch duplicates
7172
announce = announce.map(announceUrl => {
72-
announceUrl = announceUrl.toString()
73+
if (ArrayBuffer.isView(announceUrl)) announceUrl = arr2text(announceUrl)
7374
if (announceUrl[announceUrl.length - 1] === '/') {
7475
announceUrl = announceUrl.substring(0, announceUrl.length - 1)
7576
}
@@ -260,7 +261,7 @@ Client.scrape = (opts, cb) => {
260261

261262
const clientOpts = Object.assign({}, opts, {
262263
infoHash: Array.isArray(opts.infoHash) ? opts.infoHash[0] : opts.infoHash,
263-
peerId: Buffer.from('01234567890123456789'), // dummy value
264+
peerId: text2arr('01234567890123456789'), // dummy value
264265
port: 6881 // dummy value
265266
})
266267

@@ -284,9 +285,6 @@ Client.scrape = (opts, cb) => {
284285
}
285286
})
286287

287-
opts.infoHash = Array.isArray(opts.infoHash)
288-
? opts.infoHash.map(infoHash => Buffer.from(infoHash, 'hex'))
289-
: Buffer.from(opts.infoHash, 'hex')
290288
client.scrape({ infoHash: opts.infoHash })
291289
return client
292290
}

lib/client/http-tracker.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import clone from 'clone'
44
import Debug from 'debug'
55
import fetch from 'cross-fetch-ponyfill'
66
import Socks from 'socks'
7+
import { bin2hex, hex2bin, arr2text, text2arr, arr2hex } from 'uint8-util'
78

89
import common from '../common.js'
910
import Tracker from './tracker.js'
@@ -73,8 +74,8 @@ class HTTPTracker extends Tracker {
7374
}
7475

7576
const infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
76-
? opts.infoHash.map(infoHash => infoHash.toString('binary'))
77-
: (opts.infoHash && opts.infoHash.toString('binary')) || this.client._infoHashBinary
77+
? opts.infoHash.map(infoHash => hex2bin(infoHash))
78+
: (opts.infoHash && hex2bin(opts.infoHash)) || this.client._infoHashBinary
7879
const params = {
7980
info_hash: infoHashes
8081
}
@@ -162,13 +163,13 @@ class HTTPTracker extends Tracker {
162163
} catch (err) {
163164
return cb(new Error(`Error decoding tracker response: ${err.message}`))
164165
}
165-
const failure = data['failure reason'] && Buffer.from(data['failure reason']).toString()
166+
const failure = data['failure reason'] && arr2text(data['failure reason'])
166167
if (failure) {
167168
debug(`failure from ${requestUrl} (${failure})`)
168169
return cb(new Error(failure))
169170
}
170171

171-
const warning = data['warning message'] && Buffer.from(data['warning message']).toString()
172+
const warning = data['warning message'] && arr2text(data['warning message'])
172173
if (warning) {
173174
debug(`warning from ${requestUrl} (${warning})`)
174175
this.client.emit('warning', new Error(warning))
@@ -200,7 +201,7 @@ class HTTPTracker extends Tracker {
200201

201202
const response = Object.assign({}, data, {
202203
announce: this.announceUrl,
203-
infoHash: common.binaryToHex(data.info_hash)
204+
infoHash: bin2hex(data.info_hash || String(data.info_hash))
204205
})
205206
this.client.emit('update', response)
206207

@@ -254,12 +255,14 @@ class HTTPTracker extends Tracker {
254255
return
255256
}
256257

257-
keys.forEach(infoHash => {
258+
keys.forEach(_infoHash => {
258259
// TODO: optionally handle data.flags.min_request_interval
259260
// (separate from announce interval)
260-
const response = Object.assign(data[infoHash], {
261+
const infoHash = _infoHash.length !== 20 ? arr2hex(text2arr(_infoHash)) : bin2hex(_infoHash)
262+
263+
const response = Object.assign(data[_infoHash], {
261264
announce: this.announceUrl,
262-
infoHash: common.binaryToHex(infoHash)
265+
infoHash
263266
})
264267
this.client.emit('scrape', response)
265268
})

lib/client/udp-tracker.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import arrayRemove from 'unordered-array-remove'
2-
import BN from 'bn.js'
32
import clone from 'clone'
43
import Debug from 'debug'
54
import dgram from 'dgram'
6-
import randombytes from 'randombytes'
75
import Socks from 'socks'
6+
import { concat, hex2arr, randomBytes } from 'uint8-util'
87

98
import common from '../common.js'
109
import Tracker from './tracker.js'
@@ -131,7 +130,7 @@ class UDPTracker extends Tracker {
131130
}, common.REQUEST_TIMEOUT)
132131
if (timeout.unref) timeout.unref()
133132

134-
send(Buffer.concat([
133+
send(concat([
135134
common.CONNECTION_ID,
136135
common.toUInt32(common.ACTIONS.CONNECT),
137136
transactionId
@@ -175,7 +174,8 @@ class UDPTracker extends Tracker {
175174

176175
function onSocketMessage (msg) {
177176
if (proxySocket) msg = msg.slice(10)
178-
if (msg.length < 8 || msg.readUInt32BE(4) !== transactionId.readUInt32BE(0)) {
177+
const view = new DataView(transactionId.buffer)
178+
if (msg.length < 8 || msg.readUInt32BE(4) !== view.getUint32(0)) {
179179
return onError(new Error('tracker sent invalid transaction id'))
180180
}
181181

@@ -270,14 +270,14 @@ class UDPTracker extends Tracker {
270270
function announce (connectionId, opts) {
271271
transactionId = genTransactionId()
272272

273-
send(Buffer.concat([
273+
send(concat([
274274
connectionId,
275275
common.toUInt32(common.ACTIONS.ANNOUNCE),
276276
transactionId,
277277
self.client._infoHashBuffer,
278278
self.client._peerIdBuffer,
279279
toUInt64(opts.downloaded),
280-
opts.left != null ? toUInt64(opts.left) : Buffer.from('FFFFFFFFFFFFFFFF', 'hex'),
280+
opts.left != null ? toUInt64(opts.left) : hex2arr('ffffffffffffffff'),
281281
toUInt64(opts.uploaded),
282282
common.toUInt32(common.EVENTS[opts.event] || 0),
283283
common.toUInt32(0), // ip address (optional)
@@ -291,10 +291,10 @@ class UDPTracker extends Tracker {
291291
transactionId = genTransactionId()
292292

293293
const infoHash = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
294-
? Buffer.concat(opts.infoHash)
294+
? concat(opts.infoHash)
295295
: (opts.infoHash || self.client._infoHashBuffer)
296296

297-
send(Buffer.concat([
297+
send(concat([
298298
connectionId,
299299
common.toUInt32(common.ACTIONS.SCRAPE),
300300
transactionId,
@@ -307,26 +307,26 @@ class UDPTracker extends Tracker {
307307
UDPTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 60 * 1000 // 30 minutes
308308

309309
function genTransactionId () {
310-
return randombytes(4)
310+
return randomBytes(4)
311311
}
312312

313313
function toUInt16 (n) {
314-
const buf = Buffer.allocUnsafe(2)
315-
buf.writeUInt16BE(n, 0)
314+
const buf = new Uint8Array(2)
315+
const view = new DataView(buf.buffer)
316+
view.setUint16(0, n)
316317
return buf
317318
}
318319

319320
const MAX_UINT = 4294967295
320321

321322
function toUInt64 (n) {
322323
if (n > MAX_UINT || typeof n === 'string') {
323-
const bytes = new BN(n).toArray()
324-
while (bytes.length < 8) {
325-
bytes.unshift(0)
326-
}
327-
return Buffer.from(bytes)
324+
const buf = new Uint8Array(8)
325+
const view = new DataView(buf.buffer)
326+
view.setBigUint64(0, BigInt(n))
327+
return buf
328328
}
329-
return Buffer.concat([common.toUInt32(0), common.toUInt32(n)])
329+
return concat([new Uint8Array(4), common.toUInt32(n)])
330330
}
331331

332332
function noop () {}

0 commit comments

Comments
 (0)