11import clone from 'clone'
22import Debug from 'debug'
33import Peer from 'simple-peer'
4- import randombytes from 'randombytes'
54import Socket from '@thaunknown/simple-websocket'
65import Socks from 'socks'
6+ import { arr2text , arr2hex , hex2bin , bin2hex , randomBytes } from 'uint8-util'
77
8- import common from '../common.js'
8+ import { DESTROY_TIMEOUT } from '../common.js'
99import Tracker from './tracker.js'
1010
1111const debug = Debug ( 'bittorrent-tracker:websocket-tracker' )
@@ -80,8 +80,8 @@ class WebSocketTracker extends Tracker {
8080 }
8181
8282 const infoHashes = ( Array . isArray ( opts . infoHash ) && opts . infoHash . length > 0 )
83- ? opts . infoHash . map ( infoHash => infoHash . toString ( 'binary' ) )
84- : ( opts . infoHash && opts . infoHash . toString ( 'binary' ) ) || this . client . _infoHashBinary
83+ ? opts . infoHash . map ( infoHash => hex2bin ( infoHash ) )
84+ : ( opts . infoHash && hex2bin ( opts . infoHash ) ) || this . client . _infoHashBinary
8585 const params = {
8686 action : 'scrape' ,
8787 info_hash : infoHashes
@@ -138,7 +138,7 @@ class WebSocketTracker extends Tracker {
138138
139139 // Otherwise, wait a short time for potential responses to come in from the
140140 // server, then force close the socket.
141- timeout = setTimeout ( destroyCleanup , common . DESTROY_TIMEOUT )
141+ timeout = setTimeout ( destroyCleanup , DESTROY_TIMEOUT )
142142
143143 // But, if a response comes from the server before the timeout fires, do cleanup
144144 // right away.
@@ -214,7 +214,7 @@ class WebSocketTracker extends Tracker {
214214 this . expectingResponse = false
215215
216216 try {
217- data = JSON . parse ( Buffer . from ( data ) )
217+ data = JSON . parse ( arr2text ( data ) )
218218 } catch ( err ) {
219219 this . client . emit ( 'warning' , new Error ( 'Invalid tracker response' ) )
220220 return
@@ -233,7 +233,7 @@ class WebSocketTracker extends Tracker {
233233 if ( data . info_hash !== this . client . _infoHashBinary ) {
234234 debug (
235235 'ignoring websocket data from %s for %s (looking for %s: reused socket)' ,
236- this . announceUrl , common . binaryToHex ( data . info_hash ) , this . client . infoHash
236+ this . announceUrl , bin2hex ( data . info_hash ) , this . client . infoHash
237237 )
238238 return
239239 }
@@ -266,7 +266,7 @@ class WebSocketTracker extends Tracker {
266266 if ( data . complete != null ) {
267267 const response = Object . assign ( { } , data , {
268268 announce : this . announceUrl ,
269- infoHash : common . binaryToHex ( data . info_hash )
269+ infoHash : bin2hex ( data . info_hash )
270270 } )
271271 this . client . emit ( 'update' , response )
272272 }
@@ -275,7 +275,7 @@ class WebSocketTracker extends Tracker {
275275 if ( data . offer && data . peer_id ) {
276276 debug ( 'creating peer (from remote offer)' )
277277 peer = this . _createPeer ( )
278- peer . id = common . binaryToHex ( data . peer_id )
278+ peer . id = bin2hex ( data . peer_id )
279279 peer . once ( 'signal' , answer => {
280280 const params = {
281281 action : 'announce' ,
@@ -293,10 +293,10 @@ class WebSocketTracker extends Tracker {
293293 }
294294
295295 if ( data . answer && data . peer_id ) {
296- const offerId = common . binaryToHex ( data . offer_id )
296+ const offerId = bin2hex ( data . offer_id )
297297 peer = this . peers [ offerId ]
298298 if ( peer ) {
299- peer . id = common . binaryToHex ( data . peer_id )
299+ peer . id = bin2hex ( data . peer_id )
300300 this . client . emit ( 'peer' , peer )
301301 peer . signal ( data . answer )
302302
@@ -323,7 +323,7 @@ class WebSocketTracker extends Tracker {
323323 // (separate from announce interval)
324324 const response = Object . assign ( data [ infoHash ] , {
325325 announce : this . announceUrl ,
326- infoHash : common . binaryToHex ( infoHash )
326+ infoHash : bin2hex ( infoHash )
327327 } )
328328 this . client . emit ( 'scrape' , response )
329329 } )
@@ -376,13 +376,13 @@ class WebSocketTracker extends Tracker {
376376 checkDone ( )
377377
378378 function generateOffer ( ) {
379- const offerId = randombytes ( 20 ) . toString ( 'hex' )
379+ const offerId = arr2hex ( randomBytes ( 20 ) )
380380 debug ( 'creating peer (from _generateOffers)' )
381381 const peer = self . peers [ offerId ] = self . _createPeer ( { initiator : true } )
382382 peer . once ( 'signal' , offer => {
383383 offers . push ( {
384384 offer,
385- offer_id : common . hexToBinary ( offerId )
385+ offer_id : hex2bin ( offerId )
386386 } )
387387 checkDone ( )
388388 } )
0 commit comments