# Tor Installation & Setup Guide

## Option 1: Automated Setup (Recommended)

Run the setup script as root:

```bash
cd /var/www/html/onion
sudo chmod +x setup-tor.sh
sudo ./setup-tor.sh
```

This will:
- Install Tor
- Configure the hidden service
- Generate your .onion address
- Start the Tor service

## Option 2: Manual Installation

### For Debian/Raspbian/Ubuntu:

```bash
# Update package list
sudo apt update

# Install Tor
sudo apt install -y tor

# Create Tor config directory if it doesn't exist
sudo mkdir -p /etc/tor

# Copy configuration
sudo cp /var/www/html/onion/config/torrc.example /etc/tor/torrc

# Create hidden service directory
sudo mkdir -p /var/lib/tor/bloodweb-onion-chat
sudo chown -R debian-tor:debian-tor /var/lib/tor/bloodweb-onion-chat
sudo chmod 700 /var/lib/tor/bloodweb-onion-chat

# Enable and start Tor
sudo systemctl enable tor
sudo systemctl start tor

# Wait a moment for the hidden service to generate
sleep 15

# Get your .onion address
sudo cat /var/lib/tor/bloodweb-onion-chat/hostname
```

### For CentOS/RHEL/Fedora:

```bash
# Install Tor
sudo dnf install -y tor  # or: sudo yum install -y tor

# Follow the same steps as Debian above, but use:
sudo chown -R tor:tor /var/lib/tor/bloodweb-onion-chat
```

## Verify Installation

```bash
# Check Tor service status
sudo systemctl status tor

# View Tor logs
sudo journalctl -u tor -f

# Get your .onion address
sudo cat /var/lib/tor/bloodweb-onion-chat/hostname
```

## Update Warning Page

Once you have your .onion address, edit `public/tor-required.html`:

```javascript
// Find this line and replace with your actual .onion address:
<div class="onion-address" id="onion-address">
    http://[your-actual-address].onion
</div>
```

Or use sed to update it automatically:

```bash
ONION_ADDRESS=$(sudo cat /var/lib/tor/bloodweb-onion-chat/hostname)
sed -i "s|\[Your .onion address will appear here once configured\]|http://$ONION_ADDRESS|g" \
    /var/www/html/onion/public/tor-required.html
```

## Start Everything

```bash
# Start the chat server (if not already running)
cd /var/www/html/onion
npm start

# Access via Tor Browser
# Navigate to: http://[your-address].onion
```

## Troubleshooting

### Tor won't start

```bash
# Check config syntax
sudo tor --verify-config -f /etc/tor/torrc

# Check permissions
ls -la /var/lib/tor/bloodweb-onion-chat/

# View detailed logs
sudo journalctl -u tor -xe
```

### .onion address not generating

Wait 30-60 seconds after starting Tor, then check:

```bash
# List files in hidden service directory
sudo ls -la /var/lib/tor/bloodweb-onion-chat/

# Should contain:
# - hostname (your .onion address)
# - hs_ed25519_secret_key
# - hs_ed25519_public_key
```

### Permission denied errors

```bash
# Fix ownership
sudo chown -R debian-tor:debian-tor /var/lib/tor/bloodweb-onion-chat/
sudo chmod 700 /var/lib/tor/bloodweb-onion-chat/
sudo chmod 600 /var/lib/tor/bloodweb-onion-chat/*
```

## Testing Without Tor (Development)

For local testing without Tor:

```bash
# Start the server
npm start

# Access directly via localhost
# Will show warning page, click "I understand the risks" to bypass
http://localhost:3000
```

## Production Deployment

Once Tor is working, set up the systemd service for auto-start:

```bash
sudo cp /var/www/html/onion/config/bloodweb-onion-chat.service \
    /etc/systemd/system/

sudo systemctl daemon-reload
sudo systemctl enable bloodweb-onion-chat
sudo systemctl start bloodweb-onion-chat
```

You'll need to create the systemd service file (see README.md for the template).
