Skip to content

Instantly share code, notes, and snippets.

@jmoiron
Created August 1, 2012 18:39
Show Gist options
  • Save jmoiron/3229676 to your computer and use it in GitHub Desktop.
Save jmoiron/3229676 to your computer and use it in GitHub Desktop.
Toggle masquerading as a domain, forwarding configurable port to 80 (OSX and Linux)
#!/bin/bash
PORT="8000"
usage() {
cat << EOF
usage: $0 host [port]
EOF
}
redirect() {
if [ $OS = "Linux" ]; then
sudo iptables -t nat -A OUTPUT -d $HOST -p tcp --dport 80 -j REDIRECT --to-ports $PORT
else
sudo ipfw add 100 fwd $HOST,$PORT tcp from any to any 80 in
fi
echo $cmd
}
flush() {
if [ $OS = "Linux" ]; then
sudo iptables -F
sudo iptables -t nat -F
else
echo "y" | sudo ipfw flush
fi
}
if [ -z "$1" ]; then
usage
exit 1
fi
if [ -n "$2" ]; then
PORT="$2"
fi
HOST="$1"
if [ -f "/etc/hosts.masq" ]; then
echo "Turning off masquerading for $HOST"
sudo rm /etc/hosts.masq
flush
cat /etc/hosts |grep -v "127.0.0.1 $HOST" > /tmp/hosts
sudo mv /tmp/hosts /etc/
else
echo "Turning ON masquerading for $HOST"
echo "127.0.0.1 $HOST" | sudo tee -a /etc/hosts > /dev/null
redirect
sudo touch /etc/hosts.masq
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment