sochix / TLSharp

Telegram client library implemented in C#
1.01k stars 380 forks source link

Connection problem with emirati number #935

Closed solarin closed 4 years ago

solarin commented 4 years ago

i can't manage to connect with my Emiratis telephone numbers. i tried 2 numbers, both same problem, same exception.

System.IO.EndOfStreamException: Unable to read beyond the end of the stream. at System.IO.BinaryReader.FillBuffer(Int32 numBytes) at System.IO.BinaryReader.ReadInt64() at TLSharp.Core.Network.MtProtoPlainSender.d6.MoveNext() in source\repos\TLSharp\TLSharp.Core\Network\MtProtoPlainSender.cs:line 51 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() at TLSharp.Core.Auth.Authenticator.d0.MoveNext() in source\repos\TLSharp\TLSharp.Core\Auth\Authenticator.cs:line 39 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at TLSharp.Core.TelegramClient.<ConnectAsync>d__13.MoveNext() in source\repos\TLSharp\TLSharp.Core\TelegramClient.cs:line 87 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult() at TLSharp.Core.TelegramClient.<ReconnectToDcAsync>d__14.MoveNext() in source\repos\TLSharp\TLSharp.Core\TelegramClient.cs:line 150 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult() at TLSharp.Core.TelegramClient.<RequestWithDcMigration>d__15.MoveNext() in source\repos\TLSharp\TLSharp.Core\TelegramClient.cs:line 182 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult() at TLSharp.Core.TelegramClient.<SendCodeRequestAsync>d__18.MoveNext() in source\repos\TLSharp\TLSharp.Core\TelegramClient.cs:line 213 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at test_console.Program.d__2.MoveNext() in source\repos\test-console\test-console\Program.cs:line 30

solarin commented 4 years ago

looks like that the big problem is happening with the DC 149.154.165.120

in fact, if i start the software with this default ip address, the migration to a new DC is not requested and the library immediately fails with the above exception

@aarani please help bro! thanks

knocte commented 4 years ago

@solarin can you test this PR: https://github.com/sochix/TLSharp/pull/919 maybe it fixes this problem

solarin commented 4 years ago

@solarin can you test this PR: #919 maybe it fixes this problem

i don't know if it's because i am new to github or because the source repository was deleted, but i can't find it

knocte commented 4 years ago

you could click on the diff and extract it as a patch

solarin commented 4 years ago

you could click on the diff and extract it as a patch

no luck, i have been trying for more than 1 hour, i am not able to download this thing. i give up. if @aarani is so kind to send me a copy, i'll be happy to try.

i even checked "see the repo at this time": https://github.com/sochix/TLSharp/tree/2d9aa26ba22702e64b356e98a1600c189482ce27 but then it can't be downloaded... do you know how to do it?

knocte commented 4 years ago

Download this with curl or wget: https://github.com/sochix/TLSharp/commit/2d9aa26ba22702e64b356e98a1600c189482ce27.patch then apply it to your tree

solarin commented 4 years ago

error: TLSharp.Core/Network/TLClient.cs: already exists in working directory error: patch failed: TLSharp.Core/Network/TcpTransport.cs:1 error: TLSharp.Core/Network/TcpTransport.cs: patch does not apply error: patch failed: TLSharp.Core/TLSharp.Core.csproj:9 error: TLSharp.Core/TLSharp.Core.csproj: patch does not apply error: patch failed: TLSharp.Core/packages.config:2 error: TLSharp.Core/packages.config: patch does not apply error: patch failed: TLSharp.Tests.NUnit/TLSharp.Tests.NUnit.csproj:7 error: TLSharp.Tests.NUnit/TLSharp.Tests.NUnit.csproj: patch does not apply error: patch failed: TLSharp.Tests.VS/TLSharp.Tests.VS.csproj:8 error: TLSharp.Tests.VS/TLSharp.Tests.VS.csproj: patch does not apply error: patch failed: TLSharp.Tests/TLSharp.Tests.csproj:9 error: TLSharp.Tests/TLSharp.Tests.csproj: patch does not apply error: patch failed: TeleSharp.Generator/App.config:1 error: TeleSharp.Generator/App.config: patch does not apply error: patch failed: TeleSharp.Generator/TeleSharp.Generator.csproj:9 error: TeleSharp.Generator/TeleSharp.Generator.csproj: patch does not apply error: patch failed: TeleSharp.TL/TeleSharp.TL.csproj:9 error: TeleSharp.TL/TeleSharp.TL.csproj: patch does not apply

