Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DoH性能问题、DoH下AAAA重新过滤、DNS查询分组订阅 #2212

Merged
merged 2 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat/fix: dns query subscriptions group by A and AAAA. fix problem th…
…at empty result would poll dns server in DoH mode
  • Loading branch information
mzz2017 committed Feb 4, 2020
commit be9ce7fd9d1ca4b21d774f8d2336ed01974a07eb
45 changes: 38 additions & 7 deletions app/dns/dohdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sync"
"sync/atomic"
"time"
dns_feature "v2ray.com/core/features/dns"

"golang.org/x/net/dns/dnsmessage"
"v2ray.com/core/common"
Expand Down Expand Up @@ -213,9 +214,13 @@ func (s *DoHNameServer) updateIP(req *dnsRequest, ipRec *IPRecord) {

if updated {
s.ips[req.domain] = rec
s.pub.Publish(req.domain, nil)
}

switch req.reqType {
case dnsmessage.TypeA:
s.pub.Publish(req.domain+"4", nil)
case dnsmessage.TypeAAAA:
s.pub.Publish(req.domain+"6", nil)
}
s.Unlock()
common.Must(s.cleanup.Start())
}
Expand Down Expand Up @@ -336,12 +341,15 @@ func (s *DoHNameServer) findIPsForDomain(domain string, option IPOption) ([]net.
return nil, lastErr
}

if (option.IPv4Enable && record.A != nil) || (option.IPv6Enable && record.AAAA != nil) {
return nil, dns_feature.ErrEmptyResponse
}

return nil, errRecordNotFound
}

// QueryIP is called from dns.Server->queryIPTimeout
func (s *DoHNameServer) QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error) {

fqdn := Fqdn(domain)

ips, err := s.findIPsForDomain(fqdn, option)
Expand All @@ -350,9 +358,32 @@ func (s *DoHNameServer) QueryIP(ctx context.Context, domain string, option IPOpt
return ips, err
}

sub := s.pub.Subscribe(fqdn)
defer sub.Close()

// ipv4 and ipv6 belong to different subscription groups
var sub4, sub6 *pubsub.Subscriber
if option.IPv4Enable {
sub4 = s.pub.Subscribe(fqdn + "4")
defer sub4.Close()
}
if option.IPv6Enable {
sub6 = s.pub.Subscribe(fqdn + "6")
defer sub6.Close()
}
done := make(chan interface{})
go func() {
if sub4 != nil {
select {
case <-sub4.Wait():
case <-ctx.Done():
}
}
if sub6 != nil {
select {
case <-sub6.Wait():
case <-ctx.Done():
}
}
close(done)
}()
s.sendQuery(ctx, fqdn, option)

for {
Expand All @@ -364,7 +395,7 @@ func (s *DoHNameServer) QueryIP(ctx context.Context, domain string, option IPOpt
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-sub.Wait():
case <-done:
}
}
}
39 changes: 33 additions & 6 deletions app/dns/udpns.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,13 @@ func (s *ClassicNameServer) updateIP(domain string, newRec record) {

if updated {
s.ips[domain] = rec
s.pub.Publish(domain, nil)
}

if newRec.A != nil {
s.pub.Publish(domain+"4", nil)
}
if newRec.AAAA != nil {
s.pub.Publish(domain+"6", nil)
}
s.Unlock()
common.Must(s.cleanup.Start())
}
Expand Down Expand Up @@ -245,9 +249,32 @@ func (s *ClassicNameServer) QueryIP(ctx context.Context, domain string, option I
return ips, err
}

sub := s.pub.Subscribe(fqdn)
defer sub.Close()

// ipv4 and ipv6 belong to different subscription groups
var sub4, sub6 *pubsub.Subscriber
if option.IPv4Enable {
sub4 = s.pub.Subscribe(fqdn + "4")
defer sub4.Close()
}
if option.IPv6Enable {
sub6 = s.pub.Subscribe(fqdn + "6")
defer sub6.Close()
}
done := make(chan interface{})
go func() {
if sub4 != nil {
select {
case <-sub4.Wait():
case <-ctx.Done():
}
}
if sub6 != nil {
select {
case <-sub6.Wait():
case <-ctx.Done():
}
}
close(done)
}()
s.sendQuery(ctx, fqdn, option)

for {
Expand All @@ -259,7 +286,7 @@ func (s *ClassicNameServer) QueryIP(ctx context.Context, domain string, option I
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-sub.Wait():
case <-done:
}
}
}