diff --git a/AimeNet/DllMain.cs b/AimeNet/DllMain.cs index 74e8300..eb0fa18 100644 --- a/AimeNet/DllMain.cs +++ b/AimeNet/DllMain.cs @@ -43,43 +43,16 @@ public static class DllMain { LoadConfiguration(); - var builder = WebApplication.CreateSlimBuilder([]); - - builder.Logging.ClearProviders(); - builder.Logging.AddSimpleConsole(o => - { - o.SingleLine = true; - o.IncludeScopes = false; - }); - - builder.Logging.SetMinimumLevel(LogLevel.Information); - builder.Logging.AddFilter("Microsoft.AspNetCore.Http.Result", LogLevel.None); - builder.Logging.AddFilter("Microsoft.AspNetCore.Routing.EndpointMiddleware", LogLevel.Error); - + var builder = WebServer.BuildServer(); builder.WebHost.UseUrls(_serverAddresses); - builder.Services.AddCors(cors => - { - cors.AddDefaultPolicy(policy => - { - policy.AllowAnyMethod(); - policy.AllowAnyOrigin(); - policy.AllowAnyHeader(); - - policy.SetPreflightMaxAge(TimeSpan.FromHours(6)); - }); - }); - - builder.Services.ConfigureHttpJsonOptions(options => - { - options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default); - }); var app = builder.Build(); - app.UseCors(); + _serverLogger = app.Logger; - app.MapGet("/", () => Results.Ok(new SystemState(ApiVersion, _serverName))); - app.MapPost("/card", async ctx => + app.UseCors(); + app.MapGet("/amnet/info", () => Results.Ok(new SystemState(ApiVersion, _gameId, _serverName))); + app.MapPost("/amnet/signin", async ctx => { CardReadRequest request; @@ -89,14 +62,11 @@ public static class DllMain } catch (JsonException e) { - await using var writer = new StreamWriter(ctx.Response.Body, Encoding.UTF8, leaveOpen: true); - await writer.WriteLineAsync(e.Message); - ctx.Response.StatusCode = 400; return; } - lock (CardLock) + lock (_cardLock) { // don't allow multiple writes in quick succession if (_currentCard?.Expires > Environment.TickCount64) @@ -128,8 +98,6 @@ public static class DllMain ctx.Response.StatusCode = 204; }); - _serverLogger = app.Logger; - CancellationTokenRegistration cancellationRegistration = default; cancellationRegistration = app.Lifetime.ApplicationStarted.Register(() => { @@ -140,7 +108,6 @@ public static class DllMain }); app.RunAsync(); - return 0; } @@ -149,7 +116,7 @@ public static class DllMain { if (_currentCard.HasValue && _currentCard.Value.Expires < Environment.TickCount64) { - lock (CardLock) + lock (_cardLock) { _currentCard = null; } @@ -162,16 +129,24 @@ public static class DllMain [UnmanagedCallersOnly(EntryPoint = "aime_io_nfc_get_aime_id")] public static int GetAimeId(byte unitNo, nint luid, nint luidSize) { - lock (CardLock) + lock (_cardLock) { if (unitNo != 0 || _currentCard == null) { return 1; } + if (_currentCard.Value.Expires < Environment.TickCount64) + { + _serverLogger.LogWarning("Submitted card id expired before it could be read."); + _currentCard = null; + + return 1; + } + Marshal.Copy(_currentCard.Value.IdBytes, 0, luid, (int)luidSize); - _serverLogger.LogInformation("Aime Card read: {0}", string.Join(" ", Enumerable.Range(0, 5).Select(x => _currentCard.Value.OriginalId.Substring(x * 4, 4)))); + _serverLogger.LogInformation("Card read in: {0}", string.Join(" ", Enumerable.Range(0, 5).Select(x => _currentCard.Value.OriginalId.Substring(x * 4, 4)))); _currentCard = null; } diff --git a/AimeNet/WebServer.cs b/AimeNet/WebServer.cs new file mode 100644 index 0000000..94e715a --- /dev/null +++ b/AimeNet/WebServer.cs @@ -0,0 +1,44 @@ +using System; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace AimeNet; + +internal static class WebServer +{ + public static WebApplicationBuilder BuildServer() + { + var builder = WebApplication.CreateSlimBuilder([]); + + builder.Logging.ClearProviders(); + builder.Logging.AddSimpleConsole(o => + { + o.SingleLine = true; + o.IncludeScopes = false; + }); + + builder.Logging.SetMinimumLevel(LogLevel.Information); + builder.Logging.AddFilter("Microsoft.AspNetCore.Http.Result", LogLevel.None); + builder.Logging.AddFilter("Microsoft.AspNetCore.Routing.EndpointMiddleware", LogLevel.Error); + + builder.Services.AddCors(cors => + { + cors.AddDefaultPolicy(policy => + { + policy.AllowAnyMethod(); + policy.AllowAnyOrigin(); + policy.AllowAnyHeader(); + + policy.SetPreflightMaxAge(TimeSpan.FromHours(6)); + }); + }); + + builder.Services.ConfigureHttpJsonOptions(options => + { + options.SerializerOptions.TypeInfoResolverChain.Insert(0, SerializerContext.Default); + }); + + return builder; + } +} \ No newline at end of file