BUGBUGBUG

This commit is contained in:
joe 2026-01-22 15:18:56 +09:00
parent faae2cbde7
commit 229f756994

185
ipfn
View file

@ -1,21 +1,35 @@
#!/bin/bash #!/bin/bash
# Name: ipfn # Name: ipfn
# Version: 1.3.5 # Version: 1.4.3
# Date: 2026-01-22 # Date: 2026-01-22
# Description: Smart testing for multi-target ports and fixed UUID listing format. # Description: Register rules first, then report the "honest" status of the connection.
if [ "$(id -u)" -ne 0 ]; then if [ "$(id -u)" -ne 0 ]; then
exec sudo "$0" "$@" exec sudo "$0" "$@"
fi fi
TABLE_NAME="ipf_wrapper" TABLE_NAME="ipf_wrapper"
VERSION="1.3.5" VERSION="1.4.3"
PROTO="tcp" PROTO="tcp"
FORCE_FLAG=false FORCE_FLAG=false
SKIP_TEST=false SKIP_TEST=false
QUIET_MODE=false QUIET_MODE=false
# --- 1. Utility & Core --- # --- 1. Functions ---
show_help() {
echo "ipfn version ${VERSION}"
echo "Usage: ipfn [OPTIONS] [RULES]"
echo ""
echo "Options:"
echo " -l, -L List all rules"
echo " -d HANDLE/:PORT/all Delete by handle, port (:80), or 'all'"
echo " -R Reset: Clear ALL rules immediately"
echo " -t [HANDLE / :PORT] Test connectivity (Check both rule and target)"
echo " -f Force: No confirmation & Skip test"
echo " -q Quiet mode (No output)"
echo " -h, --help Show help"
}
msg() { [[ "$QUIET_MODE" == false ]] && echo -e "$@"; } msg() { [[ "$QUIET_MODE" == false ]] && echo -e "$@"; }
@ -27,6 +41,63 @@ 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
} }
get_total_packets() {
local uuid=$1
local total=0
local counts=$(nft list table inet "${TABLE_NAME}" 2>/dev/null | grep "$uuid" | grep -o 'packets [0-9]*' | awk '{print $2}')
for c in $counts; do total=$((total + c)); done
echo "$total"
}
# 登録は完了させた上で、テスト結果だけ「正直に」出す
test_strict_handle() {
local h=$1
local silent=$2
local info=$(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "handle $h")
[[ -z "$info" ]] && { [[ "$silent" != "true" ]] && echo -e "Handle $h \e[31mNOT FOUND\e[0m"; return; }
local lp=$(echo "$info" | grep -o 'dport [0-9]*' | awk '{print $2}')
local uuid=$(echo "$info" | grep -o 'ipf-id:[a-z0-9-]*')
local short_id=$(echo "$uuid" | cut -d':' -f2 | cut -c1-8)
[[ "$silent" != "true" ]] && echo -n "Testing handle $h (:$lp) [${short_id}]... " || echo -n "Verifying handle $h on :$lp... "
local count_before=$(get_total_packets "$uuid")
# 接続確認 (ターゲットが落ちていてもOK、ただの結果報告)
local connect_ok=false
nc -z -v -w 1 127.0.0.1 "$lp" >/dev/null 2>&1 && connect_ok=true
sleep 0.1
local count_after=$(get_total_packets "$uuid")
if [[ "$connect_ok" == true ]]; then
if (( count_after > count_before )); then
echo -e "\e[32mPASSED (Rule matched & Connected)\e[0m"
else
echo -e "\e[33mSKIPPED (Connected, but via prior rule)\e[0m"
fi
else
# 接続できなかった場合
if (( count_after > count_before )); then
# 登録作業は成功しているが、ターゲットが未起動
echo -e "\e[33mOFFLINE (Rule matched, but Target Down)\e[0m"
else
echo -e "\e[31mFAILED (Blocked & No Response)\e[0m"
fi
fi
}
test_port_simple() {
local p=$1
echo -n "Testing :$p... "
if nc -z -v -w 1 127.0.0.1 "$p" 2>&1 | grep -iqE "succeeded|connected|open"; then
echo -e "\e[32mOK\e[0m"
else
echo -e "\e[31mOFFLINE (No response)\e[0m"
fi
}
list_rules() { list_rules() {
[[ "$QUIET_MODE" == true ]] && return [[ "$QUIET_MODE" == true ]] && return
local highlight_uuid=$1 local highlight_uuid=$1
@ -40,8 +111,6 @@ list_rules() {
target=$(echo "$line" | grep -oE '([0-9.]+|\[[0-9a-fA-F:]+\]):[0-9]+' | head -n 1) target=$(echo "$line" | grep -oE '([0-9.]+|\[[0-9a-fA-F:]+\]):[0-9]+' | head -n 1)
lport=$(echo "$line" | grep -o 'dport [0-9]*' | awk '{print $2}') 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) full_uuid=$(echo "$line" | grep -o 'ipf-id:[a-z0-9-]*' | cut -d':' -f2)
# 表示のズレを修正 (スペース調整)
if [[ -n "$highlight_uuid" && "$full_uuid" == "$highlight_uuid" ]]; then 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}" printf "\e[1;36m*%-9s %-6s %-20s %-20s %-10s\e[0m\n" "$handle" "$proto" ":$lport" "$target" "${full_uuid:0:8}"
else else
@ -50,42 +119,6 @@ list_rules() {
done done
} }
get_total_packets() {
local uuid=$1
local total=0
local counts=$(nft list table inet "${TABLE_NAME}" 2>/dev/null | grep "$uuid" | grep -o 'packets [0-9]*' | awk '{print $2}')
for c in $counts; do total=$((total + c)); done
echo "$total"
}
test_port_simple() {
local p=$1
echo -n "Testing :$p... "
if nc -z -v -w 2 127.0.0.1 "$p" 2>&1 | grep -iqE "succeeded|connected|open"; then
echo -e "\e[32mOK\e[0m"
else
echo -e "\e[31mFAILED\e[0m"
fi
}
test_specific_rule() {
local lp=$1
local full_id=$2
local short_id="${full_id#*:}"
echo -n "Verifying specific rule [${short_id:0:8}] on :$lp... "
local count_before=$(get_total_packets "$full_id")
nc -z -v -w 1 127.0.0.1 "$lp" >/dev/null 2>&1
sleep 0.1
local count_after=$(get_total_packets "$full_id")
if (( count_after > count_before )); then
echo -e "\e[32mPASSED\e[0m (Rule matched)"
else
echo -e "\e[31mSKIPPED\e[0m (Blocked by prior rule)"
fi
}
# --- 2. Deletion & Reset ---
all_clear() { all_clear() {
if [[ "$FORCE_FLAG" == false ]]; then if [[ "$FORCE_FLAG" == false ]]; then
echo -e "\e[33mWarning: This will delete ALL forwarding rules.\e[0m" echo -e "\e[33mWarning: This will delete ALL forwarding rules.\e[0m"
@ -96,7 +129,7 @@ all_clear() {
init_nft; msg "All rules cleared successfully."; list_rules; exit 0 init_nft; msg "All rules cleared successfully."; list_rules; exit 0
} }
# --- 3. Main Loop --- # --- 2. Flag Pre-processing ---
for arg in "$@"; do for arg in "$@"; do
[[ "$arg" =~ q ]] && QUIET_MODE=true && FORCE_FLAG=true && SKIP_TEST=true [[ "$arg" =~ q ]] && QUIET_MODE=true && FORCE_FLAG=true && SKIP_TEST=true
@ -108,6 +141,8 @@ done
if [[ "$RESET_MODE" == true ]]; then all_clear; fi if [[ "$RESET_MODE" == true ]]; then all_clear; fi
if [[ $# -eq 0 ]]; then list_rules; exit 0; fi if [[ $# -eq 0 ]]; then list_rules; exit 0; fi
# --- 3. Main Loop ---
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 ;;
@ -137,43 +172,41 @@ while [[ $# -gt 0 ]]; do
list_rules; exit 0 ;; list_rules; exit 0 ;;
-t*) -t*)
h_str="${1#-t}"; [[ -z "$h_str" && -n "$2" ]] && { h_str="$2"; shift; } h_str="${1#-t}"; [[ -z "$h_str" && -n "$2" ]] && { h_str="$2"; shift; }
tp=""
# スマート・テストロジック
if [[ "$h_str" =~ ^:?([0-9]+):?$ ]]; then if [[ "$h_str" =~ ^:?([0-9]+):?$ ]]; then
tp="${BASH_REMATCH[1]}" target_val="${BASH_REMATCH[1]}"
elif [[ -n "$h_str" ]]; then if nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep -q "handle $target_val"; then
tp=$(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "handle $h_str" | grep -o 'dport [0-9]*' | awk '{print $2}') test_strict_handle "$target_val"
else else
# 引数なしの場合は一番上のルールのポートを取得 test_port_simple "$target_val"
tp=$(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "dnat" | head -n 1 | grep -o 'dport [0-9]*' | awk '{print $2}')
fi
[[ -n "$tp" ]] && test_port_simple "$tp" || msg "Error: No active rules found."
exit 0 ;;
*)
if [[ "$1" =~ ^[0-9] ]]; then
init_nft
raw="$1"
if [[ "$raw" =~ ^([0-9]+):([0-9\.]+):([0-9]+)$ ]]; then lp=${BASH_REMATCH[1]}; tip=${BASH_REMATCH[2]}; tp=${BASH_REMATCH[3]}
elif [[ "$raw" =~ ^([0-9]+):([0-9]+)$ ]]; then lp=${BASH_REMATCH[1]}; tip="127.0.0.1"; tp=${BASH_REMATCH[2]}
elif [[ "$raw" =~ ^([0-9]+)$ ]]; then lp=${BASH_REMATCH[1]}; tip="127.0.0.1"; tp=${BASH_REMATCH[1]}
fi fi
else
top_h=$(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "dnat" | head -n 1 | grep -o 'handle [0-9]*' | awk '{print $2}')
[[ -n "$top_h" ]] && test_strict_handle "$top_h" || msg "Error: No rules found."
fi
exit 0 ;;
[0-9]*)
init_nft; raw="$1"
if [[ "$raw" =~ ^([0-9]+):([0-9\.]+):([0-9]+)$ ]]; then lp=${BASH_REMATCH[1]}; tip=${BASH_REMATCH[2]}; tp=${BASH_REMATCH[3]}
elif [[ "$raw" =~ ^([0-9]+):([0-9]+)$ ]]; then lp=${BASH_REMATCH[1]}; tip="127.0.0.1"; tp=${BASH_REMATCH[2]}
elif [[ "$raw" =~ ^([0-9]+)$ ]]; then lp=${BASH_REMATCH[1]}; tip="127.0.0.1"; tp=${BASH_REMATCH[1]}
fi
u_raw=$(cat /proc/sys/kernel/random/uuid); u="ipf-id:$u_raw"; fam="ip"; [[ "$tip" =~ : ]] && fam="ip6"
nft list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep -q "dport $lp" && multi_mode=true || multi_mode=false # insertを使うので常に最新が優先
u_raw=$(cat /proc/sys/kernel/random/uuid); u="ipf-id:$u_raw"; fam="ip"; [[ "$tip" =~ : ]] && fam="ip6" nft insert rule inet "${TABLE_NAME}" prerouting "$PROTO" dport "$lp" counter dnat $fam to "$tip:$tp" comment "\"$u\""
nft insert rule inet "${TABLE_NAME}" output "$PROTO" dport "$lp" counter dnat $fam to "$tip:$tp" comment "\"$u\""
nft insert rule inet "${TABLE_NAME}" forward $fam daddr "$tip" "$PROTO" dport "$tp" ct state new,established,related accept comment "\"$u\""
nft insert rule inet "${TABLE_NAME}" forward $fam saddr "$tip" "$PROTO" sport "$tp" ct state established,related accept comment "\"$u\""
nft insert rule inet "${TABLE_NAME}" forward iifname "lo" accept comment "\"$u\""
nft insert rule inet "${TABLE_NAME}" postrouting $fam daddr "$tip" "$PROTO" dport "$tp" masquerade comment "\"$u\""
nft add rule inet "${TABLE_NAME}" prerouting "$PROTO" dport "$lp" counter dnat $fam to "$tip:$tp" comment "\"$u\"" list_rules "$u_raw"
nft add rule inet "${TABLE_NAME}" output "$PROTO" dport "$lp" counter dnat $fam to "$tip:$tp" comment "\"$u\"" if [[ "$SKIP_TEST" == false ]]; then
nft add rule inet "${TABLE_NAME}" forward $fam daddr "$tip" "$PROTO" dport "$tp" ct state new,established,related accept comment "\"$u\"" new_h=$(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "$u_raw" | grep -o 'handle [0-9]*' | awk '{print $2}')
nft add rule inet "${TABLE_NAME}" forward $fam saddr "$tip" "$PROTO" sport "$tp" ct state established,related accept comment "\"$u\"" echo ""; test_strict_handle "$new_h" "true"
nft add rule inet "${TABLE_NAME}" forward iifname "lo" accept comment "\"$u\"" fi
nft add rule inet "${TABLE_NAME}" postrouting $fam daddr "$tip" "$PROTO" dport "$tp" masquerade comment "\"$u\"" exit 0 ;;
*) msg "Error: Unknown option '$1'"; show_help; exit 1 ;;
list_rules "$u_raw"
[[ "$multi_mode" == true ]] && msg "\e[33mNote: Port :$lp is now multi-targeted.\e[0m"
[[ "$SKIP_TEST" == false ]] && { echo ""; test_specific_rule "$lp" "$u"; }
exit 0
fi ;;
esac esac
shift shift
done done