File size: 14,187 Bytes
1f21206 e1bb50d 1f21206 1ef6a2f 1f21206 e1bb50d 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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | /**
* Claude Code Desktop App โ HTTP + WebSocket Server
*
* ไธบๆก้ข็ซฏ UI ๆไพ REST API ๅ WebSocket ๅฎๆถ้ไฟกใ
* ่ฏปๅไธ CLI ๅฎๅ
จ็ธๅ็ๆไปถ็ณป็ป๏ผ็กฎไฟ CLI/UI ๆฐๆฎไบ้ใ
*/
import { handleApiRequest } from './router.js'
import { handleWebSocket, type WebSocketData } from './ws/handler.js'
import { resolveCors, type CorsResolution } from './middleware/cors.js'
import { requireAuth, requireH5Token } from './middleware/auth.js'
import { teamWatcher } from './services/teamWatcher.js'
import { cronScheduler } from './services/cronScheduler.js'
import { handleProxyRequest } from './proxy/handler.js'
import { ProviderService } from './services/providerService.js'
import { handleHahaOAuthCallback } from './api/haha-oauth.js'
import { handleHahaOpenAIOAuthCallback } from './api/haha-openai-oauth.js'
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import { OPENAI_CODEX_REDIRECT_PATH } from '../services/openaiAuth/client.js'
import { ensureDesktopCliLauncherInstalled } from './services/desktopCliLauncherService.js'
import { enableConfigs } from '../utils/config.js'
import { diagnosticsService } from './services/diagnosticsService.js'
import { ensurePersistentStorageUpgraded } from './services/persistentStorageMigrations.js'
import { handleStaticH5Request } from './staticH5.js'
import { classifyH5Request, shouldBlockDisabledH5Access, shouldRequireH5Token } from './h5AccessPolicy.js'
import { H5AccessService } from './services/h5AccessService.js'
function readArgValue(flag: string): string | undefined {
const args = process.argv.slice(2)
const index = args.indexOf(flag)
if (index === -1) return undefined
return args[index + 1]
}
function hasArgFlag(flag: string): boolean {
return process.argv.slice(2).includes(flag)
}
function resolveServerOptions() {
const portArg = readArgValue('--port')
const port = Number.parseInt(portArg || process.env.SERVER_PORT || '3456', 10)
const host = readArgValue('--host') || process.env.SERVER_HOST || '127.0.0.1'
const cliPath = readArgValue('--cli-path')
const authRequired = hasArgFlag('--auth-required')
if (cliPath) {
process.env.CLAUDE_CLI_PATH = cliPath
}
return { port, host, authRequired }
}
const SERVER_OPTIONS = resolveServerOptions()
const PORT = SERVER_OPTIONS.port
const HOST = SERVER_OPTIONS.host
function withCors(response: Response, cors: CorsResolution): Response {
const headers = new Headers(response.headers)
for (const [key, value] of Object.entries(cors.headers)) {
headers.set(key, value)
}
return new Response(response.body, {
status: response.status,
headers,
})
}
function corsRejectedResponse(cors: CorsResolution): Response {
return Response.json(
{ error: 'CORS origin not allowed' },
{ status: 403, headers: cors.headers },
)
}
function h5AccessControlRejectedResponse(): Response {
return Response.json(
{
error: 'Forbidden',
message: 'H5 access settings can only be changed from the local desktop app.',
},
{ status: 403 },
)
}
function h5AccessDisabledResponse(): Response {
return Response.json(
{
error: 'Forbidden',
message: 'H5 access is disabled. Enable H5 access from the local desktop app first.',
},
{ status: 403 },
)
}
function isH5AccessControlRequest(
req: Request,
url: URL,
context: { clientAddress: string | null },
): boolean {
if (!url.pathname.startsWith('/api/h5-access')) {
return false
}
if (url.pathname === '/api/h5-access/verify') {
return false
}
return classifyH5Request(req, url, context) !== 'local-trusted'
}
function originFromUrl(value: string | null): string | null {
if (!value) {
return null
}
try {
return new URL(value).origin
} catch {
return null
}
}
export function startServer(port = PORT, host = HOST) {
enableConfigs()
diagnosticsService.installConsoleCapture()
diagnosticsService.installProcessCapture()
ProviderService.setServerPort(port)
const localConnectHost =
host === '0.0.0.0' || host === '127.0.0.1' || host === 'localhost'
? '127.0.0.1'
: host
/**
* Explicit deployment auth remains a stronger override than H5-scoped
* request gating.
*/
const forceAuth =
SERVER_OPTIONS.authRequired ||
process.env.SERVER_AUTH_REQUIRED === '1'
const h5AccessService = new H5AccessService()
let server: ReturnType<typeof Bun.serve<WebSocketData>>
try {
server = Bun.serve<WebSocketData>({
port,
hostname: host,
idleTimeout: 60,
async fetch(req, server) {
await ensurePersistentStorageUpgraded()
const url = new URL(req.url)
const origin = req.headers.get('Origin')
const clientAddress = server.requestIP(req)?.address ?? null
const h5RequestContext = { clientAddress }
const h5Settings = await h5AccessService.getSettings()
const h5PublicOrigin = originFromUrl(h5Settings.publicBaseUrl)
const cors = await resolveCors(origin, url.origin, {
h5Enabled: h5Settings.enabled,
isOriginAllowed: async (candidateOrigin) =>
candidateOrigin === h5PublicOrigin ||
await h5AccessService.isOriginAllowed(candidateOrigin),
})
const authRequired = shouldRequireH5Token({
request: req,
url,
h5Enabled: h5Settings.enabled,
context: h5RequestContext,
})
const h5AccessDisabledBlocked = shouldBlockDisabledH5Access({
request: req,
url,
h5Enabled: h5Settings.enabled,
explicitAuthRequired: forceAuth,
context: h5RequestContext,
})
const h5AccessControlBlocked = isH5AccessControlRequest(req, url, h5RequestContext)
if (h5AccessControlBlocked) {
return h5AccessControlRejectedResponse()
}
if (h5AccessDisabledBlocked) {
return h5AccessDisabledResponse()
}
// Handle CORS preflight
if (req.method === 'OPTIONS') {
if (cors.rejected) {
return corsRejectedResponse(cors)
}
return new Response(null, { status: 204, headers: cors.headers })
}
// WebSocket upgrade
if (url.pathname.startsWith('/ws/')) {
if (cors.rejected) {
return corsRejectedResponse(cors)
}
// Enforce authentication when required
if (authRequired) {
const authError = await requireH5Token(req, url.searchParams.get('token'))
if (authError) {
return withCors(authError, cors)
}
} else if (forceAuth) {
const authError = await requireAuth(req, url.searchParams.get('token'))
if (authError) {
return withCors(authError, cors)
}
}
// Validate session ID format
const sessionId = url.pathname.split('/').pop() || ''
if (!sessionId || !/^[0-9a-zA-Z_-]{1,64}$/.test(sessionId)) {
return new Response('Invalid session ID', { status: 400 })
}
const upgraded = server.upgrade(req, {
data: {
sessionId,
connectedAt: Date.now(),
channel: 'client',
sdkToken: null,
serverPort: port,
serverHost: localConnectHost,
},
})
if (upgraded) return undefined
return new Response('WebSocket upgrade failed', { status: 400 })
}
// Internal SDK WebSocket used by the spawned Claude CLI.
if (url.pathname.startsWith('/sdk/')) {
const classification = classifyH5Request(req, url, h5RequestContext)
console.log(
`[SDK-WS] classifyH5Request=${classification}, clientAddress=${h5RequestContext.clientAddress}, origin=${req.headers.get('Origin')}, path=${url.pathname}`,
)
if (classification !== 'internal-sdk') {
return h5AccessControlRejectedResponse()
}
if (cors.rejected) {
return corsRejectedResponse(cors)
}
if (forceAuth) {
const authError = await requireAuth(req, url.searchParams.get('token'))
if (authError) {
return withCors(authError, cors)
}
}
const sessionId = url.pathname.split('/').pop() || ''
if (!sessionId || !/^[0-9a-zA-Z_-]{1,64}$/.test(sessionId)) {
return new Response('Invalid session ID', { status: 400 })
}
const upgraded = server.upgrade(req, {
data: {
sessionId,
connectedAt: Date.now(),
channel: 'sdk',
sdkToken: url.searchParams.get('token'),
serverPort: port,
serverHost: localConnectHost,
},
})
if (upgraded) return undefined
return new Response('WebSocket upgrade failed', { status: 400 })
}
if (url.pathname === '/callback') {
return handleHahaOAuthCallback(url)
}
if (
url.pathname === OPENAI_CODEX_REDIRECT_PATH ||
url.pathname === '/callback/openai'
) {
return handleHahaOpenAIOAuthCallback(url)
}
// REST API
if (url.pathname.startsWith('/api/')) {
if (cors.rejected) {
return corsRejectedResponse(cors)
}
// Enforce authentication when required
if (authRequired) {
const authError = await requireH5Token(req)
if (authError) {
return withCors(authError, cors)
}
} else if (forceAuth) {
const authError = await requireAuth(req)
if (authError) {
return withCors(authError, cors)
}
}
try {
const response = await handleApiRequest(req, url)
return withCors(response, cors)
} catch (error) {
void diagnosticsService.recordEvent({
type: 'api_request_failed',
severity: 'error',
summary: error instanceof Error ? error.message : String(error),
details: { path: url.pathname, method: req.method, error },
})
console.error('[Server] API error:', error)
return withCors(Response.json(
{ error: 'Internal server error' },
{ status: 500 },
), cors)
}
}
// Proxy โ protocol-translating reverse proxy for OpenAI-compatible APIs
if (url.pathname.startsWith('/proxy/')) {
if (cors.rejected) {
return corsRejectedResponse(cors)
}
if (authRequired) {
const authError = await requireH5Token(req)
if (authError) {
return withCors(authError, cors)
}
} else if (forceAuth) {
const authError = await requireAuth(req)
if (authError) {
return withCors(authError, cors)
}
}
try {
const response = await handleProxyRequest(req, url)
return withCors(response, cors)
} catch (error) {
void diagnosticsService.recordEvent({
type: 'proxy_request_failed',
severity: 'error',
summary: error instanceof Error ? error.message : String(error),
details: { path: url.pathname, method: req.method, error },
})
console.error('[Server] Proxy error:', error)
return withCors(Response.json(
{ type: 'error', error: { type: 'api_error', message: 'Internal proxy error' } },
{ status: 500 },
), cors)
}
}
// Health check
if (url.pathname === '/health') {
if (cors.rejected) {
return corsRejectedResponse(cors)
}
return Response.json(
{ status: 'ok', timestamp: new Date().toISOString() },
{ headers: cors.headers },
)
}
// Static H5 shell/assets are non-secret bootstrap content and must load
// before the browser can read the QR token; API/proxy/ws stay protected above.
const staticResponse = await handleStaticH5Request(req, url)
if (staticResponse) {
return staticResponse
}
return new Response('Not Found', { status: 404 })
},
websocket: handleWebSocket,
})
} catch (error) {
const message = error instanceof Error && error.message
? error.message
: `Failed to start server. Is port ${port} in use?`
throw new Error(message, { cause: error })
}
// Start watching ~/.claude/teams/ for real-time WebSocket push
teamWatcher.start()
// Start the cron scheduler to execute scheduled tasks
cronScheduler.start()
void ensureDesktopCliLauncherInstalled().catch((error) => {
console.error(
'[desktop-cli-launcher] failed to install bundled launcher:',
error instanceof Error ? error.message : error,
)
})
console.log(`[Server] Claude Code API server running at http://${host}:${port}`)
return server
}
// โโโ Graceful shutdown: kill all CLI subprocesses on exit โโโโโโโโโโโโโโโโโโโโ
import { conversationService } from './services/conversationService.js'
function cleanupAllSessions() {
const active = conversationService.getActiveSessions()
if (active.length > 0) {
console.log(`[Server] Shutting down โ killing ${active.length} CLI subprocess(es)`)
for (const sessionId of active) {
conversationService.stopSession(sessionId)
}
}
}
process.on('SIGTERM', () => {
console.log('[Server] Received SIGTERM')
cleanupAllSessions()
process.exit(0)
})
process.on('SIGINT', () => {
console.log('[Server] Received SIGINT')
cleanupAllSessions()
process.exit(0)
})
process.on('exit', () => {
cleanupAllSessions()
})
// Direct execution
if (import.meta.main) {
startServer()
}
|