diff --git a/apps/host/server/cloudflare.mjs b/apps/host/server/cloudflare.mjs index ea5a1027..3f32e9c9 100644 --- a/apps/host/server/cloudflare.mjs +++ b/apps/host/server/cloudflare.mjs @@ -3,6 +3,8 @@ import { spawn } from 'node:child_process'; import { probe, which } from './util.mjs'; import { classifyRunning, describeConnector, otherTunnelMessage, ownTunnelMessage } from './cloudflared-find.mjs'; +/** A tunnel without a key is allowed (owner decision, 0.2.2); because the link is public the warning is stronger than the local one. */ +export const NO_KEY_TUNNEL_WARNING = 'No API key is set, so anyone who finds this link can use your model from the internet. Create an API key to protect it.'; export function tunnelHostname(value) { const hostname = String(value ?? '').trim().toLowerCase(); if (hostname.length > 253 || !/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$/.test(hostname)) throw new Error('Enter a hostname such as ai.example.com, without https:// or a path.'); @@ -13,7 +15,7 @@ export function createCloudflare({ origin, originUrl, hasKey, changed = () => {} let value = { status: 'idle', mode: null, url: null, error: null }; const state = () => { const info = binary ? connectorInfo() : null; - return { ...value, installed: !!binary, originUrl, connector: info ? { path: info.path, version: info.version, source: info.source, label: info.label, summary: describeConnector(info) } : null, external: other?.running ? externalState() : null }; + return { ...value, warning: value.status === 'connected' && !hasKey() ? NO_KEY_TUNNEL_WARNING : null, installed: !!binary, originUrl, connector: info ? { path: info.path, version: info.version, source: info.source, label: info.label, summary: describeConnector(info) } : null, external: other?.running ? externalState() : null }; }; const update = patch => { value = { ...value, ...patch }; changed(state()); }; function externalState(token = '') { @@ -52,7 +54,6 @@ export function createCloudflare({ origin, originUrl, hasKey, changed = () => {} } async function start(body) { if (busy || ['starting', 'connected', 'stopping'].includes(value.status)) throw new Error('Stop the current tunnel before starting another.'); - if (!hasKey()) throw new Error('Create a Strata API key before connecting Cloudflare.'); if (!['quick', 'domain'].includes(body.mode)) throw new Error('Choose a temporary URL or your own domain.'); const hostname = body.mode === 'domain' ? tunnelHostname(body.hostname) : null; const token = body.mode === 'domain' ? String(body.token ?? '').trim() : ''; diff --git a/apps/host/server/local-share.mjs b/apps/host/server/local-share.mjs index 788932f9..686f49a6 100644 --- a/apps/host/server/local-share.mjs +++ b/apps/host/server/local-share.mjs @@ -2,14 +2,15 @@ import http from 'node:http'; import os from 'node:os'; import { apiPath } from './api-paths.mjs'; +/** Sharing without a key is allowed (owner decision, 0.2.2): the person is told once, in plain words, and the app keeps offering to make a key. */ +export const NO_KEY_WARNING = 'No API key is set, so any device on this network can use your model. Create an API key to protect it.'; export function createLocalShare({ handle, hasKey, changed = () => {}, host = '0.0.0.0', port = 8792 }) { let listener = null, active = false; const address = () => Object.values(os.networkInterfaces()).flat().find(n => n?.family === 'IPv4' && !n.internal)?.address ?? '127.0.0.1'; - const state = () => ({ active, url: active ? `http://${host === '127.0.0.1' ? host : address()}:${listener.address().port}/v1` : null }); + const state = () => ({ active, url: active ? `http://${host === '127.0.0.1' ? host : address()}:${listener.address().port}/v1` : null, warning: active && !hasKey() ? NO_KEY_WARNING : null }); async function start() { if (active) return state(); if (listener) throw new Error('Sharing is already starting.'); - if (!hasKey()) throw new Error('Set an API key before sharing with other devices.'); listener = http.createServer(async (req, res) => { const path = apiPath(new URL(req.url ?? '/', 'http://localhost').pathname); if (!['/v1/models','/v1/chat/completions','/v1/messages'].includes(path)) { res.writeHead(404); res.end('Not found'); return; } diff --git a/apps/host/src/components/CloudflareDialog.tsx b/apps/host/src/components/CloudflareDialog.tsx index 9fe60a53..e3334f4d 100644 --- a/apps/host/src/components/CloudflareDialog.tsx +++ b/apps/host/src/components/CloudflareDialog.tsx @@ -19,7 +19,7 @@ export function CloudflareDialog({ open, onClose, onCreateKey }: { const steps = mode === 'quick' ? ['Address', 'Connector', 'API key', 'Connect'] : ['Address', 'Connector', 'API key', 'Domain', 'Connect']; const last = steps.length - 1; const installed = !!tunnel?.installed, keyReady = !!serving?.access.apiKeySet, other = tunnel?.external ?? null; - const canContinue = step === 1 ? installed : step === 2 ? keyReady : step === 3 && mode === 'domain' ? !!hostname.trim() && !!token.trim() : true; + const canContinue = step === 1 ? installed : step === 2 ? true : step === 3 && mode === 'domain' ? !!hostname.trim() && !!token.trim() : true; async function inspect() { setPending(true); setError(''); try { setDetected(await api.get('/api/serving/tunnel')); } @@ -80,7 +80,7 @@ export function CloudflareDialog({ open, onClose, onCreateKey }: { } } const titles = ['Choose your address', installed ? 'Connector ready' : 'Preparing your connector', 'Protect your connection', ...(mode === 'domain' ? ['Set up your domain'] : []), 'Ready to connect']; return - {tr(active ? <>{tr("Done")} void stop()}>{tr(tunnel?.status === 'stopping' ? 'Stopping…' : 'Stop tunnel')} : <>{tr(step ? 'Back' : 'Cancel')}{tr(step === last ? void connect()}>{tr(pending ? 'Connecting…' : (error || tunnel?.error) ? 'Try again' : mode === 'quick' ? 'Create temporary URL' : 'Connect domain')} : {tr("Continue")})})} + {tr(active ? <>{tr("Done")} void stop()}>{tr(tunnel?.status === 'stopping' ? 'Stopping…' : 'Stop tunnel')} : <>{tr(step ? 'Back' : 'Cancel')}{tr(step === last ? void connect()}>{tr(pending ? 'Connecting…' : (error || tunnel?.error) ? 'Try again' : mode === 'quick' ? 'Create temporary URL' : 'Connect domain')} : {tr("Continue")})})} }>
{tr(!active ?
    {tr(steps.map((label, i) =>
  1. i || undefined}>
  2. ))}
: null)} @@ -92,9 +92,9 @@ export function CloudflareDialog({ open, onClose, onCreateKey }: { (e.currentTarget.parentElement?.children[mode === 'quick' ? 1 : 0] as HTMLElement)?.focus(); } }} onClick={() => setMode(choice.value)}>{tr(choice.title)}{tr(choice.detail)}{tr(mode === choice.value ? : null)}))}
: null)} {tr(step === 1 ?

{tr(installed ? 'Your Cloudflare connector is installed and ready.' : error ? 'Automatic setup needs another try.' : 'Strata downloads and sets up the Cloudflare connector for you.')}

{tr(installed ? <>{tr(tunnel?.connector ?

{tr("Using {0}", [tunnel.connector.summary])}

: null)}{tr(other ?

{tr(other.others.length === 1 && !other.own.length ? "cloudflared is already running for another tunnel (PID {0})." : other.own.length ? "Strata’s tunnel for this address is already running (PID {0})." : "cloudflared is already running for other tunnels (PIDs {0}).", [(other.own.length ? other.own : other.others).join(", ")])}

: null)} : error ? setRetry(v => v + 1)}>{tr("Retry setup")} : {tr("Setting up\u2026")})}
: null)} - {tr(step === 2 ? <>

{tr("Only apps with your Strata API key can use this connection.")}

{tr(keyReady ?
{tr("API key ready")}{tr("Use your existing key in the app you connect.")}
:

{tr("Create a key to protect your API. You\u2019ll return here when it\u2019s ready.")}

{tr("Create an API key")}
)} : null)} + {tr(step === 2 ? <>

{tr(keyReady ? "Only apps with your Strata API key can use this connection." : "Without an API key, anyone who finds the link can use your model.")}

{tr(keyReady ?
{tr("API key ready")}{tr("Use your existing key in the app you connect.")}
:

{tr("Create a key to protect your API. You\u2019ll return here when it\u2019s ready. You can also continue without one, but anyone who finds the link can then use your model.")}

{tr("Create an API key (recommended)")}
)} : null)} {tr(mode === 'domain' && step === 3 ? <>

{tr("Create a tunnel in your ")}{tr("Cloudflare dashboard \u2197")}{tr(". Copy the token from its connector setup.")}

{tr("Add a published application route for this hostname with this service URL:")}

{tunnel?.originUrl ?? 'Loading…'} void navigator.clipboard.writeText(tunnel!.originUrl)}/>

{tr("The token is used for this connection only and is not saved.")}

: null)} - {tr(step === last ? <>

{tr(mode === 'quick' ? 'Cloudflare will create your address when you connect.' : 'Start the connector for the domain you configured.')}

{tr("Address")}
{tr(mode === 'quick' ? 'Free temporary URL' : hostname)}
{tr("Connector")}
{tr(tunnel?.connector?.version ? "Installed · cloudflared {0}" : "Installed", tunnel?.connector?.version ? [tunnel.connector.version] : undefined)}
{tr("Access")}
{tr("API key required")}

{tr(mode === 'quick' ? 'The address changes each time. Turn off streaming in your client; replies arrive all at once.' : 'Use this hostname and your Strata API key in your client. Streaming is supported.')}

{tr("Only the inference API is shared. Your computer and Strata must stay on.")}

: null)} + {tr(step === last ? <>

{tr(mode === 'quick' ? 'Cloudflare will create your address when you connect.' : 'Start the connector for the domain you configured.')}

{tr("Address")}
{tr(mode === 'quick' ? 'Free temporary URL' : hostname)}
{tr("Connector")}
{tr(tunnel?.connector?.version ? "Installed · cloudflared {0}" : "Installed", tunnel?.connector?.version ? [tunnel.connector.version] : undefined)}
{tr("Access")}
{tr(keyReady ? "API key required" : "No API key: anyone with the link can use it")}

{tr(mode === 'quick' ? 'The address changes each time. Turn off streaming in your client; replies arrive all at once.' : 'Use this hostname and your Strata API key in your client. Streaming is supported.')}

{tr("Only the inference API is shared. Your computer and Strata must stay on.")}

: null)} )} {tr(error || tunnel?.error ? : null)} diff --git a/apps/host/src/lib/types.ts b/apps/host/src/lib/types.ts index 1e8b053e..c86aea15 100644 --- a/apps/host/src/lib/types.ts +++ b/apps/host/src/lib/types.ts @@ -223,11 +223,11 @@ export interface ServeRequest { finishReason?: string | null; } -export interface TunnelState { status: 'idle' | 'starting' | 'connected' | 'stopping' | 'error'; mode: 'quick' | 'domain' | null; url: string | null; error: string | null; installed: boolean; originUrl: string; connector?: { path: string; version: string | null; source: string; label: string; summary: string } | null; external?: { kind: 'service' | 'process'; others: number[]; own: number[]; message: string } | null; } +export interface TunnelState { status: 'idle' | 'starting' | 'connected' | 'stopping' | 'error'; mode: 'quick' | 'domain' | null; url: string | null; error: string | null; warning?: string | null; installed: boolean; originUrl: string; connector?: { path: string; version: string | null; source: string; label: string; summary: string } | null; external?: { kind: 'service' | 'process'; others: number[]; own: number[]; message: string } | null; } export interface ServingState { tunnel?: TunnelState; - sharing?: { active: boolean; url: string | null }; + sharing?: { active: boolean; url: string | null; warning?: string | null }; revision?: number; running: boolean; baseUrl: string; diff --git a/apps/host/src/screens/Serving.tsx b/apps/host/src/screens/Serving.tsx index e2642b90..ac8e43f5 100644 --- a/apps/host/src/screens/Serving.tsx +++ b/apps/host/src/screens/Serving.tsx @@ -97,7 +97,7 @@ export function ServingScreen() { {tr(error ? : null)} setGuide(false)} headerActions={
} sheet>
{tr(multipleModels ?