Switch /setip from PUT to GET, add logging, update response formats

This commit is contained in:
2025-08-20 13:19:10 +03:30
parent cc8e892f9d
commit f90db6e714

View File

@@ -19,29 +19,33 @@ 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");
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.Created();
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}"));