Finalize profile merging, add profile metadata state handling, re-attribute connections after profile deletion
This commit is contained in:
@@ -582,6 +582,11 @@ func GetAllConnections() []*Connection {
|
||||
return conns.list()
|
||||
}
|
||||
|
||||
// GetDNSConnection fetches a DNS Connection from the database.
|
||||
func GetDNSConnection(dnsConnID string) (*Connection, bool) {
|
||||
return dnsConns.get(dnsConnID)
|
||||
}
|
||||
|
||||
// SetLocalIP sets the local IP address together with its network scope. The
|
||||
// connection is not locked for this.
|
||||
func (conn *Connection) SetLocalIP(ip net.IP) {
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/safing/portbase/log"
|
||||
"github.com/safing/portbase/modules"
|
||||
"github.com/safing/portmaster/netenv"
|
||||
"github.com/safing/portmaster/network/state"
|
||||
"github.com/safing/portmaster/profile"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -12,8 +19,14 @@ var (
|
||||
defaultFirewallHandler FirewallHandler
|
||||
)
|
||||
|
||||
// Events.
|
||||
var (
|
||||
ConnectionReattributedEvent = "connection re-attributed"
|
||||
)
|
||||
|
||||
func init() {
|
||||
module = modules.Register("network", prep, start, nil, "base", "netenv", "processes")
|
||||
module.RegisterEvent(ConnectionReattributedEvent, false)
|
||||
}
|
||||
|
||||
// SetDefaultFirewallHandler sets the default firewall handler.
|
||||
@@ -45,5 +58,95 @@ func start() error {
|
||||
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
|
||||
}
|
||||
|
||||
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(_ context.Context, eventData any) error {
|
||||
profileID, ok := eventData.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("event data is not a string: %v", eventData)
|
||||
}
|
||||
profileSource, profileID, ok := strings.Cut(profileID, "/")
|
||||
if !ok {
|
||||
return fmt.Errorf("event data does not seem to be a scoped profile ID: %v", eventData)
|
||||
}
|
||||
|
||||
// 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() {
|
||||
// Check if connection is complete and attributed to the deleted profile.
|
||||
if conn.DataIsComplete() &&
|
||||
conn.ProcessContext.Profile == profileID &&
|
||||
conn.ProcessContext.Source == profileSource {
|
||||
|
||||
reAttributeConnection(ctx, conn)
|
||||
reAttributed++
|
||||
tracer.Debugf("filter: re-attributed %s to %s", conn, conn.process.PrimaryProfileID)
|
||||
}
|
||||
}
|
||||
|
||||
// Re-attribute dns connections.
|
||||
for _, conn := range dnsConns.clone() {
|
||||
// Check if connection is complete and attributed to the deleted profile.
|
||||
if conn.DataIsComplete() &&
|
||||
conn.ProcessContext.Profile == profileID &&
|
||||
conn.ProcessContext.Source == profileSource {
|
||||
|
||||
reAttributeConnection(ctx, conn)
|
||||
reAttributed++
|
||||
tracer.Debugf("filter: re-attributed %s to %s", conn, conn.process.PrimaryProfileID)
|
||||
}
|
||||
}
|
||||
|
||||
tracer.Infof("filter: re-attributed %d connections", reAttributed)
|
||||
return nil
|
||||
}
|
||||
|
||||
func reAttributeConnection(ctx context.Context, conn *Connection) {
|
||||
// Check if data is complete.
|
||||
if !conn.DataIsComplete() {
|
||||
return
|
||||
}
|
||||
|
||||
conn.Lock()
|
||||
defer conn.Unlock()
|
||||
|
||||
// Attempt to assign new profile.
|
||||
err := conn.process.RefetchProfile(ctx)
|
||||
if err != nil {
|
||||
log.Warningf("network: failed to refetch profile for %s: %s", conn, err)
|
||||
return
|
||||
}
|
||||
|
||||
// Set the new process context.
|
||||
conn.ProcessContext = getProcessContext(ctx, conn.process)
|
||||
conn.Save()
|
||||
|
||||
// Trigger event for re-attribution.
|
||||
module.TriggerEvent(ConnectionReattributedEvent, conn.ID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user