Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

18.2 KB
Raw Blame
# --- Dashboard sign-in boundary ---------------------------------------------
# This proxy adds the server-held DASHBOARD_API_KEY to admin API requests, so it
# must know who is asking. A request is trusted without a session only when it
# arrived on the loopback-published listener (__ODS_LOCAL_LISTENER__; the
# entrypoint writes "off" when BIND_ADDRESS exposes port 3001 to the network),
# names a loopback Host and carries no proxy forwarding headers. Everything
# else - the LAN, ODS proxy (listener 3011), a reverse proxy or Tailscale Serve
# in front of localhost, a DNS-rebinding page - needs a dashboard session from
# /api/auth/dashboard-session/login first. The auth subrequest shares these
# request headers and the connection's port.
map $http_host $ods_loopback_host {
    default 0;
    "~^(localhost|127\.0\.0\.1|\[::1\])(:[0-9]+)?$" 1;
}

map "$http_x_forwarded_for$http_x_forwarded_host$http_x_forwarded_proto$http_forwarded$http_x_real_ip$http_via" $ods_forwarded {
    default 1;
    "" 0;
}

map "$server_port:$ods_loopback_host:$ods_forwarded" $ods_trusted_local {
    default 0;
    "__ODS_LOCAL_LISTENER__:1:0" 1;
}

# Preserve an HTTPS terminator's indication for Secure login cookies. An
# untrusted claim can only strengthen that cookie flag, never bypass the gate.
map $http_x_forwarded_proto $ods_cookie_scheme {
    default $scheme;
    https https;
}

