From e782d218066547fedf65f631d8fe5748a003c75a Mon Sep 17 00:00:00 2001 From: dyhkwong <50692134+dyhkwong@users.noreply.github.com> Date: Sun, 8 Oct 2023 22:11:03 +0800 Subject: [PATCH] Fix connect domain for IP outbound --- outbound/default.go | 9 +++++---- outbound/socks.go | 5 +++-- outbound/wireguard.go | 5 +++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/outbound/default.go b/outbound/default.go index 72cb93aa..0382825f 100644 --- a/outbound/default.go +++ b/outbound/default.go @@ -11,6 +11,7 @@ import ( C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" + "github.com/sagernet/sing-dns" "github.com/sagernet/sing/common" "github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/bufio" @@ -74,7 +75,7 @@ func NewConnection(ctx context.Context, this N.Dialer, conn net.Conn, metadata a return CopyEarlyConn(ctx, conn, outConn) } -func NewDirectConnection(ctx context.Context, router adapter.Router, this N.Dialer, conn net.Conn, metadata adapter.InboundContext) error { +func NewDirectConnection(ctx context.Context, router adapter.Router, this N.Dialer, conn net.Conn, metadata adapter.InboundContext, domainStrategy dns.DomainStrategy) error { ctx = adapter.WithContext(ctx, &metadata) var outConn net.Conn var err error @@ -82,7 +83,7 @@ func NewDirectConnection(ctx context.Context, router adapter.Router, this N.Dial outConn, err = N.DialSerial(ctx, this, N.NetworkTCP, metadata.Destination, metadata.DestinationAddresses) } else if metadata.Destination.IsFqdn() { var destinationAddresses []netip.Addr - destinationAddresses, err = router.LookupDefault(ctx, metadata.Destination.Fqdn) + destinationAddresses, err = router.Lookup(ctx, metadata.Destination.Fqdn, domainStrategy) if err != nil { return N.ReportHandshakeFailure(conn, err) } @@ -133,7 +134,7 @@ func NewPacketConnection(ctx context.Context, this N.Dialer, conn N.PacketConn, return bufio.CopyPacketConn(ctx, conn, bufio.NewPacketConn(outConn)) } -func NewDirectPacketConnection(ctx context.Context, router adapter.Router, this N.Dialer, conn N.PacketConn, metadata adapter.InboundContext) error { +func NewDirectPacketConnection(ctx context.Context, router adapter.Router, this N.Dialer, conn N.PacketConn, metadata adapter.InboundContext, domainStrategy dns.DomainStrategy) error { ctx = adapter.WithContext(ctx, &metadata) var outConn net.PacketConn var destinationAddress netip.Addr @@ -142,7 +143,7 @@ func NewDirectPacketConnection(ctx context.Context, router adapter.Router, this outConn, destinationAddress, err = N.ListenSerial(ctx, this, metadata.Destination, metadata.DestinationAddresses) } else if metadata.Destination.IsFqdn() { var destinationAddresses []netip.Addr - destinationAddresses, err = router.LookupDefault(ctx, metadata.Destination.Fqdn) + destinationAddresses, err = router.Lookup(ctx, metadata.Destination.Fqdn, domainStrategy) if err != nil { return N.ReportHandshakeFailure(conn, err) } diff --git a/outbound/socks.go b/outbound/socks.go index ab26fd3f..bd6b1ada 100644 --- a/outbound/socks.go +++ b/outbound/socks.go @@ -9,6 +9,7 @@ import ( C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" + "github.com/sagernet/sing-dns" "github.com/sagernet/sing/common" E "github.com/sagernet/sing/common/exceptions" M "github.com/sagernet/sing/common/metadata" @@ -114,7 +115,7 @@ func (h *Socks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net. func (h *Socks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error { if h.resolve { - return NewDirectConnection(ctx, h.router, h, conn, metadata) + return NewDirectConnection(ctx, h.router, h, conn, metadata, dns.DomainStrategyUseIPv4) } else { return NewConnection(ctx, h, conn, metadata) } @@ -122,7 +123,7 @@ func (h *Socks) NewConnection(ctx context.Context, conn net.Conn, metadata adapt func (h *Socks) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error { if h.resolve { - return NewDirectPacketConnection(ctx, h.router, h, conn, metadata) + return NewDirectPacketConnection(ctx, h.router, h, conn, metadata, dns.DomainStrategyUseIPv4) } else { return NewPacketConnection(ctx, h, conn, metadata) } diff --git a/outbound/wireguard.go b/outbound/wireguard.go index e141fbb5..ad9145ba 100644 --- a/outbound/wireguard.go +++ b/outbound/wireguard.go @@ -16,6 +16,7 @@ import ( "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/transport/wireguard" + "github.com/sagernet/sing-dns" "github.com/sagernet/sing-tun" "github.com/sagernet/sing/common" "github.com/sagernet/sing/common/debug" @@ -228,11 +229,11 @@ func (w *WireGuard) ListenPacket(ctx context.Context, destination M.Socksaddr) ( } func (w *WireGuard) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error { - return NewDirectConnection(ctx, w.router, w, conn, metadata) + return NewDirectConnection(ctx, w.router, w, conn, metadata, dns.DomainStrategyAsIS) } func (w *WireGuard) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error { - return NewDirectPacketConnection(ctx, w.router, w, conn, metadata) + return NewDirectPacketConnection(ctx, w.router, w, conn, metadata, dns.DomainStrategyAsIS) } func (w *WireGuard) Start() error {