refactor: 优化UDP远程目标获取

This commit is contained in:
Skyxim 2022-05-26 23:41:09 +08:00
parent 527a602eba
commit 72fb153fe0
3 changed files with 21 additions and 12 deletions

View file

@ -128,7 +128,12 @@ func NewBase(opt BaseOption) *Base {
type conn struct { type conn struct {
net.Conn net.Conn
chain C.Chain chain C.Chain
lastAdapterRemote string
}
func (c *conn) RemoteDestination() string {
return c.lastAdapterRemote
} }
// Chains implements C.Connection // 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 { 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 { type packetConn struct {
net.PacketConn net.PacketConn
chain C.Chain chain C.Chain
lastAdapterRemote string
}
func (c *packetConn) RemoteDestination() string {
return c.lastAdapterRemote
} }
// Chains implements C.Connection // 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 { func newPacketConn(pc net.PacketConn, a C.ProxyAdapter) C.PacketConn {
return &packetConn{pc, []string{a.Name()}} return &packetConn{pc, []string{a.Name()}, a.Addr()}
} }

View file

@ -41,6 +41,7 @@ const (
type Connection interface { type Connection interface {
Chains() Chain Chains() Chain
AppendToChains(adapter ProxyAdapter) AppendToChains(adapter ProxyAdapter)
RemoteDestination() string
} }
type Chain []string type Chain []string

View file

@ -288,14 +288,12 @@ func handleUDPConn(packet *inbound.PacketAdapter) {
} }
pCtx.InjectPacketConn(rawPc) pCtx.InjectPacketConn(rawPc)
actualProxy := proxy.Unwrap(metadata) addr := rawPc.RemoteDestination()
if actualProxy != nil { if dst, _, err := net.SplitHostPort(addr); err == nil {
if dst, _, err := net.SplitHostPort(actualProxy.Addr()); err == nil { metadata.RemoteDst = dst
metadata.RemoteDst = dst } else {
} else { if addrError, ok := err.(*net.AddrError); ok && strings.Contains(addrError.Err, "missing port") {
if addrError, ok := err.(*net.AddrError); ok && strings.Contains(addrError.Err, "missing port") { metadata.RemoteDst = addr
metadata.RemoteDst = actualProxy.Addr()
}
} }
} }