@@ -117,7 +117,7 @@ func (e *ClaudeExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r
117117
118118 // Apply cloaking (system prompt injection, fake user ID, sensitive word obfuscation)
119119 // based on client type and configuration.
120- body = applyCloaking (ctx , e .cfg , auth , body , baseModel )
120+ body = applyCloaking (ctx , e .cfg , auth , body , baseModel , apiKey )
121121
122122 requestedModel := payloadRequestedModel (opts , req .Model )
123123 body = applyPayloadConfigWithRoot (e .cfg , baseModel , to .String (), "" , body , originalTranslated , requestedModel )
@@ -266,7 +266,7 @@ func (e *ClaudeExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A
266266
267267 // Apply cloaking (system prompt injection, fake user ID, sensitive word obfuscation)
268268 // based on client type and configuration.
269- body = applyCloaking (ctx , e .cfg , auth , body , baseModel )
269+ body = applyCloaking (ctx , e .cfg , auth , body , baseModel , apiKey )
270270
271271 requestedModel := payloadRequestedModel (opts , req .Model )
272272 body = applyPayloadConfigWithRoot (e .cfg , baseModel , to .String (), "" , body , originalTranslated , requestedModel )
@@ -990,10 +990,10 @@ func getClientUserAgent(ctx context.Context) string {
990990}
991991
992992// getCloakConfigFromAuth extracts cloak configuration from auth attributes.
993- // Returns (cloakMode, strictMode, sensitiveWords).
994- func getCloakConfigFromAuth (auth * cliproxyauth.Auth ) (string , bool , []string ) {
993+ // Returns (cloakMode, strictMode, sensitiveWords, cacheUserID ).
994+ func getCloakConfigFromAuth (auth * cliproxyauth.Auth ) (string , bool , []string , bool ) {
995995 if auth == nil || auth .Attributes == nil {
996- return "auto" , false , nil
996+ return "auto" , false , nil , false
997997 }
998998
999999 cloakMode := auth .Attributes ["cloak_mode" ]
@@ -1011,7 +1011,9 @@ func getCloakConfigFromAuth(auth *cliproxyauth.Auth) (string, bool, []string) {
10111011 }
10121012 }
10131013
1014- return cloakMode , strictMode , sensitiveWords
1014+ cacheUserID := strings .EqualFold (strings .TrimSpace (auth .Attributes ["cloak_cache_user_id" ]), "true" )
1015+
1016+ return cloakMode , strictMode , sensitiveWords , cacheUserID
10151017}
10161018
10171019// resolveClaudeKeyCloakConfig finds the matching ClaudeKey config and returns its CloakConfig.
@@ -1044,16 +1046,24 @@ func resolveClaudeKeyCloakConfig(cfg *config.Config, auth *cliproxyauth.Auth) *c
10441046}
10451047
10461048// injectFakeUserID generates and injects a fake user ID into the request metadata.
1047- func injectFakeUserID (payload []byte ) []byte {
1049+ // When useCache is false, a new user ID is generated for every call.
1050+ func injectFakeUserID (payload []byte , apiKey string , useCache bool ) []byte {
1051+ generateID := func () string {
1052+ if useCache {
1053+ return cachedUserID (apiKey )
1054+ }
1055+ return generateFakeUserID ()
1056+ }
1057+
10481058 metadata := gjson .GetBytes (payload , "metadata" )
10491059 if ! metadata .Exists () {
1050- payload , _ = sjson .SetBytes (payload , "metadata.user_id" , generateFakeUserID ())
1060+ payload , _ = sjson .SetBytes (payload , "metadata.user_id" , generateID ())
10511061 return payload
10521062 }
10531063
10541064 existingUserID := gjson .GetBytes (payload , "metadata.user_id" ).String ()
10551065 if existingUserID == "" || ! isValidUserID (existingUserID ) {
1056- payload , _ = sjson .SetBytes (payload , "metadata.user_id" , generateFakeUserID ())
1066+ payload , _ = sjson .SetBytes (payload , "metadata.user_id" , generateID ())
10571067 }
10581068 return payload
10591069}
@@ -1090,7 +1100,7 @@ func checkSystemInstructionsWithMode(payload []byte, strictMode bool) []byte {
10901100
10911101// applyCloaking applies cloaking transformations to the payload based on config and client.
10921102// Cloaking includes: system prompt injection, fake user ID, and sensitive word obfuscation.
1093- func applyCloaking (ctx context.Context , cfg * config.Config , auth * cliproxyauth.Auth , payload []byte , model string ) []byte {
1103+ func applyCloaking (ctx context.Context , cfg * config.Config , auth * cliproxyauth.Auth , payload []byte , model string , apiKey string ) []byte {
10941104 clientUserAgent := getClientUserAgent (ctx )
10951105
10961106 // Get cloak config from ClaudeKey configuration
@@ -1100,23 +1110,33 @@ func applyCloaking(ctx context.Context, cfg *config.Config, auth *cliproxyauth.A
11001110 var cloakMode string
11011111 var strictMode bool
11021112 var sensitiveWords []string
1113+ var cacheUserID bool
11031114
11041115 if cloakCfg != nil {
11051116 cloakMode = cloakCfg .Mode
11061117 strictMode = cloakCfg .StrictMode
11071118 sensitiveWords = cloakCfg .SensitiveWords
1119+ if cloakCfg .CacheUserID != nil {
1120+ cacheUserID = * cloakCfg .CacheUserID
1121+ }
11081122 }
11091123
11101124 // Fallback to auth attributes if no config found
11111125 if cloakMode == "" {
1112- attrMode , attrStrict , attrWords := getCloakConfigFromAuth (auth )
1126+ attrMode , attrStrict , attrWords , attrCache := getCloakConfigFromAuth (auth )
11131127 cloakMode = attrMode
11141128 if ! strictMode {
11151129 strictMode = attrStrict
11161130 }
11171131 if len (sensitiveWords ) == 0 {
11181132 sensitiveWords = attrWords
11191133 }
1134+ if cloakCfg == nil || cloakCfg .CacheUserID == nil {
1135+ cacheUserID = attrCache
1136+ }
1137+ } else if cloakCfg == nil || cloakCfg .CacheUserID == nil {
1138+ _ , _ , _ , attrCache := getCloakConfigFromAuth (auth )
1139+ cacheUserID = attrCache
11201140 }
11211141
11221142 // Determine if cloaking should be applied
@@ -1130,7 +1150,7 @@ func applyCloaking(ctx context.Context, cfg *config.Config, auth *cliproxyauth.A
11301150 }
11311151
11321152 // Inject fake user ID
1133- payload = injectFakeUserID (payload )
1153+ payload = injectFakeUserID (payload , apiKey , cacheUserID )
11341154
11351155 // Apply sensitive word obfuscation
11361156 if len (sensitiveWords ) > 0 {
0 commit comments