Class TproxyOriginalDestinationResolver
- All Implemented Interfaces:
TransparentProxyHandler.OriginalDestinationResolver
With TPROXY iptables rules (as opposed to REDIRECT), the kernel preserves the
original destination address as the socket's local address. The listener
socket must be bound with the IP_TRANSPARENT socket option so the kernel
allows binding to non-local addresses, and the iptables rules must use the
-j TPROXY target instead of -j REDIRECT.
Resolution is trivial: channel.localAddress() returns the original
destination directly (the pre-TPROXY destination). No conntrack table lookup or
getsockopt(SO_ORIGINAL_DST) is needed.
Requirements:
- Linux with Netty epoll transport
CAP_NET_ADMINcapability (forIP_TRANSPARENTsetsockopt)- TPROXY iptables rules instead of REDIRECT:
iptables -t mangle -A PREROUTING -p tcp --dport <target-port> \ -j TPROXY --tproxy-mark 0x1/0x1 --on-port <mockserver-port> ip rule add fwmark 1 lookup 100 ip route add local 0.0.0.0/0 dev lo table 100 - The
IP_TRANSPARENTsocket option set on the listener socket (wired inMockServerIpTransparentHelperor viaEpollChannelOption.IP_TRANSPARENT) - Configuration flag:
mockserver.transparentProxyTproxy=true
Chain position: in the CompositeOriginalDestinationResolver default
chain, TPROXY is placed first (before SO_ORIGINAL_DST and conntrack). When
TPROXY mode is active, the local address is the authoritative original destination;
when inactive, this resolver returns null and the chain falls through to
SO_ORIGINAL_DST / conntrack.
Difference from REDIRECT: with REDIRECT, the socket's local address is the
MockServer listen address (the redirect target), so channel.localAddress()
is useless for original-destination recovery. With TPROXY, the local address IS
the original destination.
-
Constructor Summary
ConstructorsConstructorDescriptionTproxyOriginalDestinationResolver(Configuration configuration) Creates a TPROXY resolver that checks whether TPROXY mode is active via the given configuration. -
Method Summary
Modifier and TypeMethodDescriptionresolve(io.netty.channel.Channel channel) Resolves the original destination by reading the channel's local address.
-
Constructor Details
-
TproxyOriginalDestinationResolver
Creates a TPROXY resolver that checks whether TPROXY mode is active via the given configuration.- Parameters:
configuration- the MockServer configuration (used to readtransparentProxyTproxy)
-
-
Method Details
-
resolve
Resolves the original destination by reading the channel's local address.Returns
nullwhen:- TPROXY mode is not enabled in configuration
- The channel's local address is null or not an
InetSocketAddress
- Specified by:
resolvein interfaceTransparentProxyHandler.OriginalDestinationResolver- Parameters:
channel- the accepted Netty channel- Returns:
- the original destination (from the local address), or
null
-