diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..e09826b --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,36 @@ +--- +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.1.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + args: + - '--allow-multiple-documents' + - id: check-json + - id: check-added-large-files + - id: check-symlinks + - id: destroyed-symlinks + - id: check-executables-have-shebangs + - id: requirements-txt-fixer + - repo: https://github.com/shellcheck-py/shellcheck-py + rev: v0.8.0.3 + hooks: + - id: shellcheck + - repo: https://github.com/AleksaC/hadolint-py + rev: v2.8.0 + hooks: + - id: hadolint + args: + - '--ignore' + - 'DL3007' + - '--ignore' + - 'DL3008' + - '--ignore' + - 'DL3013' + - repo: https://github.com/sirosen/check-jsonschema + rev: 0.9.1 + hooks: + - id: check-github-workflows + - id: check-github-actions diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..fdddb29 --- /dev/null +++ b/LICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/README.md b/README.md index a8d301a..8f223c3 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,23 @@ privoxy-blocklist ================= -Script converting AdBlock Plus rules into privoxy format. \ No newline at end of file +[![GitHub release](https://img.shields.io/github/release/Andrwe/privoxy-blocklist?include_prereleases=&sort=semver&color=blue)](https://github.com/Andrwe/privoxy-blocklist/releases/) +[![License](https://img.shields.io/badge/License-UNLICENSE-blue)](#license) +[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/Andrwe/privoxy-blocklist/master.svg)](https://results.pre-commit.ci/latest/github/Andrwe/privoxy-blocklist/master) + +Script converting AdBlock Plus rules into privoxy format. + +## How does it work + +The script `privoxy-blocklist.sh` downloads AdBlock Plus filter files and generates privoxy compatible filter and action files based on these. +After the generation is done it modifies the privoxy configuration files `/etc/privoxy/config` to import the generated files. + +Due to this behaviour the script must run as root user to be able to modify the privoxy configuration file. + +## Usage + +Either run `privoxy-blocklist.sh` manually with root privileges (e.g., `sudo privoxy-blocklist.sh`) or via root cronjob. + +## Kudos + +* [Badge Generator](https://michaelcurrin.github.io/badge-generator/#/) for providing easy method to generate badges/shields diff --git a/helper/install_deps.sh b/helper/install_deps.sh new file mode 100755 index 0000000..0f1ffa0 --- /dev/null +++ b/helper/install_deps.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +set -e + +exists() { + if command -v "$1" >/dev/null 2>&1; then + return 0 + fi + return 1 +} + +if exists apk;then + apk add --no-cache privoxy sed grep bash wget + exit 0 +fi +if exists apt-get; then + export DEBIAN_FRONTEND=noninteractive + apt-get update -qq -y + apt-get install -y privoxy sed grep bash wget + exit 0 +fi +if exists pacman; then + pacman -Sy privoxy sed grep bash wget + exit 0 +fi +echo "no install command found" +exit 1 diff --git a/privoxy-blocklist.sh b/privoxy-blocklist.sh index 3d37be1..41dce97 100755 --- a/privoxy-blocklist.sh +++ b/privoxy-blocklist.sh @@ -9,7 +9,7 @@ # ################## # -# Sumary: +# Sumary: # This script downloads, converts and installs # AdblockPlus lists into Privoxy # @@ -25,6 +25,8 @@ # ###################################################################### +set -euo pipefail + # script config-file SCRIPTCONF=/etc/conf.d/privoxy-blacklist # dependencies @@ -49,11 +51,15 @@ function usage() echo " -r: Remove all lists build by this script." } -[ ${UID} -ne 0 ] && echo -e "Root privileges needed. Exit.\n\n" && usage && exit 1 +if [ ${UID} -ne 0 ]; then + echo -e "Root privileges needed. Exit.\n\n" + usage + exit 1 +fi -for dep in ${DEPENDS[@]} +for dep in "${DEPENDS[@]}" do - if ! type -p ${dep} >/dev/null + if ! type -p "${dep}" >/dev/null then echo "The command ${dep} can't be found. Please install the package providing ${dep} and run $0 again. Exit" >&2 exit 1 @@ -68,39 +74,49 @@ fi function debug() { - [ ${DBG} -ge ${2} ] && echo -e "${1}" + if [ "${DBG}" -ge "${2}" ]; then + echo -e "${1}" + fi +} + +function error() +{ + printf '\e[1;31m%s\e[0m' "$@" >&2 } function main() { - for url in ${URLS[@]} + for url in "${URLS[@]}" do debug "Processing ${url} ...\n" 0 - file=${TMPDIR}/$(basename ${url}) + file="${TMPDIR}/$(basename "${url}")" actionfile=${file%\.*}.script.action filterfile=${file%\.*}.script.filter - list=$(basename ${file%\.*}) + list="$(basename "${file%\.*}")" # download list debug "Downloading ${url} ..." 0 - wget -t 3 --no-check-certificate -O ${file} ${url} >${TMPDIR}/wget-${url//\//#}.log 2>&1 - debug "$(cat ${TMPDIR}/wget-${url//\//#}.log)" 2 + wget -t 3 --no-check-certificate -O "${file}" "${url}" >"${TMPDIR}/wget-${url//\//#}.log" 2>&1 + debug "$(cat "${TMPDIR}/wget-${url//\//#}.log")" 2 debug ".. downloading done." 0 - [ "$(grep -E '^.*\[Adblock.*\].*$' ${file})" == "" ] && echo "The list recieved from ${url} isn't an AdblockPlus list. Skipped" && continue + if ! grep -qE '^.*\[Adblock.*\].*$' "${file}"; then + echo "The list recieved from ${url} isn't an AdblockPlus list. Skipped" + continue + fi # convert AdblockPlus list to Privoxy list # blacklist of urls debug "Creating actionfile for ${list} ..." 1 - echo -e "{ +block{${list}} }" > ${actionfile} - sed '/^!.*/d;1,1 d;/^@@.*/d;/\$.*/d;/#/d;s/\./\\./g;s/\?/\\?/g;s/\*/.*/g;s/(/\\(/g;s/)/\\)/g;s/\[/\\[/g;s/\]/\\]/g;s/\^/[\/\&:\?=_]/g;s/^||/\./g;s/^|/^/g;s/|$/\$/g;/|/d' ${file} >> ${actionfile} + echo -e "{ +block{${list}} }" > "${actionfile}" + sed '/^!.*/d;1,1 d;/^@@.*/d;/\$.*/d;/#/d;s/\./\\./g;s/\?/\\?/g;s/\*/.*/g;s/(/\\(/g;s/)/\\)/g;s/\[/\\[/g;s/\]/\\]/g;s/\^/[\/\&:\?=_]/g;s/^||/\./g;s/^|/^/g;s/|$/\$/g;/|/d' "${file}" >> "${actionfile}" debug "... creating filterfile for ${list} ..." 1 - echo "FILTER: ${list} Tag filter of ${list}" > ${filterfile} + echo "FILTER: ${list} Tag filter of ${list}" > "${filterfile}" # set filter for html elements - sed '/^#/!d;s/^##//g;s/^#\(.*\)\[.*\]\[.*\]*/s@<([a-zA-Z0-9]+)\\s+.*id=.?\1.*>.*<\/\\1>@@g/g;s/^#\(.*\)/s@<([a-zA-Z0-9]+)\\s+.*id=.?\1.*>.*<\/\\1>@@g/g;s/^\.\(.*\)/s@<([a-zA-Z0-9]+)\\s+.*class=.?\1.*>.*<\/\\1>@@g/g;s/^a\[\(.*\)\]/s@.*<\/a>@@g/g;s/^\([a-zA-Z0-9]*\)\.\(.*\)\[.*\]\[.*\]*/s@<\1.*class=.?\2.*>.*<\/\1>@@g/g;s/^\([a-zA-Z0-9]*\)#\(.*\):.*[\:[^:]]*[^:]*/s@<\1.*id=.?\2.*>.*<\/\1>@@g/g;s/^\([a-zA-Z0-9]*\)#\(.*\)/s@<\1.*id=.?\2.*>.*<\/\1>@@g/g;s/^\[\([a-zA-Z]*\).=\(.*\)\]/s@\1^=\2>@@g/g;s/\^/[\/\&:\?=_]/g;s/\.\([a-zA-Z0-9]\)/\\.\1/g' ${file} >> ${filterfile} + sed '/^#/!d;s/^##//g;s/^#\(.*\)\[.*\]\[.*\]*/s@<([a-zA-Z0-9]+)\\s+.*id=.?\1.*>.*<\/\\1>@@g/g;s/^#\(.*\)/s@<([a-zA-Z0-9]+)\\s+.*id=.?\1.*>.*<\/\\1>@@g/g;s/^\.\(.*\)/s@<([a-zA-Z0-9]+)\\s+.*class=.?\1.*>.*<\/\\1>@@g/g;s/^a\[\(.*\)\]/s@.*<\/a>@@g/g;s/^\([a-zA-Z0-9]*\)\.\(.*\)\[.*\]\[.*\]*/s@<\1.*class=.?\2.*>.*<\/\1>@@g/g;s/^\([a-zA-Z0-9]*\)#\(.*\):.*[\:[^:]]*[^:]*/s@<\1.*id=.?\2.*>.*<\/\1>@@g/g;s/^\([a-zA-Z0-9]*\)#\(.*\)/s@<\1.*id=.?\2.*>.*<\/\1>@@g/g;s/^\[\([a-zA-Z]*\).=\(.*\)\]/s@\1^=\2>@@g/g;s/\^/[\/\&:\?=_]/g;s/\.\([a-zA-Z0-9]\)/\\.\1/g' "${file}" >> "${filterfile}" debug "... filterfile created - adding filterfile to actionfile ..." 1 - echo "{ +filter{${list}} }" >> ${actionfile} - echo "*" >> ${actionfile} + echo "{ +filter{${list}} }" >> "${actionfile}" + echo "*" >> "${actionfile}" debug "... filterfile added ..." 1 # create domain based whitelist @@ -128,38 +144,38 @@ function main() debug "... creating and adding whitlist for urls ..." 1 # whitelist of urls - echo "{ -block }" >> ${actionfile} - sed '/^@@.*/!d;s/^@@//g;/\$.*/d;/#/d;s/\./\\./g;s/\?/\\?/g;s/\*/.*/g;s/(/\\(/g;s/)/\\)/g;s/\[/\\[/g;s/\]/\\]/g;s/\^/[\/\&:\?=_]/g;s/^||/\./g;s/^|/^/g;s/|$/\$/g;/|/d' ${file} >> ${actionfile} + echo "{ -block }" >> "${actionfile}" + sed '/^@@.*/!d;s/^@@//g;/\$.*/d;/#/d;s/\./\\./g;s/\?/\\?/g;s/\*/.*/g;s/(/\\(/g;s/)/\\)/g;s/\[/\\[/g;s/\]/\\]/g;s/\^/[\/\&:\?=_]/g;s/^||/\./g;s/^|/^/g;s/|$/\$/g;/|/d' "${file}" >> "${actionfile}" debug "... created and added whitelist - creating and adding image handler ..." 1 # whitelist of image urls - echo "{ -block +handle-as-image }" >> ${actionfile} - sed '/^@@.*/!d;s/^@@//g;/\$.*image.*/!d;s/\$.*image.*//g;/#/d;s/\./\\./g;s/\?/\\?/g;s/\*/.*/g;s/(/\\(/g;s/)/\\)/g;s/\[/\\[/g;s/\]/\\]/g;s/\^/[\/\&:\?=_]/g;s/^||/\./g;s/^|/^/g;s/|$/\$/g;/|/d' ${file} >> ${actionfile} + echo "{ -block +handle-as-image }" >> "${actionfile}" + sed '/^@@.*/!d;s/^@@//g;/\$.*image.*/!d;s/\$.*image.*//g;/#/d;s/\./\\./g;s/\?/\\?/g;s/\*/.*/g;s/(/\\(/g;s/)/\\)/g;s/\[/\\[/g;s/\]/\\]/g;s/\^/[\/\&:\?=_]/g;s/^||/\./g;s/^|/^/g;s/|$/\$/g;/|/d' "${file}" >> "${actionfile}" debug "... created and added image handler ..." 1 debug "... created actionfile for ${list}." 1 - + # install Privoxy actionsfile - install -o ${PRIVOXY_USER} -g ${PRIVOXY_GROUP} ${VERBOSE} ${actionfile} ${PRIVOXY_DIR} - if [ "$(grep $(basename ${actionfile}) ${PRIVOXY_CONF})" == "" ] + install -o "${PRIVOXY_USER}" -g "${PRIVOXY_GROUP}" "${VERBOSE[@]}" "${actionfile}" "${PRIVOXY_DIR}" + if ! grep -q "$(basename "${actionfile}")" "${PRIVOXY_CONF}" then debug "\nModifying ${PRIVOXY_CONF} ..." 0 - sed "s/^actionsfile user\.action/actionsfile $(basename ${actionfile})\nactionsfile user.action/" ${PRIVOXY_CONF} > ${TMPDIR}/config + sed "s/^actionsfile user\.action/actionsfile $(basename "${actionfile}")\nactionsfile user.action/" "${PRIVOXY_CONF}" > "${TMPDIR}/config" debug "... modification done.\n" 0 debug "Installing new config ..." 0 - install -o ${PRIVOXY_USER} -g ${PRIVOXY_GROUP} ${VERBOSE} ${TMPDIR}/config ${PRIVOXY_CONF} + install -o "${PRIVOXY_USER}" -g "${PRIVOXY_GROUP}" "${VERBOSE[@]}" "${TMPDIR}/config" "${PRIVOXY_CONF}" debug "... installation done\n" 0 - fi + fi # install Privoxy filterfile - install -o ${PRIVOXY_USER} -g ${PRIVOXY_GROUP} ${VERBOSE} ${filterfile} ${PRIVOXY_DIR} - if [ "$(grep $(basename ${filterfile}) ${PRIVOXY_CONF})" == "" ] + install -o "${PRIVOXY_USER}" -g "${PRIVOXY_GROUP}" "${VERBOSE[@]}" "${filterfile}" "${PRIVOXY_DIR}" + if ! grep -q "$(basename "${filterfile}")" "${PRIVOXY_CONF}" then debug "\nModifying ${PRIVOXY_CONF} ..." 0 - sed "s/^\(#*\)filterfile user\.filter/filterfile $(basename ${filterfile})\n\1filterfile user.filter/" ${PRIVOXY_CONF} > ${TMPDIR}/config + sed "s/^\(#*\)filterfile user\.filter/filterfile $(basename "${filterfile}")\n\1filterfile user.filter/" "${PRIVOXY_CONF}" > "${TMPDIR}/config" debug "... modification done.\n" 0 debug "Installing new config ..." 0 - install -o ${PRIVOXY_USER} -g ${PRIVOXY_GROUP} ${VERBOSE} ${TMPDIR}/config ${PRIVOXY_CONF} + install -o "${PRIVOXY_USER}" -g "${PRIVOXY_GROUP}" "${VERBOSE[@]}" "${TMPDIR}/config" "${PRIVOXY_CONF}" debug "... installation done\n" 0 - fi + fi debug "... ${url} installed successfully.\n" 0 done @@ -168,25 +184,26 @@ function main() if [[ ! -f "${SCRIPTCONF}" ]] then echo "No config found in ${SCRIPTCONF}. Creating default one and exiting because you might have to adjust it." - echo "# Config of privoxy-blocklist + cat > "${SCRIPTCONF}" < "${SCRIPTCONF}" +EOF exit 1 fi -[[ ! -r "${SCRIPTCONF}" ]] && debug "Can't read ${SCRIPTCONF}. Permission denied." -1 +if [[ ! -r "${SCRIPTCONF}" ]]; then + debug "Can't read ${SCRIPTCONF}. Permission denied." -1 +fi # load script config +# shellcheck disable=SC1090 source "${SCRIPTCONF}" # load privoxy config -[[ -r "${INIT_CONF}" ]] && source "${INIT_CONF}" +# shellcheck disable=SC1090 +if [[ -r "${INIT_CONF}" ]]; then + source "${INIT_CONF}" +fi # check whether needed variables are set -[[ -z "${PRIVOXY_CONF}" ]] && echo "\$PRIVOXY_CONF isn't set please either provice a valid initscript config or set it in ${SCRIPTCONF} ." >&2 && exit 1 -[[ -z "${PRIVOXY_USER}" ]] && echo "\$PRIVOXY_USER isn't set please either provice a valid initscript config or set it in ${SCRIPTCONF} ." >&2 && exit 1 -[[ -z "${PRIVOXY_GROUP}" ]] && echo "\$PRIVOXY_GROUP isn't set please either provice a valid initscript config or set it in ${SCRIPTCONF} ." >&2 && exit 1 +if [[ -z "${PRIVOXY_CONF}" ]]; then + error "\$PRIVOXY_CONF isn't set." + echo "Please either provide a valid initscript config or set it in ${SCRIPTCONF} ." >&2 + exit 1 +fi +if [[ -z "${PRIVOXY_USER}" ]]; then + error "\$PRIVOXY_USER isn't set" + echo "Please either provide a valid initscript config or set it in ${SCRIPTCONF} ." >&2 + exit 1 +fi +if [[ -z "${PRIVOXY_GROUP}" ]]; then + error "\$PRIVOXY_GROUP isn't set." + echo "Please either provide a valid initscript config or set it in ${SCRIPTCONF} ." >&2 + exit 1 +fi # set command to be run on exit -[ ${DBG} -le 2 ] && trap "rm -fr ${TMPDIR};exit" INT TERM EXIT +if [ "${DBG}" -le 2 ]; then + trap 'rm -fr "${TMPDIR}";exit' INT TERM EXIT +fi # set privoxy config dir -PRIVOXY_DIR="$(dirname ${PRIVOXY_CONF})" +PRIVOXY_DIR="$(dirname "${PRIVOXY_CONF}")" + +# file to store current PID +PID_FILE="${TMPDIR}/${TMPNAME}.lock" # create temporary directory and lock file -install -d -m700 ${TMPDIR} +install -d -m700 "${TMPDIR}" # check lock file -if [ -f "${TMPDIR}/${TMPNAME}.lock" ] +if [ -f "${PID_FILE}" ] then - read -r fpid <"${TMPDIR}/${TMPNAME}.lock" - ppid=$(pidof -o %PPID -x "${TMPNAME}") - if [[ $fpid = "${ppid}" ]] + if pgrep -P "$(< "${PID_FILE}")" then - echo "An Instance of ${TMPNAME} is already running. Exit" && exit 1 - else - debug "Found dead lock file." 0 - rm -f "${TMPDIR}/${TMPNAME}.lock" - debug "File removed." 0 + echo "An Instance of ${TMPNAME} is already running. Exit" + exit 1 fi + debug "Found dead lock file." 0 + rm -f "${PID_FILE}" + debug "File removed." 0 fi # safe PID in lock-file -echo $$ > "${TMPDIR}/${TMPNAME}.lock" +echo $$ > "${PID_FILE}" + +VERBOSE=() # loop for options while getopts ":hrqv:" opt do - case "${opt}" in + case "${opt}" in "v") DBG="${OPTARG}" - VERBOSE="-v" + VERBOSE=("-v") ;; "q") DBG=-1 ;; "r") - read -p "Do you really want to remove all build lists?(y/N) " choice - [ "${choice}" != "y" ] && exit 0 - rm -rf ${PRIVOXY_DIR}/*.script.{action,filter} && \ - sed '/^actionsfile .*\.script\.action$/d;/^filterfile .*\.script\.filter$/d' -i ${PRIVOXY_CONF} && echo "Lists removed." && exit 0 + read -rp "Do you really want to remove all build lists?(y/N) " choice + if [ "${choice}" != "y" ]; then + exit 0 + fi + if rm -rf "${PRIVOXY_DIR}/"*.script.{action,filter} \ + && sed '/^actionsfile .*\.script\.action$/d;/^filterfile .*\.script\.filter$/d' -i "${PRIVOXY_CONF}"; then + echo "Lists removed." + exit 0 + fi echo -e "An error occured while removing the lists.\nPlease have a look into ${PRIVOXY_DIR} whether there are .script.* files and search for *.script.* in ${PRIVOXY_CONF}." exit 1 ;; @@ -273,5 +318,9 @@ main # restore default exit command trap - INT TERM EXIT -[ ${DBG} -lt 3 ] && rm -r ${VERBOSE} "${TMPDIR}" +if [ "${DBG}" -lt 3 ]; then + rm -r "${VERBOSE[@]}" "${TMPDIR}" +fi exit 0 + +# vim: ts=4 sw=4 et diff --git a/privoxy-blocklist_test.sh b/privoxy-blocklist_test.sh deleted file mode 100755 index c44d046..0000000 --- a/privoxy-blocklist_test.sh +++ /dev/null @@ -1,312 +0,0 @@ -#!/bin/bash -# -###################################################################### -# -# Author: Andrwe Lord Weber -# Mail: lord-weber-andrwerenona-studiosorg -# Version: 0.2 -# URL: http://andrwe.dyndns.org/doku.php/blog/scripting/bash/privoxy-blocklist -# -################## -# -# Sumary: -# This script downloads, converts and installs -# AdblockPlus lists into Privoxy -# -###################################################################### - -###################################################################### -# -# TODO: -# - implement: -# domain-based filter -# id->class combination -# calss->id combination -# -###################################################################### - -###################################################################### -# -# script variables and functions -# -###################################################################### - -# array of URL for AdblockPlus lists -URLS=("https://easylist-downloads.adblockplus.org/easylist.txt" "https://easylist-downloads.adblockplus.org/easylistgermany.txt" "http://adblockplus.mozdev.org/easylist/easylist.txt") -# privoxy config dir (default: /etc/privoxy/) -CONFDIR=/etc/privoxy -# directory for temporary files -TMPDIR=/tmp/privoxy-blocklist-test -TMPNAME=$(basename ${0}) - -###################################################################### -# -# No changes needed after this line. -# -###################################################################### - -function usage() -{ - echo "${TMPNAME} is a script to convert AdBlockPlus-lists into Privoxy-lists and install them." - echo " " - echo "Options:" - echo " -h: Show this help." - echo " -q: Don't give any output." - echo " -v 1: Enable verbosity 1. Show a little bit more output." - echo " -v 2: Enable verbosity 2. Show a lot more output." - echo " -v 3: Enable verbosity 3. Show all possible output and don't delete temporary files.(For debugging only!!)" - echo " -r: Remove all lists build by this script." -} - -[ ${UID} -ne 0 ] && echo -e "Root privileges needed. Exit.\n\n" && usage && exit 1 - -# check whether an instance is already running -[ -e ${TMPDIR}/${TMPNAME}.lock ] && echo "An Instance of ${TMPNAME} is already running. Exit" && exit - -DBG=0 - -function debug() -{ - [ ${DBG} -ge ${2} ] && echo -e "${1}" -} - -function escapeLine() -{ - local pline="$1" - pline="${pline//./\.}" - pline="${pline//\?/\?}" - pline="${pline//\*/\*}" - pline="${pline//(/\(}" - pline="${pline//)/\)}" - pline="${pline//[/\[}" - pline="${pline//]/\]}" - pline="${pline//\^/[\/\&:\?=_]}" - pline="${pline/||/\.}" - pline="${pline/|/^}" - pline="${pline/%|/\$}" - echo "$pline" -} -function main() -{ - local cpoptions="" - [ ${DBG} -gt 0 ] && cpoptions="-v" - - for url in ${URLS[@]} - do - debug "Processing ${url} ...\n" 0 - local file=${TMPDIR}/$(basename ${url}) - local actionfile=${file%\.*}.script.action - local whitefile=${file%\.*}.script.white - local whiteimgfile=${file%\.*}.script.whiteimg - local filterfile=${file%\.*}.script.filter - local list=$(basename ${file%\.*}) - - # download list - debug "Downloading ${url} ..." 0 - wget -t 3 --no-check-certificate -O ${file} ${url} >${TMPDIR}/wget-${url//\//#}.log 2>&1 - debug "$(cat ${TMPDIR}/wget-${url//\//#}.log)" 2 - debug ".. downloading done." 0 - [ "$(grep -E '^\[Adblock.*\]$' ${file})" == "" ] && echo "The list recieved from ${url} isn't an AdblockPlus list. Skipped" && continue - - local line pline wline pfile - # blacklist of urls - debug "Creating actionfile for ${list} ..." 1 - echo -e "{ +block{${list}} }" > ${actionfile} - debug "... creating filterfile for ${list} ..." 1 - echo "FILTER: ${list} Tag filter of ${list}" > ${filterfile} - debug "... creating whitlistfile for urls ..." 1 - # whitelist of urls - echo "{ -block }" > ${whitefile} - # whitelist of images for urls - debug "... creating whitlistfile of images for urls ..." 1 - echo "{ -block +handle-as-image }" > ${whiteimgfile} - debug "... processing listfile ..." 0 - - # convert AdblockPlus list to Privoxy list - while read line - do - # debug "total line: $line" 2 - if [[ ${line:0:1} = ! ]] || [[ $line =~ ^[Adblock ]] - then - continue - fi - # set filter for html elements - if [[ ${line:0:1} = "#" ]] - then - pfile="$filterfile" - wline="${line:2}" - if [[ ${wline:0:1} = \# ]] - then - wline="${wline:1}" - pline=".*<\/\1>||g" - elif [[ ${wline:0:1} = \. ]] - then - pline="s|<([a-zA-Z0-9]+)\s+.*class=.?${wline:1}.*>.*<\/\1>||g" - elif [[ ${wline:0:2} = a\[ ]] - then - pline="s|.*<\/a>||g" - elif [[ ${wline} =~ ^[a-zA-Z0-9]*.* ]] - then - local tag="${wline/[#.]*/}" - wline="${wline:${#tag}}" - if [[ ${wline:0:1} = \# ]] - then - wline="${wline:1}" - pline=".*<\/$tag>||g" - elif [[ ${wline:0:1} = \. ]] - then - pline="s|<$tag\s+.*class=.?${wline:1}.*>.*<\/$tag>||g" - fi - unset tag - elif [[ ${wline:0:1} = \[ ]] - then - local firstpart="${wline/=*/}" - local secpart="${wline/*=/}" - pline="s|${firstpart:1}=${secpart:0:${#secpart}-1}.*>||g" - fi - if [[ -n "${pline}" ]] - then - pline="$(escapeLine "$pline")" - fi - # whitelist of urls - elif [[ ${line:0:2} = @@ ]] - then - pfile="$whitefile" - wline="${line:2}" - if [[ $line =~ \$.*image ]] - then - wline="${wline//\$*/}" - pfile="$whiteimgfile" - fi - if [[ $wline =~ [\$\#] ]] - then - continue - fi - if [[ -n "${wline}" ]] - then - pline="$(escapeLine "$wline")" - fi - if [[ $pline =~ \| ]] - then - continue - fi - elif [[ ! $line =~ [\$\#] ]] - then - pfile="$actionfile" - wline="$line" - if [[ -n "${wline}" ]] - then - pline="$(escapeLine "$wline")" - fi - fi - [[ -n "$pfile" ]] && echo "$pline" >> "$pfile" - # debug "written line: $pline" 2 - # debug "written file: $pfile" 2 - unset pline wline pfile - done < "${file}" - - debug "... adding filterfile to actionfile ..." 1 - echo "{ +filter{${list}} }" >> ${actionfile} - echo "*" >> ${actionfile} - debug "... adding whitelist to actionfile ..." 1 - cat "$whitefile" >> "$actionfile" - debug "... adding image handler to actionfile ..." 1 - cat "$whiteimgfile" >> "$actionfile" - debug "... created actionfile for ${list}." 1 - - debug "... done." 0 - -# domains=$(sed ${sedoptions} '/^#/d;/#/!d;s/,~/,\*/g;s/~/;:\*/g;s/^\([a-zA-Z]\)/;:\1/g' ${file}) -# [ -n "${domains}" ] && debug "... creating domainbased filterfiles ..." 1 -# debug "Found Domains: ${domains}." 2 -# ifs=$IFS -# IFS=";:" -# for domain in ${domains} -# do -# dns=$(echo ${domain} | awk -F ',' '{print $1}' | awk -F '#' '{print $1}') -# debug "Modifying line: ${domain}" 2 -# debug " ... creating filterfile for ${dns} ..." 1 -# # sed '' ${file} > ${file%\.*}-${dns%~}.script.filter -# debug " ... filterfile created ..." 1 -# debug " ... adding filterfile for ${dns} to actionfile ..." 1 -# # echo "{ +filter{${list}-${dns}} }" >> ${actionfile} -# # echo "${dns}" >> ${actionfile} -# debug " ... filterfile added ..." 1 -# done -# IFS=${ifs} -# debug "... all domainbased filterfiles created ..." 1 - - - # install Privoxy actionsfile - cp ${cpoptions} ${actionfile} ${CONFDIR} - if [ "$(grep $(basename ${actionfile}) ${CONFDIR}/config)" == "" ] - then - debug "\nModifying ${CONFDIR}/config ..." 0 - sed "s/^actionsfile user\.action/actionsfile $(basename ${actionfile})\nactionsfile user.action/" ${CONFDIR}/config > ${TMPDIR}/config - debug "... modification done.\n" 0 - debug "Installing new config ..." 0 - cp ${cpoptions} ${TMPDIR}/config ${CONFDIR} - debug "... installation done\n" 0 - fi - # install Privoxy filterfile - cp ${cpoptions} ${filterfile} ${CONFDIR} - if [ "$(grep $(basename ${filterfile}) ${CONFDIR}/config)" == "" ] - then - debug "\nModifying ${CONFDIR}/config ..." 0 - sed "s/^\(#*\)filterfile user\.filter/filterfile $(basename ${filterfile})\n\1filterfile user.filter/" ${CONFDIR}/config > ${TMPDIR}/config - debug "... modification done.\n" 0 - debug "Installing new config ..." 0 - cp ${cpoptions} ${TMPDIR}/config ${CONFDIR} - debug "... installation done\n" 0 - fi - - debug "... ${url} installed successfully.\n" 0 - done -} - -# create temporary directory and lock file -mkdir -p ${TMPDIR} -touch ${TMPDIR}/${TMPNAME}.lock - -# set command to be run on exit -[ ${DBG} -le 2 ] && trap "rm -fr ${TMPDIR};exit" INT TERM EXIT - -# loop for options -while getopts ":hrqv:" opt -do - case "${opt}" in - "h") - usage - exit 0 - ;; - "v") - DBG="${OPTARG}" - ;; - "q") - DBG=-1 - ;; - "r") - echo "Do you really want to remove all build lists?(y/N)" - read choice - [ "${choice}" != "y" ] && exit 0 - rm -rf ${CONFDIR}/*.script.{action,filter} && \ - sed '/^actionsfile .*\.script\.action$/d;/^filterfile .*\.script\.filter$/d' -i ${CONFDIR}/config && \ - echo "Lists removed." && exit 0 - echo -e "An error occured while removing the lists.\nPlease have a look into ${CONFDIR} whether there are .script.* files and search for *.script.* in ${CONFDIR}/config." - exit 1 - ;; - ":") - echo "${TMPNAME}: -${OPTARG} requires an argument" >&2 - exit 1 - ;; - esac -done - -debug "URL-List: ${URLS}\nPrivoxy-Configdir: ${CONFDIR}\nTemporary directory: ${TMPDIR}" 2 -main - -# restore default exit command -trap - INT TERM EXIT -[ ${DBG} -lt 2 ] && rm -r ${TMPDIR} -[ ${DBG} -eq 2 ] && rm -vr ${TMPDIR} -exit 0