Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 57 additions & 19 deletions gravity.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ blackList="${piholeDir}/black.list"
localList="${piholeDir}/local.list"
VPNList="/etc/openvpn/ipp.txt"

adListNX="/etc/dnsmasq.d/00-pihole-gravity.conf"
blackListNX="/etc/dnsmasq.d/00-pihole-userblacklist.conf"
whiteListNX="/etc/dnsmasq.d/99-pihole-whitelist.conf"

domainsExtension="domains"
matterAndLight="${basename}.0.matterandlight.txt"
parsedMatter="${basename}.1.parsedmatter.txt"
Expand All @@ -60,6 +64,18 @@ if [[ -f "${setupVars}" ]];then
echo -e " ${COL_LIGHT_RED}No IP addresses found! Please run 'pihole -r' to reconfigure${COL_NC}"
exit 1
fi

#If setupvars has BLOCKSTYLE_NXDOMAIN set, then clear out gravity.list and black.list
if [[ "${BLOCKSTYLE_NXDOMAIN}" == true ]]; then
truncate -s 0 "${blackList}"
truncate -s 0 "${adList}"
dnsRestartType="restart"
else
rm "${adListNX}" 2> /dev/null
rm "${blackListNX}" 2> /dev/null
rm "${whiteListNX}" 2> /dev/null
fi

else
echo -e " ${COL_LIGHT_RED}Installation Failure: ${setupVars} does not exist! ${COL_NC}
Please run 'pihole -r', and choose the 'reconfigure' option to fix."
Expand Down Expand Up @@ -475,6 +491,14 @@ gravity_ParseDomainsIntoHosts() {
}' >> "${2}" < "${1}"
}

gravity_ParseDomainsIntoConfFile() {
sed 's*^*server=/*;s*$*/*' "${1}" > "${2}"
}

gravity_ParseDomainsIntoConfFile_WL() {
sed 's*^*server=/*;s*$*/#*' "${1}" > "${2}"
}

