dae/control/dns_cache.go

62 lines
1.2 KiB
Go
Raw Permalink Normal View History

2023-02-25 01:38:21 +07:00
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (c) 2022-2024, daeuniverse Organization <dae@v2raya.org>
2023-02-25 01:38:21 +07:00
*/
package control
import (
"net/netip"
"time"
dnsmessage "github.com/miekg/dns"
"github.com/mohae/deepcopy"
2023-02-25 01:38:21 +07:00
)
type DnsCache struct {
DomainBitmap []uint32
Answer []dnsmessage.RR
Deadline time.Time
OriginalDeadline time.Time // This field is not impacted by `fixed_domain_ttl`.
2023-02-25 01:38:21 +07:00
}
func (c *DnsCache) FillInto(req *dnsmessage.Msg) {
req.Answer = deepcopy.Copy(c.Answer).([]dnsmessage.RR)
req.Rcode = dnsmessage.RcodeSuccess
2023-02-25 01:38:21 +07:00
req.Response = true
req.RecursionAvailable = true
req.Truncated = false
}
func (c *DnsCache) IncludeIp(ip netip.Addr) bool {
for _, ans := range c.Answer {
switch body := ans.(type) {
case *dnsmessage.A:
2023-02-25 01:38:21 +07:00
if !ip.Is4() {
continue
}
if a, ok := netip.AddrFromSlice(body.A); ok && a == ip {
2023-02-25 01:38:21 +07:00
return true
}
case *dnsmessage.AAAA:
2023-02-25 01:38:21 +07:00
if !ip.Is6() {
continue
}
if a, ok := netip.AddrFromSlice(body.AAAA); ok && a == ip {
2023-02-25 01:38:21 +07:00
return true
}
}
}
return false
}
func (c *DnsCache) IncludeAnyIp() bool {
for _, ans := range c.Answer {
switch ans.(type) {
case *dnsmessage.A, *dnsmessage.AAAA:
return true
}
}
return false
}