mirror of
https://github.com/itdoginfo/podkop.git
synced 2025-12-06 03:26:51 +03:00
70 lines
1.7 KiB
Bash
70 lines
1.7 KiB
Bash
# Create an nftables table in the inet family
|
|
nft_create_table() {
|
|
local name="$1"
|
|
|
|
nft add table inet "$name"
|
|
}
|
|
|
|
# Create a set within a table for storing IPv4 addresses
|
|
nft_create_ipv4_set() {
|
|
local table="$1"
|
|
local name="$2"
|
|
|
|
nft add set inet "$table" "$name" '{ type ipv4_addr; flags interval; auto-merge; }'
|
|
}
|
|
|
|
nft_create_ifname_set() {
|
|
local table="$1"
|
|
local name="$2"
|
|
|
|
nft add set inet "$table" "$name" '{ type ifname; flags interval; }'
|
|
}
|
|
|
|
# Add one or more elements to a set
|
|
nft_add_set_elements() {
|
|
local table="$1"
|
|
local set="$2"
|
|
local elements="$3"
|
|
|
|
nft add element inet "$table" "$set" "{ $elements }"
|
|
}
|
|
|
|
nft_add_set_elements_from_file_chunked() {
|
|
local filepath="$1"
|
|
local nft_table_name="$2"
|
|
local nft_set_name="$3"
|
|
local chunk_size="${4:-5000}"
|
|
|
|
local array count
|
|
count=0
|
|
while IFS= read -r line; do
|
|
line=$(echo "$line" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
|
|
|
|
[ -z "$line" ] && continue
|
|
|
|
if ! is_ipv4 "$line" && ! is_ipv4_cidr "$line"; then
|
|
log "'$line' is not IPv4 or IPv4 CIDR" "debug"
|
|
continue
|
|
fi
|
|
|
|
if [ -z "$array" ]; then
|
|
array="$line"
|
|
else
|
|
array="$array,$line"
|
|
fi
|
|
|
|
count=$((count + 1))
|
|
|
|
if [ "$count" = "$chunk_size" ]; then
|
|
log "Adding $count elements to nft set $nft_set_name" "debug"
|
|
nft_add_set_elements "$nft_table_name" "$nft_set_name" "$array"
|
|
array=""
|
|
count=0
|
|
fi
|
|
done < "$filepath"
|
|
|
|
if [ -n "$array" ]; then
|
|
log "Adding $count elements to nft set $nft_set_name" "debug"
|
|
nft_add_set_elements "$nft_table_name" "$nft_set_name" "$array"
|
|
fi
|
|
} |