From 3bda15197883e8171f1445b3f630d4cd9e9134bd Mon Sep 17 00:00:00 2001 From: Afshin Arani Date: Wed, 12 May 2021 17:18:47 +0430 Subject: [PATCH 1/2] Core: don't load seqNo and sessionId on start This commit randomizes SeqNo and SessionID on start this prevents some errors (code=32) but It doesn't remove these fields from Session class to keep compatibility with older session files. --- src/TgSharp.Core/Session.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/TgSharp.Core/Session.cs b/src/TgSharp.Core/Session.cs index c20cdd56..b8e04fd5 100644 --- a/src/TgSharp.Core/Session.cs +++ b/src/TgSharp.Core/Session.cs @@ -4,6 +4,7 @@ using TgSharp.TL; using TgSharp.Core.MTProto; using TgSharp.Core.MTProto.Crypto; +using System.Security.Cryptography; namespace TgSharp.Core { @@ -39,6 +40,10 @@ internal static Session TryLoadOrCreateNew (ISessionStore store, string sessionU DataCenter = defaultDataCenter, }; } + session.Sequence = 0; + byte[] randomSessionId = new byte[8]; + RandomNumberGenerator.Create().GetNonZeroBytes(randomSessionId); + session.Id = BitConverter.ToUInt64(randomSessionId, 0); return session; } From d38bc5f2fae16382e9b6fa0e5ea2b14e546dc60a Mon Sep 17 00:00:00 2001 From: Afshin Arani Date: Wed, 12 May 2021 18:03:25 +0430 Subject: [PATCH 2/2] Core: remove ci hack (partial revert of cec35c6) This hack is no longer because we no longer save sequence number. --- .github/workflows/CI.yml | 2 +- src/TgSharp.Core/FileSessionStore.cs | 7 ------- src/TgSharp.Core/Session.cs | 12 ------------ src/TgSharp.Core/TgSharp.Core.csproj | 3 --- 4 files changed, 1 insertion(+), 23 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 984b7e2a..22ab19d4 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -60,7 +60,7 @@ jobs: - name: Run Tests run: | - MSBuild -p:Configuration=Debug -p:CI=true src/TgSharp.sln + MSBuild -p:Configuration=Debug src/TgSharp.sln certutil -decode encodedSession.dat src/TgSharp.Tests.NUnit/bin/Debug/session.dat cp app.config src/TgSharp.Tests.NUnit/bin/Debug/TgSharp.Tests.NUnit.dll.config diff --git a/src/TgSharp.Core/FileSessionStore.cs b/src/TgSharp.Core/FileSessionStore.cs index 071656df..c77bee54 100644 --- a/src/TgSharp.Core/FileSessionStore.cs +++ b/src/TgSharp.Core/FileSessionStore.cs @@ -54,13 +54,6 @@ public static Session FromBytes (byte [] buffer, ISessionStore store, string ses using (var reader = new BinaryReader (stream)) { var id = reader.ReadUInt64 (); var sequence = reader.ReadInt32 (); - - // we do this in CI when running tests so that the they can always use a - // higher sequence than previous run -#if CI - sequence = Session.CurrentTime(); -#endif - var salt = reader.ReadUInt64 (); var lastMessageId = reader.ReadInt64 (); var timeOffset = reader.ReadInt32 (); diff --git a/src/TgSharp.Core/Session.cs b/src/TgSharp.Core/Session.cs index b8e04fd5..b803c17e 100644 --- a/src/TgSharp.Core/Session.cs +++ b/src/TgSharp.Core/Session.cs @@ -60,18 +60,6 @@ public class Session internal object Lock = new object (); public int Sequence { get; set; } -#if CI - // see the same CI-wrapped assignment in .FromBytes(), but this one will become useful - // when we generate a new session.dat for CI again - = CurrentTime (); - - // this is similar to the unixTime but rooted on the worst year of humanity instead of 1970 - internal static int CurrentTime () - { - return (int)DateTime.UtcNow.Subtract (new DateTime (2020, 1, 1)).TotalSeconds; - } -#endif - public string SessionUserId { get; set; } internal DataCenter DataCenter { get; set; } public AuthKey AuthKey { get; set; } diff --git a/src/TgSharp.Core/TgSharp.Core.csproj b/src/TgSharp.Core/TgSharp.Core.csproj index d677b52f..ad18e23d 100644 --- a/src/TgSharp.Core/TgSharp.Core.csproj +++ b/src/TgSharp.Core/TgSharp.Core.csproj @@ -30,9 +30,6 @@ prompt 4 - - $(DefineConstants);CI -