Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This project lacks documentation... #650

Open
Matszwe02 opened this issue Jan 30, 2024 · 0 comments
Open

This project lacks documentation... #650

Matszwe02 opened this issue Jan 30, 2024 · 0 comments

Comments

@Matszwe02
Copy link

So here's my implementation that I came up so far:
(I hope it will help someone)
And oh, that's a github issue, so seeding doesn't work ouside my local network, can you help me figure it out why?

    public class TorrentClient
    {
        public string[] announces = ["udp://tracker.openbittorrent.com:80/announce"];
        public string magnetLink = "";
        private List<PeerId> peers = [];
        private TorrentManager torrentManager;
        private Tuple<BEncodedDictionary, bool> encodedDict = new(new(), false);
        public bool isMine = false;
         MonoTorrent.Client.ClientEngine clientEngine;
        TorrentSettings torrentSettings;

        public TorrentClient()
        {
            GenerateClient();
        }
        
        
        private void GenerateClient()
        {
            TorrentSettingsBuilder builder = new()
            {
                MaximumConnections = 500,
                AllowInitialSeeding = true,
                AllowDht = true,
                CreateContainingDirectory = false,
            };
            EngineSettingsBuilder engineSettingsBuilder = new()
            {
                AllowPortForwarding = true,
                // AllowedEncryption = [MonoTorrent.Client.EncryptionType.PlainText, MonoTorrent.Client.EncryptionType.RC4Header, MonoTorrent.Client.EncryptionType.RC4Full],
                ConnectionTimeout = TimeSpan.FromSeconds(15)
            };
            
            MonoTorrent.Client.EngineSettings settings = engineSettingsBuilder.ToSettings();
            clientEngine = new(settings);
            torrentSettings = builder.ToSettings();
        }


        public bool isActive()
        {
            return !(torrentManager?.State == null);
        }


        public double GetPercentComplete()
        {
            if (torrentManager?.State == null) return -1;
            return torrentManager.PartialProgress;
        }


        private async void GetPeers()
        {
            if (torrentManager?.State == null) return;
            peers = await torrentManager.GetPeersAsync();
        }


        public int GetPeerCount()
        {
            GetPeers();
            if (torrentManager?.State == null) return -1;
            return peers.Count;
        }


        public string GetPeersInfo()
        {
            if (torrentManager?.State == null) return "TorrentManager error";
            string output = "";
            foreach (PeerId peer in peers)
            {
                output += peer.EncryptionType.ToString();
                output += "\t";
                output += peer.ClientApp.Client;
                output += "\t";
                output += peer.Uri.AbsoluteUri;
                output += "\n";
            }
            return output;
        }


        public string GetTorrentStatus()
        {
            if (torrentManager?.State == null) return "TorrentManager error";
            return torrentManager.State.ToString();
        }        
        

        public float GetDownloadSpeed()
        {
            if (torrentManager?.State == null) return -1;
            return torrentManager.Monitor.DownloadSpeed / 1000;
        }
        public float GetUploadSpeed()
        {
            if (torrentManager?.State == null) return -1;
            return torrentManager.Monitor.UploadSpeed / 1000;
        }
        
        
        public string GetTorrentName()
        {
            var name = torrentManager?.Torrent?.Name;
            if (name == null) return "Error";
            return name;
        }
        
        
        public void CreateTorrentFile(string path)
        {
            if (encodedDict.Item2 == true)
            {
                File.WriteAllBytes(path, encodedDict.Item1.Encode());
            }
        }


        public async void CreateTorrent(string path, string name)
        {
            ITorrentFileSource fileSource = new TorrentFileSource(path);

            TorrentCreator creator = new();

            creator.Announces.Add([.. announces]);

            creator.Comment = "torrent";
            creator.Private = false;
            creator.Publisher = "user";

            BEncodedDictionary create = await creator.CreateAsync(fileSource);
            
            encodedDict = new(create, true);
            
            Torrent torrent = Torrent.Load(create);
            MagnetLink magnet = new MagnetLink(torrent.InfoHashes, torrent.Name, announces);
            magnetLink = magnet.ToV1String();
            
            torrentManager = await clientEngine.AddAsync(torrent, System.IO.Path.GetDirectoryName(path), torrentSettings);
            await torrentManager.StartAsync();
            
            isMine = true;
            return;
        }


        public async void DownloadTorrent(string magnet, string downloadPath)
        {
            magnetLink = magnet;
            MagnetLink magnet1 = MagnetLink.FromUri(new Uri(magnet));

            torrentManager = await clientEngine.AddAsync(magnet1, downloadPath, torrentSettings);
            torrentManager.PeerConnected += (o, e) =>
            {
                Debug.WriteLine($"Connection succeeded: {e.Peer.Uri}");
            };

            torrentManager.ConnectionAttemptFailed += (o, e) =>
            {
                Debug.WriteLine($"Connection failed: {e.Peer.ConnectionUri} - {e.Reason}");
            };

            await torrentManager.StartAsync();
            isMine = false;
            return;
        }

    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant