Finalizing nsenter lines.

This commit is contained in:
2021-06-16 09:43:49 +00:00
parent e4bcab313f
commit e5b4dc4675

View File

@@ -10,6 +10,34 @@ echo 1 > /proc/sys/net/ipv4/ip_forward
############################## ##############################
###############################
COUNT_SOURCE_IP=$(set |grep SOURCE_IP |wc -l)
COUNT_SOURCE_PORT=$(set |grep SOURCE_PORT |wc -l)
COUNT_TARGET_IP=$(set |grep TARGET_IP |wc -l)
COUNT_TARGET_PORT=$(set |grep TARGET_PORT |wc -l)
for source_ip_index in $(seq 1 $COUNT_SOURCE_IP) ; do
if set |grep SOURCE_IP_ ; then
SOURCE_IP=$(eval "echo \${"SOURCE_IP_$source_ip_index"}")
fi
for source_port_index in $(seq 1 $COUNT_SOURCE_PORT) ; do
if set |grep SOURCE_PORT_ ; then
SOURCE_PORT=$(eval "echo \${"SOURCE_PORT_$source_port_index"}")
fi
for target_ip_index in $(seq 1 $COUNT_TARGET_IP) ; do
if set |grep TARGET_IP_ ; then
TARGET_IP=$(eval "echo \${"TARGET_IP_$target_ip_index"}")
fi
for target_port_index in $(seq 1 $COUNT_TARGET_PORT) ; do
if set |grep TARGET_PORT_ ; then
TARGET_PORT=$(eval "echo \${"TARGET_PORT_$target_port_index"}")
fi
############################# #############################
# NSENTER Specific settings # # NSENTER Specific settings #
@@ -22,39 +50,28 @@ iptables="nsenter -t $(docker inspect --format {{.State.Pid}} $NAME) -n -- /sbin
else $iptables -I POSTROUTING -m state --state established,related -j ACCEPT; else $iptables -I POSTROUTING -m state --state established,related -j ACCEPT;
fi fi
# DELETE UNECESSARY LINES FROM PREVIOUS RULES
LINES=$($iptables --line-number -n --list POSTROUTING | grep $SOURCE_IP |grep $TARGET_IP |grep $PROTOCOL |grep $SOURCE_PORT |grep $TARGET_PORT | awk '{print $1}'| tac)
if [ -n "$LINES" ] ; then
for i in $LINES; do
$iptables -D POSTROUTING $i
sleep 0.1
done
fi
$iptables -I PREROUTING -d $SOURCE_IP -p $PROTOCOL --dport $SOURCE_PORT -m comment --comment "$COMMENT" -j DNAT --to $TARGET_IP:$TARGET_PORT
else else
iptables="/sbin/iptables-legacy" iptables="/sbin/iptables-legacy"
if $iptables --list $CHAIN |grep ESTABLISHED |grep RELATED|grep ACCEPT ; then if $iptables --list $CHAIN |grep ESTABLISHED |grep RELATED|grep ACCEPT ; then
else $iptables -I $CHAIN -m state --state established,related -j ACCEPT; else $iptables -I $CHAIN -m state --state established,related -j ACCEPT;
fi fi
fi
# #
# DELETE UNECESSARY LINES FROM PREVIOUS RULES
############################### LINES=$($iptables --line-number -n --list $CHAIN | grep $SOURCE_IP |grep $TARGET_IP |grep $PROTOCOL |grep $TARGET_PORT | awk '{print $1}'| tac)
COUNT_SOURCE_IP=$(set |grep SOURCE_IP |wc -l)
COUNT_TARGET_IP=$(set |grep TARGET_IP |wc -l)
COUNT_TARGET_PORT=$(set |grep TARGET_PORT |wc -l)
for source_ip_index in $(seq 1 $COUNT_SOURCE_IP) ; do
if set |grep SOURCE_IP_ ; then
SOURCE_IP=$(eval "echo \${"SOURCE_IP_$source_ip_index"}")
fi
for target_ip_index in $(seq 1 $COUNT_TARGET_IP) ; do
if set |grep TARGET_IP_ ; then
TARGET_IP=$(eval "echo \${"TARGET_IP_$target_ip_index"}")
fi
for target_port_index in $(seq 1 $COUNT_TARGET_PORT) ; do
if set |grep TARGET_PORT_ ; then
TARGET_PORT=$(eval "echo \${"TARGET_PORT_$target_port_index"}")
fi
# DELETE UNECESSARY LINES FROM PREVIOUS RULES
LINES=$($iptables --line-number -n --list $CHAIN | grep $SOURCE_IP |grep $TARGET_IP |grep $PROTOCOL |grep $TARGET_PORT | awk '{print $1}'| tac)
if [ -n "$LINES" ] ; then if [ -n "$LINES" ] ; then
for i in $LINES; do for i in $LINES; do
@@ -63,7 +80,12 @@ for source_ip_index in $(seq 1 $COUNT_SOURCE_IP) ; do
done done
fi fi
$iptables -I $CHAIN -s $SOURCE_IP -d $TARGET_IP -p $PROTOCOL --dport $TARGET_PORT -m comment --comment "$COMMENT" -j ACCEPT $iptables -I $CHAIN -s $SOURCE_IP -d $TARGET_IP -p $PROTOCOL --dport $TARGET_PORT -m comment --comment "$COMMENT" -j ACCEPT
#############################
fi
done
done done
done done
done done