Add support to enable/disable the network rating system

This commit is contained in:
Patrick Pacher
2021-12-27 08:41:58 +01:00
parent ca4bac3b1b
commit 165ce53149
7 changed files with 169 additions and 340 deletions

38
status/config.go Normal file
View File

@@ -0,0 +1,38 @@
package status
import "github.com/safing/portbase/config"
var (
CfgEnableNetworkRatingSystemKey = "core/enableNetworkRating"
cfgEnableNetworkRatingSystem config.BoolOption
)
func registerConfig() error {
if err := config.Register(&config.Option{
Name: "Enable Network Rating System",
Key: CfgEnableNetworkRatingSystemKey,
Description: "Enables the Network Rating System, which allows you to quickly increase security and privacy throughout the settings by changing your the network rating level in the top left. Please note that this feature is now in the sunset phase and will be replaced by a superior and easier to understand system in the future.",
OptType: config.OptTypeBool,
ExpertiseLevel: config.ExpertiseLevelExpert,
ReleaseLevel: config.ReleaseLevelExperimental,
DefaultValue: false,
Annotations: config.Annotations{
config.DisplayOrderAnnotation: 514,
},
}); err != nil {
return err
}
cfgEnableNetworkRatingSystem = config.Concurrent.GetAsBool(CfgEnableNetworkRatingSystemKey, false)
return nil
}
// NetworkRatingEnabled returns true if the network rating system has been enabled.
func NetworkRatingEnabled() bool {
return cfgEnableNetworkRatingSystem()
}
// SetNetworkRating enables or disables the network rating system.
func SetNetworkRating(enabled bool) {
config.SetConfigOption(CfgEnableNetworkRatingSystemKey, enabled)
}

View File

@@ -14,7 +14,7 @@ var (
)
func init() {
module = modules.Register("status", nil, start, nil, "base")
module = modules.Register("status", prepare, start, nil, "base", "config")
}
func start() error {
@@ -26,7 +26,7 @@ func start() error {
triggerAutopilot()
err := module.RegisterEventHook(
if err := module.RegisterEventHook(
netenv.ModuleName,
netenv.OnlineStatusChangedEvent,
"update online status in system status",
@@ -34,8 +34,30 @@ func start() error {
triggerAutopilot()
return nil
},
)
if err != nil {
); err != nil {
return err
}
if err := module.RegisterEventHook(
"config",
"config change",
"Update network rating system",
func(_ context.Context, _ interface{}) error {
if !NetworkRatingEnabled() && ActiveSecurityLevel() != SecurityLevelNormal {
setSelectedLevel(SecurityLevelNormal)
triggerAutopilot()
}
return nil
},
); err != nil {
return err
}
return nil
}
func prepare() error {
if err := registerConfig(); err != nil {
return err
}

View File

@@ -54,6 +54,12 @@ func setSelectedSecurityLevel(r record.Record) (record.Record, error) {
}
}
// if the network rating system is not used at all we always force the security
// level to trusted.
if !NetworkRatingEnabled() {
upd.SelectedSecurityLevel = SecurityLevelNormal
}
if !IsValidSecurityLevel(upd.SelectedSecurityLevel) {
return nil, fmt.Errorf("invalid security level: %d", upd.SelectedSecurityLevel)
}