From 41d3f314476421b7562193b4a8ccfd6c308c4b36 Mon Sep 17 00:00:00 2001 From: JimhHan <50871214+JimhHan@users.noreply.github.com> Date: Fri, 19 Mar 2021 22:01:39 +0800 Subject: [PATCH] Fix: DNS query log --- app/dns/nameserver_doh.go | 7 +++++++ app/dns/nameserver_quic.go | 2 -- app/dns/nameserver_udp.go | 4 +--- transport/internet/system_dialer.go | 8 ++------ transport/internet/system_dialer_context.go | 18 ------------------ 5 files changed, 10 insertions(+), 29 deletions(-) delete mode 100644 transport/internet/system_dialer_context.go diff --git a/app/dns/nameserver_doh.go b/app/dns/nameserver_doh.go index 2022ad92..bbde1bf8 100644 --- a/app/dns/nameserver_doh.go +++ b/app/dns/nameserver_doh.go @@ -80,6 +80,12 @@ func NewDoHNameServer(url *url.URL, dispatcher routing.Dispatcher) (*DoHNameServ if err != nil { return nil, err } + log.Record(&log.AccessMessage{ + From: "DoH", + To: s.dohURL, + Status: log.AccessAccepted, + Detour: "local", + }) cc := common.ChainedClosable{} if cw, ok := link.Writer.(common.Closable); ok { @@ -372,6 +378,7 @@ func (s *DoHNameServer) QueryIP(ctx context.Context, domain string, clientIP net ips, err := s.findIPsForDomain(fqdn, option) if err != errRecordNotFound { newError(s.name, " cache HIT ", domain, " -> ", ips).Base(err).AtDebug().WriteToLog() + log.Record(&log.DNSLog{s.name, domain, ips, log.DNSCacheHit, 0, err}) return ips, err } } diff --git a/app/dns/nameserver_quic.go b/app/dns/nameserver_quic.go index 919620fe..241f336b 100644 --- a/app/dns/nameserver_quic.go +++ b/app/dns/nameserver_quic.go @@ -20,7 +20,6 @@ import ( "github.com/xtls/xray-core/common/signal/pubsub" "github.com/xtls/xray-core/common/task" dns_feature "github.com/xtls/xray-core/features/dns" - "github.com/xtls/xray-core/transport/internet" "github.com/xtls/xray-core/transport/internet/tls" ) @@ -175,7 +174,6 @@ func (s *QUICNameServer) sendQuery(ctx context.Context, domain string, clientIP if inbound := session.InboundFromContext(ctx); inbound != nil { dnsCtx = session.ContextWithInbound(dnsCtx, inbound) } - dnsCtx = internet.ContextWithLookupDomain(dnsCtx, internet.LookupDomainFromContext(ctx)) dnsCtx = session.ContextWithContent(dnsCtx, &session.Content{ Protocol: "quic", SkipDNSResolve: true, diff --git a/app/dns/nameserver_udp.go b/app/dns/nameserver_udp.go index 9b7f515b..6f9b6404 100644 --- a/app/dns/nameserver_udp.go +++ b/app/dns/nameserver_udp.go @@ -7,8 +7,6 @@ import ( "sync/atomic" "time" - "github.com/xtls/xray-core/transport/internet" - "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/log" "github.com/xtls/xray-core/common/net" @@ -195,7 +193,6 @@ func (s *ClassicNameServer) sendQuery(ctx context.Context, domain string, client if inbound := session.InboundFromContext(ctx); inbound != nil { udpCtx = session.ContextWithInbound(udpCtx, inbound) } - udpCtx = internet.ContextWithLookupDomain(udpCtx, internet.LookupDomainFromContext(ctx)) udpCtx = session.ContextWithContent(udpCtx, &session.Content{ Protocol: "dns", }) @@ -257,6 +254,7 @@ func (s *ClassicNameServer) QueryIP(ctx context.Context, domain string, clientIP ips, err := s.findIPsForDomain(fqdn, option) if err != errRecordNotFound { newError(s.name, " cache HIT ", domain, " -> ", ips).Base(err).AtDebug().WriteToLog() + log.Record(&log.DNSLog{s.name, domain, ips, log.DNSCacheHit, 0, err}) return ips, err } } diff --git a/transport/internet/system_dialer.go b/transport/internet/system_dialer.go index 7256df9f..43c9309f 100644 --- a/transport/internet/system_dialer.go +++ b/transport/internet/system_dialer.go @@ -89,14 +89,10 @@ func (d *DefaultSystemDialer) lookupIP(domain string, strategy DomainStrategy, l return d.dns.LookupIP(domain, option) } -func (d *DefaultSystemDialer) canLookupIP(ctx context.Context, dst net.Destination, sockopt *SocketConfig) bool { +func (d *DefaultSystemDialer) doLookupIP(ctx context.Context, dst net.Destination, sockopt *SocketConfig) bool { if sockopt == nil || dst.Address.Family().IsIP() || d.dns == nil { return false } - if dst.Address.Domain() == LookupDomainFromContext(ctx) { - newError("infinite loop detected").AtError().WriteToLog(session.ExportIDToError(ctx)) - return false - } return sockopt.DomainStrategy != DomainStrategy_AS_IS } @@ -128,7 +124,7 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne } } - if d.canLookupIP(ctx, dest, sockopt) { + if d.doLookupIP(ctx, dest, sockopt) { ips, err := d.lookupIP(dest.Address.String(), sockopt.DomainStrategy, src) if err == nil && len(ips) > 0 { dest.Address = net.IPAddress(ips[dice.Roll(len(ips))]) diff --git a/transport/internet/system_dialer_context.go b/transport/internet/system_dialer_context.go deleted file mode 100644 index abc6c104..00000000 --- a/transport/internet/system_dialer_context.go +++ /dev/null @@ -1,18 +0,0 @@ -package internet - -import "context" - -type systemDialer int - -const systemDialerKey systemDialer = 0 - -func ContextWithLookupDomain(ctx context.Context, domain string) context.Context { - return context.WithValue(ctx, systemDialerKey, domain) -} - -func LookupDomainFromContext(ctx context.Context) string { - if domain, ok := ctx.Value(systemDialerKey).(string); ok { - return domain - } - return "" -}