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!
- var DistributedHashTable = require( 'bittorrent-dht')
- var BittorrentProtocol = require('bittorrent-protocol')
- var TorrentDiscovery = require('torrent-discovery')
- var ParseTorrent = require('parse-torrent')
- var Net = require('net')
- var RandomBytes = require('randombytes')
- var UtP = require('ut_pex')
- var UtM = require('ut_metadata')
- class Client{
- constructor(){
- this.peerId = RandomBytes(20);
- this.DHT = new DistributedHashTable();
- this.torrents = [];
- }
- addInfohash(infohash){
- var torrent = new Torrent(infohash, this)
- torrent.discover()
- this.torrents.push(torrent)
- }
- }
- class Torrent{
- constructor(infoHash, client){
- this.infoHash = infoHash
- this.client = client
- this.discovery = null
- this.metadata = null
- this.peers = []
- }
- discover(){
- this.discovery = new TorrentDiscovery({
- infoHash: this.infoHash,
- peerId: this.client.peerId,
- dht: this.client.dht,
- tracker: false,
- port: 6000
- })
- this.discovery.on('peer', this._onPeer.bind(this))
- }
- _onPeer(address){
- var [host, port] = address.split(':')
- var Peer = {host: host, port: port}
- this._connectPeer(Peer)
- }
- _connectPeer(PeerAddress){
- //console.log("Creating new peer", PeerAddress)
- var match = false
- this.peers.forEach((peer)=>{
- if(peer.host === PeerAddress.host){
- match = true
- }
- })
- if(!match){
- var peer = new Peer(PeerAddress, this)
- this.peers.push(peer)
- }
- }
- }
- class Peer{
- constructor(address, torrent){
- this.host = address.host
- this.port = address.port
- this.torrent = torrent
- this.bitfield = null
- this.pieces = null
- this.wire = null
- this.conn = null
- this.connect()
- }
- connect(){
- this.wire = new BittorrentProtocol()
- this.wire.use(UtP())
- this.wire.ut_pex.on('peer', this.torrent._connectPeer.bind(this.torrent))
- this.wire.use(UtM())
- this.wire.ut_metadata.on('metadata', this._onMetadata.bind(this))
- this.wire.on('bitfield', this._onBitfield.bind(this))
- this.conn = new Net.createConnection({host: this.host, port: this.port})
- this.conn.on('error', (err)=>{
- console.log("Removing peer:",this.torrent.peers.indexOf(this))
- this.torrent.peers.splice(this.torrent.peers.indexOf(this), 1)
- })
- this.conn.pipe(this.wire).pipe(this.conn)
- this.wire.handshake(this.torrent.infoHash, this.torrent.client.peerId, {dht: true})
- if(!this.torrent.metadata){
- this.wire.ut_metadata.fetch()
- }
- }
- _onMetadata(meta){
- try{
- var metadata = ParseTorrent(meta)
- this.torrent.metadata = metadata
- for(var peer of this.torrent.peers){
- if(peer.wire && peer.wire.ut_metadata) peer.wire.ut_metadata.cancel()
- }
- }catch(err){
- console.error('Metadata failure', err)
- }
- }
- _onBitfield(bitfield){
- this.bitfield = bitfield
- var have = 0
- for(var i=0; i<bitfield.buffer.length << 3; i++){
- if(bitfield.get(i) === true){
- have += 1
- }
- }
- this.pieces = have
- }
- }
- setInterval(()=>{
- client.torrents.forEach((torrent)=>{
- torrent.peers.forEach((peer, i )=>{
- var status = ""
- if(torrent.metadata){
- status = peer.pieces === torrent.metadata.pieces.length ? "Seed" : "Leech"
- console.log(`[${i}] ${peer.host}:${peer.port} - ${peer.pieces}/${torrent.metadata.pieces.length} - ${status}`)
- }else{
- console.log(`[${i}] ${peer.host}:${peer.port}`)
- }
- })
- })
- },1000)
- var client = new Client()
- // INSERT INFOHASH HERE
- 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.
