fix: add DNS header to ResolveNetip req

This commit is contained in:
mzz2017 2023-02-09 13:56:21 +08:00
parent 7f5e4f8349
commit f0f68ffb84
2 changed files with 12 additions and 3 deletions

View File

@ -8,9 +8,11 @@ package netutils
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/mzz2017/softwind/pkg/fastrand"
"github.com/mzz2017/softwind/pool" "github.com/mzz2017/softwind/pool"
"golang.org/x/net/dns/dnsmessage" "golang.org/x/net/dns/dnsmessage"
"golang.org/x/net/proxy" "golang.org/x/net/proxy"
"math"
"net/netip" "net/netip"
"strings" "strings"
"sync" "sync"
@ -53,7 +55,14 @@ func ResolveNetip(ctx context.Context, d proxy.Dialer, dns netip.AddrPort, host
return nil, fmt.Errorf("only support to lookup A/AAAA record") return nil, fmt.Errorf("only support to lookup A/AAAA record")
} }
// Build DNS req. // Build DNS req.
builder := dnsmessage.NewBuilder(nil, dnsmessage.Header{}) builder := dnsmessage.NewBuilder(nil, dnsmessage.Header{
ID: uint16(fastrand.Intn(math.MaxUint16 + 1)),
Response: false,
OpCode: 0,
Truncated: false,
RecursionDesired: true,
Authoritative: false,
})
if err = builder.StartQuestions(); err != nil { if err = builder.StartQuestions(); err != nil {
return nil, err return nil, err
} }

View File

@ -194,7 +194,7 @@ func (c *ControlPlane) handlePkt(data []byte, src, dst netip.AddrPort, outboundI
c.log.WithFields(logrus.Fields{ c.log.WithFields(logrus.Fields{
"ipversions": ipversions, "ipversions": ipversions,
"l4protos": l4protos, "l4protos": l4protos,
}).Debugln("Choose DNS path") }).Traceln("Choose DNS path")
// Get the min latency path. // Get the min latency path.
for _, ver := range ipversions { for _, ver := range ipversions {
for _, proto := range l4protos { for _, proto := range l4protos {
@ -208,7 +208,7 @@ func (c *ControlPlane) handlePkt(data []byte, src, dst netip.AddrPort, outboundI
"ver": ver, "ver": ver,
"proto": proto, "proto": proto,
"outbound": outbound.Name, "outbound": outbound.Name,
}).Debugln("Choice") }).Traceln("Choice")
if bestDialer == nil || latency < bestLatency { if bestDialer == nil || latency < bestLatency {
bestDialer = d bestDialer = d
bestLatency = latency bestLatency = latency