From bf6d496e6813e38ee053e1b83c13fd4617b9f06e Mon Sep 17 00:00:00 2001 From: Kevin MacMartin Date: Fri, 8 Aug 2014 11:39:38 -0400 Subject: [PATCH] Shortened help and added p2p to hosts conversion script --- README.md | 4 ++++ buildhosts | 4 ++-- p2p-to-hosts | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100755 p2p-to-hosts diff --git a/README.md b/README.md index bfdbc00..92a1008 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,10 @@ Build and maintain `/etc/hosts` while including lists imported from local and re * `buildhosts unset`: remove lists imported from hosts list sources and restore `/etc/hosts` * `buildhosts help`: display the help +## Extra ## + +* The `p2p-to-hosts` script included in this repo can be used to convert blocklists in p2p format to hosts format (usable with this package). + ## Credits ## Written by Kevin MacMartin: diff --git a/buildhosts b/buildhosts index a8e406c..3e4677f 100755 --- a/buildhosts +++ b/buildhosts @@ -73,8 +73,8 @@ fi function buildhosts_help(){ echo -e "${HEADINGCOL} ${BUILDHOSTS_SCRIPT}:${RESETCOL} build and maintain /etc/hosts with local and remote hosts list files\n${HEADINGCOL} ${RESETCOL}" echo -e "${HEADINGCOL} commands:${RESETCOL}" - echo -e "${HEADINGCOL} ${RESETCOL}${SUCCESSCOL} ${BUILDHOSTS_SCRIPT} set:${RESETCOL} generate /etc/hosts using /etc/hosts.core and the configured sources" - echo -e "${HEADINGCOL} ${RESETCOL}${SUCCESSCOL} ${BUILDHOSTS_SCRIPT} unset:${RESETCOL} remove lists imported from hosts list sources and restore /etc/hosts" + echo -e "${HEADINGCOL} ${RESETCOL}${SUCCESSCOL} ${BUILDHOSTS_SCRIPT} set:${RESETCOL} add hosts lists to /etc/hosts" + echo -e "${HEADINGCOL} ${RESETCOL}${SUCCESSCOL} ${BUILDHOSTS_SCRIPT} unset:${RESETCOL} remove hosts lists from /etc/hosts" echo -e "${HEADINGCOL} ${RESETCOL}${SUCCESSCOL} ${BUILDHOSTS_SCRIPT} help:${RESETCOL} display this help" [[ "$1" -eq 1 ]] && exit 0 } diff --git a/p2p-to-hosts b/p2p-to-hosts new file mode 100755 index 0000000..21e3a98 --- /dev/null +++ b/p2p-to-hosts @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +if [[ -z "$1" ]] || [[ -z "$2" ]]; then + echo "Error: Run this scrip with a blocklist URL in p2p format and an output file as arguments" + 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]*$") +[[ ! $(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 + 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" + while [ ! "${IP1_1}.${IP1_2}.${IP1_3}.${IP1_4}" = "$IP2" ]; do + if [ "$IP1_4" -lt 255 ]; then + IP1_4=$(expr $IP1_4 + 1) + else + IP1_4=0 + if [ "$IP1_3" -lt 255 ]; then + IP1_3=$(expr $IP1_3 + 1) + else + IP1_3=0 + if [ "$IP1_2" -lt 255 ]; then + IP1_2=$(expr $IP1_2 + 1) + else + IP1_2=0 + if [ "$IP1_1" -lt 255 ]; then + IP1_1=$(expr $IP1_1 + 1) + else + break + fi + fi + fi + fi + echo "127.0.0.1 ${IP1_1}.${IP1_2}.${IP1_3}.${IP1_4}" >> "$2" + done +done