Created
January 22, 2018 05:57
-
-
Save ql-owo-lp/93f9737f7e5d6b90393ed35ccba6a4c1 to your computer and use it in GitHub Desktop.
adblock-generator.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!bin/bash | |
SUBSCRIPTION_LIST=( | |
https://easylist-downloads.adblockplus.org/easyprivacy.txt | |
https://easylist-downloads.adblockplus.org/easylistchina.txt | |
https://easylist-downloads.adblockplus.org/easylist.txt | |
https://easylist-downloads.adblockplus.org/malwaredomains_full.txt | |
https://easylist-downloads.adblockplus.org/exceptionrules.txt | |
) | |
IP=$(ifconfig eth0 | awk '/inet addr/{print substr($2,6)}') | |
SAVE_PATH='/etc/pihole' | |
ADBLOCK_RULES='adblock-rules.txt' | |
ADBLOCK_EXCEPT_RULES='adblock-except-rules.txt' | |
ADBLOCK_HOST='adblock-host.list' | |
generate_adblock_rules() { | |
TMP_DIR='/tmp/adblock-rules' | |
TMP_CACHE='.cache' | |
LIST=$1 | |
RULE_FILE=$2 | |
EXCEPT_RULE_FILE=$3 | |
if [[ -z "${LIST[@]}" || -z "${RULE_FILE}" || -z "${EXCEPT_RULE_FILE}" ]]; then | |
echo "At least one input is empty!" | |
exit 1 | |
fi | |
rm -rf ${TMP_DIR} && mkdir -p ${TMP_DIR} | |
# regular rules | |
for URL in "${LIST[@]}"; do | |
curl -s "${URL}" > "${TMP_DIR}/${TMP_CACHE}" | |
# regular rules | |
egrep '^\|\|[A-Za-z0-9\.-]+\^$' "${TMP_DIR}/${TMP_CACHE}" | awk '{print substr($0, 3, length($0)-3)}' >> "${TMP_DIR}/adblock.rules" | |
# exception rules | |
egrep '^@@\|\|[A-Za-z0-9\.-]+\^$' "${TMP_DIR}/${TMP_CACHE}" | awk '{print substr($0, 5, length($0)-5)}' >> "${TMP_DIR}/adblock.exception-rules" | |
done | |
# merge hosts | |
awk '!a[$0]++' "${TMP_DIR}/adblock.rules" > "${RULE_FILE}" | |
awk '!a[$0]++' "${TMP_DIR}/adblock.exception-rules" > "${EXCEPT_RULE_FILE}" | |
rm -rf ${TMP_DIR} | |
} | |
generate_host() { | |
awk "{print \"${IP}\", \$0}" $1 > $2 | |
} | |
generate_adblock_rules ${SUBSCRIPTION_LIST} "${SAVE_PATH}/${ADBLOCK_RULES}" "${SAVE_PATH}/${ADBLOCK_EXCEPT_RULES}" | |
generate_host "${SAVE_PATH}/${ADBLOCK_RULES}" "${SAVE_PATH}/${ADBLOCK_HOST}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment