diff --git a/profile/config.go b/profile/config.go index 08f1ddc7..f067cb12 100644 --- a/profile/config.go +++ b/profile/config.go @@ -2,12 +2,8 @@ package profile import ( "strings" - "sync" "github.com/safing/portbase/config" - "github.com/safing/portbase/database" - "github.com/safing/portbase/database/record" - "github.com/safing/portbase/log" "github.com/safing/portmaster/profile/endpoints" "github.com/safing/portmaster/status" ) @@ -576,55 +572,3 @@ Please note that if you are using the system resolver, bypass attempts might be return nil } - -func migrateConfiguration() error { - // we use a temporary interface for the migration because there's no need - // to keep this one around - db := database.NewInterface(&database.Options{ - Local: true, - Internal: true, - }) - - if migrated, err := db.Exists("core:migration/networkRatingSystem"); err != nil { - return err - } else if !migrated { - // determine the default value for the network rating system by searching for - // a global security level setting that is not set to the default. - networkRatingEnabled := false - for _, cfgkey := range securityLevelSettings { - def, err := config.GetOption(cfgkey) - if err != nil { - return err - } - - intValue := config.Concurrent.GetAsInt(cfgkey, 0)() - if def.DefaultValue.(uint8) != uint8(intValue) { - log.Debugf("found global security level setting with changed value. 0x%2x (default) != 0x%2x (current)", def.DefaultValue, intValue) - networkRatingEnabled = true - break - } - } - - if networkRatingEnabled { - status.SetNetworkRating(networkRatingEnabled) - } - - // create a dummy record for the network rating system migration - var r struct { - record.Base `json:"-"` - sync.Mutex `json:"-"` - } - r.CreateMeta() - r.SetKey("core:migration/networkRatingSystem") - - if err := db.Put(&r); err != nil { - return err - } - - log.Infof("migration to configurable network rating system completed successfully: value=%v", networkRatingEnabled) - } else { - log.Debugf("migration to configurable network rating system already done") - } - - return nil -} diff --git a/profile/migrations.go b/profile/migrations.go new file mode 100644 index 00000000..3ef2d7fa --- /dev/null +++ b/profile/migrations.go @@ -0,0 +1,47 @@ +package profile + +import ( + "context" + + "github.com/hashicorp/go-version" + "github.com/safing/portbase/config" + "github.com/safing/portbase/database" + "github.com/safing/portbase/database/migration" + "github.com/safing/portbase/log" + "github.com/safing/portmaster/status" +) + +func registerMigrations() error { + return migrations.Add( + migration.Migration{ + Description: "Migrate to configurable network rating system", + Version: "v1.0.3", + MigrateFunc: migrateNetworkRatingSystem, + }, + ) +} + +func migrateNetworkRatingSystem(ctx context.Context, _, _ *version.Version, db *database.Interface) error { + // determine the default value for the network rating system by searching for + // a global security level setting that is not set to the default. + networkRatingEnabled := false + for _, cfgkey := range securityLevelSettings { + def, err := config.GetOption(cfgkey) + if err != nil { + return err + } + + intValue := config.Concurrent.GetAsInt(cfgkey, 0)() + if def.DefaultValue.(uint8) != uint8(intValue) { + log.Tracer(ctx).Infof("found global security level setting with changed value. 0x%2x (default) != 0x%2x (current)", def.DefaultValue, intValue) + networkRatingEnabled = true + break + } + } + + if networkRatingEnabled { + status.SetNetworkRating(networkRatingEnabled) + } + + return nil +} diff --git a/profile/module.go b/profile/module.go index 7242c90e..668903f2 100644 --- a/profile/module.go +++ b/profile/module.go @@ -3,6 +3,7 @@ package profile import ( "os" + "github.com/safing/portbase/database/migration" "github.com/safing/portbase/log" "github.com/safing/portbase/modules" "github.com/safing/portmaster/updates" @@ -12,6 +13,7 @@ import ( ) var ( + migrations = migration.New("core:migrations/profile") module *modules.Module updatesPath string ) @@ -21,13 +23,15 @@ func init() { } func prep() error { - err := registerConfiguration() - if err != nil { + if err := registerConfiguration(); err != nil { return err } - err = registerConfigUpdater() - if err != nil { + if err := registerConfigUpdater(); err != nil { + return err + } + + if err := registerMigrations(); err != nil { return err } @@ -40,7 +44,7 @@ func start() error { updatesPath += string(os.PathSeparator) } - if err := migrateConfiguration(); err != nil { + if err := migrations.Migrate(module.Ctx); err != nil { return err }