diff --git a/adapter/outbound/util.go b/adapter/outbound/util.go index c80608a5..ea39db1f 100644 --- a/adapter/outbound/util.go +++ b/adapter/outbound/util.go @@ -17,20 +17,19 @@ import ( func serializesSocksAddr(metadata *C.Metadata) []byte { var buf [][]byte addrType := metadata.AddrType() - aType := uint8(addrType) p := uint(metadata.DstPort) port := []byte{uint8(p >> 8), uint8(p & 0xff)} switch addrType { - case socks5.AtypDomainName: + case C.AtypDomainName: lenM := uint8(len(metadata.Host)) host := []byte(metadata.Host) - buf = [][]byte{{aType, lenM}, host, port} - case socks5.AtypIPv4: + buf = [][]byte{{socks5.AtypDomainName, lenM}, host, port} + case C.AtypIPv4: host := metadata.DstIP.AsSlice() - buf = [][]byte{{aType}, host, port} - case socks5.AtypIPv6: + buf = [][]byte{{socks5.AtypIPv4}, host, port} + case C.AtypIPv6: host := metadata.DstIP.AsSlice() - buf = [][]byte{{aType}, host, port} + buf = [][]byte{{socks5.AtypIPv6}, host, port} } return bytes.Join(buf, nil) } diff --git a/adapter/outbound/vless.go b/adapter/outbound/vless.go index 8f5de920..1e7d1395 100644 --- a/adapter/outbound/vless.go +++ b/adapter/outbound/vless.go @@ -22,7 +22,6 @@ import ( tlsC "github.com/metacubex/mihomo/component/tls" C "github.com/metacubex/mihomo/constant" "github.com/metacubex/mihomo/transport/gun" - "github.com/metacubex/mihomo/transport/socks5" "github.com/metacubex/mihomo/transport/vless" "github.com/metacubex/mihomo/transport/vmess" @@ -397,15 +396,15 @@ func parseVlessAddr(metadata *C.Metadata, xudp bool) *vless.DstAddr { var addrType byte var addr []byte switch metadata.AddrType() { - case socks5.AtypIPv4: + case C.AtypIPv4: addrType = vless.AtypIPv4 addr = make([]byte, net.IPv4len) copy(addr[:], metadata.DstIP.AsSlice()) - case socks5.AtypIPv6: + case C.AtypIPv6: addrType = vless.AtypIPv6 addr = make([]byte, net.IPv6len) copy(addr[:], metadata.DstIP.AsSlice()) - case socks5.AtypDomainName: + case C.AtypDomainName: addrType = vless.AtypDomainName addr = make([]byte, len(metadata.Host)+1) addr[0] = byte(len(metadata.Host)) diff --git a/constant/metadata.go b/constant/metadata.go index 2eea8335..3a8f4c79 100644 --- a/constant/metadata.go +++ b/constant/metadata.go @@ -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 } } diff --git a/transport/tuic/v4/protocol.go b/transport/tuic/v4/protocol.go index 29536742..cb04fad5 100644 --- a/transport/tuic/v4/protocol.go +++ b/transport/tuic/v4/protocol.go @@ -444,13 +444,13 @@ func NewAddress(metadata *C.Metadata) Address { var addrType byte var addr []byte switch metadata.AddrType() { - case socks5.AtypIPv4: + case C.AtypIPv4: addrType = AtypIPv4 addr = metadata.DstIP.AsSlice() - case socks5.AtypIPv6: + case C.AtypIPv6: addrType = AtypIPv6 addr = metadata.DstIP.AsSlice() - case socks5.AtypDomainName: + case C.AtypDomainName: addrType = AtypDomainName addr = make([]byte, len(metadata.Host)+1) addr[0] = byte(len(metadata.Host)) diff --git a/transport/tuic/v5/protocol.go b/transport/tuic/v5/protocol.go index de51a080..3d96c8e2 100644 --- a/transport/tuic/v5/protocol.go +++ b/transport/tuic/v5/protocol.go @@ -423,13 +423,13 @@ func NewAddress(metadata *C.Metadata) Address { var addrType byte var addr []byte switch metadata.AddrType() { - case socks5.AtypIPv4: + case C.AtypIPv4: addrType = AtypIPv4 addr = metadata.DstIP.AsSlice() - case socks5.AtypIPv6: + case C.AtypIPv6: addrType = AtypIPv6 addr = metadata.DstIP.AsSlice() - case socks5.AtypDomainName: + case C.AtypDomainName: addrType = AtypDomainName addr = make([]byte, len(metadata.Host)+1) addr[0] = byte(len(metadata.Host))