add resource check

incorrect pass / hw handling in a loader
This commit is contained in:
Alex Bezdieniezhnykh
2025-06-15 15:01:55 +03:00
parent c0f8dd792d
commit def7aad833
14 changed files with 193 additions and 16 deletions
+104 -6
View File
@@ -1,10 +1,15 @@
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using MessagePack;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NetMQ;
using NetMQ.Sockets;
namespace Azaion.LoaderUI;
@@ -23,17 +28,40 @@ public partial class Login
InitializeComponent();
}
private void SetControlsStatus(bool isLoading)
{
LoginBtn.IsEnabled = !isLoading;
TbEmail.IsEnabled = !isLoading;
TbPassword.IsEnabled = !isLoading;
LoginBtn.Cursor = isLoading ? Cursors.Wait : Cursors.Arrow;
Cursor = isLoading ? Cursors.Wait : Cursors.Arrow;
}
private async void LoginClick(object sender, RoutedEventArgs e)
{
var creds = new ApiCredentials(TbEmail.Text, TbPassword.Password);
var creds = new ApiCredentials
{
Email = TbEmail.Text,
Password = TbPassword.Password
};
if (!creds.IsValid())
return;
LoginBtn.Cursor = Cursors.Wait;
Cursor = Cursors.Wait;
SetControlsStatus(isLoading: true);
_azaionApi.Login(creds);
try
{
Validate(creds);
}
catch (Exception exception)
{
_logger.LogError(exception, exception.Message);
TbStatus.Foreground = Brushes.Red;
TbStatus.Text = exception.Message;
SetControlsStatus(isLoading: false);
return;
}
TbStatus.Foreground = Brushes.Black;
var installerVersion = await GetInstallerVer();
var localVersion = GetLocalVer();
@@ -44,12 +72,81 @@ public partial class Login
TbStatus.Text = $"Installed {installerVersion}!";
}
else
TbStatus.Text = $"Your version is up to date!";
TbStatus.Text = "Your version is up to date!";
Process.Start(Constants.AZAION_SUITE_EXE, $"-e {creds.Email} -p {creds.Password}");
await Task.Delay(800);
TbStatus.Text = "Loading...";
while (!Process.GetProcessesByName(Constants.INFERENCE_EXE).Any())
await Task.Delay(500);
await Task.Delay(500);
Close();
}
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 {Constants.EXTERNAL_LOADER_PORT} --api {Constants.API_URL}",
CreateNoWindow = true
};
process.Start();
dealer.Options.Identity = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString("N"));
dealer.Connect($"tcp://{Constants.EXTERNAL_LOADER_HOST}:{Constants.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<RemoteCommand>(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 DownloadAndRunInstaller()
{
var (installerName, stream) = await _azaionApi.DownloadInstaller(_dirConfig?.SuiteInstallerDirectory ?? "");
@@ -65,6 +162,7 @@ public partial class Login
var process = Process.Start(processInfo);
await process!.WaitForExitAsync();
File.Delete(installerName);
}
private async Task<Version> GetInstallerVer()