mirror of
https://github.com/safing/portmaster
synced 2025-09-02 02:29:12 +00:00
Use new migration system for network-rating migration
This commit is contained in:
parent
a9144f915f
commit
6fc098d618
3 changed files with 56 additions and 61 deletions
|
@ -2,12 +2,8 @@ package profile
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/safing/portbase/config"
|
"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/profile/endpoints"
|
||||||
"github.com/safing/portmaster/status"
|
"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
|
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
|
|
||||||
}
|
|
||||||
|
|
47
profile/migrations.go
Normal file
47
profile/migrations.go
Normal file
|
@ -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
|
||||||
|
}
|
|
@ -3,6 +3,7 @@ package profile
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/safing/portbase/database/migration"
|
||||||
"github.com/safing/portbase/log"
|
"github.com/safing/portbase/log"
|
||||||
"github.com/safing/portbase/modules"
|
"github.com/safing/portbase/modules"
|
||||||
"github.com/safing/portmaster/updates"
|
"github.com/safing/portmaster/updates"
|
||||||
|
@ -12,6 +13,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
migrations = migration.New("core:migrations/profile")
|
||||||
module *modules.Module
|
module *modules.Module
|
||||||
updatesPath string
|
updatesPath string
|
||||||
)
|
)
|
||||||
|
@ -21,13 +23,15 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func prep() error {
|
func prep() error {
|
||||||
err := registerConfiguration()
|
if err := registerConfiguration(); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = registerConfigUpdater()
|
if err := registerConfigUpdater(); err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := registerMigrations(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +44,7 @@ func start() error {
|
||||||
updatesPath += string(os.PathSeparator)
|
updatesPath += string(os.PathSeparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := migrateConfiguration(); err != nil {
|
if err := migrations.Migrate(module.Ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue