Change: remove AddrType on Metadata (#2199)

This commit is contained in:
gVisor bot 2022-07-05 20:26:43 +08:00
parent 761e9cb69a
commit 5b51b8f727
10 changed files with 40 additions and 65 deletions

View file

@ -184,10 +184,9 @@ func urlToMetadata(rawURL string) (addr C.Metadata, err error) {
} }
addr = C.Metadata{ addr = C.Metadata{
AddrType: C.AtypDomainName, Host: u.Hostname(),
Host: u.Hostname(), DstIP: nil,
DstIP: nil, DstPort: port,
DstPort: port,
} }
return return
} }

View file

@ -11,9 +11,7 @@ import (
) )
func parseSocksAddr(target socks5.Addr) *C.Metadata { func parseSocksAddr(target socks5.Addr) *C.Metadata {
metadata := &C.Metadata{ metadata := &C.Metadata{}
AddrType: int(target[0]),
}
switch target[0] { switch target[0] {
case socks5.AtypDomainName: case socks5.AtypDomainName:
@ -44,21 +42,13 @@ func parseHTTPAddr(request *http.Request) *C.Metadata {
host = strings.TrimRight(host, ".") host = strings.TrimRight(host, ".")
metadata := &C.Metadata{ metadata := &C.Metadata{
NetWork: C.TCP, NetWork: C.TCP,
AddrType: C.AtypDomainName, Host: host,
Host: host, DstIP: nil,
DstIP: nil, DstPort: port,
DstPort: port,
} }
ip := net.ParseIP(host) if ip := net.ParseIP(host); ip != nil {
if ip != nil {
switch {
case ip.To4() == nil:
metadata.AddrType = C.AtypIPv6
default:
metadata.AddrType = C.AtypIPv4
}
metadata.DstIP = ip metadata.DstIP = ip
} }

View file

@ -20,10 +20,11 @@ func tcpKeepAlive(c net.Conn) {
func serializesSocksAddr(metadata *C.Metadata) []byte { func serializesSocksAddr(metadata *C.Metadata) []byte {
var buf [][]byte var buf [][]byte
aType := uint8(metadata.AddrType) addrType := metadata.AddrType()
aType := uint8(addrType)
p, _ := strconv.ParseUint(metadata.DstPort, 10, 16) p, _ := strconv.ParseUint(metadata.DstPort, 10, 16)
port := []byte{uint8(p >> 8), uint8(p & 0xff)} port := []byte{uint8(p >> 8), uint8(p & 0xff)}
switch metadata.AddrType { switch addrType {
case socks5.AtypDomainName: case socks5.AtypDomainName:
len := uint8(len(metadata.Host)) len := uint8(len(metadata.Host))
host := []byte(metadata.Host) host := []byte(metadata.Host)

View file

@ -14,6 +14,7 @@ import (
"github.com/Dreamacro/clash/component/resolver" "github.com/Dreamacro/clash/component/resolver"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/transport/gun" "github.com/Dreamacro/clash/transport/gun"
"github.com/Dreamacro/clash/transport/socks5"
"github.com/Dreamacro/clash/transport/vmess" "github.com/Dreamacro/clash/transport/vmess"
"golang.org/x/net/http2" "golang.org/x/net/http2"
@ -327,16 +328,16 @@ func NewVmess(option VmessOption) (*Vmess, error) {
func parseVmessAddr(metadata *C.Metadata) *vmess.DstAddr { func parseVmessAddr(metadata *C.Metadata) *vmess.DstAddr {
var addrType byte var addrType byte
var addr []byte var addr []byte
switch metadata.AddrType { switch metadata.AddrType() {
case C.AtypIPv4: case socks5.AtypIPv4:
addrType = byte(vmess.AtypIPv4) addrType = byte(vmess.AtypIPv4)
addr = make([]byte, net.IPv4len) addr = make([]byte, net.IPv4len)
copy(addr[:], metadata.DstIP.To4()) copy(addr[:], metadata.DstIP.To4())
case C.AtypIPv6: case socks5.AtypIPv6:
addrType = byte(vmess.AtypIPv6) addrType = byte(vmess.AtypIPv6)
addr = make([]byte, net.IPv6len) addr = make([]byte, net.IPv6len)
copy(addr[:], metadata.DstIP.To16()) copy(addr[:], metadata.DstIP.To16())
case C.AtypDomainName: case socks5.AtypDomainName:
addrType = byte(vmess.AtypDomainName) addrType = byte(vmess.AtypDomainName)
addr = make([]byte, len(metadata.Host)+1) addr = make([]byte, len(metadata.Host)+1)
addr[0] = byte(len(metadata.Host)) addr[0] = byte(len(metadata.Host))

View file

@ -18,27 +18,24 @@ func addrToMetadata(rawAddress string) (addr *C.Metadata, err error) {
ip := net.ParseIP(host) ip := net.ParseIP(host)
if ip == nil { if ip == nil {
addr = &C.Metadata{ addr = &C.Metadata{
AddrType: C.AtypDomainName, Host: host,
Host: host, DstIP: nil,
DstIP: nil, DstPort: port,
DstPort: port,
} }
return return
} else if ip4 := ip.To4(); ip4 != nil { } else if ip4 := ip.To4(); ip4 != nil {
addr = &C.Metadata{ addr = &C.Metadata{
AddrType: C.AtypIPv4, Host: "",
Host: "", DstIP: ip4,
DstIP: ip4, DstPort: port,
DstPort: port,
} }
return return
} }
addr = &C.Metadata{ addr = &C.Metadata{
AddrType: C.AtypIPv6, Host: "",
Host: "", DstIP: ip,
DstIP: ip, DstPort: port,
DstPort: port,
} }
return return
} }

View file

@ -4,14 +4,12 @@ import (
"encoding/json" "encoding/json"
"net" "net"
"strconv" "strconv"
"github.com/Dreamacro/clash/transport/socks5"
) )
// Socks addr type // Socks addr type
const ( const (
AtypIPv4 = 1
AtypDomainName = 3
AtypIPv6 = 4
TCP NetWork = iota TCP NetWork = iota
UDP UDP
@ -69,7 +67,6 @@ type Metadata struct {
DstIP net.IP `json:"destinationIP"` DstIP net.IP `json:"destinationIP"`
SrcPort string `json:"sourcePort"` SrcPort string `json:"sourcePort"`
DstPort string `json:"destinationPort"` DstPort string `json:"destinationPort"`
AddrType int `json:"-"`
Host string `json:"host"` Host string `json:"host"`
DNSMode DNSMode `json:"dnsMode"` DNSMode DNSMode `json:"dnsMode"`
ProcessPath string `json:"processPath"` ProcessPath string `json:"processPath"`
@ -83,6 +80,17 @@ func (m *Metadata) SourceAddress() string {
return net.JoinHostPort(m.SrcIP.String(), m.SrcPort) 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 { func (m *Metadata) Resolved() bool {
return m.DstIP != nil return m.DstIP != nil
} }
@ -93,11 +101,6 @@ func (m *Metadata) Pure() *Metadata {
if m.DNSMode == DNSMapping && m.DstIP != nil { if m.DNSMode == DNSMapping && m.DstIP != nil {
copy := *m copy := *m
copy.Host = "" copy.Host = ""
if copy.DstIP.To4() != nil {
copy.AddrType = AtypIPv4
} else {
copy.AddrType = AtypIPv6
}
return &copy return &copy
} }

View file

@ -16,9 +16,6 @@ func (d *Domain) RuleType() C.RuleType {
} }
func (d *Domain) Match(metadata *C.Metadata) bool { func (d *Domain) Match(metadata *C.Metadata) bool {
if metadata.AddrType != C.AtypDomainName {
return false
}
return metadata.Host == d.domain return metadata.Host == d.domain
} }

View file

@ -16,11 +16,7 @@ func (dk *DomainKeyword) RuleType() C.RuleType {
} }
func (dk *DomainKeyword) Match(metadata *C.Metadata) bool { func (dk *DomainKeyword) Match(metadata *C.Metadata) bool {
if metadata.AddrType != C.AtypDomainName { return strings.Contains(metadata.Host, dk.keyword)
return false
}
domain := metadata.Host
return strings.Contains(domain, dk.keyword)
} }
func (dk *DomainKeyword) Adapter() string { func (dk *DomainKeyword) Adapter() string {

View file

@ -16,9 +16,6 @@ func (ds *DomainSuffix) RuleType() C.RuleType {
} }
func (ds *DomainSuffix) Match(metadata *C.Metadata) bool { func (ds *DomainSuffix) Match(metadata *C.Metadata) bool {
if metadata.AddrType != C.AtypDomainName {
return false
}
domain := metadata.Host domain := metadata.Host
return strings.HasSuffix(domain, "."+ds.suffix) || domain == ds.suffix return strings.HasSuffix(domain, "."+ds.suffix) || domain == ds.suffix
} }

View file

@ -122,11 +122,6 @@ func preHandleMetadata(metadata *C.Metadata) error {
if ip := net.ParseIP(metadata.Host); ip != nil { if ip := net.ParseIP(metadata.Host); ip != nil {
metadata.DstIP = ip metadata.DstIP = ip
metadata.Host = "" metadata.Host = ""
if ip.To4() != nil {
metadata.AddrType = C.AtypIPv4
} else {
metadata.AddrType = C.AtypIPv6
}
} }
// preprocess enhanced-mode metadata // preprocess enhanced-mode metadata
@ -134,7 +129,6 @@ func preHandleMetadata(metadata *C.Metadata) error {
host, exist := resolver.FindHostByIP(metadata.DstIP) host, exist := resolver.FindHostByIP(metadata.DstIP)
if exist { if exist {
metadata.Host = host metadata.Host = host
metadata.AddrType = C.AtypDomainName
metadata.DNSMode = C.DNSMapping metadata.DNSMode = C.DNSMapping
if resolver.FakeIPEnabled() { if resolver.FakeIPEnabled() {
metadata.DstIP = nil metadata.DstIP = nil