Move matching package to feature branch

This commit is contained in:
Daniel 2019-11-07 16:48:04 +01:00
parent 5b40ffbedf
commit 43c6493ad5
5 changed files with 144 additions and 0 deletions

View file

@ -0,0 +1,17 @@
package matching
import (
"github.com/safing/portbase/database"
)
// core:profiles/user/12345-1234-125-1234-1235
// core:profiles/special/default
// /global
// core:profiles/stamp/12334-1235-1234-5123-1234
// core:profiles/identifier/base64
var (
profileDB = database.NewInterface(&database.Options{
Local: true, // we want to access crownjewel records (indexes are)
})
)

View file

@ -0,0 +1,23 @@
package matching
import (
"strings"
"github.com/safing/portmaster/process"
"github.com/safing/portmaster/profile"
)
// CheckFingerprints checks what fingerprints match and returns the total score.
func CheckFingerprints(proc *process.Process, prof *profile.Profile) (score int, err error) {
// FIXME: kinda a dummy for now
for _, fp := range prof.Fingerprints {
if strings.HasPrefix(fp, "fullpath:") {
if fp[9:] == proc.Path {
return 3, nil
}
}
}
return 0, nil
}

View file

@ -0,0 +1,18 @@
package matching
import (
"fmt"
"strings"
"github.com/safing/portmaster/process"
"github.com/safing/portmaster/profile"
)
// GetIdentificationPath returns the identifier for the given process (linux edition).
func GetIdentificationPath(p *process.Process) string {
splittedPath := strings.Split(p.Path, "/")
if len(splittedPath) > 3 {
return fmt.Sprintf("%s%s", profile.IdentifierPrefix, strings.Join(splittedPath[len(splittedPath)-3:len(splittedPath)], "/"))
}
return fmt.Sprintf("%s%s", profile.IdentifierPrefix, p.Path)
}

View file

@ -0,0 +1,25 @@
package matching
import (
"testing"
"github.com/safing/portmaster/process"
)
func TestGetIdentifierLinux(t *testing.T) {
p := &process.Process{
Path: "/usr/lib/firefox/firefox",
}
if GetIdentificationPath(p) != "lin:lib/firefox/firefox" {
t.Fatal("mismatch!")
}
p = &process.Process{
Path: "/opt/start",
}
if GetIdentificationPath(p) != "lin:/opt/start" {
t.Fatal("mismatch!")
}
}

View file

@ -0,0 +1,61 @@
package matching
import (
"fmt"
"strings"
"github.com/safing/portbase/log"
"github.com/safing/portmaster/process"
"github.com/safing/portmaster/profile"
"github.com/safing/portmaster/profile/index"
)
// GetProfileSet finds a local profile.
func GetProfileSet(proc *process.Process) (set *profile.ProfileSet, err error) {
identPath := GetIdentificationPath(proc)
pi, err := index.GetIndex(identPath)
var bestScore int
var bestProfile *profile.Profile
for _, id := range pi.UserProfiles {
prof, err := profile.GetUserProfile(id)
if err != nil {
log.Errorf("profile/matcher: failed to load profile: %s", err)
continue
}
score, err := CheckFingerprints(proc, prof)
if score > bestScore {
bestScore = score
bestProfile = prof
}
}
if bestProfile == nil {
bestProfile = ProfileFromProcess(proc)
}
// FIXME: fetch stamp profile
set = profile.NewSet(bestProfile, nil)
return set, nil
}
// ProfileFromProcess creates an initial profile based on the given process.
func ProfileFromProcess(proc *process.Process) *profile.Profile {
new := profile.New()
splittedPath := strings.Split(proc.Path, "/")
new.Name = strings.ToTitle(splittedPath[len(splittedPath)-1])
new.Identifiers = append(new.Identifiers, GetIdentificationPath(proc))
new.Fingerprints = append(new.Fingerprints, fmt.Sprintf("fullpath:%s", proc.Path))
err := new.Save(profile.UserNamespace)
if err != nil {
log.Errorf("profile/matcher: could not save new profile: %s", new.Name)
}
return new
}