2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2019-10-10 02:49:03 +02:00
|
|
|
using System.Threading.Tasks;
|
2020-06-28 10:55:27 +02:00
|
|
|
using BTCPayServer.Configuration;
|
2019-10-10 02:49:03 +02:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Hosting
|
|
|
|
{
|
|
|
|
public class HeadersOverrideMiddleware
|
|
|
|
{
|
2020-06-29 05:07:48 +02:00
|
|
|
readonly RequestDelegate _Next;
|
|
|
|
readonly string overrideXForwardedProto;
|
2019-10-10 02:49:03 +02:00
|
|
|
public HeadersOverrideMiddleware(RequestDelegate next,
|
|
|
|
IConfiguration options)
|
|
|
|
{
|
|
|
|
_Next = next ?? throw new ArgumentNullException(nameof(next));
|
|
|
|
overrideXForwardedProto = options.GetOrDefault<string>("xforwardedproto", null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Invoke(HttpContext httpContext)
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(overrideXForwardedProto))
|
|
|
|
{
|
2019-10-10 07:10:01 +02:00
|
|
|
if (!httpContext.Request.Host.Host.EndsWith(".onion", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
httpContext.Request.Headers["X-Forwarded-Proto"] = overrideXForwardedProto;
|
|
|
|
}
|
2019-10-10 02:49:03 +02:00
|
|
|
}
|
|
|
|
await _Next(httpContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|