mirror of
https://github.com/pi-hole/pi-hole.git
synced 2024-11-15 02:42:58 +00:00
Merge pull request #1631 from pi-hole/tweak/gravity
Clean up and optimise Gravity
This commit is contained in:
commit
ef1ce7d4d6
4 changed files with 677 additions and 506 deletions
|
@ -66,15 +66,15 @@ HandleOther() {
|
||||||
domain="${1,,}"
|
domain="${1,,}"
|
||||||
|
|
||||||
# Check validity of domain
|
# 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
|
if [[ "${#domain}" -le 253 ]]; then
|
||||||
validDomain=$(perl -lne 'print if /^.{1,253}$/' <<< "${validDomain}") # Overall length check
|
validDomain=$(grep -P "^((-|_)*[a-z\d]((-|_)*[a-z\d])*(-|_)*)(\.(-|_)*([a-z\d]((-|_)*[a-z\d])*))*$" <<< "${domain}") # Valid chars check
|
||||||
validDomain=$(perl -lne 'print if /^[^\.]{1,63}(\.[^\.]{1,63})*$/' <<< "${validDomain}") # Length of each label
|
validDomain=$(grep -P "^[^\.]{1,63}(\.[^\.]{1,63})*$" <<< "${validDomain}") # Length of each label
|
||||||
|
fi
|
||||||
|
|
||||||
if [[ -z "${validDomain}" ]]; then
|
if [[ -n "${validDomain}" ]]; then
|
||||||
echo -e " ${CROSS} $1 is not a valid argument or domain name!"
|
|
||||||
else
|
|
||||||
echo -e " ${TICK} $1 is a valid domain name!"
|
|
||||||
domList=("${domList[@]}" ${validDomain})
|
domList=("${domList[@]}" ${validDomain})
|
||||||
|
else
|
||||||
|
echo -e " ${CROSS} ${domain} is not a valid argument or domain name!"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,6 +107,8 @@ AddDomain() {
|
||||||
[[ "${list}" == "${wildcardlist}" ]] && listname="wildcard blacklist"
|
[[ "${list}" == "${wildcardlist}" ]] && listname="wildcard blacklist"
|
||||||
|
|
||||||
if [[ "${list}" == "${whitelist}" || "${list}" == "${blacklist}" ]]; then
|
if [[ "${list}" == "${whitelist}" || "${list}" == "${blacklist}" ]]; then
|
||||||
|
[[ "${list}" == "${whitelist}" && -z "${type}" ]] && type="--whitelist-only"
|
||||||
|
[[ "${list}" == "${blacklist}" && -z "${type}" ]] && type="--blacklist-only"
|
||||||
bool=true
|
bool=true
|
||||||
# Is the domain in the list we want to add it to?
|
# Is the domain in the list we want to add it to?
|
||||||
grep -Ex -q "${domain}" "${list}" > /dev/null 2>&1 || bool=false
|
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
|
# Remove the /* from the end of the IP addresses
|
||||||
IPV4_ADDRESS=${IPV4_ADDRESS%/*}
|
IPV4_ADDRESS=${IPV4_ADDRESS%/*}
|
||||||
IPV6_ADDRESS=${IPV6_ADDRESS%/*}
|
IPV6_ADDRESS=${IPV6_ADDRESS%/*}
|
||||||
|
[[ -z "${type}" ]] && type="--wildcard-only"
|
||||||
bool=true
|
bool=true
|
||||||
# Is the domain in the list?
|
# Is the domain in the list?
|
||||||
grep -e "address=\/${domain}\/" "${wildcardlist}" > /dev/null 2>&1 || bool=false
|
grep -e "address=\/${domain}\/" "${wildcardlist}" > /dev/null 2>&1 || bool=false
|
||||||
|
@ -138,7 +140,7 @@ AddDomain() {
|
||||||
if [[ "${verbose}" == true ]]; then
|
if [[ "${verbose}" == true ]]; then
|
||||||
echo -e " ${INFO} Adding $1 to wildcard blacklist..."
|
echo -e " ${INFO} Adding $1 to wildcard blacklist..."
|
||||||
fi
|
fi
|
||||||
reload=true
|
reload="restart"
|
||||||
echo "address=/$1/${IPV4_ADDRESS}" >> "${wildcardlist}"
|
echo "address=/$1/${IPV4_ADDRESS}" >> "${wildcardlist}"
|
||||||
if [[ "${#IPV6_ADDRESS}" > 0 ]]; then
|
if [[ "${#IPV6_ADDRESS}" > 0 ]]; then
|
||||||
echo "address=/$1/${IPV6_ADDRESS}" >> "${wildcardlist}"
|
echo "address=/$1/${IPV6_ADDRESS}" >> "${wildcardlist}"
|
||||||
|
@ -161,6 +163,8 @@ RemoveDomain() {
|
||||||
|
|
||||||
if [[ "${list}" == "${whitelist}" || "${list}" == "${blacklist}" ]]; then
|
if [[ "${list}" == "${whitelist}" || "${list}" == "${blacklist}" ]]; then
|
||||||
bool=true
|
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
|
# 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
|
grep -Ex -q "${domain}" "${list}" > /dev/null 2>&1 || bool=false
|
||||||
if [[ "${bool}" == true ]]; then
|
if [[ "${bool}" == true ]]; then
|
||||||
|
@ -175,6 +179,7 @@ RemoveDomain() {
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
elif [[ "${list}" == "${wildcardlist}" ]]; then
|
elif [[ "${list}" == "${wildcardlist}" ]]; then
|
||||||
|
[[ -z "${type}" ]] && type="--wildcard-only"
|
||||||
bool=true
|
bool=true
|
||||||
# Is it in the list?
|
# Is it in the list?
|
||||||
grep -e "address=\/${domain}\/" "${wildcardlist}" > /dev/null 2>&1 || bool=false
|
grep -e "address=\/${domain}\/" "${wildcardlist}" > /dev/null 2>&1 || bool=false
|
||||||
|
@ -192,12 +197,10 @@ RemoveDomain() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Update Gravity
|
||||||
Reload() {
|
Reload() {
|
||||||
# Reload hosts file
|
|
||||||
echo ""
|
echo ""
|
||||||
echo -e " ${INFO} Updating gravity..."
|
pihole -g --skip-download "${type:-}"
|
||||||
echo ""
|
|
||||||
pihole -g -sd
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Displaylist() {
|
Displaylist() {
|
||||||
|
@ -243,6 +246,7 @@ fi
|
||||||
|
|
||||||
PoplistFile
|
PoplistFile
|
||||||
|
|
||||||
if ${reload}; then
|
if [[ "${reload}" != false ]]; then
|
||||||
Reload
|
# Ensure that "restart" is used for Wildcard updates
|
||||||
|
Reload "${reload}"
|
||||||
fi
|
fi
|
||||||
|
|
|
@ -229,20 +229,7 @@ Reboot() {
|
||||||
}
|
}
|
||||||
|
|
||||||
RestartDNS() {
|
RestartDNS() {
|
||||||
local str="Restarting DNS service"
|
/usr/local/bin/pihole restartdns
|
||||||
[[ -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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SetQueryLogOptions() {
|
SetQueryLogOptions() {
|
||||||
|
|
1077
gravity.sh
1077
gravity.sh
File diff suppressed because it is too large
Load diff
57
pihole
57
pihole
|
@ -12,8 +12,7 @@
|
||||||
readonly PI_HOLE_SCRIPT_DIR="/opt/pihole"
|
readonly PI_HOLE_SCRIPT_DIR="/opt/pihole"
|
||||||
readonly wildcardlist="/etc/dnsmasq.d/03-pihole-wildcard.conf"
|
readonly wildcardlist="/etc/dnsmasq.d/03-pihole-wildcard.conf"
|
||||||
readonly colfile="${PI_HOLE_SCRIPT_DIR}/COL_TABLE"
|
readonly colfile="${PI_HOLE_SCRIPT_DIR}/COL_TABLE"
|
||||||
|
source "${colfile}"
|
||||||
source ${colfile}
|
|
||||||
|
|
||||||
# Must be root to use this tool
|
# Must be root to use this tool
|
||||||
if [[ ! $EUID -eq 0 ]];then
|
if [[ ! $EUID -eq 0 ]];then
|
||||||
|
@ -27,7 +26,7 @@ if [[ ! $EUID -eq 0 ]];then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
webpageFunc() {
|
webpageFunc() {
|
||||||
source /opt/pihole/webpage.sh
|
source "${PI_HOLE_SCRIPT_DIR}/webpage.sh"
|
||||||
main "$@"
|
main "$@"
|
||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
|
@ -348,33 +347,35 @@ versionFunc() {
|
||||||
}
|
}
|
||||||
|
|
||||||
restartDNS() {
|
restartDNS() {
|
||||||
dnsmasqPid=$(pidof dnsmasq)
|
local svcOption svc str output status
|
||||||
local str="Restarting DNS service"
|
svcOption="${1:-}"
|
||||||
echo -ne " ${INFO} ${str}"
|
|
||||||
if [[ "${dnsmasqPid}" ]]; then
|
# Determine if we should reload or restart dnsmasq
|
||||||
# Service already running - reload config
|
if [[ "${svcOption}" =~ "reload" ]]; then
|
||||||
if [[ -x "$(command -v systemctl)" ]]; then
|
# Using SIGHUP will NOT re-read any *.conf files
|
||||||
output=$( { systemctl restart dnsmasq; } 2>&1 )
|
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
|
else
|
||||||
output=$( { service dnsmasq restart; } 2>&1 )
|
svcOption="start"
|
||||||
fi
|
|
||||||
if [[ -z "${output}" ]]; then
|
|
||||||
echo -e "${OVER} ${TICK} ${str}"
|
|
||||||
else
|
|
||||||
echo -e "${OVER} ${CROSS} ${output}"
|
|
||||||
fi
|
fi
|
||||||
|
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
|
else
|
||||||
# Service not running, start it up
|
[[ ! -t 1 ]] && local OVER=""
|
||||||
if [[ -x "$(command -v systemctl)" ]]; then
|
echo -e "${OVER} ${CROSS} ${output}"
|
||||||
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
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -655,7 +656,7 @@ case "${1}" in
|
||||||
"enable" ) piholeEnable 1;;
|
"enable" ) piholeEnable 1;;
|
||||||
"disable" ) piholeEnable 0 "$2";;
|
"disable" ) piholeEnable 0 "$2";;
|
||||||
"status" ) statusFunc "$2";;
|
"status" ) statusFunc "$2";;
|
||||||
"restartdns" ) restartDNS;;
|
"restartdns" ) restartDNS "$2";;
|
||||||
"-a" | "admin" ) webpageFunc "$@";;
|
"-a" | "admin" ) webpageFunc "$@";;
|
||||||
"-t" | "tail" ) tailFunc;;
|
"-t" | "tail" ) tailFunc;;
|
||||||
"checkout" ) piholeCheckoutFunc "$@";;
|
"checkout" ) piholeCheckoutFunc "$@";;
|
||||||
|
|
Loading…
Reference in a new issue