From f339fe11609c1e0429ab7fc42d36817b32040afa Mon Sep 17 00:00:00 2001 From: Wizou <11647984+wiz0u@users.noreply.github.com> Date: Sun, 20 Mar 2022 13:09:25 +0100 Subject: [PATCH] lock sessionStore while updating/writing buffer to store (useful to avoid buffer copy for custom stores) --- .github/dev.yml | 2 +- EXAMPLES.md | 4 ++-- FAQ.md | 4 ++-- src/Session.cs | 25 ++++++++++++++----------- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/.github/dev.yml b/.github/dev.yml index cd48873..c557db8 100644 --- a/.github/dev.yml +++ b/.github/dev.yml @@ -2,7 +2,7 @@ pr: none trigger: - master -name: 2.1.3-dev.$(Rev:r) +name: 2.1.4-dev.$(Rev:r) pool: vmImage: ubuntu-latest diff --git a/EXAMPLES.md b/EXAMPLES.md index 455aa6b..e4f5e71 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -361,7 +361,7 @@ By default, WTelegramClient logs are displayed on the Console screen. If you are not in a Console app or don't want the logs on screen, you can redirect them as you prefer: ```csharp -// • Log to VS Output debugging pane in addition to default Console screen logging: +// • Log to VS Output debugging pane in addition (+=) to default Console screen logging: WTelegram.Helpers.Log += (lvl, str) => System.Diagnostics.Debug.WriteLine(str); // • Log to file in replacement of default Console screen logging, using this static variable: @@ -369,7 +369,7 @@ static StreamWriter WTelegramLogs = new StreamWriter("WTelegram.log", true, Enco ... WTelegram.Helpers.Log = (lvl, str) => WTelegramLogs.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} [{"TDIWE!"[lvl]}] {str}"); -// • In an ASP.NET service, you will typically send logs to a ILogger: +// • In an ASP.NET service, you will typically send logs to an ILogger: WTelegram.Helpers.Log = (lvl, str) => _logger.Log((LogLevel)lvl, str); ``` diff --git a/FAQ.md b/FAQ.md index e357937..179a272 100644 --- a/FAQ.md +++ b/FAQ.md @@ -174,7 +174,7 @@ There are various reasons why you may get this error. Here are the explanation a 1) On secondary DCs *(DC used to download files)*, a Connection shut down is considered "normal" Your main DC is the one WTelegramClient connects to during login. Secondary DC connections are established and maintained when you download files. The DC number for an operation or error is indicated with a prefix like "2>" on the log line. -If Telegram servers decide to shutdown this secondary connection, it's not an issue, WTelegramClient will re-established the connection later if necessary. +If Telegram servers decide to shutdown this secondary connection, it's not an issue, WTelegramClient will re-establish the connection later if necessary. 2) Occasional connection shutdowns on the main DC should be caught by WTelegramClient and the reactor should automatically reconnect to the DC *(up to `MaxAutoReconnects` times)*. @@ -185,7 +185,7 @@ You can choose to increase `MaxAutoReconnects` if it happens too often because y In this case, the recommended action would be to dispose the client and recreate one 4) In case of slow Internet connection or if you break in the debugger for some time, -you might also get Connection shutdown because your client couldn't send Pings to Telegram in the alloted time. +you might also get Connection shutdown because your client couldn't send Pings to Telegram in the allotted time. In this case, you can use the `PingInterval` property to increase the delay between pings *(for example 300 seconds instead of 60)*. diff --git a/src/Session.cs b/src/Session.cs index f718492..c4d3e9c 100644 --- a/src/Session.cs +++ b/src/Session.cs @@ -95,17 +95,20 @@ namespace WTelegram var utf8Json = _jsonStream.GetBuffer(); var utf8JsonLen = (int)_jsonStream.Position; int encryptedLen = 64 + (utf8JsonLen & ~15); - if (encryptedLen > _encrypted.Length) - Array.Copy(_encrypted, _encrypted = new byte[encryptedLen + 256], 16); - _encryptor.TransformBlock(_sha256.ComputeHash(utf8Json, 0, utf8JsonLen), 0, 32, _encrypted, 16); - _encryptor.TransformBlock(utf8Json, 0, encryptedLen - 64, _encrypted, 48); - _encryptor.TransformFinalBlock(utf8Json, encryptedLen - 64, utf8JsonLen & 15).CopyTo(_encrypted, encryptedLen - 16); - if (!_encryptor.CanReuseTransform) // under Mono, AES encryptor is not reusable - using (var aes = Aes.Create()) - _encryptor = aes.CreateEncryptor(_reuseKey, _encrypted[0..16]); - _store.Position = 0; - _store.Write(_encrypted, 0, encryptedLen); - _store.SetLength(encryptedLen); + lock (_store) // while updating _encrypted buffer and writing to store + { + if (encryptedLen > _encrypted.Length) + Array.Copy(_encrypted, _encrypted = new byte[encryptedLen + 256], 16); + _encryptor.TransformBlock(_sha256.ComputeHash(utf8Json, 0, utf8JsonLen), 0, 32, _encrypted, 16); + _encryptor.TransformBlock(utf8Json, 0, encryptedLen - 64, _encrypted, 48); + _encryptor.TransformFinalBlock(utf8Json, encryptedLen - 64, utf8JsonLen & 15).CopyTo(_encrypted, encryptedLen - 16); + if (!_encryptor.CanReuseTransform) // under Mono, AES encryptor is not reusable + using (var aes = Aes.Create()) + _encryptor = aes.CreateEncryptor(_reuseKey, _encrypted[0..16]); + _store.Position = 0; + _store.Write(_encrypted, 0, encryptedLen); + _store.SetLength(encryptedLen); + } _jsonStream.Position = 0; _jsonWriter.Reset(); }