Scintillating.ProxyProtocol.Middleware 0.6.0

dotnet add package Scintillating.ProxyProtocol.Middleware --version 0.6.0                
NuGet\Install-Package Scintillating.ProxyProtocol.Middleware -Version 0.6.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Scintillating.ProxyProtocol.Middleware" Version="0.6.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Scintillating.ProxyProtocol.Middleware --version 0.6.0                
#r "nuget: Scintillating.ProxyProtocol.Middleware, 0.6.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Scintillating.ProxyProtocol.Middleware as a Cake Addin
#addin nuget:?package=Scintillating.ProxyProtocol.Middleware&version=0.6.0

// Install Scintillating.ProxyProtocol.Middleware as a Cake Tool
#tool nuget:?package=Scintillating.ProxyProtocol.Middleware&version=0.6.0                

Scintillating.ProxyProtocol.Middleware is a ASP.NET Core middleware for accepting connections with PROXY protocol header.

Quickstart

  • Note that it's impossible to accept both PROXY and non-PROXY connections on same port (specification forbids it).
  • If your proxy also does TLS offload then it's possible to force the connection to be detected as TLS.
  • When TLS-offloading PP2_TYPE_ALPN is required to detect ALPN for HTTP2 connections, however not every sender supports it. You can use DetectApplicationProtocolByH2Preface as a workaround to detect protocol by presence of H2 client preamble, or use a single protocol (Http1/Http2) instead.
  • IProxyProtocolFeature will be set for all connections utilizing PROXY protocol.
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel((context, options) =>
{
    // overrides ASPNETCORE_URLS and enables PROXY protocol
    options.ListenAnyIP(8080, listenOptions =>
    {
        listenOptions.UseProxyProtocol(proxyProtocolOptions =>
            context.Configuration.GetSection("ProxyProtocol").Bind(proxyProtocolOptions)
        );
    });
    // enables PROXY protocol for all endpoints
    options.ConfigureEndpointDefaults(listenOptions =>
    {
        listenOptions.UseProxyProtocol();
    });

    var kestrelSection = context.Configuration.GetSection("Kestrel");
    // enables only for specific named endpoints inside config
    options.Configure(kestrelSection, reloadOnChange: true)
        // unencrypted endpoint
        .Endpoint("NamedEndpoint", endpointOptions => endpointOptions.ListenOptions.UseProxyProtocol()
        // endpoint with TLS-offload, http://<host>:<port>
        .Endpoint("OffloadTLS", options =>
         {
             // Protocols can also be set inside appsettings.json
             options.ListenOptions.Protocols = HttpProtocols.Http1AndHttp2;
             options.ListenOptions.UseProxyProtocol(new ProxyProtocolOptions
             {
                 // can also set DetectApplicationProtocolByH2Preface if ALPN is missing
                 TlsOptions = new ProxyProtocolTlsOptions { Enabled = true }
             });
         })
        // endpoint without TLS-offload, https://<host>:<port>
        .Endpoint("HttpsEndpoint", endpointOptions => endpointOptions.ListenOptions
            .UseProxyProtocol()
            .UseHttps()
        )
    );
});

var app = builder.Build();
app.MapGet("/", (HttpContext context) =>
{
    var feature = context.Features.Get<IProxyProtocolFeature>();
    if (feature != null)
    {
        context.Response.Headers["X-Connection-Orignal-Remote-EndPoint"] = feature.OriginalRemoteEndPoint?.ToString();
        context.Response.Headers["X-Connection-Orignal-Local-EndPoint"] = feature.OriginalLocalEndPoint?.ToString();
    }

    context.Response.Headers["X-Request-Protocol"] = context.Request.Protocol;
    context.Response.Headers["X-Connection-Remote-IP"] = context.Connection.RemoteIpAddress?.ToString();
    context.Response.Headers["X-Connection-Remote-Port"] = context.Connection.RemotePort.ToString();
    context.Response.Headers["X-Connection-Local-IP"] = context.Connection.LocalIpAddress?.ToString();
    context.Response.Headers["X-Connection-Local-Port"] = context.Connection.LocalPort.ToString();

    context.Response.Headers["X-Request-IsHttps"] = context.Request.IsHttps.ToString();
    return new { ProxyProtocol = feature?.ToString() };
});

app.MapWhen(c => c.Features.Get<IProxyProtocolFeature>() is null, b =>
{
    // map endpoints/controllers for connections that don't have PROXY protocol enabled.
});

app.Run();
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.6.0 3,329 8/25/2022
0.5.0 450 6/13/2022
0.4.0 431 5/30/2022
0.3.1 420 5/21/2022
0.3.0 450 4/23/2022
0.2.1 434 4/5/2022
0.1.0 419 4/2/2022