diff --git a/app/api/openshell/terminal/live/route.ts b/app/api/openshell/terminal/live/route.ts index 24d523d..39aad2a 100644 --- a/app/api/openshell/terminal/live/route.ts +++ b/app/api/openshell/terminal/live/route.ts @@ -4,6 +4,7 @@ import { resolveRuntimeAuthority } from '@/app/lib/runtimeAuthority' const TERMINAL_SERVER_URL = process.env.TERMINAL_SERVER_URL || 'http://127.0.0.1:3011' const TERMINAL_WS_PROXY_PATH = '/api/openshell/terminal/live/ws' +const TERMINAL_WS_PROXY_PORT = process.env.TERMINAL_WS_PROXY_PORT || process.env.OPENCLAW_DASHBOARD_WS_PROXY_PORT || '3001' const PUBLIC_BROWSER_HOST = process.env.PUBLIC_BROWSER_HOST || process.env.PUBLIC_WS_HOST || null const PUBLIC_BASE_URL = process.env.PUBLIC_BASE_URL || null @@ -16,19 +17,12 @@ function isNonRoutableHost(candidate: string, protocol = 'http:') { return ( parsed.hostname === '0.0.0.0' || parsed.hostname === '::' || - parsed.hostname === '127.0.0.1' || - parsed.hostname === 'localhost' || - parsed.hostname === '[::1]' || - parsed.hostname === '::1' + parsed.hostname === '[::]' ) } catch { return ( normalized.startsWith('0.0.0.0') || - normalized.startsWith('[::]') || - normalized.startsWith('127.0.0.1') || - normalized.startsWith('localhost') || - normalized.startsWith('[::1]') || - normalized.startsWith('::1') + normalized.startsWith('[::]') ) } } @@ -82,7 +76,9 @@ function getBrowserWebSocketUrl(request: Request, params: { } const baseProtocol = (forwardedProto && forwardedProto.trim()) || requestUrl.protocol.replace(/:$/, '') const protocol = baseProtocol === 'https' ? 'wss:' : 'ws:' - const websocketUrl = new URL(TERMINAL_WS_PROXY_PATH, `${protocol}//${host}`) + const websocketHost = new URL(`${protocol}//${host}`) + websocketHost.port = TERMINAL_WS_PROXY_PORT + const websocketUrl = new URL(TERMINAL_WS_PROXY_PATH, websocketHost) websocketUrl.searchParams.set('sessionId', params.sessionId) websocketUrl.searchParams.set('sandboxId', params.sandboxId) websocketUrl.searchParams.set('dashboardSessionId', params.dashboardSessionId) diff --git a/app/operator-terminal/page.tsx b/app/operator-terminal/page.tsx index bb2b80b..2dced42 100644 --- a/app/operator-terminal/page.tsx +++ b/app/operator-terminal/page.tsx @@ -79,6 +79,8 @@ function OperatorTerminalInner() { const terminalRef = useRef(null) const fitAddonRef = useRef(null) const socketRef = useRef(null) + const liveSessionIdRef = useRef('') + const liveSessionRequestRef = useRef | null>(null) useEffect(() => { setDashboardSessionId(ensureDashboardSessionId(requestedDashboardSessionId)) @@ -108,21 +110,23 @@ function OperatorTerminalInner() { }, [sandboxId]) const ensureLiveSession = useCallback(async () => { + if (liveSessionRequestRef.current) return liveSessionRequestRef.current setTerminalState('connecting') setTerminalStatus(sandboxId ? `Initializing live terminal session for ${sandboxId}…` : 'Initializing host-mode live terminal session…') - try { + const request = (async () => { const response = await fetch('/api/openshell/terminal/live', { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ sandboxId, sessionId: liveSession?.sessionId, dashboardSessionId }), + body: JSON.stringify({ sandboxId, sessionId: liveSessionIdRef.current, dashboardSessionId }), }) const result = await response.json() if (!response.ok || !result.ok) { throw new Error(result.error || 'Failed to initialize live terminal session.') } + liveSessionIdRef.current = result.sessionId setLiveSession({ sessionId: result.sessionId, sandboxId: result.sandboxId, @@ -131,11 +135,18 @@ function OperatorTerminalInner() { websocketUrl: result.websocketUrl, sshHostAlias: result.sshHostAlias, }) + })() + + liveSessionRequestRef.current = request + try { + await request } catch (error) { setTerminalState('error') setTerminalStatus(error instanceof Error ? error.message : 'Failed to initialize live terminal session.') + } finally { + liveSessionRequestRef.current = null } - }, [dashboardSessionId, sandboxId, liveSession?.sessionId]) + }, [dashboardSessionId, sandboxId]) useEffect(() => { refreshReadiness() }, [refreshReadiness]) useEffect(() => { ensureLiveSession() }, [ensureLiveSession]) diff --git a/server.mjs b/server.mjs index cf489a0..9bd2912 100644 --- a/server.mjs +++ b/server.mjs @@ -573,6 +573,21 @@ server.on('upgrade', (req, socket, head) => { }) dashboardWsProxyServer.on('upgrade', (req, socket, head) => { + if ((req.url || '').startsWith(terminalProxyPath)) { + if (!isAuthenticatedUpgrade(req)) { + rejectUnauthorizedUpgrade(req, socket, req.url || '/') + return + } + logBridge('terminal-sidecar-upgrade-accepted', { + path: req.url || '/', + remoteAddress: req.socket.remoteAddress || 'unknown', + }) + clientWss.handleUpgrade(req, socket, head, (ws) => { + clientWss.emit('connection', ws, req) + }) + return + } + if ( (req.url || '').startsWith(legacyDashboardProxyPrefix) || (req.url || '').startsWith(instancesProxyPrefix)