add NAT-PMP for automatic port mapping and external address discovery
This commit is contained in:
66
docker-tests/nat-pmp/router-entrypoint.sh
Executable file
66
docker-tests/nat-pmp/router-entrypoint.sh
Executable file
@@ -0,0 +1,66 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
echo "=== NAT-PMP Router Starting ==="
|
||||
|
||||
# Wait for interfaces to be ready
|
||||
sleep 1
|
||||
|
||||
# Detect interfaces by IP subnet
|
||||
# Internal network: 10.100.0.0/24, External network: 10.200.0.0/24
|
||||
ETH0_IP=$(ip -4 addr show eth0 | grep 'inet ' | awk '{print $2}' | cut -d'/' -f1)
|
||||
ETH1_IP=$(ip -4 addr show eth1 | grep 'inet ' | awk '{print $2}' | cut -d'/' -f1)
|
||||
|
||||
echo "eth0: $ETH0_IP"
|
||||
echo "eth1: $ETH1_IP"
|
||||
|
||||
# Determine which is internal (10.100.x.x) vs external (10.200.x.x)
|
||||
if echo "$ETH0_IP" | grep -q "^10\.100\."; then
|
||||
INTERNAL_IF="eth0"
|
||||
INTERNAL_IP="$ETH0_IP"
|
||||
EXTERNAL_IF="eth1"
|
||||
EXTERNAL_IP="$ETH1_IP"
|
||||
else
|
||||
INTERNAL_IF="eth1"
|
||||
INTERNAL_IP="$ETH1_IP"
|
||||
EXTERNAL_IF="eth0"
|
||||
EXTERNAL_IP="$ETH0_IP"
|
||||
fi
|
||||
|
||||
echo "Internal interface: $INTERNAL_IF ($INTERNAL_IP)"
|
||||
echo "External interface: $EXTERNAL_IF ($EXTERNAL_IP)"
|
||||
|
||||
# Set up NAT (masquerade internal traffic going out external interface)
|
||||
echo "Setting up NAT..."
|
||||
iptables -t nat -A POSTROUTING -o $EXTERNAL_IF -j MASQUERADE
|
||||
iptables -A FORWARD -i $INTERNAL_IF -o $EXTERNAL_IF -j ACCEPT
|
||||
iptables -A FORWARD -i $EXTERNAL_IF -o $INTERNAL_IF -m state --state RELATED,ESTABLISHED -j ACCEPT
|
||||
|
||||
# Create miniupnpd config with correct interfaces
|
||||
# Note: miniupnpd config format is very specific
|
||||
# ext_ip uses a public-looking IP for testing (doesn't need to route)
|
||||
FAKE_PUBLIC_IP="93.184.216.34" # example.com IP - just needs to look public
|
||||
cat > /tmp/miniupnpd.conf << EOF
|
||||
ext_ifname=$EXTERNAL_IF
|
||||
ext_ip=$FAKE_PUBLIC_IP
|
||||
listening_ip=$INTERNAL_IF
|
||||
enable_natpmp=yes
|
||||
enable_upnp=yes
|
||||
secure_mode=no
|
||||
system_uptime=yes
|
||||
lease_file=/var/lib/miniupnpd/leases
|
||||
allow 1024-65535 10.100.0.0/24 1024-65535
|
||||
EOF
|
||||
|
||||
echo "miniupnpd config:"
|
||||
cat /tmp/miniupnpd.conf
|
||||
|
||||
# Create lease directory
|
||||
mkdir -p /var/lib/miniupnpd
|
||||
|
||||
echo ""
|
||||
echo "Starting miniupnpd..."
|
||||
echo "NAT-PMP will listen on $INTERNAL_IP:5351"
|
||||
|
||||
# Start miniupnpd in foreground with debug output
|
||||
exec miniupnpd -d -f /tmp/miniupnpd.conf
|
||||
Reference in New Issue
Block a user