add NAT-PMP for automatic port mapping and external address discovery

This commit is contained in:
2026-02-09 17:05:24 -03:00
parent 7b47ef155c
commit cf5eb0d5fd
11 changed files with 970 additions and 1 deletions

125
docker-tests/nat-pmp/run-test.sh Executable file
View File

@@ -0,0 +1,125 @@
#!/bin/bash
# NAT-PMP Integration Test
#
# This test verifies that NAT-PMP works correctly for:
# 1. External address discovery
# 2. Port mapping
# 3. Full MWP connection through NAT
set -e
cd "$(dirname "$0")"
echo "=========================================="
echo " NAT-PMP Integration Test"
echo "=========================================="
# Cleanup function
cleanup() {
echo ""
echo "Cleaning up..."
docker compose down -v --remove-orphans 2>/dev/null || true
}
trap cleanup EXIT
# Build and start
echo ""
echo "[1/4] Building containers..."
docker compose build
echo ""
echo "[2/4] Starting NAT router..."
docker compose up -d router
sleep 3
# Check router is running
echo ""
echo "[3/4] Verifying NAT-PMP daemon..."
docker compose exec router pgrep miniupnpd > /dev/null || {
echo "ERROR: miniupnpd not running!"
docker compose logs router
exit 1
}
echo "miniupnpd is running"
# Show router state
echo ""
echo "Router NAT table:"
docker compose exec router iptables -t nat -L -n | head -20
echo ""
echo "[4/4] Running peer tests..."
# Start peer-a in background
echo ""
echo "--- Starting peer-a (alice) ---"
docker compose run -d --name test-peer-a peer-a /test.sh --connect-to bob
sleep 2
# Get alice's DHT ID from logs
ALICE_DHT=$(docker logs test-peer-a 2>&1 | grep -o 'DHT Node ID: [0-9a-f]*' | head -1 | cut -d' ' -f4)
if [ -z "$ALICE_DHT" ]; then
echo "ERROR: Could not get Alice's DHT ID"
docker logs test-peer-a
exit 1
fi
echo "Alice DHT ID: $ALICE_DHT"
# Check if NAT-PMP worked for alice
echo ""
echo "Alice's NAT-PMP status:"
docker logs test-peer-a 2>&1 | grep -i "nat-pmp" || echo "(no NAT-PMP messages)"
# Start peer-b connecting to alice
echo ""
echo "--- Starting peer-b (bob) ---"
docker compose run --name test-peer-b peer-b /test.sh \
--connect-to alice \
--bootstrap 10.100.0.10:18100 \
--dht-id "$ALICE_DHT" &
# Wait for connection
echo ""
echo "Waiting for MWP connection..."
sleep 15
# Check results
echo ""
echo "=========================================="
echo " Test Results"
echo "=========================================="
echo ""
echo "--- Alice logs ---"
docker logs test-peer-a 2>&1 | tail -30
echo ""
echo "--- Bob logs ---"
docker logs test-peer-b 2>&1 | tail -30
# Check for success indicators
ALICE_NATPMP=$(docker logs test-peer-a 2>&1 | grep -c "NAT-PMP:" || true)
BOB_NATPMP=$(docker logs test-peer-b 2>&1 | grep -c "NAT-PMP:" || true)
CONNECTION=$(docker logs test-peer-b 2>&1 | grep -c "Connected\|SUCCESS" || true)
echo ""
echo "=========================================="
echo " Summary"
echo "=========================================="
echo "Alice NAT-PMP messages: $ALICE_NATPMP"
echo "Bob NAT-PMP messages: $BOB_NATPMP"
echo "Connection established: $([ $CONNECTION -gt 0 ] && echo 'YES' || echo 'NO')"
# Check for external address discovery
if docker logs test-peer-a 2>&1 | grep -q "Using NAT-PMP:"; then
echo ""
echo "SUCCESS: NAT-PMP address discovery working!"
EXTERNAL=$(docker logs test-peer-a 2>&1 | grep "Using NAT-PMP:" | head -1)
echo "$EXTERNAL"
else
echo ""
echo "NOTE: NAT-PMP address discovery not confirmed"
echo "(This may be expected if miniupnpd configuration needs adjustment)"
fi
echo ""
echo "Test complete."