1
0
mirror of https://github.com/juliengk/csf-post-docker.git synced 2023-10-10 13:37:41 +02:00
csf-post-docker/docker.sh

62 lines
2.8 KiB
Bash
Raw Normal View History

2015-07-22 05:53:29 +02:00
#!/bin/bash
2016-04-12 16:21:31 +02:00
DOCKER_DIR="/bin"
IPTABLES_DIR="/sbin"
2016-03-12 20:25:09 +01:00
chain_exists() {
[ $# -lt 1 -o $# -gt 2 ] && {
echo "Usage: chain_exists <chain_name> [table]" >&2
return 1
}
local chain_name="$1" ; shift
[ $# -eq 1 ] && local table="--table $1"
2016-04-12 16:21:31 +02:00
${IPTABLES_DIR}/iptables $table -n --list "$chain_name" >/dev/null 2>&1
}
2015-07-22 05:53:29 +02:00
DOCKER_INT="docker0"
DOCKER_NETWORK="172.17.0.0/16"
2016-04-12 16:21:31 +02:00
${IPTABLES_DIR}/iptables-save | grep -v -- '-j DOCKER' | ${IPTABLES_DIR}/iptables-restore
chain_exists DOCKER && ${IPTABLES_DIR}/iptables -X DOCKER
chain_exists DOCKER nat && ${IPTABLES_DIR}/iptables -t nat -X DOCKER
2016-04-12 16:21:31 +02:00
${IPTABLES_DIR}/iptables -N DOCKER
${IPTABLES_DIR}/iptables -t nat -N DOCKER
2015-07-22 05:53:29 +02:00
2016-04-12 16:21:31 +02:00
${IPTABLES_DIR}/iptables -A FORWARD -o ${DOCKER_INT} -j DOCKER
${IPTABLES_DIR}/iptables -A FORWARD -o ${DOCKER_INT} -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
${IPTABLES_DIR}/iptables -A FORWARD -i ${DOCKER_INT} ! -o ${DOCKER_INT} -j ACCEPT
${IPTABLES_DIR}/iptables -A FORWARD -i ${DOCKER_INT} -o ${DOCKER_INT} -j ACCEPT
2015-07-22 05:53:29 +02:00
2016-04-12 16:21:31 +02:00
${IPTABLES_DIR}/iptables -t nat -A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
${IPTABLES_DIR}/iptables -t nat -A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
${IPTABLES_DIR}/iptables -t nat -A POSTROUTING -s ${DOCKER_NETWORK} ! -o ${DOCKER_INT} -j MASQUERADE
2015-07-22 05:53:29 +02:00
2016-04-12 16:21:31 +02:00
containers=`${DOCKER_DIR}/docker ps -q`
2015-07-22 05:53:29 +02:00
if [ `echo ${containers} | wc -c` -gt "1" ] ; then
for container in ${containers} ; do
2016-04-12 16:21:31 +02:00
rules=`${DOCKER_DIR}/docker port ${container} | sed 's/ //g'`
2015-07-22 05:53:29 +02:00
if [ `echo ${rules} | wc -c` -gt "1" ] ; then
2016-04-12 16:21:31 +02:00
ipaddr=`${DOCKER_DIR}/docker inspect -f "{{.NetworkSettings.IPAddress}}" ${container}`
2015-07-22 05:53:29 +02:00
for rule in ${rules} ; do
src=`echo ${rule} | awk -F'->' '{ print $2 }'`
dst=`echo ${rule} | awk -F'->' '{ print $1 }'`
src_ip=`echo ${src} | awk -F':' '{ print $1 }'`
src_port=`echo ${src} | awk -F':' '{ print $2 }'`
dst_port=`echo ${dst} | awk -F'/' '{ print $1 }'`
dst_proto=`echo ${dst} | awk -F'/' '{ print $2 }'`
2016-04-12 16:21:31 +02:00
${IPTABLES_DIR}/iptables -A DOCKER -d ${ipaddr}/32 ! -i ${DOCKER_INT} -o ${DOCKER_INT} -p ${dst_proto} -m ${dst_proto} --dport ${dst_port} -j ACCEPT
2015-07-22 05:53:29 +02:00
2016-04-12 16:21:31 +02:00
${IPTABLES_DIR}/iptables -t nat -A POSTROUTING -s ${ipaddr}/32 -d ${ipaddr}/32 -p ${dst_proto} -m ${dst_proto} --dport ${dst_port} -j MASQUERADE
${IPTABLES_DIR}/iptables -t nat -A DOCKER ! -i ${DOCKER_INT} -p ${dst_proto} -m ${dst_proto} --dport ${src_port} -j DNAT --to-destination ${ipaddr}:${dst_port}
2015-07-22 05:53:29 +02:00
done
fi
done
fi