From 86796ebf0c5f8bc3ff1bcf27aa20c8bf1ca2a8e5 Mon Sep 17 00:00:00 2001 From: Wizou <11647984+wiz0u@users.noreply.github.com> Date: Sun, 26 Feb 2023 17:09:45 +0100 Subject: [PATCH] Correctly dispose session store on ctor exception (#128) --- README.md | 2 +- src/Client.cs | 12 ++---------- src/Session.cs | 14 +++++++------- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 1456e40..26656db 100644 --- a/README.md +++ b/README.md @@ -166,7 +166,7 @@ See [Examples/Program_ListenUpdates.cs](https://github.com/wiz0u/WTelegramClient An invalid API request can result in a `RpcException` being raised, reflecting the [error code and status text](https://revgram.github.io/errors.html) of the problem. -The other configuration items that you can override include: **session_pathname, email, email_verification_code, session_key, server_address, device_model, system_version, app_version, system_lang_code, lang_pack, lang_code, firebase, user_id, bot_token** +The other configuration items that you can provide include: **session_pathname, email, email_verification_code, session_key, server_address, device_model, system_version, app_version, system_lang_code, lang_pack, lang_code, firebase, user_id, bot_token** Optional API parameters have a default value of `null` when unset. Passing `null` for a required string/array is the same as *empty* (0-length). Required API parameters/fields can sometimes be set to 0 or `null` when unused (check API documentation or experiment). diff --git a/src/Client.cs b/src/Client.cs index 2b24c02..edf2018 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -96,8 +96,8 @@ namespace WTelegram public Client(Func configProvider = null, Stream sessionStore = null) { _config = configProvider ?? DefaultConfigOrAsk; - sessionStore ??= new SessionStore(Config("session_pathname")); var session_key = _config("session_key") ?? (_apiHash = Config("api_hash")); + sessionStore ??= new SessionStore(Config("session_pathname")); _session = Session.LoadOrCreate(sessionStore, Convert.FromHexString(session_key)); if (_session.ApiId == 0) _session.ApiId = int.Parse(Config("api_id")); if (_session.MainDC != 0) _session.DCSessions.TryGetValue(_session.MainDC, out _dcSession); @@ -699,15 +699,7 @@ namespace WTelegram static async Task DefaultTcpHandler(string host, int port) { var tcpClient = new TcpClient(); - try - { - await tcpClient.ConnectAsync(host, port); - } - catch - { - tcpClient.Dispose(); - throw; - } + await tcpClient.ConnectAsync(host, port); return tcpClient; } diff --git a/src/Session.cs b/src/Session.cs index e5a8f12..76580ec 100644 --- a/src/Session.cs +++ b/src/Session.cs @@ -113,19 +113,19 @@ namespace WTelegram session = JsonSerializer.Deserialize(utf8Json.AsSpan(32), Helpers.JsonOptions); Helpers.Log(2, "Loaded previous session"); } + session ??= new Session(); + session._store = store; + Encryption.RNG.GetBytes(session._encrypted, 0, 16); + session._encryptor = aes.CreateEncryptor(rgbKey, session._encrypted); + if (!session._encryptor.CanReuseTransform) session._reuseKey = rgbKey; + session._jsonWriter = new Utf8JsonWriter(session._jsonStream, default); + return session; } catch (Exception ex) { store.Dispose(); throw new ApplicationException($"Exception while reading session file: {ex.Message}\nUse the correct api_hash/id/key, or delete the file to start a new session", ex); } - session ??= new Session(); - session._store = store; - Encryption.RNG.GetBytes(session._encrypted, 0, 16); - session._encryptor = aes.CreateEncryptor(rgbKey, session._encrypted); - if (!session._encryptor.CanReuseTransform) session._reuseKey = rgbKey; - session._jsonWriter = new Utf8JsonWriter(session._jsonStream, default); - return session; } internal void Save() // must be called with lock(session)