Procházet zdrojové kódy

修复8800下Agent身份同步

LSamien před 2 měsíci
rodič
revize
96faf4613e
2 změnil soubory, kde provedl 30 přidání a 4 odebrání
  1. 19 1
      src/server/routes.ts
  2. 11 3
      tests/server/localAuthRoutes.test.ts

+ 19 - 1
src/server/routes.ts

@@ -157,6 +157,8 @@ function profilesWithHermesBotNames(profiles: unknown[], pluginProfiles: JsonObj
     return agentName ? {
       ...profile,
       agentName,
+      description: agentName,
+      display_name: agentName,
     } : profile
   })
 }
@@ -875,6 +877,18 @@ export function createApiRouter(dependencies: RouteDependencies): Router {
       server_kind: 'yaoyao-web',
     })
   })
+  router.get('/api/profiles', async (ctx) => {
+    dependencies.auth.require(ctx, true)
+    const profilesResponse = await dependencies.upstreamSession.request('/api/profiles')
+    const pluginProfilesResponse = await dependencies.upstreamSession.request('/api/plugins/yaoyao/profiles')
+    json(ctx, profilesResponse.status, {
+      profiles: profilesWithHermesBotNames(
+        normalizedProfiles(requireSuccess(profilesResponse)),
+        pluginProfilesResponse.status >= 200 && pluginProfilesResponse.status < 300
+          ? parseJson(pluginProfilesResponse) : undefined,
+      ),
+    })
+  })
   router.put('/api/account/credentials', (ctx) => {
     const request = body(ctx)
     const currentPassword = typeof request.currentPassword === 'string' ? request.currentPassword : ''
@@ -1688,7 +1702,11 @@ export function createApiRouter(dependencies: RouteDependencies): Router {
       method: ctx.method,
       search: new URLSearchParams(ctx.querystring),
       body: ['GET', 'HEAD'].includes(ctx.method) ? undefined : optionalBody(ctx),
-      clientAddress: ctx.req.socket.remoteAddress,
+      // Hermes binds WebSocket tickets to the request address. The relay's
+      // upstream connection originates from 8800 itself, so forwarding the
+      // phone's address here would make 9119 reject the ticket during Upgrade.
+      clientAddress: upstreamPath === '/api/auth/ws-ticket'
+        ? undefined : ctx.req.socket.remoteAddress,
     })
     sendUpstreamResponse(ctx, response, dependencies.upstreamSession.jar)
   })

+ 11 - 3
tests/server/localAuthRoutes.test.ts

@@ -30,6 +30,7 @@ describe('8800 local authentication routes', () => {
       mediaOwner: 'tester', allowInsecureLan: false, insecureLan: false, production: false,
       superviseDashboard: true,
     }
+    let wsTicketForwardedFor: string | null | undefined
     const fetchImpl = (async (input: string | URL | Request, init?: RequestInit) => {
       const path = new URL(input instanceof Request ? input.url : String(input)).pathname
       const headers = new Headers(init?.headers)
@@ -42,8 +43,11 @@ describe('8800 local authentication routes', () => {
         headers: { 'content-type': 'application/json', 'set-cookie': 'hermes_service=session; Path=/; HttpOnly' },
       })
       if (path === '/api/profiles') return Response.json({ profiles: [{ name: 'default', is_default: true }] })
-      if (path === '/api/auth/ws-ticket') return Response.json({ ticket: 'upstream-ticket' })
-      if (path === '/api/plugins/yaoyao/profiles') return Response.json({ profiles: [] })
+      if (path === '/api/auth/ws-ticket') {
+        wsTicketForwardedFor = headers.get('x-forwarded-for')
+        return Response.json({ ticket: 'upstream-ticket' })
+      }
+      if (path === '/api/plugins/yaoyao/profiles') return Response.json({ profiles: [{ name: 'default', botName: '丫头' }] })
       return Response.json({ ok: true })
     }) as typeof fetch
     const runtime = createApplication({ config, fetchImpl })
@@ -96,8 +100,12 @@ describe('8800 local authentication routes', () => {
       .send({ provider: 'basic', username: 'owner', password: 'new-password', next: '' }).expect(200)
     const identity = await native.get('/api/auth/me').set('Host', '127.0.0.1:8800').expect(200)
     expect(identity.body).toMatchObject({ username: 'owner', role: 'admin', server_kind: 'yaoyao-web' })
-    await native.get('/api/profiles').set('Host', '127.0.0.1:8800').expect(200)
+    const nativeProfiles = await native.get('/api/profiles').set('Host', '127.0.0.1:8800').expect(200)
+    expect(nativeProfiles.body.profiles[0]).toMatchObject({
+      name: 'default', description: '丫头', display_name: '丫头', agentName: '丫头',
+    })
     await native.post('/api/auth/ws-ticket').set('Host', '127.0.0.1:8800').send({}).expect(200, /upstream-ticket/)
+    expect(wsTicketForwardedFor).toBeNull()
   })
 
   it('lets one 8800 claim another 8800 as a direct child node', async () => {