Skip to content

Commit

Permalink
feat: migrate to nftables (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
qoomon committed Jul 15, 2024
1 parent aa4ca5e commit 51001d8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM alpine:3.20.1

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

COPY ./entrypoint.sh /
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ These examples will send messages from docker container to docker host with `net
### Preparation
Start `netcat` server **TCP** on port `2323` to receive and display messages
```sh
nc -p 2323 -lk
nc -lk 2323
```
Start `netcat` server **UDP** on port `5353` to receive and display messages
```sh
nc -p 5353 -lk -u
nc -lk 5353 -u
```

## Docker Link
Expand Down
18 changes: 10 additions & 8 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,24 @@ echo "Docker Host: $docker_host_ip ($docker_host_source)"
PORTS="${PORTS:-"1-65535"}"
PORTS="$(echo ${PORTS//,/ })"

nft add table nat
nft add chain nat prerouting { type nat hook prerouting priority -100 \; }
nft add chain nat postrouting { type nat hook postrouting priority 100 \; }

echo "Forwarding ports: ${PORTS// /, }"
for forwarding_port in $PORTS
do
docker_container_port="${forwarding_port%%:*}"
docker_host_port="${forwarding_port#*:}"

nft add rule nat prerouting tcp \
dport "${docker_container_port}" dnat to "$docker_host_ip:$docker_host_port"
nft add rule nat prerouting udp \
dport "${docker_container_port}" dnat to "$docker_host_ip:$docker_host_port"

iptables --table nat --insert PREROUTING \
--protocol tcp --destination-port "${docker_container_port/-/:}" \
--jump DNAT --to-destination "$docker_host_ip:$docker_host_port"

iptables --table nat --insert PREROUTING \
--protocol udp --destination-port "${docker_container_port/-/:}" \
--jump DNAT --to-destination "$docker_host_ip:$docker_host_port"
done

iptables --table nat --insert POSTROUTING --jump MASQUERADE
nft add rule nat postrouting masquerade

# --- Drop root access and "Ah, ha, ha, ha, stayin' alive" ---------------------

Expand Down

0 comments on commit 51001d8

Please sign in to comment.