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

This commit is contained in:
Shelikhoo 2021-04-28 16:29:19 +01:00
parent 2e26cf6587
commit d48cf1a648
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316
3 changed files with 14 additions and 2 deletions

View File

@ -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
}

View File

@ -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)
}

View File

@ -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)
}