Revert "do not use Loader to check creds"

This reverts commit 1d32c224ba.
This commit is contained in:
Oleksandr Bezdieniezhnykh
2025-09-23 14:34:02 +03:00
parent 1d32c224ba
commit 0549c2de7e
2 changed files with 77 additions and 10 deletions
+4 -6
View File
@@ -3,28 +3,26 @@ using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Text; using System.Text;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace Azaion.LoaderUI; namespace Azaion.LoaderUI;
public interface IAzaionApi public interface IAzaionApi
{ {
Task Validate(ApiCredentials credentials); void Login(ApiCredentials credentials);
Task<string> GetLastInstallerName(string folder); Task<string> GetLastInstallerName(string folder);
Task<(string name, Stream stream)> DownloadInstaller(string folder); Task<(string name, Stream stream)> DownloadInstaller(string folder);
} }
public class AzaionApi(HttpClient client, ILogger<Login> logger) : IAzaionApi public class AzaionApi(HttpClient client) : IAzaionApi
{ {
private string _jwtToken = null!; private string _jwtToken = null!;
private const string APP_JSON = "application/json"; const string APP_JSON = "application/json";
private ApiCredentials _credentials = null!; private ApiCredentials _credentials = null!;
public async Task Validate(ApiCredentials credentials) public void Login(ApiCredentials credentials)
{ {
_credentials = credentials; _credentials = credentials;
await Get<bool>("/resources/check");
} }
public async Task<string> GetLastInstallerName(string folder) public async Task<string> GetLastInstallerName(string folder)
+73 -4
View File
@@ -1,13 +1,18 @@
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using Azaion.Common; using Azaion.Common;
using MessagePack;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using NetMQ;
using NetMQ.Sockets;
namespace Azaion.LoaderUI; namespace Azaion.LoaderUI;
@@ -36,23 +41,24 @@ public partial class Login
private async void LoginClick(object sender, RoutedEventArgs e) private async void LoginClick(object sender, RoutedEventArgs e)
{ {
var credentials = new ApiCredentials var creds = new ApiCredentials
{ {
Email = TbEmail.Text, Email = TbEmail.Text,
Password = TbPassword.Password Password = TbPassword.Password
}; };
if (string.IsNullOrWhiteSpace(credentials.Email) || string.IsNullOrWhiteSpace(credentials.Password)) if (string.IsNullOrWhiteSpace(creds.Email) || string.IsNullOrWhiteSpace(creds.Password))
return; return;
try try
{ {
SetControlsStatus(isLoading: true); SetControlsStatus(isLoading: true);
await _azaionApi.Validate(credentials); _azaionApi.Login(creds);
Validate(creds);
TbStatus.Foreground = Brushes.Black; TbStatus.Foreground = Brushes.Black;
var localVersion = Constants.GetLocalVersion(); var localVersion = Constants.GetLocalVersion();
var installerVersion = await GetInstallerVer() ?? localVersion; var installerVersion = await GetInstallerVer() ?? localVersion;
var credsEncrypted = Security.Encrypt(credentials); var credsEncrypted = Security.Encrypt(creds);
if (installerVersion > localVersion) 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<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<Version?> GetInstallerVer() private async Task<Version?> GetInstallerVer()
{ {
TbStatus.Text = "Checking for the newer version..."; TbStatus.Text = "Checking for the newer version...";