Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
a6d9ae87aa | |||
3a9d48a3b6 | |||
866a5c57e3 | |||
462f10ffb2 | |||
64e025d9f7 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -4,4 +4,6 @@ obj/
|
||||
riderModule.iml
|
||||
/_ReSharper.Caches/
|
||||
.idea
|
||||
rules.yaml
|
||||
rules.yaml
|
||||
/VirtualDDNSRouter.Client/settings.yaml
|
||||
settings.yaml
|
@@ -0,0 +1,9 @@
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace VirtualDDNSRouter.Client.Context;
|
||||
|
||||
[YamlStaticContext]
|
||||
[YamlSerializable(typeof(Settings))]
|
||||
public partial class YamlStaticContextClient : StaticContext
|
||||
{
|
||||
}
|
21
VirtualDDNSRouter.Client/Dockerfile
Normal file
21
VirtualDDNSRouter.Client/Dockerfile
Normal file
@@ -0,0 +1,21 @@
|
||||
FROM mcr.microsoft.com/dotnet/runtime:10.0 AS base
|
||||
USER $APP_UID
|
||||
WORKDIR /app
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
COPY ["VirtualDDNSRouter.Client/VirtualDDNSRouter.Client.csproj", "VirtualDDNSRouter.Client/"]
|
||||
RUN dotnet restore "VirtualDDNSRouter.Client/VirtualDDNSRouter.Client.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/VirtualDDNSRouter.Client"
|
||||
RUN dotnet build "./VirtualDDNSRouter.Client.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "./VirtualDDNSRouter.Client.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "VirtualDDNSRouter.Client.dll"]
|
34
VirtualDDNSRouter.Client/Helpers/YamlParser.cs
Normal file
34
VirtualDDNSRouter.Client/Helpers/YamlParser.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using VirtualDDNSRouter.Client.Context;
|
||||
using YamlDotNet.Serialization;
|
||||
using YamlDotNet.Serialization.NamingConventions;
|
||||
using Settings = VirtualDDNSRouter.Client.Models.Settings;
|
||||
|
||||
namespace VirtualDDNSRouter.Client.Helpers;
|
||||
|
||||
public class Helpers
|
||||
{
|
||||
private static readonly string YamlFilePath = "settings.yaml";
|
||||
|
||||
public static async Task<Settings> GetSettings()
|
||||
{
|
||||
if (!File.Exists(YamlFilePath))
|
||||
throw new FileNotFoundException($"Settings file not found: {YamlFilePath}");
|
||||
|
||||
var yamlContent = await File.ReadAllTextAsync(YamlFilePath).ConfigureAwait(false);
|
||||
|
||||
// Build the deserializer with explicit naming convention
|
||||
var deserializer = new StaticDeserializerBuilder(new YamlStaticContextClient())
|
||||
.WithNamingConvention(UnderscoredNamingConvention.Instance)
|
||||
.IgnoreUnmatchedProperties()
|
||||
.Build();
|
||||
|
||||
// Deserialize into Settings
|
||||
var settings = deserializer.Deserialize<Settings>(yamlContent);
|
||||
|
||||
if (settings is null || string.IsNullOrWhiteSpace(settings.host) || string.IsNullOrWhiteSpace(settings.path) ||
|
||||
string.IsNullOrWhiteSpace(settings.apiKey))
|
||||
throw new Exception("Invalid settings file");
|
||||
|
||||
return settings;
|
||||
}
|
||||
}
|
17
VirtualDDNSRouter.Client/Models/Settings.cs
Normal file
17
VirtualDDNSRouter.Client/Models/Settings.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace VirtualDDNSRouter.Client.Models;
|
||||
|
||||
[YamlSerializable]
|
||||
public record Settings
|
||||
{
|
||||
public string host { get; set; } = string.Empty;
|
||||
public string path { get; set; } = string.Empty;
|
||||
public ushort destinationPort { get; set; } = 80;
|
||||
public string apiKey { get; set; } = string.Empty;
|
||||
public ushort refreshIntervalMinutes { get; set; } = 5;
|
||||
|
||||
public Settings()
|
||||
{
|
||||
}
|
||||
}
|
43
VirtualDDNSRouter.Client/Program.cs
Normal file
43
VirtualDDNSRouter.Client/Program.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using VirtualDDNSRouter.Client.Helpers;
|
||||
|
||||
var settings = await Helpers.GetSettings();
|
||||
|
||||
using var cts = new CancellationTokenSource();
|
||||
Console.CancelKeyPress += (s, e) =>
|
||||
{
|
||||
Console.WriteLine("Shutdown requested…");
|
||||
e.Cancel = true;
|
||||
cts.Cancel();
|
||||
};
|
||||
|
||||
var timer = new PeriodicTimer(TimeSpan.FromMinutes(settings.refreshIntervalMinutes));
|
||||
|
||||
var client = new HttpClient { Timeout = TimeSpan.FromSeconds(10) };
|
||||
var builder = new UriBuilder
|
||||
{
|
||||
Scheme = "http",
|
||||
Host = settings.host,
|
||||
Path = $"setip/{settings.path}/{settings.destinationPort}/{settings.apiKey}"
|
||||
};
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, builder.Uri);
|
||||
|
||||
Console.WriteLine("[INFO] App started. Scheduling IP address update every " + settings.refreshIntervalMinutes +
|
||||
" minutes.");
|
||||
|
||||
try
|
||||
{
|
||||
while (await timer.WaitForNextTickAsync(cts.Token))
|
||||
{
|
||||
if (cts.IsCancellationRequested) break;
|
||||
var response = await client.SendAsync(request, cts.Token);
|
||||
if (response.IsSuccessStatusCode)
|
||||
Console.WriteLine("[INFO] IP address updated at " + DateTime.Now.ToString("HH:mm:ss"));
|
||||
else
|
||||
Console.WriteLine("[ERROR] IP address update failed at " + DateTime.Now.ToString("HH:mm:ss"));
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
Console.WriteLine("[INFO] Shutdown complete.");
|
||||
}
|
24
VirtualDDNSRouter.Client/VirtualDDNSRouter.Client.csproj
Normal file
24
VirtualDDNSRouter.Client/VirtualDDNSRouter.Client.csproj
Normal file
@@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<PublishAot>true</PublishAot>
|
||||
<InvariantGlobalization>true</InvariantGlobalization>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="..\.dockerignore">
|
||||
<Link>.dockerignore</Link>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="YamlDotNet" Version="16.3.0"/>
|
||||
<PackageReference Include="Vecc.YamlDotNet.Analyzers.StaticGenerator" Version="16.3.0"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
5
VirtualDDNSRouter.Client/settings.example.yaml
Normal file
5
VirtualDDNSRouter.Client/settings.example.yaml
Normal file
@@ -0,0 +1,5 @@
|
||||
host: example.com
|
||||
path: odoo
|
||||
destination_port: 8081
|
||||
api_key: abc123XYZ
|
||||
refresh_interval_minutes: 3
|
@@ -5,6 +5,6 @@ namespace VirtualDDNSRouter.Server.Context;
|
||||
|
||||
[YamlStaticContext]
|
||||
[YamlSerializable(typeof(Rule))]
|
||||
public partial class YamlStaticContext : YamlDotNet.Serialization.StaticContext
|
||||
public partial class YamlStaticContextServer : StaticContext
|
||||
{
|
||||
}
|
@@ -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 - AI
|
||||
}
|
||||
|
||||
|
||||
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()
|
||||
{
|
||||
}
|
||||
}
|
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using VirtualDDNSRouter.Server.Interfaces;
|
||||
using VirtualDDNSRouter.Server.Services;
|
||||
@@ -17,45 +18,45 @@ 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 ConcurrentDictionary<string, 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");
|
||||
}
|
||||
|
||||
routes[path] = 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);
|
||||
if (redirectRoute is null) return Task.FromResult(Results.NotFound());
|
||||
return Task.FromResult(Results.Redirect($"http://{redirectRoute.ipAddress}:{redirectRoute.port}"));
|
||||
if (routes.TryGetValue(path, out var route)) return Results.Redirect($"http://{route.ipAddress}:{route.port}");
|
||||
return Results.NotFound();
|
||||
});
|
||||
|
||||
app.Run();
|
||||
app.Run();
|
@@ -1,4 +1,3 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using VirtualDDNSRouter.Server.Context;
|
||||
using VirtualDDNSRouter.Server.Interfaces;
|
||||
using VirtualDDNSRouter.Server.Models;
|
||||
@@ -19,7 +18,7 @@ public class YamlParser : IYamlParser
|
||||
var yamlContent = await File.ReadAllTextAsync(_yamlFilePath).ConfigureAwait(false);
|
||||
|
||||
// Build the deserializer with explicit naming convention
|
||||
var deserializer = new StaticDeserializerBuilder(new YamlStaticContext())
|
||||
var deserializer = new StaticDeserializerBuilder(new YamlStaticContextServer())
|
||||
.WithNamingConvention(UnderscoredNamingConvention.Instance) // maps api_key -> apiKey
|
||||
.IgnoreUnmatchedProperties()
|
||||
.Build();
|
||||
@@ -27,6 +26,6 @@ public class YamlParser : IYamlParser
|
||||
// Deserialize into a list of Rule
|
||||
var rules = deserializer.Deserialize<List<Rule>>(yamlContent);
|
||||
|
||||
return rules ?? new List<Rule>();
|
||||
return rules;
|
||||
}
|
||||
}
|
@@ -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>
|
||||
|
||||
<!– Publish settings –>
|
||||
<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>
|
||||
|
||||
<!– Publish settings –>
|
||||
<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>
|
||||
|
@@ -7,6 +7,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
|
||||
compose.yaml = compose.yaml
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VirtualDDNSRouter.Client", "VirtualDDNSRouter.Client\VirtualDDNSRouter.Client.csproj", "{68BC4818-F3A1-4862-BAE6-6F43D3237E63}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -17,5 +19,9 @@ Global
|
||||
{6101BFD3-C31C-41CB-9402-A8B9F3EBEE22}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{6101BFD3-C31C-41CB-9402-A8B9F3EBEE22}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{6101BFD3-C31C-41CB-9402-A8B9F3EBEE22}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{68BC4818-F3A1-4862-BAE6-6F43D3237E63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{68BC4818-F3A1-4862-BAE6-6F43D3237E63}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{68BC4818-F3A1-4862-BAE6-6F43D3237E63}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{68BC4818-F3A1-4862-BAE6-6F43D3237E63}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
@@ -5,3 +5,9 @@
|
||||
context: .
|
||||
dockerfile: VirtualDDNSRouter.Server/Dockerfile
|
||||
|
||||
virtualddnsrouter.client:
|
||||
image: virtualddnsrouter.client
|
||||
build:
|
||||
context: .
|
||||
dockerfile: VirtualDDNSRouter.Client/Dockerfile
|
||||
|
||||
|
Reference in New Issue
Block a user