From 72fb153fe0f9a8a06f35dabeb84074df8595b3e6 Mon Sep 17 00:00:00 2001 From: Skyxim Date: Thu, 26 May 2022 23:41:09 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96UDP=E8=BF=9C?= =?UTF-8?q?=E7=A8=8B=E7=9B=AE=E6=A0=87=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adapter/outbound/base.go | 18 ++++++++++++++---- constant/adapters.go | 1 + tunnel/tunnel.go | 14 ++++++-------- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/adapter/outbound/base.go b/adapter/outbound/base.go index 0aa8dfc5..cb959ef8 100644 --- a/adapter/outbound/base.go +++ b/adapter/outbound/base.go @@ -128,7 +128,12 @@ func NewBase(opt BaseOption) *Base { type conn struct { net.Conn - chain C.Chain + chain C.Chain + lastAdapterRemote string +} + +func (c *conn) RemoteDestination() string { + return c.lastAdapterRemote } // Chains implements C.Connection @@ -142,12 +147,17 @@ func (c *conn) AppendToChains(a C.ProxyAdapter) { } func NewConn(c net.Conn, a C.ProxyAdapter) C.Conn { - return &conn{c, []string{a.Name()}} + return &conn{c, []string{a.Name()}, a.Addr()} } type packetConn struct { net.PacketConn - chain C.Chain + chain C.Chain + lastAdapterRemote string +} + +func (c *packetConn) RemoteDestination() string { + return c.lastAdapterRemote } // Chains implements C.Connection @@ -161,5 +171,5 @@ func (c *packetConn) AppendToChains(a C.ProxyAdapter) { } func newPacketConn(pc net.PacketConn, a C.ProxyAdapter) C.PacketConn { - return &packetConn{pc, []string{a.Name()}} + return &packetConn{pc, []string{a.Name()}, a.Addr()} } diff --git a/constant/adapters.go b/constant/adapters.go index b2339c5d..4e40f21e 100644 --- a/constant/adapters.go +++ b/constant/adapters.go @@ -41,6 +41,7 @@ const ( type Connection interface { Chains() Chain AppendToChains(adapter ProxyAdapter) + RemoteDestination() string } type Chain []string diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index ddc0670a..22f2c507 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -288,14 +288,12 @@ func handleUDPConn(packet *inbound.PacketAdapter) { } pCtx.InjectPacketConn(rawPc) - actualProxy := proxy.Unwrap(metadata) - if actualProxy != nil { - if dst, _, err := net.SplitHostPort(actualProxy.Addr()); err == nil { - metadata.RemoteDst = dst - } else { - if addrError, ok := err.(*net.AddrError); ok && strings.Contains(addrError.Err, "missing port") { - metadata.RemoteDst = actualProxy.Addr() - } + addr := rawPc.RemoteDestination() + if dst, _, err := net.SplitHostPort(addr); err == nil { + metadata.RemoteDst = dst + } else { + if addrError, ok := err.(*net.AddrError); ok && strings.Contains(addrError.Err, "missing port") { + metadata.RemoteDst = addr } }