TProxy
TProxy (transparent proxy) is a Linux kernel facility for transparently proxying TCP and UDP traffic. The client neither configures anything nor knows the proxy exists, which is valuable for load balancing, security monitoring and network optimisation.
Characteristics
- Transparency: the client needs no configuration and is unaware of the proxy, which can intercept and process the traffic anyway.
- TCP and UDP: both are supported, so all kinds of network traffic can be proxied.
- Flexibility: combined with iptables, redirection and processing rules can be expressed precisely.
- Non-disruptive: because it works at the network layer, proxying is seamless and does not break the conversation.
How it works
TProxy operates in the Linux kernel's network layer: it rewrites the destination address of IP packets so that traffic is redirected to the proxy. The steps are:
- Capture: iptables captures the traffic and redirects it into the TProxy module.
- Process: TProxy receives the redirected traffic, processes it and forwards it to the destination.
- Respond: the destination's replies travel back through TProxy, and the client believes it has been talking to the destination directly.
Configuration
Example steps for proxying TCP and UDP transparently.
Prerequisites
- A working Linux system.
- A kernel that supports TProxy (2.6.28 or later).
iptablesandiproute2installed.
Load kernel modules
modprobe xt_TPROXY
modprobe nf_tproxy_coreConfigure iptables
Create the mangle rules that capture and redirect traffic to the local proxy port.
bashiptables -t mangle -N DIVERT iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT iptables -t mangle -A PREROUTING -p udp -m socket -j DIVERT iptables -t mangle -A DIVERT -j MARK --set-mark 1 iptables -t mangle -A DIVERT -j ACCEPTAdd PREROUTING rules that redirect traffic to TProxy.
bashiptables -t mangle -A PREROUTING -p tcp -j TPROXY --tproxy-mark 0x1/0x1 --on-port 12345 iptables -t mangle -A PREROUTING -p udp -j TPROXY --tproxy-mark 0x1/0x1 --on-port 12345
Configure ip rule and ip route
Set the ip rule so marked traffic uses the custom table.
baship rule add fwmark 1 lookup 100Set the route table so that table 100 delivers traffic locally.
baship route add local 0.0.0.0/0 dev lo table 100
Example: proxying HTTP with TProxy
If an HTTP proxy listens on local port 12345, the following rules proxy HTTP traffic transparently.
iptables
iptables -t mangle -A PREROUTING -p tcp --dport 80 -j TPROXY --tproxy-mark 0x1/0x1 --on-port 12345
iptables -t mangle -A PREROUTING -p tcp --dport 443 -j TPROXY --tproxy-mark 0x1/0x1 --on-port 12345Use cases
- Load balancing: spread traffic across several servers transparently.
- Security monitoring: inspect and filter traffic, detecting and blocking malicious behaviour.
- Network optimisation: cache and shape traffic through the proxy to improve performance.
- Access control: grant or deny access based on traffic characteristics.
Trade-offs
Advantages
- Transparent to clients, with no client-side configuration.
- Covers both TCP and UDP.
- Flexible rules thanks to iptables.
Drawbacks
- Requires solid networking and maintenance skills.
- Adds load to the proxy host, so the hardware must keep up.
Summary
TProxy is a powerful transparent proxy facility for any scenario that needs to intercept traffic invisibly. Together with iptables and iproute2 it captures and redirects TCP and UDP traffic to implement load balancing, monitoring and optimisation. Configuration is involved, but the flexibility is why network administrators and developers rely on it.
