Clean up formatting and add missing dependencies to buildhosts

This commit is contained in:
Kevin MacMartin 2017-05-24 10:44:38 -04:00
parent fa33357b92
commit 99ed481d97
2 changed files with 38 additions and 29 deletions

View file

@ -29,7 +29,7 @@ default_sources=(
'http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext' 'http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext'
) )
script_dependencies=('curl' 'sed') script_dependencies=('curl' 'mv' 'sed' 'sort')
# Set the script name # Set the script name
script_name="${0//*\/}" script_name="${0//*\/}"
@ -121,6 +121,7 @@ function buildhosts_build {
# Download the the current source into $source_data and fail if the result is empty # Download the the current source into $source_data and fail if the result is empty
source_data=$(curl -C - -s "$source" | tr -d "\r") source_data=$(curl -C - -s "$source" | tr -d "\r")
[[ -n "$source_data" ]] \ [[ -n "$source_data" ]] \
|| buildhosts_error "Could not download list @ $source" || buildhosts_error "Could not download list @ $source"
@ -203,7 +204,7 @@ done
} }
# Fail if run with no commands # Fail if run with no commands
[[ -z "$1" ]] && buildhosts_error "A valid command must be provided" 1 [[ -z "$1" ]] && buildhosts_error 'A valid command must be provided' 1
# Run the command entered by the user # Run the command entered by the user
case "$1" in case "$1" in
@ -217,6 +218,6 @@ case "$1" in
buildhosts_help 1 buildhosts_help 1
;; ;;
*) *)
buildhosts_error "Invalid command" 1 buildhosts_error 'Invalid command' 1
;; ;;
esac esac

View file

@ -1,44 +1,52 @@
#!/usr/bin/env bash #!/usr/bin/env bash
if [[ -z "$1" ]] || [[ -z "$2" ]]; then [[ -z "$1" || -z "$2" ]] && {
echo "Error: Run this scrip with a blocklist URL in p2p format and an output file as arguments" printf '%s\n' 'Error: Run this scrip with a blocklist URL in p2p format and an output file as arguments' >&2
exit 1 exit 1
fi }
P2PLIST=$(sed 's|^.*:||' < <(wget -q -O - "$1" | gunzip) | grep -E "^[0-9]*.[0-9]*.[0-9]*.[0-9]*-[0-9]*.[0-9]*.[0-9]*.[0-9]*$") p2plist=$(sed 's|^.*:||' < <(wget -q -O - "$1" | gunzip) | grep -E "^[0-9]*.[0-9]*.[0-9]*.[0-9]*-[0-9]*.[0-9]*.[0-9]*.[0-9]*$")
[[ ! $(grep -E "^[0-9]*.[0-9]*.[0-9]*.[0-9]*-[0-9]*.[0-9]*.[0-9]*.[0-9]*$" <<< "$P2PLIST") ]] && echo "Error: The URL ${1} does not appear to contain a P2P-formatted blocklist" && exit 1
for address_range in ${P2PLIST[@]}; do grep -q -E "^[0-9]*.[0-9]*.[0-9]*.[0-9]*-[0-9]*.[0-9]*.[0-9]*.[0-9]*$" <<< "$p2plist" || {
IP1=$(cut -d '-' -f 1 <<< "$address_range") printf '%s\n' "Error: The URL $1 does not appear to contain a P2P-formatted blocklist" >&2
IP1_1=$(cut -d '.' -f 1 <<< "$IP1") exit 1
IP1_2=$(cut -d '.' -f 2 <<< "$IP1") }
IP1_3=$(cut -d '.' -f 3 <<< "$IP1")
IP1_4=$(cut -d '.' -f 4 <<< "$IP1")
IP2=$(cut -d '-' -f 2 <<< "$address_range") for address_range in "${p2plist[@]}"; do
ip1=$(cut -d '-' -f 1 <<< "$address_range")
ip1_1=$(cut -d '.' -f 1 <<< "$ip1")
ip1_2=$(cut -d '.' -f 2 <<< "$ip1")
ip1_3=$(cut -d '.' -f 3 <<< "$ip1")
ip1_4=$(cut -d '.' -f 4 <<< "$ip1")
ip2=$(cut -d '-' -f 2 <<< "$address_range")
echo "127.0.0.1 ${IP1_1}.${IP1_2}.${IP1_3}.${IP1_4}" >> "$2" printf '%s\n' "127.0.0.1 ${ip1_1}.${ip1_2}.${ip1_3}.${ip1_4}" >> "$2"
while [ ! "${IP1_1}.${IP1_2}.${IP1_3}.${IP1_4}" = "$IP2" ]; do
if [ "$IP1_4" -lt 255 ]; then while [[ ! "${ip1_1}.${ip1_2}.${ip1_3}.${ip1_4}" = "$ip2" ]]; do
IP1_4=$(expr $IP1_4 + 1) if (( ip1_4 < 255 )); then
ip1_4=$(( ip1_4 + 1 ))
else else
IP1_4=0 ip1_4=0
if [ "$IP1_3" -lt 255 ]; then
IP1_3=$(expr $IP1_3 + 1) if (( ip1_3 < 255 )); then
ip1_3=$(( ip1_3 + 1 ))
else else
IP1_3=0 ip1_3=0
if [ "$IP1_2" -lt 255 ]; then
IP1_2=$(expr $IP1_2 + 1) if (( ip1_2 < 255 )); then
ip1_2=$(( ip1_2 + 1 ))
else else
IP1_2=0 ip1_2=0
if [ "$IP1_1" -lt 255 ]; then
IP1_1=$(expr $IP1_1 + 1) if (( ip1_1 < 255 )); then
ip1_1=$(( ip1_1 + 1 ))
else else
break break
fi fi
fi fi
fi fi
fi fi
echo "127.0.0.1 ${IP1_1}.${IP1_2}.${IP1_3}.${IP1_4}" >> "$2"
printf '%s\n' "127.0.0.1 ${ip1_1}.${ip1_2}.${ip1_3}.${ip1_4}" >> "$2"
done done
done done