diff --git a/adapter/adapter.go b/adapter/adapter.go index 23dc304af..c41cff4d3 100644 --- a/adapter/adapter.go +++ b/adapter/adapter.go @@ -184,10 +184,9 @@ func urlToMetadata(rawURL string) (addr C.Metadata, err error) { } addr = C.Metadata{ - AddrType: C.AtypDomainName, - Host: u.Hostname(), - DstIP: nil, - DstPort: port, + Host: u.Hostname(), + DstIP: nil, + DstPort: port, } return } diff --git a/adapter/inbound/util.go b/adapter/inbound/util.go index 07577ec0e..4f1392707 100644 --- a/adapter/inbound/util.go +++ b/adapter/inbound/util.go @@ -11,9 +11,7 @@ import ( ) func parseSocksAddr(target socks5.Addr) *C.Metadata { - metadata := &C.Metadata{ - AddrType: int(target[0]), - } + metadata := &C.Metadata{} switch target[0] { case socks5.AtypDomainName: @@ -44,21 +42,13 @@ func parseHTTPAddr(request *http.Request) *C.Metadata { host = strings.TrimRight(host, ".") metadata := &C.Metadata{ - NetWork: C.TCP, - AddrType: C.AtypDomainName, - Host: host, - DstIP: nil, - DstPort: port, + NetWork: C.TCP, + Host: host, + DstIP: nil, + DstPort: port, } - ip := net.ParseIP(host) - if ip != nil { - switch { - case ip.To4() == nil: - metadata.AddrType = C.AtypIPv6 - default: - metadata.AddrType = C.AtypIPv4 - } + if ip := net.ParseIP(host); ip != nil { metadata.DstIP = ip } diff --git a/adapter/outbound/util.go b/adapter/outbound/util.go index b376522fa..a964a1412 100644 --- a/adapter/outbound/util.go +++ b/adapter/outbound/util.go @@ -20,10 +20,11 @@ func tcpKeepAlive(c net.Conn) { func serializesSocksAddr(metadata *C.Metadata) []byte { var buf [][]byte - aType := uint8(metadata.AddrType) + addrType := metadata.AddrType() + aType := uint8(addrType) p, _ := strconv.ParseUint(metadata.DstPort, 10, 16) port := []byte{uint8(p >> 8), uint8(p & 0xff)} - switch metadata.AddrType { + switch addrType { case socks5.AtypDomainName: len := uint8(len(metadata.Host)) host := []byte(metadata.Host) diff --git a/adapter/outbound/vmess.go b/adapter/outbound/vmess.go index ea3681a3c..31d0a2cf8 100644 --- a/adapter/outbound/vmess.go +++ b/adapter/outbound/vmess.go @@ -14,6 +14,7 @@ import ( "github.com/Dreamacro/clash/component/resolver" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/transport/gun" + "github.com/Dreamacro/clash/transport/socks5" "github.com/Dreamacro/clash/transport/vmess" "golang.org/x/net/http2" @@ -327,16 +328,16 @@ func NewVmess(option VmessOption) (*Vmess, error) { func parseVmessAddr(metadata *C.Metadata) *vmess.DstAddr { var addrType byte var addr []byte - switch metadata.AddrType { - case C.AtypIPv4: + switch metadata.AddrType() { + case socks5.AtypIPv4: addrType = byte(vmess.AtypIPv4) addr = make([]byte, net.IPv4len) copy(addr[:], metadata.DstIP.To4()) - case C.AtypIPv6: + case socks5.AtypIPv6: addrType = byte(vmess.AtypIPv6) addr = make([]byte, net.IPv6len) copy(addr[:], metadata.DstIP.To16()) - case C.AtypDomainName: + case socks5.AtypDomainName: addrType = byte(vmess.AtypDomainName) addr = make([]byte, len(metadata.Host)+1) addr[0] = byte(len(metadata.Host)) diff --git a/adapter/outboundgroup/util.go b/adapter/outboundgroup/util.go index 70d710784..1ec0254ac 100644 --- a/adapter/outboundgroup/util.go +++ b/adapter/outboundgroup/util.go @@ -18,27 +18,24 @@ func addrToMetadata(rawAddress string) (addr *C.Metadata, err error) { ip := net.ParseIP(host) if ip == nil { addr = &C.Metadata{ - AddrType: C.AtypDomainName, - Host: host, - DstIP: nil, - DstPort: port, + Host: host, + DstIP: nil, + DstPort: port, } return } else if ip4 := ip.To4(); ip4 != nil { addr = &C.Metadata{ - AddrType: C.AtypIPv4, - Host: "", - DstIP: ip4, - DstPort: port, + Host: "", + DstIP: ip4, + DstPort: port, } return } addr = &C.Metadata{ - AddrType: C.AtypIPv6, - Host: "", - DstIP: ip, - DstPort: port, + Host: "", + DstIP: ip, + DstPort: port, } return } diff --git a/constant/metadata.go b/constant/metadata.go index 6789c913e..b829b7b04 100644 --- a/constant/metadata.go +++ b/constant/metadata.go @@ -4,14 +4,12 @@ import ( "encoding/json" "net" "strconv" + + "github.com/Dreamacro/clash/transport/socks5" ) // Socks addr type const ( - AtypIPv4 = 1 - AtypDomainName = 3 - AtypIPv6 = 4 - TCP NetWork = iota UDP @@ -69,7 +67,6 @@ type Metadata struct { DstIP net.IP `json:"destinationIP"` SrcPort string `json:"sourcePort"` DstPort string `json:"destinationPort"` - AddrType int `json:"-"` Host string `json:"host"` DNSMode DNSMode `json:"dnsMode"` ProcessPath string `json:"processPath"` @@ -83,6 +80,17 @@ func (m *Metadata) SourceAddress() string { return net.JoinHostPort(m.SrcIP.String(), m.SrcPort) } +func (m *Metadata) AddrType() int { + switch true { + case m.Host != "" || m.DstIP == nil: + return socks5.AtypDomainName + case m.DstIP.To4() != nil: + return socks5.AtypIPv4 + default: + return socks5.AtypIPv6 + } +} + func (m *Metadata) Resolved() bool { return m.DstIP != nil } @@ -93,11 +101,6 @@ func (m *Metadata) Pure() *Metadata { if m.DNSMode == DNSMapping && m.DstIP != nil { copy := *m copy.Host = "" - if copy.DstIP.To4() != nil { - copy.AddrType = AtypIPv4 - } else { - copy.AddrType = AtypIPv6 - } return © } diff --git a/rule/domain.go b/rule/domain.go index 2b2a076bc..94978e6d7 100644 --- a/rule/domain.go +++ b/rule/domain.go @@ -16,9 +16,6 @@ func (d *Domain) RuleType() C.RuleType { } func (d *Domain) Match(metadata *C.Metadata) bool { - if metadata.AddrType != C.AtypDomainName { - return false - } return metadata.Host == d.domain } diff --git a/rule/domain_keyword.go b/rule/domain_keyword.go index 046eafe48..9e6611206 100644 --- a/rule/domain_keyword.go +++ b/rule/domain_keyword.go @@ -16,11 +16,7 @@ func (dk *DomainKeyword) RuleType() C.RuleType { } func (dk *DomainKeyword) Match(metadata *C.Metadata) bool { - if metadata.AddrType != C.AtypDomainName { - return false - } - domain := metadata.Host - return strings.Contains(domain, dk.keyword) + return strings.Contains(metadata.Host, dk.keyword) } func (dk *DomainKeyword) Adapter() string { diff --git a/rule/domain_suffix.go b/rule/domain_suffix.go index 092193386..70b8cfe57 100644 --- a/rule/domain_suffix.go +++ b/rule/domain_suffix.go @@ -16,9 +16,6 @@ func (ds *DomainSuffix) RuleType() C.RuleType { } func (ds *DomainSuffix) Match(metadata *C.Metadata) bool { - if metadata.AddrType != C.AtypDomainName { - return false - } domain := metadata.Host return strings.HasSuffix(domain, "."+ds.suffix) || domain == ds.suffix } diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index f9e130168..6a39ce700 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -122,11 +122,6 @@ func preHandleMetadata(metadata *C.Metadata) error { if ip := net.ParseIP(metadata.Host); ip != nil { metadata.DstIP = ip metadata.Host = "" - if ip.To4() != nil { - metadata.AddrType = C.AtypIPv4 - } else { - metadata.AddrType = C.AtypIPv6 - } } // preprocess enhanced-mode metadata @@ -134,7 +129,6 @@ func preHandleMetadata(metadata *C.Metadata) error { host, exist := resolver.FindHostByIP(metadata.DstIP) if exist { metadata.Host = host - metadata.AddrType = C.AtypDomainName metadata.DNSMode = C.DNSMapping if resolver.FakeIPEnabled() { metadata.DstIP = nil