diff --git a/common/sniff/dns.go b/common/sniff/dns.go index 9b2ad91f..d374e607 100644 --- a/common/sniff/dns.go +++ b/common/sniff/dns.go @@ -11,6 +11,7 @@ import ( C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing/common" "github.com/sagernet/sing/common/buf" + M "github.com/sagernet/sing/common/metadata" "github.com/sagernet/sing/common/task" mDNS "github.com/miekg/dns" @@ -49,5 +50,8 @@ func DomainNameQuery(ctx context.Context, packet []byte) (*adapter.InboundContex if err != nil { return nil, err } + if len(msg.Question) == 0 || msg.Question[0].Qclass != mDNS.ClassINET || !M.IsDomainName(msg.Question[0].Name) { + return nil, os.ErrInvalid + } return &adapter.InboundContext{Protocol: C.ProtocolDNS}, nil } diff --git a/common/sniff/domain.go b/common/sniff/domain.go deleted file mode 100644 index 44021d3b..00000000 --- a/common/sniff/domain.go +++ /dev/null @@ -1,6 +0,0 @@ -package sniff - -import _ "unsafe" // for linkname - -//go:linkname IsDomainName net.isDomainName -func IsDomainName(domain string) bool diff --git a/route/router.go b/route/router.go index ed4c5bf2..df975db3 100644 --- a/route/router.go +++ b/route/router.go @@ -554,7 +554,7 @@ func (r *Router) RouteConnection(ctx context.Context, conn net.Conn, metadata ad if err == nil { metadata.Protocol = sniffMetadata.Protocol metadata.Domain = sniffMetadata.Domain - if metadata.SniffOverrideDestination && sniff.IsDomainName(metadata.Domain) { + if metadata.SniffOverrideDestination && M.IsDomainName(metadata.Domain) { metadata.Destination = M.Socksaddr{ Fqdn: metadata.Domain, Port: metadata.Destination.Port, @@ -631,7 +631,7 @@ func (r *Router) RoutePacketConnection(ctx context.Context, conn N.PacketConn, m if err == nil { metadata.Protocol = sniffMetadata.Protocol metadata.Domain = sniffMetadata.Domain - if metadata.SniffOverrideDestination && sniff.IsDomainName(metadata.Domain) { + if metadata.SniffOverrideDestination && M.IsDomainName(metadata.Domain) { metadata.Destination = M.Socksaddr{ Fqdn: metadata.Domain, Port: metadata.Destination.Port,