Skip to content

Commit

Permalink
Improve DNS truncate behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Feb 24, 2024
1 parent 498f63b commit 43ef2af
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
37 changes: 37 additions & 0 deletions client_truncate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dns

import "github.com/miekg/dns"

func TruncateDNSMessage(request *dns.Msg, response *dns.Msg) (*dns.Msg, int) {
maxLen := 512
if edns0Option := request.IsEdns0(); edns0Option != nil {
if udpSize := int(edns0Option.UDPSize()); udpSize > 0 {
maxLen = udpSize
}
}
return truncateDNSMessage(response, maxLen)
}

func truncateDNSMessage(response *dns.Msg, maxLen int) (*dns.Msg, int) {
responseLen := response.Len()
if responseLen <= maxLen {
return response, responseLen
}
newResponse := *response
response = &newResponse
response.Compress = true
responseLen = response.Len()
if responseLen <= maxLen {
return response, responseLen
}
for len(response.Answer) > 0 && responseLen > maxLen {
response.Answer = response.Answer[:len(response.Answer)-1]
response.Truncated = true
responseLen = response.Len()
}
if responseLen > maxLen {
response.Ns = nil
response.Extra = nil
}
return response, response.Len()
}
1 change: 1 addition & 0 deletions transport_https.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (t *HTTPSTransport) Start() error {

func (t *HTTPSTransport) Reset() {
t.transport.CloseIdleConnections()
t.transport = t.transport.Clone()
}

func (t *HTTPSTransport) Close() error {
Expand Down
21 changes: 21 additions & 0 deletions transport_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"

"github.com/miekg/dns"
Expand All @@ -25,6 +26,8 @@ func init() {

type UDPTransport struct {
myTransportAdapter
tcpTransport *TCPTransport
logger logger.ContextLogger
}

func NewUDPTransport(options TransportOptions) (*UDPTransport, error) {
Expand All @@ -40,13 +43,31 @@ func NewUDPTransport(options TransportOptions) (*UDPTransport, error) {
if serverAddr.Port == 0 {
serverAddr.Port = 53
}
tcpTransport, err := NewTCPTransport(options)
if err != nil {
return nil, err
}
transport := &UDPTransport{
newAdapter(options, serverAddr),
tcpTransport,
options.Logger,
}
transport.handler = transport
return transport, nil
}

func (t *UDPTransport) Exchange(ctx context.Context, message *dns.Msg) (*dns.Msg, error) {
response, err := t.myTransportAdapter.Exchange(ctx, message)
if err != nil {
return nil, err
}
if response.Truncated {
t.logger.InfoContext(ctx, "response truncated, retrying with TCP")
return t.tcpTransport.Exchange(ctx, message)
}
return response, nil
}

func (t *UDPTransport) DialContext(ctx context.Context) (net.Conn, error) {
return t.dialer.DialContext(ctx, "udp", t.serverAddr)
}
Expand Down

0 comments on commit 43ef2af

Please sign in to comment.