Update and improve events according to new system
This commit is contained in:
38
core/api.go
38
core/api.go
@@ -1,7 +1,9 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/safing/portbase/api"
|
||||
"github.com/safing/portbase/log"
|
||||
@@ -11,6 +13,16 @@ import (
|
||||
"github.com/safing/portmaster/updates"
|
||||
)
|
||||
|
||||
const (
|
||||
eventShutdown = "shutdown"
|
||||
eventRestart = "restart"
|
||||
)
|
||||
|
||||
func registerEvents() {
|
||||
module.RegisterEvent(eventShutdown, true)
|
||||
module.RegisterEvent(eventRestart, true)
|
||||
}
|
||||
|
||||
func registerAPIEndpoints() error {
|
||||
if err := api.RegisterEndpoint(api.Endpoint{
|
||||
Path: "core/shutdown",
|
||||
@@ -50,15 +62,35 @@ func registerAPIEndpoints() error {
|
||||
// shutdown shuts the Portmaster down.
|
||||
func shutdown(_ *api.Request) (msg string, err error) {
|
||||
log.Warning("core: user requested shutdown via action")
|
||||
// Do not use a worker, as this would block itself here.
|
||||
go modules.Shutdown() //nolint:errcheck
|
||||
|
||||
module.StartWorker("shutdown", func(context.Context) error {
|
||||
// Notify everyone of the shutdown.
|
||||
module.TriggerEvent(eventShutdown, nil)
|
||||
// Wait a bit for the event to propagate.
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
// Do not run in worker, as this would block itself here.
|
||||
go modules.Shutdown() //nolint:errcheck
|
||||
return nil
|
||||
})
|
||||
|
||||
return "shutdown initiated", nil
|
||||
}
|
||||
|
||||
// restart restarts the Portmaster.
|
||||
func restart(_ *api.Request) (msg string, err error) {
|
||||
log.Info("core: user requested restart via action")
|
||||
updates.RestartNow()
|
||||
|
||||
module.StartWorker("restart", func(context.Context) error {
|
||||
// Notify everyone of the shutdown.
|
||||
module.TriggerEvent(eventRestart, nil)
|
||||
// Wait a bit for the event to propagate.
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
updates.RestartNow()
|
||||
return nil
|
||||
})
|
||||
|
||||
return "restart initiated", nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,99 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/safing/portbase/database"
|
||||
"github.com/safing/portbase/database/record"
|
||||
"github.com/safing/portbase/database/storage"
|
||||
)
|
||||
|
||||
// StorageInterface provices a storage.Interface to the storage manager.
|
||||
type StorageInterface struct {
|
||||
storage.InjectBase
|
||||
}
|
||||
|
||||
// Get returns a database record.
|
||||
func (s *StorageInterface) Get(key string) (record.Record, error) {
|
||||
msg := newMessage(key)
|
||||
splittedKey := strings.Split(key, "/")
|
||||
|
||||
switch splittedKey[0] {
|
||||
case "module":
|
||||
return controlModule(msg, splittedKey)
|
||||
default:
|
||||
return nil, storage.ErrNotFound
|
||||
}
|
||||
}
|
||||
|
||||
func controlModule(msg *MessageRecord, splittedKey []string) (record.Record, error) {
|
||||
// format: module/moduleName/action/param
|
||||
var moduleName string
|
||||
var action string
|
||||
var param string
|
||||
var err error
|
||||
|
||||
// parse elements
|
||||
switch len(splittedKey) {
|
||||
case 4:
|
||||
param = splittedKey[3]
|
||||
fallthrough
|
||||
case 3:
|
||||
moduleName = splittedKey[1]
|
||||
action = splittedKey[2]
|
||||
default:
|
||||
return nil, storage.ErrNotFound
|
||||
}
|
||||
|
||||
// execute
|
||||
switch action {
|
||||
case "trigger":
|
||||
err = module.InjectEvent(fmt.Sprintf("user triggered the '%s/%s' event", moduleName, param), moduleName, param, nil)
|
||||
default:
|
||||
return nil, storage.ErrNotFound
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
msg.Message = err.Error()
|
||||
} else {
|
||||
msg.Success = true
|
||||
}
|
||||
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
func registerControlDatabase() error {
|
||||
_, err := database.Register(&database.Database{
|
||||
Name: "control",
|
||||
Description: "Control Interface for the Portmaster",
|
||||
StorageType: "injected",
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = database.InjectDatabase("control", &StorageInterface{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MessageRecord is a simple record used for control database feedback
|
||||
type MessageRecord struct {
|
||||
record.Base
|
||||
sync.Mutex
|
||||
|
||||
Success bool
|
||||
Message string
|
||||
}
|
||||
|
||||
func newMessage(key string) *MessageRecord {
|
||||
m := &MessageRecord{}
|
||||
m.SetKey("control:" + key)
|
||||
m.UpdateMeta()
|
||||
return m
|
||||
}
|
||||
17
core/core.go
17
core/core.go
@@ -33,12 +33,15 @@ func prep() error {
|
||||
registerEvents()
|
||||
|
||||
// init config
|
||||
logFlagOverrides()
|
||||
err := registerConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := registerAPIEndpoints(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -47,18 +50,6 @@ func start() error {
|
||||
return fmt.Errorf("failed to start plattform-specific components: %s", err)
|
||||
}
|
||||
|
||||
if err := registerEventHooks(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := registerControlDatabase(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := registerAPIEndpoints(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
registerLogCleaner()
|
||||
|
||||
return nil
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
// DEPRECATED: remove in v0.7
|
||||
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/safing/portbase/log"
|
||||
"github.com/safing/portbase/modules"
|
||||
"github.com/safing/portmaster/updates"
|
||||
)
|
||||
|
||||
const (
|
||||
eventShutdown = "shutdown"
|
||||
eventRestart = "restart"
|
||||
)
|
||||
|
||||
func registerEvents() {
|
||||
module.RegisterEvent(eventShutdown)
|
||||
module.RegisterEvent(eventRestart)
|
||||
}
|
||||
|
||||
func registerEventHooks() error {
|
||||
err := module.RegisterEventHook("core", eventShutdown, "execute shutdown", shutdownHook)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = module.RegisterEventHook("core", eventRestart, "execute shutdown", restartHook)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// shutdownHook shuts the Portmaster down.
|
||||
func shutdownHook(ctx context.Context, _ interface{}) error {
|
||||
log.Warning("core: user requested shutdown")
|
||||
// Do not use a worker, as this would block itself here.
|
||||
go modules.Shutdown() //nolint:errcheck
|
||||
return nil
|
||||
}
|
||||
|
||||
// restartHook restarts the Portmaster.
|
||||
func restartHook(ctx context.Context, data interface{}) error {
|
||||
log.Info("core: user requested restart")
|
||||
updates.RestartNow()
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user