51 lines
1.5 KiB
C#
51 lines
1.5 KiB
C#
using VirtualDDNSRouter.Server.Interfaces;
|
|
using VirtualDDNSRouter.Server.Services;
|
|
using Route = VirtualDDNSRouter.Server.Models.Route;
|
|
|
|
|
|
var builder = WebApplication.CreateSlimBuilder(args);
|
|
|
|
builder.Services.AddSingleton<IYamlParser, YamlParser>();
|
|
|
|
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
|
builder.Services.AddOpenApi();
|
|
|
|
var app = builder.Build();
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.MapOpenApi();
|
|
}
|
|
|
|
List<Route> routes = new List<Route>();
|
|
|
|
app.MapPut("/setip/{path}/{port}/{apiKey}", async (IYamlParser yamlParser ,HttpContext context, string path, UInt16 port, string apiKey) =>
|
|
{
|
|
var rules = await yamlParser.GetRules();
|
|
var ruleValid = rules.Any(r => r.path == path && r.apiKey == apiKey);
|
|
if (!ruleValid)
|
|
{
|
|
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
|
|
{
|
|
ipAddress = clientIp,
|
|
path = path,
|
|
port = port
|
|
});
|
|
return Results.Created();
|
|
});
|
|
|
|
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}"));
|
|
});
|
|
|
|
app.Run();
|