11#! /bin/bash
22# info: add firewall ipset
3- # options: NAME [SOURCE] [IPVERSION] [AUTOUPDATE] [FORCE ]
3+ # options: NAME [SOURCE] [IPVERSION] [AUTOUPDATE] [REFRESH ]
44#
55# example: v-add-firewall-ipset country-nl 'http://ipverse.net/ipblocks/data/countries/nl.zone'
66#
@@ -14,7 +14,7 @@ ip_name=${1}
1414data_source=${2}
1515ip_version=${3:- v4}
1616autoupdate=${4:- yes}
17- force =${5:- no}
17+ refresh =${5:- no}
1818
1919# Includes
2020# shellcheck source=/etc/hestiacp/hestia.conf
@@ -30,16 +30,16 @@ source_conf "$HESTIA/conf/hestia.conf"
3030
3131check_args ' 1' " $# " ' NAME [SOURCE] [IPVERSION] [AUTOUPDATE] [FORCE]'
3232is_format_valid ' ip_name'
33- is_boolean_format_valid " $autoupdate " ' bool (yes/no)'
34- is_boolean_format_valid " $force " ' bool (yes/no)'
33+ is_boolean_format_valid " $autoupdate " ' Automatically update IP list (yes/no)'
34+ is_boolean_format_valid " $refresh " ' Refresh IP list (yes/no)'
3535is_system_enabled " $FIREWALL_SYSTEM " ' FIREWALL_SYSTEM'
3636
3737# Perform verification if read-only mode is enabled
3838check_hestia_demo_mode
3939
4040# Define variables for ipset configuration
4141ipset_hstobject=' ../../data/firewall/ipset'
42- IPSET_BIN=" $( which ipset) "
42+ IPSET_BIN=" $( command -v ipset) "
4343IPSET_PATH=" $HESTIA /data/firewall/ipset"
4444
4545# Ensure ipset is installed
@@ -49,10 +49,10 @@ if [ -z "$IPSET_BIN" ]; then
4949 else
5050 apt-get --quiet --yes install ipset > /dev/null
5151 fi
52- check_result $? " Installing ipset package"
52+ check_result $? " Installing IPset package"
5353
5454 IPSET_BIN=" $( which ipset) "
55- check_result $? " ipset binary not found"
55+ check_result $? " IPset binary not found"
5656fi
5757
5858# Ensure ipset configuration path and master file exist before attempting to parse
@@ -87,8 +87,8 @@ IPSET_MIN_SIZE=10
8787# Action #
8888# ----------------------------------------------------------#
8989
90- # Generate ip lists file if missing or when forced
91- if [ ! -f " ${IPSET_PATH} /${IPSET_FILE} .iplist" ] || [ " $force " = " yes" ]; then
90+ # Generate ip lists file if missing or required refresh
91+ if [ ! -f " ${IPSET_PATH} /${IPSET_FILE} .iplist" ] || [ " $refresh " = " yes" ]; then
9292
9393 iplist_tempfile=$( mktemp)
9494
@@ -109,7 +109,6 @@ if [ ! -f "${IPSET_PATH}/${IPSET_FILE}.iplist" ] || [ "$force" = "yes" ]; then
109109
110110 # Generate the ip list file trough a external script
111111 # ex: compiling a ip list from multiple sources on demand
112-
113112 if [ -x " ${data_source# script: } " ]; then
114113
115114 setpriv --clear-groups --reuid nobody --regid nogroup -- ${data_source# script: } " $ip_name " > " $iplist_tempfile "
@@ -121,7 +120,6 @@ if [ ! -f "${IPSET_PATH}/${IPSET_FILE}.iplist" ] || [ "$force" = "yes" ]; then
121120
122121 # Use a external ip-list file managed by other apps
123122 # ex: Using a ip list that is continously updated
124-
125123 [ -f " ${data_source# file: } " ] && cp -f " ${data_source# file: } " " $iplist_tempfile "
126124
127125 fi
@@ -136,7 +134,7 @@ if [ ! -f "${IPSET_PATH}/${IPSET_FILE}.iplist" ] || [ "$force" = "yes" ]; then
136134
137135 # Validate iplist file size
138136 iplist_size=$( sed -r -e ' /^#|^$/d' " $iplist_tempfile " | wc -l)
139- [[ " $iplist_size " -le " $IPSET_MIN_SIZE " ]] && check_result " $E_INVALID " " iplist file too small (<${IPSET_MIN_SIZE} ), ignoring"
137+ [[ " $iplist_size " -le " $IPSET_MIN_SIZE " ]] && check_result " $E_INVALID " " IP list file too small (<${IPSET_MIN_SIZE} ), ignoring"
140138 mv -f " $iplist_tempfile " " ${IPSET_PATH} /${IPSET_FILE} .iplist"
141139
142140fi
@@ -168,12 +166,14 @@ if [ ! -f "${IPSET_PATH}.conf" ] || [ -z "$(get_object_value "$ipset_hstobject"
168166 str=" $str AUTOUPDATE='$autoupdate ' SUSPENDED='no'"
169167 str=" $str TIME='$time ' DATE='$date '"
170168 echo " $str " >> $HESTIA /data/firewall/ipset.conf
169+ log_type=" added"
171170
172- elif [ " $force " = " yes" ]; then
171+ elif [ " $refresh " = " yes" ]; then
173172
174- # update iplist last regen time
173+ # Update iplist last regen time
175174 update_object_value " $ipset_hstobject " ' LISTNAME' " $ip_name " ' $TIME' " $time "
176175 update_object_value " $ipset_hstobject " ' LISTNAME' " $ip_name " ' $DATE' " $date "
176+ log_type=" refreshed"
177177
178178fi
179179
192192# ----------------------------------------------------------#
193193
194194# Logging
195- $BIN /v-log-action " system" " Info" " Firewall" " Added new IP list (Name: $ip_name , IP version: $ip_version , Autoupdate: $autoupdate )."
195+ $BIN /v-log-action " system" " Info" " Firewall" " IPset IP list ${log_type :- loaded} (Name: $ip_name , IP version: $ip_version , Autoupdate: $autoupdate )."
196196log_event " $OK " " $ARGUMENTS "
197197
198198exit
0 commit comments