diff --git a/setup-ngxblocker b/setup-ngxblocker index 46f7e1b34..9aa63fff2 100755 --- a/setup-ngxblocker +++ b/setup-ngxblocker @@ -12,6 +12,7 @@ VHOST_DIR=/etc/nginx/sites-available BOTS_DIR=/etc/nginx/bots.d CONF_DIR=/etc/nginx/conf.d MAIN_CONF=/etc/nginx/nginx.conf +INSTALLER=/usr/sbin/install-ngxblocker # setting Y / yes will whitelist only directories in $www # that look like domain.names DOT_NAMES="Y" @@ -21,22 +22,30 @@ INC_DDOS="Y" ####### end user configuration ########################### +BOLDGREEN="\033[1m\033[32m" +BOLDMAGENTA="\033[1m\033[35m" +BOLDRED="\033[1m\033[31m" +BOLDYELLOW="\033[1m\033[33m" +BOLDWHITE="\033[1m\033[37m" +RESET="\033[0m" + usage() { local script=$(basename $0) cat </dev/null; then + if [ -d $BOTS_DIR ]; then + printf "${BOLDGREEN}Updating bots.d path${RESET}: ${BOLDWHITE}$BOTS_DIR => $blacklist${RESET}\n" + include_paths=$(grep -E "include /.*.conf;$" $blacklist | awk '{print $2}' | tr -d ';') + + for x in $include_paths; do + dir=$(dirname $x) + sed -i "s|$dir|$BOTS_DIR|" $blacklist + done + else + printf "${BOLDRED}ERROR${RESET}: '$BOTS_DIR' does not exist => ${BOLDWHITE}running $INSTALLER${RESET}.\n" + $INSTALL_INC + update_paths $blacklist + fi + fi +} + check_config() { local files="$*" @@ -193,55 +224,63 @@ find_includes() { } sanitize_path() { - echo $1 |tr -cd '[:alnum:] [=@=] [=.=] [=-=] [=/=] [=_=]' \ - |tr -s '@.-/_' |awk '{print tolower($0)}' + echo $1 |tr -cd '[:alnum:] [=@=] [=.=] [=-=] [=/=] [=_=]' \ + |tr -s '@.-/_' |awk '{print tolower($0)}' } sanitize_ext() { - echo $1 |tr -cd '[:alnum:]' |awk '{print tolower($0)}' + echo $1 |tr -cd '[:alnum:]' |awk '{print tolower($0)}' } check_args() { - local option=$1 type=$2 arg=$3 - local msg="ERROR: option '-$option' argument '$arg' requires:" + local option=$1 type=$2 arg=$3 + local msg="ERROR: option '-$option' argument '$arg' requires:" - case "$type" in - path) if ! echo $arg | grep ^/ 1>/dev/null; then - printf "$msg absolute path.\n" - exit 1 - fi - ;; - none) printf "$msg argument.\n"; exit 1;; - esac + case "$type" in + path) if ! echo $arg | grep ^/ 1>/dev/null; then + printf "$msg absolute path.\n" + exit 1 + fi + ;; + script) if [ ! -x $arg ]; then + printf "$msg '$arg' is not executable / does not exist.\n" + exit 1 + fi + ;; + none) printf "$msg argument.\n"; exit 1;; + esac } get_options() { - local arg= opts= + local arg= opts= - while getopts :w:e:v:b:c:m:ndxh opts "$@" - do - if [ -n "${OPTARG}" ]; then - case "$opts" in - e) arg=$(sanitize_ext ${OPTARG});; - *) arg=$(sanitize_path ${OPTARG});; - esac - fi + while getopts :w:e:v:b:c:m:ndxh opts "$@" + do + if [ -n "${OPTARG}" ]; then + case "$opts" in + e) arg=$(sanitize_ext ${OPTARG});; + *) arg=$(sanitize_path ${OPTARG});; + esac + fi - case "$opts" in - w) WWW=$arg; check_args $opts path $arg ;; - e) VHOST_EXT=$arg;; - v) VHOST_DIR=$arg; check_args $opts path $arg ;; - b) BOTS_DIR=$arg; check_args $opts path $arg ;; - c) CONF_DIR=$arg; check_args $opts path $arg ;; - m) MAIN_CONF=$arg; check_args $opts path $arg ;; - n) DOT_NAMES=N ;; - d) INC_DDOS=N ;; - x) DRY_RUN=N ;; - h) usage ;; - \?) usage ;; - :) check_args $OPTARG none none ;; - esac - done + case "$opts" in + w) WWW=$arg; check_args $opts path $arg ;; + e) VHOST_EXT=$arg;; + v) VHOST_DIR=$arg; check_args $opts path $arg ;; + b) BOTS_DIR=$arg; check_args $opts path $arg ;; + c) CONF_DIR=$arg; check_args $opts path $arg ;; + m) MAIN_CONF=$arg; check_args $opts path $arg ;; + i) INSTALLER=$arg; check_args $opts script $arg ;; + n) DOT_NAMES=N ;; + d) INC_DDOS=N ;; + x) DRY_RUN=N ;; + h) usage ;; + \?) usage ;; + :) check_args $OPTARG none none ;; + esac + done + + INSTALL_INC="$INSTALLER -b $BOTS_DIR -c $CONF_DIR -x" } wget_opts() { @@ -266,7 +305,7 @@ check_online() { } main() { - local include_url= file= line= file_list= col_size= + local include_url= file= line= file_list= col_size= blacklist= local CONF_FILES= VHOST_INCLUDES= local REPO=https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/master @@ -342,9 +381,15 @@ main() { else printf "\nWeb directory not found ('$WWW'): not whitelisting domains.\n" fi + + # download new bots.d / conf.d files + printf "\nChecking for missing includes:\n\n" + $INSTALL_INC + blacklist=$(find $CONF_DIR -type f -name globalblacklist.conf) + # set custom bots.d path + update_paths $blacklist } ## START ## main $@ exit $? - diff --git a/update-ngxblocker b/update-ngxblocker index 4cbe6b3da..f786d382b 100755 --- a/update-ngxblocker +++ b/update-ngxblocker @@ -236,7 +236,7 @@ get_options() { case "$opts" in c) CONF_DIR=$arg; check_args $opts path $arg ;; b) BOTS_DIR=$arg; check_args $opts path $arg ;; - u) INSTALLER=$arg; check_args $opts script $arg ;; + i) INSTALLER=$arg; check_args $opts script $arg ;; r) REPO=$arg; check_args $opts url $arg ;; e) EMAIL=$arg; check_args $opts email $arg ;; n) SEND_EMAIL=N ;; @@ -252,7 +252,7 @@ get_options() { main() { local REPO=https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/master - local file=globalblacklist.conf remote_dir=conf.d url= output= update= status= tmp= + local file=globalblacklist.conf remote_dir=conf.d url= output= update= status= tmp= retval= # default to service (centos does not have 'which' by default) local service=${service_cmd:-"service"} @@ -281,8 +281,9 @@ main() { mkdir -p $CONF_DIR printf "${BOLDWHITE}Downloading: $file " curl --fail --connect-timeout 60 --retry 10 --retry-delay 5 -so $tmp $url + retval=$? - case "$?" in + case "$retval" in 0) printf "...${BOLDGREEN}OK${RESET}\n\n" mv $tmp $output ;; @@ -293,11 +294,11 @@ main() { # download new bots.d / conf.d files $INSTALL_INC - # re-read nginx configuration - if ! grep "Not Found" $EMAIL_REPORT; then + # set custom bots.d path + update_paths $output - # set custom bots.d path - update_paths $output + # re-read nginx configuration + if [ $retval = 0 ]; then $service nginx reload if [ $? = 0 ]; then @@ -310,7 +311,7 @@ main() { printf "\n${BOLDRED}Download failed${RESET}: not reloading NGINX config\n" fi else - # set custom bots.d path + # set custom bots.d path update_paths $output fi