File size: 9,651 Bytes
1f21206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import {
  api,
  getDefaultBaseUrl,
  hasExplicitDefaultBaseUrl,
  setAuthToken,
  setBaseUrl,
} from '../api/client'

export const H5_SERVER_URL_STORAGE_KEY = 'cc-haha-h5-server-url'
export const H5_TOKEN_STORAGE_KEY = 'cc-haha-h5-token'

type H5ConnectionFailureReason =
  | 'missing-token'
  | 'invalid-token'
  | 'verify-failed'
  | 'unreachable'

export type StoredH5Connection = {
  serverUrl: string | null
  token: string | null
}

export class H5ConnectionRequiredError extends Error {
  readonly serverUrl: string
  readonly reason: H5ConnectionFailureReason

  constructor(message: string, serverUrl: string, reason: H5ConnectionFailureReason) {
    super(message)
    this.name = 'H5ConnectionRequiredError'
    this.serverUrl = serverUrl
    this.reason = reason
  }
}

export function isTauriRuntime() {
  if (typeof window === 'undefined') return false
  return '__TAURI_INTERNALS__' in window || '__TAURI__' in window
}

export function isBrowserH5Runtime() {
  return typeof window !== 'undefined' && !isTauriRuntime()
}

export function readStoredH5Connection(): StoredH5Connection {
  if (typeof window === 'undefined') {
    return { serverUrl: null, token: null }
  }

  try {
    return {
      serverUrl: normalizeServerUrl(window.localStorage.getItem(H5_SERVER_URL_STORAGE_KEY)),
      token: normalizeToken(window.localStorage.getItem(H5_TOKEN_STORAGE_KEY)),
    }
  } catch {
    return { serverUrl: null, token: null }
  }
}

export function clearStoredH5Connection() {
  if (typeof window !== 'undefined') {
    try {
      window.localStorage.removeItem(H5_SERVER_URL_STORAGE_KEY)
      window.localStorage.removeItem(H5_TOKEN_STORAGE_KEY)
    } catch {
      // Ignore storage failures
    }
  }

  setAuthToken(null)
}

export async function saveAndVerifyH5Connection(serverUrl: string, token: string) {
  const normalizedServerUrl = normalizeServerUrl(serverUrl)
  const normalizedToken = normalizeToken(token)

  if (!normalizedServerUrl) {
    throw new Error('Enter a valid server URL.')
  }

  if (!normalizedToken) {
    throw new Error('Enter your H5 access token.')
  }

  setBaseUrl(normalizedServerUrl)
  setAuthToken(normalizedToken)
  rememberStoredH5ServerUrl(normalizedServerUrl)

  try {
    await waitForHealth(normalizedServerUrl)
    await verifyH5Access()
  } catch (error) {
    clearStoredH5Token()
    throw normalizeBrowserH5Error(error, normalizedServerUrl)
  }

  if (typeof window !== 'undefined') {
    try {
      window.localStorage.setItem(H5_TOKEN_STORAGE_KEY, normalizedToken)
    } catch {
      // Ignore storage failures after a successful verification.
    }
  }

  return normalizedServerUrl
}

export function isH5ConnectionRequiredError(error: unknown): error is H5ConnectionRequiredError {
  return error instanceof H5ConnectionRequiredError
}

export async function initializeDesktopServerUrl() {
  const fallbackUrl = getDefaultBaseUrl()

  if (!isTauriRuntime()) {
    return initializeBrowserServerUrl(fallbackUrl)
  }

  try {
    const { invoke } = await import('@tauri-apps/api/core')
    const serverUrl = await invoke<string>('get_server_url')
    setBaseUrl(serverUrl)
    setAuthToken(null)
    await waitForHealth(serverUrl)
    return serverUrl
  } catch (error) {
    const message =
      error instanceof Error ? error.message : `desktop server startup failed: ${String(error)}`
    console.error('[desktop] Failed to initialize desktop server URL', error)
    throw new Error(message || `desktop server startup failed (fallback would be ${fallbackUrl})`)
  }
}

async function initializeBrowserServerUrl(fallbackUrl: string) {
  const query = typeof window !== 'undefined'
    ? new URLSearchParams(window.location.search)
    : null
  const queryUrl = query?.get('serverUrl') ?? null
  const queryToken = normalizeToken(query?.get('h5Token') ?? query?.get('token'))
  const stored = readStoredH5Connection()
  const configuredUrl = getConfiguredBrowserServerUrl(fallbackUrl)
  const requestedUrl =
    normalizeServerUrl(queryUrl) ??
    configuredUrl ??
    stored.serverUrl ??
    fallbackUrl
  const token = queryToken ?? stored.token
  const browserH5Runtime = requiresH5AuthForServerUrl(requestedUrl)

  setBaseUrl(requestedUrl)
  setAuthToken(browserH5Runtime ? token : null)
  if (browserH5Runtime) {
    rememberStoredH5ServerUrl(requestedUrl)
  }

  try {
    await waitForHealth(requestedUrl)
  } catch (error) {
    if (browserH5Runtime) {
      clearStoredH5Token()
      throw normalizeBrowserH5Error(error, requestedUrl)
    }
    throw error
  }

  if (!browserH5Runtime) {
    await ensureBrowserApiAccessibleWithoutH5(requestedUrl)
    return requestedUrl
  }

  if (!token) {
    clearStoredH5Token()
    throw new H5ConnectionRequiredError(
      'Enter your H5 token to continue.',
      requestedUrl,
      'missing-token',
    )
  }

  try {
    await verifyH5Access()
  } catch (error) {
    clearStoredH5Token()
    throw normalizeBrowserH5Error(error, requestedUrl)
  }

  if (queryToken && typeof window !== 'undefined') {
    try {
      window.localStorage.setItem(H5_TOKEN_STORAGE_KEY, queryToken)
    } catch {
      // Ignore storage failures after successful verification.
    }
  }

  return requestedUrl
}

