Refactor and optimize route handling, clean up code structure, and apply minor improvements

This commit is contained in:
mahdium committed 2025-08-20 13:52:52 +03:30
1 parent 64e025d9f7
commit 462f10ffb2
4 files changed
+22 -25

No files matched your search

@@ -5,6 +5,6 @@ namespace VirtualDDNSRouter.Server.Context;
[YamlStaticContext] [YamlStaticContext]
[YamlSerializable(typeof(Rule))] [YamlSerializable(typeof(Rule))]
public partial class YamlStaticContext : YamlDotNet.Serialization.StaticContext public partial class YamlStaticContext : StaticContext
{ {
} }
+7 -5
View File
@@ -1,5 +1,4 @@
using System.Net; using System.Net;
using YamlDotNet.Serialization;
namespace VirtualDDNSRouter.Server.Models; namespace VirtualDDNSRouter.Server.Models;
@@ -9,16 +8,19 @@ public record Rule
public string apiKey { get; set; } = string.Empty; public string apiKey { get; set; } = string.Empty;
public string path { 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 record Route
{ {
public string path { get; set; } = string.Empty; public string path { get; set; } = string.Empty;
public IPAddress ipAddress { get; set; } = IPAddress.None; public IPAddress ipAddress { get; set; } = IPAddress.None;
public UInt16 port { get; set; } = 80; public ushort port { get; set; } = 80;
public Route() { } public Route()
{
}
} }
+11 -16
View File
@@ -19,15 +19,13 @@ app.UseForwardedHeaders(new ForwardedHeadersOptions
ForwardedHeaders.XForwardedProto ForwardedHeaders.XForwardedProto
}); });
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment()) app.MapOpenApi();
{
app.MapOpenApi();
}
List<Route> routes = new List<Route>(); var routes = new List<Route>();
app.MapGet("/setip/{path}/{port}/{apiKey}", async (IYamlParser yamlParser ,HttpContext context, string path, UInt16 port, string apiKey) => app.MapGet("/setip/{path}/{port}/{apiKey}",
{ async (IYamlParser yamlParser, HttpContext context, string path, ushort port, string apiKey) =>
{
var rules = await yamlParser.GetRules(); var rules = await yamlParser.GetRules();
app.Logger.LogInformation($"New setip request for {path} with port {port}."); app.Logger.LogInformation($"New setip request for {path} with port {port}.");
var ruleValid = rules.Any(r => r.path == path && r.apiKey == apiKey); var ruleValid = rules.Any(r => r.path == path && r.apiKey == apiKey);
@@ -36,29 +34,26 @@ app.MapGet("/setip/{path}/{port}/{apiKey}", async (IYamlParser yamlParser ,HttpC
app.Logger.LogInformation($"Invalid rule for {path} with port {port}."); app.Logger.LogInformation($"Invalid rule for {path} with port {port}.");
return Results.StatusCode(StatusCodes.Status403Forbidden); return Results.StatusCode(StatusCodes.Status403Forbidden);
} }
var clientIp = context.Connection.RemoteIpAddress; var clientIp = context.Connection.RemoteIpAddress;
if (clientIp is null) if (clientIp is null)
{ {
app.Logger.LogInformation($"Could not get the client ip address for {path} with port {port}."); 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"); return Results.BadRequest("Could not get the client ip address");
} }
var existingRoute = routes.FirstOrDefault(r => r.path == path); var existingRoute = routes.FirstOrDefault(r => r.path == path);
if (existingRoute is not null) if (existingRoute is not null) routes.Remove(existingRoute);
{
existingRoute.ipAddress = clientIp;
existingRoute.port = port;
}
else
{
routes.Add(new Route routes.Add(new Route
{ {
ipAddress = clientIp, ipAddress = clientIp,
path = path, path = path,
port = port port = port
}); });
}
return Results.Ok($"goto/{path}"); return Results.Ok($"goto/{path}");
}); });
app.MapGet("/goto/{path}", (string path) => app.MapGet("/goto/{path}", (string path) =>
{ {
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<!-- <TargetFramework>net10.0</TargetFramework> <!-- <TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile> <NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
@@ -33,8 +33,8 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0-preview.6.25358.103"/> <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0-preview.6.25358.103"/>
<PackageReference Include="Vecc.YamlDotNet.Analyzers.StaticGenerator" Version="16.3.0" /> <PackageReference Include="Vecc.YamlDotNet.Analyzers.StaticGenerator" Version="16.3.0"/>
<PackageReference Include="YamlDotNet" Version="16.3.0" /> <PackageReference Include="YamlDotNet" Version="16.3.0"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>