ServerManagers/src/ServerManager.WebApplication/Services/QueryMasterService.cs
Brett Hewitson f5a96f965a Dotnet 8.x Update
- updated all nugets to latest 8.x versions
- linting fixes
- switched the application insights to use the connection string.
2025-03-21 06:46:38 +10:00

60 lines
No EOL
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Microsoft.AspNetCore.Http;
using QueryMaster;
using ServerManager.WebApplication.Models;
using ServerManager.WebApplication.Models.Data;
namespace ServerManager.WebApplication.Services;
public class QueryMasterService : IServerQueryService
{
private readonly ServerQuerySettings _settings;
public QueryMasterService(ServerQuerySettings settings)
{
_settings = settings;
}
public bool CheckServerStatus(string managerCode, string managerVersion, string ipString, int port)
{
ValidateServerStatusRequest(managerCode, ipString, port);
try
{
using var server = ServerQuery.GetServerInstance(EngineType.Source, ipString, (ushort)port);
return server.GetInfo() is not null;
}
catch
{
return false;
}
}
private void ValidateServerStatusRequest(string managerCode, string ipString, int port)
{
var errors = new List<string>();
if (string.IsNullOrWhiteSpace(managerCode))
errors.Add("Manager code is required.");
else
{
var managerCodes = _settings.ManagerCodes ?? [];
if (!managerCodes.Any(c => c.Code.Equals(managerCode, StringComparison.OrdinalIgnoreCase)))
errors.Add("Manager code is invalid.");
}
if (string.IsNullOrWhiteSpace(ipString))
errors.Add("IP Address is required.");
else if (!IPAddress.TryParse(ipString, out _))
errors.Add("IP Address is invalid.");
if (port is <= ushort.MinValue or >= ushort.MaxValue)
errors.Add($"Valid port is required ({ushort.MinValue} to {ushort.MaxValue}).");
if (errors.Count > 0)
throw new ServerManagerApiException(StatusCodes.Status400BadRequest, errors);
}
}