mirror of
https://github.com/safing/portmaster
synced 2025-04-23 12:29:10 +00:00
Move matching package to feature branch
This commit is contained in:
parent
5b40ffbedf
commit
43c6493ad5
5 changed files with 144 additions and 0 deletions
17
profile/matching/database.go
Normal file
17
profile/matching/database.go
Normal 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)
|
||||
})
|
||||
)
|
23
profile/matching/fingerprints.go
Normal file
23
profile/matching/fingerprints.go
Normal 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
|
||||
}
|
18
profile/matching/identpath_linux.go
Normal file
18
profile/matching/identpath_linux.go
Normal 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)
|
||||
}
|
25
profile/matching/identpath_linux_test.go
Normal file
25
profile/matching/identpath_linux_test.go
Normal 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!")
|
||||
}
|
||||
}
|
61
profile/matching/matcher.go
Normal file
61
profile/matching/matcher.go
Normal 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
|
||||
}
|
Loading…
Add table
Reference in a new issue