2 Commits
v0.3 ... v0.5

4 changed files with 69 additions and 63 deletions

View File

@@ -5,6 +5,6 @@ namespace VirtualDDNSRouter.Server.Context;
[YamlStaticContext]
[YamlSerializable(typeof(Rule))]
public partial class YamlStaticContext : YamlDotNet.Serialization.StaticContext
public partial class YamlStaticContext : StaticContext
{
}

View File

@@ -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()
{
}
}

View File

@@ -17,40 +17,44 @@ app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto
});
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
List<Route> routes = new List<Route>();
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");
}
routes.Add(new Route
{
ipAddress = clientIp,
path = path,
port = port
});
return Results.Ok($"goto/{path}");
});
if (app.Environment.IsDevelopment()) app.MapOpenApi();
var routes = new List<Route>();
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}");
});
app.MapGet("/goto/{path}", (string path) =>
{
var redirectRoute = routes.FirstOrDefault(r => r.path == path);
@@ -58,4 +62,4 @@ app.MapGet("/goto/{path}", (string path) =>
return Task.FromResult(Results.Redirect($"http://{redirectRoute.ipAddress}:{redirectRoute.port}"));
});
app.Run();
app.Run();

View File

@@ -1,25 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<!-- <TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
<InvariantGlobalization>true</InvariantGlobalization>
&lt;!&ndash; Publish settings &ndash;&gt;
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<PublishTrimmed>true</PublishTrimmed>
<TrimMode>full</TrimMode>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
<PublishReadyToRun>true</PublishReadyToRun>
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
<OptimizationLevel>Release</OptimizationLevel>
<TieredPGO>true</TieredPGO>
<IlcOptimizationPreference>Size</IlcOptimizationPreference>-->
<!-- <TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
<InvariantGlobalization>true</InvariantGlobalization>
&lt;!&ndash; Publish settings &ndash;&gt;
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<PublishTrimmed>true</PublishTrimmed>
<TrimMode>full</TrimMode>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
<PublishReadyToRun>true</PublishReadyToRun>
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
<OptimizationLevel>Release</OptimizationLevel>
<TieredPGO>true</TieredPGO>
<IlcOptimizationPreference>Size</IlcOptimizationPreference>-->
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
@@ -33,14 +33,14 @@
<ItemGroup>
<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="YamlDotNet" Version="16.3.0" />
<PackageReference Include="Vecc.YamlDotNet.Analyzers.StaticGenerator" Version="16.3.0"/>
<PackageReference Include="YamlDotNet" Version="16.3.0"/>
</ItemGroup>
<ItemGroup>
<Content Include="..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
<Content Include="..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
</ItemGroup>
</Project>