Skip to content

Commit

Permalink
feat: add port mapping
Browse files Browse the repository at this point in the history
BREAKING CHANGE: new syntax for port ranges. old: 8000:9000; new: 8000-9000
  • Loading branch information
qoomon committed Apr 26, 2021
1 parent 44fe5e2 commit add8ca7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
FROM alpine:3.13

RUN apk add --update --no-cache \
iptables \
libcap
RUN apk --no-cache upgrade \
&& apk --no-cache add \
iptables \
libcap

COPY ./entrypoint.sh /

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

Docker Image Tags:
* `latest`
* `2`
* `2.x.x`
* `3`
* `3.x.x`

Docker image to forward **TCP** and **UDP** traffic to the docker host.

Expand All @@ -19,7 +19,7 @@ This container will determine docker host address in the following order
* Try to resolve `host.docker.internal` (`getent ahostsv4 host.docker.internal`)
* Defaults to default gateway (`ip -4 route show default`)

By default all ports (`0:65535`) are forwarded to docker host you can override ports by setting environment variable `PORTS` to a comma separated list of ports and/or port ranges e.g `443,80,8000:9000`
By default all ports (`1-65535`) are forwarded to docker host. You may restrict ports by setting environment variable `PORTS` to a space and/or comma separated list of ports and/or port ranges e.g `443, 8000-9000`. You may also configure port mapping e.g. `443:8443, 8000-9000:5000-6000`

#### ⚠️ On **Linux systems**

Expand Down
23 changes: 13 additions & 10 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,26 @@ then
# --- Configure iptables -----------------------------------------------------

# setup port forwarding
FORWARDING_PORTS="$(echo ${PORTS:-'0:65535'} | sed 's/[ ,;][ ,;]*/ /g')"
echo "Forwarding ports: ${FORWARDING_PORTS// /,}"
FORWARDING_PORTS="$(echo "${PORTS:-'1-65535'}" | sed 's/[ ,][ ,]*/ /g')"
echo "Forwarding ports: $FORWARDING_PORTS"
iptables -t nat -I POSTROUTING -j MASQUERADE
for forwarding_port in ${FORWARDING_PORTS}
for forwarding_port in $FORWARDING_PORTS
do
forwarding_ingress_port="$(echo "$forwarding_port" | cut -d':' -f1)"
forwarding_egress_port="$(echo "$forwarding_port" | cut -d':' -f2)"
forwarding_egress_port="${forwarding_egress_port:-$forwarding_ingress_port}"
forwarding_egress_port="${forwarding_egress_port/:/-}"

iptables --table nat --insert PREROUTING \
--protocol tcp \
--dport "$forwarding_port" \
--jump DNAT --to-destination "$docker_host_ip"
--protocol tcp --destination-port "${forwarding_ingress_port/-/:}" \
--jump DNAT --to-destination "$docker_host_ip:$forwarding_egress_port"
iptables --table nat --insert PREROUTING \
--protocol udp \
--dport "$forwarding_port" \
--jump DNAT --to-destination "$docker_host_ip"
--protocol udp --destination-port "${forwarding_ingress_port/-/:}" \
--jump DNAT --to-destination "$docker_host_ip:$forwarding_egress_port"
done

# --- Drop root access -------------------------------------------------------
exec su -s /bin/sh nobody "$0"
exec su -s /bin/sh nobody "$0" -- "$@"
fi


Expand Down

0 comments on commit add8ca7

Please sign in to comment.