refactor: 优化UDP远程目标获取

This commit is contained in:
gVisor bot 2022-05-26 23:41:09 +08:00
parent 3e082166be
commit f30721e3bf
3 changed files with 21 additions and 12 deletions

View file

@ -129,6 +129,11 @@ func NewBase(opt BaseOption) *Base {
type conn struct {
net.Conn
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
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 {
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 = actualProxy.Addr()
}
metadata.RemoteDst = addr
}
}