wip: migrate to mono-repo. SPN has already been moved to spn/
This commit is contained in:
45
service/status/module.go
Normal file
45
service/status/module.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package status
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/safing/portbase/modules"
|
||||
"github.com/safing/portbase/utils/debug"
|
||||
"github.com/safing/portmaster/service/netenv"
|
||||
)
|
||||
|
||||
var module *modules.Module
|
||||
|
||||
func init() {
|
||||
module = modules.Register("status", nil, start, nil, "base", "config")
|
||||
}
|
||||
|
||||
func start() error {
|
||||
if err := setupRuntimeProvider(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := module.RegisterEventHook(
|
||||
netenv.ModuleName,
|
||||
netenv.OnlineStatusChangedEvent,
|
||||
"update online status in system status",
|
||||
func(_ context.Context, _ interface{}) error {
|
||||
pushSystemStatus()
|
||||
return nil
|
||||
},
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddToDebugInfo adds the system status to the given debug.Info.
|
||||
func AddToDebugInfo(di *debug.Info) {
|
||||
di.AddSection(
|
||||
fmt.Sprintf("Status: %s", netenv.GetOnlineStatus()),
|
||||
debug.UseCodeSection|debug.AddContentLineBreaks,
|
||||
fmt.Sprintf("OnlineStatus: %s", netenv.GetOnlineStatus()),
|
||||
fmt.Sprintf("CaptivePortal: %s", netenv.GetCaptivePortal().URL),
|
||||
)
|
||||
}
|
||||
49
service/status/provider.go
Normal file
49
service/status/provider.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package status
|
||||
|
||||
import (
|
||||
"github.com/safing/portbase/database/record"
|
||||
"github.com/safing/portbase/runtime"
|
||||
"github.com/safing/portmaster/service/netenv"
|
||||
)
|
||||
|
||||
var pushUpdate runtime.PushFunc
|
||||
|
||||
func setupRuntimeProvider() (err error) {
|
||||
// register the system status getter
|
||||
statusProvider := runtime.SimpleValueGetterFunc(func(_ string) ([]record.Record, error) {
|
||||
return []record.Record{buildSystemStatus()}, nil
|
||||
})
|
||||
pushUpdate, err = runtime.Register("system/status", statusProvider)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// buildSystemStatus build a new system status record.
|
||||
func buildSystemStatus() *SystemStatusRecord {
|
||||
status := &SystemStatusRecord{
|
||||
CaptivePortal: netenv.GetCaptivePortal(),
|
||||
OnlineStatus: netenv.GetOnlineStatus(),
|
||||
}
|
||||
|
||||
status.CreateMeta()
|
||||
status.SetKey("runtime:system/status")
|
||||
|
||||
return status
|
||||
}
|
||||
|
||||
// pushSystemStatus pushes a new system status via
|
||||
// the runtime database.
|
||||
func pushSystemStatus() {
|
||||
if pushUpdate == nil {
|
||||
return
|
||||
}
|
||||
|
||||
record := buildSystemStatus()
|
||||
record.Lock()
|
||||
defer record.Unlock()
|
||||
|
||||
pushUpdate(record)
|
||||
}
|
||||
23
service/status/records.go
Normal file
23
service/status/records.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package status
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/safing/portbase/database/record"
|
||||
"github.com/safing/portmaster/service/netenv"
|
||||
)
|
||||
|
||||
// SystemStatusRecord describes the overall status of the Portmaster.
|
||||
// It's a read-only record exposed via runtime:system/status.
|
||||
type SystemStatusRecord struct {
|
||||
record.Base
|
||||
sync.Mutex
|
||||
|
||||
// OnlineStatus holds the current online status as
|
||||
// seen by the netenv package.
|
||||
OnlineStatus netenv.OnlineStatus
|
||||
// CaptivePortal holds all information about the captive
|
||||
// portal of the network the portmaster is currently
|
||||
// connected to, if any.
|
||||
CaptivePortal *netenv.CaptivePortal
|
||||
}
|
||||
60
service/status/security_level.go
Normal file
60
service/status/security_level.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package status
|
||||
|
||||
import "github.com/safing/portbase/config"
|
||||
|
||||
// MigrateSecurityLevelToBoolean migrates a security level (int) option value to a boolean option value.
|
||||
func MigrateSecurityLevelToBoolean(option *config.Option, value any) any {
|
||||
// Check new (target) option type.
|
||||
if option.OptType != config.OptTypeBool {
|
||||
// This migration converts to boolean.
|
||||
// Thus, conversion is not applicable.
|
||||
return value
|
||||
}
|
||||
|
||||
// Convert value to uint8.
|
||||
var nVal uint8
|
||||
switch v := value.(type) {
|
||||
case int:
|
||||
nVal = uint8(v)
|
||||
case int8:
|
||||
nVal = uint8(v)
|
||||
case int16:
|
||||
nVal = uint8(v)
|
||||
case int32:
|
||||
nVal = uint8(v)
|
||||
case int64:
|
||||
nVal = uint8(v)
|
||||
case uint:
|
||||
nVal = uint8(v)
|
||||
case uint8:
|
||||
nVal = v
|
||||
case uint16:
|
||||
nVal = uint8(v)
|
||||
case uint32:
|
||||
nVal = uint8(v)
|
||||
case uint64:
|
||||
nVal = uint8(v)
|
||||
case float32:
|
||||
nVal = uint8(v)
|
||||
case float64:
|
||||
nVal = uint8(v)
|
||||
default:
|
||||
// Input type not compatible.
|
||||
return value
|
||||
}
|
||||
|
||||
// Convert to boolean.
|
||||
return nVal&SecurityLevelNormal > 0
|
||||
}
|
||||
|
||||
// DisplayHintSecurityLevel is an external option hint for security levels.
|
||||
// It's meant to be used as a value for config.DisplayHintAnnotation.
|
||||
const DisplayHintSecurityLevel string = "security level"
|
||||
|
||||
// Security levels.
|
||||
const (
|
||||
SecurityLevelOff uint8 = 0
|
||||
SecurityLevelNormal uint8 = 1
|
||||
SecurityLevelHigh uint8 = 2
|
||||
SecurityLevelExtreme uint8 = 4
|
||||
)
|
||||
Reference in New Issue
Block a user