server {
    listen 3001;
    listen [::]:3001;  # IPv6 support for Docker healthcheck
    # ODS proxy (dashboard./talk.<device>.local) forwards LAN traffic here.
    # It is never trusted as local, so the admin API always needs sign-in.
    listen 3011;
    listen [::]:3011;
    server_name localhost;
    root /usr/share/nginx/html;
    index index.html;

    # Docker service IPs can change when lifecycle commands recreate
    # dashboard-api. Use Docker DNS at request time instead of pinning the IP
    # nginx resolved when this container started.
    resolver 127.0.0.11 valid=10s ipv6=off;
    resolver_timeout 5s;
    set $dashboard_api_upstream dashboard-api:3002;
    set $pixel_edge_upstream pixel-edge:9595;

    # Gzip compression
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml;

    # Admin gate for every location that adds the API key. Local browsers
    # pass without a round trip; everyone else needs a valid session cookie.
    location = /_ods_dashboard_gate {
        internal;
        # Auth subrequests inherit the original Content-Length, but send no
        # body. Keep size enforcement on the actual upload/API location.
        client_max_body_size 0;
        if ($ods_trusted_local = 1) {
            return 204;
        }
        proxy_pass http://$dashboard_api_upstream/api/auth/dashboard-session/verify;
        proxy_pass_request_body off;
        proxy_http_version 1.1;
        proxy_set_header Content-Length "";
        proxy_set_header Host $http_host;
        proxy_set_header Cookie $http_cookie;
        proxy_set_header Authorization "";
        proxy_set_header Connection "close";
    }

    # nginx-generated 401s come only from the gate above (upstream 401s pass
    # through untouched). The header tells the UI to show its sign-in screen.
    error_page 401 = @ods_sign_in_required;
    location @ods_sign_in_required {
        default_type application/json;
        add_header X-ODS-Sign-In required always;
        add_header Cache-Control no-store always;
        return 401 '{"detail":"Sign in to the ODS dashboard to continue.","code":"dashboard_sign_in_required"}';
    }

    # Sign-in and sign-out work before a session exists and never receive
    # the server-held API key.
    location ~ ^/api/auth/dashboard-session/(login|logout)$ {
        client_max_body_size 4k;
        proxy_pass http://$dashboard_api_upstream;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $ods_cookie_scheme;
        proxy_set_header Connection "close";
        proxy_set_header Authorization "";
    }

    # SPA routing - serve index.html for all routes
    location / {
        expires -1;
        try_files $uri $uri/ /index.html;
    }

    # Revalidate the entry document so it always references the current chunks.
    location = /index.html {
        expires -1;
    }

    # ODS Talk endpoints — chat + voice. These are inherently long-running:
    # on a cold first message, Hermes prompt processing of the 16k-token
    # system prompt can take 60+ seconds before any token streams back. The
    # SSE stream endpoint (/api/talk/message/stream) needs unbuffered passthrough
    # and a generous read timeout so the upstream can keep the connection open
    # while tokens trickle in.
    location ^~ /api/talk/ {
        # Talk takes voice messages up to 25 MB and images up to 10 MB
        # (routers/talk.py). nginx's 1 MB default refused ordinary photos and
        # longer voice messages before the API could check them.
        client_max_body_size 26m;
        proxy_pass http://$dashboard_api_upstream;
        proxy_http_version 1.1;
        # Preserve the browser's port for the API's same-origin CSRF check.
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Connection "";
        # ODS Talk authorizes with its own ods-session cookie; it never gets
        # the admin key, so it stays reachable without dashboard sign-in.
        proxy_set_header Authorization "";
        proxy_read_timeout 600s;
        proxy_send_timeout 600s;
        # SSE / streaming responses: don't buffer; flush frames immediately.
        proxy_buffering off;
        proxy_cache off;
        proxy_set_header X-Accel-Buffering no;
    }

    # Template apply may pull and start several services serially. Keep this
    # timeout aligned with TemplatePicker's request budget.
    location ~ ^/api/templates/[a-z0-9-]+/apply$ {
        proxy_pass http://$dashboard_api_upstream;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Connection "close";
        auth_request /_ods_dashboard_gate;
        proxy_set_header Authorization "Bearer ${DASHBOARD_API_KEY}";
        proxy_read_timeout 1800s;
        proxy_send_timeout 1800s;
    }

    # Model activation can legitimately take several minutes on host-native
    # (Windows and macOS) runtimes while the selected GGUF is loaded and consumer
    # routes are synchronized. Keep this scoped timeout aligned with the
    # dashboard API / host-agent activation budget.
    location ~ ^/api/models/.+/load$ {
        proxy_pass http://$dashboard_api_upstream;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Connection "close";
        auth_request /_ods_dashboard_gate;
        proxy_set_header Authorization "Bearer ${DASHBOARD_API_KEY}";
        proxy_read_timeout 2700s;
        proxy_send_timeout 2700s;
    }

    # Provider changes can include live proof and rollback after an outage.
    # Keep the request open beyond the API's 1800s and the UI's 1805s budgets
    # so a completed operation returns its result instead of a generic 504.
    location = /api/remote-provider/apply {
        proxy_pass http://$dashboard_api_upstream;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Connection "close";
        auth_request /_ods_dashboard_gate;
        proxy_set_header Authorization "Bearer ${DASHBOARD_API_KEY}";
        proxy_read_timeout 1860s;
        proxy_send_timeout 1860s;
    }

    # Pixel SSE stream — unbuffered, long-lived connection to dashboard-api.
    # Browser receives no credential; nginx injects DASHBOARD_API_KEY server-side.
    location = /api/pixel/chat/stream {
        client_max_body_size 8m;
        proxy_pass http://$dashboard_api_upstream;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Connection "";
        auth_request /_ods_dashboard_gate;
        proxy_set_header Authorization "Bearer ${DASHBOARD_API_KEY}";
        # The dashboard API owns a 34-minute bounded stream. Nginx remains the
        # outermost layer with one minute of headroom for CPU-only first turns.
        proxy_read_timeout 2100s;
        proxy_send_timeout 2100s;
        proxy_buffering off;
        proxy_cache off;
        proxy_set_header X-Accel-Buffering no;
    }

    # Original image bytes only; chat JSON still carries references, not base64.
    # Do not enlarge the default API limit or grant this route a public bypass.
    location ~ "^/api/pixel/images/[A-Za-z0-9_-]{1,128}(?:/img-[a-f0-9]{32})?$" {
        client_max_body_size 8m;
        client_body_timeout 30s;
        proxy_pass http://$dashboard_api_upstream;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Connection "close";
        auth_request /_ods_dashboard_gate;
        proxy_set_header Authorization "Bearer ${DASHBOARD_API_KEY}";
        proxy_request_buffering off;
        proxy_read_timeout 60s;
        proxy_send_timeout 60s;
        proxy_cache off;
    }

    # Access transitions include an idle check, gateway restart and real tool
    # probe. Keep this owner-only control outside the ordinary request timeout.
    location = /api/pixel/access-mode {
        client_max_body_size 2k;
        proxy_pass http://$dashboard_api_upstream;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Connection "close";
        auth_request /_ods_dashboard_gate;
        proxy_set_header Authorization "Bearer ${DASHBOARD_API_KEY}";
        proxy_read_timeout 350s;
        proxy_cache off;
    }

    # Recovery may spend up to 400 seconds proving and finishing an existing
    # model transaction. Keep this beyond the browser's 405-second deadline.
    location = /api/models/recovery {
        client_max_body_size 2k;
        proxy_pass http://$dashboard_api_upstream;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Connection "close";
        auth_request /_ods_dashboard_gate;
        proxy_set_header Authorization "Bearer ${DASHBOARD_API_KEY}";
        proxy_read_timeout 420s;
        proxy_send_timeout 420s;
        proxy_cache off;
    }

    # Preserve the browser-facing HTTPS scheme for the terminal's origin check,
    # as for Dashboard login behind an HTTPS terminator. The signed browser
    # session and exact same-origin checks remain mandatory at the API.
    location = /api/pixel/approval-terminal {
        client_max_body_size 4k;
        proxy_pass http://$dashboard_api_upstream;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $ods_cookie_scheme;
        proxy_set_header Connection "close";
        auth_request /_ods_dashboard_gate;
        proxy_set_header Authorization "Bearer ${DASHBOARD_API_KEY}";
        proxy_read_timeout 15s;
        proxy_cache off;
    }

    # Proxy API requests to the dashboard API. The key is added only after
    # the sign-in gate above allows the request.
    location /api/ {
        proxy_pass http://$dashboard_api_upstream;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Connection "close";
        # B1 fix: Auth header injected by entrypoint.sh after reading key from file/env
        auth_request /_ods_dashboard_gate;
        proxy_set_header Authorization "Bearer ${DASHBOARD_API_KEY}";
        proxy_read_timeout 180s;
    }

    # Pixel previews remain on the same authenticated Dashboard route for
    # remote LAN/Tailscale owners. Pixel Edge relays only immutable snapshots
    # over the private host Unix socket; its response applies an opaque CSP
    # sandbox so same-path previews cannot share browser authority.
    location ^~ /pixel-preview/ {
        # The edge supplies the isolated preview CSP. Inheriting the portal's
        # CSP adds a second policy that blocks model-authored inline scripts.
        # Local add_header directives stop that inheritance without hiding
        # the upstream sandbox, network, form-action, or frame-ancestor policy.
        add_header X-Content-Type-Options nosniff always;
        add_header X-Frame-Options SAMEORIGIN always;
        rewrite ^/pixel-preview/(.*)$ /preview/$1 break;
        proxy_pass http://$pixel_edge_upstream;
        proxy_http_version 1.1;
        proxy_set_header Host pixel-edge;
        auth_request /_ods_dashboard_gate;
        proxy_set_header Authorization "Bearer ${DASHBOARD_API_KEY}";
        proxy_set_header Connection "close";
        proxy_read_timeout 15s;
        proxy_buffering off;
        proxy_cache off;
    }

    # Cold Library enable can pull an image and cache Whisper's selected model.
    # The host agent and Dashboard API allow up to 630s and 660s respectively;
    # keep the request open until they can report an actual ready/failure state.
    location ~ ^/api/extensions/[a-z0-9_-]+/enable$ {
        proxy_pass http://$dashboard_api_upstream;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Connection "close";
        auth_request /_ods_dashboard_gate;
        proxy_set_header Authorization "Bearer ${DASHBOARD_API_KEY}";
        proxy_read_timeout 720s;
        proxy_send_timeout 720s;
    }

    # Adding Open WebUI waits for its migration and health check; the Dashboard
    # API allows the host agent 900s. Under the generic /api/ timeout the page
    # gave up at 180s while the add was still running and then misread its state.
    location = /api/webui/selection {
        proxy_pass http://$dashboard_api_upstream;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Connection "close";
        auth_request /_ods_dashboard_gate;
        proxy_set_header Authorization "Bearer ${DASHBOARD_API_KEY}";
        proxy_read_timeout 960s;
        proxy_send_timeout 960s;
    }

    # Extension refresh can synchronously recreate an enabled service and
    # prove immediate compose success. Keep this longer timeout scoped to the
    # two transactional definition endpoints.
    location ~ ^/api/extensions/[a-z0-9_-]+/(update|rollback)$ {
        proxy_pass http://$dashboard_api_upstream;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Connection "close";
        auth_request /_ods_dashboard_gate;
        proxy_set_header Authorization "Bearer ${DASHBOARD_API_KEY}";
        proxy_read_timeout 1800s;
        proxy_send_timeout 1800s;
    }

    # This public script has a stable filename, unlike Vite's hashed chunks.
    # Revalidate it so mascot behavior updates together with the React UI.
    location = /pixel-mascot.js {
        expires -1;
    }

    # The service worker also keeps a stable filename across releases.
    location = /sw.js {
        expires -1;
    }

    # Stable-name boot scripts moved out of index.html for the CSP.
    location = /theme-init.js {
        expires -1;
    }

    location = /sw-register.js {
        expires -1;
    }

    # Cache static assets
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    # Security headers
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Content-Security-Policy "default-src 'self'; script-src 'self' https://unpkg.com https://cdn.jsdelivr.net; style-src 'self' https://cdn.jsdelivr.net 'unsafe-inline'; img-src 'self' data: blob: https://huggingface.co https://cdn-avatars.huggingface.co https://t3.gstatic.com/faviconV2; connect-src 'self' http://*.localhost:__PIXEL_PREVIEW_PORT__; media-src 'self' blob:; font-src 'self' https://cdn.jsdelivr.net; frame-src 'self' http://localhost:__PIXEL_PREVIEW_PORT__ http://127.0.0.1:__PIXEL_PREVIEW_PORT__ http://*.localhost:__PIXEL_PREVIEW_PORT__;" always;
    # Pre-stage HSTS so it activates automatically when TLS lands via
    # Caddy/Tailscale. Harmless over plain HTTP (browsers ignore it).
    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
}