From 79d11fecf7038653aa30d42e9d3fd178a2cccc13 Mon Sep 17 00:00:00 2001 From: joe Date: Thu, 22 Jan 2026 12:10:38 +0900 Subject: [PATCH] =?UTF-8?q?=E5=90=84=E7=A8=AE=E3=82=AA=E3=83=97=E3=82=B7?= =?UTF-8?q?=E3=83=A7=E3=83=B3=E8=A9=95=E4=BE=A1=E9=83=A8=E5=88=86BUG?= =?UTF-8?q?=E5=8F=96=E3=82=8A=20-d63-69=20=E3=81=A8=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E3=81=97=E3=81=9F=E9=9A=9B=E3=81=AB=E3=80=81=E9=96=93=E3=81=AE?= =?UTF-8?q?=E3=80=8C=E3=82=B5=E3=83=96=E3=82=B0=E3=83=AB=E3=83=BC=E3=83=97?= =?UTF-8?q?=E3=81=AE=E3=83=8F=E3=83=B3=E3=83=89=E3=83=AB=EF=BC=8864-68?= =?UTF-8?q?=EF=BC=89=E3=80=8D=E3=82=92=E7=84=A1=E8=A6=96=E3=81=97=E3=81=A6?= =?UTF-8?q?=E3=80=81=E3=81=A1=E3=82=83=E3=82=93=E3=81=A8=E8=BB=A2=E9=80=81?= =?UTF-8?q?=E8=A8=AD=E5=AE=9A=E3=81=AE=E8=A6=AA=E7=8E=89=E3=81=A7=E3=81=82?= =?UTF-8?q?=E3=82=8B=2063=20=E3=81=A8=2069=20=E3=81=A0=E3=81=91=E3=82=92?= =?UTF-8?q?=E6=A4=9C=E5=87=BA=E3=81=97=E3=80=81=E3=81=9D=E3=81=93=E3=81=8B?= =?UTF-8?q?=E3=82=89=E8=8A=8B=E3=81=A5=E3=82=8B=E5=BC=8F=E3=81=AB=E9=96=A2?= =?UTF-8?q?=E9=80=A3=E3=83=AB=E3=83=BC=E3=83=AB=E3=82=92=E6=B6=88=E5=8E=BB?= =?UTF-8?q?=E3=81=A7=E3=81=8D=E3=81=A6=E3=81=84=E3=81=BE=E3=81=99=E3=80=82?= =?UTF-8?q?=E8=A1=A8=E7=A4=BA=E3=82=82=E3=82=B9=E3=83=83=E3=82=AD=E3=83=AA?= =?UTF-8?q?=E3=81=97=E3=81=A6=E3=80=81=E3=82=88=E3=81=86=E3=82=84=E3=81=8F?= =?UTF-8?q?=E3=80=8C=E9=81=93=E5=85=B7=E3=80=8D=E3=81=A8=E3=81=97=E3=81=A6?= =?UTF-8?q?=E4=BF=A1=E9=A0=BC=E3=81=A7=E3=81=8D=E3=82=8B=E3=83=AC=E3=83=99?= =?UTF-8?q?=E3=83=AB=E3=81=AB=E3=81=AA=E3=81=A3=E3=81=9F=E6=B0=97=E3=81=8C?= =?UTF-8?q?=E3=81=97=E3=81=BE=E3=81=99=E3=80=82=20=E4=BB=8A=E5=9B=9E?= =?UTF-8?q?=E3=81=AE=E6=9C=80=E7=B5=82=E7=9A=84=E3=81=AA=E5=89=8A=E9=99=A4?= =?UTF-8?q?=E3=83=AD=E3=82=B8=E3=83=83=E3=82=AF=E3=81=AE=E4=BB=95=E7=B5=84?= =?UTF-8?q?=E3=81=BF=20=E3=83=95=E3=82=A3=E3=83=AB=E3=82=BF=E3=83=AA?= =?UTF-8?q?=E3=83=B3=E3=82=B0:=20=E6=8C=87=E5=AE=9A=E3=81=95=E3=82=8C?= =?UTF-8?q?=E3=81=9F=E7=AF=84=E5=9B=B2=EF=BC=8863-69=EF=BC=89=E3=81=AE?= =?UTF-8?q?=E4=B8=AD=E3=81=8B=E3=82=89=E3=80=81prerouting=20=E3=83=81?= =?UTF-8?q?=E3=82=A7=E3=83=BC=E3=83=B3=E3=81=AB=E5=AE=9F=E5=9C=A8=E3=81=99?= =?UTF-8?q?=E3=82=8B=E3=83=8F=E3=83=B3=E3=83=89=E3=83=AB=E3=81=A0=E3=81=91?= =?UTF-8?q?=E3=82=92=E3=83=94=E3=83=83=E3=82=AF=E3=82=A2=E3=83=83=E3=83=97?= =?UTF-8?q?=E3=81=97=E3=81=BE=E3=81=99=E3=80=82=20UUID=E3=81=AE=E7=89=B9?= =?UTF-8?q?=E5=AE=9A:=20=E8=A6=8B=E3=81=A4=E3=81=8B=E3=81=A3=E3=81=9F?= =?UTF-8?q?=E3=83=8F=E3=83=B3=E3=83=89=E3=83=AB=E3=81=AE=E3=82=B3=E3=83=A1?= =?UTF-8?q?=E3=83=B3=E3=83=88=E6=AC=84=E3=81=8B=E3=82=89=E5=9B=BA=E6=9C=89?= =?UTF-8?q?=E3=81=AE=20ipf-id=20=E3=82=92=E6=8A=9C=E3=81=8D=E5=87=BA?= =?UTF-8?q?=E3=81=97=E3=81=BE=E3=81=99=E3=80=82=20=E4=B8=80=E6=96=89?= =?UTF-8?q?=E6=8E=83=E5=B0=84:=20=E3=81=9D=E3=81=AE=20UUID=20=E3=82=92?= =?UTF-8?q?=E6=8C=81=E3=81=A4=E3=83=AB=E3=83=BC=E3=83=AB=E3=82=92=E3=80=81?= =?UTF-8?q?NAT=EF=BC=88prerouting,=20output,=20postrouting=EF=BC=89?= =?UTF-8?q?=E3=81=A8=20Filter=EF=BC=88forward=EF=BC=89=E3=81=AE=E5=85=A8?= =?UTF-8?q?=E3=83=81=E3=82=A7=E3=83=BC=E3=83=B3=E3=81=8B=E3=82=89=E6=8E=A2?= =?UTF-8?q?=E3=81=97=E5=87=BA=E3=81=97=E3=81=A6=E5=89=8A=E9=99=A4=E3=81=97?= =?UTF-8?q?=E3=81=BE=E3=81=99=E3=80=82=20=E3=81=95=E3=82=89=E3=81=AB?= =?UTF-8?q?=E3=80=8C=E8=AA=BF=E3=81=B9=E3=82=8B=E3=80=8D=E9=9A=9B=E3=81=AE?= =?UTF-8?q?=E3=81=8A=E3=81=99=E3=81=99=E3=82=81=E3=83=86=E3=82=B9=E3=83=88?= =?UTF-8?q?=20=E3=82=82=E3=81=97=E3=81=8A=E6=99=82=E9=96=93=E3=81=82?= =?UTF-8?q?=E3=82=8C=E3=81=B0=E3=80=81=E3=81=93=E3=82=93=E3=81=AA=E3=83=91?= =?UTF-8?q?=E3=82=BF=E3=83=BC=E3=83=B3=E3=82=82=E8=A9=A6=E3=81=97=E3=81=A6?= =?UTF-8?q?=E3=81=BF=E3=81=A6=E3=81=8F=E3=81=A0=E3=81=95=E3=81=84=EF=BC=9A?= =?UTF-8?q?=20=E3=83=9D=E3=83=BC=E3=83=88=E6=8C=87=E5=AE=9A=E5=89=8A?= =?UTF-8?q?=E9=99=A4:=20./ipfn=20-d=20:11434=20=EF=BC=88=E3=83=8F=E3=83=B3?= =?UTF-8?q?=E3=83=89=E3=83=AB=E7=95=AA=E5=8F=B7=E3=82=92=E8=AA=BF=E3=81=B9?= =?UTF-8?q?=E3=81=9A=E3=81=AB=E3=83=9D=E3=83=BC=E3=83=88=E3=81=A7=E6=B6=88?= =?UTF-8?q?=E3=81=9B=E3=82=8B=E3=81=8B=EF=BC=89=20=E6=B7=B7=E5=90=88?= =?UTF-8?q?=E5=89=8A=E9=99=A4:=20./ipfn=20-d=20:80,33=20=EF=BC=88=E3=83=9D?= =?UTF-8?q?=E3=83=BC=E3=83=88=E3=81=A8=E3=83=8F=E3=83=B3=E3=83=89=E3=83=AB?= =?UTF-8?q?=E3=82=92=E6=B7=B7=E3=81=9C=E3=81=A6=E6=B6=88=E3=81=9B=E3=82=8B?= =?UTF-8?q?=E3=81=8B=EF=BC=89=20=E9=9D=99=E3=81=8B=E3=81=AA=E5=89=8A?= =?UTF-8?q?=E9=99=A4:=20./ipfn=20-qd=20:80=20=EF=BC=88=E4=BD=95=E3=82=82?= =?UTF-8?q?=E8=A8=80=E3=82=8F=E3=81=9A=E3=81=AB=E4=B8=80=E7=9E=AC=E3=81=A7?= =?UTF-8?q?=E6=B6=88=E3=81=88=E5=8E=BB=E3=82=8B=E3=81=8B=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ipfn1.0 | 111 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 88 insertions(+), 23 deletions(-) diff --git a/ipfn1.0 b/ipfn1.0 index bdab95a..a5aec64 100755 --- a/ipfn1.0 +++ b/ipfn1.0 @@ -1,8 +1,8 @@ #!/bin/bash # Name: ipfn -# Version: 1.1.6 +# Version: 1.1.9 # Date: 2026-01-22 -# Description: Fixed argument parsing order and combined flags. +# Description: Fixed syntax error and finalized safety guards. if [ "$(id -u)" -ne 0 ]; then exec sudo "$0" "$@" @@ -14,6 +14,8 @@ FORCE_FLAG=false SKIP_TEST=false QUIET_MODE=false +# --- 1. Utility Functions --- + msg() { [[ "$QUIET_MODE" == false ]] && echo -e "$@"; } init_nft() { @@ -24,7 +26,55 @@ init_nft() { nft add chain inet ${TABLE_NAME} forward { type filter hook forward priority 0 \; policy accept \; } 2>/dev/null } -# (parse_delete_targets, list_rules, test_rule_by_port, add_rule などは 1.1.5 と同様) +show_help() { + echo "Usage: ipfn [OPTIONS] [PORT:TARGET_IP:TARGET_PORT]" + echo "" + echo "Options:" + echo " -l, -L List all rules" + echo " -d HANDLE/:PORT Delete by handle or port (e.g., -d 17 or -d :80)" + echo " -df, -qd Force/Quiet delete" + echo " -t [HANDLE] Test connectivity" + echo " -f Skip test / Enable IP forward" + echo " -v Show raw nftables rules" + echo " -h Show this help" +} + +list_rules() { + [[ "$QUIET_MODE" == true ]] && return + local highlight_uuid=$1 + init_nft + msg "Forwarding Rules (ipfn):" + printf "%-10s %-6s %-20s %-20s %-10s\n" "HANDLE" "PROTO" "LOCAL" "TARGET" "UUID" + echo "-----------------------------------------------------------------------------------" + nft -a list chain inet ${TABLE_NAME} prerouting | 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-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) + 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 + printf "%-10s %-6s %-20s %-20s %-10s\n" "$handle" "$proto" ":$lport" "$target" "${full_uuid:0:8}" + fi + done +} + +test_rule_by_port() { + [[ "$QUIET_MODE" == true ]] && return + local lp=$1 + echo -n "Testing connectivity to :$lp... " + if nc -z -v -w 2 127.0.0.1 "$lp" 2>&1 | grep -iqE "succeeded|connected|open"; then + echo -e "\e[32mOK\e[0m" + else + echo -e "\e[31mFAILED\e[0m" + fi +} + +validate_prerouting_handle() { + nft -a list chain inet ${TABLE_NAME} prerouting | grep -q "handle $1" +} + parse_delete_targets() { local input=$1 local -a final_handles=() @@ -33,23 +83,40 @@ parse_delete_targets() { if [[ "$part" =~ ^:([0-9]+)$ ]]; then local target_port="${BASH_REMATCH[1]}" local found_h=$(nft -a list chain inet ${TABLE_NAME} prerouting | grep "dport $target_port" | grep -o 'handle [0-9]*' | awk '{print $2}') - if [[ -n "$found_h" ]]; then - for h in $found_h; do final_handles+=("$h"); done - else - msg "Warning: No rules found for port :$target_port" - fi + for h in $found_h; do final_handles+=("$h"); done elif [[ "$part" =~ ^([0-9]+)-([0-9]+)$ ]]; then - for ((i=${BASH_REMATCH[1]}; i<=${BASH_REMATCH[2]}; i++)); do final_handles+=("$i"); done + for ((i=${BASH_REMATCH[1]}; i<=${BASH_REMATCH[2]}; i++)); do + validate_prerouting_handle "$i" && final_handles+=("$i") + done else - final_handles+=("$part") + validate_prerouting_handle "$part" && final_handles+=("$part") fi done echo "${final_handles[@]}" } -# --- メインパース (順序を修正) --- +add_rule() { + init_nft + local lp=$(echo "$1" | cut -d':' -f1) + local tip=$(echo "$1" | cut -d':' -f2) + local tp=$(echo "$1" | cut -d':' -f3) + [[ "$QUIET_MODE" == false ]] && nft list chain inet ${TABLE_NAME} prerouting | grep -q "dport $lp" && msg "\e[33mWarning: Port :$lp is already in use.\e[0m" + local raw_uuid=$(cat /proc/sys/kernel/random/uuid) + local uuid="ipf-id:$raw_uuid" + local family="ip"; [[ "$tip" =~ : ]] && family="ip6" + nft add rule inet ${TABLE_NAME} prerouting "$PROTO" dport "$lp" dnat $family to "$tip:$tp" comment "\"$uuid\"" + nft add rule inet ${TABLE_NAME} output "$PROTO" dport "$lp" dnat $family to "$tip:$tp" comment "\"$uuid\"" + nft add rule inet ${TABLE_NAME} forward iifname "lo" accept comment "\"$uuid\"" + nft add rule inet ${TABLE_NAME} forward $family daddr "$tip" "$PROTO" dport "$tp" ct state new,established,related accept comment "\"$uuid\"" + nft add rule inet ${TABLE_NAME} forward $family saddr "$tip" "$PROTO" sport "$tp" ct state established,related accept comment "\"$uuid\"" + nft add rule inet ${TABLE_NAME} postrouting $family daddr "$tip" "$PROTO" dport "$tp" masquerade comment "\"$uuid\"" + list_rules "$raw_uuid" + [[ "$SKIP_TEST" == false ]] && { echo ""; test_rule_by_port "$lp"; } +} -# 先読みフラグ +# --- 2. Main Logic --- + +# Pre-scan flags for arg in "$@"; do [[ "$arg" =~ q ]] && QUIET_MODE=true && FORCE_FLAG=true && SKIP_TEST=true [[ "$arg" =~ f ]] && FORCE_FLAG=true && SKIP_TEST=true @@ -62,8 +129,6 @@ while [[ $# -gt 0 ]]; do -h|--help) show_help; exit 0 ;; -v|--verbose) nft list table inet ${TABLE_NAME}; exit 0 ;; -L|-l) list_rules; exit 0 ;; - - # 先に単独の -f や -t を処理する -f) if [[ $# -eq 1 ]]; then sysctl -w net.ipv4.ip_forward=1 >/dev/null @@ -81,21 +146,22 @@ while [[ $# -gt 0 ]]; do msg "Error: Multiple rules exist. Specify a handle."; exit 1 fi fi - # get_port_by_handle ロジック (nftからポートを取得) lp=$(nft -a list chain inet ${TABLE_NAME} prerouting | grep "handle $h_str" | grep -o 'dport [0-9]*' | awk '{print $2}') [[ -n "$lp" ]] && test_rule_by_port "$lp" || msg "Error: Handle $h_str not found." exit 0 ;; - - # その後で削除フラグを含むものを処理 -*[d]*) h_str=$(echo "$1" | sed -E 's/^-q?d?f?//') [[ -z "$h_str" ]] && { h_str="$2"; shift; } handles=($(parse_delete_targets "$h_str")) - [[ ${#handles[@]} -eq 0 ]] && { msg "No targets to delete."; exit 1; } + if [[ ${#handles[@]} -eq 0 ]]; then + msg "No valid prerouting handles found."; exit 1 + fi if [[ "$FORCE_FLAG" == false ]]; then - msg "Review rules to delete:" - for h in "${handles[@]}"; do nft -a list table inet ${TABLE_NAME} | grep "handle $h" | sed 's/^/ /'; done - read -p "Delete these rules? (y/N): " confirm + msg "Review rules to delete (Top-level only):" + for h in "${handles[@]}"; do + nft -a list chain inet ${TABLE_NAME} prerouting | grep "handle $h" | sed 's/^/ /' + done + read -p "Delete these rules and their sub-rules? (y/N): " confirm [[ ! "$confirm" =~ ^[yY]$ ]] && exit 0 fi for h in "${handles[@]}"; do @@ -107,10 +173,9 @@ while [[ $# -gt 0 ]]; do nft delete rule inet ${TABLE_NAME} "$c" handle "$rh" done done - msg "Deleted handle $h" + msg "Deleted associated with handle $h" done list_rules; exit 0 ;; - *) if [[ "$1" =~ ^[0-9]+: ]]; then add_rule "$1"; exit 0 else msg "\e[31mError: Unknown option '$1'\e[0m\n"; show_help; exit 1; fi ;;