Skip to content

Commit e45f733

Browse files
Merge pull request #31 from itoffshore/alpine
install-ngxblocker: 2 small fixes / setup-ngxblocker: update to new file structure
2 parents f6a4a17 + 6fb21eb commit e45f733

File tree

2 files changed

+72
-13
lines changed

2 files changed

+72
-13
lines changed

install-ngxblocker

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ wget_opts() {
166166
check_online() {
167167
local url=$1 options=$(wget_opts)
168168

169-
if wget $options $include_url &>/dev/null; then
169+
if wget $options $url &>/dev/null; then
170170
echo "true"
171171
fi
172172
}
@@ -190,6 +190,7 @@ main() {
190190
local tmp=$(mktemp)
191191
wget -q $include_url -O $tmp
192192
source $tmp 2>/dev/null
193+
rm -f $tmp
193194
else
194195
printf "Repo down or missing: $include_url\n"
195196
exit 1

setup-ngxblocker

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ INC_DDOS="Y"
2222
####### end user configuration ###########################
2323

2424
usage() {
25-
local script=$(basename $0)
26-
cat <<EOF
25+
local script=$(basename $0)
26+
cat <<EOF
2727
$script: add Nginx Bad Bot Blocker configuration [ in $MAIN_CONF ] [ $VHOST_DIR/* ]
2828
2929
Usage: $script [OPTIONS]
@@ -44,7 +44,7 @@ Examples:
4444
$script (Don't change anything: display results on stdout)
4545
$script -x (Change / update config files)
4646
EOF
47-
return 0
47+
return 0
4848
}
4949

5050
check_config() {
@@ -54,6 +54,11 @@ check_config() {
5454
echo "no vhost files in: $VHOST_DIR/*.$VHOST_EXT => exiting."
5555
exit 1
5656
fi
57+
58+
if [ ! -f "$MAIN_CONF" ]; then
59+
echo "NGINX main configuration ('$MAIN_CONF') not found => exiting."
60+
exit 1
61+
fi
5762
}
5863

5964
find_vhosts() {
@@ -171,7 +176,7 @@ find_includes() {
171176
esac
172177

173178
# if inserting beyond the end of the file
174-
if [ $line -gt $(wc -l < $file) ]; then
179+
if [ $line -gt $end ]; then
175180
# insert blank line
176181
sed -i "$end i \ " $file
177182
fi
@@ -207,40 +212,93 @@ get_options() {
207212
done
208213
}
209214

215+
wget_opts() {
216+
local opts=
217+
218+
# GNU wget / Busybox 1.26.2
219+
if wget --help 2>&1 | grep -q "\--spider"; then
220+
opts="--spider"
221+
else # Busybox wget < 1.26.2
222+
opts="-s"
223+
fi
224+
225+
echo $opts
226+
}
227+
228+
check_online() {
229+
local url=$1 options=$(wget_opts)
230+
231+
if wget $options $url &>/dev/null; then
232+
echo "true"
233+
fi
234+
}
235+
210236
main() {
211-
local file= line= vhost_includes= main_includes= file_list=
212-
main_includes="botblocker-nginx-settings.conf globalblacklist.conf"
213-
vhost_includes="blockbots.conf"
237+
local include_url= file= line= file_list=
238+
local CONF_FILES= BOT_FILES=
239+
local REPO=https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/master
240+
241+
# require root
242+
if [ "$(id -u)" != "0" ]; then
243+
echo "This script must be run as root" 1>&2
244+
exit 1
245+
fi
214246

215247
# parse command line
216248
get_options $@
249+
include_url=$REPO/include_filelist.txt
250+
251+
# check repo is online & source includes
252+
printf "Checking url: $include_url\n"
253+
if [ -n "$(check_online $include_url)" ]; then
254+
local tmp=$(mktemp)
255+
wget -q $include_url -O $tmp
256+
source $tmp 2>/dev/null
257+
rm -f $tmp
258+
else
259+
printf "Repo down or missing: $include_url\n"
260+
exit 1
261+
fi
262+
263+
# double check we have some files sourced
264+
if [ -z "$CONF_FILES" ] || [ -z "$BOT_FILES" ]; then
265+
printf "Error sourcing variables from: $include_url"
266+
exit 1
267+
fi
217268

269+
# configure ddos include
218270
case "$INC_DDOS" in
219-
y*|Y*) vhost_includes="$vhost_includes ddos.conf"
271+
n*|N*) BOT_FILES=$(echo $BOT_FILES | sed 's|ddos.conf||');;
220272
esac
221273

274+
# gather vhosts
222275
file_list=$(find_vhosts)
223276
check_config $file_list
224277

278+
# by default do not change any files
225279
if [ -z "$DRY_RUN" ]; then
226280
printf "\n** Dry Run ** | not updating files | -x or --exec to change files\n\n"
227281
fi
228282

229283
# update vhosts
230284
for file in $file_list; do
231285
line=$(find_includes $file include last server_ last location first )
232-
add_includes $line $file $BOTS_DIR $vhost_includes
286+
add_includes $line $file $BOTS_DIR $BOT_FILES
233287
done
234288

235289
# update main config
236290
line=$(find_includes $MAIN_CONF include last http first '\}' last )
237-
add_includes $line $MAIN_CONF $CONF_DIR botblocker-nginx-settings.conf globalblacklist.conf
291+
add_includes $line $MAIN_CONF $CONF_DIR $CONF_FILES
238292

239293
whitelist_ips
240-
whitelist_domains
294+
295+
if [ -d $WWW ]; then
296+
whitelist_domains
297+
else
298+
echo "Web directory not found ('$WWW'): not whitelisting domains"
299+
fi
241300
}
242301

243302
## START ##
244303
main $@
245304
exit $?
246-

0 commit comments

Comments
 (0)