From e2323092dc97f8e0d56681ec9df7e99be8afd6f3 Mon Sep 17 00:00:00 2001 From: Wizou <11647984+wiz0u@users.noreply.github.com> Date: Tue, 26 Mar 2024 12:07:03 +0100 Subject: [PATCH] net8.0 target, compatible with AOT --- src/Client.cs | 2 +- src/Compat.cs | 7 +++++++ src/Encryption.cs | 6 +++--- src/Helpers.cs | 10 ++++++++++ src/TL.cs | 8 ++++---- src/WTelegramClient.csproj | 3 ++- 6 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/Client.cs b/src/Client.cs index 046ddd4..e5da354 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -918,7 +918,7 @@ namespace WTelegram TLConfig = new Config { this_dc = _session.MainDC, dc_options = _session.DcOptions }; else { - var initParams = JSONValue.FromJsonElement(System.Text.Json.JsonSerializer.Deserialize(Config("init_params"))); + var initParams = JSONValue.FromJsonElement(System.Text.Json.JsonDocument.Parse(Config("init_params")).RootElement); TLConfig = await this.InvokeWithLayer(Layer.Version, new TL.Methods.InitConnection { diff --git a/src/Compat.cs b/src/Compat.cs index b83e832..53d54b4 100644 --- a/src/Compat.cs +++ b/src/Compat.cs @@ -76,6 +76,13 @@ static class Convert internal static string ToHexString(byte[] data) => BitConverter.ToString(data).Replace("-", ""); internal static byte[] FromHexString(string hex) => Enumerable.Range(0, hex.Length / 2).Select(i => System.Convert.ToByte(hex.Substring(i * 2, 2), 16)).ToArray(); } +public class RandomNumberGenerator +{ + internal static readonly RNGCryptoServiceProvider RNG = new(); + public static RandomNumberGenerator Create() => new(); + public void GetBytes(byte[] data) => RNG.GetBytes(data); + public void GetBytes(byte[] data, int offset, int count) => RNG.GetBytes(data, offset, count); +} #endif #if NETSTANDARD2_0 diff --git a/src/Encryption.cs b/src/Encryption.cs index 4c49f5e..d943e5d 100644 --- a/src/Encryption.cs +++ b/src/Encryption.cs @@ -16,7 +16,7 @@ namespace WTelegram internal static class Encryption { private static readonly Dictionary PublicKeys = []; - internal static readonly RNGCryptoServiceProvider RNG = new(); + internal static readonly RandomNumberGenerator RNG = RandomNumberGenerator.Create(); internal static readonly Aes AesECB = Aes.Create(); static Encryption() @@ -33,7 +33,7 @@ namespace WTelegram var sha256 = SHA256.Create(); //1) - var nonce = new Int128(RNG); + var nonce = new TL.Int128(RNG); var resPQ = await client.ReqPqMulti(nonce); //2) if (resPQ.nonce != nonce) throw new WTException("Nonce mismatch"); @@ -164,7 +164,7 @@ namespace WTelegram session.Salt = BinaryPrimitives.ReadInt64LittleEndian(pqInnerData.new_nonce.raw) ^ BinaryPrimitives.ReadInt64LittleEndian(resPQ.server_nonce.raw); session.OldSalt = session.Salt; - (byte[] key, byte[] iv) ConstructTmpAESKeyIV(Int128 server_nonce, Int256 new_nonce) + (byte[] key, byte[] iv) ConstructTmpAESKeyIV(TL.Int128 server_nonce, Int256 new_nonce) { byte[] tmp_aes_key = new byte[32], tmp_aes_iv = new byte[32]; sha1.TransformBlock(new_nonce, 0, 32, null, 0); diff --git a/src/Helpers.cs b/src/Helpers.cs index f1569c0..e76c116 100644 --- a/src/Helpers.cs +++ b/src/Helpers.cs @@ -7,6 +7,13 @@ using System.Text.Json; using System.Threading; using System.Threading.Tasks; +#if NET8_0_OR_GREATER +using System.Text.Json.Serialization; +using System.Text.Json.Serialization.Metadata; +[JsonSerializable(typeof(WTelegram.Session))] +internal partial class WTelegramContext : JsonSerializerContext { } +#endif + namespace WTelegram { public static class Helpers @@ -16,6 +23,9 @@ namespace WTelegram /// For serializing indented Json with fields included public static readonly JsonSerializerOptions JsonOptions = new() { IncludeFields = true, WriteIndented = true, +#if NET8_0_OR_GREATER + TypeInfoResolver = JsonSerializer.IsReflectionEnabledByDefault ? new DefaultJsonTypeInfoResolver() : WTelegramContext.Default, +#endif IgnoreReadOnlyProperties = true, DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull }; private static readonly ConsoleColor[] LogLevelToColor = [ ConsoleColor.DarkGray, ConsoleColor.DarkCyan, diff --git a/src/TL.cs b/src/TL.cs index 90e25fc..6d4541a 100644 --- a/src/TL.cs +++ b/src/TL.cs @@ -328,8 +328,8 @@ namespace TL { public byte[] raw; - public Int128(System.IO.BinaryReader reader) => raw = reader.ReadBytes(16); - public Int128(RNGCryptoServiceProvider rng) => rng.GetBytes(raw = new byte[16]); + public Int128(BinaryReader reader) => raw = reader.ReadBytes(16); + public Int128(RandomNumberGenerator rng) => rng.GetBytes(raw = new byte[16]); public static bool operator ==(Int128 left, Int128 right) { for (int i = 0; i < 16; i++) if (left.raw[i] != right.raw[i]) return false; return true; } public static bool operator !=(Int128 left, Int128 right) { for (int i = 0; i < 16; i++) if (left.raw[i] != right.raw[i]) return true; return false; } public override readonly bool Equals(object obj) => obj is Int128 other && this == other; @@ -342,8 +342,8 @@ namespace TL { public byte[] raw; - public Int256(System.IO.BinaryReader reader) => raw = reader.ReadBytes(32); - public Int256(RNGCryptoServiceProvider rng) => rng.GetBytes(raw = new byte[32]); + public Int256(BinaryReader reader) => raw = reader.ReadBytes(32); + public Int256(RandomNumberGenerator rng) => rng.GetBytes(raw = new byte[32]); public static bool operator ==(Int256 left, Int256 right) { for (int i = 0; i < 32; i++) if (left.raw[i] != right.raw[i]) return false; return true; } public static bool operator !=(Int256 left, Int256 right) { for (int i = 0; i < 32; i++) if (left.raw[i] != right.raw[i]) return true; return false; } public override readonly bool Equals(object obj) => obj is Int256 other && this == other; diff --git a/src/WTelegramClient.csproj b/src/WTelegramClient.csproj index 25acdfb..d2b078c 100644 --- a/src/WTelegramClient.csproj +++ b/src/WTelegramClient.csproj @@ -2,7 +2,7 @@ Library - netstandard2.0;net5.0 + netstandard2.0;net5.0;net8.0 latest WTelegram true @@ -26,6 +26,7 @@ $(ReleaseNotes.Replace("|", "%0D%0A").Replace(" - ","%0D%0A- ").Replace(" ", "%0D%0A%0D%0A")) 0419;1573;1591;NETSDK1138 TRACE;OBFUSCATION +