Skip to content

Instantly share code, notes, and snippets.

@brnrc
Last active December 16, 2015 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brnrc/6b7103112380a7500a69 to your computer and use it in GitHub Desktop.
Save brnrc/6b7103112380a7500a69 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -o errexit
# Aliases
IPT=/sbin/iptables
printf 'Cleaning the environment... '
# Delete the PROXY chain
($IPT --table nat --delete-chain PROXY || true) 2> /dev/null # ignore error
echo "Done"
# Create new chain
$IPT --table nat -N PROXY
# Any tcp connection made by group `socksified' or user root should be redirected.
$IPT --table nat --append OUTPUT --protocol tcp --match owner ! --uid-owner redsocks --jump PROXY
# Ignore LANs and some other reserved addresses.
# See http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses
# and http://tools.ietf.org/html/rfc5735 for full list of reserved networks.
noproxy=(
'0.0.0.0/8'
'10.0.0.0/8'
'127.0.0.0/8'
'169.254.0.0/16'
'172.16.0.0/12'
'192.168.0.0/16'
'224.0.0.0/4'
'240.0.0.0/4'
# add any other local address here
)
for ip in "${noproxy[@]}"; do
echo "Skipping proxy for ${ip}"
$IPT --table nat --append PROXY -d ${ip} --jump RETURN
done
# Log everything that goes through the PROXY chain to syslog
$IPT --table nat --append PROXY --protocol tcp --jump LOG --log-level debug --log-prefix "[PROXY]"
# Anything else should be redirected to port 8080
$IPT --table nat --append PROXY --protocol tcp --jump REDIRECT --to-ports 8080
# printf "\n\nHere are your new iptables rules:\n"
# $IPT --table nat -nvL PROXY --line-numbers
echo "Dont forget to configure redsocks"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment