#!/bin/sh # START set -e # VARIABLES PROGNAME=$(basename $0) CONFIG=($HOME/.maza/) HOST_FILE=(/etc/hosts) COLOR_RED=`tput setaf 1` COLOR_GREEN=`tput setaf 2` COLOR_RESET=`tput sgr0` LIST="list" START_TAG="## MAZA - List ad blocking" PROJECT="### https://github.com/tanrax/maza-ad-blocking" AUTHOR="### Created by Andros Fenollosa (https://programadorwebvalencia.com/)" END_TAG="## END MAZA" # FUNCTIONS ## HELP usage() { if [ "$*" != "" ] ; then echo "Error: $*" fi cat << EOF Usage: $PROGNAME [OPTION] Simple and efficient local ad blocking throughout the network. Options: status Check if it's active or not update Update the list of DNS to be blocked start Activate blocking DNS. stop Stop blocking DNS. --help Display this usage message and exit EOF exit 1 } status() { if grep -qF "$START_TAG" "$HOST_FILE";then echo "${COLOR_GREEN}ENABLED${COLOR_RESET}" else echo "${COLOR_RED}DISABLED${COLOR_RESET}" fi } update() { # Make conf folder rm -f $CONFIG/$LIST mkdir -p $CONFIG # Download DNS list curl -L -s "https://pgl.yoyo.org/adservers/serverlist.php?showintro=0;hostformat=hosts" -o "$CONFIG/$LIST" # Clear list ## Get PRE tag sed -i .bak -n "/
/,/<\/pre>/p" "$CONFIG/$LIST"
    ## Remove comments
    sed -i .bak '/^#/ d' "$CONFIG/$LIST"
    ## Remove first line
    sed -i .bak '1d' "$CONFIG/$LIST"
    ## Remove last line
    sed -i .bak '$ d' "$CONFIG/$LIST"
    ## Add start tag DNS list in first line
    sed -i .bak "1i\\
        $AUTHOR
        " "$CONFIG/$LIST"
    sed -i .bak "1i\\
        $PROJECT
        " "$CONFIG/$LIST"
    sed -i .bak "1i\\
        $START_TAG
        " "$CONFIG/$LIST"
    # Remove temp file
    rm "$CONFIG$LIST.bak"
    ## Add end tag DNS list in first line
    echo $END_TAG >> "$CONFIG/$LIST"
    # Notify user
    echo "${COLOR_GREEN}Done!${COLOR_RESET}"
}

start() {
    # Add List to host file
    cat "$CONFIG/$LIST" >> "$HOST_FILE"
    # Notify user
    echo "${COLOR_GREEN}ENABLED!${COLOR_RESET}"
}

stop() {
    # Remove list to host file
    sed -i .bak -n "/$START_TAG/,/$END_TAG/d" "$HOST_FILE"

    # Remove temp file
    rm "$HOST_FILE.bak"

    # Notify user
    echo "${COLOR_GREEN}DISABLED!${COLOR_RESET}"
}

# CONTROLE ARGUMENTS
isArg=""

while [ $# -gt 0 ] ; do
    case "$1" in
    --help)
        usage
        ;;
    status)
        isArg="1"
        status
        ;;
    update)
        isArg="1"
        update
        ;;
    start)
        isArg="1"
        start
        ;;
    stop)
        isArg="1"
        stop
        ;;
    *)
    esac
    shift
done

if [ -z $isArg ] ; then
    usage "Not enough arguments"
fi