0
0
mirror of https://github.com/XTLS/Xray-core.git synced 2025-08-29 18:05:31 +00:00

Fix context passing

This commit is contained in:
yuhan6665 2025-08-10 20:42:13 -04:00
parent cca0c446d0
commit 0271f2fbbc

View File

@ -31,6 +31,7 @@ type Server struct {
} }
type routingInfo struct { type routingInfo struct {
ctx context.Context
dispatcher routing.Dispatcher dispatcher routing.Dispatcher
inboundTag *session.Inbound inboundTag *session.Inbound
contentTag *session.Content contentTag *session.Content
@ -78,8 +79,10 @@ func (*Server) Network() []net.Network {
// Process implements proxy.Inbound. // Process implements proxy.Inbound.
func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Connection, dispatcher routing.Dispatcher) error { func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Connection, dispatcher routing.Dispatcher) error {
s.info = routingInfo{ s.info = routingInfo{
dispatcher: dispatcher, ctx: ctx,
inboundTag: session.InboundFromContext(ctx), dispatcher: dispatcher,
inboundTag: session.InboundFromContext(ctx),
contentTag: session.ContentFromContext(ctx),
} }
ep, err := s.bindServer.ParseEndpoint(conn.RemoteAddr().String()) ep, err := s.bindServer.ParseEndpoint(conn.RemoteAddr().String())
@ -118,12 +121,12 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Con
func (s *Server) forwardConnection(dest net.Destination, conn net.Conn) { func (s *Server) forwardConnection(dest net.Destination, conn net.Conn) {
if s.info.dispatcher == nil { if s.info.dispatcher == nil {
errors.LogError(context.Background(), "unexpected: dispatcher == nil") errors.LogError(s.info.ctx, "unexpected: dispatcher == nil")
return return
} }
defer conn.Close() defer conn.Close()
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(core.ToBackgroundDetachedContext(s.info.ctx))
sid := session.NewID() sid := session.NewID()
ctx = c.ContextWithID(ctx, sid) ctx = c.ContextWithID(ctx, sid)
inbound := session.Inbound{} // since promiscuousModeHandler mixed-up context, we shallow copy inbound (tag) and content (configs) inbound := session.Inbound{} // since promiscuousModeHandler mixed-up context, we shallow copy inbound (tag) and content (configs)