MCPcopy Create free account
hub / github.com/chainreactors/EvilProxy / addOrUpdateClientLocked

Method addOrUpdateClientLocked

internal/watcher/clients.go:167–266  ·  view source on GitHub ↗
(path string)

Source from the content-addressed store, hash-verified

165}
166
167func (w *Watcher) addOrUpdateClientLocked(path string) {
168 data, errRead := os.ReadFile(path)
169 if errRead != nil {
170 log.Errorf("failed to read auth file %s: %v", filepath.Base(path), errRead)
171 return
172 }
173 if len(data) == 0 {
174 log.Debugf("ignoring empty auth file: %s", filepath.Base(path))
175 return
176 }
177
178 sum := sha256.Sum256(data)
179 curHash := hex.EncodeToString(sum[:])
180 normalized := w.normalizeAuthPath(path)
181
182 // Parse new auth content for diff comparison
183 var newAuth coreauth.Auth
184 if errParse := json.Unmarshal(data, &newAuth); errParse != nil {
185 log.Errorf("failed to parse auth file %s: %v", filepath.Base(path), errParse)
186 return
187 }
188
189 cacheAuthContents := log.IsLevelEnabled(log.DebugLevel)
190 w.clientsMutex.Lock()
191 if w.config == nil {
192 log.Error("config is nil, cannot add or update client")
193 w.clientsMutex.Unlock()
194 return
195 }
196 cfg := w.config
197 authDir := w.authDir
198 parser := w.pluginAuthParser
199 if w.fileAuthsByPath == nil {
200 w.fileAuthsByPath = make(map[string]map[string]*coreauth.Auth)
201 }
202 if prev, ok := w.lastAuthHashes[normalized]; ok && prev == curHash {
203 log.Debugf("auth file unchanged (hash match), skipping reload: %s", filepath.Base(path))
204 w.clientsMutex.Unlock()
205 return
206 }
207
208 // Get old auth for diff comparison
209 var oldAuth *coreauth.Auth
210 if cacheAuthContents && w.lastAuthContents != nil {
211 if cached := w.lastAuthContents[normalized]; cached != nil {
212 oldAuth = cached.Clone()
213 }
214 }
215
216 // Update caches
217 if w.lastAuthHashes == nil {
218 w.lastAuthHashes = make(map[string]string)
219 }
220 w.lastAuthHashes[normalized] = curHash
221 if cacheAuthContents {
222 if w.lastAuthContents == nil {
223 w.lastAuthContents = make(map[string]*coreauth.Auth)
224 }

Callers 2

addOrUpdateClientMethod · 0.95
handleEventMethod · 0.95

Calls 12

normalizeAuthPathMethod · 0.95
persistAuthAsyncMethod · 0.95
dispatchAuthUpdatesMethod · 0.95
BuildAuthChangeDetailsFunction · 0.92
NewStableIDGeneratorFunction · 0.92
SynthesizeAuthFileFunction · 0.92
NotifyUsageRefreshFunction · 0.92
authSliceToMapFunction · 0.85
authIDSetFunction · 0.85
ErrorMethod · 0.45
CloneMethod · 0.45

Tested by

no test coverage detected