#!/bin/sh # Version: 2.1.3 # Author: Héctor Molinero Fernández # Repository: https://github.com/hectorm/hblock # License: MIT, https://opensource.org/licenses/MIT set -eu export LC_ALL=C # shellcheck disable=SC2039 HOSTNAME=${HOSTNAME-$(uname -n)} # Builtin values BUILTIN_HEADER=$(cat <<-EOF 127.0.0.1 localhost $HOSTNAME 255.255.255.255 broadcasthost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ff02::3 ip6-allhosts EOF ) BUILTIN_FOOTER='' BUILTIN_SOURCES=$(cat <<-'EOF' https://raw.githubusercontent.com/hectorm/hmirror/master/data/adaway.org/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/adblock-nocoin-list/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/adguard-simplified/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/antipopads/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/disconnect.me-ad/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/disconnect.me-malvertising/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/disconnect.me-malware/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/disconnect.me-tracking/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/dshield.org-high/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/easylist/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/easyprivacy/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/eth-phishing-detect/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/fademind-add.2o7net/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/fademind-add.dead/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/fademind-add.risk/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/fademind-add.spam/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/hostsvn/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/kadhosts/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/malwaredomainlist.com/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/malwaredomains.com-immortaldomains/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/malwaredomains.com-justdomains/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/matomo.org-spammers/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/mitchellkrogza-badd-boyz-hosts/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/pgl.yoyo.org/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/phishing.army/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/ransomwaretracker.abuse.ch/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/someonewhocares.org/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/spam404.com/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/stevenblack/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/ublock/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/ublock-badware/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/ublock-privacy/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/winhelp2002.mvps.org/list.txt https://raw.githubusercontent.com/hectorm/hmirror/master/data/zerodot1-coinblockerlists-browser/list.txt EOF ) BUILTIN_WHITELIST='' BUILTIN_BLACKLIST='' # Check if a program exists exists() { # shellcheck disable=SC2230 if command -v true; then command -v -- "$1" elif eval type type; then eval type -- "$1" else which -- "$1"; fi >/dev/null 2>&1 } # Escape strings in sed # See: https://stackoverflow.com/a/29613573 quoteRe() { printf -- '%s' "$1" | sed -e 's/[^^]/[&]/g; s/\^/\\^/g; $!a'\\''"$(printf '\n')"'\\n' | tr -d '\n'; } quoteSubst() { printf -- '%s' "$1" | sed -e ':a' -e '$!{N;ba' -e '}' -e 's/[&/\]/\\&/g; s/\n/\\&/g'; } # Remove comments from string removeComments() { printf -- '%s' "$1" | sed -e 's/[[:blank:]]*#.*//'; } # Translate true/false to yes/no getBoolVal() { [ "$1" = true ] && s='yes' || s='no'; printf -- '%s' "$s"; } # Print to stdout if quiet mode is not enabled printStdout() { if [ "$quiet" != true ]; then # shellcheck disable=SC2059 printf -- "$@" fi } # Print to stderr printStderr() { # shellcheck disable=SC2059 >&2 printf -- "$@" } # Print informational message logInfo() { printStdout ' - %s\n' "$@" } # Print action message logAction() { if [ "$color" = true ]; then printStdout '\033[1;33m + \033[1;32m%s \033[0m\n' "$@" else printStdout ' + %s \n' "$@" fi } # Print error message logError() { if [ "$color" = true ]; then printStderr '\033[1;33m + \033[1;31m%s \033[0m\n' "$@" else printStderr ' + %s \n' "$@" fi } # Create temporary file createTempFile() { if exists mktemp; then mktemp else # Since POSIX does not specify mktemp utility, use this as fallback tempCounter=${tempCounter:-9999} tempFile="${TMPDIR:-/tmp}/hblock.$$.$((tempCounter+=1))" rm -f -- "$tempFile" && (umask 077 && touch -- "$tempFile") printf -- '%s\n' "$tempFile" fi } # Print to stdout the contents of a URL fetchUrl() { # If the protocol is "file://" we can omit the download and simply use cat if [ "${1#file://}" != "$1" ]; then cat -- "${1#file://}" else userAgent='Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0' if exists curl; then curl -fsSL -A "$userAgent" -- "$1"; elif exists wget; then wget -qO- -U "$userAgent" -- "$1"; else logError 'Either wget or curl are required for this script' exit 1 fi fi } # Show help and quit showHelp() { if [ $# -eq 0 ]; then printStdout '%s\n' "$(cat <<-'EOF' Usage: hblock [options...] -O, --output Output file location (default: "/etc/hosts" file) -H, --header Content to be included at the beginning of the output file (default: HBLOCK_HEADER environment variable, "/etc/hblock.d/header" file or builtin value) -F, --footer Content to be included at the end of the output file (default: HBLOCK_FOOTER environment variable, "/etc/hblock.d/footer" file or builtin value) -S, --sources Newline separated URLs used to generate the blocklist (default: HBLOCK_SOURCES environment variable, "/etc/hblock.d/sources.list" file or builtin value) -W, --whitelist Newline separated domains to be removed from the blocklist (default: HBLOCK_WHITELIST environment variable, "/etc/hblock.d/whitelist.list" file or builtin value) -B, --blacklist Newline separated domains to be added to the blocklist (default: HBLOCK_BLACKLIST environment variable, "/etc/hblock.d/blacklist.list" file or builtin value) -R, --redirection Redirection for all entries in the blocklist (default: 0.0.0.0) -T, --template