Add additional functions to the status package
This commit is contained in:
33
status/get-config.go
Normal file
33
status/get-config.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,8 +2,6 @@ package status
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/Safing/portbase/config"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -30,35 +28,27 @@ func init() {
|
|||||||
gate17Status = &gate17StatusValue
|
gate17Status = &gate17StatusValue
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCurrentSecurityLevel returns the current security level.
|
// CurrentSecurityLevel returns the current security level.
|
||||||
func GetCurrentSecurityLevel() uint8 {
|
func CurrentSecurityLevel() uint8 {
|
||||||
return uint8(atomic.LoadUint32(currentSecurityLevel))
|
return uint8(atomic.LoadUint32(currentSecurityLevel))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSelectedSecurityLevel returns the selected security level.
|
// SelectedSecurityLevel returns the selected security level.
|
||||||
func GetSelectedSecurityLevel() uint8 {
|
func SelectedSecurityLevel() uint8 {
|
||||||
return uint8(atomic.LoadUint32(selectedSecurityLevel))
|
return uint8(atomic.LoadUint32(selectedSecurityLevel))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetThreatLevel returns the current threat level.
|
// ThreatLevel returns the current threat level.
|
||||||
func GetThreatLevel() uint8 {
|
func ThreatLevel() uint8 {
|
||||||
return uint8(atomic.LoadUint32(threatLevel))
|
return uint8(atomic.LoadUint32(threatLevel))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPortmasterStatus returns the current Portmaster status.
|
// PortmasterStatus returns the current Portmaster status.
|
||||||
func GetPortmasterStatus() uint8 {
|
func PortmasterStatus() uint8 {
|
||||||
return uint8(atomic.LoadUint32(portmasterStatus))
|
return uint8(atomic.LoadUint32(portmasterStatus))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetGate17Status returns the current Gate17 status.
|
// Gate17Status returns the current Gate17 status.
|
||||||
func GetGate17Status() uint8 {
|
func Gate17Status() uint8 {
|
||||||
return uint8(atomic.LoadUint32(gate17Status))
|
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -5,12 +5,14 @@ import "testing"
|
|||||||
func TestGet(t *testing.T) {
|
func TestGet(t *testing.T) {
|
||||||
|
|
||||||
// only test for panics
|
// only test for panics
|
||||||
GetCurrentSecurityLevel()
|
CurrentSecurityLevel()
|
||||||
GetSelectedSecurityLevel()
|
SelectedSecurityLevel()
|
||||||
GetThreatLevel()
|
ThreatLevel()
|
||||||
GetPortmasterStatus()
|
PortmasterStatus()
|
||||||
GetGate17Status()
|
Gate17Status()
|
||||||
option := GetConfigByLevel("invalid")
|
option := ConfigIsActive("invalid")
|
||||||
option()
|
option(0)
|
||||||
|
option = ConfigIsActiveConcurrent("invalid")
|
||||||
|
option(0)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ package status
|
|||||||
import "sync"
|
import "sync"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
sysStatusLock sync.RWMutex
|
|
||||||
sysStatus *SystemStatus
|
sysStatus *SystemStatus
|
||||||
|
sysStatusLock sync.RWMutex
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -29,8 +29,8 @@ type SystemStatus struct {
|
|||||||
|
|
||||||
// FmtSecurityLevel returns the current security level as a string.
|
// FmtSecurityLevel returns the current security level as a string.
|
||||||
func FmtSecurityLevel() string {
|
func FmtSecurityLevel() string {
|
||||||
current := GetCurrentSecurityLevel()
|
current := CurrentSecurityLevel()
|
||||||
selected := GetSelectedSecurityLevel()
|
selected := SelectedSecurityLevel()
|
||||||
var s string
|
var s string
|
||||||
switch current {
|
switch current {
|
||||||
case SecurityLevelOff:
|
case SecurityLevelOff:
|
||||||
|
|||||||
Reference in New Issue
Block a user