diff --git a/hook/singleton.sh b/hook/singleton.sh index 6366e34..b3e3dc4 100644 --- a/hook/singleton.sh +++ b/hook/singleton.sh @@ -6,34 +6,34 @@ # if [[ -d "/host/proc/1/" ]]; then source /apps/gitrce/hook/singleton.sh "$0"; fi __singleton_pattern() { - # 实现单例模式,确保同一时刻只有一个脚本实例在运行 + # 实现单例模式,确保同一时刻只有一个脚本实例在运行 - _script_path_this=$(realpath "$(ps -p $$ -o args= 2>/dev/null | awk '{print $2}')") + _script_path_this=$(realpath "$(ps -p $$ -o args= 2>/dev/null | awk '{print $2}')") - # 生成 PID 文件的路径 - _pid_name="$(echo "$_script_path_this" | sed 's:^/::; s/\//#/g; s/\.sh$/.pid/')" # 还原 echo "$result" | sed 's:^:/:' | sed 's/#/\//g; s/\.pid$/.sh/' - _pid_file="/apps/pid/$_pid_name" + # 生成 PID 文件的路径 + _pid_name="$(echo "$_script_path_this" | sed 's:^/::; s/\//#/g; s/\.sh$/.pid/')" # 还原 echo "$result" | sed 's:^:/:' | sed 's/#/\//g; s/\.pid$/.sh/' + _pid_file="/apps/pid/$_pid_name" - # 创建 PID 文件所在的目录 - mkdir -p "${_pid_file%/*}" + # 创建 PID 文件所在的目录 + mkdir -p "${_pid_file%/*}" - # 从 PID 文件中读取存储的 PID - _pid_data=$(cat "$_pid_file" 2>/dev/null) + # 从 PID 文件中读取存储的 PID + _pid_data=$(cat "$_pid_file" 2>/dev/null) - # 根据存储的 PID 获取对应脚本的绝对路径 - _script_path_pid=$(realpath "$(ps -p "$_pid_data" -o args= 2>/dev/null | awk '{print $2}')" 2>/dev/null) - if [[ "$_script_path_this" != "$_script_path_pid" ]]; then - # 如果当前脚本路径与存储的脚本路径不同,则更新 PID 文件并继续执行 - echo "$$" >"$_pid_file" - else - # 如果相同,则说明已有实例在运行,输出相关信息并退出 - echo "脚本单例模式运行, 已存在运行实例,当前脚本已退出" - echo "_pid_file: $_pid_file" - echo "_pid_data: $_pid_data" - echo "_script_path_this: $_script_path_this" - echo "_script_path_pid : $_script_path_pid" - exit 0 - fi + # 根据存储的 PID 获取对应脚本的绝对路径 + _script_path_pid=$(realpath "$(ps -p "$_pid_data" -o args= 2>/dev/null | awk '{print $2}')" 2>/dev/null) + if [[ "$_script_path_this" != "$_script_path_pid" ]]; then + # 如果当前脚本路径与存储的脚本路径不同,则更新 PID 文件并继续执行 + echo "$$" >"$_pid_file" + else + # 如果相同,则说明已有实例在运行,输出相关信息并退出 + echo "脚本单例模式运行, 已存在运行实例,当前脚本已退出" + echo "_pid_file: $_pid_file" + echo "_pid_data: $_pid_data" + echo "_script_path_this: $_script_path_this" + echo "_script_path_pid : $_script_path_pid" + exit 0 + fi } __singleton_pattern diff --git a/netflow/iptables.sh b/netflow/iptables.sh index 3319f6f..9f238d7 100644 --- a/netflow/iptables.sh +++ b/netflow/iptables.sh @@ -3,20 +3,29 @@ if [[ -d "/host/proc/1/" ]]; then source /apps/gitrce/hook/singleton.sh "$0"; fi -_owner_id=50000 -_chain="output_netflow_owner_${_owner_id}" +__main() { + : + update-alternatives --set iptables /usr/sbin/iptables-legacy 2>/dev/null -for cmd in iptables ip6tables; do - $cmd -t mangle -N "$_chain" 2>/dev/null || true + _owner_id=50000 + _chain="output_netflow_owner_${_owner_id}" - if ! $cmd -t mangle -C OUTPUT -m owner --gid-owner "$_owner_id" -j "$_chain" 2>/dev/null; then - $cmd -t mangle -A OUTPUT -m owner --gid-owner "$_owner_id" -j "$_chain" - fi + for cmd in iptables ip6tables; do + $cmd -t mangle -N "$_chain" 2>/dev/null || true - if ! $cmd -t mangle -C "$_chain" -j RETURN 2>/dev/null; then - $cmd -t mangle -A "$_chain" -j RETURN - fi -done + if ! $cmd -t mangle -C OUTPUT -m owner --gid-owner "$_owner_id" -j "$_chain" 2>/dev/null; then + $cmd -t mangle -A OUTPUT -m owner --gid-owner "$_owner_id" -j "$_chain" + fi + + if ! $cmd -t mangle -C "$_chain" -j RETURN 2>/dev/null; then + $cmd -t mangle -A "$_chain" -j RETURN + fi + $cmd -t mangle -L OUTPUT -v -n -x + done + +} + +__main __help() { cat >/dev/null <<-'EOF'