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,33 +2,57 @@ package network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/safing/portbase/log"
|
||||
"github.com/safing/portbase/modules"
|
||||
"github.com/safing/portmaster/base/log"
|
||||
"github.com/safing/portmaster/service/mgr"
|
||||
"github.com/safing/portmaster/service/netenv"
|
||||
"github.com/safing/portmaster/service/network/state"
|
||||
"github.com/safing/portmaster/service/profile"
|
||||
)
|
||||
|
||||
var (
|
||||
module *modules.Module
|
||||
|
||||
defaultFirewallHandler FirewallHandler
|
||||
)
|
||||
|
||||
// Events.
|
||||
var (
|
||||
const (
|
||||
ConnectionReattributedEvent = "connection re-attributed"
|
||||
)
|
||||
|
||||
func init() {
|
||||
module = modules.Register("network", prep, start, nil, "base", "netenv", "processes")
|
||||
module.RegisterEvent(ConnectionReattributedEvent, false)
|
||||
type Network struct {
|
||||
mgr *mgr.Manager
|
||||
instance instance
|
||||
|
||||
dnsRequestTicker *mgr.SleepyTicker
|
||||
connectionCleanerTicker *mgr.SleepyTicker
|
||||
|
||||
EventConnectionReattributed *mgr.EventMgr[string]
|
||||
}
|
||||
|
||||
func (n *Network) Manager() *mgr.Manager {
|
||||
return n.mgr
|
||||
}
|
||||
|
||||
func (n *Network) Start() error {
|
||||
return start()
|
||||
}
|
||||
|
||||
func (n *Network) Stop() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *Network) SetSleep(enabled bool) {
|
||||
if n.dnsRequestTicker != nil {
|
||||
n.dnsRequestTicker.SetSleep(enabled)
|
||||
}
|
||||
if n.connectionCleanerTicker != nil {
|
||||
n.connectionCleanerTicker.SetSleep(enabled)
|
||||
}
|
||||
}
|
||||
|
||||
var defaultFirewallHandler FirewallHandler
|
||||
|
||||
// SetDefaultFirewallHandler sets the default firewall handler.
|
||||
func SetDefaultFirewallHandler(handler FirewallHandler) {
|
||||
if defaultFirewallHandler == nil {
|
||||
@@ -55,17 +79,9 @@ func start() error {
|
||||
return err
|
||||
}
|
||||
|
||||
module.StartServiceWorker("clean connections", 0, connectionCleaner)
|
||||
module.StartServiceWorker("write open dns requests", 0, openDNSRequestWriter)
|
||||
|
||||
if err := module.RegisterEventHook(
|
||||
"profiles",
|
||||
profile.DeletedEvent,
|
||||
"re-attribute connections from deleted profile",
|
||||
reAttributeConnections,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
module.mgr.Go("clean connections", connectionCleaner)
|
||||
module.mgr.Go("write open dns requests", openDNSRequestWriter)
|
||||
module.instance.Profile().EventDelete.AddCallback("re-attribute connections from deleted profile", reAttributeConnections)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -74,14 +90,10 @@ var reAttributionLock sync.Mutex
|
||||
|
||||
// reAttributeConnections finds all connections of a deleted profile and re-attributes them.
|
||||
// Expected event data: scoped profile ID.
|
||||
func reAttributeConnections(_ context.Context, eventData any) error {
|
||||
profileID, ok := eventData.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("event data is not a string: %v", eventData)
|
||||
}
|
||||
func reAttributeConnections(_ *mgr.WorkerCtx, profileID string) (bool, error) {
|
||||
profileSource, profileID, ok := strings.Cut(profileID, "/")
|
||||
if !ok {
|
||||
return fmt.Errorf("event data does not seem to be a scoped profile ID: %v", eventData)
|
||||
return false, fmt.Errorf("event data does not seem to be a scoped profile ID: %v", profileID)
|
||||
}
|
||||
|
||||
// Hold a lock for re-attribution, to prevent simultaneous processing of the
|
||||
@@ -114,7 +126,7 @@ func reAttributeConnections(_ context.Context, eventData any) error {
|
||||
}
|
||||
|
||||
tracer.Infof("filter: re-attributed %d connections", reAttributed)
|
||||
return nil
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func reAttributeConnection(ctx context.Context, conn *Connection, profileID, profileSource string) (reAttributed bool) {
|
||||
@@ -144,8 +156,36 @@ func reAttributeConnection(ctx context.Context, conn *Connection, profileID, pro
|
||||
conn.Save()
|
||||
|
||||
// Trigger event for re-attribution.
|
||||
module.TriggerEvent(ConnectionReattributedEvent, conn.ID)
|
||||
module.EventConnectionReattributed.Submit(conn.ID)
|
||||
|
||||
log.Tracer(ctx).Debugf("filter: re-attributed %s to %s", conn, conn.process.PrimaryProfileID)
|
||||
return true
|
||||
}
|
||||
|
||||
var (
|
||||
module *Network
|
||||
shimLoaded atomic.Bool
|
||||
)
|
||||
|
||||
// New returns a new Network module.
|
||||
func New(instance instance) (*Network, error) {
|
||||
if !shimLoaded.CompareAndSwap(false, true) {
|
||||
return nil, errors.New("only one instance allowed")
|
||||
}
|
||||
m := mgr.New("Network")
|
||||
module = &Network{
|
||||
mgr: m,
|
||||
instance: instance,
|
||||
EventConnectionReattributed: mgr.NewEventMgr[string](ConnectionReattributedEvent, m),
|
||||
}
|
||||
|
||||
if err := prep(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return module, nil
|
||||
}
|
||||
|
||||
type instance interface {
|
||||
Profile() *profile.ProfileModule
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user