mirror of
https://github.com/safing/portmaster
synced 2025-04-21 03:19:10 +00:00
* Move portbase into monorepo * Add new simple module mgr * [WIP] Switch to new simple module mgr * Add StateMgr and more worker variants * [WIP] Switch more modules * [WIP] Switch more modules * [WIP] swtich more modules * [WIP] switch all SPN modules * [WIP] switch all service modules * [WIP] Convert all workers to the new module system * [WIP] add new task system to module manager * [WIP] Add second take for scheduling workers * [WIP] Add FIXME for bugs in new scheduler * [WIP] Add minor improvements to scheduler * [WIP] Add new worker scheduler * [WIP] Fix more bug related to new module system * [WIP] Fix start handing of the new module system * [WIP] Improve startup process * [WIP] Fix minor issues * [WIP] Fix missing subsystem in settings * [WIP] Initialize managers in constructor * [WIP] Move module event initialization to constrictors * [WIP] Fix setting for enabling and disabling the SPN module * [WIP] Move API registeration into module construction * [WIP] Update states mgr for all modules * [WIP] Add CmdLine operation support * Add state helper methods to module group and instance * Add notification and module status handling to status package * Fix starting issues * Remove pilot widget and update security lock to new status data * Remove debug logs * Improve http server shutdown * Add workaround for cleanly shutting down firewall+netquery * Improve logging * Add syncing states with notifications for new module system * Improve starting, stopping, shutdown; resolve FIXMEs/TODOs * [WIP] Fix most unit tests * Review new module system and fix minor issues * Push shutdown and restart events again via API * Set sleep mode via interface * Update example/template module * [WIP] Fix spn/cabin unit test * Remove deprecated UI elements * Make log output more similar for the logging transition phase * Switch spn hub and observer cmds to new module system * Fix log sources * Make worker mgr less error prone * Fix tests and minor issues * Fix observation hub * Improve shutdown and restart handling * Split up big connection.go source file * Move varint and dsd packages to structures repo * Improve expansion test * Fix linter warnings * Fix interception module on windows * Fix linter errors --------- Co-authored-by: Vladimir Stoilov <vladimir@safing.io>
104 lines
3.4 KiB
Go
104 lines
3.4 KiB
Go
package profile
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/safing/portmaster/base/database/record"
|
|
"github.com/safing/portmaster/service/profile/binmeta"
|
|
)
|
|
|
|
// MergeProfiles merges multiple profiles into a new one.
|
|
// The new profile is saved and returned.
|
|
// Only the icon and fingerprints are inherited from other profiles.
|
|
// All other information is taken only from the primary profile.
|
|
func MergeProfiles(name string, primary *Profile, secondaries ...*Profile) (newProfile *Profile, err error) {
|
|
if primary == nil || len(secondaries) == 0 {
|
|
return nil, errors.New("must supply both a primary and at least one secondary profile for merging")
|
|
}
|
|
|
|
// Fill info from primary profile.
|
|
nowUnix := time.Now().Unix()
|
|
newProfile = &Profile{
|
|
Base: record.Base{},
|
|
RWMutex: sync.RWMutex{},
|
|
ID: "", // Omit ID to derive it from the new fingerprints.
|
|
Source: primary.Source,
|
|
Name: name,
|
|
Description: primary.Description,
|
|
Homepage: primary.Homepage,
|
|
UsePresentationPath: false, // Disable presentation path.
|
|
Config: primary.Config,
|
|
Created: nowUnix,
|
|
}
|
|
|
|
// Fall back to name of primary profile, if none is set.
|
|
if newProfile.Name == "" {
|
|
newProfile.Name = primary.Name
|
|
}
|
|
|
|
// If any profile was edited, set LastEdited to now.
|
|
if primary.LastEdited > 0 {
|
|
newProfile.LastEdited = nowUnix
|
|
} else {
|
|
for _, sp := range secondaries {
|
|
if sp.LastEdited > 0 {
|
|
newProfile.LastEdited = nowUnix
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// Collect all icons.
|
|
newProfile.Icons = make([]binmeta.Icon, 0, len(secondaries)+1) // Guess the needed space.
|
|
newProfile.Icons = append(newProfile.Icons, primary.Icons...)
|
|
for _, sp := range secondaries {
|
|
newProfile.Icons = append(newProfile.Icons, sp.Icons...)
|
|
}
|
|
newProfile.Icons = binmeta.SortAndCompactIcons(newProfile.Icons)
|
|
|
|
// Collect all fingerprints.
|
|
newProfile.Fingerprints = make([]Fingerprint, 0, len(primary.Fingerprints)+len(secondaries)) // Guess the needed space.
|
|
newProfile.Fingerprints = addFingerprints(newProfile.Fingerprints, primary.Fingerprints, primary.ScopedID())
|
|
for _, sp := range secondaries {
|
|
newProfile.Fingerprints = addFingerprints(newProfile.Fingerprints, sp.Fingerprints, sp.ScopedID())
|
|
}
|
|
newProfile.Fingerprints = sortAndCompactFingerprints(newProfile.Fingerprints)
|
|
|
|
// Save new profile.
|
|
newProfile = New(newProfile)
|
|
if err := newProfile.Save(); err != nil {
|
|
return nil, fmt.Errorf("failed to save merged profile: %w", err)
|
|
}
|
|
|
|
// Delete all previous profiles.
|
|
if err := primary.delete(); err != nil {
|
|
return nil, fmt.Errorf("failed to delete primary profile %s: %w", primary.ScopedID(), err)
|
|
}
|
|
module.EventMigrated.Submit([]string{primary.ScopedID(), newProfile.ScopedID()})
|
|
for _, sp := range secondaries {
|
|
if err := sp.delete(); err != nil {
|
|
return nil, fmt.Errorf("failed to delete secondary profile %s: %w", sp.ScopedID(), err)
|
|
}
|
|
module.EventMigrated.Submit([]string{sp.ScopedID(), newProfile.ScopedID()})
|
|
}
|
|
|
|
return newProfile, nil
|
|
}
|
|
|
|
func addFingerprints(existing, add []Fingerprint, from string) []Fingerprint {
|
|
// Copy all fingerprints and add the they are from.
|
|
for _, addFP := range add {
|
|
existing = append(existing, Fingerprint{
|
|
Type: addFP.Type,
|
|
Key: addFP.Key,
|
|
Operation: addFP.Operation,
|
|
Value: addFP.Value,
|
|
MergedFrom: from,
|
|
})
|
|
}
|
|
|
|
return existing
|
|
}
|