Add additional functions to the status package

This commit is contained in:
Daniel 2018-10-22 17:02:54 +02:00
parent 29d0447eec
commit b1cd19a8e8
4 changed files with 55 additions and 30 deletions

33
status/get-config.go Normal file
View file

@ -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)
}
}

View file

@ -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()
}
}

View file

@ -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)
}

View file

@ -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: