Work on pm restructure

This commit is contained in:
Daniel
2018-12-03 20:02:03 +01:00
parent 62b1c03edc
commit 8fb21fd900
23 changed files with 487 additions and 326 deletions

View File

@@ -3,6 +3,7 @@ package profile
import (
"fmt"
"sync"
"time"
uuid "github.com/satori/go.uuid"
@@ -10,6 +11,10 @@ import (
"github.com/Safing/portmaster/status"
)
var (
lastUsedUpdateThreshold = 1 * time.Hour
)
// Profile is used to predefine a security profile for applications.
type Profile struct {
record.Base
@@ -24,7 +29,7 @@ type Profile struct {
Icon string
// Fingerprints
Fingerprints []string
Fingerprints []*Fingerprint
// The mininum security level to apply to connections made with this profile
SecurityLevel uint8
@@ -47,6 +52,10 @@ func New() *Profile {
return &Profile{}
}
func makeProfileKey(namespace, ID string) string {
return fmt.Sprintf("core:profiles/%s/%s", namespace, ID)
}
// Save saves the profile to the database
func (profile *Profile) Save(namespace string) error {
if profile.ID == "" {
@@ -61,12 +70,21 @@ func (profile *Profile) Save(namespace string) error {
if namespace == "" {
return fmt.Errorf("no key or namespace defined for profile %s", profile.String())
}
profile.SetKey(fmt.Sprintf("config:profiles/%s/%s", namespace, profile.ID))
profile.SetKey(makeProfileKey(namespace, profile.ID))
}
return profileDB.Put(profile)
}
// MarkUsed marks the profile as used, eventually.
func (profile *Profile) MarkUsed() (updated bool) {
if time.Now().Add(-lastUsedUpdateThreshold).Unix() > profile.ApproxLastUsed {
profile.ApproxLastUsed = time.Now().Unix()
return true
}
return false
}
// String returns a string representation of the Profile.
func (profile *Profile) String() string {
return profile.Name
@@ -79,10 +97,38 @@ func (profile *Profile) DetailedString() string {
// GetUserProfile loads a profile from the database.
func GetUserProfile(ID string) (*Profile, error) {
return nil, nil
return getProfile(userNamespace, ID)
}
// GetStampProfile loads a profile from the database.
func GetStampProfile(ID string) (*Profile, error) {
return nil, nil
return getProfile(stampNamespace, ID)
}
func getProfile(namespace, ID string) (*Profile, error) {
r, err := profileDB.Get(makeProfileKey(namespace, ID))
if err != nil {
return nil, err
}
return ensureProfile(r)
}
func ensureProfile(r record.Record) (*Profile, error) {
// unwrap
if r.IsWrapped() {
// only allocate a new struct, if we need it
new := &Profile{}
err := record.Unwrap(r, new)
if err != nil {
return nil, err
}
return new, nil
}
// or adjust type
new, ok := r.(*Profile)
if !ok {
return nil, fmt.Errorf("record not of type *Example, but %T", r)
}
return new, nil
}