Skip to content

Instantly share code, notes, and snippets.

@thomseddon
Created February 25, 2015 20:31
Show Gist options
  • Save thomseddon/9cb84c71f50779440ea3 to your computer and use it in GitHub Desktop.
Save thomseddon/9cb84c71f50779440ea3 to your computer and use it in GitHub Desktop.
Create proxies in iptables
#!/bin/bash
#
# Copyright Thom Seddon 2015
# MIT
#
if [ "$1" == "" ] || [ "$1" == "help" ]; then
echo "Usage: ~/.proxy <option>"
echo
echo " Options: "
echo " create: <inport> <outport> [protocol=tcp]"
echo " delete: <num>"
echo " help"
echo " show"
exit 1
fi
if [ "$(id -u)" != "0" ]; then
echo "Must run with sudo"
exit 1
fi
if [ "$1" == "create" ]; then
if [ "$3" == "" ]; then
echo "Wrong params"
exit 1
fi
if [ "$4" == "" ]; then
proto=tcp
else
proto=$4
fi
inip=127.0.0.1
inport=$2
outip=127.0.0.1
outport=$3
#iptables -t nat -A PREROUTING --dst $inip -p tcp --dport $inport -j DNAT --to-destination $outip:$outport
#iptables -t nat -A POSTROUTING -p tcp --dst $outip --dport $outport -j SNAT --to-source $inip
#iptables -t nat -A OUTPUT --dst $inip -p tcp --dport $inport -j DNAT --to-destination $outip:$outport
iptables -t nat -A PREROUTING -p $proto --dport $inport -j DNAT --to-destination $outip:$outport
iptables -t nat -A POSTROUTING -p $proto --dst $outip --dport $outport -j SNAT --to-source $inip
iptables -t nat -A OUTPUT -p $proto --dport $inport -j DNAT --to-destination $outip:$outport
fi
if [ "$1" == "delete" ]; then
if [ "$2" == "" ]; then
echo "Wrong params"
exit 1
fi
iptables -t nat -D PREROUTING $2
iptables -t nat -D POSTROUTING $2
iptables -t nat -D OUTPUT $2
fi
if [ "$1" == "show" ]; then
iptables -t nat -vnL --line-numbers
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment