ipset: rewrite IPSET_HOOK logic. now scripts outputs ip addresses

This commit is contained in:
bol-van 2022-03-17 13:26:06 +03:00
parent 027b3818c3
commit e079e3a607
3 changed files with 28 additions and 15 deletions

4
config
View File

@ -13,8 +13,8 @@ SET_MAXELEM=522288
# too low hashsize can cause memory allocation errors on low RAM systems , even if RAM is enough
# too large hashsize will waste lots of RAM
IPSET_OPT="hashsize 262144 maxelem $SET_MAXELEM"
# create_ipset hook
#IPSET_POST_HOOK="/etc/zapret.ipset.hook"
# dynamically generate additional ip. $1 = ipset/nfset/table name
#IPSET_HOOK="/etc/zapret.ipset.hook"
# options for ip2net. "-4" or "-6" auto added by ipset create script
IP2NET_OPT4="--prefix-length=22-30 --v4-threshold=3/4"

View File

@ -158,6 +158,9 @@ NFQUEUE работает без изменений.
Рекомендуется версия nft 1.0.2 или выше.
Относительно старые версии ядра и/или утилиты nft могут вызывать ошибки.
В частности, на ubuntu 18.04 с ядром 4.15 будут проблемы. В 20.04 - работает.
Когда это работать не будет
---------------------------
@ -945,6 +948,10 @@ TMPDIR=/opt/zapret/tmp
SET_MAXELEM=262144
IPSET_OPT="hashsize 262144 maxelem 2097152"
Хук, позволяющий внести ip адреса динамически. $1 = имя таблицы
Адреса выводятся в stdout. В случае nfset автоматически решается проблема возможного пересечения интервалов.
IPSET_HOOK="/etc/zapret.ipset.hook"
ПРО РУГАНЬ в dmesg по поводу нехватки памяти.
Может так случиться, что памяти в системе достаточно, но при попытке заполнить огромный ipset
ядро начинает громко ругаться, ipset заполняется не полностью.

View File

@ -19,6 +19,7 @@ NFSET_TEMP="$TMPDIR/nfset_temp.txt"
NFSET_SAVERAM_MIN_FILESIZE=16384
NFSET_SAVERAM_CHUNK_SIZE=1000
IPSET_HOOK_TEMP="$TMPDIR/ipset_hook.txt"
while [ -n "$1" ]; do
[ "$1" = "no-update" ] && NO_UPDATE=1
@ -53,10 +54,15 @@ ipset_restore_chunked()
ipset_get_script()
{
# $1 - ipset name
sed -nEe "s/^.+$/add $1 &/p"
}
ipset_get_script_from_file()
{
# $1 - filename
# $2 - ipset name
zzcat "$1" | sort -u | sed -nEe "s/^.+$/add $2 &/p"
zzcat "$1" | sort -u | ipset_get_script $2
}
ipset_restore()
{
@ -75,11 +81,11 @@ ipset_restore()
echo $T
if [ "$svram" = "1" ]; then
ipset_get_script "$2" "$1" >"$IPSET_CMD"
ipset_get_script_from_file "$2" "$1" >"$IPSET_CMD"
ipset_restore_chunked "$IPSET_CMD" $IPSET_SAVERAM_CHUNK_SIZE
rm -f "$IPSET_CMD"
else
ipset_get_script "$2" "$1" | ipset -! restore
ipset_get_script_from_file "$2" "$1" | ipset -! restore
fi
}
create_ipset()
@ -97,7 +103,7 @@ create_ipset()
for f in "$5" "$6" ; do
ipset_restore "$2" "$f"
done
ipset_post_hook "$2"
[ -n "$IPSET_HOOK" ] && $IPSET_HOOK $2 | ipset_get_script $2 | ipset -! restore
}
return 0
}
@ -139,7 +145,13 @@ nfset_restore()
# $2,$3,... - filenames
echo "Adding to nfset $1 : $2 $3 $4 $5"
nfset_get_script_multi "$@" | nft -f -
local hookfile
[ -n "$IPSET_HOOK" ] && {
$IPSET_HOOK $1 >"$IPSET_HOOK_TEMP"
[ -s "$IPSET_HOOK_TEMP" ] && hookfile=$IPSET_HOOK_TEMP
}
nfset_get_script_multi "$@" $hookfile | nft -f -
rm -f "$IPSET_HOOK_TEMP"
}
create_nfset()
{
@ -156,7 +168,6 @@ create_nfset()
}
[ "$DO_CLEAR" = "1" ] || {
nfset_restore $2 $4 $5
ipset_post_hook "$2"
}
return 0
}
@ -192,7 +203,7 @@ create_ipfw_table()
populate_ipfw_table $name "$1"
shift
done
ipset_post_hook $name
[ -n "$IPSET_HOOK" ] && $IPSET_HOOK $name | add_ipfw_table $name
}
return 0
}
@ -211,11 +222,6 @@ print_reloading_backend()
echo $s
}
ipset_post_hook()
{
[ -n "$IPSET_POST_HOOK" ] && $IPSET_POST_HOOK "$1"
}
oom_adjust_high
get_fwtype
@ -227,7 +233,7 @@ if [ -n "$LISTS_RELOAD" ] ; then
else
echo executing custom ip list reload command : $LISTS_RELOAD
$LISTS_RELOAD
ipset_post_hook
[ -n "$IPSET_HOOK" ] && $IPSET_HOOK
fi
else
case "$FWTYPE" in