mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 14:18:36 +00:00
Fix route-level network strategy ignoring outbound restrictions
This commit is contained in:
@@ -333,7 +333,9 @@ func (d *DefaultDialer) DialParallelNetwork(ctx context.Context, network string,
|
||||
if len(destinationAddresses) == 0 {
|
||||
destinationAddresses = []netip.Addr{destination.Addr}
|
||||
}
|
||||
if strategy == nil && d.canUseNetworkStrategy.Load() {
|
||||
if d.networkStrategy == nil || !d.canUseNetworkStrategy.Load() {
|
||||
strategy = nil
|
||||
} else if strategy == nil {
|
||||
strategy = d.networkStrategy
|
||||
}
|
||||
if fallbackDelay == 0 {
|
||||
@@ -440,7 +442,9 @@ func (d *DefaultDialer) ListenSerialNetworkPacket(ctx context.Context, destinati
|
||||
}
|
||||
|
||||
func (d *DefaultDialer) listenSerialInterfacePacket(ctx context.Context, destination M.Socksaddr, strategy *C.NetworkStrategy, interfaceType []C.InterfaceType, fallbackInterfaceType []C.InterfaceType) (net.PacketConn, error) {
|
||||
if strategy == nil && d.canUseNetworkStrategy.Load() {
|
||||
if d.networkStrategy == nil || !d.canUseNetworkStrategy.Load() {
|
||||
strategy = nil
|
||||
} else if strategy == nil {
|
||||
strategy = d.networkStrategy
|
||||
}
|
||||
if strategy == nil {
|
||||
|
||||
@@ -151,7 +151,7 @@ func (d *DefaultDialer) dialParallelInterfaceFastFallback(ctx context.Context, d
|
||||
select {
|
||||
case results <- dialResult{Conn: conn, primary: primary}:
|
||||
case <-returned:
|
||||
if primary && time.Since(startAt) <= fallbackDelay {
|
||||
if primary && N.NetworkName(network) == N.NetworkTCP && time.Since(startAt) <= fallbackDelay {
|
||||
resetFastFallback(time.Time{})
|
||||
}
|
||||
conn.Close()
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
)
|
||||
|
||||
func DialSerialNetwork(ctx context.Context, dialer N.Dialer, network string, destination M.Socksaddr, destinationAddresses []netip.Addr, strategy *C.NetworkStrategy, interfaceType []C.InterfaceType, fallbackInterfaceType []C.InterfaceType, fallbackDelay time.Duration) (net.Conn, error) {
|
||||
if len(destinationAddresses) == 0 {
|
||||
if !destination.IsIP() {
|
||||
panic("invalid usage")
|
||||
}
|
||||
destinationAddresses = []netip.Addr{destination.Addr}
|
||||
}
|
||||
if parallelDialer, isParallel := dialer.(ParallelNetworkDialer); isParallel {
|
||||
return parallelDialer.DialParallelNetwork(ctx, network, destination, destinationAddresses, strategy, interfaceType, fallbackInterfaceType, fallbackDelay)
|
||||
}
|
||||
return N.DialSerial(ctx, dialer, network, destination, destinationAddresses)
|
||||
}
|
||||
|
||||
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) {
|
||||
if len(destinationAddresses) == 0 {
|
||||
if !destination.IsIP() {
|
||||
panic("invalid usage")
|
||||
}
|
||||
destinationAddresses = []netip.Addr{destination.Addr}
|
||||
}
|
||||
if parallelDialer, isParallel := dialer.(ParallelNetworkDialer); isParallel {
|
||||
return parallelDialer.ListenSerialNetworkPacket(ctx, destination, destinationAddresses, strategy, interfaceType, fallbackInterfaceType, fallbackDelay)
|
||||
}
|
||||
return N.ListenSerial(ctx, dialer, destination, destinationAddresses)
|
||||
}
|
||||
@@ -171,7 +171,18 @@ func (d *resolveParallelNetworkDialer) DialParallelNetwork(ctx context.Context,
|
||||
if fallbackDelay == 0 {
|
||||
fallbackDelay = d.fallbackDelay
|
||||
}
|
||||
return d.dialer.DialParallelNetwork(ctx, network, destination, destinationAddresses, strategy, interfaceType, fallbackInterfaceType, fallbackDelay)
|
||||
if d.parallel || len(destinationAddresses) <= 1 {
|
||||
return d.dialer.DialParallelNetwork(ctx, network, destination, destinationAddresses, strategy, interfaceType, fallbackInterfaceType, fallbackDelay)
|
||||
}
|
||||
var errors []error
|
||||
for _, address := range destinationAddresses {
|
||||
conn, err := d.dialer.DialParallelNetwork(ctx, network, destination, []netip.Addr{address}, strategy, interfaceType, fallbackInterfaceType, fallbackDelay)
|
||||
if err == nil {
|
||||
return conn, nil
|
||||
}
|
||||
errors = append(errors, err)
|
||||
}
|
||||
return nil, E.Errors(errors...)
|
||||
}
|
||||
|
||||
func (d *resolveParallelNetworkDialer) ListenSerialNetworkPacket(ctx context.Context, destination M.Socksaddr, destinationAddresses []netip.Addr, strategy *C.NetworkStrategy, interfaceType []C.InterfaceType, fallbackInterfaceType []C.InterfaceType, fallbackDelay time.Duration) (net.PacketConn, netip.Addr, error) {
|
||||
|
||||
@@ -174,14 +174,6 @@ See [Dial Fields](/configuration/shared/dial/#network_strategy) for details.
|
||||
Only take effect if outbound is direct without `outbound.bind_interface`,
|
||||
`outbound.inet4_bind_address` and `outbound.inet6_bind_address` set.
|
||||
|
||||
#### network_type
|
||||
|
||||
See [Dial Fields](/configuration/shared/dial/#network_type) for details.
|
||||
|
||||
#### fallback_network_type
|
||||
|
||||
See [Dial Fields](/configuration/shared/dial/#fallback_network_type) for details.
|
||||
|
||||
#### fallback_delay
|
||||
|
||||
See [Dial Fields](/configuration/shared/dial/#fallback_delay) for details.
|
||||
|
||||
@@ -171,14 +171,6 @@ icon: material/new-box
|
||||
仅当出站为 `direct` 且 `outbound.bind_interface`, `outbound.inet4_bind_address`
|
||||
且 `outbound.inet6_bind_address` 未设置时生效。
|
||||
|
||||
#### network_type
|
||||
|
||||
详情参阅 [拨号字段](/zh/configuration/shared/dial/#network_type)。
|
||||
|
||||
#### fallback_network_type
|
||||
|
||||
详情参阅 [拨号字段](/zh/configuration/shared/dial/#fallback_network_type)。
|
||||
|
||||
#### fallback_delay
|
||||
|
||||
详情参阅 [拨号字段](/zh/configuration/shared/dial/#fallback_delay)。
|
||||
|
||||
+3
-9
@@ -14882,9 +14882,7 @@
|
||||
]
|
||||
},
|
||||
"fallback_delay": {
|
||||
"type": "integer",
|
||||
"minimum": 0,
|
||||
"maximum": 4294967295
|
||||
"$ref": "#/$defs/Duration"
|
||||
},
|
||||
"udp_disable_domain_unmapping": {
|
||||
"type": "boolean"
|
||||
@@ -14942,9 +14940,7 @@
|
||||
]
|
||||
},
|
||||
"fallback_delay": {
|
||||
"type": "integer",
|
||||
"minimum": 0,
|
||||
"maximum": 4294967295
|
||||
"$ref": "#/$defs/Duration"
|
||||
},
|
||||
"udp_disable_domain_unmapping": {
|
||||
"type": "boolean"
|
||||
@@ -15009,9 +15005,7 @@
|
||||
]
|
||||
},
|
||||
"fallback_delay": {
|
||||
"type": "integer",
|
||||
"minimum": 0,
|
||||
"maximum": 4294967295
|
||||
"$ref": "#/$defs/Duration"
|
||||
},
|
||||
"udp_disable_domain_unmapping": {
|
||||
"type": "boolean"
|
||||
|
||||
@@ -245,7 +245,7 @@ func (h *Outbound) WritePackets(packets [][]byte) error {
|
||||
}
|
||||
|
||||
func (h *Outbound) DialParallel(ctx context.Context, network string, destination M.Socksaddr, destinationAddresses []netip.Addr) (net.Conn, error) {
|
||||
if h.isMyLoopbackAddress(destinationAddresses...) {
|
||||
if h.isMyLoopbackAddress(destinationAddresses...) || destination.IsIP() && h.isMyLoopbackAddress(destination.Addr) {
|
||||
return nil, E.New("loopback connection to TUN range")
|
||||
}
|
||||
ctx, metadata := adapter.ExtendContext(ctx)
|
||||
@@ -262,7 +262,7 @@ func (h *Outbound) DialParallel(ctx context.Context, network string, destination
|
||||
}
|
||||
|
||||
func (h *Outbound) DialParallelNetwork(ctx context.Context, network string, destination M.Socksaddr, destinationAddresses []netip.Addr, networkStrategy *C.NetworkStrategy, networkType []C.InterfaceType, fallbackNetworkType []C.InterfaceType, fallbackDelay time.Duration) (net.Conn, error) {
|
||||
if h.isMyLoopbackAddress(destinationAddresses...) {
|
||||
if h.isMyLoopbackAddress(destinationAddresses...) || destination.IsIP() && h.isMyLoopbackAddress(destination.Addr) {
|
||||
return nil, E.New("loopback connection to TUN range")
|
||||
}
|
||||
ctx, metadata := adapter.ExtendContext(ctx)
|
||||
@@ -279,7 +279,7 @@ func (h *Outbound) DialParallelNetwork(ctx context.Context, network string, dest
|
||||
}
|
||||
|
||||
func (h *Outbound) ListenSerialNetworkPacket(ctx context.Context, destination M.Socksaddr, destinationAddresses []netip.Addr, networkStrategy *C.NetworkStrategy, networkType []C.InterfaceType, fallbackNetworkType []C.InterfaceType, fallbackDelay time.Duration) (net.PacketConn, netip.Addr, error) {
|
||||
if h.isMyLoopbackAddress(destinationAddresses...) {
|
||||
if h.isMyLoopbackAddress(destinationAddresses...) || destination.IsIP() && h.isMyLoopbackAddress(destination.Addr) {
|
||||
return nil, netip.Addr{}, E.New("loopback connection to TUN range")
|
||||
}
|
||||
ctx, metadata := adapter.ExtendContext(ctx)
|
||||
|
||||
+15
-6
@@ -107,8 +107,10 @@ func (m *ConnectionManager) NewConnection(ctx context.Context, this N.Dialer, co
|
||||
remoteConn net.Conn
|
||||
err error
|
||||
)
|
||||
if len(metadata.DestinationAddresses) > 0 || metadata.Destination.IsIP() {
|
||||
remoteConn, err = dialer.DialSerialNetwork(ctx, this, N.NetworkTCP, metadata.Destination, metadata.DestinationAddresses, metadata.NetworkStrategy, metadata.NetworkType, metadata.FallbackNetworkType, metadata.FallbackDelay)
|
||||
if parallelDialer, isParallel := this.(dialer.ParallelNetworkDialer); isParallel {
|
||||
remoteConn, err = parallelDialer.DialParallelNetwork(ctx, N.NetworkTCP, metadata.Destination, metadata.DestinationAddresses, metadata.NetworkStrategy, metadata.NetworkType, metadata.FallbackNetworkType, metadata.FallbackDelay)
|
||||
} else if len(metadata.DestinationAddresses) > 0 {
|
||||
remoteConn, err = N.DialSerial(ctx, this, N.NetworkTCP, metadata.Destination, metadata.DestinationAddresses)
|
||||
} else {
|
||||
remoteConn, err = this.DialContext(ctx, N.NetworkTCP, metadata.Destination)
|
||||
}
|
||||
@@ -172,8 +174,10 @@ func (m *ConnectionManager) NewPacketConnection(ctx context.Context, this N.Dial
|
||||
err error
|
||||
)
|
||||
if metadata.UDPConnect {
|
||||
if len(metadata.DestinationAddresses) > 0 || metadata.Destination.IsIP() {
|
||||
remoteConn, err = dialer.DialSerialNetwork(ctx, this, N.NetworkUDP, metadata.Destination, metadata.DestinationAddresses, metadata.NetworkStrategy, metadata.NetworkType, metadata.FallbackNetworkType, metadata.FallbackDelay)
|
||||
if parallelDialer, isParallel := this.(dialer.ParallelNetworkDialer); isParallel {
|
||||
remoteConn, err = parallelDialer.DialParallelNetwork(ctx, N.NetworkUDP, metadata.Destination, metadata.DestinationAddresses, metadata.NetworkStrategy, metadata.NetworkType, metadata.FallbackNetworkType, metadata.FallbackDelay)
|
||||
} else if len(metadata.DestinationAddresses) > 0 {
|
||||
remoteConn, err = N.DialSerial(ctx, this, N.NetworkUDP, metadata.Destination, metadata.DestinationAddresses)
|
||||
} else {
|
||||
remoteConn, err = this.DialContext(ctx, N.NetworkUDP, metadata.Destination)
|
||||
}
|
||||
@@ -200,8 +204,10 @@ func (m *ConnectionManager) NewPacketConnection(ctx context.Context, this N.Dial
|
||||
destinationAddress = connRemoteAddr
|
||||
}
|
||||
} else {
|
||||
if len(metadata.DestinationAddresses) > 0 {
|
||||
remotePacketConn, destinationAddress, err = dialer.ListenSerialNetworkPacket(ctx, this, metadata.Destination, metadata.DestinationAddresses, metadata.NetworkStrategy, metadata.NetworkType, metadata.FallbackNetworkType, metadata.FallbackDelay)
|
||||
if parallelDialer, isParallel := this.(dialer.ParallelNetworkDialer); isParallel {
|
||||
remotePacketConn, destinationAddress, err = parallelDialer.ListenSerialNetworkPacket(ctx, metadata.Destination, metadata.DestinationAddresses, metadata.NetworkStrategy, metadata.NetworkType, metadata.FallbackNetworkType, metadata.FallbackDelay)
|
||||
} else if len(metadata.DestinationAddresses) > 0 {
|
||||
remotePacketConn, destinationAddress, err = N.ListenSerial(ctx, this, metadata.Destination, metadata.DestinationAddresses)
|
||||
} else if packetDialer, withDestination := this.(dialer.PacketDialerWithDestination); withDestination {
|
||||
remotePacketConn, destinationAddress, err = packetDialer.ListenPacketWithDestination(ctx, metadata.Destination)
|
||||
} else {
|
||||
@@ -218,6 +224,9 @@ func (m *ConnectionManager) NewPacketConnection(ctx context.Context, this N.Dial
|
||||
m.logger.ErrorContext(ctx, err)
|
||||
return
|
||||
}
|
||||
if destinationAddress == metadata.Destination.Addr {
|
||||
destinationAddress = netip.Addr{}
|
||||
}
|
||||
}
|
||||
err = N.ReportPacketConnHandshakeSuccess(conn, remotePacketConn)
|
||||
if err != nil {
|
||||
|
||||
@@ -649,12 +649,6 @@ match:
|
||||
if routeOptions.NetworkStrategy != nil {
|
||||
metadata.NetworkStrategy = routeOptions.NetworkStrategy
|
||||
}
|
||||
if len(routeOptions.NetworkType) > 0 {
|
||||
metadata.NetworkType = routeOptions.NetworkType
|
||||
}
|
||||
if len(routeOptions.FallbackNetworkType) > 0 {
|
||||
metadata.FallbackNetworkType = routeOptions.FallbackNetworkType
|
||||
}
|
||||
if routeOptions.FallbackDelay != 0 {
|
||||
metadata.FallbackDelay = routeOptions.FallbackDelay
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user