mirror of
https://github.com/safing/portmaster
synced 2025-04-22 11:59: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
3.5 KiB
Go
164 lines
3.5 KiB
Go
package navigator
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/safing/portmaster/base/database"
|
|
"github.com/safing/portmaster/base/database/iterator"
|
|
"github.com/safing/portmaster/base/database/query"
|
|
"github.com/safing/portmaster/base/database/record"
|
|
"github.com/safing/portmaster/base/database/storage"
|
|
"github.com/safing/portmaster/service/mgr"
|
|
)
|
|
|
|
var mapDBController *database.Controller
|
|
|
|
// StorageInterface provices a storage.Interface to the
|
|
// configuration manager.
|
|
type StorageInterface struct {
|
|
storage.InjectBase
|
|
}
|
|
|
|
// Database prefixes:
|
|
// Pins: map:main/<Hub ID>
|
|
// DNS Requests: network:tree/<PID>/dns/<ID>
|
|
// IP Connections: network:tree/<PID>/ip/<ID>
|
|
|
|
func makeDBKey(mapName, hubID string) string {
|
|
return fmt.Sprintf("map:%s/%s", mapName, hubID)
|
|
}
|
|
|
|
func parseDBKey(key string) (mapName, hubID string) {
|
|
// Split into segments.
|
|
segments := strings.Split(key, "/")
|
|
|
|
// Keys have 1 or 2 segments.
|
|
switch len(segments) {
|
|
case 1:
|
|
return segments[0], ""
|
|
case 2:
|
|
return segments[0], segments[1]
|
|
default:
|
|
return "", ""
|
|
}
|
|
}
|
|
|
|
// Get returns a database record.
|
|
func (s *StorageInterface) Get(key string) (record.Record, error) {
|
|
// Parse key and check if valid.
|
|
mapName, hubID := parseDBKey(key)
|
|
if mapName == "" || hubID == "" {
|
|
return nil, storage.ErrNotFound
|
|
}
|
|
|
|
// Get map.
|
|
m, ok := getMapForAPI(mapName)
|
|
if !ok {
|
|
return nil, storage.ErrNotFound
|
|
}
|
|
|
|
// Get Pin from map.
|
|
pin, ok := m.GetPin(hubID)
|
|
if !ok {
|
|
return nil, storage.ErrNotFound
|
|
}
|
|
return pin.Export(), nil
|
|
}
|
|
|
|
// Query returns a an iterator for the supplied query.
|
|
func (s *StorageInterface) Query(q *query.Query, local, internal bool) (*iterator.Iterator, error) {
|
|
// Parse key and check if valid.
|
|
mapName, _ := parseDBKey(q.DatabaseKeyPrefix())
|
|
if mapName == "" {
|
|
return nil, storage.ErrNotFound
|
|
}
|
|
|
|
// Get map.
|
|
m, ok := getMapForAPI(mapName)
|
|
if !ok {
|
|
return nil, storage.ErrNotFound
|
|
}
|
|
|
|
// Start query worker.
|
|
it := iterator.New()
|
|
module.mgr.Go("map query", func(_ *mgr.WorkerCtx) error {
|
|
s.processQuery(m, q, it)
|
|
return nil
|
|
})
|
|
|
|
return it, nil
|
|
}
|
|
|
|
func (s *StorageInterface) processQuery(m *Map, q *query.Query, it *iterator.Iterator) {
|
|
// Return all matching pins.
|
|
for _, pin := range m.sortedPins(true) {
|
|
export := pin.Export()
|
|
if q.Matches(export) {
|
|
select {
|
|
case it.Next <- export:
|
|
case <-it.Done:
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
it.Finish(nil)
|
|
}
|
|
|
|
func registerMapDatabase() error {
|
|
_, err := database.Register(&database.Database{
|
|
Name: "map",
|
|
Description: "SPN Network Maps",
|
|
StorageType: database.StorageTypeInjected,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
controller, err := database.InjectDatabase("map", &StorageInterface{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
mapDBController = controller
|
|
return nil
|
|
}
|
|
|
|
func withdrawMapDatabase() {
|
|
mapDBController.Withdraw()
|
|
}
|
|
|
|
// PushPinChanges pushes all changed pins to subscribers.
|
|
func (m *Map) PushPinChanges() {
|
|
module.mgr.Go("push pin changes", m.pushPinChangesWorker)
|
|
}
|
|
|
|
func (m *Map) pushPinChangesWorker(ctx *mgr.WorkerCtx) error {
|
|
m.RLock()
|
|
defer m.RUnlock()
|
|
|
|
for _, pin := range m.all {
|
|
if pin.pushChanges.SetToIf(true, false) {
|
|
mapDBController.PushUpdate(pin.Export())
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// pushChange pushes changes of the pin, if the pushChanges flag is set.
|
|
func (pin *Pin) pushChange() {
|
|
// Check before starting the worker.
|
|
if pin.pushChanges.IsNotSet() {
|
|
return
|
|
}
|
|
|
|
// Start worker to push changes.
|
|
module.mgr.Go("push pin change", func(ctx *mgr.WorkerCtx) error {
|
|
if pin.pushChanges.SetToIf(true, false) {
|
|
mapDBController.PushUpdate(pin.Export())
|
|
}
|
|
return nil
|
|
})
|
|
}
|