nyk commited on
Commit
f99c465
·
unverified ·
2 Parent(s): 14743027f2d33e

Merge pull request #203 from builderz-labs/fix/194-195-gateway-remote-connect

Browse files
src/components/panels/multi-gateway-panel.tsx CHANGED
@@ -3,6 +3,7 @@
3
  import { useState, useEffect, useCallback } from 'react'
4
  import { useMissionControl } from '@/store'
5
  import { useWebSocket } from '@/lib/websocket'
 
6
 
7
  interface Gateway {
8
  id: number
@@ -93,8 +94,11 @@ export function MultiGatewayPanel() {
93
  }
94
 
95
  const connectTo = (gw: Gateway) => {
96
- const proto = window.location.protocol === 'https:' ? 'wss' : 'ws'
97
- const wsUrl = `${proto}://${gw.host}:${gw.port}`
 
 
 
98
  connect(wsUrl, '') // token is handled by the gateway entry, not passed to frontend
99
  }
100
 
@@ -192,7 +196,7 @@ export function MultiGatewayPanel() {
192
  gateway={gw}
193
  health={healthByGatewayId.get(gw.id)}
194
  isProbing={probing === gw.id}
195
- isCurrentlyConnected={connection.url?.includes(`:${gw.port}`) ?? false}
196
  onSetPrimary={() => setPrimary(gw)}
197
  onDelete={() => deleteGateway(gw.id)}
198
  onConnect={() => connectTo(gw)}
 
3
  import { useState, useEffect, useCallback } from 'react'
4
  import { useMissionControl } from '@/store'
5
  import { useWebSocket } from '@/lib/websocket'
6
+ import { buildGatewayWebSocketUrl } from '@/lib/gateway-url'
7
 
8
  interface Gateway {
9
  id: number
 
94
  }
95
 
96
  const connectTo = (gw: Gateway) => {
97
+ const wsUrl = buildGatewayWebSocketUrl({
98
+ host: gw.host,
99
+ port: gw.port,
100
+ browserProtocol: window.location.protocol,
101
+ })
102
  connect(wsUrl, '') // token is handled by the gateway entry, not passed to frontend
103
  }
104
 
 
196
  gateway={gw}
197
  health={healthByGatewayId.get(gw.id)}
198
  isProbing={probing === gw.id}
199
+ isCurrentlyConnected={(connection.url?.includes(gw.host) ?? false) || (connection.url?.includes(`:${gw.port}`) ?? false)}
200
  onSetPrimary={() => setPrimary(gw)}
201
  onDelete={() => deleteGateway(gw.id)}
202
  onConnect={() => connectTo(gw)}
src/lib/__tests__/gateway-url.test.ts ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from 'vitest'
2
+ import { buildGatewayWebSocketUrl } from '@/lib/gateway-url'
3
+
4
+ describe('buildGatewayWebSocketUrl', () => {
5
+ it('builds ws URL with host and port for local dev', () => {
6
+ expect(buildGatewayWebSocketUrl({
7
+ host: '127.0.0.1',
8
+ port: 18789,
9
+ browserProtocol: 'http:',
10
+ })).toBe('ws://127.0.0.1:18789')
11
+ })
12
+
13
+ it('omits 18789 for remote hosts on https browser context', () => {
14
+ expect(buildGatewayWebSocketUrl({
15
+ host: 'cb-vcn.tail47c878.ts.net',
16
+ port: 18789,
17
+ browserProtocol: 'https:',
18
+ })).toBe('wss://cb-vcn.tail47c878.ts.net')
19
+ })
20
+
21
+ it('keeps explicit websocket URL host value unchanged aside from protocol normalization', () => {
22
+ expect(buildGatewayWebSocketUrl({
23
+ host: 'https://gateway.example.com',
24
+ port: 18789,
25
+ browserProtocol: 'https:',
26
+ })).toBe('wss://gateway.example.com')
27
+ })
28
+
29
+ it('preserves explicit URL port when provided in host', () => {
30
+ expect(buildGatewayWebSocketUrl({
31
+ host: 'https://gateway.example.com:8443',
32
+ port: 18789,
33
+ browserProtocol: 'https:',
34
+ })).toBe('wss://gateway.example.com:8443')
35
+ })
36
+ })
src/lib/gateway-url.ts ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function isLocalHost(host: string): boolean {
2
+ const normalized = host.toLowerCase()
3
+ return (
4
+ normalized === 'localhost' ||
5
+ normalized === '127.0.0.1' ||
6
+ normalized === '::1' ||
7
+ normalized.endsWith('.local')
8
+ )
9
+ }
10
+
11
+ function normalizeProtocol(protocol: string): 'ws:' | 'wss:' {
12
+ if (protocol === 'https:' || protocol === 'wss:') return 'wss:'
13
+ return 'ws:'
14
+ }
15
+
16
+ export function buildGatewayWebSocketUrl(input: {
17
+ host: string
18
+ port: number
19
+ browserProtocol?: string
20
+ }): string {
21
+ const rawHost = String(input.host || '').trim()
22
+ const port = Number(input.port)
23
+ const browserProtocol = input.browserProtocol === 'https:' ? 'https:' : 'http:'
24
+
25
+ if (!rawHost) {
26
+ return `${browserProtocol === 'https:' ? 'wss' : 'ws'}://127.0.0.1:${port || 18789}`
27
+ }
28
+
29
+ const prefixed =
30
+ rawHost.startsWith('ws://') ||
31
+ rawHost.startsWith('wss://') ||
32
+ rawHost.startsWith('http://') ||
33
+ rawHost.startsWith('https://')
34
+ ? rawHost
35
+ : null
36
+
37
+ if (prefixed) {
38
+ try {
39
+ const parsed = new URL(prefixed)
40
+ parsed.protocol = normalizeProtocol(parsed.protocol)
41
+ return parsed.toString().replace(/\/$/, '')
42
+ } catch {
43
+ return prefixed
44
+ }
45
+ }
46
+
47
+ const wsProtocol = browserProtocol === 'https:' ? 'wss' : 'ws'
48
+ const shouldOmitPort =
49
+ wsProtocol === 'wss' &&
50
+ !isLocalHost(rawHost) &&
51
+ port === 18789
52
+
53
+ return shouldOmitPort
54
+ ? `${wsProtocol}://${rawHost}`
55
+ : `${wsProtocol}://${rawHost}:${port || 18789}`
56
+ }
src/lib/websocket.ts CHANGED
@@ -83,6 +83,8 @@ export function useWebSocket() {
83
  normalized.includes('origin not allowed') ||
84
  normalized.includes('device identity required') ||
85
  normalized.includes('device_auth_signature_invalid') ||
 
 
86
  normalized.includes('auth rate limit') ||
87
  normalized.includes('rate limited')
88
  )
@@ -100,6 +102,9 @@ export function useWebSocket() {
100
  if (normalized.includes('device_auth_signature_invalid')) {
101
  return 'Gateway rejected device signature. Clear local device identity in the browser and reconnect.'
102
  }
 
 
 
103
  if (normalized.includes('auth rate limit') || normalized.includes('rate limited')) {
104
  return 'Gateway authentication is rate limited. Wait briefly, then reconnect.'
105
  }
 
83
  normalized.includes('origin not allowed') ||
84
  normalized.includes('device identity required') ||
85
  normalized.includes('device_auth_signature_invalid') ||
86
+ normalized.includes('invalid connect params') ||
87
+ normalized.includes('/client/id') ||
88
  normalized.includes('auth rate limit') ||
89
  normalized.includes('rate limited')
90
  )
 
102
  if (normalized.includes('device_auth_signature_invalid')) {
103
  return 'Gateway rejected device signature. Clear local device identity in the browser and reconnect.'
104
  }
105
+ if (normalized.includes('invalid connect params') || normalized.includes('/client/id')) {
106
+ return 'Gateway rejected client identity params. Ensure NEXT_PUBLIC_GATEWAY_CLIENT_ID is set to openclaw-control-ui and reconnect.'
107
+ }
108
  if (normalized.includes('auth rate limit') || normalized.includes('rate limited')) {
109
  return 'Gateway authentication is rate limited. Wait briefly, then reconnect.'
110
  }