Skip to content

Commit 51001d8

Browse files
authored
feat: migrate to nftables (#63)
1 parent aa4ca5e commit 51001d8

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ FROM alpine:3.20.1
33

44
RUN apk --no-cache upgrade \
55
&& apk --no-cache add \
6-
iptables \
6+
nftables \
77
libcap
88

99
COPY ./entrypoint.sh /

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ These examples will send messages from docker container to docker host with `net
6868
### Preparation
6969
Start `netcat` server **TCP** on port `2323` to receive and display messages
7070
```sh
71-
nc -p 2323 -lk
71+
nc -lk 2323
7272
```
7373
Start `netcat` server **UDP** on port `5353` to receive and display messages
7474
```sh
75-
nc -p 5353 -lk -u
75+
nc -lk 5353 -u
7676
```
7777

7878
## Docker Link

entrypoint.sh

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,24 @@ echo "Docker Host: $docker_host_ip ($docker_host_source)"
6969
PORTS="${PORTS:-"1-65535"}"
7070
PORTS="$(echo ${PORTS//,/ })"
7171

72+
nft add table nat
73+
nft add chain nat prerouting { type nat hook prerouting priority -100 \; }
74+
nft add chain nat postrouting { type nat hook postrouting priority 100 \; }
75+
7276
echo "Forwarding ports: ${PORTS// /, }"
7377
for forwarding_port in $PORTS
7478
do
7579
docker_container_port="${forwarding_port%%:*}"
7680
docker_host_port="${forwarding_port#*:}"
81+
82+
nft add rule nat prerouting tcp \
83+
dport "${docker_container_port}" dnat to "$docker_host_ip:$docker_host_port"
84+
nft add rule nat prerouting udp \
85+
dport "${docker_container_port}" dnat to "$docker_host_ip:$docker_host_port"
7786

78-
iptables --table nat --insert PREROUTING \
79-
--protocol tcp --destination-port "${docker_container_port/-/:}" \
80-
--jump DNAT --to-destination "$docker_host_ip:$docker_host_port"
81-
82-
iptables --table nat --insert PREROUTING \
83-
--protocol udp --destination-port "${docker_container_port/-/:}" \
84-
--jump DNAT --to-destination "$docker_host_ip:$docker_host_port"
8587
done
8688

87-
iptables --table nat --insert POSTROUTING --jump MASQUERADE
89+
nft add rule nat postrouting masquerade
8890

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

0 commit comments

Comments
 (0)