From 6dadc2357aed2115dac80bf34d19d15e4f7d7a51 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Fri, 11 Nov 2022 09:19:28 +0800 Subject: [PATCH] chore: remove AddrType on Metadata --- adapter/adapter.go | 7 +++---- adapter/inbound/socket.go | 5 ----- adapter/inbound/util.go | 19 +++++------------ adapter/outbound/util.go | 5 +++-- adapter/outbound/vless.go | 13 ++++++------ adapter/outboundgroup/util.go | 27 +++++++------------------ component/sniffer/dispatcher.go | 1 - constant/metadata.go | 23 ++++++++++++--------- dns/util.go | 14 ++++--------- rules/common/domain.go | 3 --- rules/common/domain_keyword.go | 3 --- rules/common/domain_suffix.go | 3 --- rules/common/geosite.go | 4 ---- rules/logic/logic_test.go | 7 +++---- test/clash_test.go | 36 ++++++++++++++------------------- tunnel/tunnel.go | 6 ------ 16 files changed, 60 insertions(+), 116 deletions(-) diff --git a/adapter/adapter.go b/adapter/adapter.go index 2b3c7e6c..aa860ed4 100644 --- a/adapter/adapter.go +++ b/adapter/adapter.go @@ -198,10 +198,9 @@ func urlToMetadata(rawURL string) (addr C.Metadata, err error) { } addr = C.Metadata{ - AddrType: C.AtypDomainName, - Host: u.Hostname(), - DstIP: netip.Addr{}, - DstPort: port, + Host: u.Hostname(), + DstIP: netip.Addr{}, + DstPort: port, } return } diff --git a/adapter/inbound/socket.go b/adapter/inbound/socket.go index 28608be9..7fe00dbf 100644 --- a/adapter/inbound/socket.go +++ b/adapter/inbound/socket.go @@ -32,17 +32,12 @@ func NewInner(conn net.Conn, dst string, host string) *context.ConnContext { metadata.Type = C.INNER metadata.DNSMode = C.DNSMapping metadata.Host = host - metadata.AddrType = C.AtypDomainName metadata.Process = C.ClashName if h, port, err := net.SplitHostPort(dst); err == nil { metadata.DstPort = port if host == "" { if ip, err := netip.ParseAddr(h); err == nil { metadata.DstIP = ip - metadata.AddrType = C.AtypIPv4 - if ip.Is6() { - metadata.AddrType = C.AtypIPv6 - } } } } diff --git a/adapter/inbound/util.go b/adapter/inbound/util.go index 9a024529..cddbcf1f 100644 --- a/adapter/inbound/util.go +++ b/adapter/inbound/util.go @@ -13,9 +13,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: @@ -45,21 +43,14 @@ func parseHTTPAddr(request *http.Request) *C.Metadata { host = strings.TrimRight(host, ".") metadata := &C.Metadata{ - NetWork: C.TCP, - AddrType: C.AtypDomainName, - Host: host, - DstIP: netip.Addr{}, - DstPort: port, + NetWork: C.TCP, + Host: host, + DstIP: netip.Addr{}, + DstPort: port, } ip, err := netip.ParseAddr(host) if err == nil { - switch { - case ip.Is6(): - metadata.AddrType = C.AtypIPv6 - default: - metadata.AddrType = C.AtypIPv4 - } metadata.DstIP = ip } diff --git a/adapter/outbound/util.go b/adapter/outbound/util.go index d5219131..06c81868 100644 --- a/adapter/outbound/util.go +++ b/adapter/outbound/util.go @@ -44,10 +44,11 @@ func getClientXSessionCache() xtls.ClientSessionCache { 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: lenM := uint8(len(metadata.Host)) host := []byte(metadata.Host) diff --git a/adapter/outbound/vless.go b/adapter/outbound/vless.go index 4811de0f..5eb081c1 100644 --- a/adapter/outbound/vless.go +++ b/adapter/outbound/vless.go @@ -6,18 +6,19 @@ import ( "encoding/binary" "errors" "fmt" - "github.com/Dreamacro/clash/common/convert" - tlsC "github.com/Dreamacro/clash/component/tls" "io" "net" "net/http" "strconv" "sync" + "github.com/Dreamacro/clash/common/convert" "github.com/Dreamacro/clash/component/dialer" "github.com/Dreamacro/clash/component/resolver" + tlsC "github.com/Dreamacro/clash/component/tls" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/transport/gun" + "github.com/Dreamacro/clash/transport/socks5" "github.com/Dreamacro/clash/transport/vless" "github.com/Dreamacro/clash/transport/vmess" ) @@ -280,16 +281,16 @@ func (v *Vless) SupportUOT() bool { func parseVlessAddr(metadata *C.Metadata) *vless.DstAddr { var addrType byte var addr []byte - switch metadata.AddrType { - case C.AtypIPv4: + switch metadata.AddrType() { + case socks5.AtypIPv4: addrType = vless.AtypIPv4 addr = make([]byte, net.IPv4len) copy(addr[:], metadata.DstIP.AsSlice()) - case C.AtypIPv6: + case socks5.AtypIPv6: addrType = vless.AtypIPv6 addr = make([]byte, net.IPv6len) copy(addr[:], metadata.DstIP.AsSlice()) - case C.AtypDomainName: + case socks5.AtypDomainName: addrType = vless.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 e1700d94..3c769b2f 100644 --- a/adapter/outboundgroup/util.go +++ b/adapter/outboundgroup/util.go @@ -16,32 +16,19 @@ func addrToMetadata(rawAddress string) (addr *C.Metadata, err error) { return } - ip, err := netip.ParseAddr(host) - if err != nil { + if ip, err := netip.ParseAddr(host); err != nil { addr = &C.Metadata{ - AddrType: C.AtypDomainName, - Host: host, - DstIP: netip.Addr{}, - DstPort: port, + Host: host, + DstPort: port, } - err = nil - return - } else if ip.Is4() { + } else { addr = &C.Metadata{ - AddrType: C.AtypIPv4, - Host: "", - DstIP: ip, - DstPort: port, + Host: "", + DstIP: ip, + DstPort: port, } - return } - addr = &C.Metadata{ - AddrType: C.AtypIPv6, - Host: "", - DstIP: ip, - DstPort: port, - } return } diff --git a/component/sniffer/dispatcher.go b/component/sniffer/dispatcher.go index 3bd81ac8..ef586d45 100644 --- a/component/sniffer/dispatcher.go +++ b/component/sniffer/dispatcher.go @@ -112,7 +112,6 @@ func (sd *SnifferDispatcher) replaceDomain(metadata *C.Metadata, host string) { metadata.Host, host) } - metadata.AddrType = C.AtypDomainName metadata.Host = host metadata.DNSMode = C.DNSNormal } diff --git a/constant/metadata.go b/constant/metadata.go index f248cd21..2d52f2bf 100644 --- a/constant/metadata.go +++ b/constant/metadata.go @@ -6,14 +6,12 @@ import ( "net" "net/netip" "strconv" + + "github.com/Dreamacro/clash/transport/socks5" ) // Socks addr type const ( - AtypIPv4 = 1 - AtypDomainName = 3 - AtypIPv6 = 4 - TCP NetWork = iota UDP ALLNet @@ -105,7 +103,6 @@ type Metadata struct { DstIP netip.Addr `json:"destinationIP"` SrcPort string `json:"sourcePort"` DstPort string `json:"destinationPort"` - AddrType int `json:"-"` Host string `json:"host"` DNSMode DNSMode `json:"dnsMode"` Uid *int32 `json:"uid"` @@ -138,6 +135,17 @@ func (m *Metadata) SourceDetail() string { } } +func (m *Metadata) AddrType() int { + switch true { + case m.Host != "" || !m.DstIP.IsValid(): + return socks5.AtypDomainName + case m.DstIP.Is4(): + return socks5.AtypIPv4 + default: + return socks5.AtypIPv6 + } +} + func (m *Metadata) Resolved() bool { return m.DstIP.IsValid() } @@ -148,11 +156,6 @@ func (m *Metadata) Pure() *Metadata { if (m.DNSMode == DNSMapping || m.DNSMode == DNSHosts) && m.DstIP.IsValid() { copyM := *m copyM.Host = "" - if copyM.DstIP.Is4() { - copyM.AddrType = AtypIPv4 - } else { - copyM.AddrType = AtypIPv6 - } return ©M } diff --git a/dns/util.go b/dns/util.go index 50d9decd..2958a9ed 100644 --- a/dns/util.go +++ b/dns/util.go @@ -156,17 +156,11 @@ func dialContextExtra(ctx context.Context, adapterName string, network string, d networkType = C.UDP } - addrType := C.AtypIPv4 - if dstIP.Is6() { - addrType = C.AtypIPv6 - } - metadata := &C.Metadata{ - NetWork: networkType, - AddrType: addrType, - Host: "", - DstIP: dstIP, - DstPort: port, + NetWork: networkType, + Host: "", + DstIP: dstIP, + DstPort: port, } adapter, ok := tunnel.Proxies()[adapterName] diff --git a/rules/common/domain.go b/rules/common/domain.go index 2f0e4f2f..1dc6b250 100644 --- a/rules/common/domain.go +++ b/rules/common/domain.go @@ -19,9 +19,6 @@ func (d *Domain) RuleType() C.RuleType { } func (d *Domain) Match(metadata *C.Metadata) (bool, string) { - if metadata.AddrType != C.AtypDomainName { - return false, "" - } return metadata.Host == d.domain, d.adapter } diff --git a/rules/common/domain_keyword.go b/rules/common/domain_keyword.go index 58257544..4fff673e 100644 --- a/rules/common/domain_keyword.go +++ b/rules/common/domain_keyword.go @@ -19,9 +19,6 @@ func (dk *DomainKeyword) RuleType() C.RuleType { } func (dk *DomainKeyword) Match(metadata *C.Metadata) (bool, string) { - if metadata.AddrType != C.AtypDomainName { - return false, "" - } domain := metadata.Host return strings.Contains(domain, dk.keyword), dk.adapter } diff --git a/rules/common/domain_suffix.go b/rules/common/domain_suffix.go index c9016744..1e48a236 100644 --- a/rules/common/domain_suffix.go +++ b/rules/common/domain_suffix.go @@ -19,9 +19,6 @@ func (ds *DomainSuffix) RuleType() C.RuleType { } func (ds *DomainSuffix) Match(metadata *C.Metadata) (bool, string) { - if metadata.AddrType != C.AtypDomainName { - return false, "" - } domain := metadata.Host return strings.HasSuffix(domain, "."+ds.suffix) || domain == ds.suffix, ds.adapter } diff --git a/rules/common/geosite.go b/rules/common/geosite.go index 9897f349..ad3cbae5 100644 --- a/rules/common/geosite.go +++ b/rules/common/geosite.go @@ -24,10 +24,6 @@ func (gs *GEOSITE) RuleType() C.RuleType { } func (gs *GEOSITE) Match(metadata *C.Metadata) (bool, string) { - if metadata.AddrType != C.AtypDomainName { - return false, "" - } - domain := metadata.Host return gs.matcher.ApplyDomain(domain), gs.adapter } diff --git a/rules/logic/logic_test.go b/rules/logic/logic_test.go index 4ec72cc4..0410873e 100644 --- a/rules/logic/logic_test.go +++ b/rules/logic/logic_test.go @@ -73,10 +73,9 @@ func TestAND(t *testing.T) { assert.Equal(t, "DIRECT", and.adapter) assert.Equal(t, false, and.ShouldResolveIP()) m, _ := and.Match(&C.Metadata{ - Host: "baidu.com", - AddrType: C.AtypDomainName, - NetWork: C.TCP, - DstPort: "20000", + Host: "baidu.com", + NetWork: C.TCP, + DstPort: "20000", }) assert.Equal(t, true, m) diff --git a/test/clash_test.go b/test/clash_test.go index 2cc5bd9a..e6112e21 100644 --- a/test/clash_test.go +++ b/test/clash_test.go @@ -555,9 +555,8 @@ func testPacketConnTimeout(t *testing.T, pc net.PacketConn) error { func testSuit(t *testing.T, proxy C.ProxyAdapter) { assert.NoError(t, testPingPongWithConn(t, func() net.Conn { conn, err := proxy.DialContext(context.Background(), &C.Metadata{ - Host: localIP.String(), - DstPort: "10001", - AddrType: socks5.AtypDomainName, + Host: localIP.String(), + DstPort: "10001", }) require.NoError(t, err) return conn @@ -565,9 +564,8 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) { assert.NoError(t, testLargeDataWithConn(t, func() net.Conn { conn, err := proxy.DialContext(context.Background(), &C.Metadata{ - Host: localIP.String(), - DstPort: "10001", - AddrType: socks5.AtypDomainName, + Host: localIP.String(), + DstPort: "10001", }) require.NoError(t, err) return conn @@ -578,10 +576,9 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) { } pc, err := proxy.ListenPacketContext(context.Background(), &C.Metadata{ - NetWork: C.UDP, - DstIP: localIP, - DstPort: "10001", - AddrType: socks5.AtypIPv4, + NetWork: C.UDP, + DstIP: localIP, + DstPort: "10001", }) require.NoError(t, err) defer pc.Close() @@ -589,10 +586,9 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) { assert.NoError(t, testPingPongWithPacketConn(t, pc)) pc, err = proxy.ListenPacketContext(context.Background(), &C.Metadata{ - NetWork: C.UDP, - DstIP: localIP, - DstPort: "10001", - AddrType: socks5.AtypIPv4, + NetWork: C.UDP, + DstIP: localIP, + DstPort: "10001", }) require.NoError(t, err) defer pc.Close() @@ -600,10 +596,9 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) { assert.NoError(t, testLargeDataWithPacketConn(t, pc)) pc, err = proxy.ListenPacketContext(context.Background(), &C.Metadata{ - NetWork: C.UDP, - DstIP: localIP, - DstPort: "10001", - AddrType: socks5.AtypIPv4, + NetWork: C.UDP, + DstIP: localIP, + DstPort: "10001", }) require.NoError(t, err) defer pc.Close() @@ -639,9 +634,8 @@ func benchmarkProxy(b *testing.B, proxy C.ProxyAdapter) { }() conn, err := proxy.DialContext(context.Background(), &C.Metadata{ - Host: localIP.String(), - DstPort: "10001", - AddrType: socks5.AtypDomainName, + Host: localIP.String(), + DstPort: "10001", }) require.NoError(b, err) diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index ae09499c..46b603a3 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -169,11 +169,6 @@ func preHandleMetadata(metadata *C.Metadata) error { if ip, err := netip.ParseAddr(metadata.Host); err == nil { metadata.DstIP = ip metadata.Host = "" - if ip.Is4() { - metadata.AddrType = C.AtypIPv4 - } else { - metadata.AddrType = C.AtypIPv6 - } } // preprocess enhanced-mode metadata @@ -181,7 +176,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 = netip.Addr{}