Guest User

Untitled

a guest
Mar 3rd, 2026
79
0
110 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.45 KB | None | 0 0
  1. #!/bin/bash
  2. # load-geo-sets.sh - Populate nftables geolocation sets in batches
  3. # to avoid "No buffer space available" netlink errors.
  4. #
  5. # Usage: load-geo-sets.sh [countrysets_dir]
  6. #
  7. # Run this AFTER loading the main firewall ruleset (firewall.nft),
  8. # which creates the empty au-ipv4 and au-ipv6 sets.
  9.  
  10. COUNTRYSETS_DIR="${1:-/etc/nftables/firewall/geo-nft/countrysets}"
  11. TABLE_FAMILY="inet"
  12. TABLE_NAME="filter"
  13. BATCH_SIZE=512
  14.  
  15. # Load elements from a geo-nft define file into an nftables set in batches.
  16. # Args: $1=file  $2=set_name
  17. load_set() {
  18.         local file="$1"
  19.         local set_name="$2"
  20.         local batch=""
  21.         local count=0
  22.         local total=0
  23.  
  24.         if [ ! -f "$file" ]; then
  25.                 echo "Error: $file not found" >&2
  26.                 return 1
  27.         fi
  28.  
  29.         # Extract element lines: skip comments, define line, and braces.
  30.         # Element lines start with whitespace followed by an address.
  31.         while IFS= read -r line; do
  32.                 # Strip leading/trailing whitespace and trailing comma
  33.                 element="${line#"${line%%[![:space:]]*}"}"
  34.                element="${element%,}"
  35.                element="${element%"${element##*[![:space:]]}"}"
  36.  
  37.                 [ -z "$element" ] && continue
  38.  
  39.                 if [ -z "$batch" ]; then
  40.                         batch="$element"
  41.                 else
  42.                         batch="$batch, $element"
  43.                 fi
  44.                 ((count++))
  45.                 ((total++))
  46.  
  47.                 if [ "$count" -ge "$BATCH_SIZE" ]; then
  48.                         echo "add element $TABLE_FAMILY $TABLE_NAME $set_name { $batch }" | nft -f - || {
  49.                                 echo "Error: failed adding batch to $set_name at element $total" >&2
  50.                                 return 1
  51.                         }
  52.                         batch=""
  53.                         count=0
  54.                 fi
  55.         done < <(sed -n '/^[[:space:]][[:space:]]*[0-9a-fA-F]/p' "$file")
  56.  
  57.         # Load any remaining elements
  58.         if [ -n "$batch" ]; then
  59.                 echo "add element $TABLE_FAMILY $TABLE_NAME $set_name { $batch }" | nft -f - || {
  60.                         echo "Error: failed adding final batch to $set_name at element $total" >&2
  61.                         return 1
  62.                 }
  63.         fi
  64.  
  65.         echo "Loaded $total elements into $set_name"
  66. }
  67.  
  68. load_set "$COUNTRYSETS_DIR/AU.ipv4" "au-ipv4"
  69. load_set "$COUNTRYSETS_DIR/AU.ipv6" "au-ipv6"
Advertisement
Add Comment
Please, Sign In to add comment