Reload indexes when restarting with portmaster-start

This commit is contained in:
Daniel
2021-06-03 23:30:01 +02:00
parent 642e2eb112
commit 2127f1b210
3 changed files with 41 additions and 69 deletions

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"os/signal" "os/signal"
@@ -11,6 +12,10 @@ import (
"strings" "strings"
"syscall" "syscall"
"github.com/safing/portmaster/updates/helper"
"github.com/tidwall/gjson"
"github.com/safing/portbase/dataroot" "github.com/safing/portbase/dataroot"
"github.com/safing/portbase/info" "github.com/safing/portbase/info"
portlog "github.com/safing/portbase/log" portlog "github.com/safing/portbase/log"
@@ -33,7 +38,6 @@ var (
UpdateURLs: []string{ UpdateURLs: []string{
"https://updates.safing.io", "https://updates.safing.io",
}, },
Beta: false,
DevMode: false, DevMode: false,
Online: true, // is disabled later based on command Online: true, // is disabled later based on command
} }
@@ -65,7 +69,7 @@ func init() {
{ {
flags.StringVar(&dataDir, "data", "", "Configures the data directory. Alternatively, this can also be set via the environment variable PORTMASTER_DATA.") flags.StringVar(&dataDir, "data", "", "Configures the data directory. Alternatively, this can also be set via the environment variable PORTMASTER_DATA.")
flags.StringVar(&registry.UserAgent, "update-agent", "Start", "Sets the user agent for requests to the update server") flags.StringVar(&registry.UserAgent, "update-agent", "Start", "Sets the user agent for requests to the update server")
flags.BoolVar(&staging, "staging", false, "Use staging update channel (for testing only)") flags.BoolVar(&staging, "staging", false, "Deprecated, configure in settings instead.")
flags.IntVar(&maxRetries, "max-retries", 5, "Maximum number of retries when starting a Portmaster component") flags.IntVar(&maxRetries, "max-retries", 5, "Maximum number of retries when starting a Portmaster component")
flags.BoolVar(&stdinSignals, "input-signals", false, "Emulate signals using stdin.") flags.BoolVar(&stdinSignals, "input-signals", false, "Emulate signals using stdin.")
_ = rootCmd.MarkPersistentFlagDirname("data") _ = rootCmd.MarkPersistentFlagDirname("data")
@@ -164,32 +168,6 @@ func configureRegistry(mustLoadIndex bool) error {
return err return err
} }
registry.AddIndex(updater.Index{
Path: "stable.json",
Stable: true,
Beta: false,
})
// TODO: enable loading beta versions
// registry.AddIndex(updater.Index{
// Path: "beta.json",
// Stable: false,
// Beta: true,
// })
if stagingActive() {
// Set flag no matter how staging was activated.
staging = true
log.Println("WARNING: staging environment is active.")
registry.AddIndex(updater.Index{
Path: "staging.json",
Stable: true,
Beta: true,
})
}
return updateRegistryIndex(mustLoadIndex) return updateRegistryIndex(mustLoadIndex)
} }
@@ -210,6 +188,16 @@ func configureLogging() error {
} }
func updateRegistryIndex(mustLoadIndex bool) error { func updateRegistryIndex(mustLoadIndex bool) error {
// Get release channel from config.
releaseChannel := getReleaseChannel(dataRoot)
// Set indexes based on the release channel.
helper.SetIndexes(registry, releaseChannel)
// Update registry to use pre-releases or not.
registry.SetUsePreReleases(releaseChannel != helper.ReleaseChannelStable)
// Load indexes from disk or network, if needed and desired.
err := registry.LoadIndexes(context.Background()) err := registry.LoadIndexes(context.Background())
if err != nil { if err != nil {
log.Printf("WARNING: error loading indexes: %s\n", err) log.Printf("WARNING: error loading indexes: %s\n", err)
@@ -218,6 +206,7 @@ func updateRegistryIndex(mustLoadIndex bool) error {
} }
} }
// Load versions from disk to know which others we have and which are available.
err = registry.ScanStorage("") err = registry.ScanStorage("")
if err != nil { if err != nil {
log.Printf("WARNING: error during storage scan: %s\n", err) log.Printf("WARNING: error during storage scan: %s\n", err)
@@ -247,13 +236,23 @@ func detectInstallationDir() string {
return parent return parent
} }
func stagingActive() bool { func getReleaseChannel(dataRoot *utils.DirStructure) string {
// Check flag and env variable. configData, err := ioutil.ReadFile(filepath.Join(dataRoot.Path, "config.json"))
if staging || os.Getenv("PORTMASTER_STAGING") == "enabled" { if err != nil {
return true if !os.IsNotExist(err) {
log.Printf("WARNING: failed to read config.json to get release channel: %s", err)
}
return helper.ReleaseChannelStable
} }
// Check if staging index is present and acessible. // Get release channel from config, validate and return it.
_, err := os.Stat(filepath.Join(registry.StorageDir().Path, "staging.json")) channel := gjson.GetBytes(configData, helper.ReleaseChannelJSONKey).String()
return err == nil switch channel {
case helper.ReleaseChannelStable,
helper.ReleaseChannelBeta,
helper.ReleaseChannelStaging:
return channel
default:
return helper.ReleaseChannelStable
}
} }

View File

@@ -40,8 +40,7 @@ var (
func indexRequired(cmd *cobra.Command) bool { func indexRequired(cmd *cobra.Command) bool {
switch cmd { switch cmd {
case updateCmd, case updateCmd, purgeCmd:
purgeCmd:
return true return true
default: default:
return false return false
@@ -49,35 +48,9 @@ func indexRequired(cmd *cobra.Command) bool {
} }
func downloadUpdates() error { func downloadUpdates() error {
// mark required updates // Set required updates.
if onWindows { registry.MandatoryUpdates = helper.MandatoryUpdates()
registry.MandatoryUpdates = []string{ registry.AutoUnpack = helper.AutoUnpackUpdates()
helper.PlatformIdentifier("core/portmaster-core.exe"),
helper.PlatformIdentifier("kext/portmaster-kext.dll"),
helper.PlatformIdentifier("kext/portmaster-kext.sys"),
helper.PlatformIdentifier("start/portmaster-start.exe"),
helper.PlatformIdentifier("notifier/portmaster-notifier.exe"),
helper.PlatformIdentifier("notifier/portmaster-snoretoast.exe"),
}
} else {
registry.MandatoryUpdates = []string{
helper.PlatformIdentifier("core/portmaster-core"),
helper.PlatformIdentifier("start/portmaster-start"),
helper.PlatformIdentifier("notifier/portmaster-notifier"),
}
}
// add updates that we require on all platforms.
registry.MandatoryUpdates = append(
registry.MandatoryUpdates,
helper.PlatformIdentifier("app/portmaster-app.zip"),
"all/ui/modules/portmaster.zip",
)
// Add assets that need unpacking.
registry.AutoUnpack = []string{
helper.PlatformIdentifier("app/portmaster-app.zip"),
}
// logging is configured as a persistent pre-run method inherited from // logging is configured as a persistent pre-run method inherited from
// the root command but since we don't use run.Run() we need to start // the root command but since we don't use run.Run() we need to start
@@ -100,8 +73,8 @@ func downloadUpdates() error {
return fmt.Errorf("failed to create update dir: %w", err) return fmt.Errorf("failed to create update dir: %w", err)
} }
// Reset registry state. // Reset registry resources.
registry.Reset() registry.ResetResources()
} }
// Update all indexes. // Update all indexes.

2
go.mod
View File

@@ -30,7 +30,7 @@ require (
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 // indirect github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 // indirect
github.com/templexxx/xor v0.0.0-20191217153810-f85b25db303b // indirect github.com/templexxx/xor v0.0.0-20191217153810-f85b25db303b // indirect
github.com/tevino/abool v1.2.0 github.com/tevino/abool v1.2.0
github.com/tidwall/gjson v1.7.5 // indirect github.com/tidwall/gjson v1.7.5
github.com/tidwall/sjson v1.1.6 // indirect github.com/tidwall/sjson v1.1.6 // indirect
github.com/tjfoc/gmsm v1.4.0 // indirect github.com/tjfoc/gmsm v1.4.0 // indirect
github.com/tklauser/go-sysconf v0.3.5 // indirect github.com/tklauser/go-sysconf v0.3.5 // indirect