HELP復活

This commit is contained in:
joe 2026-01-22 17:23:37 +09:00
parent 38e518b6f0
commit 00c5a46278

48
ipfn
View file

@ -1,21 +1,48 @@
#!/bin/bash
# Name: ipfn
# Version: 1.4.5
# Version: 1.4.6
# Date: 2026-01-22
# Description: Fixed regex to correctly distinguish between addr:port and port:addr:port.
# Description: Restored full help message and kept fixed regex for multi-pattern testing.
if [ "$(id -u)" -ne 0 ]; then
exec sudo "$0" "$@"
fi
TABLE_NAME="ipf_wrapper"
VERSION="1.4.5"
VERSION="1.4.6"
PROTO="tcp"
FORCE_FLAG=false
SKIP_TEST=false
QUIET_MODE=false
# --- Functions ---
# --- 1. Functions ---
show_help() {
echo "ipfn version ${VERSION}"
echo "Usage: ipfn [OPTIONS] [RULES]"
echo ""
echo "Options:"
echo " -l, -L List all forwarding rules"
echo " -d HANDLE/:PORT/all Delete by handle, port (e.g. :80), or 'all'"
echo " -R Reset: Clear ALL rules immediately"
echo " -t [TARGET] Test connectivity (See 'Test Patterns' below)"
echo " -f Force: No confirmation & Skip test"
echo " -q Quiet mode: No output (implies -f)"
echo " -h, --help Show this help message"
echo ""
echo "Rules Syntax:"
echo " LPORT:TIP:TPORT Forward LPORT to TIP:TPORT (e.g. 80:10.0.0.1:80)"
echo " LPORT:TPORT Forward LPORT to 127.0.0.1:TPORT"
echo " LPORT Forward LPORT to 127.0.0.1:LPORT"
echo ""
echo "Test Patterns (-t):"
echo " -t 5 Strict test: Verify Handle 5 (Entry -> Exit -> Counter)"
echo " -t :80 Simple test: Check if Local port :80 is open"
echo " -t 1.1.1.1:80 Direct test: Check if Target 1.1.1.1:80 is UP"
echo " -t 80:1.1.1.1:80 Full test: Check both Target UP and Local :80 OPEN"
echo ""
echo "Note: New rules are INSERTED at the top (Latest rule takes priority)."
}
msg() { [[ "$QUIET_MODE" == false ]] && echo -e "$@"; }
@ -108,7 +135,7 @@ all_clear() {
init_nft; msg "All rules cleared successfully."; list_rules; exit 0
}
# --- Flag Pre-processing ---
# --- 2. Flag Pre-processing ---
for arg in "$@"; do
[[ "$arg" =~ q ]] && QUIET_MODE=true && FORCE_FLAG=true && SKIP_TEST=true
@ -120,14 +147,11 @@ done
if [[ "$RESET_MODE" == true ]]; then all_clear; fi
if [[ $# -eq 0 ]]; then list_rules; exit 0; fi
# --- Main Loop ---
# --- 3. Main Loop ---
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
echo "Usage: ipfn [OPTIONS] [RULES]"
echo "Tests: -t 5 (handle), -t :80 (port), -t 1.1.1.1:80 (direct), -t 80:1.1.1.1:80 (full)"
exit 0 ;;
-h|--help) show_help; exit 0 ;;
-L|-l) list_rules; exit 0 ;;
-*[d]*)
h_str=$(echo "$1" | sed -E 's/^-q?d?f?//')
@ -154,24 +178,20 @@ while [[ $# -gt 0 ]]; do
list_rules; exit 0 ;;
-t*)
h_str="${1#-t}"; [[ -z "$h_str" && -n "$2" ]] && { h_str="$2"; shift; }
# 1. port:addr:port
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"
# 2. addr:port
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"
# 3. :port or port:
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"
# 4. handle
elif [[ "$h_str" =~ ^[0-9]+$ ]]; then
test_strict_handle "$h_str"
else