From 931591351968f55f12b99fa5c2cc675405ea1479 Mon Sep 17 00:00:00 2001 From: Wizou <11647984+wiz0u@users.noreply.github.com> Date: Thu, 5 Sep 2024 18:51:53 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20infinite=20recursion=20on=20Dispose=20(#2?= =?UTF-8?q?74)=20=F0=9F=8E=ACTake=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/dev.yml | 2 +- EXAMPLES.md | 2 +- README.md | 6 ++++-- src/Client.cs | 15 +++++++++++---- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/.github/dev.yml b/.github/dev.yml index b994351..0128bc4 100644 --- a/.github/dev.yml +++ b/.github/dev.yml @@ -1,7 +1,7 @@ pr: none trigger: [ master ] -name: 4.1.8-dev.$(Rev:r) +name: 4.1.9-dev.$(Rev:r) pool: vmImage: ubuntu-latest diff --git a/EXAMPLES.md b/EXAMPLES.md index ff86a4d..6fb7ee1 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -504,7 +504,7 @@ These two fields derive from class `Peer` and can be of type `PeerChat`, `PeerCh The root structure where you obtained the message (typically `UpdatesBase` or `Messages_MessagesBase`) inherits from `IPeerResolver`. This allows you to call `.UserOrChat(peer)` on the root structure, in order to resolve those fields into a `User` class, or a `ChatBase`-derived class -(typically `Chat` or `Channel`) which will give you details about the peer, instead of just the ID. +(typically `Chat` or `Channel`) which will give you details about the peer, instead of just the ID, and can be implicitly converted to `InputPeer`. However, in some case _(typically when dealing with updates)_, Telegram might choose to not include details about a peer because it expects you to already know about it (`UserOrChat` returns `null`). diff --git a/README.md b/README.md index 9a6e007..8e10baa 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,15 @@ [![API Layer](https://img.shields.io/badge/API_Layer-186-blueviolet)](https://corefork.telegram.org/methods) [![NuGet version](https://img.shields.io/nuget/v/WTelegramClient?color=00508F)](https://www.nuget.org/packages/WTelegramClient/) [![NuGet prerelease](https://img.shields.io/nuget/vpre/WTelegramClient?color=C09030&label=dev+nuget)](https://www.nuget.org/packages/WTelegramClient/absoluteLatest) -[![Donate](https://img.shields.io/badge/Help_this_project:-Donate-ff4444)](https://www.buymeacoffee.com/wizou) +[![Donate](https://img.shields.io/badge/Help_this_project:-Donate-ff4444)](https://buymeacoffee.com/wizou) ## *_Telegram Client API library written 100% in C# and .NET_* This library allows you to connect to Telegram and control a user programmatically (or a bot, but [WTelegramBot](https://www.nuget.org/packages/WTelegramBot) is much easier for that). All the Telegram Client APIs (MTProto) are supported so you can do everything the user could do with a full Telegram GUI client. +Library was developed solely by one unemployed guy. [Donations are welcome](https://buymeacoffee.com/wizou). + This ReadMe is a **quick but important tutorial** to learn the fundamentals about this library. Please read it all. > ⚠️ This library requires understanding advanced C# techniques such as **asynchronous programming** or **subclass pattern matching**... @@ -202,6 +204,6 @@ as well as the [API Terms of Service](https://core.telegram.org/api/terms) or yo If you read all this ReadMe, the [Frequently Asked Questions](https://wiz0u.github.io/WTelegramClient/FAQ), the [Examples codes](https://wiz0u.github.io/WTelegramClient/EXAMPLES) and still have questions, feedback is welcome in our Telegram group [@WTelegramClient](https://t.me/WTelegramClient) -If you like this library, you can [buy me a coffee](https://www.buymeacoffee.com/wizou) ❤ This will help the project keep going. +If you like this library, you can [buy me a coffee](https://buymeacoffee.com/wizou) ❤ This will help the project keep going. © 2024 Olivier Marcoux diff --git a/src/Client.cs b/src/Client.cs index 5bb497f..3a85c60 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -246,6 +246,7 @@ namespace WTelegram return dcSession; // if we have already a session with this DC and we are connected or it is a perfect match, use it if (dcSession == null && _session.DCSessions.TryGetValue(-dcId, out dcSession) && dcSession.AuthKey != null) { + // we have already negociated an AuthKey with this DC if (dcSession.DataCenter.flags == flags && _session.DCSessions.Remove(-dcId)) return _session.DCSessions[dcId] = dcSession; // we found a misclassed DC, change its sign dcSession = new Session.DCSession { Id = Helpers.RandomLong(), // clone AuthKey for a session on the matching media_only DC @@ -872,9 +873,11 @@ namespace WTelegram var triedEndpoints = new HashSet { endpoint }; if (_session.DcOptions != null) { - var altOptions = _session.DcOptions.Where(dco => dco.id == _dcSession.DataCenter.id && dco.flags != _dcSession.DataCenter.flags - && (dco.flags & (DcOption.Flags.cdn | DcOption.Flags.tcpo_only | DcOption.Flags.media_only)) == 0) - .OrderBy(dco => dco.flags); + var flags = _dcSession.DataCenter.flags; + var altOptions = _session.DcOptions.Where(dc => dc.id == _dcSession.DataCenter.id && dc.flags != flags + && (dc.flags & DcOption.Flags.media_only) <= (flags & DcOption.Flags.media_only) + && (dc.flags & (DcOption.Flags.cdn | DcOption.Flags.tcpo_only)) == 0) + .OrderBy(dc => (dc.flags ^ flags) & DcOption.Flags.media_only).ThenBy(dc => dc.flags ^ flags); // try alternate addresses for this DC foreach (var dcOption in altOptions) { @@ -884,7 +887,11 @@ namespace WTelegram try { tcpClient = await TcpHandler(endpoint.Address.ToString(), endpoint.Port); - _dcSession.DataCenter = dcOption; + if (((dcOption.flags ^ flags) & DcOption.Flags.media_only) == 0) // test to prevent AltDC becoming MainDC + _dcSession.DataCenter = dcOption; + else + _dcSession.DataCenter = new DcOption { flags = dcOption.flags ^ DcOption.Flags.media_only, + id = dcOption.id, ip_address = dcOption.ip_address, port = dcOption.port, secret = dcOption.secret }; break; } catch (SocketException) { }