Skip to content

Instantly share code, notes, and snippets.

@clems4ever
Last active July 18, 2019 17:49
Show Gist options
  • Save clems4ever/55888f04ef78d519554add110aab126a to your computer and use it in GitHub Desktop.
Save clems4ever/55888f04ef78d519554add110aab126a to your computer and use it in GitHub Desktop.
docker-swarm iptables FORWARD custom chain enforcement
[Unit]
Description=Public filter enforcement Service
[Service]
Type=simple
ExecStart=/home/user/custom-chain-enforcement.sh
KillMode=mixed
TimeoutStartSec=0
RestartSec=0
Restart=always
[Install]
WantedBy=multi-user.target
Alias=public-filter.service
#!/bin/bash
CUSTOM_CHAIN=PUBLIC_FILTER
DELAY=10
while [ True ];
do
custom_chain_position=`iptables-save | grep -e "-A FORWARD" | grep --line-number -e "-A FORWARD -j ${CUSTOM_CHAIN}" | sed 's/\([0-9]\+\):.*/\1/g'`
if [[ -z "$custom_chain_position" ]]
then
echo "`date`: Insert custom chain."
iptables -I FORWARD -j ${CUSTOM_CHAIN}
elif [[ "$custom_chain_position" -ne "1" ]]
then
echo "`date`: Enforce the priority of custom chain."
iptables -D FORWARD ${custom_chain_position}
iptables -I FORWARD -j ${CUSTOM_CHAIN}
fi
sleep $DELAY
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment