From d48cf1a6480cb12ae58029f71fe48f087159e033 Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Wed, 28 Apr 2021 16:29:19 +0100 Subject: [PATCH] fix: make sure the ctx is propagated to connections by detached connection --- app/dns/nameserver_quic.go | 4 +++- app/dns/nameserver_udp.go | 4 +++- context.go | 8 ++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/dns/nameserver_quic.go b/app/dns/nameserver_quic.go index 7854a5a3e..c55fde3f3 100644 --- a/app/dns/nameserver_quic.go +++ b/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 } diff --git a/app/dns/nameserver_udp.go b/app/dns/nameserver_udp.go index 323009f40..914ac29c7 100644 --- a/app/dns/nameserver_udp.go +++ b/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) } diff --git a/context.go b/context.go index b0b0f8f62..874a8a6ff 100644 --- a/context.go +++ b/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) +}