1 # banIP shared function library/include - ban incoming and outgoing IPs via named nftables Sets
2 # Copyright (c) 2018-2023 Dirk Brenken (dev@brenken.org)
3 # This is free software, licensed under the GNU General Public License v3.
5 # (s)hellcheck exceptions
6 # shellcheck disable=all
11 export PATH
="/usr/sbin:/usr/bin:/sbin:/bin"
14 ban_backupdir
="/tmp/banIP-backup"
15 ban_reportdir
="/tmp/banIP-report"
16 ban_feedfile
="/etc/banip/banip.feeds"
17 ban_customfeedfile
="/etc/banip/banip.custom.feeds"
18 ban_allowlist
="/etc/banip/banip.allowlist"
19 ban_blocklist
="/etc/banip/banip.blocklist"
20 ban_mailtemplate
="/etc/banip/banip.tpl"
21 ban_pidfile
="/var/run/banip.pid"
22 ban_rtfile
="/var/run/banip_runtime.json"
23 ban_rdapfile
="/var/run/banip_rdap.json"
24 ban_rdapurl
="https://rdap.db.ripe.net/ip/"
25 ban_lock
="/var/run/banip.lock"
26 ban_logreadcmd
="$(command -v logread)"
27 ban_logcmd
="$(command -v logger)"
28 ban_ubuscmd
="$(command -v ubus)"
29 ban_nftcmd
="$(command -v nft)"
30 ban_fw4cmd
="$(command -v fw4)"
31 ban_awkcmd
="$(command -v awk)"
32 ban_grepcmd
="$(command -v grep)"
33 ban_sedcmd
="$(command -v sed)"
34 ban_catcmd
="$(command -v cat)"
35 ban_zcatcmd
="$(command -v zcat)"
36 ban_lookupcmd
="$(command -v nslookup)"
37 ban_jsoncmd
="$(command -v jsonfilter)"
38 ban_mailcmd
="$(command -v msmtp)"
39 ban_mailsender
="no-reply@banIP"
41 ban_mailtopic
="banIP notification"
42 ban_mailprofile
="ban_notify"
43 ban_mailnotification
="0"
44 ban_reportelements
="1"
45 ban_nftloglevel
="warn"
46 ban_nftpriority
="-200"
47 ban_nftpolicy
="memory"
60 ban_autoallowuplink
="subnet"
62 ban_autoblocksubnet
="0"
69 ban_blockforwardwan
=""
70 ban_blockforwardlan
=""
91 # gather system information
96 if [ -z "${ban_dev}" ]; then
97 ban_debug
="$(uci_get banip global ban_debug)"
98 ban_cores
="$(uci_get banip global ban_cores)"
100 ban_packages
="$(${ban_ubuscmd} -S call rpc-sys packagelist '{ "all
": true }' 2>/dev/null)"
101 ban_memory
="$("${ban_awkcmd}" '/^MemAvailable/{printf "%s
",int($2/1000)}' "/proc
/meminfo
" 2>/dev/null)"
102 ban_ver
="$(printf "%s
" "${ban_packages}" | "${ban_jsoncmd}" -ql1 -e '@.packages.banip')"
103 ban_sysver
="$("${ban_ubuscmd}" -S call system board 2>/dev/null | "${ban_jsoncmd}" -ql1 -e '@.model' -e '@.release.description' |
104 "${ban_awkcmd}" 'BEGIN{RS="";FS="\n"}{printf "%s
, %s
",$1,$2}')"
105 if [ -z "${ban_cores}" ]; then
106 cpu
="$("${ban_grepcmd}" -c '^processor' /proc/cpuinfo 2>/dev/null)"
107 core
="$("${ban_grepcmd}" -cm1 '^core id' /proc/cpuinfo 2>/dev/null)"
108 [ "${cpu}" = "0" ] && cpu
="1"
109 [ "${core}" = "0" ] && core
="1"
110 ban_cores
="$((cpu * core))"
119 if [ ! -d "${dir}" ]; then
122 f_log
"debug" "f_mkdir ::: directory: ${dir}"
131 if [ ! -f "${file}" ]; then
133 f_log
"debug" "f_mkfile ::: file: ${file}"
137 # create temporary files and directories
140 f_mkdir
"${ban_basedir}"
141 ban_tmpdir
="$(mktemp -p "${ban_basedir}" -d)"
142 ban_tmpfile
="$(mktemp -p "${ban_tmpdir}" -tu)"
144 f_log
"debug" "f_tmp ::: base_dir: ${ban_basedir:-"-"}, tmp_dir: ${ban_tmpdir:-"-"}"
152 if [ -d "${dir}" ]; then
154 f_log
"debug" "f_rmdir ::: directory: ${dir}"
163 if [ "${char}" = "1" ]; then
165 elif [ "${char}" = "0" ] ||
[ -z "${char}" ]; then
168 printf "%s" "${char}"
177 string
="${string#"${string%%[![:space:]]*}"}"
178 string
="${string%"${string##*[![:space:]]}"}"
179 printf "%s" "${string}"
187 ppid
="$("${ban_catcmd}" "${ban_pidfile}" 2>/dev/null)"
188 [ -n "${ppid}" ] && pids
="$(pgrep -P "${ppid}" 2>/dev/null)"
189 for pid
in ${pids}; do
190 kill -INT "${pid}" >/dev
/null
2>&1
199 local class
="${1}" log_msg
="${2}"
201 if [ -n "${log_msg}" ] && { [ "${class}" != "debug" ] || [ "${ban_debug}" = "1" ]; }; then
202 if [ -x "${ban_logcmd}" ]; then
203 "${ban_logcmd}" -p "${class}" -t "banIP-${ban_ver}[${$}]" "${log_msg}"
205 printf "%s
%s
%s
\n" "${class}" "banIP-${ban_ver}[${$}]" "${log_msg}"
208 if [ "${class}" = "err
" ]; then
209 "${ban_nftcmd}" delete table inet banIP >/dev/null 2>&1
210 if [ "${ban_enabled}" = "1" ]; then
212 [ "${ban_mailnotification}" = "1" ] && [ -n "${ban_mailreceiver}" ] && [ -x "${ban_mailcmd}" ] && f_mail
214 f_genstatus "disabled
"
216 f_rmdir "${ban_tmpdir}"
226 unset ban_dev ban_ifv4 ban_ifv6 ban_feed ban_allowurl ban_blockinput ban_blockforwardwan ban_blockforwardlan ban_logterm ban_country ban_asn
231 eval "${option}=\"${value}\""
238 eval "${option}=\"$(printf "%s" "${ban_dev}")${value} \""
241 eval "${option}=\"$(printf "%s" "${ban_ifv4}")${value} \""
244 eval "${option}=\"$(printf "%s" "${ban_ifv6}")${value} \""
247 eval "${option}=\"$(printf "%s" "${ban_trigger}")${value} \""
250 eval "${option}=\"$(printf "%s" "${ban_feed}")${value} \""
253 eval "${option}=\"$(printf "%s" "${ban_allowurl}")${value} \""
256 eval "${option}=\"$(printf "%s" "${ban_blockinput}")${value} \""
258 "ban_blockforwardwan
")
259 eval "${option}=\"$(printf "%s" "${ban_blockforwardwan}")${value} \""
261 "ban_blockforwardlan
")
262 eval "${option}=\"$(printf "%s" "${ban_blockforwardlan}")${value} \""
265 eval "${option}=\"$(printf "%s" "${ban_logterm}")${value}\\|
\""
268 eval "${option}=\"$(printf "%s" "${ban_country}")${value} \""
271 eval "${option}=\"$(printf "%s" "${ban_asn}")${value} \""
278 [ "${ban_action}" = "boot" ] && [ -z "${ban_trigger}" ] && sleep ${ban_triggerdelay}
281 # get nft/monitor actuals
286 if "${ban_nftcmd}" -t list
set inet banIP allowlistv4MAC
>/dev
/null
2>&1; then
291 if pgrep
-f "${ban_logreadcmd##*/}" -P "$("${ban_catcmd}" "${ban_pidfile}" 2>/dev
/null
)" >/dev/null 2>&1; then
292 monitor="$
(f_char
"1")"
294 monitor="$
(f_char
"0")"
296 printf "%s
" "nft
: ${nft}, monitor
: ${monitor}"
302 local item utils insecure update="0"
304 if { [ "${ban_fetchcmd}" = "uclient-fetch" ] && printf "%s" "${ban_packages}" | "${ban_grepcmd}" -q '"libustream-'; } ||
305 { [ "${ban_fetchcmd}" = "wget" ] && printf "%s" "${ban_packages}" | "${ban_grepcmd}" -q '"wget-ssl'; } ||
306 [ "${ban_fetchcmd}" = "curl
" ] || [ "${ban_fetchcmd}" = "aria2c
" ]; then
307 ban_fetchcmd="$
(command -v "${ban_fetchcmd}")"
312 if [ "${ban_autodetect}" = "1" ] && [ ! -x "${ban_fetchcmd}" ]; then
313 utils="aria2c curl wget uclient-fetch
"
314 for item in ${utils}; do
315 if { [ "${item}" = "uclient-fetch" ] && printf "%s" "${ban_packages}" | "${ban_grepcmd}" -q '"libustream-'; } ||
316 { [ "${item}" = "wget" ] && printf "%s" "${ban_packages}" | "${ban_grepcmd}" -q '"wget-ssl'; } ||
317 [ "${item}" = "curl
" ] || [ "${item}" = "aria2c
" ]; then
318 ban_fetchcmd="$
(command -v "${item}")"
319 if [ -x "${ban_fetchcmd}" ]; then
321 uci_set banip global ban_fetchcmd "${item}"
329 [ ! -x "${ban_fetchcmd}" ] && f_log "err
" "no download utility with SSL support
"
330 case "${ban_fetchcmd##*/}" in
332 [ "${ban_fetchinsecure}" = "1" ] && insecure="--check-certificate=false
"
333 ban_fetchparm="${ban_fetchparm:-"${insecure} --timeout=20 --retry-wait=10 --max-tries=${ban_fetchretry} --max-file-not-found=${ban_fetchretry} --allow-overwrite=true --auto-file-renaming=false --log-level=warn --dir=/ -o"}"
334 ban_rdapparm="--timeout=5 --allow-overwrite=true
--auto-file-renaming=false
--dir=/ -o"
337 [ "${ban_fetchinsecure}" = "1" ] && insecure="--insecure"
338 ban_fetchparm="${ban_fetchparm:-"${insecure} --connect-timeout 20 --retry-delay 10 --retry ${ban_fetchretry} --retry-all-errors --fail --silent --show-error --location -o"}"
339 ban_rdapparm="--connect-timeout 5 --silent --location -o"
342 [ "${ban_fetchinsecure}" = "1" ] && insecure="--no-check-certificate"
343 ban_fetchparm="${ban_fetchparm:-"${insecure} --timeout=20 -O"}"
344 ban_rdapparm="--timeout=5 -O"
347 [ "${ban_fetchinsecure}" = "1" ] && insecure="--no-check-certificate"
348 ban_fetchparm="${ban_fetchparm:-"${insecure} --no-cache --no-cookies --timeout=20 --waitretry=10 --tries=${ban_fetchretry} --retry-connrefused -O"}"
349 ban_rdapparm="--timeout=5 -O"
353 f_log "debug
" "f_getfetch
::: auto
/update
: ${ban_autodetect}/${update}, cmd: ${ban_fetchcmd:-"-"}, fetch_parm: ${ban_fetchparm:-"-"}, rdap_parm: ${ban_rdapparm:-"-"}"
359 local iface update="0"
361 if [ "${ban_autodetect}" = "1" ]; then
362 if [ -z "${ban_ifv4}" ]; then
364 network_find_wan iface
365 if [ -n "${iface}" ] && "${ban_ubuscmd}" -t 10 wait_for network.interface."${iface}" >/dev/null 2>&1; then
368 uci_set banip global ban_protov4 "1"
369 uci_add_list banip global ban_ifv4 "${iface}"
370 f_log "info
" "add IPv4 interface
'${iface}' to config
"
373 if [ -z "${ban_ifv6}" ]; then
375 network_find_wan6 iface
376 if [ -n "${iface}" ] && "${ban_ubuscmd}" -t 10 wait_for network.interface."${iface}" >/dev/null 2>&1; then
379 uci_set banip global ban_protov6 "1"
380 uci_add_list banip global ban_ifv6 "${iface}"
381 f_log "info
" "add IPv6 interface
'${iface}' to config
"
385 if [ -n "$
(uci
-q changes
"banip")" ]; then
389 ban_ifv4="${ban_ifv4%%?}"
390 ban_ifv6="${ban_ifv6%%?}"
391 for iface in ${ban_ifv4} ${ban_ifv6}; do
392 if ! "${ban_ubuscmd}" -t 10 wait_for network.interface."${iface}" >/dev/null 2>&1; then
393 f_log "err
" "no wan interface
'${iface}'"
397 [ -z "${ban_ifv4}" ] && [ -z "${ban_ifv6}" ] && f_log "err
" "no wan interfaces
"
399 f_log "debug
" "f_getif
::: auto
/update
: ${ban_autodetect}/${update}, interfaces (4/6): ${ban_ifv4}/${ban_ifv6}, protocols (4/6): ${ban_protov4}/${ban_protov6}"
405 local dev iface update="0" cnt="0" cnt_max="30"
407 if [ "${ban_autodetect}" = "1" ]; then
408 while [ "${cnt}" -lt "${cnt_max}" ] && [ -z "${ban_dev}" ]; do
410 for iface in ${ban_ifv4} ${ban_ifv6}; do
411 network_get_device dev "${iface}"
412 if [ -n "${dev}" ]; then
413 if printf "%s
" "${dev}" | "${ban_grepcmd}" -qE "pppoe|
6in4
"; then
416 if ! printf " %s
" "${ban_dev}" | "${ban_grepcmd}" -q " ${dev} "; then
417 ban_dev="${ban_dev}${dev} "
418 uci_add_list banip global ban_dev "${dev}"
419 f_log "info
" "add device
'${dev}' to config
"
427 if [ -n "$
(uci
-q changes
"banip")" ]; then
431 ban_dev="${ban_dev%%?}"
432 [ -z "${ban_dev}" ] && f_log "err
" "no wan devices
"
434 f_log "debug
" "f_getdev
::: auto
/update
: ${ban_autodetect}/${update}, devices: ${ban_dev}, cnt: ${cnt}"
440 local uplink iface ip update="0"
442 if [ "${ban_autoallowlist}" = "1" ] && [ "${ban_autoallowuplink}" != "disable
" ]; then
443 for iface in ${ban_ifv4} ${ban_ifv6}; do
445 if [ "${ban_autoallowuplink}" = "subnet
" ]; then
446 network_get_subnet uplink "${iface}"
447 elif [ "${ban_autoallowuplink}" = "ip
" ]; then
448 network_get_ipaddr uplink "${iface}"
450 if [ -n "${uplink}" ] && ! printf " %s " "${ban_uplink}" | "${ban_grepcmd}" -q " ${uplink} "; then
451 ban_uplink="${ban_uplink}${uplink} "
453 if [ "${ban_autoallowuplink}" = "subnet
" ]; then
454 network_get_subnet6 uplink "${iface}"
455 elif [ "${ban_autoallowuplink}" = "ip
" ]; then
456 network_get_ipaddr6 uplink "${iface}"
458 if [ -n "${uplink}" ] && ! printf " %s " "${ban_uplink}" | "${ban_grepcmd}" -q " ${uplink} "; then
459 ban_uplink="${ban_uplink}${uplink} "
462 for ip in ${ban_uplink}; do
463 if ! "${ban_grepcmd}" -q "${ip} " "${ban_allowlist}"; then
464 if [ "${update}" = "0" ]; then
465 "${ban_sedcmd}" -i '/# uplink added on /d' "${ban_allowlist}"
467 printf "%-42s%s
\n" "${ip}" "# uplink added on $(date "+%Y-%m-%d %H:%M:%S")" >>"${ban_allowlist}"
468 f_log
"info" "add uplink '${ip}' to local allowlist"
472 ban_uplink
="${ban_uplink%%?}"
473 elif [ "${ban_autoallowlist}" = "1" ] && [ "${ban_autoallowuplink}" = "disable" ]; then
474 "${ban_sedcmd}" -i '/# uplink added on /d' "${ban_allowlist}"
478 f_log
"debug" "f_getuplink ::: auto/update: ${ban_autoallowlist}/${update}, uplink: ${ban_uplink:-"-"}"
481 # get feed information
485 if [ -s "${ban_customfeedfile}" ]; then
486 if ! json_load_file
"${ban_customfeedfile}" >/dev
/null
2>&1; then
487 f_log
"info" "can't load banIP custom feed file"
488 if ! json_load_file
"${ban_feedfile}" >/dev
/null
2>&1; then
489 f_log
"err" "can't load banIP feed file"
492 elif ! json_load_file
"${ban_feedfile}" >/dev
/null
2>&1; then
493 f_log
"err" "can't load banIP feed file"
502 [ -s "${file}" ] && printf "%s" "elements={ $("${ban_catcmd}" "${file}" 2>/dev/null) };"
505 # build initial nft file with base table, chains and rules
508 local feed_log feed_rc
file="${1}"
511 # nft header (tables and chains)
513 printf "%s\n\n" "#!/usr/sbin/nft -f"
514 if "${ban_nftcmd}" -t list
set inet banIP allowlistv4MAC
>/dev
/null
2>&1; then
515 printf "%s\n" "delete table inet banIP"
517 printf "%s\n" "add table inet banIP"
518 printf "%s\n" "add chain inet banIP wan-input { type filter hook input priority ${ban_nftpriority}; policy accept; }"
519 printf "%s\n" "add chain inet banIP wan-forward { type filter hook forward priority ${ban_nftpriority}; policy accept; }"
520 printf "%s\n" "add chain inet banIP lan-forward { type filter hook forward priority ${ban_nftpriority}; policy accept; }"
522 # default wan-input rules
524 printf "%s\n" "add rule inet banIP wan-input ct state established,related counter accept"
525 printf "%s\n" "add rule inet banIP wan-input iifname != { ${ban_dev// /, } } counter accept"
526 printf "%s\n" "add rule inet banIP wan-input meta nfproto ipv4 udp sport 67-68 udp dport 67-68 counter accept"
527 printf "%s\n" "add rule inet banIP wan-input meta nfproto ipv6 udp sport 547 udp dport 546 counter accept"
528 printf "%s\n" "add rule inet banIP wan-input meta nfproto ipv4 icmp type { echo-request } limit rate 1000/second counter accept"
529 printf "%s\n" "add rule inet banIP wan-input meta nfproto ipv6 icmpv6 type { echo-request } limit rate 1000/second counter accept"
530 printf "%s\n" "add rule inet banIP wan-input meta nfproto ipv6 icmpv6 type { nd-neighbor-advert, nd-neighbor-solicit, nd-router-advert} limit rate 1000/second ip6 hoplimit 1 counter accept"
531 printf "%s\n" "add rule inet banIP wan-input meta nfproto ipv6 icmpv6 type { nd-neighbor-advert, nd-neighbor-solicit, nd-router-advert} limit rate 1000/second ip6 hoplimit 255 counter accept"
533 # default wan-forward rules
535 printf "%s\n" "add rule inet banIP wan-forward ct state established,related counter accept"
536 printf "%s\n" "add rule inet banIP wan-forward iifname != { ${ban_dev// /, } } counter accept"
538 # default lan-forward rules
540 printf "%s\n" "add rule inet banIP lan-forward ct state established,related counter accept"
541 printf "%s\n" "add rule inet banIP lan-forward oifname != { ${ban_dev// /, } } counter accept"
544 # load initial banIP table within nft (atomic load)
546 feed_log
="$("${ban_nftcmd}" -f "${file}" 2>&1)"
549 f_log
"debug" "f_nftinit ::: devices: ${ban_dev}, priority: ${ban_nftpriority}, policy: ${ban_nftpolicy}, loglevel: ${ban_nftloglevel}, rc: ${feed_rc:-"-"}, log: ${feed_log:-"-"}"
556 local log_input log_forwardwan log_forwardlan start_ts end_ts tmp_raw tmp_load tmp_file split_file ruleset_raw handle
557 local cnt_set cnt_dl restore_rc feed_direction feed_rc feed_log feed
="${1}" proto="${2}" feed_url="${3}" feed_rule="${4}" feed_flag="${5}"
559 start_ts
="$(date +%s)"
560 feed
="${feed}v${proto}"
561 tmp_load
="${ban_tmpfile}.${feed}.load"
562 tmp_raw
="${ban_tmpfile}.${feed}.raw"
563 tmp_split
="${ban_tmpfile}.${feed}.split"
564 tmp_file
="${ban_tmpfile}.${feed}.file"
565 tmp_flush
="${ban_tmpfile}.${feed}.flush"
566 tmp_nft
="${ban_tmpfile}.${feed}.nft"
567 tmp_allow
="${ban_tmpfile}.${feed%v*}"
569 [ "${ban_loginput}" = "1" ] && log_input="log level ${ban_nftloglevel} prefix \"banIP/inp-wan/drp/${feed}: \""
570 [ "${ban_logforwardwan}" = "1" ] && log_forwardwan="log level ${ban_nftloglevel} prefix \"banIP/fwd-wan/drp/${feed}: \""
571 [ "${ban_logforwardlan}" = "1" ] && log_forwardlan="log level ${ban_nftloglevel} prefix \"banIP/fwd-lan/rej/${feed}: \""
573 # set feed block direction
575 if [ "${ban_blockpolicy}" = "input" ]; then
576 if ! printf "%s" "${ban_blockinput}" | "${ban_grepcmd}" -q "${feed%v*}" &&
577 ! printf "%s" "${ban_blockforwardwan}" | "${ban_grepcmd}" -q "${feed%v*}" &&
578 ! printf "%s" "${ban_blockforwardlan}" | "${ban_grepcmd}" -q "${feed%v*}"; then
579 ban_blockinput
="${ban_blockinput} ${feed%v*}"
581 elif [ "${ban_blockpolicy}" = "forwardwan" ]; then
582 if ! printf "%s" "${ban_blockinput}" | "${ban_grepcmd}" -q "${feed%v*}" &&
583 ! printf "%s" "${ban_blockforwardwan}" | "${ban_grepcmd}" -q "${feed%v*}" &&
584 ! printf "%s" "${ban_blockforwardlan}" | "${ban_grepcmd}" -q "${feed%v*}"; then
585 ban_blockforwardwan
="${ban_blockforwardwan} ${feed%v*}"
587 elif [ "${ban_blockpolicy}" = "forwardlan" ]; then
588 if ! printf "%s" "${ban_blockinput}" | "${ban_grepcmd}" -q "${feed%v*}" &&
589 ! printf "%s" "${ban_blockforwardwan}" | "${ban_grepcmd}" -q "${feed%v*}" &&
590 ! printf "%s" "${ban_blockforwardlan}" | "${ban_grepcmd}" -q "${feed%v*}"; then
591 ban_blockforwardlan
="${ban_blockforwardlan} ${feed%v*}"
594 if printf "%s" "${ban_blockinput}" | "${ban_grepcmd}" -q "${feed%v*}"; then
595 feed_direction
="input"
597 if printf "%s" "${ban_blockforwardwan}" | "${ban_grepcmd}" -q "${feed%v*}"; then
598 feed_direction
="${feed_direction} forwardwan"
600 if printf "%s" "${ban_blockforwardlan}" | "${ban_grepcmd}" -q "${feed%v*}"; then
601 feed_direction
="${feed_direction} forwardlan"
604 # chain/rule maintenance
606 if [ "${ban_action}" = "reload" ] && "${ban_nftcmd}" -t list set inet banIP "${feed}" >/dev
/null
2>&1; then
607 ruleset_raw
="$("${ban_nftcmd}" -tj list ruleset 2>/dev/null)"
609 printf "%s\n" "flush set inet banIP ${feed}"
610 handle
="$(printf "%s
\n" "${ruleset_raw}" | "${ban_jsoncmd}" -ql1 -e "@.nftables[@.rule.table=\"banIP\"&&@.rule.chain=\"wan-input\"][@.expr[0].match.right=\"@${feed}\"].handle
")"
611 [ -n "${handle}" ] && printf "%s\n" "delete rule inet banIP wan-input handle ${handle}"
612 handle
="$(printf "%s
\n" "${ruleset_raw}" | "${ban_jsoncmd}" -ql1 -e "@.nftables[@.rule.table=\"banIP\"&&@.rule.chain=\"wan-forward\"][@.expr[0].match.right=\"@${feed}\"].handle
")"
613 [ -n "${handle}" ] && printf "%s\n" "delete rule inet banIP wan-forward handle ${handle}"
614 handle
="$(printf "%s
\n" "${ruleset_raw}" | "${ban_jsoncmd}" -ql1 -e "@.nftables[@.rule.table=\"banIP\"&&@.rule.chain=\"lan-forward\"][@.expr[0].match.right=\"@${feed}\"].handle
")"
615 [ -n "${handle}" ] && printf "%s\n" "delete rule inet banIP lan-forward handle ${handle}"
619 # restore local backups during init
621 if { [ "${ban_action}" != "reload" ] || [ "${feed_url}" = "local" ]; } && [ "${feed%v*}" != "allowlist" ] && [ "${feed%v*}" != "blocklist" ]; then
622 f_restore
"${feed}" "${feed_url}" "${tmp_load}"
624 feed_rc
="${restore_rc}"
627 # prepare local allowlist
629 if [ "${feed%v*}" = "allowlist" ] && [ ! -f "${tmp_allow}" ]; then
630 "${ban_catcmd}" "${ban_allowlist}" 2>/dev/null >"${tmp_allow}"
631 for feed_url
in ${ban_allowurl}; do
632 feed_log
="$("${ban_fetchcmd}" ${ban_fetchparm} "${tmp_load}" "${feed_url}" 2>&1)"
634 if [ "${feed_rc}" = "0" ] && [ -s "${tmp_load}" ]; then
635 "${ban_catcmd}" "${tmp_load}" 2>/dev/null >>"${tmp_allow}"
637 f_log
"info" "download for feed '${feed%v*}' failed (rc: ${feed_rc:-"-"}/log: ${feed_log})"
644 if [ "${feed%v*}" = "allowlist" ]; then
646 printf "%s\n\n" "#!/usr/sbin/nft -f"
647 [ -s "${tmp_flush}" ] && "${ban_catcmd}" "${tmp_flush}"
648 if [ "${proto}" = "4MAC" ]; then
649 "${ban_awkcmd}" '/^([0-9A-f]{2}:){5}[0-9A-f]{2}(\/([0-9]|[1-3][0-9]|4[0-8]))?([[:space:]]+([0-9]{1,3}\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])(\/(1?[0-9]|2?[0-9]|3?[0-2]))?[[:space:]]*$|[[:space:]]+$|$)/{if(!$2)$2="0.0.0.0/0";if(!seen[$1]++)printf "%s . %s, ",tolower($1),$2}' "${tmp_allow}" >"${tmp_file}"
650 printf "%s\n" "add set inet banIP ${feed} { type ether_addr . ipv4_addr; flags interval; auto-merge; policy ${ban_nftpolicy}; $(f_getelements "${tmp_file}") }"
651 [ -z "${feed_direction##*forwardlan*}" ] && printf "%s\n" "add rule inet banIP lan-forward ether saddr . ip saddr @${feed} counter accept"
652 elif [ "${proto}" = "6MAC" ]; then
653 "${ban_awkcmd}" '/^([0-9A-f]{2}:){5}[0-9A-f]{2}(\/([0-9]|[1-3][0-9]|4[0-8]))?([[:space:]]+([0-9A-f]{0,4}:){1,7}[0-9A-f]{0,4}:?(\/(1?[0-2][0-8]|[0-9][0-9]))?[[:space:]]*$|[[:space:]]+$|$)/{if(!$2)$2="::/0";if(!seen[$1]++)printf "%s . %s, ",tolower($1),$2}' "${tmp_allow}" >"${tmp_file}"
654 printf "%s\n" "add set inet banIP ${feed} { type ether_addr . ipv6_addr; flags interval; auto-merge; policy ${ban_nftpolicy}; $(f_getelements "${tmp_file}") }"
655 [ -z "${feed_direction##*forwardlan*}" ] && printf "%s\n" "add rule inet banIP lan-forward ether saddr . ip6 saddr @${feed} counter accept"
656 elif [ "${proto}" = "4" ]; then
657 "${ban_awkcmd}" '/^(([0-9]{1,3}\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])(\/(1?[0-9]|2?[0-9]|3?[0-2]))?)([[:space:]].*|$)/{printf "%s, ",$1}' "${tmp_allow}" >"${tmp_file}"
658 printf "%s\n" "add set inet banIP ${feed} { type ipv4_addr; flags interval; auto-merge; policy ${ban_nftpolicy}; $(f_getelements "${tmp_file}") }"
659 if [ -z "${feed_direction##*input*}" ]; then
660 if [ "${ban_allowlistonly}" = "1" ]; then
661 printf "%s\n" "add rule inet banIP wan-input ip saddr != @${feed} ${log_input} counter drop"
663 printf "%s\n" "add rule inet banIP wan-input ip saddr @${feed} counter accept"
666 if [ -z "${feed_direction##*forwardwan*}" ]; then
667 if [ "${ban_allowlistonly}" = "1" ]; then
668 printf "%s\n" "add rule inet banIP wan-forward ip saddr != @${feed} ${log_forwardwan} counter drop"
670 printf "%s\n" "add rule inet banIP wan-forward ip saddr @${feed} counter accept"
673 if [ -z "${feed_direction##*forwardlan*}" ]; then
674 if [ "${ban_allowlistonly}" = "1" ]; then
675 printf "%s\n" "add rule inet banIP lan-forward ip daddr != @${feed} ${log_forwardlan} counter reject with icmp type admin-prohibited"
677 printf "%s\n" "add rule inet banIP lan-forward ip daddr @${feed} counter accept"
680 elif [ "${proto}" = "6" ]; then
681 "${ban_awkcmd}" '!/^([0-9A-f]{2}:){5}[0-9A-f]{2}.*/{printf "%s\n",$1}' "${tmp_allow}" |
682 "${ban_awkcmd}" '/^(([0-9A-f]{0,4}:){1,7}[0-9A-f]{0,4}:?(\/(1?[0-2][0-8]|[0-9][0-9]))?)([[:space:]].*|$)/{printf "%s, ",tolower($1)}' >"${tmp_file}"
683 printf "%s\n" "add set inet banIP ${feed} { type ipv6_addr; flags interval; auto-merge; policy ${ban_nftpolicy}; $(f_getelements "${tmp_file}") }"
684 if [ -z "${feed_direction##*input*}" ]; then
685 if [ "${ban_allowlistonly}" = "1" ]; then
686 printf "%s\n" "add rule inet banIP wan-input ip6 saddr != @${feed} ${log_input} counter drop"
688 printf "%s\n" "add rule inet banIP wan-input ip6 saddr @${feed} counter accept"
691 if [ -z "${feed_direction##*forwardwan*}" ]; then
692 if [ "${ban_allowlistonly}" = "1" ]; then
693 printf "%s\n" "add rule inet banIP wan-forward ip6 saddr != @${feed} ${log_forwardwan} counter drop"
695 printf "%s\n" "add rule inet banIP wan-forward ip6 saddr @${feed} counter accept"
698 if [ -z "${feed_direction##*forwardlan*}" ]; then
699 if [ "${ban_allowlistonly}" = "1" ]; then
700 printf "%s\n" "add rule inet banIP lan-forward ip6 daddr != @${feed} ${log_forwardlan} counter reject with icmpv6 type admin-prohibited"
702 printf "%s\n" "add rule inet banIP lan-forward ip6 daddr @${feed} counter accept"
708 elif [ "${feed%v*}" = "blocklist" ]; then
710 printf "%s\n\n" "#!/usr/sbin/nft -f"
711 [ -s "${tmp_flush}" ] && "${ban_catcmd}" "${tmp_flush}"
712 if [ "${proto}" = "4MAC" ]; then
713 "${ban_awkcmd}" '/^([0-9A-f]{2}:){5}[0-9A-f]{2}(\/([0-9]|[1-3][0-9]|4[0-8]))?([[:space:]]+([0-9]{1,3}\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])(\/(1?[0-9]|2?[0-9]|3?[0-2]))?[[:space:]]*$|[[:space:]]+$|$)/{if(!$2)$2="0.0.0.0/0";if(!seen[$1]++)printf "%s . %s, ",tolower($1),$2}' "${ban_blocklist}" >"${tmp_file}"
714 printf "%s\n" "add set inet banIP ${feed} { type ether_addr . ipv4_addr; flags interval; auto-merge; policy ${ban_nftpolicy}; $(f_getelements "${tmp_file}") }"
715 [ -z "${feed_direction##*forwardlan*}" ] && printf "%s\n" "add rule inet banIP lan-forward ether saddr . ip saddr @${feed} counter reject"
716 elif [ "${proto}" = "6MAC" ]; then
717 "${ban_awkcmd}" '/^([0-9A-f]{2}:){5}[0-9A-f]{2}(\/([0-9]|[1-3][0-9]|4[0-8]))?([[:space:]]+([0-9A-f]{0,4}:){1,7}[0-9A-f]{0,4}:?(\/(1?[0-2][0-8]|[0-9][0-9]))?[[:space:]]*$|[[:space:]]+$|$)/{if(!$2)$2="::/0";if(!seen[$1]++)printf "%s . %s, ",tolower($1),$2}' "${ban_blocklist}" >"${tmp_file}"
718 printf "%s\n" "add set inet banIP ${feed} { type ether_addr . ipv6_addr; flags interval; auto-merge; policy ${ban_nftpolicy}; $(f_getelements "${tmp_file}") }"
719 [ -z "${feed_direction##*forwardlan*}" ] && printf "%s\n" "add rule inet banIP lan-forward ether saddr . ip6 saddr @${feed} counter reject"
720 elif [ "${proto}" = "4" ]; then
721 if [ "${ban_deduplicate}" = "1" ]; then
722 "${ban_awkcmd}" '/^(([0-9]{1,3}\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])(\/(1?[0-9]|2?[0-9]|3?[0-2]))?)([[:space:]].*|$)/{printf "%s,\n",$1}' "${ban_blocklist}" >"${tmp_raw}"
723 "${ban_awkcmd}" 'NR==FNR{member[$0];next}!($0 in member)' "${ban_tmpfile}.deduplicate" "${tmp_raw}" 2>/dev/null >"${tmp_split}"
724 "${ban_awkcmd}" 'BEGIN{FS="[ ,]"}NR==FNR{member[$1];next}!($1 in member)' "${ban_tmpfile}.deduplicate" "${ban_blocklist}" 2>/dev/null >"${tmp_raw}"
725 "${ban_catcmd}" "${tmp_raw}" 2>/dev/null >"${ban_blocklist}"
727 "${ban_awkcmd}" '/^(([0-9]{1,3}\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])(\/(1?[0-9]|2?[0-9]|3?[0-2]))?)([[:space:]].*|$)/{printf "%s,\n",$1}' "${ban_blocklist}" >"${tmp_split}"
729 "${ban_awkcmd}" '{ORS=" ";print}' "${tmp_split}" 2>/dev/null >"${tmp_file}"
730 printf "%s\n" "add set inet banIP ${feed} { type ipv4_addr; flags interval, timeout; auto-merge; policy ${ban_nftpolicy}; $(f_getelements "${tmp_file}") }"
731 [ -z "${feed_direction##*input*}" ] && printf "%s\n" "add rule inet banIP wan-input ip saddr @${feed} ${log_input} counter drop"
732 [ -z "${feed_direction##*forwardwan*}" ] && printf "%s\n" "add rule inet banIP wan-forward ip saddr @${feed} ${log_forwardwan} counter drop"
733 [ -z "${feed_direction##*forwardlan*}" ] && printf "%s\n" "add rule inet banIP lan-forward ip daddr @${feed} ${log_forwardlan} counter reject with icmp type admin-prohibited"
734 elif [ "${proto}" = "6" ]; then
735 if [ "${ban_deduplicate}" = "1" ]; then
736 "${ban_awkcmd}" '!/^([0-9A-f]{2}:){5}[0-9A-f]{2}.*/{printf "%s\n",$1}' "${ban_blocklist}" |
737 "${ban_awkcmd}" '/^(([0-9A-f]{0,4}:){1,7}[0-9A-f]{0,4}:?(\/(1?[0-2][0-8]|[0-9][0-9]))?)([[:space:]].*|$)/{printf "%s,\n",tolower($1)}' >"${tmp_raw}"
738 "${ban_awkcmd}" 'NR==FNR{member[$0];next}!($0 in member)' "${ban_tmpfile}.deduplicate" "${tmp_raw}" 2>/dev/null >"${tmp_split}"
739 "${ban_awkcmd}" 'BEGIN{FS="[ ,]"}NR==FNR{member[$1];next}!($1 in member)' "${ban_tmpfile}.deduplicate" "${ban_blocklist}" 2>/dev/null >"${tmp_raw}"
740 "${ban_catcmd}" "${tmp_raw}" 2>/dev/null >"${ban_blocklist}"
742 "${ban_awkcmd}" '!/^([0-9A-f]{2}:){5}[0-9A-f]{2}.*/{printf "%s\n",$1}' "${ban_blocklist}" |
743 "${ban_awkcmd}" '/^(([0-9A-f]{0,4}:){1,7}[0-9A-f]{0,4}:?(\/(1?[0-2][0-8]|[0-9][0-9]))?)([[:space:]].*|$)/{printf "%s,\n",tolower($1)}' >"${tmp_split}"
745 "${ban_awkcmd}" '{ORS=" ";print}' "${tmp_split}" 2>/dev/null >"${tmp_file}"
746 printf "%s\n" "add set inet banIP ${feed} { type ipv6_addr; flags interval, timeout; auto-merge; policy ${ban_nftpolicy}; $(f_getelements "${tmp_file}") }"
747 [ -z "${feed_direction##*input*}" ] && printf "%s\n" "add rule inet banIP wan-input ip6 saddr @${feed} ${log_input} counter drop"
748 [ -z "${feed_direction##*forwardwan*}" ] && printf "%s\n" "add rule inet banIP wan-forward ip6 saddr @${feed} ${log_forwardwan} counter drop"
749 [ -z "${feed_direction##*forwardlan*}" ] && printf "%s\n" "add rule inet banIP lan-forward ip6 daddr @${feed} ${log_forwardlan} counter reject with icmpv6 type admin-prohibited"
754 # handle external feeds
756 elif [ "${restore_rc}" != "0" ] && [ "${feed_url}" != "local" ]; then
757 # handle country downloads
759 if [ "${feed%v*}" = "country" ]; then
760 for country
in ${ban_country}; do
761 feed_log
="$("${ban_fetchcmd}" ${ban_fetchparm} "${tmp_raw}" "${feed_url}${country}-aggregated.zone
" 2>&1)"
763 [ "${feed_rc}" = "0" ] && "${ban_catcmd}" "${tmp_raw}" 2>/dev/null >>"${tmp_load}"
767 # handle asn downloads
769 elif [ "${feed%v*}" = "asn" ]; then
770 for asn
in ${ban_asn}; do
771 feed_log
="$("${ban_fetchcmd}" ${ban_fetchparm} "${tmp_raw}" "${feed_url}AS${asn}" 2>&1)"
773 [ "${feed_rc}" = "0" ] && "${ban_catcmd}" "${tmp_raw}" 2>/dev/null >>"${tmp_load}"
777 # handle compressed downloads
779 elif [ -n "${feed_flag}" ]; then
780 case "${feed_flag}" in
782 feed_log
="$("${ban_fetchcmd}" ${ban_fetchparm} "${tmp_raw}" "${feed_url}" 2>&1)"
784 if [ "${feed_rc}" = "0" ]; then
785 "${ban_zcatcmd}" "${tmp_raw}" 2>/dev/null >"${tmp_load}"
792 # handle normal downloads
795 feed_log
="$("${ban_fetchcmd}" ${ban_fetchparm} "${tmp_load}" "${feed_url}" 2>&1)"
799 [ "${feed_rc}" != "0" ] && f_log "info" "download for feed '${feed}' failed (rc: ${feed_rc:-"-"}/log: ${feed_log})"
803 if [ "${restore_rc}" != "0" ] && [ "${feed_rc}" = "0" ] && [ "${feed_url}" != "local" ] && [ ! -s "${tmp_nft}" ]; then
804 f_backup
"${feed}" "${tmp_load}"
806 elif [ -z "${restore_rc}" ] && [ "${feed_rc}" != "0" ] && [ "${feed_url}" != "local" ] && [ ! -s "${tmp_nft}" ]; then
807 f_restore
"${feed}" "${feed_url}" "${tmp_load}" "${feed_rc}"
811 # build nft file with Sets and rules for regular downloads
813 if [ "${feed_rc}" = "0" ] && [ ! -s "${tmp_nft}" ]; then
816 if [ "${ban_deduplicate}" = "1" ] && [ "${feed_url}" != "local" ]; then
817 "${ban_awkcmd}" "${feed_rule}" "${tmp_load}" 2>/dev/null >"${tmp_raw}"
818 "${ban_awkcmd}" 'NR==FNR{member[$0];next}!($0 in member)' "${ban_tmpfile}.deduplicate" "${tmp_raw}" 2>/dev/null | tee -a "${ban_tmpfile}.deduplicate" >"${tmp_split}"
820 "${ban_awkcmd}" "${feed_rule}" "${tmp_load}" 2>/dev/null >"${tmp_split}"
825 if [ "${feed_rc}" = "0" ]; then
826 if [ -n "${ban_splitsize//[![:digit]]/}" ] && [ "${ban_splitsize//[![:digit]]/}" -gt "0" ]; then
827 if ! "${ban_awkcmd}" "NR%${ban_splitsize//[![:digit]]/}==1{file=\"${tmp_file}.\"++i;}{ORS=\" \";print > file}" "${tmp_split}" 2>/dev
/null
; then
828 rm -f "${tmp_file}".
*
829 f_log
"info" "can't split Set '${feed}' to size '${ban_splitsize//[![:digit]]/}'"
832 "${ban_awkcmd}" '{ORS=" ";print}' "${tmp_split}" 2>/dev/null >"${tmp_file}.1"
836 rm -f "${tmp_raw}" "${tmp_load}"
837 if [ "${feed_rc}" = "0" ] && [ "${proto}" = "4" ]; then
839 # nft header (IPv4 Set)
841 printf "%s\n\n" "#!/usr/sbin/nft -f"
842 [ -s "${tmp_flush}" ] && "${ban_catcmd}" "${tmp_flush}"
843 printf "%s\n" "add set inet banIP ${feed} { type ipv4_addr; flags interval; auto-merge; policy ${ban_nftpolicy}; $(f_getelements "${tmp_file}.1") }"
845 # input and forward rules
847 [ -z "${feed_direction##*input*}" ] && printf "%s\n" "add rule inet banIP wan-input ip saddr @${feed} ${log_input} counter drop"
848 [ -z "${feed_direction##*forwardwan*}" ] && printf "%s\n" "add rule inet banIP wan-forward ip saddr @${feed} ${log_forwardwan} counter drop"
849 [ -z "${feed_direction##*forwardlan*}" ] && printf "%s\n" "add rule inet banIP lan-forward ip daddr @${feed} ${log_forwardlan} counter reject with icmp type admin-prohibited"
851 elif [ "${feed_rc}" = "0" ] && [ "${proto}" = "6" ]; then
853 # nft header (IPv6 Set)
855 printf "%s\n\n" "#!/usr/sbin/nft -f"
856 [ -s "${tmp_flush}" ] && "${ban_catcmd}" "${tmp_flush}"
857 printf "%s\n" "add set inet banIP ${feed} { type ipv6_addr; flags interval; auto-merge; policy ${ban_nftpolicy}; $(f_getelements "${tmp_file}.1") }"
859 # input and forward rules
861 [ -z "${feed_direction##*input*}" ] && printf "%s\n" "add rule inet banIP wan-input ip6 saddr @${feed} ${log_input} counter drop"
862 [ -z "${feed_direction##*forwardwan*}" ] && printf "%s\n" "add rule inet banIP wan-forward ip6 saddr @${feed} ${log_forwardwan} counter drop"
863 [ -z "${feed_direction##*forwardlan*}" ] && printf "%s\n" "add rule inet banIP lan-forward ip6 daddr @${feed} ${log_forwardlan} counter reject with icmpv6 type admin-prohibited"
868 # load generated nft file in banIP table
870 if [ "${feed_rc}" = "0" ]; then
871 cnt_dl
="$("${ban_awkcmd}" 'END{printf "%d
",NR}' "${tmp_split}" 2>/dev/null)"
872 if [ "${cnt_dl:-"0"}" -gt "0" ] || [ "${feed_url}" = "local" ] || [ "${feed%v*}" = "allowlist" ] || [ "${feed%v*}" = "blocklist" ]; then
873 feed_log
="$("${ban_nftcmd}" -f "${tmp_nft}" 2>&1)"
876 # load additional split files
878 if [ "${feed_rc}" = "0" ]; then
879 for split_file
in "${tmp_file}".
*; do
880 [ ! -f "${split_file}" ] && break
881 if [ "${split_file##*.}" = "1" ]; then
882 rm -f "${split_file}"
885 if ! "${ban_nftcmd}" add element inet banIP "${feed}" "{ $("${ban_catcmd}" "${split_file}") }" >/dev
/null
2>&1; then
886 f_log
"info" "can't add split file '${split_file##*.}' to Set '${feed}'"
888 rm -f "${split_file}"
890 if [ "${ban_debug}" = "1" ] && [ "${ban_reportelements}" = "1" ]; then
891 cnt_set
="$("${ban_nftcmd}" -j list set inet banIP "${feed}" 2>/dev/null | "${ban_jsoncmd}" -qe '@.nftables[*].set.elem[*]' | wc -l 2>/dev/null)"
895 f_log
"info" "skip empty feed '${feed}'"
898 rm -f "${tmp_split}" "${tmp_nft}"
901 f_log
"debug" "f_down ::: name: ${feed}, cnt_dl: ${cnt_dl:-"-"}, cnt_set: ${cnt_set:-"-"}, split_size: ${ban_splitsize:-"-"}, time: $((end_ts - start_ts)), rc: ${feed_rc:-"-"}, log: ${feed_log:-"-"}"
907 local backup_rc feed
="${1}" feed_file
="${2}"
909 gzip -cf "${feed_file}" >"${ban_backupdir}/banIP.${feed}.gz"
912 f_log
"debug" "f_backup ::: name: ${feed}, source: ${feed_file##*/}, target: banIP.${feed}.gz, rc: ${backup_rc}"
919 local tmp_feed restore_rc
="1" feed
="${1}" feed_url="${2}" feed_file="${3}" feed_rc="${4:-"0"}"
921 [ "${feed_rc}" != "0" ] && restore_rc
="${feed_rc}"
922 [ "${feed_url}" = "local" ] && tmp_feed="${feed%v*}v4" || tmp_feed="${feed}"
923 if [ -f "${ban_backupdir}/banIP.${tmp_feed}.gz" ]; then
924 "${ban_zcatcmd}" "${ban_backupdir}/banIP.${tmp_feed}.gz" 2>/dev/null >"${feed_file}"
928 f_log
"debug" "f_restore ::: name: ${feed}, source: banIP.${tmp_feed}.gz, target: ${feed_file##*/}, in_rc: ${feed_rc}, rc: ${restore_rc}"
932 # remove disabled Sets
935 local feedlist tmp_del ruleset_raw item table_sets handle del_set feed_log feed_rc
938 json_get_keys feedlist
939 tmp_del
="${ban_tmpfile}.final.delete"
940 ruleset_raw
="$("${ban_nftcmd}" -tj list ruleset 2>/dev/null)"
941 table_sets
="$(printf "%s
\n" "${ruleset_raw}" | "${ban_jsoncmd}" -qe '@.nftables[@.set.table="banIP
"].set.name')"
943 printf "%s\n\n" "#!/usr/sbin/nft -f"
944 for item
in ${table_sets}; do
945 if ! printf "%s" "allowlist blocklist ${ban_feed}" | "${ban_grepcmd}" -q "${item%v*}" ||
946 ! printf "%s" "allowlist blocklist ${feedlist}" | "${ban_grepcmd}" -q "${item%v*}"; then
947 del_set
="${del_set}${item}, "
948 rm -f "${ban_backupdir}/banIP.${item}.gz"
949 printf "%s\n" "flush set inet banIP ${item}"
950 handle
="$(printf "%s
\n" "${ruleset_raw}" | "${ban_jsoncmd}" -ql1 -e "@.nftables[@.rule.table=\"banIP\"&&@.rule.chain=\"wan-input\"][@.expr[0].match.right=\"@${item}\"].handle
")"
951 [ -n "${handle}" ] && printf "%s\n" "delete rule inet banIP wan-input handle ${handle}"
952 handle
="$(printf "%s
\n" "${ruleset_raw}" | "${ban_jsoncmd}" -ql1 -e "@.nftables[@.rule.table=\"banIP\"&&@.rule.chain=\"wan-forward\"][@.expr[0].match.right=\"@${item}\"].handle
")"
953 [ -n "${handle}" ] && printf "%s\n" "delete rule inet banIP wan-forward handle ${handle}"
954 handle
="$(printf "%s
\n" "${ruleset_raw}" | "${ban_jsoncmd}" -ql1 -e "@.nftables[@.rule.table=\"banIP\"&&@.rule.chain=\"lan-forward\"][@.expr[0].match.right=\"@${item}\"].handle
")"
955 [ -n "${handle}" ] && printf "%s\n" "delete rule inet banIP lan-forward handle ${handle}"
956 printf "%s\n\n" "delete set inet banIP ${item}"
961 if [ -n "${del_set}" ]; then
962 del_set
="${del_set%%??}"
963 feed_log
="$("${ban_nftcmd}" -f "${tmp_del}" 2>&1)"
968 f_log
"debug" "f_rmset ::: sets: ${del_set:-"-"}, rc: ${feed_rc:-"-"}, log: ${feed_log:-"-"}"
971 # generate status information
974 local object duration item table_sets cnt_elements
="0" custom_feed
="0" split="0" status
="${1}"
976 [ -z "${ban_dev}" ] && f_conf
977 if [ "${status}" = "active" ]; then
978 if [ -n "${ban_starttime}" ]; then
979 ban_endtime
="$(date "+%s
")"
980 duration
="$(((ban_endtime - ban_starttime) / 60))m $(((ban_endtime - ban_starttime) % 60))s"
982 table_sets
="$("${ban_nftcmd}" -tj list ruleset 2>/dev/null | "${ban_jsoncmd}" -qe '@.nftables[@.set.table="banIP
"].set.name')"
983 if [ "${ban_reportelements}" = "1" ]; then
984 for item
in ${table_sets}; do
985 cnt_elements
="$((cnt_elements + $("${ban_nftcmd}" -j list set inet banIP "${item}" 2>/dev/null | "${ban_jsoncmd}" -qe '@.nftables[*].set.elem[*]' | wc -l 2>/dev/null)))"
988 runtime
="action: ${ban_action:-"-"}, duration: ${duration:-"-"}, date: $(date "+%Y-
%m-
%d
%H
:%M
:%S
")"
990 [ -s "${ban_customfeedfile}" ] && custom_feed
="1"
991 [ "${ban_splitsize:-"0"}" -gt "0" ] && split="1"
995 json_load_file
"${ban_rtfile}" >/dev
/null
2>&1
996 json_add_string
"status" "${status}"
997 json_add_string
"version" "${ban_ver}"
998 json_add_string
"element_count" "${cnt_elements}"
999 json_add_array
"active_feeds"
1000 for object
in ${table_sets:-"-"}; do
1002 json_add_string
"feed" "${object}"
1006 json_add_array
"active_devices"
1007 for object
in ${ban_dev:-"-"}; do
1009 json_add_string
"device" "${object}"
1012 for object
in ${ban_ifv4:-"-"} ${ban_ifv6:-"-"}; do
1014 json_add_string
"interface" "${object}"
1018 json_add_array
"active_uplink"
1019 for object
in ${ban_uplink:-"-"}; do
1021 json_add_string
"uplink" "${object}"
1025 json_add_string
"nft_info" "priority: ${ban_nftpriority}, policy: ${ban_nftpolicy}, loglevel: ${ban_nftloglevel}, expiry: ${ban_nftexpiry:-"-"}"
1026 json_add_string
"run_info" "base: ${ban_basedir}, backup: ${ban_backupdir}, report: ${ban_reportdir}, custom feed: $(f_char ${custom_feed})"
1027 json_add_string
"run_flags" "auto: $(f_char ${ban_autodetect}), proto (4/6): $(f_char ${ban_protov4})/$(f_char ${ban_protov6}), log (wan-inp/wan-fwd/lan-fwd): $(f_char ${ban_loginput})/$(f_char ${ban_logforwardwan})/$(f_char ${ban_logforwardlan}), dedup: $(f_char ${ban_deduplicate}), split: $(f_char ${split}), allowed only: $(f_char ${ban_allowlistonly})"
1028 json_add_string
"last_run" "${runtime:-"-"}"
1029 json_add_string
"system_info" "cores: ${ban_cores}, memory: ${ban_memory}, device: ${ban_sysver}"
1030 json_dump
>"${ban_rtfile}"
1033 # get status information
1036 local key keylist
type value index_key1 index_key2 index_value1 index_value2
1038 [ -z "${ban_dev}" ] && f_conf
1039 json_load_file
"${ban_rtfile}" >/dev
/null
2>&1
1040 if json_get_keys keylist
; then
1041 printf "%s\n" "::: banIP runtime information"
1042 for key
in ${keylist}; do
1043 json_get_var value
"${key}" >/dev
/null
2>&1
1044 if [ "${key}" = "status" ]; then
1045 value
="${value} ($(f_actual))"
1046 elif [ "${key}" = "active_devices" ]; then
1047 json_select
"${key}" >/dev
/null
2>&1
1049 while json_get_type
type "${index}" && [ "${type}" = "object" ]; do
1050 json_get_keys index_key1
"${index}" >/dev
/null
2>&1
1051 json_get_keys index_key2
"$((index + 1))" >/dev
/null
2>&1
1052 json_get_values index_value1
"${index}" >/dev
/null
2>&1
1053 if [ "${index}" = "1" ] && [ "${index_key1// /}" = "device" ] && [ "${index_key2// /}" = "interface" ]; then
1054 json_get_values index_value2
"$((index + 1))" >/dev
/null
2>&1
1055 value
="${index_value1} ::: ${index_value2}"
1056 index
="$((index + 1))"
1057 elif [ "${index}" = "1" ]; then
1058 value
="${index_value1}"
1059 elif [ "${index}" != "1" ] && [ "${index_key1// /}" = "device" ] && [ "${index_key2// /}" = "interface" ]; then
1060 json_get_values index_value2
"$((index + 1))" >/dev
/null
2>&1
1061 value
="${value}, ${index_value1} ::: ${index_value2}"
1062 index
="$((index + 1))"
1063 elif [ "${index}" != "1" ]; then
1064 value
="${value}, ${index_value1}"
1066 index
="$((index + 1))"
1069 elif [ "${key%_*}" = "active" ]; then
1070 json_select
"${key}" >/dev
/null
2>&1
1072 while json_get_type
type "${index}" && [ "${type}" = "object" ]; do
1073 json_get_values index_value1
"${index}" >/dev
/null
2>&1
1074 if [ "${index}" = "1" ]; then
1075 value
="${index_value1}"
1077 value
="${value}, ${index_value1}"
1079 index
="$((index + 1))"
1083 printf " + %-17s : %s\n" "${key}" "${value:-"-"}"
1086 printf "%s\n" "::: no banIP runtime information available"
1093 local cnt list domain lookup ip elementsv4 elementsv6 start_time end_time duration cnt_domain
="0" cnt_ip
="0" feed
="${1}"
1095 [ -z "${ban_dev}" ] && f_conf
1096 start_time
="$(date "+%s
")"
1097 if [ "${feed}" = "allowlist" ]; then
1098 list
="$("${ban_awkcmd}" '/^([[:alnum:]_-]{1,63}\.)+[[:alpha:]]+([[:space:]]|$)/{printf "%s
",tolower($1)}' "${ban_allowlist}" 2>/dev/null)"
1099 elif [ "${feed}" = "blocklist" ]; then
1100 list
="$("${ban_awkcmd}" '/^([[:alnum:]_-]{1,63}\.)+[[:alpha:]]+([[:space:]]|$)/{printf "%s
",tolower($1)}' "${ban_blocklist}" 2>/dev/null)"
1103 for domain
in ${list}; do
1104 lookup
="$("${ban_lookupcmd}" "${domain}" ${ban_resolver} 2>/dev/null | "${ban_awkcmd}" '/^Address[ 0-9]*: /{if(!seen[$NF]++)printf "%s ",$NF}' 2>/dev/null)"
1105 for ip in ${lookup}; do
1106 if [ "${ip%%.*}" = "0" ] || [ -z "${ip%%::*}" ]; then
1109 if { [ "${feed}" = "allowlist" ] && ! "${ban_grepcmd}" -q "^${ip}" "${ban_allowlist}"; } ||
1110 { [ "${feed}" = "blocklist" ] && ! "${ban_grepcmd}" -q "^${ip}" "${ban_blocklist}"; }; then
1111 if [ "${ip##*:}" = "${ip}" ]; then
1112 elementsv4="${elementsv4} ${ip},"
1114 elementsv6="${elementsv6} ${ip},"
1116 if [ "${feed}" = "allowlist" ] && [ "${ban_autoallowlist}" = "1" ]; then
1117 printf "%-42s%s\n" "${ip}" "# '${domain}' added on $(date "+%Y-%m-%d %H:%M:%S")" >>"${ban_allowlist}"
1118 elif [ "${feed}" = "blocklist" ] && [ "${ban_autoblocklist}" = "1" ]; then
1119 printf "%-42s%s\n" "${ip}" "# '${domain}' added on $(date "+%Y-%m-%d %H:%M:%S")" >>"${ban_blocklist}"
1121 cnt_ip="$((cnt_ip + 1))"
1125 cnt_domain="$((cnt_domain + 1))"
1127 if [ -n "${elementsv4}" ]; then
1128 if ! "${ban_nftcmd}" add element inet banIP "${feed}v4" "{ ${elementsv4} }" >/dev/null 2>&1; then
1129 f_log "info" "can't add lookup
file to Set
'${feed}v4'"
1132 if [ -n "${elementsv6}" ]; then
1133 if ! "${ban_nftcmd}" add element inet banIP "${feed}v6" "{ ${elementsv6} }" >/dev/null 2>&1; then
1134 f_log "info
" "can
't add lookup file to Set '${feed}v6
'"
1137 end_time="$(date "+%s")"
1138 duration="$(((end_time - start_time) / 60))m $(((end_time - start_time) % 60))s"
1140 f_log "debug" "f_lookup ::: feed: ${feed}, domains: ${cnt_domain}, IPs: ${cnt_ip}, duration: ${duration}"
1146 local report_jsn report_txt tmp_val ruleset_raw item table_sets set_cnt set_input set_forwardwan set_forwardlan set_cntinput set_cntforwardwan set_cntforwardlan output="${1}"
1147 local detail set_details jsnval timestamp autoadd_allow autoadd_block sum_sets sum_setinput sum_setforwardwan sum_setforwardlan sum_setelements sum_cntinput sum_cntforwardwan sum_cntforwardlan
1149 [ -z "${ban_dev}" ] && f_conf
1150 f_mkdir "${ban_reportdir}"
1151 report_jsn="${ban_reportdir}/ban_report.jsn"
1152 report_txt="${ban_reportdir}/ban_report.txt"
1154 # json output preparation
1156 ruleset_raw="$("${ban_nftcmd}" -tj list ruleset 2>/dev/null)"
1157 table_sets="$(printf "%s" "${ruleset_raw}" | "${ban_jsoncmd}" -qe '@.nftables
[@.
set.table
="banIP"].
set.name
')"
1160 sum_setforwardwan="0"
1161 sum_setforwardlan="0"
1164 sum_cntforwardwan="0"
1165 sum_cntforwardlan="0"
1166 timestamp="$(date "+%Y-%m-%d %H:%M:%S")"
1170 printf "\t%s\n" '"sets":{'
1171 for item in ${table_sets}; do
1172 set_cntinput="$(printf "%s" "${ruleset_raw}" | "${ban_jsoncmd}" -ql1 -e "@.nftables[@.rule.table=\"banIP\"&&@.rule.chain=\"wan-input\"][@.expr[0].match.right=\"@${item}\"].expr[*].counter.packets")"
1173 set_cntforwardwan="$(printf "%s" "${ruleset_raw}" | "${ban_jsoncmd}" -ql1 -e "@.nftables[@.rule.table=\"banIP\"&&@.rule.chain=\"wan-forward\"][@.expr[0].match.right=\"@${item}\"].expr[*].counter.packets")"
1174 set_cntforwardlan="$(printf "%s" "${ruleset_raw}" | "${ban_jsoncmd}" -ql1 -e "@.nftables[@.rule.table=\"banIP\"&&@.rule.chain=\"lan-forward\"][@.expr[0].match.right=\"@${item}\"].expr[*].counter.packets")"
1175 if [ "${ban_reportelements}" = "1" ]; then
1176 set_cnt="$("${ban_nftcmd}" -j list set inet banIP "${item}" 2>/dev/null | "${ban_jsoncmd}" -qe '@.nftables
[*].
set.elem
[*]' | wc -l 2>/dev/null)"
1177 sum_setelements="$((sum_setelements + set_cnt))"
1180 sum_setelements="n/a"
1182 if [ -n "${set_cntinput}" ]; then
1184 sum_setinput="$((sum_setinput + 1))"
1185 sum_cntinput="$((sum_cntinput + set_cntinput))"
1190 if [ -n "${set_cntforwardwan}" ]; then
1192 sum_setforwardwan="$((sum_setforwardwan + 1))"
1193 sum_cntforwardwan="$((sum_cntforwardwan + set_cntforwardwan))"
1196 set_cntforwardwan=""
1198 if [ -n "${set_cntforwardlan}" ]; then
1200 sum_setforwardlan="$((sum_setforwardlan + 1))"
1201 sum_cntforwardlan="$((sum_cntforwardlan + set_cntforwardlan))"
1204 set_cntforwardlan=""
1206 [ "${sum_sets}" -gt "0" ] && printf "%s\n" ","
1207 printf "\t\t%s\n" "\"${item}\":{"
1208 printf "\t\t\t%s\n" "\"cnt_elements\": \"${set_cnt}\","
1209 printf "\t\t\t%s\n" "\"cnt_input\": \"${set_cntinput}\","
1210 printf "\t\t\t%s\n" "\"input\": \"${set_input}\","
1211 printf "\t\t\t%s\n" "\"cnt_forwardwan\": \"${set_cntforwardwan}\","
1212 printf "\t\t\t%s\n" "\"wan_forward\": \"${set_forwardwan}\","
1213 printf "\t\t\t%s\n" "\"cnt_forwardlan\": \"${set_cntforwardlan}\","
1214 printf "\t\t\t%s\n" "\"lan_forward\": \"${set_forwardlan}\""
1216 sum_sets="$((sum_sets + 1))"
1218 printf "\n\t%s\n" "},"
1219 printf "\t%s\n" "\"timestamp\": \"${timestamp}\","
1220 printf "\t%s\n" "\"autoadd_allow\": \"$("${ban_grepcmd}" -c "added on ${timestamp% *}" "${ban_allowlist}")\","
1221 printf "\t%s\n" "\"autoadd_block\": \"$("${ban_grepcmd}" -c "added on ${timestamp% *}" "${ban_blocklist}")\","
1222 printf "\t%s\n" "\"sum_sets\": \"${sum_sets}\","
1223 printf "\t%s\n" "\"sum_setinput\": \"${sum_setinput}\","
1224 printf "\t%s\n" "\"sum_setforwardwan\": \"${sum_setforwardwan}\","
1225 printf "\t%s\n" "\"sum_setforwardlan\": \"${sum_setforwardlan}\","
1226 printf "\t%s\n" "\"sum_setelements\": \"${sum_setelements}\","
1227 printf "\t%s\n" "\"sum_cntinput\": \"${sum_cntinput}\","
1228 printf "\t%s\n" "\"sum_cntforwardwan\": \"${sum_cntforwardwan}\","
1229 printf "\t%s\n" "\"sum_cntforwardlan\": \"${sum_cntforwardlan}\""
1233 # text output preparation
1235 if [ "${output}" != "json" ] && [ -s "${report_jsn}" ]; then
1238 if json_load_file "${report_jsn}" >/dev/null 2>&1; then
1239 json_get_var timestamp "timestamp" >/dev/null 2>&1
1240 json_get_var autoadd_allow "autoadd_allow" >/dev/null 2>&1
1241 json_get_var autoadd_block "autoadd_block" >/dev/null 2>&1
1242 json_get_var sum_sets "sum_sets" >/dev/null 2>&1
1243 json_get_var sum_setinput "sum_setinput" >/dev/null 2>&1
1244 json_get_var sum_setforwardwan "sum_setforwardwan" >/dev/null 2>&1
1245 json_get_var sum_setforwardlan "sum_setforwardlan" >/dev/null 2>&1
1246 json_get_var sum_setelements "sum_setelements" >/dev/null 2>&1
1247 json_get_var sum_cntinput "sum_cntinput" >/dev/null 2>&1
1248 json_get_var sum_cntforwardwan "sum_cntforwardwan" >/dev/null 2>&1
1249 json_get_var sum_cntforwardlan "sum_cntforwardlan" >/dev/null 2>&1
1251 printf "%s\n%s\n%s\n" ":::" "::: banIP Set Statistics" ":::"
1252 printf "%s\n" " Timestamp: ${timestamp}"
1253 printf "%s\n" " ------------------------------"
1254 printf "%s\n" " auto-added to allowlist today: ${autoadd_allow}"
1255 printf "%s\n\n" " auto-added to blocklist today: ${autoadd_block}"
1256 json_select "sets" >/dev/null 2>&1
1257 json_get_keys table_sets >/dev/null 2>&1
1258 if [ -n "${table_sets}" ]; then
1259 printf "%-25s%-15s%-24s%-24s%s\n" " Set" "| Elements" "| WAN-Input (packets)" "| WAN-Forward (packets)" "| LAN-Forward (packets)"
1260 printf "%s\n" " ---------------------+--------------+-----------------------+-----------------------+------------------------"
1261 for item in ${table_sets}; do
1262 printf " %-21s" "${item}"
1263 json_select "${item}"
1264 json_get_keys set_details
1265 for detail in ${set_details}; do
1266 json_get_var jsnval "${detail}" >/dev/null 2>&1
1269 printf "%-15s" "| ${jsnval}"
1271 "cnt_input" | "cnt_forwardwan" | "cnt_forwardlan")
1272 [ -n "${jsnval}" ] && tmp_val=": ${jsnval}"
1275 printf "%-24s" "| ${jsnval}${tmp_val}"
1283 printf "%s\n" " ---------------------+--------------+-----------------------+-----------------------+------------------------"
1284 printf "%-25s%-15s%-24s%-24s%s\n" " ${sum_sets}" "| ${sum_setelements}" "| ${sum_setinput} (${sum_cntinput})" "| ${sum_setforwardwan} (${sum_cntforwardwan})" "| ${sum_setforwardlan} (${sum_cntforwardlan})"
1290 # output channel (text|json|mail)
1294 [ -s "${report_txt}" ] && "${ban_catcmd}" "${report_txt}"
1297 [ -s "${report_jsn}" ] && "${ban_catcmd}" "${report_jsn}"
1300 [ -n "${ban_mailreceiver}" ] && [ -x "${ban_mailcmd}" ] && f_mail
1303 rm -f "${report_txt}"
1309 local item table_sets ip proto hold cnt result_flag="/var/run/banIP.search" input="${1}"
1311 if [ -n "${input}" ]; then
1312 ip="$(printf "%s" "${input}" | "${ban_awkcmd}" 'BEGIN{RS="(([0-9]{1,3}\\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])(\/(1?[0-9]|2?[0-9]|3?[0-2]))?[[:space:]]*$)"}{printf "%s",RT}')"
1313 [ -n "${ip}" ] && proto="v4
"
1314 if [ -z "${proto}" ]; then
1315 ip="$
(printf "%s" "${input}" | "${ban_awkcmd}" 'BEGIN{RS="(([0-9A-f]{0,4}:){1,7}[0-9A-f]{0,4}:?(\/(1?[0-2][0-8]|[0-9][0-9]))?)([[:space:]].*|$)"}{printf "%s",RT
}')"
1316 [ -n "${ip}" ] && proto="v6"
1319 if [ -n "${proto}" ]; then
1320 table_sets="$("${ban_nftcmd}" -tj list ruleset 2>/dev/null | "${ban_jsoncmd}" -qe "@.nftables[@.set.table=\"banIP\"&&@.set.type=\"ip${proto}_addr\"].set.name")"
1322 printf "%s\n%s\n%s\n" ":::" "::: no valid search input" ":::"
1325 printf "%s\n%s\n%s\n" ":::" "::: banIP Search" ":::"
1326 printf " %s\n" "Looking for IP '${ip}' on $(date "+%Y-%m-%d %H:%M:%S")"
1327 printf " %s\n" "---"
1329 for item in ${table_sets}; do
1330 [ -f "${result_flag}" ] && break
1332 if "${ban_nftcmd}" get element inet banIP "${item}" "{ ${ip} }" >/dev/null 2>&1; then
1333 printf " %s\n" "IP found in Set '${item}'"
1337 hold="$((cnt % ban_cores))"
1338 [ "${hold}" = "0" ] && wait
1342 [ -f "${result_flag}" ] && rm -f "${result_flag}" || printf " %s\n" "IP not found"
1348 local set_elements input="${1}"
1350 if [ -z "${input}" ]; then
1351 printf "%s\n%s\n%s\n" ":::" "::: no valid survey input" ":::"
1354 set_elements="$("${ban_nftcmd}" -j list set inet banIP "${input}" 2>/dev/null | "${ban_jsoncmd}" -qe '@.nftables
[*].
set.elem
[*]')"
1355 printf "%s\n%s\n%s\n" ":::" "::: banIP Survey" ":::"
1356 printf " %s\n" "List of elements in the Set '${input}' on $(date "+%Y-%m-%d %H:%M:%S")"
1357 printf " %s\n" "---"
1358 [ -n "${set_elements}" ] && printf "%s\n" "${set_elements}" || printf " %s\n" "empty Set"
1366 # load mail template
1368 if [ -r "${ban_mailtemplate}" ]; then
1369 . "${ban_mailtemplate}"
1371 f_log "info" "no mail template"
1373 [ -z "${mail_text}" ] && f_log "info" "no mail content"
1374 [ "${ban_debug}" = "1" ] && msmtp_debug="--debug"
1378 ban_mailhead="From: ${ban_mailsender}\nTo: ${ban_mailreceiver}\nSubject: ${ban_mailtopic}\nReply-to: ${ban_mailsender}\nMime-Version: 1.0\nContent-Type: text/html;charset=utf-8\nContent-Disposition: inline\n\n"
1379 printf "%b" "${ban_mailhead}${mail_text}" | "${ban_mailcmd}" --timeout=10 ${msmtp_debug} -a "${ban_mailprofile}" "${ban_mailreceiver}" >/dev/null 2>&1
1380 f_log "info" "send status mail (${?})"
1382 f_log "debug" "f_mail ::: notification: ${ban_mailnotification}, template: ${ban_mailtemplate}, profile: ${ban_mailprofile}, receiver: ${ban_mailreceiver}, rc: ${?}"
1388 local nft_expiry line proto ip log_raw log_count rdap_log rdap_rc rdap_elements rdap_info
1390 if [ -x "${ban_logreadcmd}" ] && [ -n "${ban_logterm%%??}" ] && [ "${ban_loglimit}" != "0" ]; then
1392 f_log "info" "start detached banIP log service"
1393 [ -n "${ban_nftexpiry}" ] && nft_expiry="timeout $(printf "%s" "${ban_nftexpiry}" | "${ban_grepcmd}" -oE "([0-9]+[d|h|m|s])+$")"
1395 "${ban_logreadcmd}" -fe "${ban_logterm%%??}" 2>/dev/null |
1396 while read -r line; do
1399 ip="$(printf "%s" "${line}" | "${ban_awkcmd}" 'BEGIN{RS="(([0-9]{1,3}\\.){3}[0-9]{1,3})+"}{if(!seen[RT]++)printf "%s ",RT}')"
1400 ip="$
(f_trim
"${ip}")"
1402 [ -n "${ip}" ] && proto="v4
"
1403 if [ -z "${proto}" ]; then
1404 ip="$
(printf "%s" "${line}" | "${ban_awkcmd}" 'BEGIN{RS="([A-Fa-f0-9]{1,4}::?){3,7}[A-Fa-f0-9]{1,4}"}{if(!seen
[RT
]++)printf "%s ",RT
}')"
1405 ip="$(f_trim "${ip}")"
1407 [ -n "${ip}" ] && proto="v6"
1409 if [ -n "${proto}" ] && ! "${ban_nftcmd}" get element inet banIP blocklist"${proto}" "{ ${ip} }" >/dev/null 2>&1; then
1410 f_log "info" "suspicious IP '${ip}'"
1411 log_raw="$("${ban_logreadcmd}" -l "${ban_loglimit}" 2>/dev/null)"
1412 log_count="$(printf "%s\n" "${log_raw}" | "${ban_grepcmd}" -c "suspicious IP '${ip}'")"
1413 if [ "${log_count}" -ge "${ban_logcount}" ]; then
1414 if [ "${ban_autoblocksubnet}" = "1" ]; then
1415 rdap_log
="$("${ban_fetchcmd}" ${ban_rdapparm} "${ban_rdapfile}" "${ban_rdapurl}${ip}" 2>&1)"
1417 if [ "${rdap_rc}" = "0" ] && [ -s "${ban_rdapfile}" ]; then
1418 rdap_elements
="$(jsonfilter -i "${ban_rdapfile}" -qe '@.cidr0_cidrs.*' | awk 'BEGIN{FS="[\" ]"}{printf "%s
/%s
, ",$6,$11}')"
1419 rdap_info
="$(jsonfilter -i "${ban_rdapfile}" -qe '@.country' -qe '@.notices[@.title="Source
"].description[1]' | awk 'BEGIN{RS="";FS="\n"}{printf "%s
, %s
",$1,$2}')"
1420 if [ -n "${rdap_elements//\/*/}" ]; then
1421 if "${ban_nftcmd}" add element inet banIP "blocklist${proto}" "{ ${rdap_elements%%??} ${nft_expiry} }" >/dev
/null
2>&1; then
1422 f_log
"info" "add IP range '${rdap_elements%%??}' (source: ${rdap_info:-"-"} ::: expiry: ${ban_nftexpiry:-"-"}) to blocklist${proto} set"
1426 f_log
"info" "rdap request failed (rc: ${rdap_rc:-"-"}/log: ${rdap_log})"
1429 if [ "${ban_autoblocksubnet}" = "0" ] || [ "${rdap_rc}" != "0" ] || [ ! -s "${ban_rdapfile}" ] || [ -z "${rdap_elements//\/*/}" ]; then
1430 if "${ban_nftcmd}" add element inet banIP "blocklist${proto}" "{ ${ip} ${nft_expiry} }" >/dev
/null
2>&1; then
1431 f_log
"info" "add IP '${ip}' (expiry: ${ban_nftexpiry:-"-"}) to blocklist${proto} set"
1434 if [ -z "${ban_nftexpiry}" ] && [ "${ban_autoblocklist}" = "1" ] && ! "${ban_grepcmd}" -q "^${ip}" "${ban_blocklist}"; then
1435 printf "%-42s%s\n" "${ip}" "# added on $(date "+%Y-
%m-
%d
%H
:%M
:%S
")" >>"${ban_blocklist}"
1436 f_log
"info" "add IP '${ip}' to local blocklist"
1442 f_log
"info" "start detached no-op banIP service"
1449 if [ -r "/lib/functions.sh" ] && [ -r "/lib/functions/network.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]; then
1450 .
"/lib/functions.sh"
1451 .
"/lib/functions/network.sh"
1452 .
"/usr/share/libubox/jshn.sh"
1454 rm -rf "${ban_lock}"
1458 # check banIP availability
1461 if [ "${ban_action}" != "stop" ]; then
1462 [ ! -d "/etc/banip" ] && f_log
"err" "no banIP config directory"
1463 [ ! -r "/etc/config/banip" ] && f_log
"err" "no banIP config"
1464 [ "$(uci_get banip global ban_enabled)" = "0" ] && f_log
"err" "banIP is disabled"