Skip to content

Commit 956b4ba

Browse files
committed
Switch to miekg/dns v2
1 parent e2d09ac commit 956b4ba

136 files changed

Lines changed: 26281 additions & 415 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dnscrypt-proxy/coldstart.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package main
33
import (
44
"fmt"
55
"net"
6+
"net/netip"
67
"strings"
78
"sync"
89
"time"
910

11+
"codeberg.org/miekg/dns"
12+
"codeberg.org/miekg/dns/rdata"
1013
"github.com/jedisct1/dlog"
11-
"github.com/miekg/dns"
1214
)
1315

1416
type CaptivePortalEntryIPs []net.IP
@@ -25,53 +27,56 @@ func (captivePortalHandler *CaptivePortalHandler) Stop() {
2527
captivePortalHandler.wg.Wait()
2628
}
2729

28-
func (ipsMap *CaptivePortalMap) GetEntry(msg *dns.Msg) (*dns.Question, *CaptivePortalEntryIPs) {
30+
func (ipsMap *CaptivePortalMap) GetEntry(msg *dns.Msg) (dns.RR, *CaptivePortalEntryIPs) {
2931
if len(msg.Question) != 1 {
3032
return nil, nil
3133
}
32-
question := &msg.Question[0]
33-
name, err := NormalizeQName(question.Name)
34+
question := msg.Question[0]
35+
hdr := question.Header()
36+
name, err := NormalizeQName(hdr.Name)
3437
if err != nil {
3538
return nil, nil
3639
}
3740
ips, ok := (*ipsMap)[name]
3841
if !ok {
3942
return nil, nil
4043
}
41-
if question.Qclass != dns.ClassINET {
44+
if hdr.Class != dns.ClassINET {
4245
return nil, nil
4346
}
4447
return question, &ips
4548
}
4649

47-
func HandleCaptivePortalQuery(msg *dns.Msg, question *dns.Question, ips *CaptivePortalEntryIPs) *dns.Msg {
50+
func HandleCaptivePortalQuery(msg *dns.Msg, question dns.RR, ips *CaptivePortalEntryIPs) *dns.Msg {
4851
respMsg := EmptyResponseFromMessage(msg)
4952
ttl := uint32(1)
50-
if question.Qtype == dns.TypeA {
53+
hdr := question.Header()
54+
qtype := dns.RRToType(question)
55+
if qtype == dns.TypeA {
5156
for _, xip := range *ips {
5257
if ip := xip.To4(); ip != nil {
5358
rr := new(dns.A)
54-
rr.Hdr = dns.RR_Header{Name: question.Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: ttl}
55-
rr.A = ip
59+
rr.Hdr = dns.Header{Name: hdr.Name, Class: dns.ClassINET, TTL: ttl}
60+
rr.A = rdata.A{Addr: netip.AddrFrom4([4]byte(ip))}
5661
respMsg.Answer = append(respMsg.Answer, rr)
5762
}
5863
}
59-
} else if question.Qtype == dns.TypeAAAA {
64+
} else if qtype == dns.TypeAAAA {
6065
for _, xip := range *ips {
6166
if xip.To4() == nil {
6267
rr := new(dns.AAAA)
63-
rr.Hdr = dns.RR_Header{Name: question.Name, Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: ttl}
64-
rr.AAAA = xip
68+
rr.Hdr = dns.Header{Name: hdr.Name, Class: dns.ClassINET, TTL: ttl}
69+
rr.AAAA = rdata.AAAA{Addr: netip.AddrFrom16([16]byte(xip.To16()))}
6570
respMsg.Answer = append(respMsg.Answer, rr)
6671
}
6772
}
6873
}
6974

70-
qType, ok := dns.TypeToString[question.Qtype]
75+
qTypeStr, ok := dns.TypeToString[qtype]
7176
if !ok {
72-
qType = fmt.Sprint(question.Qtype)
77+
qTypeStr = fmt.Sprint(qtype)
7378
}
74-
dlog.Infof("Query for captive portal detection: [%v] (%v)", question.Name, qType)
79+
dlog.Infof("Query for captive portal detection: [%v] (%v)", hdr.Name, qTypeStr)
7580
return respMsg
7681
}
7782

@@ -97,7 +102,8 @@ func handleColdStartClient(clientPc *net.UDPConn, cancelChannel chan struct{}, i
97102
}
98103
packet := buffer[:length]
99104
msg := &dns.Msg{}
100-
if err := msg.Unpack(packet); err != nil {
105+
msg.Data = packet
106+
if err := msg.Unpack(); err != nil {
101107
return false
102108
}
103109
question, ips := ipsMap.GetEntry(msg)
@@ -108,8 +114,8 @@ func handleColdStartClient(clientPc *net.UDPConn, cancelChannel chan struct{}, i
108114
if respMsg == nil {
109115
return false
110116
}
111-
if response, err := respMsg.Pack(); err == nil {
112-
clientPc.WriteTo(response, clientAddr)
117+
if err := respMsg.Pack(); err == nil {
118+
clientPc.WriteTo(respMsg.Data, clientAddr)
113119
}
114120
return false
115121
}

dnscrypt-proxy/common.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,38 @@ func InitializePluginLogger(logFile, format string, maxSize, maxAge, maxBackups
337337
}
338338
return nil, ""
339339
}
340+
341+
// reverseAddr returns the in-addr.arpa. or ip6.arpa. hostname of the IP
342+
// address suitable for reverse DNS (PTR) record lookups.
343+
func reverseAddr(addr string) (string, error) {
344+
ip := net.ParseIP(addr)
345+
if ip == nil {
346+
return "", errors.New("unrecognized address: " + addr)
347+
}
348+
if v4 := ip.To4(); v4 != nil {
349+
buf := make([]byte, 0, net.IPv4len*4+len("in-addr.arpa."))
350+
for i := len(v4) - 1; i >= 0; i-- {
351+
buf = strconv.AppendInt(buf, int64(v4[i]), 10)
352+
buf = append(buf, '.')
353+
}
354+
buf = append(buf, "in-addr.arpa."...)
355+
return string(buf), nil
356+
}
357+
// Must be IPv6
358+
const hexDigits = "0123456789abcdef"
359+
buf := make([]byte, 0, net.IPv6len*4+len("ip6.arpa."))
360+
for i := len(ip) - 1; i >= 0; i-- {
361+
v := ip[i]
362+
buf = append(buf, hexDigits[v&0xF], '.', hexDigits[v>>4], '.')
363+
}
364+
buf = append(buf, "ip6.arpa."...)
365+
return string(buf), nil
366+
}
367+
368+
// fqdn returns the fully qualified domain name (with trailing dot)
369+
func fqdn(name string) string {
370+
if len(name) == 0 || name[len(name)-1] == '.' {
371+
return name
372+
}
373+
return name + "."
374+
}

dnscrypt-proxy/dnscrypt_certs.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"strings"
88
"time"
99

10+
"codeberg.org/miekg/dns"
1011
"github.com/jedisct1/dlog"
11-
"github.com/miekg/dns"
1212
"golang.org/x/crypto/ed25519"
1313
)
1414

@@ -40,8 +40,7 @@ func FetchCurrentDNSCryptCert(
4040
if serverName == nil {
4141
serverName = &providerName
4242
}
43-
query := dns.Msg{}
44-
query.SetQuestion(providerName, dns.TypeTXT)
43+
query := dns.NewMsg(providerName, dns.TypeTXT)
4544
if !strings.HasPrefix(providerName, "2.dnscrypt-cert.") {
4645
if relay != nil && !proxy.anonDirectCertFallback {
4746
dlog.Warnf(
@@ -61,7 +60,7 @@ func FetchCurrentDNSCryptCert(
6160
in, rtt, fragmentsBlocked, err := DNSExchange(
6261
proxy,
6362
proto,
64-
&query,
63+
query,
6564
serverAddress,
6665
relay,
6766
serverName,
@@ -78,7 +77,7 @@ func FetchCurrentDNSCryptCert(
7877
for _, answerRr := range in.Answer {
7978
var txt string
8079
if t, ok := answerRr.(*dns.TXT); !ok {
81-
dlog.Noticef("[%v] Extra record of type [%v] found in certificate", *serverName, answerRr.Header().Rrtype)
80+
dlog.Noticef("[%v] Extra record of type [%v] found in certificate", *serverName, dns.RRToType(answerRr))
8281
continue
8382
} else {
8483
txt = strings.Join(t.Txt, "")

0 commit comments

Comments
 (0)