diff --git a/Examples/Program_DownloadSavedMedia.cs b/Examples/Program_DownloadSavedMedia.cs index f6e088c..d1004b9 100644 --- a/Examples/Program_DownloadSavedMedia.cs +++ b/Examples/Program_DownloadSavedMedia.cs @@ -21,7 +21,7 @@ namespace WTelegramClientTest async void Client_Update(IObject arg) { - if (arg is not Updates { updates: var updates }) return; + if (arg is not Updates { updates: var updates } upd) return; foreach (var update in updates) { if (update is not UpdateNewMessage { message: Message message }) diff --git a/src/Client.cs b/src/Client.cs index e84cae9..5877014 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -98,11 +98,12 @@ namespace WTelegram _dcSession = dcSession; } - internal string Config(string config) - => _config(config) ?? DefaultConfig(config) ?? throw new ApplicationException("You must provide a config value for " + config); + internal Task ConfigAsync(string what) => Task.Run(() => Config(what)); + internal string Config(string what) + => _config(what) ?? DefaultConfig(what) ?? throw new ApplicationException("You must provide a config value for " + what); /// Default config values, used if your Config callback returns - public static string DefaultConfig(string config) => config switch + public static string DefaultConfig(string what) => what switch { "session_pathname" => Path.Combine( Path.GetDirectoryName(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar))), @@ -119,7 +120,7 @@ namespace WTelegram "lang_pack" => "", "lang_code" => CultureInfo.CurrentUICulture.TwoLetterISOLanguageName, "user_id" => "-1", - "verification_code" or "password" => AskConfig(config), + "verification_code" or "password" => AskConfig(what), _ => null // api_id api_hash phone_number... it's up to you to reply to these correctly }; @@ -1013,13 +1014,13 @@ namespace WTelegram for (int retry = 1; authorization == null; retry++) try { - var verification_code = Config("verification_code"); + var verification_code = await ConfigAsync("verification_code"); authorization = await this.Auth_SignIn(phone_number, sentCode.phone_code_hash, verification_code); } catch (RpcException e) when (e.Code == 401 && e.Message == "SESSION_PASSWORD_NEEDED") { var accountPassword = await this.Account_GetPassword(); - var checkPasswordSRP = Check2FA(accountPassword, () => Config("password")); + var checkPasswordSRP = await Check2FA(accountPassword, () => ConfigAsync("password")); authorization = await this.Auth_CheckPassword(checkPasswordSRP); } catch (RpcException e) when (e.Code == 400 && e.Message == "PHONE_CODE_INVALID" && retry != 3) diff --git a/src/Encryption.cs b/src/Encryption.cs index e32557f..9fa9c69 100644 --- a/src/Encryption.cs +++ b/src/Encryption.cs @@ -362,7 +362,7 @@ j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB return output; } - internal static InputCheckPasswordSRP Check2FA(Account_Password accountPassword, Func getPassword) + internal static async Task Check2FA(Account_Password accountPassword, Func> getPassword) { if (accountPassword.current_algo is not PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow algo) throw new ApplicationException("2FA authentication uses an unsupported algo: " + accountPassword.current_algo?.GetType().Name); @@ -376,7 +376,7 @@ j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB System.Threading.Thread.Sleep(100); Helpers.Log(3, $"This account has enabled 2FA. A password is needed. {accountPassword.hint}"); - var passwordBytes = Encoding.UTF8.GetBytes(getPassword()); + var passwordBytes = Encoding.UTF8.GetBytes(await getPassword()); using var sha256 = SHA256.Create(); sha256.TransformBlock(algo.salt1, 0, algo.salt1.Length, null, 0); @@ -436,7 +436,7 @@ j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB sha256.TransformFinalBlock(k_a, 0, 32); var m1 = sha256.Hash; - validTask.Wait(); + await validTask; return new InputCheckPasswordSRP { A = g_a_256, M1 = m1, srp_id = accountPassword.srp_id }; }