各種オプション評価部分BUG取り
-d63-69 と指定した際に、間の「サブグループのハンドル(64-68)」を無視して、ちゃんと転送設定の親玉である 63 と 69 だけを検出し、そこから芋づる式に関連ルールを消去できています。表示もスッキリして、ようやく「道具」として信頼できるレベルになった気がします。 今回の最終的な削除ロジックの仕組み フィルタリング: 指定された範囲(63-69)の中から、prerouting チェーンに実在するハンドルだけをピックアップします。 UUIDの特定: 見つかったハンドルのコメント欄から固有の ipf-id を抜き出します。 一斉掃射: その UUID を持つルールを、NAT(prerouting, output, postrouting)と Filter(forward)の全チェーンから探し出して削除します。 さらに「調べる」際のおすすめテスト もしお時間あれば、こんなパターンも試してみてください: ポート指定削除: ./ipfn -d :11434 (ハンドル番号を調べずにポートで消せるか) 混合削除: ./ipfn -d :80,33 (ポートとハンドルを混ぜて消せるか) 静かな削除: ./ipfn -qd :80 (何も言わずに一瞬で消え去るか)
This commit is contained in:
parent
df59b6f1c4
commit
79d11fecf7
1 changed files with 88 additions and 23 deletions
111
ipfn1.0
111
ipfn1.0
|
|
@ -1,8 +1,8 @@
|
|||
#!/bin/bash
|
||||
# Name: ipfn
|
||||
# Version: 1.1.6
|
||||
# Version: 1.1.9
|
||||
# Date: 2026-01-22
|
||||
# Description: Fixed argument parsing order and combined flags.
|
||||
# Description: Fixed syntax error and finalized safety guards.
|
||||
|
||||
if [ "$(id -u)" -ne 0 ]; then
|
||||
exec sudo "$0" "$@"
|
||||
|
|
@ -14,6 +14,8 @@ FORCE_FLAG=false
|
|||
SKIP_TEST=false
|
||||
QUIET_MODE=false
|
||||
|
||||
# --- 1. Utility Functions ---
|
||||
|
||||
msg() { [[ "$QUIET_MODE" == false ]] && echo -e "$@"; }
|
||||
|
||||
init_nft() {
|
||||
|
|
@ -24,7 +26,55 @@ init_nft() {
|
|||
nft add chain inet ${TABLE_NAME} forward { type filter hook forward priority 0 \; policy accept \; } 2>/dev/null
|
||||
}
|
||||
|
||||
# (parse_delete_targets, list_rules, test_rule_by_port, add_rule などは 1.1.5 と同様)
|
||||
show_help() {
|
||||
echo "Usage: ipfn [OPTIONS] [PORT:TARGET_IP:TARGET_PORT]"
|
||||
echo ""
|
||||
echo "Options:"
|
||||
echo " -l, -L List all rules"
|
||||
echo " -d HANDLE/:PORT Delete by handle or port (e.g., -d 17 or -d :80)"
|
||||
echo " -df, -qd Force/Quiet delete"
|
||||
echo " -t [HANDLE] Test connectivity"
|
||||
echo " -f Skip test / Enable IP forward"
|
||||
echo " -v Show raw nftables rules"
|
||||
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-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
|
||||
}
|
||||
|
||||
validate_prerouting_handle() {
|
||||
nft -a list chain inet ${TABLE_NAME} prerouting | grep -q "handle $1"
|
||||
}
|
||||
|
||||
parse_delete_targets() {
|
||||
local input=$1
|
||||
local -a final_handles=()
|
||||
|
|
@ -33,23 +83,40 @@ parse_delete_targets() {
|
|||
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}')
|
||||
if [[ -n "$found_h" ]]; then
|
||||
for h in $found_h; do final_handles+=("$h"); done
|
||||
else
|
||||
msg "Warning: No rules found for port :$target_port"
|
||||
fi
|
||||
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 final_handles+=("$i"); done
|
||||
for ((i=${BASH_REMATCH[1]}; i<=${BASH_REMATCH[2]}; i++)); do
|
||||
validate_prerouting_handle "$i" && final_handles+=("$i")
|
||||
done
|
||||
else
|
||||
final_handles+=("$part")
|
||||
validate_prerouting_handle "$part" && final_handles+=("$part")
|
||||
fi
|
||||
done
|
||||
echo "${final_handles[@]}"
|
||||
}
|
||||
|
||||
# --- メインパース (順序を修正) ---
|
||||
add_rule() {
|
||||
init_nft
|
||||
local lp=$(echo "$1" | cut -d':' -f1)
|
||||
local tip=$(echo "$1" | cut -d':' -f2)
|
||||
local tp=$(echo "$1" | cut -d':' -f3)
|
||||
[[ "$QUIET_MODE" == false ]] && nft list chain inet ${TABLE_NAME} prerouting | grep -q "dport $lp" && msg "\e[33mWarning: Port :$lp is already in use.\e[0m"
|
||||
local raw_uuid=$(cat /proc/sys/kernel/random/uuid)
|
||||
local uuid="ipf-id:$raw_uuid"
|
||||
local family="ip"; [[ "$tip" =~ : ]] && family="ip6"
|
||||
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\""
|
||||
nft add rule inet ${TABLE_NAME} forward iifname "lo" accept comment "\"$uuid\""
|
||||
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} postrouting $family daddr "$tip" "$PROTO" dport "$tp" masquerade comment "\"$uuid\""
|
||||
list_rules "$raw_uuid"
|
||||
[[ "$SKIP_TEST" == false ]] && { echo ""; test_rule_by_port "$lp"; }
|
||||
}
|
||||
|
||||
# 先読みフラグ
|
||||
# --- 2. Main Logic ---
|
||||
|
||||
# Pre-scan flags
|
||||
for arg in "$@"; do
|
||||
[[ "$arg" =~ q ]] && QUIET_MODE=true && FORCE_FLAG=true && SKIP_TEST=true
|
||||
[[ "$arg" =~ f ]] && FORCE_FLAG=true && SKIP_TEST=true
|
||||
|
|
@ -62,8 +129,6 @@ while [[ $# -gt 0 ]]; do
|
|||
-h|--help) show_help; exit 0 ;;
|
||||
-v|--verbose) nft list table inet ${TABLE_NAME}; exit 0 ;;
|
||||
-L|-l) list_rules; exit 0 ;;
|
||||
|
||||
# 先に単独の -f や -t を処理する
|
||||
-f)
|
||||
if [[ $# -eq 1 ]]; then
|
||||
sysctl -w net.ipv4.ip_forward=1 >/dev/null
|
||||
|
|
@ -81,21 +146,22 @@ while [[ $# -gt 0 ]]; do
|
|||
msg "Error: Multiple rules exist. Specify a handle."; exit 1
|
||||
fi
|
||||
fi
|
||||
# get_port_by_handle ロジック (nftからポートを取得)
|
||||
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 targets to delete."; exit 1; }
|
||||
if [[ ${#handles[@]} -eq 0 ]]; then
|
||||
msg "No valid prerouting handles found."; exit 1
|
||||
fi
|
||||
if [[ "$FORCE_FLAG" == false ]]; then
|
||||
msg "Review rules to delete:"
|
||||
for h in "${handles[@]}"; do nft -a list table inet ${TABLE_NAME} | grep "handle $h" | sed 's/^/ /'; done
|
||||
read -p "Delete these rules? (y/N): " confirm
|
||||
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
|
||||
|
|
@ -107,10 +173,9 @@ while [[ $# -gt 0 ]]; do
|
|||
nft delete rule inet ${TABLE_NAME} "$c" handle "$rh"
|
||||
done
|
||||
done
|
||||
msg "Deleted handle $h"
|
||||
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 ;;
|
||||
|
|
|
|||
Loading…
Reference in a new issue