From 0549c2de7e6fa339263c1f1889850b067a0c6d77 Mon Sep 17 00:00:00 2001 From: Oleksandr Bezdieniezhnykh Date: Tue, 23 Sep 2025 14:34:02 +0300 Subject: [PATCH] Revert "do not use Loader to check creds" This reverts commit 1d32c224ba15260a18968ea5578e7ee570f3f11e. --- Azaion.LoaderUI/AzaionApi.cs | 10 ++--- Azaion.LoaderUI/Login.xaml.cs | 77 +++++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 10 deletions(-) diff --git a/Azaion.LoaderUI/AzaionApi.cs b/Azaion.LoaderUI/AzaionApi.cs index 923782c..effc478 100644 --- a/Azaion.LoaderUI/AzaionApi.cs +++ b/Azaion.LoaderUI/AzaionApi.cs @@ -3,28 +3,26 @@ using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Text; -using Microsoft.Extensions.Logging; using Newtonsoft.Json; namespace Azaion.LoaderUI; public interface IAzaionApi { - Task Validate(ApiCredentials credentials); + void Login(ApiCredentials credentials); Task GetLastInstallerName(string folder); Task<(string name, Stream stream)> DownloadInstaller(string folder); } -public class AzaionApi(HttpClient client, ILogger logger) : IAzaionApi +public class AzaionApi(HttpClient client) : IAzaionApi { private string _jwtToken = null!; - private const string APP_JSON = "application/json"; + const string APP_JSON = "application/json"; private ApiCredentials _credentials = null!; - public async Task Validate(ApiCredentials credentials) + public void Login(ApiCredentials credentials) { _credentials = credentials; - await Get("/resources/check"); } public async Task GetLastInstallerName(string folder) diff --git a/Azaion.LoaderUI/Login.xaml.cs b/Azaion.LoaderUI/Login.xaml.cs index 48f1493..fd5352d 100644 --- a/Azaion.LoaderUI/Login.xaml.cs +++ b/Azaion.LoaderUI/Login.xaml.cs @@ -1,13 +1,18 @@ using System.Diagnostics; using System.IO; +using System.Text; using System.Text.RegularExpressions; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using Azaion.Common; +using MessagePack; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using NetMQ; +using NetMQ.Sockets; + namespace Azaion.LoaderUI; @@ -36,23 +41,24 @@ public partial class Login private async void LoginClick(object sender, RoutedEventArgs e) { - var credentials = new ApiCredentials + var creds = new ApiCredentials { Email = TbEmail.Text, Password = TbPassword.Password }; - if (string.IsNullOrWhiteSpace(credentials.Email) || string.IsNullOrWhiteSpace(credentials.Password)) + if (string.IsNullOrWhiteSpace(creds.Email) || string.IsNullOrWhiteSpace(creds.Password)) return; try { SetControlsStatus(isLoading: true); - await _azaionApi.Validate(credentials); + _azaionApi.Login(creds); + Validate(creds); TbStatus.Foreground = Brushes.Black; var localVersion = Constants.GetLocalVersion(); var installerVersion = await GetInstallerVer() ?? localVersion; - var credsEncrypted = Security.Encrypt(credentials); + var credsEncrypted = Security.Encrypt(creds); if (installerVersion > localVersion) { @@ -91,6 +97,69 @@ public partial class Login } } + private void Validate(ApiCredentials creds) + { + var dealer = new DealerSocket(); + try + { + using var process = new Process(); + process.StartInfo = new ProcessStartInfo + { + FileName = Constants.EXTERNAL_LOADER_PATH, + Arguments = $"--port {ConstantsLoader.EXTERNAL_LOADER_PORT} --api {Constants.DEFAULT_API_URL}", + CreateNoWindow = true + }; + process.Start(); + dealer.Options.Identity = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString("N")); + dealer.Connect($"tcp://{Constants.DEFAULT_ZMQ_INFERENCE_HOST}:{ConstantsLoader.EXTERNAL_LOADER_PORT}"); + + var result = SendCommand(dealer, RemoteCommand.Create(CommandType.Login, creds)); + if (result.CommandType != CommandType.Ok) + throw new Exception(result.Message); + + result = SendCommand(dealer, RemoteCommand.Create(CommandType.CheckResource)); + if (result.CommandType != CommandType.Ok) + throw new Exception(result.Message); + } + catch (Exception e) + { + _logger.LogError(e, e.Message); + throw; + } + finally + { + SendCommand(dealer, RemoteCommand.Create(CommandType.Exit)); + dealer.Close(); + dealer.Dispose(); + } + } + + private RemoteCommand SendCommand(DealerSocket dealer, RemoteCommand command, int retryCount = 30, int retryDelayMs = 800) + { + try + { + dealer.SendFrame(MessagePackSerializer.Serialize(command)); + + var tryNum = 0; + while (tryNum++ < retryCount) + { + if (!dealer.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(retryDelayMs), out var bytes)) + continue; + var res = MessagePackSerializer.Deserialize(bytes); + if (res.CommandType == CommandType.Error) + throw new Exception(res.Message); + return res; + } + + throw new Exception($"Sent {command} {retryCount} times, with wait time {retryDelayMs}ms for each call. No response from client."); + } + catch (Exception e) + { + _logger.LogError(e, e.Message); + throw; + } + } + private async Task GetInstallerVer() { TbStatus.Text = "Checking for the newer version...";