Reduce open dns requests by better handling dual stack requests

This commit is contained in:
Daniel
2021-10-07 11:12:45 +02:00
parent 77d5b083eb
commit be0a94ba98
2 changed files with 18 additions and 13 deletions

View File

@@ -151,7 +151,7 @@ func handleRequest(ctx context.Context, w dns.ResponseWriter, request *dns.Msg)
// that will happen later anyway. // that will happen later anyway.
case network.VerdictUndecided, network.VerdictAccept: case network.VerdictUndecided, network.VerdictAccept:
// Save the request as open, as we don't know if there will be a connection or not. // Save the request as open, as we don't know if there will be a connection or not.
network.SaveOpenDNSRequest(conn) network.SaveOpenDNSRequest(conn, uint16(q.QType))
firewall.UpdateIPsAndCNAMEs(q, rrCache, conn) firewall.UpdateIPsAndCNAMEs(q, rrCache, conn)
default: default:

View File

@@ -29,33 +29,38 @@ const (
openDNSRequestLimit = 3 * time.Second openDNSRequestLimit = 3 * time.Second
) )
func getDNSRequestCacheKey(pid int, fqdn string) string { func getDNSRequestCacheKey(pid int, fqdn string, qType uint16) string {
return strconv.Itoa(pid) + "/" + fqdn return strconv.Itoa(pid) + "/" + fqdn + dns.Type(qType).String()
} }
func removeOpenDNSRequest(pid int, fqdn string) { func removeOpenDNSRequest(pid int, fqdn string) {
openDNSRequestsLock.Lock() openDNSRequestsLock.Lock()
defer openDNSRequestsLock.Unlock() defer openDNSRequestsLock.Unlock()
key := getDNSRequestCacheKey(pid, fqdn) // Delete PID-specific requests.
_, ok := openDNSRequests[key] delete(openDNSRequests, getDNSRequestCacheKey(pid, fqdn, dns.TypeA))
if ok { delete(openDNSRequests, getDNSRequestCacheKey(pid, fqdn, dns.TypeAAAA))
delete(openDNSRequests, key)
return
}
// If process is known, also check for non-attributed requests.
if pid != process.UnidentifiedProcessID { if pid != process.UnidentifiedProcessID {
// check if there is an open dns request from an unidentified process delete(openDNSRequests, getDNSRequestCacheKey(process.UnidentifiedProcessID, fqdn, dns.TypeA))
delete(openDNSRequests, unidentifiedProcessScopePrefix+fqdn) delete(openDNSRequests, getDNSRequestCacheKey(process.UnidentifiedProcessID, fqdn, dns.TypeAAAA))
} }
} }
// SaveOpenDNSRequest saves a dns request connection that was allowed to proceed. // SaveOpenDNSRequest saves a dns request connection that was allowed to proceed.
func SaveOpenDNSRequest(conn *Connection) { func SaveOpenDNSRequest(conn *Connection, qType uint16) {
openDNSRequestsLock.Lock() openDNSRequestsLock.Lock()
defer openDNSRequestsLock.Unlock() defer openDNSRequestsLock.Unlock()
key := getDNSRequestCacheKey(conn.process.Pid, conn.Entity.Domain) // Only save open A and AAAA requests.
switch qType {
case dns.TypeA, dns.TypeAAAA:
default:
return
}
key := getDNSRequestCacheKey(conn.process.Pid, conn.Entity.Domain, qType)
if existingConn, ok := openDNSRequests[key]; ok { if existingConn, ok := openDNSRequests[key]; ok {
// End previous request and save it. // End previous request and save it.
existingConn.Lock() existingConn.Lock()