SHARE
TWEET

DHT InfoHash scraper

a guest Oct 23rd, 2018 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var DistributedHashTable = require( 'bittorrent-dht')
  2. var BittorrentProtocol = require('bittorrent-protocol')
  3. var TorrentDiscovery = require('torrent-discovery')
  4. var ParseTorrent = require('parse-torrent')
  5.  
  6. var Net = require('net')
  7. var RandomBytes = require('randombytes')
  8.  
  9.  
  10. var UtP = require('ut_pex')
  11. var UtM = require('ut_metadata')
  12.  
  13. class Client{
  14.   constructor(){
  15.     this.peerId = RandomBytes(20);
  16.     this.DHT = new DistributedHashTable();
  17.     this.torrents = [];
  18.   }
  19.   addInfohash(infohash){
  20.     var torrent = new Torrent(infohash, this)
  21.     torrent.discover()
  22.     this.torrents.push(torrent)
  23.   }
  24. }
  25.  
  26. class Torrent{
  27.   constructor(infoHash, client){
  28.     this.infoHash = infoHash
  29.     this.client = client
  30.     this.discovery = null
  31.     this.metadata = null
  32.     this.peers = []
  33.   }
  34.   discover(){
  35.     this.discovery = new TorrentDiscovery({
  36.       infoHash: this.infoHash,
  37.       peerId: this.client.peerId,
  38.       dht: this.client.dht,
  39.       tracker: false,
  40.       port: 6000
  41.     })
  42.  
  43.     this.discovery.on('peer', this._onPeer.bind(this))
  44.   }
  45.   _onPeer(address){
  46.     var [host, port] = address.split(':')
  47.     var Peer = {host: host, port: port}
  48.     this._connectPeer(Peer)
  49.   }
  50.   _connectPeer(PeerAddress){
  51.     //console.log("Creating new peer", PeerAddress)
  52.     var match = false
  53.     this.peers.forEach((peer)=>{
  54.       if(peer.host === PeerAddress.host){
  55.         match = true
  56.       }
  57.     })
  58.     if(!match){
  59.       var peer = new Peer(PeerAddress, this)
  60.       this.peers.push(peer)
  61.     }
  62.   }
  63. }
  64.  
  65. class Peer{
  66.   constructor(address, torrent){
  67.     this.host = address.host
  68.     this.port = address.port
  69.     this.torrent = torrent
  70.  
  71.     this.bitfield = null
  72.     this.pieces = null
  73.    
  74.     this.wire = null
  75.     this.conn = null
  76.  
  77.     this.connect()
  78.   }
  79.  
  80.   connect(){
  81.     this.wire = new BittorrentProtocol()
  82.     this.wire.use(UtP())
  83.     this.wire.ut_pex.on('peer', this.torrent._connectPeer.bind(this.torrent))
  84.  
  85.     this.wire.use(UtM())
  86.     this.wire.ut_metadata.on('metadata', this._onMetadata.bind(this))
  87.     this.wire.on('bitfield', this._onBitfield.bind(this))
  88.  
  89.     this.conn = new Net.createConnection({host: this.host, port: this.port})
  90.     this.conn.on('error', (err)=>{
  91.       console.log("Removing peer:",this.torrent.peers.indexOf(this))
  92.       this.torrent.peers.splice(this.torrent.peers.indexOf(this), 1)
  93.     })
  94.     this.conn.pipe(this.wire).pipe(this.conn)
  95.     this.wire.handshake(this.torrent.infoHash, this.torrent.client.peerId, {dht: true})
  96.  
  97.     if(!this.torrent.metadata){
  98.       this.wire.ut_metadata.fetch()
  99.     }
  100.  
  101.   }
  102.   _onMetadata(meta){
  103.     try{
  104.       var metadata = ParseTorrent(meta)
  105.       this.torrent.metadata = metadata
  106.       for(var peer of this.torrent.peers){
  107.         if(peer.wire && peer.wire.ut_metadata) peer.wire.ut_metadata.cancel()
  108.       }
  109.     }catch(err){
  110.       console.error('Metadata failure', err)
  111.     }
  112.   }
  113.   _onBitfield(bitfield){
  114.     this.bitfield = bitfield
  115.     var have = 0
  116.     for(var i=0; i<bitfield.buffer.length << 3; i++){
  117.       if(bitfield.get(i) === true){
  118.         have += 1
  119.       }
  120.     }
  121.     this.pieces = have
  122.   }
  123. }
  124.  
  125. setInterval(()=>{
  126.   client.torrents.forEach((torrent)=>{
  127.     torrent.peers.forEach((peer, i )=>{
  128.       var status = ""
  129.       if(torrent.metadata){
  130.         status = peer.pieces === torrent.metadata.pieces.length ? "Seed" : "Leech"
  131.         console.log(`[${i}] ${peer.host}:${peer.port} - ${peer.pieces}/${torrent.metadata.pieces.length} - ${status}`)
  132.        
  133.       }else{
  134.         console.log(`[${i}] ${peer.host}:${peer.port}`)
  135.       }
  136.     })
  137.   })
  138. },1000)
  139.  
  140. var client = new Client()
  141. // INSERT INFOHASH HERE
  142. client.addInfohash('')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top