This commit is contained in:
Brett Hewitson 2021-11-20 22:16:59 +10:00
parent f6ae6364a9
commit 2e8596e0b7
16 changed files with 588 additions and 1 deletions

View file

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29424.173
VisualStudioVersion = 16.0.31911.196
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServerManager.Common", "ServerManager.Common\ServerManager.Common.csproj", "{7C99D9F7-0C65-4116-927A-94EB018C88FD}"
EndProject
@ -40,6 +40,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ARKServerManager.Common", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConanServerManager.Common", "ConanServerManager.Common\ConanServerManager.Common.csproj", "{630422CA-4BCC-4D1D-9701-87D8EAF0B209}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServerManager.WebApplication", "ServerManager.WebApplication\ServerManager.WebApplication.csproj", "{39C42E58-36BD-4C6B-9AD2-7F9EBCA7A68A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug - Beta|Any CPU = Debug - Beta|Any CPU
@ -125,6 +127,12 @@ Global
{630422CA-4BCC-4D1D-9701-87D8EAF0B209}.Debug|Any CPU.Build.0 = Debug|Any CPU
{630422CA-4BCC-4D1D-9701-87D8EAF0B209}.Release|Any CPU.ActiveCfg = Release|Any CPU
{630422CA-4BCC-4D1D-9701-87D8EAF0B209}.Release|Any CPU.Build.0 = Release|Any CPU
{39C42E58-36BD-4C6B-9AD2-7F9EBCA7A68A}.Debug - Beta|Any CPU.ActiveCfg = Debug - Beta|Any CPU
{39C42E58-36BD-4C6B-9AD2-7F9EBCA7A68A}.Debug - Beta|Any CPU.Build.0 = Debug - Beta|Any CPU
{39C42E58-36BD-4C6B-9AD2-7F9EBCA7A68A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{39C42E58-36BD-4C6B-9AD2-7F9EBCA7A68A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{39C42E58-36BD-4C6B-9AD2-7F9EBCA7A68A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{39C42E58-36BD-4C6B-9AD2-7F9EBCA7A68A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View file

@ -0,0 +1,135 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using ServerManager.WebApplication.Models;
using ServerManager.WebApplication.Models.ApiVersion1;
using ServerManager.WebApplication.Services;
using System;
using System.Collections.Generic;
namespace ServerManager.WebApplication.Controllers
{
[Route("api/server")]
[ApiController]
[ApiVersion("1.0")]
[Produces("application/json")]
public class ServerController : ControllerBase
{
private readonly IConfiguration _configuration;
private readonly ILogger<ServerController> _logger;
private readonly IServerQueryService _serverQueryService;
public ServerController(IConfiguration configuration, ILogger<ServerController> logger, IServerQueryService serverQueryService)
{
_configuration = configuration;
_logger = logger;
_serverQueryService = serverQueryService;
}
// GET: api/server/192.168.1.1/27017
[HttpGet()]
[Route("{ipString}/{port}", Name = "GetServerStatus_V1")]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
public ActionResult GetServerStatus_V1([FromRoute] string ipString, [FromRoute] int port)
{
// check for valid service
if (_serverQueryService == null)
{
var response = new ErrorResponse { Errors = new List<string> { "Server query service not available." } };
return StatusCode(StatusCodes.Status503ServiceUnavailable, response);
}
try
{
var result = _serverQueryService.CheckServerStatus(Guid.Empty.ToString(), "0.0", ipString, port);
var response = new ServerStatusResponse { Available = result.ToString() };
return Ok(response);
}
catch (ServerManagerApiException ex)
{
var response = new ErrorResponse { Errors = ex.Messages };
return StatusCode(ex.StatusCode, response);
}
catch (Exception ex)
{
var response = new ErrorResponse { Errors = new List<string> { ex.Message } };
return StatusCode(StatusCodes.Status500InternalServerError, response);
}
}
// GET: api/server/00000000-0000-0000-0000-000000000000/192.168.1.1/27017
[HttpGet()]
[Route("{managerCode}/{ipString}/{port}", Name = "GetServerStatus_V2")]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
public ActionResult GetServerStatus_V2([FromRoute] string managerCode, [FromRoute] string ipString, [FromRoute] int port)
{
// check for valid service
if (_serverQueryService == null)
{
var response = new ErrorResponse { Errors = new List<string> { "Server query service not available." } };
return StatusCode(StatusCodes.Status503ServiceUnavailable, response);
}
try
{
var result = _serverQueryService.CheckServerStatus(managerCode, "0.0", ipString, port);
var response = new ServerStatusResponse { Available = result.ToString() };
return Ok(response);
}
catch (ServerManagerApiException ex)
{
var response = new ErrorResponse { Errors = ex.Messages };
return StatusCode(ex.StatusCode, response);
}
catch (Exception ex)
{
var response = new ErrorResponse { Errors = new List<string> { ex.Message } };
return StatusCode(StatusCodes.Status500InternalServerError, response);
}
}
// GET: api/server/00000000-0000-0000-0000-000000000000/1.0/192.168.1.1/27017
[HttpGet()]
[Route("{managerCode}/{managerVersion}/{ipString}/{port}", Name = "GetServerStatus_V3")]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
public ActionResult GetServerStatus_V3([FromRoute] string managerCode, [FromRoute] string managerVersion, [FromRoute] string ipString, [FromRoute] int port)
{
// check for valid service
if (_serverQueryService == null)
{
var response = new ErrorResponse { Errors = new List<string> { "Server query service not available." } };
return StatusCode(StatusCodes.Status503ServiceUnavailable, response);
}
try
{
var result = _serverQueryService.CheckServerStatus(managerCode, managerVersion, ipString, port);
var response = new ServerStatusResponse { Available = result.ToString() };
return Ok(response);
}
catch (ServerManagerApiException ex)
{
var response = new ErrorResponse { Errors = ex.Messages };
return StatusCode(ex.StatusCode, response);
}
catch (Exception ex)
{
var response = new ErrorResponse { Errors = new List<string> { ex.Message } };
return StatusCode(StatusCodes.Status500InternalServerError, response);
}
}
}
}

View file

@ -0,0 +1,56 @@
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Linq;
using System.Text.Json;
namespace ServerManager.WebApplication.Extensions
{
public class SwaggerDefaultValues : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
var apiDescription = context.ApiDescription;
operation.Deprecated |= apiDescription.IsDeprecated();
foreach (var responseType in context.ApiDescription.SupportedResponseTypes)
{
var responseKey = responseType.IsDefaultResponse ? "default" : responseType.StatusCode.ToString();
var response = operation.Responses[responseKey];
foreach (var contentType in response.Content.Keys)
{
if (!responseType.ApiResponseFormats.Any(x => x.MediaType == contentType))
{
response.Content.Remove(contentType);
}
}
}
if (operation.Parameters is null)
{
return;
}
foreach (var parameter in operation.Parameters)
{
var description = apiDescription.ParameterDescriptions
.First(p => p.Name == parameter.Name);
if (parameter.Description is null)
{
parameter.Description = description.ModelMetadata?.Description;
}
if (parameter.Schema.Default is null && description.DefaultValue is not null)
{
var json = JsonSerializer.Serialize(description.DefaultValue, description.ModelMetadata.ModelType);
parameter.Schema.Default = OpenApiAnyFactory.CreateFromJson(json);
}
parameter.Required |= description.IsRequired;
}
}
}
}

View file

@ -0,0 +1,16 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace ServerManager.WebApplication.Models.ApiVersion1
{
public class ErrorResponse
{
/// <summary>
/// List of errors.
/// </summary>
[Required]
[Description("List of errors.")]
public ICollection<string> Errors { get; set; } = new List<string>();
}
}

View file

@ -0,0 +1,15 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace ServerManager.WebApplication.Models.ApiVersion1
{
public class ServerStatusResponse
{
/// <summary>
/// True if the server is available; otherwise false.
/// </summary>
[Required]
[Description("True if the server is available; otherwise false.")]
public string Available { get; set; } = false.ToString();
}
}

View file

@ -0,0 +1,13 @@
using System.Runtime.Serialization;
namespace ServerManager.WebApplication.Models.Data
{
[DataContract]
public class ManagerCode
{
[DataMember]
public string Name { get; set; } = string.Empty;
[DataMember]
public string Code { get; set; } = string.Empty;
}
}

View file

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace ServerManager.WebApplication.Models
{
public class ServerManagerApiException : Exception
{
public ServerManagerApiException() : base()
{ }
public ServerManagerApiException(int statusCode, ICollection<string> messages) : base()
{
StatusCode = statusCode;
Messages = messages;
}
public ServerManagerApiException(int statusCode, ICollection<string> messages, Exception innerException) : base(null, innerException)
{
StatusCode = statusCode;
Messages = messages;
}
protected ServerManagerApiException(SerializationInfo info, StreamingContext context) : base(info, context)
{ }
public int StatusCode { get; private set; } = 0;
public ICollection<string> Messages { get; private set; } = new List<string>();
}
}

View file

@ -0,0 +1,20 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace ServerManager.WebApplication
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}

View file

@ -0,0 +1,31 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:36011",
"sslPort": 44353
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"ServerManager.WebApplication": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View file

@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Configurations>Debug;Release;Debug - Beta</Configurations>
<ApplicationIcon>Art\favicon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug - Beta|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\QueryMaster\QueryMaster.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,7 @@
namespace ServerManager.WebApplication.Services
{
public interface IServerQueryService
{
bool CheckServerStatus(string managerCode, string managerVersion, string ipString, int port);
}
}

View file

@ -0,0 +1,78 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using QueryMaster;
using ServerManager.WebApplication.Models;
using ServerManager.WebApplication.Models.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
namespace ServerManager.WebApplication.Services
{
public class QueryMasterService : IServerQueryService
{
internal const string CONFIG_MANAGERCODES = "ManagerCodes";
private readonly IConfiguration _configuration;
public QueryMasterService(IConfiguration configuration)
{
_configuration = configuration;
}
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);
var serverInfo = server.GetInfo();
return serverInfo != 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 = _configuration.GetSection(CONFIG_MANAGERCODES).Get<List<ManagerCode>>() ?? new List<ManagerCode>();
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 IPAddress _))
{
errors.Add("IP Address is invalid.");
}
if (port <= ushort.MinValue || port >= ushort.MaxValue)
{
errors.Add($"Valid port is required ({ushort.MinValue} to {ushort.MaxValue}).");
}
if (errors.Count > 0)
{
throw new ServerManagerApiException(StatusCodes.Status400BadRequest, errors);
}
}
}
}

View file

@ -0,0 +1,101 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Versioning;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ServerManager.WebApplication.Extensions;
using ServerManager.WebApplication.Services;
namespace ServerManager.WebApplication
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddResponseCaching();
/*
* https://github.com/Microsoft/aspnet-api-versioning/wiki
*/
services.AddApiVersioning(o =>
{
o.DefaultApiVersion = ApiVersion.Default;
o.AssumeDefaultVersionWhenUnspecified = true;
o.ReportApiVersions = true;
o.ApiVersionReader = ApiVersionReader.Combine(
new MediaTypeApiVersionReader("Version"),
new HeaderApiVersionReader("X-Version")
);
});
services.AddVersionedApiExplorer(o =>
{
// add the versioned api explorer, which also adds IApiVersionDescriptionProvider service
// note: the specified format code will format the version as "'v'major[.minor][-status]"
o.GroupNameFormat = "'v'VVV";
});
services.AddScoped<IServerQueryService, QueryMasterService>();
services.AddSwaggerGen(o =>
{
o.OperationFilter<SwaggerDefaultValues>();
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApiVersionDescriptionProvider provider)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
var enableSwagger = Configuration.GetValue<bool>("EnableSwagger");
if (enableSwagger)
{
var swaggerRoutePrefix = Configuration.GetValue<string>("SwaggerRoutePrefix");
if (!string.IsNullOrWhiteSpace(swaggerRoutePrefix) && !swaggerRoutePrefix.EndsWith("/"))
{
swaggerRoutePrefix += "/";
}
app.UseSwagger();
app.UseSwaggerUI(o =>
{
// build a swagger endpoint for each discovered API version
foreach (var description in provider.ApiVersionDescriptions)
{
o.SwaggerEndpoint($"/{swaggerRoutePrefix}swagger/{description.GroupName}/swagger.json", $"Server Managers API {description.GroupName.ToUpperInvariant()}");
}
});
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseResponseCaching();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}

View file

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View file

@ -0,0 +1,36 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"EnableSwagger": true,
"SwaggerRoutePrefix": "",
"ManagerCodes": [
{
"Name": "Unknown",
"Code": "00000000-0000-0000-0000-000000000000"
},
{
"Name": "Ark",
"Code": "ED89B8FA-0E0B-46CC-A90B-595E69AE9A7E"
},
{
"Name": "Conan",
"Code": "F2653C3D-BC83-440A-AD99-FD9D9466DE04"
},
{
"Name": "Dark and Light",
"Code": "D80E19F9-33D2-4466-9177-A11506998E48"
},
{
"Name": "Pantropy",
"Code": "BE852556-BFC7-4AF2-82F3-F8A1CAF5C241"
}
]
}