Fix operator terminal websocket input

This commit is contained in:
Codex
2026-04-24 21:19:18 +00:00
parent c5f5cd33fe
commit 8daac62abc
3 changed files with 35 additions and 13 deletions
+6 -10
View File
@@ -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)
+14 -3
View File
@@ -79,6 +79,8 @@ function OperatorTerminalInner() {
const terminalRef = useRef<XTermInstance | null>(null)
const fitAddonRef = useRef<FitAddonInstance | null>(null)
const socketRef = useRef<WebSocket | null>(null)
const liveSessionIdRef = useRef<string>('')
const liveSessionRequestRef = useRef<Promise<void> | 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])
+15
View File
@@ -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)