mirror of
https://github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker.git
synced 2025-09-02 18:50:13 +00:00
install-ngxblocker: use getopts not getopt
long options for the command line are removed. getopt is replaced with the newer getopts (which is compatible with Debian's DASH Shell) * fixes https://github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/issues/35 * adds sanity checks for user command line switches * adds -v command line option to print blacklist version
This commit is contained in:
parent
6147522b7e
commit
f67776d8a9
1 changed files with 72 additions and 27 deletions
|
@ -41,20 +41,34 @@ REPO=https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blo
|
||||||
usage() {
|
usage() {
|
||||||
local script=$(basename $0)
|
local script=$(basename $0)
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
$script: download Nginx Bad Bot Blocker configuration to: [ $CONF_DIR ] [ $BOTS_DIR ]
|
$script: INSTALL Nginx Bad Bot Blocker configuration to: [ $CONF_DIR ] [ $BOTS_DIR ]
|
||||||
|
|
||||||
Usage: $script [OPTIONS]
|
Usage: $script [OPTIONS]
|
||||||
[ -b | --bots ] : Bot rules directory (default: $BOTS_DIR)
|
[ -b ] : Bot rules directory (default: $BOTS_DIR)
|
||||||
[ -c | --conf ] : NGINX conf directory (default: $CONF_DIR)
|
[ -c ] : NGINX conf directory (default: $CONF_DIR)
|
||||||
[ -r | --repo ] : Change repo url (default: $REPO)
|
[ -r ] : Change repo url (default: $REPO)
|
||||||
[ -x | --exec ] : Actually change the files (default: don't change anything)
|
[ -x ] : Actually change the files (default: don't change anything)
|
||||||
[ -h | --help ] : this help message
|
[ -v ] : Print blacklist version
|
||||||
|
[ -h ] : this help message
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
$script (Don't change anything: display results on stdout)
|
$script (Don't change anything: display results on stdout)
|
||||||
$script -x (Download / update config files)
|
$script -x (Download / update config files)
|
||||||
EOF
|
EOF
|
||||||
return 0
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
check_version() {
|
||||||
|
local file=$CONF_DIR/globalblacklist.conf
|
||||||
|
|
||||||
|
if [ -f $file ]; then
|
||||||
|
grep Version $file
|
||||||
|
grep 'Updated:' $file
|
||||||
|
else
|
||||||
|
printf "Missing '$file' (pass -c \$path before -v)\n"
|
||||||
|
fi
|
||||||
|
|
||||||
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
longest_str() {
|
longest_str() {
|
||||||
|
@ -127,25 +141,56 @@ check_config() {
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
get_options() {
|
sanitize_path() {
|
||||||
local options=$(getopt -o b:c:r:hx --long \
|
echo $1 |tr -cd '[:alnum:] [=@=] [=.=] [=-=] [=/=] [=_=]' \
|
||||||
bots:,conf:,repo:,help,exec -- "$@" 2>/dev/null)
|
|tr -s '@.-/_' |awk '{print tolower($0)}'
|
||||||
|
}
|
||||||
|
|
||||||
if [ $? -ne 0 ]; then
|
sanitize_url() {
|
||||||
usage
|
echo $1 |tr -cd '[:alnum:] [=:=] [=.=] [=-=] [=/=]' \
|
||||||
|
|tr -s ':.-' |awk '{print tolower($0)}'
|
||||||
|
}
|
||||||
|
|
||||||
|
check_args() {
|
||||||
|
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
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
;;
|
||||||
|
url) if ! echo $arg | grep -E ^http[s]?://[0-9a-zA-Z-]+[.]+[/0-9a-zA-Z.]+ 1>/dev/null; then
|
||||||
|
printf "$msg url => http[s]://the.url\n"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
none) printf "$msg argument.\n"; exit 1;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
eval set -- "$options"
|
get_options() {
|
||||||
|
local arg= opts=
|
||||||
|
|
||||||
while :; do
|
while getopts :b:c:r:xvh opts "$@"
|
||||||
case "$1" in
|
do
|
||||||
-h | --help) usage && exit 1;;
|
if [ -n "${OPTARG}" ]; then
|
||||||
-x | --exec) DRY_RUN=N; shift;;
|
case "$opts" in
|
||||||
-b | --bots) BOTS_DIR=$2; shift 2;;
|
r) arg=$(sanitize_url ${OPTARG});;
|
||||||
-c | --conf) CONF_DIR=$2; shift 2;;
|
*) arg=$(sanitize_path ${OPTARG});;
|
||||||
-r | --repo) REPO=$2; shift 2;;
|
esac
|
||||||
*) break;;
|
fi
|
||||||
|
|
||||||
|
case "$opts" in
|
||||||
|
b) BOTS_DIR=$arg; check_args $opts path $arg ;;
|
||||||
|
c) CONF_DIR=$arg; check_args $opts path $arg ;;
|
||||||
|
r) REPO=$arg; check_args $opts url $arg ;;
|
||||||
|
x) DRY_RUN=N ;;
|
||||||
|
v) check_version ;;
|
||||||
|
h) usage ;;
|
||||||
|
\?) usage ;;
|
||||||
|
:) check_args $OPTARG none none ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue