mirror of
https://github.com/safing/portmaster
synced 2025-04-21 11:29:09 +00:00
* 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>
164 lines
4 KiB
Go
164 lines
4 KiB
Go
package navigator
|
|
|
|
import (
|
|
"errors"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"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"
|
|
)
|
|
|
|
const (
|
|
// cfgOptionRoutingAlgorithmKey is copied from profile/config.go to avoid import loop.
|
|
cfgOptionRoutingAlgorithmKey = "spn/routingAlgorithm"
|
|
|
|
// cfgOptionRoutingAlgorithmKey is copied from captain/config.go to avoid import loop.
|
|
cfgOptionTrustNodeNodesKey = "spn/trustNodes"
|
|
)
|
|
|
|
var (
|
|
// ErrHomeHubUnset is returned when the Home Hub is required and not set.
|
|
ErrHomeHubUnset = errors.New("map has no Home Hub set")
|
|
|
|
// ErrEmptyMap is returned when the Map is empty.
|
|
ErrEmptyMap = errors.New("map is empty")
|
|
|
|
// ErrHubNotFound is returned when the Hub was not found on the Map.
|
|
ErrHubNotFound = errors.New("hub not found")
|
|
|
|
// ErrAllPinsDisregarded is returned when all pins have been disregarded.
|
|
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 *Navigator
|
|
shimLoaded atomic.Bool
|
|
|
|
// Main is the primary map used.
|
|
Main *Map
|
|
|
|
devMode config.BoolOption
|
|
cfgOptionRoutingAlgorithm config.StringOption
|
|
cfgOptionTrustNodeNodes config.StringArrayOption
|
|
)
|
|
|
|
func prep() error {
|
|
return registerAPIEndpoints()
|
|
}
|
|
|
|
func start() error {
|
|
Main = NewMap(conf.MainMapName, true)
|
|
devMode = config.Concurrent.GetAsBool(config.CfgDevModeKey, false)
|
|
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
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
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
|
|
}
|
|
|
|
func stop() error {
|
|
withdrawMapDatabase()
|
|
|
|
Main.CancelHubUpdateHook()
|
|
Main.SaveMeasuredHubs()
|
|
Main.Close()
|
|
|
|
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{}
|