chore: code cleanup
This commit is contained in:
@ -240,6 +240,34 @@ func (m *Metadata) Valid() bool {
|
||||
return m.Host != "" || m.DstIP.IsValid()
|
||||
}
|
||||
|
||||
func (m *Metadata) SetRemoteAddr(addr net.Addr) error {
|
||||
if addr == nil {
|
||||
return nil
|
||||
}
|
||||
if rawAddr, ok := addr.(interface{ RawAddr() net.Addr }); ok {
|
||||
if rawAddr := rawAddr.RawAddr(); rawAddr != nil {
|
||||
if err := m.SetRemoteAddr(rawAddr); err == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if addr, ok := addr.(interface{ AddrPort() netip.AddrPort }); ok { // *net.TCPAddr, *net.UDPAddr, M.Socksaddr
|
||||
if addrPort := addr.AddrPort(); addrPort.Port() != 0 {
|
||||
m.DstPort = addrPort.Port()
|
||||
if addrPort.IsValid() { // sing's M.Socksaddr maybe return an invalid AddrPort if it's a DomainName
|
||||
m.DstIP = addrPort.Addr().Unmap()
|
||||
return nil
|
||||
} else {
|
||||
if addr, ok := addr.(interface{ AddrString() string }); ok { // must be sing's M.Socksaddr
|
||||
m.Host = addr.AddrString() // actually is M.Socksaddr.Fqdn
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return m.SetRemoteAddress(addr.String())
|
||||
}
|
||||
|
||||
func (m *Metadata) SetRemoteAddress(rawAddress string) error {
|
||||
host, port, err := net.SplitHostPort(rawAddress)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user