* Move portbase into monorepo * Add new simple module mgr * [WIP] Switch to new simple module mgr * Add StateMgr and more worker variants * [WIP] Switch more modules * [WIP] Switch more modules * [WIP] swtich more modules * [WIP] switch all SPN modules * [WIP] switch all service modules * [WIP] Convert all workers to the new module system * [WIP] add new task system to module manager * [WIP] Add second take for scheduling workers * [WIP] Add FIXME for bugs in new scheduler * [WIP] Add minor improvements to scheduler * [WIP] Add new worker scheduler * [WIP] Fix more bug related to new module system * [WIP] Fix start handing of the new module system * [WIP] Improve startup process * [WIP] Fix minor issues * [WIP] Fix missing subsystem in settings * [WIP] Initialize managers in constructor * [WIP] Move module event initialization to constrictors * [WIP] Fix setting for enabling and disabling the SPN module * [WIP] Move API registeration into module construction * [WIP] Update states mgr for all modules * [WIP] Add CmdLine operation support * Add state helper methods to module group and instance * Add notification and module status handling to status package * Fix starting issues * Remove pilot widget and update security lock to new status data * Remove debug logs * Improve http server shutdown * Add workaround for cleanly shutting down firewall+netquery * Improve logging * Add syncing states with notifications for new module system * Improve starting, stopping, shutdown; resolve FIXMEs/TODOs * [WIP] Fix most unit tests * Review new module system and fix minor issues * Push shutdown and restart events again via API * Set sleep mode via interface * Update example/template module * [WIP] Fix spn/cabin unit test * Remove deprecated UI elements * Make log output more similar for the logging transition phase * Switch spn hub and observer cmds to new module system * Fix log sources * Make worker mgr less error prone * Fix tests and minor issues * Fix observation hub * Improve shutdown and restart handling * Split up big connection.go source file * Move varint and dsd packages to structures repo * Improve expansion test * Fix linter warnings * Fix interception module on windows * Fix linter errors --------- Co-authored-by: Vladimir Stoilov <vladimir@safing.io>
115 lines
2.8 KiB
Go
115 lines
2.8 KiB
Go
package binmeta
|
|
|
|
import (
|
|
"context"
|
|
"crypto"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/safing/portmaster/base/api"
|
|
)
|
|
|
|
// 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 = ""
|
|
|
|
// ErrIconIgnored is returned when the icon should be ignored.
|
|
var ErrIconIgnored = errors.New("icon is ignored")
|
|
|
|
// GetProfileIcon returns the profile icon with the given ID and extension.
|
|
func GetProfileIcon(name string) (data []byte, err error) {
|
|
// Check if enabled.
|
|
if ProfileIconStoragePath == "" {
|
|
return nil, errors.New("api icon storage not configured")
|
|
}
|
|
|
|
// Check if icon should be ignored.
|
|
if IgnoreIcon(name) {
|
|
return nil, ErrIconIgnored
|
|
}
|
|
|
|
// Build storage path.
|
|
iconPath := filepath.Clean(
|
|
filepath.Join(ProfileIconStoragePath, name),
|
|
)
|
|
|
|
iconPath, err = filepath.Abs(iconPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to check icon path: %w", err)
|
|
}
|
|
|
|
// 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 {
|
|
return nil, api.ErrorWithStatus(errors.New("invalid icon"), http.StatusBadRequest)
|
|
}
|
|
|
|
return os.ReadFile(iconPath)
|
|
}
|
|
|
|
// UpdateProfileIcon creates or updates the given icon.
|
|
func UpdateProfileIcon(data []byte, ext string) (filename string, err error) {
|
|
// Check icon size.
|
|
if len(data) > 1_000_000 {
|
|
return "", errors.New("icon too big")
|
|
}
|
|
|
|
// Calculate sha1 sum of icon.
|
|
h := crypto.SHA1.New()
|
|
if _, err := h.Write(data); err != nil {
|
|
return "", err
|
|
}
|
|
sum := hex.EncodeToString(h.Sum(nil))
|
|
|
|
// Check if icon should be ignored.
|
|
if IgnoreIcon(sum) {
|
|
return "", ErrIconIgnored
|
|
}
|
|
|
|
// Check ext.
|
|
ext = strings.ToLower(ext)
|
|
switch ext {
|
|
case "gif":
|
|
case "jpeg":
|
|
ext = "jpg"
|
|
case "jpg":
|
|
case "png":
|
|
case "svg":
|
|
case "tiff":
|
|
case "webp":
|
|
default:
|
|
return "", errors.New("unsupported icon format")
|
|
}
|
|
|
|
// Save to disk.
|
|
filename = sum + "." + ext
|
|
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,
|
|
Source: IconSourceCore,
|
|
}, nil
|
|
}
|
|
|
|
// TODO: Clean up icons regularly.
|