Restructure modules (#1572)
* Move portbase into monorepo * Add new simple module mgr * [WIP] Switch to new simple module mgr * Add StateMgr and more worker variants * [WIP] Switch more modules * [WIP] Switch more modules * [WIP] swtich more modules * [WIP] switch all SPN modules * [WIP] switch all service modules * [WIP] Convert all workers to the new module system * [WIP] add new task system to module manager * [WIP] Add second take for scheduling workers * [WIP] Add FIXME for bugs in new scheduler * [WIP] Add minor improvements to scheduler * [WIP] Add new worker scheduler * [WIP] Fix more bug related to new module system * [WIP] Fix start handing of the new module system * [WIP] Improve startup process * [WIP] Fix minor issues * [WIP] Fix missing subsystem in settings * [WIP] Initialize managers in constructor * [WIP] Move module event initialization to constrictors * [WIP] Fix setting for enabling and disabling the SPN module * [WIP] Move API registeration into module construction * [WIP] Update states mgr for all modules * [WIP] Add CmdLine operation support * Add state helper methods to module group and instance * Add notification and module status handling to status package * Fix starting issues * Remove pilot widget and update security lock to new status data * Remove debug logs * Improve http server shutdown * Add workaround for cleanly shutting down firewall+netquery * Improve logging * Add syncing states with notifications for new module system * Improve starting, stopping, shutdown; resolve FIXMEs/TODOs * [WIP] Fix most unit tests * Review new module system and fix minor issues * Push shutdown and restart events again via API * Set sleep mode via interface * Update example/template module * [WIP] Fix spn/cabin unit test * Remove deprecated UI elements * Make log output more similar for the logging transition phase * Switch spn hub and observer cmds to new module system * Fix log sources * Make worker mgr less error prone * Fix tests and minor issues * Fix observation hub * Improve shutdown and restart handling * Split up big connection.go source file * Move varint and dsd packages to structures repo * Improve expansion test * Fix linter warnings * Fix interception module on windows * Fix linter errors --------- Co-authored-by: Vladimir Stoilov <vladimir@safing.io>
This commit is contained in:
@@ -2,12 +2,13 @@ package navigator
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/safing/portbase/config"
|
||||
"github.com/safing/portbase/log"
|
||||
"github.com/safing/portbase/modules"
|
||||
"github.com/safing/portmaster/base/config"
|
||||
"github.com/safing/portmaster/base/log"
|
||||
"github.com/safing/portmaster/service/intel/geoip"
|
||||
"github.com/safing/portmaster/service/mgr"
|
||||
"github.com/safing/portmaster/spn/conf"
|
||||
)
|
||||
|
||||
@@ -33,8 +34,27 @@ var (
|
||||
ErrAllPinsDisregarded = errors.New("all pins have been disregarded")
|
||||
)
|
||||
|
||||
type Navigator struct {
|
||||
mgr *mgr.Manager
|
||||
|
||||
instance instance
|
||||
}
|
||||
|
||||
func (n *Navigator) Manager() *mgr.Manager {
|
||||
return n.mgr
|
||||
}
|
||||
|
||||
func (n *Navigator) Start() error {
|
||||
return start()
|
||||
}
|
||||
|
||||
func (n *Navigator) Stop() error {
|
||||
return stop()
|
||||
}
|
||||
|
||||
var (
|
||||
module *modules.Module
|
||||
module *Navigator
|
||||
shimLoaded atomic.Bool
|
||||
|
||||
// Main is the primary map used.
|
||||
Main *Map
|
||||
@@ -44,10 +64,6 @@ var (
|
||||
cfgOptionTrustNodeNodes config.StringArrayOption
|
||||
)
|
||||
|
||||
func init() {
|
||||
module = modules.Register("navigator", prep, start, stop, "terminal", "geoip", "netenv")
|
||||
}
|
||||
|
||||
func prep() error {
|
||||
return registerAPIEndpoints()
|
||||
}
|
||||
@@ -55,65 +71,65 @@ func prep() error {
|
||||
func start() error {
|
||||
Main = NewMap(conf.MainMapName, true)
|
||||
devMode = config.Concurrent.GetAsBool(config.CfgDevModeKey, false)
|
||||
cfgOptionRoutingAlgorithm = config.Concurrent.GetAsString(cfgOptionRoutingAlgorithmKey, DefaultRoutingProfileID)
|
||||
cfgOptionTrustNodeNodes = config.Concurrent.GetAsStringArray(cfgOptionTrustNodeNodesKey, []string{})
|
||||
|
||||
if conf.Integrated() {
|
||||
cfgOptionRoutingAlgorithm = config.Concurrent.GetAsString(cfgOptionRoutingAlgorithmKey, DefaultRoutingProfileID)
|
||||
} else {
|
||||
cfgOptionRoutingAlgorithm = func() string { return DefaultRoutingProfileID }
|
||||
}
|
||||
|
||||
err := registerMapDatabase()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Wait for geoip databases to be ready.
|
||||
// Try again if not yet ready, as this is critical.
|
||||
// The "wait" parameter times out after 1 second.
|
||||
// Allow 30 seconds for both databases to load.
|
||||
geoInitCheck:
|
||||
for i := 0; i < 30; i++ {
|
||||
switch {
|
||||
case !geoip.IsInitialized(false, true): // First, IPv4.
|
||||
case !geoip.IsInitialized(true, true): // Then, IPv6.
|
||||
default:
|
||||
break geoInitCheck
|
||||
module.mgr.Go("initializing hubs", func(wc *mgr.WorkerCtx) error {
|
||||
// Wait for geoip databases to be ready.
|
||||
// Try again if not yet ready, as this is critical.
|
||||
// The "wait" parameter times out after 1 second.
|
||||
// Allow 30 seconds for both databases to load.
|
||||
geoInitCheck:
|
||||
for range 30 {
|
||||
switch {
|
||||
case !geoip.IsInitialized(false, true): // First, IPv4.
|
||||
case !geoip.IsInitialized(true, true): // Then, IPv6.
|
||||
default:
|
||||
break geoInitCheck
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = Main.InitializeFromDatabase()
|
||||
if err != nil {
|
||||
// Wait for three seconds, then try again.
|
||||
time.Sleep(3 * time.Second)
|
||||
err = Main.InitializeFromDatabase()
|
||||
if err != nil {
|
||||
// Even if the init fails, we can try to start without it and get data along the way.
|
||||
log.Warningf("spn/navigator: %s", err)
|
||||
// Wait for three seconds, then try again.
|
||||
time.Sleep(3 * time.Second)
|
||||
err = Main.InitializeFromDatabase()
|
||||
if err != nil {
|
||||
// Even if the init fails, we can try to start without it and get data along the way.
|
||||
log.Warningf("spn/navigator: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
err = Main.RegisterHubUpdateHook()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: delete superseded hubs after x amount of time
|
||||
|
||||
module.NewTask("update states", Main.updateStates).
|
||||
Repeat(1 * time.Hour).
|
||||
Schedule(time.Now().Add(3 * time.Minute))
|
||||
|
||||
module.NewTask("update failing states", Main.updateFailingStates).
|
||||
Repeat(1 * time.Minute).
|
||||
Schedule(time.Now().Add(3 * time.Minute))
|
||||
|
||||
if conf.PublicHub() {
|
||||
// Only measure Hubs on public Hubs.
|
||||
module.NewTask("measure hubs", Main.measureHubs).
|
||||
Repeat(5 * time.Minute).
|
||||
Schedule(time.Now().Add(1 * time.Minute))
|
||||
|
||||
// Only register metrics on Hubs, as they only make sense there.
|
||||
err := registerMetrics()
|
||||
err = Main.RegisterHubUpdateHook()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: delete superseded hubs after x amount of time
|
||||
_ = module.mgr.Delay("update states", 3*time.Minute, Main.updateStates).Repeat(1 * time.Hour)
|
||||
_ = module.mgr.Delay("update failing states", 3*time.Minute, Main.updateFailingStates).Repeat(1 * time.Minute)
|
||||
|
||||
if conf.PublicHub() {
|
||||
// Only measure Hubs on public Hubs.
|
||||
module.mgr.Delay("measure hubs", 5*time.Minute, Main.measureHubs).Repeat(1 * time.Minute)
|
||||
|
||||
// Only register metrics on Hubs, as they only make sense there.
|
||||
err := registerMetrics()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -127,3 +143,22 @@ func stop() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// New returns a new Navigator module.
|
||||
func New(instance instance) (*Navigator, error) {
|
||||
if !shimLoaded.CompareAndSwap(false, true) {
|
||||
return nil, errors.New("only one instance allowed")
|
||||
}
|
||||
m := mgr.New("Navigator")
|
||||
module = &Navigator{
|
||||
mgr: m,
|
||||
instance: instance,
|
||||
}
|
||||
if err := prep(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return module, nil
|
||||
}
|
||||
|
||||
type instance interface{}
|
||||
|
||||
Reference in New Issue
Block a user