diff --git a/ipf b/ipf index bb6d497..2df31c6 100755 --- a/ipf +++ b/ipf @@ -1,15 +1,15 @@ #!/bin/bash # Name: ipf -# Version: 1.5.3 +# Version: 1.5.6 # Date: 2026-01-22 -# Description: -f enables Kernel forwarding/routing and acts as 'Force' (skips tests/confirm). +# Description: Fixed bug in -t argument parsing and NC probe logic. if [ "$(id -u)" -ne 0 ]; then exec sudo "$0" "$@" fi TABLE_NAME="ipf" -VERSION="1.5.3" +VERSION="1.5.6" PROTO="tcp" FORCE_FLAG=false SKIP_TEST=false @@ -30,21 +30,14 @@ show_help() { echo "Usage: ipf [OPTIONS] [RULE]" echo "" echo "Options:" - echo " -f Kernel: Enable forwarding & route_localnet" - echo " Global: Acts as 'Force' (Skips confirmation & tests)" + echo " -f Kernel: Enable forwarding / Global: Force (Skip tests)" echo " -l, -L List all forwarding rules" - echo " -d HANDLE/:PORT/all Delete rule (Add -f to skip confirmation)" - echo " -R Reset: Clear ALL rules (Add -f to skip confirmation)" - echo " -t [TARGET] Test connectivity (Handle, :Port, or Addr:Port)" - echo " -y Same as -f (Force/Yes)" - echo " -q Quiet mode (implies -f)" + echo " -d HANDLE/:PORT/all Delete rule" + echo " -t [TARGET] Test: (Handle, :Port, or IP:Port)" + echo " -R Reset: Clear ALL rules" + echo " -y Same as -f" + echo " -q Quiet mode" echo " -v, --version Show version" - echo " -h, --help Show this help message" - echo "" - echo "Rule Syntax:" - echo " LPORT:TIP:TPORT Forward LPORT to TIP:TPORT" - echo " LPORT:TPORT Forward LPORT to 127.0.0.1:TPORT" - echo " LPORT Forward LPORT to 127.0.0.1:LPORT" } msg() { [[ "$QUIET_MODE" == false ]] && echo -e "$@"; } @@ -59,16 +52,15 @@ init_nft() { 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}') + local total=0 for c in $counts; do total=$((total + c)); done echo "$total" } test_connection() { - local ip=$1 - local port=$2 - nc -z -v -w 1 "$ip" "$port" >/dev/null 2>&1 + # 接続テストの本体 + nc -z -w 2 "$1" "$2" >/dev/null 2>&1 } test_strict_handle() { @@ -87,7 +79,7 @@ test_strict_handle() { local count_before=$(get_total_packets "$uuid") if test_connection "$tip" "$tp"; then - nc -z -v -w 1 127.0.0.1 "$lp" >/dev/null 2>&1 + nc -z -w 1 127.0.0.1 "$lp" >/dev/null 2>&1 sleep 0.1 local count_after=$(get_total_packets "$uuid") if (( count_after > count_before )); then echo -e "\e[32mPASSED\e[0m"; else echo -e "\e[33mSKIPPED (Blocked)\e[0m"; fi @@ -145,8 +137,8 @@ add_rule() { all_clear() { if [[ "$FORCE_FLAG" == false ]]; then - echo -e "\e[33mWarning: This will delete ALL ipf rules.\e[0m" - read -p "Are you sure? (y/N): " confirm + echo -e "\e[33mWarning: Clear ALL ipf rules?\e[0m" + read -p "(y/N): " confirm [[ ! "$confirm" =~ ^[yY]$ ]] && exit 0 fi nft delete table inet "${TABLE_NAME}" 2>/dev/null @@ -178,7 +170,7 @@ while [[ $# -gt 0 ]]; do -*[d]*) h_str=$(echo "$1" | sed -E 's/^-q?d?f?y?//') [[ -z "$h_str" ]] && { h_str="$2"; shift; } - if [[ "$h_str" == "all" || "$h_str" == "*" ]]; then all_clear; fi + if [[ "$h_str" == "all" ]]; then all_clear; fi handles=() IFS=',' read -r -a parts <<< "$h_str" for part in "${parts[@]}"; do @@ -199,20 +191,27 @@ while [[ $# -gt 0 ]]; do done list_rules; exit 0 ;; -t*) - h_str="${1#-t}"; [[ -z "$h_str" && -n "$2" ]] && { h_str="$2"; shift; } - if [[ "$h_str" =~ ^([0-9]+):([0-9\.]+):([0-9]+)$ ]]; then - lp=${BASH_REMATCH[1]}; tip=${BASH_REMATCH[2]}; tp=${BASH_REMATCH[3]} - echo -n "Checking Target $tip:$tp... "; test_connection "$tip" "$tp" && echo -e "\e[32mUP\e[0m" || echo -e "\e[31mDOWN\e[0m" - echo -n "Checking Local :$lp... "; nc -z -v -w 1 127.0.0.1 "$lp" >/dev/null 2>&1 && echo -e "\e[32mOPEN\e[0m" || echo -e "\e[31mCLOSED\e[0m" - elif [[ "$h_str" =~ ^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+):([0-9]+)$ ]]; then + # 引数解析のバグ修正 + h_str="${1#-t}" + if [[ -z "$h_str" ]]; then + h_str="$2" + shift + fi + + if [[ "$h_str" =~ ^([0-9\.]+):([0-9]+)$ ]]; then tip=${BASH_REMATCH[1]}; tp=${BASH_REMATCH[2]} - echo -n "Checking Target $tip:$tp... "; test_connection "$tip" "$tp" && echo -e "\e[32mUP\e[0m" || echo -e "\e[31mDOWN\e[0m" - elif [[ "$h_str" =~ ^:([0-9]+)$ || "$h_str" =~ ^([0-9]+):$ ]]; then - p="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"; echo -n "Checking Local :$p... "; nc -z -v -w 1 127.0.0.1 "$p" >/dev/null 2>&1 && echo -e "\e[32mOK\e[0m" || echo -e "\e[31mOFFLINE\e[0m" - elif [[ "$h_str" =~ ^[0-9]+$ ]]; then test_strict_handle "$h_str" + echo -n "Checking Target $tip:$tp... " + test_connection "$tip" "$tp" && echo -e "\e[32mUP\e[0m" || echo -e "\e[31mDOWN\e[0m" + elif [[ "$h_str" =~ ^:([0-9]+)$ ]]; then + p="${BASH_REMATCH[1]}" + echo -n "Checking Local :$p... " + nc -z -w 1 127.0.0.1 "$p" >/dev/null 2>&1 && echo -e "\e[32mOK\e[0m" || echo -e "\e[31mOFFLINE\e[0m" + elif [[ "$h_str" =~ ^[0-9]+$ ]]; then + test_strict_handle "$h_str" 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 to test." + [[ -n "$top_h" ]] && test_strict_handle "$top_h" || msg "Error: Invalid test target '$h_str'" fi exit 0 ;; [0-9]*) add_rule "$1"; exit 0 ;;