Stagger connection attempts in parallel dial

This commit is contained in:
世界 2026-10-23 14:47:30 +08:00
parent 6aea1f8ba8
commit 6cde71cfc0
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 20 additions and 78 deletions

View file

@ -7,7 +7,6 @@ import (
"time"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
@ -51,82 +50,25 @@ func DialParallelNetwork(ctx context.Context, dialer ParallelInterfaceDialer, ne
}
destinationAddresses = []netip.Addr{destination.Addr}
}
return N.DialParallel(ctx, &networkStrategyDialer{
ParallelInterfaceDialer: dialer,
strategy: strategy,
interfaceType: interfaceType,
fallbackInterfaceType: fallbackInterfaceType,
fallbackDelay: fallbackDelay,
}, network, destination, destinationAddresses, preferIPv6, fallbackDelay)
}
if fallbackDelay == 0 {
fallbackDelay = N.DefaultFallbackDelay
}
type networkStrategyDialer struct {
ParallelInterfaceDialer
strategy *C.NetworkStrategy
interfaceType []C.InterfaceType
fallbackInterfaceType []C.InterfaceType
fallbackDelay time.Duration
}
returned := make(chan struct{})
defer close(returned)
addresses4 := common.Filter(destinationAddresses, func(address netip.Addr) bool {
return address.Is4() || address.Is4In6()
})
addresses6 := common.Filter(destinationAddresses, func(address netip.Addr) bool {
return address.Is6() && !address.Is4In6()
})
if len(addresses4) == 0 || len(addresses6) == 0 {
return DialSerialNetwork(ctx, dialer, network, destination, destinationAddresses, strategy, interfaceType, fallbackInterfaceType, fallbackDelay)
}
var primaries, fallbacks []netip.Addr
if preferIPv6 {
primaries = addresses6
fallbacks = addresses4
} else {
primaries = addresses4
fallbacks = addresses6
}
type dialResult struct {
net.Conn
error
primary bool
done bool
}
results := make(chan dialResult) // unbuffered
startRacer := func(ctx context.Context, primary bool) {
ras := primaries
if !primary {
ras = fallbacks
}
c, err := DialSerialNetwork(ctx, dialer, network, destination, ras, strategy, interfaceType, fallbackInterfaceType, fallbackDelay)
select {
case results <- dialResult{Conn: c, error: err, primary: primary, done: true}:
case <-returned:
if c != nil {
c.Close()
}
}
}
var primary, fallback dialResult
primaryCtx, primaryCancel := context.WithCancel(ctx)
defer primaryCancel()
go startRacer(primaryCtx, true)
fallbackTimer := time.NewTimer(fallbackDelay)
defer fallbackTimer.Stop()
for {
select {
case <-fallbackTimer.C:
fallbackCtx, fallbackCancel := context.WithCancel(ctx)
defer fallbackCancel()
go startRacer(fallbackCtx, false)
case res := <-results:
if res.error == nil {
return res.Conn, nil
}
if res.primary {
primary = res
} else {
fallback = res
}
if primary.done && fallback.done {
return nil, primary.error
}
if res.primary && fallbackTimer.Stop() {
fallbackTimer.Reset(0)
}
}
}
func (d *networkStrategyDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
return d.DialParallelInterface(ctx, network, destination, d.strategy, d.interfaceType, d.fallbackInterfaceType, d.fallbackDelay)
}
func ListenSerialNetworkPacket(ctx context.Context, dialer N.Dialer, destination M.Socksaddr, destinationAddresses []netip.Addr, strategy *C.NetworkStrategy, interfaceType []C.InterfaceType, fallbackInterfaceType []C.InterfaceType, fallbackDelay time.Duration) (net.PacketConn, netip.Addr, error) {

2
go.mod
View file

@ -43,7 +43,7 @@ require (
github.com/sagernet/netlink v0.0.0-20260814022025-64455d367bbf
github.com/sagernet/nftables v0.3.0-mod.4
github.com/sagernet/quic-go v0.61.0-sing-box-mod.12
github.com/sagernet/sing v0.9.7-0.20261023115509-3a1dcc10f6e0
github.com/sagernet/sing v0.9.7-0.20261023115510-03a6cce64bf7
github.com/sagernet/sing-anytls v0.0.0-20260928104022-580984e4d8cb
github.com/sagernet/sing-cloudflared v0.1.4-0.20261021152903-2d40d788fcf6
github.com/sagernet/sing-mux v0.3.10-0.20260929204512-caf09fe32475

4
go.sum
View file

@ -316,8 +316,8 @@ github.com/sagernet/nftables v0.3.0-mod.4 h1:vnOtcDYeSXv2e5RoRuGH0lrpttQFJ8iC4IC
github.com/sagernet/nftables v0.3.0-mod.4/go.mod h1:8kslHG4VvYNihcco+i6uxIX7qbT8A56T0y5q7U44ZaQ=
github.com/sagernet/quic-go v0.61.0-sing-box-mod.12 h1:9/USaaiONHFeqBePb+4C/A0MBM2dmxED49UGBpHLHS4=
github.com/sagernet/quic-go v0.61.0-sing-box-mod.12/go.mod h1:hmLC8GJPp+BrpBgrgJnacvI4fZGIhasuy1ROJJ8GR4E=
github.com/sagernet/sing v0.9.7-0.20261023115509-3a1dcc10f6e0 h1:WzNh3peK4qbvPRmxQqkbEnKowdjyvD8d26Um9nB0zfI=
github.com/sagernet/sing v0.9.7-0.20261023115509-3a1dcc10f6e0/go.mod h1:K3Owt3xPhHugvlnlPPxZJ/exXdaJfEPOTNorGk4AXjo=
github.com/sagernet/sing v0.9.7-0.20261023115510-03a6cce64bf7 h1:idTY/eRW81qzIN0DLYoaUyeeY/yTDLY2qn6zimm6z6Y=
github.com/sagernet/sing v0.9.7-0.20261023115510-03a6cce64bf7/go.mod h1:K3Owt3xPhHugvlnlPPxZJ/exXdaJfEPOTNorGk4AXjo=
github.com/sagernet/sing-anytls v0.0.0-20260928104022-580984e4d8cb h1:n4sF2zv0CR1gcb4awh61t11t0FZmcP40x5enB8HARjE=
github.com/sagernet/sing-anytls v0.0.0-20260928104022-580984e4d8cb/go.mod h1:VRKLnj0ObEsN228h+zuzf91p+6sjtYE8SBaLPVsBy5o=
github.com/sagernet/sing-cloudflared v0.1.4-0.20261021152903-2d40d788fcf6 h1:cedQ/AnIVZXNS0RbV5E6xXbLan7Qu8rOj7+B1kvb7fs=