#!/bin/bash
# TCP Proxy using IPTables
IPTABLES=/sbin/iptables
echo 1 > /proc/sys/net/ipv4/ip_forward
# Flush nat table
$IPTABLES -t nat -F
# tcpproxy LOCAL_IP LOCAL_PORT REMOTE_IP REMOTE_PORT
function tcpproxy()
{
$IPTABLES -t nat -A PREROUTING --dst $1 -p tcp --dport $2 -j DNAT --to-destination $3:$4
$IPTABLES -t nat -A POSTROUTING --dst $3 -p tcp --dport $4 -j SNAT --to-source $1
$IPTABLES -t nat -A OUTPUT --dst $1 -p tcp --dport $2 -j DNAT --to-destination $3:$4
}
tcpproxy 192.168.11.152 9998 192.168.19.45 8888
将发向
192.168.11.152 9998
的请求代理到
192.168.19.45 8888