コンテナやVMに対応
なるべく生IP維持
This commit is contained in:
parent
79d11fecf7
commit
8cdccae6bf
1 changed files with 85 additions and 33 deletions
118
ipfn1.0
118
ipfn1.0
|
|
@ -1,8 +1,8 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# Name: ipfn
|
# Name: ipfn
|
||||||
# Version: 1.1.9
|
# Version: 1.2.4
|
||||||
# Date: 2026-01-22
|
# Date: 2026-01-22
|
||||||
# Description: Fixed syntax error and finalized safety guards.
|
# Description: Enhanced for VM/LXD/Docker with bridge-nf tuning and robust forwarding.
|
||||||
|
|
||||||
if [ "$(id -u)" -ne 0 ]; then
|
if [ "$(id -u)" -ne 0 ]; then
|
||||||
exec sudo "$0" "$@"
|
exec sudo "$0" "$@"
|
||||||
|
|
@ -14,29 +14,48 @@ FORCE_FLAG=false
|
||||||
SKIP_TEST=false
|
SKIP_TEST=false
|
||||||
QUIET_MODE=false
|
QUIET_MODE=false
|
||||||
|
|
||||||
# --- 1. Utility Functions ---
|
# --- 1. Utility & Core Setup ---
|
||||||
|
|
||||||
msg() { [[ "$QUIET_MODE" == false ]] && echo -e "$@"; }
|
msg() { [[ "$QUIET_MODE" == false ]] && echo -e "$@"; }
|
||||||
|
|
||||||
init_nft() {
|
init_nft() {
|
||||||
nft add table inet ${TABLE_NAME} 2>/dev/null
|
nft add table inet ${TABLE_NAME} 2>/dev/null
|
||||||
|
# NAT Chains
|
||||||
nft add chain inet ${TABLE_NAME} prerouting { type nat hook prerouting priority -100 \; } 2>/dev/null
|
nft add chain inet ${TABLE_NAME} prerouting { type nat hook prerouting priority -100 \; } 2>/dev/null
|
||||||
nft add chain inet ${TABLE_NAME} output { type nat hook output priority -100 \; } 2>/dev/null
|
nft add chain inet ${TABLE_NAME} output { type nat hook output priority -100 \; } 2>/dev/null
|
||||||
nft add chain inet ${TABLE_NAME} postrouting { type nat hook postrouting priority 100 \; } 2>/dev/null
|
nft add chain inet ${TABLE_NAME} postrouting { type nat hook postrouting priority 100 \; } 2>/dev/null
|
||||||
|
# Filter Chain (For VM/CT Forwarding)
|
||||||
nft add chain inet ${TABLE_NAME} forward { type filter hook forward priority 0 \; policy accept \; } 2>/dev/null
|
nft add chain inet ${TABLE_NAME} forward { type filter hook forward priority 0 \; policy accept \; } 2>/dev/null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enable_forwarding() {
|
||||||
|
msg "Optimizing kernel parameters for VM/LXD/Docker..."
|
||||||
|
sysctl -w net.ipv4.ip_forward=1 >/dev/null
|
||||||
|
sysctl -w net.ipv6.conf.all.forwarding=1 >/dev/null
|
||||||
|
sysctl -w net.ipv4.conf.all.route_localnet=1 >/dev/null
|
||||||
|
# ブリッジパケットをNetfilterに渡す設定 (存在する場合のみ)
|
||||||
|
sysctl -w net.bridge.bridge-nf-call-iptables=1 2>/dev/null
|
||||||
|
sysctl -w net.bridge.bridge-nf-call-ip6tables=1 2>/dev/null
|
||||||
|
msg "\e[32mForwarding and Bridge-NF enabled.\e[0m"
|
||||||
|
}
|
||||||
|
|
||||||
show_help() {
|
show_help() {
|
||||||
echo "Usage: ipfn [OPTIONS] [PORT:TARGET_IP:TARGET_PORT]"
|
echo "Usage: ipfn [OPTIONS] [RULES]"
|
||||||
|
echo ""
|
||||||
|
echo "Rules Format:"
|
||||||
|
echo " 80:10.10.100.5:8080 Full (LocalPort:TargetIP:TargetPort)"
|
||||||
|
echo " 80:8080 IP defaults to 127.0.0.1"
|
||||||
|
echo " 11434 Map same port to 127.0.0.1"
|
||||||
echo ""
|
echo ""
|
||||||
echo "Options:"
|
echo "Options:"
|
||||||
echo " -l, -L List all rules"
|
echo " -l, -L List all rules"
|
||||||
echo " -d HANDLE/:PORT Delete by handle or port (e.g., -d 17 or -d :80)"
|
echo " -d HANDLE/:PORT/all Delete specific rules or '*' for all"
|
||||||
echo " -df, -qd Force/Quiet delete"
|
echo " -R Reset: Clear ALL rules immediately"
|
||||||
echo " -t [HANDLE] Test connectivity"
|
echo " -q Quiet mode (No output, Auto-yes)"
|
||||||
echo " -f Skip test / Enable IP forward"
|
echo " -t [HANDLE] Test connectivity"
|
||||||
echo " -v Show raw nftables rules"
|
echo " -f Enable IP forward & Bridge tuning / Skip test"
|
||||||
echo " -h Show this help"
|
echo " -v Verbose (raw nftables output)"
|
||||||
|
echo " -h Show this help"
|
||||||
}
|
}
|
||||||
|
|
||||||
list_rules() {
|
list_rules() {
|
||||||
|
|
@ -49,7 +68,7 @@ list_rules() {
|
||||||
nft -a list chain inet ${TABLE_NAME} prerouting | grep "dnat" | while read -r line; do
|
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}')
|
handle=$(echo "$line" | grep -o 'handle [0-9]*' | awk '{print $2}')
|
||||||
proto=$(echo "$line" | grep -q "udp" && echo "udp" || echo "tcp")
|
proto=$(echo "$line" | grep -q "udp" && echo "udp" || echo "tcp")
|
||||||
target=$(echo "$line" | grep -oE '[0-9.]*:[0-9]+' | head -n 1)
|
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}')
|
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)
|
full_uuid=$(echo "$line" | grep -o 'ipf-id:[a-z0-9-]*' | cut -d':' -f2)
|
||||||
if [[ -n "$highlight_uuid" && "$full_uuid" == "$highlight_uuid" ]]; then
|
if [[ -n "$highlight_uuid" && "$full_uuid" == "$highlight_uuid" ]]; then
|
||||||
|
|
@ -71,13 +90,28 @@ test_rule_by_port() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
validate_prerouting_handle() {
|
# --- 2. Deletion Logic ---
|
||||||
nft -a list chain inet ${TABLE_NAME} prerouting | grep -q "handle $1"
|
|
||||||
|
validate_rule_handle() {
|
||||||
|
nft -a list chain inet ${TABLE_NAME} prerouting | grep "handle $1" | grep -q "dnat"
|
||||||
|
}
|
||||||
|
|
||||||
|
all_clear() {
|
||||||
|
if [[ "$FORCE_FLAG" == false ]]; then
|
||||||
|
msg "\e[33mWarning: This will delete ALL forwarding 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 rules cleared successfully."
|
||||||
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
parse_delete_targets() {
|
parse_delete_targets() {
|
||||||
local input=$1
|
local input=$1
|
||||||
local -a final_handles=()
|
local -a final_handles=()
|
||||||
|
[[ "$input" == "*" || "$input" == "all" ]] && all_clear
|
||||||
IFS=',' read -r -a parts <<< "$input"
|
IFS=',' read -r -a parts <<< "$input"
|
||||||
for part in "${parts[@]}"; do
|
for part in "${parts[@]}"; do
|
||||||
if [[ "$part" =~ ^:([0-9]+)$ ]]; then
|
if [[ "$part" =~ ^:([0-9]+)$ ]]; then
|
||||||
|
|
@ -86,42 +120,62 @@ parse_delete_targets() {
|
||||||
for h in $found_h; do final_handles+=("$h"); done
|
for h in $found_h; do final_handles+=("$h"); done
|
||||||
elif [[ "$part" =~ ^([0-9]+)-([0-9]+)$ ]]; then
|
elif [[ "$part" =~ ^([0-9]+)-([0-9]+)$ ]]; then
|
||||||
for ((i=${BASH_REMATCH[1]}; i<=${BASH_REMATCH[2]}; i++)); do
|
for ((i=${BASH_REMATCH[1]}; i<=${BASH_REMATCH[2]}; i++)); do
|
||||||
validate_prerouting_handle "$i" && final_handles+=("$i")
|
validate_rule_handle "$i" && final_handles+=("$i")
|
||||||
done
|
done
|
||||||
else
|
else
|
||||||
validate_prerouting_handle "$part" && final_handles+=("$part")
|
validate_rule_handle "$part" && final_handles+=("$part")
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
echo "${final_handles[@]}"
|
echo "${final_handles[@]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# --- 3. Adding Logic ---
|
||||||
|
|
||||||
add_rule() {
|
add_rule() {
|
||||||
init_nft
|
init_nft
|
||||||
local lp=$(echo "$1" | cut -d':' -f1)
|
local raw_input=$1
|
||||||
local tip=$(echo "$1" | cut -d':' -f2)
|
local lp tip tp
|
||||||
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"
|
if [[ "$raw_input" =~ ^([0-9]+):([0-9\.]+):([0-9]+)$ ]]; then
|
||||||
|
lp=${BASH_REMATCH[1]}; tip=${BASH_REMATCH[2]}; tp=${BASH_REMATCH[3]}
|
||||||
|
elif [[ "$raw_input" =~ ^([0-9]+):([0-9]+)$ ]]; then
|
||||||
|
lp=${BASH_REMATCH[1]}; tip="127.0.0.1"; tp=${BASH_REMATCH[2]}
|
||||||
|
elif [[ "$raw_input" =~ ^([0-9]+)$ ]]; then
|
||||||
|
lp=${BASH_REMATCH[1]}; tip="127.0.0.1"; tp=${BASH_REMATCH[1]}
|
||||||
|
else
|
||||||
|
msg "\e[31mError: Invalid rule format '$raw_input'\e[0m"; exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
local raw_uuid=$(cat /proc/sys/kernel/random/uuid)
|
local raw_uuid=$(cat /proc/sys/kernel/random/uuid)
|
||||||
local uuid="ipf-id:$raw_uuid"
|
local uuid="ipf-id:$raw_uuid"
|
||||||
local family="ip"; [[ "$tip" =~ : ]] && family="ip6"
|
local family="ip"; [[ "$tip" =~ : ]] && family="ip6"
|
||||||
|
|
||||||
|
# DNAT
|
||||||
nft add rule inet ${TABLE_NAME} prerouting "$PROTO" dport "$lp" dnat $family to "$tip:$tp" comment "\"$uuid\""
|
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} output "$PROTO" dport "$lp" dnat $family to "$tip:$tp" comment "\"$uuid\""
|
||||||
nft add rule inet ${TABLE_NAME} forward iifname "lo" accept comment "\"$uuid\""
|
|
||||||
|
# FORWARD (VM/CT対応)
|
||||||
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 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} forward $family saddr "$tip" "$PROTO" sport "$tp" ct state established,related accept comment "\"$uuid\""
|
||||||
|
nft add rule inet ${TABLE_NAME} forward iifname "lo" accept comment "\"$uuid\""
|
||||||
|
|
||||||
|
# POSTROUTING (MASQUERADE: 戻りパケットの保証)
|
||||||
nft add rule inet ${TABLE_NAME} postrouting $family daddr "$tip" "$PROTO" dport "$tp" masquerade comment "\"$uuid\""
|
nft add rule inet ${TABLE_NAME} postrouting $family daddr "$tip" "$PROTO" dport "$tp" masquerade comment "\"$uuid\""
|
||||||
|
|
||||||
list_rules "$raw_uuid"
|
list_rules "$raw_uuid"
|
||||||
[[ "$SKIP_TEST" == false ]] && { echo ""; test_rule_by_port "$lp"; }
|
[[ "$SKIP_TEST" == false ]] && { echo ""; test_rule_by_port "$lp"; }
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- 2. Main Logic ---
|
# --- 4. Main Parsing ---
|
||||||
|
|
||||||
# Pre-scan flags
|
|
||||||
for arg in "$@"; do
|
for arg in "$@"; do
|
||||||
[[ "$arg" =~ q ]] && QUIET_MODE=true && FORCE_FLAG=true && SKIP_TEST=true
|
[[ "$arg" =~ q ]] && QUIET_MODE=true && FORCE_FLAG=true && SKIP_TEST=true
|
||||||
[[ "$arg" =~ f ]] && FORCE_FLAG=true && SKIP_TEST=true
|
[[ "$arg" =~ f ]] && FORCE_FLAG=true && SKIP_TEST=true
|
||||||
|
[[ "$arg" == "-R" ]] && RESET_MODE=true
|
||||||
done
|
done
|
||||||
|
|
||||||
|
if [[ "$RESET_MODE" == true ]]; then all_clear; fi
|
||||||
|
|
||||||
if [[ $# -eq 0 ]]; then list_rules; exit 0; fi
|
if [[ $# -eq 0 ]]; then list_rules; exit 0; fi
|
||||||
|
|
||||||
while [[ $# -gt 0 ]]; do
|
while [[ $# -gt 0 ]]; do
|
||||||
|
|
@ -130,11 +184,8 @@ while [[ $# -gt 0 ]]; do
|
||||||
-v|--verbose) nft list table inet ${TABLE_NAME}; exit 0 ;;
|
-v|--verbose) nft list table inet ${TABLE_NAME}; exit 0 ;;
|
||||||
-L|-l) list_rules; exit 0 ;;
|
-L|-l) list_rules; exit 0 ;;
|
||||||
-f)
|
-f)
|
||||||
if [[ $# -eq 1 ]]; then
|
enable_forwarding
|
||||||
sysctl -w net.ipv4.ip_forward=1 >/dev/null
|
[[ $# -eq 1 ]] && exit 0 ;;
|
||||||
sysctl -w net.ipv4.conf.all.route_localnet=1 >/dev/null
|
|
||||||
msg "Kernel parameters updated."; exit 0
|
|
||||||
fi ;;
|
|
||||||
-t*)
|
-t*)
|
||||||
h_str="${1#-t}"
|
h_str="${1#-t}"
|
||||||
if [[ -z "$h_str" && ( "$2" =~ ^[0-9,-]+$ ) ]]; then h_str="$2"; shift; fi
|
if [[ -z "$h_str" && ( "$2" =~ ^[0-9,-]+$ ) ]]; then h_str="$2"; shift; fi
|
||||||
|
|
@ -153,9 +204,7 @@ while [[ $# -gt 0 ]]; do
|
||||||
h_str=$(echo "$1" | sed -E 's/^-q?d?f?//')
|
h_str=$(echo "$1" | sed -E 's/^-q?d?f?//')
|
||||||
[[ -z "$h_str" ]] && { h_str="$2"; shift; }
|
[[ -z "$h_str" ]] && { h_str="$2"; shift; }
|
||||||
handles=($(parse_delete_targets "$h_str"))
|
handles=($(parse_delete_targets "$h_str"))
|
||||||
if [[ ${#handles[@]} -eq 0 ]]; then
|
[[ ${#handles[@]} -eq 0 ]] && { msg "No valid targets."; exit 1; }
|
||||||
msg "No valid prerouting handles found."; exit 1
|
|
||||||
fi
|
|
||||||
if [[ "$FORCE_FLAG" == false ]]; then
|
if [[ "$FORCE_FLAG" == false ]]; then
|
||||||
msg "Review rules to delete (Top-level only):"
|
msg "Review rules to delete (Top-level only):"
|
||||||
for h in "${handles[@]}"; do
|
for h in "${handles[@]}"; do
|
||||||
|
|
@ -177,8 +226,11 @@ while [[ $# -gt 0 ]]; do
|
||||||
done
|
done
|
||||||
list_rules; exit 0 ;;
|
list_rules; exit 0 ;;
|
||||||
*)
|
*)
|
||||||
if [[ "$1" =~ ^[0-9]+: ]]; then add_rule "$1"; exit 0
|
if [[ "$1" =~ ^[0-9] ]]; then
|
||||||
else msg "\e[31mError: Unknown option '$1'\e[0m\n"; show_help; exit 1; fi ;;
|
add_rule "$1"; exit 0
|
||||||
|
else
|
||||||
|
msg "\e[31mError: Unknown option '$1'\e[0m\n"; show_help; exit 1
|
||||||
|
fi ;;
|
||||||
esac
|
esac
|
||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue