safing-portmaster/service/network/module.go
Daniel Hååvi 80664d1a27
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>
2024-08-09 18:15:48 +03:00

191 lines
4.8 KiB
Go

package network
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"sync/atomic"
"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"
)
// Events.
const (
ConnectionReattributedEvent = "connection re-attributed"
)
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 {
defaultFirewallHandler = handler
}
}
func prep() error {
if netenv.IPv6Enabled() {
state.EnableTCPDualStack()
state.EnableUDPDualStack()
}
return registerAPIEndpoints()
}
func start() error {
err := registerAsDatabase()
if err != nil {
return err
}
if err := registerMetrics(); 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
}
var reAttributionLock sync.Mutex
// reAttributeConnections finds all connections of a deleted profile and re-attributes them.
// Expected event data: scoped profile ID.
func reAttributeConnections(_ *mgr.WorkerCtx, profileID string) (bool, error) {
profileSource, profileID, ok := strings.Cut(profileID, "/")
if !ok {
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
// same connections and make logging cleaner.
reAttributionLock.Lock()
defer reAttributionLock.Unlock()
// Create tracing context.
ctx, tracer := log.AddTracer(context.Background())
defer tracer.Submit()
tracer.Infof("network: re-attributing connections from deleted profile %s/%s", profileSource, profileID)
// Count and log how many connections were re-attributed.
var reAttributed int
// Re-attribute connections.
for _, conn := range conns.clone() {
if reAttributeConnection(ctx, conn, profileID, profileSource) {
reAttributed++
tracer.Debugf("filter: re-attributed %s to %s", conn, conn.process.PrimaryProfileID)
}
}
// Re-attribute dns connections.
for _, conn := range dnsConns.clone() {
if reAttributeConnection(ctx, conn, profileID, profileSource) {
reAttributed++
tracer.Debugf("filter: re-attributed %s to %s", conn, conn.process.PrimaryProfileID)
}
}
tracer.Infof("filter: re-attributed %d connections", reAttributed)
return false, nil
}
func reAttributeConnection(ctx context.Context, conn *Connection, profileID, profileSource string) (reAttributed bool) {
// Lock the connection before checking anything to avoid a race condition with connection data collection.
conn.Lock()
defer conn.Unlock()
// Check if the connection has the profile we are looking for.
switch {
case !conn.DataIsComplete():
return false
case conn.ProcessContext.Profile != profileID:
return false
case conn.ProcessContext.Source != profileSource:
return false
}
// Attempt to assign new profile.
err := conn.process.RefetchProfile(ctx)
if err != nil {
log.Tracer(ctx).Warningf("network: failed to refetch profile for %s: %s", conn, err)
return false
}
// Set the new process context.
conn.ProcessContext = getProcessContext(ctx, conn.process)
conn.Save()
// Trigger event for re-attribution.
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
}