#!/bin/sh
#
# Copyright (c) 2021 Gregory L. Dietsche <Gregory.Dietsche@cuw.edu>
# This is free software, licensed under the MIT License
#

HasIpAddressChanged() {
  # Create a sorted, unique list of addresses using DNS
  HOST_LIVE=$(nslookup "$1" | grep "Address\ [0-9]\+:" | sed -r 's/^Address [0-9]+: //' | sort | uniq -i)

  # Create a sorted, unique list of addresess currently in use
  HOST_SAFE=$(grep -i "^[:0-9a-f]" "$2" | sed 's/ .*//' | sort | uniq -i)

  # dns resolution errors / not being connected to the internet can cause this.
  if [ -z "$HOST_LIVE" ]; then
    return 1
  fi

  #If the lists do not match, then we want to update to match the DNS records.
  if [ "$HOST_LIVE" = "$HOST_SAFE" ]; then
    return 1 # IP has NOT changed
  fi

  return 0 # IP has changed
}

StartIpAddressUpdate(){
  echo Updating "$1"
  RELOAD_DNSMASQ=1

  #Create the new hosts file...
  cat >"$1"<<EOL
##########################################################################
#                     **** IMPORTANT ****                                #
# Do not make changes to this file instead please execute:               #
# /usr/sbin/safe-search-maintenance                                      #
# If this file is not working, please ensure dnsmasq is able to READ it! #
##########################################################################

# Last Updated On: $(date)

EOL
}

MakeHost(){
  for ipAddr in $HOST_LIVE
  do
    echo "$ipAddr $1"
  done
}

#################################################
### strict.bing.com                           ###
#################################################
HOST_FILE=/etc/safe-search/available/bing.default
if HasIpAddressChanged strict.bing.com $HOST_FILE; then
  StartIpAddressUpdate $HOST_FILE
  MakeHost "bing.com www.bing.com">>$HOST_FILE
fi

#################################################
### safe.duckduckgo.com                       ###
#################################################
HOST_FILE=/etc/safe-search/available/duckduckgo.default
if HasIpAddressChanged safe.duckduckgo.com $HOST_FILE; then
  StartIpAddressUpdate $HOST_FILE
  MakeHost "duckduckgo.com">>$HOST_FILE
fi

#################################################
### restrict.youtube.com                      ###
#################################################
HOST_FILE=/etc/safe-search/available/youtube.restrict
if HasIpAddressChanged restrict.youtube.com $HOST_FILE; then
  StartIpAddressUpdate $HOST_FILE
  MakeHost "www.youtube.com m.youtube.com youtubei.googleapis.com youtube.googleapis.com www.youtube-nocookie.com">>$HOST_FILE
fi

#################################################
### restrictmoderate.youtube.com              ###
#################################################
HOST_FILE=/etc/safe-search/available/youtube.restrictmoderate
if HasIpAddressChanged restrictmoderate.youtube.com $HOST_FILE; then
  StartIpAddressUpdate $HOST_FILE
  MakeHost "www.youtube.com m.youtube.com youtubei.googleapis.com youtube.googleapis.com www.youtube-nocookie.com">>$HOST_FILE
fi

#################################################
### forcesafesearch.google.com                ###
### https://www.google.com/supported_domains  ###
#################################################
HOST_FILE=/etc/safe-search/available/google.default
if HasIpAddressChanged forcesafesearch.google.com $HOST_FILE; then
  StartIpAddressUpdate $HOST_FILE
  cat >>"$HOST_FILE"<<EOL
