mirror of
https://github.com/techgarage-ir/MTWireGuard.git
synced 2025-08-18 00:18:17 +02:00
35 lines
1.1 KiB
C#
35 lines
1.1 KiB
C#
|
using MTWireGuard.Application;
|
|||
|
using MTWireGuard.Application.Repositories;
|
|||
|
|
|||
|
namespace MTWireGuard.Middlewares
|
|||
|
{
|
|||
|
public class ClientReportingMiddleware(RequestDelegate next)
|
|||
|
{
|
|||
|
public async Task Invoke(HttpContext context, IMikrotikRepository api)
|
|||
|
{
|
|||
|
if ((context.Request.Path.Value == "/" || context.Request.Path.Value == "/Index") && (!context.User.Identity.IsAuthenticated))
|
|||
|
{
|
|||
|
var ip = context.Connection.RemoteIpAddress;
|
|||
|
var users = await api.GetUsersAsync();
|
|||
|
var user = users.Find(x => x.Address == $"{ip}/32");
|
|||
|
if (user != null)
|
|||
|
{
|
|||
|
context.Session.Set("user", user);
|
|||
|
context.Response.Redirect("/Client", true);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
await next(context);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public static class ClientReportingMiddlewareExtensions
|
|||
|
{
|
|||
|
public static IApplicationBuilder UseClientReporting(this IApplicationBuilder builder)
|
|||
|
{
|
|||
|
return builder.UseMiddleware<ClientReportingMiddleware>();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|