Merge pull request #1631 from pi-hole/tweak/gravity

Clean up and optimise Gravity
This commit is contained in:
Adam Warner 2017-09-21 19:58:38 +01:00 committed by GitHub
commit ef1ce7d4d6
4 changed files with 677 additions and 506 deletions

View file

@ -66,15 +66,15 @@ HandleOther() {
domain="${1,,}"
# Check validity of domain
validDomain=$(perl -lne 'print if /^((-|_)*[a-z\d]((-|_)*[a-z\d])*(-|_)*)(\.(-|_)*([a-z\d]((-|_)*[a-z\d])*))*$/' <<< "${domain}") # Valid chars check
validDomain=$(perl -lne 'print if /^.{1,253}$/' <<< "${validDomain}") # Overall length check
validDomain=$(perl -lne 'print if /^[^\.]{1,63}(\.[^\.]{1,63})*$/' <<< "${validDomain}") # Length of each label
if [[ "${#domain}" -le 253 ]]; then
validDomain=$(grep -P "^((-|_)*[a-z\d]((-|_)*[a-z\d])*(-|_)*)(\.(-|_)*([a-z\d]((-|_)*[a-z\d])*))*$" <<< "${domain}") # Valid chars check
validDomain=$(grep -P "^[^\.]{1,63}(\.[^\.]{1,63})*$" <<< "${validDomain}") # Length of each label
fi
if [[ -z "${validDomain}" ]]; then
echo -e " ${CROSS} $1 is not a valid argument or domain name!"
else
echo -e " ${TICK} $1 is a valid domain name!"
if [[ -n "${validDomain}" ]]; then
domList=("${domList[@]}" ${validDomain})
else
echo -e " ${CROSS} ${domain} is not a valid argument or domain name!"
fi
}
@ -107,6 +107,8 @@ AddDomain() {
[[ "${list}" == "${wildcardlist}" ]] && listname="wildcard blacklist"
if [[ "${list}" == "${whitelist}" || "${list}" == "${blacklist}" ]]; then
[[ "${list}" == "${whitelist}" && -z "${type}" ]] && type="--whitelist-only"
[[ "${list}" == "${blacklist}" && -z "${type}" ]] && type="--blacklist-only"
bool=true
# Is the domain in the list we want to add it to?
grep -Ex -q "${domain}" "${list}" > /dev/null 2>&1 || bool=false
@ -129,7 +131,7 @@ AddDomain() {
# Remove the /* from the end of the IP addresses
IPV4_ADDRESS=${IPV4_ADDRESS%/*}
IPV6_ADDRESS=${IPV6_ADDRESS%/*}
[[ -z "${type}" ]] && type="--wildcard-only"
bool=true
# Is the domain in the list?
grep -e "address=\/${domain}\/" "${wildcardlist}" > /dev/null 2>&1 || bool=false
@ -138,7 +140,7 @@ AddDomain() {
if [[ "${verbose}" == true ]]; then
echo -e " ${INFO} Adding $1 to wildcard blacklist..."
fi
reload=true
reload="restart"
echo "address=/$1/${IPV4_ADDRESS}" >> "${wildcardlist}"
if [[ "${#IPV6_ADDRESS}" > 0 ]]; then
echo "address=/$1/${IPV6_ADDRESS}" >> "${wildcardlist}"
@ -161,6 +163,8 @@ RemoveDomain() {
if [[ "${list}" == "${whitelist}" || "${list}" == "${blacklist}" ]]; then
bool=true
[[ "${list}" == "${whitelist}" && -z "${type}" ]] && type="--whitelist-only"
[[ "${list}" == "${blacklist}" && -z "${type}" ]] && type="--blacklist-only"
# Is it in the list? Logic follows that if its whitelisted it should not be blacklisted and vice versa
grep -Ex -q "${domain}" "${list}" > /dev/null 2>&1 || bool=false
if [[ "${bool}" == true ]]; then
@ -175,6 +179,7 @@ RemoveDomain() {
fi
fi
elif [[ "${list}" == "${wildcardlist}" ]]; then
[[ -z "${type}" ]] && type="--wildcard-only"
bool=true
# Is it in the list?
grep -e "address=\/${domain}\/" "${wildcardlist}" > /dev/null 2>&1 || bool=false
@ -192,12 +197,10 @@ RemoveDomain() {
fi
}
# Update Gravity
Reload() {
# Reload hosts file
echo ""
echo -e " ${INFO} Updating gravity..."
echo ""
pihole -g -sd
pihole -g --skip-download "${type:-}"
}
Displaylist() {
@ -243,6 +246,7 @@ fi
PoplistFile
if ${reload}; then
Reload
if [[ "${reload}" != false ]]; then
# Ensure that "restart" is used for Wildcard updates
Reload "${reload}"
fi

View file

@ -229,20 +229,7 @@ Reboot() {
}
RestartDNS() {
local str="Restarting DNS service"
[[ -t 1 ]] && echo -ne " ${INFO} ${str}"
if command -v systemctl &> /dev/null; then
output=$( { systemctl restart dnsmasq; } 2>&1 )
else
output=$( { service dnsmasq restart; } 2>&1 )
fi
if [[ -z "${output}" ]]; then
[[ -t 1 ]] && echo -e "${OVER} ${TICK} ${str}"
else
[[ ! -t 1 ]] && OVER=""
echo -e "${OVER} ${CROSS} ${output}"
fi
/usr/local/bin/pihole restartdns
}
SetQueryLogOptions() {

1047
gravity.sh

File diff suppressed because it is too large Load diff

55
pihole
View file

@ -12,8 +12,7 @@
readonly PI_HOLE_SCRIPT_DIR="/opt/pihole"
readonly wildcardlist="/etc/dnsmasq.d/03-pihole-wildcard.conf"
readonly colfile="${PI_HOLE_SCRIPT_DIR}/COL_TABLE"
source ${colfile}
source "${colfile}"
# Must be root to use this tool
if [[ ! $EUID -eq 0 ]];then
@ -27,7 +26,7 @@ if [[ ! $EUID -eq 0 ]];then
fi
webpageFunc() {
source /opt/pihole/webpage.sh
source "${PI_HOLE_SCRIPT_DIR}/webpage.sh"
main "$@"
exit 0
}
@ -348,34 +347,36 @@ versionFunc() {
}
restartDNS() {
dnsmasqPid=$(pidof dnsmasq)
local str="Restarting DNS service"
echo -ne " ${INFO} ${str}"
if [[ "${dnsmasqPid}" ]]; then
# Service already running - reload config
if [[ -x "$(command -v systemctl)" ]]; then
output=$( { systemctl restart dnsmasq; } 2>&1 )
local svcOption svc str output status
svcOption="${1:-}"
# Determine if we should reload or restart dnsmasq
if [[ "${svcOption}" =~ "reload" ]]; then
# Using SIGHUP will NOT re-read any *.conf files
svc="killall -s SIGHUP dnsmasq"
elif [[ -z "${svcOption}" ]]; then
# Get PID of dnsmasq to determine if it needs to start or restart
if pidof dnsmasq &> /dev/null; then
svcOption="restart"
else
output=$( { service dnsmasq restart; } 2>&1 )
svcOption="start"
fi
if [[ -z "${output}" ]]; then
echo -e "${OVER} ${TICK} ${str}"
svc="service dnsmasq ${svcOption}"
fi
# Print output to Terminal, but not to Web Admin
str="${svcOption^}ing DNS service"
[[ -t 1 ]] && echo -ne " ${INFO} ${str}..."
output=$( { ${svc}; } 2>&1 )
status="$?"
if [[ "${status}" -eq 0 ]]; then
[[ -t 1 ]] && echo -e "${OVER} ${TICK} ${str}"
else
[[ ! -t 1 ]] && local OVER=""
echo -e "${OVER} ${CROSS} ${output}"
fi
else
# Service not running, start it up
if [[ -x "$(command -v systemctl)" ]]; then
output=$( { systemctl start dnsmasq; } 2>&1 )
else
output=$( { service dnsmasq start; } 2>&1 )
fi
if [[ -z "${output}" ]]; then
echo -e "${OVER} ${TICK} ${str}"
else
echo -e "${OVER} ${CROSS} ${output}"
fi
fi
}
piholeEnable() {
@ -655,7 +656,7 @@ case "${1}" in
"enable" ) piholeEnable 1;;
"disable" ) piholeEnable 0 "$2";;
"status" ) statusFunc "$2";;
"restartdns" ) restartDNS;;
"restartdns" ) restartDNS "$2";;
"-a" | "admin" ) webpageFunc "$@";;
"-t" | "tail" ) tailFunc;;
"checkout" ) piholeCheckoutFunc "$@";;