bidirectional ip reflection for listener address discovery

This commit is contained in:
2026-02-09 16:02:23 -03:00
parent 59e9e420be
commit 7b47ef155c
31 changed files with 3192 additions and 269 deletions

View File

@@ -0,0 +1,287 @@
# NAT Traversal Test Environment for MWP
#
# Network Topology:
#
# ┌─────────────────── public (10.200.0.0/24) ──────────────────┐
# │ │
# │ bootstrap (10.200.0.10) │
# │ │ │
# │ ┌────┴────┐ ┌─────────┐ │
# │ │ nat1 │ 10.200.0.2 │ nat2 │ 10.200.0.3│
# │ │ gateway │ │ gateway │ │
# │ └────┬────┘ └────┬────┘ │
# └───────┼────────────────────────────────────┼────────────────┘
# │ │
# ┌───────┴───────┐ ┌────────┴──────┐
# │ private_alice │ │ private_bob │
# │ (10.201.0.0) │ │ (10.202.0.0) │
# │ │ │ │
# │ alice │ │ bob │
# │ 10.201.0.10 │ │ 10.202.0.10 │
# └───────────────┘ └───────────────┘
#
# Usage:
# docker-compose up --build
# docker-compose logs -f alice bob
networks:
public:
driver: bridge
ipam:
config:
- subnet: 10.200.0.0/24
private_alice:
driver: bridge
internal: false
ipam:
config:
- subnet: 10.201.0.0/24
private_bob:
driver: bridge
internal: false
ipam:
config:
- subnet: 10.202.0.0/24
services:
# ==========================================================================
# NAT Gateway 1 (for Alice)
# ==========================================================================
nat1:
image: alpine:latest
container_name: nat1
cap_add:
- NET_ADMIN
sysctls:
- net.ipv4.ip_forward=1
networks:
public:
ipv4_address: 10.200.0.2
private_alice:
ipv4_address: 10.201.0.2
command:
- sh
- -c
- |
echo 'Installing iptables...'
apk add --no-cache iptables
echo 'Setting up NAT gateway 1...'
# Enable IP forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward
# Flush existing rules
iptables -t nat -F
iptables -F FORWARD
# NAT: Masquerade traffic from private network going to public
iptables -t nat -A POSTROUTING -s 10.201.0.0/24 -o eth0 -j MASQUERADE
# Allow forwarding
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
echo 'NAT1 gateway ready'
iptables -t nat -L -n -v
# Keep container running
sleep infinity
healthcheck:
test: ["CMD", "sh", "-c", "iptables -t nat -L >/dev/null 2>&1"]
interval: 5s
timeout: 10s
retries: 5
start_period: 30s
# ==========================================================================
# NAT Gateway 2 (for Bob)
# ==========================================================================
nat2:
image: alpine:latest
container_name: nat2
cap_add:
- NET_ADMIN
sysctls:
- net.ipv4.ip_forward=1
networks:
public:
ipv4_address: 10.200.0.3
private_bob:
ipv4_address: 10.202.0.2
command:
- sh
- -c
- |
echo 'Installing iptables...'
apk add --no-cache iptables
echo 'Setting up NAT gateway 2...'
# Enable IP forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward
# Flush existing rules
iptables -t nat -F
iptables -F FORWARD
# NAT: Masquerade traffic from private network going to public
iptables -t nat -A POSTROUTING -s 10.202.0.0/24 -o eth0 -j MASQUERADE
# Allow forwarding
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
echo 'NAT2 gateway ready'
iptables -t nat -L -n -v
# Keep container running
sleep infinity
healthcheck:
test: ["CMD", "sh", "-c", "iptables -t nat -L >/dev/null 2>&1"]
interval: 5s
timeout: 10s
retries: 5
start_period: 30s
# ==========================================================================
# Bootstrap Node (on public network - acts as rendezvous point)
# This node just runs MWP and serves DHT requests - it doesn't connect to anyone
# ==========================================================================
bootstrap:
build:
context: ../..
dockerfile: docker-tests/nat/Dockerfile
container_name: bootstrap
networks:
public:
ipv4_address: 10.200.0.10
command:
- sh
- -c
- |
echo '=========================================='
echo 'Starting bootstrap node on public network'
echo 'IP: 10.200.0.10, Port: 18100'
echo '=========================================='
mwp-demo --name bootstrap --port 18100 --deterministic
healthcheck:
test: ["CMD", "sh", "-c", "netstat -tuln | grep 18100"]
interval: 5s
timeout: 10s
retries: 5
start_period: 30s
# ==========================================================================
# Alice (behind NAT1)
# ==========================================================================
alice:
build:
context: ../..
dockerfile: docker-tests/nat/Dockerfile
container_name: alice
cap_add:
- NET_ADMIN
depends_on:
nat1:
condition: service_healthy
bootstrap:
condition: service_healthy
networks:
private_alice:
ipv4_address: 10.201.0.20
environment:
- BOOTSTRAP_DHT_ID=${BOOTSTRAP_DHT_ID:-}
command:
- sh
- -c
- |
echo '=========================================='
echo 'Configuring Alice network'
echo '=========================================='
# Set default route through NAT gateway
ip route del default 2>/dev/null || true
ip route add default via 10.201.0.2
echo 'Alice routing table:'
ip route
echo 'Testing connectivity to bootstrap...'
ping -c 2 10.200.0.10 || echo 'Ping failed (may be blocked)'
echo 'Waiting for bootstrap node...'
sleep 5
if [ -z "$BOOTSTRAP_DHT_ID" ]; then
echo 'ERROR: BOOTSTRAP_DHT_ID not set!'
exit 1
fi
echo '=========================================='
echo 'Starting Alice'
echo 'Bootstrap: 10.200.0.10:18100'
echo "DHT ID: $BOOTSTRAP_DHT_ID"
echo '=========================================='
mwp-demo --name alice --port 18100 --deterministic --connect-to bob \
--bootstrap 10.200.0.10:18100 \
--dht-id "$BOOTSTRAP_DHT_ID"
# ==========================================================================
# Bob (behind NAT2)
# ==========================================================================
bob:
build:
context: ../..
dockerfile: docker-tests/nat/Dockerfile
container_name: bob
cap_add:
- NET_ADMIN
depends_on:
nat2:
condition: service_healthy
bootstrap:
condition: service_healthy
networks:
private_bob:
ipv4_address: 10.202.0.20
environment:
- BOOTSTRAP_DHT_ID=${BOOTSTRAP_DHT_ID:-}
command:
- sh
- -c
- |
echo '=========================================='
echo 'Configuring Bob network'
echo '=========================================='
# Set default route through NAT gateway
ip route del default 2>/dev/null || true
ip route add default via 10.202.0.2
echo 'Bob routing table:'
ip route
echo 'Testing connectivity to bootstrap...'
ping -c 2 10.200.0.10 || echo 'Ping failed (may be blocked)'
echo 'Waiting for network setup...'
sleep 8
if [ -z "$BOOTSTRAP_DHT_ID" ]; then
echo 'ERROR: BOOTSTRAP_DHT_ID not set!'
exit 1
fi
echo '=========================================='
echo 'Starting Bob'
echo 'Bootstrap: 10.200.0.10:18100'
echo "DHT ID: $BOOTSTRAP_DHT_ID"
echo '=========================================='
mwp-demo --name bob --port 18100 --deterministic --connect-to alice \
--bootstrap 10.200.0.10:18100 \
--dht-id "$BOOTSTRAP_DHT_ID"