Skip to content

Commit 59a106b

Browse files
committedJan 27, 2025··
Merge branch 'develop' into feature/new-installer

File tree

7 files changed

+35
-15
lines changed

7 files changed

+35
-15
lines changed
 

‎Earthfile

+1
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ rust-base:
425425

426426
DO rust+INIT --keep_fingerprints=true
427427

428+
# For now we need tauri-cli 2.0.0 for bulding
428429
DO rust+CARGO --args="install tauri-cli --version 2.1.0 --locked"
429430

430431
# Explicitly cache here.

‎service/firewall/interception/dnsmonitor/etwlink_windows.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ type ETWSession struct {
2222
state uintptr
2323
}
2424

25-
// NewSession creates new ETW event listener and initilizes it. This is a low level interface, make sure to call DestorySession when you are done using it.
26-
func NewSession(etwInterface *integration.ETWFunctions, callback func(domain string, result string)) (*ETWSession, error) {
25+
// NewSession creates new ETW event listener and initializes it. This is a low level interface, make sure to call DestroySession when you are done using it.
26+
func NewSession(etwInterface *integration.ETWFunctions, callback func(domain string, pid uint32, result string)) (*ETWSession, error) {
2727
if etwInterface == nil {
2828
return nil, fmt.Errorf("etw interface was nil")
2929
}
@@ -35,8 +35,8 @@ func NewSession(etwInterface *integration.ETWFunctions, callback func(domain str
3535
_ = etwSession.i.StopOldSession()
3636

3737
// Initialize notification activated callback
38-
win32Callback := windows.NewCallback(func(domain *uint16, result *uint16) uintptr {
39-
callback(windows.UTF16PtrToString(domain), windows.UTF16PtrToString(result))
38+
win32Callback := windows.NewCallback(func(domain *uint16, pid uint32, result *uint16) uintptr {
39+
callback(windows.UTF16PtrToString(domain), pid, windows.UTF16PtrToString(result))
4040
return 0
4141
})
4242
// The function only allocates memory it will not fail.
@@ -83,7 +83,7 @@ func (l *ETWSession) FlushTrace() error {
8383
return l.i.FlushTrace(l.state)
8484
}
8585

86-
// StopTrace stopes the trace. This will cause StartTrace to return.
86+
// StopTrace stops the trace. This will cause StartTrace to return.
8787
func (l *ETWSession) StopTrace() error {
8888
return l.i.StopTrace(l.state)
8989
}

‎service/firewall/interception/dnsmonitor/eventlistener_linux.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,5 @@ func (l *Listener) processAnswer(domain string, queryResult *QueryResult) {
141141
}
142142
}
143143

144-
saveDomain(domain, ips, cnames)
144+
saveDomain(domain, ips, cnames, resolver.IPInfoProfileScopeGlobal)
145145
}

‎service/firewall/interception/dnsmonitor/eventlistener_windows.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
package dnsmonitor
55

66
import (
7+
"context"
78
"fmt"
89
"net"
910
"strconv"
1011
"strings"
1112

1213
"github.com/miekg/dns"
1314
"github.com/safing/portmaster/service/mgr"
15+
"github.com/safing/portmaster/service/process"
1416
"github.com/safing/portmaster/service/resolver"
1517
)
1618

@@ -79,7 +81,7 @@ func (l *Listener) stop() error {
7981
return nil
8082
}
8183

82-
func (l *Listener) processEvent(domain string, result string) {
84+
func (l *Listener) processEvent(domain string, pid uint32, result string) {
8385
if processIfSelfCheckDomain(dns.Fqdn(domain)) {
8486
// Not need to process result.
8587
return
@@ -90,6 +92,15 @@ func (l *Listener) processEvent(domain string, result string) {
9092
return
9193
}
9294

95+
profileScope := resolver.IPInfoProfileScopeGlobal
96+
// Get the profile ID if the process can be found
97+
if proc, err := process.GetOrFindProcess(context.Background(), int(pid)); err == nil {
98+
if profile := proc.Profile(); profile != nil {
99+
if localProfile := profile.LocalProfile(); localProfile != nil {
100+
profileScope = localProfile.ID
101+
}
102+
}
103+
}
93104
cnames := make(map[string]string)
94105
ips := []net.IP{}
95106

@@ -115,5 +126,5 @@ func (l *Listener) processEvent(domain string, result string) {
115126
}
116127
}
117128
}
118-
saveDomain(domain, ips, cnames)
129+
saveDomain(domain, ips, cnames, profileScope)
119130
}

‎service/firewall/interception/dnsmonitor/module.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (dl *DNSMonitor) Flush() error {
6161
return dl.listener.flush()
6262
}
6363

64-
func saveDomain(domain string, ips []net.IP, cnames map[string]string) {
64+
func saveDomain(domain string, ips []net.IP, cnames map[string]string, profileScope string) {
6565
fqdn := dns.Fqdn(domain)
6666
// Create new record for this IP.
6767
record := resolver.ResolvedDomain{
@@ -75,7 +75,7 @@ func saveDomain(domain string, ips []net.IP, cnames map[string]string) {
7575
record.AddCNAMEs(cnames)
7676

7777
// Add to cache
78-
saveIPsInCache(ips, resolver.IPInfoProfileScopeGlobal, record)
78+
saveIPsInCache(ips, profileScope, record)
7979
}
8080

8181
func New(instance instance) (*DNSMonitor, error) {

‎service/network/connection.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,9 @@ func (conn *Connection) GatherConnectionInfo(pkt packet.Packet) (err error) {
538538

539539
// Find domain and DNS context of entity.
540540
if conn.Entity.Domain == "" && conn.process.Profile() != nil {
541+
profileScope := conn.process.Profile().LocalProfile().ID
541542
// check if we can find a domain for that IP
542-
ipinfo, err := resolver.GetIPInfo(conn.process.Profile().LocalProfile().ID, pkt.Info().RemoteIP().String())
543+
ipinfo, err := resolver.GetIPInfo(profileScope, pkt.Info().RemoteIP().String())
543544
if err != nil {
544545
// Try again with the global scope, in case DNS went through the system resolver.
545546
ipinfo, err = resolver.GetIPInfo(resolver.IPInfoProfileScopeGlobal, pkt.Info().RemoteIP().String())
@@ -555,6 +556,13 @@ func (conn *Connection) GatherConnectionInfo(pkt packet.Packet) (err error) {
555556
// Error flushing, dont try again.
556557
break
557558
}
559+
// Try with profile scope
560+
ipinfo, err = resolver.GetIPInfo(profileScope, pkt.Info().RemoteIP().String())
561+
if err == nil {
562+
log.Tracer(pkt.Ctx()).Debugf("network: found domain with scope (%s) from dnsmonitor after %d tries", profileScope, +1)
563+
break
564+
}
565+
// Try again with the global scope
558566
ipinfo, err = resolver.GetIPInfo(resolver.IPInfoProfileScopeGlobal, pkt.Info().RemoteIP().String())
559567
if err == nil {
560568
log.Tracer(pkt.Ctx()).Debugf("network: found domain from dnsmonitor after %d tries", i+1)

‎windows_core_dll/dllmain.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static const GUID PORTMASTER_ETW_SESSION_GUID = {
2222
#define LOGSESSION_NAME L"PortmasterDNSEventListener"
2323

2424
// Fuction type of the callback that will be called on each event.
25-
typedef uint64_t(*GoEventRecordCallback)(wchar_t* domain, wchar_t* result);
25+
typedef uint64_t(*GoEventRecordCallback)(wchar_t* domain, uint32_t pid, wchar_t* result);
2626

2727
// Holds the state of the ETW Session.
2828
struct ETWSessionState {
@@ -41,7 +41,7 @@ static bool getPropertyValue(PEVENT_RECORD evt, LPWSTR prop, PBYTE* pData) {
4141
DataDescriptor.ArrayIndex = 0;
4242

4343
DWORD PropertySize = 0;
44-
// Check if the data is avaliable and what is the size of it.
44+
// Check if the data is available and what is the size of it.
4545
DWORD status =
4646
TdhGetPropertySize(evt, 0, NULL, 1, &DataDescriptor, &PropertySize);
4747
if (ERROR_SUCCESS != status) {
@@ -79,7 +79,7 @@ static void WINAPI EventRecordCallback(PEVENT_RECORD eventRecord) {
7979
ETWSessionState* state = (ETWSessionState*)eventRecord->UserContext;
8080

8181
if (resultValue != NULL && domainValue != NULL) {
82-
state->callback((wchar_t*)domainValue, (wchar_t*)resultValue);
82+
state->callback((wchar_t*)domainValue, eventRecord->EventHeader.ProcessId, (wchar_t*)resultValue);
8383
}
8484

8585
free(resultValue);
@@ -160,7 +160,7 @@ extern "C" {
160160
EVENT_TRACE_CONTROL_STOP);
161161
}
162162

163-
// PM_ETWFlushTrace Closes the session and frees resourses.
163+
// PM_ETWFlushTrace Closes the session and frees recourses.
164164
__declspec(dllexport) uint32_t PM_ETWDestroySession(ETWSessionState* state) {
165165
if (state == NULL) {
166166
return 1;

0 commit comments

Comments
 (0)
Please sign in to comment.