From 776b4860ef4d81defb0206b952b85ac92d7fc0bc Mon Sep 17 00:00:00 2001
From: Alexandr Stelnykovych <alexandr.stelnykovych@ivpn.net>
Date: Wed, 26 Mar 2025 17:35:27 +0200
Subject: [PATCH 1/2] [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 149e1609bb34d21bb0e7a82331bf077d7d6ed97f Mon Sep 17 00:00:00 2001
From: Alexandr Stelnykovych <alexandr.stelnykovych@ivpn.net>
Date: Wed, 26 Mar 2025 18:03:29 +0200
Subject: [PATCH 2/2] [updates] fix: Initial update failed due to the published
 date of the actual index being in the past

---
 service/updates/module.go | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/service/updates/module.go b/service/updates/module.go
index 8a441ac9..6d815d13 100644
--- a/service/updates/module.go
+++ b/service/updates/module.go
@@ -205,9 +205,16 @@ func New(instance instance, name string, cfg Config) (*Updater, error) {
 		Name:    configure.DefaultBinaryIndexName,
 		Version: info.VersionNumber(),
 	})
-	if err == nil && index.init(currentPlatform) == nil {
-		module.index = index
-		return module, nil
+	if err == nil {
+		// As the index is generated from the current directory,
+		// we must set the published date to a fixed point in the past.
+		// New indexes will only be considered if their published date is later than the current one.
+		index.Published = time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
+
+		if index.init(currentPlatform) == nil {
+			module.index = index
+			return module, nil
+		}
 	}
 
 	// Fall back to empty index.