diff --git a/adapter/inbound.go b/adapter/inbound.go index 938c5f84..3027f1eb 100644 --- a/adapter/inbound.go +++ b/adapter/inbound.go @@ -6,6 +6,7 @@ import ( "net/netip" "time" + "github.com/sagernet/sing-box/common/interrupt" "github.com/sagernet/sing-box/common/tlsspoof" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/log" @@ -196,6 +197,7 @@ func ContextForMultiplexSession(ctx context.Context) context.Context { sessionContext.Outbound = metadata.Outbound } ctx = ContextWithDNSTransportTag(ctx, "") + ctx = interrupt.ContextWithIsExternalConnection(ctx, false) return WithContext(ctx, &sessionContext) } diff --git a/common/interrupt/context.go b/common/interrupt/context.go index 44726b2d..82dc2e7b 100644 --- a/common/interrupt/context.go +++ b/common/interrupt/context.go @@ -4,10 +4,11 @@ import "context" type contextKeyIsExternalConnection struct{} -func ContextWithIsExternalConnection(ctx context.Context) context.Context { - return context.WithValue(ctx, contextKeyIsExternalConnection{}, true) +func ContextWithIsExternalConnection(ctx context.Context, isExternal bool) context.Context { + return context.WithValue(ctx, contextKeyIsExternalConnection{}, isExternal) } func IsExternalConnectionFromContext(ctx context.Context) bool { - return ctx.Value(contextKeyIsExternalConnection{}) != nil + isExternal, _ := ctx.Value(contextKeyIsExternalConnection{}).(bool) + return isExternal } diff --git a/dns/client.go b/dns/client.go index 654fc3ae..51cedd60 100644 --- a/dns/client.go +++ b/dns/client.go @@ -12,6 +12,7 @@ import ( "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/common/compatible" + "github.com/sagernet/sing-box/common/interrupt" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing/common" E "github.com/sagernet/sing/common/exceptions" @@ -348,7 +349,7 @@ func (c *Client) beginExchange(ctx context.Context, transport adapter.DNSTranspo operation.release() return nil, nil, exchangeDone, E.New("DNS query loopback in transport[", contextTransport, "]") } - operation.ctx = adapter.ContextWithDNSTransportTag(ctx, transport.Tag()) + operation.ctx = adapter.ContextWithDNSTransportTag(interrupt.ContextWithIsExternalConnection(ctx, false), transport.Tag()) if !disableCache && responseChecker != nil && c.rdrc != nil { rejected := c.rdrc.LoadRDRC(transport.Tag(), question.Name, question.Qtype) if rejected { diff --git a/protocol/snell/outbound.go b/protocol/snell/outbound.go index eb41c362..3da966a3 100644 --- a/protocol/snell/outbound.go +++ b/protocol/snell/outbound.go @@ -112,6 +112,9 @@ func (h *Outbound) DialContext(ctx context.Context, network string, destination switch networkName { case N.NetworkTCP: h.logger.InfoContext(ctx, "outbound connection to ", destination) + if h.reuse { + return h.client.DialContext(adapter.ContextForMultiplexSession(ctx), destination) + } return h.client.DialContext(ctx, destination) case N.NetworkUDP: h.logger.InfoContext(ctx, "outbound packet connection to ", destination) diff --git a/protocol/ssh/outbound.go b/protocol/ssh/outbound.go index 7e9aa0ac..a9ff8116 100644 --- a/protocol/ssh/outbound.go +++ b/protocol/ssh/outbound.go @@ -141,7 +141,7 @@ func (s *Outbound) connect(ctx context.Context) (client *ssh.Client, err error) return s.client, nil } - conn, err := s.dialer.DialContext(ctx, N.NetworkTCP, s.serverAddr) + conn, err := s.dialer.DialContext(adapter.ContextForMultiplexSession(ctx), N.NetworkTCP, s.serverAddr) if err != nil { return nil, err } diff --git a/route/route.go b/route/route.go index 5fd88382..4155a866 100644 --- a/route/route.go +++ b/route/route.go @@ -164,7 +164,7 @@ func (r *Router) routeConnection(ctx context.Context, conn net.Conn, metadata ad for _, tracker := range r.trackers { conn = tracker.RoutedConnection(ctx, conn, metadata, selectedRule, selectedOutbound) } - ctx = interrupt.ContextWithIsExternalConnection(ctx) + ctx = interrupt.ContextWithIsExternalConnection(ctx, true) onClose = registerInterrupt(chain, conn, onClose) outbound := chain[len(chain)-1] if outboundHandler, isHandler := outbound.(adapter.ConnectionHandler); isHandler { @@ -333,7 +333,7 @@ func (r *Router) routePacketConnection(ctx context.Context, conn N.PacketConn, m if metadata.FakeIP { conn = newFakeIPNATPacketConn(bufio.NewNetPacketConn(conn), metadata.OriginDestination, metadata.Destination) } - ctx = interrupt.ContextWithIsExternalConnection(ctx) + ctx = interrupt.ContextWithIsExternalConnection(ctx, true) onClose = registerInterrupt(chain, conn, onClose) outbound := chain[len(chain)-1] if outboundHandler, isHandler := outbound.(adapter.PacketConnectionHandler); isHandler { diff --git a/transport/v2raygrpclite/client.go b/transport/v2raygrpclite/client.go index 62163e44..3ac0404c 100644 --- a/transport/v2raygrpclite/client.go +++ b/transport/v2raygrpclite/client.go @@ -67,7 +67,7 @@ func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, opt } if tlsConfig == nil { client.transport.DialTLSContext = func(ctx context.Context, network, addr string, cfg *tls.STDConfig) (net.Conn, error) { - return dialer.DialContext(ctx, network, M.ParseSocksaddr(addr)) + return dialer.DialContext(adapter.ContextForMultiplexSession(ctx), network, M.ParseSocksaddr(addr)) } } else { if len(tlsConfig.NextProtos()) == 0 { @@ -75,7 +75,7 @@ func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, opt } tlsDialer := tls.NewDialer(dialer, tlsConfig) client.transport.DialTLSContext = func(ctx context.Context, network, addr string, cfg *tls.STDConfig) (net.Conn, error) { - return tlsDialer.DialTLSContext(ctx, M.ParseSocksaddr(addr)) + return tlsDialer.DialTLSContext(adapter.ContextForMultiplexSession(ctx), M.ParseSocksaddr(addr)) } } diff --git a/transport/v2rayhttp/client.go b/transport/v2rayhttp/client.go index 21554e6a..b831086d 100644 --- a/transport/v2rayhttp/client.go +++ b/transport/v2rayhttp/client.go @@ -57,7 +57,7 @@ func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, opt ReadIdleTimeout: time.Duration(options.IdleTimeout), PingTimeout: time.Duration(options.PingTimeout), DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.STDConfig) (net.Conn, error) { - return tlsDialer.DialTLSContext(ctx, M.ParseSocksaddr(addr)) + return tlsDialer.DialTLSContext(adapter.ContextForMultiplexSession(ctx), M.ParseSocksaddr(addr)) }, } }