Skip to content

Commit

Permalink
Resolve conflicting change in DNS #309 #341
Browse files Browse the repository at this point in the history
Co-Authored-By: yuhan6665 <1588741+yuhan6665@users.noreply.github.com>
  • Loading branch information
2 people authored and JimhHan committed Mar 7, 2021
1 parent f50eff5 commit a107a77
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
14 changes: 7 additions & 7 deletions app/dns/dohdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,13 @@ func NewDoHNameServer(url *url.URL, dispatcher routing.Dispatcher, clientIP net.
ForceAttemptHTTP2: true,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
dispatcherCtx := context.Background()
if inbound := session.InboundFromContext(ctx); inbound != nil {
dispatcherCtx = session.ContextWithInbound(dispatcherCtx, inbound)
}
if content := session.ContentFromContext(ctx); content != nil {
dispatcherCtx = session.ContextWithContent(dispatcherCtx, content)
}
dispatcherCtx = internet.ContextWithLookupDomain(dispatcherCtx, internet.LookupDomainFromContext(ctx))

dest, err := net.ParseDestination(network + ":" + addr)
if err != nil {
return nil, err
}

dispatcherCtx = session.ContextWithContent(dispatcherCtx, &session.Content{Protocol: "tls"})
dispatcherCtx = log.ContextWithAccessMessage(dispatcherCtx, &log.AccessMessage{
From: "DoH",
To: s.dohURL,
Expand All @@ -76,6 +70,12 @@ func NewDoHNameServer(url *url.URL, dispatcher routing.Dispatcher, clientIP net.
})

link, err := s.dispatcher.Dispatch(dispatcherCtx, dest)
select {
case <-ctx.Done():
return nil, ctx.Err()
default:

}
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions app/dns/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Server struct {
sync.Mutex
hosts *StaticHosts
clientIP net.IP
clients []Client // clientIdx -> Client
clients []Client // clientIdx -> Client
ctx context.Context
ipIndexMap []*MultiGeoIPMatcher // clientIdx -> *MultiGeoIPMatcher
domainRules [][]string // clientIdx -> domainRuleIdx -> DomainRule
Expand Down Expand Up @@ -307,7 +307,7 @@ func (s *Server) queryIPTimeout(idx int, client Client, domain string, option dn
Tag: s.tag,
})
}
ctx = internet.ContextWithLookupDomain(ctx, Fqdn(domain))
ctx = internet.ContextWithLookupDomain(ctx, domain)
ips, err := client.QueryIP(ctx, domain, option)
cancel()

Expand Down
20 changes: 14 additions & 6 deletions transport/internet/system_dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,30 @@ func (d *DefaultSystemDialer) lookupIP(domain string, strategy DomainStrategy, l
return nil, nil
}

var lookup = d.dns.LookupIP
var option = dns.IPOption{
IPv4Enable: true,
IPv6Enable: true,
FakeEnable: false,
}

switch {
case strategy == DomainStrategy_USE_IP4 || (localAddr != nil && localAddr.Family().IsIPv4()):
if lookupIPv4, ok := d.dns.(dns.IPv4Lookup); ok {
lookup = lookupIPv4.LookupIPv4
option = dns.IPOption{
IPv4Enable: true,
IPv6Enable: false,
FakeEnable: false,
}
case strategy == DomainStrategy_USE_IP6 || (localAddr != nil && localAddr.Family().IsIPv6()):
if lookupIPv4, ok := d.dns.(dns.IPv4Lookup); ok {
lookup = lookupIPv4.LookupIPv4
option = dns.IPOption{
IPv4Enable: false,
IPv6Enable: true,
FakeEnable: false,
}
case strategy == DomainStrategy_AS_IS:
return nil, nil
}

return lookup(domain)
return d.dns.LookupIP(domain, option)
}

func (d *DefaultSystemDialer) canLookupIP(ctx context.Context, dst net.Destination, sockopt *SocketConfig) bool {
Expand Down

0 comments on commit a107a77

Please sign in to comment.