Skip to content

Instantly share code, notes, and snippets.

@marsam
Last active August 29, 2015 14:03
Show Gist options
  • Save marsam/4007055e81d139d5bb09 to your computer and use it in GitHub Desktop.
Save marsam/4007055e81d139d5bb09 to your computer and use it in GitHub Desktop.
Get shit DONE
#!/usr/bin/env bash
#
# TODO:
# - Use tc/netem: http://linux.die.net/man/8/tc
set -eo pipefail
typeset config="$HOME/.shitdone.conf"
typeset iptables_bkp="$HOME/shitdone.iptables.rules"
if [[ ! -e $config ]]; then
config=$PWD/shitdone.conf
fi
is_valid_domain() {
[[ "$1" =~ ^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,6}$ ]]
}
is_empty_file() {
[[ ! -s "$1" ]]
}
enable_shitdone() {
if [[ -f "${iptables_bkp}" ]]; then
echo "${iptables_bkp} exists, is '$(basename $0)' already applied?"
exit 1
fi
domains=$(cat "${config}")
iptables-save > "${iptables_bkp}"
for domain in $domains; do
if is_valid_domain "${domain}"; then
iptables -A INPUT -p tcp -m string --string "${domain}" --algo kmp -j DROP
iptables -A OUTPUT -p tcp -m string --string "${domain}" --algo kmp -j DROP
else
echo "Sorry, ${domain} is not a valid domain... skiping"
fi
done
echo "... ready? get shit done!"
}
disable_shitdone() {
if [[ ! -f "${iptables_bkp}" ]]; then
echo "${iptables_bkp} doesn't exists. Are you sure '$(basename $0)' is applied?"
exit 1
fi
if is_empty_file "${iptables_bkp}"; then
iptables -F
else
iptables-restore < "${iptables_bkp}"
fi
rm "${iptables_bkp}"
echo "... $(basename $0) disabled"
}
case "$1" in
enable)
enable_shitdone
;;
disable)
disable_shitdone
;;
*)
echo "Usage: $(basename $0) {enable|disable}"
esac
imgur.com
reddit.com
twitter.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment