Merge pull request #1385 from safing/feature/process-xdgicons

Fix and improve profile import/export and add MVP linux icon support
This commit is contained in:
Daniel Hovie
2023-12-14 13:33:17 +01:00
committed by GitHub
14 changed files with 329 additions and 27 deletions

View File

@@ -1,6 +1,7 @@
package tags
import (
"context"
"fmt"
"strings"
@@ -8,6 +9,7 @@ import (
"github.com/safing/portbase/utils/osdetail"
"github.com/safing/portmaster/process"
"github.com/safing/portmaster/profile"
"github.com/safing/portmaster/profile/icons"
)
func init() {
@@ -81,11 +83,10 @@ func (h *SVCHostTagHandler) AddTags(p *process.Process) {
// Returns nil to skip.
func (h *SVCHostTagHandler) CreateProfile(p *process.Process) *profile.Profile {
if tag, ok := p.GetTag(svchostTagKey); ok {
return profile.New(&profile.Profile{
// Create new profile based on tag.
newProfile := profile.New(&profile.Profile{
Source: profile.SourceLocal,
Name: "Windows Service: " + osdetail.GenerateBinaryNameFromPath(tag.Value),
Icon: `C:\Windows\System32\@WLOGO_48x48.png`,
IconType: profile.IconTypeFile,
UsePresentationPath: false,
Fingerprints: []profile.Fingerprint{
profile.Fingerprint{
@@ -96,6 +97,14 @@ func (h *SVCHostTagHandler) CreateProfile(p *process.Process) *profile.Profile {
},
},
})
// Load default icon for windows service.
icon, err := icons.LoadAndSaveIcon(context.TODO(), `C:\Windows\System32\@WLOGO_48x48.png`)
if err == nil {
newProfile.Icons = []icons.Icon{*icon}
}
return newProfile
}
return nil

View File

@@ -10,6 +10,7 @@ import (
"github.com/safing/portbase/api"
"github.com/safing/portbase/formats/dsd"
"github.com/safing/portbase/utils"
"github.com/safing/portmaster/profile/icons"
)
func registerAPIEndpoints() error {
@@ -98,7 +99,7 @@ func handleGetProfileIcon(ar *api.Request) (data []byte, err error) {
ext := filepath.Ext(name)
// Get profile icon.
data, err = GetProfileIcon(name)
data, err = icons.GetProfileIcon(name)
if err != nil {
return nil, err
}
@@ -152,7 +153,7 @@ func handleUpdateProfileIcon(ar *api.Request) (any, error) {
}
// Update profile icon.
filename, err := UpdateProfileIcon(ar.InputData, ext)
filename, err := icons.UpdateProfileIcon(ar.InputData, ext)
if err != nil {
return nil, err
}

View File

@@ -1,6 +1,7 @@
package profile
import (
"context"
"errors"
"fmt"
"path"
@@ -146,7 +147,9 @@ func GetLocalProfile(id string, md MatchingData, createProfileCallback func() *P
// Trigger further metadata fetching from system if profile was created.
if created && profile.UsePresentationPath && !special {
module.StartWorker("get profile metadata", profile.updateMetadataFromSystem)
module.StartWorker("get profile metadata", func(ctx context.Context) error {
return profile.updateMetadataFromSystem(ctx, md)
})
}
// Prepare profile for first use.

View File

@@ -0,0 +1,10 @@
//go:build !linux
package icons
import "context"
// FindIcon returns nil, nil for unsupported platforms.
func FindIcon(ctx context.Context, binName string, homeDir string) (*Icon, error) {
return nil, nil
}

100
profile/icons/find_linux.go Normal file
View File

@@ -0,0 +1,100 @@
package icons
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
// FindIcon finds an icon for the given binary name.
// Providing the home directory of the user running the process of that binary can help find an icon.
func FindIcon(ctx context.Context, binName string, homeDir string) (*Icon, error) {
// Search for icon.
iconPath, err := search(binName, homeDir)
if iconPath == "" {
if err != nil {
return nil, fmt.Errorf("failed to find icon for %s: %w", binName, err)
}
return nil, nil
}
return LoadAndSaveIcon(ctx, iconPath)
}
func search(binName string, homeDir string) (iconPath string, err error) {
binName = strings.ToLower(binName)
// Search for icon path.
for _, iconLoc := range iconLocations {
basePath := iconLoc.GetPath(binName, homeDir)
if basePath == "" {
continue
}
switch iconLoc.Type {
case FlatDir:
iconPath, err = searchDirectory(basePath, binName)
case XDGIcons:
iconPath, err = searchXDGIconStructure(basePath, binName)
}
if iconPath != "" {
return
}
}
return
}
func searchXDGIconStructure(baseDirectory string, binName string) (iconPath string, err error) {
for _, xdgIconDir := range xdgIconPaths {
directory := filepath.Join(baseDirectory, xdgIconDir)
iconPath, err = searchDirectory(directory, binName)
if iconPath != "" {
return
}
}
return
}
func searchDirectory(directory string, binName string) (iconPath string, err error) {
entries, err := os.ReadDir(directory)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return "", nil
}
return "", fmt.Errorf("failed to read directory %s: %w", directory, err)
}
fmt.Println(directory)
var (
bestMatch string
bestMatchExcessChars int
)
for _, entry := range entries {
// Skip dirs.
if entry.IsDir() {
continue
}
iconName := strings.ToLower(entry.Name())
iconName = strings.TrimSuffix(iconName, filepath.Ext(iconName))
switch {
case len(iconName) < len(binName):
// Continue to next.
case iconName == binName:
// Exact match, return immediately.
return filepath.Join(directory, entry.Name()), nil
case strings.HasPrefix(iconName, binName):
excessChars := len(iconName) - len(binName)
if bestMatch == "" || excessChars < bestMatchExcessChars {
bestMatch = entry.Name()
bestMatchExcessChars = excessChars
}
}
}
return bestMatch, nil
}

View File

@@ -0,0 +1,32 @@
package icons
import (
"os"
"testing"
)
func TestFindIcon(t *testing.T) {
if testing.Short() {
t.Skip("test depends on linux desktop environment")
}
t.Parallel()
home := os.Getenv("HOME")
testFindIcon(t, "evolution", home)
testFindIcon(t, "nextcloud", home)
}
func testFindIcon(t *testing.T, binName string, homeDir string) {
t.Helper()
iconPath, err := search(binName, homeDir)
if err != nil {
t.Error(err)
return
}
if iconPath == "" {
t.Errorf("no icon found for %s", binName)
return
}
t.Logf("icon for %s found: %s", binName, iconPath)
}

View File

@@ -1,4 +1,4 @@
package profile
package icons
import (
"errors"
@@ -42,7 +42,8 @@ func (t IconType) sortOrder() int {
}
}
func sortAndCompactIcons(icons []Icon) []Icon {
// SortAndCompact sorts and compacts a list of icons.
func SortAndCompact(icons []Icon) []Icon {
// Sort.
slices.SortFunc[[]Icon, Icon](icons, func(a, b Icon) int {
aOrder := a.Type.sortOrder()

View File

@@ -1,6 +1,7 @@
package profile
package icons
import (
"context"
"crypto"
"encoding/hex"
"errors"
@@ -13,18 +14,21 @@ import (
"github.com/safing/portbase/api"
)
var profileIconStoragePath = ""
// ProfileIconStoragePath defines the location where profile icons are stored.
// Must be set before anything else from this package is called.
// Must not be changed once set.
var ProfileIconStoragePath = ""
// GetProfileIcon returns the profile icon with the given ID and extension.
func GetProfileIcon(name string) (data []byte, err error) {
// Check if enabled.
if profileIconStoragePath == "" {
if ProfileIconStoragePath == "" {
return nil, errors.New("api icon storage not configured")
}
// Build storage path.
iconPath := filepath.Clean(
filepath.Join(profileIconStoragePath, name),
filepath.Join(ProfileIconStoragePath, name),
)
iconPath, err = filepath.Abs(iconPath)
@@ -34,7 +38,7 @@ func GetProfileIcon(name string) (data []byte, err error) {
// Do a quick check if we are still within the right directory.
// This check is not entirely correct, but is sufficient for this use case.
if filepath.Dir(iconPath) != profileIconStoragePath {
if filepath.Dir(iconPath) != ProfileIconStoragePath {
return nil, api.ErrorWithStatus(errors.New("invalid icon"), http.StatusBadRequest)
}
@@ -72,7 +76,25 @@ func UpdateProfileIcon(data []byte, ext string) (filename string, err error) {
// Save to disk.
filename = sum + "." + ext
return filename, os.WriteFile(filepath.Join(profileIconStoragePath, filename), data, 0o0644) //nolint:gosec
return filename, os.WriteFile(filepath.Join(ProfileIconStoragePath, filename), data, 0o0644) //nolint:gosec
}
// LoadAndSaveIcon loads an icon from disk, updates it in the icon database
// and returns the icon object.
func LoadAndSaveIcon(ctx context.Context, iconPath string) (*Icon, error) {
// Load icon and save it.
data, err := os.ReadFile(iconPath)
if err != nil {
return nil, fmt.Errorf("failed to read icon %s: %w", iconPath, err)
}
filename, err := UpdateProfileIcon(data, filepath.Ext(iconPath))
if err != nil {
return nil, fmt.Errorf("failed to import icon %s: %w", iconPath, err)
}
return &Icon{
Type: IconTypeAPI,
Value: filename,
}, nil
}
// TODO: Clean up icons regularly.

View File

@@ -0,0 +1,68 @@
package icons
import (
"fmt"
)
// IconLocation describes an icon location.
type IconLocation struct {
Directory string
Type IconLocationType
PathArg PathArg
}
// IconLocationType describes an icon location type.
type IconLocationType uint8
// Icon Location Types.
const (
FlatDir IconLocationType = iota
XDGIcons
)
// PathArg describes an icon location path argument.
type PathArg uint8
// Path Args.
const (
NoPathArg PathArg = iota
Home
BinName
)
var (
iconLocations = []IconLocation{
{Directory: "/usr/share/pixmaps", Type: FlatDir},
{Directory: "/usr/share", Type: XDGIcons},
{Directory: "%s/.local/share", Type: XDGIcons, PathArg: Home},
{Directory: "%s/.local/share/flatpak/exports/share", Type: XDGIcons, PathArg: Home},
{Directory: "/usr/share/%s", Type: XDGIcons, PathArg: BinName},
}
xdgIconPaths = []string{
// UI currently uses 48x48, so 256x256 should suffice for the future, even at 2x. (12.2023)
"icons/hicolor/256x256/apps",
"icons/hicolor/192x192/apps",
"icons/hicolor/128x128/apps",
"icons/hicolor/96x96/apps",
"icons/hicolor/72x72/apps",
"icons/hicolor/64x64/apps",
"icons/hicolor/48x48/apps",
"icons/hicolor/512x512/apps",
}
)
// GetPath returns the path of an icon.
func (il IconLocation) GetPath(binName string, homeDir string) string {
switch il.PathArg {
case NoPathArg:
return il.Directory
case Home:
if homeDir != "" {
return fmt.Sprintf(il.Directory, homeDir)
}
case BinName:
return fmt.Sprintf(il.Directory, binName)
}
return ""
}

View File

@@ -7,6 +7,7 @@ import (
"time"
"github.com/safing/portbase/database/record"
"github.com/safing/portmaster/profile/icons"
)
// MergeProfiles merges multiple profiles into a new one.
@@ -52,12 +53,12 @@ func MergeProfiles(name string, primary *Profile, secondaries ...*Profile) (newP
}
// Collect all icons.
newProfile.Icons = make([]Icon, 0, len(secondaries)+1) // Guess the needed space.
newProfile.Icons = make([]icons.Icon, 0, len(secondaries)+1) // Guess the needed space.
newProfile.Icons = append(newProfile.Icons, primary.Icons...)
for _, sp := range secondaries {
newProfile.Icons = append(newProfile.Icons, sp.Icons...)
}
newProfile.Icons = sortAndCompactIcons(newProfile.Icons)
newProfile.Icons = icons.SortAndCompact(newProfile.Icons)
// Collect all fingerprints.
newProfile.Fingerprints = make([]Fingerprint, 0, len(primary.Fingerprints)+len(secondaries)) // Guess the needed space.

View File

@@ -11,6 +11,7 @@ import (
"github.com/safing/portbase/database/migration"
"github.com/safing/portbase/database/query"
"github.com/safing/portbase/log"
"github.com/safing/portmaster/profile/icons"
)
func registerMigrations() error {
@@ -27,7 +28,7 @@ func registerMigrations() error {
},
migration.Migration{
Description: "Migrate from random profile IDs to fingerprint-derived IDs",
Version: "v1.6.0",
Version: "v1.6.3", // Re-run after mixed results in v1.6.0
MigrateFunc: migrateToDerivedIDs,
},
)
@@ -102,7 +103,7 @@ func migrateIcons(ctx context.Context, _, to *version.Version, db *database.Inte
}
// Migrate to icon list.
profile.Icons = []Icon{{
profile.Icons = []icons.Icon{{
Type: profile.IconType,
Value: profile.Icon,
}}
@@ -161,6 +162,8 @@ func migrateToDerivedIDs(ctx context.Context, _, to *version.Version, db *databa
// Parse profile.
profile, err := EnsureProfile(r)
if err != nil {
failed++
lastErr = err
log.Tracer(ctx).Debugf("profiles: failed to parse profile %s for migration: %s", r.Key(), err)
continue
}

View File

@@ -11,6 +11,7 @@ import (
"github.com/safing/portbase/log"
"github.com/safing/portbase/modules"
_ "github.com/safing/portmaster/core/base"
"github.com/safing/portmaster/profile/icons"
"github.com/safing/portmaster/updates"
)
@@ -52,7 +53,7 @@ func prep() error {
if err := iconsDir.Ensure(); err != nil {
return fmt.Errorf("failed to create/check icons directory: %w", err)
}
profileIconStoragePath = iconsDir.Path
icons.ProfileIconStoragePath = iconsDir.Path
return nil
}

View File

@@ -18,6 +18,7 @@ import (
"github.com/safing/portbase/utils/osdetail"
"github.com/safing/portmaster/intel/filterlists"
"github.com/safing/portmaster/profile/endpoints"
"github.com/safing/portmaster/profile/icons"
)
// ProfileSource is the source of the profile.
@@ -68,9 +69,9 @@ type Profile struct { //nolint:maligned // not worth the effort
// See IconType for more information.
Icon string
// Deprecated: IconType describes the type of the Icon property.
IconType IconType
IconType icons.IconType
// Icons holds a list of icons to represent the application.
Icons []Icon
Icons []icons.Icon
// Deprecated: LinkedPath used to point to the executableis this
// profile was created for.
@@ -505,7 +506,7 @@ func (profile *Profile) updateMetadata(binaryPath string) (changed bool) {
// updateMetadataFromSystem updates the profile metadata with data from the
// operating system and saves it afterwards.
func (profile *Profile) updateMetadataFromSystem(ctx context.Context) error {
func (profile *Profile) updateMetadataFromSystem(ctx context.Context, md MatchingData) error {
var changed bool
// This function is only valid for local profiles.
@@ -531,6 +532,22 @@ func (profile *Profile) updateMetadataFromSystem(ctx context.Context) error {
return nil
}
// Get icon if path matches presentation path.
var newIcon *icons.Icon
if profile.PresentationPath == md.Path() {
// Get home from ENV.
var home string
if env := md.Env(); env != nil {
home = env["HOME"]
}
var err error
newIcon, err = icons.FindIcon(ctx, profile.PresentationPath, home)
if err != nil {
log.Warningf("profile: failed to find icon for %s: %s", profile.PresentationPath, err)
newIcon = nil
}
}
// Apply new data to profile.
func() {
// Lock profile for applying metadata.
@@ -542,6 +559,16 @@ func (profile *Profile) updateMetadataFromSystem(ctx context.Context) error {
profile.Name = newName
changed = true
}
// Apply new icon if found.
if newIcon != nil {
if len(profile.Icons) == 0 {
profile.Icons = []icons.Icon{*newIcon}
} else {
profile.Icons = append(profile.Icons, *newIcon)
profile.Icons = icons.SortAndCompact(profile.Icons)
}
}
}()
// If anything changed, save the profile.

View File

@@ -14,6 +14,7 @@ import (
"github.com/safing/portbase/config"
"github.com/safing/portbase/log"
"github.com/safing/portmaster/profile"
"github.com/safing/portmaster/profile/icons"
)
// ProfileExport holds an export of a profile.
@@ -254,6 +255,9 @@ func ExportProfile(scopedID string) (*ProfileExport, error) {
export.Created = &created
}
// Derive ID to ensure the ID is always correct.
export.ID = profile.DeriveProfileID(p.Fingerprints)
// Add first exportable icon to export.
if len(p.Icons) > 0 {
var err error
@@ -270,6 +274,14 @@ func ExportProfile(scopedID string) (*ProfileExport, error) {
}
}
// Remove presentation path if both Name and Icon are set.
if export.Name != "" && export.IconData != "" {
p.UsePresentationPath = false
}
if !p.UsePresentationPath {
p.PresentationPath = ""
}
return export, nil
}
@@ -284,11 +296,15 @@ func ImportProfile(r *ProfileImportRequest, requiredProfileSource profile.Profil
if r.Export.Source != "" && r.Export.Source != requiredProfileSource {
return nil, ErrMismatch
}
// Check ID.
// Convert fingerprints to internal representation.
fingerprints := convertFingerprintsToInternal(r.Export.Fingerprints)
if len(fingerprints) == 0 {
return nil, fmt.Errorf("%w: the export contains no fingerprints", ErrInvalidProfileData)
}
// Derive ID from fingerprints.
profileID := profile.DeriveProfileID(fingerprints)
if r.Export.ID != "" && r.Export.ID != profileID {
return nil, ErrMismatch
return nil, fmt.Errorf("%w: the export profile ID does not match the fingerprints, remove to ignore", ErrInvalidProfileData)
}
r.Export.ID = profileID
// Check Fingerprints.
@@ -385,18 +401,26 @@ func ImportProfile(r *ProfileImportRequest, requiredProfileSource profile.Profil
p.Created = in.Created.Unix()
}
// Fill in required values.
if p.Config == nil {
p.Config = make(map[string]any)
}
if p.Created == 0 {
p.Created = time.Now().Unix()
}
// Add icon to profile, if set.
if in.IconData != "" {
du, err := dataurl.DecodeString(in.IconData)
if err != nil {
return nil, fmt.Errorf("%w: icon data is invalid: %w", ErrImportFailed, err)
}
filename, err := profile.UpdateProfileIcon(du.Data, du.MediaType.Subtype)
filename, err := icons.UpdateProfileIcon(du.Data, du.MediaType.Subtype)
if err != nil {
return nil, fmt.Errorf("%w: icon is invalid: %w", ErrImportFailed, err)
}
p.Icons = []profile.Icon{{
Type: profile.IconTypeAPI,
p.Icons = []icons.Icon{{
Type: icons.IconTypeAPI,
Value: filename,
}}
}