Skip to content

Commit

Permalink
Split FixedResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Nov 4, 2024
1 parent af17d0a commit 7e89046
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
36 changes: 1 addition & 35 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,47 +520,13 @@ func (c *Client) exchangeToLookup(ctx context.Context, transport Transport, mess
if err != nil {
return nil, wrapError(err)
}
response := dns.Msg{
MsgHdr: dns.MsgHdr{
Id: message.Id,
Rcode: dns.RcodeSuccess,
Response: true,
},
Question: message.Question,
}
var timeToLive uint32
if rewriteTTL, loaded := RewriteTTLFromContext(ctx); loaded {
timeToLive = rewriteTTL
} else {
timeToLive = DefaultTTL
}
for _, address := range result {
if address.Is4In6() {
address = netip.AddrFrom4(address.As4())
}
if address.Is4() {
response.Answer = append(response.Answer, &dns.A{
Hdr: dns.RR_Header{
Name: question.Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: timeToLive,
},
A: address.AsSlice(),
})
} else {
response.Answer = append(response.Answer, &dns.AAAA{
Hdr: dns.RR_Header{
Name: question.Name,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: timeToLive,
},
AAAA: address.AsSlice(),
})
}
}
return &response, nil
return FixedResponse(message.Id, question, result, timeToLive), nil
}

func (c *Client) lookupToExchange(ctx context.Context, transport Transport, name string, qType uint16, strategy DomainStrategy, responseChecker func(responseAddrs []netip.Addr) bool) ([]netip.Addr, error) {
Expand Down
42 changes: 42 additions & 0 deletions transport_hosts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dns

import (
"net/netip"

"github.com/miekg/dns"
)

func FixedResponse(id uint16, question dns.Question, addresses []netip.Addr, timeToLive uint32) *dns.Msg {
response := dns.Msg{
MsgHdr: dns.MsgHdr{
Id: id,
Rcode: dns.RcodeSuccess,
Response: true,
},
Question: []dns.Question{question},
}
for _, address := range addresses {
if address.Is4() {
response.Answer = append(response.Answer, &dns.A{
Hdr: dns.RR_Header{
Name: question.Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: timeToLive,
},
A: address.AsSlice(),
})
} else {
response.Answer = append(response.Answer, &dns.AAAA{
Hdr: dns.RR_Header{
Name: question.Name,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: timeToLive,
},
AAAA: address.AsSlice(),
})
}
}
return &response
}
2 changes: 2 additions & 0 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

func TestTransports(t *testing.T) {
t.Parallel()
serverAddressList := []string{
"114.114.114.114",
"tcp://114.114.114.114",
Expand All @@ -24,6 +25,7 @@ func TestTransports(t *testing.T) {
for _, serverAddressItem := range serverAddressList {
serverAddress := serverAddressItem
t.Run(serverAddress, func(t *testing.T) {
t.Parallel()
transport, err := dns.CreateTransport(dns.TransportOptions{
Context: context.Background(),
Logger: logger.NOP(),
Expand Down

0 comments on commit 7e89046

Please sign in to comment.