Update metodi REST x gestione Enroll

This commit is contained in:
Samuele Locatelli
2025-01-07 12:16:08 +01:00
parent e9947b4cc6
commit 87fce6dd73
9 changed files with 382 additions and 102 deletions
@@ -152,10 +152,10 @@ namespace EgwControlCenter.App.Components.Compo
ACService.DoReloadConfig();
}
protected override void OnInitialized()
protected override async Task OnInitializedAsync()
{
// mi faccio staccare un codice di auth da remoto
passcode = ACService.GetAuthPassocde();
passcode = await ACService.GetAuthPassocde();
tCounter = startCounter;
RunTimer();
// aggancio gestione eventi
@@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<Version>1.1.2501.0709</Version>
<Version>1.1.2501.0712</Version>
<Configurations>Debug;Release;DEBUG_Local</Configurations>
</PropertyGroup>
<ItemGroup>
@@ -4,8 +4,8 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<ApplicationRevision>0709</ApplicationRevision>
<ApplicationVersion>1.1.2501.0709</ApplicationVersion>
<ApplicationRevision>0712</ApplicationRevision>
<ApplicationVersion>1.1.2501.0712</ApplicationVersion>
<BootstrapperEnabled>True</BootstrapperEnabled>
<Configuration>Release</Configuration>
<CreateWebPageOnPublish>True</CreateWebPageOnPublish>
+83 -6
View File
@@ -1,4 +1,5 @@
using EgwControlCenter.Core.DTO;
using DeviceId;
using EgwControlCenter.Core.DTO;
using EgwControlCenter.Core.Models;
using EgwCoreLib.Utils;
using Newtonsoft.Json;
@@ -79,14 +80,90 @@ namespace EgwControlCenter.Core
private Random rnd = new Random();
/// <summary>
/// Restituisce un codice di auth temporaneo da autorizzare
/// Restituisce un codice di auth temporaneo INT da impiegare x autorizzare
/// </summary>
/// <returns></returns>
public int GetAuthPassocde()
public async Task<int> GetAuthPassocde()
{
// fixme todo !!! implementare da lettura remota oppure gen locale...
int tempCode = rnd.Next(10000000, 99999999);
return tempCode;
int tVal = 0;
//verifico che NON ci sia una richiesta già in corso...
if (CurrEnrollData.IdReq == 0)
{
// in primis preparo dizionario dei dati da allegare alla richiesta
Dictionary<string, string> reqInfo = new Dictionary<string, string>();
// preparo dati DeviceId da allegare in coda
string deviceId = new DeviceIdBuilder()
.AddMachineName()
.AddUserName()
.AddOsVersion()
.OnWindows(windows => windows
.AddMacAddressFromWmi(excludeWireless: true, excludeNonPhysical: true)
.AddProcessorId()
.AddMotherboardSerialNumber()
.AddSystemDriveSerialNumber())
.ToString();
// aggiungo dati MachineInfo...
reqInfo = MachineDataValidator.userInfo;
foreach (var item in MachineDataValidator.netInfo)
{
if (!reqInfo.ContainsKey(item.Key))
{
reqInfo.Add(item.Key, item.Value);
}
}
// raggiungo DevideID
reqInfo.Add("DeviceID", deviceId);
// faccio la chiamata rest e salvo risposta...
CurrEnrollData = await CurrCheck.GetNewEnroll(reqInfo);
}
// risposta valida se trovo un id richeista > 0...
if (CurrEnrollData.IdReq > 0)
{
tVal = CurrEnrollData.Passcode;
}
return tVal;
}
/// <summary>
/// Verifica se sia stato assegnato il codice licenza x una richiesta di enroll e nel caso recupera licenza e restituisce il codice da salvare in conf
/// </summary>
/// <returns></returns>
public async Task<string> CheckAssignIdxLic()
{
string tVal = "";
// verifico dati richeista enroll, altrimenti faccio refresh richiesta...
if (CurrEnrollData.IdReq == 0)
{
int newCode = await GetAuthPassocde();
}
// recupero info
EnrollRequestDTO updRec = await CurrCheck.CheckCurrEnroll();
if (updRec.IdReq > 0)
{
CurrEnrollData = updRec;
// se la licenza è valida --> procedo!
if( updRec.IdxLic>0)
{
LicenzaDTO currLic = await CurrCheck.GetEnrollLic();
// se licenza valida --> restituisco e salvo!
if(currLic.IsValid && currLic.IsActive)
{
tVal = currLic.Chiave;
}
}
}
return tVal;
}
/// <summary>
/// Richiesta enroll corrente (se presente come file oppure nuova...)
/// </summary>
private EnrollRequestDTO CurrEnrollData
{
get => CurrCheck.CurrEnrollData;
set => CurrCheck.CurrEnrollData = value;
}
/// <summary>
@@ -14,7 +14,7 @@ namespace EgwControlCenter.Core.DTO
/// <summary>
/// ID univoco richiesta
/// </summary>
public int IdReq { get; set; }
public int IdReq { get; set; } = 0;
/// <summary>
/// Passcode usato per autorizzare (un valore random NON DUPLICATO con quelli attivi al momento della richiesta)
@@ -36,6 +36,11 @@ namespace EgwControlCenter.Core.DTO
/// </summary>
public DateTime? DtAppr { get; set; } = null;
/// <summary>
/// Scadenza richiesta
/// </summary>
public DateTime DtScadenza { get; set; } = DateTime.Now.AddSeconds(5);
/// <summary>
/// Username approvatore
/// </summary>
+246 -89
View File
@@ -3,16 +3,8 @@ using EgwControlCenter.Core.Models;
using Newtonsoft.Json;
using NLog;
using RestSharp;
using RestSharp.Serializers;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Markup;
namespace EgwControlCenter.Core
{
@@ -61,6 +53,50 @@ namespace EgwControlCenter.Core
get => Path.Combine(AppDir, ConfName);
}
/// <summary>
/// Richiesta enroll corrente (se presente come file oppure nuova...)
/// </summary>
public EnrollRequestDTO CurrEnrollData
{
get
{
DateTime adesso = DateTime.Now;
EnrollRequestDTO answ = new EnrollRequestDTO();
// cerco se presente il file di enroll request
if (File.Exists(EnrollReqPath))
{
// leggo il file e verifico sia ancora valido...
var rawData = File.ReadAllText(EnrollReqPath);
if (!string.IsNullOrEmpty(rawData))
{
answ = JsonConvert.DeserializeObject<EnrollRequestDTO>(rawData) ?? new EnrollRequestDTO();
// verifico validità scadenza...
if (answ.DtScadenza < adesso)
{
answ = new EnrollRequestDTO();
// elimino file scaduto...
File.Delete(EnrollReqPath);
}
}
}
return answ;
}
set
{
DateTime adesso = DateTime.Now;
// verifico la scadenza sia valida...
if (value.DtScadenza > adesso)
{
// salvo!
var rawData = JsonConvert.SerializeObject(value, Formatting.Indented);
if (rawData != null && rawData.Length > 2)
{
File.WriteAllText(EnrollReqPath, rawData);
}
}
}
}
/// <summary>
/// Configurazione degli oggetti da monitorare
/// </summary>
@@ -163,6 +199,50 @@ namespace EgwControlCenter.Core
return setUpdated;
}
/// <summary>
/// Verifica se il server remoto sia in buono stato...
/// </summary>
/// <returns></returns>
public async Task<bool> CheckRemote()
{
// in questo caso la richiesta è per la generica app UpdateManager...
string CodApp = "UpdateManager";
bool answ = false;
try
{
// client chiamate rest
var client = new RestClient(restOptFast);
// provo cmq ad inizializzare la richiesta
ReleaseReqDTO reqDto = new ReleaseReqDTO()
{
CodApp = CodApp,
CodImp = CurrPatrolCont.CodImpiego,
AppKey = CurrPatrolCont.AppKey
};
// Chiamo il metodo!
if (useAppKey && reqDto.IsValid)
{
var actReq = new RestRequest($"/api/health", Method.Get);
// effettuo vera chiamata
var currResp = await client.GetAsync(actReq);
if (currResp.StatusCode == System.Net.HttpStatusCode.OK && currResp.Content != null)
{
var rawData = $"{currResp.Content}";
answ = rawData.Contains("OK", StringComparison.InvariantCultureIgnoreCase);
}
else
{
Log.Error($"Errore in ricezione info REST CheckRemote | StatusCode: {currResp.StatusCode} | content: {currResp.Content}");
}
}
}
catch (Exception exc)
{
Log.Error($"Eccezione in fase gestione REST services CheckRemote{Environment.NewLine}{exc}");
}
return answ;
}
/// <summary>
/// Esegue verifica remota versioni x i sw tracciati
/// </summary>
@@ -197,6 +277,119 @@ namespace EgwControlCenter.Core
return fatto;
}
/// <summary>
/// Effettua un tentativo di enroll richiedendo info x gestione auth
/// </summary>
/// <param name="InfoData">Dizionario info da associare all'enroll</param>
/// <returns></returns>
public async Task<EnrollRequestDTO> GetNewEnroll(Dictionary<string, string> InfoData)
{
EnrollRequestDTO answ = new EnrollRequestDTO();
try
{
// client chiamate rest
var client = new RestClient(restOptStd);
// Chiamo il metodo!
var actReq = new RestRequest($"/api/enroller/getNewEnrollRec", Method.Post);
string payload = JsonConvert.SerializeObject(InfoData);
actReq.AddJsonBody(payload);
// effettuo vera chiamata
var currResp = await client.PostAsync(actReq);
if (currResp.StatusCode == System.Net.HttpStatusCode.OK && currResp.Content != null)
{
// mi restituisce info
var recEnroll = JsonConvert.DeserializeObject<EnrollRequestDTO>(currResp.Content);
if (recEnroll != null)
{
// verifico che contenga il dizionario allegato alla richiesta...
if (Utils.CompareDict(recEnroll.DictAttrib, InfoData))
{
answ = recEnroll;
}
}
}
}
catch (Exception exc)
{
Log.Error($"Eccezione in fase gestione REST services GetNewEnroll{Environment.NewLine}{exc}");
}
return answ;
}
/// <summary>
/// Effettua verifica stato record enroll (dato valore EnrollData...)
/// </summary>
/// <returns></returns>
public async Task<EnrollRequestDTO> CheckCurrEnroll()
{
EnrollRequestDTO answ = new EnrollRequestDTO();
// procedo se ho valori in CurrEnrollData...
if (CurrEnrollData.IdReq > 0)
{
try
{
// client chiamate rest
var client = new RestClient(restOptStd);
// Chiamo il metodo!
var actReq = new RestRequest($"/api/enroller/{CurrEnrollData.IdReq}?passcode={CurrEnrollData.Passcode}", Method.Get);
// effettuo vera chiamata
var currResp = await client.GetAsync(actReq);
if (currResp.StatusCode == System.Net.HttpStatusCode.OK && currResp.Content != null)
{
// mi restituisce info
var recEnroll = JsonConvert.DeserializeObject<EnrollRequestDTO>(currResp.Content);
if (recEnroll != null)
{
answ = recEnroll;
}
}
}
catch (Exception exc)
{
Log.Error($"Eccezione in fase gestione REST services CheckCurrEnroll{Environment.NewLine}{exc}");
}
}
return answ;
}
/// <summary>
/// Effettua verifica stato record enroll
/// </summary>
/// <returns></returns>
public async Task<LicenzaDTO> GetEnrollLic()
{
LicenzaDTO answ = new LicenzaDTO();
// procedo se ho valori in CurrEnrollData...
if (CurrEnrollData.IdReq > 0)
{
try
{
// client chiamate rest
var client = new RestClient(restOptStd);
// Chiamo il metodo!
var actReq = new RestRequest($"/api/enroller/getLicense/{CurrEnrollData.IdReq}?passcode={CurrEnrollData.Passcode}&idLic={CurrEnrollData.IdxLic}", Method.Get);
// effettuo vera chiamata
var currResp = await client.GetAsync(actReq);
if (currResp.StatusCode == System.Net.HttpStatusCode.OK && currResp.Content != null)
{
// mi restituisce info
var recEnroll = JsonConvert.DeserializeObject<LicenzaDTO>(currResp.Content);
if (recEnroll != null)
{
answ = recEnroll;
}
}
}
catch (Exception exc)
{
Log.Error($"Eccezione in fase gestione REST services GetEnrollLic{Environment.NewLine}{exc}");
}
}
return answ;
}
public bool HasUpdate()
{
bool answ = false;
@@ -431,13 +624,6 @@ namespace EgwControlCenter.Core
/// </summary>
private static Logger Log = LogManager.GetCurrentClassLogger();
/// <summary>
/// Conf client RestSharp standard:
/// - base URI al sito
/// - timeout 1 min
/// </summary>
private static RestClientOptions restOptStd = new RestClientOptions { Timeout = TimeSpan.FromSeconds(60), BaseUrl = new Uri(Const.apiUrl) };
/// <summary>
/// Conf client RestSharp fast (usato x check remote):
/// - base URI al sito
@@ -445,6 +631,13 @@ namespace EgwControlCenter.Core
/// </summary>
private static RestClientOptions restOptFast = new RestClientOptions { Timeout = TimeSpan.FromSeconds(5), BaseUrl = new Uri(Const.apiUrl) };
/// <summary>
/// Conf client RestSharp standard:
/// - base URI al sito
/// - timeout 1 min
/// </summary>
private static RestClientOptions restOptStd = new RestClientOptions { Timeout = TimeSpan.FromSeconds(60), BaseUrl = new Uri(Const.apiUrl) };
private Random rnd = new Random();
/// <summary>
@@ -462,6 +655,15 @@ namespace EgwControlCenter.Core
private Dictionary<string, ReleaseDTO> CriticalRelCurrent { get; set; } = new Dictionary<string, ReleaseDTO>();
private string DataDir { get; set; } = "";
/// <summary>
/// Path file salvataggio Enroll Request (se presente)
/// </summary>
private string EnrollReqPath
{
get => Path.Combine(DataDir, ERName);
}
private string ERName { get; set; } = "EnrollReq.json";
private string LSName { get; set; } = "LastStatus.json";
/// <summary>
@@ -587,6 +789,35 @@ namespace EgwControlCenter.Core
return answ;
}
/// <summary>
/// Recupera oggetto releaseDTO da file exe indicato analizzando ver number
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
private ReleaseDTO? ExeGetRelease(string filePath, bool canInstall)
{
ReleaseDTO? answ = null;
if (File.Exists(filePath))
{
FileVersionInfo fileVersInfo = FileVersionInfo.GetVersionInfo(filePath);
// string codApp = fileVersInfo.ProductName;
string codApp = Path.GetFileNameWithoutExtension(filePath);
string versNum = $"{fileVersInfo.ProductVersion}";
string versTxt = $"{fileVersInfo.FileVersion}";
// creo OBJ e lo restituisco...
answ = new ReleaseDTO()
{
CodApp = codApp,
LocalPath = filePath,
ReleaseDate = DateTime.Now,
VersNum = versNum,
VersText = versTxt,
CanInstall = canInstall
};
}
return answ;
}
/// <summary>
/// Recupera elenco applicazioni con ultima release CRITICAL
/// </summary>
@@ -697,51 +928,6 @@ namespace EgwControlCenter.Core
return answ;
}
/// <summary>
/// Verifica se il server remoto sia in buono stato...
/// </summary>
/// <returns></returns>
public async Task<bool> CheckRemote()
{
// in questo caso la richiesta è per la generica app UpdateManager...
string CodApp = "UpdateManager";
bool answ = false;
try
{
// client chiamate rest
var client = new RestClient(restOptFast);
// provo cmq ad inizializzare la richiesta
ReleaseReqDTO reqDto = new ReleaseReqDTO()
{
CodApp = CodApp,
CodImp = CurrPatrolCont.CodImpiego,
AppKey = CurrPatrolCont.AppKey
};
// Chiamo il metodo!
if (useAppKey && reqDto.IsValid)
{
var actReq = new RestRequest($"/api/health", Method.Get);
// effettuo vera chiamata
var currResp = await client.GetAsync(actReq);
if (currResp.StatusCode == System.Net.HttpStatusCode.OK && currResp.Content != null)
{
var rawData = $"{currResp.Content}";
answ = rawData.Contains("OK", StringComparison.InvariantCultureIgnoreCase);
}
else
{
Log.Error($"Errore in ricezione info REST CheckRemote | StatusCode: {currResp.StatusCode} | content: {currResp.Content}");
}
}
}
catch (Exception exc)
{
Log.Error($"Eccezione in fase gestione REST services CheckRemote{Environment.NewLine}{exc}");
}
return answ;
}
/// <summary>
/// init privato oggetti
/// </summary>
@@ -829,35 +1015,6 @@ namespace EgwControlCenter.Core
return answ;
}
/// <summary>
/// Recupera oggetto releaseDTO da file exe indicato analizzando ver number
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
private ReleaseDTO? ExeGetRelease(string filePath, bool canInstall)
{
ReleaseDTO? answ = null;
if (File.Exists(filePath))
{
FileVersionInfo fileVersInfo = FileVersionInfo.GetVersionInfo(filePath);
// string codApp = fileVersInfo.ProductName;
string codApp = Path.GetFileNameWithoutExtension(filePath);
string versNum = $"{fileVersInfo.ProductVersion}";
string versTxt = $"{fileVersInfo.FileVersion}";
// creo OBJ e lo restituisco...
answ = new ReleaseDTO()
{
CodApp = codApp,
LocalPath = filePath,
ReleaseDate = DateTime.Now,
VersNum = versNum,
VersText = versTxt,
CanInstall = canInstall
};
}
return answ;
}
/// <summary>
/// Rilegge i dati di configurazione e ultimo stato salvato
/// </summary>
+40
View File
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EgwControlCenter.Core
{
public class Utils
{
/// <summary>
/// Comparazione tra 2 dizionari
/// </summary>
/// <param name="dict1"></param>
/// <param name="dict2"></param>
/// <returns></returns>
public static bool CompareDict(Dictionary<string,string> dict1, Dictionary<string,string> dict2)
{
bool areEqual = true;
if (dict1.Count != dict2.Count)
{
areEqual = false;
}
else
{
// ciclo da dict1 ogni valore che so essere in ugual numero in dict2
foreach (var kvp in dict1)
{
if (!dict2.TryGetValue(kvp.Key, out string? value) || value != kvp.Value)
{
areEqual = false;
break;
}
}
}
return areEqual;
}
}
}
+2 -1
View File
@@ -51,5 +51,6 @@ In caso di chiave esistente ma piena, si deve procedere modificando la chiave di
Date | Vers | Note
---------|----------|---------
2024.09.01 | 1.0.2409.* | Prima versione WinForm only, NET 8
2024.09.27 | 1.1.2409.* | Versione NET 8 con WinForm + BLAZOR + integrazione LiMan completa e testata x Macchine
2024.10.02 | 1.1.2410.* | Versione NET 8 con WinForm + BLAZOR + integrazione LiMan completa e testata x Macchine
2024.12.24 | 1.1.2412.* | Inizio gestione applicativi IOB-WIN e IOB-MAN, compreso download + install
2025.01.07 | 1.2.2501.* | Gestione auth TOTP applicativi con LiMan inserita
BIN
View File
Binary file not shown.