1
0
mirror of https://github.com/balkian/dotfiles.git synced 2024-11-14 16:52:29 +00:00
dotfiles/scripts/.bin/nodistractions.sh

68 lines
1.6 KiB
Bash
Raw Permalink Normal View History

2018-09-12 17:04:59 +00:00
#!/bin/sh
2018-10-09 14:30:18 +00:00
# This script will block all traffic to ports 80 and 443.
#
2018-09-12 17:04:59 +00:00
# If you want to allow some websites/IPs to still work
2018-10-09 14:30:18 +00:00
# after adding the filters.
#
# Set the $ALLOWED_DEST variable to the network/mask you want to accept
#
#
# Another option is to add specific rules to
2018-09-12 17:04:59 +00:00
# iptables to allow it. For instance:
#
# sudo iptables --insert OUTPUT --protocol tcp --destination 138.4.0.0/16 --jump ACCEPT
#
# iptables rules are interpreted top to bottom.
# --append adds rules to the end of the file
# --insert adds rules to the top of the file
# Hence, you can either append the rule before running
# the script, or insert the rule so it takes precedence.
2018-10-09 14:30:18 +00:00
ALLOWED_DEST=${ALLOWED_DEST:=138.4.0.0/16}
2018-09-12 17:04:59 +00:00
export SUDO_ASKPASS=/usr/lib/ssh/ssh-askpass
CMD="sudo -A iptables"
RULE="OUTPUT --protocol tcp --jump DROP --dport"
2018-10-09 14:30:18 +00:00
ALLOW_RULE="OUTPUT --protocol tcp --jump ACCEPT --destination $ALLOWED_DEST"
2018-09-12 17:04:59 +00:00
stop_filter() {
$CMD --delete $RULE 80
$CMD --delete $RULE 443
2018-10-09 14:30:18 +00:00
$CMD --delete $ALLOW_RULE
2018-09-12 17:04:59 +00:00
}
filter() {
stop_filter >&2 /dev/null # Avoid re-adding
$CMD --append $RULE 80
$CMD --append $RULE 443
2018-10-09 14:30:18 +00:00
$CMD --insert $ALLOW_RULE
2018-09-12 17:04:59 +00:00
}
help() {
2018-10-09 14:30:18 +00:00
echo "Block all traffic to ports 80 and 443"
echo ""
2018-09-12 17:04:59 +00:00
echo "Usage: $0 on|off"
2018-10-09 14:30:18 +00:00
echo ""
echo "Set the ALLOWED_DEST variable to whitelist some IPs/network"
echo "Currently whitelisted: $ALLOWED_DEST"
2018-09-12 17:04:59 +00:00
}
if [ "$#" -ne 1 ]; then
filter
else
case "$1" in
"on")
filter
;;
"off")
zenity --question --text="Are you sure you want to let distractions in?" && stop_filter
;;
*)
help
esac
fi