alanmcgovern / monotorrent

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

Disable DHT - Private torrent tries to connect to IP Addresses not added as peers #453

Closed ghost closed 3 years ago

ghost commented 3 years ago

I'm creating a private torrent and then trying to add peers manually instead of using a tracker, i have yet to complete the functionality that adds peers on both sides but i add the correct peer information on one side and after that the torrent adds some other IPs that have not been added as peers and the torrent is created as private, is there some way to prevent this (possibly by disabling DHT completely globally in the engine?)

Here is my torrent creation code:

           MonoTorrent.TorrentCreator torrentcreator = new MonoTorrent.TorrentCreator();
            ITorrentFileSource file_source = new TorrentFileSource(filepath);
            torrentcreator.Comment = comment;//CommentsRichTxt.Text;
            torrentcreator.CreatedBy = "Noob";
            torrentcreator.Publisher = "pub";
            torrentcreator.Private = true;

            BEncodedDictionary result = await torrentcreator.CreateAsync(file_source);

            Torrent torrent = Torrent.Load(result);
            return torrent.InfoHash.ToHex();

engine settings:

         var settingBuilder = new EngineSettingsBuilder {
                // Allow the engine to automatically forward ports using upnp/nat-pmp (if a compatible router is available)
                AllowPortForwarding = true,

                // Automatically save a cache of the DHT table when all torrents are stopped.
                //AutoSaveLoadDhtCache = true,

                // Automatically save 'FastResume' data when TorrentManager.StopAsync is invoked, automatically load it
                // before hash checking the torrent. Fast Resume data will be loaded as part of 'engine.AddAsync' if
                // torrent metadata is available. Otherwise, if a magnetlink is used to download a torrent, fast resume
                // data will be loaded after the metadata has been downloaded. 
                AutoSaveLoadFastResume = true,

                // If a MagnetLink is used to download a torrent, the engine will try to load a copy of the metadata
                // it's cache directory. Otherwise the metadata will be downloaded and stored in the cache directory
                // so it can be reloaded later.
                AutoSaveLoadMagnetLinkMetadata = true,
                CacheDirectory = this.local_data.temp_folder,

                // Use a fixed port to accept incoming connections from other peers.
                ListenPort = this.torrent_port,

                // Use a random port for DHT communications.
                //DhtPort = this.torrent_port,
                AllowedEncryption = new List<EncryptionType>() { EncryptionType.PlainText, EncryptionType.RC4Full, EncryptionType.RC4Header } //EncryptionType.PlainText, EncryptionType.RC4Full, EncryptionType.RC4Header
            };

Then i add the peer manually using:


            Peer peer = new Peer(new MonoTorrent.BEncoding.BEncodedString(random_id), uri, new List<EncryptionType> { EncryptionType.PlainText });

            foreach(TorrentManager manager in this.Engine.Torrents) {
                manager.AddPeerAsync(peer);
            }
ghost commented 3 years ago

Don't know if it is pertinent but I'm trying to connect two machines on a local network, and I'm using the external ip address with the same port number on both machines, which seemed to work earlier on before i changed something in the code, (the external IP addresses were routed to there respective internal IP addresses but it didn't connect, now it doesn't seem to happen and both machines try to connect to the external IP address unsuccessfully (i get: connection failed EncryptionNegiotiationFailed, which i assume is because either machine is not reaching the other one and is possibly trying to connect to itself? Because i have the same allowed encryption on both sides) then it continues trying to connect to seemingly random IP addresses (but the same ip addresses every time) and succeeds in connecting to some (I'm assuming this is DHT i have no idea why this is happening).

ghost commented 3 years ago

I've managed to connect the two peers but I'm still getting attempts at connecting to other IPs that I'm not adding as peers:

Connection failed: http://89.17.132.197:55123/ - EncryptionNegiotiationFailed (my IP) Connection failed: ipv4://89.17.132.197:55123/ - HandshakeFailed Connection failed: ipv4://89.17.132.197:55123/ - HandshakeFailed Connection failed: ipv4://89.17.132.197:55123/ - HandshakeFailed Connection failed: ipv4://89.17.132.197:55123/ - HandshakeFailed Connection failed: ipv4://89.17.132.197:55123/ - HandshakeFailed Thread started: #21 Connection failed: ipv4://89.17.132.197:55123/ - HandshakeFailed Connection succeeded: ipv4://89.17.132.197:50340/ (connection succeeds on my IP different port, I'm assuming MonoTorrent is smart enough to assign different ports to each peer to enable connection?) Connection failed: ipv4://54.215.207.56:7277/ - HandshakeFailed Connection failed: ipv4://54.215.207.56:7277/ - HandshakeFailed

Although the torrent doesn't seed, shouldn't it be enough to assign the directory where file resides like so:

        TorrentManager manager = await this.Engine.AddAsync(magnet, save_directory);

where save_directory is the directory where the file contained in the torrent resides, and then to do manager.StartAsync(); ?

ghost commented 3 years ago

TorrentSettingsBuilder settings_builder = new TorrentSettingsBuilder { AllowDht = false, AllowInitialSeeding = true, CreateContainingDirectory = false
}; TorrentManager manager = await this.Engine.AddAsync(magnet, save_directory, settings_builder.ToSettings());

this solved my problem