mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 14:18:36 +00:00
Fix disabled UDP domain unmapping ignored by direct outbound
This commit is contained in:
+20
-10
@@ -3,6 +3,7 @@ package dialer
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/netip"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -17,8 +18,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
_ N.Dialer = (*resolveDialer)(nil)
|
||||
_ ParallelInterfaceDialer = (*resolveParallelNetworkDialer)(nil)
|
||||
_ ResolveDialer = (*resolveDialer)(nil)
|
||||
_ ParallelInterfaceResolveDialer = (*resolveParallelNetworkDialer)(nil)
|
||||
)
|
||||
|
||||
type ResolveDialer interface {
|
||||
@@ -28,6 +29,7 @@ type ResolveDialer interface {
|
||||
|
||||
type ParallelInterfaceResolveDialer interface {
|
||||
ParallelInterfaceDialer
|
||||
PacketDialerWithDestination
|
||||
QueryOptions() adapter.DNSQueryOptions
|
||||
}
|
||||
|
||||
@@ -112,23 +114,31 @@ func (d *resolveDialer) DialContext(ctx context.Context, network string, destina
|
||||
}
|
||||
|
||||
func (d *resolveDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
||||
err := d.initialize()
|
||||
conn, destinationAddress, err := d.ListenPacketWithDestination(ctx, destination)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !destinationAddress.IsValid() {
|
||||
return conn, nil
|
||||
}
|
||||
return bufio.NewNATPacketConn(bufio.NewPacketConn(conn), M.SocksaddrFrom(destinationAddress, destination.Port), destination), nil
|
||||
}
|
||||
|
||||
func (d *resolveDialer) ListenPacketWithDestination(ctx context.Context, destination M.Socksaddr) (net.PacketConn, netip.Addr, error) {
|
||||
err := d.initialize()
|
||||
if err != nil {
|
||||
return nil, netip.Addr{}, err
|
||||
}
|
||||
if !destination.IsDomain() {
|
||||
return d.dialer.ListenPacket(ctx, destination)
|
||||
conn, err := d.dialer.ListenPacket(ctx, destination)
|
||||
return conn, netip.Addr{}, err
|
||||
}
|
||||
ctx = log.ContextWithOverrideLevel(ctx, log.LevelDebug)
|
||||
addresses, err := d.router.Lookup(ctx, destination.Fqdn, d.queryOptions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, netip.Addr{}, err
|
||||
}
|
||||
conn, destinationAddress, err := N.ListenSerial(ctx, d.dialer, destination, addresses)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bufio.NewNATPacketConn(bufio.NewPacketConn(conn), M.SocksaddrFrom(destinationAddress, destination.Port), destination), nil
|
||||
return N.ListenSerial(ctx, d.dialer, destination, addresses)
|
||||
}
|
||||
|
||||
func (d *resolveDialer) QueryOptions() adapter.DNSQueryOptions {
|
||||
|
||||
@@ -29,11 +29,12 @@ func RegisterOutbound(registry *outbound.Registry) {
|
||||
}
|
||||
|
||||
var (
|
||||
_ N.ParallelDialer = (*Outbound)(nil)
|
||||
_ dialer.ParallelNetworkDialer = (*Outbound)(nil)
|
||||
_ dialer.DirectDialer = (*Outbound)(nil)
|
||||
_ adapter.FlowOutbound = (*Outbound)(nil)
|
||||
_ adapter.InterfaceUpdateListener = (*Outbound)(nil)
|
||||
_ N.ParallelDialer = (*Outbound)(nil)
|
||||
_ dialer.ParallelNetworkDialer = (*Outbound)(nil)
|
||||
_ dialer.PacketDialerWithDestination = (*Outbound)(nil)
|
||||
_ dialer.DirectDialer = (*Outbound)(nil)
|
||||
_ adapter.FlowOutbound = (*Outbound)(nil)
|
||||
_ adapter.InterfaceUpdateListener = (*Outbound)(nil)
|
||||
)
|
||||
|
||||
type Outbound struct {
|
||||
@@ -41,7 +42,7 @@ type Outbound struct {
|
||||
ctx context.Context
|
||||
logger logger.ContextLogger
|
||||
network adapter.NetworkManager
|
||||
dialer dialer.ParallelInterfaceDialer
|
||||
dialer dialer.ParallelInterfaceResolveDialer
|
||||
domainStrategy C.DomainStrategy
|
||||
fallbackDelay time.Duration
|
||||
isEmpty bool
|
||||
@@ -71,7 +72,7 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL
|
||||
//nolint:staticcheck
|
||||
domainStrategy: C.DomainStrategy(options.DomainStrategy),
|
||||
fallbackDelay: time.Duration(options.FallbackDelay),
|
||||
dialer: outboundDialer.(dialer.ParallelInterfaceDialer),
|
||||
dialer: outboundDialer.(dialer.ParallelInterfaceResolveDialer),
|
||||
isEmpty: reflect.DeepEqual(options.DialerOptions, option.DialerOptions{
|
||||
AbstractDialerOptions: option.AbstractDialerOptions{UDPFragmentDefault: true},
|
||||
}),
|
||||
@@ -183,6 +184,21 @@ func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (n
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func (h *Outbound) ListenPacketWithDestination(ctx context.Context, destination M.Socksaddr) (net.PacketConn, netip.Addr, error) {
|
||||
if h.isMyLoopbackAddress(destination.Addr) {
|
||||
return nil, netip.Addr{}, E.New("loopback connection to TUN range")
|
||||
}
|
||||
ctx, metadata := adapter.ExtendContext(ctx)
|
||||
metadata.Outbound = h.Tag()
|
||||
metadata.Destination = destination
|
||||
h.logger.InfoContext(ctx, "outbound packet connection")
|
||||
if !metadata.UDPDisableDomainUnmapping {
|
||||
conn, err := h.dialer.ListenPacket(ctx, destination)
|
||||
return conn, netip.Addr{}, err
|
||||
}
|
||||
return h.dialer.ListenPacketWithDestination(ctx, destination)
|
||||
}
|
||||
|
||||
func (h *Outbound) PreMatchFlow(network string, destination netip.Addr) adapter.PreMatchAction {
|
||||
if network == N.NetworkICMP && h.icmpPort != nil {
|
||||
if h.isMyLoopbackAddress(destination) {
|
||||
|
||||
Reference in New Issue
Block a user