Resolve conflicting changes in DNS #309 #341 (#346)

Co-authored-by: yuhan6665 <1588741+yuhan6665@users.noreply.github.com>
This commit is contained in:
Jim Han 2021-03-07 15:12:50 +08:00 committed by GitHub
parent f50eff5ebb
commit d7cd71b741
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 19 deletions

View file

@ -55,19 +55,13 @@ func NewDoHNameServer(url *url.URL, dispatcher routing.Dispatcher, clientIP net.
ForceAttemptHTTP2: true, ForceAttemptHTTP2: true,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
dispatcherCtx := context.Background() 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) dest, err := net.ParseDestination(network + ":" + addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
dispatcherCtx = session.ContextWithContent(dispatcherCtx, &session.Content{Protocol: "tls"})
dispatcherCtx = log.ContextWithAccessMessage(dispatcherCtx, &log.AccessMessage{ dispatcherCtx = log.ContextWithAccessMessage(dispatcherCtx, &log.AccessMessage{
From: "DoH", From: "DoH",
To: s.dohURL, To: s.dohURL,
@ -76,6 +70,12 @@ func NewDoHNameServer(url *url.URL, dispatcher routing.Dispatcher, clientIP net.
}) })
link, err := s.dispatcher.Dispatch(dispatcherCtx, dest) link, err := s.dispatcher.Dispatch(dispatcherCtx, dest)
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -307,7 +307,7 @@ func (s *Server) queryIPTimeout(idx int, client Client, domain string, option dn
Tag: s.tag, Tag: s.tag,
}) })
} }
ctx = internet.ContextWithLookupDomain(ctx, Fqdn(domain)) ctx = internet.ContextWithLookupDomain(ctx, domain)
ips, err := client.QueryIP(ctx, domain, option) ips, err := client.QueryIP(ctx, domain, option)
cancel() cancel()

View file

@ -22,12 +22,12 @@ var (
// InitSystemDialer: It's private method and you are NOT supposed to use this function. // InitSystemDialer: It's private method and you are NOT supposed to use this function.
func InitSystemDialer(dc dns.Client, om outbound.Manager) { func InitSystemDialer(dc dns.Client, om outbound.Manager) {
effectiveSystemDialer.init(dc, om) effectiveSystemDialer.Init(dc, om)
} }
type SystemDialer interface { type SystemDialer interface {
Dial(ctx context.Context, source net.Address, destination net.Destination, sockopt *SocketConfig) (net.Conn, error) Dial(ctx context.Context, source net.Address, destination net.Destination, sockopt *SocketConfig) (net.Conn, error)
init(dc dns.Client, om outbound.Manager) Init(dc dns.Client, om outbound.Manager)
} }
type DefaultSystemDialer struct { type DefaultSystemDialer struct {
@ -63,22 +63,30 @@ func (d *DefaultSystemDialer) lookupIP(domain string, strategy DomainStrategy, l
return nil, nil return nil, nil
} }
var lookup = d.dns.LookupIP var option = dns.IPOption{
IPv4Enable: true,
IPv6Enable: true,
FakeEnable: false,
}
switch { switch {
case strategy == DomainStrategy_USE_IP4 || (localAddr != nil && localAddr.Family().IsIPv4()): case strategy == DomainStrategy_USE_IP4 || (localAddr != nil && localAddr.Family().IsIPv4()):
if lookupIPv4, ok := d.dns.(dns.IPv4Lookup); ok { option = dns.IPOption{
lookup = lookupIPv4.LookupIPv4 IPv4Enable: true,
IPv6Enable: false,
FakeEnable: false,
} }
case strategy == DomainStrategy_USE_IP6 || (localAddr != nil && localAddr.Family().IsIPv6()): case strategy == DomainStrategy_USE_IP6 || (localAddr != nil && localAddr.Family().IsIPv6()):
if lookupIPv4, ok := d.dns.(dns.IPv4Lookup); ok { option = dns.IPOption{
lookup = lookupIPv4.LookupIPv4 IPv4Enable: false,
IPv6Enable: true,
FakeEnable: false,
} }
case strategy == DomainStrategy_AS_IS: case strategy == DomainStrategy_AS_IS:
return nil, nil 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 { func (d *DefaultSystemDialer) canLookupIP(ctx context.Context, dst net.Destination, sockopt *SocketConfig) bool {
@ -184,7 +192,7 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
return dialer.DialContext(ctx, dest.Network.SystemString(), dest.NetAddr()) return dialer.DialContext(ctx, dest.Network.SystemString(), dest.NetAddr())
} }
func (d *DefaultSystemDialer) init(dc dns.Client, om outbound.Manager) { func (d *DefaultSystemDialer) Init(dc dns.Client, om outbound.Manager) {
d.dns = dc d.dns = dc
d.obm = om d.obm = om
} }
@ -249,7 +257,7 @@ func WithAdapter(dialer SystemDialerAdapter) SystemDialer {
} }
} }
func (v *SimpleSystemDialer) init(_ dns.Client, _ outbound.Manager) {} func (v *SimpleSystemDialer) Init(_ dns.Client, _ outbound.Manager) {}
func (v *SimpleSystemDialer) Dial(ctx context.Context, src net.Address, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) { func (v *SimpleSystemDialer) Dial(ctx context.Context, src net.Address, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) {
return v.adapter.Dial(dest.Network.SystemString(), dest.NetAddr()) return v.adapter.Dial(dest.Network.SystemString(), dest.NetAddr())