123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- #!/bin/bash
- VERSION=1.3.0
- # get the non-root user, so we can add the custom filters for them
- REGUSER=$(last | grep "logged in" | grep -o '^\S*')
- HOME_USER="/home/$REGUSER/autohosts/"
- # initialize reusable stuff, to make the terminal results prettier
- function croncheck() {
- crontab -l | grep -q 'autohosts' && EXIST=1 || EXIST=0
- }
- function green() {
- echo -e "\e[32m$1\e[0m"
- }
- function red() {
- echo -e "\e[0;31m$1\e[0m"
- }
- function brown() {
- echo -e "\e[0;33m$1\e[0m"
- }
- function purple() {
- echo -e "\e[0;35m$1\e[0m"
- }
- if [[ $EUID -ne 0 ]]; then
- red "This script must be ran as root, or with sudo privileges."
- exit 1
- fi
- ### setup ###
- # if this is the first run, move to appropriate directory and set a config file
- if [ ! -e /etc/autohosts.conf ];
- then
- # move the script to /usr/local/bin so it can be executed as an alias
- green "Creating an alias for autohosts.."
- cp autohosts /usr/local/bin/ && chmod +x /usr/local/bin/autohosts
- # migrate custom filters file to the user's home directory
- green "Creating custom filters directory at $REGUSER"
- mkdir "$HOME_USER" && cp custom_filters "$HOME_USER" && chown "$REGUSER:$REGUSER" -R "$HOME_USER"
- # double-check to ensure we don't have a conflicting cron
- croncheck
- if [ "$EXIST" == 0 ];
- then
- # if no cronjob exists, create one
- crontab -l | { cat; echo "22 19 * * 0 /usr/local/bin/autohosts > /dev/null 2>&1"; } | crontab -
- croncheck
- if [ "$EXIST" == 1 ];
- then
- green "Cronjob created..."
- else
- red "There was a problem creating the cronjob, you might have to create it manually!"
- fi
- fi
- # set a var that is later used to see if the application has been configured already
- echo FIRSTRUN=0 > /etc/autohosts.conf
- echo VERSION=$VERSION >> /etc/autohosts.conf
- purple "Setup is complete!"
- fi
- ### end setup ###
- source /etc/autohosts.conf
- purple "== UPDATING =="
- green "Obtaining hosts file from Steven Black's repo and LocalFigurez Firefox Telemetry filter..."
- # check os type
- if [[ "$OSTYPE" != "darwin"* ]];
- then
- # not a mac, use wget
- wget https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts -O /etc/hosts
- # include LocalFigurez Firefox snooping list
- wget https://raw.githubusercontent.com/angela-d/autohosts/master/firefox-includes -O ->> /etc/hosts
- else
- # mac.. use curl, instead
- curl -O https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts > /etc/hosts
- # append LocalFigurez Firefox snooping list
- curl https://raw.githubusercontent.com/angela-d/autohosts/master/firefox-includes >> /etc/hosts
- fi
- # check for and include any custom filters
- if [ -e "$HOME_USER"custom_filters ];
- then
- green "Processing custom filters..."
- cat "$HOME_USER"'custom_filters' >> /etc/hosts
- else
- brown "No custom filters found at home_user, skipping..."
- fi
- # if this is a mac, refresh the dns cache
- if [[ "$OSTYPE" == "darwin"* ]];
- then
- killall -HUP mDNSResponder
- fi
- # devdom: https://notabug.org/angela/devdom
- green "Checking if devdom is installed (will execute devdom hosts update, if so)"
- if [ -e /usr/bin/devdom ];
- then
- devdom hosts
- green "devdom hosts have been appended..."
- else
- brown "Devdom is not installed; skipping."
- fi
- purple "Hosts update complete!"
|