diff --git a/src/Server-Managers.sln b/src/Server-Managers.sln index 377bdd90..7cddbc75 100644 --- a/src/Server-Managers.sln +++ b/src/Server-Managers.sln @@ -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 diff --git a/src/ServerManager.WebApplication/Art/favicon.ico b/src/ServerManager.WebApplication/Art/favicon.ico new file mode 100644 index 00000000..1ce0205a Binary files /dev/null and b/src/ServerManager.WebApplication/Art/favicon.ico differ diff --git a/src/ServerManager.WebApplication/Controllers/ServerController.cs b/src/ServerManager.WebApplication/Controllers/ServerController.cs new file mode 100644 index 00000000..c41916fc --- /dev/null +++ b/src/ServerManager.WebApplication/Controllers/ServerController.cs @@ -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 _logger; + private readonly IServerQueryService _serverQueryService; + + public ServerController(IConfiguration configuration, ILogger 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 { "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 { 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 { "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 { 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 { "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 { ex.Message } }; + return StatusCode(StatusCodes.Status500InternalServerError, response); + } + } + } +} diff --git a/src/ServerManager.WebApplication/Extensions/SwaggerDefaultValues.cs b/src/ServerManager.WebApplication/Extensions/SwaggerDefaultValues.cs new file mode 100644 index 00000000..7a0bbb32 --- /dev/null +++ b/src/ServerManager.WebApplication/Extensions/SwaggerDefaultValues.cs @@ -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; + } + } + } +} diff --git a/src/ServerManager.WebApplication/Models/ApiVersion1/ErrorResponse.cs b/src/ServerManager.WebApplication/Models/ApiVersion1/ErrorResponse.cs new file mode 100644 index 00000000..35398904 --- /dev/null +++ b/src/ServerManager.WebApplication/Models/ApiVersion1/ErrorResponse.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; + +namespace ServerManager.WebApplication.Models.ApiVersion1 +{ + public class ErrorResponse + { + /// + /// List of errors. + /// + [Required] + [Description("List of errors.")] + public ICollection Errors { get; set; } = new List(); + } +} diff --git a/src/ServerManager.WebApplication/Models/ApiVersion1/ServerStatusResponse.cs b/src/ServerManager.WebApplication/Models/ApiVersion1/ServerStatusResponse.cs new file mode 100644 index 00000000..b466e292 --- /dev/null +++ b/src/ServerManager.WebApplication/Models/ApiVersion1/ServerStatusResponse.cs @@ -0,0 +1,15 @@ +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; + +namespace ServerManager.WebApplication.Models.ApiVersion1 +{ + public class ServerStatusResponse + { + /// + /// True if the server is available; otherwise false. + /// + [Required] + [Description("True if the server is available; otherwise false.")] + public string Available { get; set; } = false.ToString(); + } +} diff --git a/src/ServerManager.WebApplication/Models/Data/ManagerCode.cs b/src/ServerManager.WebApplication/Models/Data/ManagerCode.cs new file mode 100644 index 00000000..c7bb3384 --- /dev/null +++ b/src/ServerManager.WebApplication/Models/Data/ManagerCode.cs @@ -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; + } +} diff --git a/src/ServerManager.WebApplication/Models/ServerManagerApiException.cs b/src/ServerManager.WebApplication/Models/ServerManagerApiException.cs new file mode 100644 index 00000000..29892826 --- /dev/null +++ b/src/ServerManager.WebApplication/Models/ServerManagerApiException.cs @@ -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 messages) : base() + { + StatusCode = statusCode; + Messages = messages; + } + + public ServerManagerApiException(int statusCode, ICollection 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 Messages { get; private set; } = new List(); + } +} diff --git a/src/ServerManager.WebApplication/Program.cs b/src/ServerManager.WebApplication/Program.cs new file mode 100644 index 00000000..8dbaa0ab --- /dev/null +++ b/src/ServerManager.WebApplication/Program.cs @@ -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(); + }); + } +} diff --git a/src/ServerManager.WebApplication/Properties/launchSettings.json b/src/ServerManager.WebApplication/Properties/launchSettings.json new file mode 100644 index 00000000..765a1f22 --- /dev/null +++ b/src/ServerManager.WebApplication/Properties/launchSettings.json @@ -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" + } + } + } +} diff --git a/src/ServerManager.WebApplication/ServerManager.WebApplication.csproj b/src/ServerManager.WebApplication/ServerManager.WebApplication.csproj new file mode 100644 index 00000000..0204e11b --- /dev/null +++ b/src/ServerManager.WebApplication/ServerManager.WebApplication.csproj @@ -0,0 +1,31 @@ + + + + net5.0 + Debug;Release;Debug - Beta + Art\favicon.ico + + + + none + false + AnyCPU + + + AnyCPU + + + AnyCPU + + + + + + + + + + + + + diff --git a/src/ServerManager.WebApplication/Services/IServerQueryService.cs b/src/ServerManager.WebApplication/Services/IServerQueryService.cs new file mode 100644 index 00000000..bf78545b --- /dev/null +++ b/src/ServerManager.WebApplication/Services/IServerQueryService.cs @@ -0,0 +1,7 @@ +namespace ServerManager.WebApplication.Services +{ + public interface IServerQueryService + { + bool CheckServerStatus(string managerCode, string managerVersion, string ipString, int port); + } +} diff --git a/src/ServerManager.WebApplication/Services/QueryMasterService.cs b/src/ServerManager.WebApplication/Services/QueryMasterService.cs new file mode 100644 index 00000000..ad23114f --- /dev/null +++ b/src/ServerManager.WebApplication/Services/QueryMasterService.cs @@ -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(); + + if (string.IsNullOrWhiteSpace(managerCode)) + { + errors.Add("Manager code is required."); + } + else + { + var managerCodes = _configuration.GetSection(CONFIG_MANAGERCODES).Get>() ?? new List(); + 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); + } + } + } +} diff --git a/src/ServerManager.WebApplication/Startup.cs b/src/ServerManager.WebApplication/Startup.cs new file mode 100644 index 00000000..764fd55e --- /dev/null +++ b/src/ServerManager.WebApplication/Startup.cs @@ -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(); + + services.AddSwaggerGen(o => + { + o.OperationFilter(); + }); + } + + // 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("EnableSwagger"); + if (enableSwagger) + { + var swaggerRoutePrefix = Configuration.GetValue("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(); + }); + } + } +} diff --git a/src/ServerManager.WebApplication/appsettings.Development.json b/src/ServerManager.WebApplication/appsettings.Development.json new file mode 100644 index 00000000..8983e0fc --- /dev/null +++ b/src/ServerManager.WebApplication/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/src/ServerManager.WebApplication/appsettings.json b/src/ServerManager.WebApplication/appsettings.json new file mode 100644 index 00000000..c82953df --- /dev/null +++ b/src/ServerManager.WebApplication/appsettings.json @@ -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" + } + ] +}