Last active
December 25, 2015 17:59
-
-
Save jakebellacera/7016584 to your computer and use it in GitHub Desktop.
focus - blacklists certain websites
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
################################################################################ | |
# FOCUS | |
# ===== | |
# Blacklists certain websites. | |
# | |
# How to use: | |
# 1. Edit the DOMAINS variable with the domains you'd like to blacklist | |
# 2. run `/path/to/focus start` to start focusing | |
# 3. run `/path/to/focus stop` to stop focusing | |
# | |
# Usage: | |
# focus start starts focusing | |
# focus stop stops focusing | |
# | |
# Notes: | |
# * You should include this script in your PATH so you can run focus from | |
# anywhere. | |
# * You must include a domain rule for each subdomain (e.g. example.com and | |
# www.example.com). | |
# * If you change your domains while you are focusing, you will need to stop | |
# focusing and then start again. | |
# * If you're running a webserver listening on localhost, blocked websites | |
# that you'd normally visit over HTTPS will throw, in most browsers, a SSL | |
# certificate error. Rest assured that this is the expected behavior and it | |
# will not occur after you stop focusing. | |
################################################################################ | |
# The domains you want to blacklist | |
DOMAINS=(example.com www.example.com foobar.com www.foobar.com) | |
# Other stuff | |
BEGIN_CAP="# BEGIN focus" | |
END_CAP="# END focus" | |
REDIRECT_IP="127.0.0.1" | |
HOSTS_FILE=/etc/hosts | |
TMP_FILE=/tmp/focus_hosts | |
if [[ -n "$1" ]]; then | |
# start arg | |
if [[ "$1" == "start" ]]; then | |
# Prompt for password | |
if [ $EUID != 0 ]; then | |
sudo "$0" "$@" | |
exit $? | |
fi | |
# First, check if we're already focusing | |
if [[ ! -z "$(sed -n "/$BEGIN_CAP/p" /etc/hosts)" ]]; then | |
echo "You're already focusing, young padawan." | |
exit 0 | |
fi | |
# Append domains | |
echo "Focusing..." | |
echo -e $BEGIN_CAP"\n$(printf "$REDIRECT_IP\t%s\n" "${DOMAINS[@]}")\n"$END_CAP >> $HOSTS_FILE | |
sudo dscacheutil -flushcache | |
# stop arg | |
elif [[ "$1" == "stop" ]]; then | |
# Prompt for password | |
if [ $EUID != 0 ]; then | |
sudo "$0" "$@" | |
exit $? | |
fi | |
echo "Stopping..." | |
sed "/^$BEGIN_CAP/,/^$END_CAP/d" /etc/hosts > $TMP_FILE | |
sudo mv $TMP_FILE $HOSTS_FILE | |
sudo dscacheutil -flushcache | |
fi | |
else | |
echo -e "Available commands are:\n focus start\t\tstart focusing\n focus stop\t\tstops focusing" | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment