mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
Core: refactor FileSessionStore class
* Move it into its own file. * Fix typo in var name "Exsist" * Move Session.ToBytes() and Session.FromBytes() methods to it.
This commit is contained in:
parent
ef89274e1f
commit
ef499c6fef
122
src/TgSharp.Core/FileSessionStore.cs
Normal file
122
src/TgSharp.Core/FileSessionStore.cs
Normal file
|
|
@ -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 ();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -79,6 +79,7 @@
|
|||
<Compile Include="TelegramClient.cs" />
|
||||
<Compile Include="Utils\Helpers.cs" />
|
||||
<Compile Include="DataCenter.cs" />
|
||||
<Compile Include="FileSessionStore.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue