Thanos Apostolidis
Thanos Apostolidis

Reputation: 33

How to download a torrent knowing only it's infohash and peers?

I've built a DHT crawler reading/using BEP 5 from the bittorrent standard. With it I can gather infohashes from torrents and query DHT nodes for peers. Thus said, having:

  1. A torrent infohash.
  2. Current torrent peers.

How can I download the torrent?

BEP 9 suggest a magnet schema in the following form (omitting the tracker and name part):

magnet:?xt=urn:btih:<info-hash>&dn=<name>&tr=<tracker-url>&x.pe=<peer-address>

With this approach my current torrent client (Transmission) is stuck trying to get peers. In case of compatibility issues, I tried many other clients with no luck.

My second approach was to load the corresponding DHT node to the client temporarily and load the magnet url in the simplest form of:

magnet:?xt=urn:btih:<info-hash>

yielding no results.

In the following code, supposing we have infohash "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" that we received from listening DHT node "router.bittorrent.com:6881", shouldn't the following sample fetch the metadata?

import libtorrent as lt
import time

session = lt.session()
session.listen_on(6881, 6891)
session.add_dht_router("router.bittorrent.com", 6881)
session.start_dht()

time.sleep(1)

params = {"url":"magnet:?xt=urn:btih:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "save_path":'.'}
h = session.add_torrent(params)

while (not h.has_metadata()):
    time.sleep(1)

# download...

Small edit to clarify on the first approach:

Having a peer I just discovered from an infohash in the DHT with ip:port of X:Y and another peer in the same logic with ip:port of Z:Y, shouldn't the following magnet, pasted in any torrent client supporting BEP 9, download the torrent?

magnet:?xt=urn:btih:<info-hash>&x.pe=X:Y&x.pe=Z:Y

Upvotes: 1

Views: 4521

Answers (1)

the8472
the8472

Reputation: 43125

If you need a general overview how a magnet link download works in principle then this answer should cover it.

If you want to debug your implementation you'll need to drill down into the details and make sure that all necessary steps are observable so you can check where stuff fails.

  • Does the DHT lookup return any peers?
  • Can you connect to the peers? Do the peers indicate support for the necessary extensions?
  • Does your your client make the requests to obtain the metdata?
  • Does it get replies?
  • Do the replies validate?

Upvotes: 0

Related Questions