186 lines
6.7 KiB
Bash
Executable file
186 lines
6.7 KiB
Bash
Executable file
#!/bin/bash
|
|
# Name: ipfn
|
|
# Version: 1.0.6
|
|
# Date: 2026-01-22
|
|
# Description: Supports -dHANDLE / -dfHANDLE (no space) and asterisk highlighting.
|
|
|
|
# sudo権限チェック
|
|
if [ "$(id -u)" -ne 0 ]; then
|
|
exec sudo "$0" "$@"
|
|
fi
|
|
|
|
TABLE_NAME="ipf_wrapper"
|
|
QUIET=false
|
|
PROTO="tcp"
|
|
FORCE_DELETE=false
|
|
|
|
# --- 初期化 ---
|
|
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
|
|
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} forward { type filter hook forward priority 0 \; policy accept \; } 2>/dev/null
|
|
}
|
|
|
|
# --- ルール表示 (アスタリスク & ハイライト対応) ---
|
|
list_rules() {
|
|
local highlight_uuid=$1
|
|
init_nft
|
|
echo "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)
|
|
short_uuid="${full_uuid:0:8}"
|
|
|
|
if [[ -n "$highlight_uuid" && "$full_uuid" == "$highlight_uuid" ]]; then
|
|
# 新規ルールは HANDLE の前に * を付け、シアン太字で表示
|
|
printf "\e[1;36m*%-9s %-6s %-20s %-20s %-10s\e[0m\n" "$handle" "$proto" ":$lport" "$target" "$short_uuid"
|
|
else
|
|
printf "%-10s %-6s %-20s %-20s %-10s\n" "$handle" "$proto" ":$lport" "$target" "$short_uuid"
|
|
fi
|
|
done
|
|
}
|
|
|
|
# --- 疎通確認 ---
|
|
test_rule() {
|
|
local handle=$1
|
|
init_nft
|
|
local rule_line=$(nft -a list chain inet ${TABLE_NAME} prerouting | grep "handle $handle")
|
|
if [[ -z "$rule_line" ]]; then
|
|
echo "Error: Rule handle $handle not found."
|
|
exit 1
|
|
fi
|
|
local lp=$(echo "$rule_line" | grep -o 'dport [0-9]*' | awk '{print $2}')
|
|
echo "Testing connectivity to local port :$lp..."
|
|
if nc -z -v -w 3 127.0.0.1 "$lp" 2>&1; then
|
|
echo -e "\e[32mSuccess: Port is responding.\e[0m"
|
|
else
|
|
echo -e "\e[31mFailed: Port is NOT responding.\e[0m"
|
|
fi
|
|
}
|
|
|
|
# --- ルール追加 ---
|
|
add_rule() {
|
|
init_nft
|
|
local port_ip_port=$1
|
|
local local_port=$(echo "$port_ip_port" | cut -d':' -f1)
|
|
local target_ip=$(echo "$port_ip_port" | cut -d':' -f2)
|
|
local target_port=$(echo "$port_ip_port" | cut -d':' -f3)
|
|
|
|
local raw_uuid=$(cat /proc/sys/kernel/random/uuid)
|
|
local uuid="ipf-id:$raw_uuid"
|
|
local comment="comment \"$uuid\""
|
|
local family="ip"
|
|
[[ "$target_ip" =~ : ]] && family="ip6"
|
|
|
|
nft add rule inet ${TABLE_NAME} prerouting "$PROTO" dport "$local_port" dnat $family to "$target_ip:$target_port" "$comment"
|
|
nft add rule inet ${TABLE_NAME} output "$PROTO" dport "$local_port" dnat $family to "$target_ip:$target_port" "$comment"
|
|
nft add rule inet ${TABLE_NAME} forward iifname "lo" accept "$comment"
|
|
nft add rule inet ${TABLE_NAME} forward $family daddr "$target_ip" "$PROTO" dport "$target_port" ct state new,established,related accept "$comment"
|
|
nft add rule inet ${TABLE_NAME} forward $family saddr "$target_ip" "$PROTO" sport "$target_port" ct state established,related accept "$comment"
|
|
nft add rule inet ${TABLE_NAME} postrouting $family daddr "$target_ip" "$PROTO" dport "$target_port" masquerade "$comment"
|
|
|
|
echo "ipfn: Rule added."
|
|
echo ""
|
|
list_rules "$raw_uuid"
|
|
}
|
|
|
|
# --- ルール削除 ---
|
|
delete_rule() {
|
|
init_nft
|
|
local handle=$1
|
|
local rule_info=$(nft -a list chain inet ${TABLE_NAME} prerouting | grep "handle $handle")
|
|
if [[ -z "$rule_info" ]]; then
|
|
echo "Error: Rule handle $handle not found."
|
|
exit 1
|
|
fi
|
|
|
|
local uuid=$(echo "$rule_info" | grep -o 'ipf-id:[a-z0-9-]*' | head -n 1)
|
|
local lport=$(echo "$rule_info" | grep -o 'dport [0-9]*' | awk '{print $2}')
|
|
local target=$(echo "$rule_info" | grep -oE '[0-9.]*:[0-9]+' | head -n 1)
|
|
|
|
local do_delete=false
|
|
if [[ "$FORCE_DELETE" == true ]]; then
|
|
do_delete=true
|
|
else
|
|
echo "About to delete rule group:"
|
|
echo " Handle: $handle"
|
|
echo " Local: :$lport"
|
|
echo " Target: $target"
|
|
echo -n "Are you sure? (y/N): "
|
|
read -r confirm
|
|
[[ "$confirm" =~ ^[yY]$ ]] && do_delete=true
|
|
fi
|
|
|
|
if [[ "$do_delete" == true ]]; then
|
|
for chain in prerouting output forward postrouting; do
|
|
nft -a list chain inet ${TABLE_NAME} "$chain" | grep "$uuid" | grep -o 'handle [0-9]*' | awk '{print $2}' | while read -r h; do
|
|
nft delete rule inet ${TABLE_NAME} "$chain" handle "$h"
|
|
done
|
|
done
|
|
echo "ipfn: Rule group deleted."
|
|
echo ""
|
|
list_rules
|
|
else
|
|
echo "Aborted."
|
|
fi
|
|
}
|
|
|
|
# --- メイン処理 ---
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
-L|-l) list_rules; exit 0 ;;
|
|
|
|
# 強制削除 (-df または -df17)
|
|
-df*)
|
|
FORCE_DELETE=true
|
|
handle="${1#-df}"
|
|
if [[ -z "$handle" ]]; then handle="$2"; shift; fi
|
|
delete_rule "$handle"
|
|
exit 0 ;;
|
|
|
|
# 通常削除 (-d または -d17)
|
|
-d*)
|
|
handle="${1#-d}"
|
|
if [[ -z "$handle" ]]; then handle="$2"; shift; fi
|
|
delete_rule "$handle"
|
|
exit 0 ;;
|
|
|
|
# テスト (-t または -t17)
|
|
-t*)
|
|
handle="${1#-t}"
|
|
if [[ -z "$handle" ]]; then handle="$2"; shift; fi
|
|
test_rule "$handle"
|
|
exit 0 ;;
|
|
|
|
-p) PROTO="$2"; shift 2 ;;
|
|
-q) QUIET=true; shift ;;
|
|
-f)
|
|
# 削除フラグとして機能させるための先読み
|
|
FORCE_DELETE=true
|
|
if [[ $# -eq 1 ]]; then
|
|
sysctl -w net.ipv4.ip_forward=1
|
|
sysctl -w net.ipv4.conf.all.route_localnet=1
|
|
echo "Kernel parameters updated."
|
|
exit 0
|
|
fi
|
|
;;
|
|
-v) nft list table inet ${TABLE_NAME}; exit 0 ;;
|
|
*)
|
|
if [[ "$1" =~ ^[0-9]+: ]]; then
|
|
add_rule "$1"
|
|
exit 0
|
|
fi
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
echo "Usage: ipfn [PORT:IP:PORT | -L | -d(HANDLE) | -df(HANDLE) | -t(HANDLE) | -f]"
|