mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 06:46:30 +00:00
do not use Loader to check creds
This commit is contained in:
@@ -3,26 +3,28 @@ 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
|
||||||
{
|
{
|
||||||
void Login(ApiCredentials credentials);
|
Task Validate(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) : IAzaionApi
|
public class AzaionApi(HttpClient client, ILogger<Login> logger) : IAzaionApi
|
||||||
{
|
{
|
||||||
private string _jwtToken = null!;
|
private string _jwtToken = null!;
|
||||||
const string APP_JSON = "application/json";
|
private const string APP_JSON = "application/json";
|
||||||
private ApiCredentials _credentials = null!;
|
private ApiCredentials _credentials = null!;
|
||||||
|
|
||||||
public void Login(ApiCredentials credentials)
|
public async Task Validate(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)
|
||||||
|
|||||||
@@ -1,18 +1,13 @@
|
|||||||
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;
|
||||||
|
|
||||||
@@ -41,24 +36,23 @@ public partial class Login
|
|||||||
|
|
||||||
private async void LoginClick(object sender, RoutedEventArgs e)
|
private async void LoginClick(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
var creds = new ApiCredentials
|
var credentials = new ApiCredentials
|
||||||
{
|
{
|
||||||
Email = TbEmail.Text,
|
Email = TbEmail.Text,
|
||||||
Password = TbPassword.Password
|
Password = TbPassword.Password
|
||||||
};
|
};
|
||||||
if (string.IsNullOrWhiteSpace(creds.Email) || string.IsNullOrWhiteSpace(creds.Password))
|
if (string.IsNullOrWhiteSpace(credentials.Email) || string.IsNullOrWhiteSpace(credentials.Password))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
SetControlsStatus(isLoading: true);
|
SetControlsStatus(isLoading: true);
|
||||||
_azaionApi.Login(creds);
|
await _azaionApi.Validate(credentials);
|
||||||
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(creds);
|
var credsEncrypted = Security.Encrypt(credentials);
|
||||||
|
|
||||||
if (installerVersion > localVersion)
|
if (installerVersion > localVersion)
|
||||||
{
|
{
|
||||||
@@ -97,69 +91,6 @@ 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...";
|
||||||
|
|||||||
Reference in New Issue
Block a user