Skip to content

Commit

Permalink
[service] Fix error on unitilized dns monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
vlabo committed Dec 2, 2024
1 parent 2a9d754 commit ed2338f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
7 changes: 7 additions & 0 deletions service/firewall/interception/dnsmonitor/etwlink_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func (l *ETWSession) IsRunning() bool {

// FlushTrace flushes the trace buffer.
func (l *ETWSession) FlushTrace() error {
if l.i == nil {
return fmt.Errorf("session not initialized")
}

l.shutdownMutex.Lock()
defer l.shutdownMutex.Unlock()

Expand All @@ -86,6 +90,9 @@ func (l *ETWSession) StopTrace() error {

// DestroySession closes the session and frees the allocated memory. Listener cannot be used after this function is called.
func (l *ETWSession) DestroySession() error {
if l.i == nil {
return fmt.Errorf("session not initialized")
}
l.shutdownMutex.Lock()
defer l.shutdownMutex.Unlock()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func initializeSessions(module *DNSMonitor, listener *Listener) error {
}

func (l *Listener) flush() error {
if l.etw == nil {
return fmt.Errorf("etw not initialized")
}
return l.etw.FlushTrace()
}

Expand Down
12 changes: 12 additions & 0 deletions service/integration/integration_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package integration

import (
"fmt"
"sync"

"github.com/safing/portmaster/base/log"
"github.com/safing/portmaster/service/mgr"
Expand All @@ -24,14 +25,25 @@ func (i *OSIntegration) Initialize() error {
if err != nil {
log.Errorf("integration: failed to load dll: %s", err)

callbackLock := sync.Mutex{}
// listen for event from the updater and try to load again if any.
i.instance.Updates().EventResourcesUpdated.AddCallback("core-dll-loader", func(wc *mgr.WorkerCtx, s struct{}) (cancel bool, err error) {
// Make sure no multiple callas are executed at the same time.
callbackLock.Lock()
defer callbackLock.Unlock()

// Try to load again.
err = i.loadDLL()
if err != nil {
log.Errorf("integration: failed to load dll: %s", err)
} else {
log.Info("integration: initialize successful after updater event")
}
return false, nil
})

} else {
log.Info("integration: initialize successful")
}
return nil
}
Expand Down
6 changes: 5 additions & 1 deletion service/network/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,11 @@ func (conn *Connection) GatherConnectionInfo(pkt packet.Packet) (err error) {
if module.instance.Resolver().IsDisabled() && conn.shouldWaitForDomain() {
// Flush the dns listener buffer and try again.
for i := range 4 {
_ = module.instance.DNSMonitor().Flush()
err = module.instance.DNSMonitor().Flush()
if err != nil {
// Error flushing, dont try again.
break
}
ipinfo, err = resolver.GetIPInfo(resolver.IPInfoProfileScopeGlobal, pkt.Info().RemoteIP().String())
if err == nil {
log.Tracer(pkt.Ctx()).Debugf("network: found domain from dnsmonitor after %d tries", i+1)
Expand Down

0 comments on commit ed2338f

Please sign in to comment.