TProxy
TProxy (transparent proxy) is a Linux kernel module used to transparently proxy TCP and UDP traffic. The defining feature of a transparent proxy is that clients are unaware their traffic is passing through a proxy server, which makes it highly useful in scenarios such as load balancing, security monitoring, and network optimization. The following is a detailed introduction to TProxy:
Characteristics of TProxy
- Transparency: clients require no configuration and need not be aware of the proxy's existence; the proxy server can intercept and process traffic.
- TCP/UDP support: TProxy supports both TCP and UDP traffic, and can proxy various types of network communication.
- Flexibility: combined with iptables, it allows traffic redirection and handling rules to be defined flexibly.
- Non-disruptive: because TProxy operates at the network layer, traffic proxying is seamless to clients and does not interrupt communication.
How It Works
TProxy operates at the network layer of the Linux kernel, transparently redirecting traffic to the proxy server for processing by modifying the destination address of IP packets. The specific steps are as follows:
- Capturing traffic: use iptables to capture traffic and redirect it to the TProxy module.
- Proxy processing: TProxy receives the redirected traffic, processes it, and forwards it to the target server.
- Returning responses: response traffic from the target server is returned to the client through TProxy, and the client believes it is communicating directly with the target server.
Configuration Steps
The following are example steps for configuring TProxy to transparently proxy TCP and UDP traffic:
Prerequisites
- A Linux operating system installed and configured.
- A kernel version that supports TProxy (Linux kernel 2.6.28 or later).
- The iptables and iproute2 tools installed.
Loading Kernel Modules
modprobe xt_TPROXY
modprobe nf_tproxy_coreConfiguring iptables
Creating mangle table rules: Capture traffic and redirect it to a 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 ACCEPTAdding PREROUTING rules: 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
Configuring ip rule and ip route
Setting up ip rule: Define routing rules for marked traffic.
baship rule add fwmark 1 lookup 100Setting up the routing table: Configure routing table 100 to redirect traffic locally.
baship route add local 0.0.0.0/0 dev lo table 100
Example: Proxying HTTP Traffic with TProxy
Suppose you have an HTTP proxy server listening on local port 12345. The following configuration can be used to transparently proxy HTTP traffic:
Configuring 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: TProxy can transparently distribute traffic across multiple servers to achieve load balancing.
- Security monitoring: a transparent proxy can monitor and filter network traffic, detecting and blocking malicious activity.
- Network optimization: caching and optimizing traffic through a proxy server improves network performance.
- Access control: traffic-based access control and permission management.
Advantages and Disadvantages
Advantages:
- Transparent to clients; no client configuration changes required.
- Supports both TCP and UDP traffic, giving it broad applicability.
- Combined with iptables, rule configuration is flexible.
Disadvantages:
- Requires considerable networking expertise for configuration and maintenance.
- Increases the proxy server's load, placing higher demands on hardware.
Summary
TProxy is a powerful transparent proxy tool suited to scenarios that require transparent proxying and traffic handling. Combined with iptables and iproute2, TProxy can flexibly capture and redirect TCP and UDP traffic to implement load balancing, security monitoring, and network optimization. Despite its configuration complexity, its power and flexibility make it an important tool for network administrators and developers.
