diff --git a/BTCPayServer/Hosting/HeadersOverrideMiddleware.cs b/BTCPayServer/Hosting/HeadersOverrideMiddleware.cs new file mode 100644 index 000000000..e93c75010 --- /dev/null +++ b/BTCPayServer/Hosting/HeadersOverrideMiddleware.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; +using BTCPayServer.Configuration; + +namespace BTCPayServer.Hosting +{ + public class HeadersOverrideMiddleware + { + RequestDelegate _Next; + string overrideXForwardedProto; + public HeadersOverrideMiddleware(RequestDelegate next, + IConfiguration options) + { + _Next = next ?? throw new ArgumentNullException(nameof(next)); + overrideXForwardedProto = options.GetOrDefault("xforwardedproto", null); + } + + public async Task Invoke(HttpContext httpContext) + { + if (!string.IsNullOrEmpty(overrideXForwardedProto)) + { + httpContext.Request.Headers["X-Forwarded-Proto"] = overrideXForwardedProto; + } + await _Next(httpContext); + } + } +}