Rename ipfn to ipf
This commit is contained in:
parent
00c5a46278
commit
44fc5aa6ca
2 changed files with 236 additions and 224 deletions
236
ipf
Executable file
236
ipf
Executable file
|
|
@ -0,0 +1,236 @@
|
|||
#!/bin/bash
|
||||
# Name: ipfn
|
||||
# Version: 1.2.4
|
||||
# Date: 2026-01-22
|
||||
# Description: Enhanced for VM/LXD/Docker with bridge-nf tuning and robust forwarding.
|
||||
|
||||
if [ "$(id -u)" -ne 0 ]; then
|
||||
exec sudo "$0" "$@"
|
||||
fi
|
||||
|
||||
TABLE_NAME="ipf_wrapper"
|
||||
PROTO="tcp"
|
||||
FORCE_FLAG=false
|
||||
SKIP_TEST=false
|
||||
QUIET_MODE=false
|
||||
|
||||
# --- 1. Utility & Core Setup ---
|
||||
|
||||
msg() { [[ "$QUIET_MODE" == false ]] && echo -e "$@"; }
|
||||
|
||||
init_nft() {
|
||||
nft add table inet ${TABLE_NAME} 2>/dev/null
|
||||
# NAT Chains
|
||||
nft add chain inet ${TABLE_NAME} prerouting { type nat hook prerouting priority -100 \; } 2>/dev/null
|
||||
nft add chain inet ${TABLE_NAME} output { type nat hook output priority -100 \; } 2>/dev/null
|
||||
nft add chain inet ${TABLE_NAME} postrouting { type nat hook postrouting priority 100 \; } 2>/dev/null
|
||||
# Filter Chain (For VM/CT Forwarding)
|
||||
nft add chain inet ${TABLE_NAME} forward { type filter hook forward priority 0 \; policy accept \; } 2>/dev/null
|
||||
}
|
||||
|
||||
enable_forwarding() {
|
||||
msg "Optimizing kernel parameters for VM/LXD/Docker..."
|
||||
sysctl -w net.ipv4.ip_forward=1 >/dev/null
|
||||
sysctl -w net.ipv6.conf.all.forwarding=1 >/dev/null
|
||||
sysctl -w net.ipv4.conf.all.route_localnet=1 >/dev/null
|
||||
# ブリッジパケットをNetfilterに渡す設定 (存在する場合のみ)
|
||||
sysctl -w net.bridge.bridge-nf-call-iptables=1 2>/dev/null
|
||||
sysctl -w net.bridge.bridge-nf-call-ip6tables=1 2>/dev/null
|
||||
msg "\e[32mForwarding and Bridge-NF enabled.\e[0m"
|
||||
}
|
||||
|
||||
show_help() {
|
||||
echo "Usage: ipfn [OPTIONS] [RULES]"
|
||||
echo ""
|
||||
echo "Rules Format:"
|
||||
echo " 80:10.10.100.5:8080 Full (LocalPort:TargetIP:TargetPort)"
|
||||
echo " 80:8080 IP defaults to 127.0.0.1"
|
||||
echo " 11434 Map same port to 127.0.0.1"
|
||||
echo ""
|
||||
echo "Options:"
|
||||
echo " -l, -L List all rules"
|
||||
echo " -d HANDLE/:PORT/all Delete specific rules or '*' for all"
|
||||
echo " -R Reset: Clear ALL rules immediately"
|
||||
echo " -q Quiet mode (No output, Auto-yes)"
|
||||
echo " -t [HANDLE] Test connectivity"
|
||||
echo " -f Enable IP forward & Bridge tuning / Skip test"
|
||||
echo " -v Verbose (raw nftables output)"
|
||||
echo " -h Show this help"
|
||||
}
|
||||
|
||||
list_rules() {
|
||||
[[ "$QUIET_MODE" == true ]] && return
|
||||
local highlight_uuid=$1
|
||||
init_nft
|
||||
msg "Forwarding Rules (ipfn):"
|
||||
printf "%-10s %-6s %-20s %-20s %-10s\n" "HANDLE" "PROTO" "LOCAL" "TARGET" "UUID"
|
||||
echo "-----------------------------------------------------------------------------------"
|
||||
nft -a list chain inet ${TABLE_NAME} prerouting | grep "dnat" | while read -r line; do
|
||||
handle=$(echo "$line" | grep -o 'handle [0-9]*' | awk '{print $2}')
|
||||
proto=$(echo "$line" | grep -q "udp" && echo "udp" || echo "tcp")
|
||||
target=$(echo "$line" | grep -oE '([0-9.]+|\[[0-9a-fA-F:]+\]):[0-9]+' | head -n 1)
|
||||
lport=$(echo "$line" | grep -o 'dport [0-9]*' | awk '{print $2}')
|
||||
full_uuid=$(echo "$line" | grep -o 'ipf-id:[a-z0-9-]*' | cut -d':' -f2)
|
||||
if [[ -n "$highlight_uuid" && "$full_uuid" == "$highlight_uuid" ]]; then
|
||||
printf "\e[1;36m*%-9s %-6s %-20s %-20s %-10s\e[0m\n" "$handle" "$proto" ":$lport" "$target" "${full_uuid:0:8}"
|
||||
else
|
||||
printf "%-10s %-6s %-20s %-20s %-10s\n" "$handle" "$proto" ":$lport" "$target" "${full_uuid:0:8}"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
test_rule_by_port() {
|
||||
[[ "$QUIET_MODE" == true ]] && return
|
||||
local lp=$1
|
||||
echo -n "Testing connectivity to :$lp... "
|
||||
if nc -z -v -w 2 127.0.0.1 "$lp" 2>&1 | grep -iqE "succeeded|connected|open"; then
|
||||
echo -e "\e[32mOK\e[0m"
|
||||
else
|
||||
echo -e "\e[31mFAILED\e[0m"
|
||||
fi
|
||||
}
|
||||
|
||||
# --- 2. Deletion Logic ---
|
||||
|
||||
validate_rule_handle() {
|
||||
nft -a list chain inet ${TABLE_NAME} prerouting | grep "handle $1" | grep -q "dnat"
|
||||
}
|
||||
|
||||
all_clear() {
|
||||
if [[ "$FORCE_FLAG" == false ]]; then
|
||||
msg "\e[33mWarning: This will delete ALL forwarding rules.\e[0m"
|
||||
read -p "Are you sure? (y/N): " confirm
|
||||
[[ ! "$confirm" =~ ^[yY]$ ]] && exit 0
|
||||
fi
|
||||
nft delete table inet ${TABLE_NAME} 2>/dev/null
|
||||
init_nft
|
||||
msg "All rules cleared successfully."
|
||||
exit 0
|
||||
}
|
||||
|
||||
parse_delete_targets() {
|
||||
local input=$1
|
||||
local -a final_handles=()
|
||||
[[ "$input" == "*" || "$input" == "all" ]] && all_clear
|
||||
IFS=',' read -r -a parts <<< "$input"
|
||||
for part in "${parts[@]}"; do
|
||||
if [[ "$part" =~ ^:([0-9]+)$ ]]; then
|
||||
local target_port="${BASH_REMATCH[1]}"
|
||||
local found_h=$(nft -a list chain inet ${TABLE_NAME} prerouting | grep "dport $target_port" | grep -o 'handle [0-9]*' | awk '{print $2}')
|
||||
for h in $found_h; do final_handles+=("$h"); done
|
||||
elif [[ "$part" =~ ^([0-9]+)-([0-9]+)$ ]]; then
|
||||
for ((i=${BASH_REMATCH[1]}; i<=${BASH_REMATCH[2]}; i++)); do
|
||||
validate_rule_handle "$i" && final_handles+=("$i")
|
||||
done
|
||||
else
|
||||
validate_rule_handle "$part" && final_handles+=("$part")
|
||||
fi
|
||||
done
|
||||
echo "${final_handles[@]}"
|
||||
}
|
||||
|
||||
# --- 3. Adding Logic ---
|
||||
|
||||
add_rule() {
|
||||
init_nft
|
||||
local raw_input=$1
|
||||
local lp tip tp
|
||||
|
||||
if [[ "$raw_input" =~ ^([0-9]+):([0-9\.]+):([0-9]+)$ ]]; then
|
||||
lp=${BASH_REMATCH[1]}; tip=${BASH_REMATCH[2]}; tp=${BASH_REMATCH[3]}
|
||||
elif [[ "$raw_input" =~ ^([0-9]+):([0-9]+)$ ]]; then
|
||||
lp=${BASH_REMATCH[1]}; tip="127.0.0.1"; tp=${BASH_REMATCH[2]}
|
||||
elif [[ "$raw_input" =~ ^([0-9]+)$ ]]; then
|
||||
lp=${BASH_REMATCH[1]}; tip="127.0.0.1"; tp=${BASH_REMATCH[1]}
|
||||
else
|
||||
msg "\e[31mError: Invalid rule format '$raw_input'\e[0m"; exit 1
|
||||
fi
|
||||
|
||||
local raw_uuid=$(cat /proc/sys/kernel/random/uuid)
|
||||
local uuid="ipf-id:$raw_uuid"
|
||||
local family="ip"; [[ "$tip" =~ : ]] && family="ip6"
|
||||
|
||||
# DNAT
|
||||
nft add rule inet ${TABLE_NAME} prerouting "$PROTO" dport "$lp" dnat $family to "$tip:$tp" comment "\"$uuid\""
|
||||
nft add rule inet ${TABLE_NAME} output "$PROTO" dport "$lp" dnat $family to "$tip:$tp" comment "\"$uuid\""
|
||||
|
||||
# FORWARD (VM/CT対応)
|
||||
nft add rule inet ${TABLE_NAME} forward $family daddr "$tip" "$PROTO" dport "$tp" ct state new,established,related accept comment "\"$uuid\""
|
||||
nft add rule inet ${TABLE_NAME} forward $family saddr "$tip" "$PROTO" sport "$tp" ct state established,related accept comment "\"$uuid\""
|
||||
nft add rule inet ${TABLE_NAME} forward iifname "lo" accept comment "\"$uuid\""
|
||||
|
||||
# POSTROUTING (MASQUERADE: 戻りパケットの保証)
|
||||
nft add rule inet ${TABLE_NAME} postrouting $family daddr "$tip" "$PROTO" dport "$tp" masquerade comment "\"$uuid\""
|
||||
|
||||
list_rules "$raw_uuid"
|
||||
[[ "$SKIP_TEST" == false ]] && { echo ""; test_rule_by_port "$lp"; }
|
||||
}
|
||||
|
||||
# --- 4. Main Parsing ---
|
||||
|
||||
for arg in "$@"; do
|
||||
[[ "$arg" =~ q ]] && QUIET_MODE=true && FORCE_FLAG=true && SKIP_TEST=true
|
||||
[[ "$arg" =~ f ]] && FORCE_FLAG=true && SKIP_TEST=true
|
||||
[[ "$arg" == "-R" ]] && RESET_MODE=true
|
||||
done
|
||||
|
||||
if [[ "$RESET_MODE" == true ]]; then all_clear; fi
|
||||
|
||||
if [[ $# -eq 0 ]]; then list_rules; exit 0; fi
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
-h|--help) show_help; exit 0 ;;
|
||||
-v|--verbose) nft list table inet ${TABLE_NAME}; exit 0 ;;
|
||||
-L|-l) list_rules; exit 0 ;;
|
||||
-f)
|
||||
enable_forwarding
|
||||
[[ $# -eq 1 ]] && exit 0 ;;
|
||||
-t*)
|
||||
h_str="${1#-t}"
|
||||
if [[ -z "$h_str" && ( "$2" =~ ^[0-9,-]+$ ) ]]; then h_str="$2"; shift; fi
|
||||
if [[ -z "$h_str" ]]; then
|
||||
count=$(nft list chain inet ${TABLE_NAME} prerouting | grep -c "dnat")
|
||||
if [ "$count" -eq 1 ]; then
|
||||
h_str=$(nft -a list chain inet ${TABLE_NAME} prerouting | grep "handle" | awk '{print $NF}')
|
||||
else
|
||||
msg "Error: Multiple rules exist. Specify a handle."; exit 1
|
||||
fi
|
||||
fi
|
||||
lp=$(nft -a list chain inet ${TABLE_NAME} prerouting | grep "handle $h_str" | grep -o 'dport [0-9]*' | awk '{print $2}')
|
||||
[[ -n "$lp" ]] && test_rule_by_port "$lp" || msg "Error: Handle $h_str not found."
|
||||
exit 0 ;;
|
||||
-*[d]*)
|
||||
h_str=$(echo "$1" | sed -E 's/^-q?d?f?//')
|
||||
[[ -z "$h_str" ]] && { h_str="$2"; shift; }
|
||||
handles=($(parse_delete_targets "$h_str"))
|
||||
[[ ${#handles[@]} -eq 0 ]] && { msg "No valid targets."; exit 1; }
|
||||
if [[ "$FORCE_FLAG" == false ]]; then
|
||||
msg "Review rules to delete (Top-level only):"
|
||||
for h in "${handles[@]}"; do
|
||||
nft -a list chain inet ${TABLE_NAME} prerouting | grep "handle $h" | sed 's/^/ /'
|
||||
done
|
||||
read -p "Delete these rules and their sub-rules? (y/N): " confirm
|
||||
[[ ! "$confirm" =~ ^[yY]$ ]] && exit 0
|
||||
fi
|
||||
for h in "${handles[@]}"; do
|
||||
ri=$(nft -a list chain inet ${TABLE_NAME} prerouting | grep "handle $h")
|
||||
[[ -z "$ri" ]] && continue
|
||||
uuid=$(echo "$ri" | grep -o 'ipf-id:[a-z0-9-]*' | head -n 1)
|
||||
for c in prerouting output forward postrouting; do
|
||||
nft -a list chain inet ${TABLE_NAME} "$c" | grep "$uuid" | grep -o 'handle [0-9]*' | awk '{print $2}' | while read -r rh; do
|
||||
nft delete rule inet ${TABLE_NAME} "$c" handle "$rh"
|
||||
done
|
||||
done
|
||||
msg "Deleted associated with handle $h"
|
||||
done
|
||||
list_rules; exit 0 ;;
|
||||
*)
|
||||
if [[ "$1" =~ ^[0-9] ]]; then
|
||||
add_rule "$1"; exit 0
|
||||
else
|
||||
msg "\e[31mError: Unknown option '$1'\e[0m\n"; show_help; exit 1
|
||||
fi ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
224
ipfn
224
ipfn
|
|
@ -1,224 +0,0 @@
|
|||
#!/bin/bash
|
||||
# Name: ipfn
|
||||
# Version: 1.4.6
|
||||
# Date: 2026-01-22
|
||||
# Description: Restored full help message and kept fixed regex for multi-pattern testing.
|
||||
|
||||
if [ "$(id -u)" -ne 0 ]; then
|
||||
exec sudo "$0" "$@"
|
||||
fi
|
||||
|
||||
TABLE_NAME="ipf_wrapper"
|
||||
VERSION="1.4.6"
|
||||
PROTO="tcp"
|
||||
FORCE_FLAG=false
|
||||
SKIP_TEST=false
|
||||
QUIET_MODE=false
|
||||
|
||||
# --- 1. Functions ---
|
||||
|
||||
show_help() {
|
||||
echo "ipfn version ${VERSION}"
|
||||
echo "Usage: ipfn [OPTIONS] [RULES]"
|
||||
echo ""
|
||||
echo "Options:"
|
||||
echo " -l, -L List all forwarding rules"
|
||||
echo " -d HANDLE/:PORT/all Delete by handle, port (e.g. :80), or 'all'"
|
||||
echo " -R Reset: Clear ALL rules immediately"
|
||||
echo " -t [TARGET] Test connectivity (See 'Test Patterns' below)"
|
||||
echo " -f Force: No confirmation & Skip test"
|
||||
echo " -q Quiet mode: No output (implies -f)"
|
||||
echo " -h, --help Show this help message"
|
||||
echo ""
|
||||
echo "Rules Syntax:"
|
||||
echo " LPORT:TIP:TPORT Forward LPORT to TIP:TPORT (e.g. 80:10.0.0.1:80)"
|
||||
echo " LPORT:TPORT Forward LPORT to 127.0.0.1:TPORT"
|
||||
echo " LPORT Forward LPORT to 127.0.0.1:LPORT"
|
||||
echo ""
|
||||
echo "Test Patterns (-t):"
|
||||
echo " -t 5 Strict test: Verify Handle 5 (Entry -> Exit -> Counter)"
|
||||
echo " -t :80 Simple test: Check if Local port :80 is open"
|
||||
echo " -t 1.1.1.1:80 Direct test: Check if Target 1.1.1.1:80 is UP"
|
||||
echo " -t 80:1.1.1.1:80 Full test: Check both Target UP and Local :80 OPEN"
|
||||
echo ""
|
||||
echo "Note: New rules are INSERTED at the top (Latest rule takes priority)."
|
||||
}
|
||||
|
||||
msg() { [[ "$QUIET_MODE" == false ]] && echo -e "$@"; }
|
||||
|
||||
init_nft() {
|
||||
nft add table inet "${TABLE_NAME}" 2>/dev/null
|
||||
nft add chain inet "${TABLE_NAME}" prerouting { type nat hook prerouting priority -100 \; } 2>/dev/null
|
||||
nft add chain inet "${TABLE_NAME}" output { type nat hook output priority -100 \; } 2>/dev/null
|
||||
nft add chain inet "${TABLE_NAME}" postrouting { type nat hook postrouting priority 100 \; } 2>/dev/null
|
||||
nft add chain inet "${TABLE_NAME}" forward { type filter hook forward priority 0 \; policy accept \; } 2>/dev/null
|
||||
}
|
||||
|
||||
get_total_packets() {
|
||||
local uuid=$1
|
||||
local total=0
|
||||
local counts=$(nft list table inet "${TABLE_NAME}" 2>/dev/null | grep "$uuid" | grep -o 'packets [0-9]*' | awk '{print $2}')
|
||||
for c in $counts; do total=$((total + c)); done
|
||||
echo "$total"
|
||||
}
|
||||
|
||||
test_connection() {
|
||||
local ip=$1
|
||||
local port=$2
|
||||
nc -z -v -w 1 "$ip" "$port" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
test_strict_handle() {
|
||||
local h=$1
|
||||
local info=$(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "handle $h")
|
||||
[[ -z "$info" ]] && { echo -e "Handle $h \e[31mNOT FOUND\e[0m"; return; }
|
||||
|
||||
local lp=$(echo "$info" | grep -o 'dport [0-9]*' | awk '{print $2}')
|
||||
local target=$(echo "$info" | grep -oE 'to ([0-9.]+|\[[0-9a-fA-F:]+\]):[0-9]+' | awk '{print $2}')
|
||||
local tip=$(echo "$target" | cut -d':' -f1 | tr -d '[]')
|
||||
local tp=$(echo "$target" | cut -d':' -f2)
|
||||
local uuid=$(echo "$info" | grep -o 'ipf-id:[a-z0-9-]*')
|
||||
local short_id=$(echo "$uuid" | cut -d':' -f2 | cut -c1-8)
|
||||
|
||||
echo -n "Testing handle $h (Local :$lp -> Target $tip:$tp) [${short_id}]... "
|
||||
local count_before=$(get_total_packets "$uuid")
|
||||
|
||||
if test_connection "$tip" "$tp"; then
|
||||
nc -z -v -w 1 127.0.0.1 "$lp" >/dev/null 2>&1
|
||||
sleep 0.1
|
||||
local count_after=$(get_total_packets "$uuid")
|
||||
if (( count_after > count_before )); then
|
||||
echo -e "\e[32mPASSED (Rule active & Target UP)\e[0m"
|
||||
else
|
||||
echo -e "\e[33mSKIPPED (Target UP, but blocked by prior rule)\e[0m"
|
||||
fi
|
||||
else
|
||||
nc -z -v -w 1 127.0.0.1 "$lp" >/dev/null 2>&1
|
||||
sleep 0.1
|
||||
local count_after=$(get_total_packets "$uuid")
|
||||
if (( count_after > count_before )); then
|
||||
echo -e "\e[33mOFFLINE (Rule matched, but Target Down)\e[0m"
|
||||
else
|
||||
echo -e "\e[31mFAILED (Target Down & Rule blocked)\e[0m"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
list_rules() {
|
||||
[[ "$QUIET_MODE" == true ]] && return
|
||||
local highlight_uuid=$1
|
||||
init_nft
|
||||
msg "Forwarding Rules (ipfn):"
|
||||
printf "%-10s %-6s %-20s %-20s %-10s\n" "HANDLE" "PROTO" "LOCAL" "TARGET" "UUID"
|
||||
echo "-----------------------------------------------------------------------------------"
|
||||
nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "dnat" | while read -r line; do
|
||||
handle=$(echo "$line" | grep -o 'handle [0-9]*' | awk '{print $2}')
|
||||
proto=$(echo "$line" | grep -q "udp" && echo "udp" || echo "tcp")
|
||||
target=$(echo "$line" | grep -oE '([0-9.]+|\[[0-9a-fA-F:]+\]):[0-9]+' | head -n 1)
|
||||
lport=$(echo "$line" | grep -o 'dport [0-9]*' | awk '{print $2}')
|
||||
full_uuid=$(echo "$line" | grep -o 'ipf-id:[a-z0-9-]*' | cut -d':' -f2)
|
||||
if [[ -n "$highlight_uuid" && "$full_uuid" == "$highlight_uuid" ]]; then
|
||||
printf "\e[1;36m*%-9s %-6s %-20s %-20s %-10s\e[0m\n" "$handle" "$proto" ":$lport" "$target" "${full_uuid:0:8}"
|
||||
else
|
||||
printf " %-9s %-6s %-20s %-20s %-10s\n" "$handle" "$proto" ":$lport" "$target" "${full_uuid:0:8}"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
all_clear() {
|
||||
if [[ "$FORCE_FLAG" == false ]]; then
|
||||
echo -e "\e[33mWarning: This will delete ALL forwarding rules.\e[0m"
|
||||
read -p "Are you sure? (y/N): " confirm
|
||||
[[ ! "$confirm" =~ ^[yY]$ ]] && exit 0
|
||||
fi
|
||||
nft delete table inet "${TABLE_NAME}" 2>/dev/null
|
||||
init_nft; msg "All rules cleared successfully."; list_rules; exit 0
|
||||
}
|
||||
|
||||
# --- 2. Flag Pre-processing ---
|
||||
|
||||
for arg in "$@"; do
|
||||
[[ "$arg" =~ q ]] && QUIET_MODE=true && FORCE_FLAG=true && SKIP_TEST=true
|
||||
[[ "$arg" =~ f ]] && FORCE_FLAG=true && SKIP_TEST=true
|
||||
[[ "$arg" == "-R" ]] && RESET_MODE=true
|
||||
[[ "$arg" == "--version" ]] && { echo "ipfn version ${VERSION}"; exit 0; }
|
||||
done
|
||||
|
||||
if [[ "$RESET_MODE" == true ]]; then all_clear; fi
|
||||
if [[ $# -eq 0 ]]; then list_rules; exit 0; fi
|
||||
|
||||
# --- 3. Main Loop ---
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
-h|--help) show_help; exit 0 ;;
|
||||
-L|-l) list_rules; exit 0 ;;
|
||||
-*[d]*)
|
||||
h_str=$(echo "$1" | sed -E 's/^-q?d?f?//')
|
||||
[[ -z "$h_str" ]] && { h_str="$2"; shift; }
|
||||
if [[ "$h_str" == "all" || "$h_str" == "*" ]]; then all_clear; fi
|
||||
handles=()
|
||||
IFS=',' read -r -a parts <<< "$h_str"
|
||||
for part in "${parts[@]}"; do
|
||||
if [[ "$part" =~ ^:([0-9]+)$ ]]; then
|
||||
p="${BASH_REMATCH[1]}"
|
||||
for h in $(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "dport $p" | grep -o 'handle [0-9]*' | awk '{print $2}'); do handles+=("$h"); done
|
||||
else handles+=("$part"); fi
|
||||
done
|
||||
for h in "${handles[@]}"; do
|
||||
uuid=$(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "handle $h" | grep -o 'ipf-id:[a-z0-9-]*')
|
||||
[[ -z "$uuid" ]] && continue
|
||||
for c in prerouting output forward postrouting; do
|
||||
nft -a list chain inet "${TABLE_NAME}" "$c" 2>/dev/null | grep "$uuid" | grep -o 'handle [0-9]*' | awk '{print $2}' | while read -r rh; do
|
||||
nft delete rule inet "${TABLE_NAME}" "$c" handle "$rh"
|
||||
done
|
||||
done
|
||||
msg "Deleted handle $h"
|
||||
done
|
||||
list_rules; exit 0 ;;
|
||||
-t*)
|
||||
h_str="${1#-t}"; [[ -z "$h_str" && -n "$2" ]] && { h_str="$2"; shift; }
|
||||
if [[ "$h_str" =~ ^([0-9]+):([0-9\.]+):([0-9]+)$ ]]; then
|
||||
lp=${BASH_REMATCH[1]}; tip=${BASH_REMATCH[2]}; tp=${BASH_REMATCH[3]}
|
||||
echo -n "Checking Target $tip:$tp... "
|
||||
test_connection "$tip" "$tp" && echo -e "\e[32mUP\e[0m" || echo -e "\e[31mDOWN\e[0m"
|
||||
echo -n "Checking Local :$lp... "
|
||||
nc -z -v -w 1 127.0.0.1 "$lp" >/dev/null 2>&1 && echo -e "\e[32mOPEN\e[0m" || echo -e "\e[31mCLOSED\e[0m"
|
||||
elif [[ "$h_str" =~ ^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+):([0-9]+)$ ]]; then
|
||||
tip=${BASH_REMATCH[1]}; tp=${BASH_REMATCH[2]}
|
||||
echo -n "Checking Target $tip:$tp... "
|
||||
test_connection "$tip" "$tp" && echo -e "\e[32mUP\e[0m" || echo -e "\e[31mDOWN\e[0m"
|
||||
elif [[ "$h_str" =~ ^:([0-9]+)$ || "$h_str" =~ ^([0-9]+):$ ]]; then
|
||||
p="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
|
||||
echo -n "Checking Local :$p... "
|
||||
nc -z -v -w 1 127.0.0.1 "$p" >/dev/null 2>&1 && echo -e "\e[32mOK\e[0m" || echo -e "\e[31mOFFLINE\e[0m"
|
||||
elif [[ "$h_str" =~ ^[0-9]+$ ]]; then
|
||||
test_strict_handle "$h_str"
|
||||
else
|
||||
top_h=$(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "dnat" | head -n 1 | grep -o 'handle [0-9]*' | awk '{print $2}')
|
||||
[[ -n "$top_h" ]] && test_strict_handle "$top_h" || msg "Error: No rules to test."
|
||||
fi
|
||||
exit 0 ;;
|
||||
[0-9]*)
|
||||
init_nft; raw="$1"
|
||||
if [[ "$raw" =~ ^([0-9]+):([0-9\.]+):([0-9]+)$ ]]; then lp=${BASH_REMATCH[1]}; tip=${BASH_REMATCH[2]}; tp=${BASH_REMATCH[3]}
|
||||
elif [[ "$raw" =~ ^([0-9]+):([0-9]+)$ ]]; then lp=${BASH_REMATCH[1]}; tip="127.0.0.1"; tp=${BASH_REMATCH[2]}
|
||||
elif [[ "$raw" =~ ^([0-9]+)$ ]]; then lp=${BASH_REMATCH[1]}; tip="127.0.0.1"; tp=${BASH_REMATCH[1]}
|
||||
fi
|
||||
u_raw=$(cat /proc/sys/kernel/random/uuid); u="ipf-id:$u_raw"; fam="ip"; [[ "$tip" =~ : ]] && fam="ip6"
|
||||
nft insert rule inet "${TABLE_NAME}" prerouting "$PROTO" dport "$lp" counter dnat $fam to "$tip:$tp" comment "\"$u\""
|
||||
nft insert rule inet "${TABLE_NAME}" output "$PROTO" dport "$lp" counter dnat $fam to "$tip:$tp" comment "\"$u\""
|
||||
nft insert rule inet "${TABLE_NAME}" forward $fam daddr "$tip" "$PROTO" dport "$tp" ct state new,established,related accept comment "\"$u\""
|
||||
nft insert rule inet "${TABLE_NAME}" forward $fam saddr "$tip" "$PROTO" sport "$tp" ct state established,related accept comment "\"$u\""
|
||||
nft insert rule inet "${TABLE_NAME}" forward iifname "lo" accept comment "\"$u\""
|
||||
nft insert rule inet "${TABLE_NAME}" postrouting $fam daddr "$tip" "$PROTO" dport "$tp" masquerade comment "\"$u\""
|
||||
list_rules "$u_raw"
|
||||
if [[ "$SKIP_TEST" == false ]]; then
|
||||
new_h=$(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "$u_raw" | grep -o 'handle [0-9]*' | awk '{print $2}')
|
||||
echo ""; test_strict_handle "$new_h"
|
||||
fi
|
||||
exit 0 ;;
|
||||
*) msg "Error: Unknown option '$1'"; exit 1 ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
Loading…
Reference in a new issue