fix hardware service

This commit is contained in:
Alex Bezdieniezhnykh
2025-05-02 13:25:33 +03:00
parent 7842fe1067
commit 472ed6533e
15 changed files with 118 additions and 142 deletions
+2 -13
View File
@@ -11,10 +11,10 @@ public interface IAzaionApi
ApiCredentials Credentials { get; }
User CurrentUser { get; }
void UpdateOffsets(UserQueueOffsets offsets);
Stream GetResource(string filename, string folder);
//Stream GetResource(string filename, string folder);
}
public class AzaionApi(HttpClient client, ICache cache, ApiCredentials credentials, IHardwareService hardwareService) : IAzaionApi
public class AzaionApi(HttpClient client, ICache cache, ApiCredentials credentials) : IAzaionApi
{
private string _jwtToken = null!;
const string APP_JSON = "application/json";
@@ -32,17 +32,6 @@ public class AzaionApi(HttpClient client, ICache cache, ApiCredentials credentia
}
}
public Stream GetResource(string filename, string folder)
{
var hardware = cache.GetFromCache(SecurityConstants.HARDWARE_INFO_KEY, hardwareService.GetHardware);
var response = Send(new HttpRequestMessage(HttpMethod.Post, $"/resources/get/{folder}")
{
Content = new StringContent(JsonConvert.SerializeObject(new { filename, credentials.Password, hardware }), Encoding.UTF8, APP_JSON)
});
return response.Content.ReadAsStream();
}
public void UpdateOffsets(UserQueueOffsets offsets)
{
Put($"/users/queue-offsets/{CurrentUser.Email}", offsets);
@@ -8,96 +8,96 @@ namespace Azaion.CommonSecurity.Services;
public interface IHardwareService
{
HardwareInfo GetHardware();
//HardwareInfo GetHardware();
}
public class HardwareService : IHardwareService
{
private const string WIN32_GET_HARDWARE_COMMAND =
"powershell -Command \"" +
"Get-CimInstance -ClassName Win32_Processor | Select-Object -ExpandProperty Name | Write-Output; " +
"Get-CimInstance -ClassName Win32_VideoController | Select-Object -ExpandProperty Name | Write-Output; " +
"Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty TotalVisibleMemorySize | Write-Output" +
"\"";
// private const string WIN32_GET_HARDWARE_COMMAND =
// "powershell -Command \"" +
// "Get-CimInstance -ClassName Win32_Processor | Select-Object -ExpandProperty Name | Write-Output; " +
// "Get-CimInstance -ClassName Win32_VideoController | Select-Object -ExpandProperty Name | Write-Output; " +
// "Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty TotalVisibleMemorySize | Write-Output" +
// "\"";
//
// private const string UNIX_GET_HARDWARE_COMMAND =
// "/bin/bash -c \"free -g | grep Mem: | awk '{print $2}' && " +
// "lscpu | grep 'Model name:' | cut -d':' -f2 && " +
// "lspci | grep VGA | cut -d':' -f3\"";
private const string UNIX_GET_HARDWARE_COMMAND =
"/bin/bash -c \"free -g | grep Mem: | awk '{print $2}' && " +
"lscpu | grep 'Model name:' | cut -d':' -f2 && " +
"lspci | grep VGA | cut -d':' -f3\"";
// public HardwareInfo GetHardware()
// {
// try
// {
// var output = RunCommand(Environment.OSVersion.Platform == PlatformID.Win32NT
// ? WIN32_GET_HARDWARE_COMMAND
// : UNIX_GET_HARDWARE_COMMAND);
//
// var lines = output
// .Replace("TotalVisibleMemorySize=", "")
// .Replace("Name=", "")
// .Replace(" ", " ")
// .Trim()
// .Split(['\n', '\r'], StringSplitOptions.RemoveEmptyEntries)
// .Select(x => x.Trim())
// .ToArray();
//
// if (lines.Length < 3)
// throw new Exception("Can't get hardware info");
//
// var hardwareInfo = new HardwareInfo
// {
// CPU = lines[0],
// GPU = lines[1],
// Memory = lines[2],
// MacAddress = GetMacAddress()
// };
// return hardwareInfo;
// }
// catch (Exception ex)
// {
// Console.WriteLine(ex.Message);
// throw;
// }
// }
public HardwareInfo GetHardware()
{
try
{
var output = RunCommand(Environment.OSVersion.Platform == PlatformID.Win32NT
? WIN32_GET_HARDWARE_COMMAND
: UNIX_GET_HARDWARE_COMMAND);
// private string GetMacAddress()
// {
// var macAddress = NetworkInterface
// .GetAllNetworkInterfaces()
// .Where(nic => nic.OperationalStatus == OperationalStatus.Up)
// .Select(nic => nic.GetPhysicalAddress().ToString())
// .FirstOrDefault();
//
// return macAddress ?? string.Empty;
// }
//
// private string RunCommand(string command)
// {
// try
// {
// using var process = new Process();
// process.StartInfo.FileName = Environment.OSVersion.Platform == PlatformID.Unix ? "/bin/bash" : "cmd.exe";
// process.StartInfo.Arguments = Environment.OSVersion.Platform == PlatformID.Unix
// ? $"-c \"{command}\""
// : $"/c {command}";
// process.StartInfo.RedirectStandardOutput = true;
// process.StartInfo.UseShellExecute = false;
// process.StartInfo.CreateNoWindow = true;
//
// process.Start();
// var result = process.StandardOutput.ReadToEnd();
// process.WaitForExit();
//
// return result.Trim();
// }
// catch
// {
// return string.Empty;
// }
// }
var lines = output
.Replace("TotalVisibleMemorySize=", "")
.Replace("Name=", "")
.Replace(" ", " ")
.Trim()
.Split(['\n', '\r'], StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim())
.ToArray();
if (lines.Length < 3)
throw new Exception("Can't get hardware info");
var hardwareInfo = new HardwareInfo
{
CPU = lines[0],
GPU = lines[1],
Memory = lines[2],
MacAddress = GetMacAddress()
};
return hardwareInfo;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
}
private string GetMacAddress()
{
var macAddress = NetworkInterface
.GetAllNetworkInterfaces()
.Where(nic => nic.OperationalStatus == OperationalStatus.Up)
.Select(nic => nic.GetPhysicalAddress().ToString())
.FirstOrDefault();
return macAddress ?? string.Empty;
}
private string RunCommand(string command)
{
try
{
using var process = new Process();
process.StartInfo.FileName = Environment.OSVersion.Platform == PlatformID.Unix ? "/bin/bash" : "cmd.exe";
process.StartInfo.Arguments = Environment.OSVersion.Platform == PlatformID.Unix
? $"-c \"{command}\""
: $"/c {command}";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();
var result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return result.Trim();
}
catch
{
return string.Empty;
}
}
private static string ToHash(string str) =>
Convert.ToBase64String(SHA384.HashData(Encoding.UTF8.GetBytes(str)));
// private static string ToHash(string str) =>
// Convert.ToBase64String(SHA384.HashData(Encoding.UTF8.GetBytes(str)));
}
@@ -60,7 +60,8 @@ public class InferenceClient : IInferenceClient
private async Task ProcessClientCommands()
{
//TODO: implement always on ready to client's requests. Utilize RemoteCommand
await Task.CompletedTask;
}
public void Stop()