Load Windows Svc icon with new icon system

This commit is contained in:
Daniel
2023-12-13 16:25:48 +01:00
parent cf3d4e030f
commit 9c969f9465
3 changed files with 32 additions and 16 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

@@ -21,19 +21,7 @@ func FindIcon(ctx context.Context, binName string, homeDir string) (*Icon, error
return nil, nil
}
// 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
return LoadAndSaveIcon(ctx, iconPath)
}
func search(binName string, homeDir string) (iconPath string, err error) {

View File

@@ -1,6 +1,7 @@
package icons
import (
"context"
"crypto"
"encoding/hex"
"errors"
@@ -78,4 +79,22 @@ func UpdateProfileIcon(data []byte, ext string) (filename string, err error) {
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.