Add functions

Start to add functions for code reuse
This commit is contained in:
Dan Schaper 2015-11-23 00:36:01 -08:00
parent f02c37ec07
commit 668d14e32d
1 changed files with 27 additions and 26 deletions

View File

@ -46,36 +46,35 @@ echo "** Neutrino emissions detected..."
# Create the pihole resource directory if it doesn't exist. Future files will be stored here # Create the pihole resource directory if it doesn't exist. Future files will be stored here
if [[ -d $piholeDir ]];then if [[ -d $piholeDir ]];then
: # Temporary hack to allow non-root access to pihole directory
# Will update later, needed for existing installs, new installs should
# create this directory as non-root
sudo chmod 777 $piholeDir
find "$piholeDir" -type f -exec sudo chmod 666 {} \;
else else
echo "** Creating pihole directory..." echo "** Creating pihole directory..."
sudo mkdir $piholeDir mkdir $piholeDir
fi fi
# Loop through domain list. Download each one and remove commented lines (lines beginning with '# 'or '/') and blank lines ###########################
for ((i = 0; i < "${#sources[@]}"; i++)) function gravity_patterncheck() {
do
url=${sources[$i]}
# Get just the domain from the URL
domain=$(echo "$url" | cut -d'/' -f3)
# Save the file as list.#.domain patternBuffer=$1
saveLocation=$piholeDir/list.$i.$domain.$justDomainsExtension
agent="Mozilla/10.0" # check if the patternbuffer is a non-zero length file
if [[ -s "$patternBuffer" ]];then
echo -n "Getting $domain list... " # Remove comments and print only the domain name
# Most of the lists downloaded are already in hosts file format but the spacing/formating is not contigious
# Use a case statement to download lists that need special cURL commands # This helps with that and makes it easier to read
# to complete properly and reset the user agent when required # It also helps with debugging so each stage of the script can be researched more in depth
case "$domain" in awk '($1 !~ /^#/) { if (NF>1) {print $2} else {print $1}}' $patternBuffer | \
"adblock.mahakala.is") sed -nr -e 's/\.{2,}/./g' -e '/\./p' > $saveLocation
agent='Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0' echo "Done."
cmd_ext="-e http://forum.xda-developers.com/" else
;; # curl didn't download any host files, probably because of the date check
echo "Transporter logic detected no changes, pattern skipped..."
"pgl.yoyo.org") fi
echo "** $numberOf domains being pulled in by gravity..." }
# Remove carriage returns and preceding whitespace # Remove carriage returns and preceding whitespace
# not really needed anymore? # not really needed anymore?
@ -90,10 +89,12 @@ do
echo "** Formatting domains into a HOSTS file..." echo "** Formatting domains into a HOSTS file..."
cat $piholeDir/$eventHorizon | awk '{sub(/\r$/,""); print "'"$piholeIP"' " $0}' > $piholeDir/$accretionDisc cat $piholeDir/$eventHorizon | awk '{sub(/\r$/,""); print "'"$piholeIP"' " $0}' > $piholeDir/$accretionDisc
# Copy the file over as /etc/pihole/gravity.list so dnsmasq can use it # Copy the file over as /etc/pihole/gravity.list so dnsmasq can use it
sudo cp $piholeDir/$accretionDisc $adList cp $piholeDir/$accretionDisc $adList
kill -HUP $(pidof dnsmasq) sudo kill -HUP $(pidof dnsmasq)
} }
gravity_spinup
# Whitelist (if applicable) then remove duplicates and format for dnsmasq # Whitelist (if applicable) then remove duplicates and format for dnsmasq
if [[ -r $whitelist ]];then if [[ -r $whitelist ]];then
# Remove whitelist entries # Remove whitelist entries