$(MakeHost "google.com")
$(MakeHost "google.ad")
$(MakeHost "google.ae")
$(MakeHost "google.com.af")
$(MakeHost "google.com.ag")
$(MakeHost "google.com.ai")
$(MakeHost "google.al")
$(MakeHost "google.am")
$(MakeHost "google.co.ao")
$(MakeHost "google.com.ar")
$(MakeHost "google.as")
$(MakeHost "google.at")
$(MakeHost "google.com.au")
$(MakeHost "google.az")
$(MakeHost "google.ba")
$(MakeHost "google.com.bd")
$(MakeHost "google.be")
$(MakeHost "google.bf")
$(MakeHost "google.bg")
$(MakeHost "google.com.bh")
$(MakeHost "google.bi")
$(MakeHost "google.bj")
$(MakeHost "google.com.bn")
$(MakeHost "google.com.bo")
$(MakeHost "google.com.br")
$(MakeHost "google.bs")
$(MakeHost "google.bt")
$(MakeHost "google.co.bw")
$(MakeHost "google.by")
$(MakeHost "google.com.bz")
$(MakeHost "google.ca")
$(MakeHost "google.cd")
$(MakeHost "google.cf")
$(MakeHost "google.cg")
$(MakeHost "google.ch")
$(MakeHost "google.ci")
$(MakeHost "google.co.ck")
$(MakeHost "google.cl")
$(MakeHost "google.cm")
$(MakeHost "google.cn")
$(MakeHost "google.com.co")
$(MakeHost "google.co.cr")
$(MakeHost "google.com.cu")
$(MakeHost "google.cv")
$(MakeHost "google.com.cy")
$(MakeHost "google.cz")
$(MakeHost "google.de")
$(MakeHost "google.dj")
$(MakeHost "google.dk")
$(MakeHost "google.dm")
$(MakeHost "google.com.do")
$(MakeHost "google.dz")
$(MakeHost "google.com.ec")
$(MakeHost "google.ee")
$(MakeHost "google.com.eg")
$(MakeHost "google.es")
$(MakeHost "google.com.et")
$(MakeHost "google.fi")
$(MakeHost "google.com.fj")
$(MakeHost "google.fm")
$(MakeHost "google.fr")
$(MakeHost "google.ga")
$(MakeHost "google.ge")
$(MakeHost "google.gg")
$(MakeHost "google.com.gh")
$(MakeHost "google.com.gi")
$(MakeHost "google.gl")
$(MakeHost "google.gm")
$(MakeHost "google.gp")
$(MakeHost "google.gr")
$(MakeHost "google.com.gt")
$(MakeHost "google.gy")
$(MakeHost "google.com.hk")
$(MakeHost "google.hn")
$(MakeHost "google.hr")
$(MakeHost "google.ht")
$(MakeHost "google.hu")
$(MakeHost "google.co.id")
$(MakeHost "google.ie")
$(MakeHost "google.co.il")
$(MakeHost "google.im")
$(MakeHost "google.co.in")
$(MakeHost "google.iq")
$(MakeHost "google.is")
$(MakeHost "google.it")
$(MakeHost "google.je")
$(MakeHost "google.com.jm")
$(MakeHost "google.jo")
$(MakeHost "google.co.jp")
$(MakeHost "google.co.ke")
$(MakeHost "google.com.kh")
$(MakeHost "google.ki")
$(MakeHost "google.kg")
$(MakeHost "google.co.kr")
$(MakeHost "google.com.kw")
$(MakeHost "google.kz")
$(MakeHost "google.la")
$(MakeHost "google.com.lb")
$(MakeHost "google.li")
$(MakeHost "google.lk")
$(MakeHost "google.co.ls")
$(MakeHost "google.lt")
$(MakeHost "google.lu")
$(MakeHost "google.lv")
$(MakeHost "google.com.ly")
$(MakeHost "google.co.ma")
$(MakeHost "google.md")
$(MakeHost "google.me")
$(MakeHost "google.mg")
$(MakeHost "google.mk")
$(MakeHost "google.ml")
$(MakeHost "google.com.mm")
$(MakeHost "google.mn")
$(MakeHost "google.ms")
$(MakeHost "google.com.mt")
$(MakeHost "google.mu")
$(MakeHost "google.mv")
$(MakeHost "google.mw")
$(MakeHost "google.com.mx")
$(MakeHost "google.com.my")
$(MakeHost "google.co.mz")
$(MakeHost "google.com.na")
$(MakeHost "google.com.nf")
$(MakeHost "google.com.ng")
$(MakeHost "google.com.ni")
$(MakeHost "google.ne")
$(MakeHost "google.nl")
$(MakeHost "google.no")
$(MakeHost "google.com.np")
$(MakeHost "google.nr")
$(MakeHost "google.nu")
$(MakeHost "google.co.nz")
$(MakeHost "google.com.om")
$(MakeHost "google.com.pa")
$(MakeHost "google.com.pe")
$(MakeHost "google.com.pg")
$(MakeHost "google.com.ph")
$(MakeHost "google.com.pk")
$(MakeHost "google.pl")
$(MakeHost "google.pn")
$(MakeHost "google.com.pr")
$(MakeHost "google.ps")
$(MakeHost "google.pt")
$(MakeHost "google.com.py")
$(MakeHost "google.com.qa")
$(MakeHost "google.ro")
$(MakeHost "google.ru")
$(MakeHost "google.rw")
$(MakeHost "google.com.sa")
$(MakeHost "google.com.sb")
$(MakeHost "google.sc")
$(MakeHost "google.se")
$(MakeHost "google.com.sg")
$(MakeHost "google.sh")
$(MakeHost "google.si")
$(MakeHost "google.sk")
$(MakeHost "google.com.sl")
$(MakeHost "google.sn")
$(MakeHost "google.so")
$(MakeHost "google.sm")
$(MakeHost "google.sr")
$(MakeHost "google.st")
$(MakeHost "google.com.sv")
$(MakeHost "google.td")
$(MakeHost "google.tg")
$(MakeHost "google.co.th")
$(MakeHost "google.com.tj")
$(MakeHost "google.tk")
$(MakeHost "google.tl")
$(MakeHost "google.tm")
$(MakeHost "google.tn")
$(MakeHost "google.to")
$(MakeHost "google.com.tr")
$(MakeHost "google.tt")
$(MakeHost "google.com.tw")
$(MakeHost "google.co.tz")
$(MakeHost "google.com.ua")
$(MakeHost "google.co.ug")
$(MakeHost "google.co.uk")
$(MakeHost "google.com.uy")
$(MakeHost "google.co.uz")
$(MakeHost "google.com.vc")
$(MakeHost "google.co.ve")
$(MakeHost "google.vg")
$(MakeHost "google.co.vi")
$(MakeHost "google.com.vn")
$(MakeHost "google.vu")
$(MakeHost "google.ws")
$(MakeHost "google.rs")
$(MakeHost "google.co.za")
$(MakeHost "google.co.zm")
$(MakeHost "google.co.zw")
$(MakeHost "google.cat")
$(MakeHost "www.google.com")
$(MakeHost "www.google.ad")
$(MakeHost "www.google.ae")
$(MakeHost "www.google.com.af")
$(MakeHost "www.google.com.ag")
$(MakeHost "www.google.com.ai")
$(MakeHost "www.google.al")
$(MakeHost "www.google.am")
$(MakeHost "www.google.co.ao")
$(MakeHost "www.google.com.ar")
$(MakeHost "www.google.as")
$(MakeHost "www.google.at")
$(MakeHost "www.google.com.au")
$(MakeHost "www.google.az")
$(MakeHost "www.google.ba")
$(MakeHost "www.google.com.bd")
$(MakeHost "www.google.be")
$(MakeHost "www.google.bf")
$(MakeHost "www.google.bg")
$(MakeHost "www.google.com.bh")
$(MakeHost "www.google.bi")
$(MakeHost "www.google.bj")
$(MakeHost "www.google.com.bn")
$(MakeHost "www.google.com.bo")
$(MakeHost "www.google.com.br")
$(MakeHost "www.google.bs")
$(MakeHost "www.google.bt")
$(MakeHost "www.google.co.bw")
$(MakeHost "www.google.by")
$(MakeHost "www.google.com.bz")
$(MakeHost "www.google.ca")
$(MakeHost "www.google.cd")
$(MakeHost "www.google.cf")
$(MakeHost "www.google.cg")
$(MakeHost "www.google.ch")
$(MakeHost "www.google.ci")
$(MakeHost "www.google.co.ck")
$(MakeHost "www.google.cl")
$(MakeHost "www.google.cm")
$(MakeHost "www.google.cn")
$(MakeHost "www.google.com.co")
$(MakeHost "www.google.co.cr")
$(MakeHost "www.google.com.cu")
$(MakeHost "www.google.cv")
$(MakeHost "www.google.com.cy")
$(MakeHost "www.google.cz")
$(MakeHost "www.google.de")
$(MakeHost "www.google.dj")
$(MakeHost "www.google.dk")
$(MakeHost "www.google.dm")
$(MakeHost "www.google.com.do")
$(MakeHost "www.google.dz")
$(MakeHost "www.google.com.ec")
$(MakeHost "www.google.ee")
$(MakeHost "www.google.com.eg")
$(MakeHost "www.google.es")
$(MakeHost "www.google.com.et")
$(MakeHost "www.google.fi")
$(MakeHost "www.google.com.fj")
$(MakeHost "www.google.fm")
$(MakeHost "www.google.fr")
$(MakeHost "www.google.ga")
$(MakeHost "www.google.ge")
$(MakeHost "www.google.gg")
$(MakeHost "www.google.com.gh")
$(MakeHost "www.google.com.gi")
$(MakeHost "www.google.gl")
$(MakeHost "www.google.gm")
$(MakeHost "www.google.gp")
$(MakeHost "www.google.gr")
$(MakeHost "www.google.com.gt")
$(MakeHost "www.google.gy")
$(MakeHost "www.google.com.hk")
$(MakeHost "www.google.hn")
$(MakeHost "www.google.hr")
$(MakeHost "www.google.ht")
$(MakeHost "www.google.hu")
$(MakeHost "www.google.co.id")
$(MakeHost "www.google.ie")
$(MakeHost "www.google.co.il")
$(MakeHost "www.google.im")
$(MakeHost "www.google.co.in")
$(MakeHost "www.google.iq")
$(MakeHost "www.google.is")
$(MakeHost "www.google.it")
$(MakeHost "www.google.je")
$(MakeHost "www.google.com.jm")
$(MakeHost "www.google.jo")
$(MakeHost "www.google.co.jp")
$(MakeHost "www.google.co.ke")
$(MakeHost "www.google.com.kh")
$(MakeHost "www.google.ki")
$(MakeHost "www.google.kg")
$(MakeHost "www.google.co.kr")
$(MakeHost "www.google.com.kw")
$(MakeHost "www.google.kz")
$(MakeHost "www.google.la")
$(MakeHost "www.google.com.lb")
$(MakeHost "www.google.li")
$(MakeHost "www.google.lk")
$(MakeHost "www.google.co.ls")
$(MakeHost "www.google.lt")
$(MakeHost "www.google.lu")
$(MakeHost "www.google.lv")
$(MakeHost "www.google.com.ly")
$(MakeHost "www.google.co.ma")
$(MakeHost "www.google.md")
$(MakeHost "www.google.me")
$(MakeHost "www.google.mg")
$(MakeHost "www.google.mk")
$(MakeHost "www.google.ml")
$(MakeHost "www.google.com.mm")
$(MakeHost "www.google.mn")
$(MakeHost "www.google.ms")
$(MakeHost "www.google.com.mt")
$(MakeHost "www.google.mu")
$(MakeHost "www.google.mv")
$(MakeHost "www.google.mw")
$(MakeHost "www.google.com.mx")
$(MakeHost "www.google.com.my")
$(MakeHost "www.google.co.mz")
$(MakeHost "www.google.com.na")
$(MakeHost "www.google.com.nf")
$(MakeHost "www.google.com.ng")
$(MakeHost "www.google.com.ni")
$(MakeHost "www.google.ne")
$(MakeHost "www.google.nl")
$(MakeHost "www.google.no")
$(MakeHost "www.google.com.np")
$(MakeHost "www.google.nr")
$(MakeHost "www.google.nu")
$(MakeHost "www.google.co.nz")
$(MakeHost "www.google.com.om")
$(MakeHost "www.google.com.pa")
$(MakeHost "www.google.com.pe")
$(MakeHost "www.google.com.pg")
$(MakeHost "www.google.com.ph")
$(MakeHost "www.google.com.pk")
$(MakeHost "www.google.pl")
$(MakeHost "www.google.pn")
$(MakeHost "www.google.com.pr")
$(MakeHost "www.google.ps")
$(MakeHost "www.google.pt")
$(MakeHost "www.google.com.py")
$(MakeHost "www.google.com.qa")
$(MakeHost "www.google.ro")
$(MakeHost "www.google.ru")
$(MakeHost "www.google.rw")
$(MakeHost "www.google.com.sa")
$(MakeHost "www.google.com.sb")
$(MakeHost "www.google.sc")
$(MakeHost "www.google.se")
$(MakeHost "www.google.com.sg")
$(MakeHost "www.google.sh")
$(MakeHost "www.google.si")
$(MakeHost "www.google.sk")
$(MakeHost "www.google.com.sl")
$(MakeHost "www.google.sn")
$(MakeHost "www.google.so")
$(MakeHost "www.google.sm")
$(MakeHost "www.google.sr")
$(MakeHost "www.google.st")
$(MakeHost "www.google.com.sv")
$(MakeHost "www.google.td")
$(MakeHost "www.google.tg")
$(MakeHost "www.google.co.th")
$(MakeHost "www.google.com.tj")
$(MakeHost "www.google.tk")
$(MakeHost "www.google.tl")
$(MakeHost "www.google.tm")
$(MakeHost "www.google.tn")
$(MakeHost "www.google.to")
$(MakeHost "www.google.com.tr")
$(MakeHost "www.google.tt")
$(MakeHost "www.google.com.tw")
$(MakeHost "www.google.co.tz")
$(MakeHost "www.google.com.ua")
$(MakeHost "www.google.co.ug")
$(MakeHost "www.google.co.uk")
$(MakeHost "www.google.com.uy")
$(MakeHost "www.google.co.uz")
$(MakeHost "www.google.com.vc")
$(MakeHost "www.google.co.ve")
$(MakeHost "www.google.vg")
$(MakeHost "www.google.co.vi")
$(MakeHost "www.google.com.vn")
$(MakeHost "www.google.vu")
$(MakeHost "www.google.ws")
$(MakeHost "www.google.rs")
$(MakeHost "www.google.co.za")
$(MakeHost "www.google.co.zm")
$(MakeHost "www.google.co.zw")
$(MakeHost "www.google.cat")
EOL
fi

if [ "$RELOAD_DNSMASQ" = "1" ]; then
  echo restarting dnsmasq to activate new IP addresses.
  /etc/init.d/dnsmasq restart
fi
