chore: using uint16 for ports in Metadata

This commit is contained in:
wwqgtxx
2023-08-09 13:51:02 +08:00
parent bad9f2e6dc
commit e2e0fd4eba
16 changed files with 84 additions and 79 deletions

View File

@ -128,10 +128,10 @@ type Metadata struct {
Type Type `json:"type"`
SrcIP netip.Addr `json:"sourceIP"`
DstIP netip.Addr `json:"destinationIP"`
SrcPort string `json:"sourcePort"`
DstPort string `json:"destinationPort"`
SrcPort uint16 `json:"sourcePort,string"` // `,string` is used to compatible with old version json output
DstPort uint16 `json:"destinationPort,string"` // `,string` is used to compatible with old version json output
InIP netip.Addr `json:"inboundIP"`
InPort string `json:"inboundPort"`
InPort uint16 `json:"inboundPort,string"` // `,string` is used to compatible with old version json output
InName string `json:"inboundName"`
InUser string `json:"inboundUser"`
Host string `json:"host"`
@ -147,11 +147,11 @@ type Metadata struct {
}
func (m *Metadata) RemoteAddress() string {
return net.JoinHostPort(m.String(), m.DstPort)
return net.JoinHostPort(m.String(), strconv.FormatUint(uint64(m.DstPort), 10))
}
func (m *Metadata) SourceAddress() string {
return net.JoinHostPort(m.SrcIP.String(), m.SrcPort)
return net.JoinHostPort(m.SrcIP.String(), strconv.FormatUint(uint64(m.SrcPort), 10))
}
func (m *Metadata) SourceDetail() string {
@ -172,7 +172,7 @@ func (m *Metadata) SourceDetail() string {
}
func (m *Metadata) SourceValid() bool {
return m.SrcPort != "" && m.SrcIP.IsValid()
return m.SrcPort != 0 && m.SrcIP.IsValid()
}
func (m *Metadata) AddrType() int {
@ -211,8 +211,7 @@ func (m *Metadata) Pure() *Metadata {
}
func (m *Metadata) AddrPort() netip.AddrPort {
port, _ := strconv.ParseUint(m.DstPort, 10, 16)
return netip.AddrPortFrom(m.DstIP.Unmap(), uint16(port))
return netip.AddrPortFrom(m.DstIP.Unmap(), m.DstPort)
}
func (m *Metadata) UDPAddr() *net.UDPAddr {
@ -242,6 +241,11 @@ func (m *Metadata) SetRemoteAddress(rawAddress string) error {
return err
}
var uint16Port uint16
if port, err := strconv.ParseUint(port, 10, 16); err == nil {
uint16Port = uint16(port)
}
if ip, err := netip.ParseAddr(host); err != nil {
m.Host = host
m.DstIP = netip.Addr{}
@ -249,7 +253,7 @@ func (m *Metadata) SetRemoteAddress(rawAddress string) error {
m.Host = ""
m.DstIP = ip.Unmap()
}
m.DstPort = port
m.DstPort = uint16Port
return nil
}