#! /bin/bash

# define our color set
red='\033[0;31m'
green='\033[0;32m'
grey='\033[0;37m'
noColor='\033[0m'

# reset counters
ResolvesHereCounter=0
NotHereCounter=0
TBDCounter=0
noMatches=true

# get list of the server's IPs. One per line so we can loop through them
serverIPList=$(hostname -I | tr '[:space:]' '\n' | tr -s [:space:])


#------------------------------------------------------------------------
# For Loop that goes through the domains and digs them, then compares the result(s) to the server IP(s). Throws domain into proper array.
#------------------------------------------------------------------------
for domain in $(cat /etc/userdatadomains | cut -d: -f1); do 
    	digResults=$(dig @8.8.8.8 $domain +short)

# if the dig results in no hits, throw it in the NotHere array
        if [ -z "$digResults" ]; then
                NotHere[$NotHereCounter]=$domain
                NHIP[$NotHereCounter]=$digResults
                NotHereCounter=$((NotHereCounter + 1))
	fi

# run through all the dig results and if any match a server IP add it to the ResolvesHere array and break out of the loops (to ensure our counters stay accurate) 
	for digIP in $digResults; do
		for serverIP in $serverIPList; do
			if [[ $serverIP == $digIP ]]; then
				ResolvesHere[$ResolvesHereCounter]=$domain
				RHIP[$ResolvesHereCounter]=$digResults
				ResolvesHereCounter=$((ResolvesHereCounter + 1))
				noMatches=false
				break 2
			fi
		done
	done

# if there were no matches AND theres only 1 dig IP result, then we know for a fact that the domain does not resolve here
	if [[ $noMatches == true ]] && [[ $(echo "$digResults" | grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}' -c) -eq 1 ]]; then
                NotHere[$NotHereCounter]=$domain
                NHIP[$NotHereCounter]=$digResults
                NotHereCounter=$((NotHereCounter + 1))

# however, if there are no matches BUT there's more than 1 IP, then it cannot be determined
	elif [[ $noMatches == true ]] && [[ $(echo "$digResults" | grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}' -c) -ge 2 ]]; then
                TBD[$TBDCounter]=$domain
                TBDIP[$TBDCounter]=$digResults
                TBDCounter=$((TBDCounter + 1))
	fi

noMatches=true
done



#------------------------------------------------------------------------
# Print out the results of each Array
#------------------------------------------------------------------------
echo -e "${green}\e[4m=== ($ResolvesHereCounter) Sites that resolve to the server ===\e[0m${noColor}"
for (( i = 0; i < ResolvesHereCounter; i++ )); do
	printf "%-40s %s %s \n" "${ResolvesHere[$i]}" "$(echo ${RHIP[$i]} | column -t)"; 
done
printf "\n"

echo -e "${red}\e[4m=== ($NotHereCounter) Sites that do NOT resolve to the server ===\e[0m${noColor}"
for (( i = 0; i < NotHereCounter; i++ )); do
        printf "%-40s %s %s \n" "${NotHere[$i]}" "$(echo ${NHIP[$i]} | column -t)";
done
printf "\n"

echo -e "${grey}\e[4m=== ($TBDCounter) Sites that have multiple IPs (i.e. cannot be determined) ===\e[0m${noColor}" 
for (( i = 0; i < TBDCounter; i++ )); do
        printf "%-40s %s %s \n" "${TBD[$i]}" "$(echo ${TBDIP[$i]} | column -t)";
done
printf "\n\n"

#CloudFlare IPv4 range https://www.cloudflare.com/ips-v4 