solarin commented 4 years ago

by the way, this problem seems to be connected with DataCenter number 5. maybe also others, i don't know. dc4 is working fine

knocte commented 4 years ago

error: TeleSharp.TL/TeleSharp.TL.csproj: patch does not apply

How are you applying the patch? git am or the patch command?

Anyway, regardless of this, what happens is that the 2d9aa26ba22702e64b356e98a1600c189482ce27 commit needs to be applied into a previous commit, not current master. So, first, checkout this commit in your local clone: f24da2cbf162cba5805fd62c9649998ec4db837e, and after that, apply the patch.

solarin commented 4 years ago

git apply ok let me see

solarin commented 4 years ago

same exception

Furthermore, there is a new bug in this code

   public TcpTransport(string address, int port, TcpClientConnectionHandler handler = null)
        {
            //if (handler == null)
            //{
            var ipAddress = IPAddress.Parse(address);
            tcpClient = new TLClient(ipAddress, port, ReceiveMessage);
            tcpClient.ConnectAsync();
            //}
            //else
            //    tcpClient = handler(address, port);
        }

the call ConnectAsync might not end before someone calls send on the TcpTransport, causing an exception.

solarin commented 4 years ago

everything works if telegram migrates the communication to DC4. With other datacenters it doesn't work. how is it possible that other data centers use a different protcol?

solarin commented 4 years ago

i think i understood why this is happening :D

let me see

solarin commented 4 years ago

image because some data centers only accept media!

solarin commented 4 years ago

yes, filtering the MediaOnly also fixes the problem, but ReconnectToDcAsync should also be fixed because it creates a TcpTransport, then it calls ConnectAsync which basically reloads the session with the old datacenter address and recreates a new TcpTransport.

my proposed solution is that ReconnectToDcAsync does not recreate a TcpTransport, but saves the session after updating the new datacenter. then everything stays as it is.

this is compatible with #939 and/or #937

please @knocte check asap and let's address this boring issue. we need either #939 or #937 for this fix

knocte commented 4 years ago

939? you mean #931 right?

knocte commented 4 years ago

my proposed solution is that ReconnectToDcAsync does not recreate a TcpTransport, but saves the session after updating the new datacenter. then everything stays as it is.

Not sure what you mean by this. When explaining changes to the code, it's better to post a diff than using prose.

filtering the MediaOnly also fixes the problem

You mean this change?:

@@ -111,17 +111,16 @@ namespace TLSharp.Core
             {
                 TLRequestExportAuthorization exportAuthorization = new TLRequestExportAuthorization() { DcId = dcId };
                 exported = await SendRequestAsync<TLExportedAuthorization>(exportAuthorization, token).ConfigureAwait(false);
             }

-            IEnumerable<TLDcOption> dcs;
+            IEnumerable<TLDcOption> dcs =
+                dcOptions.Where (d => d.Id == dcId && !d.MediaOnly);
             if (dcIpVersion == DataCenterIPVersion.OnlyIPv6)
-                dcs = dcOptions.Where(d => d.Id == dcId && d.Ipv6); // selects only ipv6 addresses     
+                dcs = dcs.Where(d => d.Id == dcId && d.Ipv6);
             else if (dcIpVersion == DataCenterIPVersion.OnlyIPv4)
-                dcs = dcOptions.Where(d => d.Id == dcId && !d.Ipv6); // selects only ipv4 addresses
-            else
-                dcs = dcOptions.Where(d => d.Id == dcId); // any
+                dcs = dcs.Where(d => d.Id == dcId && !d.Ipv6);

             TLDcOption dc;
             if (dcIpVersion != DataCenterIPVersion.Default)
             {
                 if (!dcs.Any())
solarin commented 4 years ago

i just added

dcs = dcs.Where(d => !d.MediaOnly)

after all the if-else

but there is also the ReconnectToDcAsync to change. i'll make a pr later. if i am still alive. debugging this thing is more difficult than my project :D