ipf/ipfn1.0
joe a1abd44d49 ポート番号で消す: ./ipfn -d :80
複数のポートを一気に消す: ./ipfn -d :80,:443,:8080
    ハンドルとポートを混ぜる: ./ipfn -d 17,:8080,20-22
    無言でポート削除: ./ipfn -qd :80
    ポート番号の頭に : (コロン) を付けるだけで、スクリプトが裏側で「ハンドル番号への変換」を行ってから削除を実行します。
2026-01-22 12:02:08 +09:00

108 lines
4.3 KiB
Bash
Executable file

#!/bin/bash
# Name: ipfn
# Version: 1.1.5
# Date: 2026-01-22
# Description: Supports deleting by port (e.g., -d :80).
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
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
}
# --- 削除解析の心臓部 ---
parse_delete_targets() {
local input=$1
local -a final_handles=()
IFS=',' read -r -a parts <<< "$input"
for part in "${parts[@]}"; do
if [[ "$part" =~ ^:([0-9]+)$ ]]; then
# ポート指定 (:80) の場合、ハンドルを逆引き
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
elif [[ "$part" =~ ^([0-9]+)-([0-9]+)$ ]]; then
# 範囲指定
for ((i=${BASH_REMATCH[1]}; i<=${BASH_REMATCH[2]}; i++)); do final_handles+=("$i"); done
else
# 単一ハンドル
final_handles+=("$part")
fi
done
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]"
echo ""
echo "Options:"
echo " -d HANDLE/PORT Delete by handle(17) or port(:80). Supports list(17,:80) or range(10-15)"
echo " -df HANDLE/PORT Force delete"
echo " -qd HANDLE/PORT Quiet delete"
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
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 ;;
-*[qd]*)
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 handles to delete."; exit 1; }
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
[[ ! "$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 rule associated with handle $h"
done
list_rules; exit 0 ;;
# (中略: -t, -f, add_rule の case 処理は ver.1.1.4 と同じ)
*)
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