From 42524ba04e4851355c40e76df971abe2be2f538d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 17 Sep 2022 16:53:40 +0800 Subject: [PATCH] Fix dns sniffer --- common/sniff/dns.go | 4 ++++ common/sniff/domain.go | 6 ------ route/router.go | 4 ++-- 3 files changed, 6 insertions(+), 8 deletions(-) delete mode 100644 common/sniff/domain.go 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,