diff --git a/status/get-config.go b/status/get-config.go new file mode 100644 index 00000000..134a9cef --- /dev/null +++ b/status/get-config.go @@ -0,0 +1,33 @@ +package status + +import ( + "github.com/Safing/portbase/config" +) + +type ( + // SecurityLevelOption defines the returned function by ConfigIsActive. + SecurityLevelOption func(minSecurityLevel uint8) bool +) + +func max(a, b uint8) uint8 { + if a > b { + return a + } + return b +} + +// ConfigIsActive returns whether the given security level dependent config option is on or off. +func ConfigIsActive(name string) SecurityLevelOption { + activeAtLevel := config.GetAsInt(name, int64(SecurityLevelDynamic)) + return func(minSecurityLevel uint8) bool { + return uint8(activeAtLevel()) <= max(CurrentSecurityLevel(), minSecurityLevel) + } +} + +// ConfigIsActiveConcurrent returns whether the given security level dependent config option is on or off and is concurrency safe. +func ConfigIsActiveConcurrent(name string) SecurityLevelOption { + activeAtLevel := config.Concurrent.GetAsInt(name, int64(SecurityLevelDynamic)) + return func(minSecurityLevel uint8) bool { + return uint8(activeAtLevel()) <= max(CurrentSecurityLevel(), minSecurityLevel) + } +} diff --git a/status/get.go b/status/get.go index 7666b2f1..158a36eb 100644 --- a/status/get.go +++ b/status/get.go @@ -2,8 +2,6 @@ package status import ( "sync/atomic" - - "github.com/Safing/portbase/config" ) var ( @@ -30,35 +28,27 @@ func init() { gate17Status = &gate17StatusValue } -// GetCurrentSecurityLevel returns the current security level. -func GetCurrentSecurityLevel() uint8 { +// CurrentSecurityLevel returns the current security level. +func CurrentSecurityLevel() uint8 { return uint8(atomic.LoadUint32(currentSecurityLevel)) } -// GetSelectedSecurityLevel returns the selected security level. -func GetSelectedSecurityLevel() uint8 { +// SelectedSecurityLevel returns the selected security level. +func SelectedSecurityLevel() uint8 { return uint8(atomic.LoadUint32(selectedSecurityLevel)) } -// GetThreatLevel returns the current threat level. -func GetThreatLevel() uint8 { +// ThreatLevel returns the current threat level. +func ThreatLevel() uint8 { return uint8(atomic.LoadUint32(threatLevel)) } -// GetPortmasterStatus returns the current Portmaster status. -func GetPortmasterStatus() uint8 { +// PortmasterStatus returns the current Portmaster status. +func PortmasterStatus() uint8 { return uint8(atomic.LoadUint32(portmasterStatus)) } -// GetGate17Status returns the current Gate17 status. -func GetGate17Status() uint8 { +// Gate17Status returns the current Gate17 status. +func Gate17Status() uint8 { return uint8(atomic.LoadUint32(gate17Status)) } - -// GetConfigByLevel returns whether the given security level dependent config option is on or off. -func GetConfigByLevel(name string) func() bool { - activatedLevel := config.GetAsInt(name, int64(SecurityLevelDynamic)) - return func() bool { - return uint8(activatedLevel()) <= GetCurrentSecurityLevel() - } -} diff --git a/status/get_test.go b/status/get_test.go index e4348e2c..2777157e 100644 --- a/status/get_test.go +++ b/status/get_test.go @@ -5,12 +5,14 @@ import "testing" func TestGet(t *testing.T) { // only test for panics - GetCurrentSecurityLevel() - GetSelectedSecurityLevel() - GetThreatLevel() - GetPortmasterStatus() - GetGate17Status() - option := GetConfigByLevel("invalid") - option() + CurrentSecurityLevel() + SelectedSecurityLevel() + ThreatLevel() + PortmasterStatus() + Gate17Status() + option := ConfigIsActive("invalid") + option(0) + option = ConfigIsActiveConcurrent("invalid") + option(0) } diff --git a/status/status.go b/status/status.go index 5939e400..54d19e36 100644 --- a/status/status.go +++ b/status/status.go @@ -3,8 +3,8 @@ package status import "sync" var ( - sysStatusLock sync.RWMutex sysStatus *SystemStatus + sysStatusLock sync.RWMutex ) func init() { @@ -29,8 +29,8 @@ type SystemStatus struct { // FmtSecurityLevel returns the current security level as a string. func FmtSecurityLevel() string { - current := GetCurrentSecurityLevel() - selected := GetSelectedSecurityLevel() + current := CurrentSecurityLevel() + selected := SelectedSecurityLevel() var s string switch current { case SecurityLevelOff: