GUI compatibility: Detach interactive Config calls from MainThread

This commit is contained in:
Wizou 2021-12-05 11:47:52 +01:00
parent 409cf25619
commit 934ee9bae4
3 changed files with 11 additions and 10 deletions

View file

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

View file

@ -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<string> 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);
/// <summary>Default config values, used if your Config callback returns <see langword="null"/></summary>
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)

View file

@ -362,7 +362,7 @@ j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB
return output;
}
internal static InputCheckPasswordSRP Check2FA(Account_Password accountPassword, Func<string> getPassword)
internal static async Task<InputCheckPasswordSRP> Check2FA(Account_Password accountPassword, Func<Task<string>> 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 };
}