### ### Configuration settings ### # maps entries to IPv4 0.0.0.0 (default) # ENDPOINT_IP4="0.0.0.0" ENDPOINT_IP4=$(uci get network.lan.ipaddr) # can be used if you want to use a local web server to suppress connection error requests # directory where the compressed host files are being stored persistently PERSISTENTDIR="/usr/share/inithost/hostfiles" # directory where dnsmasq expects the active uncompressed host files ACTIVEHOSTDIR="/tmp/hosts" # if you change this default you will need to add a 'addnhost' config entry in /etc/dnsmasq.conf # Use rule files in this directory to rewrite /etc/dnsmasq.conf to apply certain DNS rules # for hosts which satisfy wildcard expressions RULESDIR="/usr/share/inithost/dnsmasq_rules" # Define whitelists HOSTWHITELIST="$PERSISTENTDIR/whitelist" RULEWHITELIST="$RULESDIR/whitelist" ### ### Functions -- if other host files are used from the web these need to get extended ### # for a better understanding read also /etc/init.d/inithost ### filter_whitelist # remove white listed items from the contents of a host file and prepends the routed IP # no params filter_whitelist() { awk ' BEGIN{ while (getline<"'$HOSTWHITELIST'"){wl[$1]=1} } { if (!($1 in wl)) print "'$ENDPOINT_IP4'",$0; } ' } ### update_active_hostfile # updates an active host file from the web and prepares it for use with dnsmasq # $1 -- name of the active host file # $2 -- URL from where to fetch the updated host file update_active_hostfile() { # declare the URL where to fetch the host file hname="$1" url="$2" hf="$PERSISTENTDIR/$hname" hfl=$(readlink $hf) # by default we'd avoid frequent updates local needs_updating="0" # does an compressed persistent host file exist? if [ -f ${hf}.gz ]; then # Yes! then compare time stamps of persistent host file and web host file logger "check ${hf}.gz for update..." remote_mtime=$(last_mtime $url) local_mtime=$(date +%s -r ${hf}.gz) # if not disabled and host file is newer than local copy perform an update if [ "$hfl" == "/dev/null" ]; then logger "disabled by configuration" elif [ "$remote_mtime" -gt "$local_mtime" ]; then needs_updating="1" else logger "already up-to-date" fi else # Does an uncompressed persistent host file exist? if [ -f $hf ]; then logger "check $hf for update..." remote_mtime=$(last_mtime $url) local_mtime=$(date +%s -r $hf) # if host file is newer perform an update if [ "$remote_mtime" -gt "$local_mtime" ]; then needs_updating="1" else logger "already up-to-date" fi else # No! We probably retrieve it from the web for the first time! needs_updating="1" fi fi if [[ "$needs_updating" -eq "1" ]]; then logger "updating $hf ..." curl -m 20 -s $url | hostfilter "$hname" |\ sed 's/^0.0.0.0[[:space:]]*//' | sort -u |\ awk 'NF==1{print}' | filter_whitelist > $hf && touch $hf.gz fi } hostfilter() { case $1 in "mvps") grep 0.0.0.0 | sed 's/[[:space:]]*#.*$//g' | grep -v localhost | tr -d '\r' | tr -s '\t' | tr -d '\015' ;; "adaway") grep -v "#" | grep -v "::1" | sed '/^$/d' | sed 's/\ /\\ /g' | awk '{print $2}' | grep -v '^\\' | grep -v '\\$' ;; "hfnet") grep -v "#" | grep -v "::1" | sed '/^$/d' | sed 's/\ /\\ /g' | awk '{print $2}' | grep -v '^\\' | grep -v '\\$' ;; "mwd") grep -v "#" | sed '/^$/d' | sed 's/\ /\\ /g' | awk '{print $3}' | grep -v '^\\' | grep -v '\\$' ;; "sowc") grep -v "#" | sed '/^$/d' | sed 's/\ /\\ /g' | grep -v '^\\' | grep -v '\\$' | awk '{print $2}' | grep -v '^\\' | grep -v '\\$' ;; "gjtech") grep -v "#" | sed '/^$/d' | sed 's/\ /\\ /g' | awk '{print $2}' | grep -v '^\\' | grep -v '\\$' ;; "yoyo") grep -v "#" | grep -v '^<' | grep -v '^ ' | grep -v '^$(printf '\t')' ;; "quidsup") grep -v "#" ;; *) awk '{print "filter undefined"; exit(1)}' ;; esac } ### MAIN FUNCTION # for any new host file add a new entry here! update_hostfiles_from_web() { update_active_hostfile "mvps" "http://winhelp2002.mvps.org/hosts.txt" update_active_hostfile "adaway" "http://adaway.org/hosts.txt" update_active_hostfile "hfnet" "http://hosts-file.net/.%5Cad_servers.txt" update_active_hostfile "mwd" "http://www.malwaredomainlist.com/hostslist/hosts.txt" update_active_hostfile "sowc" "http://someonewhocares.org/hosts/hosts" update_active_hostfile "gjtech" "http://adblock.gjtech.net/?format=unix-hosts" update_active_hostfile "yoyo" "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=;showintro=0" update_active_hostfile "quidsup" "https://raw.githubusercontent.com/quidsup/notrack/master/trackers.txt" # static host files (no web updates available): # $PERSISTENTDIR/custom # $PERSISTENTDIR/porn }