From b6d69ecddd62aac16ad901b479cd787a42a12b32 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 23 Jul 2020 09:21:49 +0200 Subject: [PATCH] Configure update registry user agent --- cmds/portmaster-start/main.go | 1 + updates/main.go | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/cmds/portmaster-start/main.go b/cmds/portmaster-start/main.go index 9ebfcbdb..fb1fa045 100644 --- a/cmds/portmaster-start/main.go +++ b/cmds/portmaster-start/main.go @@ -63,6 +63,7 @@ func init() { flags := rootCmd.PersistentFlags() { flags.StringVar(&dataDir, "data", "", "Configures the data directory. Alternatively, this can also be set via the environment variable PORTMASTER_DATA.") + flags.StringVar(®istry.UserAgent, "update-agent", "Start", "Sets the user agent for requests to the update server") flags.IntVar(&maxRetries, "max-retries", 5, "Maximum number of retries when starting a Portmaster component") flags.BoolVar(&stdinSignals, "input-signals", false, "Emulate signals using stdid.") _ = rootCmd.MarkPersistentFlagDirname("data") diff --git a/updates/main.go b/updates/main.go index 2d2310c7..1aed7bfa 100644 --- a/updates/main.go +++ b/updates/main.go @@ -2,6 +2,7 @@ package updates import ( "context" + "flag" "fmt" "runtime" "time" @@ -45,11 +46,18 @@ const ( ) var ( - module *modules.Module - registry *updater.ResourceRegistry + module *modules.Module + registry *updater.ResourceRegistry + userAgentFromFlag string + updateTask *modules.Task updateASAP bool disableTaskSchedule bool + + // UserAgent is an HTTP User-Agent that is used to add + // more context to requests made by the registry when + // fetching resources from the update server. + UserAgent = "Core" ) const ( @@ -62,6 +70,8 @@ func init() { module = modules.Register(ModuleName, prep, start, stop, "base") module.RegisterEvent(VersionUpdateEvent) module.RegisterEvent(ResourceUpdateEvent) + + flag.StringVar(&userAgentFromFlag, "update-agent", "", "Sets the user agent for requests to the update server") } func prep() error { @@ -121,11 +131,16 @@ func start() error { UpdateURLs: []string{ "https://updates.safing.io", }, + UserAgent: UserAgent, MandatoryUpdates: mandatoryUpdates, Beta: releaseChannel() == releaseChannelBeta, DevMode: devMode(), Online: true, } + if userAgentFromFlag != "" { + // override with flag value + registry.UserAgent = userAgentFromFlag + } // initialize err := registry.Initialize(dataroot.Root().ChildDir("updates", 0755)) if err != nil {