Browse Source

fix: make sure the ctx is propagated to connections by detached connection

Shelikhoo 4 years ago
parent
commit
d48cf1a648
3 changed files with 14 additions and 2 deletions
  1. 3 1
      app/dns/nameserver_quic.go
  2. 3 1
      app/dns/nameserver_udp.go
  3. 8 0
      context.go

+ 3 - 1
app/dns/nameserver_quic.go

@@ -9,6 +9,8 @@ import (
 	"sync/atomic"
 	"time"
 
+	core "github.com/v2fly/v2ray-core/v4"
+
 	"github.com/lucas-clemente/quic-go"
 	"golang.org/x/net/dns/dnsmessage"
 	"golang.org/x/net/http2"
@@ -370,7 +372,7 @@ func (s *QUICNameServer) openSession(ctx context.Context) (quic.Session, error)
 		HandshakeIdleTimeout: handshakeIdleTimeout,
 	}
 
-	session, err := quic.DialAddrContext(ctx, s.destination.NetAddr(), tlsConfig.GetTLSConfig(tls.WithNextProto("http/1.1", http2.NextProtoTLS, NextProtoDQ)), quicConfig)
+	session, err := quic.DialAddrContext(core.ToBackgroundDetachedContext(ctx), s.destination.NetAddr(), tlsConfig.GetTLSConfig(tls.WithNextProto("http/1.1", http2.NextProtoTLS, NextProtoDQ)), quicConfig)
 	if err != nil {
 		return nil, err
 	}

+ 3 - 1
app/dns/nameserver_udp.go

@@ -9,6 +9,8 @@ import (
 	"sync/atomic"
 	"time"
 
+	core "github.com/v2fly/v2ray-core/v4"
+
 	"golang.org/x/net/dns/dnsmessage"
 
 	"github.com/v2fly/v2ray-core/v4/common"
@@ -192,7 +194,7 @@ func (s *ClassicNameServer) sendQuery(ctx context.Context, domain string, client
 	for _, req := range reqs {
 		s.addPendingRequest(req)
 		b, _ := dns.PackMessage(req.msg)
-		udpCtx := ctx
+		udpCtx := core.ToBackgroundDetachedContext(ctx)
 		if inbound := session.InboundFromContext(ctx); inbound != nil {
 			udpCtx = session.ContextWithInbound(udpCtx, inbound)
 		}

+ 8 - 0
context.go

@@ -61,3 +61,11 @@ func mustToContext(ctx context.Context, v *Instance) context.Context {
 	}
 	return ctx
 }
+
+/* ToBackgroundDetachedContext create a detached context from another context
+   Internal API
+*/
+func ToBackgroundDetachedContext(ctx context.Context) context.Context {
+	instance := MustFromContext(ctx)
+	return toContext(context.Background(), instance)
+}