Skip to content

Commit

Permalink
Add flags to specify custom update server
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Apr 20, 2023
1 parent 16c7561 commit 8273894
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
28 changes: 25 additions & 3 deletions cmds/portmaster-start/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log"
"net/url"
"os"
"os/signal"
"path/filepath"
Expand All @@ -24,11 +25,13 @@ import (

var (
dataDir string
staging bool
maxRetries int
dataRoot *utils.DirStructure
logsRoot *utils.DirStructure

updateURLFlag string
userAgentFlag string

// Create registry.
registry = &updater.ResourceRegistry{
Name: "updates",
Expand Down Expand Up @@ -67,8 +70,8 @@ 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(&registry.UserAgent, "update-agent", "Start", "Sets the user agent for requests to the update server")
flags.BoolVar(&staging, "staging", false, "Deprecated, configure in settings instead.")
flags.StringVar(&updateURLFlag, "update-server", "", "Set an alternative update server (full URL)")
flags.StringVar(&userAgentFlag, "update-agent", "", "Set an alternative 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 stdin.")
_ = rootCmd.MarkPersistentFlagDirname("data")
Expand Down Expand Up @@ -137,6 +140,25 @@ func initCobra() {
}

func configureRegistry(mustLoadIndex bool) error {
// Check if update server URL supplied via flag is a valid URL.
if updateURLFlag != "" {
u, err := url.Parse(updateURLFlag)
if err != nil {
return fmt.Errorf("supplied update server URL is invalid: %w", err)
}
if u.Scheme != "https" {
return errors.New("supplied update server URL must use HTTPS")
}
}

// Override values from flags.
if userAgentFlag != "" {
registry.UserAgent = userAgentFlag
}
if updateURLFlag != "" {
registry.UpdateURLs = []string{updateURLFlag}
}

// If dataDir is not set, check the environment variable.
if dataDir == "" {
dataDir = os.Getenv("PORTMASTER_DATA")
Expand Down
6 changes: 2 additions & 4 deletions cmds/portmaster-start/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,8 @@ func logProgress(state *updater.RegistryState) {
len(downloadDetails.Resources),
downloadDetails.Resources[downloadDetails.FinishedUpTo],
)
} else {
if state.Updates.LastDownloadAt == nil {
log.Println("finalizing downloads")
}
} else if state.Updates.LastDownloadAt == nil {
log.Println("finalizing downloads")
}
}
}
Expand Down
43 changes: 31 additions & 12 deletions updates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package updates

import (
"context"
"errors"
"flag"
"fmt"
"net/url"
"runtime"
"time"

Expand Down Expand Up @@ -41,9 +43,11 @@ const (
)

var (
module *modules.Module
registry *updater.ResourceRegistry
userAgentFromFlag string
module *modules.Module
registry *updater.ResourceRegistry

userAgentFromFlag string
updateServerFromFlag string

updateTask *modules.Task
updateASAP bool
Expand All @@ -59,6 +63,11 @@ var (
// fetching resources from the update server.
UserAgent = fmt.Sprintf("Portmaster (%s %s)", runtime.GOOS, runtime.GOARCH)

// DefaultUpdateURLs defines the default base URLs of the update server.
DefaultUpdateURLs = []string{
"https://updates.safing.io",
}

// DisableSoftwareAutoUpdate specifies whether software updates should be disabled.
// This is used on Android, as it will never require binary updates.
DisableSoftwareAutoUpdate = false
Expand All @@ -75,17 +84,26 @@ func init() {
module.RegisterEvent(VersionUpdateEvent, true)
module.RegisterEvent(ResourceUpdateEvent, true)

flag.StringVar(&userAgentFromFlag, "update-agent", "", "set the user agent for requests to the update server")

var dummy bool
flag.BoolVar(&dummy, "staging", false, "deprecated, configure in settings instead")
flag.StringVar(&updateServerFromFlag, "update-server", "", "set an alternative update server (full URL)")
flag.StringVar(&userAgentFromFlag, "update-agent", "", "set an alternative user agent for requests to the update server")
}

func prep() error {
if err := registerConfig(); err != nil {
return err
}

// Check if update server URL supplied via flag is a valid URL.
if updateServerFromFlag != "" {
u, err := url.Parse(updateServerFromFlag)
if err != nil {
return fmt.Errorf("supplied update server URL is invalid: %w", err)
}
if u.Scheme != "https" {
return errors.New("supplied update server URL must use HTTPS")
}
}

return registerAPIEndpoints()
}

Expand All @@ -104,21 +122,22 @@ func start() error {

// create registry
registry = &updater.ResourceRegistry{
Name: ModuleName,
UpdateURLs: []string{
"https://updates.safing.io",
},
Name: ModuleName,
UpdateURLs: DefaultUpdateURLs,
UserAgent: UserAgent,
MandatoryUpdates: helper.MandatoryUpdates(),
AutoUnpack: helper.AutoUnpackUpdates(),
Verification: helper.VerificationConfig,
DevMode: devMode(),
Online: true,
}
// Override values from flags.
if userAgentFromFlag != "" {
// override with flag value
registry.UserAgent = userAgentFromFlag
}
if updateServerFromFlag != "" {
registry.UpdateURLs = []string{updateServerFromFlag}
}

// pre-init state
updateStateExport, err := LoadStateExport()
Expand Down

0 comments on commit 8273894

Please sign in to comment.