ipf/ipfn1.0
2026-01-22 12:05:14 +09:00

119 lines
4.9 KiB
Bash
Executable file

#!/bin/bash
# Name: ipfn
# Version: 1.1.6
# Date: 2026-01-22
# Description: Fixed argument parsing order and combined flags.
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, list_rules, test_rule_by_port, add_rule などは 1.1.5 と同様)
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
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[@]}"
}
# --- メインパース (順序を修正) ---
# 先読みフラグ
for arg in "$@"; do
[[ "$arg" =~ q ]] && QUIET_MODE=true && FORCE_FLAG=true && SKIP_TEST=true
[[ "$arg" =~ f ]] && FORCE_FLAG=true && SKIP_TEST=true
done
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 や -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?//')
[[ -z "$h_str" ]] && { h_str="$2"; shift; }
handles=($(parse_delete_targets "$h_str"))
[[ ${#handles[@]} -eq 0 ]] && { msg "No targets 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 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