Correctly dispose session store on ctor exception (#128)

This commit is contained in:
Wizou 2023-02-26 17:09:45 +01:00
parent 5d0fd6452f
commit 86796ebf0c
3 changed files with 10 additions and 18 deletions

View file

@ -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).

View file

@ -96,8 +96,8 @@ namespace WTelegram
public Client(Func<string, string> 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<TcpClient> 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;
}

View file

@ -113,19 +113,19 @@ namespace WTelegram
session = JsonSerializer.Deserialize<Session>(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)