diff --git a/ipf b/ipf index 58be4ca..bb6d497 100755 --- a/ipf +++ b/ipf @@ -1,15 +1,15 @@ #!/bin/bash # Name: ipf -# Version: 1.5.2 +# Version: 1.5.3 # 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 exec sudo "$0" "$@" fi TABLE_NAME="ipf" -VERSION="1.5.2" +VERSION="1.5.3" PROTO="tcp" FORCE_FLAG=false SKIP_TEST=false @@ -31,7 +31,7 @@ show_help() { echo "" echo "Options:" 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 " -d HANDLE/:PORT/all Delete rule (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 " -q Quiet mode (implies -f)" echo " -v, --version Show version" + echo " -h, --help Show this help message" echo "" - echo "Quick Start:" - echo " ipf -f 80:10.0.0.1:80 # Setup kernel AND add rule" - echo " ipf -fd all # Force delete everything" + 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 "$@"; } @@ -88,11 +90,7 @@ test_strict_handle() { nc -z -v -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 (Active)\e[0m" - else - echo -e "\e[33mSKIPPED (Blocked by prior rule)\e[0m" - fi + if (( count_after > count_before )); then echo -e "\e[32mPASSED\e[0m"; else echo -e "\e[33mSKIPPED (Blocked)\e[0m"; fi else echo -e "\e[31mOFFLINE (Target Down)\e[0m" fi @@ -139,7 +137,7 @@ add_rule() { nft insert rule inet "${TABLE_NAME}" postrouting $fam daddr "$tip" "$PROTO" dport "$tp" masquerade comment "\"$u\"" 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}') echo ""; test_strict_handle "$new_h" fi @@ -158,9 +156,9 @@ all_clear() { # --- 2. Flag Pre-processing --- for arg in "$@"; do - [[ "$arg" =~ q ]] && QUIET_MODE=true && FORCE_FLAG=true && SKIP_TEST=true - [[ "$arg" =~ f ]] && FORCE_FLAG=true - [[ "$arg" =~ y ]] && FORCE_FLAG=true + if [[ "$arg" =~ q ]]; then QUIET_MODE=true; FORCE_FLAG=true; SKIP_TEST=true; fi + if [[ "$arg" =~ f ]]; then FORCE_FLAG=true; SKIP_TEST=true; fi + if [[ "$arg" =~ y ]]; then FORCE_FLAG=true; SKIP_TEST=true; fi [[ "$arg" == "-R" ]] && RESET_MODE=true [[ "$arg" == "-v" || "$arg" == "--version" ]] && { echo "ipf version ${VERSION}"; exit 0; } done @@ -204,27 +202,20 @@ while [[ $# -gt 0 ]]; do 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" + 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 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... "; 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" + 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" 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." fi exit 0 ;; - [0-9]*) - add_rule "$1"; exit 0 ;; + [0-9]*) add_rule "$1"; exit 0 ;; *) msg "\e[31mError: Unknown option '$1'\e[0m"; show_help; exit 1 ;; esac shift