2021-08-04 00:40:09 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
2021-08-06 07:28:54 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Security.Cryptography;
|
2021-08-04 00:40:09 +02:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
|
|
|
|
|
|
|
namespace WTelegram
|
|
|
|
|
|
{
|
|
|
|
|
|
internal class Session
|
|
|
|
|
|
{
|
|
|
|
|
|
public long AuthKeyID;
|
|
|
|
|
|
public byte[] AuthKey;
|
|
|
|
|
|
public long Salt;
|
|
|
|
|
|
public long Id;
|
|
|
|
|
|
public int Seqno;
|
|
|
|
|
|
public long ServerTicksOffset;
|
|
|
|
|
|
public long LastSentMsgId;
|
|
|
|
|
|
public TL.DcOption DataCenter;
|
2021-08-06 07:28:54 +02:00
|
|
|
|
public byte[] User; // serialization of TL.User
|
2021-08-04 00:40:09 +02:00
|
|
|
|
|
|
|
|
|
|
public DateTime SessionStart => _sessionStart;
|
|
|
|
|
|
private readonly DateTime _sessionStart = DateTime.UtcNow;
|
|
|
|
|
|
private string _pathname;
|
2021-08-06 07:28:54 +02:00
|
|
|
|
private byte[] _apiHash; // used as AES key for encryption of session file
|
2021-08-04 00:40:09 +02:00
|
|
|
|
|
2021-08-06 07:28:54 +02:00
|
|
|
|
internal static Session LoadOrCreate(string pathname, byte[] apiHash)
|
2021-08-04 00:40:09 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (File.Exists(pathname))
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2021-08-06 07:28:54 +02:00
|
|
|
|
var session = Load(pathname, apiHash);
|
2021-08-04 00:40:09 +02:00
|
|
|
|
session._pathname = pathname;
|
2021-08-06 07:28:54 +02:00
|
|
|
|
session._apiHash = apiHash;
|
2021-08-04 10:11:07 +02:00
|
|
|
|
Helpers.Log(2, "Loaded previous session");
|
2021-08-04 00:40:09 +02:00
|
|
|
|
return session;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2021-08-06 07:28:54 +02:00
|
|
|
|
throw new ApplicationException($"Exception while reading session file: {ex.Message}\nDelete the file to start a new session", ex);
|
2021-08-04 00:40:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-08-06 07:28:54 +02:00
|
|
|
|
return new Session { _pathname = pathname, _apiHash = apiHash, Id = Helpers.RandomLong() };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal static Session Load(string pathname, byte[] apiHash)
|
|
|
|
|
|
{
|
|
|
|
|
|
var input = File.ReadAllBytes(pathname);
|
|
|
|
|
|
using var aes = Aes.Create();
|
|
|
|
|
|
using var decryptor = aes.CreateDecryptor(apiHash, input[0..16]);
|
|
|
|
|
|
var utf8Json = decryptor.TransformFinalBlock(input, 16, input.Length - 16);
|
|
|
|
|
|
if (!SHA256.HashData(utf8Json.AsSpan(32)).SequenceEqual(utf8Json[0..32]))
|
|
|
|
|
|
throw new ApplicationException("Integrity check failed in session loading");
|
|
|
|
|
|
return JsonSerializer.Deserialize<Session>(utf8Json.AsSpan(32), Helpers.JsonOptions);
|
2021-08-04 00:40:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal void Save()
|
|
|
|
|
|
{
|
2021-08-06 07:28:54 +02:00
|
|
|
|
var utf8Json = JsonSerializer.SerializeToUtf8Bytes(this, Helpers.JsonOptions);
|
|
|
|
|
|
var finalBlock = new byte[16];
|
2021-08-10 08:25:37 +02:00
|
|
|
|
var output = new byte[(16 + 32 + utf8Json.Length + 16) & ~15];
|
2021-08-06 07:28:54 +02:00
|
|
|
|
Encryption.RNG.GetBytes(output, 0, 16);
|
|
|
|
|
|
using var aes = Aes.Create();
|
|
|
|
|
|
using var encryptor = aes.CreateEncryptor(_apiHash, output[0..16]);
|
|
|
|
|
|
encryptor.TransformBlock(SHA256.HashData(utf8Json), 0, 32, output, 16);
|
|
|
|
|
|
encryptor.TransformBlock(utf8Json, 0, utf8Json.Length & ~15, output, 48);
|
|
|
|
|
|
utf8Json.AsSpan(utf8Json.Length & ~15).CopyTo(finalBlock);
|
|
|
|
|
|
encryptor.TransformFinalBlock(finalBlock, 0, utf8Json.Length & 15).CopyTo(output.AsMemory(48 + utf8Json.Length & ~15));
|
|
|
|
|
|
File.WriteAllBytes(_pathname, output);
|
2021-08-04 00:40:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal (long msgId, int seqno) NewMsg(bool isContent)
|
|
|
|
|
|
{
|
|
|
|
|
|
long msgId = DateTime.UtcNow.Ticks + ServerTicksOffset - 621355968000000000L;
|
|
|
|
|
|
msgId = msgId * 428 + (msgId >> 24) * 25110956; // approximately unixtime*2^32 and divisible by 4
|
|
|
|
|
|
if (msgId <= LastSentMsgId) msgId = LastSentMsgId += 4; else LastSentMsgId = msgId;
|
|
|
|
|
|
|
|
|
|
|
|
int seqno = isContent ? Seqno++ * 2 + 1 : Seqno * 2;
|
|
|
|
|
|
Save();
|
|
|
|
|
|
return (msgId, seqno);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal DateTime MsgIdToStamp(long serverMsgId)
|
|
|
|
|
|
=> new((serverMsgId >> 32) * 10000000 - ServerTicksOffset + 621355968000000000L, DateTimeKind.Utc);
|
2021-08-06 07:28:54 +02:00
|
|
|
|
|
|
|
|
|
|
internal void Reset(TL.DcOption newDC = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
DataCenter = newDC;
|
|
|
|
|
|
AuthKeyID = Salt = Seqno = 0;
|
|
|
|
|
|
AuthKey = User = null;
|
|
|
|
|
|
}
|
2021-08-04 00:40:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|