# Create "localhost" entries into hosts format
gravity_ParseLocalDomains() {
local hostname
Expand Down Expand Up @@ -504,24 +528,29 @@ gravity_ParseLocalDomains() {
# Create primary blacklist entries
gravity_ParseBlacklistDomains() {
local output status

# Empty $accretionDisc if it already exists, otherwise, create it
: > "${piholeDir}/${accretionDisc}"

if [[ -f "${piholeDir}/${whitelistMatter}" ]]; then
gravity_ParseDomainsIntoHosts "${piholeDir}/${whitelistMatter}" "${piholeDir}/${accretionDisc}"
else
# There was no whitelist file, so use preEventHorizon instead of whitelistMatter.
gravity_ParseDomainsIntoHosts "${piholeDir}/${preEventHorizon}" "${piholeDir}/${accretionDisc}"
fi

# Move the file over as /etc/pihole/gravity.list so dnsmasq can use it
output=$( { mv "${piholeDir}/${accretionDisc}" "${adList}"; } 2>&1 )
status="$?"

if [[ "${status}" -ne 0 ]]; then
echo -e "\\n ${CROSS} Unable to move ${accretionDisc} from ${piholeDir}\\n ${output}"
gravity_Cleanup "error"
if [[ "${BLOCKSTYLE_NXDOMAIN}" == true ]]; then
gravity_ParseDomainsIntoConfFile "${piholeDir}/${whitelistMatter}" "${adListNX}"
else
# Empty $accretionDisc if it already exists, otherwise, create it
: > "${piholeDir}/${accretionDisc}"

if [[ -f "${piholeDir}/${whitelistMatter}" ]]; then
gravity_ParseDomainsIntoHosts "${piholeDir}/${whitelistMatter}" "${piholeDir}/${accretionDisc}"
else
# There was no whitelist file, so use preEventHorizon instead of whitelistMatter.
gravity_ParseDomainsIntoHosts "${piholeDir}/${preEventHorizon}" "${piholeDir}/${accretionDisc}"
fi
# Move the file over as /etc/pihole/gravity.list so dnsmasq can use it
output=$( { mv "${piholeDir}/${accretionDisc}" "${adList}"; } 2>&1 )
status="$?"

if [[ "${status}" -ne 0 ]]; then
echo -e "\\n ${CROSS} Unable to move ${accretionDisc} from ${piholeDir}\\n ${output}"
gravity_Cleanup "error"
fi
fi
}

Expand All @@ -531,10 +560,17 @@ gravity_ParseUserDomains() {
return 0
fi

gravity_ParseDomainsIntoHosts "${blacklistFile}" "${blackList}.tmp"
# Copy the file over as /etc/pihole/black.list so dnsmasq can use it
mv "${blackList}.tmp" "${blackList}" 2> /dev/null || \
if [[ "${BLOCKSTYLE_NXDOMAIN}" == true ]]; then
gravity_ParseDomainsIntoConfFile "${blacklistFile}" "${blackListNX}"
gravity_ParseDomainsIntoConfFile_WL "${whitelistFile}" "${whiteListNX}"
else
gravity_ParseDomainsIntoHosts "${blacklistFile}" "${blackList}.tmp"
# Copy the file over as /etc/pihole/black.list so dnsmasq can use it
mv "${blackList}.tmp" "${blackList}" 2> /dev/null || \
echo -e "\\n ${CROSS} Unable to move ${blackList##*/}.tmp to ${piholeDir}"
fi


}

# Trap Ctrl-C
Expand Down Expand Up @@ -633,13 +669,15 @@ if [[ "${skipDownload}" == false ]] || [[ "${listType}" == "whitelist" ]]; then
gravity_Whitelist
fi

#"${piholeDir}/${whitelistMatter}" Now contains everything from preEventHorizon excluding everything from the blocklist

gravity_ShowBlockCount

# Perform when downloading blocklists, or modifying the white/blacklist (not wildcards)
if [[ "${skipDownload}" == false ]] || [[ "${listType}" == *"list" ]]; then
str="Parsing domains into hosts format"
echo -ne " ${INFO} ${str}..."

gravity_ParseUserDomains

# Perform when downloading blocklists
Expand Down
21 changes: 21 additions & 0 deletions pihole
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

readonly PI_HOLE_SCRIPT_DIR="/opt/pihole"
readonly wildcardlist="/etc/dnsmasq.d/03-pihole-wildcard.conf"
readonly adListNX="/etc/dnsmasq.d/00-pihole-gravity.conf"
readonly userListNX="/etc/dnsmasq.d/00-pihole-userblacklist.conf"
readonly whiteListNX="/etc/dnsmasq.d/99-pihole-whitelist.conf"
readonly colfile="${PI_HOLE_SCRIPT_DIR}/COL_TABLE"
source "${colfile}"

Expand Down Expand Up @@ -381,6 +384,15 @@ Time:
if [[ -e "$wildcardlist" ]]; then
mv "$wildcardlist" "/etc/pihole/wildcard.list"
fi
if [[ -e "$adListNX" ]]; then
mv "$adListNX" "/etc/pihole/gravityNX.list"
fi
if [[ -e "$userListNX" ]]; then
mv "$userListNX" "/etc/pihole/blackNX.list"
fi
if [[ -e "$whiteListNX" ]]; then
mv "$whiteListNX" "/etc/pihole/whitelistNX.list"
fi
if [[ $# > 1 ]]; then
local error=false
if [[ "${2}" == *"s" ]]; then
Expand Down Expand Up @@ -427,6 +439,15 @@ Time:
if [[ -e "/etc/pihole/wildcard.list" ]]; then
mv "/etc/pihole/wildcard.list" "$wildcardlist"
fi
if [[ -e "/etc/pihole/gravityNX.list" ]]; then
mv "/etc/pihole/gravityNX.list" "$adListNX"
fi
if [[ -e "/etc/pihole/blackNX.list" ]]; then
mv "/etc/pihole/blackNX.list" "$userListNX"
fi
if [[ -e "/etc/pihole/whitelistNX.list" ]]; then
mv "/etc/pihole/whitelistNX.list" "$whiteListNX"
fi
fi

restartDNS
Expand Down