mirror of
https://github.com/safing/portmaster
synced 2025-09-01 18:19:12 +00:00
Add support for snap
This commit is contained in:
parent
a1ffbe7317
commit
90ea59204f
3 changed files with 139 additions and 7 deletions
135
process/tags/snap_unix.go
Normal file
135
process/tags/snap_unix.go
Normal file
|
@ -0,0 +1,135 @@
|
|||
package tags
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/safing/portbase/utils/osdetail"
|
||||
"github.com/safing/portmaster/process"
|
||||
"github.com/safing/portmaster/profile"
|
||||
)
|
||||
|
||||
func init() {
|
||||
err := process.RegisterTagHandler(new(SnapHandler))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
snapName = "Snap"
|
||||
snapNameKey = "snap-name"
|
||||
snapVersionKey = "snap-version"
|
||||
|
||||
snapBaseDir = "/snap/"
|
||||
)
|
||||
|
||||
// SnapHandler handles Snap processes on Unix systems.
|
||||
type SnapHandler struct{}
|
||||
|
||||
// Name returns the tag handler name.
|
||||
func (h *SnapHandler) Name() string {
|
||||
return snapName
|
||||
}
|
||||
|
||||
// TagDescriptions returns a list of all possible tags and their description
|
||||
// of this handler.
|
||||
func (h *SnapHandler) TagDescriptions() []process.TagDescription {
|
||||
return []process.TagDescription{
|
||||
{
|
||||
ID: snapNameKey,
|
||||
Name: "Snap Name",
|
||||
Description: "Name of snap package.",
|
||||
},
|
||||
{
|
||||
ID: snapVersionKey,
|
||||
Name: "Snap Version",
|
||||
Description: "Version and revision of the snap package.",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// AddTags adds tags to the given process.
|
||||
func (h *SnapHandler) AddTags(p *process.Process) {
|
||||
// Check for snap env and verify location.
|
||||
snapPkgBaseDir, ok := p.Env["SNAP"]
|
||||
if ok && strings.HasPrefix(p.Path, snapPkgBaseDir) {
|
||||
// Try adding tags from env.
|
||||
added := h.addTagsFromEnv(p)
|
||||
if added {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt adding tags from path instead, if env did not work out.
|
||||
h.addTagsFromPath(p)
|
||||
}
|
||||
|
||||
func (h *SnapHandler) addTagsFromEnv(p *process.Process) (added bool) {
|
||||
// Get and verify snap metadata.
|
||||
snapPkgName, ok := p.Env["SNAP_NAME"]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
snapPkgVersion, ok := p.Env["SNAP_VERSION"]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
// Add snap tags.
|
||||
p.Tags = append(p.Tags, profile.Tag{
|
||||
Key: snapNameKey,
|
||||
Value: snapPkgName,
|
||||
})
|
||||
p.Tags = append(p.Tags, profile.Tag{
|
||||
Key: snapVersionKey,
|
||||
Value: snapPkgVersion,
|
||||
})
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (h *SnapHandler) addTagsFromPath(p *process.Process) {
|
||||
// Check if the binary is within the snap base dir.
|
||||
if !strings.HasPrefix(p.Path, snapBaseDir) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get snap package name from path.
|
||||
splitted := strings.SplitN(strings.TrimPrefix(p.Path, snapBaseDir), "/", 2)
|
||||
if len(splitted) < 2 || splitted[0] == "" {
|
||||
return
|
||||
}
|
||||
|
||||
// Add snap tags.
|
||||
p.Tags = append(p.Tags, profile.Tag{
|
||||
Key: snapNameKey,
|
||||
Value: splitted[0],
|
||||
})
|
||||
}
|
||||
|
||||
// CreateProfile creates a profile based on the tags of the process.
|
||||
// Returns nil to skip.
|
||||
func (h *SnapHandler) CreateProfile(p *process.Process) *profile.Profile {
|
||||
if tag, ok := p.GetTag(snapNameKey); ok {
|
||||
// Check if we have the snap version.
|
||||
// Only use presentation path if we have it.
|
||||
_, hasVersion := p.GetTag(snapVersionKey)
|
||||
|
||||
return profile.New(&profile.Profile{
|
||||
Source: profile.SourceLocal,
|
||||
Name: osdetail.GenerateBinaryNameFromPath(tag.Value),
|
||||
PresentationPath: p.Path,
|
||||
UsePresentationPath: hasVersion,
|
||||
Fingerprints: []profile.Fingerprint{
|
||||
{
|
||||
Type: profile.FingerprintTypeTagID,
|
||||
Key: tag.Key,
|
||||
Operation: profile.FingerprintOperationEqualsID,
|
||||
Value: tag.Value, // Value of snapNameKey.
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -89,7 +89,7 @@ func (h *SVCHostTagHandler) CreateProfile(p *process.Process) *profile.Profile {
|
|||
Type: profile.FingerprintTypeTagID,
|
||||
Key: tag.Key,
|
||||
Operation: profile.FingerprintOperationEqualsID,
|
||||
Value: tag.Value,
|
||||
Value: tag.Value, // Value of svchostTagKey.
|
||||
},
|
||||
},
|
||||
})
|
||||
|
|
|
@ -4,12 +4,9 @@ import (
|
|||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/safing/portbase/utils/osdetail"
|
||||
|
||||
"github.com/safing/portbase/log"
|
||||
|
||||
"github.com/safing/portbase/utils"
|
||||
|
||||
"github.com/safing/portbase/utils/osdetail"
|
||||
"github.com/safing/portmaster/process"
|
||||
"github.com/safing/portmaster/profile"
|
||||
)
|
||||
|
@ -35,7 +32,7 @@ const (
|
|||
|
||||
var winStorePaths = []string{`C:\Program Files\WindowsApps\`}
|
||||
|
||||
// WinStoreHandler handles AppImage processes on Unix systems.
|
||||
// WinStoreHandler handles Windows Store Apps.
|
||||
type WinStoreHandler struct{}
|
||||
|
||||
// Name returns the tag handler name.
|
||||
|
@ -112,7 +109,7 @@ func (h *WinStoreHandler) CreateProfile(p *process.Process) *profile.Profile {
|
|||
Type: profile.FingerprintTypeTagID,
|
||||
Key: tag.Key,
|
||||
Operation: profile.FingerprintOperationEqualsID,
|
||||
Value: tag.Value, // Value of appImagePathTagKey.
|
||||
Value: tag.Value, // Value of winStoreAppNameTagKey.
|
||||
},
|
||||
},
|
||||
})
|
||||
|
|
Loading…
Add table
Reference in a new issue