-q -f があれば test skip する様に修正 1.5.3

This commit is contained in:
joe 2026-01-22 20:59:05 +09:00
parent 3d2b788372
commit 14a4ab7f41

49
ipf
View file

@ -1,15 +1,15 @@
#!/bin/bash #!/bin/bash
# Name: ipf # Name: ipf
# Version: 1.5.2 # Version: 1.5.3
# Date: 2026-01-22 # Date: 2026-01-22
# Description: -f for Kernel Sysctl & Force. Full logic restored. # Description: -f enables Kernel forwarding/routing and acts as 'Force' (skips tests/confirm).
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.2" VERSION="1.5.3"
PROTO="tcp" PROTO="tcp"
FORCE_FLAG=false FORCE_FLAG=false
SKIP_TEST=false SKIP_TEST=false
@ -31,7 +31,7 @@ show_help() {
echo "" echo ""
echo "Options:" echo "Options:"
echo " -f Kernel: Enable forwarding & route_localnet" echo " -f Kernel: Enable forwarding & route_localnet"
echo " Global: Acts as 'Force' for delete/reset" 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 (Add -f to skip confirmation)"
echo " -R Reset: Clear ALL rules (Add -f to skip confirmation)" echo " -R Reset: Clear ALL rules (Add -f to skip confirmation)"
@ -39,10 +39,12 @@ show_help() {
echo " -y Same as -f (Force/Yes)" echo " -y Same as -f (Force/Yes)"
echo " -q Quiet mode (implies -f)" echo " -q Quiet mode (implies -f)"
echo " -v, --version Show version" echo " -v, --version Show version"
echo " -h, --help Show this help message"
echo "" echo ""
echo "Quick Start:" echo "Rule Syntax:"
echo " ipf -f 80:10.0.0.1:80 # Setup kernel AND add rule" echo " LPORT:TIP:TPORT Forward LPORT to TIP:TPORT"
echo " ipf -fd all # Force delete everything" 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 "$@"; }
@ -88,11 +90,7 @@ test_strict_handle() {
nc -z -v -w 1 127.0.0.1 "$lp" >/dev/null 2>&1 nc -z -v -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 if (( count_after > count_before )); then echo -e "\e[32mPASSED\e[0m"; else echo -e "\e[33mSKIPPED (Blocked)\e[0m"; fi
echo -e "\e[32mPASSED (Active)\e[0m"
else
echo -e "\e[33mSKIPPED (Blocked by prior rule)\e[0m"
fi
else else
echo -e "\e[31mOFFLINE (Target Down)\e[0m" echo -e "\e[31mOFFLINE (Target Down)\e[0m"
fi fi
@ -139,7 +137,7 @@ add_rule() {
nft insert rule inet "${TABLE_NAME}" postrouting $fam daddr "$tip" "$PROTO" dport "$tp" masquerade comment "\"$u\"" nft insert rule inet "${TABLE_NAME}" postrouting $fam daddr "$tip" "$PROTO" dport "$tp" masquerade comment "\"$u\""
list_rules "$u_raw" list_rules "$u_raw"
if [[ "$SKIP_TEST" == false ]]; then if [[ "$SKIP_TEST" == false && "$FORCE_FLAG" == false ]]; then
new_h=$(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "$u_raw" | grep -o 'handle [0-9]*' | awk '{print $2}') new_h=$(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "$u_raw" | grep -o 'handle [0-9]*' | awk '{print $2}')
echo ""; test_strict_handle "$new_h" echo ""; test_strict_handle "$new_h"
fi fi
@ -158,9 +156,9 @@ all_clear() {
# --- 2. Flag Pre-processing --- # --- 2. Flag Pre-processing ---
for arg in "$@"; do for arg in "$@"; do
[[ "$arg" =~ q ]] && QUIET_MODE=true && FORCE_FLAG=true && SKIP_TEST=true if [[ "$arg" =~ q ]]; then QUIET_MODE=true; FORCE_FLAG=true; SKIP_TEST=true; fi
[[ "$arg" =~ f ]] && FORCE_FLAG=true if [[ "$arg" =~ f ]]; then FORCE_FLAG=true; SKIP_TEST=true; fi
[[ "$arg" =~ y ]] && FORCE_FLAG=true if [[ "$arg" =~ y ]]; then FORCE_FLAG=true; SKIP_TEST=true; fi
[[ "$arg" == "-R" ]] && RESET_MODE=true [[ "$arg" == "-R" ]] && RESET_MODE=true
[[ "$arg" == "-v" || "$arg" == "--version" ]] && { echo "ipf version ${VERSION}"; exit 0; } [[ "$arg" == "-v" || "$arg" == "--version" ]] && { echo "ipf version ${VERSION}"; exit 0; }
done done
@ -204,27 +202,20 @@ while [[ $# -gt 0 ]]; do
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; }
if [[ "$h_str" =~ ^([0-9]+):([0-9\.]+):([0-9]+)$ ]]; then if [[ "$h_str" =~ ^([0-9]+):([0-9\.]+):([0-9]+)$ ]]; then
lp=${BASH_REMATCH[1]}; tip=${BASH_REMATCH[2]}; tp=${BASH_REMATCH[3]} lp=${BASH_REMATCH[1]}; tip=${BASH_REMATCH[2]}; tp=${BASH_REMATCH[3]}
echo -n "Checking Target $tip:$tp... " echo -n "Checking Target $tip:$tp... "; test_connection "$tip" "$tp" && echo -e "\e[32mUP\e[0m" || echo -e "\e[31mDOWN\e[0m"
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"
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 elif [[ "$h_str" =~ ^([0-9]+\.[0-9]+\.[0-9]+\.[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... " echo -n "Checking Target $tip:$tp... "; test_connection "$tip" "$tp" && echo -e "\e[32mUP\e[0m" || echo -e "\e[31mDOWN\e[0m"
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 elif [[ "$h_str" =~ ^:([0-9]+)$ || "$h_str" =~ ^([0-9]+):$ ]]; then
p="${BASH_REMATCH[1]}${BASH_REMATCH[2]}" 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"
echo -n "Checking Local :$p... " elif [[ "$h_str" =~ ^[0-9]+$ ]]; then test_strict_handle "$h_str"
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"
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: No rules to test."
fi fi
exit 0 ;; exit 0 ;;
[0-9]*) [0-9]*) add_rule "$1"; exit 0 ;;
add_rule "$1"; exit 0 ;;
*) msg "\e[31mError: Unknown option '$1'\e[0m"; show_help; exit 1 ;; *) msg "\e[31mError: Unknown option '$1'\e[0m"; show_help; exit 1 ;;
esac esac
shift shift