大改編 1.5.9
This commit is contained in:
parent
d31a72201f
commit
28da4e2709
1 changed files with 163 additions and 64 deletions
227
ipf
227
ipf
|
|
@ -1,47 +1,38 @@
|
|||
#!/bin/bash
|
||||
# Name: ipf
|
||||
# Version: 1.5.6
|
||||
# Version: 1.5.9
|
||||
# Date: 2026-01-22
|
||||
# Description: Fixed bug in -t argument parsing and NC probe logic.
|
||||
# Description: Full feature set. Includes conflict prevention, fixed -t parsing, and -f logic.
|
||||
|
||||
# Root check
|
||||
if [ "$(id -u)" -ne 0 ]; then
|
||||
exec sudo "$0" "$@"
|
||||
fi
|
||||
|
||||
# Global Variables
|
||||
TABLE_NAME="ipf"
|
||||
VERSION="1.5.6"
|
||||
VERSION="1.5.9"
|
||||
PROTO="tcp"
|
||||
FORCE_FLAG=false
|
||||
SKIP_TEST=false
|
||||
QUIET_MODE=false
|
||||
RESET_MODE=false
|
||||
|
||||
# --- 1. Functions ---
|
||||
# --- 1. Core Functions ---
|
||||
|
||||
msg() {
|
||||
[[ "$QUIET_MODE" == false ]] && echo -e "$@"
|
||||
}
|
||||
|
||||
enable_forwarding() {
|
||||
msg "\e[34m[System]\e[0m Enabling IP forwarding and localnet routing..."
|
||||
sysctl -w net.ipv4.ip_forward=1 >/dev/null
|
||||
# Enable route_localnet for all interfaces to allow forwarding to 127.0.0.1
|
||||
for dev in /proc/sys/net/ipv4/conf/*/route_localnet; do
|
||||
echo 1 > "$dev" 2>/dev/null
|
||||
done
|
||||
}
|
||||
|
||||
show_help() {
|
||||
echo "ipf version ${VERSION}"
|
||||
echo "Usage: ipf [OPTIONS] [RULE]"
|
||||
echo ""
|
||||
echo "Options:"
|
||||
echo " -f Kernel: Enable forwarding / Global: Force (Skip tests)"
|
||||
echo " -l, -L List all forwarding rules"
|
||||
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"
|
||||
}
|
||||
|
||||
msg() { [[ "$QUIET_MODE" == false ]] && echo -e "$@"; }
|
||||
|
||||
init_nft() {
|
||||
nft add table inet "${TABLE_NAME}" 2>/dev/null
|
||||
nft add chain inet "${TABLE_NAME}" prerouting { type nat hook prerouting priority -100 \; } 2>/dev/null
|
||||
|
|
@ -52,21 +43,47 @@ init_nft() {
|
|||
|
||||
get_total_packets() {
|
||||
local uuid=$1
|
||||
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
|
||||
local counts=$(nft list table inet "${TABLE_NAME}" 2>/dev/null | grep "$uuid" | grep -o 'packets [0-9]*' | awk '{print $2}')
|
||||
for c in $counts; do
|
||||
total=$((total + c))
|
||||
done
|
||||
echo "$total"
|
||||
}
|
||||
|
||||
test_connection() {
|
||||
# 接続テストの本体
|
||||
nc -z -w 2 "$1" "$2" >/dev/null 2>&1
|
||||
# Crucial: Argument order for nc (options first, then target)
|
||||
nc -z -w 1 "$1" "$2" >/dev/null 2>&1
|
||||
return $?
|
||||
}
|
||||
|
||||
delete_by_handle() {
|
||||
local h=$1
|
||||
# Find UUID associated with this handle
|
||||
local uuid=$(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "handle $h" | grep -o 'ipf-id:[a-z0-9-]*')
|
||||
|
||||
if [[ -z "$uuid" ]]; then
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Delete rules in all chains that share this UUID
|
||||
local chains=("prerouting" "output" "forward" "postrouting")
|
||||
for c in "${chains[@]}"; do
|
||||
nft -a list chain inet "${TABLE_NAME}" "$c" 2>/dev/null | grep "$uuid" | grep -o 'handle [0-9]*' | awk '{print $2}' | while read -r rh; do
|
||||
nft delete rule inet "${TABLE_NAME}" "$c" handle "$rh"
|
||||
done
|
||||
done
|
||||
return 0
|
||||
}
|
||||
|
||||
test_strict_handle() {
|
||||
local h=$1
|
||||
local info=$(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "handle $h")
|
||||
[[ -z "$info" ]] && { echo -e "Handle $h \e[31mNOT FOUND\e[0m"; return; }
|
||||
|
||||
if [[ -z "$info" ]]; then
|
||||
echo -e "Handle $h \e[31mNOT FOUND\e[0m"
|
||||
return
|
||||
fi
|
||||
|
||||
local lp=$(echo "$info" | grep -o 'dport [0-9]*' | awk '{print $2}')
|
||||
local target=$(echo "$info" | grep -oE 'to ([0-9.]+|\[[0-9a-fA-F:]+\]):[0-9]+' | awk '{print $2}')
|
||||
|
|
@ -79,10 +96,15 @@ test_strict_handle() {
|
|||
local count_before=$(get_total_packets "$uuid")
|
||||
|
||||
if test_connection "$tip" "$tp"; then
|
||||
# Actual trigger test via localhost
|
||||
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
|
||||
if (( count_after > count_before )); then
|
||||
echo -e "\e[32mPASSED\e[0m"
|
||||
else
|
||||
echo -e "\e[33mSKIPPED (Check other rules)\e[0m"
|
||||
fi
|
||||
else
|
||||
echo -e "\e[31mOFFLINE (Target Down)\e[0m"
|
||||
fi
|
||||
|
|
@ -95,12 +117,14 @@ list_rules() {
|
|||
msg "Forwarding Rules (ipf):"
|
||||
printf "%-10s %-6s %-20s %-20s %-10s\n" "HANDLE" "PROTO" "LOCAL" "TARGET" "UUID"
|
||||
echo "-----------------------------------------------------------------------------------"
|
||||
|
||||
nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "dnat" | while read -r line; do
|
||||
handle=$(echo "$line" | grep -o 'handle [0-9]*' | awk '{print $2}')
|
||||
proto=$(echo "$line" | grep -q "udp" && echo "udp" || echo "tcp")
|
||||
target=$(echo "$line" | grep -oE '([0-9.]+|\[[0-9a-fA-F:]+\]):[0-9]+' | head -n 1)
|
||||
lport=$(echo "$line" | grep -o 'dport [0-9]*' | awk '{print $2}')
|
||||
full_uuid=$(echo "$line" | grep -o 'ipf-id:[a-z0-9-]*' | cut -d':' -f2)
|
||||
local handle=$(echo "$line" | grep -o 'handle [0-9]*' | awk '{print $2}')
|
||||
local proto=$(echo "$line" | grep -q "udp" && echo "udp" || echo "tcp")
|
||||
local target=$(echo "$line" | grep -oE '([0-9.]+|\[[0-9a-fA-F:]+\]):[0-9]+' | head -n 1)
|
||||
local lport=$(echo "$line" | grep -o 'dport [0-9]*' | awk '{print $2}')
|
||||
local full_uuid=$(echo "$line" | grep -o 'ipf-id:[a-z0-9-]*' | cut -d':' -f2)
|
||||
|
||||
if [[ -n "$highlight_uuid" && "$full_uuid" == "$highlight_uuid" ]]; then
|
||||
printf "\e[1;36m*%-9s %-6s %-20s %-20s %-10s\e[0m\n" "$handle" "$proto" ":$lport" "$target" "${full_uuid:0:8}"
|
||||
else
|
||||
|
|
@ -114,13 +138,32 @@ add_rule() {
|
|||
init_nft
|
||||
[[ "$FORCE_FLAG" == true ]] && enable_forwarding
|
||||
|
||||
if [[ "$raw" =~ ^([0-9]+):([0-9\.]+):([0-9]+)$ ]]; then lp=${BASH_REMATCH[1]}; tip=${BASH_REMATCH[2]}; tp=${BASH_REMATCH[3]}
|
||||
elif [[ "$raw" =~ ^([0-9]+):([0-9]+)$ ]]; then lp=${BASH_REMATCH[1]}; tip="127.0.0.1"; tp=${BASH_REMATCH[2]}
|
||||
elif [[ "$raw" =~ ^([0-9]+)$ ]]; then lp=${BASH_REMATCH[1]}; tip="127.0.0.1"; tp=${BASH_REMATCH[1]}
|
||||
else msg "\e[31mError: Invalid rule format '$raw'\e[0m"; return 1; fi
|
||||
# Parsing
|
||||
if [[ "$raw" =~ ^([0-9]+):([0-9\.]+):([0-9]+)$ ]]; then
|
||||
lp=${BASH_REMATCH[1]}; tip=${BASH_REMATCH[2]}; tp=${BASH_REMATCH[3]}
|
||||
elif [[ "$raw" =~ ^([0-9]+):([0-9]+)$ ]]; then
|
||||
lp=${BASH_REMATCH[1]}; tip="127.0.0.1"; tp=${BASH_REMATCH[2]}
|
||||
elif [[ "$raw" =~ ^([0-9]+)$ ]]; then
|
||||
lp=${BASH_REMATCH[1]}; tip="127.0.0.1"; tp=${BASH_REMATCH[1]}
|
||||
else
|
||||
msg "\e[31mError: Invalid rule format '$raw'\e[0m"
|
||||
return 1
|
||||
fi
|
||||
|
||||
u_raw=$(cat /proc/sys/kernel/random/uuid); u="ipf-id:$u_raw"; fam="ip"; [[ "$tip" =~ : ]] && fam="ip6"
|
||||
# --- Conflict Management ---
|
||||
# Delete any existing rules using the same local port to prevent shadowing
|
||||
local existing_h=$(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "dport $lp" | grep -o 'handle [0-9]*' | awk '{print $2}')
|
||||
for eh in $existing_h; do
|
||||
delete_by_handle "$eh"
|
||||
done
|
||||
|
||||
# UUID Generation
|
||||
local u_raw=$(cat /proc/sys/kernel/random/uuid)
|
||||
local u="ipf-id:$u_raw"
|
||||
local fam="ip"
|
||||
[[ "$tip" =~ : ]] && fam="ip6"
|
||||
|
||||
# Rule Insertion (using insert to stay at the top)
|
||||
nft insert rule inet "${TABLE_NAME}" prerouting "$PROTO" dport "$lp" counter dnat $fam to "$tip:$tp" comment "\"$u\""
|
||||
nft insert rule inet "${TABLE_NAME}" output "$PROTO" dport "$lp" counter dnat $fam to "$tip:$tp" comment "\"$u\""
|
||||
nft insert rule inet "${TABLE_NAME}" forward $fam daddr "$tip" "$PROTO" dport "$tp" ct state new,established,related accept comment "\"$u\""
|
||||
|
|
@ -129,20 +172,43 @@ add_rule() {
|
|||
nft insert rule inet "${TABLE_NAME}" postrouting $fam daddr "$tip" "$PROTO" dport "$tp" masquerade comment "\"$u\""
|
||||
|
||||
list_rules "$u_raw"
|
||||
|
||||
# Auto-test unless forced
|
||||
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"
|
||||
local 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
|
||||
}
|
||||
|
||||
all_clear() {
|
||||
if [[ "$FORCE_FLAG" == false ]]; then
|
||||
echo -e "\e[33mWarning: Clear ALL ipf rules?\e[0m"
|
||||
read -p "(y/N): " confirm
|
||||
echo -e "\e[33mWarning: This will delete ALL ipf rules.\e[0m"
|
||||
read -p "Are you sure? (y/N): " confirm
|
||||
[[ ! "$confirm" =~ ^[yY]$ ]] && exit 0
|
||||
fi
|
||||
nft delete table inet "${TABLE_NAME}" 2>/dev/null
|
||||
init_nft; msg "All ipf rules cleared."; list_rules; exit 0
|
||||
init_nft
|
||||
msg "All ipf rules cleared."
|
||||
list_rules
|
||||
exit 0
|
||||
}
|
||||
|
||||
show_help() {
|
||||
echo "ipf version ${VERSION}"
|
||||
echo "Usage: ipf [OPTIONS] [RULE]"
|
||||
echo ""
|
||||
echo "Options:"
|
||||
echo " -f Kernel: Enable forwarding & route_localnet"
|
||||
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)"
|
||||
echo " -t [TARGET] Test connectivity (Handle, :Port, or IP:Port)"
|
||||
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"
|
||||
}
|
||||
|
||||
# --- 2. Flag Pre-processing ---
|
||||
|
|
@ -152,7 +218,6 @@ for arg in "$@"; do
|
|||
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
|
||||
|
||||
if [[ "$RESET_MODE" == true ]]; then all_clear; fi
|
||||
|
|
@ -164,58 +229,92 @@ while [[ $# -gt 0 ]]; do
|
|||
case "$1" in
|
||||
-h|--help) show_help; exit 0 ;;
|
||||
-l|-L) list_rules; exit 0 ;;
|
||||
-v|--version) echo "ipf version ${VERSION}"; exit 0 ;;
|
||||
-f|-y|-q)
|
||||
[[ "$1" == "-f" ]] && enable_forwarding
|
||||
shift; [[ $# -eq 0 ]] && exit 0; continue ;;
|
||||
shift
|
||||
[[ $# -eq 0 ]] && exit 0
|
||||
continue
|
||||
;;
|
||||
-*[d]*)
|
||||
# Handle combined flags like -fd or -qd
|
||||
h_str=$(echo "$1" | sed -E 's/^-q?d?f?y?//')
|
||||
[[ -z "$h_str" ]] && { h_str="$2"; shift; }
|
||||
if [[ "$h_str" == "all" ]]; then all_clear; fi
|
||||
if [[ -z "$h_str" ]]; then
|
||||
h_str="$2"
|
||||
shift
|
||||
fi
|
||||
|
||||
if [[ "$h_str" == "all" || "$h_str" == "*" ]]; then
|
||||
all_clear
|
||||
fi
|
||||
|
||||
# Collect handles to delete
|
||||
handles=()
|
||||
IFS=',' read -r -a parts <<< "$h_str"
|
||||
for part in "${parts[@]}"; do
|
||||
if [[ "$part" =~ ^:([0-9]+)$ ]]; then
|
||||
p="${BASH_REMATCH[1]}"
|
||||
for h in $(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "dport $p" | grep -o 'handle [0-9]*' | awk '{print $2}'); do handles+=("$h"); done
|
||||
else handles+=("$part"); fi
|
||||
done
|
||||
for h in "${handles[@]}"; do
|
||||
uuid=$(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "handle $h" | grep -o 'ipf-id:[a-z0-9-]*')
|
||||
[[ -z "$uuid" ]] && continue
|
||||
for c in prerouting output forward postrouting; do
|
||||
nft -a list chain inet "${TABLE_NAME}" "$c" 2>/dev/null | grep "$uuid" | grep -o 'handle [0-9]*' | awk '{print $2}' | while read -r rh; do
|
||||
nft delete rule inet "${TABLE_NAME}" "$c" handle "$rh"
|
||||
# Find all handles for this local port
|
||||
for h in $(nft -a list chain inet "${TABLE_NAME}" prerouting 2>/dev/null | grep "dport $p" | grep -o 'handle [0-9]*' | awk '{print $2}'); do
|
||||
handles+=("$h")
|
||||
done
|
||||
done
|
||||
msg "Deleted handle $h"
|
||||
else
|
||||
handles+=("$part")
|
||||
fi
|
||||
done
|
||||
list_rules; exit 0 ;;
|
||||
|
||||
for h in "${handles[@]}"; do
|
||||
if delete_by_handle "$h"; then
|
||||
msg "Deleted handle $h"
|
||||
else
|
||||
msg "\e[31mError: Handle $h not found\e[0m"
|
||||
fi
|
||||
done
|
||||
list_rules
|
||||
exit 0
|
||||
;;
|
||||
-t*)
|
||||
# 引数解析のバグ修正
|
||||
h_str="${1#-t}"
|
||||
if [[ -z "$h_str" ]]; then
|
||||
h_str="$2"
|
||||
shift
|
||||
fi
|
||||
|
||||
# Pattern matching for different test types
|
||||
if [[ "$h_str" =~ ^([0-9\.]+):([0-9]+)$ ]]; then
|
||||
# IP:PORT test
|
||||
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]+)$ ]]; then
|
||||
# :PORT test (local)
|
||||
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
|
||||
# Handle test
|
||||
test_strict_handle "$h_str"
|
||||
else
|
||||
# デフォルト(最初のルールをテスト)
|
||||
# Default: test the first rule found
|
||||
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: Invalid test target '$h_str'"
|
||||
if [[ -n "$top_h" ]]; then
|
||||
test_strict_handle "$top_h"
|
||||
else
|
||||
msg "\e[31mError: No valid test target found for '$h_str'\e[0m"
|
||||
fi
|
||||
fi
|
||||
exit 0 ;;
|
||||
[0-9]*) add_rule "$1"; exit 0 ;;
|
||||
*) msg "\e[31mError: Unknown option '$1'\e[0m"; show_help; exit 1 ;;
|
||||
exit 0
|
||||
;;
|
||||
[0-9]*)
|
||||
# Positional argument as a rule
|
||||
add_rule "$1"
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
msg "\e[31mError: Unknown option '$1'\e[0m"
|
||||
show_help
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
|
|
|||
Loading…
Reference in a new issue