Class ProxyProtocolOriginalDestinationHandler

java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
org.mockserver.netty.proxy.ProxyProtocolOriginalDestinationHandler
All Implemented Interfaces:
io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler

public class ProxyProtocolOriginalDestinationHandler extends io.netty.channel.ChannelInboundHandlerAdapter
Inbound handler that detects and parses PROXY protocol v1 (text format) and v2 (binary format) headers prepended to TCP connections by upstream load balancers (e.g., AWS GWLB, HAProxy, nginx with proxy_protocol on).

Placement: This handler must be added FIRST in the transparent-proxy pipeline (before TransparentProxyHandler and the port-unification handler). It inspects the first inbound bytes and dispatches on the first byte:

  • 0x0D — candidate PROXY v2 (binary): the 12-byte v2 signature begins with 0x0D, which can never start a v1 header, an HTTP request, or a TLS record.
  • 'P' — candidate PROXY v1 (text): the "PROXY " ASCII signature.
  • anything else — not a PROXY header; pass through unchanged.
In all cases the handler consumes any recognised header, sets the REMOTE_SOCKET / PROXYING / TRANSPARENT_ORIGINAL_DST_RESOLVED channel attributes, and removes itself from the pipeline.

PROXY v1 format (HAProxy spec):

 PROXY TCP4 <srcIP> <dstIP> <srcPort> <dstPort>\r\n
 PROXY TCP6 <srcIP> <dstIP> <srcPort> <dstPort>\r\n
 PROXY UNKNOWN\r\n
 
Maximum v1 header length is 107 bytes (per the spec). The handler enforces this bound.

PROXY v2 format (HAProxy spec): a 12-byte binary signature (0D 0A 0D 0A 00 0D 0A 51 55 49 54 0A) followed by a 4-byte prefix (version+command, address-family+transport, and a big-endian uint16 address-block length), then the address block. For the PROXY command (LOCAL is a health-check with no address) and the INET / INET6 families, the destination address and port are extracted. UNIX-socket and unsupported families are consumed but leave destination resolution to downstream strategies.

Fail-safe: Malformed, oversized, or unrecognised headers cause the handler to remove itself and pass through all bytes unchanged, logging a warning. The connection continues as if no PROXY header was present.

See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler

    io.netty.channel.ChannelHandler.Sharable
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    channelRead(io.netty.channel.ChannelHandlerContext ctx, Object msg)
     
    void
    handlerRemoved(io.netty.channel.ChannelHandlerContext ctx)
     

    Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter

    channelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered

    Methods inherited from class io.netty.channel.ChannelHandlerAdapter

    ensureNotSharable, handlerAdded, isSharable

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface io.netty.channel.ChannelHandler

    handlerAdded
  • Constructor Details

    • ProxyProtocolOriginalDestinationHandler

      public ProxyProtocolOriginalDestinationHandler(MockServerLogger logger)
  • Method Details

    • channelRead

      public void channelRead(io.netty.channel.ChannelHandlerContext ctx, Object msg) throws Exception
      Specified by:
      channelRead in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      channelRead in class io.netty.channel.ChannelInboundHandlerAdapter
      Throws:
      Exception
    • handlerRemoved

      public void handlerRemoved(io.netty.channel.ChannelHandlerContext ctx)
      Specified by:
      handlerRemoved in interface io.netty.channel.ChannelHandler
      Overrides:
      handlerRemoved in class io.netty.channel.ChannelHandlerAdapter