chore: cleanup metadata code

This commit is contained in:
wwqgtxx
2025-04-09 12:33:01 +08:00
parent cac2bf72e1
commit 4b15568a29
5 changed files with 41 additions and 24 deletions

View File

@@ -6,11 +6,15 @@ import (
"net"
"net/netip"
"strconv"
"github.com/metacubex/mihomo/transport/socks5"
)
// Socks addr type
// SOCKS address types as defined in RFC 1928 section 5.
const (
AtypIPv4 AddrType = 1
AtypDomainName AddrType = 3
AtypIPv6 AddrType = 4
)
const (
TCP NetWork = iota
UDP
@@ -37,6 +41,21 @@ const (
INNER
)
type AddrType byte
func (a AddrType) String() string {
switch a {
case AtypIPv4:
return "IPv4"
case AtypDomainName:
return "DomainName"
case AtypIPv6:
return "IPv6"
default:
return "Unknown"
}
}
type NetWork int
func (n NetWork) String() string {
@@ -207,14 +226,14 @@ func (m *Metadata) SourceValid() bool {
return m.SrcPort != 0 && m.SrcIP.IsValid()
}
func (m *Metadata) AddrType() int {
func (m *Metadata) AddrType() AddrType {
switch true {
case m.Host != "" || !m.DstIP.IsValid():
return socks5.AtypDomainName
return AtypDomainName
case m.DstIP.Is4():
return socks5.AtypIPv4
return AtypIPv4
default:
return socks5.AtypIPv6
return AtypIPv6
}
}