diff --git a/src/TgSharp.Core/FileSessionStore.cs b/src/TgSharp.Core/FileSessionStore.cs new file mode 100644 index 0000000..071656d --- /dev/null +++ b/src/TgSharp.Core/FileSessionStore.cs @@ -0,0 +1,122 @@ +using System; +using System.IO; + +using TgSharp.TL; +using TgSharp.Core.MTProto; +using TgSharp.Core.MTProto.Crypto; + +namespace TgSharp.Core +{ + public class FileSessionStore : ISessionStore + { + private readonly DirectoryInfo basePath; + + public FileSessionStore (DirectoryInfo basePath = null) + { + if (basePath != null && !basePath.Exists) { + throw new ArgumentException ("basePath doesn't exist", nameof (basePath)); + } + this.basePath = basePath; + } + + public void Save (Session session) + { + string sessionFileName = $"{session.SessionUserId}.dat"; + var sessionPath = basePath == null ? sessionFileName : + Path.Combine (basePath.FullName, sessionFileName); + + using (var stream = new FileStream (sessionPath, FileMode.OpenOrCreate)) { + var result = ToBytes (session); + stream.Write (result, 0, result.Length); + } + } + + public Session Load (string sessionUserId) + { + string sessionFileName = $"{sessionUserId}.dat"; + var sessionPath = basePath == null ? sessionFileName : + Path.Combine (basePath.FullName, sessionFileName); + + if (!File.Exists (sessionPath)) + return null; + + using (var stream = new FileStream (sessionPath, FileMode.Open)) { + var buffer = new byte [2048]; + stream.Read (buffer, 0, 2048); + + return FromBytes (buffer, this, sessionUserId); + } + } + + public static Session FromBytes (byte [] buffer, ISessionStore store, string sessionUserId) + { + using (var stream = new MemoryStream (buffer)) + 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 (); + var serverAddress = Serializers.String.Read (reader); + var port = reader.ReadInt32 (); + + var doesAuthExist = reader.ReadInt32 () == 1; + int sessionExpires = 0; + TLUser TLUser = null; + if (doesAuthExist) { + sessionExpires = reader.ReadInt32 (); + TLUser = (TLUser)ObjectUtils.DeserializeObject (reader); + } + + var authData = Serializers.Bytes.Read (reader); + var defaultDataCenter = new DataCenter (serverAddress, port); + + return new Session () { + AuthKey = new AuthKey (authData), + Id = id, + Salt = salt, + Sequence = sequence, + LastMessageId = lastMessageId, + TimeOffset = timeOffset, + SessionExpires = sessionExpires, + TLUser = TLUser, + SessionUserId = sessionUserId, + DataCenter = defaultDataCenter, + }; + } + } + + internal byte [] ToBytes (Session session) + { + using (var stream = new MemoryStream ()) + using (var writer = new BinaryWriter (stream)) { + writer.Write (session.Id); + writer.Write (session.Sequence); + writer.Write (session.Salt); + writer.Write (session.LastMessageId); + writer.Write (session.TimeOffset); + Serializers.String.Write (writer, session.DataCenter.Address); + writer.Write (session.DataCenter.Port); + + if (session.TLUser != null) { + writer.Write (1); + writer.Write (session.SessionExpires); + ObjectUtils.SerializeObject (session.TLUser, writer); + } else { + writer.Write (0); + } + + Serializers.Bytes.Write (writer, session.AuthKey.Data); + + return stream.ToArray (); + } + } + } +} diff --git a/src/TgSharp.Core/MTProto/Serializers.cs b/src/TgSharp.Core/MTProto/Serializers.cs index 2cec1e3..c69ccad 100644 --- a/src/TgSharp.Core/MTProto/Serializers.cs +++ b/src/TgSharp.Core/MTProto/Serializers.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; + using System.IO; using System.Text; @@ -6,7 +6,6 @@ namespace TgSharp.Core.MTProto { public class Serializers { - public static class Bytes { public static byte[] Read(BinaryReader binaryReader) diff --git a/src/TgSharp.Core/Session.cs b/src/TgSharp.Core/Session.cs index a18d0a0..93db017 100644 --- a/src/TgSharp.Core/Session.cs +++ b/src/TgSharp.Core/Session.cs @@ -13,51 +13,6 @@ namespace TgSharp.Core Session Load(string sessionUserId); } - public class FileSessionStore : ISessionStore - { - private readonly DirectoryInfo basePath; - - public FileSessionStore(DirectoryInfo basePath = null) - { - if (basePath != null && !basePath.Exists) - { - throw new ArgumentException("basePath doesn't exist", nameof(basePath)); - } - this.basePath = basePath; - } - - public void Save(Session session) - { - string sessionFileName = $"{session.SessionUserId}.dat"; - var sessionPath = basePath == null ? sessionFileName : - Path.Combine(basePath.FullName, sessionFileName); - - using (var stream = new FileStream(sessionPath, FileMode.OpenOrCreate)) - { - var result = session.ToBytes(); - stream.Write(result, 0, result.Length); - } - } - - public Session Load(string sessionUserId) - { - string sessionFileName = $"{sessionUserId}.dat"; - var sessionPath = basePath == null ? sessionFileName : - Path.Combine(basePath.FullName, sessionFileName); - - if (!File.Exists(sessionPath)) - return null; - - using (var stream = new FileStream(sessionPath, FileMode.Open)) - { - var buffer = new byte[2048]; - stream.Read(buffer, 0, 2048); - - return Session.FromBytes(buffer, this, sessionUserId); - } - } - } - public class FakeSessionStore : ISessionStore { public void Save(Session session) @@ -104,7 +59,7 @@ namespace TgSharp.Core = CurrentTime (); // this is similar to the unixTime but rooted on the worst year of humanity instead of 1970 - private static int CurrentTime () + internal static int CurrentTime () { return (int)DateTime.UtcNow.Subtract (new DateTime (2020, 1, 1)).TotalSeconds; } @@ -126,84 +81,6 @@ namespace TgSharp.Core random = new Random(); } - public byte[] ToBytes() - { - using (var stream = new MemoryStream()) - using (var writer = new BinaryWriter(stream)) - { - writer.Write(Id); - writer.Write(Sequence); - writer.Write(Salt); - writer.Write(LastMessageId); - writer.Write(TimeOffset); - Serializers.String.Write(writer, DataCenter.Address); - writer.Write(DataCenter.Port); - - if (TLUser != null) - { - writer.Write(1); - writer.Write(SessionExpires); - ObjectUtils.SerializeObject(TLUser, writer); - } - else - { - writer.Write(0); - } - - Serializers.Bytes.Write(writer, AuthKey.Data); - - return stream.ToArray(); - } - } - - public static Session FromBytes(byte[] buffer, ISessionStore store, string sessionUserId) - { - using (var stream = new MemoryStream(buffer)) - 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 = CurrentTime(); -#endif - - var salt = reader.ReadUInt64(); - var lastMessageId = reader.ReadInt64(); - var timeOffset = reader.ReadInt32(); - var serverAddress = Serializers.String.Read(reader); - var port = reader.ReadInt32(); - - var isAuthExsist = reader.ReadInt32() == 1; - int sessionExpires = 0; - TLUser TLUser = null; - if (isAuthExsist) - { - sessionExpires = reader.ReadInt32(); - TLUser = (TLUser)ObjectUtils.DeserializeObject(reader); - } - - var authData = Serializers.Bytes.Read(reader); - var defaultDataCenter = new DataCenter (serverAddress, port); - - return new Session() - { - AuthKey = new AuthKey(authData), - Id = id, - Salt = salt, - Sequence = sequence, - LastMessageId = lastMessageId, - TimeOffset = timeOffset, - SessionExpires = sessionExpires, - TLUser = TLUser, - SessionUserId = sessionUserId, - DataCenter = defaultDataCenter, - }; - } - } - public long GetNewMessageId() { long time = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds); diff --git a/src/TgSharp.Core/TgSharp.Core.csproj b/src/TgSharp.Core/TgSharp.Core.csproj index 799b3e8..d677b52 100644 --- a/src/TgSharp.Core/TgSharp.Core.csproj +++ b/src/TgSharp.Core/TgSharp.Core.csproj @@ -79,6 +79,7 @@ + diff --git a/src/TgSharp.TL/ObjectDeserializer.cs b/src/TgSharp.TL/ObjectDeserializer.cs index 564a927..795bc26 100644 --- a/src/TgSharp.TL/ObjectDeserializer.cs +++ b/src/TgSharp.TL/ObjectDeserializer.cs @@ -1,9 +1,5 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace TgSharp.TL {