SuRGeoNix / BitSwarm

Bittorrent library for clients & streaming purposes
GNU Lesser General Public License v3.0
109 stars 8 forks source link

HttpClient Options #13

Closed Wiilf closed 3 years ago

Wiilf commented 3 years ago

Added some options our other downloader classes uses, transfers work fine. May need tuning. C#9

Tracker.cs

using System.Security.Authentication;

static Tracker()
        {
            // Allowing Untrusted SSL Certificates with HttpClient
            var handler = new HttpClientHandler()
            {
                SslProtocols = SslProtocols.Tls12,
                AllowAutoRedirect = true,
                AutomaticDecompression = DecompressionMethods.GZip,
                CookieContainer = new CookieContainer()
            };
            using var httpClient = new HttpClient(handler);
            ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
            httpClient.Timeout = new TimeSpan(0, 0, 30); // TODO: Options?
            httpClient.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("BitSwarm", BitSwarm.Version));
            key = rnd.Next(1, Int32.MaxValue);
        }
        public async void AnnounceTCP(Int32 num_want = -1, Int64 downloaded = 0, Int64 left = 0, Int64 uploaded = 0)
        {
            try
            {
                // Options?
                var handler = new HttpClientHandler()
                {
                    SslProtocols = SslProtocols.Tls12,
                    AllowAutoRedirect = true,
                    AutomaticDecompression = DecompressionMethods.GZip,
                    CookieContainer = new CookieContainer()
                };
                using var httpClient = new HttpClient(handler);
                string query = !string.IsNullOrEmpty(uri.Query) ? "&" : "?";
                query += $"info_hash={Utils.StringHexToUrlEncode(options.InfoHash)}&peer_id={Utils.StringHexToUrlEncode(BitConverter.ToString(options.PeerId).Replace("-", ""))}&port=11111&left={left}&downloaded={downloaded}&uploaded={uploaded}&event=started&compact=1&numwant={num_want}&key={key:x}";
Wiilf commented 3 years ago

Confirmed, this change alone has my private torrents connecting with no problems! I suggest the above changes to the main repo, due to the fact that a lot of trackers are using reverse-DNS (Cloudflare, FreeDNS(afraid.org), DynDNS, etc).

I've used this as well

httpClient.DefaultRequestHeaders.ConnectionClose = false;
SuRGeoNix commented 3 years ago

Not sure why you use different httpClient everytime. I've updated the repo but with the same httpClient, let me know if that is causing any issues

Wiilf commented 2 years ago

Following your existing code. With those changes, although should be called with a procedure imo broke user agent reporting. NULL.

I've revised and separated some logic. Working as expected.

*Additionally, and separately - this codebase needs to see uploading capability right away. This is a grand staple of BitTorrent.

We will work to see this implemented, will not be easy I think due to limited skill in this particular language.

SuRGeoNix commented 2 years ago

@Wiilf, I find it difficult to understand you. So, with the current implementation user agent is not working?

"I've revised and separated some logic. Working as expected."

So you had to do some changes to fix it and if yes can you create PR or share the fix?

"*Additionally, and separately - this codebase needs to see uploading capability right away. This is a grand staple of BitTorrent."

You mean that uploading/seeding is important to implement? I agree, however that requires time effort that I currently don't have.