-d debug
This commit is contained in:
parent
2fdc3890a3
commit
a22bad0622
1 changed files with 27 additions and 19 deletions
46
ipfn
46
ipfn
|
|
@ -1,8 +1,8 @@
|
|||
#!/bin/bash
|
||||
# Name: ipfn
|
||||
# Version: 1.2.7
|
||||
# Version: 1.2.8
|
||||
# Date: 2026-01-22
|
||||
# Description: Fixed grep errors after 'all' delete and streamlined exit paths.
|
||||
# Description: Fixed logical fall-through after all-clear to prevent grep errors.
|
||||
|
||||
if [ "$(id -u)" -ne 0 ]; then
|
||||
exec sudo "$0" "$@"
|
||||
|
|
@ -19,11 +19,11 @@ QUIET_MODE=false
|
|||
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
|
||||
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
|
||||
}
|
||||
|
||||
enable_forwarding() {
|
||||
|
|
@ -56,7 +56,7 @@ list_rules() {
|
|||
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
|
||||
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)
|
||||
|
|
@ -84,7 +84,7 @@ test_rule_by_port() {
|
|||
# --- 2. Deletion & Reset ---
|
||||
|
||||
validate_rule_handle() {
|
||||
nft -a list chain inet "${TABLE_NAME}" prerouting | grep "handle $1" | grep -q "dnat"
|
||||
nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "handle $1" | grep -q "dnat"
|
||||
}
|
||||
|
||||
all_clear() {
|
||||
|
|
@ -97,13 +97,18 @@ all_clear() {
|
|||
init_nft
|
||||
msg "All rules cleared successfully."
|
||||
list_rules
|
||||
exit 0 # 全削除したらここで終了
|
||||
exit 0 # 確実にここで終わらせる
|
||||
}
|
||||
|
||||
# --- 3. Parsing Logic ---
|
||||
|
||||
parse_delete_targets() {
|
||||
local input="$1"
|
||||
local -a final_handles=()
|
||||
|
||||
# 'all' や '*' なら即座に全削除へ(戻ってこない)
|
||||
[[ "$input" == "*" || "$input" == "all" ]] && all_clear
|
||||
|
||||
IFS=',' read -r -a parts <<< "$input"
|
||||
for part in "${parts[@]}"; do
|
||||
if [[ "$part" =~ ^:([0-9]+)$ ]]; then
|
||||
|
|
@ -121,8 +126,6 @@ parse_delete_targets() {
|
|||
echo "${final_handles[@]}"
|
||||
}
|
||||
|
||||
# --- 3. Adding Logic ---
|
||||
|
||||
add_rule() {
|
||||
init_nft
|
||||
local raw_input="$1"
|
||||
|
|
@ -149,7 +152,7 @@ add_rule() {
|
|||
[[ "$SKIP_TEST" == false ]] && { echo ""; test_rule_by_port "$lp"; }
|
||||
}
|
||||
|
||||
# --- 4. Main Parsing ---
|
||||
# --- 4. Main Parsing Loop ---
|
||||
|
||||
for arg in "$@"; do
|
||||
[[ "$arg" =~ q ]] && QUIET_MODE=true && FORCE_FLAG=true && SKIP_TEST=true
|
||||
|
|
@ -173,11 +176,11 @@ while [[ $# -gt 0 ]]; do
|
|||
if [[ "$h_str" =~ ^:?([0-9]+):?$ ]]; then
|
||||
target_port="${BASH_REMATCH[1]}"
|
||||
elif [[ -n "$h_str" ]]; then
|
||||
target_port=$(nft -a list chain inet "${TABLE_NAME}" prerouting | grep "handle $h_str" | grep -o 'dport [0-9]*' | awk '{print $2}')
|
||||
target_port=$(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "handle $h_str" | grep -o 'dport [0-9]*' | awk '{print $2}')
|
||||
else
|
||||
count=$(nft list chain inet "${TABLE_NAME}" prerouting | grep -c "dnat")
|
||||
count=$(nft list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep -c "dnat")
|
||||
if [ "$count" -eq 1 ]; then
|
||||
target_port=$(nft -a list chain inet "${TABLE_NAME}" prerouting | grep "dnat" | grep -o 'dport [0-9]*' | awk '{print $2}')
|
||||
target_port=$(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "dnat" | grep -o 'dport [0-9]*' | awk '{print $2}')
|
||||
fi
|
||||
fi
|
||||
[[ -n "$target_port" ]] && test_rule_by_port "$target_port" || msg "Error: Port/Handle not found."
|
||||
|
|
@ -185,22 +188,27 @@ while [[ $# -gt 0 ]]; do
|
|||
-*[d]*)
|
||||
h_str=$(echo "$1" | sed -E 's/^-q?d?f?//')
|
||||
[[ -z "$h_str" ]] && { h_str="$2"; shift; }
|
||||
|
||||
# parse_delete_targets の中で 'all' 判定があれば exit するので、ここには戻らない
|
||||
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/^/ /'
|
||||
nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | 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")
|
||||
ri=$(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | 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 -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue