diff --git a/ipnat/build/Dockerfile b/ipnat/build/Dockerfile new file mode 100644 index 0000000..b7cf5fa --- /dev/null +++ b/ipnat/build/Dockerfile @@ -0,0 +1,33 @@ +FROM debian:bullseye + +USER root + +# Install all packages +ENV DEBIAN_FRONTEND noninteractive +RUN apt-get update --yes && \ + apt-get upgrade --yes && \ + apt-get install --yes --no-install-recommends \ + bash \ + #tini \ + locales \ + iptables \ + && \ + # Clean up and generate locales + apt-get clean && rm -rf /var/lib/apt/lists/* && \ + echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && \ + locale-gen + + +# Requires cap-add NET_ADMIN NET_RAW + +EXPOSE 33 +EXPOSE 993 +EXPOSE 587 + +COPY start.sh /start.sh +COPY iptables.sh /iptables.sh + +#ENTRYPOINT ["tini", "-g", "--"] +ENTRYPOINT ["bash"] +CMD ["start.sh"] + diff --git a/ipnat/build/iptables.sh b/ipnat/build/iptables.sh new file mode 100644 index 0000000..365c155 --- /dev/null +++ b/ipnat/build/iptables.sh @@ -0,0 +1,28 @@ +nat () { + # All traffic to $THIS_PORT on this container + # will be redirected to $THAT_PORT on $THAT_IP + $THAT_IP=$1 + THIS_PORT=$2 + THAT_PORT=$3 + + # Accept forward incoming traffic + iptables -I FORWARD -d $FWD_IP -m tcp -p tcp --dport $THAT_PORT -j ACCEPT + + # Accept forward return traffic + iptables -I FORWARD -s $FWD_IP -m tcp -p tcp --sport $THAT_PORT -j ACCEPT + + # Redirect packets to remote + iptables -t nat -I PREROUTING -m tcp -p tcp --dport $THIS_PORT -j DNAT --to-destination $THAT_IP:$THAT_PORT +} + +NAT_IP=10.143.0.20 +nat $NAT_IP 33 10013 +nat $NAT_IP 993 10015 +nat $NAT_IP 587 10016 + + + + + +# Include this line ONCE, at the end. +iptables -t nat -I POSTROUTING -d $FWD_IP -j MASQUERADE \ No newline at end of file diff --git a/ipnat/build/start.sh b/ipnat/build/start.sh new file mode 100644 index 0000000..ce1b159 --- /dev/null +++ b/ipnat/build/start.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +touch /var/log/iptables.log + +bash /iptables.sh + +iptables -A INPUT -j LOG --log-prefix "[I]iptables: " +iptables -A OUTPUT -j LOG --log-prefix "[O]iptables: " +iptables -A FORWARD -j LOG --log-prefix "[F]iptables: " + +tail -f /var/log/iptables.log \ No newline at end of file diff --git a/ipnat/docker-compose.yml b/ipnat/docker-compose.yml new file mode 100644 index 0000000..979a5c5 --- /dev/null +++ b/ipnat/docker-compose.yml @@ -0,0 +1,14 @@ +version: "2" + +services: + ipnat: + build: ./build + container_name: ipnat + restart: unless-stopped + + ports: + - "10010:10010" + + cap_add: + - NET_ADMIN + - NET_RAW \ No newline at end of file