setup-ngxblocker: fix sed error / refactor $col_size

fixes https://github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/pull/47
This commit is contained in:
Stuart Cardall 2017-05-03 17:34:32 +00:00
parent df2d7f0b43
commit 3e4f1abc7d
No known key found for this signature in database
GPG key ID: AEB857F1C891D0C6

View file

@ -66,7 +66,7 @@ find_vhosts() {
} }
whitelist_ips() { whitelist_ips() {
local ip= conf=$BOTS_DIR/whitelist-ips.conf local ip= conf=$BOTS_DIR/whitelist-ips.conf col_size=$1
mkdir -p $BOTS_DIR mkdir -p $BOTS_DIR
@ -79,7 +79,7 @@ whitelist_ips() {
fi fi
fi fi
else else
printf "%-10s %-$(( $col_size -14 ))s %-s\n" \ printf "%-10s %-${col_size}s %-s\n" \
"WARN:" "dig binary missing" "=> install bind-tools to whitelist external ip address" "WARN:" "dig binary missing" "=> install bind-tools to whitelist external ip address"
fi fi
} }
@ -117,15 +117,14 @@ check_wildcard() {
} }
add_includes() { add_includes() {
local ph='<<!!>>' line=$1 file=$2 conf_dir=$3 text= update= local ph='<<!!>>' line=$1 file=$2 conf_dir=$3 col_size=$4 text= update=
local include_list="$(echo $@ | awk '{$1=$2=$3=""}sub("^"OFS"+","")')" local include_list="$(echo $@ | awk '{$1=$2=$3=$4=""}sub("^"OFS"+","")')"
local col_size=$(( $(longest_str $include_list) + $(echo $conf_dir | wc -m) ))
for text in $include_list; do for text in $include_list; do
if ! grep "$text" $file 1>/dev/null; then if ! grep "$text" $file 1>/dev/null; then
update='true' update='true'
text="include $conf_dir/$text;" text="include $conf_dir/$text;"
printf "%-10s %-$(( $col_size +10 ))s %s\n" "inserting:" "$text" "=> $file" printf "%-10s %-${col_size}s %s\n" "inserting:" "$text" "=> $file"
if [ "$DRY_RUN" = "N" ]; then if [ "$DRY_RUN" = "N" ]; then
# $ph is just a placeholder so sed inserts a \t (tab) # $ph is just a placeholder so sed inserts a \t (tab)
sed -i "$line i $ph \t$text $ph" $file sed -i "$line i $ph \t$text $ph" $file
@ -168,9 +167,11 @@ find_includes() {
local start=$(find_line $file $start_range $start_first_last) local start=$(find_line $file $start_range $start_first_last)
local end=$(find_line $file $end_range $end_first_last) local end=$(find_line $file $end_range $end_first_last)
if [ -n "$start" ] && [ -n "$end" ]; then
sed -n "$start,$end"p $file > $tmp sed -n "$start,$end"p $file > $tmp
line=$(find_line $tmp $search $search_first_last) line=$(find_line $tmp $search $search_first_last)
rm -f $tmp rm -f $tmp
fi
# search string not found # search string not found
if [ -z "$line" ]; then if [ -z "$line" ]; then
@ -306,7 +307,7 @@ main() {
# gather vhosts # gather vhosts
file_list=$(find_vhosts) file_list=$(find_vhosts)
check_config $file_list check_config $file_list
col_size=$(( $(longest_str $file_list) + $(echo $BOTS_DIR | wc -m) )) col_size=$(( $(longest_str $CONF_FILES) + $(echo $CONF_DIR | wc -m) + 10 ))
# by default do not change any files # by default do not change any files
if [ -z "$DRY_RUN" ]; then if [ -z "$DRY_RUN" ]; then
@ -315,25 +316,26 @@ main() {
printf "\n" printf "\n"
fi fi
# update main config
line=$(find_includes $MAIN_CONF include last http first '\}' last )
if [ -n "$(check_wildcard $MAIN_CONF $CONF_DIR)" ]; then # also recalculate column width
col_size=$(( $(longest_str $VHOST_INCLUDES) + $(echo $BOTS_DIR | wc -m) + 10 ))
printf "%-10s %-${col_size}s %s\n" "INFO:" "$CONF_DIR/* detected" "=> $MAIN_CONF"
else # wildcard conf.d ok in nginx.conf
add_includes $line $MAIN_CONF $CONF_DIR $col_size $CONF_FILES
fi
# update vhosts # update vhosts
for file in $file_list; do for file in $file_list; do
line=$(find_includes $file include last server_ last location first ) line=$(find_includes $file include last server_ last location first )
if [ -n "$(check_wildcard $file $BOTS_DIR)" ]; then if [ -n "$(check_wildcard $file $BOTS_DIR)" ]; then
printf "%-10s %-$(( $col_size -14 ))s %s\n" "WARN:" "$BOTS_DIR/* detected" "=> $file" printf "%-10s %-${col_size}s %s\n" "WARN:" "$BOTS_DIR/* detected" "=> $file"
else # don't use wildcards in vhost files else # don't use wildcards in vhost files
add_includes $line $file $BOTS_DIR $VHOST_INCLUDES add_includes $line $file $BOTS_DIR $col_size $VHOST_INCLUDES
fi fi
done done
# update main config whitelist_ips $col_size
line=$(find_includes $MAIN_CONF include last http first '\}' last )
if [ -n "$(check_wildcard $MAIN_CONF $CONF_DIR)" ]; then
printf "%-10s %-$(( $col_size -14 ))s %s\n" "INFO:" "$CONF_DIR/* detected" "=> $MAIN_CONF"
else # wildcard conf.d ok in nginx.conf
add_includes $line $MAIN_CONF $CONF_DIR $CONF_FILES
fi
whitelist_ips
if [ -d $WWW ]; then if [ -d $WWW ]; then
whitelist_domains whitelist_domains
@ -345,3 +347,4 @@ main() {
## START ## ## START ##
main $@ main $@
exit $? exit $?