diff --git a/proxy/wireguard/server.go b/proxy/wireguard/server.go index bdf27568..ecf35541 100644 --- a/proxy/wireguard/server.go +++ b/proxy/wireguard/server.go @@ -7,6 +7,7 @@ import ( "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/buf" + c "github.com/xtls/xray-core/common/ctx" "github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/log" "github.com/xtls/xray-core/common/net" @@ -30,10 +31,8 @@ type Server struct { } type routingInfo struct { - ctx context.Context dispatcher routing.Dispatcher inboundTag *session.Inbound - outboundTag *session.Outbound contentTag *session.Content } @@ -78,18 +77,9 @@ func (*Server) Network() []net.Network { // Process implements proxy.Inbound. func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Connection, dispatcher routing.Dispatcher) error { - inbound := session.InboundFromContext(ctx) - inbound.Name = "wireguard" - inbound.CanSpliceCopy = 3 - outbounds := session.OutboundsFromContext(ctx) - ob := outbounds[len(outbounds)-1] - s.info = routingInfo{ - ctx: core.ToBackgroundDetachedContext(ctx), dispatcher: dispatcher, inboundTag: session.InboundFromContext(ctx), - outboundTag: ob, - contentTag: session.ContentFromContext(ctx), } ep, err := s.bindServer.ParseEndpoint(conn.RemoteAddr().String()) @@ -128,12 +118,26 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Con func (s *Server) forwardConnection(dest net.Destination, conn net.Conn) { if s.info.dispatcher == nil { - errors.LogError(s.info.ctx, "unexpected: dispatcher == nil") + errors.LogError(context.Background(), "unexpected: dispatcher == nil") return } defer conn.Close() - ctx, cancel := context.WithCancel(core.ToBackgroundDetachedContext(s.info.ctx)) + ctx, cancel := context.WithCancel(context.Background()) + sid := session.NewID() + ctx = c.ContextWithID(ctx, sid) + if s.info.inboundTag != nil { + ctx = session.ContextWithInbound(ctx, s.info.inboundTag) + } + if s.info.contentTag != nil { + ctx = session.ContextWithContent(ctx, s.info.contentTag) + } + inbound := session.InboundFromContext(ctx) + inbound.Name = "wireguard" + inbound.CanSpliceCopy = 3 + outbounds := []*session.Outbound{{}} + ctx = session.ContextWithOutbounds(ctx, outbounds) // since promiscuousModeHandler mixed-up context, we need to define new Outbounds here + plcy := s.policyManager.ForLevel(0) timer := signal.CancelAfterInactivity(ctx, cancel, plcy.Timeouts.ConnectionIdle) @@ -144,25 +148,9 @@ func (s *Server) forwardConnection(dest net.Destination, conn net.Conn) { Reason: "", }) - if s.info.inboundTag != nil { - ctx = session.ContextWithInbound(ctx, s.info.inboundTag) - } - - // what's this? - // Session information should not be shared between different connections - // why reuse them in server level? This will cause incorrect destoverride and unexpected routing behavior. - // Disable it temporarily. Maybe s.info should be removed. - - // if s.info.outboundTag != nil { - // ctx = session.ContextWithOutbounds(ctx, []*session.Outbound{s.info.outboundTag}) - // } - // if s.info.contentTag != nil { - // ctx = session.ContextWithContent(ctx, s.info.contentTag) - // } - link, err := s.info.dispatcher.Dispatch(ctx, dest) if err != nil { - errors.LogErrorInner(s.info.ctx, err, "dispatch connection") + errors.LogErrorInner(ctx, err, "dispatch connection") } defer cancel() @@ -188,7 +176,7 @@ func (s *Server) forwardConnection(dest net.Destination, conn net.Conn) { if err := task.Run(ctx, requestDonePost, responseDone); err != nil { common.Interrupt(link.Reader) common.Interrupt(link.Writer) - errors.LogDebugInner(s.info.ctx, err, "connection ends") + errors.LogDebugInner(ctx, err, "connection ends") return } }