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 {
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()}
}

View File

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

View File

@ -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
}
}