mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 14:18:36 +00:00
Fix shared connections inheriting external interrupt mark
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
+2
-1
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
+2
-2
@@ -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 {
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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))
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user