async function waitForHealth(serverUrl: string) {
  let lastError: unknown

  for (let attempt = 0; attempt < 30; attempt++) {
    try {
      const response = await fetch(`${serverUrl}/health`, {
        cache: 'no-store',
      })
      if (response.ok) {
        const contentType = response.headers.get('content-type') ?? ''
        if (!contentType.toLowerCase().includes('application/json')) {
          lastError = new Error(`healthcheck returned non-JSON response from ${serverUrl}/health`)
        } else {
          const body = await response.json().catch(() => null)
          if (body && typeof body === 'object' && 'status' in body && body.status === 'ok') {
            return
          }
          lastError = new Error(`healthcheck returned invalid response from ${serverUrl}/health`)
        }
      } else {
        lastError = new Error(`healthcheck returned ${response.status}`)
      }
    } catch (error) {
      lastError = error
    }

    await new Promise((resolve) => setTimeout(resolve, 250))
  }

  throw new Error(
    lastError instanceof Error
      ? `Server healthcheck failed: ${lastError.message}`
      : 'Server healthcheck failed',
  )
}

async function verifyH5Access() {
  await api.post<{ ok: true }>('/api/h5-access/verify')
}

async function ensureBrowserApiAccessibleWithoutH5(serverUrl: string) {
  const response = await fetch(`${serverUrl}/api/status`, {
    cache: 'no-store',
  })
  if (response.status === 401) {
    throw new H5ConnectionRequiredError(
      'Enter your H5 token to continue.',
      serverUrl,
      'missing-token',
    )
  }
}

function normalizeServerUrl(value: string | null | undefined) {
  const trimmed = value?.trim()
  if (!trimmed) return null

  try {
    return new URL(trimmed).toString().replace(/\/$/, '')
  } catch {
    return null
  }
}

function normalizeToken(value: string | null | undefined) {
  const trimmed = value?.trim()
  return trimmed ? trimmed : null
}

function getSameOriginServerUrl() {
  if (typeof window === 'undefined') {
    return null
  }

  if (window.location.protocol !== 'http:' && window.location.protocol !== 'https:') {
    return null
  }

  return normalizeServerUrl(window.location.origin)
}

function getConfiguredBrowserServerUrl(fallbackUrl: string) {
  if (hasExplicitDefaultBaseUrl()) {
    return normalizeServerUrl(fallbackUrl)
  }

  return getSameOriginServerUrl()
}

export function isLoopbackHostname(hostname: string) {
  const normalized = hostname.trim().replace(/^\[/, '').replace(/\]$/, '').toLowerCase()
  return normalized === '127.0.0.1' || normalized === 'localhost' || normalized === '::1'
}

export function requiresH5AuthForServerUrl(serverUrl: string, browserHostname = getBrowserHostname()) {
  void browserHostname
  try {
    return !isLoopbackHostname(new URL(serverUrl).hostname)
  } catch {
    return false
  }
}

function getBrowserHostname() {
  if (typeof window === 'undefined') return null
  return window.location.hostname
}

function normalizeBrowserH5Error(error: unknown, serverUrl: string) {
  if (error instanceof H5ConnectionRequiredError) {
    return error
  }

  if (error instanceof Error && error.message.startsWith('Server healthcheck failed')) {
    return new H5ConnectionRequiredError(
      `Unable to reach ${serverUrl}. Check the server URL or network access.`,
      serverUrl,
      'unreachable',
    )
  }

  const message =
    error instanceof Error ? error.message : 'Unable to verify the H5 access token.'
  const status = typeof error === 'object' && error !== null && 'status' in error
    ? (error as { status?: unknown }).status
    : undefined
  const unauthorized = status === 401 || message.includes('401') || message.toLowerCase().includes('unauthorized')
  return new H5ConnectionRequiredError(
    unauthorized ? 'The saved H5 token is no longer valid.' : 'Unable to verify the H5 access token.',
    serverUrl,
    unauthorized ? 'invalid-token' : 'verify-failed',
  )
}

function rememberStoredH5ServerUrl(serverUrl: string) {
  if (typeof window === 'undefined') return

  try {
    window.localStorage.setItem(H5_SERVER_URL_STORAGE_KEY, serverUrl)
  } catch {
    // Ignore storage failures.
  }
}

function clearStoredH5Token() {
  if (typeof window !== 'undefined') {
    try {
      window.localStorage.removeItem(H5_TOKEN_STORAGE_KEY)
    } catch {
      // Ignore storage failures.
    }
  }

  setAuthToken(null)
}