2020-06-28 21:44:35 -05:00
|
|
|
using System;
|
2019-10-10 09:49:03 +09:00
|
|
|
using System.Threading.Tasks;
|
2020-06-28 17:55:27 +09:00
|
|
|
using BTCPayServer.Configuration;
|
2019-10-10 09:49:03 +09:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Hosting
|
|
|
|
{
|
|
|
|
public class HeadersOverrideMiddleware
|
|
|
|
{
|
2020-06-28 22:07:48 -05:00
|
|
|
readonly RequestDelegate _Next;
|
|
|
|
readonly string overrideXForwardedProto;
|
2019-10-10 09:49:03 +09: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 14:10:01 +09:00
|
|
|
if (!httpContext.Request.Host.Host.EndsWith(".onion", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
httpContext.Request.Headers["X-Forwarded-Proto"] = overrideXForwardedProto;
|
|
|
|
}
|
2019-10-10 09:49:03 +09:00
|
|
|
}
|
|
|
|
await _Next(httpContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|