オプション指定BUG

This commit is contained in:
joe 2026-01-22 12:05:14 +09:00
parent a1abd44d49
commit df59b6f1c4

69
ipfn1.0
View file

@ -1,8 +1,8 @@
#!/bin/bash #!/bin/bash
# Name: ipfn # Name: ipfn
# Version: 1.1.5 # Version: 1.1.6
# Date: 2026-01-22 # Date: 2026-01-22
# Description: Supports deleting by port (e.g., -d :80). # Description: Fixed argument parsing order and combined flags.
if [ "$(id -u)" -ne 0 ]; then if [ "$(id -u)" -ne 0 ]; then
exec sudo "$0" "$@" exec sudo "$0" "$@"
@ -24,14 +24,13 @@ init_nft() {
nft add chain inet ${TABLE_NAME} forward { type filter hook forward priority 0 \; policy accept \; } 2>/dev/null 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 と同様)
parse_delete_targets() { parse_delete_targets() {
local input=$1 local input=$1
local -a final_handles=() local -a final_handles=()
IFS=',' read -r -a parts <<< "$input" IFS=',' read -r -a parts <<< "$input"
for part in "${parts[@]}"; do for part in "${parts[@]}"; do
if [[ "$part" =~ ^:([0-9]+)$ ]]; then if [[ "$part" =~ ^:([0-9]+)$ ]]; then
# ポート指定 (:80) の場合、ハンドルを逆引き
local target_port="${BASH_REMATCH[1]}" 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}') 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 if [[ -n "$found_h" ]]; then
@ -40,53 +39,65 @@ parse_delete_targets() {
msg "Warning: No rules found for port :$target_port" msg "Warning: No rules found for port :$target_port"
fi fi
elif [[ "$part" =~ ^([0-9]+)-([0-9]+)$ ]]; then 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 final_handles+=("$i"); done
else else
# 単一ハンドル
final_handles+=("$part") final_handles+=("$part")
fi fi
done done
echo "${final_handles[@]}" echo "${final_handles[@]}"
} }
# (中略: show_help, list_rules, test_rule_by_port, add_rule は ver.1.1.4 と同様) # --- メインパース (順序を修正) ---
show_help() { # 先読みフラグ
echo "Usage: ipfn [OPTIONS] [PORT:TARGET_IP:TARGET_PORT]" for arg in "$@"; do
echo "" [[ "$arg" =~ q ]] && QUIET_MODE=true && FORCE_FLAG=true && SKIP_TEST=true
echo "Options:" [[ "$arg" =~ f ]] && FORCE_FLAG=true && SKIP_TEST=true
echo " -d HANDLE/PORT Delete by handle(17) or port(:80). Supports list(17,:80) or range(10-15)" done
echo " -df HANDLE/PORT Force delete"
echo " -qd HANDLE/PORT Quiet delete" if [[ $# -eq 0 ]]; then list_rules; exit 0; fi
echo " -l List rules"
echo " -t [HANDLE] Test connectivity"
echo " -f Skip test / Enable IP forward"
echo " -v Verbose (raw nftables)"
echo " -h Help"
}
# --- 以下、削除ロジックの統合 ---
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case "$1" in case "$1" in
-h|--help) show_help; exit 0 ;; -h|--help) show_help; exit 0 ;;
-v|--verbose) nft list table inet ${TABLE_NAME}; exit 0 ;; -v|--verbose) nft list table inet ${TABLE_NAME}; exit 0 ;;
-L|-l) list_rules; exit 0 ;; -L|-l) list_rules; exit 0 ;;
-*[qd]*)
# 先に単独の -f や -t を処理する
-f)
if [[ $# -eq 1 ]]; then
sysctl -w net.ipv4.ip_forward=1 >/dev/null
sysctl -w net.ipv4.conf.all.route_localnet=1 >/dev/null
msg "Kernel parameters updated."; exit 0
fi ;;
-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
# 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?//') h_str=$(echo "$1" | sed -E 's/^-q?d?f?//')
[[ -z "$h_str" ]] && { h_str="$2"; shift; } [[ -z "$h_str" ]] && { h_str="$2"; shift; }
# ポートとハンドルを統合して解析
handles=($(parse_delete_targets "$h_str")) handles=($(parse_delete_targets "$h_str"))
[[ ${#handles[@]} -eq 0 ]] && { msg "No handles to delete."; exit 1; } [[ ${#handles[@]} -eq 0 ]] && { msg "No targets to delete."; exit 1; }
if [[ "$FORCE_FLAG" == false ]]; then if [[ "$FORCE_FLAG" == false ]]; then
msg "Review rules to delete:" msg "Review rules to delete:"
for h in "${handles[@]}"; do nft -a list table inet ${TABLE_NAME} | grep "handle $h" | sed 's/^/ /'; done 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 read -p "Delete these rules? (y/N): " confirm
[[ ! "$confirm" =~ ^[yY]$ ]] && exit 0 [[ ! "$confirm" =~ ^[yY]$ ]] && exit 0
fi fi
for h in "${handles[@]}"; do 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 | grep "handle $h")
[[ -z "$ri" ]] && continue [[ -z "$ri" ]] && continue
@ -96,10 +107,10 @@ while [[ $# -gt 0 ]]; do
nft delete rule inet ${TABLE_NAME} "$c" handle "$rh" nft delete rule inet ${TABLE_NAME} "$c" handle "$rh"
done done
done done
msg "Deleted rule associated with handle $h" msg "Deleted handle $h"
done done
list_rules; exit 0 ;; list_rules; exit 0 ;;
# (中略: -t, -f, add_rule の case 処理は ver.1.1.4 と同じ)
*) *)
if [[ "$1" =~ ^[0-9]+: ]]; then add_rule "$1"; 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 ;; else msg "\e[31mError: Unknown option '$1'\e[0m\n"; show_help; exit 1; fi ;;