2022-08-29 16:24:52 +02:00
|
|
|
|
using System.Net;
|
2023-12-04 23:06:42 +01:00
|
|
|
|
using Bit.IntegrationTestCommon.Factories;
|
2022-08-29 16:24:52 +02:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
|
|
|
|
namespace Bit.IntegrationTestCommon;
|
|
|
|
|
|
|
|
|
|
public class FakeRemoteIpAddressMiddleware
|
|
|
|
|
{
|
|
|
|
|
private readonly RequestDelegate _next;
|
|
|
|
|
private readonly IPAddress _fakeIpAddress;
|
|
|
|
|
|
|
|
|
|
public FakeRemoteIpAddressMiddleware(RequestDelegate next, IPAddress fakeIpAddress = null)
|
|
|
|
|
{
|
|
|
|
|
_next = next;
|
2023-12-04 23:06:42 +01:00
|
|
|
|
_fakeIpAddress = fakeIpAddress ?? IPAddress.Parse(FactoryConstants.WhitelistedIp);
|
2022-08-29 16:24:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Invoke(HttpContext httpContext)
|
|
|
|
|
{
|
|
|
|
|
httpContext.Connection.RemoteIpAddress ??= _fakeIpAddress;
|
|
|
|
|
await _next(httpContext);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class CustomStartupFilter : IStartupFilter
|
|
|
|
|
{
|
|
|
|
|
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
|
|
|
|
|
{
|
|
|
|
|
return app =>
|
|
|
|
|
{
|
|
|
|
|
app.UseMiddleware<FakeRemoteIpAddressMiddleware>();
|
|
|
|
|
next(app);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|