Improve status module
This commit is contained in:
106
status/set.go
106
status/set.go
@@ -1,61 +1,93 @@
|
||||
package status
|
||||
|
||||
import "sync/atomic"
|
||||
import (
|
||||
"sync/atomic"
|
||||
|
||||
// SetCurrentSecurityLevel sets the current security level.
|
||||
func SetCurrentSecurityLevel(level uint8) {
|
||||
sysStatusLock.Lock()
|
||||
defer sysStatusLock.Unlock()
|
||||
sysStatus.CurrentSecurityLevel = level
|
||||
atomicUpdateCurrentSecurityLevel(level)
|
||||
"github.com/Safing/portbase/log"
|
||||
)
|
||||
|
||||
// autopilot automatically adjusts the security level as needed
|
||||
func (s *SystemStatus) autopilot() {
|
||||
// check if users is overruling
|
||||
if s.SelectedSecurityLevel > SecurityLevelOff {
|
||||
s.ActiveSecurityLevel = s.SelectedSecurityLevel
|
||||
atomicUpdateActiveSecurityLevel(s.SelectedSecurityLevel)
|
||||
return
|
||||
}
|
||||
|
||||
// update active security level
|
||||
switch s.ThreatMitigationLevel {
|
||||
case SecurityLevelOff:
|
||||
s.ActiveSecurityLevel = SecurityLevelDynamic
|
||||
atomicUpdateActiveSecurityLevel(SecurityLevelDynamic)
|
||||
case SecurityLevelDynamic, SecurityLevelSecure, SecurityLevelFortress:
|
||||
s.ActiveSecurityLevel = s.ThreatMitigationLevel
|
||||
atomicUpdateActiveSecurityLevel(s.ThreatMitigationLevel)
|
||||
default:
|
||||
log.Errorf("status: threat mitigation level is set to invalid value: %d", s.ThreatMitigationLevel)
|
||||
}
|
||||
}
|
||||
|
||||
// SetSelectedSecurityLevel sets the selected security level.
|
||||
func SetSelectedSecurityLevel(level uint8) {
|
||||
sysStatusLock.Lock()
|
||||
defer sysStatusLock.Unlock()
|
||||
sysStatus.SelectedSecurityLevel = level
|
||||
atomicUpdateSelectedSecurityLevel(level)
|
||||
}
|
||||
// setSelectedSecurityLevel sets the selected security level.
|
||||
func setSelectedSecurityLevel(level uint8) {
|
||||
switch level {
|
||||
case SecurityLevelOff, SecurityLevelDynamic, SecurityLevelSecure, SecurityLevelFortress:
|
||||
status.Lock()
|
||||
defer status.Unlock()
|
||||
|
||||
// SetThreatLevel sets the current threat level.
|
||||
func SetThreatLevel(level uint8) {
|
||||
sysStatusLock.Lock()
|
||||
defer sysStatusLock.Unlock()
|
||||
sysStatus.ThreatLevel = level
|
||||
atomicUpdateThreatLevel(level)
|
||||
status.SelectedSecurityLevel = level
|
||||
atomicUpdateSelectedSecurityLevel(level)
|
||||
status.autopilot()
|
||||
|
||||
go status.Save()
|
||||
default:
|
||||
log.Errorf("status: tried to set selected security level to invalid value: %d", level)
|
||||
}
|
||||
}
|
||||
|
||||
// SetPortmasterStatus sets the current Portmaster status.
|
||||
func SetPortmasterStatus(status uint8) {
|
||||
sysStatusLock.Lock()
|
||||
defer sysStatusLock.Unlock()
|
||||
sysStatus.PortmasterStatus = status
|
||||
atomicUpdatePortmasterStatus(status)
|
||||
func SetPortmasterStatus(pmStatus uint8, msg string) {
|
||||
switch pmStatus {
|
||||
case StatusOff, StatusError, StatusWarning, StatusOk:
|
||||
status.Lock()
|
||||
defer status.Unlock()
|
||||
|
||||
status.PortmasterStatus = pmStatus
|
||||
status.PortmasterStatusMsg = msg
|
||||
atomicUpdatePortmasterStatus(pmStatus)
|
||||
|
||||
go status.Save()
|
||||
default:
|
||||
log.Errorf("status: tried to set portmaster to invalid status: %d", status)
|
||||
}
|
||||
}
|
||||
|
||||
// SetGate17Status sets the current Gate17 status.
|
||||
func SetGate17Status(status uint8) {
|
||||
sysStatusLock.Lock()
|
||||
defer sysStatusLock.Unlock()
|
||||
sysStatus.Gate17Status = status
|
||||
atomicUpdateGate17Status(status)
|
||||
func SetGate17Status(g17Status uint8, msg string) {
|
||||
switch g17Status {
|
||||
case StatusOff, StatusError, StatusWarning, StatusOk:
|
||||
status.Lock()
|
||||
defer status.Unlock()
|
||||
|
||||
status.Gate17Status = g17Status
|
||||
status.Gate17StatusMsg = msg
|
||||
atomicUpdateGate17Status(g17Status)
|
||||
|
||||
go status.Save()
|
||||
default:
|
||||
log.Errorf("status: tried to set gate17 to invalid status: %d", status)
|
||||
}
|
||||
}
|
||||
|
||||
// update functions for atomic stuff
|
||||
|
||||
func atomicUpdateCurrentSecurityLevel(level uint8) {
|
||||
atomic.StoreUint32(currentSecurityLevel, uint32(level))
|
||||
func atomicUpdateActiveSecurityLevel(level uint8) {
|
||||
atomic.StoreUint32(activeSecurityLevel, uint32(level))
|
||||
}
|
||||
|
||||
func atomicUpdateSelectedSecurityLevel(level uint8) {
|
||||
atomic.StoreUint32(selectedSecurityLevel, uint32(level))
|
||||
}
|
||||
|
||||
func atomicUpdateThreatLevel(level uint8) {
|
||||
atomic.StoreUint32(threatLevel, uint32(level))
|
||||
}
|
||||
|
||||
func atomicUpdatePortmasterStatus(status uint8) {
|
||||
atomic.StoreUint32(portmasterStatus, uint32(status))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user