66 lines
1.9 KiB
C#
66 lines
1.9 KiB
C#
using System.Text.Json.Serialization;
|
|
using Microsoft.AspNetCore.Http.HttpResults;
|
|
using VirtualDDNSRouter.Server.Interfaces;
|
|
using VirtualDDNSRouter.Server.Models;
|
|
using VirtualDDNSRouter.Server.Services;
|
|
using Route = VirtualDDNSRouter.Server.Models.Route;
|
|
|
|
|
|
var builder = WebApplication.CreateSlimBuilder(args);
|
|
|
|
builder.Services.ConfigureHttpJsonOptions(options =>
|
|
{
|
|
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
|
|
});
|
|
|
|
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);
|
|
return Task.FromResult(Results.Redirect($"http://{redirectRoute.ipAddress}:{redirectRoute.port}"));
|
|
|
|
});
|
|
|
|
app.Run();
|
|
|
|
|
|
|
|
//[JsonSerializable(typeof(Rule))]
|
|
[JsonSerializable(typeof(Route))]
|
|
internal partial class AppJsonSerializerContext : JsonSerializerContext
|
|
{
|
|
} |