diff --git a/VirtualDDNSRouter.Server/Context/YamlStaticContext.cs b/VirtualDDNSRouter.Server/Context/YamlStaticContext.cs index 33e1ee3..6c98bee 100644 --- a/VirtualDDNSRouter.Server/Context/YamlStaticContext.cs +++ b/VirtualDDNSRouter.Server/Context/YamlStaticContext.cs @@ -5,6 +5,6 @@ namespace VirtualDDNSRouter.Server.Context; [YamlStaticContext] [YamlSerializable(typeof(Rule))] -public partial class YamlStaticContext : YamlDotNet.Serialization.StaticContext +public partial class YamlStaticContext : StaticContext { } \ No newline at end of file diff --git a/VirtualDDNSRouter.Server/Models/Rule.cs b/VirtualDDNSRouter.Server/Models/Rule.cs index c05e12c..4e658fa 100644 --- a/VirtualDDNSRouter.Server/Models/Rule.cs +++ b/VirtualDDNSRouter.Server/Models/Rule.cs @@ -1,5 +1,4 @@ using System.Net; -using YamlDotNet.Serialization; namespace VirtualDDNSRouter.Server.Models; @@ -9,16 +8,19 @@ public record Rule public string apiKey { get; set; } = string.Empty; public string path { get; set; } = string.Empty; - public Rule() { } // Needed for AOT static deserializer + public Rule() + { + } // Needed for AOT static deserializer } - public record Route { public string path { get; set; } = string.Empty; public IPAddress ipAddress { get; set; } = IPAddress.None; - public UInt16 port { get; set; } = 80; + public ushort port { get; set; } = 80; - public Route() { } + public Route() + { + } } \ No newline at end of file diff --git a/VirtualDDNSRouter.Server/Program.cs b/VirtualDDNSRouter.Server/Program.cs index 8f3b1d9..4a293ed 100644 --- a/VirtualDDNSRouter.Server/Program.cs +++ b/VirtualDDNSRouter.Server/Program.cs @@ -17,48 +17,43 @@ app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto -}); +}); -if (app.Environment.IsDevelopment()) -{ - app.MapOpenApi(); -} +if (app.Environment.IsDevelopment()) app.MapOpenApi(); -List routes = new List(); +var routes = new List(); -app.MapGet("/setip/{path}/{port}/{apiKey}", async (IYamlParser yamlParser ,HttpContext context, string path, UInt16 port, string apiKey) => -{ - var rules = await yamlParser.GetRules(); - app.Logger.LogInformation($"New setip request for {path} with port {port}."); - var ruleValid = rules.Any(r => r.path == path && r.apiKey == apiKey); - if (!ruleValid) - { - app.Logger.LogInformation($"Invalid rule for {path} with port {port}."); - return Results.StatusCode(StatusCodes.Status403Forbidden); - } - var clientIp = context.Connection.RemoteIpAddress; - if (clientIp is null) - { - app.Logger.LogInformation($"Could not get the client ip address for {path} with port {port}."); - return Results.BadRequest("Could not get the client ip address"); - } - var existingRoute = routes.FirstOrDefault(r => r.path == path); - if (existingRoute is not null) - { - existingRoute.ipAddress = clientIp; - existingRoute.port = port; - } - else +app.MapGet("/setip/{path}/{port}/{apiKey}", + async (IYamlParser yamlParser, HttpContext context, string path, ushort port, string apiKey) => { + var rules = await yamlParser.GetRules(); + app.Logger.LogInformation($"New setip request for {path} with port {port}."); + var ruleValid = rules.Any(r => r.path == path && r.apiKey == apiKey); + if (!ruleValid) + { + app.Logger.LogInformation($"Invalid rule for {path} with port {port}."); + return Results.StatusCode(StatusCodes.Status403Forbidden); + } + + var clientIp = context.Connection.RemoteIpAddress; + if (clientIp is null) + { + app.Logger.LogInformation($"Could not get the client ip address for {path} with port {port}."); + return Results.BadRequest("Could not get the client ip address"); + } + + var existingRoute = routes.FirstOrDefault(r => r.path == path); + if (existingRoute is not null) routes.Remove(existingRoute); + routes.Add(new Route { ipAddress = clientIp, path = path, port = port }); - } - return Results.Ok($"goto/{path}"); -}); + + return Results.Ok($"goto/{path}"); + }); app.MapGet("/goto/{path}", (string path) => { @@ -67,4 +62,4 @@ app.MapGet("/goto/{path}", (string path) => return Task.FromResult(Results.Redirect($"http://{redirectRoute.ipAddress}:{redirectRoute.port}")); }); -app.Run(); +app.Run(); \ No newline at end of file diff --git a/VirtualDDNSRouter.Server/VirtualDDNSRouter.Server.csproj b/VirtualDDNSRouter.Server/VirtualDDNSRouter.Server.csproj index 13f0e30..7536a41 100644 --- a/VirtualDDNSRouter.Server/VirtualDDNSRouter.Server.csproj +++ b/VirtualDDNSRouter.Server/VirtualDDNSRouter.Server.csproj @@ -1,25 +1,25 @@ - + net10.0 enable @@ -33,14 +33,14 @@ - - + + - - .dockerignore - + + .dockerignore +