@@ -19,10 +19,6 @@ import (
1919
2020// 预编译正则表达式(避免每次调用重新编译)
2121var (
22- // 匹配 user_id 格式:
23- // 旧格式: user_{64位hex}_account__session_{uuid} (account 后无 UUID)
24- // 新格式: user_{64位hex}_account_{uuid}_session_{uuid} (account 后有 UUID)
25- userIDRegex = regexp .MustCompile (`^user_[a-f0-9]{64}_account_([a-f0-9-]*)_session_([a-f0-9-]{36})$` )
2622 // 匹配 User-Agent 版本号: xxx/x.y.z
2723 userAgentVersionRegex = regexp .MustCompile (`/(\d+)\.(\d+)\.(\d+)` )
2824)
@@ -209,12 +205,12 @@ func (s *IdentityService) ApplyFingerprint(req *http.Request, fp *Fingerprint) {
209205}
210206
211207// RewriteUserID 重写body中的metadata.user_id
212- // 输入格式:user_{clientId}_account__session_{sessionUUID}
213- // 输出格式:user_{cachedClientID}_account_{accountUUID}_session_{newHash}
208+ // 支持旧拼接格式和新 JSON 格式的 user_id 解析,
209+ // 根据 fingerprintUA 版本选择输出格式。
214210//
215211// 重要:此函数使用 json.RawMessage 保留其他字段的原始字节,
216212// 避免重新序列化导致 thinking 块等内容被修改。
217- func (s * IdentityService ) RewriteUserID (body []byte , accountID int64 , accountUUID , cachedClientID string ) ([]byte , error ) {
213+ func (s * IdentityService ) RewriteUserID (body []byte , accountID int64 , accountUUID , cachedClientID , fingerprintUA string ) ([]byte , error ) {
218214 if len (body ) == 0 || accountUUID == "" || cachedClientID == "" {
219215 return body , nil
220216 }
@@ -241,24 +237,21 @@ func (s *IdentityService) RewriteUserID(body []byte, accountID int64, accountUUI
241237 return body , nil
242238 }
243239
244- // 匹配格式:
245- // 旧格式: user_{64位hex}_account__session_{uuid}
246- // 新格式: user_{64位hex}_account_{uuid}_session_{uuid}
247- matches := userIDRegex .FindStringSubmatch (userID )
248- if matches == nil {
240+ // 解析 user_id(兼容旧拼接格式和新 JSON 格式)
241+ parsed := ParseMetadataUserID (userID )
242+ if parsed == nil {
249243 return body , nil
250244 }
251245
252- // matches[1] = account UUID (可能为空), matches[2] = session UUID
253- sessionTail := matches [2 ] // 原始session UUID
246+ sessionTail := parsed .SessionID // 原始session UUID
254247
255248 // 生成新的session hash: SHA256(accountID::sessionTail) -> UUID格式
256249 seed := fmt .Sprintf ("%d::%s" , accountID , sessionTail )
257250 newSessionHash := generateUUIDFromSeed (seed )
258251
259- // 构建新的user_id
260- // 格式: user_{cachedClientID}_account_{account_uuid}_session_{newSessionHash}
261- newUserID := fmt . Sprintf ( "user_%s_account_%s_session_%s" , cachedClientID , accountUUID , newSessionHash )
252+ // 根据客户端版本选择输出格式
253+ version := ExtractCLIVersion ( fingerprintUA )
254+ newUserID := FormatMetadataUserID ( cachedClientID , accountUUID , newSessionHash , version )
262255
263256 metadata ["user_id" ] = newUserID
264257
@@ -278,9 +271,9 @@ func (s *IdentityService) RewriteUserID(body []byte, accountID int64, accountUUI
278271//
279272// 重要:此函数使用 json.RawMessage 保留其他字段的原始字节,
280273// 避免重新序列化导致 thinking 块等内容被修改。
281- func (s * IdentityService ) RewriteUserIDWithMasking (ctx context.Context , body []byte , account * Account , accountUUID , cachedClientID string ) ([]byte , error ) {
274+ func (s * IdentityService ) RewriteUserIDWithMasking (ctx context.Context , body []byte , account * Account , accountUUID , cachedClientID , fingerprintUA string ) ([]byte , error ) {
282275 // 先执行常规的 RewriteUserID 逻辑
283- newBody , err := s .RewriteUserID (body , account .ID , accountUUID , cachedClientID )
276+ newBody , err := s .RewriteUserID (body , account .ID , accountUUID , cachedClientID , fingerprintUA )
284277 if err != nil {
285278 return newBody , err
286279 }
@@ -312,10 +305,9 @@ func (s *IdentityService) RewriteUserIDWithMasking(ctx context.Context, body []b
312305 return newBody , nil
313306 }
314307
315- // 查找 _session_ 的位置,替换其后的内容
316- const sessionMarker = "_session_"
317- idx := strings .LastIndex (userID , sessionMarker )
318- if idx == - 1 {
308+ // 解析已重写的 user_id
309+ uidParsed := ParseMetadataUserID (userID )
310+ if uidParsed == nil {
319311 return newBody , nil
320312 }
321313
@@ -337,8 +329,9 @@ func (s *IdentityService) RewriteUserIDWithMasking(ctx context.Context, body []b
337329 logger .LegacyPrintf ("service.identity" , "Warning: failed to set masked session ID for account %d: %v" , account .ID , err )
338330 }
339331
340- // 替换 session 部分:保留 _session_ 之前的内容,替换之后的内容
341- newUserID := userID [:idx + len (sessionMarker )] + maskedSessionID
332+ // 用 FormatMetadataUserID 重建(保持与 RewriteUserID 相同的格式)
333+ version := ExtractCLIVersion (fingerprintUA )
334+ newUserID := FormatMetadataUserID (uidParsed .DeviceID , uidParsed .AccountUUID , maskedSessionID , version )
342335
343336 slog .Debug ("session_id_masking_applied" ,
344337 "account_id" , account .ID ,
0 commit comments