-
-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1364 from safing/feature/unix-tag-improvements
Improve unix process matching tags
- Loading branch information
Showing
3 changed files
with
232 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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(flatpakHandler)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
const ( | ||
flatpakName = "Flatpak" | ||
flatpakIDTagKey = "flatpak-id" | ||
) | ||
|
||
// flatpakHandler handles flatpak processes on Unix systems. | ||
type flatpakHandler struct{} | ||
|
||
// Name returns the tag handler name. | ||
func (h *flatpakHandler) Name() string { | ||
return flatpakName | ||
} | ||
|
||
// TagDescriptions returns a list of all possible tags and their description | ||
// of this handler. | ||
func (h *flatpakHandler) TagDescriptions() []process.TagDescription { | ||
return []process.TagDescription{ | ||
{ | ||
ID: flatpakIDTagKey, | ||
Name: "Flatpak ID", | ||
Description: "ID of the flatpak.", | ||
}, | ||
} | ||
} | ||
|
||
// AddTags adds tags to the given process. | ||
func (h *flatpakHandler) AddTags(p *process.Process) { | ||
// Check if binary lives in the /app space. | ||
if !strings.HasPrefix(p.Path, "/app/") { | ||
return | ||
} | ||
|
||
// Get the Flatpak ID. | ||
flatpakID, ok := p.Env["FLATPAK_ID"] | ||
if !ok || flatpakID == "" { | ||
return | ||
} | ||
|
||
// Add matching path for regular profile matching. | ||
p.MatchingPath = p.Path | ||
|
||
// Add app image tag. | ||
p.Tags = append(p.Tags, profile.Tag{ | ||
Key: flatpakIDTagKey, | ||
Value: flatpakID, | ||
}) | ||
} | ||
|
||
// CreateProfile creates a profile based on the tags of the process. | ||
// Returns nil to skip. | ||
func (h *flatpakHandler) CreateProfile(p *process.Process) *profile.Profile { | ||
if tag, ok := p.GetTag(flatpakIDTagKey); ok { | ||
return profile.New(&profile.Profile{ | ||
Source: profile.SourceLocal, | ||
Name: osdetail.GenerateBinaryNameFromPath(p.Path), | ||
PresentationPath: p.Path, | ||
UsePresentationPath: true, | ||
Fingerprints: []profile.Fingerprint{ | ||
{ | ||
Type: profile.FingerprintTypeTagID, | ||
Key: tag.Key, | ||
Operation: profile.FingerprintOperationEqualsID, | ||
Value: tag.Value, // Value of flatpakIDTagKey. | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters