Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
64e025d9f7 | |||
09bc23a739 | |||
f90db6e714 |
@@ -1,3 +1,4 @@
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using VirtualDDNSRouter.Server.Interfaces;
|
||||
using VirtualDDNSRouter.Server.Services;
|
||||
using Route = VirtualDDNSRouter.Server.Models.Route;
|
||||
@@ -12,6 +13,12 @@ builder.Services.AddOpenApi();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
||||
{
|
||||
ForwardedHeaders = ForwardedHeaders.XForwardedFor |
|
||||
ForwardedHeaders.XForwardedProto
|
||||
});
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
@@ -19,29 +26,42 @@ if (app.Environment.IsDevelopment())
|
||||
|
||||
List<Route> routes = new List<Route>();
|
||||
|
||||
app.MapPut("/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, 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) return Results.BadRequest("Could not get the client ip address");
|
||||
routes.Add(new Route
|
||||
if (clientIp is null)
|
||||
{
|
||||
ipAddress = clientIp,
|
||||
path = path,
|
||||
port = port
|
||||
});
|
||||
return Results.Created();
|
||||
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
|
||||
{
|
||||
routes.Add(new Route
|
||||
{
|
||||
ipAddress = clientIp,
|
||||
path = path,
|
||||
port = port
|
||||
});
|
||||
}
|
||||
return Results.Ok($"goto/{path}");
|
||||
});
|
||||
|
||||
app.MapGet("/goto/{path}", (string path) =>
|
||||
{
|
||||
var ruleExists = routes.Any(r => r.path == path);
|
||||
if (!ruleExists) Results.NoContent();
|
||||
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}"));
|
||||
|
Reference in New Issue
Block a user