rename server project

This commit is contained in:
ppc
2024-08-31 18:46:50 +01:00
parent 98faadad47
commit 1fa0f70113
7 changed files with 4 additions and 6 deletions
+41
View File
@@ -0,0 +1,41 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<PublishAot>true</PublishAot>
<OutputType>Library</OutputType>
<EventSourceSupport>false</EventSourceSupport>
<TargetFramework>net8.0-windows</TargetFramework>
<OptimizationPreference>Size</OptimizationPreference>
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
</PropertyGroup>
<PropertyGroup>
<AssemblyName>amnet</AssemblyName>
<ProductName>AMNet</ProductName>
<CompanyName>ppc</CompanyName>
<Title>AMNet Server</Title>
<Description>Server component for the AMNet IC Card switcher</Description>
<Copyright>Copyright 2024 (c) ppc</Copyright>
</PropertyGroup>
<PropertyGroup>
<DebugType>None</DebugType>
<DebugSymbols>false</DebugSymbols>
<MetricsSupport>false</MetricsSupport>
<DebuggerSupport>false</DebuggerSupport>
<EventSourceSupport>false</EventSourceSupport>
<InvariantGlobalization>true</InvariantGlobalization>
<MetadataUpdaterSupport>false</MetadataUpdaterSupport>
<EnableUnsafeUTF7Encoding>false</EnableUnsafeUTF7Encoding>
<HttpActivityPropagationSupport>false</HttpActivityPropagationSupport>
<EnableUnsafeBinaryFormatterSerialization>false</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
+206
View File
@@ -0,0 +1,206 @@
using System;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Threading;
using Windows.Win32;
using Windows.Win32.Foundation;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace AMNet.Server;
public static class DllMain
{
private const int ApiVersion = 1;
private const string WebAddress = "http://card.ppc.moe";
private static ILogger _serverLogger;
private static string[] _serverAddresses;
private static string _serverName;
private static string _gameId;
private static (byte[] IdBytes, string OriginalId, long Expires)? _currentCard;
private static readonly object _cardLock = new();
static DllMain()
{
PInvoke.AllocConsole();
}
[UnmanagedCallersOnly(EntryPoint = "aime_io_get_api_version")]
public static ushort GetApiVersion() => 0x0100;
[UnmanagedCallersOnly(EntryPoint = "aime_io_init")]
public static int Init()
{
LoadConfiguration();
var builder = WebServer.BuildServer();
builder.WebHost.UseUrls(_serverAddresses);
var app = builder.Build();
_serverLogger = app.Logger;
app.UseCors();
app.MapGet("/amnet/info", () => Results.Ok(new SystemState(ApiVersion, _gameId, _serverName)));
app.MapPost("/amnet/signin", async ctx =>
{
CardReadRequest request;
try
{
request = await ctx.Request.ReadFromJsonAsync<CardReadRequest>();
}
catch (JsonException e)
{
ctx.Response.StatusCode = 400;
await ctx.Response.WriteAsync(e.Message);
return;
}
if (string.IsNullOrEmpty(request?.MatrixCode))
{
ctx.Response.StatusCode = 400;
await ctx.Response.WriteAsync("Card id not provided.");
return;
}
if (request.MatrixCode.Length is 0 or < 20)
{
ctx.Response.StatusCode = 422;
await ctx.Response.WriteAsync("Invalid card id length.");
return;
}
if (request.MatrixCode.Any(x => !char.IsNumber(x)))
{
ctx.Response.StatusCode = 422;
await ctx.Response.WriteAsync("Invalid card id format.");
return;
}
lock (_cardLock)
{
// ratelimit check
if (_currentCard?.Expires > Environment.TickCount64)
{
ctx.Response.StatusCode = 429;
ctx.Response.Headers.RetryAfter = ((int)TimeSpan.FromMilliseconds(_currentCard.Value.Expires - Environment.TickCount64).TotalSeconds).ToString();
return;
}
// ensure the matrix code is 20-digits long, otherwise pad with zeros
var matrixCode = request.MatrixCode.PadLeft(20, '0');
var bytes = new byte[10];
for (var i = 0; i < 10; i++)
{
var value = matrixCode.Substring(i * 2, 2);
bytes[i] = byte.Parse(value, NumberStyles.HexNumber);
}
// it's called a tick count, but it uses milliseconds despite having a unit of time called a tick???
_currentCard = (bytes, matrixCode, Environment.TickCount64 + 5000);
}
ctx.Response.StatusCode = 202;
});
CancellationTokenRegistration cancellationRegistration = default;
cancellationRegistration = app.Lifetime.ApplicationStarted.Register(() =>
{
_serverLogger.LogInformation("AMNet Server ({gameId}) started successfully.", _gameId);
_serverLogger.LogInformation("Visit {addr} from a mobile device on the same network to get started.", WebAddress);
cancellationRegistration.Dispose();
});
app.RunAsync();
return 0;
}
[UnmanagedCallersOnly(EntryPoint = "aime_io_nfc_poll")]
public static int NfcPoll(byte unitNo)
{
if (_currentCard.HasValue && _currentCard.Value.Expires < Environment.TickCount64)
{
lock (_cardLock)
{
_currentCard = null;
}
}
// there's no polling here (handled by the webserver)
return 0;
}
[UnmanagedCallersOnly(EntryPoint = "aime_io_nfc_get_aime_id")]
public static int GetAimeId(byte unitNo, nint luid, nint luidSize)
{
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("Card read in: {0}", string.Join(" ", Enumerable.Range(0, 5).Select(x => _currentCard.Value.OriginalId.Substring(x * 4, 4))));
_currentCard = null;
}
return 0;
}
[UnmanagedCallersOnly(EntryPoint = "aime_io_nfc_get_felica_id")]
public static unsafe int GetFelicaId(byte unitNo, ulong* idm)
{
// felica not supported
return 1;
}
[UnmanagedCallersOnly(EntryPoint = "aime_io_led_set_color")]
public static void SetLedColour(byte unitNo, byte r, byte g, byte b)
{
// do nothing
}
private static unsafe void LoadConfiguration()
{
const string configFileName = @".\segatools.ini";
var gameId = stackalloc char[5];
var gameIdStr = new PWSTR(gameId);
var serverName = stackalloc char[64];
var serverNameStr = new PWSTR(serverName);
var serverAddress = stackalloc char[1024];
var serverAddressStr = new PWSTR(serverAddress);
PInvoke.GetPrivateProfileString("aimeio", "gameId", string.Empty, gameIdStr, 5, configFileName);
PInvoke.GetPrivateProfileString("aimeio", "serverName", Environment.MachineName, serverNameStr, 64, configFileName);
PInvoke.GetPrivateProfileString("aimeio", "serverAddress", "http://+:6070", serverAddressStr, 1024, configFileName);
_gameId = gameIdStr.ToString();
_serverName = serverNameStr.ToString();
_serverAddresses = serverAddressStr.ToString().Split(';');
}
}
+3
View File
@@ -0,0 +1,3 @@
GetPrivateProfileStringW
GetPrivateProfileInt
AllocConsole
+16
View File
@@ -0,0 +1,16 @@
using System.Text.Json.Serialization;
namespace AMNet.Server;
public record SystemState(
[property: JsonPropertyName("apiVersion")] int ApiVersion,
[property: JsonPropertyName("gameId")] string GameId,
[property: JsonPropertyName("serverName")] string ServerName);
public record CardReadRequest(
[property: JsonPropertyName("cardId")] string MatrixCode);
[JsonSerializable(typeof(SystemState))]
[JsonSerializable(typeof(CardReadRequest))]
[JsonSourceGenerationOptions(WriteIndented = true)]
public partial class SerializerContext : JsonSerializerContext;
+44
View File
@@ -0,0 +1,44 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace AMNet.Server;
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;
}
}