From 776b4860ef4d81defb0206b952b85ac92d7fc0bc Mon Sep 17 00:00:00 2001 From: Alexandr Stelnykovych Date: Wed, 26 Mar 2025 17:35:27 +0200 Subject: [PATCH 1/3] [updates] fix: use correct binaries index name --- cmds/updatemgr/scan.go | 3 ++- service/config.go | 8 ++++---- service/configure/updates.go | 3 +++ service/updates/module.go | 2 ++ 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/cmds/updatemgr/scan.go b/cmds/updatemgr/scan.go index 9ef29f15..e5458ee6 100644 --- a/cmds/updatemgr/scan.go +++ b/cmds/updatemgr/scan.go @@ -4,13 +4,14 @@ import ( "encoding/json" "fmt" + "github.com/safing/portmaster/service/configure" "github.com/safing/portmaster/service/updates" "github.com/spf13/cobra" ) var ( scanConfig = updates.IndexScanConfig{ - Name: "Portmaster Binaries", + Name: configure.DefaultBinaryIndexName, PrimaryArtifact: "linux_amd64/portmaster-core", BaseURL: "https://updates.safing.io/", IgnoreFiles: []string{ diff --git a/service/config.go b/service/config.go index 39ad2d03..bef4110e 100644 --- a/service/config.go +++ b/service/config.go @@ -115,7 +115,7 @@ func MakeUpdateConfigs(svcCfg *ServiceConfig) (binaryUpdateConfig, intelUpdateCo switch runtime.GOOS { case "windows": binaryUpdateConfig = &updates.Config{ - Name: "binaries", + Name: configure.DefaultBinaryIndexName, Directory: svcCfg.BinDir, DownloadDirectory: filepath.Join(svcCfg.DataDir, "download_binaries"), PurgeDirectory: filepath.Join(svcCfg.BinDir, "upgrade_obsolete_binaries"), @@ -130,7 +130,7 @@ func MakeUpdateConfigs(svcCfg *ServiceConfig) (binaryUpdateConfig, intelUpdateCo Notify: true, } intelUpdateConfig = &updates.Config{ - Name: "intel", + Name: configure.DefaultIntelIndexName, Directory: filepath.Join(svcCfg.DataDir, "intel"), DownloadDirectory: filepath.Join(svcCfg.DataDir, "download_intel"), PurgeDirectory: filepath.Join(svcCfg.DataDir, "upgrade_obsolete_intel"), @@ -146,7 +146,7 @@ func MakeUpdateConfigs(svcCfg *ServiceConfig) (binaryUpdateConfig, intelUpdateCo case "linux": binaryUpdateConfig = &updates.Config{ - Name: "binaries", + Name: configure.DefaultBinaryIndexName, Directory: svcCfg.BinDir, DownloadDirectory: filepath.Join(svcCfg.DataDir, "download_binaries"), PurgeDirectory: filepath.Join(svcCfg.DataDir, "upgrade_obsolete_binaries"), @@ -161,7 +161,7 @@ func MakeUpdateConfigs(svcCfg *ServiceConfig) (binaryUpdateConfig, intelUpdateCo Notify: true, } intelUpdateConfig = &updates.Config{ - Name: "intel", + Name: configure.DefaultIntelIndexName, Directory: filepath.Join(svcCfg.DataDir, "intel"), DownloadDirectory: filepath.Join(svcCfg.DataDir, "download_intel"), PurgeDirectory: filepath.Join(svcCfg.DataDir, "upgrade_obsolete_intel"), diff --git a/service/configure/updates.go b/service/configure/updates.go index 3fec4afc..0625ff26 100644 --- a/service/configure/updates.go +++ b/service/configure/updates.go @@ -5,6 +5,9 @@ import ( ) var ( + DefaultBinaryIndexName = "Portmaster Binaries" + DefaultIntelIndexName = "intel" + DefaultStableBinaryIndexURLs = []string{ "https://updates.safing.io/stable.v3.json", } diff --git a/service/updates/module.go b/service/updates/module.go index b57830b2..8a441ac9 100644 --- a/service/updates/module.go +++ b/service/updates/module.go @@ -18,6 +18,7 @@ import ( "github.com/safing/portmaster/base/log" "github.com/safing/portmaster/base/notifications" "github.com/safing/portmaster/base/utils" + "github.com/safing/portmaster/service/configure" "github.com/safing/portmaster/service/mgr" ) @@ -201,6 +202,7 @@ func New(instance instance, name string, cfg Config) (*Updater, error) { module.corruptedInstallation = fmt.Errorf("invalid index: %w", err) } index, err = GenerateIndexFromDir(cfg.Directory, IndexScanConfig{ + Name: configure.DefaultBinaryIndexName, Version: info.VersionNumber(), }) if err == nil && index.init(currentPlatform) == nil { From 2a85a4bfd00faece71d741ddf17d84dd3f7bd7d5 Mon Sep 17 00:00:00 2001 From: Alexandr Stelnykovych Date: Fri, 28 Mar 2025 14:23:31 +0200 Subject: [PATCH 2/3] [updates] fix: update from locally generated index (timestamps and downgrade) --- service/updates/index.go | 17 +++++++++++++++++ service/updates/index_scan.go | 9 +++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/service/updates/index.go b/service/updates/index.go index d98d92d7..13285f9d 100644 --- a/service/updates/index.go +++ b/service/updates/index.go @@ -118,6 +118,14 @@ type Index struct { Artifacts []*Artifact `json:"Artifacts"` versionNum *semver.Version + + // isLocallyGenerated indicates whether the index was generated from a local directory. + // + // When true: + // - The `Published` field represents the generation time, not a formal release date. + // This timestamp should be ignored when checking for online updates. + // - Downgrades from this locally generated version to an online index should be prevented. + isLocallyGenerated bool } // LoadIndex loads and parses an index from the given filename. @@ -235,6 +243,15 @@ func (index *Index) ShouldUpgradeTo(newIndex *Index) error { case index.Name != newIndex.Name: return errors.New("new index name does not match") + case index.isLocallyGenerated: + if newIndex.versionNum.GreaterThan(index.versionNum) { + // Upgrade! (from a locally generated index to an online index) + return nil + } else { + // "Do nothing". + return ErrSameIndex + } + case index.Published.After(newIndex.Published): return errors.New("new index is older (time)") diff --git a/service/updates/index_scan.go b/service/updates/index_scan.go index 1dc1a7f6..3ac52f5f 100644 --- a/service/updates/index_scan.go +++ b/service/updates/index_scan.go @@ -234,10 +234,11 @@ func GenerateIndexFromDir(sourceDir string, cfg IndexScanConfig) (*Index, error) // Create base index. index := &Index{ - Name: cfg.Name, - Version: cfg.Version, - Published: time.Now(), - versionNum: indexVersion, + Name: cfg.Name, + Version: cfg.Version, + Published: time.Now(), + versionNum: indexVersion, + isLocallyGenerated: true, } if index.Version == "" && cfg.PrimaryArtifact != "" { pv, ok := artifacts[cfg.PrimaryArtifact] From b83b2901c8f6a06eb800188c17ca617115e54623 Mon Sep 17 00:00:00 2001 From: Alexandr Stelnykovych Date: Fri, 28 Mar 2025 17:20:49 +0200 Subject: [PATCH 3/3] [updates] fix: skip PurgeDirectory during backup --- service/updates/upgrade.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/service/updates/upgrade.go b/service/updates/upgrade.go index 9d18de98..250baede 100644 --- a/service/updates/upgrade.go +++ b/service/updates/upgrade.go @@ -73,6 +73,10 @@ func (u *Updater) upgradeMoveFiles(downloader *Downloader) error { if slices.Contains(u.cfg.Ignore, file.Name()) { continue } + // ignore PurgeDirectory itself + if strings.EqualFold(u.cfg.PurgeDirectory, filepath.Join(u.cfg.Directory, file.Name())) { + continue + } // Otherwise, move file to purge dir. src := filepath.Join(u.cfg.Directory, file.Name())