BUG取れたか?

This commit is contained in:
joe 2026-01-22 21:04:56 +09:00
parent 14a4ab7f41
commit d31a72201f

69
ipf
View file

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