#!/bin/sh
#
# license: Standard BSD2CLAUSE (BSD 2-clause Simplified License),
# Please read from the web.
#
            
# For whatever DNS server port/package your using do this
# Un-comment for which ever DNS server you are using.
# Do same at end of this script to select correct output format. 
  DNS="unbound"
#  DNS="named"
#  DNS="dnsmasq"
  path="/usr/local/etc/$DNS"
#  path="/tmp"
             
# If using the local_unbound version un-comment next 2 lines.
#  DNS="local_unbound"
#  path="/var/unbound"
            
            
  op="$path/fqdn-list"
  if [ -f "${op}" ]; then
    mv $op "$op-prev1"
    truncate -s 0 $op
  else      
    truncate -s 0 $op
  fi        
                        
  DL_DIR="$path/fqdn1_downloaded"
  if [ ! -d "$DL_DIR" ]; then
     mkdir -p "$DL_DIR"
  else      
    rm -rf   "$DL_DIR"
    mkdir -p "$DL_DIR"
  fi            
            
  CLEAN_DIR="$path/fqdn1_clean"
  if [ ! -d "$CLEAN_DIR" ]; then
    mkdir -p "$CLEAN_DIR"
  else      
    rm -rf   "$CLEAN_DIR"
    mkdir -p "$CLEAN_DIR"
  fi        
            
  ND_DIR="$path/fqdn1_nodups"
  if [ ! -d "$ND_DIR" ]; then
    mkdir -p "$ND_DIR"
  else      
    rm -rf   "$ND_DIR"
    mkdir -p "$ND_DIR"
  fi        
            
            
#  FETCH="/usr/bin/fetch -q"
  FETCH="/usr/bin/fetch "
            
  cd "$DL_DIR"
            
# These host files make up the default set of host files downloaded by 
# this script. The StevenBlack basic list (adware & malware) host
# file is an minimum that everyone should use. This group of files will
# successfully load into unbound on a computer with 2GB of memory. 
#
            
  echo " "  
  echo "Downloading host file provider FQDN files."
  echo " "  
            
  $FETCH -o bh.hosts "http://mirror1.malwaredomains.com/files/justdomains"
  $FETCH -o ch.hosts "http://sysctl.org/cameleon/hosts" #web-ads
  $FETCH -o hpp.hosts "http://hosts-file.net/hphosts-partial.txt"
  $FETCH -o sa.hosts "https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt"
  $FETCH -o sm.hosts "https://s3.amazonaws.com/lists.disconnect.me/simple_malware.txt"
  $FETCH -o st.hosts "https://s3.amazonaws.com/lists.disconnect.me/simple_tracking.txt"
  $FETCH -o sv.hosts "https://s3.amazonaws.com/lists.disconnect.me/simple_malvertising.txt"
  $FETCH -o wsp1.hosts "https://raw.githubusercontent.com/crazy-max/WindowsSpyBlocker/master/data/hosts/win10/spy.txt"
  $FETCH -o wsp2.hosts "https://raw.githubusercontent.com/crazy-max/WindowsSpyBlocker/master/data/hosts/win81/spy.txt"
  $FETCH -o wsp3.hosts "https://raw.githubusercontent.com/crazy-max/WindowsSpyBlocker/master/data/hosts/win7/spy.txt"
  $FETCH -o aw.hosts   "https://adaway.org/hosts.txt"
  $FETCH -o fdm.hosts  "https://raw.githubusercontent.com/FadeMind/hosts.extras/master/UncheckyAds/hosts"
  $FETCH -o mdl.hosts  "http://www.malwaredomainlist.com/hostslist/hosts.txt"
  $FETCH -o mvps.hosts "http://winhelp2002.mvps.org/hosts.txt"
  $FETCH -o swc.hosts  "http://someonewhocares.org/hosts/zero/hosts"
  $FETCH -o yoyo.hosts "http://pgl.yoyo.org/as/serverlist.php?hostformat=hosts&showintro=0&useip=0.0.0.0&mimetype=plaintext"
            
# StevenBlack GitHub Hosts
# Uncomment ONE line containing the list group you want to apply
# See https://github.com/StevenBlack/hosts for more combinations
#                
# basic list (adware & malware)
# $FETCH -o sb.hosts https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
# (basic & fakenews) 
# $FETCH -o sb.hosts https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews/hosts
# (basic & gambling)
# $FETCH -o sb.hosts https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/gambling/hosts
# (basic & porn)
# $FETCH -o sb.hosts https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/porn/hosts
# (basic & social)
# $FETCH -o sb.hosts https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/social/hosts
# (basic & fakenews & gambling & porn)  
  $FETCH -o sb.hosts https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling-porn/hosts
            
  # Add my blacklist file to process stream if it exists.            
  if [ -f "$path/blacklist.txt" ]; then
   /bin/cp "$path/blacklist.txt" "${DL_DIR}/mbl.hosts}"
  fi        
            
  # pgm1 reads the downloaded file creates a output file containing 
  # host name only. pgm2 sorts the host name only file right to left 
  # then sorts again to drop dups.
  #         
  pgm1="/usr/local/bin/dns2blackhole-sanitize"
  pgm2="/usr/local/bin/dns2blackhole-sort-fqdn"
  CAT="/bin/cat"
            
  echo " "  
  echo "Sanitizing and dropping dups from the downloaded files."
  echo " "  
            
  list=`ls` 
  if [ "${list}" ]; then
    # There are downloaded files.
    for filename in ${list}; do
      ${pgm1} "${DL_DIR}/${filename}" "${CLEAN_DIR}/${filename}" 
      ${CAT} "${CLEAN_DIR}/${filename}" | ${pgm2} > "${ND_DIR}/${filename}"
    done    
  else      
    echo "No downloaded."
    exit 2  
  fi        
              
  wl="$path/whitelist.txt"
  pgm3="/usr/local/bin/dns2blackhole-whitelist"
            
  echo " "  
  echo "Applying whitelist & creating FQDN list."
  echo " "  
            
# For unbound, Process whitelist file if it exists.
  if [ -f "${wl}" ]; then
    ${CAT} ${ND_DIR}/* | ${pgm2} | ${pgm3} "${wl}" | awk '
      { print "local-zone:", $1 " always_nxdomain" }' > $op
  else      
    ${CAT} ${ND_DIR}/* | ${pgm2} | awk '
      { print "local-zone:", $1 " always_nxdomain" }' > $op
  fi        
            
            
# For dnsmasq, Process whitelist file if it exists.
#  if [ -f "${wl}" ]; then
#    ${CAT} ${ND_DIR}/* | ${pgm2} | ${pgm3} "${wl}" | awk '
#      { print "address=/" $1 "/127.0.0.1" }' > $op
#  else     
#    ${CAT} ${ND_DIR}/* | ${pgm2} | awk '
#      { print "address=/" $1 "/127.0.0.1" }' > $op
#  fi       
            
            
# For named, Process whitelist file if it exists.
#  if [ -f "${wl}" ]; then
#    ${CAT} ${ND_DIR}/* | ${pgm2} | ${pgm3} "${wl}" | awk '
#      { print "zone \"" $1 "\" {type master; file \"/etc/bad.hosts\";};" }' > $op
#  else     
#    ${CAT} ${ND_DIR}/* | ${pgm2} | awk '
#      { print "zone \"" $1 "\" {type master; file \"/etc/bad.hosts\";};" }' > $op
#  fi       
            
  service $DNS restart          
