# Port Conflict Resolution

## Overview

The existing pi-pm3 setup includes ttyd services that provide web-based terminal access:
- **ttyd-bash** on port `8000` - Bash terminal
- **ttyd-pm3** on port `8080` - Proxmark3 terminal
- **RaspAP** on port `80` - WiFi management interface

Dangerous Pi's FastAPI backend defaults to port `8000`, which **conflicts with ttyd-bash**.

## Resolution Options

You have several options to resolve this conflict:

### Option 1: Disable ttyd-bash (Recommended)

**Use this if**: You plan to use Dangerous Pi's web interface instead of the ttyd terminals.

**Steps**:
```bash
sudo systemctl stop ttyd-bash
sudo systemctl disable ttyd-bash
```

**Pros**:
- Simple and clean
- Frees up port 8000 for Dangerous Pi
- Reduces resource usage

**Cons**:
- Loses standalone bash terminal (can use Dangerous Pi's terminal instead)

### Option 2: Change Dangerous Pi Port to 8001

**Use this if**: You want to keep both ttyd-bash and Dangerous Pi running simultaneously.

**Steps**:
```bash
# Edit environment file
sudo nano /opt/dangerous-pi/.env

# Change PORT line to:
PORT=8001

# Restart service
sudo systemctl restart dangerous-pi
```

**Access Dangerous Pi at**: `http://<pi-ip>:8001`

**Pros**:
- Both services available
- No modification to existing ttyd setup

**Cons**:
- Non-standard port for Dangerous Pi
- Slightly more complex URL

### Option 3: Change ttyd-bash Port to 8002

**Use this if**: You want Dangerous Pi on the standard port 8000 but still want ttyd-bash available.

**Steps**:
```bash
# Edit ttyd-bash service
sudo nano /etc/systemd/system/ttyd-bash.service

# Find the line with --port 8000 and change to:
# --port 8002

# Reload and restart
sudo systemctl daemon-reload
sudo systemctl restart ttyd-bash
```

**Access ttyd-bash at**: `http://<pi-ip>:8002`

**Pros**:
- Dangerous Pi on standard port
- ttyd-bash still available

**Cons**:
- Requires modifying existing service
- ttyd-bash on non-standard port

## Automated Resolution Script

For convenience, use the included resolution script:

```bash
/opt/dangerous-pi/scripts/resolve-port-conflict.sh
```

This interactive script will guide you through the options above.

## Port Summary

After resolution, your ports may look like:

### Configuration A (Option 1 - Disable ttyd-bash)
- Port `80` - RaspAP
- Port `8000` - **Dangerous Pi** ✓
- Port `8080` - ttyd-pm3
- ttyd-bash - Disabled

### Configuration B (Option 2 - Dangerous Pi on 8001)
- Port `80` - RaspAP
- Port `8000` - ttyd-bash
- Port `8001` - **Dangerous Pi** ✓
- Port `8080` - ttyd-pm3

### Configuration C (Option 3 - ttyd-bash on 8002)
- Port `80` - RaspAP
- Port `8000` - **Dangerous Pi** ✓
- Port `8002` - ttyd-bash
- Port `8080` - ttyd-pm3

## Pi-gen Build Integration

If you're building a custom image with pi-gen, you can pre-configure the resolution:

Edit `/home/work/dangerous-pi/pi-gen/stageDTPM3/04-dangerous-pi/01-run-chroot.sh` and uncomment the desired option:

```bash
# Option 1: Disable ttyd-bash
if systemctl is-enabled ttyd-bash 2>/dev/null; then
    systemctl disable ttyd-bash
fi

# OR Option 2: Change Dangerous Pi port
sed -i 's/^PORT=.*/PORT=8001/' /opt/dangerous-pi/.env

# OR Option 3: Change ttyd-bash port
sed -i 's/--port 8000/--port 8002/' /etc/systemd/system/ttyd-bash.service
```

Then rebuild the image.

## Verification

After making changes, verify the configuration:

### Check which services are listening on which ports:
```bash
sudo netstat -tlnp | grep :80
```

### Check Dangerous Pi status:
```bash
sudo systemctl status dangerous-pi
```

### Check ttyd-bash status:
```bash
sudo systemctl status ttyd-bash
```

### Test access:
```bash
# Dangerous Pi (adjust port as needed)
curl http://localhost:8000/api/health

# ttyd-bash (if enabled, adjust port as needed)
curl http://localhost:8000
```

## Recommendations

**For most users**: We recommend **Option 1** (disable ttyd-bash) because:
- Dangerous Pi provides a more comprehensive web interface
- It includes PM3 command capabilities
- Simpler configuration
- Fewer services running = better performance on Pi Zero 2 W

**For advanced users**: If you need both interfaces for specific workflows, use **Option 2** or **Option 3**.

## Troubleshooting

### Service won't start - "Address already in use"
Check which service is using the port:
```bash
sudo lsof -i :8000
```

Stop the conflicting service before starting Dangerous Pi.

### Can't access after changing port
1. Verify the service is running:
   ```bash
   sudo systemctl status dangerous-pi
   ```

2. Check the port in the environment file:
   ```bash
   grep PORT /opt/dangerous-pi/.env
   ```

3. Check firewall rules (if enabled):
   ```bash
   sudo iptables -L -n
   ```

### Changes not taking effect
After modifying configurations, always:
```bash
# If you changed service files:
sudo systemctl daemon-reload

# Restart the service:
sudo systemctl restart dangerous-pi
```

## Future Considerations

In future versions, we may:
- Auto-detect port conflicts on startup
- Dynamically select an available port
- Provide a web-based port configuration tool
- Integrate or replace ttyd entirely

For now, manual configuration provides the most flexibility.
