arvidn / libtorrent

an efficient feature complete C++ bittorrent implementation
http://libtorrent.org
Other
5.22k stars 993 forks source link

DHT seeding in python #7749

Open MrNonoss opened 1 week ago

MrNonoss commented 1 week ago

Please provide the following information

libtorrent version (or branch): 2.0.9.0 platform/architecture: MacOS intel compiler and compiler version: Python3.10

please describe what symptom you see, what you would expect to see instead and how to reproduce it.

I often need to share large files across several computers in a LAN, sometimes without internet access.

I try to write a python script to:

My script seems to be working:

Would you mind taking a look at my seeding function to help me fix this?

# Seed Torrent 
def seed_torrent(torrent_path, save_path):
    settings = {
        'listen_interfaces': '0.0.0.0:6881',
        'enable_lsd': True,
        'enable_dht': True,
        'use_dht_as_fallback': True,
        'dht_restrict_routing_ips' : False,
    }

    session = lt.session()
    session.apply_settings(settings)

    info = lt.torrent_info(torrent_path)

    params = {
        'ti': info,
        'save_path': save_path,
        'flags': lt.torrent_flags.seed_mode
    }
    h = session.add_torrent(params)
    print(f"#---> Seed started on port : {session.listen_port()}")

    # Verification step to check if the session is activated and seeding
    if not session.is_listening():
        print("#---> Error: session is not running.")
        sys.exit(1)

    if not session.is_dht_running():
        print("#---> Error: DHT is not running.")
        sys.exit(1)

    try:
        while not h.status().is_seeding:
            s = h.status()

            print('\r%.2f%% complete (down: %.1f kB/s up: %.1f kB/s peers: %d) %s' % (
                s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000,
                s.num_peers, s.state), end=' ')

            alerts = session.pop_alerts()
            for a in alerts:
                if a.category() & lt.alert.category_t.error_notification:
                    print(a)

            sys.stdout.flush()
            time.sleep(1)
    except KeyboardInterrupt:
        print("Seeding interrupted by user.")
    finally:
        print("File Shared Successfully!")

I included a couple of verification to check if the session is active and DHT activated. Session is valid, but DHT always fails the first run.

many thanks for your help

MassaRoddel commented 1 week ago

DHT needs a bootstrap node. You can provide one for the session or add node ips to the torrent file. (On LAN LSD should be enough)

MrNonoss commented 1 week ago

Many thanks for your answer. I tried to google this to understand, however I cannot seem not get what I should add. As per the documentation, I find a setting dht_bootstrap_nodes but don't get what I should fill.

Would you have an exemple to provide?

Just to be sure, you last comment indicates that DHT is not mandatory to share within a LAN, and another protocol, LSD, would work?

MassaRoddel commented 6 days ago

The DHT bootstrap node has a similar function like a tracker, except it is only needed to find other DHT nodes (torrent clients) at the beginning. Torrent files can have ip address entries of such DHT nodes. Basically, all your torrent clients should get the ip address of the torrent client you want to be the DHT bootstrap node (or run extra instance for that purpose). LSD (local service discovery) should work on LAN (without routers).

HanabishiRecca commented 6 days ago

From what I understand, to share in my LAN only, without the need to setup trackers, the DHT protocol should be used.

No, DHT would not help connecting across a LAN.

another protocol, LSD, would work?

It could work, depending on your network configuration.

But it's actually easier to just add the peer manually on the client side. E.g. for qBittorrent: select the torrent, go to the Peers tab, click RMB there > Add peers..., paste address:port of the seed in a LAN (like 192.168.1.1:6881) and that's it.

MrNonoss commented 6 days ago

Oh, I get it better now, many thanks for your valuable inputs.

But it's actually easier to just add the peer manually on the client side.

And I assume I would need to manually enter all the peers in each client, or it would download from only one source, which defeat the purpose of a torrent here.