alanmcgovern / monotorrent

The official repository for MonoTorrent, a bittorrent library for .NET
https://github.com/alanmcgovern/monotorrent
MIT License
1.15k stars 397 forks source link

Troubles with torrent seeding #671

Open Kaidoz opened 3 months ago

Kaidoz commented 3 months ago

There's a problem with the client seeding. I am using home pc.

I tried to seeding torrent in every possible way, but attempts were unsuccessful. I take a torrent file, start its "StartAsync", after checking the hash, the seeding starts, but it is not actually there.

If I take the torrent file and start it in qBitTorrent - seeding goes.

I tried to switch off the firewall, also changed ports and various parameters.

Code: `

public class ManageTorrentSeedService { private ClientEngine _engine;

    public ManageTorrentSeedService()
    {
        const int httpListeningPort = 55125;

        var settingBuilder = new EngineSettingsBuilder
        {
            AllowPortForwarding = true,
            AutoSaveLoadDhtCache = true,
            AutoSaveLoadFastResume = true,

            AutoSaveLoadMagnetLinkMetadata = true,

            ListenEndPoints = new Dictionary<string, IPEndPoint> {
                { "ipv4", new IPEndPoint (IPAddress.Any, 55123) },
                { "ipv6", new IPEndPoint (IPAddress.IPv6Any, 55123) }
            },

            DhtEndPoint = new IPEndPoint(IPAddress.Any, 55123),

            HttpStreamingPrefix = $"http://0.0.0.0:{httpListeningPort}/"
        }.ToSettings();

        _engine = new ClientEngine(settingBuilder);
    }

    private async Task SeedClientAsync(string path, string torrentFileOrMagnetLink, CancellationToken cancellationToken)
    {
        try
        {
            var torrentSettings = new TorrentSettingsBuilder()
            {
                CreateContainingDirectory = false,
                AllowPeerExchange = true,
                AllowInitialSeeding = true,

            }.ToSettings();

            //torrentSettings.Ann
            var torrent = await Torrent.LoadAsync(torrentFileOrMagnetLink);
            var manager = await _engine.AddAsync(torrent, path, torrentSettings);

            string[] files = Directory.GetFiles(path);
            manager.TorrentStateChanged += Manager_TorrentStateChanged;
            manager.PeerConnected += Manager_PeerConnected;
            await manager.StartAsync();

            while (cancellationToken.IsCancellationRequested == false)
            {
                await Task.Delay(1000, cancellationToken);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }

}`

Kaidoz commented 3 months ago

Trouble solved. Added code before "StartAsync":

                foreach (var trackerUrl in await GetTrackers())
                {
                    var uri = new Uri(trackerUrl);
                    await manager.TrackerManager.AddTrackerAsync(uri);
                }
alanmcgovern commented 2 months ago

Trouble solved. Added code before "StartAsync":

                foreach (var trackerUrl in await GetTrackers())
                {
                    var uri = new Uri(trackerUrl);
                    await manager.TrackerManager.AddTrackerAsync(uri);
                }

It'd probably be easier if the appropriate trackers are included in the .torrent file when creating it. Failing that, DHT should make this work too, though the initial peer discovery could be slow if there are only one or two actual peers accessing the torrent :)

That said, is there anything I need to do on the library side or was this simply a setup issue for the torrents you're working with?

Kaidoz commented 2 months ago

Trouble solved. Added code before "StartAsync":

                foreach (var trackerUrl in await GetTrackers())
                {
                    var uri = new Uri(trackerUrl);
                    await manager.TrackerManager.AddTrackerAsync(uri);
                }

It'd probably be easier if the appropriate trackers are included in the .torrent file when creating it. Failing that, DHT should make this work too, though the initial peer discovery could be slow if there are only one or two actual peers accessing the torrent :)

That said, is there anything I need to do on the library side or was this simply a setup issue for the torrents you're working with?

Trackers have been added to torrent file. If upload a torrent file to qBitTorrent it works fine.

alanmcgovern commented 2 months ago

Hrm, then what exactly do you mean by the previous code snippet where you said "adding this to StartAsync fixed the issue"?

Where exactly did you add that tracker code? I thought this was in your own app/library, which made me think you were adding extra trackers beyond what was contained in the .torrent file itself.

Kaidoz commented 2 months ago

Hrm, then what exactly do you mean by the previous code snippet where you said "adding this to StartAsync fixed the issue"?

Where exactly did you add that tracker code? I thought this was in your own app/library, which made me think you were adding extra trackers beyond what was contained in the .torrent file itself.

When creating a torrent, I create it as follows:

         internal async Task<Torrent> CreateTorrent(string folderPath, string game)
        {
            var creator = new TorrentCreator();
            var files = new TorrentFileSource(folderPath);

            creator.Announces.Add(await GetTrackers());

            string path = GetTorrentFile(game);

            await using var stream = new FileStream(path, FileMode.Create);

            await creator.CreateAsync(files, stream);

            return await Torrent.LoadAsync(path);
        }

When I load a torrent file, I dont get a seeding due to lack of trackers. It worked only when I added them manually

alanmcgovern commented 2 months ago

Can you share an example of one of those torrents where the trackers don't show up correctly?

I wonder if you're accidentally adding all of the trackers in 1 tier when you create the torrent , versus adding each tracker as a separate tier when you do it manually.

However the way that operates should be the same in all spec compliant torrent clients so it's surprising that one client would work better than another

Kaidoz commented 2 months ago

Can you share an example of one of those torrents where the trackers don't show up correctly?

I wonder if you're accidentally adding all of the trackers in 1 tier when you create the torrent , versus adding each tracker as a separate tier when you do it manually.

However the way that operates should be the same in all spec compliant torrent clients so it's surprising that one client would work better than another Rust.zip

Code GetTrackers:

        private static async Task<List<string>> GetTrackers()
        {
            try
            {
                string text = await new HttpClient().GetStringAsync("https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt");

                List<string> trackers = new List<string>();

                string[] lines = text.Split("\n");

                foreach (var line in lines)
                {
                    if (string.IsNullOrWhiteSpace(line) == false)
                    {
                        trackers.Add(line.Trim());
                    }
                }

                SaveCacheTrackers(trackers);

                return trackers;
            }
            catch
            {
                return GetLocalTrackers();
            }
        }
keraf commented 2 months ago

Having the same issue here (on 3.0.2). My torrents have the trackers added to them but they won't start sending data unless I manually add the tracker. The state of the torrent says "seeding" but no bytes are leaving. Using the same torrent with the same tracker with two different clients (qBittorrent and Transmission) works right away.

It's a bit problematic for private torrents as the API doesn't allow manually adding trackers to them.

alanmcgovern commented 2 months ago

Could both of you try the new alpha release - 3.0.3-alpha.unstable.rev0003 ? It has definitely not been tested as thoroughly as I'd like, but it does contain some fixes for the kind of issue you're describing.

In addition, can you attach a logger for both the leecher client and seeder client and share the logs with me? It may have some private data (IP addresses of peers, filepaths for your torrent etc) so please review before sharing. Trim/redact anything you want redacted. It might help me figure out what the issue is if the new release doesn't help!