MCPcopy
hub / github.com/safing/portmaster / addEndpointEntry

Method addEndpointEntry

service/profile/profile.go:372–426  ·  view source on GitHub ↗
(cfgKey, newEntry string)

Source from the content-addressed store, hash-verified

370}
371
372func (profile *Profile) addEndpointEntry(cfgKey, newEntry string) {
373 changed := false
374
375 // When finished, save the profile.
376 defer func() {
377 if !changed {
378 return
379 }
380
381 err := profile.Save()
382 if err != nil {
383 log.Warningf("profile: failed to save profile %s after add an endpoint rule: %s", profile.ScopedID(), err)
384 }
385 }()
386
387 // Lock the profile for editing.
388 profile.Lock()
389 defer profile.Unlock()
390
391 // Get the endpoint list configuration value and add the new entry.
392 endpointList, ok := profile.configPerspective.GetAsStringArray(cfgKey)
393 if ok {
394 // A list already exists, check for duplicates within the same prefix.
395 newEntryPrefix := strings.Split(newEntry, " ")[0] + " "
396 for _, entry := range endpointList {
397 if !strings.HasPrefix(entry, newEntryPrefix) {
398 // We found an entry with a different prefix than the new entry.
399 // Beyond this entry we cannot possibly know if identical entries will
400 // match, so we will have to add the new entry no matter what the rest
401 // of the list has.
402 break
403 }
404
405 if entry == newEntry {
406 // An identical entry is already in the list, abort.
407 log.Debugf("profile: ignoring new endpoint rule for %s, as identical is already present: %s", profile, newEntry)
408 return
409 }
410 }
411 endpointList = append([]string{newEntry}, endpointList...)
412 } else {
413 endpointList = []string{newEntry}
414 }
415
416 // Save new value back to profile.
417 config.PutValueIntoHierarchicalConfig(profile.Config, cfgKey, endpointList)
418 changed = true
419
420 // Reload the profile manually in order to parse the newly added entry.
421 profile.dataParsed = false
422 err := profile.parseConfig()
423 if err != nil {
424 log.Errorf("profile: failed to parse %s config after adding endpoint: %s", profile, err)
425 }
426}
427
428// LayeredProfile returns the layered profile associated with this profile.
429func (profile *Profile) LayeredProfile() *LayeredProfile {

Callers 2

AddEndpointMethod · 0.95
AddServiceEndpointMethod · 0.95

Calls 10

SaveMethod · 0.95
ScopedIDMethod · 0.95
parseConfigMethod · 0.95
WarningfFunction · 0.92
DebugfFunction · 0.92
ErrorfFunction · 0.92
LockMethod · 0.65
UnlockMethod · 0.65
GetAsStringArrayMethod · 0.45

Tested by

no test coverage detected