Refactor: return metadata.Addr only

This commit is contained in:
xjasonlyu
2022-04-03 23:16:36 +08:00
parent 8f97bda4f5
commit 9797cb31c0
3 changed files with 9 additions and 6 deletions

View File

@@ -25,9 +25,6 @@ func (m *Metadata) SourceAddress() string {
}
func (m *Metadata) Addr() net.Addr {
if udpAddr := m.UDPAddr(); udpAddr != nil {
return udpAddr
}
return &Addr{metadata: m}
}

View File

@@ -45,8 +45,8 @@ type directPacketConn struct {
}
func (pc *directPacketConn) WriteTo(b []byte, addr net.Addr) (int, error) {
if ma, ok := addr.(*M.Addr); ok && ma.Metadata().DstIP != nil {
return pc.PacketConn.WriteTo(b, ma.Metadata().UDPAddr())
if udpAddr, ok := addr.(*net.UDPAddr); ok {
return pc.PacketConn.WriteTo(b, udpAddr)
}
udpAddr, err := net.ResolveUDPAddr("udp", addr.String())

View File

@@ -48,7 +48,13 @@ func handleUDPConn(uc adapter.UDPConn) {
pc = newUDPTracker(pc, metadata)
defer pc.Close()
remote := metadata.Addr()
var remote net.Addr
if udpAddr := metadata.UDPAddr(); udpAddr != nil {
remote = udpAddr
} else {
remote = metadata.Addr()
}
go handleUDPToRemote(uc, pc, remote)
handleUDPToLocal(uc, pc, remote)
}