Fix URL test failover for routed connections

This commit is contained in:
世界
2026-10-23 19:57:09 +08:00
parent 92fe54be05
commit 5372a195b3
3 changed files with 24 additions and 7 deletions
+1 -1
View File
@@ -223,7 +223,7 @@ func New(options Options) (*Box, error) {
}
service.MustRegister[adapter.DNSRouter](ctx, dnsRouter)
service.MustRegister[adapter.DNSRuleSetUpdateValidator](ctx, dnsRouter)
connectionManager := route.NewConnectionManager(logFactory.NewLogger("connection"))
connectionManager := route.NewConnectionManager(ctx, logFactory.NewLogger("connection"))
service.MustRegister[adapter.ConnectionManager](ctx, connectionManager)
networkManager, err := route.NewNetworkManager(ctx, logFactory.NewLogger("network"), routeOptions, dnsOptions)
if err != nil {
@@ -812,7 +812,7 @@ func newAppleHTTPTestTransport(t *testing.T, server *appleHTTPTestServer, option
func newAppleHTTPTestContext() context.Context {
return service.ContextWith[adapter.ConnectionManager](
context.Background(),
route.NewConnectionManager(log.NewNOPFactory().NewLogger("connection")),
route.NewConnectionManager(context.Background(), log.NewNOPFactory().NewLogger("connection")),
)
}
+22 -5
View File
@@ -6,6 +6,7 @@ import (
"net"
"net/netip"
"os"
"slices"
"strings"
"sync"
"sync/atomic"
@@ -16,6 +17,7 @@ import (
"github.com/sagernet/sing-box/common/sniff"
"github.com/sagernet/sing-box/common/tlsfragment"
"github.com/sagernet/sing-box/common/tlsspoof"
"github.com/sagernet/sing-box/common/urltest"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
@@ -26,19 +28,22 @@ import (
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/x/list"
"github.com/sagernet/sing/service"
)
var _ adapter.ConnectionManager = (*ConnectionManager)(nil)
type ConnectionManager struct {
logger logger.ContextLogger
access sync.Mutex
connections list.List[io.Closer]
logger logger.ContextLogger
urlTestHistory *urltest.HistoryStorage
access sync.Mutex
connections list.List[io.Closer]
}
func NewConnectionManager(logger logger.ContextLogger) *ConnectionManager {
func NewConnectionManager(ctx context.Context, logger logger.ContextLogger) *ConnectionManager {
return &ConnectionManager{
logger: logger,
logger: logger,
urlTestHistory: service.PtrFromContext[urltest.HistoryStorage](ctx),
}
}
@@ -117,6 +122,7 @@ func (m *ConnectionManager) NewConnection(ctx context.Context, this N.Dialer, co
dialerString = " using outbound/" + outbound.Type() + "[" + outbound.Tag() + "]"
}
err = E.Cause(err, "open connection to ", remoteString, dialerString)
m.deleteURLTestHistory(metadata.OutboundChain)
N.CloseOnHandshakeFailure(conn, onClose, err)
m.logger.ErrorContext(ctx, err)
return
@@ -192,6 +198,7 @@ func (m *ConnectionManager) NewPacketConnection(ctx context.Context, this N.Dial
dialerString = " using outbound/" + outbound.Type() + "[" + outbound.Tag() + "]"
}
err = E.Cause(err, "open packet connection to ", remoteString, dialerString)
m.deleteURLTestHistory(metadata.OutboundChain)
N.CloseOnHandshakeFailure(conn, onClose, err)
m.logger.ErrorContext(ctx, err)
return
@@ -215,6 +222,7 @@ func (m *ConnectionManager) NewPacketConnection(ctx context.Context, this N.Dial
dialerString = " using outbound/" + outbound.Type() + "[" + outbound.Tag() + "]"
}
err = E.Cause(err, "listen packet connection using ", dialerString)
m.deleteURLTestHistory(metadata.OutboundChain)
N.CloseOnHandshakeFailure(conn, onClose, err)
m.logger.ErrorContext(ctx, err)
return
@@ -272,6 +280,15 @@ func (m *ConnectionManager) NewPacketConnection(ctx context.Context, this N.Dial
go m.packetConnectionCopy(ctx, destination, conn, true, &done, onClose)
}
func (m *ConnectionManager) deleteURLTestHistory(chain []adapter.Outbound) {
if slices.ContainsFunc(chain, func(it adapter.Outbound) bool {
_, isURLTestGroup := it.(adapter.URLTestGroup)
return isURLTestGroup
}) {
m.urlTestHistory.DeleteURLTestHistory(chain[len(chain)-1].Tag())
}
}
func (m *ConnectionManager) connectionCopy(ctx context.Context, source net.Conn, destination net.Conn, direction bool, done *atomic.Bool, onClose N.CloseHandlerFunc) {
_, err := bufio.CopyWithIncreateBuffer(destination, source, bufio.DefaultIncreaseBufferAfter, bufio.DefaultBatchSize)
if err != nil {